Shannon Ma Virtualized

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

Snapshotting with the XenServer 5.0 SDK

with 8 comments

Throughout the past couple of months at Citrix, I’ve been working on our next-generation automation framework: CADI (Citrix Automation Development Infrastructure). This framework empowers our developers to easily create and run automated system tests, which will ultimately result in higher quality products. Just about all of the framework’s functionality is exposed via PowerShell, so for instance we can setup a XenServer virtual machine with the latest XenApp build and launch an ICA session from a remote client to the XenApp server right from the PowerShell CLI.

During this project, I was responsible for driving the design and development of the test environment setup feature. Its main role is to give users the ability to provision machines, install software, and configure them. The automated tests would then take over, try to find bugs in our software, and report their results.

Coming into the project, we knew we wanted to use our lab hardware in the most efficient way and have the ability to take a snapshot of a machine in case we needed to see why a test failed. To achieve this we leveraged XenServer 5.0 whose SDK supports taking and reverting snapshots. This snapshot functionality is not currently available via XenCenter, however for those of your interested I’ve posted the C# take and revert snapshot code here. Please note XenAPI.net, which is released as part of the XenServer 5.0 SDK, is required to compile this.

The TakeSnapshot method is pretty straight forward. It will connect up to the XenServer host, retrieve the specified virtual machine, and call the VM.snapshot method on it. Once this has executed, you will see the snapshot appear as a template on your XenServer.

The RevertSnapshot code on the other hand isn’t very straight forward; however no worries here as I’ll walk through it. The code starts off by connecting to the XenServer host and retrieving the specified virtual machine. Then it does the following:

  1. Retrieve the snapshot we’re interested in. Remember, the snapshots are templates stored on the XenServer and they are linked to their virtual machines. The VM.get_snapshots method allows us to retrieve a virtual machine’s snapshots.
  2. Shutdown the virtual machine we are reverting. This is required as we will be modifying the virtual machine.
  3. Delete all VDIs (virtual disk images), except CDs since they can’t be deleted, and VBDs (virtual block drives) associated with the virtual machine. A VDI is synonymous with a hard drive while a VBD is the component that connects the VDI to the virtual machine.
  4. Clone the VDIs from the snapshot and connect them to the virtual machine.
  5. Power on the virtual machine.

I’ve been thinking about writing a couple PowerShell cmdlets for taking and reverting snapshots. If I receive enough interest, I’ll probably write them up and post them here. So if you find this post valuable feel free to leave a message. Till next time!

Written by shannonma

September 29, 2008 at 5:37 am

Posted in Engineering, Virtualization

Tagged with

8 Responses

Subscribe to comments with RSS.

  1. Many thanks Shannon for sharing this code.

    Best Regards,

    Pierre

    Pierre Marmignon

    October 2, 2008 at 12:09 am

  2. Great information! This would be very useful for doing deployments, both in the development stages and during rollout.

    Shannon O'Shea

    October 16, 2008 at 2:04 pm

  3. This is great stuff. And, yes, I’d love it if you would write a couple of PowerShell cmdlets for taking and reverting snapshots. Thanks!

    Richard Nash

    October 16, 2008 at 6:21 pm

  4. Look forward to testing this. I would like to see the PowerShell cmdlets too. Thanks for sharing!

    Kitson

    October 28, 2008 at 7:33 pm

  5. I would love to see the PowerShell cmdlets!~

    Cyrus

    November 5, 2008 at 11:39 pm

  6. [...] 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 [...]

  7. That would be great to have those PowerShell cmlets.

    keep up the good work.

    thanks.

    Pedro Faria

    April 21, 2009 at 2:50 pm


Leave a Reply