Nine41 Consulting | Automating the Mundane…Scripting for VMWare Fusion Efficiency
apple, device management, dep, vpp, systems management, landesk, ivanti, lanrev, absolute, heat, mdm,
85
post-template-default,single,single-post,postid-85,single-format-standard,qode-quick-links-1.0,ajax_fade,page_not_loaded,,columns-3,qode-child-theme-ver-1.0.0,qode-theme-ver-11.0,qode-theme-bridge,wpb-js-composer js-comp-ver-5.1.1,vc_responsive
 

Automating the Mundane…Scripting for VMWare Fusion Efficiency

Automating the Mundane…Scripting for VMWare Fusion Efficiency

Growing up, it was not uncommon to hear someone quote the phrase “you tell me what you think about when you do not have to think, and I’ll tell you what you are.”  It’s a quote that can be deep, philosophical and ignite some serious introspection.

For me, the answer is easy.  It’s efficiency.  My brain is constantly chewing on how to make whatever insufferable process I’ve been experiencing in the last hour, day, week, etc; more palatable, more efficient, less…well, less sufferable.

Now, I know that declaring war on inefficiency should not be humanity’s primary purpose in life; not everything is a race to the finish line with as limited wasted movement as possible, but tell that to some other guy who’s brain doesn’t dissect every tiny amount of wasted time as potential opportunity for improvement.

When a former employer was asked by a potential new employer what my biggest weakness was, he responded with “well, I don’t know if this is exactly a weakness, but Bennett will constantly be looking for a way to improve a process, to make things more streamlined, even if he spends weeks devising a plan to save a minute’s worth of work during an 8 hour work day.”

Personally I hear that and I think, “wow, look how inefficient I’m being on spending such time to save so little.”

Nonetheless, hear I sit, a self-proclaimed efficiency hunter ready to tell you how I spent last week efficiency hunting within my daily workflow, to save not only time, but frustration and to improve the consistency of my work.

My daily routine as a LANDESK Sales Engineer consists of demonstrating one or more of our products to potential customers.  Due to the number of different products desired to be seen and the potential use cases from the customer that may require an integration demo, I may have up to 8 different virtual machines that need to be spun up.

In order to maintain consistency in my demo environment, I will take snapshots of each VM so I can demo the full capabilities of the software products and quickly reset back to square one for the next demo.

As such, I’m often starting a number of machines, stopping a number of machines, snapshotting them, reverting to snapshots, deleting old snapshots and even just needing to see what snapshots I’ve created.  All of this can be a bit cumbersome when you do it many times a day.

So when I received my new MacBook Pro last week, my efficiency brain kicked in, it was time to automate the entire process.  Luckily for me, VMWare has a utility that allows command line access to do all of the starting and stopping of the machines, as well as the snapshot management.

Perfect right?  Well almost, I didn’t want to write my own script, I wanted to just borrow someone else’s.  Alas, I scoured page one and even page 2 of Google (I know, page 2!) for a number of different search criteria hoping to find a hit, only to come away empty handed.

My efficiency brain wouldn’t let me just quit and give up though.  So I went to work and spent from 9 PM to 3 AM writing my own script and am now making that script available to you for your own efficiency pleasure.

Enjoy!  All you should have to do is fill out the variables at the top and you’ll soon be on your way to VMWare Fusion Management efficiency bliss.  I’ve saved my script as a .command file so I can execute directly from my desktop.

[code language=”bash”]
#/bin/bash

# Version history
# v1.0 – initial release
#

# Add the paths to all of your VMs
declare -a arrayPathForVMs=(“Documents/VMs/LDSERVER/LDSERVER.vmx” “Documents/VMs/OSX/OSX.vmx” “Documents/VMs/Win8/Win8.vmx”)

# Create a directory location variable
vmrunPath=”/Applications/VMware Fusion.app/Contents/Library/vmrun”
setScriptPath=”$HOME”

while :
do
clear
cat <<EOF
=========================================
VM Fusion Management
—————————————–
Please enter your choice:

(1) Start VMs
(2) Revert to a Snapshot
(3) Create a Snapshot
(4) Delete a Snapshot
(5) List Snapshots
(6) Suspend all VMs
(7) Exit

—————————————–
EOF
read -n1 -s
case “$REPLY” in

“1”)
echo “Starting your VMs…”
cd “${setScriptPath}”
for i in “${arrayPathForVMs[@]}”;
do
echo “Launching” “$i”
“${vmrunPath}” -T fusion start “${i}”;
done
echo “All VMs started.”
Sleep 2
exit 0
;;

