Autostart VMware Virtual Machines at Boot in Linux

By | November 17, 2013

Do you run VMware Workstation on your Linux machine?  I do.  A lot.  So much in fact that I’ve found it necessary to do things with VMware workstation that it doesn’t do on its own.  Maybe in time it will change and add such features, but for now you can’t auto-start VMs on boot.  Well, not without some tweaking.

Mind you, there are two ways to do it.  One is much more reliable than the other.  The first method is quick and dirty and works *most* of the time.  You can put the command to start up your VM(s) right in the /etc/rc.local file.  But you don’t have any guarantee when it will run during the boot up sequence, and if anything you have in that file ends up failing before the rest of it can execute, you’ve also lost.  Here’s an example:

 The Ugly Way

Inside /etc/rc.local →

/usr/bin/sudo -g tommy -u tommy /usr/bin/vmrun start /path/to/myvm.vmx nogui

Technically You Could Still Put Lipstick On That Pig

Now like I said, you have no guarantee when that command is going to kick off during your boot sequence.  In many cases, it will kick off too early and your VM won’t start (because vmware services aren’t fully up yet).  You can mitigate that by making a small change, but this still won’t give you the most reliable solution:

Inside /etc/rc.local →

# schedule the VM to start 1 minute after this code runs in /etc/rc.local
 
/bin/cat <<VMSTART | /usr/bin/at now + 1 minute >/dev/null 2>&1
/usr/bin/sudo -g tommy -u tommy /usr/bin/vmrun start /path/to/myvm.vmx nogui
VMSTART

But You Can Just As Easily Do Much Better

What you really need is an init script that fits into your boot sequence the way any other startup script would.  With an init script (properly written and LSB-compliant) you can make your script depend on other services so that the boot sequence automatically puts it in the right order.  And if it fails, you can look in dmesg and syslog to find out what may have gone wrong.

Use This Script Instead

But you don’t have an init script?  That’s OK.  Because I do.  I wrote this, tested it, and it works very nicely on Debian/Ubuntu.  Save the following script as /etc/init.d/vmware-autostarts.  Make it executable (chmod +x /etc/init.d/vmware-autostarts).  Customize it as desired with the list of VMs you’d like to autostart, and install it into the bootup sequence like so:

sudo update-rc.d vmware-autostarts defaults

The script below will also auto-stop the VMs as well.  If the VMs have VMware tools installed, a graceful shutdown will be possible.  Otherwise a hard shutdown will happen. At this point I could go into a long-winded, fully-detailed explanation of what this does. But why? The code is simple, and self-explanatory. Just install it, configure it, and use it. You can call it in stand-alone mode from the command line as well, to test it out beforehand.

NOTE 1: If you use a redhat-based distro version 7+, you’ll probably want to consider converting this to some kind of systemd unit file and run systemctl enable vmware-autostarts && systemctl start vmware-autostarts && systemctl status vmware-autostarts

NOTE 2: If you are using Workstation 12+ on Ubuntu 14.04, one of our readers has pointed out that you should switch the VM_cmd_exec sudo command in the script below to look like: VM_cmd_exec="sudo vmrun -T ws ..."