Skip to content

Instantly share code, notes, and snippets.

@kentwait
Last active June 20, 2024 12:53
Show Gist options
  • Save kentwait/ea49b270f4f7480541409c5ded093ec9 to your computer and use it in GitHub Desktop.
Save kentwait/ea49b270f4f7480541409c5ded093ec9 to your computer and use it in GitHub Desktop.
Mount VirtualBox shared folder on boot using fstab

How to mount a VirtualBox shared folder when the Guest OS boots

Problem

While using VirtualBox's Guest Additions to mount shared folders provides a seamless way to mount shared folders, there are also disadvantages.

  • Shared folders will always mount in /media/sf_(share name) unless specified using through vbox drivers in the guest OS
  • Mounting does not happen at boot-time. Applications that require paths at boot will not be able to access the shared folder even after it is mounted - such as Docker.

Solution

Instead of relying on Guest Additions, if you have sudo permissions, shared folders can be mounted at boot-time using fstab. Using this approach allows

  • Mounting shared folders to specific paths other than /media/sf_(share name)
  • Shared folders are guaranteed to be mounted at boot time.

However, it is important that the shared folder must exist on the Host OS or else the Guest OS will fail to boot.

Instructions

These steps were adapted from http://jhshi.me/2016/07/12/mounting-virtualbox-shared-folder-on-boot/index.html.

First, find out your numerical uid and gid by performing the following shell commands.

$ echo $UID
1000
$ echo $GID
1000

Here, my UID is 1000 and GID is 1000. This is necessary in order to give you read and write permissions when you mount the shared folder.

Let's assume you want to mount a shared folder with a share name you specified in VirtualBox as vb_shared to /home/kent/shared. Make sure that that path exists and create the directories if it isn't.

Now, open fstab and edit. Here I use nano.

$ sudo nano /etc/fstab

Edit text marked by () by removing the () and using your own values and append the following at the end of fstab.

(vb_shared)    (/home/kent/shared)    vboxsf    defaults,uid=(1000),gid=(1000),umask=0022    0    0

This line will mount your shared folder named vb_shared to mount to the path (that exists) /home/kent/shared. Once mounted, user with uid 1000 belong to group with gid 1000 will have read and write access through umask=0022.

If you have more than one shared folder, add one line for each shared folder you want to mount.

Now save the edited fstab.

Lastly, edit /etc/module to add vboxsf so that it is present at boot-time.

$ sudo nano /etc/modules

Append the following line and save.

vboxsf

Restart and the shared folder/s should now be mounted at the specified path/s.

@blizzardwj
Copy link

Still works nowdays. My environment is NAS (AS6604T) + virtualbox (6.1x). umask cannot be recognized by NAS OS, so I omit this parameter. That is a quite important step to make PT download-flow work.

@supremeKAI40
Copy link

I managed to do it in one try but when, I shutdown the guest OS and restarted after a few days, I see the error "failed to mount " in boot screen.

The OS starts and folders are all there but the space from host OS which was shared is not mounted.

Here's the line in fstab, somehow still fails to mount.

image

The folder space does show in "other locations" in file explorer of guest OS although inaccessible and says "cannot mount without root" on clicking
image

Guest OS: Ubuntu 20.04
Host OS:Windows 10 20H2
Virtualbox version: 6.1
Guest Additions: 6.1.20

Please help me, using this VM is crucial but exchanging 10s of GBs of data is not really feasible...

@eisenreich
Copy link

With Ubuntu 22.04.4 LTS it did not work - adding comment=systemd.automount to the options in fstab solved the problem

vb_shared    /home/kent/shared    vboxsf    defaults,uid=1000,gid=1000,umask=0022,comment=systemd.automount    0    0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment