Virtual Box Tips

2017-04-16

Backing up VirtualBox virtual machine

Just make a copy of the .vbox and .vdi files.

Reducing disk space of dynamic disks

This doesn't work for encrypted disks.

On client virtual machine:

  • For Windows
    • Defragment disk, e.g defrag c:
    • Zero free space, e.g sdelete -z c: (sdelete is part of SysInternals)
  • For Linux (mounted)
    • dd if=/dev/zero of=zeroes bs=1M until there is no space left
    • rm zeroes
  • For Linux using ext2, ext3, ext4 (not mounted)
    • Boot with systemrecuecd
    • Zero free space on device with e.g zerofree -v /dev/sda1

On host machine:

VBoxManage.exe modifyhd /path/to/file.vdi --compact

Alternate technique

It may be helpful to defragment and zero free space on existing disk first.

  • Create new VDI of smaller size.
  • Boot GParted or Clonezilla using ISO image, and clone existing disk to the new smaller sized disk (vdi).
  • Change VM settings and boot VM using smaller disk.

Sharing a folder RHEL or CentOS Linux VirtualBox VM

Update VirtualBox Guest Additions modules

Requires gcc and kernel headers installed. If not then run:

yum install -y gcc kernel-devel-$(uname -r)

Open in menu: Devices > Insert Guest Additions CD image

Mount the drive and run the VBoxLinuxAdditions.run file.

mount /dev/cdrom /mnt
sh /mnt/VBoxLinuxAdditions.run
umount /mnt

Check module is installed with lsmod | grep vbox. You should see the vboxsf module.

Sharing a folder

To attach a folder for sharing:

  • Under Device > Shared Folders - Add your desired folder. Select "permanent" if you want this folder shared all the time.
  • Mount the drive with: mount -t vboxsf FOLDERNAME MOUNTPOINT
  • When finished, umount MOUNTPOINT

To make mount point persistent between reboots, add entry in /etc/rc.local (or /etc/fstab).