]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
systemd-growfs: use sd_device_new_from_devnum()
authorDevendra Tewari <devendra.tewari@gmail.com>
Fri, 26 Aug 2022 11:32:48 +0000 (08:32 -0300)
committerLuca Boccassi <luca.boccassi@gmail.com>
Fri, 26 Aug 2022 19:33:03 +0000 (20:33 +0100)
Use sd_device_new_from_devnum() instead of
device_path_make_major_minor_sysfs().

src/basic/devnum-util.c
src/basic/devnum-util.h
src/partition/growfs.c

index fbefa2bd69ca40556a5a90fce83473567fc5e440..70c07315c599f6743a11b3d76d171da7b2c36cc2 100644 (file)
@@ -5,7 +5,6 @@
 
 #include "chase-symlinks.h"
 #include "devnum-util.h"
-#include "fs-util.h"
 #include "parse-util.h"
 #include "path-util.h"
 #include "string-util.h"
@@ -58,53 +57,6 @@ int device_path_make_major_minor(mode_t mode, dev_t devnum, char **ret) {
         return 0;
 }
 
-int device_path_make_major_minor_sysfs(mode_t mode, dev_t devnum, char **ret) {
-        _cleanup_free_ char *syspath = NULL, *link = NULL, *fname = NULL;
-        _cleanup_free_ char *devpath = NULL;
-        const char *t;
-        int r;
-
-        /* Generates the /dev/... path given a dev_t. What makes this different
-         * from device_path_make_major_minor is that it works even when udev 
-         * hasn't yet run */
-
-        if (S_ISCHR(mode))
-                t = "char";
-        else if (S_ISBLK(mode))
-                t = "block";
-        else
-                return -ENODEV;
-
-        if (asprintf(&syspath, "/sys/dev/%s/" DEVNUM_FORMAT_STR, t, DEVNUM_FORMAT_VAL(devnum)) < 0)
-                return -ENOMEM;
-
-        r = readlink_malloc(syspath, &link);
-        if (r < 0)
-                return r;
-
-        r = path_extract_filename(link, &fname);
-        if (r < 0)
-                return r;
-
-        devpath = path_join("/dev", fname);
-        if (!devpath)
-                return -ENOMEM;
-
-        struct stat st;
-        if (stat(devpath, &st) < 0)
-                return -errno;
-
-        if (((st.st_mode ^ mode) & S_IFMT) != 0)
-                return S_ISBLK(mode) ? -ENOTBLK : -ENODEV;
-
-        if (st.st_rdev != devnum)
-                return -ENXIO;
-
-        *ret = TAKE_PTR(devpath);
-
-        return 0;
-}
-
 int device_path_make_canonical(mode_t mode, dev_t devnum, char **ret) {
         _cleanup_free_ char *p = NULL;
         int r;
index 7adb6fb249ee36637ccbb28a0ce731ecb8b00366..3f1894b2fd7875d8e0e54a05fa92446bb0314c1a 100644 (file)
@@ -29,7 +29,6 @@ int parse_devnum(const char *s, dev_t *ret);
         })
 
 int device_path_make_major_minor(mode_t mode, dev_t devnum, char **ret);
-int device_path_make_major_minor_sysfs(mode_t mode, dev_t devnum, char **ret);
 int device_path_make_canonical(mode_t mode, dev_t devnum, char **ret);
 int device_path_parse_major_minor(const char *path, mode_t *ret_mode, dev_t *ret_devnum);
 
index e940dccd48b96a89f3da15f995e2d54efe6d19a4..88e503c86d0ee8def69688ffa87958fb63084e88 100644 (file)
 #include <sys/types.h>
 #include <sys/vfs.h>
 
+#include "sd-device.h"
+
 #include "blockdev-util.h"
 #include "btrfs-util.h"
 #include "cryptsetup-util.h"
 #include "device-nodes.h"
+#include "device-util.h"
 #include "devnum-util.h"
 #include "dissect-image.h"
 #include "escape.h"
