Hot Off the Compiler: PowerShell SnapIn for XenServer Snapshots

As a follow-up to my Snapshotting with the XenServer 5.0 SDK post, I’ve written a PowerShell snapin that will allow you to take and revert snapshots with XenServer. You can download the source code and binaries here. You must have the .NET Framework 3.5 installed before you can use it. To install the snapin you will need to run the following command after you’ve downloaded and extracted the ZIP file:

C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /i XenServerSnapshotPSSnapIn.dll

Then you can open up your PowerShell console and enter the following to take a snapshot of your virtual machine named Bluemoon, which by the way is my favorite beer!

Add-PSSnapin XenServerSnapshot*
Checkpoint-VM -Hostname 192.168.0.2 -Username root -Password qwerty -VM Bluemoon -Snapshot BluemoonBackup

If you decide you’d like to revert to this snapshot you can do this:

Restore-VM -Hostname 192.168.0.2 -Username root -Password qwerty -VM Bluemoon -Snapshot BluemoonBackup

And before I leave the office for the night, here are a few things to note:

  • The cmdlets return true if they were able to execute successfully, otherwise they return false.
  • Virtual machine names are case-sensitive.
  • PowerShell must be installed to compile the source. XenAPI.net is included with the download.
  • I’ve made a few tweaks to this code from the original version.

Added 09-March-2009: The snapshot feature in XenServer requires a VHD-on-EXT3 storage repository.  To change the default repository to EXT3, please see http://support.citrix.com/article/ctx116324.  Alternatively, the following script can be used:

dev=`cat /etc/xensource-inventory | grep DEFAULT_SR_PHYSDEVS | cut -d ‘=’ -f2 | grep -o -E “[^'].*[^']“`

sr_uuid=`xe sr-list type=lvm | grep uuid | cut -d ‘:’ -f2 | cut -c 2-`

pbd_uuid=`xe pbd-list sr-uuid=$sr_uuid | grep -E ‘^uuid.*’ | cut -d ‘:’ -f2 | cut -c 2-`

inst_uuid=`cat /etc/xensource-inventory | grep INSTALLATION_UUID | cut -d ‘=’ -f2 | grep -o -E “[^'].*[^']“`

xe pbd-unplug uuid=$pbd_uuid

xe sr-destroy uuid=$sr_uuid

new_sr_uuid=`xe sr-create content-type=”local SR” host-uuid=$inst_uuid type=ext device-config-device=$dev shared=false name-label=”Local File SR”`

pool_id=`xe pool-list | grep uuid | cut -d ‘:’ -f2 | cut -c 2-`

xe pool-param-set default-SR=$new_sr_uuid uuid=$pool_id

xe pool-param-set suspend-image-SR=$new_sr_uuid uuid=$pool_id

#echo $dev
#echo $sr_uuid
#echo $pbd_uuid
#echo $inst_uuid
#echo $new_sr_uuid
#echo $pool_id

Feel free to post a comment here if you have any feedback. Enjoy!

