ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
9b3774
From c7532112a37ffdd3cc9851ae04fdcb543b99ed1c Mon Sep 17 00:00:00 2001
9b3774
From: Hubert Kario <hubert@kario.pl>
9b3774
Date: Sun, 20 Sep 2020 18:59:58 +0200
9b3774
Subject: [PATCH] Try stopping MD RAID devices in shutdown too
9b3774
9b3774
Currently the systemd-shutdown command attempts to stop swaps, DM
9b3774
(crypt, LVM2) and loop devices, but it doesn't attempt to stop MD
9b3774
RAID devices, which means that if the RAID is set up on crypt,
9b3774
loop, etc. device, it won't be able to stop those underlying devices.
9b3774
9b3774
This code extends the shutdown application to also attempt stopping
9b3774
the MD RAID devices.
9b3774
9b3774
Signed-off-by: Hubert Kario <hubert@kario.pl>
9b3774
(cherry picked from commit 0b220a5f2a31844eaa1f5426bab02d41d54f471c)
9b3774
9b3774
Resolves: #1817706
9b3774
---
9b3774
 src/core/shutdown.c |  37 +++++++++----
9b3774
 src/core/umount.c   | 125 ++++++++++++++++++++++++++++++++++++++++++++
9b3774
 src/core/umount.h   |   2 +
9b3774
 3 files changed, 154 insertions(+), 10 deletions(-)
9b3774
9b3774
diff --git a/src/core/shutdown.c b/src/core/shutdown.c
9b3774
index 038345b752..b8a983986a 100644
9b3774
--- a/src/core/shutdown.c
9b3774
+++ b/src/core/shutdown.c
9b3774
@@ -251,7 +251,7 @@ static void sync_with_progress(void) {
9b3774
 }
9b3774
 
9b3774
 int main(int argc, char *argv[]) {
9b3774
-        bool need_umount, need_swapoff, need_loop_detach, need_dm_detach;
9b3774
+        bool need_umount, need_swapoff, need_loop_detach, need_dm_detach, need_md_detach;
9b3774
         bool in_container, use_watchdog = false, can_initrd;
9b3774
         _cleanup_free_ char *cgroup = NULL;
9b3774
         char *arguments[3];
9b3774
@@ -331,6 +331,7 @@ int main(int argc, char *argv[]) {
9b3774
         need_swapoff = !in_container;
9b3774
         need_loop_detach = !in_container;
9b3774
         need_dm_detach = !in_container;
9b3774
+        need_md_detach = !in_container;
9b3774
         can_initrd = !in_container && !in_initrd() && access("/run/initramfs/shutdown", X_OK) == 0;
9b3774
 
9b3774
         /* Unmount all mountpoints, swaps, and loopback devices */
9b3774
@@ -383,6 +384,18 @@ int main(int argc, char *argv[]) {
9b3774
                                 log_error_errno(r, "Failed to detach loop devices: %m");
9b3774
                 }
9b3774
 
9b3774
+                if (need_md_detach) {
9b3774
+                        log_info("Stopping MD devices.");
9b3774
+                        r = md_detach_all(&changed, umount_log_level);
9b3774
+                        if (r == 0) {
9b3774
+                                need_md_detach = false;
9b3774
+                                log_info("All MD devices stopped.");
9b3774
+                        } else if (r > 0)
9b3774
+                                log_info("Not all MD devices stopped, %d left.", r);
9b3774
+                        else
9b3774
+                                log_error_errno(r, "Failed to stop MD devices: %m");
9b3774
+                }
9b3774
+
9b3774
                 if (need_dm_detach) {
9b3774
                         log_info("Detaching DM devices.");
9b3774
                         r = dm_detach_all(&changed, umount_log_level);
9b3774
@@ -395,8 +408,9 @@ int main(int argc, char *argv[]) {
9b3774
                                 log_error_errno(r, "Failed to detach DM devices: %m");
9b3774
                 }
9b3774
 
9b3774
-                if (!need_umount && !need_swapoff && !need_loop_detach && !need_dm_detach) {
9b3774
-                        log_info("All filesystems, swaps, loop devices and DM devices detached.");
9b3774
+                if (!need_umount && !need_swapoff && !need_loop_detach && !need_dm_detach
9b3774
+                            && !need_md_detach) {
9b3774
+                        log_info("All filesystems, swaps, loop devices, MD devices and DM devices detached.");
9b3774
                         /* Yay, done */
9b3774
                         break;
9b3774
                 }
9b3774
@@ -414,19 +428,21 @@ int main(int argc, char *argv[]) {
9b3774
                 /* If in this iteration we didn't manage to
9b3774
                  * unmount/deactivate anything, we simply give up */
9b3774
                 if (!changed) {
9b3774
-                        log_info("Cannot finalize remaining%s%s%s%s continuing.",
9b3774
+                        log_info("Cannot finalize remaining%s%s%s%s%s continuing.",
9b3774
                                  need_umount ? " file systems," : "",
9b3774
                                  need_swapoff ? " swap devices," : "",
9b3774
                                  need_loop_detach ? " loop devices," : "",
9b3774
-                                 need_dm_detach ? " DM devices," : "");
9b3774
+                                 need_dm_detach ? " DM devices," : "",
9b3774
+                                 need_md_detach ? " MD devices," : "");
9b3774
                         break;
9b3774
                 }
9b3774
 
9b3774
-                log_debug("Couldn't finalize remaining %s%s%s%s trying again.",
9b3774
+                log_debug("Couldn't finalize remaining %s%s%s%s%s trying again.",
9b3774
                           need_umount ? " file systems," : "",
9b3774
                           need_swapoff ? " swap devices," : "",
9b3774
                           need_loop_detach ? " loop devices," : "",
9b3774
-                          need_dm_detach ? " DM devices," : "");
9b3774
+                          need_dm_detach ? " DM devices," : "",
9b3774
+                          need_md_detach ? " MD devices," : "");
9b3774
         }
9b3774
 
9b3774
         /* We're done with the watchdog. */
9b3774
@@ -455,12 +471,13 @@ int main(int argc, char *argv[]) {
9b3774
 
9b3774
         }
9b3774
 
9b3774
-        if (need_umount || need_swapoff || need_loop_detach || need_dm_detach)
9b3774
-                log_error("Failed to finalize %s%s%s%s ignoring",
9b3774
+        if (need_umount || need_swapoff || need_loop_detach || need_dm_detach || need_md_detach)
9b3774
+                log_error("Failed to finalize%s%s%s%s%s ignoring.",
9b3774
                           need_umount ? " file systems," : "",
9b3774
                           need_swapoff ? " swap devices," : "",
9b3774
                           need_loop_detach ? " loop devices," : "",
9b3774
-                          need_dm_detach ? " DM devices," : "");
9b3774
+                          need_dm_detach ? " DM devices," : "",
9b3774
+                          need_md_detach ? " MD devices," : "");
9b3774
 
9b3774
         /* The kernel will automatically flush ATA disks and suchlike on reboot(), but the file systems need to be
9b3774
          * sync'ed explicitly in advance. So let's do this here, but not needlessly slow down containers. Note that we
9b3774
diff --git a/src/core/umount.c b/src/core/umount.c
9b3774
index 3f02bf141a..ed90c6b1fc 100644
9b3774
--- a/src/core/umount.c
9b3774
+++ b/src/core/umount.c
9b3774
@@ -5,6 +5,8 @@
9b3774
 
9b3774
 #include <errno.h>
9b3774
 #include <fcntl.h>
9b3774
+#include <linux/major.h>
9b3774
+#include <linux/raid/md_u.h>
9b3774
 #include <linux/loop.h>
9b3774
 #include <string.h>
9b3774
 #include <sys/mount.h>
9b3774
@@ -332,6 +334,66 @@ static int dm_list_get(MountPoint **head) {
9b3774
         return 0;
9b3774
 }
9b3774
 
9b3774
+static int md_list_get(MountPoint **head) {
9b3774
+        _cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
9b3774
+        struct udev_list_entry *item = NULL, *first = NULL;
9b3774
+        _cleanup_(udev_unrefp) struct udev *udev = NULL;
9b3774
+        int r;
9b3774
+
9b3774
+        assert(head);
9b3774
+
9b3774
+        udev = udev_new();
9b3774
+        if (!udev)
9b3774
+                return -ENOMEM;
9b3774
+
9b3774
+        e = udev_enumerate_new(udev);
9b3774
+        if (!e)
9b3774
+                return -ENOMEM;
9b3774
+
9b3774
+        r = udev_enumerate_add_match_subsystem(e, "block");
9b3774
+        if (r < 0)
9b3774
+                return r;
9b3774
+
9b3774
+        r = udev_enumerate_add_match_sysname(e, "md*");
9b3774
+        if (r < 0)
9b3774
+                return r;
9b3774
+
9b3774
+        first = udev_enumerate_get_list_entry(e);
9b3774
+        udev_list_entry_foreach(item, first) {
9b3774
+                _cleanup_(udev_device_unrefp) struct udev_device *d;
9b3774
+                _cleanup_free_ char *p = NULL;
9b3774
+                const char *dn;
9b3774
+                MountPoint *m;
9b3774
+                dev_t devnum;
9b3774
+
9b3774
+                d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
9b3774
+                if (!d)
9b3774
+                        return -ENOMEM;
9b3774
+
9b3774
+                devnum = udev_device_get_devnum(d);
9b3774
+                dn = udev_device_get_devnode(d);
9b3774
+                if (major(devnum) == 0 || !dn)
9b3774
+                        continue;
9b3774
+
9b3774
+                p = strdup(dn);
9b3774
+                if (!p)
9b3774
+                        return -ENOMEM;
9b3774
+
9b3774
+                m = new(MountPoint, 1);
9b3774
+                if (!m)
9b3774
+                        return -ENOMEM;
9b3774
+
9b3774
+                *m = (MountPoint) {
9b3774
+                        .path = TAKE_PTR(p),
9b3774
+                        .devnum = devnum,
9b3774
+                };
9b3774
+
9b3774
+                LIST_PREPEND(mount_point, *head, m);
9b3774
+        }
9b3774
+
9b3774
+        return 0;
9b3774
+}
9b3774
+
9b3774
 static int delete_loopback(const char *device) {
9b3774
         _cleanup_close_ int fd = -1;
9b3774
         int r;
9b3774
@@ -379,6 +441,23 @@ static int delete_dm(dev_t devnum) {
9b3774
         return 0;
9b3774
 }
9b3774
 
9b3774
+static int delete_md(MountPoint *m) {
9b3774
+
9b3774
+        _cleanup_close_ int fd = -1;
9b3774
+
9b3774
+        assert(major(m->devnum) != 0);
9b3774
+        assert(m->path != 0);
9b3774
+
9b3774
+        fd = open(m->path, O_RDONLY|O_CLOEXEC|O_EXCL);
9b3774
+        if (fd < 0)
9b3774
+                return -errno;
9b3774
+
9b3774
+        if (ioctl(fd, STOP_ARRAY, NULL) < 0)
9b3774
+                return -errno;
9b3774
+
9b3774
+        return 0;
9b3774
+}
9b3774
+
9b3774
 static bool nonunmountable_path(const char *path) {
9b3774
         return path_equal(path, "/")
9b3774
 #if ! HAVE_SPLIT_USR
9b3774
@@ -618,6 +697,37 @@ static int dm_points_list_detach(MountPoint **head, bool *changed, int umount_lo
9b3774
         return n_failed;
9b3774
 }
9b3774
 
9b3774
+static int md_points_list_detach(MountPoint **head, bool *changed, int umount_log_level) {
9b3774
+        MountPoint *m, *n;
9b3774
+        int n_failed = 0, r;
9b3774
+        dev_t rootdev = 0;
9b3774
+
9b3774
+        assert(head);
9b3774
+        assert(changed);
9b3774
+
9b3774
+        (void) get_block_device("/", &rootdev);
9b3774
+
9b3774
+        LIST_FOREACH_SAFE(mount_point, m, n, *head) {
9b3774
+                if (major(rootdev) != 0 && rootdev == m->devnum) {
9b3774
+                        n_failed ++;
9b3774
+                        continue;
9b3774
+                }
9b3774
+
9b3774
+                log_info("Stopping MD %s (%u:%u).", m->path, major(m->devnum), minor(m->devnum));
9b3774
+                r = delete_md(m);
9b3774
+                if (r < 0) {
9b3774
+                        log_full_errno(umount_log_level, r, "Could not stop MD %s: %m", m->path);
9b3774
+                        n_failed++;
9b3774
+                        continue;
9b3774
+                }
9b3774
+
9b3774
+                *changed = true;
9b3774
+                mount_point_free(head, m);
9b3774
+        }
9b3774
+
9b3774
+        return n_failed;
9b3774
+}
9b3774
+
9b3774
 static int umount_all_once(bool *changed, int umount_log_level) {
9b3774
         int r;
9b3774
         _cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, mp_list_head);
9b3774
@@ -696,3 +806,18 @@ int dm_detach_all(bool *changed, int umount_log_level) {
9b3774
 
9b3774
         return dm_points_list_detach(&dm_list_head, changed, umount_log_level);
9b3774
 }
9b3774
+
9b3774
+int md_detach_all(bool *changed, int umount_log_level) {
9b3774
+        _cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, md_list_head);
9b3774
+        int r;
9b3774
+
9b3774
+        assert(changed);
9b3774
+
9b3774
+        LIST_HEAD_INIT(md_list_head);
9b3774
+
9b3774
+        r = md_list_get(&md_list_head);
9b3774
+        if (r < 0)
9b3774
+                return r;
9b3774
+
9b3774
+        return md_points_list_detach(&md_list_head, changed, umount_log_level);
9b3774
+}
9b3774
diff --git a/src/core/umount.h b/src/core/umount.h
9b3774
index 6f2b24d195..b01062484f 100644
9b3774
--- a/src/core/umount.h
9b3774
+++ b/src/core/umount.h
9b3774
@@ -15,6 +15,8 @@ int loopback_detach_all(bool *changed, int umount_log_level);
9b3774
 
9b3774
 int dm_detach_all(bool *changed, int umount_log_level);
9b3774
 
9b3774
+int md_detach_all(bool *changed, int umount_log_level);
9b3774
+
9b3774
 /* This is exported just for testing */
9b3774
 typedef struct MountPoint {
9b3774
         char *path;