]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - misc-utils/lsblk.c
lsblk: allow to specify tree column
[thirdparty/util-linux.git] / misc-utils / lsblk.c
index 6a678196635cb198a62ac8cb629fce7f1b27d697..454f067c82891e592c1fcbf0605cb06b3f4e6e90 100644 (file)
@@ -141,13 +141,12 @@ struct colinfo {
        double          whint;          /* width hint (N < 1 is in percent of termwidth) */
        int             flags;          /* SCOLS_FL_* */
        const char      *help;
-
        int             type;           /* COLTYPE_* */
 };
 
 /* columns descriptions */
 static struct colinfo infos[] = {
-       [COL_NAME]   = { "NAME",    0.25, SCOLS_FL_TREE | SCOLS_FL_NOEXTREMES, N_("device name") },
+       [COL_NAME]   = { "NAME",    0.25, SCOLS_FL_NOEXTREMES, N_("device name") },
        [COL_KNAME]  = { "KNAME",   0.3, 0, N_("internal kernel device name") },
        [COL_PKNAME] = { "PKNAME",  0.3, 0, N_("internal parent kernel device name") },
        [COL_PATH]   = { "PATH",    0.3,  0, N_("path to the device node") },
@@ -217,7 +216,6 @@ struct lsblk *lsblk;        /* global handler */
 static int columns[ARRAY_SIZE(infos) * 2];
 static size_t ncolumns;
 
-
 static inline void add_column(int id)
 {
        if (ncolumns >= ARRAY_SIZE(columns))
@@ -405,7 +403,7 @@ static char *get_type(struct lsblk_device *dev)
 {
        char *res = NULL, *p;
 
-       if (cxt->partition)
+       if (device_is_partition(dev))
                return xstrdup("part");
 
        if (is_dm(dev->name)) {
@@ -592,14 +590,20 @@ static void set_sortdata_u64(struct libscols_line *ln, int col, uint64_t x)
        scols_cell_set_userdata(ce, data);
 }
 
-static void set_sortdata_u64_from_string(struct libscols_line *ln, int col, const char *str)
+/* do not modify *data on any error */
+static void str2u64(const char *str, uint64_t *data)
 {
-       uint64_t x;
+       uintmax_t num;
+       char *end = NULL;
 
-       if (!str || sscanf(str, "%"SCNu64, &x) != 1)
+       errno = 0;
+       if (str == NULL || *str == '\0')
                return;
+       num = strtoumax(str, &end, 10);
 
-       set_sortdata_u64(ln, col, x);
+       if (errno || str == end || (end && *end))
+               return;
+       *data = num;
 }
 
 static void unref_sortdata(struct libscols_table *tb)
@@ -674,23 +678,57 @@ static struct stat *device_get_stat(struct lsblk_device *dev)
        return &dev->st;
 }
 
+static int is_removable_device(struct lsblk_device *dev, struct lsblk_device *parent)
+{
+       struct path_cxt *pc;
+
+       if (dev->removable != -1)
+               goto done;
+       if (ul_path_scanf(dev->sysfs, "removable", "%d", &dev->removable) == 1)
+               goto done;
+
+       if (parent) {
+               pc = sysfs_blkdev_get_parent(dev->sysfs);
+               if (!pc)
+                       goto done;
+
+               if (pc == parent->sysfs)
+                       /* dev is partition and parent is whole-disk  */
+                       dev->removable = is_removable_device(parent, NULL);
+               else
+                       /* parent is something else, use sysfs parent */
+                       ul_path_scanf(pc, "removable", "%d", &dev->removable);
+       }
+done:
+       if (dev->removable == -1)
+               dev->removable = 0;
+       return dev->removable;
+}
+
+static uint64_t device_get_discard_granularity(struct lsblk_device *dev)
+{
+       if (dev->discard_granularity == (uint64_t) -1
+           && ul_path_read_u64(dev->sysfs, &dev->discard_granularity,
+                               "queue/discard_granularity") != 0)
+               dev->discard_granularity = 0;
+
+       return dev->discard_granularity;
+}
+
 /*
- * Generates data (string) for column specified by column ID for specified device
+ * Generates data (string) for column specified by column ID for specified device. If sortdata
+ * is not NULL then returns number usable to sort the column if the data are available for the
+ * column.
  */
-static void set_scols_data(
-               struct lsblk_device *dev,
-               struct lsblk_device *parent,
-               int col,
-               int id,
-               struct libscols_line *ln)
+static char *device_get_data(
+               struct lsblk_device *dev,               /* device */
+               struct lsblk_device *parent,            /* device parent as defined in the tree */
+               int id,                                 /* column ID (COL_*) */
+               uint64_t *sortdata)                     /* returns sort data as number */
 {
        struct lsblk_devprop *prop;
-       int sort = 0;
        char *str = NULL;
 
-       if (lsblk->sort_id == id)
-               sort = 1;
-
        switch(id) {
        case COL_NAME:
                str = dev->dm_name ? mk_dm_name(dev->dm_name) : mk_name(dev->name);
@@ -709,7 +747,7 @@ static void set_scols_data(
        case COL_OWNER:
        {
                struct stat *st = device_get_stat(dev);
-               struct passwd *pw = st ? NULL : getpwuid(st->st_uid);
+               struct passwd *pw = st ? getpwuid(st->st_uid) : NULL;
                if (pw)
                        str = xstrdup(pw->pw_name);
                break;
@@ -717,7 +755,7 @@ static void set_scols_data(
        case COL_GROUP:
        {
                struct stat *st = device_get_stat(dev);
-               struct group *gr = st ? NULL : getgrgid(st->st_gid);
+               struct group *gr = st ? getgrgid(st->st_gid) : NULL;
                if (gr)
                        str = xstrdup(gr->gr_name);
                break;
@@ -736,8 +774,8 @@ static void set_scols_data(
                        xasprintf(&str, "%u:%u", dev->maj, dev->min);
                else
                        xasprintf(&str, "%3u:%-3u", dev->maj, dev->min);
-               if (sort)
-                       set_sortdata_u64(ln, col, makedev(dev->maj, dev->min));
+               if (sortdata)
+                       *sortdata = makedev(dev->maj, dev->min);
                break;
        case COL_FSTYPE:
                prop = lsblk_device_get_properties(dev);
@@ -800,18 +838,14 @@ static void set_scols_data(
                break;
        case COL_RA:
                ul_path_read_string(dev->sysfs, &str, "queue/read_ahead_kb");
-               if (sort)
-                       set_sortdata_u64_from_string(ln, col, str);
+               if (sortdata)
+                       str2u64(str, sortdata);
                break;
        case COL_RO:
                str = xstrdup(is_readonly_device(dev) ? "1" : "0");
                break;
        case COL_RM:
-               ul_path_read_string(dev->sysfs, &str, "removable");
-               if (!str && sysfs_blkdev_get_parent(dev->sysfs))
-                       ul_path_read_string(sysfs_blkdev_get_parent(dev->sysfs),
-                                           &str,
-                                           "removable");
+               str = xstrdup(is_removable_device(dev, parent) ? "1" : "0");
                break;
        case COL_HOTPLUG:
                str = sysfs_blkdev_is_hotpluggable(dev->sysfs) ? xstrdup("1") : xstrdup("0");
@@ -823,7 +857,7 @@ static void set_scols_data(
                ul_path_read_string(dev->sysfs, &str, "queue/add_random");
                break;
        case COL_MODEL:
-               if (!dev->partition && dev->nslaves == 0) {
+               if (!device_is_partition(dev) && dev->nslaves == 0) {
                        prop = lsblk_device_get_properties(dev);
                        if (prop && prop->model)
                                str = xstrdup(prop->model);
@@ -832,7 +866,7 @@ static void set_scols_data(
                }
                break;
        case COL_SERIAL:
-               if (!dev->partition && dev->nslaves == 0) {
+               if (!device_is_partition(dev) && dev->nslaves == 0) {
                        prop = lsblk_device_get_properties(dev);
                        if (prop && prop->serial)
                                str = xstrdup(prop->serial);
@@ -841,11 +875,11 @@ static void set_scols_data(
                }
                break;
        case COL_REV:
-               if (!dev->partition && dev->nslaves == 0)
+               if (!device_is_partition(dev) && dev->nslaves == 0)
                        ul_path_read_string(dev->sysfs, &str, "device/rev");
                break;
        case COL_VENDOR:
-               if (!dev->partition && dev->nslaves == 0)
+               if (!device_is_partition(dev) && dev->nslaves == 0)
                        ul_path_read_string(dev->sysfs, &str, "device/vendor");
                break;
        case COL_SIZE:
@@ -855,11 +889,11 @@ static void set_scols_data(
                        xasprintf(&str, "%ju", dev->size);
                else
                        str = size_to_human_string(SIZE_SUFFIX_1LETTER, dev->size);
-               if (sort)
-                       set_sortdata_u64(ln, col, dev->size);
+               if (sortdata)
+                       *sortdata = dev->size;
                break;
        case COL_STATE:
-               if (!dev->partition && !dev->dm_name)
+               if (!device_is_partition(dev) && !dev->dm_name)
                        ul_path_read_string(dev->sysfs, &str, "device/state");
                else if (dev->dm_name) {
                        int x = 0;
@@ -869,36 +903,36 @@ static void set_scols_data(
                break;
        case COL_ALIOFF:
                ul_path_read_string(dev->sysfs, &str, "alignment_offset");
-               if (sort)
-                       set_sortdata_u64_from_string(ln, col, str);
+               if (sortdata)
+                       str2u64(str, sortdata);
                break;
        case COL_MINIO:
                ul_path_read_string(dev->sysfs, &str, "queue/minimum_io_size");
-               if (sort)
-                       set_sortdata_u64_from_string(ln, col, str);
+               if (sortdata)
+                       str2u64(str, sortdata);
                break;
        case COL_OPTIO:
                ul_path_read_string(dev->sysfs, &str, "queue/optimal_io_size");
-               if (sort)
-                       set_sortdata_u64_from_string(ln, col, str);
+               if (sortdata)
+                       str2u64(str, sortdata);
                break;
        case COL_PHYSEC:
                ul_path_read_string(dev->sysfs, &str, "queue/physical_block_size");
-               if (sort)
-                       set_sortdata_u64_from_string(ln, col, str);
+               if (sortdata)
+                       str2u64(str, sortdata);
                break;
        case COL_LOGSEC:
                ul_path_read_string(dev->sysfs, &str, "queue/logical_block_size");
-               if (sort)
-                       set_sortdata_u64_from_string(ln, col, str);
+               if (sortdata)
+                       str2u64(str, sortdata);
                break;
        case COL_SCHED:
                str = get_scheduler(dev);
                break;
        case COL_RQ_SIZE:
                ul_path_read_string(dev->sysfs, &str, "queue/nr_requests");
-               if (sort)
-                       set_sortdata_u64_from_string(ln, col, str);
+               if (sortdata)
+                       str2u64(str, sortdata);
                break;
        case COL_TYPE:
                str = get_type(dev);
@@ -917,43 +951,41 @@ static void set_scols_data(
                str = get_subsystems(dev);
                break;
        case COL_DALIGN:
-               if (dev->discard)
+               if (device_get_discard_granularity(dev) > 0)
                        ul_path_read_string(dev->sysfs, &str, "discard_alignment");
                if (!str)
                        str = xstrdup("0");
-               if (sort)
-                       set_sortdata_u64_from_string(ln, col, str);
+               if (sortdata)
+                       str2u64(str, sortdata);
                break;
        case COL_DGRAN:
                if (lsblk->bytes) {
                        ul_path_read_string(dev->sysfs, &str, "queue/discard_granularity");
-                       if (sort)
-                               set_sortdata_u64_from_string(ln, col, str);
+                       if (sortdata)
+                               str2u64(str, sortdata);
                } else {
-                       uint64_t x;
-                       if (ul_path_read_u64(dev->sysfs, &x, "queue/discard_granularity") == 0) {
-                               str = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
-                               if (sort)
-                                       set_sortdata_u64(ln, col, x);
-                       }
+                       uint64_t x = device_get_discard_granularity(dev);
+                       str = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
+                       if (sortdata)
+                               *sortdata = x;
                }
                break;
        case COL_DMAX:
                if (lsblk->bytes) {
                        ul_path_read_string(dev->sysfs, &str, "queue/discard_max_bytes");
-                       if (sort)
-                               set_sortdata_u64_from_string(ln, col, str);
+                       if (sortdata)
+                               str2u64(str, sortdata);
                } else {
                        uint64_t x;
                        if (ul_path_read_u64(dev->sysfs, &x, "queue/discard_max_bytes") == 0) {
                                str = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
-                               if (sort)
-                                       set_sortdata_u64(ln, col, x);
+                               if (sortdata)
+                                       *sortdata = x;
                        }
                }
                break;
        case COL_DZERO:
-               if (dev->discard)
+               if (device_get_discard_granularity(dev) > 0)
                        ul_path_read_string(dev->sysfs, &str, "queue/discard_zeroes_data");
                if (!str)
                        str = xstrdup("0");
@@ -961,15 +993,15 @@ static void set_scols_data(
        case COL_WSAME:
                if (lsblk->bytes) {
                        ul_path_read_string(dev->sysfs, &str, "queue/write_same_max_bytes");
-                       if (sort)
-                               set_sortdata_u64_from_string(ln, col, str);
+                       if (sortdata)
+                               str2u64(str, sortdata);
                } else {
                        uint64_t x;
 
                        if (ul_path_read_u64(dev->sysfs, &x, "queue/write_same_max_bytes") == 0) {
                                str = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
-                               if (sort)
-                                       set_sortdata_u64(ln, col, x);
+                               if (sortdata)
+                                       *sortdata = x;
                        }
                }
                if (!str)
@@ -980,37 +1012,87 @@ static void set_scols_data(
                break;
        };
 
-       if (str && scols_line_refer_data(ln, col, str))
-               err(EXIT_FAILURE, _("failed to add output data"));
+       return str;
 }
 
 /*
  * Adds data for all wanted columns about the device to the smartcols table
  */
-static void device_to_scols(struct lsblk_device *dev, struct lsblk_device *parent, struct libscols_table *tab)
+static void device_to_scols(
+                       struct lsblk_device *dev,
+                       struct lsblk_device *parent,
+                       struct libscols_table *tab,
+                       struct libscols_line *parent_line)
 {
        size_t i;
+       struct libscols_line *ln;
        struct lsblk_iter itr;
        struct lsblk_device *child = NULL;
+       int link_group = 0;
 
        ON_DBG(DEV, if (ul_path_isopen_dirfd(dev->sysfs)) ul_debugobj(dev, "%s ---> is open!", dev->name));
 
-       dev->scols_line = scols_table_new_line(tab, parent ? parent->scols_line : NULL);
-       if (!dev->scols_line)
+       /* Do not print device more than one in --list mode */
+       if (!(lsblk->flags & LSBLK_TREE) && dev->is_printed)
+               return;
+
+       if (lsblk->merge && list_count_entries(&dev->parents) > 1) {
+               if (!lsblk_device_is_last_parent(dev, parent))
+                       return;
+               link_group = 1;
+       }
+
+       ln = scols_table_new_line(tab, link_group ? NULL : parent_line);
+       if (!ln)
                err(EXIT_FAILURE, _("failed to allocate output line"));
 
-       for (i = 0; i < ncolumns; i++)
-               set_scols_data(dev, parent, i, get_column_id(i), dev->scols_line);
+       dev->is_printed = 1;
+
+       if (link_group) {
+               struct lsblk_device *p;
+               struct libscols_line *gr = parent_line;
+
+               /* Merge all my parents to the one group */
+               lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD);
+               while (lsblk_device_next_parent(dev, &itr, &p) == 0) {
+                       if (!p->scols_line)
+                               continue;
+                       scols_table_group_lines(tab, gr, p->scols_line, 0);
+               }
+
+               /* Link the group -- this makes group->child connection */
+               scols_line_link_group(ln, gr, 0);
+       }
+
+       /* read column specific data and set it to smartcols table line */
+       for (i = 0; i < ncolumns; i++) {
+               char *data;
+               int id = get_column_id(i);
+
+               if (lsblk->sort_id != id)
+                       data = device_get_data(dev, parent, id, NULL);
+               else {
+                       uint64_t sortdata = (uint64_t) -1;
+
+                       data = device_get_data(dev, parent, id, &sortdata);
+                       if (data && sortdata != (uint64_t) -1)
+                               set_sortdata_u64(ln, i, sortdata);
+               }
+               if (data && scols_line_refer_data(ln, i, data))
+                       err(EXIT_FAILURE, _("failed to add output data"));
+       }
+
+       dev->scols_line = ln;
 
        if (dev->npartitions == 0)
                /* For partitions we often read from parental whole-disk sysfs,
                 * otherwise we can close */
                ul_path_close_dirfd(dev->sysfs);
 
-       lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD);
 
+       lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD);
        while (lsblk_device_next_child(dev, &itr, &child) == 0)
-               device_to_scols(child, dev, tab);
+               device_to_scols(child, dev, tab, ln);
 
        /* Let's be careful with number of open files */
        ul_path_close_dirfd(dev->sysfs);
@@ -1027,7 +1109,7 @@ static void devtree_to_scols(struct lsblk_devtree *tr, struct libscols_table *ta
        lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD);
 
        while (lsblk_devtree_next_root(tr, &itr, &dev) == 0)
-               device_to_scols(dev, NULL, tab);
+               device_to_scols(dev, NULL, tab, NULL);
 }
 
 /*
@@ -1043,7 +1125,11 @@ static int initialize_device(struct lsblk_device *dev,
                        name, wholedisk, wholedisk ? wholedisk->name : ""));
 
        dev->name = xstrdup(name);
-       dev->partition = wholedisk != NULL;
+
+       if (wholedisk) {
+               dev->wholedisk = wholedisk;
+               lsblk_ref_device(wholedisk);
+       }
 
        dev->filename = get_device_path(dev);
        if (!dev->filename) {
@@ -1071,10 +1157,6 @@ static int initialize_device(struct lsblk_device *dev,
        if (ul_path_read_u64(dev->sysfs, &dev->size, "size") == 0)      /* in sectors */
                dev->size <<= 9;                                        /* in bytes */
 
-       if (ul_path_read_s32(dev->sysfs, &dev->discard,
-                          "queue/discard_granularity") != 0)
-               dev->discard = 0;
-
        /* Ignore devices of zero size */
        if (!lsblk->all_devices && dev->size == 0) {
                DBG(DEV, ul_debugobj(dev, "zero size device -- ignore"));
@@ -1112,7 +1194,7 @@ static struct lsblk_device *devtree_get_device_or_new(struct lsblk_devtree *tr,
        struct lsblk_device *dev = lsblk_devtree_get_device(tr, name);
 
        if (!dev) {
-               dev = lsblk_new_device(tr);
+               dev = lsblk_new_device();
                if (!dev)
                        err(EXIT_FAILURE, _("failed to allocate device"));
 
@@ -1147,7 +1229,7 @@ static int process_partitions(struct lsblk_devtree *tr, struct lsblk_device *dis
         * Do not process further if there are no partitions for
         * this device or the device itself is a partition.
         */
-       if (!disk->npartitions || disk->partition)
+       if (!disk->npartitions || device_is_partition(disk))
                return -EINVAL;
 
        DBG(DEV, ul_debugobj(disk, "%s: probe whole-disk for partitions", disk->name));
@@ -1221,13 +1303,13 @@ static int process_dependencies(
 
        assert(dev);
 
+       if (lsblk->nodeps)
+               return 0;
+
        /* read all or specified partition */
        if (do_partitions && dev->npartitions)
                process_partitions(tr, dev);
 
-       if (lsblk->nodeps)
-               return 0;
-
        DBG(DEV, ul_debugobj(dev, "%s: reading dependencies", dev->name));
 
        if (!(lsblk->inverse ? dev->nslaves : dev->nholders)) {
@@ -1492,6 +1574,7 @@ static int process_all_devices(struct lsblk_devtree *tr)
                        DBG(DEV, ul_debug(" %s: ignore (in-middle)", d->d_name));
                        goto next;
                }
+
                lsblk_devtree_add_root(tr, dev);
                process_dependencies(tr, dev, 1);
 next:
@@ -1586,6 +1669,43 @@ static int cmp_u64_cells(struct libscols_cell *a,
        return *adata == *bdata ? 0 : *adata >= *bdata ? 1 : -1;
 }
 
+static void device_set_dedupkey(
+                       struct lsblk_device *dev,
+                       struct lsblk_device *parent,
+                       int id)
+{
+       struct lsblk_iter itr;
+       struct lsblk_device *child = NULL;
+
+       dev->dedupkey = device_get_data(dev, parent, id, NULL);
+       if (dev->dedupkey)
+               DBG(DEV, ul_debugobj(dev, "%s: de-duplication key: %s", dev->name, dev->dedupkey));
+
+       if (dev->npartitions == 0)
+               /* For partitions we often read from parental whole-disk sysfs,
+                * otherwise we can close */
+               ul_path_close_dirfd(dev->sysfs);
+
+       lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD);
+
+       while (lsblk_device_next_child(dev, &itr, &child) == 0)
+               device_set_dedupkey(child, dev, id);
+
+       /* Let's be careful with number of open files */
+       ul_path_close_dirfd(dev->sysfs);
+}
+
+static void devtree_set_dedupkeys(struct lsblk_devtree *tr, int id)
+{
+       struct lsblk_iter itr;
+       struct lsblk_device *dev = NULL;
+
+       lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD);
+
+       while (lsblk_devtree_next_root(tr, &itr, &dev) == 0)
+               device_set_dedupkey(dev, NULL, id);
+}
+
 static void __attribute__((__noreturn__)) usage(void)
 {
        FILE *out = stdout;
@@ -1598,28 +1718,30 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(_("List information about block devices.\n"), out);
 
        fputs(USAGE_OPTIONS, out);
+       fputs(_(" -D, --discard        print discard capabilities\n"), out);
+       fputs(_(" -E, --dedup <column> de-duplicate output by <column>\n"), out);
+       fputs(_(" -I, --include <list> show only devices with specified major numbers\n"), out);
+       fputs(_(" -J, --json           use JSON output format\n"), out);
+       fputs(_(" -O, --output-all     output all columns\n"), out);
+       fputs(_(" -P, --pairs          use key=\"value\" output format\n"), out);
+       fputs(_(" -S, --scsi           output info about SCSI devices\n"), out);
+       fputs(_(" -T, --tree[=<column>] use tree format output\n"), out);
        fputs(_(" -a, --all            print all devices\n"), out);
        fputs(_(" -b, --bytes          print SIZE in bytes rather than in human readable format\n"), out);
        fputs(_(" -d, --nodeps         don't print slaves or holders\n"), out);
-       fputs(_(" -D, --discard        print discard capabilities\n"), out);
-       fputs(_(" -z, --zoned          print zone model\n"), out);
        fputs(_(" -e, --exclude <list> exclude devices by major number (default: RAM disks)\n"), out);
        fputs(_(" -f, --fs             output info about filesystems\n"), out);
        fputs(_(" -i, --ascii          use ascii characters only\n"), out);
-       fputs(_(" -I, --include <list> show only devices with specified major numbers\n"), out);
-       fputs(_(" -J, --json           use JSON output format\n"), out);
        fputs(_(" -l, --list           use list format output\n"), out);
-       fputs(_(" -T, --tree           use tree format output\n"), out);
+       fputs(_(" -M, --merge          group parents of sub-trees (usable for RAIDs, Multi-path)\n"), out);
        fputs(_(" -m, --perms          output info about permissions\n"), out);
        fputs(_(" -n, --noheadings     don't print headings\n"), out);
        fputs(_(" -o, --output <list>  output columns\n"), out);
-       fputs(_(" -O, --output-all     output all columns\n"), out);
        fputs(_(" -p, --paths          print complete device path\n"), out);
-       fputs(_(" -P, --pairs          use key=\"value\" output format\n"), out);
        fputs(_(" -r, --raw            use raw output format\n"), out);
        fputs(_(" -s, --inverse        inverse dependencies\n"), out);
-       fputs(_(" -S, --scsi           output info about SCSI devices\n"), out);
        fputs(_(" -t, --topology       output info about topology\n"), out);
+       fputs(_(" -z, --zoned          print zone model\n"), out);
        fputs(_(" -x, --sort <column>  sort output by <column>\n"), out);
        fputs(_("     --sysroot <dir>  use specified directory as system root\n"), out);
        fputs(USAGE_SEPARATOR, out);
@@ -1644,12 +1766,17 @@ static void check_sysdevblock(void)
 
 int main(int argc, char *argv[])
 {
-       struct lsblk _ls = { .sort_id = -1, .flags = LSBLK_TREE };
+       struct lsblk _ls = {
+               .sort_id = -1,
+               .dedup_id = -1,
+               .flags = LSBLK_TREE,
+               .tree_id = COL_NAME
+       };
        struct lsblk_devtree *tr = NULL;
        int c, status = EXIT_FAILURE;
        char *outarg = NULL;
        size_t i;
-       int force_tree = 0;
+       int force_tree = 0, has_tree_col = 0;
 
        enum {
                OPT_SYSROOT = CHAR_MAX + 1
@@ -1660,11 +1787,13 @@ int main(int argc, char *argv[])
                { "bytes",      no_argument,       NULL, 'b' },
                { "nodeps",     no_argument,       NULL, 'd' },
                { "discard",    no_argument,       NULL, 'D' },
+               { "dedup",      required_argument, NULL, 'E' },
                { "zoned",      no_argument,       NULL, 'z' },
                { "help",       no_argument,       NULL, 'h' },
                { "json",       no_argument,       NULL, 'J' },
                { "output",     required_argument, NULL, 'o' },
                { "output-all", no_argument,       NULL, 'O' },
+               { "merge",      no_argument,       NULL, 'M' },
                { "perms",      no_argument,       NULL, 'm' },
                { "noheadings", no_argument,       NULL, 'n' },
                { "list",       no_argument,       NULL, 'l' },
@@ -1680,7 +1809,7 @@ int main(int argc, char *argv[])
                { "scsi",       no_argument,       NULL, 'S' },
                { "sort",       required_argument, NULL, 'x' },
                { "sysroot",    required_argument, NULL, OPT_SYSROOT },
-               { "tree",       no_argument,       NULL, 'T' },
+               { "tree",       optional_argument, NULL, 'T' },
                { "version",    no_argument,       NULL, 'V' },
                { NULL, 0, NULL, 0 },
        };
@@ -1708,7 +1837,7 @@ int main(int argc, char *argv[])
        lsblk_init_debug();
 
        while((c = getopt_long(argc, argv,
-                              "abdDze:fhJlnmo:OpPiI:rstVSTx:", longopts, NULL)) != -1) {
+                              "abdDzE:e:fhJlnMmo:OpPiI:rstVST:x:", longopts, NULL)) != -1) {
 
                err_exclusive_options(c, longopts, excl, excl_st);
 
@@ -1745,6 +1874,9 @@ int main(int argc, char *argv[])
                case 'l':
                        lsblk->flags &= ~LSBLK_TREE; /* disable the default */
                        break;
+               case 'M':
+                       lsblk->merge = 1;
+                       break;
                case 'n':
                        lsblk->flags |= LSBLK_NOHEADINGS;
                        break;
@@ -1817,14 +1949,21 @@ int main(int argc, char *argv[])
                        break;
                case 'T':
                        force_tree = 1;
+                       if (optarg)
+                               lsblk->tree_id = column_name_to_id(optarg, strlen(optarg));
                        break;
-
                case OPT_SYSROOT:
                        lsblk->sysroot = optarg;
                        break;
                case 'V':
                        printf(UTIL_LINUX_VERSION);
                        return EXIT_SUCCESS;
+               case 'E':
+                       lsblk->dedup_id = column_name_to_id(optarg, strlen(optarg));
+                       if (lsblk->dedup_id >= 0)
+                               break;
+                       errtryhelp(EXIT_FAILURE);
+                       break;
                case 'x':
                        lsblk->flags &= ~LSBLK_TREE; /* disable the default */
                        lsblk->sort_id = column_name_to_id(optarg, strlen(optarg));
@@ -1873,6 +2012,12 @@ int main(int argc, char *argv[])
                lsblk->sort_hidden = 1;
        }
 
+       if (lsblk->dedup_id >= 0 && column_id_to_number(lsblk->dedup_id) < 0) {
+               /* the deduplication column is not between output columns -- add as hidden */
+               add_column(lsblk->dedup_id);
+               lsblk->dedup_hidden = 1;
+       }
+
        lsblk_mnt_init();
        scols_init_debug(0);
        ul_path_init_debug();
@@ -1896,10 +2041,18 @@ int main(int argc, char *argv[])
                struct libscols_column *cl;
                int id = get_column_id(i), fl = ci->flags;
 
-               if (!(lsblk->flags & LSBLK_TREE) && id == COL_NAME)
-                       fl &= ~SCOLS_FL_TREE;
+               if ((lsblk->flags & LSBLK_TREE)
+                   && has_tree_col == 0
+                   && id == lsblk->tree_id) {
+                       fl |= SCOLS_FL_TREE;
+                       fl &= ~SCOLS_FL_RIGHT;
+                       has_tree_col = 1;
+               }
+
                if (lsblk->sort_hidden && lsblk->sort_id == id)
                        fl |= SCOLS_FL_HIDDEN;
+               if (lsblk->dedup_hidden && lsblk->dedup_id == id)
+                       fl |= SCOLS_FL_HIDDEN;
 
                cl = scols_table_new_column(lsblk->table, ci->name, ci->whint, fl);
                if (!cl) {
@@ -1957,6 +2110,11 @@ int main(int argc, char *argv[])
                                          EXIT_SUCCESS;         /* all success */
        }
 
+       if (lsblk->dedup_id > -1) {
+               devtree_set_dedupkeys(tr, lsblk->dedup_id);
+               lsblk_devtree_deduplicate_devices(tr);
+       }
+
        devtree_to_scols(tr, lsblk->table);
 
        if (lsblk->sort_col)