“2”)
echo -n “Which snapshot do you want to revert to?”
read snapshotName
cd “${setScriptPath}”
for i in “${arrayPathForVMs[@]}”;
do
echo “Reverting to” “$snapshotName” “on” “$i”
“${vmrunPath}” -T fusion revertToSnapshot “${i}” “${snapshotName}”;
done
echo “All VMs reverted.”
Sleep 2
exit 0
;;

“3”)
echo -n “What will the snapshot name be?”
read newSnapshotName
cd “${setScriptPath}”
for i in “${arrayPathForVMs[@]}”;
do
echo “Creating” “$newSnapshotName” “on” “$i”
“${vmrunPath}” -T fusion snapshot “${i}” “${newSnapshotName}”;
done
echo “Snapshots created for all VMs”
Sleep 2
exit 0
;;

“4”)
echo -n “Are you sure you want to delete a snapshot (y/n)? ”
read answer
if echo “$answer” | grep -iq “^y” ;then
echo -n “What snapshot do you want to delete?”
read snapshotToDelete
cd “${setScriptPath}”
for i in “${arrayPathForVMs[@]}”;
do
echo “Deleting” “$snapshotToDelete” “on” “$i”
“${vmrunPath}” -T fusion deleteSnapshot “${i}” “${snapshotToDelete}”;
done
echo “Snapshot deleted for all VMs”
read -p “Press [Enter] to close.”
exit 0
else
echo “No changes have been made.”
read -p “Press [Enter] to close.”
exit 0
fi
;;

“5”)
echo “Listing your snapshots…”
for i in “${arrayPathForVMs[@]}”;
do
echo “Snapshots for” “$i”
“${vmrunPath}” -T fusion listSnapshots “${setScriptPath}/””${i}”;
done
echo “All snapshots displayed”
read -p “Press [Enter] to close.”
exit 0
;;

“6”)
echo “Suspending your VMs…”
cd “${setScriptPath}”
for i in “${arrayPathForVMs[@]}”;
do
echo “Suspending” “$i”
“${vmrunPath}” -T fusion suspend “${i}”;
done
echo “All VMs suspended.”
Sleep 2
exit 0

;;

“7”)
echo “Exiting…”
exit 0

;;

“Quit”)
break
;;

*) echo invalid option;;
esac
done

[/code]

5 Comments
  • Eric Blair
    Posted at 11:14h, 08 February Reply

    Thanks so much for posting this. I’m *almost* there. Any chance you can see what’s wrong?

    The script can’t seem to find the vmrun executable, even though I think I’ve set the path correctly. I confirmed it’s in the same place you specify — by doing Show Package Contends in the finder and and dragging vmrun to a Terminal window.

    I’m running OS X 10.10.5 and VMWare Fusion 8.1.0.

    ** Here’s how I edited the top of your script: **

    # Set Paths to My VMs
    declare -a arrayPathForVMs=(“/Users/red/Desktop/Virtual Machines/OS X Everyday.vmwarevm/OS X Everyday.vmx” “/Users/red/Desktop/Virtual Machines/OS X Yellow.vmwarevm/OS X Yellow.vmx” “/Users/red/Desktop/Virtual Machines/OS X Net.vmwarevm/OS X Net.vmx”)

    # Set Path to CLI vmrun command
    vmrunPath=”/Applications/VMware Fusion.app/Contents/Library/vmrun”
    setScriptPath=”$HOME”

    ** Here’s where I stored your script **

    /Users/red/_bash_scripts/vm-ops.sh
    (when that didn’t work I tried storing the script in $HOME but that didn’t work either)

    Here’s the result of choosing option 6 when your script runs:

    Suspending your VMs…
    Suspending /Users/red/Desktop/Virtual Machines/OS X Everyday.vmwarevm/OS X Everyday.vmx
    /Users/red/vm-ops.sh: line 127: /Applications/VMware Fusion.app/Contents/Library/vmrun: No such file or directory
    Suspending /Users/red/Desktop/Virtual Machines/OS X Yellow.vmwarevm/OS X Yellow.vmx
    /Users/red/vm-ops.sh: line 127: /Applications/VMware Fusion.app/Contents/Library/vmrun: No such file or directory
    Suspending /Users/red/Desktop/Virtual Machines/OS X Net.vmwarevm/OS X Net.vmx
    /Users/red/vm-ops.sh: line 127: /Applications/VMware Fusion.app/Contents/Library/vmrun: No such file or directory

    What did I do wrong?

  • Bennett
    Posted at 17:27h, 08 February Reply

    Eric, you have your vmrunPath in quotes therefore the OS is going to take the literal text you input, including the slashes to specify a space. Try removing the backslash after VMWare as shown here: “/Applications/VMware Fusion.app/Contents/Library/vmrun” instead of ”/Applications/VMware Fusion.app/Contents/Library/vmrun”

  • Eric Blair
    Posted at 10:44h, 09 February Reply

    First, thank you again for this script. I adapted the menu to add other commands (e.g. ‘stop’) I commonly use.

    For the vmrun path: Removing the backslash inside quotes didn’t work for me, so I removed the quotes and left the backslash. That worked.

    For the VM array: Same thing — I had to remove the quotes and leave the backslash. And … the script didn’t work when it pointed to a .vmx file inside a .vmware file. It worked when I specified only the .vmware file.

    Here’s how the working version looks, in case it helps someone else:

    # define which VMs will be in the group
    declare -a arrayPathForVMs=(/Users/red/Desktop/Virtual Machines/OS X Everyday.vmwarevm/OS X Everyday.vmx /Users/red/Desktop/Virtual Machines/OS X Yellow.vmwarevm/OS X Yellow.vmx /Users/red/Desktop/Virtual Machines/OS X Net.vmwarevm/OS X Net.vmx)

    # define path to vmrun command
    vmrunPath=/Applications/VMware Fusion.app/Contents/Library/vmrun

  • Eric Blair
    Posted at 11:10h, 09 February Reply

    And … no good deed goes unpunished. May I ask one more thing? I know just enough to be dangerous and don’t want to screw this up.

    In some cases I’d like to make sure all mounted non-system volumes (external hard drives, DMGs and VeraCrypt volumes) are cleanly ejected from the running VMs.
    1. For example, I may mount a DMG or VeraCrypt volume inside a VM, and those volume files are stored in a VMWare shared folder. Using the host, I regularly need to copy or rsync that DMG or VeraCrypt file. It doesn’t seem smart to do that while the volume is mounted in a VM, even if the VM is suspended.
    2. I’ve thought of writing a shell script inside each VM that would eject all non-system volumes, then run it from the host with the vmrun runScriptInGuest command. I know how to dismount-all from the VeraCrypt CLI (all it needs is veracrypt -d), but I don’t see that option in diskutil. I assume I can never unmount /dev/disk0 or /dev/disk1 (the HFS volumes are all FileVaulted), but I want to use diskutil umount on all the others, for an arbitrary set of /dev/disk(n). Any idea how I’d do that? Or maybe there’s a 3rd party app that I could call with vmrun runProgramInGuest?
    3. Is it helpful to unmount external volumes from a VM before a vmrun stop operation, or is that redundant?

    Thank you again.

  • Eric Blair
    Posted at 11:14h, 09 February Reply

    Ug. Correction to 10:44 a.m. post. As you can see from what I pasted, my VM array list did point to the .vmx files, not the .vmware files. I had to use .vmware files for another, unrelated alias I created.

Post A Comment

ARE YOU READY TO GET STARTED?
Please fill out your information, and a specialist will reach out to discuss our services in more details.
Your Information will never be shared with any third party.
        
Free Training Videos
Register to gain access to all of our free content.
YOUR PHONE
YOUR NAME
YOUR EMAIL
        
Get Started
Provide us your contact information and we will reach out as quickly as possible.
YOUR PHONE
YOUR NAME
YOUR EMAIL
        
Additional Questions?
Provide us your contact information and we will reach out as quickly as possible.
YOUR PHONE
YOUR NAME
YOUR EMAIL
        
Pay by PO?
Provide us your contact information and we will reach out to help you sign up by PO.
YOUR PHONE
YOUR NAME
YOUR EMAIL
Subscribe Now
Subscribing to our site gives you access to our Apple Admin 101 training videos as well as allowing us to notify you of each new blog post we release.
YOUR PHONE
YOUR NAME
YOUR EMAIL
Pay by PO
Send us your contact information and we will reach out to help you sign up by PO.
YOUR PHONE
YOUR NAME
YOUR EMAIL
Additional Questions?
Send us your contact information and your questions and we will reach out as quickly as possible.
YOUR PHONE
YOUR NAME
YOUR EMAIL
YOUR QUESTIONS
Get Started
Provide us your contact information and we will reach out as quickly as possible.
YOUR PHONE
YOUR NAME
YOUR EMAIL
Free Training Videos
Subscribing to our site gives you access to our Apple Admin 101 training videos as well as allowing us to notify you of each new blog post we release.
FIRST NAME
LAST NAME
PHONE
EMAIL
Subscribe Now
Subscribing to our site gives you access to our Apple Admin 101 training videos as well as allowing us to notify you of each new blog post we release.
FIRST NAME
LAST NAME
PHONE
EMAIL