I’ve come across a couple laptops recently where I wanted to be able to quickly (and safely) experiment on them. Experiments that had the possibility of seriously messing things up.
A more traditional approach to this would be to make an image of the machine, perform the experiments, and if it fails catastrophically, restore the image.
Virtual machines have significant advantages in this, because you can snapshot them and instantly restore to that snapshot. But I was unable to set up a simple VM to do this as I needed one configured the exact same way.
So after some research, and playing around, I found that it’s actually quite simple to make a VM from a physical machine.
The steps I would take to accomplish this are:
- Image the physical machine to a file
- Convert the raw file to a virtual machine disk file (.vmdk)
- Create a new Virtual Machine and point it to the .vmdk file
Below are those steps in detail.
1. Image the Physical Machine to a File
To image the machine, my approach was to take a Linux live cd (on a usb stick) and use the command “dd” to make an image of the disk. First, I would fire up gparted in order to determine the correct disk path that I wanted:
Note: or use something like
$ fdisk -l
Once you know the path of what you want to image – for example: /dev/sdb – you can bring up a console and begin.
I like to add two things to the dd command in order to get more out of it:
- Compress the output with gzip
- Pass the data through pv [pipe viewer] in order to see the operation’s speed
matthew@lubuntu-vm:temp$ sudo dd if=/dev/sda | pv | gzip -c > ./image.dd.gz
Now, make sure that your destination is large enough to hold the image. An external drive or network share or somesuch.
2. Convert the Raw File to a Virtual Machine Disk File
First, if you sent your image through gzip to save some space like I did, extract it.
Next, using the VirtualBox commandline tool “VBoxManage”, convert the raw file to a VMDK file:
VBoxManage convertfromraw raw-image-name.dd vmdk-name.vmdk --format VMDK
- I’m sure other formats work too, but this is the only one I’ve tried
- It takes a while
3. Create a New Virtual Machine and Point it to the .vmdk File
This step is fairly straight forward. Create a new virtual machine and when it asks you if you’d like to make a new virtual machine disk, choose to select your own and point it to the .vmdk file you made.
When you start the virtual machine, it’ll look as if you’ve started the physical machine.