Skip to content

Instantly share code, notes, and snippets.

@twobitfool
Last active May 28, 2023 01:51
Show Gist options
  • Save twobitfool/4694073 to your computer and use it in GitHub Desktop.
Save twobitfool/4694073 to your computer and use it in GitHub Desktop.
Create a SmartOS VM in VirtualBox
#!/bin/sh
#
# Creates a SmartOS VM in VirtualBox (assuming you alread have VirtualBox installed)
#
# This script will:
# * Download the latest live ISO image of SmartOS
# * Create a VirtualBox VM, or update an existing VM with the latest ISO
# * Configure the VM with a zones disk, and boot it!
#
#
# To create a node.js vm see: http://GIST
#
#
# Original script from Jonathan Perkin:
# http://www.perkin.org.uk/posts/automated-virtualbox-smartos-installs.html
#
#
# Changes (to original script):
# * Use md5 or md5sum command (whichever exists)
# * Use bridged networking (instead of NAT) w/prompt for network device
# * Promiscuous mode (Allow-all) for direct access to/from SmartOS VMs
#
disksize="32" # GB (dynamically sized disk)
memsize="1024" # MB
vmname="smartos"
dlsite="https://download.joyent.com/pub/iso"
#
# Prompt for the network device that the VM should use
#
network_adapters="`VBoxManage list bridgedifs | egrep '^Name:' | sed "s/Name:[ \t]*//"`"
echo
echo "Which network adapter should the VM be attached to..."
echo "`echo "${network_adapters}" | awk '{printf " %d: %s\n", NR, $0}' `"
echo
printf "Pick an adapter [1]: "
read -s val
# Default to the first adapter
if [ -z "${val}" ]; then
val="1"
fi
adapter="`echo "${network_adapters}" | head -n ${val} | tail -n 1 `"
echo
echo "Using ${adapter}..."
echo
vboxdir=$(VBoxManage list systemproperties \
| awk '/^Default.machine.folder/ { print $4 }')
#
# Download MD5 file and parse it for the latest ISO image and checksum
#
curl -o smartos-sums.txt ${dlsite}/md5sums.txt 2>/dev/null
latest_md5=$(awk '/latest.iso/ { print $1 }' smartos-sums.txt)
smartos_version=$(sed -ne "/^${latest_md5}/s/.*-\(.*\).iso/\1/p" \
smartos-sums.txt)
if [ -z "${smartos_version}" ]; then
echo "ERROR: Couldn't determine latest version"
exit 1
fi
#
# Check for md5 tools
#
if [ -n "`which md5`" ]; then
md5command="md5 -q"
fi
if [ -n "`which md5sum`" ]; then
md5command="md5sum"
fi
if [ -z "${md5command}" ]; then
echo "md5 (and md5sum) not found"
exit 1
fi
#
# Download the latest ISO image and verify
#
mkdir -p "${vboxdir}/${vmname}"
if [ ! -f "${vboxdir}/${vmname}/smartos-${smartos_version}.iso" ]; then
echo "Downloading ${dlsite}/smartos-${smartos_version}.iso"
curl -o "${vboxdir}/${vmname}/smartos-${smartos_version}.iso" \
${dlsite}/smartos-${smartos_version}.iso
dl_md5=$(${md5command} "${vboxdir}/${vmname}/smartos-${smartos_version}.iso" \
| awk '{ print $1 }')
if [ -z "${dl_md5}" ]; then
echo "ERROR: Couldn't fetch ISO image"
exit 1
fi
if [ "${latest_md5}" != "${dl_md5}" ]; then
echo "ERROR: md5 checksums do not match"
exit 1
fi
fi
#
# Create VirtualBox VM
#
echo "Creating/Updating Virtual Machine"
VBoxManage showvminfo "${vmname}" >/dev/null 2>&1
if [ $? -eq 0 ]; then
# VM already exists, just update the ISO image
VBoxManage storageattach "${vmname}" --storagectl "IDE Controller" \
--port 1 --device 0 --type dvddrive \
--medium "${vboxdir}/${vmname}/smartos-${smartos_version}.iso"
else
# Create the VM
VBoxManage createvm --name "${vmname}" --ostype OpenSolaris_64 --register
VBoxManage storagectl "${vmname}" --name "IDE Controller" --add ide
# Attach the ISO image
VBoxManage storageattach "${vmname}" --storagectl "IDE Controller" \
--port 1 --device 0 --type dvddrive \
--medium "${vboxdir}/${vmname}/smartos-${smartos_version}.iso"
# Create and attach the zone disk
VBoxManage createhd --filename "${vboxdir}/${vmname}/smartos-zones.vdi" \
--size $(echo "${disksize}*1024" | bc)
VBoxManage storageattach "${vmname}" --storagectl "IDE Controller" \
--port 0 --device 0 --type hdd \
--medium "${vboxdir}/${vmname}/smartos-zones.vdi"
# Set misc settings
VBoxManage modifyvm "${vmname}" --boot1 dvd --boot2 disk --boot3 none
VBoxManage modifyvm "${vmname}" --memory ${memsize}
# Bridged (promiscuous) mode is needed for SmartOS VMs to get network access
VBoxManage modifyvm "${vmname}" --nic1 bridged --bridgeadapter1 "${adapter}"
VBoxManage modifyvm "${vmname}" --nicpromisc1 allow-all
fi
#
# Start it up
#
VirtualBox --startvm "${vmname}" &
# Instructions
#
echo ""
echo "After the VM starts, you will need to:"
echo " * Configure the network (likely just “dhcp”)"
echo " * Add c0d0 as the /zones disk"
echo " * Set a root password"
echo " * Say 'y' to the final 'are you sure?' prompt"
echo ""
echo ""
echo "Then after the configuration (and reboot) of SmartOS..."
echo " * log in as the root (using the root password you picked)"
echo " * ipadm show-addr # <- Use this IP to SSH from your host machine"
echo " * ssh root@IP_FROM_ABOVE # from your host machine"
echo ""
echo ""
echo "Finally, you need to install a VM inside of SmartOS. For Example:"
echo " * imgadm update"
echo " * imgadm avail # find the UUID of the image you want to install"
echo " * imgadm import UUID_OF_IMAGE # e.g. 1fc068b0-13b0-11e2-9f4e-2f3f6a96d9bc"
echo " * vmadm create -f VM_JSON_FILE"
echo " * zlogin VM_UUID"
echo ""
echo "WARNING: On some systems (e.g. Mac OSX using wifi), the SmartOS VMs will be unable"
echo " to reach any machine outside of the host machine. Switching the network "
echo " adapter (in the VirtualBox VM settings) to NAT, and rebooting the VM, "
echo " will make it possible to reach the outside world, but it will then lose "
echo " the connection to the host machine. VMWare Fusion does not have this issue."
echo ""
# # To prevent error/warning messages from VM
# svcadm disable dns/multicast:default
# svcadm disable network/smtp:sendmail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment