valeriyvdovin / rpms / systemd

Forked from rpms/systemd 3 years ago
Clone

Blame SOURCES/0037-gpt-auto-generator-do-not-assume-that-dev-block-u-u-.patch

a4b143
From cb6800ec93c7d49f5f43355fef44f6797759cc0a Mon Sep 17 00:00:00 2001
a4b143
From: Lennart Poettering <lennart@poettering.net>
a4b143
Date: Tue, 17 Sep 2013 18:04:40 -0500
a4b143
Subject: [PATCH] gpt-auto-generator: do not assume that /dev/block/%u:%u is
a4b143
 useable
a4b143
a4b143
The generator might run before udev, and udev sets up the /dev/block/
a4b143
symlinks, hence we cannot use them from the gpt generator. Instead,
a4b143
manually translate a major/minor to a device node.
a4b143
---
a4b143
 src/gpt-auto-generator/gpt-auto-generator.c | 109 +++++++++++++++++++---------
a4b143
 1 file changed, 74 insertions(+), 35 deletions(-)
a4b143
a4b143
diff --git a/src/gpt-auto-generator/gpt-auto-generator.c b/src/gpt-auto-generator/gpt-auto-generator.c
a4b143
index 880661e..ca54925 100644
a4b143
--- a/src/gpt-auto-generator/gpt-auto-generator.c
a4b143
+++ b/src/gpt-auto-generator/gpt-auto-generator.c
a4b143
@@ -55,18 +55,13 @@ static inline void blkid_free_probep(blkid_probe *b) {
a4b143
 }
a4b143
 #define _cleanup_blkid_freep_probe_ _cleanup_(blkid_free_probep)
a4b143
 
