Tuesday, September 18, 2012

Adding space to Ubuntu VM without reboot.

Using LVM allows you to add space to a virtual machine without rebooting it. However you need to be using it already if you want to grow existing file systems. This post only covers increasing the size, as shrinking is more risky and I generally avoid it.

Increasing the size of a filesystem where LVM is used directly on the disk:
  1. If you're using LVM on the storage device (not partition) directly, it's trivial to add more space.
  2. Increase the size of the virtual machine's storage device.
  3. Run `dmesg` and find the appropriate SCSI device (e.g. 2:0:0:0)
  4. Tell the kernel to rescan that device:
    $ echo 1 | sudo tee /sys/class/scsi_device/2\:0\:0\:0/device/rescan
    $ sudo dmesg
    …
    [151064.769416] sda: detected capacity change from 5368709120 to 6442450944
  1. Use `pvresize` to increase the size of the physical volume:
    $ sudo pvresize /dev/sda
    $ sudo lvextend -l+100%FREE /dev/VolGroupName/root
    $ sudo resize2fs /dev/VolGroupName/root

This is only possible when the drive isn't partitioned. If the drive is partition, the easiest solution is to add a new storage device to the VM, use LVM directly on that, and add it to the existing volume group:

  1. Add new storage device and detect it in the guest using scsitools:
    sudo apt-get install scsitools
    sudo rescan-scsi-bus.sh
  1. Do something along the lines of:
    $ sudo pvcreate /dev/sdb
    $ sudo vgextend VolGroupName /dev/sdb
    $ sudo lvextend -l+100%FREE /dev/VolGroupName/root
    $ sudo resize2fs /dev/VolGroupName/root