@@ -31,7 +34,8 @@ static bool arg_dry_run = false;
 
 #if HAVE_LIBCRYPTSETUP
 static int resize_crypt_luks_device(dev_t devno, const char *fstype, dev_t main_devno) {
-        _cleanup_free_ char *devpath = NULL, *main_devpath = NULL;
+        _cleanup_(sd_device_unrefp) sd_device *main_dev = NULL, *dev = NULL;
+        const char *devpath, *main_devpath;
         _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
         _cleanup_close_ int main_devfd = -1;
         uint64_t size;
@@ -41,22 +45,33 @@ static int resize_crypt_luks_device(dev_t devno, const char *fstype, dev_t main_
         if (r < 0)
                 return log_error_errno(r, "Cannot resize LUKS device: %m");
 
-        r = device_path_make_major_minor_sysfs(S_IFBLK, main_devno, &main_devpath);
+        r = sd_device_new_from_devnum(&main_dev, 'b', main_devno);
+        if (r < 0)
+                return log_error_errno(r, "Failed to create main sd-device for block device " DEVNUM_FORMAT_STR ": %m",
+                                       DEVNUM_FORMAT_VAL(main_devno));
+
+        r = sd_device_get_devpath(main_dev, &main_devpath);
         if (r < 0)
-                return log_error_errno(r, "Failed to format device major/minor path: %m");
+                return log_device_error_errno(main_dev, r, "Failed to get main devpath: %m");
 
-        main_devfd = open(main_devpath, O_RDONLY|O_CLOEXEC);
+        main_devfd = sd_device_open(main_dev, O_RDONLY|O_CLOEXEC);
         if (main_devfd < 0)
-                return log_error_errno(errno, "Failed to open \"%s\": %m", main_devpath);
+                return log_device_error_errno(main_dev, main_devfd, "Failed to open block device \"%s\": %m",
+                                              main_devpath);
 
         if (ioctl(main_devfd, BLKGETSIZE64, &size) != 0)
                 return log_error_errno(errno, "Failed to query size of \"%s\" (before resize): %m",
                                        main_devpath);
 
         log_debug("%s is %"PRIu64" bytes", main_devpath, size);
-        r = device_path_make_major_minor_sysfs(S_IFBLK, devno, &devpath);
+        r = sd_device_new_from_devnum(&dev, 'b', devno);
+        if (r < 0)
+                return log_error_errno(r, "Failed to create sd-device for block device " DEVNUM_FORMAT_STR ": %m",
+                                       DEVNUM_FORMAT_VAL(devno));
+
+        r = sd_device_get_devpath(dev, &devpath);
         if (r < 0)
-                return log_error_errno(r, "Failed to format major/minor path: %m");
+                return log_device_error_errno(dev, r, "Failed to get devpath: %m");
 
         r = sym_crypt_init(&cd, devpath);
         if (r < 0)
@@ -90,7 +105,9 @@ static int maybe_resize_underlying_device(
                 const char *mountpath,
                 dev_t main_devno) {
 
-        _cleanup_free_ char *fstype = NULL, *devpath = NULL;
+        _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
+        _cleanup_free_ char *fstype = NULL;
+        const char *devpath;
         dev_t devno;
         int r;
 
@@ -108,16 +125,21 @@ static int maybe_resize_underlying_device(
         if (devno == 0)
                 return log_error_errno(SYNTHETIC_ERRNO(ENODEV), "File system \"%s\" not backed by block device.", arg_target);
 
-        log_debug("Underlying device %d:%d, main dev %d:%d, %s",
-                  major(devno), minor(devno),
-                  major(main_devno), minor(main_devno),
+        log_debug("Underlying device " DEVNUM_FORMAT_STR ", main dev " DEVNUM_FORMAT_STR ", %s",
+                  DEVNUM_FORMAT_VAL(devno),
+                  DEVNUM_FORMAT_VAL(main_devno),
                   devno == main_devno ? "same" : "different");
         if (devno == main_devno)
                 return 0;
 
-        r = device_path_make_major_minor_sysfs(S_IFBLK, devno, &devpath);
+        r = sd_device_new_from_devnum(&dev, 'b', devno);
         if (r < 0)
-                return log_error_errno(r, "Failed to format device major/minor path: %m");
+                return log_error_errno(r, "Failed to create sd-device for block device " DEVNUM_FORMAT_STR ": %m",
+                                       DEVNUM_FORMAT_VAL(devno));
+
+        r = sd_device_get_devpath(dev, &devpath);
+        if (r < 0)
+                return log_device_error_errno(dev, r, "Failed to get devpath: %m");
 
         r = probe_filesystem(devpath, &fstype);
         if (r == -EUCLEAN)
@@ -202,8 +224,9 @@ static int parse_argv(int argc, char *argv[]) {
 }
 
 static int run(int argc, char *argv[]) {
+        _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
         _cleanup_close_ int mountfd = -1, devfd = -1;
-        _cleanup_free_ char *devpath = NULL;
+        const char *devpath;
         uint64_t size, newsize;
         struct stat st;
         dev_t devno;
@@ -237,13 +260,18 @@ static int run(int argc, char *argv[]) {
         if (r < 0)
                 log_warning_errno(r, "Unable to resize underlying device of \"%s\", proceeding anyway: %m", arg_target);
 
-        r = device_path_make_major_minor_sysfs(S_IFBLK, devno, &devpath);
+        r = sd_device_new_from_devnum(&dev, 'b', devno);
+        if (r < 0)
+                return log_error_errno(r, "Failed to create sd-device for block device " DEVNUM_FORMAT_STR ": %m",
+                                       DEVNUM_FORMAT_VAL(devno));
+
+        r = sd_device_get_devpath(dev, &devpath);
         if (r < 0)
-                return log_error_errno(r, "Failed to format device major/minor path: %m");
+                return log_device_error_errno(dev, r, "Failed to get devpath: %m");
 
-        devfd = open(devpath, O_RDONLY|O_CLOEXEC|O_NOCTTY);
+        devfd = sd_device_open(dev, O_RDONLY|O_CLOEXEC);
         if (devfd < 0)
-                return log_error_errno(errno, "Failed to open \"%s\": %m", devpath);
+                return log_device_error_errno(dev, devfd, "Failed to open block device \"%s\": %m", devpath);
 
         if (fstat(devfd, &st) < 0)
                 return log_error_errno(r, "Failed to stat() device %s: %m", devpath);