VMware ESXi - Change the location of an ESX virtual machine swap file on cmd

Change the location of an ESX virtual machine swap file on cmd

These days i ran into an error message when i tried to start a newly added virtual machine from my inventory list in vSphere Client 6. As a VMware Knowledgebase-Article stated this had to do with the missing swapfile space on the datastore volume i used. I have missed to create the virtual machine using thin-provisioning. To move it form datastore1 to datastore2 i could use the vSphere Menu by clicking on Configuration > Virtual Machine Swapfile Location but i like solutions that can get the same results just by using the command line.

  1. we have to find out what the name of the .vmdk file is which belongs to the virtual machine where we move the swapfile from. Right click in the VM in the inventory list and choose Edit Options > Hardware > Harddisk . Under Harddisk File you will see the datastore number, its containing folder and the exact name of the .vmdk file. Write that name down as we we need it later on: [datastore1]/Debian8/ Debian8-xxx.vmdk
  2. after taking the VM down and deleting it from the inventory list by right-clicking on it, connect to the ESXi Server using putty.
  3. cd /vmfs/volumes/ && ls -la . You will see the datastore symlinks that point to your volumes that are located at Config > Storage in your vSphere Client
    datastore1 -> xxx-cc780e45-1cb5-xxx
    datastore2 -> xxx-e641da4e-87eb-xxx
  4. jump into the datastore folder where you want to copy the vm to.  cd datastore2
  5. make a new (target-)folder there that you name the way you want your virtual machine to be called in the inventory list. mkdir "Debian8"
  6. now we make a copy of the VM´s .vmdk file we noted in step 1 (source folder) into the newly created folder from step 5 (target folder) preventing that it is cloned as thick-provision (what it does if you use the vSphere Menu-way described above).
    vmkfstools -i "/vmfs/volumes/xxx-cc780e45-1cb5-xxx/Debian8/Debian8-xxx.vmdk" -d thin "/vmfs/volumes/xxx-e641da4e-87eb-xxx/Debian8/Debian8-xxx.vmdk"
  7. When it says Clone: 100% done everything went well.
  8. You can now clone the other files. To prevent overwriting the just cloned .vmdk in the target folder and the flat file that was created there automatically rename the source files:
    mv "/vmfs/volumes/xxx-cc780e45-1cb5-xxx/Debian8/Debian8-xxx.vmdk /vmfs/volumes/xxx-cc780e45-1cb5-xxx/Debian8/Debian8-xxx.old && mv /vmfs/volumes/xxx-cc780e45-1cb5-xxx/Debian8/Debian8-flat.vmdk /vmfs/volumes/xxx-cc780e45-1cb5-xxx/Debian8/Debian8-flat.old"
  9. now jump into the source folder and copy all other files over into the the target folder except the .log and .old files: cd /vmfs/volumes/xxx-cc780e45-1cb5-xxx/Debian8/ && ls -la && cp "Debian8*.*v* /vmfs/volumes/xxx-e641da4e-87eb-xxx/Debian8/" && cd "/vmfs/volumes/xxx-e641da4e-87eb-xxx/Debian8/" && ls -la
  10. Now readd the VM to the inventory list by going to Configuration > Storage > Datastorage > Search Datastorage .Click on the Debian8 folder and Right-Click on the Debian8.vmx file, choose Add to Inventorylist, name it Debian8 and choose the Resourcepool it belonged to before.
  11. Now you can restart the VM the way you did before, but that will fail. As you will see there is a little yellow exclamation mark beneath the vm icon in the inventory list.
  12. Click on it and answer the question wether you copied or moved the vm. As we renamed the old .vmdk file so that there is no second .vmdk file anymore we choose MOVED. That´s it