Migrating a CentOS-3 physical machine into a KVM virtual machine

I documented the following procdure when migrating an 11-yr-old CentOS-3 server system into a KVM virtual machine early last year after its CPU cooling fan finally wore out.

Migrate CentOS-3 physical machine to KVM virtual machine
========================================================
*** CentOS-3 physical machine (source system) ***
[root@dragon root]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/hda2             966M  271M  646M  30% /
/dev/hda7             150G   72G   71G  51% /home
none                  375M     0  375M   0% /dev/shm
/dev/hda5             479M  8.2M  446M   2% /tmp
/dev/hda3             3.8G  1.6G  2.1G  43% /usr
/dev/hda6              74G   32G   38G  46% /var

Stop services:
# for s in mailman postfix postgrey amavisd clamd cyrus-imapd httpd saslauthd mysqld; do
  service $s stop
done
# svc -d /etc/dnscache /etc/dnscache/log

Start services:
# for s in mysqld saslauthd httpd cyrus-imapd clamd amavisd postgrey postfix mailman; do
  service $s start
done
# svc -u /etc/dnscache/log /etc/dnscache


*** CentOS 6.5 KVM host ***
Network Settings:
/etc/sysconfig/network-scripts/ifcfg-br0:
--------------- 
DEVICE=br0
TYPE=Bridge
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=dhcp
#IPADDR=202.109.113.88
#PREFIX=24
#GATEWAY=202.109.113.254
---------------

/etc/sysconfig/network-scripts/ifcfg-p4p2:
---------------
DEVICE=p4p2
HWADDR=A4:1F:72:81:B6:BE
TYPE=Ethernet
UUID=4386f3c9-3399-4e74-80b8-74e99f8a5b0a
ONBOOT=yes
NM_CONTROLLED=no
#BOOTPROTO=dhcp
BRIDGE=br0
---------------

# service network restart

# yum update
# yum install rsync vim
# yum install kvm libvirt python-virtinst qemu-kvm
# reboot

virt-manager should be installed in your local computer. It connects to the KVM VNC through ssh.

Create LVM logical volumes for the CentOS-3 VM:
# pvcreate /dev/sdb1
# vgcreate vg1 /dev/sdb1
# lvcreate -L120G -ncentos3 vg1

Create VM:
# virt-install -n centos3 -r 1024 --vcpus=1 --disk path=/dev/vg1/centos3  -c /var/lib/libvirt/images/systemrescuecd-x86-3.8.1.iso --vnc --noautoconsole --os-type linux --accelerate --hvm


*** KVM Virtual Machine (target system) ***
Boot the VM from systemrescuecd and use it to migrate the entire filesystem from the original CentOS-3 physical machine.

# fdisk /dev/sda (sda1 2G linux swap, sda2 120G ext3)
# mke2fs -O has_journal,filetype,sparse_super,^ext_attr,^resize_inode,^dir_index \
-I 128 -j -L "/" /dev/sda2
# mount /dev/sda2 /mnt/custom
# for p in usr home var; do
    rsync -e ssh -avx root@dragon.linux-vs.org:/$p/ /mnt/custom/$p/ --exclude "/lost+found/" --delete-excluded --delete
done
# rsync -e ssh -avx root@dragon.linux-vs.org:/ /mnt/custom/ --exclude-from  excludes --delete 
excludes:
/tmp/*
/var/*
/usr/*
/home/*

* Right before taking the physical machine offline, rsync filesystem once more after stopping its running services. You can also rsync directly from its hard drive after taking it offline.

Install the boot loader and make some adjustments so that the VM can boot from its virtual hard drive:

/mnt/custom/boot/grub/devices.map: /dev/hda -> /dev/sda
# grub-install --root-directory=/mnt/custom /dev/sda
/mnt/custom/boot/grub/grub.conf: modify root device names if necessary

/etc/fstab: remove redundant mount points


[Reference]
1. http://www.howtoforge.com/virtualization-with-kvm-on-a-centos-6.0-server
2. http://linux.dell.com/files/whitepapers/KVM_Virtualization_in_RHEL_6_made_easy.pdf
3. http://www.braindeadprojects.com/blog/category/virtualization/

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.