How to Import and Export Virtual Machines in Citrix XenServer

June 17, 2018
How to Import and Export Virtual Machines in Citrix XenServer

“How to Import and Export Virtual Machines in Citrix XenServer” covers the entire process of exporting a virtual machine on a Citrix XenServer installation to a USB flash drive. We will then cover the process of importing the virtual machine into a XenServer installation. This feature of XenServer is extremely valuable as I recently found out. My XenServer was installed on a solid state hard drive (SSD) which, after only 4 short months, began to fail. Every time that I logged into any of my virtual machines hosted on XenServer via SSH, I would be greeted with relentless and unstoppable scrolling messages about bad sectors being found all over the place. The virtual machines hosted on my XenServer are very important and take quite a bit of time to install and configure, so logically I was very concerned about losing all of the Vms and starting over again. Since I never had to export a virtual machine from Citrix XenServer before, I turned to Google to research the topic. To my surprise, the solution was very easy to find. Even more surprising was that the process was neither daunting, nor complicated, but was instead quick and downright simple.

1. In this tutorial, we will be connecting to our XenServer from a Linux machine via SSH. Before beginning the export process, ensure that the virtual machine that you are exporting is powered off. Open a terminal and enter the following command to SSH into your XenServer. Make sure to enter the hostname of your XenServer if different than xenserver.

How to Import and Export Virtual Machines in Citrix XenServer

2. If you have never used SSH to connect to your XenServer before, you will receive a message about the ‘authenticity of host ...’ Enter yes and press the Enter key on your keyboard to continue connecting.

How to Import and Export Virtual Machines in Citrix XenServer

3. You will then be prompted to enter the root password for your XenServer. Enter the root password and press Enter on your keyboard.

How to Import and Export Virtual Machines in Citrix XenServer

4. Once logged into your XenServer via SSH, your prompt should change to your XenServer terminal prompt like this:

How to Import and Export Virtual Machines in Citrix XenServer

5. Now we have to insert our USB flash drive into an empty USB slot on our XenServer. Since my Virtual Machines are Ubuntu 16.04 Server installs and since I am using the thin provisioning feature of XenServer, my virtual machines are relatively small and I can get away with a 32GB flash drive. Just make sure that the flash drive that you choose to hold the exported virtual machines is large enough to contain them or the process will fail.

6. After you have chosen and inserted your USB flash drive into your XenServer, the first thing to do is identify the name of the device file for our flash drive. In your terminal, enter the following command to identify your flash drive:

fdisk -l
How to Import and Export Virtual Machines in Citrix XenServer

7. The output of the command should look similar to the image below.

How to Import and Export Virtual Machines in Citrix XenServer

8. Identify your device from the list. In my case, it is the last device in the list.

How to Import and Export Virtual Machines in Citrix XenServer

9. The next thing that we have to do is create a directory to which we will mount our device. Enter the following command into the terminal:

mkdir /mnt/usb
How to Import and Export Virtual Machines in Citrix XenServer

10. Now that the partition is mounted, we have to format the partition we are using to the EXT4 filesystem. To do this, enter the following into the terminal, replacing sdc1 with the partition you are using on your flash drive:

mkfs -t ext4 /dev/sdc1
How to Import and Export Virtual Machines in Citrix XenServer

11. Next, we will mount our device to the newly created directory. Note that we are mounting a partition of the device, not the entire device file. In my case, you can see from the image in step 8 that the partition that I must mount is /dev/sdc1.

mount /dev/sdc1 /mnt/usb
How to Import and Export Virtual Machines in Citrix XenServer

12. With our flash drive formatted and mounted, we can begin the export process. To view a list of the virtual machines on your XenServer, enter the following command into the terminal:

xe vm-list
How to Import and Export Virtual Machines in Citrix XenServer

13. The output of the command should look similar to the following image. As you can see, I have 4 virtual machines plus the Control domain listed. I will be exporting the virtual machine named “Tutorial_VM”.

How to Import and Export Virtual Machines in Citrix XenServer

14. If you see from the output of the previous command that the virtual machine that you need to export is running, you can shut it down with the following command, replacing <the-vm-name> with the actual name of your virtual machine:

xe vm-shutdown vm=<the-vm-name>
How to Import and Export Virtual Machines in Citrix XenServer

15. The last step to exporting our virtual machine is to enter the following command into the terminal, replacing <the-vm-name> with the actual name of the virtual machine. Make sure to add the complete path to the partition we mounted earlier before the filename in the command:

xe vm-export vm=<the-vm-name> filename=/mnt/usb/<the-vm-name>.xva
How to Import and Export Virtual Machines in Citrix XenServer

16. After entering the previous command and pressing Enter on your keyboard, XenServer will begin exporting your virtual machine to your flash drive. This process will take awhile, especially if your virtual machine is very large. There will be no output from the process until the process completes. When the VM is exported, you will receive a message like the one in the image below:

How to Import and Export Virtual Machines in Citrix XenServer

17. To verify that your virtual machine has been successfully exported to your flash drive, enter the following command:

ls /mnt/usb
How to Import and Export Virtual Machines in Citrix XenServer

18. You should see your virtual machine in the output of the previous command.

How to Import and Export Virtual Machines in Citrix XenServer

19. You can now use the following command to unmount your flash drive before removing it.

umount /mnt/usb
How to Import and Export Virtual Machines in Citrix XenServer

20. You can now safely remove your flash drive.

21. To import your virtual machine into XenServer, simply locate and mount your flash drive’s partition containing you virtual machine to a directory located at mnt/usb like in the export steps. Then, only one command is needed to import you virtual machine. Enter the following into the terminal to import your virtual machine, replacing <the-vm-name> with the actual name of your virtual machine:

xe vm-import vm=<the-vm-name> filename=/mnt/usb/<the-vm-name>.xva
How to Import and Export Virtual Machines in Citrix XenServer

That’s all there is to exporting and importing virtual machines with Citrix XenServer. As you can see, it is a quick and painless procedure and an incredibly valuable feature of XenServer.