How to Export/Import Virtual Machine On ESX Server

We recently updated our hardware (old ESX server) and had to export and import the virtual machines to the new server

In achieving this goal, we had to use an external USB drive to backup and transfer VMDK files from an old ESX server to the new one.

Requirements:

  • External USB drive with enough capacity
  • The drive has to be formated (preferably ext3 or ext4)
  • Coffee, cigarett or anything but no alcohol 😉

Format and Mount the Drive:

 Steps:

  • Connect the external drive to the ESX server
  • Login to the server console using ssh connection (e.g putty)
  • To find out where the device is connected, enter “fdisk -l” (without the quotes ” “)
  1. fdisk -l

 

 

 

 

Note: As seen above, a partition has been created and only need to be formated

  • Format the new drive by typing these commands

  mkfs.ext3 -m 1 /dev/sdb1

 e2label /dev/sdb1 /ExtUSB (optional)

  where sdb1 is the partition shown above and 1 for 1% of the partition for administrative purposes

  for more information, type man mkfs.ext3   

  • Create a folder (mount point) where you want to mount the new drive (Example: mkdir /ExtUSB)
  • mount -t ext3 /dev/sdb1 /ExtUSB

  To make this permanet, type vi /etc/fstab and enter:

  LABEL=/ExtUSB1  /ExtUSB   ext3   defaults        0 0

  Save the file (type  :wq and press ENTER)

  • mount the partition

  mount -a

  • Check the to see that the new partition is mounted as expected by typing

  mount

Export the VM files and Logs to the external USB drive:

Vmktools is the VMware ESX Server file system management tool we used for this task. It is a program for creating and manipulating virtual disks, files systems, logical volumes and physical storage devices on the VMware ESX Server. To export or import VMDK (virtual disk) files, the command to use is ‘vmkstools’.

Export:

  • Enter the following comands as it is to export the VMDK files

  vmkfstools -i /vmfs/volumes/[datastore_name]/[vm_name]/[vm_name].vmdk /mnt/usb/[vm_name].vmdk -d 2gbsparse

  • To also export the configuration files for the virtual machine, just copy all those files with the following extentions: *.vmx, /.log, *.nvram, *.vmxf and *.vmsd. The best way to do this, is to first zip the files in tar.gz format and then copy to the external USB drive as follows:

  cd /vmfs/volumes/[datastore_name]/[vm_name]/

  tar -czvf [vm_name].tar.gz *.vmx *.log *.nvram *.vmxf *.vmsd

  cp [vm_name].tar.gz /mnt/usb/

Import:

Prior to importing the exported files, make sure you have created a new virtual machine (name it whatever you want). The VMDK and other files in the newly created virtual machine must be deleted to avoid conflicts with the ones to be imported. Take a look at these images to get an idea where to find these files:

Browse Data Storage

Browse the Data Storage

Delete VMDK Files

Delete all files from the VM folder as shown above

  • Enter the following comands to import the VMDK files

 vmkfstools -i /[path_to_USB_mount]/[vm_name].vmdk /vmfs/volumes/[datastore_name]/[vm_name]/[vm_name].vmdk

  • To also import the configuration files for the virtual machine, first extract [vm_name].tar.gz file to a folder (should be created first) and just copy all those files with the following extentions: *.vmx, /.log, *.nvram, *.vmxf and *.vmsd to /vmfs/volumes/[datastore_name]/[vm_name]/. The following comands should take care of this task:

  cd ~ (change to home directory)

  mkdir VMDK-Files

  cp /ExtUSB/[vm_name].tar.gz VMK-Files/

  cd VMDK-Files/

 tar -xzvf [vm_name].tar.gz

  rm -rf [vm_name].tar.gz

    cp * /vmfs/volumes/[datastore_name]/[vm_name]/

You can check to see if all files are imported as shown below:

Imported VMDK Files

Now, go ahead and start your VM machine. If carefully done, it should work as expected.

Related Site:

http://www.vladan.fr/


Posted in ESX, ESXi, Linux, VMware and tagged , , , by with comments disabled.