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"
61 #include "closestream.h"
67 UL_DEBUG_DEFINE_MASK(lsblk
);
68 UL_DEBUG_DEFINE_MASKNAMES(lsblk
) = UL_DEBUG_EMPTY_MASKNAMES
;
70 #define LSBLK_DEBUG_INIT (1 << 1)
71 #define LSBLK_DEBUG_FILTER (1 << 2)
72 #define LSBLK_DEBUG_DEV (1 << 3)
73 #define LSBLK_DEBUG_CXT (1 << 4)
74 #define LSBLK_DEBUG_ALL 0xFFFF
76 #define DBG(m, x) __UL_DBG(lsblk, LSBLK_DEBUG_, m, x)
77 #define ON_DBG(m, x) __UL_DBG_CALL(lsblk, LSBLK_DEBUG_, m, x)
80 #define LSBLK_EXIT_SOMEOK 64
81 #define LSBLK_EXIT_ALLFAILED 32
131 /* basic table settings */
133 LSBLK_ASCII
= (1 << 0),
134 LSBLK_RAW
= (1 << 1),
135 LSBLK_NOHEADINGS
= (1 << 2),
136 LSBLK_EXPORT
= (1 << 3),
137 LSBLK_TREE
= (1 << 4),
138 LSBLK_JSON
= (1 << 5),
142 SORT_STRING
= 0, /* default is to use scols_cell_get_data() */
143 SORT_U64
= 1 /* use private pointer from scols_cell_get_userdata() */
148 const char *name
; /* header */
149 double whint
; /* width hint (N < 1 is in percent of termwidth) */
150 int flags
; /* SCOLS_FL_* */
153 int sort_type
; /* SORT_* */
156 /* columns descriptions */
157 static struct colinfo infos
[] = {
158 [COL_NAME
] = { "NAME", 0.25, SCOLS_FL_TREE
| SCOLS_FL_NOEXTREMES
, N_("device name") },
159 [COL_KNAME
] = { "KNAME", 0.3, 0, N_("internal kernel device name") },
160 [COL_PKNAME
] = { "PKNAME", 0.3, 0, N_("internal parent kernel device name") },
161 [COL_MAJMIN
] = { "MAJ:MIN", 6, 0, N_("major:minor device number"), SORT_U64
},
162 [COL_FSTYPE
] = { "FSTYPE", 0.1, SCOLS_FL_TRUNC
, N_("filesystem type") },
163 [COL_TARGET
] = { "MOUNTPOINT", 0.10, SCOLS_FL_TRUNC
, N_("where the device is mounted") },
164 [COL_LABEL
] = { "LABEL", 0.1, 0, N_("filesystem LABEL") },
165 [COL_UUID
] = { "UUID", 36, 0, N_("filesystem UUID") },
167 [COL_PARTTYPE
] = { "PARTTYPE", 36, 0, N_("partition type UUID") },
168 [COL_PARTLABEL
] = { "PARTLABEL", 0.1, 0, N_("partition LABEL") },
169 [COL_PARTUUID
] = { "PARTUUID", 36, 0, N_("partition UUID") },
170 [COL_PARTFLAGS
] = { "PARTFLAGS", 36, 0, N_("partition flags") },
172 [COL_RA
] = { "RA", 3, SCOLS_FL_RIGHT
, N_("read-ahead of the device"), SORT_U64
},
173 [COL_RO
] = { "RO", 1, SCOLS_FL_RIGHT
, N_("read-only device") },
174 [COL_RM
] = { "RM", 1, SCOLS_FL_RIGHT
, N_("removable device") },
175 [COL_HOTPLUG
]= { "HOTPLUG", 1, SCOLS_FL_RIGHT
, N_("removable or hotplug device (usb, pcmcia, ...)") },
176 [COL_ROTA
] = { "ROTA", 1, SCOLS_FL_RIGHT
, N_("rotational device") },
177 [COL_RAND
] = { "RAND", 1, SCOLS_FL_RIGHT
, N_("adds randomness") },
178 [COL_MODEL
] = { "MODEL", 0.1, SCOLS_FL_TRUNC
, N_("device identifier") },
179 [COL_SERIAL
] = { "SERIAL", 0.1, SCOLS_FL_TRUNC
, N_("disk serial number") },
180 [COL_SIZE
] = { "SIZE", 5, SCOLS_FL_RIGHT
, N_("size of the device"), SORT_U64
},
181 [COL_STATE
] = { "STATE", 7, SCOLS_FL_TRUNC
, N_("state of the device") },
182 [COL_OWNER
] = { "OWNER", 0.1, SCOLS_FL_TRUNC
, N_("user name"), },
183 [COL_GROUP
] = { "GROUP", 0.1, SCOLS_FL_TRUNC
, N_("group name") },
184 [COL_MODE
] = { "MODE", 10, 0, N_("device node permissions") },
185 [COL_ALIOFF
] = { "ALIGNMENT", 6, SCOLS_FL_RIGHT
, N_("alignment offset"), SORT_U64
},
186 [COL_MINIO
] = { "MIN-IO", 6, SCOLS_FL_RIGHT
, N_("minimum I/O size"), SORT_U64
},
187 [COL_OPTIO
] = { "OPT-IO", 6, SCOLS_FL_RIGHT
, N_("optimal I/O size"), SORT_U64
},
188 [COL_PHYSEC
] = { "PHY-SEC", 7, SCOLS_FL_RIGHT
, N_("physical sector size"), SORT_U64
},
189 [COL_LOGSEC
] = { "LOG-SEC", 7, SCOLS_FL_RIGHT
, N_("logical sector size"), SORT_U64
},
190 [COL_SCHED
] = { "SCHED", 0.1, 0, N_("I/O scheduler name") },
191 [COL_RQ_SIZE
]= { "RQ-SIZE", 5, SCOLS_FL_RIGHT
, N_("request queue size"), SORT_U64
},
192 [COL_TYPE
] = { "TYPE", 4, 0, N_("device type") },
193 [COL_DALIGN
] = { "DISC-ALN", 6, SCOLS_FL_RIGHT
, N_("discard alignment offset"), SORT_U64
},
194 [COL_DGRAN
] = { "DISC-GRAN", 6, SCOLS_FL_RIGHT
, N_("discard granularity"), SORT_U64
},
195 [COL_DMAX
] = { "DISC-MAX", 6, SCOLS_FL_RIGHT
, N_("discard max bytes"), SORT_U64
},
196 [COL_DZERO
] = { "DISC-ZERO", 1, SCOLS_FL_RIGHT
, N_("discard zeroes data") },
197 [COL_WSAME
] = { "WSAME", 6, SCOLS_FL_RIGHT
, N_("write same max bytes"), SORT_U64
},
198 [COL_WWN
] = { "WWN", 18, 0, N_("unique storage identifier") },
199 [COL_HCTL
] = { "HCTL", 10, 0, N_("Host:Channel:Target:Lun for SCSI") },
200 [COL_TRANSPORT
] = { "TRAN", 6, 0, N_("device transport type") },
201 [COL_SUBSYS
] = { "SUBSYSTEMS", 0.1, SCOLS_FL_NOEXTREMES
, N_("de-duplicated chain of subsystems") },
202 [COL_REV
] = { "REV", 4, SCOLS_FL_RIGHT
, N_("device revision") },
203 [COL_VENDOR
] = { "VENDOR", 0.1, SCOLS_FL_TRUNC
, N_("device vendor") },
207 struct libscols_table
*table
; /* output table */
208 struct libscols_column
*sort_col
;/* sort output by this column */
211 unsigned int all_devices
:1; /* print all devices, including empty */
212 unsigned int bytes
:1; /* print SIZE in bytes */
213 unsigned int inverse
:1; /* print inverse dependencies */
214 unsigned int nodeps
:1; /* don't print slaves/holders */
215 unsigned int scsi
:1; /* print only device with HCTL (SCSI) */
216 unsigned int paths
:1; /* print devnames with "/dev" prefix */
217 unsigned int sort_hidden
:1; /* sort column not between output columns */
220 struct lsblk
*lsblk
; /* global handler */
222 /* columns[] array specifies all currently wanted output column. The columns
223 * are defined by infos[] array and you can specify (on command line) each
224 * column twice. That's enough, dynamically allocated array of the columns is
225 * unnecessary overkill and over-engineering in this case */
226 static int columns
[ARRAY_SIZE(infos
) * 2];
227 static size_t ncolumns
;
229 static inline size_t err_columns_index(size_t arysz
, size_t idx
)
232 errx(EXIT_FAILURE
, _("too many columns specified, "
233 "the limit is %zu columns"),
238 #define add_column(ary, n, id) \
239 ((ary)[ err_columns_index(ARRAY_SIZE(ary), (n)) ] = (id))
241 static int excludes
[256];
242 static size_t nexcludes
;
244 static int includes
[256];
245 static size_t nincludes
;
247 static struct libmnt_table
*mtab
, *swaps
;
248 static struct libmnt_cache
*mntcache
;
255 struct blkdev_cxt
*parent
;
257 struct libscols_line
*scols_line
;
260 char *name
; /* kernel name in /sys/block */
261 char *dm_name
; /* DM name (dm/block) */
263 char *filename
; /* path to device node */
265 struct sysfs_cxt sysfs
;
267 int partition
; /* is partition? TRUE/FALSE */
269 int probed
; /* already probed */
270 char *fstype
; /* detected fs, NULL or "?" if cannot detect */
271 char *uuid
; /* filesystem UUID (or stack uuid) */
272 char *label
; /* filesystem label */
273 char *parttype
; /* partiton type UUID */
274 char *partuuid
; /* partition UUID */
275 char *partlabel
; /* partiton label */
276 char *partflags
; /* partition flags */
277 char *wwn
; /* storage WWN */
278 char *serial
; /* disk serial number */
280 int npartitions
; /* # of partitions this device has */
281 int nholders
; /* # of devices mapped directly to this device
282 * /sys/block/.../holders */
283 int nslaves
; /* # of devices this device maps to */
284 int maj
, min
; /* devno */
285 int discard
; /* supports discard */
287 uint64_t size
; /* device size */
290 static void lsblk_init_debug(void)
292 __UL_INIT_DEBUG(lsblk
, LSBLK_DEBUG_
, 0, LSBLK_DEBUG
);
295 static int is_maj_excluded(int maj
)
299 assert(ARRAY_SIZE(excludes
) > nexcludes
);
302 return 0; /* filter not enabled, device not exluded */
304 for (i
= 0; i
< nexcludes
; i
++) {
305 if (excludes
[i
] == maj
) {
306 DBG(FILTER
, ul_debug("exclude: maj=%d", maj
));
313 static int is_maj_included(int maj
)
317 assert(ARRAY_SIZE(includes
) > nincludes
);
320 return 1; /* filter not enabled, device is included */
322 for (i
= 0; i
< nincludes
; i
++) {
323 if (includes
[i
] == maj
) {
324 DBG(FILTER
, ul_debug("include: maj=%d", maj
));
331 /* array with IDs of enabled columns */
332 static int get_column_id(int num
)
335 assert((size_t) num
< ncolumns
);
336 assert(columns
[num
] < (int) ARRAY_SIZE(infos
));
340 static struct colinfo
*get_column_info(int num
)
342 return &infos
[ get_column_id(num
) ];
345 static int column_name_to_id(const char *name
, size_t namesz
)
349 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
350 const char *cn
= infos
[i
].name
;
352 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
355 warnx(_("unknown column: %s"), name
);
359 static int column_id_to_number(int id
)
363 for (i
= 0; i
< ncolumns
; i
++)
364 if (columns
[i
] == id
)
369 static void reset_blkdev_cxt(struct blkdev_cxt
*cxt
)
374 DBG(CXT
, ul_debugobj(cxt
, "reset"));
384 free(cxt
->partlabel
);
388 sysfs_deinit(&cxt
->sysfs
);
390 memset(cxt
, 0, sizeof(*cxt
));
393 static int is_dm(const char *name
)
395 return strncmp(name
, "dm-", 3) ? 0 : 1;
398 static struct dirent
*xreaddir(DIR *dp
)
404 while ((d
= readdir(dp
))) {
405 if (!strcmp(d
->d_name
, ".") ||
406 !strcmp(d
->d_name
, ".."))
409 /* blacklist here? */
415 static char *get_device_path(struct blkdev_cxt
*cxt
)
422 if (is_dm(cxt
->name
))
423 return canonicalize_dm_name(cxt
->name
);
425 snprintf(path
, sizeof(path
), "/dev/%s", cxt
->name
);
426 sysfs_devname_sys_to_dev(path
);
427 return xstrdup(path
);
430 static int table_parser_errcb(struct libmnt_table
*tb
__attribute__((__unused__
)),
431 const char *filename
, int line
)
434 warnx(_("%s: parse error: ignore entry at line %d."),
439 static int is_active_swap(const char *filename
)
442 swaps
= mnt_new_table();
446 mntcache
= mnt_new_cache();
448 mnt_table_set_parser_errcb(swaps
, table_parser_errcb
);
449 mnt_table_set_cache(swaps
, mntcache
);
450 mnt_table_parse_swaps(swaps
, NULL
);
453 return mnt_table_find_srcpath(swaps
, filename
, MNT_ITER_BACKWARD
) != 0;
456 static char *get_device_mountpoint(struct blkdev_cxt
*cxt
)
458 struct libmnt_fs
*fs
;
462 assert(cxt
->filename
);
465 mtab
= mnt_new_table();
469 mntcache
= mnt_new_cache();
471 mnt_table_set_parser_errcb(mtab
, table_parser_errcb
);
472 mnt_table_set_cache(mtab
, mntcache
);
473 mnt_table_parse_mtab(mtab
, NULL
);
476 /* Note that maj:min in /proc/self/mouninfo does not have to match with
477 * devno as returned by stat(), so we have to try devname too
479 fs
= mnt_table_find_devno(mtab
, makedev(cxt
->maj
, cxt
->min
), MNT_ITER_BACKWARD
);
481 fs
= mnt_table_find_srcpath(mtab
, cxt
->filename
, MNT_ITER_BACKWARD
);
483 return is_active_swap(cxt
->filename
) ? xstrdup("[SWAP]") : NULL
;
486 fsroot
= mnt_fs_get_root(fs
);
487 if (fsroot
&& strcmp(fsroot
, "/") != 0) {
488 /* hmm.. we found bind mount or btrfs subvolume, let's try to
489 * get real FS root mountpoint */
490 struct libmnt_fs
*rfs
;
491 struct libmnt_iter
*itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
493 mnt_table_set_iter(mtab
, itr
, fs
);
494 while (mnt_table_next_fs(mtab
, itr
, &rfs
) == 0) {
495 fsroot
= mnt_fs_get_root(rfs
);
496 if ((!fsroot
|| strcmp(fsroot
, "/") == 0)
497 && mnt_fs_match_source(rfs
, cxt
->filename
, mntcache
)) {
505 DBG(DEV
, ul_debugobj(cxt
, "mountpoint: %s", mnt_fs_get_target(fs
)));
506 return xstrdup(mnt_fs_get_target(fs
));
510 static int get_udev_properties(struct blkdev_cxt
*cxt
511 __attribute__((__unused__
)))
516 static int get_udev_properties(struct blkdev_cxt
*cxt
)
518 struct udev_device
*dev
;
521 return 0; /* already done */
528 dev
= udev_device_new_from_subsystem_sysname(udev
, "block", cxt
->name
);
532 if ((data
= udev_device_get_property_value(dev
, "ID_FS_LABEL_ENC"))) {
533 cxt
->label
= xstrdup(data
);
534 unhexmangle_string(cxt
->label
);
536 if ((data
= udev_device_get_property_value(dev
, "ID_FS_UUID_ENC"))) {
537 cxt
->uuid
= xstrdup(data
);
538 unhexmangle_string(cxt
->uuid
);
540 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_NAME"))) {
541 cxt
->partlabel
= xstrdup(data
);
542 unhexmangle_string(cxt
->partlabel
);
544 if ((data
= udev_device_get_property_value(dev
, "ID_FS_TYPE")))
545 cxt
->fstype
= xstrdup(data
);
546 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_TYPE")))
547 cxt
->parttype
= xstrdup(data
);
548 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_UUID")))
549 cxt
->partuuid
= xstrdup(data
);
550 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_FLAGS")))
551 cxt
->partflags
= xstrdup(data
);
552 if ((data
= udev_device_get_property_value(dev
, "ID_WWN")))
553 cxt
->wwn
= xstrdup(data
);
554 if ((data
= udev_device_get_property_value(dev
, "ID_SERIAL_SHORT")))
555 cxt
->serial
= xstrdup(data
);
556 udev_device_unref(dev
);
558 DBG(DEV
, ul_debugobj(cxt
, "%s: found udev properties", cxt
->name
));
561 return cxt
->probed
== 1 ? 0 : -1;
564 #endif /* HAVE_LIBUDEV */
566 static void probe_device(struct blkdev_cxt
*cxt
)
568 blkid_probe pr
= NULL
;
577 if (get_udev_properties(cxt
) == 0)
578 return; /* success */
582 /* try libblkid (fallback) */
584 return; /* no permissions to read from the device */
586 pr
= blkid_new_probe_from_filename(cxt
->filename
);
590 blkid_probe_enable_superblocks(pr
, 1);
591 blkid_probe_set_superblocks_flags(pr
, BLKID_SUBLKS_LABEL
|
594 blkid_probe_enable_partitions(pr
, 1);
595 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
597 if (!blkid_do_safeprobe(pr
)) {
598 const char *data
= NULL
;
600 if (!blkid_probe_lookup_value(pr
, "TYPE", &data
, NULL
))
601 cxt
->fstype
= xstrdup(data
);
602 if (!blkid_probe_lookup_value(pr
, "UUID", &data
, NULL
))
603 cxt
->uuid
= xstrdup(data
);
604 if (!blkid_probe_lookup_value(pr
, "LABEL", &data
, NULL
))
605 cxt
->label
= xstrdup(data
);
606 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_TYPE", &data
, NULL
))
607 cxt
->parttype
= xstrdup(data
);
608 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_UUID", &data
, NULL
))
609 cxt
->partuuid
= xstrdup(data
);
610 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_NAME", &data
, NULL
))
611 cxt
->partlabel
= xstrdup(data
);
612 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_FLAGS", &data
, NULL
))
613 cxt
->partflags
= xstrdup(data
);
614 DBG(DEV
, ul_debugobj(cxt
, "%s: found blkid properties", cxt
->name
));
617 blkid_free_probe(pr
);
621 static int is_readonly_device(struct blkdev_cxt
*cxt
)
625 if (sysfs_scanf(&cxt
->sysfs
, "ro", "%d", &ro
) == 1)
628 /* fallback if "ro" attribute does not exist */
629 fd
= open(cxt
->filename
, O_RDONLY
);
631 if (ioctl(fd
, BLKROGET
, &ro
) != 0)
638 static char *get_scheduler(struct blkdev_cxt
*cxt
)
640 char *str
= sysfs_strdup(&cxt
->sysfs
, "queue/scheduler");
641 char *p
, *res
= NULL
;
645 p
= strchr(str
, '[');
648 p
= strchr(res
, ']');
659 static char *get_type(struct blkdev_cxt
*cxt
)
661 char *res
= NULL
, *p
;
663 if (is_dm(cxt
->name
)) {
664 char *dm_uuid
= sysfs_strdup(&cxt
->sysfs
, "dm/uuid");
666 /* The DM_UUID prefix should be set to subsystem owning
667 * the device - LVM, CRYPT, DMRAID, MPATH, PART */
670 char *dm_uuid_prefix
= strsep(&tmp
, "-");
672 if (dm_uuid_prefix
) {
673 /* kpartx hack to remove partition number */
674 if (strncasecmp(dm_uuid_prefix
, "part", 4) == 0)
675 dm_uuid_prefix
[4] = '\0';
677 res
= xstrdup(dm_uuid_prefix
);
683 /* No UUID or no prefix - just mark it as DM device */
686 } else if (!strncmp(cxt
->name
, "loop", 4)) {
687 res
= xstrdup("loop");
689 } else if (!strncmp(cxt
->name
, "md", 2)) {
690 char *md_level
= sysfs_strdup(&cxt
->sysfs
, "md/level");
691 res
= md_level
? md_level
: xstrdup("md");
694 const char *type
= NULL
;
697 if (!sysfs_read_int(&cxt
->sysfs
, "device/type", &x
))
698 type
= blkdev_scsi_type_to_name(x
);
701 type
= cxt
->partition
? "part" : "disk";
705 for (p
= res
; p
&& *p
; p
++)
706 *p
= tolower((unsigned char) *p
);
710 /* Thanks to lsscsi code for idea of detection logic used here */
711 static char *get_transport(struct blkdev_cxt
*cxt
)
713 struct sysfs_cxt
*sysfs
= &cxt
->sysfs
;
715 const char *trans
= NULL
;
717 /* SCSI - Serial Peripheral Interface */
718 if (sysfs_scsi_host_is(sysfs
, "spi"))
721 /* FC/FCoE - Fibre Channel / Fibre Channel over Ethernet */
722 else if (sysfs_scsi_host_is(sysfs
, "fc")) {
723 attr
= sysfs_scsi_host_strdup_attribute(sysfs
, "fc", "symbolic_name");
726 trans
= strstr(attr
, " over ") ? "fcoe" : "fc";
730 /* SAS - Serial Attached SCSI */
731 else if (sysfs_scsi_host_is(sysfs
, "sas") ||
732 sysfs_scsi_has_attribute(sysfs
, "sas_device"))
736 /* SBP - Serial Bus Protocol (FireWire) */
737 else if (sysfs_scsi_has_attribute(sysfs
, "ieee1394_id"))
741 else if (sysfs_scsi_host_is(sysfs
, "iscsi"))
744 /* USB - Universal Serial Bus */
745 else if (sysfs_scsi_path_contains(sysfs
, "usb"))
749 else if (sysfs_scsi_host_is(sysfs
, "scsi")) {
750 attr
= sysfs_scsi_host_strdup_attribute(sysfs
, "scsi", "proc_name");
753 if (!strncmp(attr
, "ahci", 4) || !strncmp(attr
, "sata", 4))
755 else if (strstr(attr
, "ata"))
760 return trans
? xstrdup(trans
) : NULL
;
763 static char *get_subsystems(struct blkdev_cxt
*cxt
)
766 char *sub
, *chain
, *res
= NULL
;
767 size_t len
= 0, last
= 0;
769 chain
= sysfs_get_devchain(&cxt
->sysfs
, path
, sizeof(path
));
773 while (sysfs_next_subsystem(&cxt
->sysfs
, chain
, &sub
) == 0) {
776 /* don't create "block:scsi:scsi", but "block:scsi" */
777 if (len
&& strcmp(res
+ last
, sub
) == 0) {
783 res
= xrealloc(res
, len
+ sz
+ 2);
787 memcpy(res
+ len
, sub
, sz
+ 1);
797 #define is_parsable(_l) (scols_table_is_raw((_l)->table) || \
798 scols_table_is_export((_l)->table) || \
799 scols_table_is_json((_l)->table))
801 static char *mk_name(const char *name
)
807 xasprintf(&p
, "/dev/%s", name
);
811 sysfs_devname_sys_to_dev(p
);
815 static char *mk_dm_name(const char *name
)
821 xasprintf(&p
, "/dev/mapper/%s", name
);
827 /* stores data to scols cell userdata (invisible and independent on output)
828 * to make the original values accessible for sort functions
830 static void set_sortdata_u64(struct libscols_line
*ln
, int col
, uint64_t x
)
832 struct libscols_cell
*ce
= scols_line_get_cell(ln
, col
);
837 data
= xmalloc(sizeof(uint64_t));
839 scols_cell_set_userdata(ce
, data
);
842 static void set_sortdata_u64_from_string(struct libscols_line
*ln
, int col
, const char *str
)
846 if (!str
|| sscanf(str
, "%"SCNu64
, &x
) != 1)
849 set_sortdata_u64(ln
, col
, x
);
852 static void unref_sortdata(struct libscols_table
*tb
)
854 struct libscols_iter
*itr
;
855 struct libscols_line
*ln
;
857 if (!tb
|| !lsblk
->sort_col
)
859 itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
862 while (scols_table_next_line(tb
, itr
, &ln
) == 0) {
863 struct libscols_cell
*ce
= scols_line_get_column_cell(ln
,
865 void *data
= scols_cell_get_userdata(ce
);
869 scols_free_iter(itr
);
872 static void set_scols_data(struct blkdev_cxt
*cxt
, int col
, int id
, struct libscols_line
*ln
)
874 int sort
= 0, st_rc
= 0;
877 if (!cxt
->st
.st_rdev
&& (id
== COL_OWNER
|| id
== COL_GROUP
||
879 st_rc
= stat(cxt
->filename
, &cxt
->st
);
881 if (lsblk
->sort_id
== id
)
886 str
= cxt
->dm_name
? mk_dm_name(cxt
->dm_name
) : mk_name(cxt
->name
);
889 str
= mk_name(cxt
->name
);
893 str
= mk_name(cxt
->parent
->name
);
897 struct passwd
*pw
= st_rc
? NULL
: getpwuid(cxt
->st
.st_uid
);
899 str
= xstrdup(pw
->pw_name
);
904 struct group
*gr
= st_rc
? NULL
: getgrgid(cxt
->st
.st_gid
);
906 str
= xstrdup(gr
->gr_name
);
914 strmode(cxt
->st
.st_mode
, md
);
920 if (is_parsable(lsblk
))
921 xasprintf(&str
, "%u:%u", cxt
->maj
, cxt
->min
);
923 xasprintf(&str
, "%3u:%-3u", cxt
->maj
, cxt
->min
);
925 set_sortdata_u64(ln
, col
, makedev(cxt
->maj
, cxt
->min
));
930 str
= xstrdup(cxt
->fstype
);
933 str
= get_device_mountpoint(cxt
);
938 str
= xstrdup(cxt
->label
);
943 str
= xstrdup(cxt
->uuid
);
948 str
= xstrdup(cxt
->parttype
);
953 str
= xstrdup(cxt
->partlabel
);
958 str
= xstrdup(cxt
->partuuid
);
963 str
= xstrdup(cxt
->partflags
);
966 get_udev_properties(cxt
);
968 str
= xstrdup(cxt
->wwn
);
971 str
= sysfs_strdup(&cxt
->sysfs
, "queue/read_ahead_kb");
973 set_sortdata_u64_from_string(ln
, col
, str
);
976 str
= xstrdup(is_readonly_device(cxt
) ? "1" : "0");
979 str
= sysfs_strdup(&cxt
->sysfs
, "removable");
980 if (!str
&& cxt
->sysfs
.parent
)
981 str
= sysfs_strdup(cxt
->sysfs
.parent
, "removable");
984 str
= sysfs_is_hotpluggable(&cxt
->sysfs
) ? xstrdup("1") : xstrdup("0");
987 str
= sysfs_strdup(&cxt
->sysfs
, "queue/rotational");
990 str
= sysfs_strdup(&cxt
->sysfs
, "queue/add_random");
993 if (!cxt
->partition
&& cxt
->nslaves
== 0)
994 str
= sysfs_strdup(&cxt
->sysfs
, "device/model");
997 if (!cxt
->partition
&& cxt
->nslaves
== 0) {
998 get_udev_properties(cxt
);
1000 str
= xstrdup(cxt
->serial
);
1004 if (!cxt
->partition
&& cxt
->nslaves
== 0)
1005 str
= sysfs_strdup(&cxt
->sysfs
, "device/rev");
1008 if (!cxt
->partition
&& cxt
->nslaves
== 0)
1009 str
= sysfs_strdup(&cxt
->sysfs
, "device/vendor");
1015 xasprintf(&str
, "%ju", cxt
->size
);
1017 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, cxt
->size
);
1019 set_sortdata_u64(ln
, col
, cxt
->size
);
1022 if (!cxt
->partition
&& !cxt
->dm_name
)
1023 str
= sysfs_strdup(&cxt
->sysfs
, "device/state");
1024 else if (cxt
->dm_name
) {
1026 if (sysfs_read_int(&cxt
->sysfs
, "dm/suspended", &x
) == 0)
1027 str
= xstrdup(x
? "suspended" : "running");
1031 str
= sysfs_strdup(&cxt
->sysfs
, "alignment_offset");
1033 set_sortdata_u64_from_string(ln
, col
, str
);
1036 str
= sysfs_strdup(&cxt
->sysfs
, "queue/minimum_io_size");
1038 set_sortdata_u64_from_string(ln
, col
, str
);
1041 str
= sysfs_strdup(&cxt
->sysfs
, "queue/optimal_io_size");
1043 set_sortdata_u64_from_string(ln
, col
, str
);
1046 str
= sysfs_strdup(&cxt
->sysfs
, "queue/physical_block_size");
1048 set_sortdata_u64_from_string(ln
, col
, str
);
1051 str
= sysfs_strdup(&cxt
->sysfs
, "queue/logical_block_size");
1053 set_sortdata_u64_from_string(ln
, col
, str
);
1056 str
= get_scheduler(cxt
);
1059 str
= sysfs_strdup(&cxt
->sysfs
, "queue/nr_requests");
1061 set_sortdata_u64_from_string(ln
, col
, str
);
1064 str
= get_type(cxt
);
1069 if (sysfs_scsi_get_hctl(&cxt
->sysfs
, &h
, &c
, &t
, &l
) == 0)
1070 xasprintf(&str
, "%d:%d:%d:%d", h
, c
, t
, l
);
1074 str
= get_transport(cxt
);
1077 str
= get_subsystems(cxt
);
1081 str
= sysfs_strdup(&cxt
->sysfs
, "discard_alignment");
1085 set_sortdata_u64_from_string(ln
, col
, str
);
1089 str
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_granularity");
1091 set_sortdata_u64_from_string(ln
, col
, str
);
1094 if (sysfs_read_u64(&cxt
->sysfs
,
1095 "queue/discard_granularity", &x
) == 0) {
1096 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1098 set_sortdata_u64(ln
, col
, x
);
1104 str
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_max_bytes");
1106 set_sortdata_u64_from_string(ln
, col
, str
);
1109 if (sysfs_read_u64(&cxt
->sysfs
,
1110 "queue/discard_max_bytes", &x
) == 0) {
1111 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1113 set_sortdata_u64(ln
, col
, x
);
1119 str
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_zeroes_data");
1125 str
= sysfs_strdup(&cxt
->sysfs
, "queue/write_same_max_bytes");
1127 set_sortdata_u64_from_string(ln
, col
, str
);
1131 if (sysfs_read_u64(&cxt
->sysfs
,
1132 "queue/write_same_max_bytes", &x
) == 0) {
1133 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1135 set_sortdata_u64(ln
, col
, x
);
1144 scols_line_refer_data(ln
, col
, str
);
1147 static void fill_table_line(struct blkdev_cxt
*cxt
, struct libscols_line
*scols_parent
)
1151 cxt
->scols_line
= scols_table_new_line(lsblk
->table
, scols_parent
);
1152 if (!cxt
->scols_line
)
1155 for (i
= 0; i
< ncolumns
; i
++)
1156 set_scols_data(cxt
, i
, get_column_id(i
), cxt
->scols_line
);
1159 static int set_cxt(struct blkdev_cxt
*cxt
,
1160 struct blkdev_cxt
*parent
,
1161 struct blkdev_cxt
*wholedisk
,
1166 DBG(CXT
, ul_debugobj(cxt
, "setting context for %s [parent=%p, wholedisk=%p]",
1167 name
, parent
, wholedisk
));
1169 cxt
->parent
= parent
;
1170 cxt
->name
= xstrdup(name
);
1171 cxt
->partition
= wholedisk
!= NULL
;
1173 cxt
->filename
= get_device_path(cxt
);
1174 if (!cxt
->filename
) {
1175 DBG(CXT
, ul_debugobj(cxt
, "%s: failed to get device path", cxt
->name
));
1178 DBG(CXT
, ul_debugobj(cxt
, "%s: filename=%s", cxt
->name
, cxt
->filename
));
1180 devno
= sysfs_devname_to_devno(cxt
->name
, wholedisk
? wholedisk
->name
: NULL
);
1183 DBG(CXT
, ul_debugobj(cxt
, "%s: unknown device name", cxt
->name
));
1187 if (lsblk
->inverse
) {
1188 if (sysfs_init(&cxt
->sysfs
, devno
, wholedisk
? &wholedisk
->sysfs
: NULL
)) {
1189 DBG(CXT
, ul_debugobj(cxt
, "%s: failed to initialize sysfs handler", cxt
->name
));
1193 parent
->sysfs
.parent
= &cxt
->sysfs
;
1195 if (sysfs_init(&cxt
->sysfs
, devno
, parent
? &parent
->sysfs
: NULL
)) {
1196 DBG(CXT
, ul_debugobj(cxt
, "%s: failed to initialize sysfs handler", cxt
->name
));
1201 cxt
->maj
= major(devno
);
1202 cxt
->min
= minor(devno
);
1205 if (sysfs_read_u64(&cxt
->sysfs
, "size", &cxt
->size
) == 0) /* in sectors */
1206 cxt
->size
<<= 9; /* in bytes */
1208 if (sysfs_read_int(&cxt
->sysfs
,
1209 "queue/discard_granularity", &cxt
->discard
) != 0)
1212 /* Ignore devices of zero size */
1213 if (!lsblk
->all_devices
&& cxt
->size
== 0) {
1214 DBG(CXT
, ul_debugobj(cxt
, "zero size device -- ignore"));
1217 if (is_dm(cxt
->name
)) {
1218 cxt
->dm_name
= sysfs_strdup(&cxt
->sysfs
, "dm/name");
1219 if (!cxt
->dm_name
) {
1220 DBG(CXT
, ul_debugobj(cxt
, "%s: failed to get dm name", cxt
->name
));
1225 cxt
->npartitions
= sysfs_count_partitions(&cxt
->sysfs
, cxt
->name
);
1226 cxt
->nholders
= sysfs_count_dirents(&cxt
->sysfs
, "holders");
1227 cxt
->nslaves
= sysfs_count_dirents(&cxt
->sysfs
, "slaves");
1229 DBG(CXT
, ul_debugobj(cxt
, "%s: npartitions=%d, nholders=%d, nslaves=%d",
1230 cxt
->name
, cxt
->npartitions
, cxt
->nholders
, cxt
->nslaves
));
1232 /* ignore non-SCSI devices */
1233 if (lsblk
->scsi
&& sysfs_scsi_get_hctl(&cxt
->sysfs
, NULL
, NULL
, NULL
, NULL
)) {
1234 DBG(CXT
, ul_debugobj(cxt
, "non-scsi device -- ignore"));
1238 DBG(CXT
, ul_debugobj(cxt
, "%s: context successfully initialized", cxt
->name
));
1242 static int process_blkdev(struct blkdev_cxt
*cxt
, struct blkdev_cxt
*parent
,
1243 int do_partitions
, const char *part_name
);
1246 * List device partitions if any.
1248 static int list_partitions(struct blkdev_cxt
*wholedisk_cxt
, struct blkdev_cxt
*parent_cxt
,
1249 const char *part_name
)
1253 struct blkdev_cxt part_cxt
= { 0 };
1256 assert(wholedisk_cxt
);
1259 * Do not process further if there are no partitions for
1260 * this device or the device itself is a partition.
1262 if (!wholedisk_cxt
->npartitions
|| wholedisk_cxt
->partition
)
1265 DBG(CXT
, ul_debugobj(wholedisk_cxt
, "probe whole-disk for partitions"));
1267 dir
= sysfs_opendir(&wholedisk_cxt
->sysfs
, NULL
);
1269 err(EXIT_FAILURE
, _("failed to open device directory in sysfs"));
1271 while ((d
= xreaddir(dir
))) {
1272 /* Process particular partition only? */
1273 if (part_name
&& strcmp(part_name
, d
->d_name
))
1276 if (!(sysfs_is_partition_dirent(dir
, d
, wholedisk_cxt
->name
)))
1279 DBG(CXT
, ul_debugobj(wholedisk_cxt
, " checking %s", d
->d_name
));
1281 if (lsblk
->inverse
) {
1288 if (set_cxt(&part_cxt
, parent_cxt
, wholedisk_cxt
, d
->d_name
))
1291 if (!parent_cxt
&& part_cxt
.nholders
)
1294 wholedisk_cxt
->parent
= &part_cxt
;
1295 fill_table_line(&part_cxt
, parent_cxt
? parent_cxt
->scols_line
: NULL
);
1297 process_blkdev(wholedisk_cxt
, &part_cxt
, 0, NULL
);
1305 int ps
= set_cxt(&part_cxt
, wholedisk_cxt
, wholedisk_cxt
, d
->d_name
);
1307 /* Print whole disk only once */
1309 fill_table_line(wholedisk_cxt
, parent_cxt
? parent_cxt
->scols_line
: NULL
);
1310 if (ps
== 0 && !lsblk
->nodeps
)
1311 process_blkdev(&part_cxt
, wholedisk_cxt
, 0, NULL
);
1314 reset_blkdev_cxt(&part_cxt
);
1318 DBG(CXT
, ul_debugobj(wholedisk_cxt
, "probe whole-disk for partitions -- done"));
1323 static int get_wholedisk_from_partition_dirent(DIR *dir
, const char *dirname
,
1324 struct dirent
*d
, struct blkdev_cxt
*cxt
)
1326 char path
[PATH_MAX
];
1330 if ((len
= readlink_at(dirfd(dir
), dirname
,
1331 d
->d_name
, path
, sizeof(path
) - 1)) < 0)
1336 /* The path ends with ".../<device>/<partition>" */
1337 p
= strrchr(path
, '/');
1342 p
= strrchr(path
, '/');
1347 return set_cxt(cxt
, NULL
, NULL
, p
);
1351 * List device dependencies: partitions, holders (inverse = 0) or slaves (inverse = 1).
1353 static int list_deps(struct blkdev_cxt
*cxt
)
1357 struct blkdev_cxt dep
= { 0 };
1358 char dirname
[PATH_MAX
];
1359 const char *depname
;
1366 DBG(CXT
, ul_debugobj(cxt
, "%s: list dependencies", cxt
->name
));
1368 if (!(lsblk
->inverse
? cxt
->nslaves
: cxt
->nholders
))
1371 depname
= lsblk
->inverse
? "slaves" : "holders";
1372 dir
= sysfs_opendir(&cxt
->sysfs
, depname
);
1376 DBG(CXT
, ul_debugobj(cxt
, "%s: checking for '%s' dependence", cxt
->name
, depname
));
1378 snprintf(dirname
, sizeof(dirname
), "%s/%s", cxt
->sysfs
.dir_path
, depname
);
1380 while ((d
= xreaddir(dir
))) {
1381 /* Is the dependency a partition? */
1382 if (sysfs_is_partition_dirent(dir
, d
, NULL
)) {
1383 if (!get_wholedisk_from_partition_dirent(dir
, dirname
, d
, &dep
)) {
1384 DBG(CXT
, ul_debugobj(cxt
, "%s: %s: dependence is partition",
1385 cxt
->name
, d
->d_name
));
1386 process_blkdev(&dep
, cxt
, 1, d
->d_name
);
1389 /* The dependency is a whole device. */
1390 else if (!set_cxt(&dep
, cxt
, NULL
, d
->d_name
)) {
1391 DBG(CXT
, ul_debugobj(cxt
, "%s: %s: dependence is whole-disk",
1392 cxt
->name
, d
->d_name
));
1393 /* For inverse tree we don't want to show partitions
1394 * if the dependence is pn whle-disk */
1395 process_blkdev(&dep
, cxt
, lsblk
->inverse
? 0 : 1, NULL
);
1397 reset_blkdev_cxt(&dep
);
1401 DBG(CXT
, ul_debugobj(cxt
, "%s: checking for '%s' -- done", cxt
->name
, depname
));
1405 static int process_blkdev(struct blkdev_cxt
*cxt
, struct blkdev_cxt
*parent
,
1406 int do_partitions
, const char *part_name
)
1408 if (do_partitions
&& cxt
->npartitions
)
1409 list_partitions(cxt
, parent
, part_name
); /* partitoins + whole-disk */
1411 fill_table_line(cxt
, parent
? parent
->scols_line
: NULL
); /* whole-disk only */
1413 return list_deps(cxt
);
1416 /* Iterate devices in sysfs */
1417 static int iterate_block_devices(void)
1421 struct blkdev_cxt cxt
= { 0 };
1423 if (!(dir
= opendir(_PATH_SYS_BLOCK
)))
1426 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_BLOCK
));
1428 while ((d
= xreaddir(dir
))) {
1430 DBG(DEV
, ul_debug(" %s dentry", d
->d_name
));
1432 if (set_cxt(&cxt
, NULL
, NULL
, d
->d_name
))
1435 if (is_maj_excluded(cxt
.maj
) || !is_maj_included(cxt
.maj
))
1438 /* Skip devices in the middle of dependency tree. */
1439 if ((lsblk
->inverse
? cxt
.nholders
: cxt
.nslaves
) > 0)
1442 process_blkdev(&cxt
, NULL
, 1, NULL
);
1444 reset_blkdev_cxt(&cxt
);
1449 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_BLOCK
" -- done"));
1453 static char *devno_to_sysfs_name(dev_t devno
, char *devname
, char *buf
, size_t buf_size
)
1455 char path
[PATH_MAX
];
1458 if (!sysfs_devno_path(devno
, path
, sizeof(path
))) {
1459 warn(_("%s: failed to compose sysfs path"), devname
);
1463 len
= readlink(path
, buf
, buf_size
- 1);
1465 warn(_("%s: failed to read link"), path
);
1470 return xstrdup(strrchr(buf
, '/') + 1);
1473 static int process_one_device(char *devname
)
1475 struct blkdev_cxt parent
= { 0 }, cxt
= { 0 };
1477 char buf
[PATH_MAX
+ 1], *name
= NULL
, *diskname
= NULL
;
1479 int real_part
= 0, rc
= -EINVAL
;
1481 if (stat(devname
, &st
) || !S_ISBLK(st
.st_mode
)) {
1482 warnx(_("%s: not a block device"), devname
);
1486 if (!(name
= devno_to_sysfs_name(st
.st_rdev
, devname
, buf
, PATH_MAX
))) {
1487 warn(_("%s: failed to get sysfs name"), devname
);
1491 if (!strncmp(name
, "dm-", 3)) {
1492 /* dm mapping is never a real partition! */
1495 if (blkid_devno_to_wholedisk(st
.st_rdev
, buf
, sizeof(buf
), &disk
)) {
1496 warn(_("%s: failed to get whole-disk device number"), devname
);
1500 real_part
= st
.st_rdev
!= disk
;
1505 * Device is not a partition.
1507 if (set_cxt(&cxt
, NULL
, NULL
, name
))
1509 process_blkdev(&cxt
, NULL
, !lsblk
->inverse
, NULL
);
1512 * Partition, read sysfs name of the device.
1514 if (set_cxt(&parent
, NULL
, NULL
, diskname
))
1516 if (set_cxt(&cxt
, &parent
, &parent
, name
))
1520 process_blkdev(&parent
, &cxt
, 1, cxt
.name
);
1522 process_blkdev(&cxt
, &parent
, 1, NULL
);
1528 reset_blkdev_cxt(&cxt
);
1531 reset_blkdev_cxt(&parent
);
1536 static void parse_excludes(const char *str0
)
1538 const char *str
= str0
;
1540 while (str
&& *str
) {
1545 n
= strtoul(str
, &end
, 10);
1547 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1548 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1549 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1550 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1551 excludes
[nexcludes
++] = n
;
1553 if (nexcludes
== ARRAY_SIZE(excludes
))
1554 /* TRANSLATORS: The standard value for %d is 256. */
1555 errx(EXIT_FAILURE
, _("the list of excluded devices is "
1556 "too large (limit is %d devices)"),
1557 (int)ARRAY_SIZE(excludes
));
1559 str
= end
&& *end
? end
+ 1 : NULL
;
1563 static void parse_includes(const char *str0
)
1565 const char *str
= str0
;
1567 while (str
&& *str
) {
1572 n
= strtoul(str
, &end
, 10);
1574 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1575 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1576 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1577 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1578 includes
[nincludes
++] = n
;
1580 if (nincludes
== ARRAY_SIZE(includes
))
1581 /* TRANSLATORS: The standard value for %d is 256. */
1582 errx(EXIT_FAILURE
, _("the list of included devices is "
1583 "too large (limit is %d devices)"),
1584 (int)ARRAY_SIZE(includes
));
1585 str
= end
&& *end
? end
+ 1 : NULL
;
1590 * see set_sortdata_u64() and columns initialization in main()
1592 static int cmp_u64_cells(struct libscols_cell
*a
,
1593 struct libscols_cell
*b
,
1594 __attribute__((__unused__
)) void *data
)
1596 uint64_t *adata
= (uint64_t *) scols_cell_get_userdata(a
),
1597 *bdata
= (uint64_t *) scols_cell_get_userdata(b
);
1599 if (adata
== NULL
&& bdata
== NULL
)
1605 return *adata
== *bdata
? 0 : *adata
>= *bdata
? 1 : -1;
1608 static void __attribute__((__noreturn__
)) help(FILE *out
)
1612 fputs(USAGE_HEADER
, out
);
1613 fprintf(out
, _(" %s [options] [<device> ...]\n"), program_invocation_short_name
);
1615 fputs(USAGE_SEPARATOR
, out
);
1616 fputs(_("List information about block devices.\n"), out
);
1618 fputs(USAGE_OPTIONS
, out
);
1619 fputs(_(" -a, --all print all devices\n"), out
);
1620 fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out
);
1621 fputs(_(" -d, --nodeps don't print slaves or holders\n"), out
);
1622 fputs(_(" -D, --discard print discard capabilities\n"), out
);
1623 fputs(_(" -e, --exclude <list> exclude devices by major number (default: RAM disks)\n"), out
);
1624 fputs(_(" -f, --fs output info about filesystems\n"), out
);
1625 fputs(_(" -i, --ascii use ascii characters only\n"), out
);
1626 fputs(_(" -I, --include <list> show only devices with specified major numbers\n"), out
);
1627 fputs(_(" -J, --json use JSON output format\n"), out
);
1628 fputs(_(" -l, --list use list format output\n"), out
);
1629 fputs(_(" -m, --perms output info about permissions\n"), out
);
1630 fputs(_(" -n, --noheadings don't print headings\n"), out
);
1631 fputs(_(" -o, --output <list> output columns\n"), out
);
1632 fputs(_(" -O, --output-all output all columns\n"), out
);
1633 fputs(_(" -p, --paths print complete device path\n"), out
);
1634 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out
);
1635 fputs(_(" -r, --raw use raw output format\n"), out
);
1636 fputs(_(" -s, --inverse inverse dependencies\n"), out
);
1637 fputs(_(" -S, --scsi output info about SCSI devices\n"), out
);
1638 fputs(_(" -t, --topology output info about topology\n"), out
);
1639 fputs(_(" -x, --sort <column> sort output by <column>\n"), out
);
1640 fputs(USAGE_SEPARATOR
, out
);
1641 fputs(USAGE_HELP
, out
);
1642 fputs(USAGE_VERSION
, out
);
1644 fprintf(out
, _("\nAvailable columns (for --output):\n"));
1646 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
1647 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
1649 fprintf(out
, USAGE_MAN_TAIL("lsblk(8)"));
1651 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
1654 static void check_sysdevblock(void)
1656 if (access(_PATH_SYS_DEVBLOCK
, R_OK
) != 0)
1657 err(EXIT_FAILURE
, _("failed to access sysfs directory: %s"),
1658 _PATH_SYS_DEVBLOCK
);
1661 int main(int argc
, char *argv
[])
1663 struct lsblk _ls
= { .sort_id
= -1 };
1664 int scols_flags
= LSBLK_TREE
;
1665 int c
, status
= EXIT_FAILURE
;
1666 char *outarg
= NULL
;
1669 static const struct option longopts
[] = {
1670 { "all", 0, 0, 'a' },
1671 { "bytes", 0, 0, 'b' },
1672 { "nodeps", 0, 0, 'd' },
1673 { "discard", 0, 0, 'D' },
1674 { "help", 0, 0, 'h' },
1675 { "json", 0, 0, 'J' },
1676 { "output", 1, 0, 'o' },
1677 { "output-all", 0, 0, 'O' },
1678 { "perms", 0, 0, 'm' },
1679 { "noheadings", 0, 0, 'n' },
1680 { "list", 0, 0, 'l' },
1681 { "ascii", 0, 0, 'i' },
1682 { "raw", 0, 0, 'r' },
1683 { "inverse", 0, 0, 's' },
1684 { "fs", 0, 0, 'f' },
1685 { "exclude", 1, 0, 'e' },
1686 { "include", 1, 0, 'I' },
1687 { "topology", 0, 0, 't' },
1688 { "paths", 0, 0, 'p' },
1689 { "pairs", 0, 0, 'P' },
1690 { "scsi", 0, 0, 'S' },
1691 { "sort", 1, 0, 'x' },
1692 { "version", 0, 0, 'V' },
1696 static const ul_excl_t excl
[] = { /* rows and cols in in ASCII order */
1707 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1709 setlocale(LC_ALL
, "");
1710 bindtextdomain(PACKAGE
, LOCALEDIR
);
1711 textdomain(PACKAGE
);
1712 atexit(close_stdout
);
1718 while((c
= getopt_long(argc
, argv
,
1719 "abdDe:fhJlnmo:OpPiI:rstVSx:", longopts
, NULL
)) != -1) {
1721 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1725 lsblk
->all_devices
= 1;
1734 add_column(columns
, ncolumns
++, COL_NAME
);
1735 add_column(columns
, ncolumns
++, COL_DALIGN
);
1736 add_column(columns
, ncolumns
++, COL_DGRAN
);
1737 add_column(columns
, ncolumns
++, COL_DMAX
);
1738 add_column(columns
, ncolumns
++, COL_DZERO
);
1741 parse_excludes(optarg
);
1747 scols_flags
|= LSBLK_JSON
;
1750 scols_flags
&= ~LSBLK_TREE
; /* disable the default */
1753 scols_flags
|= LSBLK_NOHEADINGS
;
1759 for (ncolumns
= 0 ; ncolumns
< ARRAY_SIZE(infos
); ncolumns
++)
1760 columns
[ncolumns
] = ncolumns
;
1766 scols_flags
|= LSBLK_EXPORT
;
1767 scols_flags
&= ~LSBLK_TREE
; /* disable the default */
1770 scols_flags
|= LSBLK_ASCII
;
1773 parse_includes(optarg
);
1776 scols_flags
&= ~LSBLK_TREE
; /* disable the default */
1777 scols_flags
|= LSBLK_RAW
; /* enable raw */
1783 add_column(columns
, ncolumns
++, COL_NAME
);
1784 add_column(columns
, ncolumns
++, COL_FSTYPE
);
1785 add_column(columns
, ncolumns
++, COL_LABEL
);
1786 add_column(columns
, ncolumns
++, COL_UUID
);
1787 add_column(columns
, ncolumns
++, COL_TARGET
);
1790 add_column(columns
, ncolumns
++, COL_NAME
);
1791 add_column(columns
, ncolumns
++, COL_SIZE
);
1792 add_column(columns
, ncolumns
++, COL_OWNER
);
1793 add_column(columns
, ncolumns
++, COL_GROUP
);
1794 add_column(columns
, ncolumns
++, COL_MODE
);
1797 add_column(columns
, ncolumns
++, COL_NAME
);
1798 add_column(columns
, ncolumns
++, COL_ALIOFF
);
1799 add_column(columns
, ncolumns
++, COL_MINIO
);
1800 add_column(columns
, ncolumns
++, COL_OPTIO
);
1801 add_column(columns
, ncolumns
++, COL_PHYSEC
);
1802 add_column(columns
, ncolumns
++, COL_LOGSEC
);
1803 add_column(columns
, ncolumns
++, COL_ROTA
);
1804 add_column(columns
, ncolumns
++, COL_SCHED
);
1805 add_column(columns
, ncolumns
++, COL_RQ_SIZE
);
1806 add_column(columns
, ncolumns
++, COL_RA
);
1807 add_column(columns
, ncolumns
++, COL_WSAME
);
1812 add_column(columns
, ncolumns
++, COL_NAME
);
1813 add_column(columns
, ncolumns
++, COL_HCTL
);
1814 add_column(columns
, ncolumns
++, COL_TYPE
);
1815 add_column(columns
, ncolumns
++, COL_VENDOR
);
1816 add_column(columns
, ncolumns
++, COL_MODEL
);
1817 add_column(columns
, ncolumns
++, COL_REV
);
1818 add_column(columns
, ncolumns
++, COL_TRANSPORT
);
1821 printf(UTIL_LINUX_VERSION
);
1822 return EXIT_SUCCESS
;
1824 scols_flags
&= ~LSBLK_TREE
; /* disable the default */
1825 lsblk
->sort_id
= column_name_to_id(optarg
, strlen(optarg
));
1826 if (lsblk
->sort_id
>= 0)
1834 check_sysdevblock();
1837 add_column(columns
, ncolumns
++, COL_NAME
);
1838 add_column(columns
, ncolumns
++, COL_MAJMIN
);
1839 add_column(columns
, ncolumns
++, COL_RM
);
1840 add_column(columns
, ncolumns
++, COL_SIZE
);
1841 add_column(columns
, ncolumns
++, COL_RO
);
1842 add_column(columns
, ncolumns
++, COL_TYPE
);
1843 add_column(columns
, ncolumns
++, COL_TARGET
);
1846 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
1847 &ncolumns
, column_name_to_id
) < 0)
1848 return EXIT_FAILURE
;
1850 if (nexcludes
== 0 && nincludes
== 0)
1851 excludes
[nexcludes
++] = 1; /* default: ignore RAM disks */
1853 if (lsblk
->sort_id
>= 0 && column_id_to_number(lsblk
->sort_id
) < 0) {
1854 /* the sort column is not between output columns -- add as hidden */
1855 add_column(columns
, ncolumns
++, lsblk
->sort_id
);
1856 lsblk
->sort_hidden
= 1;
1860 scols_init_debug(0);
1863 * initialize output columns
1865 if (!(lsblk
->table
= scols_new_table()))
1866 errx(EXIT_FAILURE
, _("failed to initialize output table"));
1867 scols_table_enable_raw(lsblk
->table
, !!(scols_flags
& LSBLK_RAW
));
1868 scols_table_enable_export(lsblk
->table
, !!(scols_flags
& LSBLK_EXPORT
));
1869 scols_table_enable_ascii(lsblk
->table
, !!(scols_flags
& LSBLK_ASCII
));
1870 scols_table_enable_json(lsblk
->table
, !!(scols_flags
& LSBLK_JSON
));
1871 scols_table_enable_noheadings(lsblk
->table
, !!(scols_flags
& LSBLK_NOHEADINGS
));
1873 if (scols_flags
& LSBLK_JSON
)
1874 scols_table_set_name(lsblk
->table
, "blockdevices");
1876 for (i
= 0; i
< ncolumns
; i
++) {
1877 struct colinfo
*ci
= get_column_info(i
);
1878 struct libscols_column
*cl
;
1879 int id
= get_column_id(i
), fl
= ci
->flags
;
1881 if (!(scols_flags
& LSBLK_TREE
) && id
== COL_NAME
)
1882 fl
&= ~SCOLS_FL_TREE
;
1883 if (lsblk
->sort_hidden
&& lsblk
->sort_id
== id
)
1884 fl
|= SCOLS_FL_HIDDEN
;
1886 cl
= scols_table_new_column(lsblk
->table
, ci
->name
, ci
->whint
, fl
);
1888 warn(_("failed to initialize output column"));
1891 if (!lsblk
->sort_col
&& lsblk
->sort_id
== id
) {
1892 lsblk
->sort_col
= cl
;
1893 scols_column_set_cmpfunc(cl
,
1894 ci
->sort_type
== SORT_STRING
?
1895 scols_cmpstr_cells
: cmp_u64_cells
, NULL
);
1900 status
= iterate_block_devices() == 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
1902 int cnt
= 0, cnt_err
= 0;
1904 while (optind
< argc
) {
1905 if (process_one_device(argv
[optind
++]) != 0)
1909 status
= cnt
== 0 ? EXIT_FAILURE
: /* nothing */
1910 cnt
== cnt_err
? LSBLK_EXIT_ALLFAILED
:/* all failed */
1911 cnt_err
? LSBLK_EXIT_SOMEOK
: /* some ok */
1912 EXIT_SUCCESS
; /* all success */
1915 if (lsblk
->sort_col
)
1916 scols_sort_table(lsblk
->table
, lsblk
->sort_col
);
1918 scols_print_table(lsblk
->table
);
1921 if (lsblk
->sort_col
)
1922 unref_sortdata(lsblk
->table
);
1924 scols_unref_table(lsblk
->table
);
1926 mnt_unref_table(mtab
);
1927 mnt_unref_table(swaps
);
1928 mnt_unref_cache(mntcache
);