Perl programmer for hire: download my resume (PDF).
John Bokma's Hacking & Hiking

Mounting VirtualBox Shared Folders

October 4, 2016

Yesterday I experimented a bit with VirtualBox shared folders. I was able to share several directories in my home directory on OS X under my home directory on a Ubuntu 16.04.1 installation running in a virtual machine. Today I wanted to repeat the steps and write everything down.

As I am still migrating from Linux to OS X I need access to Linux one way or another for the time being. While I have been using Docker for Mac for some applications I ran into issues like a huge speed penalty for non-bind-mounts and containers just freezing. As I haven't been able yet to pin-point the exact cause of the latter I am falling back to Ubuntu running under VirtualBox for the time being. And as I eventually want to work on my files under OS X using shared folders sounds like a workable temporary solution.

Creating a Ubuntu 16.04.1 Virtual Machine

I created a Ubuntu 16.04.1 virtual machine using the following settings:

During the installation process I selected "Download updates while installing".

I probably could get away without swap, or use a swap file inside a shared folder so I can avoid Time Machine from backing it up. But that's probably for later.

The name for this virtual machine was taken from the Cadwal Chronicles by Jack Vance. A trilogy I recommend to read if you like SF.

After the installation I restarted the virtual machine, inserted the Guest Additions CD, ran the installer, and restarted the machine once more. Note that the Guest Additions are required for shared folders. Moreover, it allows me to run the Ubuntu desktop in 1920 x 1200 full screen.

Creating the Mount Points on the Guest OS

Besides several directories in my home directory I also wanted to have MySQL's data directory shared with OS X. So I needed to find out the numeric user id for mysql and group id for mysql. I ran the following commands in a virtual machine that had already MySQL installed:

$ id -u mysql
$ id -g mysql

Next, I created the mount-points that were not there yet:

mkdir -p ~/{.my-local,Amber,mysql-dumps}
sudo mkdir /var/lib/mysql
sudo chown 121:129 /var/lib/mysql

I use .my-local for perlbrew and to store some other programs. My work is stored in the Amber directory. Back in 1994 when I started my own company I named it 'Castle Amber', hence the name.

Adding the Shared Folders to the Virtual Machine

For the next step I used the command line on OS X to add the shared folders. However, with the virtual machine still running I got the following error:

VBoxManage: error: The machine 'Ecce 16.04.1' is already locked for a session (o
r being unlocked)

So I stopped the virtual machine gracefully and ran the following commands.

VBoxManage sharedfolder add "Ecce 16.04.1" --name ".my-local" \
           --hostpath /Users/john/.my-local
VBoxManage sharedfolder add "Ecce 16.04.1" --name "Amber" \
           --hostpath /Users/john/Amber
VBoxManage sharedfolder add "Ecce 16.04.1" --name "Documents" \
           --hostpath /Users/john/Documents
VBoxManage sharedfolder add "Ecce 16.04.1" --name "Downloads" \
           --hostpath /Users/john/Downloads
VBoxManage sharedfolder add "Ecce 16.04.1" --name "Pictures" \
           --hostpath /Users/john/Pictures
VBoxManage sharedfolder add "Ecce 16.04.1" --name "mysql-dumps" \
           --hostpath /Users/john/mysql-dumps
mkdir -p ~/Library/MySQL/mysql
VBoxManage sharedfolder add "Ecce 16.04.1" --name "mysql" \
           --hostpath /Users/john/Library/MySQL/mysql

Note that you can find out the exact name of a virtual machine by listing all available ones using:

$ VBoxManage list vms
"Ecce 16.04.1" {f19fbb74-cfe2-4987-bb58-0702dc557ccc}

As I wanted the data created by MySQL out of the way I decided to create a directory MySQL under /Users/john/Library with a sub directory named mysql.

Auto-Mounting Shared Folders

Since I wanted to specify where to mount each shared folder I couldn't use the auto-mount feature of VirtualBox but had to add each mount command to /etc/rc.local:

#!/bin/sh -e
# rc.local
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
# In order to enable or disable this script just change the execution
# bits.
# By default this script does nothing.

mount -t vboxsf -o uid=1000,gid=1000 .my-local   /home/john/.my-local
mount -t vboxsf -o uid=1000,gid=1000 Amber       /home/john/Amber
mount -t vboxsf -o uid=1000,gid=1000 Documents   /home/john/Documents
mount -t vboxsf -o uid=1000,gid=1000 Downloads   /home/john/Downloads
mount -t vboxsf -o uid=1000,gid=1000 Pictures    /home/john/Pictures
mount -t vboxsf -o uid=1000,gid=1000 mysql-dumps /home/john/mysql-dumps

mount -t vboxsf -o uid=121,gid=129 mysql /var/lib/mysql

exit 0

The numeric values for uid and gid were obtained using the id command. For my account these values are:

$ id -u john
$ id -g john

and for user mysql group mysql as given in the previous section.

After saving the changes to /etc/rc.local I restarted the virtual machine and logged back in. I verified that each mount point in my home directory showed the expected files and directories and that I was the owner using ls -al.

Next I installed MySQL and made the installation secure as follows:

sudo apt-get install mysql-server mysql-client

As expected, the data created by this step became available in OS X under /Users/john/Library/MySQL/mysql:

$ ls -al ~/Library/MySQL/mysql/
total 245776
drwxr-xr-x   12 john  staff       408 Oct  4 15:13 .
drwxr-xr-x    3 john  staff       102 Oct  4 14:53 ..
-rw-r-----    1 john  staff        56 Oct  4 15:13 auto.cnf
-rw-r--r--    1 john  staff         0 Oct  4 15:13 debian-5.7.flag
-rw-r-----    1 john  staff       413 Oct  4 15:13 ib_buffer_pool
-rw-r-----    1 john  staff  50331648 Oct  4 15:14 ib_logfile0
-rw-r-----    1 john  staff  50331648 Oct  4 15:13 ib_logfile1
-rw-r-----    1 john  staff  12582912 Oct  4 15:14 ibdata1
-rw-r-----    1 john  staff  12582912 Oct  4 15:13 ibtmp1
drwxr-xr-x   77 john  staff      2618 Oct  4 15:13 mysql
drwxr-xr-x   90 john  staff      3060 Oct  4 15:13 performance_schema
drwxr-xr-x  108 john  staff      3672 Oct  4 15:13 sys

Running ls -al inside the guest shows, again as expected:

$ sudo ls -al /var/lib/mysql
[sudo] password for john: 
total 122892
drwxr-xr-x  1 mysql mysql      408 oct  4 15:13 .
drwxr-xr-x 70 root  root      4096 oct  4 15:13 ..
-rw-r-----  1 mysql mysql       56 oct  4 15:13 auto.cnf
-rw-r--r--  1 mysql mysql        0 oct  4 15:13 debian-5.7.flag
-rw-r-----  1 mysql mysql      413 oct  4 15:13 ib_buffer_pool
-rw-r-----  1 mysql mysql 12582912 oct  4 15:14 ibdata1
-rw-r-----  1 mysql mysql 50331648 oct  4 15:14 ib_logfile0
-rw-r-----  1 mysql mysql 50331648 oct  4 15:13 ib_logfile1
-rw-r-----  1 mysql mysql 12582912 oct  4 15:13 ibtmp1
drwxr-xr-x  1 mysql mysql     2618 oct  4 15:13 mysql
drwxr-xr-x  1 mysql mysql     3060 oct  4 15:13 performance_schema
drwxr-xr-x  1 mysql mysql     3672 oct  4 15:13 sys

While the sorting order differs, the sizes and timestamp of each file is identical.