Skip to content

Instantly share code, notes, and snippets.

@raarts
Last active Apr 18, 2022
Embed
What would you like to do?
Script to rename a virtual machine in ESXi 6
#!/bin/sh
#
# shell script to rename a virtual machine in ESXi
#set -x
if [ $# -ne 4 ]; then
echo "Usage: $0 VOLNAME DIRNAME OLDNAME NEWNAME
where VOLNAME is the volume name, e.g. datastore1,
DIRNAME is the the name of the directory of the virtual machine,
OLDNAME is the name of the files of the virtual machine before the '.',
NEWNAME is the new name for the directory and the files.
example vm-rename: VMs \"(NEW) p56 25 centos6 x8664 c.net\" p56-25-centos6-x8664-c.net p56-25-centos6-x8664-C.net
Do NOT forget to unregister the VM from the inventory first!!
"
exit 1
fi
VOLNAME="$1"
DIRNAME="$2"
export OLDNAME="$3"
export NEWNAME="$4"
VM_DIRPATH="/vmfs/volumes/$VOLNAME/$DIRNAME"
NW_DIRPATH="/vmfs/volumes/$VOLNAME/$NEWNAME"
if [ ! -d "$VM_DIRPATH" ]; then
echo "The directory path $VM_DIRPATH is invalid"
exit 1
fi
if [ "$DIRNAME" != "$NEWNAME" ]; then
if [ -d "$NW_DIRPATH" ]; then
echo "The new directory path $NW_DIRPATH already exists"
exit 1
fi
fi
cd "$VM_DIRPATH"
if [ ! -f "$OLDNAME".vmdk ]; then
echo "$OLDNAME.vmdk not found. Exiting. No harm done yet."
exit 1
fi
if [ ! -f "$OLDNAME".vmx ]; then
echo "$OLDNAME.vmx not found. Exiting. No harm done yet."
exit 1
fi
if [ -f "$OLDNAME".vmx.lck ]; then
echo "$OLDNAME.vmx.lck found. Is this VM running? Exiting. No harm done yet."
exit 1
fi
### DONE CHECKING, NOW GET TO WORK
# First rename the vmdk files. We have to use vmkfstools for this
# Use a find trick to handle spaces in names.
#
find . -type f -name "*$OLDNAME*.vmdk" -exec sh -c "
FILE=\$(echo \"\$0\" | sed \"s/\$OLDNAME/\$NEWNAME/g\");
vmkfstools -E \"\$0\" \"\$FILE\";
" {} \;
# Replace all file references in the .vmx file
#
cp "$OLDNAME".vmx "$OLDNAME".vmx.backup
sed -i "s/$OLDNAME/$NEWNAME/g" "$OLDNAME".vmx
if [ $? -ne 0 ]; then
echo "ERROR using sed to replace \"$OLDNAME\" with \"$NEWNAME\" in \"$OLDNAME\".vmx. Exiting.."
echo "The VM may now be left in an inconsistent state, and you may need to fix it manually."
exit 1
fi
# Rename the remaining files. Use `find` trick to handle spaces in names
#
find . -type f -name "*$OLDNAME*" -exec sh -c "
FILE=\$(echo \"\$0\" | sed \"s/\$OLDNAME/\$NEWNAME/g\");
echo renaming \"\$0\" to \"\$FILE\";
mv \"\$0\" \"\$FILE\";
" {} \;
# Finally rename the directory
#
cd ..
if [ "$DIRNAME" != "$NEWNAME" ]; then
mv "$DIRNAME" "$NEWNAME"
if [ $? -ne 0 ]; then
echo "ERROR renaming \"$DIRNAME\" to \"$NEWNAME\". Exiting.."
echo "The VM is now in an inconsistent state, and you need to fix it manually."
exit 1
fi
fi
echo "All Done. You now need to register $NEWNAME to the inventory."
#EOF
@jeffshead
Copy link

jeffshead commented Feb 22, 2017

Thanks for sharing!
Can you please explain how to run your script on the ESXi server? I have SSH, logged in as root, uploaded your script to '/tmp' but I do not know how to run the script! I do not know the command. Simply typing "vm-rename" or "vm-rename.sh" gives me a file not found error.

@jpluimers
Copy link

jpluimers commented Apr 18, 2022

I forked the fork gist.github.com/Gremgoll/8bd91258d71fe895c0d416e2543ca2dc (which already had a few changes with improvements) and pushed a change with fixes and extensions to gist.github.com/jpluimers/fcc601dd41ac89f601a5174be92c841c and github.com/jpluimers/vm-rename (the last two have the same content, but I find it easier to work with repositories on the main GitHub site than in gists).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment