]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsblk: read also GROUP,OWNER and MODE from dumps
authorKarel Zak <kzak@redhat.com>
Tue, 15 Oct 2019 11:08:09 +0000 (13:08 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 15 Oct 2019 11:08:09 +0000 (13:08 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
misc-utils/lsblk-properties.c
misc-utils/lsblk.8
misc-utils/lsblk.c
misc-utils/lsblk.h

index 8c0b28272fdcbcc086b96e60422050020167edc8..f5549f4604d34c3ae4a711386097ba9b0f0e8f36 100644 (file)
@@ -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;
        }
index 2910ee356cdd4e832e14c922f7dad3668800df93..7a8e72b8bf145bdce1eb16e8a8a46e57b7942379 100644 (file)
@@ -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
index 7ab9dc23c29ac0b2536ed67917654cfaaae917b4..1693b9af73ffbdca7e6b835680e11d1d3d8bb562 100644 (file)
@@ -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);
index 34853f6a769eb95e31b7cf069889a10990adef9b..2f500c0c565ca7465eaf265f2d180432cfd16c80 100644 (file)
@@ -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