Jack Wallen shows you a nifty trick for easily starting, stopping, pausing, and resuming your VirtualBox virtual machines on a headless Linux server.
There's a nifty feature that comes with VirtualBox that allows you to create a desktop shortcut for virtual machines. This, however, doesn't do you much good if you're running VirtualBox on a GUI-less server. If that's the case, you don't always want to have to issue the command to start a VM every time it's needed. To that end, what do you do? If you're using Linux to host VirtualBox VMs, it's really quite simple—you create bash scripts to manage the starting, stopping, and resuming of those virtual machines.
I'm going to show you how to do just that. I'll assume you already have VirtualBox installed along with all the virtual machines you need. With that said, let's see how this is done.
Listing your VMs
The first thing you need to find out is the name of your virtual machines. If this were a GUI, you could simply open up VirtualBox and see them listed. But since the server has no GUI, you'll need to make use of a handy command to list your VMs. That command is:
VBoxManage list vms
The output of the command (Figure A) will show you the name and UID of each of your virtual machines. You'll want the name of your VM.
Creating the bash script
Let's say I want to create a script to start the VM "UbuntuSERVER Clone". Create the file with the command nano ubuntuserverclone.start. The contents of that file will be:
#! /bin/bash VBoxManage startvm "UbuntuSERVER Clone" --type headless
Save and close that file.
For each task you'd have to create a different file. You could create a ubuntuserverclone.stop file with the following contents:
#! /bin/bash VBoxManage controlvm "UbuntuSERVER Clone" poweroff --type headless
To pause that same virtual machine, create a file called ubuntuserverclone.pause with the following contents:
#! /bin/bash VBoxManage controlvm "UbuntuSERVER Clone" pause --type headless
To resume that paused virtual machine, create a file called ubuntuserverclone.resume with the contents:
#! /bin/bash VBoxManage controlvm "UbuntuSERVER Clone" resume --type headless
Make sure, after you create each file, that you give it executable rights with the command:
chmod u+x ubunserverclone.start
Do that for each file you create substituting the name of the file each time, so your new bash scripts will execute the proper action.
Running the script
You have two options: You can run those scripts from within a particular directory, or you can copy them to a directory in your $PATH, so they can be run globally. If you opt to not have them run globally, simply open a terminal, change into the directory housing the scripts, and issue the command ./ubuntuserverclone.start.
If you opt to go the global route, you could copy the files with the command sudo cp ubuntuserverclone* /usr/local/bin. Next give it the proper permission with the command sudo chmod o+x /usr/local/bin/ubuntuserverclone.*. Now you could run each script, from any directory, simply by issuing the command ubuntuserverclone.start (or .stop, .pause, .resume).
Easy control of your headless VMs
And that's all there is to making the control of your VirtualBox headless virtual machines incredibly easy. Create controlling bash scripts for each of your VMs, so you can start, stop, pause, and resume them without having to issue the full command every time.
- How to create VirtualBox networks with the Host Network Manager (TechRepublic)
- How to clone VirtualBox Virtual Machines from the command line (TechRepublic)
- How to enable copy and paste in VirtualBox (TechRepublic)
- How to set bridged networking in a VirtualBox virtual machine (TechRepublic)
- Four ways to run Windows 10 on your Mac (ZDNet)