How To Virtualize A Physical Linux Machine

Do you like HowtoForge? Please consider to support us by becoming a subscriber.
Submitted by mannes (Contact Author) (Forums) on Tue, 2008-06-03 12:34. :: Virtualization

How To Virtualize A Physical Linux Machine

This tutorial shows how you can virtualize a physical Linux machine. It is intended not only for virtualization, but for any relocation of a RedHat Linux based system from one machine to another.

Here are the steps to be completed:

1. Install a virtualization software on the desired machine.

2. Create an empty virtual machine with enough disk space, selecting the OS you’re going to migrate.

3. Download a Linux Live-CD ISO (e.g. Knoppix).

4. Burn one copy on a CD and then copy the ISO to the virtualization server (the host).

5. Reboot the original Linux box (the physical machine that you want to virtualize) with the CD you’ve just burnt, and connect the ISO to the newly created virtual machine (i.e., boot the VM from the ISO).

6. Check both live CDs have net connection correctly configured and that they can see each other.

7. In the virtual machine, type this:

nc -l -p 10000 | dd of=/dev/hda

8. On the machine you’re migrating, type:

dd conv=noerror if=/dev/hda | nc $IP_OF_THE_VM 10000

9. Wait until dd finishes its job and do a clean reboot of the virtual machine.

10. Boot in rescue mode with the distro's first CD or DVD and chroot to the new system (on the virtual machine).

11. Run kudzu to remove old devices and find the new ones (or manually edit your /etc/modprobe.conf file, which is the most important since it includes your scsi-device). For SuSE Linux systems, you may need to run yast to discover the new hardware.

12. Create a new init using mkinitrd on your kernel version. Example:

/sbin/mkinitrd -v -f /boot/initrd-2.6.20-1-new.img 2.6.20-1

13. Edit your /boot/grub/menu.lst file to boot your newly created initrd image.

14. Keep your fingers crossed and reboot your VM.


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Sergiu Pol (not registered) on Mon, 2009-06-29 14:55.

For general usage this tutorial is very good. Actually is a general tutorial for cloning a HDD with Linux installed.

 But if you care about the following things, the above steps are not enough:

1) The physical HDD can be large (250, 500, 750 or 1000 GB) and the Linux partition can be only 20-30 GB. Or some percentage of HDD space can be unpartitioned for later usage. Applying the tutorial ALL the source HDD will be coppied to the destination HDD (the virtual one).

2) The file systems in partitions from the physical HDD can be only 50% or less utilized so is very inefficient to clone the whole FS in the virtual HDD.

Among the above written steps I used some solutions to use in the VM as little space as possible:
A. Reduce the source FS and the device it sits on (partition or logical volume)
B. Copy the HDD without the unpartitioned space

Along with A. and B., we can use some tricks that can make this faster:
- http://tipstricksandmore.blogspot.com/2009/01/after-converting-physical-rhel4-system.html
- http://kb.vmware.com/selfservice/microsites/search.do?cmd=displayKC&docType=kc&externalId=1002402&sliceId=2&docTypeID=DT_KB_1_1&dialogID=14730726&stateId=0%200%204678181
- http://bisqwit.iki.fi/source/lvm2defrag.html
- usage of dd over the network with compression (good especially for empty spaces) and 4k IO size (faster than default IO size): dd bs=4096 if=/dev/sda | gzip | ssh root@destination_hostname "gzip -dc | dd bs=4096 of=/dev/sda"
- clone the MBR (1st soltion): dd if=/dev/sda of=/dev/hda bs=512 count=1
- clone the MBR (1st soltion): sfdisk -d /dev/sda | sfdisk -f /dev/sdb (over the network you can use sfdisk -d /dev/sda |  ssh root@destination_hostname "sfdisk -f /dev/sdb")
- modify destination hostname and network settings (RHEL and Fedora):
1. check name: (C) uname -n; (C) hostname -a
2. edit /etc/hosts
3. edit /etc/sysconfig/network
4. edit /etc/sysctl.conf
5. reboot (or echo hostname.mydomain.com > /proc/sys/kernel/hostname)
6. edit /etc/sysconfig/network-scripts/ifcfg-eth0 eth1 ...
7. restart network service or computer

Have fun :)


Submitted by mannes (registered user) on Fri, 2009-07-03 10:58.
I used this implementation to rescue the data and OS from an old and low performance machine (PIII Xeon). The installed HDD wasn't that big, so dd was a good choice for the time being. The server in cause was a simple 1U old HP server, with a pretty small HDD (I don't recall its size anymore). I guess there are lot's of people out there that want to get rid of the old hardware, and to keep the valuable data along with OS. For this purpose, the described method is the way to do it. But there's always room for improvement....
Submitted by Anonymous (not registered) on Tue, 2009-01-06 15:16.

I'm trying to execute these commands but am told that /dev/hda is read-only.  It is, after all, the cdrom.  My target vm has SDA and SDA2 (largest drive space) after booting with Knoppix.  Should I be using these instead?

:)

Submitted by mannes (registered user) on Thu, 2009-02-19 09:00.
first have a look at what disks you have in the system with fdisk -l
Submitted by comedit (registered user) on Fri, 2008-06-20 23:12.

I am looking for something like this for (k)ubuntu.

Does this also work for (k)ubuntu Since I really need to virtualize my physical kubuntu machine

 

Thanks in advance

Submitted by mannes (registered user) on Thu, 2009-02-19 09:59.

to move the data out of the physical HDD and onto the virtual one, this HowTo should do the trick. I'm no Debian/Ubuntu expert, but there should be a way to rescan for hardware changes and set up the new found hardware accordingly

Submitted by maddog (not registered) on Mon, 2008-09-29 22:48.
wow never thought about such netcat usage - great idea!
Submitted by korovamilk (registered user) on Mon, 2008-06-09 10:30.

smart use of netcat here,

great tutorial... thanks for sharing it!

Submitted by SamTzu (registered user) on Wed, 2008-06-04 10:53.

Great tutorial. thx

Sam