Linux

Virtualizing a physical Linux machine

Jack Wallen explains how with just two commands you can have a file ready to import into your virtual machine tool of choice.

Many businesses are opting to virtualize their existing machines. This solves a number of issues, which includes allowing you to: migrate many servers to one, have more control over backups, and more easily restore a machine after a major issue.

It's easy to create new virtual machines and download and run a virtual appliance, but what about converting a currently running Linux machine to a virtual machine that can be used with either VMware or VirtualBox? Believe it or not, with just two commands you can have a file ready to be imported into your virtual machine tool of choice. I'll walk you through the process, but first, some preparation is required. Here's what you'll need:

  • An external drive as big or bigger than the drive to be virtualized
  • The dd tool
  • The qemu emulator software package
  • A lot of patience

Let's make sure we have the external drive prepared. More than likely, the image to be copied to the external drive will be quite large. Because of that, the drive must be formatted in such a way that it can accept larger files (this means NTFS, ext3, or such). If your drive was formatted in the older FAT32 format, it will cause the dd command to fail after 4 GB of data have been transferred. For formatting the external drive from within Linux, I highly recommend using the Gparted tool to ensure you are formatting the right drive and formatting the right drive with the right filesystem type.

Prepping the machine

Once the external drive is prepared, it's time to prepare the physical machine for conversion. The more space used on the physical drive, the longer the conversion process will take. I ran a test on a drive that had over 100 GB of data, and the creation of the .img file took nearly three days. Because of this, you should delete or move all unnecessary files (such as images, movies, music, etc.) from the physical drive. Then, you should make sure you have all the updates you want, and the machine is in just the state you need. After that is complete, it's time to start the conversion process.

Creating the image file

What we are going to do is use the dd command to create a bit by bit copy of the existing hard drive. Plug in your external drive and make note of the mount point (check /media where you should find that drive). Let's say your external drive is mounted at /media/EXTERNAL_DRIVE. Now, you have to know the name of the drive you want to copy. To find that out, issue the command mount, and you should see something similar to this:

jlwallen@jlwallen-VGN-NS325J:~$ mount

/dev/sda6 on / type ext4 (rw,errors=remount-ro,user_xattr)

proc on /proc type proc (rw,noexec,nosuid,nodev)

sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)

none on /sys/fs/fuse/connections type fusectl (rw)

none on /sys/kernel/debug type debugfs (rw)

none on /sys/kernel/security type securityfs (rw)

udev on /dev type devtmpfs (rw,mode=0755)

devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620)

tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755)

none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242880)

none on /run/shm type tmpfs (rw,nosuid,nodev)

gvfs-fuse-daemon on /home/jlwallen/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev,user=jlwallen)

The important line is the first ¨C indicating the physical drive is located at /dev/sda. That is what we will use for the creation of the image file. The command to create this is:

dd if=/dev/sda of=/media/EXTERNAL_DRIVE/sda.img bs=1024

After you fire up this command, you need to walk away from the computer for two reasons: (1) The command will take quite a while to complete, and (2) You do not want to interact with that computer while dd runs, because you could wind up with an I/O Error. When that command finishes, take a look at the file on your external drive -- it should be a rather sizable file named sda.img.

Converting to a virtual machine image

Before you start this process, you must make sure qemu is installed. Open your Add/Remove Software tool, do a search for "qemu" (no quotes), and install the package from there. When that installs, it will also install the necessary command to be used in this process.

You can run this command that will convert the img file to a Qemu-ready image: qemu-img convert -f raw sda.img -O qcow sda-qcow.img. You can also run this command that will convert the img file to a VWware vmdk file: qemu-img convert -f raw sda.img -O vmdk sda.vmdk.

Both commands will also take a while, though not nearly as long as the command to create the img file. When the command is complete, you will have either an .img or a .vmdk file ready to be used in either Qemu or VMware. You can migrate the file over to your virtualization server and fire it up.

This is a great way to migrate running machines to a more modern, flexible system. Although virtualization isn't for everyone, if it is something you want to look into, this method is a great way to kick the tires.

About

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website getjackd.net.

14 comments
nightwatch921
nightwatch921

Can this work for android phone and then try to use it with virtual machine ?

mbbs
mbbs

