VirtualBox Shared Folders under Linux
Tuesday, May 29, 2018 |My work machine runs Windows (go ahead and laugh. I’ll wait). While I’ve been able to tweak the machine and get a moderately acceptable setup, there are times when I’d really like to use Linux for something, so I spin up a virtual machine with VirtualBox. While that works, I don’t really like having source code — especially with changes in flight — on the VM, as it makes it a bit more dangerous/difficlt to destroy the VM should I need the disk space (which happens more often than I’d like). I set out, then to get shared folders working so I can keep the source on my host machine, and just do the work in the VM. Unfortunately, it doesn’t seem to be as simple as adding a shared folder to the VirtualBox config. This post, then, will detail the steps I took to make things work for me.
The zeroth step, clearly, is to set up the VM. I’ll not cover that here (though perhaps I can in another post if there’s interest, but it seems to be a well-covered topic already). Once you have a working VM, we need to make our configuration changes.
Step 1 (which I did with the VM shut down, though that’s not strictly necessary), is to configure the shared folders. From the VirtualBox Manager window, we need to select the VM we want to modify, and select the Shared Folders tab:
From here, we need to click the Add a folder button on the right, and specify the host directory (Folder Path
) and
the… device name (Folder Name
) by which the folder is exposed to the guest:
Once we’ve configured that, we can boot the VM if it’s not already running. From a command prompt, we need to make some system changes. The most important thing is to verify that the VirtualBox guest additions are installed. Like setting up the VM, this is well documented elsewhere, so I’ll leave that as an exercise for the reader, but I will offer these simple commands that installed the GA for me on my Fedora VM:
1
2
$ dnf install kernel-headers kernel-devel elfutils-libelf-devel gcc dkms make bzip2 perl
$ bash /run/media/jdlee/VBox_GAs_*/VBoxLinuxAdditions.run
One way to verify that the Guest Additions are properly installed is to load the kernel module:
1
# modprobe vboxsf
The command should complete without error. If it does not, you may need to revisit the Guest Additions installation.
That done, it seems that for a user to access the shared folder, the account needs to belong to the vboxsf
group,
so we need to add the user to the group:
1
2
3
4
5
# groups jdlee
jdlee : jdlee wheel
# usermod -a G vboxsf jdlee
# groups jdlee
jdlee : jdlee wheel vboxsf
In my case, I want to mount the host directory as the src
directory in my account’s home directory, so I added this
to /etc/fstab
:
1
HOST_SRC /home/jdlee/src vboxsf defaults,uid=jdlee,gid=jdlee 0 0
I can then mount the shard folder via mount -a
. If you get a message about a protocol error, verify that the device name/label
given in column 1 in the fstab
entry matches the Folder Name
given in the VirtualBox configuration.
At this point, you should be ready to start using the shared folder like any other local directory. I typically reboot the VM to verify that I have all the bits in the right places while the effort is still fresh in my mind, but that’s totally optional.
Have I overlooked anything? Is there a simpler approach? Feel free to sound off in the comments!