]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsblk: use prefix for DM name
authorKarel Zak <kzak@redhat.com>
Tue, 19 Jun 2018 10:02:52 +0000 (12:02 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 21 Jun 2018 11:19:28 +0000 (13:19 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/canonicalize.h
lib/canonicalize.c
misc-utils/lsblk.c

index 0a292fc32cf372f8415e2dd7396fbf93e9f79d5a..ff6ef0dd6e49ae2faee08271ba49057b63b1a30a 100644 (file)
@@ -18,6 +18,7 @@
 extern char *canonicalize_path(const char *path);
 extern char *canonicalize_path_restricted(const char *path);
 extern char *canonicalize_dm_name(const char *ptname);
+extern char *__canonicalize_dm_name(const char *prefix, const char *ptname);
 
 extern char *absolute_path(const char *path);
 
index ef3342b12b1e5294bcf1ab3fe76f8f177c7480de..f3a2a3af29d0880acfd8e5bd847eb7c147a1aae3 100644 (file)
@@ -24,7 +24,7 @@
  * Since 2.6.29 (patch 784aae735d9b0bba3f8b9faef4c8b30df3bf0128) kernel sysfs
  * provides the real DM device names in /sys/block/<ptname>/dm/name
  */
-char *canonicalize_dm_name(const char *ptname)
+char *__canonicalize_dm_name(const char *prefix, const char *ptname)
 {
        FILE    *f;
        size_t  sz;
@@ -33,7 +33,10 @@ char *canonicalize_dm_name(const char *ptname)
        if (!ptname || !*ptname)
                return NULL;
 
-       snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptname);
+       if (!prefix)
+               prefix = "";
+
+       snprintf(path, sizeof(path), "%s/sys/block/%s/dm/name", prefix, ptname);
        if (!(f = fopen(path, "r" UL_CLOEXECSTR)))
                return NULL;
 
@@ -42,13 +45,18 @@ char *canonicalize_dm_name(const char *ptname)
                name[sz - 1] = '\0';
                snprintf(path, sizeof(path), _PATH_DEV_MAPPER "/%s", name);
 
-               if (access(path, F_OK) == 0)
+               if (prefix || access(path, F_OK) == 0)
                        res = strdup(path);
        }
        fclose(f);
        return res;
 }
 
+char *canonicalize_dm_name(const char *ptname)
+{
+       return __canonicalize_dm_name(NULL, ptname);
+}
+
 static int is_dm_devname(char *canonical, char **name)
 {
        struct stat sb;
index 2d0c981274ece5ce4219f3c19a2e5479ddc8c5de..26b58ab4e727adb1f5e70e46bf64286dfedfcfbb 100644 (file)
@@ -446,7 +446,7 @@ static char *get_device_path(struct blkdev_cxt *cxt)
        assert(cxt->name);
 
        if (is_dm(cxt->name))
-               return canonicalize_dm_name(cxt->name);
+               return __canonicalize_dm_name(lsblk->sysroot, cxt->name);
 
        snprintf(path, sizeof(path), "/dev/%s", cxt->name);
        sysfs_devname_sys_to_dev(path);
@@ -1473,9 +1473,17 @@ static int iterate_block_devices(void)
        DIR *dir;
        struct dirent *d;
        struct blkdev_cxt cxt = { NULL };
+       struct path_cxt *pc = ul_new_path(_PATH_SYS_BLOCK);
 
-       if (!(dir = opendir(_PATH_SYS_BLOCK)))
-               return -errno;
+       if (!pc)
+               err(EXIT_FAILURE, _("failed to allocate /sys handler"));
+
+       ul_path_set_prefix(pc, lsblk->sysroot);
+
+       /* TODO: reuse @pc in set_cxt(), etc. */
+       dir = ul_path_opendir(pc, NULL);
+       if (!dir)
+               goto done;
 
        DBG(DEV, ul_debug("iterate on " _PATH_SYS_BLOCK));
 
@@ -1499,7 +1507,8 @@ static int iterate_block_devices(void)
        }
 
        closedir(dir);
-
+done:
+       ul_unref_path(pc);
        DBG(DEV, ul_debug("iterate on " _PATH_SYS_BLOCK " -- done"));
        return 0;
 }
@@ -1929,6 +1938,7 @@ int main(int argc, char *argv[])
 
        mnt_init_debug(0);
        scols_init_debug(0);
+       ul_path_init_debug();
 
        /*
         * initialize output columns