VMware ESX – Recreate a VMX file for a VM

To totally unlock this section you need to Log-in


On a VMware ESX infrastructure (especially during VM migration between two not connected ESX) there could be a tedious scenario: once you have extracted a VM from the old ESX and imported to the new one you realize that the VM's VMX file is probably corrupted.

Normally, you could (and should) recreate a missing or corrupt VMX file by restoring it from a backup, but it could not work either.

An easy way for recreating a VMX file is usually the usage of VMware Infrastructure Client (VIC), via vSphere Web Portal or via CLI/RCLI: you will recreate a new VM inside the new infrastructure and then you will re-add the VMDK (virtual disks) files to the new VM, or you could also point the new VM’s drives to the existing disk (VMDK) files of the server with the corrupt/missing VMX file.

To begin this process and make things clean we will first remove (not the data) the VM from the VMware Infrastructure clients inventory list (right click on the VM and select Remove from Inventory).

Then to replace a corrupted VMX file you can rename, preferable option, or delete the offending VMX file.

First start the New Virtual Machine Wizard and select a Virtual Machine Configuration type of Custom.

VMware ESX - Recreate a VMX file for a VM

The next screen of the wizard will ask for a Name for the new VM. Make sure the name you type in here matches the name of the directory on your VMFS partition that hosts the VM with the missing/corrupt VMX file.

If you enter in a different name here the New Virtual Machine Wizard will create a directory of that name that will contain the VMX file (along with a couple of other files important to the running of the VM) whilst your disk (VMDK) file(s) could be located in another directory (this is not a real problem but could bring confusion).

Even if there could be situations (very few) where you may want to keep your disk and configuration files separate, you really should keep them all together to reduce the risk of any confusion and accidental moves or deletions of these VM related files.

VMware ESX - Recreate a VMX file for a VM

The next screen of the VM creation process will ask for the location of the datastore (LUN). As mentioned above, in most situations it is best to select the same LUN/Disk on which the VMDK (disk) files are located.

VMware ESX - Recreate a VMX file for a VM

From now on we will go through the next few steps of the process for select and adjust (if required) any of the VM configuration parameters (eg: Guest OS, number of virtual processors, memory, etc).

When you get to the Select a Disk screen then select Use an existing virtual virtual disk and select the primary VMDK boot disk file for the VM with the missing/corrupt VMX file.

Proceed through the rest of the GUI process until you get to the Ready to Complete New Virtual Machine screen. At this point if you wish to add any additional secondary disks then check the Edit the virtual machine settings before submitting box and add in any additional disks, NICs, etc.

VMware ESX - Recreate a VMX file for a VM

Once complete then press the Finish button. Within the VMware Infrastructure Client interface you will now see the newly recreated VM back in the inventory list.

Using the Datastore Browser navigate to the folder of VM and you should now see a freshly created VMX file.

VMware ESX - Recreate a VMX file for a VM

Using CLI for Creating VMX

VMware ESX Server has tools available for command-line creation and cloning of virtual machines. These tools are available via the service console and require that you access the service console with root-level privileges.

As we already know, the .vmx file holds the VM’s configuration which, in the large part, is hardware related. If required, you can edit this file manually using a text editor.

Alternatively, power off the VM and use the vSphere client to affect any changes from the VM’s Configuration Parameters found using right-click on the VM and then choosing Edit Settings.

To create a new dummy, but working, .vmx file we will create a dummy VM named TestVM and stored on the local datastore:

$ vim-cmd vmsvc/createdummyvm testVM [LocalDatastore_001]/testVM/testVM.vmx


$ vim-cmd vmsvc/getallvms
Vmid    Name                     File                     Guest OS    Version   Annotation
1      testVM   [LocalDatastore_001] testVM/testVM.vmx   otherGuest   vmx-10

At this point, if we have downloaded and opened the original .vmx file, using a common text editor (such as Notepad++ or anything else) we could see some lines like the following (that represent the first HDD attached to a VM):

