• NetBSD
  • Mounting union filesystem on read only mountpoint

Hi,

i recently tried using mount_union (or rather mount -t union) to mount a rw mounted directory on a mountpoint residing inside a ro mounted filesystem. Sadly mount fails with "Operation not supported". Trying the same command with the filesystem of the target mountpoint being mounted rw works just fine. Am i missing something here or is there any possibility to work around this issue? Overlaying a writable directory on top of a read-only directory is quite useful sometimes and it would be sad if it's just not possible.

  • rvp replied to this.

    nettester i recently tried using mount_union (or rather mount -t union) to mount a rw mounted directory on a mountpoint residing inside a ro mounted filesystem.

    This works if the underlying FS is ffs, msdos, ntfs (others not tested; ext2 doesn't work for some reason):

    Script started on Wed Jul 19 05:08:27 2023
    
    /root# uname -a
    NetBSD x202e.localdomain 9.3_STABLE NetBSD 9.3_STABLE (MYKERNEL) #0: Sat Jul  8 06:16:26 UTC 2023  bld@x202e.localdomain:/tmp/obj/usr/src/sys/arch/amd64/compile/MYKERNEL amd64
    /root# mkdir /tmp/xxx
    /root# echo a > /tmp/xxx/a
    /root# echo b > /tmp/xxx/b
    /root# mount -r -t ntfs /dev/dk1 /mnt
    /root# mount -t union /tmp/xxx /mnt
    /root# ls /mnt
    $AttrDef                          $UpCase                           Boot                              SWPackage.ini
    $BadClus                          $Volume                           DetectResult.ini                  Sources
    $Bitmap                           AsFac.log                         EFI                               System Volume Information
    $Boot                             AsPartition.ini                   FACLog                            a
    $Extend                           AsTCDProc_20121102075803.log      OEM                               b
    $LogFile                          AsTCDProc_20121102090634.log      PASS.txt                          bootmgr
    $MFTMirr                          AsTool.state                      PQA.CMD                           bootmgr.efi
    $Secure                           AsToolCDVer.txt                   Recovery                          en-us
    /root# rm /mnt/a /mnt/b
    /root# echo c > /mnt/c
    /root# rm /mnt/c
    /root# mount
    /dev/dk14 on / type ffs (log, noatime, local)
    kernfs on /kern type kernfs (local)
    ptyfs on /dev/pts type ptyfs (local)
    procfs on /proc type procfs (local)
    tmpfs on /var/shm type tmpfs (local)
    tmpfs on /tmp type tmpfs (local)
    /dev/dk1 on /mnt type ntfs (read-only, local)
    <above>:/tmp/xxx on /mnt type union (local)
    /root# umount /mnt
    /root# umount /mnt
    /root# ^D
    
    Script done on Wed Jul 19 05:11:06 2023

      rvp

      Wow, thanks a lot! I think you nailed it. The underlying filesystem is in fact ext2 (i am not entirely sure why it worked in the beginning but i figure there is a possibility that i had a tmpfs mounted during my experiments and not the real final filesystem).

      So the "workaround" is probably to use something that isn't ext2. That should'nt be much of a problem. The only reason i was using ext2 in the first place was that i could automate generating disk images using mke2fs but since i've already setup a vm for automated package building anyways i guess i could also use it to build disk images and completely skip ext2 in favor of a more "native" filesystem.

      As far i as i am concerned the thread can be closed as solved or am i supposed to do that? Sorry, i am not entirely familiar with the customs around here yet.

      • Jay likes this.
      13 days later

      Just in case somewhere in the future anyone else wants their union mounted over a read only filesystem to be writable here are some quick&dirty patches. My advise would be to not expect much though. My knowledge of NetBSD's filesystem code is practically zero (especially the patch concerning union_lookup - i don't have the slightest clue what it's effect is or if it even makes sense). So if it makes your system blow up (or probably rather lose data) i am very sorry but you have been warned. From my (very minimal) testing it seems to work (tm) and that's all i can say right now.

      Edit: Since i am seemingly to stupid to figure out how to format code i've moved the diffs to a pastebin service.

      union.diff: https://paste.c-net.org/CombingRelaxed
      union_mount.diff: https://paste.c-net.org/UsefulCylons

      • rvp replied to this.
      • Jay likes this.
        5 days later

        nettester anyone else wants their union mounted over a read only filesystem to be writable

        Hmm. Doesn't seem to be needed on 10.99.7 (latest -HEAD image downloaded just now):

        $ uname -r
        10.99.7
        $ install -d -m 700 -o rvp -g rvp /tmp/mnt	# create union-suitable dir.
        $ sudo mount -r /dev/dk3 /mnt			# mount RO partition
        $ sudo mount -t union /tmp/mnt /mnt		# mount RW union dir. over RO part.
        $ ls -l /mnt/etc/passwd
        -rw-r--r--  1 root  wheel  1619 Jul  1 04:40 /mnt/etc/passwd
        $ ed /mnt/etc/passwd
        1619
        /rvp/
        rvp:*:1001:1001:RVP:/home/rvp:/bin/sh
        s/1001/0/g
        p
        rvp:*:0:0:RVP:/home/rvp:/bin/sh
        w
        1613
        q
        $ ls -l /mnt/etc/passwd
        -rw-r--r--  1 rvp  rvp  1613 Aug  6 22:49 /mnt/etc/passwd
        $ sudo umount /mnt
        $ sudo umount /mnt

        Maybe you should file a PR for 9.3?

          rvp Interesting! So i wasn't the first person it seems. It makes a lot of sense though. The lower layer dictating the read/write ability is probably not what people expect most of the time.

          Concerning filing a PR i am not sure what you mean. A PR on backporting the functionality from 10.99.7 to 9.3? The quality of my own code here is way to low to be PR worthy. Like i said above there is parts where i am not really sure about the effect (mostly the read-only check in union_lookup) and i didn't spend a lot of thought on it in general (is copyup really a good solution for union_access?). I merely punched in code. Also i have no idea how UNMNT_BITS works and if/how i am supposed to add the new flag to it. TL;DR: Dumping my code on unsuspecting victims as is probably isn't the best idea.

          • rvp replied to this.
          • Jay likes this.

            nettester Concerning filing a PR i am not sure what you mean. A PR on backporting the functionality from 10.99.7 to 9.3?

            Yes:

            This and this should already be in 10-BETA. Please ask for a pull-up to the 9.4 branch.