The two tools I use for creating new vm’s with libvirt are virt-install and vmbuilder. I use virt-install when I will be installing an OS from installable media or using an existing disk image, and vmbuilder when I need to create JeOS based vm’s.
After following the guides at wiki.ubuntu.com I ended up with two fairly long template commands:
sudo virt-install --hvm -n <hostname> -r <memory> -f <hdd_image> -s <image_size_in_gb> -c <iso> --accelerate --connect=qemu:///system --vnc --noautoconsole
sudo vmbuilder kvm ubuntu --suite intrepid --flavour virtual --arch amd64 -o --libvirt qemu:///system --part vmbuilder.partition --user myname --name 'My Name' --pass default --addpkg puppet --addpkg unattended-upgrades --addpkg acpid --mirror http://apt/ubuntu --tmpfs - --firstboot vmbuilder.firstboot.sh --hostname <hostname> --mem <memory> --dest <hdd_image_basedir>
The command for virt-install is pretty lean compared to the command for vmbuilder. But vmbuilder allows for most of the configuration to be stored in ~/.vmbuilder.cfg. It did take a bit of digging as not a lot about it was on the ubuntu wiki though.
If you are looking for extra help in vmbuilder have a look at:
vmbuilder kvm ubuntu --help
And after setting up the config files below, to create new machines I just use for example:
sudo vmbuilder kvm ubuntu --hostname cacti --mem 256 --dest /srv/libvirt/cacti/
So here is the config I use as an example for anybody who plans on using vmbuilder a lot.
~/.vmbuilder.cfg
[DEFAULT] arch = amd64 part = vmbuilder.partition user = myname name = My Name pass = default tmpfs = - firstboot = vmbuilder.firstboot.sh firstlogin = vmbuilder.firstlogin.sh [ubuntu] mirror = http://apt/ubuntu suite = intrepid flavour = virtual addpkg = puppet, unattended-upgrades, acpid part = vmbuilder.partition #ssh_user_key = .ssh/authorized_keys [kvm] libvirt = qemu:///system
http://apt/ubuntu is my local approx machine for caching packages. The ssh_user_key is commented out at the moment as it creates the file correctly in the user account, but sets the owner to root.
vmbuilder.partition
root 8000 swap 4000
vmbuilder.firstboot.sh
# This script will run the first time the virtual machine boots # It is ran as root. locale-gen en_AU.UTF-8 apt-get update apt-get install -qqy --force-yes openssh-server
The firstboot script just ensures that if I create an image and duplicate it a few times that each machine will have a unique ssh host key. But the image must be duplicated before the first boot.
vmbuilder.firstlogin.sh
# This script will run the first time the virtual machine boots
# It is ran as root.
# Prompt for a password change
passwd
Because I did not want to put my password in the configuration files as plain text, I only put default as the password and when I login for the first time I am prompted to change it. This will hopefully be fixed when I get the ssh_user_key working though.
VMBuilder/plugins/libvirt/templates/libvirtxml.tmpl
<domain type='kvm'> <name>$hostname</name> <memory>#echo $mem * 1024 #</memory> <vcpu>1</vcpu> <os> <type>hvm</type> <boot dev='hd'/> </os> <features> <acpi/> </features> <clock offset='utc'/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>destroy</on_crash> <devices> <emulator>/usr/bin/kvm</emulator> <interface type='bridge'> <source bridge='br0'/> <model type='virtio'/> </interface> <input type='mouse' bus='ps2'/> <graphics type='vnc' port='-1' listen='127.0.0.1'/> #for $disk in $disks <disk type='file' device='disk'> <source file='$disk.filename' /> <target dev='hd$disk.devletters()' /> </disk> #end for </devices> </domain>
And the only difference with this template and the default one is this template file sets up bridge networking with virtio by default.
One Comment
That’s some pretty terrific stuff – much appreciated!
One Trackback
[…] The two tools I use for creating new vm’s with libvirt are virt-install and vmbuilder. I use virt-install when I will be installing an OS from installable media or using an existing disk image, and vmbuilder when I need to create JeOS based vm’s. More here […]