VirtualBox’s Little Secret: The Command Line

Casual VirtualBox users might not know about the awesome power that lurks just beneath the surface in the Command Line Interface (CLI).

The most obvious question is, “Why work at the command line when you have a perfectly good GUI?” The answer is power. Since 1995, there’s a whole generation of computer users who think that the computer is a collection of windows and graphics, but, in reality, nothing could fall farther from the truth. Why, 1995, you ask? 1995 ushered in a new age of graphical computing with the introduction of Windows 95. Soon following that fateful August was the release of FVWM95, which was the Linux answer to the Windows 95 Explorer interface. It seemed to me that the good old days of the command line were gone forever. No one wanted to use DOS anymore and they weren’t too thrilled with Linux either. The command line is still alive and well in Linux environments. It’s still very much alive in the Windows realm. And now that Macs are Unix-based, it’s kicking up some non-GUI dust as well.

It may surprise you to know that sneaking around under that cute VirtuaBox GUI, with its colorful graphics, explicit Settings page and numerous dropdown lists, is the occult world of the command line. The true power of this dark realm reveals itself to those who dare enter its musty catacombs. After you discover the VirtualBox CLI, you might decide that the GUI is little more than lame window dressing (pun intended) created for those passersby who would otherwise ignore the uninspiring black rectangle of the real application.

Command Line Power

The rectangular box to which I refer is the terminal window. The command line offers power automation. Using scripts in the form of Perl, shell scripts or some other language is the only automation method that I’m aware of on *nix systems. The system scheduler daemon (cron) assists you in firing your scripts at the right times. It’s the power of the command line through automation that requires system administrators have scripting capability in their cache of skills. Every system administrator job opening you see will have shell scripting and at least one other interpreted language listed as requirements.

For virtualization, the command line allows you to rapidly create virtual disks, add a new network interface, start VMs and create new VMs, all without the use of a GUI. Scripted automation also allows you to schedule a VM’s start and shutdown without intervention. And, if you’re a really clever scripter, you can create a script that will send you through a series of prompts to create a VM, start, install and update it with very little intervention. If you’re the hands-off type and you have a list of VMs that you use on a regular basis, you can setup a set of scripts (think templates) for each type of VM that you use and create your VM within seconds of needing one.

Let’s take a look at the simplicity and elegance of the command line functionality in VirtualBox.

Installation and Basics

Download a binary distribution from VirtualBox Linux Downloads for your system. For you purists, the source code is also available. My selection (RHEL5/CentOS5 rpm) installed everything in the following locations:

  • Binaries – /usr/lib/virtualbox linked from /usr/bin
  • Libraries – /usr/lib/virtualbox
  • Sources – /usr/share/virtualbox
  • Guest Additions (ISO) – /usr/share/virtualbox

If your distribution doesn’t have the latest VirtualBox available (3.1.2, as of this writing), use one of the downloadable packages or the source code. Once you’ve installed VirtualBox, you may proceed as a regular user.

VM Creation and Configuration

The most difficult part of using the command line is knowing where to start. The GUI makes this easy by having a New button available for you to use. The command line is a bit more reluctant to give up its secrets. To create and setup your new VM, open a terminal window or ssh to the Linux host where you installed VirtualBox and enter the following commands.

$ /usr/bin/VBoxManage createvm --name Debian5 –register

This creates the new VM named, Debian5 and registers it with VirtualBox. The VM created is an XML file located in your home directory: ~/.VirtualBox/Machines/Debian5/Debian5.xml.

Create a virtual disk for your VM:

$ /usr/bin/VBoxManage createhd --filename Debian5.vdi --size 4000 --variant Fixed

You created a virtual disk named Debian5.vdi, 4GB (4,000MB) in size and with fixed size (non-expanding).

The virtual disk is created under ~/.VirtualBox/HardDisks/Debian.vdi

Create a Storage Controller to which you’ll attach the virtual disk and a virtual CD/DVD drive.

$ /usr/bin/VBoxManage storagectl Debian5 --name "IDE Controller" --add ide --controller PIIX4

This command created an IDE controller named “IDE Controller” and controller type PIIX4.

Now, attach your virtual disk to your VM:

$ /usr/bin/VBoxManage storageattach Debian5 --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium Debian5.vdi

Attach an ISO image for the operating system you want to install:

$ /usr/bin/VBoxManage storageattach Debian5 --storagectl "IDE Controller" --port 0 --device 1 --type dvddrive --medium /ISO/debian-40r3-i386-netinst.iso

Setup Networking:

$ /usr/bin/VBoxManage modifyvm Debian5 --nic1 bridged --cableconnected1 on --bridgeadapter1 eth0

This creates a bridged NIC with cable connected on startup and the Linux system sees the adapter as eth0.

Start the VM:

$ /usr/bin/VBoxManage startvm Debian5

If all goes as planned, you’ll see the following messages and then your VM window ready for installation.

Waiting for the remote session to open...

Remote session has been successfully opened.

As you can see, creating and setting up a VM at the command line frees you from your mouse click madness and opens up a whole new world of possibilities for VM automation. And, we’ve only scratched the surface here but hopefully this brief introduction will pique your curiosity and set you on a new quest to learn the command line’s awesome power.

Fatal error: Call to undefined function aa_author_bios() in /opt/apache/dms/b2b/linux-mag.com/site/www/htdocs/wp-content/themes/linuxmag/single.php on line 62