Skip to content

Instantly share code, notes, and snippets.

Last active August 9, 2023 15:54
  • Star 58 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
example of using Ubuntu cloud images with virtualbox
## Install necessary packages
$ sudo apt-get install virtualbox-ose qemu-utils genisoimage cloud-utils
## get kvm unloaded so virtualbox can load
$ sudo modprobe -r kvm_amd kvm_intel
$ sudo service virtualbox stop
$ sudo service virtualbox start
## URL to most recent cloud image of 12.04
$ img_url=""
$ img_url="${img_url}/ubuntu-12.04-server-cloudimg-amd64-disk1.img"
## on precise, cloud-localds is not in archive. just download.
$ localds_url=""
$ which cloud-localds ||
{ sudo wget "$localds_url" -O /usr/local/bin/cloud-localds &&
sudo chmod 755 /usr/local/bin/cloud-localds; }
## download a cloud image to run, and convert it to virtualbox 'vdi' format
$ img_dist="${img_url##*/}"
$ img_raw="${img_dist%.img}.raw"
$ my_disk1="my-disk1.vdi"
$ wget $img_url -O "$img_dist"
$ qemu-img convert -O raw "${img_dist}" "${img_raw}"
$ vboxmanage convertfromraw "$img_raw" "$my_disk1"
## create user-data file and a iso file with that user-data on it.
$ seed_iso="my-seed.iso"
$ cat > my-user-data <<EOF
password: passw0rd
chpasswd: { expire: False }
ssh_pwauth: True
$ cloud-localds "$seed_iso" my-user-data
## create a virtual machine using vboxmanage
$ vmname="precise-nocloud-1"
$ vboxmanage createvm --name "$vmname" --register
$ vboxmanage modifyvm "$vmname" \
--memory 512 --boot1 disk --acpi on \
--nic1 nat --natpf1 "guestssh,tcp,,2222,,22"
## Another option for networking would be:
## --nic1 bridged --bridgeadapter1 eth0
$ vboxmanage storagectl "$vmname" --name "IDE_0" --add ide
$ vboxmanage storageattach "$vmname" \
--storagectl "IDE_0" --port 0 --device 0 \
--type hdd --medium "$my_disk1"
$ vboxmanage storageattach "$vmname" \
--storagectl "IDE_0" --port 1 --device 0 \
--type dvddrive --medium "$seed_iso"
$ vboxmanage modifyvm "$vmname" \
--uart1 0x3F8 4 --uartmode1 server my.ttyS0
## start up the VM
$ vboxheadless --vnc --startvm "$vmname"
## You should be able to connect to the vnc port that vboxheadless
## showed was used. The default would be '5900', so 'xvcviewer :5900'
## to connect.
## Also, after the system boots, you can ssh in with 'ubuntu:passw0rd'
## via 'ssh -p 2222 ubuntu@localhost'
## To see the serial console, where kernel output goes, you
## can use 'socat', like this:
## socat UNIX:my.socket -
## vboxmanage controlvm "$vmname" poweroff
$ vboxmanage controlvm "$vmname" poweroff
## clean up after ourselves
$ vboxmanage storageattach "$vmname" \
--storagectl "IDE_0" --port 0 --device 0 --medium none
$ vboxmanage storageattach "$vmname" \
--storagectl "IDE_0" --port 1 --device 0 --medium none
$ vboxmanage closemedium dvd "${seed_iso}"
$ vboxmanage closemedium disk "${my_disk1}"
$ vboxmanage unregistervm $vmname --delete
Copy link

Awesome contribution @azizasm your iso works perfect for a quickstart.

For future references I'd like to add that Ubuntu Cloud images fail to boot when a serial port is not available, if you want them to boot in VirtualBox just make sure to enable the serial port COM1

Copy link

Looks like cloud-localds is available in the cloud-image-utils package.

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