Regain unused disk space from OpenStack instances

Posted Over 8 years ago. Visible to the public.

This is how you regain disk space from OpenStack instances if you are using kvm and qcow.

If your instance used up all configured disk space once the disk file remains big. You can end up in a situation where for example the instance use only 20GB disk space but the disk file on the server has 100GB (or even more).

To resize the disk file do the following:

  1. Check storage on the instance:

    vm $ df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/vda1        99G   19G   75G  21% /
    udev            2.0G   12K  2.0G   1% /dev
    tmpfs           396M  236K  396M   1% /run
    none            5.0M     0  5.0M   0% /run/lock
    none            2.0G     0  2.0G   0% /run/shm
    
  2. Check storage on the openstack host:

    host $ df -h
    Filesystem                    Size  Used Avail Use% Mounted on
    /dev/sda3                      14G  3.2G  9.9G  25% /
    udev                           24G   12K   24G   1% /dev
    tmpfs                         9.5G  572K  9.5G   1% /run
    none                          5.0M     0  5.0M   0% /run/lock
    none                           24G     0   24G   0% /run/shm
    /dev/sda1                     223M  163M   48M  78% /boot
    [...]
    /dev/mapper/system-instances  1.1T  782G  214G  79% /var/lib/nova/instances
    cgroup                         24G     0   24G   0% /sys/fs/cgroup
    

    You need at least as much storage left on the host as it used by the vm. So in this case at least 19GB (you should always calculate some buffer).

  3. Check size of the instances disk file:

    host $ ls -lha /var/lib/nova/instances/instance-000000d0/disk
    -rw-r--r-- 1 libvirt-qemu kvm 99G Sep 22 18:24 /var/lib/nova/instances/instance-000000d0/disk
    

    Be careful. If the disk file size is lower than the configured limit it will get to the maximum size before you can regain the disk space.

  4. Zero out the disk of the instance:

    # calculate 1024*74 (GBs available minus one for buffer)
    vm $ dd if=/dev/zero of=/foobar bs=1M count=75776
    # You should monitor the instance in the meantime that no other process uses the disk space
    # after it's done, delete the file
    vm $ rm /foobar
    
  5. Shut down the instance:

    vm $ shutdown -HP now
    
  6. Check if it's complete shut down on the host:

    host $ virsh list --all
    Id Name                 State
    ----------------------------------
    38 instance-000000d0  shut off
    
  7. Write your disk file again (without using the empty space):

    host $ mv /var/lib/nova/instances/instance-000000d0/disk /var/lib/nova/instances/instance-000000d0/disk_backup
    host $ qemu-img convert -O qcow2 /var/lib/nova/instances/instance-000000d0/disk_backup /var/lib/nova/instances/instance-000000d0/disk
    
  8. Start the instance again:

    host $ virsh start instance-000000d0
    
  9. Check if you instance is correctly working:

    vm $ are you ok?
    
  10. Delete the old disk file:

    host $ rm /var/lib/nova/instances/instance-000000d0/disk_backup
    
  11. Profit:

    host $ df -h
    Filesystem                    Size  Used Avail Use% Mounted on
    /dev/sda3                      14G  3.2G  9.9G  25% /
    udev                           24G   12K   24G   1% /dev
    tmpfs                         9.5G  572K  9.5G   1% /run
    none                          5.0M     0  5.0M   0% /run/lock
    none                           24G     0   24G   0% /run/shm
    /dev/sda1                     223M  163M   48M  78% /boot
    [...]
    /dev/mapper/system-instances  1.1T  708G  288G  72% /var/lib/nova/instances
    cgroup                         24G     0   24G   0% /sys/fs/cgroup
    
Last edit
Over 8 years ago
Kim Klotz
License
Source code in this card is licensed under the MIT License.
Posted by Kim Klotz to makandra dev (2015-09-22 16:35)