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