Fri, 28 Oct 2011

Shrinking disks in VirtualBox

Keywords: Virtualbox shrink disk size minimise

Update - 19.07.2014

This is actually a little easier now. Or I know better, perhaps.

Again, there should be no snapshots.

1. On the VM install zerofree:

# aptitude install zerofree

2. Boot into single user mode.

3. Run zero free on each virtual disk:

# zerofree /dev/sda1
# zerofree /dev/sdb1

4. Power down the VM

(On a Windows guest, for steps 1-4 just download and run sdelete -z)

5. On Windows, compact each virtual disk:

> "c:\Program Files\Oracle\VirtualBox\VBoxManage" modifyhd root.vdi --compact

And you're done.

If you've been using VirtualBox for a while with dynamically allocated disks, you've probably found that the size of the virtual disk exceeds that of the data stored therein. Sometimes by quite some ammount. Once used, the disk space is never returned.

I recently bought an SSD and wanted to put one of my Ubuntu virtual machines on it, that being where I do most of my work. However, the three virtual disks being used by that VM had grown quite large - too large to store on my SSD. There were also snapshots in use, despite the VM apparently not using any. This, I suspect, may have been due to mounting a virtual disk from another VM which had snapshots. Or it might not. In any case, things were a little bit of a mess and I wanted to sort them out too.

The first thing I did was to clear out as much of the disks as I could that was just being used for storage. Then we get to the complicated part of trying to reduce their size. I have seen suggestions to use zerofree or resize2fs but it wasn't clear to me whether they worked with ext4. So here is the method I used.

1. Using VirtualBox 4.1.x, clone the VM. This removes all the problems with the snapshots and makes a single definitive copy of all the virtual disks. If you have more than one virtual disk, you could remove those disks beforehand, since copies of those disks are not actually required, though a copy of the disk containing the root filesystem is required.

2. Create new, empty virtual disks for each of your original disks.

3. Add these new empty disks to your VM, along with at least the copy of the root filesystem.

4. Start your VM, run fdisk and mkfs.ext4 or mkswap to create the filesystems.

5. Create mountpoints and mount your new and cloned disks.

6. For all filesystems except the root filesystem, run cp -a to populate the new disk either from the clone, if you made one, or just from wherever they are mounted on the VM.

7. For the root filesystem do the same, but this must be done from the clone.

8. Assume your new root filesystem is mounted as /mnt/root. Run:

# mount -o bind /proc /mnt/root/proc
# mount -o bind /dev /mnt/root/dev
# mount -o bind /dev/pts /mnt/root/dev/pts
# mount -o bind /sys /mnt/root/sys

Edit /etc/fstab to reflect your new disk layout. If possible, it might be easier if you just mount the root filesytem at first and don't use the UUID format.

# chroot /mnt/root
# grub-install --root-directory=/ /dev/hda

Replace hda with whichever device you are booting from.

# update-grub
# exit

9. Create a new virtual machine, use your cloned one, or reuse your old one. Attach the appropriate disks and start the VM. If you are lucky, grub will be set up and the VM will boot.

10. Find out the UUIDs of your partitions by running blkid. Edit /etc/fstab appropriately and reboot.

11. Copy averything to where you want it to be. (My SSD.)

And that's all there is too it!

[/software/virtualbox] permanent link

Mon, 18 Oct 2010

Ubuntu 10.10 crashes running under VirtualBox 3.2.10.

Keywords: Virtualbox Ubuntu 10.10 crashes timer noapictimer nohz=off highres=off grub

I recently bought myself a new laptop. It's a rather nice HP Envy 14. Unfortunately no linux distribution properly supports it yet and so I run a Ubuntu guest virtual machine on it under VirtualBox with Windows 7 as the host.

Recently the VM started crashing. It would stop accepting keyboard input, and wouldn't shut itself down nicely using the mouse either from within Ubuntu or from VirtualBox. The only solution was to get VirtualBox to poweroff the VM. I upgraded Ubuntu to 10.10 and VirtualBox to 3.2.10, hoping to fix the problem, but it didn't help.

What did help though, was adding the following startup options:

noapictimer nohz=off highres=off

These can be added to the GRUB_CMDLINE_LINUX_DEFAULT line in /etc/default/grub, then run update-grub and the problem seems to be fixed.

Although sound under a VM isn't really the best idea, I just can't find any music players that I like which run under windows, and so I use Amarok 1.4. (No, I don't like Amarok 2.) Previously I was getting more frequent crashes whilst playing music. With this fix I've not had any crashes, even when playing flac files. Looking at the solution, this makes sense.

[/software/virtualbox] permanent link

Sun, 10 Oct 2010

Installing VirtualBox guest additions on a Ubuntu 10.10 guest

Keywords: Virtualbox Ubuntu 10.10 X server guest additions

Adding VirtualBox guest additions to a Ubuntu 10.10 guest fails for the X server, since 10.10 includes a later version than the guest additions are expecting.

The solution:

$ sudo aptitude install virtualbox-ose-guest-x11

[/software/virtualbox] permanent link

Sun, 18 Apr 2010

Installing a raw disk in VirtualBox

Adding a raw disk to VirtualBox is a lot harder than it is with vmware. Here's how to do it on Windows 7:

cd "\Program Files\Sun\VirtualBox"
VBoxManage internalcommands createrawvmdk -filename C:\Users\pjcj\.VirtualBox\HardDisks\1.5TB.vmdk -rawdisk \\.\PhysicalDrive1

I had to start the command prompt window an administrator in order to be able to do this. I then had to start VirtualBox as administrator in order to be able to add the disk from the Virtual Device Manager.

Update 19.06.2010:

Oracle bought Sun. The path is now:

cd "\Program Files\Oracle\VirtualBox"

But more importantly, if you do as I said above and do all this as an administrator, then you can't connect any USB devices, which is a shame. The solution to this, such as it is, is to turn off UAC (User Access Control). Type UAC into the start menu and drag the slider down to the bottom. Reboot, acknowledge that you are doing something silly, and off you go.

[/software/virtualbox] permanent link

September 2017
Sun Mon Tue Wed Thu Fri Sat