Remounting read-only without changing other options












4















Working on systemd-shutdown, it is using mount() to remount filesystems as read-only.



/* MS_REMOUNT requires that the data parameter
* should be the same from the original mount
* except for the desired changes. Since we want
* to remount read-only, we should filter out
* rw (and ro too, because it confuses the kernel) */
...filter_options(m->options, "rwro", NULL, NULL, &options);

...mount(NULL, m->path, NULL, MS_REMOUNT|MS_RDONLY, options)...


But when I looked at strace mount -oremount,rw /boot, the mount system call was passed NULL as its last argument. Does that show it's not actually necessary to copy and mangle the old option string?










share|improve this question





























    4















    Working on systemd-shutdown, it is using mount() to remount filesystems as read-only.



    /* MS_REMOUNT requires that the data parameter
    * should be the same from the original mount
    * except for the desired changes. Since we want
    * to remount read-only, we should filter out
    * rw (and ro too, because it confuses the kernel) */
    ...filter_options(m->options, "rwro", NULL, NULL, &options);

    ...mount(NULL, m->path, NULL, MS_REMOUNT|MS_RDONLY, options)...


    But when I looked at strace mount -oremount,rw /boot, the mount system call was passed NULL as its last argument. Does that show it's not actually necessary to copy and mangle the old option string?










    share|improve this question



























      4












      4








      4








      Working on systemd-shutdown, it is using mount() to remount filesystems as read-only.



      /* MS_REMOUNT requires that the data parameter
      * should be the same from the original mount
      * except for the desired changes. Since we want
      * to remount read-only, we should filter out
      * rw (and ro too, because it confuses the kernel) */
      ...filter_options(m->options, "rwro", NULL, NULL, &options);

      ...mount(NULL, m->path, NULL, MS_REMOUNT|MS_RDONLY, options)...


      But when I looked at strace mount -oremount,rw /boot, the mount system call was passed NULL as its last argument. Does that show it's not actually necessary to copy and mangle the old option string?










      share|improve this question
















      Working on systemd-shutdown, it is using mount() to remount filesystems as read-only.



      /* MS_REMOUNT requires that the data parameter
      * should be the same from the original mount
      * except for the desired changes. Since we want
      * to remount read-only, we should filter out
      * rw (and ro too, because it confuses the kernel) */
      ...filter_options(m->options, "rwro", NULL, NULL, &options);

      ...mount(NULL, m->path, NULL, MS_REMOUNT|MS_RDONLY, options)...


      But when I looked at strace mount -oremount,rw /boot, the mount system call was passed NULL as its last argument. Does that show it's not actually necessary to copy and mangle the old option string?







      linux mount system-calls






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 9 at 18:42







      sourcejedi

















      asked Oct 18 '17 at 18:10









      sourcejedisourcejedi

      23.4k437103




      23.4k437103






















          1 Answer
          1






          active

          oldest

          votes


















          4














          No.



          If you edit /etc/fstab and add a non-generic option foo, then /sbin/mount will pass "foo" as the last argument to mount(). In your example, it was passing NULL, but I think it meant the empty string "". Presumably they have the same effect.





          Apparently /sbin/mount behaves differently if you pass both a path and device (!).




          The remount functionality follows the standard way the mount command works with options from fstab. This means that mount does not
          read fstab (or mtab) only when both device and dir are specified.



           mount -o remount,rw /dev/foo /dir


          After this call all old mount options are replaced and arbitrary stuff from fstab (or mtab) is ignored, except the loop= option
          which is internally generated and maintained by the mount command.



           mount -o remount,rw  /dir


          After this call, mount reads fstab and merges these options with the options from the command line (-o). If no mountpoint is found
          in fstab, then a remount with unspecified source is allowed.




          By implication, it seems that last case also overrides any current options of the mount point.





          The code quoted in the question is somewhat suspicious. It appears to reset the mount flags. Current behaviour:




          Remounting an existing mount



          ... The mountflags and data arguments should match the values used in the original mount() call, except for those parameters that are being deliberately changed.




          so that's probably where the comment came from.




          The following mountflags can be changed: MS_LAZYTIME, MS_MANDLOCK, MS_NOATIME, MS_NODEV, MS_NODIRATIME, MS_NOEXEC, MS_NOSUID, MS_RELATIME,
          MS_RDONLY, and MS_SYNCHRONOUS.




          I guess the code is currently working ok. Remounting as read-only avoids questions about subsequent writes, or a read-write remount being denied because the blockdev is readonly. And systemd-shutdown should have sent SIGKILL to any other processes with access to the filesystem by this point, so we can probably ignore security options like NOEXEC.




          Attempts to change the setting of the MS_DIRSYNC flag during a remount are silently ignored.




          Although it implies other flags might not be silently ignored and could cause the call to fail if they don't match, I don't think the core kernel code does anything like that.




          Since Linux 3.17, if none of MS_NOATIME, MS_NODIRATIME, MS_RELATIME, or MS_STRICTATIME is specified in mountflags, then the remount operation preserves the existing values of these flags (rather than defaulting to MS_RELATIME).



          Since Linux 2.6.26, this flag can also be used to make an existing bind mount read-only by specifying mountflags as:



          MS_REMOUNT | MS_BIND | MS_RDONLY



          I guess it's just as well systemd-shutdown didn't process fstab the exact same way mount -oremount,ro /boot would have :). (The old sysvinit scripts are ok at least in Debian, because they don't use remount RO for anything other than the root filesystem).






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "106"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f398941%2fremounting-read-only-without-changing-other-options%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            4














            No.



            If you edit /etc/fstab and add a non-generic option foo, then /sbin/mount will pass "foo" as the last argument to mount(). In your example, it was passing NULL, but I think it meant the empty string "". Presumably they have the same effect.





            Apparently /sbin/mount behaves differently if you pass both a path and device (!).




            The remount functionality follows the standard way the mount command works with options from fstab. This means that mount does not
            read fstab (or mtab) only when both device and dir are specified.



             mount -o remount,rw /dev/foo /dir


            After this call all old mount options are replaced and arbitrary stuff from fstab (or mtab) is ignored, except the loop= option
            which is internally generated and maintained by the mount command.



             mount -o remount,rw  /dir


            After this call, mount reads fstab and merges these options with the options from the command line (-o). If no mountpoint is found
            in fstab, then a remount with unspecified source is allowed.




            By implication, it seems that last case also overrides any current options of the mount point.





            The code quoted in the question is somewhat suspicious. It appears to reset the mount flags. Current behaviour:




            Remounting an existing mount



            ... The mountflags and data arguments should match the values used in the original mount() call, except for those parameters that are being deliberately changed.




            so that's probably where the comment came from.




            The following mountflags can be changed: MS_LAZYTIME, MS_MANDLOCK, MS_NOATIME, MS_NODEV, MS_NODIRATIME, MS_NOEXEC, MS_NOSUID, MS_RELATIME,
            MS_RDONLY, and MS_SYNCHRONOUS.




            I guess the code is currently working ok. Remounting as read-only avoids questions about subsequent writes, or a read-write remount being denied because the blockdev is readonly. And systemd-shutdown should have sent SIGKILL to any other processes with access to the filesystem by this point, so we can probably ignore security options like NOEXEC.




            Attempts to change the setting of the MS_DIRSYNC flag during a remount are silently ignored.




            Although it implies other flags might not be silently ignored and could cause the call to fail if they don't match, I don't think the core kernel code does anything like that.




            Since Linux 3.17, if none of MS_NOATIME, MS_NODIRATIME, MS_RELATIME, or MS_STRICTATIME is specified in mountflags, then the remount operation preserves the existing values of these flags (rather than defaulting to MS_RELATIME).



            Since Linux 2.6.26, this flag can also be used to make an existing bind mount read-only by specifying mountflags as:



            MS_REMOUNT | MS_BIND | MS_RDONLY



            I guess it's just as well systemd-shutdown didn't process fstab the exact same way mount -oremount,ro /boot would have :). (The old sysvinit scripts are ok at least in Debian, because they don't use remount RO for anything other than the root filesystem).






            share|improve this answer




























              4














              No.



              If you edit /etc/fstab and add a non-generic option foo, then /sbin/mount will pass "foo" as the last argument to mount(). In your example, it was passing NULL, but I think it meant the empty string "". Presumably they have the same effect.





              Apparently /sbin/mount behaves differently if you pass both a path and device (!).




              The remount functionality follows the standard way the mount command works with options from fstab. This means that mount does not
              read fstab (or mtab) only when both device and dir are specified.



               mount -o remount,rw /dev/foo /dir


              After this call all old mount options are replaced and arbitrary stuff from fstab (or mtab) is ignored, except the loop= option
              which is internally generated and maintained by the mount command.



               mount -o remount,rw  /dir


              After this call, mount reads fstab and merges these options with the options from the command line (-o). If no mountpoint is found
              in fstab, then a remount with unspecified source is allowed.




              By implication, it seems that last case also overrides any current options of the mount point.





              The code quoted in the question is somewhat suspicious. It appears to reset the mount flags. Current behaviour:




              Remounting an existing mount



              ... The mountflags and data arguments should match the values used in the original mount() call, except for those parameters that are being deliberately changed.




              so that's probably where the comment came from.




              The following mountflags can be changed: MS_LAZYTIME, MS_MANDLOCK, MS_NOATIME, MS_NODEV, MS_NODIRATIME, MS_NOEXEC, MS_NOSUID, MS_RELATIME,
              MS_RDONLY, and MS_SYNCHRONOUS.




              I guess the code is currently working ok. Remounting as read-only avoids questions about subsequent writes, or a read-write remount being denied because the blockdev is readonly. And systemd-shutdown should have sent SIGKILL to any other processes with access to the filesystem by this point, so we can probably ignore security options like NOEXEC.




              Attempts to change the setting of the MS_DIRSYNC flag during a remount are silently ignored.




              Although it implies other flags might not be silently ignored and could cause the call to fail if they don't match, I don't think the core kernel code does anything like that.




              Since Linux 3.17, if none of MS_NOATIME, MS_NODIRATIME, MS_RELATIME, or MS_STRICTATIME is specified in mountflags, then the remount operation preserves the existing values of these flags (rather than defaulting to MS_RELATIME).



              Since Linux 2.6.26, this flag can also be used to make an existing bind mount read-only by specifying mountflags as:



              MS_REMOUNT | MS_BIND | MS_RDONLY



              I guess it's just as well systemd-shutdown didn't process fstab the exact same way mount -oremount,ro /boot would have :). (The old sysvinit scripts are ok at least in Debian, because they don't use remount RO for anything other than the root filesystem).






              share|improve this answer


























                4












                4








                4







                No.



                If you edit /etc/fstab and add a non-generic option foo, then /sbin/mount will pass "foo" as the last argument to mount(). In your example, it was passing NULL, but I think it meant the empty string "". Presumably they have the same effect.





                Apparently /sbin/mount behaves differently if you pass both a path and device (!).




                The remount functionality follows the standard way the mount command works with options from fstab. This means that mount does not
                read fstab (or mtab) only when both device and dir are specified.



                 mount -o remount,rw /dev/foo /dir


                After this call all old mount options are replaced and arbitrary stuff from fstab (or mtab) is ignored, except the loop= option
                which is internally generated and maintained by the mount command.



                 mount -o remount,rw  /dir


                After this call, mount reads fstab and merges these options with the options from the command line (-o). If no mountpoint is found
                in fstab, then a remount with unspecified source is allowed.




                By implication, it seems that last case also overrides any current options of the mount point.





                The code quoted in the question is somewhat suspicious. It appears to reset the mount flags. Current behaviour:




                Remounting an existing mount



                ... The mountflags and data arguments should match the values used in the original mount() call, except for those parameters that are being deliberately changed.




                so that's probably where the comment came from.




                The following mountflags can be changed: MS_LAZYTIME, MS_MANDLOCK, MS_NOATIME, MS_NODEV, MS_NODIRATIME, MS_NOEXEC, MS_NOSUID, MS_RELATIME,
                MS_RDONLY, and MS_SYNCHRONOUS.




                I guess the code is currently working ok. Remounting as read-only avoids questions about subsequent writes, or a read-write remount being denied because the blockdev is readonly. And systemd-shutdown should have sent SIGKILL to any other processes with access to the filesystem by this point, so we can probably ignore security options like NOEXEC.




                Attempts to change the setting of the MS_DIRSYNC flag during a remount are silently ignored.




                Although it implies other flags might not be silently ignored and could cause the call to fail if they don't match, I don't think the core kernel code does anything like that.




                Since Linux 3.17, if none of MS_NOATIME, MS_NODIRATIME, MS_RELATIME, or MS_STRICTATIME is specified in mountflags, then the remount operation preserves the existing values of these flags (rather than defaulting to MS_RELATIME).



                Since Linux 2.6.26, this flag can also be used to make an existing bind mount read-only by specifying mountflags as:



                MS_REMOUNT | MS_BIND | MS_RDONLY



                I guess it's just as well systemd-shutdown didn't process fstab the exact same way mount -oremount,ro /boot would have :). (The old sysvinit scripts are ok at least in Debian, because they don't use remount RO for anything other than the root filesystem).






                share|improve this answer













                No.



                If you edit /etc/fstab and add a non-generic option foo, then /sbin/mount will pass "foo" as the last argument to mount(). In your example, it was passing NULL, but I think it meant the empty string "". Presumably they have the same effect.





                Apparently /sbin/mount behaves differently if you pass both a path and device (!).




                The remount functionality follows the standard way the mount command works with options from fstab. This means that mount does not
                read fstab (or mtab) only when both device and dir are specified.



                 mount -o remount,rw /dev/foo /dir


                After this call all old mount options are replaced and arbitrary stuff from fstab (or mtab) is ignored, except the loop= option
                which is internally generated and maintained by the mount command.



                 mount -o remount,rw  /dir


                After this call, mount reads fstab and merges these options with the options from the command line (-o). If no mountpoint is found
                in fstab, then a remount with unspecified source is allowed.




                By implication, it seems that last case also overrides any current options of the mount point.





                The code quoted in the question is somewhat suspicious. It appears to reset the mount flags. Current behaviour:




                Remounting an existing mount



                ... The mountflags and data arguments should match the values used in the original mount() call, except for those parameters that are being deliberately changed.




                so that's probably where the comment came from.




                The following mountflags can be changed: MS_LAZYTIME, MS_MANDLOCK, MS_NOATIME, MS_NODEV, MS_NODIRATIME, MS_NOEXEC, MS_NOSUID, MS_RELATIME,
                MS_RDONLY, and MS_SYNCHRONOUS.




                I guess the code is currently working ok. Remounting as read-only avoids questions about subsequent writes, or a read-write remount being denied because the blockdev is readonly. And systemd-shutdown should have sent SIGKILL to any other processes with access to the filesystem by this point, so we can probably ignore security options like NOEXEC.




                Attempts to change the setting of the MS_DIRSYNC flag during a remount are silently ignored.




                Although it implies other flags might not be silently ignored and could cause the call to fail if they don't match, I don't think the core kernel code does anything like that.




                Since Linux 3.17, if none of MS_NOATIME, MS_NODIRATIME, MS_RELATIME, or MS_STRICTATIME is specified in mountflags, then the remount operation preserves the existing values of these flags (rather than defaulting to MS_RELATIME).



                Since Linux 2.6.26, this flag can also be used to make an existing bind mount read-only by specifying mountflags as:



                MS_REMOUNT | MS_BIND | MS_RDONLY



                I guess it's just as well systemd-shutdown didn't process fstab the exact same way mount -oremount,ro /boot would have :). (The old sysvinit scripts are ok at least in Debian, because they don't use remount RO for anything other than the root filesystem).







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Oct 18 '17 at 18:10









                sourcejedisourcejedi

                23.4k437103




                23.4k437103






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f398941%2fremounting-read-only-without-changing-other-options%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Morgemoulin

                    Scott Moir

                    Souastre