Tuesday, 5 July 2011

Reclaim Disk Space from Deleted XenServer Snapshots and Clones

Running this script will incur some downtime for the VM, due the suspend/resume operations performed.

This instructions are for XenServer 5.6 and later

Citrix recommends that you back up the VM on which you will run the space reclamation tool. You can use the XenCenter export option for this purpose.

Run the following command from the XenServer CLI:
xe host-call-plugin host-uuid=<host-UUID> plugin=coalesce-leaf fn=leaf-coalesce args:vm_uuid=<VM-UUID>
The amount of time required varies based on the amount of data written to the disk since the last snapshot. Smaller VMs (that is, 10 GB or less) take less than a minute.

If the Virtual Disk Images (VDIs) to be coalesced are on shared storage, you must execute the off-line coalesce tool on the pool master.

To get Pool Master UUID you can use this command:
xe pool-list params=master | egrep -o "[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}"
To get uuids of all running VMs
xe vm-list is-control-domain=false power-state=running params=uuid | egrep -o "[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}"
so you can create a script to execute the off-line coalescing tool on every VM like this:
#!/bin/bash

MASTER=$(xe pool-list params=master | egrep -o "[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}")
#All VMs
RUNNING_VMS=$(xe vm-list is-control-domain=false params=uuid | egrep -o "[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}")
#All running VMs
#RUNNING_VMS=$(xe vm-list is-control-domain=false power-state=running params=uuid | egrep -o "[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}")
#All halted VMs
#RUNNING_VMS=$(xe vm-list is-control-domain=false power-state=halted  params=uuid | egrep -o "[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}")

for VM in $RUNNING_VMS; do
    echo " "
    echo "=== Starting coalesce leaf process for $VM at $(date) ==="
    echo " "

    xe host-call-plugin host-uuid=$MASTER plugin=coalesce-leaf fn=leaf-coalesce args:vm_uuid=$VM

    echo " "
    echo "=== Coalesce leaf process for $VM ended at $(date) ==="
    echo " "
done
references: http://support.citrix.com/article/CTX123400

Possibly Related Posts

6 comments:

  1. I get an error "VM has no leaf-coalesceable VDIs" when I run the above. I get this error for every VM that is running on my server.

    ReplyDelete
  2. Hello Humanly, that might just mean that there is nothing that needs coalescing...

    Try running the command from this post -> http://sysadminnotebook.blogspot.pt/2012/01/xenserver-list-snapshots.html

    And check if there are more nodes in the VDI tree than VMs + Snapshots.

    ReplyDelete
  3. I found this:
    vhd=VHD-fbdea66b-ac46-4320-a5d9-eed86d1bb48f capacity=171798691840 size=172142624768 hidden=0 parent=VHD-655e6778-9133-41b7-a272-611931811fe9
    vhd=VHD-6701ba00-a716-45d1-bbfc-0b9abfa50e2d capacity=214748364800 size=55050240000 hidden=1 parent=none
    vhd=VHD-9bab1d07-a50a-43be-b150-0f86707724ff capacity=214748364800 size=215176183808 hidden=0 parent=VHD-6701ba00-a716-45d1-bbfc-0b9abfa50e2d
    vhd=VHD-dea7a18f-d553-4716-968a-8665e674e776 capacity=214748364800 size=8388608 hidden=0 parent=VHD-6701ba00-a716-45d1-bbfc-0b9abfa50e2d
    vhd=VHD-c502d44f-8237-4958-9d8f-495586c67f80 capacity=85899345920 size=19146997760 hidden=1 parent=none
    vhd=VHD-360f277f-9d81-42b8-aa25-658c5814f340 capacity=85899345920 size=86075506688 hidden=0 parent=VHD-c502d44f-8237-4958-9d8f-495586c67f80
    vhd=VHD-3d7463e9-364c-4bf8-b094-19800fe44cc1 capacity=85899345920 size=8388608 hidden=0 parent=VHD-c502d44f-8237-4958-9d8f-495586c67f80
    vhd=VHD-0d1e67fa-0fbd-418c-8c73-259ad6ba7228 capacity=432013312 size=440401920 hidden=0 parent=none
    vhd=VHD-58a87f5c-8447-42ef-b6c7-999f8eec5f6d capacity=689963008 size=696254464 hidden=0 parent=none
    vhd=VHD-b9f0e325-842e-4335-a3c5-4c82fd20b5d5 capacity=666894336 size=675282944 hidden=0 parent=none

    This is all right? Or I have to delete them?

    ReplyDelete
  4. This script working only on HALTED vm?

    ReplyDelete
  5. The VMs must at least be suspended but the plugin will take care of that for you.

    ReplyDelete
  6. Hello!
    I get an error "VM has no leaf-coalesceable VDIs" when I run the above.
    From what I understand, is not accomplishing the procedure "coalesce-leaf"
    In the result below shows that a disk has snapshots pending. Has a way of committing these snapshots?

    #for i in `vgs --noheadings -o vg_name`; do vhd-util scan -p -l $i -f 'VHD-*' ; done

    vhd=VHD-864fe00e-0e32-47b9-8ca6-0c1c81e2ff5d capacity=107374182400 size=107495817216 hidden=1 parent=none
    vhd=VHD-ce2ebf4b-c8f3-47e5-87c0-0a2bd028db23 capacity=268435456000 size=218892337152 hidden=1 parent=VHD-864fe00e-0e32-47b9-8ca6-0c1c81e2ff5d
    vhd=VHD-d7393c22-fbed-41a4-aad4-9577511e11b7 capacity=268435456000 size=268968132608 hidden=0 parent=VHD-ce2ebf4b-c8f3-47e5-87c0-0a2bd028db23
    vhd=VHD-0c18e282-f0fa-49db-b1be-a9f3f841fc76 capacity=25769803776 size=25828524032 hidden=0 parent=none
    vhd=VHD-4ca19c35-04fe-4d82-bee8-32b5cb39a779 capacity=42949672960 size=43041947648 hidden=0 parent=none
    vhd=VHD-79e07b06-8778-4416-93b1-9e274bd4c87f capacity=262144000 size=268435456 hidden=0 parent=none
    vhd=VHD-9210ab58-81be-4c3f-a983-6987e69957f0 capacity=161061273600 size=161384235008 hidden=0 parent=none
    vhd=VHD-d13fdb13-cf6c-449d-a875-0439c0e1e9b4 capacity=32212254720 size=32283557888 hidden=0 parent=none

    ReplyDelete