From 751c39383adaf5ff5a860516238d524b0e20f835 Mon Sep 17 00:00:00 2001
From: Dave Reisner <dreisner@archlinux.org>
Date: Wed, 2 Apr 2014 10:41:30 -0400
Subject: [PATCH] switch_root: verify initramfs by f_type, not devno

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.

[adopted from master for backport into 2.24.x branch]

Signed-off-by: Dave Reisner <dreisner@archlinux.org>
---
 sys-utils/switch_root.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/sys-utils/switch_root.c b/sys-utils/switch_root.c
index f26f7da..40e222d 100644
--- a/sys-utils/switch_root.c
+++ b/sys-utils/switch_root.c
@@ -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>
@@ -33,6 +34,8 @@
 #include <ctype.h>
 #include <dirent.h>
 
+#include <linux/magic.h>
+
 #include "c.h"
 #include "nls.h"
 #include "closestream.h"
@@ -174,12 +177,12 @@ 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 == RAMFS_MAGIC || stfs.f_type == TMPFS_MAGIC))
+				recursiveRemove(cfd);
+			else
+				warn(_("old root filesystem is not an initramfs"));
 
 			if (pid == 0)
 				exit(EXIT_SUCCESS);
-- 
1.9.1

