In this article, I will show you how to write a VMware Autostart program using the Bash shell scripting language and configure systemd to start VMware Workstation Pro 16 virtual machines on boot automatically. This way, you will still have a backup plan when VMware Workstation Pro drops the Shared VM feature for good. So, let’s get started.
Table of Contents
- Introduction to the VMware Autostart Program
- Installing Required Dependencies
- Writing the VMware Autostart Program
- Automatically Starting VMware Autostart Program on Boot
- Testing the VMware Autostart Program
- Known Issues
- GitHub Repository
- Conclusion
- References
Introduction to the VMware Autostart Program
The main purpose of the VMware Autostart program:
1) Read a configuration file consisting of all the VMware Workstation Pro 16 VMs that you would like to start at boot time automatically.
2) Automatically start the configured VMs at boot time and suspend them before the system shutdown or restart.
To accomplish this, I have created,
1) A central VM configuration file config.json.
2) A Bash shell script autostart. It will read the configuration file config.json to determine the VMs to manage. It will start or suspend the configured VMs depending on the command-line argument provided to the shell script at runtime.
3) A systemd unit file vmware-autostart.service. It will automatically run the Bash script autostart to start all the configured VMs at boot time. It will also run the Bash script autostart to suspend the configured VMs before the system shuts down or restarts.
Installing Required Dependencies
I have used a JSON file config.json for storing the VMware VM configuration that the VMware Autostart program will use to determine the virtual machines to autostart on boot.
To parse the JSON configuration file from the Bash shell script of the VMware Autostart program, I have used the jq command.
On Ubuntu, Debian, Linux Mint, KDE Neon, and other Debian based Linux distributions; you can install jq with the following command:
On CentOS 8/RHEL 8, you can install jq with the following command:
Once jq is installed, run the following command to see whether jq is accessible from the terminal.
Writing the VMware Autostart Program
This section will show you how to write the VMware Autostart program and explain how it works.
First, create a new project directory /opt/vmware-autostart and create a bin/ subdirectory in the project directory as follows:
Create a new config.json file in the project directory /opt/vmware-autostart with the following command:
The config.json file keeps a list of VMware Workstation Pro 16 VMs that you would like to start on boot automatically.
A VMware Workstation Pro 16 VM entry has only 3 properties.
- name – The name of the virtual machine
- vmxpath – The absolute path to the .vmx file of the virtual machine
- autostart – A boolean. If true, the VM will be automatically started at boot time. If false, the VM won’t be managed by the VMware Autostart program. So, it won’t automatically start at boot time.
You can find the virtual machine’s name and vmxpath from the VMware Workstation Pro 16 app as marked in the screenshot below.
Now that you know the configuration file format, let’s write the VMware Autostart program using the Bash shell scripting language.
Create a new Bash script autostart in the /opt/vmware-autostart/bin directory with the following command:
Type in the following lines of codes in the autostart shell script.
msg_auto_answer_disable() {
echo "disabling msg.autoAnswer for $vm_name"
sed -i.bak -s '/^msg\.autoAnswer/d' "$vmx_path"
}
msg_auto_answer_enable() {
echo "enabling msg.autoAnswer for $vm_name"
echo 'msg.autoAnswer = "TRUE"' >> "$vmx_path"
}
start_vm() {
echo "$vm_name is starting..."
/usr/bin/vmrun -T ws start "$vmx_path" nogui 2>/dev/null && echo "$vm_name started." || echo "$vm_name failed to start."
}
suspend_vm() {
echo "$vm_name is suspending..."
/usr/bin/vmrun -T ws suspend "$vmx_path" hard 2>/dev/null && echo "$vm_name suspended." || echo "$vm_name failed to suspended."
}
config_file=/opt/vmware-autostart/config.json
num_vms=$(jq '.vms | length' < $config_file)
action="$1"
for ((counter=0; counter < $num_vms; counter++))
do
vm_name=$(jq -j ".vms[$counter].name" < $config_file)
vmx_path=$(jq -j ".vms[$counter].vmxpath" < $config_file)
vm_autostart=$(jq -j ".vms[$counter].autostart" < $config_file)
if [ "$action" == "start" -a "$vm_autostart" == "true" ]
then
msg_auto_answer_disable && msg_auto_answer_enable && start_vm
fi
if [ "$action" == "suspend" -a "$vm_autostart" == "true" ]
then
msg_auto_answer_disable && suspend_vm
fi
done
exit 0
The autostart Bash script should look as follows.
Once you’re done, press <Ctrl> + X followed by Y and <Enter> to save the autostart Bash script.
Here,
The msg_auto_answer_disable and msg_auto_answer_enable functions are used to remove and set the msg.autoAnswer option in the .vmx file of the VM, respectively.
When msg.autoAnswer is set to TRUE, VMware Workstation Pro 16 virtual machines won’t wait for any user inputs. If this is not set, you won’t be able to start virtual machines in the background using the autostart script if it requires any user interaction to start.
The start_vm function is used to start a virtual machine using the .vmx file of the virtual machine. It also prints the necessary log messages to the screen.
The suspend_vm function is used to suspend a virtual machine using the .vmx file of the virtual machine. It also prints the necessary log messages to the screen.
Some variables are defined in lines 23-25.
config_file – The absolute path to the config.json file.
num_vms – Uses jq to calculate the number of VMs available in the config.json file.
action – The first command-line argument is the action to perform. The autostart script uses it to determine whether to start VMs or suspend VMs.
Lines 27-42 are used to traverse through all the available virtual machines in the config.json file, enable/disable msg.autoAnswer property for each virtual machine, and start/suspend the virtual machines.
In the loop, lines 29-31 define some variables.
vm_name – Uses jq to get the name property of the virtual machine from the config.json file.
vmx_path – Uses jq to get the vmxpath property of the virtual machine from the config.json file.
vm_autostart – Uses jq to get the autostart property of the virtual machine from the config.json file.
If the action value is start and vm_autostart (the autostart property in config.json) is true, remove the msg.autoAnswer property from the .vmx file (if exists) of the virtual machine, set msg.autoAnswer property to TRUE in the .vmx file of the virtual machine, and start the virtual machine.
If the action value is suspended and vm_autostart (the autostart property in config.json) is true, remove the msg.autoAnswer property from the .vmx file suspend the virtual machine.
Once you’ve created the autostart Bash script, make it executable with the following command:
Automatically Starting VMware Autostart Program on Boot
In this section, I will create a system service so that it runs the autostart Bash script automatically at boot time.
Create a new file vmware-autostart.service in the project directory /opt/vmware-autostart as follows:
Type in the following lines in the vmware-autostart.service file.
Description=Automatically Start VMware Virtual Machine
After=network.target vmware.service
Requires=network.target vmware.service
Conflicts=shutdown.target
Before=shutdown.target multi-user.target
[Service]
Type=oneshot
ExecStart=/opt/vmware-autostart/bin/autostart start
ExecStop=/opt/vmware-autostart/bin/autostart suspend
Restart=no
RemainAfterExit=yes
User=shovon
Group=shovon
[Install]
WantedBy=multi-user.target
Once you’re done, press <Ctrl> + X followed by Y and <Enter> to save the vmware-autostart.service file.
Here,
ExecStart runs the autostart script with a start command-line option at boot time. So, all the virtual machines available in the config.json file will be started automatically on system boot.
ExecStop does the opposite. It runs the autostart script with the suspend command-line option before shutting down or restarting the computer. So, all the virtual machines available in the config.json file will be suspended on system shutdown or restart.
The autostart script will be run as the User shovon and Group shovon. You must replace it with your login User and primary Group name.
You can find your login User and primary Group name with the following command:
Make a symbolic link of the vmware-autostart.service file in the /etc/systemd/system directory as follows:
For the systemd changes to take effect, run the following command:
Now, add the systemd service vmware-autostart.service to the system startup to automatically start at boot time.
Testing the VMware Autostart Program
To verify whether the program works as expected, you have to restart your computer. Before you do that, make sure to add the VMs that you want to automatically start on boot in the /opt/vmware-autostart/config.json file.
I will add the ubuntu-desktop-1 and work pc 1 VMs in the /opt/vmware-autostart/config.json file for the demonstration.
Once you’re done, restart your computer with the following command:
Once your computer boots, you should see that the systemd service vmware-autostart.service is active.
From the systemd logs, you can see that the VMs ubuntu-desktop-1 and work pc 1 started automatically just fine.
To confirm that the VMware Workstation Pro 16 VMs started automatically on boot, run the following command:
As you can see, the VMware Workstation Pro 16 VMs are running just fine.
You can also verify that the VMs are running from the VMware Workstation Pro 16 app, as you can see in the screenshot below.
To test whether the VMs will be suspended when you shutdown or restart your computer, open one of the VMs you’ve configured to autostart and start any app.
Close the VMware Workstation Pro 16 app while the VM is running.
Click on Run in Background as marked in the screenshot below.
Now, reboot your computer with the following command:
Once your computer boots, open the VMware Workstation Pro 16 app, and you should see that the VM is in the same state as last time (recovered from the suspended state).
Known Issues
Keep a virtual machine opened in the VMware Workstation Pro 16 app. The VMware Autostart program won’t be able to suspend the virtual machine when you shutdown or reboot your computer while keeping the VMware Workstation Pro 16 app open. It might be rare for people to shutdown or reboot their computer while keeping programs open. But the program could be improved to ensure that the VMware Workstation Pro 16 app is closed before suspending virtual machines.
GitHub Repository
The VMware Autostart program demonstrated in this article is also available on GitHub. You can clone the GitHub repository (shovon8/vmware-autostart) of this program to save yourself some typing.
GitHub Link: https://github.com/shovon8/vmware-autostart
Conclusion
This article shows you how to write a VMware Autostart program to automatically start VMware Workstation Pro 16 virtual machines on boot and suspend them before the system shutdown or reboots.
References
[1] VMware – Command Line Application
[2] Using vmrun to Control Virtual Machines
[3] Count JSON Array Elements with jq – PHPFog.com
[4] How to remove quotes from the results? · Issue #1735 · stedolan/jq · GitHub