Browse Source

lsblk: use prefix for DM name

Signed-off-by: Karel Zak <kzak@redhat.com>
pull/651/head
Karel Zak 4 years ago
parent
commit
5a89aa99a7
  1. 1
      include/canonicalize.h
  2. 14
      lib/canonicalize.c
  3. 18
      misc-utils/lsblk.c

1
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);

14
lib/canonicalize.c

@ -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;

18
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

Loading…
Cancel
Save