36 Responses to Hot Off the Compiler: PowerShell SnapIn for XenServer Snapshots

  1. Franco says:

    Hi, i’m trying to use your powershell snap-in.
    I’ve followed your istruction, on my Windows XP i,ve installed .net framework 3.5 SP1, PowerShell 1.0
    When i try to run the script i receive the following error :

    Checkpoint-VM : SR_OPERATION_NOT_SUPPORTED At line:1 char:14 + Checkpoint-VM <<<< -Hostname 192.168.2.8 -Username root -Password xxxxxxx -VM inventory -Snapshot inventoryBackup

    What am i doing of wrong ?

    thanks

  2. shannonma says:

    Forgot to mention the snapshot feature requires a VHD-on-EXT3 storage repository. http://support.citrix.com/article/ctx116324 talks about how you can convert your storage repository to this format. Alternatively you can run this script:

    dev=`cat /etc/xensource-inventory | grep DEFAULT_SR_PHYSDEVS | cut -d ‘=’ -f2 | grep -o -E “[^'].*[^']“`

    sr_uuid=`xe sr-list type=lvm | grep uuid | cut -d ‘:’ -f2 | cut -c 2-`

    pbd_uuid=`xe pbd-list sr-uuid=$sr_uuid | grep -E ‘^uuid.*’ | cut -d ‘:’ -f2 | cut -c 2-`

    inst_uuid=`cat /etc/xensource-inventory | grep INSTALLATION_UUID | cut -d ‘=’ -f2 | grep -o -E “[^'].*[^']“`

    xe pbd-unplug uuid=$pbd_uuid

    xe sr-destroy uuid=$sr_uuid

    new_sr_uuid=`xe sr-create content-type=”local SR” host-uuid=$inst_uuid type=ext device-config-device=$dev shared=false name-label=”Local File SR”`

    pool_id=`xe pool-list | grep uuid | cut -d ‘:’ -f2 | cut -c 2-`

    xe pool-param-set default-SR=$new_sr_uuid uuid=$pool_id

    xe pool-param-set suspend-image-SR=$new_sr_uuid uuid=$pool_id

    #echo $dev
    #echo $sr_uuid
    #echo $pbd_uuid
    #echo $inst_uuid
    #echo $new_sr_uuid
    #echo $pool_id

  3. Joe Shonk says:

    Will this Quiesce the OS and Applications (invoke VSS)? If not, how is he integrity of the OS and Application Data insured?

    Joe

  4. shannonma says:

    Hi Joe,

    In XenServer 5.0 there is an experimental VM.snapshot_with_quiesce API that uses VSS to take care of this. However, please note it is not production ready and has some known bugs.

    The Checkpoint-VM cmdlet uses the VM.snapshot API, which does not quiesce anything. To guarantee the integrity of your data you should shut down the virtual machine before calling this cmdlet or API.

    Please let me know if you have any other questions.

    Shannon

  5. Pingback: XenServer 5.0 SDK Available for Download « Shannon Ma Virtualized

  6. Richard Nash says:

    Shannon, you da man! This works like a charm and will really make my life easier.

    Thanks!

  7. david says:

    Shannon, Thanks this is great – I have been waiting for snapshot functionality in XenServer for a long time.
    What about deleting snapshots? Do you just delete the template in XenCentre?

  8. shannonma says:

    That’s correct, you can delete the snapshot by just deleting the template via XenCenter and telling it to delete all associated disks. Glad to hear the feedback!

  9. Cem says:

    Hi,

    Interesting article, i’ve tried to do a snapshot of a VDI or a VM; both come with the error :

    The SR backend does not support the operation (check the SR’s allowed operations)

    In the ‘allowed operations’ list, i have :

    # xe vdi-param-list uuid=…
    allowed-operations (SRO): clone; destroy; resize

    same for vm :

    # xe vm-param-list uuid=…
    allowed-operations (SRO): snapshot; copy; clone; start; start_on; export; destroy; make_into_template

    I’m not using any NetApp or EquaLogic …

    Question is : is snapshot possible without a very expensive storage solution ?

    Any help would be ‘greatly’ appreciated.

    Thx

  10. Boeri says:

    Is it possible to change a iSCSI SR (LVM) to File-based VHD-on-EXT3 ? Or do I have to use LVM based snapshots with some scripting?

  11. Cem says:

    Just tried to create a vhd-ext3 storage; both vdi-snapshot and vm-snapshot works, just GREAT !

    Now, they don’t show in ‘vm-list’ ?

  12. Boeri says:

    It’s not very clear yet..
    I want to create a vhd-ext3 storage on a shared iscsi target (Openfiler).

    Can I just replace the type=lvmoiscsi with type=ext in the command:

    xe sr-create host-uuid= content-type=user \
    name-label= shared=true \
    device-config:target= device-config:targetIQN= \
    device-config:SCSIid= \
    type=lvmoiscsi

    sorry for the confusion

  13. Ron Kraft says:

    Hello:

    I keep receiving ckpoint-VM : HOST_IS_SLAVE. I only have two servers in my pool. Any ideas on how to correct?

    • shannonma says:

      It sounds like the Hostname parameter isn’t pointing to the Resource Pool master, I’d recommend giving this a try.

  14. Julian says:

    Hey Shannon,

    This works perfectly, Thanks!

    Quick question, is there a big performance hit by using local EST3 instead of local LVM?

    Thanks,
    Julian

    • shannonma says:

      This is purely based on observation, but in my experience an EXT3 based XenServer seems more responsive.

  15. Simon Gadsby says:

    You wrote:
    > “the snapshot feature requires a VHD-on-EXT3 storage repository.”

    I assume you are referring to non-shared storage? With Netapp for example I think you can create snapshots with the Netapp SR type?

  16. Stig says:

    Don’t forget Powershell 1.0.. we did..

  17. Franco says:

    Where do we Run the Script for converting to EXT3? Sorry I never ran a script on my Xenservers so not sure what steps I need to do… Do I have to save it as a VBS? Where does it run?

    THanks…

  18. Franco says:

    I have converted the local SR to EXT3 that works great…
    but my environment has a Shared storage on ISCSI hosting all my VMs…

    I receive the error SR_Operation_Not_Supported

    Does this work? Taking a snapshot of a vm on a Shared ISCSI storage to the local EXT3 SR?

    Thanks,
    FB

  19. Note that starting with XenServer 5.5, now in beta, snapshots are supported on all SR types, local (lvm and ext) AND networked (nfs, iSCSI, FC, and vendor-specific).

  20. Pingback: XenServer Snapshots [feedly] « Wag the Real

    • Reinhart De Bock says:

      Hey guys,
      Verry nice work!

      I have a question.
      Can i use a powershell var for the name of the snapshot?
      An example:

      Add-PSSnapin XenserverSnapshot*
      $date= get-date
      Checkpoint-VM -Hostname 15.0.0.10 -Username root -Password none -VM Debock-dc1 -Snapshot $date

      Maybe my syntax is wrong, but this doesnt work

      Kind Regards

      Reinhart

      • shannonma says:

        Yeah, you can use a PowerShell variable but it needs to be a string. Get-Date actually returns a datetime object. To make it a string just do $date = (Get-Date).ToString().

  21. lamersmurf says:

    Will the same powershell scripts work with 5.5 on all SR types? :-)

    Rene Vester

  22. reinhart says:

    Hey guys,

    Is there a way that i can restore a single drive snapshot? So i got a vm with c and d partition, both are snapshotted at the same time.
    Now i wanna restore a single drive, is this possible?

    Kind Regards

    Reinhart

    • shannonma says:

      The current set of cmdlets don’t allow you to do this. If the C and D partitions are part of the same disk, I don’t think this is possible. However, if they’re on seperate disk images you could probably modify the source code to handle this. I’ll keep the second case in mind as an enhancement.

  23. SU says:

    GR8, this works like a charm….thanks a lot

  24. Ruud Boersma says:

    Hi,

    Any idea why revert-vm or restore-vm are unknown. All other commandlets seem to response.

    Best regards,

    Ruud.

  25. Franco says:

    XenServer Midnight ride has just been released to beta which includes the snapshot revert functions….

    You made a great script but I’ll wait for mid oght ride…

    Fb out…

  26. Pingback: Confluence: ITS-ACAD-SCAN: Student Computing & Networking Team

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>