From: Karel Zak Date: Wed, 20 Feb 2019 11:49:24 +0000 (+0100) Subject: lsblk: allow to specify tree column X-Git-Tag: v2.34-rc1~95 X-Git-Url: http://git.ipfire.org/?p=thirdparty%2Futil-linux.git;a=commitdiff_plain;h=b9c088f2f3a30adaa658b1e1ad0cf30b618a1559 lsblk: allow to specify tree column * document --tree (was missing in the man page) * add optional argument to --tree to specify tree For example: $ lsblk -o KNAME,SIZE,MOUNTPOINT --tree=KNAME /dev/dm-0 KNAME SIZE MOUNTPOINT dm-0 232.9G └─dm-1 232.9G └─dm-2 232.9G Signed-off-by: Karel Zak --- diff --git a/misc-utils/lsblk.8 b/misc-utils/lsblk.8 index 2787569fed..07cd7a16fb 100644 --- a/misc-utils/lsblk.8 +++ b/misc-utils/lsblk.8 @@ -30,6 +30,8 @@ is subject to change. So whenever possible, you should avoid using default outputs in your scripts. Always explicitly define expected columns by using .B \-\-output .I columns-list +and +.B \-\-list in environments where a stable output is required. .PP Note that @@ -135,6 +137,10 @@ Output info about SCSI devices only. All partitions, slaves and holder devices Print dependencies in inverse order. If the \fB\-\-list\fR output is requested then the lines are still ordered by dependencies. .TP +.BR \-T , " \-\-tree" [ =\fIcolumn ] +Force tree-like output format. If \fIcolumn\fP is specified, then a tree is printed in the column. +The default is NAME column. +.TP .BR \-t , " \-\-topology" Output info about block-device topology. This option is equivalent to diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c index ffa1f82bfb..454f067c82 100644 --- a/misc-utils/lsblk.c +++ b/misc-utils/lsblk.c @@ -146,7 +146,7 @@ struct colinfo { /* 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") }, @@ -1725,7 +1725,7 @@ static void __attribute__((__noreturn__)) usage(void) 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 use tree format output\n"), out); + fputs(_(" -T, --tree[=] 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); @@ -1769,13 +1769,14 @@ int main(int argc, char *argv[]) struct lsblk _ls = { .sort_id = -1, .dedup_id = -1, - .flags = LSBLK_TREE + .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 @@ -1808,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 }, }; @@ -1836,7 +1837,7 @@ int main(int argc, char *argv[]) lsblk_init_debug(); while((c = getopt_long(argc, argv, - "abdDzE:e:fhJlnMmo:OpPiI:rstVSTx:", longopts, NULL)) != -1) { + "abdDzE:e:fhJlnMmo:OpPiI:rstVST:x:", longopts, NULL)) != -1) { err_exclusive_options(c, longopts, excl, excl_st); @@ -1948,8 +1949,9 @@ 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; @@ -2039,8 +2041,14 @@ 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) diff --git a/misc-utils/lsblk.h b/misc-utils/lsblk.h index e15c5b42ad..da2edc09b7 100644 --- a/misc-utils/lsblk.h +++ b/misc-utils/lsblk.h @@ -35,7 +35,9 @@ struct lsblk { struct libscols_table *table; /* output table */ struct libscols_column *sort_col;/* sort output by this column */ - int sort_id; + + int sort_id; /* id of the sort column */ + int tree_id; /* od of column used for tree */ int dedup_id;