04 August, 2010

Automatic script for Xen Coalesce (gain space back after using snapshots)

If you are using snapshots in XenServer you may noticed a spaced beed used on the storage that you cannot explained.
This is due to using snapshots. After you create the initial snapshot xen will continue writing to it even if it is not there.
To reclaim the space the VM's VDI need to go a proccess named "Coalesce" which integrates the data from the snapshot into the original VDI.

I used article CTX123400 to base my script on.
Also you will need to download "plink".

3 files:
  1. VM_List.txt - a list of the machine names (case sensitive)
  2. Step1.cmd
  3. Step2.cmd

Step1.cmd: replace [] with your own data
==========================
echo off
set user=[user]
set password=[password]
set server=[server ip]
del log_Coalesce.txt /q
for /f %%A in (vm_list.txt) do (step2.cmd %%A >>log_Coalesce.txt)
==========================

Step2.cmd:
==========================
title ***** Working on %1 *****
echo ***** Start working on %1 *****
echo.echo Start at:
echo %date% - %time%
set vm_uuid=
"C:\Program Files\Citrix\XenCenter\xe.exe" -s %server% -u %user% -pw %Password% vm-list name-label="%1" --minimal > vm_uuid.txt
set /p vm_uuid=

plink -ssh %server% -l %user% -pw %password% coalesce-leaf -u %vm_uuid%
echo.
echo %date% - %time%
echo ***** End working on %1 *****
echo.
============================


Enjoy

2 comments:

  1. I would change step2.cmd to look like this

    @echo off
    title ***** Working on %1 *****
    echo ***** Start working on %1 *****
    echo Start at: %date% - %time%
    echo.
    "C:\Program Files\Citrix\XenCenter\xe.exe" -s %server% -u %user% -pw %Password% vm-list name-label="%1" --minimal > vm_uuid.txt
    for /f %%A in (vm_uuid.txt) do (set vm_uuid=%%A)
    plink -ssh %server% -l %user% -pw %password% coalesce-leaf -u %vm_uuid%
    echo.
    echo.
    echo End at: %date% - %time%
    echo ***** End working on %1 *****
    echo.
    echo -----------------------------------------------------------------------------------------------------
    echo.

    ReplyDelete
  2. #!/usr/bin/env bash
    master=`xe pool-list |grep "master" | python -c "print raw_input().split(':')[-1].strip()"`
    array=$(xe vm-list --minimal | tr "," "\n")
    for vm in $array
    do
    xe host-call-plugin host-uuid=$master plugin=coalesce-leaf fn=leaf-coalesce args:vm_uuid=$vm
    done

    ReplyDelete