Renaming a virtual machine and its files

When I was renaming some virtual machines in my test lab, I discovered that the file names are not renamed with the virtual machine. In the future this may be causing some problems so I was looking for a method to rename the files also. On the VMware site I’ve found a knowledgebase article about it. It describes the correct way to rename also the files. See KB Article: 1029513. Original link: http://kb.vmware.com/selfservice/search.do?cmd=displayKC&docType=kc&externalId=1029513

Purpose

This article provides steps to rename a virtual machine and its files.
These steps may be useful if you rename a virtual machine, but its files retain the original file names. You may want to rename the virtual machine disk files to prevent possible confusion.

Resolution

The content of the displayName configuration option is updated when you rename a virtual machine. However, the underlying files are not renamed.

Renaming the files

To rename the files:
  1. Log into the VMware vSphere Client.
  2. Locate the virtual machine in your host inventory.
  3. Begin a Storage vMotion or an offline Storage Migration of the virtual machine.For more information, see :
  4. The destination copy’s file names are be updated to your desired values.

If this is not an option, you may also rename the files by hand after the virtual machine has been powered down.

Manually renaming virtual machine files

Warning: Before proceeding, ensure that:

  • The virtual machine has a current backup and that it has been powered down.
  • The virtual machine does not have snapshots or virtual disks shared with other virtual machines.

To manually rename the virtual machine’s files:

  1. Log into the VMware vSphere Client.
  2. Locate the virtual machine in your host inventory.
  3. Power down the virtual machine.
  4. Right-click on the virtual machine and choose Remove from inventory.
  5. Connect to the terminal of the ESX server via SSH, System Management Interface, or directly at its console, and log in.Note: For additional instructions for ESXi, see Tech Support Mode for Emergency Support (1003677).
  6. Navigate to the directory containing the virtual machine. For example, cd /vmfs/volumes/<datastore>/<virtual machine>/.
  7. Run this command to rename the virtual disk files:vmkfstools -E “originalname.vmdk” “newname.vmdk”Note: Is it unnecessary to once again rename the originalname-flat.vmdk file after running the vmkfstools command.
  8. Copy the virtual machine configuration file (.vmx) using the command:cp “originalname.vmx” “newname.vmx”
  9. Edit the copied configuration file, such as (newname.vmx), using the vi editor:vi “newname.vmx”Note: For VMware ESX hosts, the nano editor is also available. If you are uncomfortable using the vi editor, seek assistance from a Linux/Unix administrator or file a Support Request and contact VMware Technical Support.
  10. Within the configuration file, modify all old instances of the virtual machine’s file names to the new file names. There should be at least the following to adjust:nvram = “originalname.nvram”
    displayName = “originalname”
    extendedConfigFile = “originalname.vmxf”
    scsi0:0.fileName = “originalname.vmdk”
    […]
    migrate.hostlog = “./originalname-UUID.hlog”

    Repeat this process for each virtual machine disk. Such as:

    scsi0:1.fileName = “originalname_1.vmdk”
    scsi0:2.fileName = “originalname_2.vmdk”

    Correct the VMkernel swap file reference:

    sched.swap.derivedName = “/vmfs/volumes/DatastoreUUID/originalname/originalname-UUID.vswp

    To

    sched.swap.derivedName = “/vmfs/volumes/DatastoreUUID/newname/newname-UUID.vswp

    Note: Be sure to rename both the .vswp file and the directory name for the swap file, bolded above.

  11. Correct any other remaining lines referencing the original path or file names.
  12. Save the file and exit the editor.
  13. Rename all the remaining files, except for the .vmx configuration file, to the new names desired. For example:mv “originalname.nvram” “newname.nvram”
  14. Change directory to the parent directory using cd .. and rename the directory for the virtual machine:mv “originalname” “newname”
  15. Using the VMware vSphere Client, browse the datastore and navigate to the renamed virtual machine directory.
  16. Right-click on the virtual machine’s configuration file (.vmx) and choose Add to inventory.Alternatively, you can use this command to inventory the virtual machine:vmware-cmd -s register “/vmfs/volues/DatastoreName/newname/newname.vmx”
  17. Power on the virtual machine.
  18. A question for the virtual machine displays in the Summary tab during power-on. Review the question by:
    • Clicking the Summary tab
    • Right-clicking the virtual machine in your inventory and selecting Answer question.When prompted, select I moved it, then click OK.Warning: Selecting I Copied It results in a change of the virtual machine’s UUID and MAC address, which may have detrimental effects on guest applications that are sensitive towards MAC address changes, and virtual machine backups that rely on UUIDs.

Additional Information

The command-line interpreter on ESX is case-sensitive and requires escaping of special characters used in some virtual machine file names. The above examples encourage the use of quotation marks around command arguments to ensure spaces and special characters are interpreted literally and do not require escape sequences.

For example, a virtual machine named “Original VM” is referenced either as:
“Original VM” with quotation marks, or Original\ VM.

Special characters such as opening and closing parentheses also require character escaping. For a virtual machine named “Original VM (1)”:
“Original VM (1)” with quotation marks, or Original\ VM\ \(1\).

The former quotation method simplifies the process considerably and improves readability.

Additional information on escape characters can be found in the Bash Reference Manual.

Related articles:

Disclaimer.
The information in this article is provided “AS IS” with no warranties, and confers no rights. This article does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion.

Marco

Marco works for ViaData as a Senior Technical Consultant. He has over 15 years experience as a system engineer and consultant, specialized in virtualization. VMware VCP4, VCP5-DC & VCP5-DT. VMware vExpert 2013, 2014,2015 & 2016. Microsoft MCSE & MCITP Enterprise Administrator. Veeam VMSP, VMTSP & VMCE.