This is the P2V method that we used to convert a physical Linux server to a virtual machine running on KVM. It is a quick virtualization method that dumps the entire disk over the network to a new host machine. Ideally the origin server should be stopped in order to boot in rescue mode, thus making the copy with unmounted file systems. In our scenario it was not possible to halt the physical server, and this procedure did the trick.

Copy the disc

At the destination server start netcat listening to a port (in this example 43333), redirecting all its input to a file. If you connect using SSH do it in a screen so you can close the session.

nc -nvv -w30 -l 43333 | gzip -dc > disk.raw

At the origin server use dd to dump its disk. It is then compressed and sent to the netcat at port 43333 waiting with the command above. Gzip compresses the stream and reduces the amount of data to be transferred over the net. Our disk was 72 GB and took 4 hours to 10 Mbps, via the Internet.

dd if=/dev/cciss/c0d0 | gzip -c | nc -w30 -vvn your_server_address 43333

Note: The syntax of nc depends on the package you have installed. In this example it is netcat-openbsd.

Review the file systems

Now you have a disk image named disk.raw in the desination server. But the origin server was accessing its filesystems while dd was reading: you should run an fsck of them. If you are lucky the filesystem should not be badly damaged.

In Linux you can configure a file to be treated as a device assigning it to a loop device. We check the next free loop device.

losetup -f
/dev/loop0

Assign disk.raw to loop0 and it can be accessed using /dev/loop0

losetup /dev/loop0 disk.raw

Now read loop0 partitions and create appropriate devices to access it, we use kpartx.

kpartx -av /dev/loop0

The partitions are accessible via /dev/mapper/loop0p[n]. Do an fsck.

fsck.ext3 -f /dev/mapper/loop0p1
fsck.ext3 -f /dev/mapper/loop0p2
...

At this point you can mount the partitions to verify that the files are indeed there and maybe fix the device names in /etc/fstab.

Before starting the virtual machine, you need to undo this process to prevent damage to the filesystem.

kpartx -dv /dev/loop0
losetup -d /dev/loop0

Create the virtual machine

Create a KVM virtual machine with disk.raw attached and try booting.

It may not boot because the device name of the disc may have changed and the kernel does not find the root filesystem. Using the KVM console you will have to fix the line in GRUB or LILO, correcting the root = option in the kernel line. When it finally boots remember to fix the boot loader and /etc/fstab.

In our case the physical device /dev/cciss/c0d0p[n] became /dev/hda[n] in the VM.

Wrapping up

You must configure the new virtual network interface and verify that the services work properly.

During the virtualization the data on the original physical server has been updated and it must be synchronized before any real server change. So stop the service within a maintenance window, sync the data and then you can change DNS zones or whatever. Downtime has been minimized.