You are not logged in.

#1 2011-02-23 19:07:41

emphire
Member
From: Canada
Registered: 2007-03-21
Posts: 189

[SOLVED] Mounting a directory as read-only with bind in fstab

I'm trying to figure out how to mount a directory (bind) as read-only in /etc/fstab.

Some background:

With the following entry in fstab:

/tmp/testmount /tmp/testmount_ro none bind,ro 0 0

I get the following error when trying to mount:

 $ mount /tmp/testmount_ro
mount: warning: /tmp/testmount_ro seems to be mounted read-write.
 $ touch /tmp/testmount_ro/test
 $ ls /tmp/testmount_ro
test
 $ rm /tmp/testmount_ro/test
 $ umount /tmp/testmount_ro/

Testing from the command line, I get the same error:

 $ mount /tmp/testmount /tmp/testmount_ro -o bind,ro
mount: warning: /tmp/testmount_ro seems to be mounted read-write.
 $ touch /tmp/testmount_ro/test
 $ ls /tmp/testmount_ro
test
 $ rm /tmp/testmount_ro/test

but I don't get the error if I remount it as read-only:

 $ mount /tmp/testmount /tmp/testmount_ro -o remount,ro
 $ touch /tmp/testmount_ro/test
touch: cannot touch `/tmp/testmount_ro/test': Read-only file system

but it turns out it's actually doing the read-only remount on the partition above (/tmp)

 $ touch /tmp/test
touch: cannot touch `/tmp/test': Read-only file system

Does anyone know how to get this working in fstab (or otherwise)?  Should I be using a different method for providing a read-only version of a directory?

Last edited by emphire (2011-02-24 00:49:26)

Offline

#2 2011-02-23 23:01:39

fukawi2
Forum Moderator
From: .vic.au
Registered: 2007-09-28
Posts: 5,520
Website

Re: [SOLVED] Mounting a directory as read-only with bind in fstab

From the mount manpage, it doesn't seem like you can do it:

Note  that  the  filesystem mount options will remain the same as those on the original mount point, and cannot be
changed by passing the -o option along with --bind/--rbind. The mount options can be changed by a separate remount
command, for example:

                     mount --bind olddir newdir
                     mount -o remount,ro newdir

Note  that  behavior  of  the remount operation depends on the /etc/mtab file. The first command stores the 'bind'
flag to the /etc/mtab file and the second command reads the flag from the file.  If you have a system without  the
/etc/mtab  file or if you explicitly define source and target for the remount command (then mount(8) does not read
/etc/mtab), then you have to use bind flag (or option) for the remount command too. For example:

                     mount --bind olddir newdir
                     mount -o remount,ro,bind olddir newdir

You might have to hack-solve it by adding the remount command to rc.local

Last edited by fukawi2 (2011-02-23 23:02:32)

Offline

#3 2011-02-24 00:48:47

emphire
Member
From: Canada
Registered: 2007-03-21
Posts: 189

Re: [SOLVED] Mounting a directory as read-only with bind in fstab

fukawi2 wrote:

You might have to hack-solve it by adding the remount command to rc.local

I was hoping to do it from /etc/fstab, but an rc.local hack it is!  Thanks!

Offline

Board footer

Powered by FluxBB