ramfs mounted through fstab is automatically owned by root

emmdee asked:

ramfs was chosen specifically because there is zero chance of files being written to disk (swap space is used with tmpfs). tmpfs is out of scope for this question.

Here is my fstab entry for the mount point:

ramfs /path/to/mount ramfs  defaults,nofail   0 0

After mounting, I can set the permissions just fine:

chown -R myuser:myuser /path/to/mount

The problem comes after a reboot. The owner becomes root after reboot and it manually has to change back to my user. All I need to do is have the user id set at mount time (or persist through reboots).

My answer:

ramfs has absolutely no mount options, so it isn’t possible to set a mount uid/gid as it is with tmpfs.

From the man page:

Ramfs is a memory based filesystem. Mount it and you have it. Unmount it and it is gone. Present since Linux 2.3.99pre4. There are no mount options.

Obviously you are aware of the danger of your system running out of memory and crashing because your user accidentally overflowed the ramfs, and you have accepted that risk. So we won’t mention that tmpfs solves this problem, and is why ramfs hasn’t gotten much developer attention and is rarely used. To quote the kernel developers:

One downside of ramfs is you can keep writing data into it until you fill
up all memory, and the VM can’t free it because the VM thinks that files
should get written to backing store (rather than swap space), but ramfs hasn’t
got any backing store. Because of this, only root (or a trusted user) should
be allowed write access to a ramfs mount.

A ramfs derivative called tmpfs was created to add size limits, and the ability
to write the data to swap space. Normal users can be allowed write access to
tmpfs mounts. See Documentation/filesystems/tmpfs.txt for more information.

The only way you can do this, therefore, is to continue to set the ownership after the filesystem is mounted, e.g. in rc.local.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.