Snapshotting with the XenServer 5.0 SDK

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!

8 Responses to Snapshotting with the XenServer 5.0 SDK

  1. Many thanks Shannon for sharing this code.

    Best Regards,

    Pierre

  2. Shannon O'Shea says:

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

  3. Richard Nash says:

    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!

  4. Kitson says:

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

  5. Cyrus says:

    I would love to see the PowerShell cmdlets!~

  6. Pingback: Hot Off the Compiler: PowerShell SnapIn for XenServer Snapshots « Shannon Ma Virtualized

  7. Pedro Faria says:

    That would be great to have those PowerShell cmlets.

    keep up the good work.

    thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.