Jump to content


Photo

A neat way to export VMs from Linux

Started by Richard Mosquera , 22 October 2010 - 11:37 AM
9 replies to this topic

Richard Mosquera Members

Richard Mosquera
  • 23 posts

Posted 22 October 2010 - 11:37 AM

I've been running into issues exporting VMs from XenCenter - either it fails and disconnects during the export, or has "block checksum errors". Sometimes I'm exporting a huge VM that I want to save somewhere other than my desktop, perhaps another server.

Firstly I'd like to make a feature request: that the xe vm-export or template-export commands allow you to export to stdout - this would make them much more unix-like - you could pipe VM exports into scripts, network sockets, whatever - with some ssh trickery you could transfer VMs between pools by chaining vm-export on one pool with vm-import on the other pool. Lots of possibilities.

I thought I'd share a trick which I've found really useful, using sshfs. For those not familiar, sshfs basically allows you to remotely mount a directory on any remote machine that you can ssh to. However, we want to do the opposite: mount a directory that lives on your local machine, on XenServer.

For example, on your generic Linux server (e.g. 'alice') you have 1TB of storage for VM templates mounted on /export/vm. You want to download a template you've just created on your Xen server (e.g. 'bob') to that server.

Firstly, we need to install fuse and sshfs on XenServer. You need 3 RPM packages:

fuse-libs and fuse - get them off the CentOS 5 DVD or download:
http://rpm.pbone.net/index.php3/stat/4/idpl/13941321/dir/centos_5/com/fuse-libs-2.7.4-8.el5.i386.rpm.html
http://rpm.pbone.net/index.php3/stat/4/idpl/13941319/dir/centos_5/com/fuse-2.7.4-8.el5.i386.rpm.html

sshfs - download:
http://rpm.pbone.net/index.php3/stat/4/idpl/12948308/dir/redhat_el_5/com/fuse-sshfs-2.2-5.el5.i386.rpm.html

Install all 3 packages on your XenServer.

You're ready to go - first ssh from alice to bob, setting up a reverse tunnel on port 2222 back to the ssh server on alice.
*user@alice:/export/vm$* ssh -R 2222:localhost:22 root@bob
root@bob's password:
root@bob ~#

Create a new mountpoint on bob for this:
*root@bob ~#* mkdir /mnt/sshfs

Now use sshfs to mount alice's /export/vm directory on /mnt/sshfs.
*root@bob ~#* sshfs -p 2222 user@localhost:/export/vm /mnt/sshfs
user@localhost's password:
*root@bob ~#*

All done - you should be able to cd to /mnt/sshfs, run ls, and see the contents of alice's /export/vm directory.

At this point you are free to run commands like:

*root@bob ~#* xe template-export template-uuid=870aa6ad-172f-0432-65b6-214c6c9cdb32 filename=/mnt/sshfs/rhel55_x86_64_v3.xva

When you're done just umount /mnt/sshfs, and logout.



Alex Brett Members

Alex Brett
  • 22 posts

Posted 05 November 2010 - 03:04 PM

This is already possible - for export just leave the filename parameter blank and it will go to stdout, e.g.
xe vm-export uuid=<vm_uuid> filename=

As for import, if you specify the filename as "/dev/stdin" then it will read from stdin, e.g.:
xe vm-import filename=/dev/stdin

You can test this easily by doing:
xe vm-export uuid=<vm_uuid> filename= | xe vm-import filename=/dev/stdin

That should end up effectively copying the VM by exporting and reimporting it.



Tobias Kreidl Members

Tobias Kreidl
  • 14,536 posts

Posted 05 November 2010 - 03:07 PM

Exactly, it can then be piped or redirected, similar to what's possible with Linux "dump" commands, cpio, dd, etc.
--Tobias



Alejandro Gallinato Members

Alejandro Gallinato
  • 1 posts

Posted 11 November 2010 - 04:43 AM

Alex, the VM export and import from pipe works fine, I have been looking for this trick over 2 years, thanks.



Jonathan Kinney Members

Jonathan Kinney
  • 2 posts

Posted 22 June 2012 - 04:35 AM

Ok, we have this going for us:

xe vm-export vm=somevm1 filename= | ssh xenserver2.exampleserver.org "dd of=/mnt/transfertemp/somevm1.xva"

This is neat and all, but does anyone out there have a way to directly import it on the destination, rather than going to a file? Don't get me wrong, hours are saved this way, but is there something better?



Tobias Kreidl Members

Tobias Kreidl
  • 14,536 posts

Posted 22 June 2012 - 05:16 AM

The whole package developed to use snapshots and this technique for backup and restore is available _here_.
You typically want to put it somewhere, as a backup. Otherwise, you can forget and move VDIs and then introduce them to the same VM if you want to move the VM's VDIs more directly to a different SR.

Also, given the potential risk of corruption during the copying process, it seems it would be taking a risk to assume it's going to copy over flawlessly to a new destination.

--Tobias

Edited by: Tobias Kreidl on Jun 21, 2012 10:18 PM



Jonathan Kinney Members

Jonathan Kinney
  • 2 posts

Posted 22 June 2012 - 09:08 PM

Ok, I relent, take it from the guy with nearly 7K posts here, pipe over network, not such a good idea for data integrity, I just ran into an md5sum mismatch. I have however not ran into corruption before when using sshfs, so maybe I will look at trying that. There is something to be said for having the transfer running parallel with the dump, such as ~20 minutes for a ~20GB vm (depending on resources of course).



Tobias Kreidl Members

Tobias Kreidl
  • 14,536 posts

Posted 22 June 2012 - 09:52 PM

Certainly. if you could check the integrity of the transfer (e.g., as you suggested, sshfs) there would be some assurance of whether the copy completed successfully and without error. Of course, there would then have to be some cleaning up done to get rid of any stale remnants in the event of errors. The encryption overhead is probably only a few percent, but having the peace of mind of a successful transfer is priceless (sounds like a credit card ad, eh?).
--Tobias



Jeremy Blow Members

Jeremy Blow
  • 4 posts

Posted 31 August 2012 - 08:22 AM


For those looking to pipe directly to vm-import over ssh instead of writing to a file first, you can do:

xe vm-export vm=vmname filename= | ssh root@server2 'cat - | xe vm-import filename=/dev/stdin'

ssh will give you the integrity checking you need - Tobias is correct, adds very little overhead. But, if you really want to max it out you can use arcfour128 for the ssh cypher:

xe vm-export vm=vmname filename= | ssh -c arcfour128 root@server2 'cat - | xe vm-import filename=/dev/stdin'

When the import completes you may receive a message "Error code: CLIENT_ERROR", I think the message is mostly harmless, most likely a result of vm-import not having this as a use case.

Enjoy!

Jeremy



Jeremy Blow Members
  • #10

Jeremy Blow
  • 4 posts

Posted 29 July 2014 - 04:26 AM

Just a follow-up for those having difficulty w/ XS 6.2 allowing /dev/stdin as a filename for vm-import. You can also import a xva remotely by using the xe executable on a remote system. In my case, that meant scp'ing /opt/xensource/bin/xe to the system holding the xva's (Ubuntu 12.04 LTS/x86_64), installing stunnel (sudo apt-get install stunnel), and running xe w/ remote xen host parameters. E.g. ./xe vm-import filename=/dir/filename.xva -h 1.2.3.4 -u user -pw pass. Hope this helps.