First playing with libvirt and cloudinit

Exactly one year earlier I write an article (fr) about understanding some shit I’ve done with my system. Here we go again…

This time it’s about trying a new tool for hosting some virtual machines and containers, thanks to my friend droper to help me understanding it. I will say some obvious thing when you know it but yes some times is better to repeat this obvious things.

So, I’ve had some good feedback about libvirt for hosting vm and also want to have a simple way to configure user on it. I first looked at cloudinit and it’s seems to be a good thing. Well… I’ve had some difficulty to understand it and the doc about cloudinit it’s… how to say… not complete enough for my taste. But whatever, I’m here to try it, starting with creating a new virtual machine.

For my tests I mainly use the cloundinit documentation nocloud datasource.

So first thing to do is to grab some virtual image, like the debian’s cloud image of the day.

wget https://cdimage.debian.org/cdimage/cloud/buster/daily/20200512-261/debian-10-generic-amd64-daily-20200512-261.qcow2

Since I just want to test cloudinit I will juste simply make a copy of the image that I just downloaded and use this copied image as my main vm disk.

cp debian-10-generic-amd64-daily-20200512-261.qcow2 disk.qcow2

Hint - Wait, why don’t just use the image without copying it ???

Since I will use the debian cloud image for various tests I don’t want cloudinit « flag it » and starts the installation procedure only once. Yes with my first attempts I didn’t copy it and I dind’t understand why cloudinit didn’t want to apply the configuration, very frustrating.

For the next step I will simply create a new user with a plain text password for some quick tests. For that I create a user-data file based on some examples in cloudinit documentation. Here is what my user-data config file looks like:

#cloud-config

users:
  - name: toot
    plain_text_passwd: 'toot'
    lock_passwd: false

For the meta-data file I will just make an empty one for the next of our journey.

touch meta-data

Now we have all the necessary configuration files we need to generate a new image who will contains our meta-data and user-data files and who will be consumed by cloudinit when booting the virtual machine.

sudo genisoimage  -output seed.iso -volid cidata -joliet -rock user-data meta-data

The final step is to launch the virt-install command for create our new virtual machine.

virt-install --name myvm \
  --import \
  --os-variant debian10 \
  --memory 4096 \
  --vcpu 2 \
  --network user \
  --graphics none \
  --disk disk.qcow2 \
  --disk seed.iso,readonly=on \
  --qemu-commandline='-smbios type=1,serial=ds=nocloud;' \
  --noreboot \
  --autostart \
  --noautoconsole

Indication

Keep attention to the command line option qemu-commandline, I noticed that it’s mandatory for Debian but not for Ubuntu. It’s a detail but It took me some time to notice this… I don’t know for other distribution yet but I think it’s usefull to keep that in mind and don’t forget to use it each time you want to create an virtual machine based on cloudinit.

Now we can starting it and then try to connect to it in console mode.

virsh start --console myvm

For the network part, I will try it later since my network configuration on my Archlinux desktop is actually a mess (I’m not going to talk about the mess on my Archlinux laptop either).

Yes, I could make a fresh install of both of my systems but this is the easy way. I prefer to waste more time and have more fun to repair my mess. (I don’t do this for work and servers obviously, all is documented but maybe it’s better to have a home lab and trying things on it…)