From 5a89aa99a7f13722102fa8730f4647da9138134d Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 19 Jun 2018 12:02:52 +0200 Subject: [PATCH] lsblk: use prefix for DM name Signed-off-by: Karel Zak --- include/canonicalize.h | 1 + lib/canonicalize.c | 14 +++++++++++--- misc-utils/lsblk.c | 18 ++++++++++++++---- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/include/canonicalize.h b/include/canonicalize.h index 0a292fc32c..ff6ef0dd6e 100644 --- a/include/canonicalize.h +++ b/include/canonicalize.h @@ -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); diff --git a/lib/canonicalize.c b/lib/canonicalize.c index ef3342b12b..f3a2a3af29 100644 --- a/lib/canonicalize.c +++ b/lib/canonicalize.c @@ -24,7 +24,7 @@ * Since 2.6.29 (patch 784aae735d9b0bba3f8b9faef4c8b30df3bf0128) kernel sysfs * provides the real DM device names in /sys/block//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; diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c index 2d0c981274..26b58ab4e7 100644 --- a/misc-utils/lsblk.c +++ b/misc-utils/lsblk.c @@ -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 -- 2.39.2