How To Virtualize A Physical Linux Machine

Want to support HowtoForge? Become 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 Petersen Allen (not registered) on Thu, 2011-06-02 21:36.
This worked like a charm for migrating a physical Centos 5.3 machine to VMWare, but I did run into one problem. After performing the instructions above, the machine wouldn't boot and was hanging on udev initialization. The original box had two CPUs, as did the VM. But once I changed the VM to only use one CPU it booted just fine. Just something to keep in mind.
Submitted by Colin (not registered) on Thu, 2009-08-27 06:24.

I used a variation of this method to transfer an ubuntu desktop installation which was being used more as a server than anything. As there were other partitions on the drive that I didn't want transferred I used the command: dd conv=noerror if=/dev/sda1 bs=4096 |gzip |nc {IP ADDR OF VM} {PORT} nc -l -p {PORT} | gzip -dc dd bs=4096 of=/dev/sda1 I then transferred the boot sector with the command: dd if=/dev/sda bs=512 count=1 |nc {IP ADDR OF VM} {PORT} nc -l {PORT} |dd bs=512 count=1 of=/dev/sda For reasons unknown to me, while it successfully copied over the entire filesystem, it didnt boot. The other thing which was odd was that it actually recreated the full paritition table from the old machine (which I thought was located somewhere between the boot sector and the first partition). While everything had copied, Gparted showed the entire virtual drive as unallocated, which was no small concern. To get it to boot I just reinstalled the grub bootloader by booting a live CD in the VM and typing: sudo grub root (hd0,0) setup (hd0) After that it booted fine - I didn't do the kudzu or mkinitfd as I had troubles with the ubuntu equivalent and the chrooted environment. It booted fine in the VM except for all its complaints about missing partitions. Deleting the non-existent paritions in FDISK and re-saving fixed the 'unallocated' parition and everything now appears fine. Now if I can just get it to recognise more than 640x480... Thanks for the How-To.

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 jamesloker (registered user) on Thu, 2011-07-07 13:51.

Its almost the same as the above except:

Boot the physical machine from the CD of Ubuntu desktop and the VM from the Ubuntu ISO

MAKE SURE THE VM IS BRIDGED

Open a terminal window
enter the following
sudo passwd root
enter a password (u can use password as it isnt saved after a reboot)
and retype

enter superuser
su
and use the password you just created

On the VM box type
nc -l 10000 | dd of=/dev/sda

On the physical machine type
dd conv=noerror if=/dev/sda | nc IPA.DDR.ESS.??? 10000

and that will copy it over the network

 

Submitted by jamesloker (registered user) on Thu, 2011-07-07 12:38.

For ubuntu its pretty much the same except:

On the VM machine

  1. Load from the Desktop CD and choose run ubuntu
  2. open a terminal (Applications > Accessories > Terminal)
  3. sudo passwd root (give it something like password)
  4. su (giving the password)
  5. nc -l 10000 | dd of=/dev/sda

On the host

  1. same as VM for 1 2 3 and 4
  2. nc $IP_ADDRESS_OF_VM 10000 | dd conv=noerror if=/dev/sda

This worked for me

 

 

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