Wednesday, November 18, 2009

Shared Folders in VirtualBox with Ubuntu Server

I really enjoy VirtualBox. After knowing it a little, I quit thinking about VMWare, for my Linux courses. Now, there is something really necessary when you work with more than one Virtual Machines: a shared folder.

A shared folder exists in the host system, and accessed by one or more guest systems. Thus, if VM1 needs to copy a file to VM2, just copy it to that folder. Easy like that.

Defining the Shared Folders
First, in Virtualbox console, you set one or more folders to be shared, as shown:

In the right panel above, you can see "Shared Folders". In this example there are already 2 shared folders. Click "Shared Folders", written in blue.

All shared folders are listed here. To add one more, click the folder with a "plus" signal, at the right side.

Write the host's folder full location, at "Folder Path" field. Optionally, this folder can be set as Read Only.

Virtualbox will suggest a name according to the folder's name, as you can see above.

Preparing Virtualbox Software
Now, let's make it available in our virtualized Ubuntu Server. First, we need to install it:

$ sudo aptitude install build-essential dkms

After that, "install Guest Additions" in "Devices" menu. It is an ISO image, and will work as if it is a CD.

Let's access it's contents

$ sudo mount /media/cdrom
$ cd /media/cdrom

In this folder there are several scripts, one for each system. You need to know wich version you have. Type the following:

$ uname -m
x86_64

My VM is a 64 bits. In my case I should type the following:

$ sudo ./VBoxLinuxAdditions-amd64.run

If the answer is "i686", the script must be "VBoxLinuxAdditions-x86.run". Any of them will first verify it's integrity, and after start some compilations. It will take a couple of minutes. Since we are using Ubuntu Server, a message warns that "X Window drivers will not be installed".

Building VirtualBox Kernel Modules
After all that, we are ready to build Virtualbox kernel modules, which is a really simple step. First, you will need to install kernel headers. Verify your kernel version with the command

$ uname -r

My system is kernel "2.6.31-14-server". My kernel is "server" type, so my kernel headers are installed with the following command:

$ sudo aptitude install linux-headers-server

It is a good idea to always update your kernel. When the headers installation ends, type the following command:

$ sudo invoke-rc.d vboxadd setup

After recompiling, Virtualbox recommends you to reboot. As you know, few operations require a system reboot, and most of them are kernel changings.

Mounting Shared Folders
We can now access it normally by mounting, using the module vboxsf (VirtualBox Shared Folders). To mount it manually, we need to know the Shared Folders' name, which is "Example". I choose "/mnt" as mount point.

$ sudo mount -t vboxsf Example /mnt

After that, you can copy files to and from /mnt folder. All these files will be available to the host system (in my case, Windows Vista System). If you want, with Windows Explorer you can access these files.

You can also configure Linux to automatically mount this directory. As "root" user, edit /etc/rc.local and add the following lines by the end of the file:

if [ -z "$(grep '/media/SwitchFiles' /proc/mounts)" ]; then
/bin/mount -t vboxsf SwitchFiles /media/SwitchFiles
fi

/proc/mounts show all mount points. If "/media/SwitchFiles" is not listed there it will be mounted. Of course, you should create this directory, case it does not exist yet.

My application
Since I have many VMs with Ubuntu Server and Ubuntu Desktop working toghether, frequently I have to install some packages, thus downloading them. With shared folders, I can download once, through any VM and use it in all the others VMs. My "/var/cache/apt/archives" is the mount point of a shared folder in Windows, in "/etc/fstab" in all VMs, as follows:

if [ -z "$(grep '/var/cache/apt/archives' /proc/mounts)" ]; then
/bin/mount -t vboxsf AptArchives /var/cache/apt/archives
fi

Of course, in my Virtualbox console, I have created "AptArchives" shared folder in each VM.

Do not forget to comment and to tell if this was useful or not.

1 comment:

how to password shared folder said...

Fantastic, great job man. THANKS
I must to say virtualbox really do me a lot.