Weblog entry #3 for johns
This howto explains how I migrated/converted a Debian Lenny vserver container to a KVM (libvirt) virtual machine. The instructions can only be followed as-is if you used the newvserver script (from the vserver-debiantools package) to create your vserver container.
First, create and start a new VM with enough diskspace and a suitable liveCD. I chose grml, which I can highly recommend for this type of task. It contains a SSH daemon as well as LVM and SWRAID support.
kvmbox$ virt-install --connect=qemu:///system -n $vmname -r 256 \ --os-variant=debianlenny -c grml_2010.04.iso --livecd --disk \ path=... -w bridge=br_eth0 -k $kblayout --vnc
Find and connect to the VNC display.
kvmbox$ virsh --connect=qemu:///system vncdisplay $vmname :3 yourbox$ ssh -f -L 5903:localhost:5903 kvmbox sleep 60m yourbox$ vncviewer localhost:3
In the liveCD on the VM, set a root password and start the SSH daemon.
grml$ sudo passwd root grml$ sudo /etc/init.d/ssh start
SSH into the liveCD on the VM, it's more handy to work that way. Partition, create and mount filesystems. /proc and /dev are bind mounted in order for grub-install to work. I am opting to use a swapfile, thus there's no mkswap command.
yourbox$ ssh root@$vmip grml# cfdisk /dev/vda grml# mkfs.ext3 /dev/vda1 grml# mount /dev/vda1 /mnt grml# mount --bind /dev /mnt/dev grml# mount --bind /proc /mnt/proc
Now it's time to stop and disable the vserver container, and to copy the filesystem over to the VM.
vserverbox$ sudo vserver $vmname stop vserverbox$ sudo rm /etc/vservers/$vmname/apps/init/mark vserverbox$ sudo tar --numeric-owner -c /etc/vservers/$vmname/vdir/ | \ ssh root@$vmip "tar -x --strip=4 -C /mnt"
In the VM, chroot into the newly copied filesystem and remove the vserver customizations. GRUB and kernel are also needed.
grml# chroot /mnt /bin/bash Re-enable hardware-related initscripts: chroot# pkgs=$(for f in /etc/rc3.d/K90*; do \ dpkg -S $(readlink -f $f); done | cut -d: -f1 | sort | uniq) chroot# for f in /etc/rc3.d/K90*; do update-rc.d -f $(basename $(readlink -f $f)) remove; done chroot# for pkg in $pkgs; do dpkg-reconfigure $pkg; done The above commands depend on the fact that the newvserver script disables such initscripts by deleting all links and setting them to be stopped in runlevel 3 at priority 90. Add root filesystem and swap to /etc/fstab: /dev/sda1 / ext3 errors=remount-ro 0 1 /swapfile none swap sw 0 0 Configure network interfaces in /etc/network/interfaces: # The loopback network interface auto lo iface lo inet loopback auto eth0 iface eth0 inet static address 192.168.0.xyz netmask 255.255.255.0 gateway 192.168.0.1 Re-add ttys to /etc/inittab: 1:2345:respawn:/sbin/getty 38400 tty1 2:23:respawn:/sbin/getty 38400 tty2 3:23:respawn:/sbin/getty 38400 tty3 4:23:respawn:/sbin/getty 38400 tty4 5:23:respawn:/sbin/getty 38400 tty5 6:23:respawn:/sbin/getty 38400 tty6 Install grub and kernel and acpid: chroot# mkdir /boot/grub chroot# aptitude install grub linux-image-2.6-686 acpid Abort initrd kernel image installation? No. Install grub to the VM's MBR: chroot# grub-mkdevicemap chroot# grub-install /dev/vda chroot# update-grub Exit the chroot, and shutdown the VM. chroot# exit grml# umount /mnt/proc grml# umount /mnt/dev grml# umount /mnt grml# halt
To make it easier to restart the process, the virt-install command I used earlier specified that the boot device should always be CD. To boot from HD, follow these steps.
kvmbox$ virsh --connect=qemu:///system edit $vmname Change the boot dev to hd.
You can now start the VM.