d743bb5
From 207b45b46c41b84cbd2be29331c105308d66b3bd Mon Sep 17 00:00:00 2001
d743bb5
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
d743bb5
Date: Thu, 11 May 2017 12:12:41 -0400
d743bb5
Subject: [PATCH] pid1: improve logging when failing to remount / ro (#5940)
d743bb5
d743bb5
https://bugzilla.redhat.com/show_bug.cgi?id=1227736#c49
d743bb5
d743bb5
We counted how many filesystems could not be unmounted, but only for those
d743bb5
filesystems which we tried to unmount. Since we only remount / ro, without
d743bb5
attempting to unmount, we would emit a confusing error message:
d743bb5
d743bb5
Remounting '/' read-only with options 'seclabel,space_cache,subvolid=5,subvol=/'.
d743bb5
Remounting '/' read-only with options 'seclabel,space_cache,subvolid=5,subvol=/'.
d743bb5
Remounting '/' read-only with options 'seclabel,space_cache,subvolid=5,subvol=/'.
d743bb5
All filesystems unmounted.
d743bb5
d743bb5
Warn when remount-ro fails, and for filesystems which we won't try to unmount,
d743bb5
include the failure to remount-ro in n_failed.
d743bb5
d743bb5
A few minor cleanups:
d743bb5
- remove unecessary goto which jumps to the next line anyway
d743bb5
- always calculate n_failed, even if log_error is false. This causes no change
d743bb5
  in behaviour, but I think the code is easier to follow, since the log setting
d743bb5
  cannot influence other logic.
d743bb5
(cherry picked from commit c826cd3f7cfd950c8a86d57dfa6303f70de3e207)
d743bb5
---
d743bb5
 src/core/umount.c | 33 ++++++++++++++++++++-------------
d743bb5
 1 file changed, 20 insertions(+), 13 deletions(-)
d743bb5
d743bb5
diff --git a/src/core/umount.c b/src/core/umount.c
d743bb5
index 2f4b12bdb9..77b5bd9556 100644
d743bb5
--- a/src/core/umount.c
d743bb5
+++ b/src/core/umount.c
d743bb5
@@ -369,6 +369,14 @@ static int delete_dm(dev_t devnum) {
d743bb5
         return 0;
d743bb5
 }
d743bb5
 
d743bb5
+static bool nonunmountable_path(const char *path) {
d743bb5
+        return path_equal(path, "/")
d743bb5
+#ifndef HAVE_SPLIT_USR
d743bb5
+                || path_equal(path, "/usr")
d743bb5
+#endif
d743bb5
+                || path_startswith(path, "/run/initramfs");
d743bb5
+}
d743bb5
+
d743bb5
 static int mount_points_list_umount(MountPoint **head, bool *changed, bool log_error) {
d743bb5
         MountPoint *m, *n;
d743bb5
         int n_failed = 0;
d743bb5
@@ -404,21 +412,21 @@ static int mount_points_list_umount(MountPoint **head, bool *changed, bool log_e
d743bb5
                          * somehwere else via a bind mount. If we
d743bb5
                          * explicitly remount the super block of that
d743bb5
                          * alias read-only we hence should be
d743bb5
-                         * relatively safe regarding keeping the fs we
d743bb5
-                         * can otherwise not see dirty. */
d743bb5
+                         * relatively safe regarding keeping dirty an fs
d743bb5
+                         * we cannot otherwise see. */
d743bb5
                         log_info("Remounting '%s' read-only with options '%s'.", m->path, options);
d743bb5
-                        (void) mount(NULL, m->path, NULL, MS_REMOUNT|MS_RDONLY, options);
d743bb5
+                        if (mount(NULL, m->path, NULL, MS_REMOUNT|MS_RDONLY, options) < 0) {
d743bb5
+                                if (log_error)
d743bb5
+                                        log_notice_errno(errno, "Failed to remount '%s' read-only: %m", m->path);
d743bb5
+                                if (nonunmountable_path(m->path))
d743bb5
+                                        n_failed++;
d743bb5
+                        }
d743bb5
                 }
d743bb5
 
d743bb5
                 /* Skip / and /usr since we cannot unmount that
d743bb5
                  * anyway, since we are running from it. They have
d743bb5
                  * already been remounted ro. */
d743bb5
-                if (path_equal(m->path, "/")
d743bb5
-#ifndef HAVE_SPLIT_USR
d743bb5
-                    || path_equal(m->path, "/usr")
d743bb5
-#endif
d743bb5
-                    || path_startswith(m->path, "/run/initramfs")
d743bb5
-                )
d743bb5
+                if (nonunmountable_path(m->path))
d743bb5
                         continue;
d743bb5
 
d743bb5
                 /* Trying to umount. We don't force here since we rely
d743bb5
@@ -430,8 +438,9 @@ static int mount_points_list_umount(MountPoint **head, bool *changed, bool log_e
d743bb5
                                 *changed = true;
d743bb5
 
d743bb5
                         mount_point_free(head, m);
d743bb5
-                } else if (log_error) {
d743bb5
-                        log_warning_errno(errno, "Could not unmount %s: %m", m->path);
d743bb5
+                } else {
d743bb5
+                        if (log_error)
d743bb5
+                                log_warning_errno(errno, "Could not unmount %s: %m", m->path);
d743bb5
                         n_failed++;
d743bb5
                 }
d743bb5
         }
d743bb5
@@ -555,8 +564,6 @@ int umount_all(bool *changed) {
d743bb5
 
d743bb5
         /* umount one more time with logging enabled */
d743bb5
         r = mount_points_list_umount(&mp_list_head, &umount_changed, true);
d743bb5
-        if (r <= 0)
d743bb5
-                goto end;
d743bb5
 
d743bb5
   end:
d743bb5
         mount_points_list_free(&mp_list_head);