
I've only had to do this a couple of times in my life, but it's useful to know ;) There are 2 ways this can be achieved. I'll state the PROS/CONS at the end of each guide.
Details:
Host system - Gentoo X86_64 with 24Tb storage :), VirtualBox
Guest HDD - Gentoo X86_64 500Gb
I'm going to assume that you have good Linux knowledge using the cli with mounting, tarballing, fdisk and formatting filesystems.
#1 (pure disk dump)
Obviously connect the drive you wish to convert.
Create a drive image using dd:
dd if=/dev/sdX of=/path/to/output/file.dd
Then convert it to a VDI (Virtual Disk Image):
VBoxManage convertfromraw ImageFile.dd OutputFile.vdi
Create a new virtual machine and attach the vdi as the main drive.
Once booted into the VM, you will need to install virtualbox guest addititions and may need to reconfigure/recompile the kernel.
Done!
PROS:
- Not complicated.
- Relatively quick to do.
CONS:
- Uses a lot of space as dd reads every sector of the source drive.
- If you delete any data from the VM, it will not reduce the VDI size. Dynamic disks only grow, not shrink.
#2 (only copy files)
Connect the HDD.
Open a terminal.
su to root
Mount each filesystem. (I did the following)
mkdir -p /mnt/{oldhdd,newhdd}/{bootfs,rootfs,homefs}
mount /dev/sdb1 /mnt/oldhdd/bootfs
mount /dev/sdb2 /mnt/oldhdd/rootfs
mount /dev/sdb3 /mnt/oldhdd/homefs
cd into the place you wish to save the data.
cd /mnt/newhdd/bootfs
tar czvf bootfs.tgz /mnt/oldhdd/bootfs/.
cd ../rootfs
tar czvf rootfs.tgz /mnt/oldhdd/rootfs/.
cd ../homefs
tar czvf homefs.tgz /mnt/oldhdd/homefs/.
If you don't have an existing Linux VM, create one including an empty HDD and boot it with your favourite Linux CD/DVD. Ensure the tarballs you have just created are available to the VM. hint: setup a shared folder in the VBoxManager GUI and mount it in the VM.
You'll need to partition the empty HDD appropriately to accommodate the data you'll be restoring and format each filesystem. Mount each filesystem you've just created and and the shared directory then unpack the tarballs to the new HDD.
So we'll mount the prepared HDD.
mount /dev/sda2 /mnt/newhdd/rootfs
mount /dev/sda1 /mnt/newhdd/rootfs/boot
eg
tar zxvf /path/to/share/rootfs.tgz -C /mnt/newhdd/rootfs
tar zxvf /path/to/share/bootfs.tgz -C /mnt/newhdd/rootfs/boot
The fun doesn't end yet :) Now we have restored our data, we now need to reinstall the boot loader. In this case, grub2. But first we need to chroot into our restored data.
cd /mnt/newhdd/rootfs
mount -t proc none /mnt/newhdd/rootfs/etc/proc
chroot /mnt/newhdd/rootfs /bin/bash
env-update
source /etc/profile
grub-install /dev/sda
edit /etc/fstab if required
grub-mkconfig -o /boot/grub/grub.cfg
exit
Unmount the filesystems and reboot the VM. If everything went to plan, you should now be booted into your VM. You will need to install guest additions and may need to reconfigure and recompile your kernel.
PROS:
The VDI only uses the space required.
CONS:
More involved and takes longer.
#3 (existing VM)
This is a combination of #1 & #2 methods.
Create a drive image using dd:
dd if=/dev/sdX of=/path/to/output/file.dd
Then convert it to a VDI (Virtual Disk Image):
VBoxManage convertfromraw ImageFile.dd OutputFile.vdi
Add the new VDI as an additional HDD to your VM. Also create a new HDD and partition appropriately.
Within the VM,
tar clf - -C /mnt/oldhdd/bootfs .| tar xf - -C /mnt/newhdd/bootfs