VirtualBox

Posted on 2015-03-01 in Trucs et astuces

Transform img disk (qemu) to vdi (Virtual Box)

You need to convert img in raw with qemu-img and then raw to vdi. It will take time.

$ qemu-img convert WindowsXP.img -O raw WindowsXP.raw
$ VBoxManage convertdd WindowsXP.raw $HOME/.VirtualBox/VDI/WindowsXP.vdi

Transform vdi to img

$ VBoxManage clonehd sdb.vdi sdb.raw --format RAW
$ qemu-img convert sdb.raw -O img sdb.img

Compress an image

Prepare Windows guest

  1. run defrag in the guest (Windows)
  2. nullify free space: sdelete –z

Prepare Linux guest

sudo dd if=/dev/zero of=/bigemptyfile bs=4096k
sudo rm -rf /bigemptyfile

Compess

  1. shutdown the guest VM
  2. Run VBoxManage's compact command: VBoxManage.exe modifyhd thedisk.vdi --compact

Resize a disk

vboxmanage modifyhd /home/CHEMIN/VIRTUALBOX\VOTRE.vdi –resize 80000 # 80 Go HDD

Mount a VDI to the local FS

Run these commands as root:

  1. Enable the proper kernel module: modprobe nbd
  2. Create a virtual device with at /dev/nbd0 with qemu-nbd -c /dev/nbd0 /PATH/TO/YOUR/VDI/disk.vdi
  3. List the partitions on the disk with parted /dev/nbd0 print
  4. Mount the partition in /mnt/vdi (eg if you want partition 2):
    1. mkdir /mnt/vdi
    2. mount /dev/nbd0p2 /mnt/vdi/
  5. Extract the data you need by navigating under /mnt/vdi.
  6. Unmount the volume: umount /dev/nbd0p2
  7. Disable the virtual drive with qemu-nbd --disconnect /dev/nbd0

Note: If you want to mount a LVM volume, you will get an mount: /mnt/vdi: unknown filesystem type 'LVM2_member'. error. You need to do before instead of the mount step:

  1. Identify the LVM volume with lvscan. You should see a path to the LVM volume to mount. /dev/fedora/root in may case with this output:

    ACTIVE            '/dev/fedora/swap' [3.00 GiB] inherit
    ACTIVE            '/dev/fedora/root' [<26.00 GiB] inherit
    
  2. You can then mount it with mount /dev/fedora/root /mnt/vdi/

  3. To unmount, use umount /dev/fedora/root

  4. Disable the virtual drive normally.

Sources: