Bug 277027 - Null-mount a single file (not a directory)
Summary: Null-mount a single file (not a directory)
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 14.0-RELEASE
Hardware: Any Any
: --- Affects Some People
Assignee: freebsd-fs (Nobody)
URL:
Keywords: feature
Depends on:
Blocks:
 
Reported: 2024-02-13 13:47 UTC by vini.ipsmaker
Modified: 2024-02-14 18:39 UTC (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description vini.ipsmaker 2024-02-13 13:47:41 UTC
This nullfs feature has been requested in scattered places, so I thought we could have a bug to track progress that users can subscribe to. Examples:

- https://lists.freebsd.org/pipermail/freebsd-questions/2020-December/292395.html (we can't null-mount a single file (useful to inject configs or sockets; linux has mount --bind for that))

- https://forums.freebsd.org/threads/how-to-share-a-regular-file-between-jails.70793/
Comment 1 Mark Johnston freebsd_committer freebsd_triage 2024-02-13 15:04:36 UTC
This isn't covered by https://cgit.freebsd.org/src/commit/?id=521fbb722c33663cf00a83bca70ad7cb790687b3 ?
Comment 2 Michael Osipov freebsd_committer freebsd_triage 2024-02-13 15:05:56 UTC
(In reply to Mark Johnston from comment #1)

there is no MFC, unfortunately...
Comment 3 rob2g2 2024-02-13 15:12:04 UTC
with FreeBSD 14 it works:
# echo "foobar" > /tmp/foobar.txt
# touch /tmp/mount.txt
# mount_nullfs /tmp/foobar.txt /tmp/mount.txt
# cat /tmp/mount.txt
foobar
# echo "more-foobar" > /tmp/mount.txt
# cat /tmp/foobar.txt
more-foobar
# umount /tmp/mount.txt
Comment 4 Michael Osipov freebsd_committer freebsd_triage 2024-02-13 15:20:15 UTC
Here it is:
stable/14: 521fbb722c33663cf00a83bca70ad7cb790687b3
stable/13: d373650a53977de4aba1da737525bd37eeba72ca

The commit in main lacks attribution and the backports lack cherry-pick attribution.
Comment 5 Mark Johnston freebsd_committer freebsd_triage 2024-02-13 15:23:12 UTC
(In reply to Michael Osipov from comment #4)
The commit in stable/14 is the same as the one in main, the feature appeared before stable/14 was created.  The stable/13 commit has an annotation pointing to the original commit.  What missing attribution are you referring to?
Comment 6 Michael Osipov freebsd_committer freebsd_triage 2024-02-13 15:26:23 UTC
(In reply to Mark Johnston from comment #5)

Yeah, it is only stable/14, but MFC is missing on main that is why I am surprised why it did find it on stable branches.

Question is now: Is 521fbb722c33663cf00a83bca70ad7cb790687b3 on 14.0-RELEASE?
Comment 7 Mark Johnston freebsd_committer freebsd_triage 2024-02-13 15:28:28 UTC
(In reply to Michael Osipov from comment #6)
> Yeah, it is only stable/14, but MFC is missing on main that is why I am surprised why it did find it on stable branches.

You mean the commit on main doesn't have a "MFC after" tag?  Sure, that's fine.  Sometimes one forgets to add it.

> Question is now: Is 521fbb722c33663cf00a83bca70ad7cb790687b3 on 14.0-RELEASE?

Yes.
Comment 8 Michael Osipov freebsd_committer freebsd_triage 2024-02-13 15:30:31 UTC
(In reply to vini.ipsmaker from comment #0)

As it seems everything is there, you should give it a try, no?
Comment 9 vini.ipsmaker 2024-02-13 16:27:02 UTC
I've tested here on 14.0-RELEASE. It works for regular files, but it doesn't work for UNIX sockets:

Output (after I started an UNIX socket server at /tmp/sock)

# > /tmp/sock2
# mount_nullfs /tmp/sock /tmp/sock2
mount_nullfs: /tmp/sock: must be either a file or directory
Comment 10 vini.ipsmaker 2024-02-13 16:48:47 UTC
It also doesn't work for character/block files (mknod):

# mount_nullfs /dev/tty /tmp/mount.txt
mount_nullfs: /dev/tty: must be either a file or directory

Not the safest thing in the world to do (attack surface would be huge which would be hard to audit), but some clients don't care about that and would like to have containerized applications that can offload workloads to specialized hardware anyway (GPUs being one example).

However it doesn't matter that much on FreeBSD (one can just call mknod directly before starting the jail I guess). I'm just mentioning for the sake of completeness because this also works on Linux (on Linux is more important because the way user/mount namespaces work).
Comment 11 dfr 2024-02-14 13:27:49 UTC
(In reply to vini.ipsmaker from comment #10)

It seems reasonable to allow mounting sockets and fifos but certainly not for devices - having device nodes outside of devfs has not worked since about FreeBSD 6.0 (as mentioned in the manual for mknod(1)). The correct way of exposing devices in jails is to use devfs rules to add specific nodes to the jail's devfs. For podman users, the '--device' argument to 'podman run' and 'podman create' is supported on FreeBSD and under the hood, it adds suitable devfs rules to the container jail.
Comment 12 vini.ipsmaker 2024-02-14 18:39:46 UTC
> The correct way of exposing devices in jails is to use devfs rules to add specific nodes to the jail's devfs. For podman users, the '--device' argument to 'podman run' and 'podman create' is supported on FreeBSD and under the hood, it adds suitable devfs rules to the container jail.

This seems very reasonable to me. So we only need null-mount support for UNIX sockets and FIFOs now.