a4b143
-static int verify_gpt_partition(dev_t dev, sd_id128_t *type, unsigned *nr, char **fstype) {
a4b143
-        _cleanup_free_ char *t = NULL;
a4b143
+static int verify_gpt_partition(const char *node, sd_id128_t *type, unsigned *nr, char **fstype) {
a4b143
         _cleanup_blkid_freep_probe_ blkid_probe b = NULL;
a4b143
         const char *v;
a4b143
         int r;
a4b143
 
a4b143
-        r = asprintf(&t, "/dev/block/%u:%u", major(dev), minor(dev));
a4b143
-        if (r < 0)
a4b143
-                return -ENOMEM;
a4b143
-
a4b143
         errno = 0;
a4b143
-        b = blkid_new_probe_from_filename(t);
a4b143
+        b = blkid_new_probe_from_filename(node);
a4b143
         if (!b)
a4b143
                 return errno != 0 ? -errno : -ENOMEM;
a4b143
 
a4b143
@@ -237,8 +232,7 @@ static int add_home(const char *path, const char *fstype) {
a4b143
         return 0;
a4b143
 }
a4b143
 
a4b143
-static int enumerate_partitions(dev_t dev) {
a4b143
-        struct udev *udev;
a4b143
+static int enumerate_partitions(struct udev *udev, dev_t dev) {
a4b143
         struct udev_enumerate *e = NULL;
a4b143
         struct udev_device *parent = NULL, *d = NULL;
a4b143
         struct udev_list_entry *first, *item;
a4b143
@@ -246,10 +240,6 @@ static int enumerate_partitions(dev_t dev) {
a4b143
         _cleanup_free_ char *home = NULL, *home_fstype = NULL;
a4b143
         int r;
a4b143
 
a4b143
-        udev = udev_new();
a4b143
-        if (!udev)
a4b143
-                return log_oom();
a4b143
-
a4b143
         e = udev_enumerate_new(udev);
a4b143
         if (!e) {
a4b143
                 r = log_oom();
a4b143
@@ -294,7 +284,6 @@ static int enumerate_partitions(dev_t dev) {
a4b143
                 struct udev_device *q;
a4b143
                 sd_id128_t type_id;
a4b143
                 unsigned nr;
a4b143
-                dev_t sub;
a4b143
 
a4b143
                 q = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
a4b143
                 if (!q) {
a4b143
@@ -314,12 +303,10 @@ static int enumerate_partitions(dev_t dev) {
a4b143
                         goto finish;
a4b143
                 }
a4b143
 
a4b143
-                sub = udev_device_get_devnum(q);
a4b143
-
a4b143
-                r = verify_gpt_partition(sub, &type_id, &nr, &fstype);
a4b143
+                r = verify_gpt_partition(node, &type_id, &nr, &fstype);
a4b143
                 if (r < 0) {
a4b143
-                        log_error("Failed to verify GPT partition /dev/block/%u:%u: %s",
a4b143
-                                  major(sub), minor(sub), strerror(-r));
a4b143
+                        log_error("Failed to verify GPT partition %s: %s",
a4b143
+                                  node, strerror(-r));
a4b143
                         udev_device_unref(q);
a4b143
                         goto finish;
a4b143
                 }
a4b143
@@ -360,8 +347,6 @@ finish:
a4b143
         if (e)
a4b143
                 udev_enumerate_unref(e);
a4b143
 
a4b143
-        if (udev)
a4b143
-                udev_unref(udev);
a4b143
 
a4b143
         return r;
a4b143
 }
a4b143
@@ -440,13 +425,50 @@ static int get_block_device(const char *path, dev_t *dev) {
a4b143
         return 0;
a4b143
 }
a4b143
 
a4b143
+static int devno_to_devnode(struct udev *udev, dev_t devno, char **ret) {
a4b143
+        struct udev_device *d = NULL;
a4b143
+        const char *t;
a4b143
+        char *n;
a4b143
+        int r;
a4b143
+
a4b143
+        d = udev_device_new_from_devnum(udev, 'b', devno);
a4b143
+        if (!d) {
a4b143
+                r = log_oom();
a4b143
+                goto finish;
a4b143
+        }
a4b143
+
a4b143
+        t = udev_device_get_devnode(d);
a4b143
+        if (!t) {
a4b143
+                r = -ENODEV;
a4b143
+                goto finish;
a4b143
+        }
a4b143
+
a4b143
+        n = strdup(t);
a4b143
+        if (!n) {
a4b143
+                r = -ENOMEM;
a4b143
+                goto finish;
a4b143
+        }
a4b143
+
a4b143
+        *ret = n;
a4b143
+        r = 0;
a4b143
+
a4b143
+finish:
a4b143
+        if (d)
a4b143
+                udev_device_unref(d);
a4b143
+
a4b143
+        return r;
a4b143
+}
a4b143
+
a4b143
 int main(int argc, char *argv[]) {
a4b143
-        dev_t dev;
a4b143
+        _cleanup_free_ char *node = NULL;
a4b143
+        struct udev *udev = NULL;
a4b143
+        dev_t devno;
a4b143
         int r;
a4b143
 
a4b143
         if (argc > 1 && argc != 4) {
a4b143
                 log_error("This program takes three or no arguments.");
a4b143
-                return EXIT_FAILURE;
a4b143
+                r = -EINVAL;
a4b143
+                goto finish;
a4b143
         }
a4b143
 
a4b143
         if (argc > 1)
a4b143
@@ -458,31 +480,48 @@ int main(int argc, char *argv[]) {
a4b143
 
a4b143
         umask(0022);
a4b143
 
a4b143
-        if (in_initrd())
a4b143
-                return EXIT_SUCCESS;
a4b143
+        if (in_initrd()) {
a4b143
+                r = 0;
a4b143
+                goto finish;
a4b143
+        }
a4b143
 
a4b143
-        r = get_block_device("/", &dev;;
a4b143
+        r = get_block_device("/", &devno);
a4b143
         if (r < 0) {
a4b143
                 log_error("Failed to determine block device of root file system: %s", strerror(-r));
a4b143
-                return EXIT_FAILURE;
a4b143
+                goto finish;
a4b143
         }
a4b143
         if (r == 0) {
a4b143
                 log_debug("Root file system not on a (single) block device.");
a4b143
-                return EXIT_SUCCESS;
a4b143
+                goto finish;
a4b143
+        }
a4b143
+
a4b143
+        udev = udev_new();
a4b143
+        if (!udev) {
a4b143
+                r = log_oom();
a4b143
+                goto finish;
a4b143
+        }
a4b143
+
a4b143
+        r = devno_to_devnode(udev, devno, &node);
a4b143
+        if (r < 0) {
a4b143
+                log_error("Failed to determine block device node from major/minor: %s", strerror(-r));
a4b143
+                goto finish;
a4b143
         }
a4b143
 
a4b143
-        log_debug("Root device /dev/block/%u:%u.", major(dev), minor(dev));
a4b143
+        log_debug("Root device %s.", node);
a4b143
 
a4b143
-        r = verify_gpt_partition(dev, NULL, NULL, NULL);
a4b143
+        r = verify_gpt_partition(node, NULL, NULL, NULL);
a4b143
         if (r < 0) {
a4b143
-                log_error("Failed to verify GPT partition /dev/block/%u:%u: %s",
a4b143
-                          major(dev), minor(dev), strerror(-r));
a4b143
-                return EXIT_FAILURE;
a4b143
+                log_error("Failed to verify GPT partition %s: %s", node, strerror(-r));
a4b143
+                goto finish;
a4b143
         }
a4b143
         if (r == 0)
a4b143
-                return EXIT_SUCCESS;
a4b143
+                goto finish;
a4b143
+
a4b143
+        r = enumerate_partitions(udev, devno);
a4b143
 
a4b143
-        r = enumerate_partitions(dev);
a4b143
+finish:
a4b143
+        if (udev)
a4b143
+                udev_unref(udev);
a4b143
 
a4b143
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
a4b143
 }