From: Karel Zak Date: Tue, 15 Oct 2019 11:08:09 +0000 (+0200) Subject: lsblk: read also GROUP,OWNER and MODE from dumps X-Git-Tag: v2.35-rc1~99 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6f74ede50aaa6a7471e5a123cacbaaf147e0578c;p=thirdparty%2Futil-linux.git lsblk: read also GROUP,OWNER and MODE from dumps Signed-off-by: Karel Zak --- diff --git a/misc-utils/lsblk-properties.c b/misc-utils/lsblk-properties.c index 8c0b28272f..f5549f4604 100644 --- a/misc-utils/lsblk-properties.c +++ b/misc-utils/lsblk-properties.c @@ -34,6 +34,10 @@ void lsblk_device_free_properties(struct lsblk_devprop *p) free(p->model); free(p->partflags); + free(p->mode); + free(p->owner); + free(p->group); + free(p); } @@ -185,6 +189,7 @@ static struct lsblk_devprop *get_properties_by_file(struct lsblk_device *ld) prop = ld->properties = xcalloc(1, sizeof(*ld->properties)); while (fgets(buf, sizeof(buf), fp) != NULL) { + /* udev based */ if (lookup(buf, "ID_FS_LABEL_ENC", &prop->label)) unhexmangle_string(prop->label); else if (lookup(buf, "ID_FS_UUID_ENC", &prop->uuid)) @@ -202,6 +207,12 @@ static struct lsblk_devprop *get_properties_by_file(struct lsblk_device *ld) else if (lookup(buf, "ID_WWN", &prop->wwn)) ; else if (lookup(buf, "ID_SCSI_SERIAL", &prop->serial)) ; else if (lookup(buf, "ID_SERIAL_SHORT", &prop->serial)) ; + + /* lsblk specific */ + else if (lookup(buf, "MODE", &prop->mode)) ; + else if (lookup(buf, "OWNER", &prop->owner)) ; + else if (lookup(buf, "GROUP", &prop->group)) ; + else continue; } diff --git a/misc-utils/lsblk.8 b/misc-utils/lsblk.8 index 2910ee356c..7a8e72b8bf 100644 --- a/misc-utils/lsblk.8 +++ b/misc-utils/lsblk.8 @@ -162,7 +162,8 @@ Print the zone model for each device. .BR " \-\-sysroot " \fIdirectory\fP Gather data for a Linux instance other than the instance from which the lsblk command is issued. The specified directory is the system root of the Linux -instance to be inspected. This option is designed for the testing purpose. +instance to be inspected. The real device nodes in the target directory can +be replaced by text files with udev attributes. .SH NOTES For partitions, some information (e.g. queue attributes) is inherited from the diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c index 7ab9dc23c2..1693b9af73 100644 --- a/misc-utils/lsblk.c +++ b/misc-utils/lsblk.c @@ -711,7 +711,7 @@ static char *device_get_data( int id, /* column ID (COL_*) */ uint64_t *sortdata) /* returns sort data as number */ { - struct lsblk_devprop *prop; + struct lsblk_devprop *prop = NULL; char *str = NULL; switch(id) { @@ -730,30 +730,42 @@ static char *device_get_data( str = xstrdup(dev->filename); break; case COL_OWNER: - { - struct stat *st = device_get_stat(dev); - struct passwd *pw = st ? getpwuid(st->st_uid) : NULL; - if (pw) - str = xstrdup(pw->pw_name); + if (lsblk->sysroot) + prop = lsblk_device_get_properties(dev); + if (prop && prop->owner) { + str = xstrdup(prop->owner); + } else { + struct stat *st = device_get_stat(dev); + struct passwd *pw = st ? getpwuid(st->st_uid) : NULL; + if (pw) + str = xstrdup(pw->pw_name); + } break; - } case COL_GROUP: - { - struct stat *st = device_get_stat(dev); - struct group *gr = st ? getgrgid(st->st_gid) : NULL; - if (gr) - str = xstrdup(gr->gr_name); + if (lsblk->sysroot) + prop = lsblk_device_get_properties(dev); + if (prop && prop->group) { + str = xstrdup(prop->group); + } else { + struct stat *st = device_get_stat(dev); + struct group *gr = st ? getgrgid(st->st_gid) : NULL; + if (gr) + str = xstrdup(gr->gr_name); + } break; - } case COL_MODE: - { - struct stat *st = device_get_stat(dev); - char md[11] = { '\0' }; + if (lsblk->sysroot) + prop = lsblk_device_get_properties(dev); + if (prop && prop->mode) { + str = xstrdup(prop->mode); + } else { + struct stat *st = device_get_stat(dev); + char md[11] = { '\0' }; - if (st) - str = xstrdup(xstrmode(st->st_mode, md)); + if (st) + str = xstrdup(xstrmode(st->st_mode, md)); + } break; - } case COL_MAJMIN: if (is_parsable(lsblk)) xasprintf(&str, "%u:%u", dev->maj, dev->min); diff --git a/misc-utils/lsblk.h b/misc-utils/lsblk.h index 34853f6a76..2f500c0c56 100644 --- a/misc-utils/lsblk.h +++ b/misc-utils/lsblk.h @@ -59,6 +59,7 @@ struct lsblk { extern struct lsblk *lsblk; /* global handler */ struct lsblk_devprop { + /* udev / blkid based */ char *fstype; /* detected fs, NULL or "?" if cannot detect */ char *uuid; /* filesystem UUID (or stack uuid) */ char *ptuuid; /* partition table UUID */ @@ -71,6 +72,11 @@ struct lsblk_devprop { char *wwn; /* storage WWN */ char *serial; /* disk serial number */ char *model; /* disk model */ + + /* lsblk specific (for --sysroot only) */ + char *owner; /* user name */ + char *group; /* group name */ + char *mode; /* access mode in ls(1)-like notation */ }; /* Device dependence