Just moozing

Before you can check your notes, you must make them…

Converting a physical machine to a virtual machine

with 2 comments

I have an old Linux server, that I want to put into my virtualized environment.

The process is fairly simple if you have command line experience, since Linux is very forgiving when it comes to changing hardware. Windows would probably fail a lot, since the hardware difference between a physical machine and a virtual one i significant. A quick Google search found this, but I have not tested it.

I tested this using libvirt and qemu/KVM on a Debian/Jessie host. The physical machine is also a Debian/Jessie installation.


Copy disk

I rely on dd for this

dd if= of=hdd.img bs=4M

The device name is e.g. /dev/hdd, not the partition, e.g. /dev/hdd1.

I have physically moved the disk and are copying using an external USB device. You could boot using clonezilla or similar and put it on a network drive.


Convert to qcow2

I always use qcow2 due to the space saving capability.

qemu-img convert -f raw -O qcow2 hdd.img hdd.qcow2

This might take some time, depending on copying speeds and CPU power.

And to take advantage of qcow2, we might need to process the image once more to shrink it.

qemu-img convert -c -O qcow2 hdd.qcow2 hdd-shrunk.qcow2

Some refs for the above commands: conversion, shrinking

A note on sizes:

  • Original disk: 160 GB
  • Raw image file: 150 BG
  • First qcow2 image: 144 GB
  • After shrinking: 118 GB

You decide, if it is worth it for you 🙂


Move the image

Libvirt has storage pools, and the new disk must be available in one of them. You may need to be root to do this.

An example

mv hdd.qcow2 /var/lib/libvirt/images


and force a pool refresh

virsh pool-refresh default

In the above example, it is assumed that the image is copied to the “default” pool.


Create a new virtual machine

  1. In virt-manager, create a new machine.
  2. Choose import existing disk image
  3. Select the disk image
  4. Select the other options to fit your need

I would seek inspiration in the specs of the physical machine, like setting the same amount of RAM and CPUs as the physical machine. It is tweakable later.


Now you should be able to boot the new virtual machine that is a clone of the physical machine.


Written by moozing

June 24, 2015 at 12:00

Posted in Tech

Tagged with , ,

2 Responses

Subscribe to comments with RSS.

  1. Didn’t bother testing, but if i’m not mistaken the majority of what you’ve written can be done in a single command, without unnessecary space hogging images inbetween the steps. Something like:
    sudo qemu-img convert -c -f raw -O qcow2 /dev/sda /var/lib/libvirt/images/hdd-shrunk.qcow2


    April 28, 2017 at 13:24

    • you are probably right 🙂
      I just like to include the intermediate results


      May 15, 2017 at 10:24

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: