Shannon Ma Virtualized

A Spotlight on Virtualization, IT, and the Business of Software

Hot Off the Compiler: PowerShell SnapIn for XenServer Snapshots

with 34 comments

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!

Written by shannonma

November 6, 2008 at 12:20 am

34 Responses

Subscribe to comments with RSS.

  1. 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

    Franco

    November 6, 2008 at 2:38 pm

  2. 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

    shannonma

    November 6, 2008 at 3:29 pm

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

    Joe

    Joe Shonk

    November 12, 2008 at 5:28 pm

  4. 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

    shannonma

    November 13, 2008 at 9:16 pm

  5. [...] for XenServer. This is a one-to-one mapping of the existing XenServer API. The functionality in XenServerSnapshotPSSnapIn can be replicated with this, however it will take many lines of scripting. I recommend using [...]

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

    Thanks!

    Richard Nash

    November 25, 2008 at 9:18 pm

  7. 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?

    david

    November 26, 2008 at 1:56 pm

  8. 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!

    shannonma

    November 26, 2008 at 2:19 pm

  9. 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

    Cem

    March 8, 2009 at 3:41 pm

    • Yeah it’s possible, however your XenServer needs to be configured for VHD-on-EXT3 storage. You can find instructions for this at http://support.citrix.com/article/ctx116324. Also, the 2nd comment in this post includes additional info on this.

      shannonma

      March 8, 2009 at 11:06 pm

  10. 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?

    Boeri

    March 9, 2009 at 1:31 pm

  11. 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’ ?

    Cem

    March 9, 2009 at 2:04 pm

  12. 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

    Boeri

    March 11, 2009 at 4:02 pm

  13. Hello:

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

    Ron Kraft

    March 21, 2009 at 5:18 pm

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

      shannonma

      March 25, 2009 at 10:59 pm

  14. Hey Shannon,

    This works perfectly, Thanks!

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

    Thanks,
    Julian

    Julian

    March 29, 2009 at 12:15 am

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

      shannonma

      March 29, 2009 at 11:04 am

  15. 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?

    Simon Gadsby

    March 30, 2009 at 11:40 am

    • Yeah, I was referring to non-shared storage.

      shannonma

      March 30, 2009 at 12:14 pm

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

    Stig

    April 2, 2009 at 10:24 am

  17. 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…

    Franco

    April 10, 2009 at 4:04 pm

  18. 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

    Franco

    April 10, 2009 at 5:20 pm

  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).

    Roger Klorese

    May 12, 2009 at 5:13 pm

    • 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

      Reinhart De Bock

      June 5, 2009 at 4:52 pm

      • 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().

        shannonma

        June 6, 2009 at 1:02 pm

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

    Rene Vester

    lamersmurf

    June 2, 2009 at 7:22 am

  21. 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

    reinhart

    June 8, 2009 at 12:16 pm

    • 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.

      shannonma

      June 8, 2009 at 12:26 pm

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

    SU

    August 12, 2009 at 10:43 am

  23. Hi,

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

    Best regards,

    Ruud.

    Ruud Boersma

    September 1, 2009 at 2:06 pm


Leave a Reply