How to create a VirtualBox VM from Ubuntu Cloud Image

Publikováno

v

,

Sometimes I need to test something without the risk of breaking my regular Ubuntu install. Usually a regular unprivileged LXC container is enough, but sometimes I need more privileges or nested virtualization support.

An easy way to spin up a quick VM is from the published Ubuntu Cloud Images. Below are steps required to import them into VirtualBox and configure network and SSH access.

1. Download the qcow2 image

Download the QCow2 UEFI/GPT Bootable disk image (.img file) for your chosen Ubuntu release. Convert it into vdi (native VirtualBox format) using the following command (from qemu-utils):

qemu-img convert -O vdi noble-server-cloudimg-amd64.img noble-server-cloudimg-amd64.vdi

VirtualBox doesn’t have support resizing vmdk disk images.

2. Create seed image for cloud-init

The image contains the cloud-init tool, which will read a config file on first boot and configure the system accordingly. The config is a yaml file, which is converted to an ISO image using cloud-localds.

My config creates a single ubuntu user with sudo access and imports my SSH key, so I can access the VM without password. All wired network interfaces will request IP from the DHCP server.

cloud-localds my-seed.iso my-user-data.yml

3. Create the VM

The VM can now be created as normal with the cloud image attached as HDD and my-seed.iso as CD. It may be useful to create a snapshot before the first boot, because it makes tweaking the yaml easier. The VM should boot up and I should be able to log in via SSH.