The other day I needed a small OS to install on KVM just to test that a VM got the right DNS name. I noticed there was a small Linux Distribution called Cirros available in virt-builder.
There were a few small issues starting the image though.
- During bootup it tries to load metadata for the VM from the cloud provider it is running on. This makes the bootup quite slow.
- The default settings for the DHCP client
udhcpcare slow. - Part of the boot output got redirected to ttyS0.
- The
--hostnamesetting ofvirt-builderdid not work. - No DNS entry with the hostname is requested.
- I also intermittently ran into an issue that it ignored DHCP leases it was offered. Link
Due to the DHCP issue I tried both Cirros 0.3.1 and 0.3.5. 0.3.5 seemed a little bit more prone to the DHCP issue.
Pretty much all the issues can be fixed by using virt-builder's --edit flag.
- The cloud metadata loading can be disabled editing the
/etc/cirros-init/configfile. And settingDATASOURCE_LISTtonocloud. - The udhcpc settings can be configured in
/etc/network/interfacesby configuringudhcpc_opts. This does not work anymore in 0.3.5. - Removing
console=ttyS0from the command line in/boot/grub/mendu.lstfixes the output going nowhere. - The hostname is easily fixed by editing
/etc/hostname - The DNS entry can be requested via
udhcpc_optsas well. - No real solution, it seems a 2nd offer is often accepted. The link above lists some other work arounds.
virt-builder is quite convenient if you want apply some small tweaks to a VM. It's possible to edit files, install packages, copy files into the VM, run commands at bootup, install SSH keys etc. It's worth checking out the man page.
The final incantation to create a cirros-0.3.1 VM on KVM then becomes:
> virt-builder cirros-0.3.1 \
--format qcow2 \
--output /var/lib/libvirt/images/cirros31.qcow2 \
--size 50M \
--memsize=512 \
--network \
--edit '/etc/hostname:s/.+/cirros31/' \
--edit '/etc/cirros-init/config:s/DATASOURCE_LIST=.+/DATASOURCE_LIST="nocloud"/' \
--edit '/etc/network/interfaces:s/dhcp/dhcp
udhcpc_opts -t 40 -T 3 -x hostname:cirros31/' \
--edit '/boot/grub/menu.lst:s/console=ttyS0//'
> virt-install --name=cirros31 \
--ram=512 \
--vcpus=1 \
--disk path=/var/lib/libvirt/images/cirros31.qcow2,format=qcow2,bus=virtio \
--import \
--network network:default \
--noautoconsoleFor Cirros 0.3.5 there are some differences:
- Specifying
udhcpc_optsin/etc/network/interfacesdoesn't work anymore, so no tweaking of the amount and timeouts for the DHCP requests. I did not find another way to get around the timeouts. - It requests the DNS name by default, so that's good.
For simple testing I'd prefer 0.3.1 at the moment. But still you can create a 0.3.5 VM like this:
> virt-builder cirros-0.3.5 \
--format qcow2 \
--output /var/lib/libvirt/images/cirros35.qcow2 \
--size 50M \
--memsize 512 \
--network \
--edit '/etc/hostname:s/.+/cirros35/' \
--edit '/etc/cirros-init/config:s/DATASOURCE_LIST=.+/DATASOURCE_LIST="nocloud"/' \
--edit '/boot/grub/menu.lst:s/console=ttyS0//'
> virt-install --name=cirros35 \
--ram=512 \
--vcpus=1 \
--disk path=/var/lib/libvirt/images/cirros35.qcow2,format=qcow2,bus=virtio \
--import \
--network network:default \
--noautoconsoleEnjoy!