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
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)
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.
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:
Next we need to format the newly created partitions and mount them.
Next stop, install the Debian system using debootstrap, feel free to include extra packages and to use a different mirror.
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:
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)
Last but not least, setup a root password
Now lets unload the image, fix grub once again and be done with.
Now edit /mnt/boot/grub/grub.cfg and find & replace nbd0p2 by sda2.
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 ;)