2 * lsblk(8) - list block devices
4 * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved.
5 * Written by Milan Broz <gmazyland@gmail.com>
6 * Karel Zak <kzak@redhat.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it would be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
32 #include <sys/ioctl.h>
43 #include "pathnames.h"
45 #include "canonicalize.h"
50 #include "closestream.h"
52 #include "fileutils.h"
56 #include "column-list-table.h"
60 UL_DEBUG_DEFINE_MASK(lsblk
);
61 UL_DEBUG_DEFINE_MASKNAMES(lsblk
) = UL_DEBUG_EMPTY_MASKNAMES
;
63 #define LSBLK_EXIT_SOMEOK 64
64 #define LSBLK_EXIT_ALLFAILED 32
66 static int column_id_to_number(int id
);
143 /* basic table settings */
145 LSBLK_ASCII
= (1 << 0),
146 LSBLK_RAW
= (1 << 1),
147 LSBLK_NOHEADINGS
= (1 << 2),
148 LSBLK_EXPORT
= (1 << 3),
149 LSBLK_TREE
= (1 << 4),
150 LSBLK_JSON
= (1 << 5),
151 LSBLK_SHELLVAR
= (1 << 6)
154 /* Types used for qsort() and JSON */
156 COLTYPE_STR
= 0, /* default */
157 COLTYPE_NUM
= 1, /* always u64 number */
158 COLTYPE_SORTNUM
= 2, /* string on output, u64 for qsort() */
159 COLTYPE_SIZE
= 3, /* srring by default, number when --bytes */
160 COLTYPE_BOOL
= 4 /* 0 or 1 */
165 const char * const name
; /* header */
166 double whint
; /* width hint (N < 1 is in percent of termwidth) */
167 int flags
; /* SCOLS_FL_* */
169 int type
; /* COLTYPE_* */
172 /* columns descriptions */
173 static const struct colinfo infos
[] = {
174 [COL_ALIOFF
] = { "ALIGNMENT", 6, SCOLS_FL_RIGHT
, N_("alignment offset"), COLTYPE_NUM
},
175 [COL_ID
] = { "ID", 0.1, SCOLS_FL_NOEXTREMES
, N_("udev ID (based on ID-LINK)") },
176 [COL_IDLINK
] = { "ID-LINK", 0.1, SCOLS_FL_NOEXTREMES
, N_("the shortest udev /dev/disk/by-id link name") },
177 [COL_DALIGN
] = { "DISC-ALN", 6, SCOLS_FL_RIGHT
, N_("discard alignment offset"), COLTYPE_NUM
},
178 [COL_DAX
] = { "DAX", 1, SCOLS_FL_RIGHT
, N_("dax-capable device"), COLTYPE_BOOL
},
179 [COL_DGRAN
] = { "DISC-GRAN", 6, SCOLS_FL_RIGHT
, N_("discard granularity, use <number> if --bytes is given"), COLTYPE_SIZE
},
180 [COL_DISKSEQ
] = { "DISK-SEQ", 1, SCOLS_FL_RIGHT
, N_("disk sequence number"), COLTYPE_NUM
},
181 [COL_DMAX
] = { "DISC-MAX", 6, SCOLS_FL_RIGHT
, N_("discard max bytes, use <number> if --bytes is given"), COLTYPE_SIZE
},
182 [COL_DZERO
] = { "DISC-ZERO", 1, SCOLS_FL_RIGHT
, N_("discard zeroes data"), COLTYPE_BOOL
},
183 [COL_FSAVAIL
] = { "FSAVAIL", 5, SCOLS_FL_RIGHT
, N_("filesystem size available for unprivileged users, use <number> if --bytes is given"), COLTYPE_SIZE
},
184 [COL_FSROOTS
] = { "FSROOTS", 0.1, SCOLS_FL_WRAP
, N_("mounted filesystem roots") },
185 [COL_FSSIZE
] = { "FSSIZE", 5, SCOLS_FL_RIGHT
, N_("filesystem size, use <number> if --bytes is given"), COLTYPE_SIZE
},
186 [COL_FSTYPE
] = { "FSTYPE", 0.1, SCOLS_FL_TRUNC
, N_("filesystem type") },
187 [COL_FSUSED
] = { "FSUSED", 5, SCOLS_FL_RIGHT
, N_("filesystem size used, use <number> if --bytes is given"), COLTYPE_SIZE
},
188 [COL_FSUSEPERC
] = { "FSUSE%", 3, SCOLS_FL_RIGHT
, N_("filesystem use percentage") },
189 [COL_FSVERSION
] = { "FSVER", 0.1, SCOLS_FL_TRUNC
, N_("filesystem version") },
190 [COL_GROUP
] = { "GROUP", 0.1, SCOLS_FL_TRUNC
, N_("group name") },
191 [COL_HCTL
] = { "HCTL", 10, 0, N_("Host:Channel:Target:Lun for SCSI") },
192 [COL_HOTPLUG
] = { "HOTPLUG", 1, SCOLS_FL_RIGHT
, N_("removable or hotplug device (usb, pcmcia, ...)"), COLTYPE_BOOL
},
193 [COL_KNAME
] = { "KNAME", 0.3, 0, N_("internal kernel device name") },
194 [COL_LABEL
] = { "LABEL", 0.1, 0, N_("filesystem LABEL") },
195 [COL_LOGSEC
] = { "LOG-SEC", 7, SCOLS_FL_RIGHT
, N_("logical sector size"), COLTYPE_NUM
},
196 [COL_MAJMIN
] = { "MAJ:MIN", 6, 0, N_("major:minor device number"), COLTYPE_SORTNUM
},
197 [COL_MAJ
] = { "MAJ", 3, 0, N_("major device number"), COLTYPE_SORTNUM
},
198 [COL_MIN
] = { "MIN", 3, 0, N_("minor device number"), COLTYPE_SORTNUM
},
199 [COL_MINIO
] = { "MIN-IO", 6, SCOLS_FL_RIGHT
, N_("minimum I/O size"), COLTYPE_NUM
},
200 [COL_MODEL
] = { "MODEL", 0.1, SCOLS_FL_TRUNC
, N_("device identifier") },
201 [COL_MODE
] = { "MODE", 10, 0, N_("device node permissions") },
202 [COL_MQ
] = { "MQ", 3, SCOLS_FL_RIGHT
, N_("device queues") },
203 [COL_NAME
] = { "NAME", 0.25, SCOLS_FL_NOEXTREMES
, N_("device name") },
204 [COL_OPTIO
] = { "OPT-IO", 6, SCOLS_FL_RIGHT
, N_("optimal I/O size"), COLTYPE_NUM
},
205 [COL_OWNER
] = { "OWNER", 0.1, SCOLS_FL_TRUNC
, N_("user name"), },
206 [COL_PARTFLAGS
] = { "PARTFLAGS", 36, 0, N_("partition flags") },
207 [COL_PARTLABEL
] = { "PARTLABEL", 0.1, 0, N_("partition LABEL") },
208 [COL_PARTN
] = { "PARTN", 2, SCOLS_FL_RIGHT
, N_("partition number as read from the partition table"), COLTYPE_NUM
},
209 [COL_PARTTYPENAME
] = { "PARTTYPENAME", 0.1, 0, N_("partition type name") },
210 [COL_PARTTYPE
] = { "PARTTYPE", 36, 0, N_("partition type code or UUID") },
211 [COL_PARTUUID
] = { "PARTUUID", 36, 0, N_("partition UUID") },
212 [COL_PATH
] = { "PATH", 0.3, 0, N_("path to the device node") },
213 [COL_PHYSEC
] = { "PHY-SEC", 7, SCOLS_FL_RIGHT
, N_("physical sector size"), COLTYPE_NUM
},
214 [COL_PKNAME
] = { "PKNAME", 0.3, 0, N_("internal parent kernel device name") },
215 [COL_PTTYPE
] = { "PTTYPE", 0.1, 0, N_("partition table type") },
216 [COL_PTUUID
] = { "PTUUID", 36, 0, N_("partition table identifier (usually UUID)") },
217 [COL_RAND
] = { "RAND", 1, SCOLS_FL_RIGHT
, N_("adds randomness"), COLTYPE_BOOL
},
218 [COL_RA
] = { "RA", 3, SCOLS_FL_RIGHT
, N_("read-ahead of the device"), COLTYPE_NUM
},
219 [COL_REV
] = { "REV", 4, SCOLS_FL_RIGHT
, N_("device revision") },
220 [COL_RM
] = { "RM", 1, SCOLS_FL_RIGHT
, N_("removable device"), COLTYPE_BOOL
},
221 [COL_ROTA
] = { "ROTA", 1, SCOLS_FL_RIGHT
, N_("rotational device"), COLTYPE_BOOL
},
222 [COL_RO
] = { "RO", 1, SCOLS_FL_RIGHT
, N_("read-only device"), COLTYPE_BOOL
},
223 [COL_RQ_SIZE
]= { "RQ-SIZE", 5, SCOLS_FL_RIGHT
, N_("request queue size"), COLTYPE_NUM
},
224 [COL_SCHED
] = { "SCHED", 0.1, 0, N_("I/O scheduler name") },
225 [COL_SERIAL
] = { "SERIAL", 0.1, SCOLS_FL_TRUNC
, N_("disk serial number") },
226 [COL_SIZE
] = { "SIZE", 5, SCOLS_FL_RIGHT
, N_("size of the device, use <number> if --bytes is given"), COLTYPE_SIZE
},
227 [COL_START
] = { "START", 5, SCOLS_FL_RIGHT
, N_("partition start offset (in 512-byte sectors)"), COLTYPE_NUM
},
228 [COL_STATE
] = { "STATE", 7, SCOLS_FL_TRUNC
, N_("state of the device") },
229 [COL_SUBSYS
] = { "SUBSYSTEMS", 0.1, SCOLS_FL_NOEXTREMES
, N_("de-duplicated chain of subsystems") },
230 [COL_TARGETS
] = { "MOUNTPOINTS", 0.10, SCOLS_FL_WRAP
| SCOLS_FL_NOEXTREMES
, N_("all locations where device is mounted") },
231 [COL_TARGET
] = { "MOUNTPOINT", 0.10, SCOLS_FL_TRUNC
| SCOLS_FL_NOEXTREMES
, N_("where the device is mounted") },
232 [COL_TRANSPORT
] = { "TRAN", 6, 0, N_("device transport type") },
233 [COL_TYPE
] = { "TYPE", 4, 0, N_("device type") },
234 [COL_UUID
] = { "UUID", 36, 0, N_("filesystem UUID") },
235 [COL_VENDOR
] = { "VENDOR", 0.1, SCOLS_FL_TRUNC
, N_("device vendor") },
236 [COL_WSAME
] = { "WSAME", 6, SCOLS_FL_RIGHT
, N_("write same max bytes, use <number> if --bytes is given"), COLTYPE_SIZE
},
237 [COL_WWN
] = { "WWN", 18, 0, N_("unique storage identifier") },
238 [COL_ZONED
] = { "ZONED", 0.3, 0, N_("zone model") },
239 [COL_ZONE_SZ
] = { "ZONE-SZ", 9, SCOLS_FL_RIGHT
, N_("zone size, use <number> if --bytes is given"), COLTYPE_SIZE
},
240 [COL_ZONE_WGRAN
] = { "ZONE-WGRAN", 10, SCOLS_FL_RIGHT
, N_("zone write granularity, use <number> if --bytes is given"), COLTYPE_SIZE
},
241 [COL_ZONE_APP
] = { "ZONE-APP", 11, SCOLS_FL_RIGHT
, N_("zone append max bytes, use <number> if --bytes is given"), COLTYPE_SIZE
},
242 [COL_ZONE_NR
] = { "ZONE-NR", 8, SCOLS_FL_RIGHT
, N_("number of zones"), COLTYPE_NUM
},
243 [COL_ZONE_OMAX
] = { "ZONE-OMAX", 10, SCOLS_FL_RIGHT
, N_("maximum number of open zones"), COLTYPE_NUM
},
244 [COL_ZONE_AMAX
] = { "ZONE-AMAX", 10, SCOLS_FL_RIGHT
, N_("maximum number of active zones"), COLTYPE_NUM
},
247 /* "userdata" used by callback for libsmartcols filter */
249 struct lsblk_device
*dev
;
250 struct lsblk_device
*parent
;
253 struct lsblk
*lsblk
; /* global handler */
256 * columns[] array specifies all currently wanted output column. The columns
257 * are defined by infos[] array and you can specify (on command line) each
258 * column twice. That's enough, dynamically allocated array of the columns is
259 * unnecessary overkill and over-engineering in this case
261 static int columns
[ARRAY_SIZE(infos
) * 2];
262 static size_t ncolumns
;
264 static inline void add_column(int id
)
266 if (ncolumns
>= ARRAY_SIZE(columns
))
267 errx(EXIT_FAILURE
, _("too many columns specified, "
268 "the limit is %zu columns"),
269 ARRAY_SIZE(columns
) - 1);
270 columns
[ ncolumns
++ ] = id
;
273 static inline void add_uniq_column(int id
)
275 if (column_id_to_number(id
) < 0)
279 static void lsblk_init_debug(void)
281 __UL_INIT_DEBUG_FROM_ENV(lsblk
, LSBLK_DEBUG_
, 0, LSBLK_DEBUG
);
285 * exclude/include devices filter based on major device numbers
287 static int excludes
[256];
288 static size_t nexcludes
;
290 static int includes
[256];
291 static size_t nincludes
;
293 static int is_maj_excluded(int maj
)
297 assert(ARRAY_SIZE(excludes
) > nexcludes
);
300 return 0; /* filter not enabled, device not excluded */
302 for (i
= 0; i
< nexcludes
; i
++) {
303 if (excludes
[i
] == maj
) {
304 DBG(FILTER
, ul_debug("exclude: maj=%d", maj
));
311 static int is_maj_included(int maj
)
315 assert(ARRAY_SIZE(includes
) > nincludes
);
318 return 1; /* filter not enabled, device is included */
320 for (i
= 0; i
< nincludes
; i
++) {
321 if (includes
[i
] == maj
) {
322 DBG(FILTER
, ul_debug("include: maj=%d", maj
));
329 /* Converts column sequential number to column ID (COL_*) */
330 static int get_column_id(int num
)
333 assert((size_t) num
< ncolumns
);
334 assert(columns
[num
] < (int) ARRAY_SIZE(infos
));
338 /* Returns column description for the column sequential number */
339 static const struct colinfo
*get_column_info(int num
)
341 return &infos
[ get_column_id(num
) ];
344 /* Converts column name (as defined in the infos[] to the column ID */
345 static int column_name_to_id(const char *name
, size_t namesz
)
349 /* name as diplayed for users */
350 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
351 const char *cn
= infos
[i
].name
;
353 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
357 /* name as used in expressions, JSON output etc. */
358 if (strnchr(name
, namesz
, '_')) {
362 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
363 if (scols_shellvar_name(infos
[i
].name
, &buf
, &bufsz
) != 0)
365 if (!strncasecmp(name
, buf
, namesz
) && !*(buf
+ namesz
)) {
373 warnx(_("unknown column: %s"), name
);
377 /* Converts column ID (COL_*) to column sequential number */
378 static int column_id_to_number(int id
)
382 for (i
= 0; i
< ncolumns
; i
++)
383 if (columns
[i
] == id
)
388 /* Checks for DM prefix in the device name */
389 static int is_dm(const char *name
)
391 return strncmp(name
, "dm-", 3) ? 0 : 1;
394 /* Returns full pat to the device node (TODO: what about sysfs_blkdev_get_path()) */
395 static char *get_device_path(struct lsblk_device
*dev
)
402 if (is_dm(dev
->name
))
403 return __canonicalize_dm_name(lsblk
->sysroot
, dev
->name
);
405 snprintf(path
, sizeof(path
), "/dev/%s", dev
->name
);
406 sysfs_devname_sys_to_dev(path
);
407 return xstrdup(path
);
410 static int is_readonly_device(struct lsblk_device
*dev
)
414 if (ul_path_scanf(dev
->sysfs
, "ro", "%d", &ro
) == 1)
417 /* fallback if "ro" attribute does not exist */
418 fd
= open(dev
->filename
, O_RDONLY
);
420 if (ioctl(fd
, BLKROGET
, &ro
) != 0)
427 static char *get_scheduler(struct lsblk_device
*dev
)
430 char *p
, *res
= NULL
;
432 if (ul_path_read_buffer(dev
->sysfs
, buf
, sizeof(buf
), "queue/scheduler") == 0)
434 p
= strchr(buf
, '[');
437 p
= strchr(res
, ']');
447 static char *get_type(struct lsblk_device
*dev
)
449 char *res
= NULL
, *p
;
451 if (device_is_partition(dev
))
452 return xstrdup("part");
454 if (is_dm(dev
->name
)) {
455 char *dm_uuid
= NULL
;
457 /* The DM_UUID prefix should be set to subsystem owning
458 * the device - LVM, CRYPT, DMRAID, MPATH, PART */
459 if (ul_path_read_string(dev
->sysfs
, &dm_uuid
, "dm/uuid") > 0
462 char *dm_uuid_prefix
= strsep(&tmp
, "-");
464 if (dm_uuid_prefix
) {
465 /* kpartx hack to remove partition number */
466 if (strncasecmp(dm_uuid_prefix
, "part", 4) == 0)
467 dm_uuid_prefix
[4] = '\0';
469 res
= xstrdup(dm_uuid_prefix
);
475 /* No UUID or no prefix - just mark it as DM device */
478 } else if (!strncmp(dev
->name
, "loop", 4)) {
479 res
= xstrdup("loop");
481 } else if (!strncmp(dev
->name
, "md", 2)) {
482 char *md_level
= NULL
;
484 ul_path_read_string(dev
->sysfs
, &md_level
, "md/level");
485 res
= md_level
? md_level
: xstrdup("md");
488 const char *type
= NULL
;
491 if (ul_path_read_s32(dev
->sysfs
, &x
, "device/type") == 0)
492 type
= blkdev_scsi_type_to_name(x
);
498 for (p
= res
; p
&& *p
; p
++)
499 *p
= tolower((unsigned char) *p
);
503 /* Thanks to lsscsi code for idea of detection logic used here */
504 static const char *get_transport(struct lsblk_device
*dev
)
506 struct path_cxt
*sysfs
= dev
->sysfs
;
508 const char *trans
= NULL
;
511 /* SCSI - Serial Peripheral Interface */
512 if (sysfs_blkdev_scsi_host_is(sysfs
, "spi"))
515 /* FC/FCoE - Fibre Channel / Fibre Channel over Ethernet */
516 else if (sysfs_blkdev_scsi_host_is(sysfs
, "fc")) {
517 attr
= sysfs_blkdev_scsi_host_strdup_attribute(sysfs
, "fc", "symbolic_name");
520 trans
= strstr(attr
, " over ") ? "fcoe" : "fc";
524 /* SAS - Serial Attached SCSI */
525 else if (sysfs_blkdev_scsi_host_is(sysfs
, "sas") ||
526 sysfs_blkdev_scsi_has_attribute(sysfs
, "sas_device"))
530 /* SBP - Serial Bus Protocol (FireWire) */
531 else if (sysfs_blkdev_scsi_has_attribute(sysfs
, "ieee1394_id"))
535 else if (sysfs_blkdev_scsi_host_is(sysfs
, "iscsi"))
538 /* USB - Universal Serial Bus */
539 else if (sysfs_blkdev_scsi_path_contains(sysfs
, "usb"))
543 else if (sysfs_blkdev_scsi_host_is(sysfs
, "scsi")) {
544 attr
= sysfs_blkdev_scsi_host_strdup_attribute(sysfs
, "scsi", "proc_name");
547 if (!strncmp(attr
, "ahci", 4) || !strncmp(attr
, "sata", 4))
549 else if (strstr(attr
, "ata"))
553 } else if (strncmp(dev
->name
, "nvme", 4) == 0) {
555 } else if (strncmp(dev
->name
, "vd", 2) == 0)
557 else if (strncmp(dev
->name
, "mmcblk", 6) == 0)
563 static char *get_subsystems(struct lsblk_device
*dev
)
566 char *sub
, *chain
, *res
= NULL
;
567 size_t len
= 0, last
= 0;
569 chain
= sysfs_blkdev_get_devchain(dev
->sysfs
, path
, sizeof(path
));
573 while (sysfs_blkdev_next_subsystem(dev
->sysfs
, chain
, &sub
) == 0) {
576 /* don't create "block:scsi:scsi", but "block:scsi" */
577 if (len
&& strcmp(res
+ last
, sub
) == 0) {
583 res
= xrealloc(res
, len
+ sz
+ 2);
587 memcpy(res
+ len
, sub
, sz
+ 1);
597 #define is_parsable(_l) (scols_table_is_raw((_l)->table) || \
598 scols_table_is_export((_l)->table) || \
599 scols_table_is_json((_l)->table))
601 static char *mk_name(const char *name
)
607 xasprintf(&p
, "/dev/%s", name
);
611 sysfs_devname_sys_to_dev(p
);
615 static char *mk_dm_name(const char *name
)
621 xasprintf(&p
, "/dev/mapper/%s", name
);
627 /* stores data to scols cell userdata (invisible and independent on output)
628 * to make the original values accessible for sort functions
630 static void set_rawdata_u64(struct libscols_line
*ln
, int col
, uint64_t x
)
632 struct libscols_cell
*ce
= scols_line_get_cell(ln
, col
);
637 data
= xmalloc(sizeof(uint64_t));
639 scols_cell_set_userdata(ce
, data
);
642 /* do not modify *data on any error */
643 static void str2u64(const char *str
, uint64_t *data
)
649 if (str
== NULL
|| *str
== '\0')
651 num
= strtoumax(str
, &end
, 10);
653 if (errno
|| str
== end
|| (end
&& *end
))
658 static void unref_line_rawdata(struct libscols_line
*ln
, struct libscols_table
*tb
)
662 for (i
= 0; i
< ncolumns
; i
++) {
663 struct libscols_column
*cl
= scols_table_get_column(tb
, i
);
664 struct libscols_cell
*ce
;
667 if (cl
!= lsblk
->sort_col
&& !scols_column_has_data_func(cl
))
670 ce
= scols_line_get_column_cell(ln
, cl
);
671 data
= scols_cell_get_userdata(ce
);
676 static void unref_table_rawdata(struct libscols_table
*tb
)
678 struct libscols_iter
*itr
;
679 struct libscols_line
*ln
;
681 if (!tb
|| !lsblk
->rawdata
)
684 itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
687 while (scols_table_next_line(tb
, itr
, &ln
) == 0)
688 unref_line_rawdata(ln
, tb
);
690 scols_free_iter(itr
);
693 static char *get_vfs_attribute(struct lsblk_device
*dev
, int id
)
696 uint64_t vfs_attr
= 0;
698 if (!dev
->fsstat
.f_blocks
) {
699 const char *mnt
= lsblk_device_get_mountpoint(dev
);
700 if (!mnt
|| dev
->is_swap
)
702 if (statvfs(mnt
, &dev
->fsstat
) != 0)
708 vfs_attr
= dev
->fsstat
.f_frsize
* dev
->fsstat
.f_blocks
;
711 vfs_attr
= dev
->fsstat
.f_frsize
* dev
->fsstat
.f_bavail
;
714 vfs_attr
= dev
->fsstat
.f_frsize
* (dev
->fsstat
.f_blocks
- dev
->fsstat
.f_bfree
);
717 if (dev
->fsstat
.f_blocks
== 0)
720 xasprintf(&sizestr
, "%.0f%%",
721 (double)(dev
->fsstat
.f_blocks
- dev
->fsstat
.f_bfree
) /
722 dev
->fsstat
.f_blocks
* 100);
727 sizestr
= xstrdup("0");
728 else if (lsblk
->bytes
)
729 xasprintf(&sizestr
, "%ju", vfs_attr
);
731 sizestr
= size_to_human_string(SIZE_SUFFIX_1LETTER
, vfs_attr
);
736 static struct stat
*device_get_stat(struct lsblk_device
*dev
)
739 && stat(dev
->filename
, &dev
->st
) != 0)
745 static int is_removable_device(struct lsblk_device
*dev
, struct lsblk_device
*parent
)
749 if (dev
->removable
!= -1)
752 dev
->removable
= sysfs_blkdev_is_removable(dev
->sysfs
);
754 if (!dev
->removable
&& parent
) {
755 pc
= sysfs_blkdev_get_parent(dev
->sysfs
);
759 if (pc
== parent
->sysfs
)
760 /* dev is partition and parent is whole-disk */
761 dev
->removable
= is_removable_device(parent
, NULL
);
763 /* parent is something else, use sysfs parent */
764 dev
->removable
= sysfs_blkdev_is_removable(pc
);
767 if (dev
->removable
== -1)
769 return dev
->removable
;
772 static uint64_t device_get_discard_granularity(struct lsblk_device
*dev
)
774 if (dev
->discard_granularity
== (uint64_t) -1
775 && ul_path_read_u64(dev
->sysfs
, &dev
->discard_granularity
,
776 "queue/discard_granularity") != 0)
777 dev
->discard_granularity
= 0;
779 return dev
->discard_granularity
;
782 static void device_read_bytes(struct lsblk_device
*dev
, char *path
, char **str
,
788 ul_path_read_string(dev
->sysfs
, str
, path
);
790 str2u64(*str
, rawdata
);
794 if (ul_path_read_u64(dev
->sysfs
, &x
, path
) == 0) {
795 *str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
801 static void process_mq(struct lsblk_device
*dev
, char **str
)
803 unsigned int queues
= 0;
805 DBG(DEV
, ul_debugobj(dev
, "%s: process mq", dev
->name
));
807 queues
= ul_path_count_dirents(dev
->sysfs
, "mq");
810 DBG(DEV
, ul_debugobj(dev
, "%s: no mq supported, use a single queue", dev
->name
));
814 DBG(DEV
, ul_debugobj(dev
, "%s: has %d queues", dev
->name
, queues
));
815 xasprintf(str
, "%3u", queues
);
819 * Generates data (string) for column specified by column ID for specified device. If rawdata
820 * is not NULL then returns number usable to sort the column if the data are available for the
823 static char *device_get_data(
824 struct lsblk_device
*dev
, /* device */
825 struct lsblk_device
*parent
, /* device parent as defined in the tree */
826 int id
, /* column ID (COL_*) */
827 uint64_t *rawdata
, /* returns sort data as number */
830 struct lsblk_devprop
*prop
= NULL
;
835 str
= dev
->dm_name
? mk_dm_name(dev
->dm_name
) : mk_name(dev
->name
);
838 str
= mk_name(dev
->name
);
842 str
= mk_name(parent
->name
);
846 str
= xstrdup(dev
->filename
);
850 prop
= lsblk_device_get_properties(dev
);
851 if (prop
&& prop
->owner
) {
852 str
= xstrdup(prop
->owner
);
854 struct stat
*st
= device_get_stat(dev
);
855 struct passwd
*pw
= st
? getpwuid(st
->st_uid
) : NULL
;
857 str
= xstrdup(pw
->pw_name
);
862 prop
= lsblk_device_get_properties(dev
);
863 if (prop
&& prop
->group
) {
864 str
= xstrdup(prop
->group
);
866 struct stat
*st
= device_get_stat(dev
);
867 struct group
*gr
= st
? getgrgid(st
->st_gid
) : NULL
;
869 str
= xstrdup(gr
->gr_name
);
874 prop
= lsblk_device_get_properties(dev
);
875 if (prop
&& prop
->mode
) {
876 str
= xstrdup(prop
->mode
);
878 struct stat
*st
= device_get_stat(dev
);
879 char md
[11] = { '\0' };
882 str
= xstrdup(xstrmode(st
->st_mode
, md
));
886 if (is_parsable(lsblk
))
887 xasprintf(&str
, "%u:%u", dev
->maj
, dev
->min
);
889 xasprintf(&str
, "%3u:%-3u", dev
->maj
, dev
->min
);
891 *rawdata
= makedev(dev
->maj
, dev
->min
);
894 xasprintf(&str
, "%u", dev
->maj
);
899 xasprintf(&str
, "%u", dev
->min
);
904 prop
= lsblk_device_get_properties(dev
);
905 if (prop
&& prop
->fstype
)
906 str
= xstrdup(prop
->fstype
);
912 str
= get_vfs_attribute(dev
, id
);
915 prop
= lsblk_device_get_properties(dev
);
916 if (prop
&& prop
->fsversion
)
917 str
= xstrdup(prop
->fsversion
);
921 const char *p
= lsblk_device_get_mountpoint(dev
);
929 struct ul_buffer buf
= UL_INIT_BUFFER
;
930 struct libmnt_fs
**fss
= lsblk_device_get_filesystems(dev
, &n
);
932 for (i
= 0; i
< n
; i
++) {
933 struct libmnt_fs
*fs
= fss
[i
];
934 if (mnt_fs_is_swaparea(fs
))
935 ul_buffer_append_string(&buf
, "[SWAP]");
937 ul_buffer_append_string(&buf
, mnt_fs_get_target(fs
));
939 ul_buffer_append_data(&buf
, "\0", 1);
941 str
= ul_buffer_get_data(&buf
, datasiz
, NULL
);
947 struct ul_buffer buf
= UL_INIT_BUFFER
;
948 struct libmnt_fs
**fss
= lsblk_device_get_filesystems(dev
, &n
);
950 for (i
= 0; i
< n
; i
++) {
951 struct libmnt_fs
*fs
= fss
[i
];
952 const char *root
= mnt_fs_get_root(fs
);
953 if (mnt_fs_is_swaparea(fs
))
955 ul_buffer_append_string(&buf
, root
? root
: "/");
957 ul_buffer_append_data(&buf
, "\0", 1);
959 str
= ul_buffer_get_data(&buf
, datasiz
, NULL
);
963 prop
= lsblk_device_get_properties(dev
);
964 if (prop
&& prop
->label
)
965 str
= xstrdup(prop
->label
);
968 prop
= lsblk_device_get_properties(dev
);
969 if (prop
&& prop
->uuid
)
970 str
= xstrdup(prop
->uuid
);
973 prop
= lsblk_device_get_properties(dev
);
974 if (prop
&& prop
->ptuuid
)
975 str
= xstrdup(prop
->ptuuid
);
978 prop
= lsblk_device_get_properties(dev
);
979 if (prop
&& prop
->pttype
)
980 str
= xstrdup(prop
->pttype
);
983 prop
= lsblk_device_get_properties(dev
);
984 if (prop
&& prop
->parttype
)
985 str
= xstrdup(prop
->parttype
);
987 case COL_PARTTYPENAME
:
988 prop
= lsblk_device_get_properties(dev
);
989 if (prop
&& prop
->parttype
&& prop
->pttype
) {
990 const char *x
= lsblk_parttype_code_to_string(
991 prop
->parttype
, prop
->pttype
);
997 prop
= lsblk_device_get_properties(dev
);
998 if (prop
&& prop
->partlabel
)
999 str
= xstrdup(prop
->partlabel
);
1002 prop
= lsblk_device_get_properties(dev
);
1003 if (prop
&& prop
->partuuid
)
1004 str
= xstrdup(prop
->partuuid
);
1007 prop
= lsblk_device_get_properties(dev
);
1008 if (prop
&& prop
->partflags
)
1009 str
= xstrdup(prop
->partflags
);
1012 prop
= lsblk_device_get_properties(dev
);
1013 if (prop
&& prop
->partn
)
1014 str
= xstrdup(prop
->partn
);
1017 prop
= lsblk_device_get_properties(dev
);
1018 if (prop
&& prop
->wwn
)
1019 str
= xstrdup(prop
->wwn
);
1022 prop
= lsblk_device_get_properties(dev
);
1023 if (prop
&& prop
->idlink
)
1024 str
= xstrdup(prop
->idlink
);
1027 prop
= lsblk_device_get_properties(dev
);
1028 if (prop
&& prop
->idlink
) {
1029 /* skip bus/subsystem prefix */
1030 const char *p
= strchr(prop
->idlink
, '-');
1031 str
= p
&& *(p
+ 1) ? xstrdup(p
+1) : xstrdup(prop
->idlink
);
1035 ul_path_read_string(dev
->sysfs
, &str
, "queue/read_ahead_kb");
1037 str2u64(str
, rawdata
);
1040 str
= xstrdup(is_readonly_device(dev
) ? "1" : "0");
1043 str
= xstrdup(is_removable_device(dev
, parent
) ? "1" : "0");
1046 str
= sysfs_blkdev_is_hotpluggable(dev
->sysfs
) ? xstrdup("1") : xstrdup("0");
1049 ul_path_read_string(dev
->sysfs
, &str
, "queue/rotational");
1052 ul_path_read_string(dev
->sysfs
, &str
, "queue/add_random");
1055 if (!device_is_partition(dev
) && dev
->nslaves
== 0) {
1056 prop
= lsblk_device_get_properties(dev
);
1057 if (prop
&& prop
->model
)
1058 str
= xstrdup(prop
->model
);
1060 ul_path_read_string(dev
->sysfs
, &str
, "device/model");
1064 if (!device_is_partition(dev
) && dev
->nslaves
== 0) {
1065 prop
= lsblk_device_get_properties(dev
);
1066 if (prop
&& prop
->serial
)
1067 str
= xstrdup(prop
->serial
);
1069 ul_path_read_string(dev
->sysfs
, &str
, "device/serial");
1073 if (!device_is_partition(dev
) && dev
->nslaves
== 0) {
1074 prop
= lsblk_device_get_properties(dev
);
1075 if (prop
&& prop
->revision
)
1076 str
= xstrdup(prop
->revision
);
1078 ul_path_read_string(dev
->sysfs
, &str
, "device/rev");
1082 if (!device_is_partition(dev
) && dev
->nslaves
== 0)
1083 ul_path_read_string(dev
->sysfs
, &str
, "device/vendor");
1087 xasprintf(&str
, "%ju", dev
->size
);
1089 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, dev
->size
);
1091 *rawdata
= dev
->size
;
1094 ul_path_read_string(dev
->sysfs
, &str
, "start");
1096 str2u64(str
, rawdata
);
1099 if (!device_is_partition(dev
) && !dev
->dm_name
)
1100 ul_path_read_string(dev
->sysfs
, &str
, "device/state");
1101 else if (dev
->dm_name
) {
1103 if (ul_path_read_s32(dev
->sysfs
, &x
, "dm/suspended") == 0)
1104 str
= xstrdup(x
? "suspended" : "running");
1108 ul_path_read_string(dev
->sysfs
, &str
, "alignment_offset");
1110 str2u64(str
, rawdata
);
1113 ul_path_read_string(dev
->sysfs
, &str
, "queue/minimum_io_size");
1115 str2u64(str
, rawdata
);
1118 ul_path_read_string(dev
->sysfs
, &str
, "queue/optimal_io_size");
1120 str2u64(str
, rawdata
);
1123 ul_path_read_string(dev
->sysfs
, &str
, "queue/physical_block_size");
1125 str2u64(str
, rawdata
);
1128 ul_path_read_string(dev
->sysfs
, &str
, "queue/logical_block_size");
1130 str2u64(str
, rawdata
);
1133 str
= get_scheduler(dev
);
1136 ul_path_read_string(dev
->sysfs
, &str
, "queue/nr_requests");
1138 str2u64(str
, rawdata
);
1141 str
= get_type(dev
);
1146 if (sysfs_blkdev_scsi_get_hctl(dev
->sysfs
, &h
, &c
, &t
, &l
) == 0)
1147 xasprintf(&str
, "%d:%d:%d:%d", h
, c
, t
, l
);
1152 const char *trans
= get_transport(dev
);
1154 str
= xstrdup(trans
);
1158 str
= get_subsystems(dev
);
1161 if (device_get_discard_granularity(dev
) > 0)
1162 ul_path_read_string(dev
->sysfs
, &str
, "discard_alignment");
1166 str2u64(str
, rawdata
);
1170 ul_path_read_string(dev
->sysfs
, &str
, "queue/discard_granularity");
1172 str2u64(str
, rawdata
);
1174 uint64_t x
= device_get_discard_granularity(dev
);
1175 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1181 device_read_bytes(dev
, "queue/discard_max_bytes", &str
, rawdata
);
1184 if (device_get_discard_granularity(dev
) > 0)
1185 ul_path_read_string(dev
->sysfs
, &str
, "queue/discard_zeroes_data");
1190 device_read_bytes(dev
, "queue/write_same_max_bytes", &str
, rawdata
);
1195 ul_path_read_string(dev
->sysfs
, &str
, "queue/zoned");
1201 if (ul_path_read_u64(dev
->sysfs
, &x
, "queue/chunk_sectors") == 0) {
1204 xasprintf(&str
, "%ju", x
);
1206 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1212 case COL_ZONE_WGRAN
:
1213 device_read_bytes(dev
, "queue/zone_write_granularity", &str
, rawdata
);
1216 device_read_bytes(dev
, "queue/zone_append_max_bytes", &str
, rawdata
);
1219 ul_path_read_string(dev
->sysfs
, &str
, "queue/nr_zones");
1221 str2u64(str
, rawdata
);
1224 ul_path_read_string(dev
->sysfs
, &str
, "queue/max_open_zones");
1228 str2u64(str
, rawdata
);
1231 ul_path_read_string(dev
->sysfs
, &str
, "queue/max_active_zones");
1235 str2u64(str
, rawdata
);
1238 ul_path_read_string(dev
->sysfs
, &str
, "queue/dax");
1241 process_mq(dev
, &str
);
1244 ul_path_read_string(dev
->sysfs
, &str
, "diskseq");
1246 str2u64(str
, rawdata
);
1253 static void device_fill_scols_cell(struct lsblk_device
*dev
,
1254 struct lsblk_device
*parent
,
1255 struct libscols_line
*ln
,
1258 struct libscols_cell
*ce
;
1259 struct libscols_column
*cl
= scols_table_get_column(lsblk
->table
, colnum
);
1262 int rc
, id
= get_column_id(colnum
);
1264 if (lsblk
->sort_id
== id
|| scols_column_has_data_func(cl
)) {
1265 uint64_t rawdata
= (uint64_t) -1;
1267 data
= device_get_data(dev
, parent
, id
, &rawdata
, &datasiz
);
1268 if (data
&& rawdata
!= (uint64_t) -1)
1269 set_rawdata_u64(ln
, colnum
, rawdata
);
1271 data
= device_get_data(dev
, parent
, id
, NULL
, &datasiz
);
1275 DBG(DEV
, ul_debugobj(dev
, " refer data[%zu]=\"%s\"", colnum
, data
));
1276 ce
= scols_line_get_cell(ln
, colnum
);
1279 rc
= datasiz
? scols_cell_refer_memory(ce
, data
, datasiz
+ 1)
1280 : scols_cell_refer_data(ce
, data
);
1282 err(EXIT_FAILURE
, _("failed to add output data"));
1285 static int filter_filler_cb(
1286 struct libscols_filter
*fltr
__attribute__((__unused__
)),
1287 struct libscols_line
*ln
,
1291 struct filler_data
*fid
= (struct filler_data
*) userdata
;
1293 device_fill_scols_cell(fid
->dev
, fid
->parent
, ln
, colnum
);
1298 * Adds data for all wanted columns about the device to the smartcols table
1300 static void device_to_scols(
1301 struct lsblk_device
*dev
,
1302 struct lsblk_device
*parent
,
1303 struct libscols_table
*tab
,
1304 struct libscols_line
*parent_line
)
1307 struct libscols_line
*ln
;
1308 struct lsblk_iter itr
;
1309 struct lsblk_device
*child
= NULL
;
1310 int link_group
= 0, nocount
= 0;
1313 DBG(DEV
, ul_debugobj(dev
, "add '%s' to scols", dev
->name
));
1314 ON_DBG(DEV
, if (ul_path_isopen_dirfd(dev
->sysfs
)) ul_debugobj(dev
, " %s ---> is open!", dev
->name
));
1316 if (!parent
&& dev
->wholedisk
)
1317 parent
= dev
->wholedisk
;
1319 /* Do not print device more than once on --list if tree order is not requested */
1320 if (!(lsblk
->flags
& LSBLK_TREE
) && !lsblk
->force_tree_order
&& dev
->is_printed
)
1323 if (lsblk
->merge
&& list_count_entries(&dev
->parents
) > 1) {
1324 if (!lsblk_device_is_last_parent(dev
, parent
))
1329 ln
= scols_table_new_line(tab
, link_group
? NULL
: parent_line
);
1331 err(EXIT_FAILURE
, _("failed to allocate output line"));
1333 dev
->is_printed
= 1;
1335 /* filter lines, smartcols filter can ask for data */
1336 if (lsblk
->filter
) {
1338 struct filler_data fid
= {
1343 scols_filter_set_filler_cb(lsblk
->filter
,
1344 filter_filler_cb
, (void *) &fid
);
1346 if (scols_line_apply_filter(ln
, lsblk
->filter
, &status
))
1347 err(EXIT_FAILURE
, _("failed to apply filter"));
1349 struct libscols_line
*x
= scols_line_get_parent(ln
);
1352 scols_line_remove_child(x
, ln
);
1353 unref_line_rawdata(ln
, tab
);
1354 scols_table_remove_line(tab
, ln
);
1359 /* read column specific data and set it to smartcols table line */
1360 for (i
= 0; ln
&& i
< ncolumns
; i
++) {
1361 if (scols_line_is_filled(ln
, i
))
1363 device_fill_scols_cell(dev
, parent
, ln
, i
);
1366 if (ln
&& link_group
) {
1367 struct lsblk_device
*p
;
1368 struct libscols_line
*gr
= parent_line
;
1370 /* Merge all my parents to the one group */
1371 DBG(DEV
, ul_debugobj(dev
, " grouping parents [--merge]"));
1372 lsblk_reset_iter(&itr
, LSBLK_ITER_FORWARD
);
1373 while (lsblk_device_next_parent(dev
, &itr
, &p
) == 0) {
1374 if (!p
->scols_line
) {
1375 DBG(DEV
, ul_debugobj(dev
, " *** ignore '%s' no scols line yet", p
->name
));
1378 DBG(DEV
, ul_debugobj(dev
, " group '%s'", p
->name
));
1379 scols_table_group_lines(tab
, p
->scols_line
, gr
, 0);
1382 /* Link the group -- this makes group->child connection */
1383 DBG(DEV
, ul_debugobj(dev
, " linking the group [--merge]"));
1384 scols_line_link_group(ln
, gr
, 0);
1387 /* The same device could be printed more than once, don't use it in counter */
1388 if (dev
->scols_line
)
1391 dev
->scols_line
= ln
;
1393 if (dev
->npartitions
== 0)
1394 /* For partitions we often read from parental whole-disk sysfs,
1395 * otherwise we can close */
1396 ul_path_close_dirfd(dev
->sysfs
);
1398 lsblk_reset_iter(&itr
, LSBLK_ITER_FORWARD
);
1399 while (lsblk_device_next_child(dev
, &itr
, &child
) == 0) {
1400 DBG(DEV
, ul_debugobj(dev
, "%s -> continue to child", dev
->name
));
1401 device_to_scols(child
, dev
, tab
, ln
);
1402 DBG(DEV
, ul_debugobj(dev
, "%s <- child done", dev
->name
));
1405 /* apply highligther */
1406 if (ln
&& lsblk
->hlighter
) {
1409 if (scols_line_apply_filter(ln
, lsblk
->hlighter
, &status
) == 0
1411 scols_line_set_color(ln
, lsblk
->hlighter_seq
);
1414 /* apply counters */
1416 for (i
= 0; ln
&& i
< lsblk
->ncts
; i
++)
1417 scols_line_apply_filter(ln
, lsblk
->ct_filters
[i
], NULL
);
1420 /* Let's be careful with number of open files */
1421 ul_path_close_dirfd(dev
->sysfs
);
1425 * Walks on tree and adds one line for each device to the smartcols table
1427 static void devtree_to_scols(struct lsblk_devtree
*tr
, struct libscols_table
*tab
)
1429 struct lsblk_iter itr
;
1430 struct lsblk_device
*dev
= NULL
;
1432 lsblk_reset_iter(&itr
, LSBLK_ITER_FORWARD
);
1434 while (lsblk_devtree_next_root(tr
, &itr
, &dev
) == 0)
1435 device_to_scols(dev
, NULL
, tab
, NULL
);
1438 static int ignore_empty(struct lsblk_device
*dev
)
1440 /* show all non-empty devices */
1444 if (lsblk
->noempty
&& dev
->size
== 0)
1447 /* ignore empty loop devices without backing file */
1448 if (dev
->maj
== LOOPDEV_MAJOR
&&
1449 !loopdev_has_backing_file(dev
->filename
))
1456 * Reads very basic information about the device from sysfs into the device struct
1458 static int initialize_device(struct lsblk_device
*dev
,
1459 struct lsblk_device
*wholedisk
,
1464 DBG(DEV
, ul_debugobj(dev
, "initialize %s [wholedisk=%p %s]",
1465 name
, wholedisk
, wholedisk
? wholedisk
->name
: ""));
1467 if (sysfs_devname_is_hidden(lsblk
->sysroot
, name
)) {
1468 DBG(DEV
, ul_debugobj(dev
, "%s: hidden, ignore", name
));
1472 dev
->name
= xstrdup(name
);
1475 dev
->wholedisk
= wholedisk
;
1476 lsblk_ref_device(wholedisk
);
1479 dev
->filename
= get_device_path(dev
);
1480 if (!dev
->filename
) {
1481 DBG(DEV
, ul_debugobj(dev
, "%s: failed to get device path", dev
->name
));
1484 DBG(DEV
, ul_debugobj(dev
, "%s: filename=%s", dev
->name
, dev
->filename
));
1486 devno
= __sysfs_devname_to_devno(lsblk
->sysroot
, dev
->name
, wholedisk
? wholedisk
->name
: NULL
);
1488 DBG(DEV
, ul_debugobj(dev
, "%s: unknown device name", dev
->name
));
1492 dev
->sysfs
= ul_new_sysfs_path(devno
, wholedisk
? wholedisk
->sysfs
: NULL
, lsblk
->sysroot
);
1494 DBG(DEV
, ul_debugobj(dev
, "%s: failed to initialize sysfs handler", dev
->name
));
1498 dev
->maj
= major(devno
);
1499 dev
->min
= minor(devno
);
1502 if (ul_path_read_u64(dev
->sysfs
, &dev
->size
, "size") == 0) /* in sectors */
1503 dev
->size
<<= 9; /* in bytes */
1505 /* Ignore devices of zero size */
1506 if (!lsblk
->all_devices
&& ignore_empty(dev
)) {
1507 DBG(DEV
, ul_debugobj(dev
, "zero size device -- ignore"));
1510 if (is_dm(dev
->name
)) {
1511 ul_path_read_string(dev
->sysfs
, &dev
->dm_name
, "dm/name");
1512 if (!dev
->dm_name
) {
1513 DBG(DEV
, ul_debugobj(dev
, "%s: failed to get dm name", dev
->name
));
1518 dev
->npartitions
= sysfs_blkdev_count_partitions(dev
->sysfs
, dev
->name
);
1519 dev
->nholders
= ul_path_count_dirents(dev
->sysfs
, "holders");
1520 dev
->nslaves
= ul_path_count_dirents(dev
->sysfs
, "slaves");
1522 DBG(DEV
, ul_debugobj(dev
, "%s: npartitions=%d, nholders=%d, nslaves=%d",
1523 dev
->name
, dev
->npartitions
, dev
->nholders
, dev
->nslaves
));
1525 /* ignore non-SCSI devices */
1526 if (lsblk
->scsi
&& sysfs_blkdev_scsi_get_hctl(dev
->sysfs
, NULL
, NULL
, NULL
, NULL
)) {
1527 DBG(DEV
, ul_debugobj(dev
, "non-scsi device -- ignore"));
1531 /* ignore non-NVMe devices */
1533 const char *transport
= get_transport(dev
);
1535 if (!transport
|| strcmp(transport
, "nvme")) {
1536 DBG(DEV
, ul_debugobj(dev
, "non-nvme device -- ignore"));
1541 /* ignore non-virtio devices */
1542 if (lsblk
->virtio
) {
1543 const char *transport
= get_transport(dev
);
1545 if (!transport
|| strcmp(transport
, "virtio")) {
1546 DBG(DEV
, ul_debugobj(dev
, "non-virtio device -- ignore"));
1551 DBG(DEV
, ul_debugobj(dev
, "%s: context successfully initialized", dev
->name
));
1555 static struct lsblk_device
*devtree_get_device_or_new(struct lsblk_devtree
*tr
,
1556 struct lsblk_device
*disk
,
1559 struct lsblk_device
*dev
= lsblk_devtree_get_device(tr
, name
);
1562 dev
= lsblk_new_device();
1564 err(EXIT_FAILURE
, _("failed to allocate device"));
1566 if (initialize_device(dev
, disk
, name
) != 0) {
1567 lsblk_unref_device(dev
);
1570 lsblk_devtree_add_device(tr
, dev
);
1571 lsblk_unref_device(dev
); /* keep it referenced by devtree only */
1573 DBG(DEV
, ul_debugobj(dev
, "%s: already processed", name
));
1578 static struct lsblk_device
*devtree_pktcdvd_get_dep(
1579 struct lsblk_devtree
*tr
,
1580 struct lsblk_device
*dev
,
1583 char buf
[PATH_MAX
], *name
;
1586 devno
= lsblk_devtree_pktcdvd_get_mate(tr
,
1587 makedev(dev
->maj
, dev
->min
), !want_slave
);
1591 name
= sysfs_devno_to_devname(devno
, buf
, sizeof(buf
));
1595 return devtree_get_device_or_new(tr
, NULL
, name
);
1598 static int process_dependencies(
1599 struct lsblk_devtree
*tr
,
1600 struct lsblk_device
*dev
,
1604 * Read devices from whole-disk device into tree
1606 static int process_partitions(struct lsblk_devtree
*tr
, struct lsblk_device
*disk
)
1614 * Do not process further if there are no partitions for
1615 * this device or the device itself is a partition.
1617 if (!disk
->npartitions
|| device_is_partition(disk
))
1620 DBG(DEV
, ul_debugobj(disk
, "%s: probe whole-disk for partitions", disk
->name
));
1622 dir
= ul_path_opendir(disk
->sysfs
, NULL
);
1624 err(EXIT_FAILURE
, _("failed to open device directory in sysfs"));
1626 while ((d
= xreaddir(dir
))) {
1627 struct lsblk_device
*part
;
1629 if (!(sysfs_blkdev_is_partition_dirent(dir
, d
, disk
->name
)))
1632 DBG(DEV
, ul_debugobj(disk
, " checking %s", d
->d_name
));
1634 part
= devtree_get_device_or_new(tr
, disk
, d
->d_name
);
1638 if (lsblk_device_new_dependence(disk
, part
) == 0)
1639 process_dependencies(tr
, part
, 0);
1641 ul_path_close_dirfd(part
->sysfs
);
1644 /* For partitions we need parental (whole-disk) sysfs directory pretty
1645 * often, so close it now when all is done */
1646 ul_path_close_dirfd(disk
->sysfs
);
1648 DBG(DEV
, ul_debugobj(disk
, "probe whole-disk for partitions -- done"));
1653 static char *get_wholedisk_from_partition_dirent(DIR *dir
, struct dirent
*d
, char *buf
, size_t bufsz
)
1658 if ((len
= readlinkat(dirfd(dir
), d
->d_name
, buf
, bufsz
- 1)) < 0)
1663 /* The path ends with ".../<device>/<partition>" */
1664 p
= strrchr(buf
, '/');
1669 p
= strrchr(buf
, '/');
1678 * Reads slaves/holders and partitions for specified device into device tree
1680 static int process_dependencies(
1681 struct lsblk_devtree
*tr
,
1682 struct lsblk_device
*dev
,
1687 const char *depname
;
1688 struct lsblk_device
*dep
= NULL
;
1695 /* read all or specified partition */
1696 if (do_partitions
&& dev
->npartitions
)
1697 process_partitions(tr
, dev
);
1699 DBG(DEV
, ul_debugobj(dev
, "%s: reading dependencies", dev
->name
));
1701 if (!(lsblk
->inverse
? dev
->nslaves
: dev
->nholders
)) {
1702 DBG(DEV
, ul_debugobj(dev
, " ignore (no slaves/holders)"));
1706 depname
= lsblk
->inverse
? "slaves" : "holders";
1707 dir
= ul_path_opendir(dev
->sysfs
, depname
);
1709 DBG(DEV
, ul_debugobj(dev
, " ignore (no slaves/holders directory)"));
1712 ul_path_close_dirfd(dev
->sysfs
);
1714 DBG(DEV
, ul_debugobj(dev
, " %s: checking for '%s' dependence", dev
->name
, depname
));
1716 while ((d
= xreaddir(dir
))) {
1717 struct lsblk_device
*disk
= NULL
;
1719 /* Is the dependency a partition? */
1720 if (sysfs_blkdev_is_partition_dirent(dir
, d
, NULL
)) {
1725 DBG(DEV
, ul_debugobj(dev
, " %s: dependence is partition", d
->d_name
));
1727 diskname
= get_wholedisk_from_partition_dirent(dir
, d
, buf
, sizeof(buf
));
1729 disk
= devtree_get_device_or_new(tr
, NULL
, diskname
);
1731 DBG(DEV
, ul_debugobj(dev
, " ignore no wholedisk ???"));
1735 dep
= devtree_get_device_or_new(tr
, disk
, d
->d_name
);
1739 if (lsblk_device_new_dependence(dev
, dep
) == 0)
1740 process_dependencies(tr
, dep
, 1);
1743 && lsblk_device_new_dependence(dep
, disk
) == 0)
1744 process_dependencies(tr
, disk
, 0);
1746 /* The dependency is a whole device. */
1748 DBG(DEV
, ul_debugobj(dev
, " %s: %s: dependence is whole-disk",
1749 dev
->name
, d
->d_name
));
1751 dep
= devtree_get_device_or_new(tr
, NULL
, d
->d_name
);
1755 if (lsblk_device_new_dependence(dev
, dep
) == 0)
1756 /* For inverse tree we don't want to show partitions
1757 * if the dependence is on whole-disk */
1758 process_dependencies(tr
, dep
, lsblk
->inverse
? 0 : 1);
1761 if (dep
&& dep
->sysfs
)
1762 ul_path_close_dirfd(dep
->sysfs
);
1763 if (disk
&& disk
->sysfs
)
1764 ul_path_close_dirfd(disk
->sysfs
);
1768 dep
= devtree_pktcdvd_get_dep(tr
, dev
, lsblk
->inverse
);
1770 if (dep
&& lsblk_device_new_dependence(dev
, dep
) == 0) {
1771 lsblk_devtree_remove_root(tr
, dep
);
1772 process_dependencies(tr
, dep
, lsblk
->inverse
? 0 : 1);
1779 * Defines the device as root node in the device tree and walks on all dependencies of the device.
1781 static int __process_one_device(struct lsblk_devtree
*tr
, char *devname
, dev_t devno
)
1783 struct lsblk_device
*dev
= NULL
;
1784 struct lsblk_device
*disk
= NULL
;
1785 char buf
[PATH_MAX
+ 1], *name
= NULL
, *diskname
= NULL
;
1786 int real_part
= 0, rc
= -EINVAL
;
1788 if (devno
== 0 && devname
) {
1791 DBG(DEV
, ul_debug("%s: reading alone device", devname
));
1793 if (stat(devname
, &st
) || !S_ISBLK(st
.st_mode
)) {
1794 warnx(_("%s: not a block device"), devname
);
1799 DBG(DEV
, ul_debug("%d:%d: reading alone device", major(devno
), minor(devno
)));
1801 assert(devno
|| devname
);
1805 /* TODO: sysfs_devno_to_devname() internally initializes path_cxt, it
1806 * would be better to use ul_new_sysfs_path() + sysfs_blkdev_get_name()
1807 * and reuse path_cxt for initialize_device()
1809 name
= sysfs_devno_to_devname(devno
, buf
, sizeof(buf
));
1812 warn(_("%s: failed to get sysfs name"), devname
);
1815 name
= xstrdup(name
);
1817 if (!strncmp(name
, "dm-", 3)) {
1818 /* dm mapping is never a real partition! */
1823 if (blkid_devno_to_wholedisk(devno
, buf
, sizeof(buf
), &diskno
)) {
1824 warn(_("%s: failed to get whole-disk device number"), name
);
1828 real_part
= devno
!= diskno
;
1833 * Device is not a partition.
1835 DBG(DEV
, ul_debug(" non-partition"));
1837 dev
= devtree_get_device_or_new(tr
, NULL
, name
);
1841 lsblk_devtree_add_root(tr
, dev
);
1842 process_dependencies(tr
, dev
, !lsblk
->inverse
);
1845 * Partition, read sysfs name of the disk device
1847 DBG(DEV
, ul_debug(" partition"));
1849 disk
= devtree_get_device_or_new(tr
, NULL
, diskname
);
1853 dev
= devtree_get_device_or_new(tr
, disk
, name
);
1857 lsblk_devtree_add_root(tr
, dev
);
1858 process_dependencies(tr
, dev
, 1);
1861 && lsblk_device_new_dependence(dev
, disk
) == 0)
1862 process_dependencies(tr
, disk
, 0);
1864 ul_path_close_dirfd(disk
->sysfs
);
1869 if (dev
&& dev
->sysfs
)
1870 ul_path_close_dirfd(dev
->sysfs
);
1871 if (disk
&& disk
->sysfs
)
1872 ul_path_close_dirfd(disk
->sysfs
);
1877 static int process_one_device(struct lsblk_devtree
*tr
, char *devname
)
1880 return __process_one_device(tr
, devname
, 0);
1884 * The /sys/block contains only root devices, and no partitions. It seems more
1885 * simple to scan /sys/dev/block where are all devices without exceptions to get
1886 * top-level devices for the reverse tree.
1888 static int process_all_devices_inverse(struct lsblk_devtree
*tr
)
1892 struct path_cxt
*pc
= ul_new_path(_PATH_SYS_DEVBLOCK
);
1894 assert(lsblk
->inverse
);
1897 err(EXIT_FAILURE
, _("failed to allocate /sys handler"));
1899 ul_path_set_prefix(pc
, lsblk
->sysroot
);
1900 dir
= ul_path_opendir(pc
, NULL
);
1904 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_DEVBLOCK
));
1906 while ((d
= xreaddir(dir
))) {
1910 DBG(DEV
, ul_debug(" %s dentry", d
->d_name
));
1912 if (sscanf(d
->d_name
, "%d:%d", &maj
, &min
) != 2)
1914 devno
= makedev(maj
, min
);
1916 if (is_maj_excluded(maj
) || !is_maj_included(maj
))
1918 if (ul_path_countf_dirents(pc
, "%s/holders", d
->d_name
) != 0)
1920 if (sysfs_devno_count_partitions(devno
) != 0)
1922 __process_one_device(tr
, NULL
, devno
);
1928 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_DEVBLOCK
" -- done"));
1933 * Reads root nodes (devices) from /sys/block into devices tree
1935 static int process_all_devices(struct lsblk_devtree
*tr
)
1939 struct path_cxt
*pc
;
1941 assert(lsblk
->inverse
== 0);
1943 pc
= ul_new_path(_PATH_SYS_BLOCK
);
1945 err(EXIT_FAILURE
, _("failed to allocate /sys handler"));
1947 ul_path_set_prefix(pc
, lsblk
->sysroot
);
1948 dir
= ul_path_opendir(pc
, NULL
);
1952 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_BLOCK
));
1954 while ((d
= xreaddir(dir
))) {
1955 struct lsblk_device
*dev
= NULL
;
1957 DBG(DEV
, ul_debug(" %s dentry", d
->d_name
));
1958 dev
= devtree_get_device_or_new(tr
, NULL
, d
->d_name
);
1962 /* remove unwanted devices */
1963 if (is_maj_excluded(dev
->maj
) || !is_maj_included(dev
->maj
)) {
1964 DBG(DEV
, ul_debug(" %s: ignore (by filter)", d
->d_name
));
1965 lsblk_devtree_remove_device(tr
, dev
);
1971 DBG(DEV
, ul_debug(" %s: ignore (in-middle)", d
->d_name
));
1975 lsblk_devtree_add_root(tr
, dev
);
1976 process_dependencies(tr
, dev
, 1);
1978 /* Let's be careful with number of open files */
1979 if (dev
&& dev
->sysfs
)
1980 ul_path_close_dirfd(dev
->sysfs
);
1986 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_BLOCK
" -- done"));
1991 * Parses major numbers as specified on lsblk command line
1993 static void parse_excludes(const char *str0
)
1995 const char *str
= str0
;
1997 while (str
&& *str
) {
2002 n
= strtoul(str
, &end
, 10);
2004 if (end
== str
|| (end
&& *end
&& *end
!= ','))
2005 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
2006 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
2007 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
2008 excludes
[nexcludes
++] = n
;
2010 if (nexcludes
== ARRAY_SIZE(excludes
))
2011 /* TRANSLATORS: The standard value for %d is 256. */
2012 errx(EXIT_FAILURE
, _("the list of excluded devices is "
2013 "too large (limit is %d devices)"),
2014 (int)ARRAY_SIZE(excludes
));
2016 str
= end
&& *end
? end
+ 1 : NULL
;
2021 * Parses major numbers as specified on lsblk command line
2022 * (TODO: what about refactor and merge parse_excludes() and parse_includes().)
2024 static void parse_includes(const char *str0
)
2026 const char *str
= str0
;
2028 while (str
&& *str
) {
2033 n
= strtoul(str
, &end
, 10);
2035 if (end
== str
|| (end
&& *end
&& *end
!= ','))
2036 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
2037 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
2038 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
2039 includes
[nincludes
++] = n
;
2041 if (nincludes
== ARRAY_SIZE(includes
))
2042 /* TRANSLATORS: The standard value for %d is 256. */
2043 errx(EXIT_FAILURE
, _("the list of included devices is "
2044 "too large (limit is %d devices)"),
2045 (int)ARRAY_SIZE(includes
));
2046 str
= end
&& *end
? end
+ 1 : NULL
;
2051 * see set_rawdata_u64() and columns initialization in main()
2053 static int cmp_u64_cells(struct libscols_cell
*a
,
2054 struct libscols_cell
*b
,
2055 __attribute__((__unused__
)) void *data
)
2057 uint64_t *adata
= (uint64_t *) scols_cell_get_userdata(a
),
2058 *bdata
= (uint64_t *) scols_cell_get_userdata(b
);
2060 if (adata
== NULL
&& bdata
== NULL
)
2066 return *adata
== *bdata
? 0 : *adata
>= *bdata
? 1 : -1;
2069 static void *get_u64_cell(const struct libscols_column
*cl
__attribute__((__unused__
)),
2070 struct libscols_cell
*ce
,
2071 void *data
__attribute__((__unused__
)))
2073 return scols_cell_get_userdata(ce
);
2076 static void device_set_dedupkey(
2077 struct lsblk_device
*dev
,
2078 struct lsblk_device
*parent
,
2081 struct lsblk_iter itr
;
2082 struct lsblk_device
*child
= NULL
;
2084 dev
->dedupkey
= device_get_data(dev
, parent
, id
, NULL
, NULL
);
2086 DBG(DEV
, ul_debugobj(dev
, "%s: de-duplication key: %s", dev
->name
, dev
->dedupkey
));
2088 if (dev
->npartitions
== 0)
2089 /* For partitions we often read from parental whole-disk sysfs,
2090 * otherwise we can close */
2091 ul_path_close_dirfd(dev
->sysfs
);
2093 lsblk_reset_iter(&itr
, LSBLK_ITER_FORWARD
);
2095 while (lsblk_device_next_child(dev
, &itr
, &child
) == 0)
2096 device_set_dedupkey(child
, dev
, id
);
2098 /* Let's be careful with number of open files */
2099 ul_path_close_dirfd(dev
->sysfs
);
2102 static void devtree_set_dedupkeys(struct lsblk_devtree
*tr
, int id
)
2104 struct lsblk_iter itr
;
2105 struct lsblk_device
*dev
= NULL
;
2107 lsblk_reset_iter(&itr
, LSBLK_ITER_FORWARD
);
2109 while (lsblk_devtree_next_root(tr
, &itr
, &dev
) == 0)
2110 device_set_dedupkey(dev
, NULL
, id
);
2113 static struct libscols_filter
*new_filter(const char *query
)
2115 struct libscols_filter
*f
;
2117 f
= scols_new_filter(NULL
);
2119 err(EXIT_FAILURE
, _("failed to allocate filter"));
2120 if (query
&& scols_filter_parse_string(f
, query
) != 0)
2121 errx(EXIT_FAILURE
, _("failed to parse \"%s\": %s"), query
,
2122 scols_filter_get_errmsg(f
));
2126 static struct libscols_filter
*new_counter_filter(const char *query
)
2128 lsblk
->ct_filters
= xreallocarray(lsblk
->ct_filters
, lsblk
->ncts
+ 1,
2129 sizeof(struct libscols_filter
*));
2131 lsblk
->ct_filters
[lsblk
->ncts
] = new_filter(query
);
2134 return lsblk
->ct_filters
[lsblk
->ncts
- 1];
2137 static void set_counter_properties(const char *str0
)
2139 struct libscols_filter
*fltr
= NULL
;
2140 struct libscols_counter
*ct
;
2141 char *p
, *str
= xstrdup(str0
);
2142 char *name
= NULL
, *param
= NULL
, *func
= NULL
;
2144 for (p
= strtok(str
, ":"); p
!= NULL
; p
= strtok((char *)0, ":")) {
2152 errx(EXIT_FAILURE
, _("unexpected counter specification: %s"), str0
);
2156 errx(EXIT_FAILURE
, _("counter not properly specified"));
2158 /* use the latest counter filter (--ct-filter) or create empty */
2160 fltr
= lsblk
->ct_filters
[lsblk
->ncts
- 1];
2162 fltr
= new_counter_filter(NULL
);
2164 ct
= scols_filter_new_counter(fltr
);
2166 err(EXIT_FAILURE
, _("failed to allocate counter"));
2168 scols_counter_set_name(ct
, name
);
2170 scols_counter_set_param(ct
, param
);
2174 if (strcmp(func
, "max") == 0)
2175 x
= SCOLS_COUNTER_MAX
;
2176 else if (strcmp(func
, "min") == 0)
2177 x
= SCOLS_COUNTER_MIN
;
2178 else if (strcmp(func
, "sum") == 0)
2179 x
= SCOLS_COUNTER_SUM
;
2180 else if (strcmp(func
, "count") == 0)
2181 x
= SCOLS_COUNTER_COUNT
;
2183 errx(EXIT_FAILURE
, _("unsupported counter type: %s"), func
);
2185 scols_counter_set_func(ct
, x
);
2190 static void print_counters(void)
2192 struct libscols_iter
*itr
;
2195 fputc('\n', stdout
);
2196 fputs(_("Summary:\n"), stdout
);
2198 itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
2200 err(EXIT_FAILURE
, _("failed to allocate iterator"));
2202 for (i
= 0; i
< lsblk
->ncts
; i
++) {
2203 struct libscols_filter
*fltr
= lsblk
->ct_filters
[i
];
2204 struct libscols_counter
*ct
= NULL
;
2206 scols_reset_iter(itr
, SCOLS_ITER_FORWARD
);
2207 while (scols_filter_next_counter(fltr
, itr
, &ct
) == 0) {
2208 printf("%16llu %s\n",
2209 scols_counter_get_result(ct
),
2210 scols_counter_get_name(ct
));
2214 scols_free_iter(itr
);
2217 static void set_column_type(const struct colinfo
*ci
, struct libscols_column
*cl
, int fl
)
2221 /* See init_scols_filter(), it may overwrite the type */
2226 scols_column_set_json_type(cl
, SCOLS_JSON_NUMBER
);
2227 scols_column_set_data_type(cl
, SCOLS_DATA_U64
);
2230 scols_column_set_json_type(cl
, SCOLS_JSON_BOOLEAN
);
2231 scols_column_set_data_type(cl
, SCOLS_DATA_BOOLEAN
);
2238 if (fl
& SCOLS_FL_WRAP
)
2239 scols_column_set_json_type(cl
, SCOLS_JSON_ARRAY_STRING
);
2241 scols_column_set_json_type(cl
, SCOLS_JSON_STRING
);
2243 scols_column_set_data_type(cl
, SCOLS_DATA_STRING
);
2246 static void init_scols_filter(struct libscols_table
*tb
, struct libscols_filter
*f
)
2248 struct libscols_iter
*itr
;
2249 const char *name
= NULL
;
2252 itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
2254 err(EXIT_FAILURE
, _("failed to allocate iterator"));
2256 while (scols_filter_next_holder(f
, itr
, &name
, 0) == 0) {
2257 struct libscols_column
*col
= scols_table_get_column_by_name(tb
, name
);
2258 int id
= column_name_to_id(name
, strlen(name
));
2259 const struct colinfo
*ci
= id
>= 0 ? &infos
[id
] : NULL
;
2263 continue; /* report all unknown columns */
2267 col
= scols_table_new_column(tb
, ci
->name
,
2268 ci
->whint
, SCOLS_FL_HIDDEN
);
2270 err(EXIT_FAILURE
,_("failed to allocate output column"));
2272 set_column_type(ci
, col
, ci
->flags
);
2275 /* For sizes use rawdata (u64) rather than strings from table */
2276 if (ci
->type
== COLTYPE_SIZE
2278 && !scols_column_has_data_func(col
)) {
2280 scols_column_set_data_type(col
, SCOLS_DATA_U64
);
2281 scols_column_set_data_func(col
, get_u64_cell
, NULL
);
2285 scols_filter_assign_column(f
, itr
, name
, col
);
2288 scols_free_iter(itr
);
2293 errx(EXIT_FAILURE
, _("failed to initialize filter"));
2297 static void __attribute__((__noreturn__
)) usage(void)
2301 fputs(USAGE_HEADER
, out
);
2302 fprintf(out
, _(" %s [options] [<device> ...]\n"), program_invocation_short_name
);
2304 fputs(USAGE_SEPARATOR
, out
);
2305 fputs(_("List information about block devices.\n"), out
);
2307 fputs(USAGE_OPTIONS
, out
);
2308 fputs(_(" -A, --noempty don't print empty devices\n"), out
);
2309 fputs(_(" -D, --discard print discard capabilities\n"), out
);
2310 fputs(_(" -E, --dedup <column> de-duplicate output by <column>\n"), out
);
2311 fputs(_(" -I, --include <list> show only devices with specified major numbers\n"), out
);
2312 fputs(_(" -J, --json use JSON output format\n"), out
);
2313 fputs(_(" -M, --merge group parents of sub-trees (usable for RAIDs, Multi-path)\n"), out
);
2314 fputs(_(" -O, --output-all output all columns\n"), out
);
2315 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out
);
2316 fputs(_(" -Q, --filter <expr> print only lines maching the expression\n"), out
);
2317 fputs(_(" --highlight <expr> colorize lines maching the expression\n"), out
);
2318 fputs(_(" --ct-filter <expr> restrict the next counter\n"), out
);
2319 fputs(_(" --ct <name>[:<param>[:<func>]] define a custom counter\n"), out
);
2320 fputs(_(" -S, --scsi output info about SCSI devices\n"), out
);
2321 fputs(_(" -N, --nvme output info about NVMe devices\n"), out
);
2322 fputs(_(" -v, --virtio output info about virtio devices\n"), out
);
2323 fputs(_(" -T, --tree[=<column>] use tree format output\n"), out
);
2324 fputs(_(" -a, --all print all devices\n"), out
);
2325 fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out
);
2326 fputs(_(" -d, --nodeps don't print slaves or holders\n"), out
);
2327 fputs(_(" -e, --exclude <list> exclude devices by major number (default: RAM disks)\n"), out
);
2328 fputs(_(" -f, --fs output info about filesystems\n"), out
);
2329 fputs(_(" -i, --ascii use ascii characters only\n"), out
);
2330 fputs(_(" -l, --list use list format output\n"), out
);
2331 fputs(_(" -m, --perms output info about permissions\n"), out
);
2332 fputs(_(" -n, --noheadings don't print headings\n"), out
);
2333 fputs(_(" -o, --output <list> output columns (see --list-columns)\n"), out
);
2334 fputs(_(" -p, --paths print complete device path\n"), out
);
2335 fputs(_(" -r, --raw use raw output format\n"), out
);
2336 fputs(_(" -s, --inverse inverse dependencies\n"), out
);
2337 fputs(_(" -t, --topology output info about topology\n"), out
);
2338 fputs(_(" -w, --width <num> specifies output width as number of characters\n"), out
);
2339 fputs(_(" -x, --sort <column> sort output by <column>\n"), out
);
2340 fputs(_(" -y, --shell use column names to be usable as shell variable identifiers\n"), out
);
2341 fputs(_(" -z, --zoned print zone related information\n"), out
);
2342 fputs(_(" --sysroot <dir> use specified directory as system root\n"), out
);
2344 fputs(USAGE_SEPARATOR
, out
);
2345 fputs(_(" -H, --list-columns list the available columns\n"), out
);
2346 fprintf(out
, USAGE_HELP_OPTIONS(22));
2348 fprintf(out
, USAGE_MAN_TAIL("lsblk(8)"));
2354 static void __attribute__((__noreturn__
)) list_colunms(void)
2357 struct libscols_table
*tb
= xcolumn_list_table_new("lsblk-columns", stdout
,
2358 lsblk
->flags
& LSBLK_RAW
,
2359 lsblk
->flags
& LSBLK_JSON
);
2361 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
2362 const struct colinfo
*ci
= &infos
[i
];
2364 xcolumn_list_table_append_line(tb
, ci
->name
,
2365 ci
->type
== COLTYPE_SIZE
? -1 :
2366 ci
->type
== COLTYPE_NUM
? SCOLS_JSON_NUMBER
:
2367 ci
->type
== COLTYPE_BOOL
? SCOLS_JSON_BOOLEAN
:
2368 ci
->flags
& SCOLS_FL_WRAP
? SCOLS_JSON_ARRAY_STRING
:
2370 ci
->type
== COLTYPE_SIZE
? "<string|number>" : NULL
,
2375 scols_print_table(tb
);
2376 scols_unref_table(tb
);
2381 static void check_sysdevblock(void)
2383 if (access(_PATH_SYS_DEVBLOCK
, R_OK
) != 0)
2384 err(EXIT_FAILURE
, _("failed to access sysfs directory: %s"),
2385 _PATH_SYS_DEVBLOCK
);
2388 int main(int argc
, char *argv
[])
2390 struct lsblk _ls
= {
2393 .flags
= LSBLK_TREE
,
2396 struct lsblk_devtree
*tr
= NULL
;
2397 int c
, status
= EXIT_FAILURE
, collist
= 0;
2398 char *outarg
= NULL
;
2400 unsigned int width
= 0;
2401 int force_tree
= 0, has_tree_col
= 0;
2404 OPT_SYSROOT
= CHAR_MAX
+ 1,
2410 static const struct option longopts
[] = {
2411 { "all", no_argument
, NULL
, 'a' },
2412 { "bytes", no_argument
, NULL
, 'b' },
2413 { "nodeps", no_argument
, NULL
, 'd' },
2414 { "noempty", no_argument
, NULL
, 'A' },
2415 { "discard", no_argument
, NULL
, 'D' },
2416 { "dedup", required_argument
, NULL
, 'E' },
2417 { "zoned", no_argument
, NULL
, 'z' },
2418 { "help", no_argument
, NULL
, 'h' },
2419 { "json", no_argument
, NULL
, 'J' },
2420 { "output", required_argument
, NULL
, 'o' },
2421 { "output-all", no_argument
, NULL
, 'O' },
2422 { "filter", required_argument
, NULL
, 'Q' },
2423 { "highlight", required_argument
, NULL
, OPT_HIGHLIGHT
},
2424 { "merge", no_argument
, NULL
, 'M' },
2425 { "perms", no_argument
, NULL
, 'm' },
2426 { "noheadings", no_argument
, NULL
, 'n' },
2427 { "list", no_argument
, NULL
, 'l' },
2428 { "ascii", no_argument
, NULL
, 'i' },
2429 { "raw", no_argument
, NULL
, 'r' },
2430 { "inverse", no_argument
, NULL
, 's' },
2431 { "fs", no_argument
, NULL
, 'f' },
2432 { "exclude", required_argument
, NULL
, 'e' },
2433 { "include", required_argument
, NULL
, 'I' },
2434 { "topology", no_argument
, NULL
, 't' },
2435 { "paths", no_argument
, NULL
, 'p' },
2436 { "pairs", no_argument
, NULL
, 'P' },
2437 { "scsi", no_argument
, NULL
, 'S' },
2438 { "nvme", no_argument
, NULL
, 'N' },
2439 { "virtio", no_argument
, NULL
, 'v' },
2440 { "sort", required_argument
, NULL
, 'x' },
2441 { "sysroot", required_argument
, NULL
, OPT_SYSROOT
},
2442 { "shell", no_argument
, NULL
, 'y' },
2443 { "tree", optional_argument
, NULL
, 'T' },
2444 { "version", no_argument
, NULL
, 'V' },
2445 { "width", required_argument
, NULL
, 'w' },
2446 { "ct-filter", required_argument
, NULL
, OPT_COUNTER_FILTER
},
2447 { "ct", required_argument
, NULL
, OPT_COUNTER
},
2448 { "list-columns", no_argument
, NULL
, 'H' },
2449 { NULL
, 0, NULL
, 0 },
2452 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
2461 { 'P','T', 'l','r' },
2464 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
2466 setlocale(LC_ALL
, "");
2467 bindtextdomain(PACKAGE
, LOCALEDIR
);
2468 textdomain(PACKAGE
);
2469 close_stdout_atexit();
2474 scols_init_debug(0);
2476 while((c
= getopt_long(argc
, argv
,
2477 "AabdDzE:e:fHhJlNnMmo:OpPQ:iI:rstVvST::w:x:y",
2478 longopts
, NULL
)) != -1) {
2480 err_exclusive_options(c
, longopts
, excl
, excl_st
);
2487 lsblk
->all_devices
= 1;
2496 add_uniq_column(COL_NAME
);
2497 add_uniq_column(COL_DALIGN
);
2498 add_uniq_column(COL_DGRAN
);
2499 add_uniq_column(COL_DMAX
);
2500 add_uniq_column(COL_DZERO
);
2503 add_uniq_column(COL_NAME
);
2504 add_uniq_column(COL_ZONED
);
2505 add_uniq_column(COL_ZONE_SZ
);
2506 add_uniq_column(COL_ZONE_NR
);
2507 add_uniq_column(COL_ZONE_AMAX
);
2508 add_uniq_column(COL_ZONE_OMAX
);
2509 add_uniq_column(COL_ZONE_APP
);
2510 add_uniq_column(COL_ZONE_WGRAN
);
2513 parse_excludes(optarg
);
2516 lsblk
->flags
|= LSBLK_JSON
;
2519 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
2525 lsblk
->flags
|= LSBLK_NOHEADINGS
;
2531 for (ncolumns
= 0 ; ncolumns
< ARRAY_SIZE(infos
); ncolumns
++)
2532 columns
[ncolumns
] = ncolumns
;
2538 lsblk
->flags
|= LSBLK_EXPORT
;
2539 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
2542 lsblk
->filter
= new_filter(optarg
);
2543 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
2546 lsblk
->flags
|= LSBLK_SHELLVAR
;
2549 lsblk
->flags
|= LSBLK_ASCII
;
2552 parse_includes(optarg
);
2555 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
2556 lsblk
->flags
|= LSBLK_RAW
; /* enable raw */
2562 add_uniq_column(COL_NAME
);
2563 add_uniq_column(COL_FSTYPE
);
2564 add_uniq_column(COL_FSVERSION
);
2565 add_uniq_column(COL_LABEL
);
2566 add_uniq_column(COL_UUID
);
2567 add_uniq_column(COL_FSAVAIL
);
2568 add_uniq_column(COL_FSUSEPERC
);
2569 add_uniq_column(COL_TARGETS
);
2572 add_uniq_column(COL_NAME
);
2573 add_uniq_column(COL_SIZE
);
2574 add_uniq_column(COL_OWNER
);
2575 add_uniq_column(COL_GROUP
);
2576 add_uniq_column(COL_MODE
);
2579 add_uniq_column(COL_NAME
);
2580 add_uniq_column(COL_ALIOFF
);
2581 add_uniq_column(COL_MINIO
);
2582 add_uniq_column(COL_OPTIO
);
2583 add_uniq_column(COL_PHYSEC
);
2584 add_uniq_column(COL_LOGSEC
);
2585 add_uniq_column(COL_ROTA
);
2586 add_uniq_column(COL_SCHED
);
2587 add_uniq_column(COL_RQ_SIZE
);
2588 add_uniq_column(COL_RA
);
2589 add_uniq_column(COL_WSAME
);
2594 add_uniq_column(COL_NAME
);
2595 add_uniq_column(COL_HCTL
);
2596 add_uniq_column(COL_TYPE
);
2597 add_uniq_column(COL_VENDOR
);
2598 add_uniq_column(COL_MODEL
);
2599 add_uniq_column(COL_REV
);
2600 add_uniq_column(COL_SERIAL
);
2601 add_uniq_column(COL_TRANSPORT
);
2606 add_uniq_column(COL_NAME
);
2607 add_uniq_column(COL_TYPE
);
2608 add_uniq_column(COL_MODEL
);
2609 add_uniq_column(COL_SERIAL
);
2610 add_uniq_column(COL_REV
);
2611 add_uniq_column(COL_TRANSPORT
);
2612 add_uniq_column(COL_RQ_SIZE
);
2613 add_uniq_column(COL_MQ
);
2618 add_uniq_column(COL_NAME
);
2619 add_uniq_column(COL_TYPE
);
2620 add_uniq_column(COL_TRANSPORT
);
2621 add_uniq_column(COL_SIZE
);
2622 add_uniq_column(COL_RQ_SIZE
);
2623 add_uniq_column(COL_MQ
);
2630 lsblk
->tree_id
= column_name_to_id(optarg
, strlen(optarg
));
2634 lsblk
->sysroot
= optarg
;
2637 lsblk
->dedup_id
= column_name_to_id(optarg
, strlen(optarg
));
2638 if (lsblk
->dedup_id
>= 0)
2640 errtryhelp(EXIT_FAILURE
);
2643 width
= strtou32_or_err(optarg
, _("invalid output width number argument"));
2646 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
2647 lsblk
->sort_id
= column_name_to_id(optarg
, strlen(optarg
));
2648 if (lsblk
->sort_id
>= 0)
2650 errtryhelp(EXIT_FAILURE
);
2653 case OPT_COUNTER_FILTER
:
2654 new_counter_filter(optarg
);
2657 set_counter_properties(optarg
);
2660 lsblk
->hlighter
= new_filter(optarg
);
2669 print_version(EXIT_SUCCESS
);
2671 errtryhelp(EXIT_FAILURE
);
2676 list_colunms(); /* print end exit */
2679 lsblk
->flags
|= LSBLK_TREE
;
2681 check_sysdevblock();
2684 add_column(COL_NAME
);
2685 add_column(COL_MAJMIN
);
2687 add_column(COL_SIZE
);
2689 add_column(COL_TYPE
);
2690 add_column(COL_TARGETS
);
2693 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
2694 &ncolumns
, column_name_to_id
) < 0)
2695 return EXIT_FAILURE
;
2697 if (lsblk
->all_devices
== 0 && nexcludes
== 0 && nincludes
== 0)
2698 excludes
[nexcludes
++] = 1; /* default: ignore RAM disks */
2700 if (lsblk
->sort_id
< 0)
2701 /* Since Linux 4.8 we have sort devices by default, because
2702 * /sys is no more sorted */
2703 lsblk
->sort_id
= COL_MAJMIN
;
2705 /* For --{inverse,raw,pairs} --list we still follow parent->child relation */
2706 if (!(lsblk
->flags
& LSBLK_TREE
)
2707 && (lsblk
->inverse
|| lsblk
->flags
& LSBLK_EXPORT
|| lsblk
->flags
& LSBLK_RAW
))
2708 lsblk
->force_tree_order
= 1;
2710 if (lsblk
->sort_id
>= 0 && column_id_to_number(lsblk
->sort_id
) < 0) {
2711 /* the sort column is not between output columns -- add as hidden */
2712 add_column(lsblk
->sort_id
);
2713 lsblk
->sort_hidden
= 1;
2716 if (lsblk
->dedup_id
>= 0 && column_id_to_number(lsblk
->dedup_id
) < 0) {
2717 /* the deduplication column is not between output columns -- add as hidden */
2718 add_column(lsblk
->dedup_id
);
2719 lsblk
->dedup_hidden
= 1;
2723 ul_path_init_debug();
2726 * initialize output columns
2728 if (!(lsblk
->table
= scols_new_table()))
2729 errx(EXIT_FAILURE
, _("failed to allocate output table"));
2730 scols_table_enable_raw(lsblk
->table
, !!(lsblk
->flags
& LSBLK_RAW
));
2731 scols_table_enable_export(lsblk
->table
, !!(lsblk
->flags
& LSBLK_EXPORT
));
2732 scols_table_enable_shellvar(lsblk
->table
, !!(lsblk
->flags
& LSBLK_SHELLVAR
));
2733 scols_table_enable_ascii(lsblk
->table
, !!(lsblk
->flags
& LSBLK_ASCII
));
2734 scols_table_enable_json(lsblk
->table
, !!(lsblk
->flags
& LSBLK_JSON
));
2735 scols_table_enable_noheadings(lsblk
->table
, !!(lsblk
->flags
& LSBLK_NOHEADINGS
));
2737 if (lsblk
->flags
& LSBLK_JSON
)
2738 scols_table_set_name(lsblk
->table
, "blockdevices");
2740 scols_table_set_termwidth(lsblk
->table
, width
);
2741 scols_table_set_termforce(lsblk
->table
, SCOLS_TERMFORCE_ALWAYS
);
2744 for (i
= 0; i
< ncolumns
; i
++) {
2745 const struct colinfo
*ci
= get_column_info(i
);
2746 struct libscols_column
*cl
;
2747 int id
= get_column_id(i
), fl
= ci
->flags
;
2749 if ((lsblk
->flags
& LSBLK_TREE
)
2750 && has_tree_col
== 0
2751 && id
== lsblk
->tree_id
) {
2752 fl
|= SCOLS_FL_TREE
;
2753 fl
&= ~SCOLS_FL_RIGHT
;
2757 if (lsblk
->sort_hidden
&& lsblk
->sort_id
== id
)
2758 fl
|= SCOLS_FL_HIDDEN
;
2759 if (lsblk
->dedup_hidden
&& lsblk
->dedup_id
== id
)
2760 fl
|= SCOLS_FL_HIDDEN
;
2763 && lsblk
->flags
& LSBLK_JSON
2764 && has_tree_col
== 0
2765 && i
+ 1 == ncolumns
)
2766 /* The "--tree --json" specified, but no column with
2767 * SCOLS_FL_TREE yet; force it for the last column
2769 fl
|= SCOLS_FL_TREE
;
2771 cl
= scols_table_new_column(lsblk
->table
, ci
->name
, ci
->whint
, fl
);
2773 warn(_("failed to allocate output column"));
2776 if (!lsblk
->sort_col
&& lsblk
->sort_id
== id
) {
2777 lsblk
->sort_col
= cl
;
2779 scols_column_set_cmpfunc(cl
,
2780 ci
->type
== COLTYPE_NUM
? cmp_u64_cells
:
2781 ci
->type
== COLTYPE_SIZE
? cmp_u64_cells
:
2782 ci
->type
== COLTYPE_SORTNUM
? cmp_u64_cells
: scols_cmpstr_cells
,
2785 /* multi-line cells (now used for MOUNTPOINTS) */
2786 if (fl
& SCOLS_FL_WRAP
)
2787 scols_column_set_wrapfunc(cl
, NULL
, scols_wrapzero_nextchunk
, NULL
);
2789 set_column_type(ci
, cl
, fl
);
2793 init_scols_filter(lsblk
->table
, lsblk
->filter
);
2795 if (lsblk
->hlighter
&& colors_init(UL_COLORMODE_AUTO
, "lsblk") > 0) {
2796 lsblk
->hlighter_seq
= color_scheme_get_sequence("highlight-line", UL_COLOR_RED
);
2797 scols_table_enable_colors(lsblk
->table
, 1);
2798 init_scols_filter(lsblk
->table
, lsblk
->hlighter
);
2800 for (i
= 0; i
< lsblk
->ncts
; i
++)
2801 init_scols_filter(lsblk
->table
, lsblk
->ct_filters
[i
]);
2803 tr
= lsblk_new_devtree();
2805 err(EXIT_FAILURE
, _("failed to allocate device tree"));
2807 if (optind
== argc
) {
2808 int rc
= lsblk
->inverse
?
2809 process_all_devices_inverse(tr
) :
2810 process_all_devices(tr
);
2812 status
= rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
2814 int cnt
= 0, cnt_err
= 0;
2816 while (optind
< argc
) {
2817 if (process_one_device(tr
, argv
[optind
++]) != 0)
2821 status
= cnt
== 0 ? EXIT_FAILURE
: /* nothing */
2822 cnt
== cnt_err
? LSBLK_EXIT_ALLFAILED
:/* all failed */
2823 cnt_err
? LSBLK_EXIT_SOMEOK
: /* some ok */
2824 EXIT_SUCCESS
; /* all success */
2827 if (lsblk
->dedup_id
> -1) {
2828 devtree_set_dedupkeys(tr
, lsblk
->dedup_id
);
2829 lsblk_devtree_deduplicate_devices(tr
);
2832 devtree_to_scols(tr
, lsblk
->table
);
2834 if (lsblk
->sort_col
)
2835 scols_sort_table(lsblk
->table
, lsblk
->sort_col
);
2836 if (lsblk
->force_tree_order
)
2837 scols_sort_table_by_tree(lsblk
->table
);
2839 scols_print_table(lsblk
->table
);
2845 unref_table_rawdata(lsblk
->table
);
2847 scols_unref_table(lsblk
->table
);
2848 scols_unref_filter(lsblk
->filter
);
2849 scols_unref_filter(lsblk
->hlighter
);
2851 for (i
= 0; i
< lsblk
->ncts
; i
++)
2852 scols_unref_filter(lsblk
->ct_filters
[i
]);
2853 free(lsblk
->ct_filters
);
2856 lsblk_properties_deinit();
2857 lsblk_unref_devtree(tr
);