Wednesday, May 2, 2012

How to Rename a Virtual Machine with the CLI in esxi 5.0

With the release of esxi/vsphere 5, storage vmotion no longer renames the files of a vm if the vm was previously renamed. It only updates the folder to reflect the new name. If a person wants to rename a virtual machine and make sure that the virtual machine reflects the new name in the folder and files, follow these steps.

Warning: Try this on a non-production environment. We are not talking about what is supported, just what works. Also, be aware that you may have more files involved, such as an rdm, snapshot and so forth. Proceed at your own risk.

Step # 1:

Access the directory that contains the folder with the files. In this case, the vm is called machinex and it is located in datastore1. The vm needs to be powered off.

# cd /vmfs/volumes/datastore1

Step # 2:

Rename the folder

# mv machinex machiney

Step # 3:

Access the new directory

# cd machiney

Step # 4:

Rename all the files manually

# mv machinex.vmx machiney.vmx
# mv machinex.vmsd machiney.vmsd
# mv machinex.nvram machiney.nvram
# mv machinex.vmdk machiney.vmdk
# mv machinex-flat.vmdk machiney-flat.vmdk
# mv machinex.vmxf machiney.vmxf

Note: In this case, there were no snapshots so the .vmsd file was empty.

Step # 5:

Edit the contents of the .vmx file using vi. Use a global search and replace.

# vi machiney.vmx
:%s/machinex/machiney/g

Step # 6:

Edit the contents of the .vmdk file

# vi machiney.vmdk
:%s/machinex/machiney/g

Step # 7:

Get the vmid of the original vm and unregister the vm
 
# vim-cmd vmsvc/getallvms
# vim-cmd vmsvc/unregister 5

Step # 8:

Register the new virtual machine

# vim-cmd solo/registervm /vmfs/volumes/datastore1/machiney/machiney.vmx


Note: This was eventually taken care off in 5.0 u2 and 5.1 u1:

If using those versions, this can be taken care of via the vsphere client.
There is a parameter called provisioning.relocate.enableRename which is set to false. Just change it to true and restart the vcenter service.

Here are the steps:
  1. Log into the vSphere Client as an Administrator.
  2. Click Administration > vCenter Server Settings.
  3. Click Advanced Settings.
  4. Add this advanced parameter Key:

    provisioning.relocate.enableRename=true