The flexibility of Linux is one of its many benefits. And in particular, one aspect of that flexibility is allowing the user to completely customize his or her computing environment. For example, suppose you’re a stalwart Linux fan (of course you are), but you’re sharing a machine with someone who prefers another OS. You could dual-boot, but then what happens to all the important background processes you have running? Let’s set up your system so that, when this particular user logs in, they’ll go directly to a Virtual Machine running their favorite operating system.
We’ll be using VirtualBox for this article, but you should be able to adjust the concept if you’re using another VM application (it will just need to have the ability to start a VM from the command-line). The initial steps you’ll need to take are:
- Install VirtualBox.
- Create a Virtual Machine with your favorite OS installed (MTE has covered installing both Windows and Mac OS X previously).
- Start-up your new Virtual Machine and configure to taste.
The Linux Log-in Process
It’s useful to understand a little about how the log-in process works in Linux GUI’s. When the system starts, a display manager will run automatically… this is why nowadays you’re greeted with a nice graphical log-in, rather than a plain old command prompt on start-up. Once you log in, the display manager will start one application or script, which will in turn start a number of other programs, scripts, and processes. For example, in a KDE environment, the display manager doesn’t care about all the background processes – it only needs to run the script “startkde“. The start-up script will remain running as long as you’re using the desktop. Once you exit your desktop (by using an option like “Log Out” or “Restart”), the desktop’s processes will all shut themselves down, and when they’re all done, the start-up script knows it can exit as well. The display manager is waiting in the background, and when the start-up script exits, the display manager takes control and displays your log-in screen again.
Setting Up Your Custom Start-up Script
So, in order to allow a user to log directly into a VM, all we need to do is create a start-up script that will run this VM for them automatically. The first step is to create this script, which is straightforward in Linux. Create a new text file (let’s call it winxp-session), and paste in the following:
You can try running the Virtual Box command from the terminal to make sure it works correctly… if not, you probably have the name of the VM misspelled (keep in mind if your VM’s name has spaces, you’ll need to enclose it in single quotes). Next, run the following command in order to make the script executable:
Then you’ll need to copy this file to someplace it can be executed. The “/usr/bin” directory is an option here, as is “/usr/local/bin”. You’ll need to be root to do so for both these locations. To place it in “/usr/bin”, use the following command:
The final step is to create a shortcut in the location where the display manager looks for available start-up scripts. In Ubuntu, this is in the “/usr/share/xsessions/” directory. With a text editor, create a new file (as, for example, “/usr/share/xsessions/winxp-session.desktop”):
Now, if you’re in the desktop, log out. Your new VM-based desktop will be ready for you when the display manager comes up again. Note, however, that this session will only be available for the user who created the VM (since it will only be in that user’s “~/VirtualBox VMs/” directory).
Let us know if this is helpful to you, or if you have any question.