2 * lsblk(8) - list block devices
4 * Copyright (C) 2010,2011,2012 Red Hat, Inc. All rights reserved.
5 * Written by Milan Broz <mbroz@redhat.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.
29 #include <sys/types.h>
34 #include <sys/ioctl.h>
44 #include <libsmartcols.h>
53 #include "pathnames.h"
55 #include "canonicalize.h"
60 #include "closestream.h"
66 static UL_DEBUG_DEFINE_MASK(lsblk
);
67 UL_DEBUG_DEFINE_MASKNAMES(lsblk
) = UL_DEBUG_EMPTY_MASKNAMES
;
69 #define LSBLK_DEBUG_INIT (1 << 1)
70 #define LSBLK_DEBUG_FILTER (1 << 2)
71 #define LSBLK_DEBUG_DEV (1 << 3)
72 #define LSBLK_DEBUG_CXT (1 << 4)
73 #define LSBLK_DEBUG_ALL 0xFFFF
75 #define DBG(m, x) __UL_DBG(lsblk, LSBLK_DEBUG_, m, x)
76 #define ON_DBG(m, x) __UL_DBG_CALL(lsblk, LSBLK_DEBUG_, m, x)
79 #define LSBLK_EXIT_SOMEOK 64
80 #define LSBLK_EXIT_ALLFAILED 32
82 static int column_id_to_number(int id
);
133 /* basic table settings */
135 LSBLK_ASCII
= (1 << 0),
136 LSBLK_RAW
= (1 << 1),
137 LSBLK_NOHEADINGS
= (1 << 2),
138 LSBLK_EXPORT
= (1 << 3),
139 LSBLK_TREE
= (1 << 4),
140 LSBLK_JSON
= (1 << 5),
144 SORT_STRING
= 0, /* default is to use scols_cell_get_data() */
145 SORT_U64
= 1 /* use private pointer from scols_cell_get_userdata() */
150 const char *name
; /* header */
151 double whint
; /* width hint (N < 1 is in percent of termwidth) */
152 int flags
; /* SCOLS_FL_* */
155 int sort_type
; /* SORT_* */
158 /* columns descriptions */
159 static struct colinfo infos
[] = {
160 [COL_NAME
] = { "NAME", 0.25, SCOLS_FL_TREE
| SCOLS_FL_NOEXTREMES
, N_("device name") },
161 [COL_KNAME
] = { "KNAME", 0.3, 0, N_("internal kernel device name") },
162 [COL_PKNAME
] = { "PKNAME", 0.3, 0, N_("internal parent kernel device name") },
163 [COL_MAJMIN
] = { "MAJ:MIN", 6, 0, N_("major:minor device number"), SORT_U64
},
164 [COL_FSTYPE
] = { "FSTYPE", 0.1, SCOLS_FL_TRUNC
, N_("filesystem type") },
165 [COL_TARGET
] = { "MOUNTPOINT", 0.10, SCOLS_FL_TRUNC
, N_("where the device is mounted") },
166 [COL_LABEL
] = { "LABEL", 0.1, 0, N_("filesystem LABEL") },
167 [COL_UUID
] = { "UUID", 36, 0, N_("filesystem UUID") },
169 [COL_PARTTYPE
] = { "PARTTYPE", 36, 0, N_("partition type UUID") },
170 [COL_PARTLABEL
] = { "PARTLABEL", 0.1, 0, N_("partition LABEL") },
171 [COL_PARTUUID
] = { "PARTUUID", 36, 0, N_("partition UUID") },
172 [COL_PARTFLAGS
] = { "PARTFLAGS", 36, 0, N_("partition flags") },
174 [COL_RA
] = { "RA", 3, SCOLS_FL_RIGHT
, N_("read-ahead of the device"), SORT_U64
},
175 [COL_RO
] = { "RO", 1, SCOLS_FL_RIGHT
, N_("read-only device") },
176 [COL_RM
] = { "RM", 1, SCOLS_FL_RIGHT
, N_("removable device") },
177 [COL_HOTPLUG
]= { "HOTPLUG", 1, SCOLS_FL_RIGHT
, N_("removable or hotplug device (usb, pcmcia, ...)") },
178 [COL_ROTA
] = { "ROTA", 1, SCOLS_FL_RIGHT
, N_("rotational device") },
179 [COL_RAND
] = { "RAND", 1, SCOLS_FL_RIGHT
, N_("adds randomness") },
180 [COL_MODEL
] = { "MODEL", 0.1, SCOLS_FL_TRUNC
, N_("device identifier") },
181 [COL_SERIAL
] = { "SERIAL", 0.1, SCOLS_FL_TRUNC
, N_("disk serial number") },
182 [COL_SIZE
] = { "SIZE", 5, SCOLS_FL_RIGHT
, N_("size of the device"), SORT_U64
},
183 [COL_STATE
] = { "STATE", 7, SCOLS_FL_TRUNC
, N_("state of the device") },
184 [COL_OWNER
] = { "OWNER", 0.1, SCOLS_FL_TRUNC
, N_("user name"), },
185 [COL_GROUP
] = { "GROUP", 0.1, SCOLS_FL_TRUNC
, N_("group name") },
186 [COL_MODE
] = { "MODE", 10, 0, N_("device node permissions") },
187 [COL_ALIOFF
] = { "ALIGNMENT", 6, SCOLS_FL_RIGHT
, N_("alignment offset"), SORT_U64
},
188 [COL_MINIO
] = { "MIN-IO", 6, SCOLS_FL_RIGHT
, N_("minimum I/O size"), SORT_U64
},
189 [COL_OPTIO
] = { "OPT-IO", 6, SCOLS_FL_RIGHT
, N_("optimal I/O size"), SORT_U64
},
190 [COL_PHYSEC
] = { "PHY-SEC", 7, SCOLS_FL_RIGHT
, N_("physical sector size"), SORT_U64
},
191 [COL_LOGSEC
] = { "LOG-SEC", 7, SCOLS_FL_RIGHT
, N_("logical sector size"), SORT_U64
},
192 [COL_SCHED
] = { "SCHED", 0.1, 0, N_("I/O scheduler name") },
193 [COL_RQ_SIZE
]= { "RQ-SIZE", 5, SCOLS_FL_RIGHT
, N_("request queue size"), SORT_U64
},
194 [COL_TYPE
] = { "TYPE", 4, 0, N_("device type") },
195 [COL_DALIGN
] = { "DISC-ALN", 6, SCOLS_FL_RIGHT
, N_("discard alignment offset"), SORT_U64
},
196 [COL_DGRAN
] = { "DISC-GRAN", 6, SCOLS_FL_RIGHT
, N_("discard granularity"), SORT_U64
},
197 [COL_DMAX
] = { "DISC-MAX", 6, SCOLS_FL_RIGHT
, N_("discard max bytes"), SORT_U64
},
198 [COL_DZERO
] = { "DISC-ZERO", 1, SCOLS_FL_RIGHT
, N_("discard zeroes data") },
199 [COL_WSAME
] = { "WSAME", 6, SCOLS_FL_RIGHT
, N_("write same max bytes"), SORT_U64
},
200 [COL_WWN
] = { "WWN", 18, 0, N_("unique storage identifier") },
201 [COL_HCTL
] = { "HCTL", 10, 0, N_("Host:Channel:Target:Lun for SCSI") },
202 [COL_TRANSPORT
] = { "TRAN", 6, 0, N_("device transport type") },
203 [COL_SUBSYS
] = { "SUBSYSTEMS", 0.1, SCOLS_FL_NOEXTREMES
, N_("de-duplicated chain of subsystems") },
204 [COL_REV
] = { "REV", 4, SCOLS_FL_RIGHT
, N_("device revision") },
205 [COL_VENDOR
] = { "VENDOR", 0.1, SCOLS_FL_TRUNC
, N_("device vendor") },
206 [COL_ZONED
] = { "ZONED", 0.3, 0, N_("zone model") },
210 struct libscols_table
*table
; /* output table */
211 struct libscols_column
*sort_col
;/* sort output by this column */
214 int flags
; /* LSBLK_* */
216 unsigned int all_devices
:1; /* print all devices, including empty */
217 unsigned int bytes
:1; /* print SIZE in bytes */
218 unsigned int inverse
:1; /* print inverse dependencies */
219 unsigned int nodeps
:1; /* don't print slaves/holders */
220 unsigned int scsi
:1; /* print only device with HCTL (SCSI) */
221 unsigned int paths
:1; /* print devnames with "/dev" prefix */
222 unsigned int sort_hidden
:1; /* sort column not between output columns */
223 unsigned int force_tree_order
:1;/* sort lines by parent->tree relation */
226 static struct lsblk
*lsblk
; /* global handler */
228 /* columns[] array specifies all currently wanted output column. The columns
229 * are defined by infos[] array and you can specify (on command line) each
230 * column twice. That's enough, dynamically allocated array of the columns is
231 * unnecessary overkill and over-engineering in this case */
232 static int columns
[ARRAY_SIZE(infos
) * 2];
233 static size_t ncolumns
;
236 static inline void add_column(int id
)
238 if (ncolumns
>= ARRAY_SIZE(columns
))
239 errx(EXIT_FAILURE
, _("too many columns specified, "
240 "the limit is %zu columns"),
241 ARRAY_SIZE(columns
) - 1);
242 columns
[ ncolumns
++ ] = id
;
245 static inline void add_uniq_column(int id
)
247 if (column_id_to_number(id
) < 0)
251 static int excludes
[256];
252 static size_t nexcludes
;
254 static int includes
[256];
255 static size_t nincludes
;
257 static struct libmnt_table
*mtab
, *swaps
;
258 static struct libmnt_cache
*mntcache
;
261 static struct udev
*udev
;
265 struct blkdev_cxt
*parent
;
267 struct libscols_line
*scols_line
;
270 char *name
; /* kernel name in /sys/block */
271 char *dm_name
; /* DM name (dm/block) */
273 char *filename
; /* path to device node */
275 struct sysfs_cxt sysfs
;
277 int partition
; /* is partition? TRUE/FALSE */
279 int probed
; /* already probed */
280 char *fstype
; /* detected fs, NULL or "?" if cannot detect */
281 char *uuid
; /* filesystem UUID (or stack uuid) */
282 char *label
; /* filesystem label */
283 char *parttype
; /* partition type UUID */
284 char *partuuid
; /* partition UUID */
285 char *partlabel
; /* partition label */
286 char *partflags
; /* partition flags */
287 char *wwn
; /* storage WWN */
288 char *serial
; /* disk serial number */
290 int npartitions
; /* # of partitions this device has */
291 int nholders
; /* # of devices mapped directly to this device
292 * /sys/block/.../holders */
293 int nslaves
; /* # of devices this device maps to */
294 int maj
, min
; /* devno */
295 int discard
; /* supports discard */
297 uint64_t size
; /* device size */
300 static void lsblk_init_debug(void)
302 __UL_INIT_DEBUG(lsblk
, LSBLK_DEBUG_
, 0, LSBLK_DEBUG
);
305 static int is_maj_excluded(int maj
)
309 assert(ARRAY_SIZE(excludes
) > nexcludes
);
312 return 0; /* filter not enabled, device not excluded */
314 for (i
= 0; i
< nexcludes
; i
++) {
315 if (excludes
[i
] == maj
) {
316 DBG(FILTER
, ul_debug("exclude: maj=%d", maj
));
323 static int is_maj_included(int maj
)
327 assert(ARRAY_SIZE(includes
) > nincludes
);
330 return 1; /* filter not enabled, device is included */
332 for (i
= 0; i
< nincludes
; i
++) {
333 if (includes
[i
] == maj
) {
334 DBG(FILTER
, ul_debug("include: maj=%d", maj
));
341 /* array with IDs of enabled columns */
342 static int get_column_id(int num
)
345 assert((size_t) num
< ncolumns
);
346 assert(columns
[num
] < (int) ARRAY_SIZE(infos
));
350 static struct colinfo
*get_column_info(int num
)
352 return &infos
[ get_column_id(num
) ];
355 static int column_name_to_id(const char *name
, size_t namesz
)
359 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
360 const char *cn
= infos
[i
].name
;
362 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
365 warnx(_("unknown column: %s"), name
);
369 static int column_id_to_number(int id
)
373 for (i
= 0; i
< ncolumns
; i
++)
374 if (columns
[i
] == id
)
379 static void reset_blkdev_cxt(struct blkdev_cxt
*cxt
)
384 DBG(CXT
, ul_debugobj(cxt
, "reset"));
394 free(cxt
->partlabel
);
398 sysfs_deinit(&cxt
->sysfs
);
400 memset(cxt
, 0, sizeof(*cxt
));
403 static int is_dm(const char *name
)
405 return strncmp(name
, "dm-", 3) ? 0 : 1;
408 static struct dirent
*xreaddir(DIR *dp
)
414 while ((d
= readdir(dp
))) {
415 if (!strcmp(d
->d_name
, ".") ||
416 !strcmp(d
->d_name
, ".."))
419 /* blacklist here? */
425 static char *get_device_path(struct blkdev_cxt
*cxt
)
432 if (is_dm(cxt
->name
))
433 return canonicalize_dm_name(cxt
->name
);
435 snprintf(path
, sizeof(path
), "/dev/%s", cxt
->name
);
436 sysfs_devname_sys_to_dev(path
);
437 return xstrdup(path
);
440 static int table_parser_errcb(struct libmnt_table
*tb
__attribute__((__unused__
)),
441 const char *filename
, int line
)
444 warnx(_("%s: parse error at line %d -- ignored"), filename
, line
);
448 static int is_active_swap(const char *filename
)
451 swaps
= mnt_new_table();
455 mntcache
= mnt_new_cache();
457 mnt_table_set_parser_errcb(swaps
, table_parser_errcb
);
458 mnt_table_set_cache(swaps
, mntcache
);
459 mnt_table_parse_swaps(swaps
, NULL
);
462 return mnt_table_find_srcpath(swaps
, filename
, MNT_ITER_BACKWARD
) != NULL
;
465 static char *get_device_mountpoint(struct blkdev_cxt
*cxt
)
467 struct libmnt_fs
*fs
;
471 assert(cxt
->filename
);
474 mtab
= mnt_new_table();
478 mntcache
= mnt_new_cache();
480 mnt_table_set_parser_errcb(mtab
, table_parser_errcb
);
481 mnt_table_set_cache(mtab
, mntcache
);
482 mnt_table_parse_mtab(mtab
, NULL
);
485 /* Note that maj:min in /proc/self/mountinfo does not have to match with
486 * devno as returned by stat(), so we have to try devname too
488 fs
= mnt_table_find_devno(mtab
, makedev(cxt
->maj
, cxt
->min
), MNT_ITER_BACKWARD
);
490 fs
= mnt_table_find_srcpath(mtab
, cxt
->filename
, MNT_ITER_BACKWARD
);
492 return is_active_swap(cxt
->filename
) ? xstrdup("[SWAP]") : NULL
;
495 fsroot
= mnt_fs_get_root(fs
);
496 if (fsroot
&& strcmp(fsroot
, "/") != 0) {
497 /* hmm.. we found bind mount or btrfs subvolume, let's try to
498 * get real FS root mountpoint */
499 struct libmnt_fs
*rfs
;
500 struct libmnt_iter
*itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
502 mnt_table_set_iter(mtab
, itr
, fs
);
503 while (mnt_table_next_fs(mtab
, itr
, &rfs
) == 0) {
504 fsroot
= mnt_fs_get_root(rfs
);
505 if ((!fsroot
|| strcmp(fsroot
, "/") == 0)
506 && mnt_fs_match_source(rfs
, cxt
->filename
, mntcache
)) {
514 DBG(DEV
, ul_debugobj(cxt
, "mountpoint: %s", mnt_fs_get_target(fs
)));
515 return xstrdup(mnt_fs_get_target(fs
));
519 static int get_udev_properties(struct blkdev_cxt
*cxt
520 __attribute__((__unused__
)))
525 static int get_udev_properties(struct blkdev_cxt
*cxt
)
527 struct udev_device
*dev
;
530 return 0; /* already done */
537 dev
= udev_device_new_from_subsystem_sysname(udev
, "block", cxt
->name
);
541 if ((data
= udev_device_get_property_value(dev
, "ID_FS_LABEL_ENC"))) {
542 cxt
->label
= xstrdup(data
);
543 unhexmangle_string(cxt
->label
);
545 if ((data
= udev_device_get_property_value(dev
, "ID_FS_UUID_ENC"))) {
546 cxt
->uuid
= xstrdup(data
);
547 unhexmangle_string(cxt
->uuid
);
549 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_NAME"))) {
550 cxt
->partlabel
= xstrdup(data
);
551 unhexmangle_string(cxt
->partlabel
);
553 if ((data
= udev_device_get_property_value(dev
, "ID_FS_TYPE")))
554 cxt
->fstype
= xstrdup(data
);
555 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_TYPE")))
556 cxt
->parttype
= xstrdup(data
);
557 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_UUID")))
558 cxt
->partuuid
= xstrdup(data
);
559 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_FLAGS")))
560 cxt
->partflags
= xstrdup(data
);
562 data
= udev_device_get_property_value(dev
, "ID_WWN_WITH_EXTENSION");
564 data
= udev_device_get_property_value(dev
, "ID_WWN");
566 cxt
->wwn
= xstrdup(data
);
568 if ((data
= udev_device_get_property_value(dev
, "ID_SERIAL_SHORT")))
569 cxt
->serial
= xstrdup(data
);
570 udev_device_unref(dev
);
572 DBG(DEV
, ul_debugobj(cxt
, "%s: found udev properties", cxt
->name
));
575 return cxt
->probed
== 1 ? 0 : -1;
578 #endif /* HAVE_LIBUDEV */
580 static void probe_device(struct blkdev_cxt
*cxt
)
582 blkid_probe pr
= NULL
;
591 if (get_udev_properties(cxt
) == 0)
592 return; /* success */
596 /* try libblkid (fallback) */
598 return; /* no permissions to read from the device */
600 pr
= blkid_new_probe_from_filename(cxt
->filename
);
604 blkid_probe_enable_superblocks(pr
, 1);
605 blkid_probe_set_superblocks_flags(pr
, BLKID_SUBLKS_LABEL
|
608 blkid_probe_enable_partitions(pr
, 1);
609 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
611 if (!blkid_do_safeprobe(pr
)) {
612 const char *data
= NULL
;
614 if (!blkid_probe_lookup_value(pr
, "TYPE", &data
, NULL
))
615 cxt
->fstype
= xstrdup(data
);
616 if (!blkid_probe_lookup_value(pr
, "UUID", &data
, NULL
))
617 cxt
->uuid
= xstrdup(data
);
618 if (!blkid_probe_lookup_value(pr
, "LABEL", &data
, NULL
))
619 cxt
->label
= xstrdup(data
);
620 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_TYPE", &data
, NULL
))
621 cxt
->parttype
= xstrdup(data
);
622 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_UUID", &data
, NULL
))
623 cxt
->partuuid
= xstrdup(data
);
624 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_NAME", &data
, NULL
))
625 cxt
->partlabel
= xstrdup(data
);
626 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_FLAGS", &data
, NULL
))
627 cxt
->partflags
= xstrdup(data
);
628 DBG(DEV
, ul_debugobj(cxt
, "%s: found blkid properties", cxt
->name
));
631 blkid_free_probe(pr
);
635 static int is_readonly_device(struct blkdev_cxt
*cxt
)
639 if (sysfs_scanf(&cxt
->sysfs
, "ro", "%d", &ro
) == 1)
642 /* fallback if "ro" attribute does not exist */
643 fd
= open(cxt
->filename
, O_RDONLY
);
645 if (ioctl(fd
, BLKROGET
, &ro
) != 0)
652 static char *get_scheduler(struct blkdev_cxt
*cxt
)
654 char *str
= sysfs_strdup(&cxt
->sysfs
, "queue/scheduler");
655 char *p
, *res
= NULL
;
659 p
= strchr(str
, '[');
662 p
= strchr(res
, ']');
673 static char *get_type(struct blkdev_cxt
*cxt
)
675 char *res
= NULL
, *p
;
677 if (is_dm(cxt
->name
)) {
678 char *dm_uuid
= sysfs_strdup(&cxt
->sysfs
, "dm/uuid");
680 /* The DM_UUID prefix should be set to subsystem owning
681 * the device - LVM, CRYPT, DMRAID, MPATH, PART */
684 char *dm_uuid_prefix
= strsep(&tmp
, "-");
686 if (dm_uuid_prefix
) {
687 /* kpartx hack to remove partition number */
688 if (strncasecmp(dm_uuid_prefix
, "part", 4) == 0)
689 dm_uuid_prefix
[4] = '\0';
691 res
= xstrdup(dm_uuid_prefix
);
697 /* No UUID or no prefix - just mark it as DM device */
700 } else if (!strncmp(cxt
->name
, "loop", 4)) {
701 res
= xstrdup("loop");
703 } else if (!strncmp(cxt
->name
, "md", 2)) {
704 char *md_level
= sysfs_strdup(&cxt
->sysfs
, "md/level");
705 res
= md_level
? md_level
: xstrdup("md");
708 const char *type
= NULL
;
711 if (!sysfs_read_int(&cxt
->sysfs
, "device/type", &x
))
712 type
= blkdev_scsi_type_to_name(x
);
715 type
= cxt
->partition
? "part" : "disk";
719 for (p
= res
; p
&& *p
; p
++)
720 *p
= tolower((unsigned char) *p
);
724 /* Thanks to lsscsi code for idea of detection logic used here */
725 static char *get_transport(struct blkdev_cxt
*cxt
)
727 struct sysfs_cxt
*sysfs
= &cxt
->sysfs
;
729 const char *trans
= NULL
;
731 /* SCSI - Serial Peripheral Interface */
732 if (sysfs_scsi_host_is(sysfs
, "spi"))
735 /* FC/FCoE - Fibre Channel / Fibre Channel over Ethernet */
736 else if (sysfs_scsi_host_is(sysfs
, "fc")) {
737 attr
= sysfs_scsi_host_strdup_attribute(sysfs
, "fc", "symbolic_name");
740 trans
= strstr(attr
, " over ") ? "fcoe" : "fc";
744 /* SAS - Serial Attached SCSI */
745 else if (sysfs_scsi_host_is(sysfs
, "sas") ||
746 sysfs_scsi_has_attribute(sysfs
, "sas_device"))
750 /* SBP - Serial Bus Protocol (FireWire) */
751 else if (sysfs_scsi_has_attribute(sysfs
, "ieee1394_id"))
755 else if (sysfs_scsi_host_is(sysfs
, "iscsi"))
758 /* USB - Universal Serial Bus */
759 else if (sysfs_scsi_path_contains(sysfs
, "usb"))
763 else if (sysfs_scsi_host_is(sysfs
, "scsi")) {
764 attr
= sysfs_scsi_host_strdup_attribute(sysfs
, "scsi", "proc_name");
767 if (!strncmp(attr
, "ahci", 4) || !strncmp(attr
, "sata", 4))
769 else if (strstr(attr
, "ata"))
773 } else if (strncmp(cxt
->name
, "nvme", 4) == 0)
776 return trans
? xstrdup(trans
) : NULL
;
779 static char *get_subsystems(struct blkdev_cxt
*cxt
)
782 char *sub
, *chain
, *res
= NULL
;
783 size_t len
= 0, last
= 0;
785 chain
= sysfs_get_devchain(&cxt
->sysfs
, path
, sizeof(path
));
789 while (sysfs_next_subsystem(&cxt
->sysfs
, chain
, &sub
) == 0) {
792 /* don't create "block:scsi:scsi", but "block:scsi" */
793 if (len
&& strcmp(res
+ last
, sub
) == 0) {
799 res
= xrealloc(res
, len
+ sz
+ 2);
803 memcpy(res
+ len
, sub
, sz
+ 1);
813 #define is_parsable(_l) (scols_table_is_raw((_l)->table) || \
814 scols_table_is_export((_l)->table) || \
815 scols_table_is_json((_l)->table))
817 static char *mk_name(const char *name
)
823 xasprintf(&p
, "/dev/%s", name
);
827 sysfs_devname_sys_to_dev(p
);
831 static char *mk_dm_name(const char *name
)
837 xasprintf(&p
, "/dev/mapper/%s", name
);
843 /* stores data to scols cell userdata (invisible and independent on output)
844 * to make the original values accessible for sort functions
846 static void set_sortdata_u64(struct libscols_line
*ln
, int col
, uint64_t x
)
848 struct libscols_cell
*ce
= scols_line_get_cell(ln
, col
);
853 data
= xmalloc(sizeof(uint64_t));
855 scols_cell_set_userdata(ce
, data
);
858 static void set_sortdata_u64_from_string(struct libscols_line
*ln
, int col
, const char *str
)
862 if (!str
|| sscanf(str
, "%"SCNu64
, &x
) != 1)
865 set_sortdata_u64(ln
, col
, x
);
868 static void unref_sortdata(struct libscols_table
*tb
)
870 struct libscols_iter
*itr
;
871 struct libscols_line
*ln
;
873 if (!tb
|| !lsblk
->sort_col
)
875 itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
878 while (scols_table_next_line(tb
, itr
, &ln
) == 0) {
879 struct libscols_cell
*ce
= scols_line_get_column_cell(ln
,
881 void *data
= scols_cell_get_userdata(ce
);
885 scols_free_iter(itr
);
888 static void set_scols_data(struct blkdev_cxt
*cxt
, int col
, int id
, struct libscols_line
*ln
)
890 int sort
= 0, st_rc
= 0;
893 if (!cxt
->st
.st_rdev
&& (id
== COL_OWNER
|| id
== COL_GROUP
||
895 st_rc
= stat(cxt
->filename
, &cxt
->st
);
897 if (lsblk
->sort_id
== id
)
902 str
= cxt
->dm_name
? mk_dm_name(cxt
->dm_name
) : mk_name(cxt
->name
);
905 str
= mk_name(cxt
->name
);
909 str
= mk_name(cxt
->parent
->name
);
913 struct passwd
*pw
= st_rc
? NULL
: getpwuid(cxt
->st
.st_uid
);
915 str
= xstrdup(pw
->pw_name
);
920 struct group
*gr
= st_rc
? NULL
: getgrgid(cxt
->st
.st_gid
);
922 str
= xstrdup(gr
->gr_name
);
930 xstrmode(cxt
->st
.st_mode
, md
);
936 if (is_parsable(lsblk
))
937 xasprintf(&str
, "%u:%u", cxt
->maj
, cxt
->min
);
939 xasprintf(&str
, "%3u:%-3u", cxt
->maj
, cxt
->min
);
941 set_sortdata_u64(ln
, col
, makedev(cxt
->maj
, cxt
->min
));
946 str
= xstrdup(cxt
->fstype
);
949 str
= get_device_mountpoint(cxt
);
954 str
= xstrdup(cxt
->label
);
959 str
= xstrdup(cxt
->uuid
);
964 str
= xstrdup(cxt
->parttype
);
969 str
= xstrdup(cxt
->partlabel
);
974 str
= xstrdup(cxt
->partuuid
);
979 str
= xstrdup(cxt
->partflags
);
982 get_udev_properties(cxt
);
984 str
= xstrdup(cxt
->wwn
);
987 str
= sysfs_strdup(&cxt
->sysfs
, "queue/read_ahead_kb");
989 set_sortdata_u64_from_string(ln
, col
, str
);
992 str
= xstrdup(is_readonly_device(cxt
) ? "1" : "0");
995 str
= sysfs_strdup(&cxt
->sysfs
, "removable");
996 if (!str
&& cxt
->sysfs
.parent
)
997 str
= sysfs_strdup(cxt
->sysfs
.parent
, "removable");
1000 str
= sysfs_is_hotpluggable(&cxt
->sysfs
) ? xstrdup("1") : xstrdup("0");
1003 str
= sysfs_strdup(&cxt
->sysfs
, "queue/rotational");
1006 str
= sysfs_strdup(&cxt
->sysfs
, "queue/add_random");
1009 if (!cxt
->partition
&& cxt
->nslaves
== 0)
1010 str
= sysfs_strdup(&cxt
->sysfs
, "device/model");
1013 if (!cxt
->partition
&& cxt
->nslaves
== 0) {
1014 get_udev_properties(cxt
);
1016 str
= xstrdup(cxt
->serial
);
1018 str
= sysfs_strdup(&cxt
->sysfs
, "device/serial");
1022 if (!cxt
->partition
&& cxt
->nslaves
== 0)
1023 str
= sysfs_strdup(&cxt
->sysfs
, "device/rev");
1026 if (!cxt
->partition
&& cxt
->nslaves
== 0)
1027 str
= sysfs_strdup(&cxt
->sysfs
, "device/vendor");
1033 xasprintf(&str
, "%ju", cxt
->size
);
1035 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, cxt
->size
);
1037 set_sortdata_u64(ln
, col
, cxt
->size
);
1040 if (!cxt
->partition
&& !cxt
->dm_name
)
1041 str
= sysfs_strdup(&cxt
->sysfs
, "device/state");
1042 else if (cxt
->dm_name
) {
1044 if (sysfs_read_int(&cxt
->sysfs
, "dm/suspended", &x
) == 0)
1045 str
= xstrdup(x
? "suspended" : "running");
1049 str
= sysfs_strdup(&cxt
->sysfs
, "alignment_offset");
1051 set_sortdata_u64_from_string(ln
, col
, str
);
1054 str
= sysfs_strdup(&cxt
->sysfs
, "queue/minimum_io_size");
1056 set_sortdata_u64_from_string(ln
, col
, str
);
1059 str
= sysfs_strdup(&cxt
->sysfs
, "queue/optimal_io_size");
1061 set_sortdata_u64_from_string(ln
, col
, str
);
1064 str
= sysfs_strdup(&cxt
->sysfs
, "queue/physical_block_size");
1066 set_sortdata_u64_from_string(ln
, col
, str
);
1069 str
= sysfs_strdup(&cxt
->sysfs
, "queue/logical_block_size");
1071 set_sortdata_u64_from_string(ln
, col
, str
);
1074 str
= get_scheduler(cxt
);
1077 str
= sysfs_strdup(&cxt
->sysfs
, "queue/nr_requests");
1079 set_sortdata_u64_from_string(ln
, col
, str
);
1082 str
= get_type(cxt
);
1087 if (sysfs_scsi_get_hctl(&cxt
->sysfs
, &h
, &c
, &t
, &l
) == 0)
1088 xasprintf(&str
, "%d:%d:%d:%d", h
, c
, t
, l
);
1092 str
= get_transport(cxt
);
1095 str
= get_subsystems(cxt
);
1099 str
= sysfs_strdup(&cxt
->sysfs
, "discard_alignment");
1103 set_sortdata_u64_from_string(ln
, col
, str
);
1107 str
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_granularity");
1109 set_sortdata_u64_from_string(ln
, col
, str
);
1112 if (sysfs_read_u64(&cxt
->sysfs
,
1113 "queue/discard_granularity", &x
) == 0) {
1114 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1116 set_sortdata_u64(ln
, col
, x
);
1122 str
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_max_bytes");
1124 set_sortdata_u64_from_string(ln
, col
, str
);
1127 if (sysfs_read_u64(&cxt
->sysfs
,
1128 "queue/discard_max_bytes", &x
) == 0) {
1129 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1131 set_sortdata_u64(ln
, col
, x
);
1137 str
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_zeroes_data");
1143 str
= sysfs_strdup(&cxt
->sysfs
, "queue/write_same_max_bytes");
1145 set_sortdata_u64_from_string(ln
, col
, str
);
1149 if (sysfs_read_u64(&cxt
->sysfs
,
1150 "queue/write_same_max_bytes", &x
) == 0) {
1151 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1153 set_sortdata_u64(ln
, col
, x
);
1160 str
= sysfs_strdup(&cxt
->sysfs
, "queue/zoned");
1164 if (str
&& scols_line_refer_data(ln
, col
, str
))
1165 err(EXIT_FAILURE
, _("failed to add output data"));
1168 static void fill_table_line(struct blkdev_cxt
*cxt
, struct libscols_line
*scols_parent
)
1172 cxt
->scols_line
= scols_table_new_line(lsblk
->table
, scols_parent
);
1173 if (!cxt
->scols_line
)
1174 err(EXIT_FAILURE
, _("failed to allocate output line"));
1176 for (i
= 0; i
< ncolumns
; i
++)
1177 set_scols_data(cxt
, i
, get_column_id(i
), cxt
->scols_line
);
1180 static int set_cxt(struct blkdev_cxt
*cxt
,
1181 struct blkdev_cxt
*parent
,
1182 struct blkdev_cxt
*wholedisk
,
1187 DBG(CXT
, ul_debugobj(cxt
, "setting context for %s [parent=%p, wholedisk=%p]",
1188 name
, parent
, wholedisk
));
1190 cxt
->parent
= parent
;
1191 cxt
->name
= xstrdup(name
);
1192 cxt
->partition
= wholedisk
!= NULL
;
1194 cxt
->filename
= get_device_path(cxt
);
1195 if (!cxt
->filename
) {
1196 DBG(CXT
, ul_debugobj(cxt
, "%s: failed to get device path", cxt
->name
));
1199 DBG(CXT
, ul_debugobj(cxt
, "%s: filename=%s", cxt
->name
, cxt
->filename
));
1201 devno
= sysfs_devname_to_devno(cxt
->name
, wholedisk
? wholedisk
->name
: NULL
);
1204 DBG(CXT
, ul_debugobj(cxt
, "%s: unknown device name", cxt
->name
));
1208 if (lsblk
->inverse
) {
1209 if (sysfs_init(&cxt
->sysfs
, devno
, wholedisk
? &wholedisk
->sysfs
: NULL
)) {
1210 DBG(CXT
, ul_debugobj(cxt
, "%s: failed to initialize sysfs handler", cxt
->name
));
1214 parent
->sysfs
.parent
= &cxt
->sysfs
;
1216 if (sysfs_init(&cxt
->sysfs
, devno
, parent
? &parent
->sysfs
: NULL
)) {
1217 DBG(CXT
, ul_debugobj(cxt
, "%s: failed to initialize sysfs handler", cxt
->name
));
1222 cxt
->maj
= major(devno
);
1223 cxt
->min
= minor(devno
);
1226 if (sysfs_read_u64(&cxt
->sysfs
, "size", &cxt
->size
) == 0) /* in sectors */
1227 cxt
->size
<<= 9; /* in bytes */
1229 if (sysfs_read_int(&cxt
->sysfs
,
1230 "queue/discard_granularity", &cxt
->discard
) != 0)
1233 /* Ignore devices of zero size */
1234 if (!lsblk
->all_devices
&& cxt
->size
== 0) {
1235 DBG(CXT
, ul_debugobj(cxt
, "zero size device -- ignore"));
1238 if (is_dm(cxt
->name
)) {
1239 cxt
->dm_name
= sysfs_strdup(&cxt
->sysfs
, "dm/name");
1240 if (!cxt
->dm_name
) {
1241 DBG(CXT
, ul_debugobj(cxt
, "%s: failed to get dm name", cxt
->name
));
1246 cxt
->npartitions
= sysfs_count_partitions(&cxt
->sysfs
, cxt
->name
);
1247 cxt
->nholders
= sysfs_count_dirents(&cxt
->sysfs
, "holders");
1248 cxt
->nslaves
= sysfs_count_dirents(&cxt
->sysfs
, "slaves");
1250 DBG(CXT
, ul_debugobj(cxt
, "%s: npartitions=%d, nholders=%d, nslaves=%d",
1251 cxt
->name
, cxt
->npartitions
, cxt
->nholders
, cxt
->nslaves
));
1253 /* ignore non-SCSI devices */
1254 if (lsblk
->scsi
&& sysfs_scsi_get_hctl(&cxt
->sysfs
, NULL
, NULL
, NULL
, NULL
)) {
1255 DBG(CXT
, ul_debugobj(cxt
, "non-scsi device -- ignore"));
1259 DBG(CXT
, ul_debugobj(cxt
, "%s: context successfully initialized", cxt
->name
));
1263 static int process_blkdev(struct blkdev_cxt
*cxt
, struct blkdev_cxt
*parent
,
1264 int do_partitions
, const char *part_name
);
1267 * List device partitions if any.
1269 static int list_partitions(struct blkdev_cxt
*wholedisk_cxt
, struct blkdev_cxt
*parent_cxt
,
1270 const char *part_name
)
1274 struct blkdev_cxt part_cxt
= { NULL
};
1277 assert(wholedisk_cxt
);
1280 * Do not process further if there are no partitions for
1281 * this device or the device itself is a partition.
1283 if (!wholedisk_cxt
->npartitions
|| wholedisk_cxt
->partition
)
1286 DBG(CXT
, ul_debugobj(wholedisk_cxt
, "probe whole-disk for partitions"));
1288 dir
= sysfs_opendir(&wholedisk_cxt
->sysfs
, NULL
);
1290 err(EXIT_FAILURE
, _("failed to open device directory in sysfs"));
1292 while ((d
= xreaddir(dir
))) {
1293 /* Process particular partition only? */
1294 if (part_name
&& strcmp(part_name
, d
->d_name
))
1297 if (!(sysfs_is_partition_dirent(dir
, d
, wholedisk_cxt
->name
)))
1300 DBG(CXT
, ul_debugobj(wholedisk_cxt
, " checking %s", d
->d_name
));
1302 if (lsblk
->inverse
) {
1309 if (set_cxt(&part_cxt
, parent_cxt
, wholedisk_cxt
, d
->d_name
))
1312 if (!parent_cxt
&& part_cxt
.nholders
)
1315 wholedisk_cxt
->parent
= &part_cxt
;
1316 fill_table_line(&part_cxt
, parent_cxt
? parent_cxt
->scols_line
: NULL
);
1318 process_blkdev(wholedisk_cxt
, &part_cxt
, 0, NULL
);
1326 int ps
= set_cxt(&part_cxt
, wholedisk_cxt
, wholedisk_cxt
, d
->d_name
);
1328 /* Print whole disk only once */
1330 fill_table_line(wholedisk_cxt
, parent_cxt
? parent_cxt
->scols_line
: NULL
);
1331 if (ps
== 0 && !lsblk
->nodeps
)
1332 process_blkdev(&part_cxt
, wholedisk_cxt
, 0, NULL
);
1335 reset_blkdev_cxt(&part_cxt
);
1339 DBG(CXT
, ul_debugobj(wholedisk_cxt
, "probe whole-disk for partitions -- done"));
1344 static int get_wholedisk_from_partition_dirent(DIR *dir
,
1345 struct dirent
*d
, struct blkdev_cxt
*cxt
)
1347 char path
[PATH_MAX
];
1351 if ((len
= readlinkat(dirfd(dir
), d
->d_name
, path
, sizeof(path
) - 1)) < 0)
1356 /* The path ends with ".../<device>/<partition>" */
1357 p
= strrchr(path
, '/');
1362 p
= strrchr(path
, '/');
1367 return set_cxt(cxt
, NULL
, NULL
, p
);
1371 * List device dependencies: partitions, holders (inverse = 0) or slaves (inverse = 1).
1373 static int list_deps(struct blkdev_cxt
*cxt
)
1377 struct blkdev_cxt dep
= { NULL
};
1378 const char *depname
;
1385 DBG(CXT
, ul_debugobj(cxt
, "%s: list dependencies", cxt
->name
));
1387 if (!(lsblk
->inverse
? cxt
->nslaves
: cxt
->nholders
))
1390 depname
= lsblk
->inverse
? "slaves" : "holders";
1391 dir
= sysfs_opendir(&cxt
->sysfs
, depname
);
1395 DBG(CXT
, ul_debugobj(cxt
, "%s: checking for '%s' dependence", cxt
->name
, depname
));
1397 while ((d
= xreaddir(dir
))) {
1398 /* Is the dependency a partition? */
1399 if (sysfs_is_partition_dirent(dir
, d
, NULL
)) {
1400 if (!get_wholedisk_from_partition_dirent(dir
, d
, &dep
)) {
1401 DBG(CXT
, ul_debugobj(cxt
, "%s: %s: dependence is partition",
1402 cxt
->name
, d
->d_name
));
1403 process_blkdev(&dep
, cxt
, 1, d
->d_name
);
1406 /* The dependency is a whole device. */
1407 else if (!set_cxt(&dep
, cxt
, NULL
, d
->d_name
)) {
1408 DBG(CXT
, ul_debugobj(cxt
, "%s: %s: dependence is whole-disk",
1409 cxt
->name
, d
->d_name
));
1410 /* For inverse tree we don't want to show partitions
1411 * if the dependence is on whole-disk */
1412 process_blkdev(&dep
, cxt
, lsblk
->inverse
? 0 : 1, NULL
);
1414 reset_blkdev_cxt(&dep
);
1418 DBG(CXT
, ul_debugobj(cxt
, "%s: checking for '%s' -- done", cxt
->name
, depname
));
1422 static int process_blkdev(struct blkdev_cxt
*cxt
, struct blkdev_cxt
*parent
,
1423 int do_partitions
, const char *part_name
)
1425 if (do_partitions
&& cxt
->npartitions
)
1426 list_partitions(cxt
, parent
, part_name
); /* partitions + whole-disk */
1428 fill_table_line(cxt
, parent
? parent
->scols_line
: NULL
); /* whole-disk only */
1430 return list_deps(cxt
);
1433 /* Iterate devices in sysfs */
1434 static int iterate_block_devices(void)
1438 struct blkdev_cxt cxt
= { NULL
};
1440 if (!(dir
= opendir(_PATH_SYS_BLOCK
)))
1443 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_BLOCK
));
1445 while ((d
= xreaddir(dir
))) {
1447 DBG(DEV
, ul_debug(" %s dentry", d
->d_name
));
1449 if (set_cxt(&cxt
, NULL
, NULL
, d
->d_name
))
1452 if (is_maj_excluded(cxt
.maj
) || !is_maj_included(cxt
.maj
))
1455 /* Skip devices in the middle of dependency tree. */
1456 if ((lsblk
->inverse
? cxt
.nholders
: cxt
.nslaves
) > 0)
1459 process_blkdev(&cxt
, NULL
, 1, NULL
);
1461 reset_blkdev_cxt(&cxt
);
1466 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_BLOCK
" -- done"));
1470 static char *devno_to_sysfs_name(dev_t devno
, char *devname
, char *buf
, size_t buf_size
)
1472 char path
[PATH_MAX
];
1475 if (!sysfs_devno_path(devno
, path
, sizeof(path
))) {
1476 warn(_("%s: failed to compose sysfs path"), devname
);
1480 len
= readlink(path
, buf
, buf_size
- 1);
1482 warn(_("%s: failed to read link"), path
);
1487 return xstrdup(strrchr(buf
, '/') + 1);
1490 static int process_one_device(char *devname
)
1492 struct blkdev_cxt parent
= { NULL
}, cxt
= { NULL
};
1494 char buf
[PATH_MAX
+ 1], *name
= NULL
, *diskname
= NULL
;
1496 int real_part
= 0, rc
= -EINVAL
;
1498 if (stat(devname
, &st
) || !S_ISBLK(st
.st_mode
)) {
1499 warnx(_("%s: not a block device"), devname
);
1503 if (!(name
= devno_to_sysfs_name(st
.st_rdev
, devname
, buf
, PATH_MAX
))) {
1504 warn(_("%s: failed to get sysfs name"), devname
);
1508 if (!strncmp(name
, "dm-", 3)) {
1509 /* dm mapping is never a real partition! */
1512 if (blkid_devno_to_wholedisk(st
.st_rdev
, buf
, sizeof(buf
), &disk
)) {
1513 warn(_("%s: failed to get whole-disk device number"), devname
);
1517 real_part
= st
.st_rdev
!= disk
;
1522 * Device is not a partition.
1524 if (set_cxt(&cxt
, NULL
, NULL
, name
))
1526 process_blkdev(&cxt
, NULL
, !lsblk
->inverse
, NULL
);
1529 * Partition, read sysfs name of the device.
1531 if (set_cxt(&parent
, NULL
, NULL
, diskname
))
1533 if (set_cxt(&cxt
, &parent
, &parent
, name
))
1537 process_blkdev(&parent
, &cxt
, 1, cxt
.name
);
1539 process_blkdev(&cxt
, &parent
, 1, NULL
);
1545 reset_blkdev_cxt(&cxt
);
1548 reset_blkdev_cxt(&parent
);
1553 static void parse_excludes(const char *str0
)
1555 const char *str
= str0
;
1557 while (str
&& *str
) {
1562 n
= strtoul(str
, &end
, 10);
1564 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1565 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1566 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1567 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1568 excludes
[nexcludes
++] = n
;
1570 if (nexcludes
== ARRAY_SIZE(excludes
))
1571 /* TRANSLATORS: The standard value for %d is 256. */
1572 errx(EXIT_FAILURE
, _("the list of excluded devices is "
1573 "too large (limit is %d devices)"),
1574 (int)ARRAY_SIZE(excludes
));
1576 str
= end
&& *end
? end
+ 1 : NULL
;
1580 static void parse_includes(const char *str0
)
1582 const char *str
= str0
;
1584 while (str
&& *str
) {
1589 n
= strtoul(str
, &end
, 10);
1591 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1592 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1593 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1594 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1595 includes
[nincludes
++] = n
;
1597 if (nincludes
== ARRAY_SIZE(includes
))
1598 /* TRANSLATORS: The standard value for %d is 256. */
1599 errx(EXIT_FAILURE
, _("the list of included devices is "
1600 "too large (limit is %d devices)"),
1601 (int)ARRAY_SIZE(includes
));
1602 str
= end
&& *end
? end
+ 1 : NULL
;
1607 * see set_sortdata_u64() and columns initialization in main()
1609 static int cmp_u64_cells(struct libscols_cell
*a
,
1610 struct libscols_cell
*b
,
1611 __attribute__((__unused__
)) void *data
)
1613 uint64_t *adata
= (uint64_t *) scols_cell_get_userdata(a
),
1614 *bdata
= (uint64_t *) scols_cell_get_userdata(b
);
1616 if (adata
== NULL
&& bdata
== NULL
)
1622 return *adata
== *bdata
? 0 : *adata
>= *bdata
? 1 : -1;
1625 static void __attribute__((__noreturn__
)) usage(void)
1630 fputs(USAGE_HEADER
, out
);
1631 fprintf(out
, _(" %s [options] [<device> ...]\n"), program_invocation_short_name
);
1633 fputs(USAGE_SEPARATOR
, out
);
1634 fputs(_("List information about block devices.\n"), out
);
1636 fputs(USAGE_OPTIONS
, out
);
1637 fputs(_(" -a, --all print all devices\n"), out
);
1638 fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out
);
1639 fputs(_(" -d, --nodeps don't print slaves or holders\n"), out
);
1640 fputs(_(" -D, --discard print discard capabilities\n"), out
);
1641 fputs(_(" -z, --zoned print zone model\n"), out
);
1642 fputs(_(" -e, --exclude <list> exclude devices by major number (default: RAM disks)\n"), out
);
1643 fputs(_(" -f, --fs output info about filesystems\n"), out
);
1644 fputs(_(" -i, --ascii use ascii characters only\n"), out
);
1645 fputs(_(" -I, --include <list> show only devices with specified major numbers\n"), out
);
1646 fputs(_(" -J, --json use JSON output format\n"), out
);
1647 fputs(_(" -l, --list use list format output\n"), out
);
1648 fputs(_(" -T, --tree use tree format output\n"), out
);
1649 fputs(_(" -m, --perms output info about permissions\n"), out
);
1650 fputs(_(" -n, --noheadings don't print headings\n"), out
);
1651 fputs(_(" -o, --output <list> output columns\n"), out
);
1652 fputs(_(" -O, --output-all output all columns\n"), out
);
1653 fputs(_(" -p, --paths print complete device path\n"), out
);
1654 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out
);
1655 fputs(_(" -r, --raw use raw output format\n"), out
);
1656 fputs(_(" -s, --inverse inverse dependencies\n"), out
);
1657 fputs(_(" -S, --scsi output info about SCSI devices\n"), out
);
1658 fputs(_(" -t, --topology output info about topology\n"), out
);
1659 fputs(_(" -x, --sort <column> sort output by <column>\n"), out
);
1660 fputs(USAGE_SEPARATOR
, out
);
1661 printf(USAGE_HELP_OPTIONS(22));
1663 fprintf(out
, USAGE_COLUMNS
);
1665 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
1666 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
1668 printf(USAGE_MAN_TAIL("lsblk(8)"));
1673 static void check_sysdevblock(void)
1675 if (access(_PATH_SYS_DEVBLOCK
, R_OK
) != 0)
1676 err(EXIT_FAILURE
, _("failed to access sysfs directory: %s"),
1677 _PATH_SYS_DEVBLOCK
);
1680 int main(int argc
, char *argv
[])
1682 struct lsblk _ls
= { .sort_id
= -1, .flags
= LSBLK_TREE
};
1683 int c
, status
= EXIT_FAILURE
;
1684 char *outarg
= NULL
;
1688 static const struct option longopts
[] = {
1689 { "all", no_argument
, NULL
, 'a' },
1690 { "bytes", no_argument
, NULL
, 'b' },
1691 { "nodeps", no_argument
, NULL
, 'd' },
1692 { "discard", no_argument
, NULL
, 'D' },
1693 { "zoned", no_argument
, NULL
, 'z' },
1694 { "help", no_argument
, NULL
, 'h' },
1695 { "json", no_argument
, NULL
, 'J' },
1696 { "output", required_argument
, NULL
, 'o' },
1697 { "output-all", no_argument
, NULL
, 'O' },
1698 { "perms", no_argument
, NULL
, 'm' },
1699 { "noheadings", no_argument
, NULL
, 'n' },
1700 { "list", no_argument
, NULL
, 'l' },
1701 { "ascii", no_argument
, NULL
, 'i' },
1702 { "raw", no_argument
, NULL
, 'r' },
1703 { "inverse", no_argument
, NULL
, 's' },
1704 { "fs", no_argument
, NULL
, 'f' },
1705 { "exclude", required_argument
, NULL
, 'e' },
1706 { "include", required_argument
, NULL
, 'I' },
1707 { "topology", no_argument
, NULL
, 't' },
1708 { "paths", no_argument
, NULL
, 'p' },
1709 { "pairs", no_argument
, NULL
, 'P' },
1710 { "scsi", no_argument
, NULL
, 'S' },
1711 { "sort", required_argument
, NULL
, 'x' },
1712 { "tree", no_argument
, NULL
, 'T' },
1713 { "version", no_argument
, NULL
, 'V' },
1714 { NULL
, 0, NULL
, 0 },
1717 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
1725 { 'P','T', 'l','r' },
1728 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1730 setlocale(LC_ALL
, "");
1731 bindtextdomain(PACKAGE
, LOCALEDIR
);
1732 textdomain(PACKAGE
);
1733 atexit(close_stdout
);
1739 while((c
= getopt_long(argc
, argv
,
1740 "abdDze:fhJlnmo:OpPiI:rstVSTx:", longopts
, NULL
)) != -1) {
1742 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1746 lsblk
->all_devices
= 1;
1755 add_uniq_column(COL_NAME
);
1756 add_uniq_column(COL_DALIGN
);
1757 add_uniq_column(COL_DGRAN
);
1758 add_uniq_column(COL_DMAX
);
1759 add_uniq_column(COL_DZERO
);
1762 add_uniq_column(COL_NAME
);
1763 add_uniq_column(COL_ZONED
);
1766 parse_excludes(optarg
);
1772 lsblk
->flags
|= LSBLK_JSON
;
1775 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
1778 lsblk
->flags
|= LSBLK_NOHEADINGS
;
1784 for (ncolumns
= 0 ; ncolumns
< ARRAY_SIZE(infos
); ncolumns
++)
1785 columns
[ncolumns
] = ncolumns
;
1791 lsblk
->flags
|= LSBLK_EXPORT
;
1792 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
1795 lsblk
->flags
|= LSBLK_ASCII
;
1798 parse_includes(optarg
);
1801 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
1802 lsblk
->flags
|= LSBLK_RAW
; /* enable raw */
1808 add_uniq_column(COL_NAME
);
1809 add_uniq_column(COL_FSTYPE
);
1810 add_uniq_column(COL_LABEL
);
1811 add_uniq_column(COL_UUID
);
1812 add_uniq_column(COL_TARGET
);
1815 add_uniq_column(COL_NAME
);
1816 add_uniq_column(COL_SIZE
);
1817 add_uniq_column(COL_OWNER
);
1818 add_uniq_column(COL_GROUP
);
1819 add_uniq_column(COL_MODE
);
1822 add_uniq_column(COL_NAME
);
1823 add_uniq_column(COL_ALIOFF
);
1824 add_uniq_column(COL_MINIO
);
1825 add_uniq_column(COL_OPTIO
);
1826 add_uniq_column(COL_PHYSEC
);
1827 add_uniq_column(COL_LOGSEC
);
1828 add_uniq_column(COL_ROTA
);
1829 add_uniq_column(COL_SCHED
);
1830 add_uniq_column(COL_RQ_SIZE
);
1831 add_uniq_column(COL_RA
);
1832 add_uniq_column(COL_WSAME
);
1837 add_uniq_column(COL_NAME
);
1838 add_uniq_column(COL_HCTL
);
1839 add_uniq_column(COL_TYPE
);
1840 add_uniq_column(COL_VENDOR
);
1841 add_uniq_column(COL_MODEL
);
1842 add_uniq_column(COL_REV
);
1843 add_uniq_column(COL_TRANSPORT
);
1849 printf(UTIL_LINUX_VERSION
);
1850 return EXIT_SUCCESS
;
1852 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
1853 lsblk
->sort_id
= column_name_to_id(optarg
, strlen(optarg
));
1854 if (lsblk
->sort_id
>= 0)
1858 errtryhelp(EXIT_FAILURE
);
1863 lsblk
->flags
|= LSBLK_TREE
;
1865 check_sysdevblock();
1868 add_column(COL_NAME
);
1869 add_column(COL_MAJMIN
);
1871 add_column(COL_SIZE
);
1873 add_column(COL_TYPE
);
1874 add_column(COL_TARGET
);
1877 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
1878 &ncolumns
, column_name_to_id
) < 0)
1879 return EXIT_FAILURE
;
1881 if (nexcludes
== 0 && nincludes
== 0)
1882 excludes
[nexcludes
++] = 1; /* default: ignore RAM disks */
1884 if (lsblk
->sort_id
< 0)
1885 /* Since Linux 4.8 we have sort devices by default, because
1886 * /sys is no more sorted */
1887 lsblk
->sort_id
= COL_MAJMIN
;
1889 /* For --inverse --list we still follow parent->child relation */
1890 if (lsblk
->inverse
&& !(lsblk
->flags
& LSBLK_TREE
))
1891 lsblk
->force_tree_order
= 1;
1893 if (lsblk
->sort_id
>= 0 && column_id_to_number(lsblk
->sort_id
) < 0) {
1894 /* the sort column is not between output columns -- add as hidden */
1895 add_column(lsblk
->sort_id
);
1896 lsblk
->sort_hidden
= 1;
1900 scols_init_debug(0);
1903 * initialize output columns
1905 if (!(lsblk
->table
= scols_new_table()))
1906 errx(EXIT_FAILURE
, _("failed to allocate output table"));
1907 scols_table_enable_raw(lsblk
->table
, !!(lsblk
->flags
& LSBLK_RAW
));
1908 scols_table_enable_export(lsblk
->table
, !!(lsblk
->flags
& LSBLK_EXPORT
));
1909 scols_table_enable_ascii(lsblk
->table
, !!(lsblk
->flags
& LSBLK_ASCII
));
1910 scols_table_enable_json(lsblk
->table
, !!(lsblk
->flags
& LSBLK_JSON
));
1911 scols_table_enable_noheadings(lsblk
->table
, !!(lsblk
->flags
& LSBLK_NOHEADINGS
));
1913 if (lsblk
->flags
& LSBLK_JSON
)
1914 scols_table_set_name(lsblk
->table
, "blockdevices");
1916 for (i
= 0; i
< ncolumns
; i
++) {
1917 struct colinfo
*ci
= get_column_info(i
);
1918 struct libscols_column
*cl
;
1919 int id
= get_column_id(i
), fl
= ci
->flags
;
1921 if (!(lsblk
->flags
& LSBLK_TREE
) && id
== COL_NAME
)
1922 fl
&= ~SCOLS_FL_TREE
;
1923 if (lsblk
->sort_hidden
&& lsblk
->sort_id
== id
)
1924 fl
|= SCOLS_FL_HIDDEN
;
1926 cl
= scols_table_new_column(lsblk
->table
, ci
->name
, ci
->whint
, fl
);
1928 warn(_("failed to allocate output column"));
1931 if (!lsblk
->sort_col
&& lsblk
->sort_id
== id
) {
1932 lsblk
->sort_col
= cl
;
1933 scols_column_set_cmpfunc(cl
,
1934 ci
->sort_type
== SORT_STRING
?
1935 scols_cmpstr_cells
: cmp_u64_cells
, NULL
);
1940 status
= iterate_block_devices() == 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
1942 int cnt
= 0, cnt_err
= 0;
1944 while (optind
< argc
) {
1945 if (process_one_device(argv
[optind
++]) != 0)
1949 status
= cnt
== 0 ? EXIT_FAILURE
: /* nothing */
1950 cnt
== cnt_err
? LSBLK_EXIT_ALLFAILED
:/* all failed */
1951 cnt_err
? LSBLK_EXIT_SOMEOK
: /* some ok */
1952 EXIT_SUCCESS
; /* all success */
1955 if (lsblk
->sort_col
)
1956 scols_sort_table(lsblk
->table
, lsblk
->sort_col
);
1957 if (lsblk
->force_tree_order
)
1958 scols_sort_table_by_tree(lsblk
->table
);
1960 scols_print_table(lsblk
->table
);
1963 if (lsblk
->sort_col
)
1964 unref_sortdata(lsblk
->table
);
1966 scols_unref_table(lsblk
->table
);
1968 mnt_unref_table(mtab
);
1969 mnt_unref_table(swaps
);
1970 mnt_unref_cache(mntcache
);