Advertisement
vartik

vmrename.sh

Mar 20th, 2024 (edited)
808
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.62 KB | None | 0 0
  1. #!/bin/sh
  2. VERSION=1.2
  3. AUTHOR="Branislav Vartik"
  4. OLD=$1
  5. NEW=$2
  6. if [ -n "$2" ] ; then
  7.     if vim-cmd vmsvc/getallvms | grep -q "$OLD/$OLD.vmx" ; then
  8.         echo "ERROR: VM must NOT be in invertory"
  9.     elif [ -e "$OLD/$OLD.vmsd" ] && grep snapshot.numSnapshots "$OLD/$OLD.vmsd" ; then
  10.         echo "ERROR: Snapshot must NOT exist"
  11.     elif [ ! -e "$OLD/$OLD.vmx" ] ; then
  12.         echo "ERROR: $OLD/$OLD.vmx not found"
  13.     else
  14.         echo "Modifying $OLD/$OLD.vmxf"
  15.         cp -a "$OLD/$OLD.vmxf" "$OLD/$OLD.vmxf.bak"
  16.         sed -ri "s/(>)$OLD(\.vmx<)/\1$NEW\2/" "$OLD/$OLD.vmxf"
  17.         echo "Modifying $OLD/$OLD.vmx"
  18.         cp -a "$OLD/$OLD.vmx" "$OLD/$OLD.vmx.bak"
  19.         sed -ri "s/^(displayName = \")$OLD(\".*)$/\1$NEW\2/g" "$OLD/$OLD.vmx"
  20.         sed -ri "s/^((nvram|extendedConfigFile|(ide|scsi)[0-9]+:[0-9]+\.fileName) = \")$OLD((_[^\-]+|)(\.\w+)\".*)$/\1$NEW\4/g" "$OLD/$OLD.vmx"
  21.         for i in vmx vmxf nvram vmsd ; do
  22.             if [ -e "$OLD/$OLD.$i" ] ; then
  23.                 echo "Renaming file $OLD/$OLD.$i to $OLD/$NEW.$i"
  24.                 mv "$OLD/$OLD.$i" "$OLD/$NEW.$i"
  25.             fi
  26.         done
  27.         ls "$OLD"/*.vmdk | egrep "^$OLD/$OLD(_[^\-]+|).vmdk$" | while read i ; do
  28.             j=$(echo $i | sed -r "s/^$OLD\/$OLD((_[^\-]+|)\.vmdk)$/$OLD\/$NEW\1/")
  29.             echo "Renaming disk $i to $j"
  30.             vmkfstools -E "$i" "$j"
  31.         done
  32.         echo Renaming folder "$OLD" to "$NEW"
  33.         mv "$OLD" "$NEW"
  34.     fi
  35. else
  36.     echo "VMrename v$VERSION"
  37.     echo "Author: $AUTHOR"
  38.     echo
  39.     echo "Usage: $0 <oldname> <newname>"
  40.     echo
  41.     echo "Limitations:"
  42.     echo "- processes only VMDK files in VMX folder"
  43.     echo "- modifies only IDE/SCSI fileNames without absolute/relative path"
  44.     echo "- VM must NOT be in ESXi inventory"
  45.     echo "- VM must NOT have any snapshot"
  46. fi
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement