Skip to content

Instantly share code, notes, and snippets.

Embed
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="http://cloud-images.ubuntu.com/server/releases/12.04/release"
$ img_url="${img_url}/ubuntu-12.04-server-cloudimg-amd64-disk1.img"
## on precise, cloud-localds is not in archive. just download.
$ localds_url="http://bazaar.launchpad.net/~cloud-utils-dev/cloud-utils/trunk/download/head:/cloudlocalds-20120823015036-zkgo0cswqhhvener-1/cloud-localds"
$ 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
#cloud-config
password: passw0rd
chpasswd: { expire: False }
ssh_pwauth: True
EOF
$ 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
@faddat

This comment has been minimized.

Copy link

commented Apr 4, 2016

Thank you.

@ricardo-elevenpaths

This comment has been minimized.

Copy link

commented Dec 9, 2018

Thank you, it helped a lot! for windows users:

#!/bin/bash

# IN WINDOWS JUST INSTALL VIRTUAL BOX DOWNLOADING IT WITH IDE

# RUN BASH THROUGH POWERSHELL
apt-get update

img_url="http://cloud-images.ubuntu.com/server/releases/18.10/release"
img_url="${img_url}/ubuntu-18.10-server-cloudimg-amd64.img"

## 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}"
echo "vboxmanage.exe convertfromraw \"$img_raw\" \"$my_disk1\""

# RUN THIS COMMAND IN CMD
# "C:\Program Files\Oracle\VirtualBox\vboxmanage.exe" convertfromraw "ubuntu-18.10-server-cloudimg-amd64.raw" "my-disk1.vdi"

# GO BACK TO BASH
## create user-data file and a iso file with that user-data on it.
seed_iso="my-seed.iso"
cat > my-user-data <<EOF
#cloud-config
password: passw0rd
chpasswd: { expire: False }
ssh_pwauth: True
EOF
./cloud-localds "$seed_iso" my-user-data
# IF THIS ERROR HAPPENED: missing 'genisoimage'.  Required for --filesystem=iso9660.
# apt-get install genisoimage

vmname="precise-nocloud-1"
"C:\Program Files\Oracle\VirtualBox\vboxmanage.exe" createvm --name "precise-nocloud-1" --register
"C:\Program Files\Oracle\VirtualBox\vboxmanage.exe" modifyvm "precise-nocloud-1" --memory 512 --boot1 disk --acpi on --nic1 bridged --bridgeadapter1 wifi0
# IT DOESN'T REALLY MATTER, ONCE YOU START THE VM, AN ERROR COMES UP, SO YOU JUST CHOOSE THE CORRECT ONE IN THAT MOMENT.
##   --nic1 nat --natpf1 "guestssh,tcp,,2222,,22"
## Another option for networking would be:
##   --nic1 bridged --bridgeadapter1 eth0
"C:\Program Files\Oracle\VirtualBox\vboxmanage.exe" storagectl "precise-nocloud-1" --name "IDE_0"  --add ide
"C:\Program Files\Oracle\VirtualBox\vboxmanage.exe" storageattach "precise-nocloud-1" --storagectl "IDE_0" --port 0 --device 0 --type hdd --medium "my-disk1.vdi"
"C:\Program Files\Oracle\VirtualBox\vboxmanage.exe" storageattach "precise-nocloud-1" --storagectl "IDE_0" --port 1 --device 0 --type dvddrive --medium "my-seed.iso"
"C:\Program Files\Oracle\VirtualBox\vboxmanage.exe" modifyvm "precise-nocloud-1" --uart1 0x3F8 4 --uartmode1 server \\.\pipe\winxp

## start up the VM
# YOU CAN START IT FROM THE VBOX IDE OR:
#"C:\Program Files\Oracle\VirtualBox\vboxheadless.exe" --vnc --startvm "precise-nocloud-1"

# REST OF THINGS CAN BE DONE FROM THE IDE

## 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
@artm

This comment has been minimized.

Copy link

commented Jan 30, 2019

Thanks both @smoser and @ricardo-elevenpaths, this was a good starting point!

Some things I learned along the way:

  1. The cloud image in .vmdk format can be used in VirtualBox without conversion. Saves some time and hassle.
  2. I find it useful to redirect the serial port into a file instead of a pipe, this way I always have a boot log on the host (--uart1 0x3F8 4 --uartmode1 file "${VM_BOOT_LOG}" where VM_BOOT_LOG contains the path to the desired location of the boot log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.