Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- VERSION=1.2
- AUTHOR="Branislav Vartik"
- OLD=$1
- NEW=$2
- if [ -n "$2" ] ; then
- if vim-cmd vmsvc/getallvms | grep -q "$OLD/$OLD.vmx" ; then
- echo "ERROR: VM must NOT be in invertory"
- elif [ -e "$OLD/$OLD.vmsd" ] && grep snapshot.numSnapshots "$OLD/$OLD.vmsd" ; then
- echo "ERROR: Snapshot must NOT exist"
- elif [ ! -e "$OLD/$OLD.vmx" ] ; then
- echo "ERROR: $OLD/$OLD.vmx not found"
- else
- echo "Modifying $OLD/$OLD.vmxf"
- cp -a "$OLD/$OLD.vmxf" "$OLD/$OLD.vmxf.bak"
- sed -ri "s/(>)$OLD(\.vmx<)/\1$NEW\2/" "$OLD/$OLD.vmxf"
- echo "Modifying $OLD/$OLD.vmx"
- cp -a "$OLD/$OLD.vmx" "$OLD/$OLD.vmx.bak"
- sed -ri "s/^(displayName = \")$OLD(\".*)$/\1$NEW\2/g" "$OLD/$OLD.vmx"
- sed -ri "s/^((nvram|extendedConfigFile|(ide|scsi)[0-9]+:[0-9]+\.fileName) = \")$OLD((_[^\-]+|)(\.\w+)\".*)$/\1$NEW\4/g" "$OLD/$OLD.vmx"
- for i in vmx vmxf nvram vmsd ; do
- if [ -e "$OLD/$OLD.$i" ] ; then
- echo "Renaming file $OLD/$OLD.$i to $OLD/$NEW.$i"
- mv "$OLD/$OLD.$i" "$OLD/$NEW.$i"
- fi
- done
- ls "$OLD"/*.vmdk | egrep "^$OLD/$OLD(_[^\-]+|).vmdk$" | while read i ; do
- j=$(echo $i | sed -r "s/^$OLD\/$OLD((_[^\-]+|)\.vmdk)$/$OLD\/$NEW\1/")
- echo "Renaming disk $i to $j"
- vmkfstools -E "$i" "$j"
- done
- echo Renaming folder "$OLD" to "$NEW"
- mv "$OLD" "$NEW"
- fi
- else
- echo "VMrename v$VERSION"
- echo "Author: $AUTHOR"
- echo
- echo "Usage: $0 <oldname> <newname>"
- echo
- echo "Limitations:"
- echo "- processes only VMDK files in VMX folder"
- echo "- modifies only IDE/SCSI fileNames without absolute/relative path"
- echo "- VM must NOT be in ESXi inventory"
- echo "- VM must NOT have any snapshot"
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement