]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
switch_root: verify initramfs by f_type, not devno
authorDave Reisner <dreisner@archlinux.org>
Wed, 2 Apr 2014 14:41:30 +0000 (10:41 -0400)
committerKarel Zak <kzak@redhat.com>
Wed, 23 Apr 2014 09:55:08 +0000 (11:55 +0200)
As of linux 3.14, the initramfs device will have both major and
minor 0, causing our paranoia check to fail. Make this version agnostic
by checking the filesystem type, rather than a device number.

Signed-off-by: Dave Reisner <dreisner@archlinux.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/switch_root.c

index 1222fb1b49fe74fd98b3edbaf8896338d9c1a981..975360f01d39e7545a6fce5359c9daa078f11437 100644 (file)
@@ -23,6 +23,7 @@
 #include <sys/mount.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/statfs.h>
 #include <sys/param.h>
 #include <fcntl.h>
 #include <stdio.h>
 #define MNT_DETACH       0x00000002    /* Just detach from the tree */
 #endif
 
+#define STATFS_RAMFS_MAGIC      0x858458f6
+#define STATFS_TMPFS_MAGIC      0x01021994
+
+
 /* remove all files/directories below dirName -- don't cross mountpoints */
 static int recursiveRemove(int fd)
 {
@@ -177,12 +182,13 @@ static int switchroot(const char *newroot)
        if (cfd >= 0) {
                pid = fork();
                if (pid <= 0) {
-                       if (fstat(cfd, &sb) == 0) {
-                               if (sb.st_dev == makedev(0, 1))
-                                       recursiveRemove(cfd);
-                               else
-                                       warn(_("old root filesystem is not an initramfs"));
-                       }
+                       struct statfs stfs;
+                       if (fstatfs(cfd, &stfs) == 0 &&
+                           (stfs.f_type == STATFS_RAMFS_MAGIC ||
+                            stfs.f_type == STATFS_TMPFS_MAGIC))
+                               recursiveRemove(cfd);
+                       else
+                               warn(_("old root filesystem is not an initramfs"));
 
                        if (pid == 0)
                                exit(EXIT_SUCCESS);