Page MenuHomePhabricator

automatic shared folder mounting in VMs
Closed, ResolvedPublic

Description

Migrated from:
https://github.com/Whonix/Whonix/issues/223


Where we are:

The /mnt/shared folder gets already created by the shared-folder-help package.


Non-solutions:

TODO:

Implement this using systemd.

For VirtualBox, use Requires=vboxguest [or so, just do nothing if vboxguest or just output an info if not installed], then run.

mount -t vboxsf -o uid=1000,gid=1000 shared /mnt/shared

For kvm/qemu/libvirt, check necessary conditions(?), then run.

mount -t 9p -o trans=virtio shared /mnt/shared -oversion=9p2000.L

Details

Impact
Normal

Event Timeline

Patrick updated the task description. (Show Details)Feb 7 2015, 6:25 PM
Patrick added subscribers: Patrick, HulaHoop.
Patrick created this task.
Patrick raised the priority of this task from to Normal.

With the migration to systemd, is it now easier to add this?

Neither easier nor harder.

Patrick set Impact to Needs Triage.
HulaHoop added a comment.EditedJun 13 2015, 1:40 AM

fstab.d should be available in the libmount version in Jessie:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=666163

A bug in the kernel causes the regular expression for mounting 9p shares to fail. A tested workaround works in /etc/fstab :

shared /mnt/shared 9p x-systemd.automount,x-systemd.device-timeout=10,trans=virtio,version=9p2000.L,rw 0 0

https://bugzilla.redhat.com/show_bug.cgi?id=1184122

Tests with fstab.d giving me problems. It could be a matter of not knowing how it works. I first didn't know that there should be a directory called fstab.d and not a file. Apparently configuration files in there should be named a special way:

/etc/fstab.d/00_header.fstab, /etc/fstab.d/50_middle.fstab, /etc/fstab.d/99_end.fstab

A different option for share mounting is /sbin/mount.filetype for example /sbin/mount.nfs. Needs more research doesn't seem as easy as dropping a text file in sbin

https://unix.stackexchange.com/a/62826

https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=666163 says fstab.d support has been removed. Not a great long term solution.

systemd could help. It supports ConditionVirtualization=kvm. Has tmpfiles.d. + Running the mount command. Should be doable.

Anyone feel free to submit a tested patch.

HulaHoop added a comment.EditedJun 13 2015, 4:47 PM

systemd can understand mount entries but they still have to be specified in /etc/fstab which won't solve anything.

More information on fstab.d:

Turns out fstab.d is not supported by default in libmount however the mount option can take an alternate mount table file/directory as specified in a command. Please read the linked answer that I'm trying to paraphrase here:

https://askubuntu.com/a/169995


Alternatives to system-wide fstab is to tell udsisks to mount a directory for a user account upon login:

https://askubuntu.com/questions/364954/do-personal-fstab-files-exist-for-user-accounts

If the manual mount command that does not involve /etc/fstab, i.e. mount -t 9p -o trans=virtio shared /mnt/shared -oversion=9p2000.L work for you in terminal, then a systemd unit file could do the same. Also without requiring /etc/fstab.

HulaHoop added a comment.EditedJun 13 2015, 8:39 PM

Excellent example of shared folder mounting with systemd files in Arch documentation. It will take changing them from vmware to suit KVM and drop them in the same paths for testing:

https://wiki.archlinux.org/index.php/VMware/Installing_Arch_as_a_guest#Systemd

Notes:

filetype=9p

These units must be "enabled" to make them automatically start with subsequent system startups


Command cheat sheet:

https://wiki.archlinux.org/index.php/Systemd#Using_units

Show the status of a unit, including whether it is running or not:

$ systemctl status unit

Check whether a unit is already enabled or not:

$ systemctl is-enabled unit

Enable a unit to be started on bootup:

  1. systemctl enable unit

Nice. Much better than manually running mount from systemd.

HulaHoop added a comment.EditedJun 13 2015, 11:16 PM

How far I've gone:
I managed to debug errors until I corrected the parameters in the unit files so they should be correct. The remaining error has something to do with the 9p kernel module and I'm not sure where to go from here.

host mount[8341]: mount: mount(2) failed: No such file or directory
host kernel: 9pnet_virtio: no channels available
host systemd[1]: mnt-shared.mount mount process exited, code=exited status=32
host systemd[1]: Failed to mount Load KVM shared folders.

/etc/systemd/system/mnt-shared.mount

[Unit]
Description=Load KVM shared folders
ConditionPathExists=/mnt/shared
ConditionVirtualization=kvm

[Mount]
What=/mnt/shared
Where=/mnt/shared
Type=9p
Options=defaults
DirectoryMode=0777
TimeoutSec=10

[Install]
WantedBy=multi-user.target

/etc/systemd/system/mnt-shared.automount

[Unit]
Description=Load KVM shared folders
ConditionPathExists=/mnt/shared
ConditionVirtualization=kvm

[Automount]
Where=/mnt/shared
DirectoryMode=0777

[Install]
WantedBy=multi-user.target

Debugging:
Run as root

systemctl daemon-reload
systemctl start mnt-shared.mount
journalctl -xn

q to quit detailed error report by journalctl

edit the files once again and save thenreload daemons and so on.

This option is dead because of some rare bug. There are only 4 search results for it and none of the situations really applies to our setup here.

A solution you probably won't like is patching the fstab file directly.

Something based on the third method on here:
http://blog.allanglesit.com/2012/05/bash-programmatically-add-entries-in-fstab/

Followed by column -t /etc/fstab to make it reader friendly:
https://unix.stackexchange.com/a/96046

HulaHoop (HulaHoop):

A solution you probably won't like is patching the fstab file directly.

Indeed.

Have systemd run the 'mount' command would be better and should be possible.

Ok I see what you're talking about.

The mount command is placed in a regular script file and then a custom systemd service is configured to execute it on startup:

https://unix.stackexchange.com/questions/47695/how-to-write-startup-script-for-systemd

I can do the systemd service file part but I'm unsure as to how the script should look like. If its as simple as pasting the command I use in there, no problem.

Yes, somewhat.

I don't think you need a separate script. Instead of running a single
line script, you could try running the mount command directly from the
systemd unit file.

HulaHoop added a comment.EditedJun 14 2015, 9:25 PM

Great news! automatic shared folders is now achieved :D

I used this as an example:
http://lukas.zapletalovi.com/2013/08/execute-command-during-start-with-systemd.html

Two systemd service files were created under /etc/systemd/system/

To enable and start them:

sudo systemctl enable service-name
sudo systemctl start service-name

mnt-shared-kvm.service

[Unit]
Description=Mounts KVM shared folder during start
ConditionPathExists=/mnt/shared
ConditionVirtualization=kvm

[Service]
Type=oneshot
ExecStart=/bin/mount -t 9p -o trans=virtio shared /mnt/shared -oversion=9p2000.L
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

mnt-shared-vbox.service

[Unit]
Description=Mounts VirtualBox shared folder during start
ConditionPathExists=/mnt/shared
ConditionVirtualization=oracle
After=vboxguest.service
After=virtualbox-guest-utils.service

[Service]
Type=oneshot
ExecStart=/bin/mount -t vboxsf -o uid=1000,gid=1000 shared /mnt/shared
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
Patrick added a comment.EditedJun 14 2015, 10:37 PM

Nice!

TODO:

  • In section [Unit] it should probably use After= something. I.e. make it run after VBox / KVM guest additions.
  • debian/rules modifications
  • debian/control modifications
  • use /lib/systemd/system/
  • commit to shared-folder-help package
  • build package, test if it actually works
HulaHoop added a comment.EditedJun 15 2015, 1:04 AM

What is the name of the systemd vbox guest additions service so I can add it? KVM does not rely on spice or a guest additions equivalent to share folders.

A vbox guest additions dependency only makes sense if its installed by default.

How to find out? Look manually into /etc/init.d/virtualbox-guest-utils
(or systemd unit files if it had those) and/or 'grep -i provides
/etc/init.d/virtualbox-guest-utils'.

After=vboxguest.service
After=virtualbox-guest-utils.service

OK I edited my comment and added those in under the Unit section.

Patrick changed Impact from Needs Triage to Normal.
Patrick changed the task status from Open to Review.Aug 6 2015, 5:27 PM

Merged. Requires testing when new test images get available.

In T144#6289, @Patrick wrote:

Merged. Requires testing when new test images get available.

https://forums.whonix.org/t/whonix-12-0-0-3-2-rc-testers-wanted/

Does it work in KVM?

do not let systemd service enter failed state of host config has not been applied:
https://github.com/Whonix/shared-folder-help/commit/24143991888ab900effe4b11f7eb55172af6793d

Patrick closed this task as Resolved.Mar 16 2016, 4:42 PM
Patrick claimed this task.

@HulaHoop reported it works in KVM.