Skip to content

Instantly share code, notes, and snippets.

@chrisdickinson
Created November 25, 2014 01:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdickinson/8e42f31f5fff28cdbfe4 to your computer and use it in GitHub Desktop.
Save chrisdickinson/8e42f31f5fff28cdbfe4 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Configurables:
#
# - Disk size is in GB
# - Memory size is in MB
# - SSH port is the local forwarded port to the VM:22
#
disksize="32"
memsize="1024"
sshport="8322"
vmname="SmartOS"
dlsite="https://us-east.manta.joyent.com//Joyent_Dev/public/SmartOS/20141113T164921Z/"
vboxdir=$(VBoxManage list systemproperties \
| awk '/^Default.machine.folder/ { print $4 }')
isopath="$1"
if [ -z "${isopath}" ]; then
#
# Find a suitable md5sum program.
#
if type md5 >/dev/null 2>&1; then
md5sum='md5'
column='NF'
elif type digest >/dev/null 2>&1 &&
digest md5 /dev/null >/dev/null 2>&1; then
md5sum='digest md5'
column='NF'
elif type digest >/dev/null 2>&1 &&
digest -a md5 /dev/null >/dev/null 2>&1; then
md5sum='digest -a md5'
column='1'
elif type md5sum >/dev/null 2>&1; then
md5sum='md5sum'
column='1'
elif type openssl >/dev/null 2>&1 &&
openssl md5 -hex /dev/null >/dev/null 2>&1; then
md5sum='openssl md5 -hex'
column='NF'
else
echo "ERROR: Sorry, could not find an md5 program" 1>&2
exit 1
fi
#
# Download MD5 file and parse it for the latest ISO image and checksum
#
if [ -e smartos-sums.txt ]; then
curl -o smartos-sums.txt ${dlsite}/md5sums.txt 2>/dev/null
fi
latest_md5=$(awk '/.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
#
# Download the latest ISO image and verify
#
mkdir -p "${vboxdir}/${vmname}"
echo "${vboxdir}/${vmname} <- ${dlsite}/smartos-${smartos_version}.iso"
isopath="${vboxdir}/${vmname}/smartos-${smartos_version}.iso"
if [ ! -f "${isopath}" ]; then
echo "Downloading ${dlsite}/smartos-${smartos_version}.iso"
curl -o "${isopath}" \
${dlsite}/smartos-${smartos_version}.iso
else
echo "Skipping download."
fi
${md5sum} "${isopath}" | awk '{ print $'${column}' }'
dl_md5=$(${md5sum} "${isopath}" \
| awk '{ print $'${column}' }')
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 -- remove ${isopath} and re-run"
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 "${isopath}"
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 "${isopath}"
# 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}
VBoxManage modifyvm "${vmname}" --natpf1 "SSH,tcp,,${sshport},,22"
fi
#
# Start it up
#
echo "Starting Virtual Machine"
VirtualBox --startvm "${vmname}" &
## Set up SmartOS in VirtualBox
# http://www.perkin.org.uk/posts/automated-virtualbox-smartos-installs.html
# set up global zone with Joyent datasets
if [[ ! -e /var/db/imgadm/sources.list || `grep -v "https://datasets.joyent.com/datasets" /var/db/imgadm/sources.list` ]]; then
echo "https://datasets.joyent.com/datasets" >> /var/db/imgadm/sources.list
fi
imgadm update
# download sdc:sdc:base64:1.7.1 dataset
imgadm import d34c301e-10c3-11e4-9b79-5f67ca448df0
# find gateway based on global zone gateway
export GATEWAY=$(netstat -r | grep default | awk '{ print $2 }')
# create a 'manage' zone
if [[ ! `vmadm list -H alias=manage` ]]; then
echo "{
\"brand\": \"joyent\",
\"dataset_uuid\": \"d34c301e-10c3-11e4-9b79-5f67ca448df0\",
\"quota\": 1,
\"max_physical_memory\": 64,
\"alias\": \"manage\",
\"resolvers\": [\"10.1.1.1\", \"8.8.8.8\"],
\"nics\": [
{
\"nic_tag\": \"admin\",
\"ip\": \"dhcp\",
\"netmask\": \"dhcp\",
\"gateway\": \"${GATEWAY}\"
}
]
}" | vmadm create
fi
echo "{
\"brand\": \"joyent\",
\"dataset_uuid\": \"d34c301e-10c3-11e4-9b79-5f67ca448df0\",
\"quota\": 10,
\"alias\": \"main\",
\"max_physical_memory\": 1024,
\"resolvers\": [\"10.1.1.1\", \"8.8.8.8\"],
\"nics\": [
{
\"nic_tag\": \"admin\",
\"ip\": \"dhcp\",
\"netmask\": \"dhcp\",
\"gateway\": \"${GATEWAY}\"
}
]
}" | vmadm create
@Raynos
Copy link

Raynos commented Nov 25, 2014

5:07 PM Raynos: https://gist.github.com/chrisdickinson/8e42f31f5fff28cdbfe4 might help -- the in-vm stuff is a bit rough
5:09 PM from that point, you can scp stuff to the server (if you make an alias like "smartos") with scp file smartos:/zones/<zone-uuid>/root/ to put the file into the root of the zone's filesystem
5:11 PM if you're looking for the ability to debug linux cores on that smartos vm, then: make sure to copy the node binary that generated the core as well as the core itself to the smartos vm,
5:11 PM download http://us-east.manta.joyent.com/NodeCore/public/libproc-update.so from the vm,
5:12 PM s/from the/to the/g
5:13 PM then you should be able to do (from inside the zone aliased as "main" in the above) LD_PRELOAD_64=./libproc64-update.so mdb path/to/node-binary-that-generated-the-core path/to/core

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment