Update for 2018: some of the commands have been changed below to reflect new possibilities present in Ubuntu 18.04, namely the excellent
losetup
command.)
I often find myself in the position of having to transfer all my files, applications, and other configurations that make my laptop “mine” onto a new laptop.
What’s so strange about that, you might add? Well, I go through all of this once every six months. It’s not that I keep buying new computers — I don’t. But I often obtain them in other ways — I trade, I help someone buy a new computer and in turn get their old one, etc.
So, tired of having to constantly re-install everything (or, at the very least, if I’ve imaged one laptop to another, having to spend a week or so having to get everything running just right), I decided to just convert my current main computer into a VM that I could just run on any computer, running any sort of OS that’s enough to run VirtualBox.
(This tutorial was created with VirtualBox in mind, but other VM’s have similar ways of converting the final file after you get to about step 3 or so.)
It seems like it should be easy, and after a little bit of work, I found out that it’s not too hard.
- First, you’ve got to make an image of your current installation. (This is much easier if you have your entire Ubuntu install on one partition, i.e., no /home partitions on another hard drive or partition. You can probably figure out how to manage that, but this tutorial will be just for one-partition installs).
- Boot your computer with another startup disk (CD, jump drive, whatever), and then perform the following command:
dd if=/dev/sda1 of=image.bin
- “/dev/sda1” refers to the partition name that your main install is on — you can find this by doing a “sudo blkid” or “sudo fdisk -l”
- “image.bin” refers to the output file that the image will be contained in — this can be anywhere you want, but set it to a location that’s not on the hard drive you’re trying to image.
- Boot your computer with another startup disk (CD, jump drive, whatever), and then perform the following command:
- At this point, I tried to turn the image.bin file into a .vdi file so that VirtualBox could use it for a virtual machine — the problem is, at this point, your .bin file is just a partition, and not a real “virtual” hard drive. There’s no partition table, etc. — you have to simulate these things.
- You do this by creation an empty “sparse image” where we’ll copy the image, simulating a hard disk, and then create a partition table:
dd if=/dev/zero of=newhd.img bs=1G count=0 seek=100
- In this, “newhd.img”represents the location of the file we’re creating, and “100” represents the size of the virtual hard drive we’re creating, in gigabytes. You may want to make this larger or smaller depending on the image you made.
- Now, edit the image with “fdisk newhd.img“, and, following the commands presented in the fdisk interface, create a new partition table, and create a partition as large as the image you created. (The commands inside fdisk are pretty self-explanatory.)
- Now, make the partitions available as individual devices to your system.
sudo kpartx -a newhd.img
- Now, copy the original .bin file you made in step 1 onto the newly mounted partition:
sudo cp image.bin /dev/mapper/loop0p1
- Now, run a disk check, and expand the copied partition to fill all of the available space, and then finally close the mounted partitions:
- sudo e2fsck -f /dev/mapper/loop0p1
sudo resize2fs /dev/mapper/loop0p1
sudo kpartx -d newhd.img
- You do this by creation an empty “sparse image” where we’ll copy the image, simulating a hard disk, and then create a partition table:
- At this point, you should have a newhd.img file, which represents the entire hard drive you’ll virtually mount in your VM — the only step left is to convert it from a raw image of a hard drive into a .vdi file for use in VirtualBox:
- VBoxManage convertfromraw -format VDI newhd.img newhd.vdi
The only steps left at this point are to create your new VM in VirtualBox, and then start it using this HD. It more than likely won’t boot, so what you’ll need to do is start it with a livecd of your choice, and then fix the boot (I used the wonderful boot-repair utility available to Ubuntu).
Â
Â
Sources:
https://superuser.com/questions/554862/how-to-convert-img-to-usable-virtualbox-format
https://askubuntu.com/questions/69363/mount-single-partition-from-image-of-entire-disk-device