Physical To Virtual Migration

| | | |

OpenVZ and KVM are Linux based virtualization programs, both are part of the Proxmox VE distribution. The goal of this article is to provide some knowledge on moving physical machines to virtual containers (OpenVZ) or fully virtualized machines (KVM). This article is not specific to Proxmox VE and the principles outlined and scripts provided should work on "stock" KVM or OpenVZ machines with a few minor changes to path settings.

Physical Microsoft Windows Machine to KVM

First we will look at the process of moving a physical box to a KVM virtual machine. I am going to focus on moving a Windows 2003 Server machine. The reason for focusing on Windows is because it does not make much sense to run a Linux server in KVM given the advantages that OpenVZ provides.

The first thing that needs to happen is you need to prepare the physical machine that you are moving. This involves making notes of what hardware is currently being used paying special attention to the hard drive driver. If you are using a SCSI driver or SATA driver your virtual machine may not boot as KVM uses an IDE virtual disk. There is a fix for this outlined in a Microsoft knowledge base article Q314082 (Link).

Next you will need to have access to a couple of tools which are free but not open source. The first tool you need is VMware Converter. Download VMware Convert to the physical box that you are converting and run it. It will guide you step by step on creating an image. The only thing that is required for this tool is a place to put the resulting image. You can use network storage or a USB flash drive, anything but the disk of the machine being converted.

Once the image is made you need to convert the image to a single growable file. To do this I used the vwmare-vdiskmanager.exe program that comes as part of the free VMware Server program. The syntax for this program is easy:

vmware-vdiskmanager -r win2003.vmdk -t 0 win2003-pve.vmdk

I then used WinSCP to copy the resulting file to my Proxmox VE machine. To use it in Proxmox VE you need to copy it to /var/lib/vz/images. You then need to preform one more conversion of the image to make it usable on Proxmox VE. You need to convert the file to qcow2 format:

qemu-img convert -f vmdk win2003-pve.vmdk -O qcow2 win2003-pve.qcow2

I then used Proxmox VE to create a blank KVM virtual machine as a template. Edit the configuration file and replace the line that starts with hda: to point to the new qcow2 file that you created. For example:

hda: /var/lib/vz/images/win2003-pve.qcow2

You are now able to start the KVM virtual machine. You may want to install paravirtualized Network Drivers for increased performance of the network. This same basic procedure will work with Windows XP and Windows Server 2008.

Physical Linux Machine to OpenVZ Container

The scripts that are shown are very much a work-in-progress and if you have suggestions on improving them please leave a comment with improvements. This is not quite as straight forward as the KVM machine migration, I will do my best to guide you.

First the script:

#!/bin/sh
#
# Bash Script used to move a physical host to a virtual on
# This script must be run as root from host node that the container
# will be on.
#
# Version 2.0 
# 6/10/2008 Andrew Niemantsverdriet
#

echo -n "Enter the host to move: "
read host

echo -n "Enter the OpenVZ container ID: "
read ctid
echo

echo "Rsyncing $host to CTID: $ctid"

rsync -arvpz --numeric-ids --exclude-from '/root/.excludes' $host:/ /var/lib/vz/private/$ctid/

#Clean Ups
echo "Cleaning Up..."
sed -i -e '/getty/d' /var/lib/vz/private/$ctid/etc/inittab

rm -f /var/lib/vz/private/$ctid/etc/mtab
ln -s /proc/mounts /var/lib/vz/private/$ctid/etc/mtab

cp /var/lib/vz/private/$ctid/etc/fstab /var/lib/vz/private/$ctid/etc/fstab.old
grep devpts /var/lib/vz/private/$ctid/etc/fstab.old > /var/lib/vz/private/$ctid/etc/fstab

echo -n "Do you want to start CTID: $ctid now? (y/n): "
read ok

if [ "$ok" = "n" ]; then
  echo
  echo "Process Complete"
  exit 1
else
  vzctl start $ctid
  echo "You can now enter container and disable un-needed services"
fi

And the .exclude file place in /root/.excludes:

.bash_history
/boot
/dev/*
/mnt/*
/tmp/*
/proc/*
/sys/*
/usr/src/*
/etc/sysconfig/network-scripts/ifcfg-eth*

Running the script is pretty straight forward. First it asks for the host name to migrate and next it asks for the container to put it in. This should be an already created container, and it should not be running. Next the script uses rsync to grab the data from the physical box. It ignores files listed in the .exclude file. The script then goes and does some basic cleanups to enable the container to boot and to remove parts that OpenVZ does not use. Lastly the script asks if you want to start the container.

Once you get the container running there are few manual cleanups left to do. The biggest is to disable udev, which is very distribution specific. In CentOS 5 you edit the /etc/rc.sysinit file and comment out the line that looks like this: /sbin/start_udev

Lastly you need to turn off un-needed services in CentOS 5. These are things like acpid, kudzu, lm-sensors, microcode_ctl and netpluged.

In Closing

Hopefully I have provided enough information for you to successfully migrate a physical box to a virtual one. If you have questions please leave them in the comment section and I will do my best to help you out.


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Just what I was looking for.

Thanks mate,
It worked like a champ.

David


Help ?

"I then used Proxmox VE to create a blank KVM virtual machine as a template"

When I got to this stage and revisited the Proxmox web site I became very confused. It seems at this stage (before using proxmox), we have created a physical machine image which is stored in a qcow2 format file for use by qemu (or libvirt/kvm, whetever).

SO, all I should need to do now is point the virtual machine manager at the disk image, and start it running ? I can understand possibly requiring a configuration file which describes a few basic options like what OS, what file system , etc.

But why can't I create one of those by hand ?


something new to try

Thanks for this...

I now have something else to try...


About Networking

I have run across a server with IPv6 enabled in CentOS 5 to turn that off edit the /etc/sysconfig/network file and set NETWORKING_IPV6=no. OpenVZ does not have IPv6 on by default.

_
/-\ ndrew


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.