Skip to content

Creating KVM Templates using native KVM procedure

Linux

Prepare a virtual server with a limited amount of disk space. For example to create a template of a minimal install of CentOS 2GB of disk is enough.

At the partitioning stage create the main / (slash) partition as a primary ext3 or ext4 as large as you can only leaving a few MB on the disk.

Use the existing space as swap creating it as a primary volume.

You should now have a / partition (sda1) and a swap partition (sda2).

Follow the rest of of the install/setup process making sure you set the networking as DHCP until it is complete.

The next step is to make sure the source virtual server is shutdown and ready for packaging. You will also need to know the KVMID and logical volume group name.

On the host node you will need to mount the virtual servers partitions:

kpartx -a /dev/VOLUME_GROUP/KVMID_img

mount /dev/mapper/VOLUME_GROUP-KVMID_img1 /mnt

This has now mounted the / partition on the host to /mnt.

/etc/fstab inside the virtual server needs to be modified to reflect the correct partitions:

vi /mnt/etc/fstab

It needs to look like this:

/dev/vda1 / ext3 defaults 1 1

/dev/vda2 swap swap defaults 0 0

none /dev/shm tmpfs defaults 0 0

Next lets remove any bash history:

rm /mnt/root/.bash_history

Some distros need the following to get networking working:

rm /mnt/etc/udev/rules.d/70-persistent-net.rules

touch /mnt/etc/udev/rules.d/70-persistent-net.rules

chattr +i /mnt/etc/udev/rules.d/70-persistent-net.rules

Unmount the virtual server:

cd \~

umount /mnt

The next step is worth doing as libguestfs will then compress the swap partition. If you don't do this it will make your template a lot larger:

mkfs.ext3 /dev/mapper/VOLUME_GROUP-KVMID_img2

Remove the mappings:

kpartx -d /dev/VOLUME_GROUP/KVMID_img

Sysprep the virtual server:

virt-sysprep -a /dev/VOLUME_GROUP/KVMID_img

The last thing to do is package the template:

virt-sparsify -v /dev/VOLUME_GROUP/KVMID_img -o compat=0.10 --convert qcow2 --compress /home/solusvm/kvm/template/linux-centos-6.6-x86_64-min-gen2-v1.gz

Back to top