This looks very interesting but is there a method to convert one of the OSs on a disc on which multiple OSs are installed, to convert only 1 OS. For example disk SDA contains SDA1=windows, SDA2=/ of an ubuntu OS, SDA3= /home of the ubuntu OS SDA4= / of a Fedora OS, SDA5= /home of the fedora OS SDA6 is an NTFS data partition common for all OSs. and I want to make a VM image for the Fedora OS. Is it possible to create an image of the window OS ?

fernpromero
fernpromero

Creating a bit by bit copy of the hard disk is awkward and very slow!!! Is it not possible to create a partition image at filesystem level (i.e. not bit by bit, like Norton Ghost does) and converting that image into a virtual hard disk supported by VMWare? I believe that VMWare Converter allows to convert some types of partition images to a virtual hard disk. Please, check out this link: http://www.vmware.com/products/converter/faqs.html

12p542
12p542

Would changing the block size speed up the process without affecting the end result?

HankGurdjieff
HankGurdjieff

I stumbled on this article and it could help me out with a current problem I'm having, so a big thanks! I had the same concern as pfrields above about running dd on a live system, but as pointed out, that is easy to avoid by booting from some other media.

Neon Samurai
Neon Samurai

Ripping a drive image then converting it to the VM's native format is an interesting approach. I've done similar when moving VMs between VMware and Virtualbox. For metal to vm conversion I've generally used these two methods: System Image backup and restore - confirm the drivers for the VM environment are on the *nix server - rip a bootable system backup image with whatever (Clonezilla, Mondo Rescue, Norton Ghost) - create the vm guest machine with applicable ram/storage/nic - restore bootable backup image into the VM (mount ISO as VM's cd drive) System rebuild - my systems are based on a scripted build; bare metal to fully configured in a mostly automated hour - create the vm guest machine with applicable ram/storage/nic - minimal *nix distribution install - run normal build scripts for repository setup, package installs, configuration - push data/content back to production machine from developer production branches The first may be desirable if you have to move the system as is. If you have a setup where content/data resides (subversion, git or similar version/content manager) or your doing a clean restart then the second is desirable to have a fresh build on the new "hardware" environment.

pfrields
pfrields

In most cases /dev/sda is the drive the system booted from and runs on. Therefore you have no guarantee the system won't use the drive from time to time during the imaging process, which has the potential to make your target image file inconsistent or (at worst) unusable. You should boot from a live disc or alternate boot device to make the image. Also, I'm not sure this was clear in the article, but removing files from the hard disk you're imaging with dd has no effect on the time it takes to copy. The dd command is not reading only active files, but rather doing a complete copy of the drive. Finally, the block size you gave will be less than optimal performance for most modern drives, where the optimal block size would be somewhere between 8192 (8 KB) and 32768 (32 KB). However, larger block sizes tend to have negligible impact, so a block size of 1 MB is usually fine (bs=1M).

NickNielsen
NickNielsen

If the drive is mounted. Run the MOUNT command by itself. Find the line with the ntfs file system. Then enter the mount point into the dd command as follows: dd if=/dev/sd[i]X[/i] of=/media/EXTERNAL_DRIVE/sda.img bs=1024 where sd[i]X[/i] is the mount point (sda, sdc, etc.) for the Windows partition.

pgit
pgit

Combine a few steps by booting the G4U (ghost for Unix) live CD. It combines dd and the image creation options. I've never had any luck with clonezilla, but mondo rescue has been great. (smoothly restore over the network on occasion) Been years since I've had a copy of Norton Ghost, but IIRC it is the preferable tool. It Just Plain Works (tm)

pgit
pgit

Yep, you're setting yourself up for a lot of wasted time, when the system randomly accesses the system drive, altering the input file. I always boot to a Linux live CD, Slax, Knoppix and lately fedora. You don't mount the drives, either, that only puts them in the local file system. They are still present as devices, dd works on the device level, not file system. Gave ya a +1 for beating me to the post. :)

mbbs
mbbs

Thx, this answers the q for the windows partition, I was not sure that a windows could be converted to a VM using this method. Is there also a solution to create an image of one of the linux OSs without having to copy the entire disk ? They consist of 2 or 3 partitions per OS (/,swap and /home) I looked in the dd man page but did not immediately see an option to include multiple partitions, but as I am not a dd expert, there may well be a way to do this.

NickNielsen
NickNielsen

But Idon't think the capability is there. Not being a Linux expert and not as familiar with VMs as I should be, I can't say for sure. My take would be to merge the partitions, then save them to the image file. It may also be possible to copy / to the image file, start your virtual machine, mount the appropriate /swap and /home partitions, then save the VM with /swap and /home incorporated.