scsi0:0.deviceType = "scsi-hardDisk"
scsi0:0.fileName = "EXAMPLE.vmdk"
sched.scsi0:0.shares = "normal"
scsi0:0.present = "TRUE"

Knowing this we could copy and paste/replace, in our new testVM.vmx file, these lines or adapting the testVM.vmx file with info gathered by the old .vmx file (take note that in this example the HDD file will need to be placed in the same folder as the .vmx file).

At this point, with the new testVM.vmx we can try powering on the new dummy VM:

$ vim-cmd vmsvc/power.on 1

Get runtime informations about the running VM:

$ vim-cmd vmsvc/get.runtime 1
Runtime information
(vim.vm.RuntimeInfo) {
   dynamicType = ,
   host = 'vim.HostSystem:ha-host',
   connectionState = "connected",
   powerState = "poweredOn",
   faultToleranceState = "notConfigured",
   dasVmProtection = (vim.vm.RuntimeInfo.DasProtectionState) null,
   toolsInstallerMounted = false,
   suspendTime = ,
   bootTime = "2015-02-04T00:28:55.507435Z",
   suspendInterval = 0,
   question = (vim.vm.QuestionInfo) null,
   memoryOverhead = 36478976,
   maxCpuUsage = 2496,
   maxMemoryUsage = 32,
   numMksConnections = 0,
   recordReplayState = "inactive",
   cleanPowerOff = ,
   needSecondaryReason = ,
   onlineStandby = false,
   minRequiredEVCModeKey = ,
   consolidationNeeded = false,
   featureRequirement = (vim.vm.FeatureRequirement) [
      (vim.vm.FeatureRequirement) {
         dynamicType = ,
         key = "cpuid.SSE3",
         featureName = "cpuid.SSE3",
         value = "Bool:Min:1",
      (vim.vm.FeatureRequirement) {
         dynamicType = ,
         key = "cpuid.SSSE3",
         featureName = "cpuid.SSSE3",
         value = "Bool:Min:1",
      (vim.vm.FeatureRequirement) {
         dynamicType = ,
         key = "cpuid.NX",
         featureName = "cpuid.NX",
         value = "Bool:Min:1",
      (vim.vm.FeatureRequirement) {
         dynamicType = ,
         key = "cpuid.RDTSCP",
         featureName = "cpuid.RDTSCP",
         value = "Bool:Min:1",
      (vim.vm.FeatureRequirement) {
         dynamicType = ,
         key = "cpuid.Intel",
         featureName = "cpuid.Intel",
         value = "Bool:Min:1",
   vFlashCacheAllocation = 0,

Once finished the control we will be able to power it off:

$ vim-cmd vmsvc/power.off 1

And finally we will be able to get informations about the datastore location of VM:

$ vim-cmd vmsvc/get.datastores 1
name                 LocalDatastore_001
url                  /vmfs/volumes/54d15e2c-eeeeeeee-9cff-080027b1c126
capacity             10468982784
freeSpace            9544138752
accessible           1
type                 VMFS
multipleHostAccess   <unset>

Finally, type the following all on one line in the console window to register the new virtual machine with the ESX server:

vmware-cmd -s register "Your Path"/testVM/testVM.vmx

Your Path should be in a similar format to /vmfs/volumes/storage1/.

A returned value of "1" after running this command indicates a successful registration of the virtual machine, so now open up the GUI of your ESX server to verify that the new VM is listed.

If so, you are ready to power on the virtual machine and install your operating system directly from vSphere Client (Web too). You have successfully created a new virtual machine utilizing the VMware command-line tools.

In case of corrupted VMDK

Usually VMDK could corrupt pretty easily if not well maintained (during migrations, hard copy, etc.). If your host machine restarts unexpectedly or if the VM is not shut down properly etc. you may then experience issues the next time you try to restart.

One of the most common issue regarding VMDKs is that the VM takes for ever to load/startup or presents you with a black screen and no progress: in this case usually we will need to go to the directory where your VM files reside, search for and delete all directories and files ending in .lck (lock files).

A second usual scenario is showed with the following message:

Warning: the system was unable to load a page of memory; this can be caused by network problems or a failing hard disk drive.

In this case this error will be related not to the VMDK file directly, but to .vmem and .vmss files that reside in the same directory. To quickly fix this issue we will have to go to the VM directory and delete all .vmem and .vmss files.

These files represent the state of you VM before it was last shut down and are used to restore it to this state during start-up once the VM is restarted.

If these files are corrupted for some reason, the VM startup will show the error above. Deleting these files allows you to reboot your VM OS with a clean slate.

However, if we suspect that a VMDK file has been corrupted we can attempt a recovery using the vmkfstools command line tool with specific options that will check and/or repair the virtual disk in case of an unclean shutdown.

vmkfstools --fix check /vmfs/volumes/esx4-1-local-storage-1/dummy/dummy.vmdk
Disk is error free

Another scenario in which we could think that a VM is corrupted is when we are using an Open Virtualization Format (OVF) or Open Virtualization Appliances (OVA) of EFI (Unified Extensible Firmware Interface) Shell based Linux virtual machines and after the importing they fail to boot.

In this case the cause is not the corrupted VMDK but the missing .nvram file: the .nvram file is not exported with the OVF and OVA files.

To fix this issue we will need to follow this procedure:

  1. Take a copy of the original source virtual machine's NVRAM file (vmname.nvram) from the virtual machine folder.
  2. Deploy the OVF/OVA to the new virtual machine.
  3. Power on and attempt to boot the new virtual machine. This will fail. It will generated a new NVRAM file in the virtual machine datastore folder.
  4. Power off the new virtual machine.
  5. Copy the original NVRAM file into the destination folder, and rename it accordingly to overwrite the existing NVRAM file created previously.
  6. Power on and boot the new virtual machine.

Finally, if the VMDK is really corrupted, we could try to use ddrescue Linux utility, following this procedure:

  • Use a Linux VM and connect to an ESXi host that has access to the VMFS-volume.
  • Then connect also to a second ESXi with a healthy datastore.
mkdir /esxi-in
sshfs -o ro root@esxi-host:/ /esxi-in 
mkdir /esxi-out
sshfs root@esxi-host:/ /esxi-out

NOTE: SSHFS (SSH Filesystem) is a filesystem client to mount and interact with directories and files located on a remote server or workstation over a normal ssh connection. It will let us to mount a remote SSH system as it was a local folder on our local system.

Now, using ddrescue utility, which is a data recovery tool, we will be able to try a recovery of the whole VMDK:

ddrescue /esxi-in/vmfs/volumes/datastore-damaged/vm-name/disk-with-i-o-errors-flat.vmdk /esxi-out/vmfs/volumes/healthy-datastore/recovery/fixed-disk-flat.vmdk  /esxi-out/vmfs/volumes/healthy-datastore/recovery/fixed-disk-flat.vmdk.log 

Make sure to create a log-file. If this works you can regard the fixed-disk-flat.vmdk as healthy.

If you don't want to use (or you can't use an external VM or system to connect to two ESX hosts) the ddrescue approach you can try try to clone the VMDK with vmkfstools:

vmkfstools -i disk-with-i-o-errors.vmdk fixed-disk.vmdk

If that works without vmkfstools-errors you can regard fixed-disk.vmdk as healthy.

You can achieve the same result by using dd utility from an ESX host (on console or from SSH):

dd if=disk-with-i-o-errors-flat.vmdk of=fixed-disk-flat.vmdk bs=1M

Even in this case, if the process complete successfully the VMDK can be assumed as healthy. Note that dd utility could fail (read-only access) if Storage I/O Control (SIOC) is enabled on the VMFS datastore. SIOC is a protective layer and should be disabled to use this last approach.