lsblk: add option --tree

Now lsblk uses --list when --sort <column> is specified. This patch
allows to specify --tree to overwrite this default behavior add to
force tree-like output. In this case tree branches are sorted by the
<column>.

$ lsblk --sort SIZE
NAME MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda2   8:2    0   200M  0 part /boot
sda1   8:1    0   200M  0 part /boot/efi
sda6   8:6    0   7.8G  0 part [SWAP]
sda5   8:5    0  35.1G  0 part /home/misc
sda4   8:4    0    50G  0 part /
sdb1   8:17   0  74.5G  0 part /home/archive
sdb    8:16   0  74.5G  0 disk
sda3   8:3    0 130.3G  0 part /home
sda    8:0    0 223.6G  0 disk

$ lsblk --sort SIZE --tree
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sdb      8:16   0  74.5G  0 disk
└─sdb1   8:17   0  74.5G  0 part /home/archive
sda      8:0    0 223.6G  0 disk
├─sda2   8:2    0   200M  0 part /boot
├─sda1   8:1    0   200M  0 part /boot/efi
├─sda6   8:6    0   7.8G  0 part [SWAP]
├─sda5   8:5    0  35.1G  0 part /home/misc
├─sda4   8:4    0    50G  0 part /
└─sda3   8:3    0 130.3G  0 part /home

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2017-06-09 11:33:18 +02:00
parent bff78d701d
commit 1d9e35cc1e
3 changed files with 14 additions and 6 deletions

View File

@ -26,9 +26,6 @@ lscpu
lsblk
-----
- (!) add --tree to force tree output for operations like --sort where the
--list is enabled by default.
- currently it does not show mountpoint for all devices in btrfs RAID. It's because
/proc/#/mountinfo contains reference to the one device only. Maybe we can add some
btrfs specific code to provide a better output for FS based stacks. Not sure.

View File

@ -129,7 +129,9 @@ This option is equivalent to
Display version information and exit.
.TP
.BR \-x , " \-\-sort " \fIcolumn\fP
Sort output lines by \fIcolumn\fP. This option enables \fB\-\-list\fR output.
Sort output lines by \fIcolumn\fP. This option enables \fB\-\-list\fR output format by default.
It is possible to use the option \fI\-\-tree\fP to force tree-like output and
than the tree branches are sorted by the \fIcolumn\fP.
.SH NOTES
For partitions, some information (e.g. queue attributes) is inherited from the
parent device.

View File

@ -1644,6 +1644,7 @@ static void __attribute__((__noreturn__)) help(FILE *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, --perms output info about permissions\n"), out);
fputs(_(" -n, --noheadings don't print headings\n"), out);
fputs(_(" -o, --output <list> output columns\n"), out);
@ -1682,6 +1683,7 @@ int main(int argc, char *argv[])
int c, status = EXIT_FAILURE;
char *outarg = NULL;
size_t i;
int force_tree = 0;
static const struct option longopts[] = {
{ "all", no_argument, NULL, 'a' },
@ -1707,6 +1709,7 @@ int main(int argc, char *argv[])
{ "pairs", no_argument, NULL, 'P' },
{ "scsi", no_argument, NULL, 'S' },
{ "sort", required_argument, NULL, 'x' },
{ "tree", no_argument, NULL, 'T' },
{ "version", no_argument, NULL, 'V' },
{ NULL, 0, NULL, 0 },
};
@ -1719,7 +1722,7 @@ int main(int argc, char *argv[])
{ 'O','f' },
{ 'O','m' },
{ 'O','t' },
{ 'P','l','r' },
{ 'P','T', 'l','r' },
{ 0 }
};
int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
@ -1734,7 +1737,7 @@ int main(int argc, char *argv[])
lsblk_init_debug();
while((c = getopt_long(argc, argv,
"abdDze:fhJlnmo:OpPiI:rstVSx:", longopts, NULL)) != -1) {
"abdDze:fhJlnmo:OpPiI:rstVSTx:", longopts, NULL)) != -1) {
err_exclusive_options(c, longopts, excl, excl_st);
@ -1839,6 +1842,9 @@ int main(int argc, char *argv[])
add_uniq_column(COL_REV);
add_uniq_column(COL_TRANSPORT);
break;
case 'T':
force_tree = 1;
break;
case 'V':
printf(UTIL_LINUX_VERSION);
return EXIT_SUCCESS;
@ -1853,6 +1859,9 @@ int main(int argc, char *argv[])
}
}
if (force_tree)
lsblk->flags |= LSBLK_TREE;
check_sysdevblock();
if (!ncolumns) {