Creating a KVM virtual machine using debootstrap

 

How to create a Debian virtual machine outside a virtualization environment.

The challenge here proposed is to create a virtual machine inside a remote server and boot it up using KVM. Sure I could have used VNC and go through all the setup steps of the Debian install disk running inside the virtual machine, but that would have taken ages and would be virtually impossible to automate.

Therefore the next steps detail how to install Debian using debootstrap on a qcow2 image disk and how to successfully boot the debian install using kvm (I’m assuming here your kvm environment is already properly setup).

First thing is to install debootstrap

apt-get install debootstrap

Next step, create an image file in which we will install Debian (example uses a 2Gb virtual disk) using qemu-tools (or kvm-tools depending on your distribution naming scheme)

kvm-img create -f qcow2 debian.qcow2 2G

Next lets load the nbd kernel module (you read it right, the Network Block Device), and associate the previous image to one of the nbd devices.

modprobe nbd max_part=16
kvm-nbd -c /dev/nbd0 debian.qcow2

You should now be able to partition your disk at will. In this example I’ll use an initial swap partition of 512MB plus the root partition. To help all you copy&paste “guru’s” here’s how to do it using sfdisk:

sfdisk /dev/nbd0 -D -uM << EOF
,512,82
;
EOF

Next we need to format the newly created partitions and mount them.

mkswap /dev/nbd0p1
mkfs.ext3 /dev/nbd0p2
mount /dev/nbd0p2 /mnt/

Next stop, install the Debian system using debootstrap, feel free to include extra packages and to use a different mirror.

debootstrap --include=less,locales-all,vim,sudo,openssh-server stable /mnt  http://ftp.us.debian.org/debian

Now go take a coffee or something… By the time you get back your Debian system should be almost installed.

Next step, configure your system to boot. We will need to chroot into the new system, but we will also require all the special filesystems in the chroot environment, therefore lets mount everything:

mount --bind /dev/ /mnt/dev
LANG=C chroot /mnt/ /bin/bash
mount -t proc none /proc
mount -t sysfs none /sys

Next lets install a kernel and grub (apt will try to guess how to install grub, unfortunately not successfully, just ignore it and don’t install grub automatically)

apt-get install linux-image-amd64 grub
grub-install /dev/nbd0
update-grub

Last but not least, setup a root password

passwd root

Now lets unload the image, fix grub once again and be done with.

umount /proc/ /sys/ /dev/
exit
grub-install /dev/nbd0 --root-directory=/mnt --modules="biosdisk part_msdos"

Now edit /mnt/boot/grub/grub.cfg and find & replace nbd0p2 by sda2.

umount /mnt
kvm-nbd -d /dev/nbd0

The End.

You should now be able to boot the machine.

HINT: You should probably like to configure your network interfaces (/etc/network/interfaces) and /etc/fstab before booting. Just copy from your existing Debian system ;)

written by Diogo Gomes