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)
78 #define UL_DEBUG_CURRENT_MASK UL_DEBUG_MASK(lsblk)
81 #define LSBLK_EXIT_SOMEOK 64
82 #define LSBLK_EXIT_ALLFAILED 32
84 static int column_id_to_number(int id
);
135 /* basic table settings */
137 LSBLK_ASCII
= (1 << 0),
138 LSBLK_RAW
= (1 << 1),
139 LSBLK_NOHEADINGS
= (1 << 2),
140 LSBLK_EXPORT
= (1 << 3),
141 LSBLK_TREE
= (1 << 4),
142 LSBLK_JSON
= (1 << 5),
146 SORT_STRING
= 0, /* default is to use scols_cell_get_data() */
147 SORT_U64
= 1 /* use private pointer from scols_cell_get_userdata() */
152 const char *name
; /* header */
153 double whint
; /* width hint (N < 1 is in percent of termwidth) */
154 int flags
; /* SCOLS_FL_* */
157 int sort_type
; /* SORT_* */
160 /* columns descriptions */
161 static struct colinfo infos
[] = {
162 [COL_NAME
] = { "NAME", 0.25, SCOLS_FL_TREE
| SCOLS_FL_NOEXTREMES
, N_("device name") },
163 [COL_KNAME
] = { "KNAME", 0.3, 0, N_("internal kernel device name") },
164 [COL_PKNAME
] = { "PKNAME", 0.3, 0, N_("internal parent kernel device name") },
165 [COL_MAJMIN
] = { "MAJ:MIN", 6, 0, N_("major:minor device number"), SORT_U64
},
166 [COL_FSTYPE
] = { "FSTYPE", 0.1, SCOLS_FL_TRUNC
, N_("filesystem type") },
167 [COL_TARGET
] = { "MOUNTPOINT", 0.10, SCOLS_FL_TRUNC
, N_("where the device is mounted") },
168 [COL_LABEL
] = { "LABEL", 0.1, 0, N_("filesystem LABEL") },
169 [COL_UUID
] = { "UUID", 36, 0, N_("filesystem UUID") },
171 [COL_PARTTYPE
] = { "PARTTYPE", 36, 0, N_("partition type UUID") },
172 [COL_PARTLABEL
] = { "PARTLABEL", 0.1, 0, N_("partition LABEL") },
173 [COL_PARTUUID
] = { "PARTUUID", 36, 0, N_("partition UUID") },
174 [COL_PARTFLAGS
] = { "PARTFLAGS", 36, 0, N_("partition flags") },
176 [COL_RA
] = { "RA", 3, SCOLS_FL_RIGHT
, N_("read-ahead of the device"), SORT_U64
},
177 [COL_RO
] = { "RO", 1, SCOLS_FL_RIGHT
, N_("read-only device") },
178 [COL_RM
] = { "RM", 1, SCOLS_FL_RIGHT
, N_("removable device") },
179 [COL_HOTPLUG
]= { "HOTPLUG", 1, SCOLS_FL_RIGHT
, N_("removable or hotplug device (usb, pcmcia, ...)") },
180 [COL_ROTA
] = { "ROTA", 1, SCOLS_FL_RIGHT
, N_("rotational device") },
181 [COL_RAND
] = { "RAND", 1, SCOLS_FL_RIGHT
, N_("adds randomness") },
182 [COL_MODEL
] = { "MODEL", 0.1, SCOLS_FL_TRUNC
, N_("device identifier") },
183 [COL_SERIAL
] = { "SERIAL", 0.1, SCOLS_FL_TRUNC
, N_("disk serial number") },
184 [COL_SIZE
] = { "SIZE", 5, SCOLS_FL_RIGHT
, N_("size of the device"), SORT_U64
},
185 [COL_STATE
] = { "STATE", 7, SCOLS_FL_TRUNC
, N_("state of the device") },
186 [COL_OWNER
] = { "OWNER", 0.1, SCOLS_FL_TRUNC
, N_("user name"), },
187 [COL_GROUP
] = { "GROUP", 0.1, SCOLS_FL_TRUNC
, N_("group name") },
188 [COL_MODE
] = { "MODE", 10, 0, N_("device node permissions") },
189 [COL_ALIOFF
] = { "ALIGNMENT", 6, SCOLS_FL_RIGHT
, N_("alignment offset"), SORT_U64
},
190 [COL_MINIO
] = { "MIN-IO", 6, SCOLS_FL_RIGHT
, N_("minimum I/O size"), SORT_U64
},
191 [COL_OPTIO
] = { "OPT-IO", 6, SCOLS_FL_RIGHT
, N_("optimal I/O size"), SORT_U64
},
192 [COL_PHYSEC
] = { "PHY-SEC", 7, SCOLS_FL_RIGHT
, N_("physical sector size"), SORT_U64
},
193 [COL_LOGSEC
] = { "LOG-SEC", 7, SCOLS_FL_RIGHT
, N_("logical sector size"), SORT_U64
},
194 [COL_SCHED
] = { "SCHED", 0.1, 0, N_("I/O scheduler name") },
195 [COL_RQ_SIZE
]= { "RQ-SIZE", 5, SCOLS_FL_RIGHT
, N_("request queue size"), SORT_U64
},
196 [COL_TYPE
] = { "TYPE", 4, 0, N_("device type") },
197 [COL_DALIGN
] = { "DISC-ALN", 6, SCOLS_FL_RIGHT
, N_("discard alignment offset"), SORT_U64
},
198 [COL_DGRAN
] = { "DISC-GRAN", 6, SCOLS_FL_RIGHT
, N_("discard granularity"), SORT_U64
},
199 [COL_DMAX
] = { "DISC-MAX", 6, SCOLS_FL_RIGHT
, N_("discard max bytes"), SORT_U64
},
200 [COL_DZERO
] = { "DISC-ZERO", 1, SCOLS_FL_RIGHT
, N_("discard zeroes data") },
201 [COL_WSAME
] = { "WSAME", 6, SCOLS_FL_RIGHT
, N_("write same max bytes"), SORT_U64
},
202 [COL_WWN
] = { "WWN", 18, 0, N_("unique storage identifier") },
203 [COL_HCTL
] = { "HCTL", 10, 0, N_("Host:Channel:Target:Lun for SCSI") },
204 [COL_TRANSPORT
] = { "TRAN", 6, 0, N_("device transport type") },
205 [COL_SUBSYS
] = { "SUBSYSTEMS", 0.1, SCOLS_FL_NOEXTREMES
, N_("de-duplicated chain of subsystems") },
206 [COL_REV
] = { "REV", 4, SCOLS_FL_RIGHT
, N_("device revision") },
207 [COL_VENDOR
] = { "VENDOR", 0.1, SCOLS_FL_TRUNC
, N_("device vendor") },
208 [COL_ZONED
] = { "ZONED", 0.3, 0, N_("zone model") },
212 struct libscols_table
*table
; /* output table */
213 struct libscols_column
*sort_col
;/* sort output by this column */
216 int flags
; /* LSBLK_* */
218 unsigned int all_devices
:1; /* print all devices, including empty */
219 unsigned int bytes
:1; /* print SIZE in bytes */
220 unsigned int inverse
:1; /* print inverse dependencies */
221 unsigned int nodeps
:1; /* don't print slaves/holders */
222 unsigned int scsi
:1; /* print only device with HCTL (SCSI) */
223 unsigned int paths
:1; /* print devnames with "/dev" prefix */
224 unsigned int sort_hidden
:1; /* sort column not between output columns */
225 unsigned int force_tree_order
:1;/* sort lines by parent->tree relation */
228 static struct lsblk
*lsblk
; /* global handler */
230 /* columns[] array specifies all currently wanted output column. The columns
231 * are defined by infos[] array and you can specify (on command line) each
232 * column twice. That's enough, dynamically allocated array of the columns is
233 * unnecessary overkill and over-engineering in this case */
234 static int columns
[ARRAY_SIZE(infos
) * 2];
235 static size_t ncolumns
;
238 static inline void add_column(int id
)
240 if (ncolumns
>= ARRAY_SIZE(columns
))
241 errx(EXIT_FAILURE
, _("too many columns specified, "
242 "the limit is %zu columns"),
243 ARRAY_SIZE(columns
) - 1);
244 columns
[ ncolumns
++ ] = id
;
247 static inline void add_uniq_column(int id
)
249 if (column_id_to_number(id
) < 0)
253 static int excludes
[256];
254 static size_t nexcludes
;
256 static int includes
[256];
257 static size_t nincludes
;
259 static struct libmnt_table
*mtab
, *swaps
;
260 static struct libmnt_cache
*mntcache
;
263 static struct udev
*udev
;
267 struct blkdev_cxt
*parent
;
269 struct libscols_line
*scols_line
;
272 char *name
; /* kernel name in /sys/block */
273 char *dm_name
; /* DM name (dm/block) */
275 char *filename
; /* path to device node */
277 struct sysfs_cxt sysfs
;
279 int partition
; /* is partition? TRUE/FALSE */
281 int probed
; /* already probed */
282 char *fstype
; /* detected fs, NULL or "?" if cannot detect */
283 char *uuid
; /* filesystem UUID (or stack uuid) */
284 char *label
; /* filesystem label */
285 char *parttype
; /* partition type UUID */
286 char *partuuid
; /* partition UUID */
287 char *partlabel
; /* partition label */
288 char *partflags
; /* partition flags */
289 char *wwn
; /* storage WWN */
290 char *serial
; /* disk serial number */
292 int npartitions
; /* # of partitions this device has */
293 int nholders
; /* # of devices mapped directly to this device
294 * /sys/block/.../holders */
295 int nslaves
; /* # of devices this device maps to */
296 int maj
, min
; /* devno */
297 int discard
; /* supports discard */
299 uint64_t size
; /* device size */
302 static void lsblk_init_debug(void)
304 __UL_INIT_DEBUG_FROM_ENV(lsblk
, LSBLK_DEBUG_
, 0, LSBLK_DEBUG
);
307 static int is_maj_excluded(int maj
)
311 assert(ARRAY_SIZE(excludes
) > nexcludes
);
314 return 0; /* filter not enabled, device not excluded */
316 for (i
= 0; i
< nexcludes
; i
++) {
317 if (excludes
[i
] == maj
) {
318 DBG(FILTER
, ul_debug("exclude: maj=%d", maj
));
325 static int is_maj_included(int maj
)
329 assert(ARRAY_SIZE(includes
) > nincludes
);
332 return 1; /* filter not enabled, device is included */
334 for (i
= 0; i
< nincludes
; i
++) {
335 if (includes
[i
] == maj
) {
336 DBG(FILTER
, ul_debug("include: maj=%d", maj
));
343 /* array with IDs of enabled columns */
344 static int get_column_id(int num
)
347 assert((size_t) num
< ncolumns
);
348 assert(columns
[num
] < (int) ARRAY_SIZE(infos
));
352 static struct colinfo
*get_column_info(int num
)
354 return &infos
[ get_column_id(num
) ];
357 static int column_name_to_id(const char *name
, size_t namesz
)
361 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
362 const char *cn
= infos
[i
].name
;
364 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
367 warnx(_("unknown column: %s"), name
);
371 static int column_id_to_number(int id
)
375 for (i
= 0; i
< ncolumns
; i
++)
376 if (columns
[i
] == id
)
381 static void reset_blkdev_cxt(struct blkdev_cxt
*cxt
)
386 DBG(CXT
, ul_debugobj(cxt
, "reset"));
396 free(cxt
->partlabel
);
400 sysfs_deinit(&cxt
->sysfs
);
402 memset(cxt
, 0, sizeof(*cxt
));
405 static int is_dm(const char *name
)
407 return strncmp(name
, "dm-", 3) ? 0 : 1;
410 static struct dirent
*xreaddir(DIR *dp
)
416 while ((d
= readdir(dp
))) {
417 if (!strcmp(d
->d_name
, ".") ||
418 !strcmp(d
->d_name
, ".."))
421 /* blacklist here? */
427 static char *get_device_path(struct blkdev_cxt
*cxt
)
434 if (is_dm(cxt
->name
))
435 return canonicalize_dm_name(cxt
->name
);
437 snprintf(path
, sizeof(path
), "/dev/%s", cxt
->name
);
438 sysfs_devname_sys_to_dev(path
);
439 return xstrdup(path
);
442 static int table_parser_errcb(struct libmnt_table
*tb
__attribute__((__unused__
)),
443 const char *filename
, int line
)
446 warnx(_("%s: parse error at line %d -- ignored"), filename
, line
);
450 static int is_active_swap(const char *filename
)
453 swaps
= mnt_new_table();
457 mntcache
= mnt_new_cache();
459 mnt_table_set_parser_errcb(swaps
, table_parser_errcb
);
460 mnt_table_set_cache(swaps
, mntcache
);
461 mnt_table_parse_swaps(swaps
, NULL
);
464 return mnt_table_find_srcpath(swaps
, filename
, MNT_ITER_BACKWARD
) != NULL
;
467 static char *get_device_mountpoint(struct blkdev_cxt
*cxt
)
469 struct libmnt_fs
*fs
;
473 assert(cxt
->filename
);
476 mtab
= mnt_new_table();
480 mntcache
= mnt_new_cache();
482 mnt_table_set_parser_errcb(mtab
, table_parser_errcb
);
483 mnt_table_set_cache(mtab
, mntcache
);
484 mnt_table_parse_mtab(mtab
, NULL
);
487 /* Note that maj:min in /proc/self/mountinfo does not have to match with
488 * devno as returned by stat(), so we have to try devname too
490 fs
= mnt_table_find_devno(mtab
, makedev(cxt
->maj
, cxt
->min
), MNT_ITER_BACKWARD
);
492 fs
= mnt_table_find_srcpath(mtab
, cxt
->filename
, MNT_ITER_BACKWARD
);
494 return is_active_swap(cxt
->filename
) ? xstrdup("[SWAP]") : NULL
;
497 fsroot
= mnt_fs_get_root(fs
);
498 if (fsroot
&& strcmp(fsroot
, "/") != 0) {
499 /* hmm.. we found bind mount or btrfs subvolume, let's try to
500 * get real FS root mountpoint */
501 struct libmnt_fs
*rfs
;
502 struct libmnt_iter
*itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
504 mnt_table_set_iter(mtab
, itr
, fs
);
505 while (mnt_table_next_fs(mtab
, itr
, &rfs
) == 0) {
506 fsroot
= mnt_fs_get_root(rfs
);
507 if ((!fsroot
|| strcmp(fsroot
, "/") == 0)
508 && mnt_fs_match_source(rfs
, cxt
->filename
, mntcache
)) {
516 DBG(DEV
, ul_debugobj(cxt
, "mountpoint: %s", mnt_fs_get_target(fs
)));
517 return xstrdup(mnt_fs_get_target(fs
));
521 static int get_udev_properties(struct blkdev_cxt
*cxt
522 __attribute__((__unused__
)))
527 static int get_udev_properties(struct blkdev_cxt
*cxt
)
529 struct udev_device
*dev
;
532 return 0; /* already done */
539 dev
= udev_device_new_from_subsystem_sysname(udev
, "block", cxt
->name
);
543 if ((data
= udev_device_get_property_value(dev
, "ID_FS_LABEL_ENC"))) {
544 cxt
->label
= xstrdup(data
);
545 unhexmangle_string(cxt
->label
);
547 if ((data
= udev_device_get_property_value(dev
, "ID_FS_UUID_ENC"))) {
548 cxt
->uuid
= xstrdup(data
);
549 unhexmangle_string(cxt
->uuid
);
551 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_NAME"))) {
552 cxt
->partlabel
= xstrdup(data
);
553 unhexmangle_string(cxt
->partlabel
);
555 if ((data
= udev_device_get_property_value(dev
, "ID_FS_TYPE")))
556 cxt
->fstype
= xstrdup(data
);
557 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_TYPE")))
558 cxt
->parttype
= xstrdup(data
);
559 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_UUID")))
560 cxt
->partuuid
= xstrdup(data
);
561 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_FLAGS")))
562 cxt
->partflags
= xstrdup(data
);
564 data
= udev_device_get_property_value(dev
, "ID_WWN_WITH_EXTENSION");
566 data
= udev_device_get_property_value(dev
, "ID_WWN");
568 cxt
->wwn
= xstrdup(data
);
570 if ((data
= udev_device_get_property_value(dev
, "ID_SERIAL_SHORT")))
571 cxt
->serial
= xstrdup(data
);
572 udev_device_unref(dev
);
574 DBG(DEV
, ul_debugobj(cxt
, "%s: found udev properties", cxt
->name
));
577 return cxt
->probed
== 1 ? 0 : -1;
580 #endif /* HAVE_LIBUDEV */
582 static void probe_device(struct blkdev_cxt
*cxt
)
584 blkid_probe pr
= NULL
;
593 if (get_udev_properties(cxt
) == 0)
594 return; /* success */
598 /* try libblkid (fallback) */
600 return; /* no permissions to read from the device */
602 pr
= blkid_new_probe_from_filename(cxt
->filename
);
606 blkid_probe_enable_superblocks(pr
, 1);
607 blkid_probe_set_superblocks_flags(pr
, BLKID_SUBLKS_LABEL
|
610 blkid_probe_enable_partitions(pr
, 1);
611 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
613 if (!blkid_do_safeprobe(pr
)) {
614 const char *data
= NULL
;
616 if (!blkid_probe_lookup_value(pr
, "TYPE", &data
, NULL
))
617 cxt
->fstype
= xstrdup(data
);
618 if (!blkid_probe_lookup_value(pr
, "UUID", &data
, NULL
))
619 cxt
->uuid
= xstrdup(data
);
620 if (!blkid_probe_lookup_value(pr
, "LABEL", &data
, NULL
))
621 cxt
->label
= xstrdup(data
);
622 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_TYPE", &data
, NULL
))
623 cxt
->parttype
= xstrdup(data
);
624 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_UUID", &data
, NULL
))
625 cxt
->partuuid
= xstrdup(data
);
626 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_NAME", &data
, NULL
))
627 cxt
->partlabel
= xstrdup(data
);
628 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_FLAGS", &data
, NULL
))
629 cxt
->partflags
= xstrdup(data
);
630 DBG(DEV
, ul_debugobj(cxt
, "%s: found blkid properties", cxt
->name
));
633 blkid_free_probe(pr
);
637 static int is_readonly_device(struct blkdev_cxt
*cxt
)
641 if (sysfs_scanf(&cxt
->sysfs
, "ro", "%d", &ro
) == 1)
644 /* fallback if "ro" attribute does not exist */
645 fd
= open(cxt
->filename
, O_RDONLY
);
647 if (ioctl(fd
, BLKROGET
, &ro
) != 0)
654 static char *get_scheduler(struct blkdev_cxt
*cxt
)
656 char *str
= sysfs_strdup(&cxt
->sysfs
, "queue/scheduler");
657 char *p
, *res
= NULL
;
661 p
= strchr(str
, '[');
664 p
= strchr(res
, ']');
675 static char *get_type(struct blkdev_cxt
*cxt
)
677 char *res
= NULL
, *p
;
679 if (is_dm(cxt
->name
)) {
680 char *dm_uuid
= sysfs_strdup(&cxt
->sysfs
, "dm/uuid");
682 /* The DM_UUID prefix should be set to subsystem owning
683 * the device - LVM, CRYPT, DMRAID, MPATH, PART */
686 char *dm_uuid_prefix
= strsep(&tmp
, "-");
688 if (dm_uuid_prefix
) {
689 /* kpartx hack to remove partition number */
690 if (strncasecmp(dm_uuid_prefix
, "part", 4) == 0)
691 dm_uuid_prefix
[4] = '\0';
693 res
= xstrdup(dm_uuid_prefix
);
699 /* No UUID or no prefix - just mark it as DM device */
702 } else if (!strncmp(cxt
->name
, "loop", 4)) {
703 res
= xstrdup("loop");
705 } else if (!strncmp(cxt
->name
, "md", 2)) {
706 char *md_level
= sysfs_strdup(&cxt
->sysfs
, "md/level");
707 res
= md_level
? md_level
: xstrdup("md");
710 const char *type
= NULL
;
713 if (!sysfs_read_int(&cxt
->sysfs
, "device/type", &x
))
714 type
= blkdev_scsi_type_to_name(x
);
717 type
= cxt
->partition
? "part" : "disk";
721 for (p
= res
; p
&& *p
; p
++)
722 *p
= tolower((unsigned char) *p
);
726 /* Thanks to lsscsi code for idea of detection logic used here */
727 static char *get_transport(struct blkdev_cxt
*cxt
)
729 struct sysfs_cxt
*sysfs
= &cxt
->sysfs
;
731 const char *trans
= NULL
;
733 /* SCSI - Serial Peripheral Interface */
734 if (sysfs_scsi_host_is(sysfs
, "spi"))
737 /* FC/FCoE - Fibre Channel / Fibre Channel over Ethernet */
738 else if (sysfs_scsi_host_is(sysfs
, "fc")) {
739 attr
= sysfs_scsi_host_strdup_attribute(sysfs
, "fc", "symbolic_name");
742 trans
= strstr(attr
, " over ") ? "fcoe" : "fc";
746 /* SAS - Serial Attached SCSI */
747 else if (sysfs_scsi_host_is(sysfs
, "sas") ||
748 sysfs_scsi_has_attribute(sysfs
, "sas_device"))
752 /* SBP - Serial Bus Protocol (FireWire) */
753 else if (sysfs_scsi_has_attribute(sysfs
, "ieee1394_id"))
757 else if (sysfs_scsi_host_is(sysfs
, "iscsi"))
760 /* USB - Universal Serial Bus */
761 else if (sysfs_scsi_path_contains(sysfs
, "usb"))
765 else if (sysfs_scsi_host_is(sysfs
, "scsi")) {
766 attr
= sysfs_scsi_host_strdup_attribute(sysfs
, "scsi", "proc_name");
769 if (!strncmp(attr
, "ahci", 4) || !strncmp(attr
, "sata", 4))
771 else if (strstr(attr
, "ata"))
775 } else if (strncmp(cxt
->name
, "nvme", 4) == 0)
778 return trans
? xstrdup(trans
) : NULL
;
781 static char *get_subsystems(struct blkdev_cxt
*cxt
)
784 char *sub
, *chain
, *res
= NULL
;
785 size_t len
= 0, last
= 0;
787 chain
= sysfs_get_devchain(&cxt
->sysfs
, path
, sizeof(path
));
791 while (sysfs_next_subsystem(&cxt
->sysfs
, chain
, &sub
) == 0) {
794 /* don't create "block:scsi:scsi", but "block:scsi" */
795 if (len
&& strcmp(res
+ last
, sub
) == 0) {
801 res
= xrealloc(res
, len
+ sz
+ 2);
805 memcpy(res
+ len
, sub
, sz
+ 1);
815 #define is_parsable(_l) (scols_table_is_raw((_l)->table) || \
816 scols_table_is_export((_l)->table) || \
817 scols_table_is_json((_l)->table))
819 static char *mk_name(const char *name
)
825 xasprintf(&p
, "/dev/%s", name
);
829 sysfs_devname_sys_to_dev(p
);
833 static char *mk_dm_name(const char *name
)
839 xasprintf(&p
, "/dev/mapper/%s", name
);
845 /* stores data to scols cell userdata (invisible and independent on output)
846 * to make the original values accessible for sort functions
848 static void set_sortdata_u64(struct libscols_line
*ln
, int col
, uint64_t x
)
850 struct libscols_cell
*ce
= scols_line_get_cell(ln
, col
);
855 data
= xmalloc(sizeof(uint64_t));
857 scols_cell_set_userdata(ce
, data
);
860 static void set_sortdata_u64_from_string(struct libscols_line
*ln
, int col
, const char *str
)
864 if (!str
|| sscanf(str
, "%"SCNu64
, &x
) != 1)
867 set_sortdata_u64(ln
, col
, x
);
870 static void unref_sortdata(struct libscols_table
*tb
)
872 struct libscols_iter
*itr
;
873 struct libscols_line
*ln
;
875 if (!tb
|| !lsblk
->sort_col
)
877 itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
880 while (scols_table_next_line(tb
, itr
, &ln
) == 0) {
881 struct libscols_cell
*ce
= scols_line_get_column_cell(ln
,
883 void *data
= scols_cell_get_userdata(ce
);
887 scols_free_iter(itr
);
890 static void set_scols_data(struct blkdev_cxt
*cxt
, int col
, int id
, struct libscols_line
*ln
)
892 int sort
= 0, st_rc
= 0;
895 if (!cxt
->st
.st_rdev
&& (id
== COL_OWNER
|| id
== COL_GROUP
||
897 st_rc
= stat(cxt
->filename
, &cxt
->st
);
899 if (lsblk
->sort_id
== id
)
904 str
= cxt
->dm_name
? mk_dm_name(cxt
->dm_name
) : mk_name(cxt
->name
);
907 str
= mk_name(cxt
->name
);
911 str
= mk_name(cxt
->parent
->name
);
915 struct passwd
*pw
= st_rc
? NULL
: getpwuid(cxt
->st
.st_uid
);
917 str
= xstrdup(pw
->pw_name
);
922 struct group
*gr
= st_rc
? NULL
: getgrgid(cxt
->st
.st_gid
);
924 str
= xstrdup(gr
->gr_name
);
932 xstrmode(cxt
->st
.st_mode
, md
);
938 if (is_parsable(lsblk
))
939 xasprintf(&str
, "%u:%u", cxt
->maj
, cxt
->min
);
941 xasprintf(&str
, "%3u:%-3u", cxt
->maj
, cxt
->min
);
943 set_sortdata_u64(ln
, col
, makedev(cxt
->maj
, cxt
->min
));
948 str
= xstrdup(cxt
->fstype
);
951 str
= get_device_mountpoint(cxt
);
956 str
= xstrdup(cxt
->label
);
961 str
= xstrdup(cxt
->uuid
);
966 str
= xstrdup(cxt
->parttype
);
971 str
= xstrdup(cxt
->partlabel
);
976 str
= xstrdup(cxt
->partuuid
);
981 str
= xstrdup(cxt
->partflags
);
984 get_udev_properties(cxt
);
986 str
= xstrdup(cxt
->wwn
);
989 str
= sysfs_strdup(&cxt
->sysfs
, "queue/read_ahead_kb");
991 set_sortdata_u64_from_string(ln
, col
, str
);
994 str
= xstrdup(is_readonly_device(cxt
) ? "1" : "0");
997 str
= sysfs_strdup(&cxt
->sysfs
, "removable");
998 if (!str
&& cxt
->sysfs
.parent
)
999 str
= sysfs_strdup(cxt
->sysfs
.parent
, "removable");
1002 str
= sysfs_is_hotpluggable(&cxt
->sysfs
) ? xstrdup("1") : xstrdup("0");
1005 str
= sysfs_strdup(&cxt
->sysfs
, "queue/rotational");
1008 str
= sysfs_strdup(&cxt
->sysfs
, "queue/add_random");
1011 if (!cxt
->partition
&& cxt
->nslaves
== 0)
1012 str
= sysfs_strdup(&cxt
->sysfs
, "device/model");
1015 if (!cxt
->partition
&& cxt
->nslaves
== 0) {
1016 get_udev_properties(cxt
);
1018 str
= xstrdup(cxt
->serial
);
1020 str
= sysfs_strdup(&cxt
->sysfs
, "device/serial");
1024 if (!cxt
->partition
&& cxt
->nslaves
== 0)
1025 str
= sysfs_strdup(&cxt
->sysfs
, "device/rev");
1028 if (!cxt
->partition
&& cxt
->nslaves
== 0)
1029 str
= sysfs_strdup(&cxt
->sysfs
, "device/vendor");
1035 xasprintf(&str
, "%ju", cxt
->size
);
1037 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, cxt
->size
);
1039 set_sortdata_u64(ln
, col
, cxt
->size
);
1042 if (!cxt
->partition
&& !cxt
->dm_name
)
1043 str
= sysfs_strdup(&cxt
->sysfs
, "device/state");
1044 else if (cxt
->dm_name
) {
1046 if (sysfs_read_int(&cxt
->sysfs
, "dm/suspended", &x
) == 0)
1047 str
= xstrdup(x
? "suspended" : "running");
1051 str
= sysfs_strdup(&cxt
->sysfs
, "alignment_offset");
1053 set_sortdata_u64_from_string(ln
, col
, str
);
1056 str
= sysfs_strdup(&cxt
->sysfs
, "queue/minimum_io_size");
1058 set_sortdata_u64_from_string(ln
, col
, str
);
1061 str
= sysfs_strdup(&cxt
->sysfs
, "queue/optimal_io_size");
1063 set_sortdata_u64_from_string(ln
, col
, str
);
1066 str
= sysfs_strdup(&cxt
->sysfs
, "queue/physical_block_size");
1068 set_sortdata_u64_from_string(ln
, col
, str
);
1071 str
= sysfs_strdup(&cxt
->sysfs
, "queue/logical_block_size");
1073 set_sortdata_u64_from_string(ln
, col
, str
);
1076 str
= get_scheduler(cxt
);
1079 str
= sysfs_strdup(&cxt
->sysfs
, "queue/nr_requests");
1081 set_sortdata_u64_from_string(ln
, col
, str
);
1084 str
= get_type(cxt
);
1089 if (sysfs_scsi_get_hctl(&cxt
->sysfs
, &h
, &c
, &t
, &l
) == 0)
1090 xasprintf(&str
, "%d:%d:%d:%d", h
, c
, t
, l
);
1094 str
= get_transport(cxt
);
1097 str
= get_subsystems(cxt
);
1101 str
= sysfs_strdup(&cxt
->sysfs
, "discard_alignment");
1105 set_sortdata_u64_from_string(ln
, col
, str
);
1109 str
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_granularity");
1111 set_sortdata_u64_from_string(ln
, col
, str
);
1114 if (sysfs_read_u64(&cxt
->sysfs
,
1115 "queue/discard_granularity", &x
) == 0) {
1116 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1118 set_sortdata_u64(ln
, col
, x
);
1124 str
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_max_bytes");
1126 set_sortdata_u64_from_string(ln
, col
, str
);
1129 if (sysfs_read_u64(&cxt
->sysfs
,
1130 "queue/discard_max_bytes", &x
) == 0) {
1131 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1133 set_sortdata_u64(ln
, col
, x
);
1139 str
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_zeroes_data");
1145 str
= sysfs_strdup(&cxt
->sysfs
, "queue/write_same_max_bytes");
1147 set_sortdata_u64_from_string(ln
, col
, str
);
1151 if (sysfs_read_u64(&cxt
->sysfs
,
1152 "queue/write_same_max_bytes", &x
) == 0) {
1153 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1155 set_sortdata_u64(ln
, col
, x
);
1162 str
= sysfs_strdup(&cxt
->sysfs
, "queue/zoned");
1166 if (str
&& scols_line_refer_data(ln
, col
, str
))
1167 err(EXIT_FAILURE
, _("failed to add output data"));
1170 static void fill_table_line(struct blkdev_cxt
*cxt
, struct libscols_line
*scols_parent
)
1174 cxt
->scols_line
= scols_table_new_line(lsblk
->table
, scols_parent
);
1175 if (!cxt
->scols_line
)
1176 err(EXIT_FAILURE
, _("failed to allocate output line"));
1178 for (i
= 0; i
< ncolumns
; i
++)
1179 set_scols_data(cxt
, i
, get_column_id(i
), cxt
->scols_line
);
1182 static int set_cxt(struct blkdev_cxt
*cxt
,
1183 struct blkdev_cxt
*parent
,
1184 struct blkdev_cxt
*wholedisk
,
1189 DBG(CXT
, ul_debugobj(cxt
, "setting context for %s [parent=%p, wholedisk=%p]",
1190 name
, parent
, wholedisk
));
1192 cxt
->parent
= parent
;
1193 cxt
->name
= xstrdup(name
);
1194 cxt
->partition
= wholedisk
!= NULL
;
1196 cxt
->filename
= get_device_path(cxt
);
1197 if (!cxt
->filename
) {
1198 DBG(CXT
, ul_debugobj(cxt
, "%s: failed to get device path", cxt
->name
));
1201 DBG(CXT
, ul_debugobj(cxt
, "%s: filename=%s", cxt
->name
, cxt
->filename
));
1203 devno
= sysfs_devname_to_devno(cxt
->name
, wholedisk
? wholedisk
->name
: NULL
);
1206 DBG(CXT
, ul_debugobj(cxt
, "%s: unknown device name", cxt
->name
));
1210 if (lsblk
->inverse
) {
1211 if (sysfs_init(&cxt
->sysfs
, devno
, wholedisk
? &wholedisk
->sysfs
: NULL
)) {
1212 DBG(CXT
, ul_debugobj(cxt
, "%s: failed to initialize sysfs handler", cxt
->name
));
1216 parent
->sysfs
.parent
= &cxt
->sysfs
;
1218 if (sysfs_init(&cxt
->sysfs
, devno
, parent
? &parent
->sysfs
: NULL
)) {
1219 DBG(CXT
, ul_debugobj(cxt
, "%s: failed to initialize sysfs handler", cxt
->name
));
1224 cxt
->maj
= major(devno
);
1225 cxt
->min
= minor(devno
);
1228 if (sysfs_read_u64(&cxt
->sysfs
, "size", &cxt
->size
) == 0) /* in sectors */
1229 cxt
->size
<<= 9; /* in bytes */
1231 if (sysfs_read_int(&cxt
->sysfs
,
1232 "queue/discard_granularity", &cxt
->discard
) != 0)
1235 /* Ignore devices of zero size */
1236 if (!lsblk
->all_devices
&& cxt
->size
== 0) {
1237 DBG(CXT
, ul_debugobj(cxt
, "zero size device -- ignore"));
1240 if (is_dm(cxt
->name
)) {
1241 cxt
->dm_name
= sysfs_strdup(&cxt
->sysfs
, "dm/name");
1242 if (!cxt
->dm_name
) {
1243 DBG(CXT
, ul_debugobj(cxt
, "%s: failed to get dm name", cxt
->name
));
1248 cxt
->npartitions
= sysfs_count_partitions(&cxt
->sysfs
, cxt
->name
);
1249 cxt
->nholders
= sysfs_count_dirents(&cxt
->sysfs
, "holders");
1250 cxt
->nslaves
= sysfs_count_dirents(&cxt
->sysfs
, "slaves");
1252 DBG(CXT
, ul_debugobj(cxt
, "%s: npartitions=%d, nholders=%d, nslaves=%d",
1253 cxt
->name
, cxt
->npartitions
, cxt
->nholders
, cxt
->nslaves
));
1255 /* ignore non-SCSI devices */
1256 if (lsblk
->scsi
&& sysfs_scsi_get_hctl(&cxt
->sysfs
, NULL
, NULL
, NULL
, NULL
)) {
1257 DBG(CXT
, ul_debugobj(cxt
, "non-scsi device -- ignore"));
1261 DBG(CXT
, ul_debugobj(cxt
, "%s: context successfully initialized", cxt
->name
));
1265 static int process_blkdev(struct blkdev_cxt
*cxt
, struct blkdev_cxt
*parent
,
1266 int do_partitions
, const char *part_name
);
1269 * List device partitions if any.
1271 static int list_partitions(struct blkdev_cxt
*wholedisk_cxt
, struct blkdev_cxt
*parent_cxt
,
1272 const char *part_name
)
1276 struct blkdev_cxt part_cxt
= { NULL
};
1279 assert(wholedisk_cxt
);
1282 * Do not process further if there are no partitions for
1283 * this device or the device itself is a partition.
1285 if (!wholedisk_cxt
->npartitions
|| wholedisk_cxt
->partition
)
1288 DBG(CXT
, ul_debugobj(wholedisk_cxt
, "probe whole-disk for partitions"));
1290 dir
= sysfs_opendir(&wholedisk_cxt
->sysfs
, NULL
);
1292 err(EXIT_FAILURE
, _("failed to open device directory in sysfs"));
1294 while ((d
= xreaddir(dir
))) {
1295 /* Process particular partition only? */
1296 if (part_name
&& strcmp(part_name
, d
->d_name
))
1299 if (!(sysfs_is_partition_dirent(dir
, d
, wholedisk_cxt
->name
)))
1302 DBG(CXT
, ul_debugobj(wholedisk_cxt
, " checking %s", d
->d_name
));
1304 if (lsblk
->inverse
) {
1311 if (set_cxt(&part_cxt
, parent_cxt
, wholedisk_cxt
, d
->d_name
))
1314 if (!parent_cxt
&& part_cxt
.nholders
)
1317 wholedisk_cxt
->parent
= &part_cxt
;
1318 fill_table_line(&part_cxt
, parent_cxt
? parent_cxt
->scols_line
: NULL
);
1320 process_blkdev(wholedisk_cxt
, &part_cxt
, 0, NULL
);
1328 int ps
= set_cxt(&part_cxt
, wholedisk_cxt
, wholedisk_cxt
, d
->d_name
);
1330 /* Print whole disk only once */
1332 fill_table_line(wholedisk_cxt
, parent_cxt
? parent_cxt
->scols_line
: NULL
);
1333 if (ps
== 0 && !lsblk
->nodeps
)
1334 process_blkdev(&part_cxt
, wholedisk_cxt
, 0, NULL
);
1337 reset_blkdev_cxt(&part_cxt
);
1341 DBG(CXT
, ul_debugobj(wholedisk_cxt
, "probe whole-disk for partitions -- done"));
1346 static int get_wholedisk_from_partition_dirent(DIR *dir
,
1347 struct dirent
*d
, struct blkdev_cxt
*cxt
)
1349 char path
[PATH_MAX
];
1353 if ((len
= readlinkat(dirfd(dir
), d
->d_name
, path
, sizeof(path
) - 1)) < 0)
1358 /* The path ends with ".../<device>/<partition>" */
1359 p
= strrchr(path
, '/');
1364 p
= strrchr(path
, '/');
1369 return set_cxt(cxt
, NULL
, NULL
, p
);
1373 * List device dependencies: partitions, holders (inverse = 0) or slaves (inverse = 1).
1375 static int list_deps(struct blkdev_cxt
*cxt
)
1379 struct blkdev_cxt dep
= { NULL
};
1380 const char *depname
;
1387 DBG(CXT
, ul_debugobj(cxt
, "%s: list dependencies", cxt
->name
));
1389 if (!(lsblk
->inverse
? cxt
->nslaves
: cxt
->nholders
))
1392 depname
= lsblk
->inverse
? "slaves" : "holders";
1393 dir
= sysfs_opendir(&cxt
->sysfs
, depname
);
1397 DBG(CXT
, ul_debugobj(cxt
, "%s: checking for '%s' dependence", cxt
->name
, depname
));
1399 while ((d
= xreaddir(dir
))) {
1400 /* Is the dependency a partition? */
1401 if (sysfs_is_partition_dirent(dir
, d
, NULL
)) {
1402 if (!get_wholedisk_from_partition_dirent(dir
, d
, &dep
)) {
1403 DBG(CXT
, ul_debugobj(cxt
, "%s: %s: dependence is partition",
1404 cxt
->name
, d
->d_name
));
1405 process_blkdev(&dep
, cxt
, 1, d
->d_name
);
1408 /* The dependency is a whole device. */
1409 else if (!set_cxt(&dep
, cxt
, NULL
, d
->d_name
)) {
1410 DBG(CXT
, ul_debugobj(cxt
, "%s: %s: dependence is whole-disk",
1411 cxt
->name
, d
->d_name
));
1412 /* For inverse tree we don't want to show partitions
1413 * if the dependence is on whole-disk */
1414 process_blkdev(&dep
, cxt
, lsblk
->inverse
? 0 : 1, NULL
);
1416 reset_blkdev_cxt(&dep
);
1420 DBG(CXT
, ul_debugobj(cxt
, "%s: checking for '%s' -- done", cxt
->name
, depname
));
1424 static int process_blkdev(struct blkdev_cxt
*cxt
, struct blkdev_cxt
*parent
,
1425 int do_partitions
, const char *part_name
)
1427 if (do_partitions
&& cxt
->npartitions
)
1428 list_partitions(cxt
, parent
, part_name
); /* partitions + whole-disk */
1430 fill_table_line(cxt
, parent
? parent
->scols_line
: NULL
); /* whole-disk only */
1432 return list_deps(cxt
);
1435 /* Iterate devices in sysfs */
1436 static int iterate_block_devices(void)
1440 struct blkdev_cxt cxt
= { NULL
};
1442 if (!(dir
= opendir(_PATH_SYS_BLOCK
)))
1445 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_BLOCK
));
1447 while ((d
= xreaddir(dir
))) {
1449 DBG(DEV
, ul_debug(" %s dentry", d
->d_name
));
1451 if (set_cxt(&cxt
, NULL
, NULL
, d
->d_name
))
1454 if (is_maj_excluded(cxt
.maj
) || !is_maj_included(cxt
.maj
))
1457 /* Skip devices in the middle of dependency tree. */
1458 if ((lsblk
->inverse
? cxt
.nholders
: cxt
.nslaves
) > 0)
1461 process_blkdev(&cxt
, NULL
, 1, NULL
);
1463 reset_blkdev_cxt(&cxt
);
1468 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_BLOCK
" -- done"));
1472 static char *devno_to_sysfs_name(dev_t devno
, char *devname
, char *buf
, size_t buf_size
)
1474 char path
[PATH_MAX
];
1477 if (!sysfs_devno_path(devno
, path
, sizeof(path
))) {
1478 warn(_("%s: failed to compose sysfs path"), devname
);
1482 len
= readlink(path
, buf
, buf_size
- 1);
1484 warn(_("%s: failed to read link"), path
);
1489 return xstrdup(strrchr(buf
, '/') + 1);
1492 static int process_one_device(char *devname
)
1494 struct blkdev_cxt parent
= { NULL
}, cxt
= { NULL
};
1496 char buf
[PATH_MAX
+ 1], *name
= NULL
, *diskname
= NULL
;
1498 int real_part
= 0, rc
= -EINVAL
;
1500 if (stat(devname
, &st
) || !S_ISBLK(st
.st_mode
)) {
1501 warnx(_("%s: not a block device"), devname
);
1505 if (!(name
= devno_to_sysfs_name(st
.st_rdev
, devname
, buf
, PATH_MAX
))) {
1506 warn(_("%s: failed to get sysfs name"), devname
);
1510 if (!strncmp(name
, "dm-", 3)) {
1511 /* dm mapping is never a real partition! */
1514 if (blkid_devno_to_wholedisk(st
.st_rdev
, buf
, sizeof(buf
), &disk
)) {
1515 warn(_("%s: failed to get whole-disk device number"), devname
);
1519 real_part
= st
.st_rdev
!= disk
;
1524 * Device is not a partition.
1526 if (set_cxt(&cxt
, NULL
, NULL
, name
))
1528 process_blkdev(&cxt
, NULL
, !lsblk
->inverse
, NULL
);
1531 * Partition, read sysfs name of the device.
1533 if (set_cxt(&parent
, NULL
, NULL
, diskname
))
1535 if (set_cxt(&cxt
, &parent
, &parent
, name
))
1539 process_blkdev(&parent
, &cxt
, 1, cxt
.name
);
1541 process_blkdev(&cxt
, &parent
, 1, NULL
);
1547 reset_blkdev_cxt(&cxt
);
1550 reset_blkdev_cxt(&parent
);
1555 static void parse_excludes(const char *str0
)
1557 const char *str
= str0
;
1559 while (str
&& *str
) {
1564 n
= strtoul(str
, &end
, 10);
1566 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1567 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1568 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1569 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1570 excludes
[nexcludes
++] = n
;
1572 if (nexcludes
== ARRAY_SIZE(excludes
))
1573 /* TRANSLATORS: The standard value for %d is 256. */
1574 errx(EXIT_FAILURE
, _("the list of excluded devices is "
1575 "too large (limit is %d devices)"),
1576 (int)ARRAY_SIZE(excludes
));
1578 str
= end
&& *end
? end
+ 1 : NULL
;
1582 static void parse_includes(const char *str0
)
1584 const char *str
= str0
;
1586 while (str
&& *str
) {
1591 n
= strtoul(str
, &end
, 10);
1593 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1594 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1595 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1596 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1597 includes
[nincludes
++] = n
;
1599 if (nincludes
== ARRAY_SIZE(includes
))
1600 /* TRANSLATORS: The standard value for %d is 256. */
1601 errx(EXIT_FAILURE
, _("the list of included devices is "
1602 "too large (limit is %d devices)"),
1603 (int)ARRAY_SIZE(includes
));
1604 str
= end
&& *end
? end
+ 1 : NULL
;
1609 * see set_sortdata_u64() and columns initialization in main()
1611 static int cmp_u64_cells(struct libscols_cell
*a
,
1612 struct libscols_cell
*b
,
1613 __attribute__((__unused__
)) void *data
)
1615 uint64_t *adata
= (uint64_t *) scols_cell_get_userdata(a
),
1616 *bdata
= (uint64_t *) scols_cell_get_userdata(b
);
1618 if (adata
== NULL
&& bdata
== NULL
)
1624 return *adata
== *bdata
? 0 : *adata
>= *bdata
? 1 : -1;
1627 static void __attribute__((__noreturn__
)) usage(void)
1632 fputs(USAGE_HEADER
, out
);
1633 fprintf(out
, _(" %s [options] [<device> ...]\n"), program_invocation_short_name
);
1635 fputs(USAGE_SEPARATOR
, out
);
1636 fputs(_("List information about block devices.\n"), out
);
1638 fputs(USAGE_OPTIONS
, out
);
1639 fputs(_(" -a, --all print all devices\n"), out
);
1640 fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out
);
1641 fputs(_(" -d, --nodeps don't print slaves or holders\n"), out
);
1642 fputs(_(" -D, --discard print discard capabilities\n"), out
);
1643 fputs(_(" -z, --zoned print zone model\n"), out
);
1644 fputs(_(" -e, --exclude <list> exclude devices by major number (default: RAM disks)\n"), out
);
1645 fputs(_(" -f, --fs output info about filesystems\n"), out
);
1646 fputs(_(" -i, --ascii use ascii characters only\n"), out
);
1647 fputs(_(" -I, --include <list> show only devices with specified major numbers\n"), out
);
1648 fputs(_(" -J, --json use JSON output format\n"), out
);
1649 fputs(_(" -l, --list use list format output\n"), out
);
1650 fputs(_(" -T, --tree use tree format output\n"), out
);
1651 fputs(_(" -m, --perms output info about permissions\n"), out
);
1652 fputs(_(" -n, --noheadings don't print headings\n"), out
);
1653 fputs(_(" -o, --output <list> output columns\n"), out
);
1654 fputs(_(" -O, --output-all output all columns\n"), out
);
1655 fputs(_(" -p, --paths print complete device path\n"), out
);
1656 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out
);
1657 fputs(_(" -r, --raw use raw output format\n"), out
);
1658 fputs(_(" -s, --inverse inverse dependencies\n"), out
);
1659 fputs(_(" -S, --scsi output info about SCSI devices\n"), out
);
1660 fputs(_(" -t, --topology output info about topology\n"), out
);
1661 fputs(_(" -x, --sort <column> sort output by <column>\n"), out
);
1662 fputs(USAGE_SEPARATOR
, out
);
1663 printf(USAGE_HELP_OPTIONS(22));
1665 fprintf(out
, USAGE_COLUMNS
);
1667 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
1668 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
1670 printf(USAGE_MAN_TAIL("lsblk(8)"));
1675 static void check_sysdevblock(void)
1677 if (access(_PATH_SYS_DEVBLOCK
, R_OK
) != 0)
1678 err(EXIT_FAILURE
, _("failed to access sysfs directory: %s"),
1679 _PATH_SYS_DEVBLOCK
);
1682 int main(int argc
, char *argv
[])
1684 struct lsblk _ls
= { .sort_id
= -1, .flags
= LSBLK_TREE
};
1685 int c
, status
= EXIT_FAILURE
;
1686 char *outarg
= NULL
;
1690 static const struct option longopts
[] = {
1691 { "all", no_argument
, NULL
, 'a' },
1692 { "bytes", no_argument
, NULL
, 'b' },
1693 { "nodeps", no_argument
, NULL
, 'd' },
1694 { "discard", no_argument
, NULL
, 'D' },
1695 { "zoned", no_argument
, NULL
, 'z' },
1696 { "help", no_argument
, NULL
, 'h' },
1697 { "json", no_argument
, NULL
, 'J' },
1698 { "output", required_argument
, NULL
, 'o' },
1699 { "output-all", no_argument
, NULL
, 'O' },
1700 { "perms", no_argument
, NULL
, 'm' },
1701 { "noheadings", no_argument
, NULL
, 'n' },
1702 { "list", no_argument
, NULL
, 'l' },
1703 { "ascii", no_argument
, NULL
, 'i' },
1704 { "raw", no_argument
, NULL
, 'r' },
1705 { "inverse", no_argument
, NULL
, 's' },
1706 { "fs", no_argument
, NULL
, 'f' },
1707 { "exclude", required_argument
, NULL
, 'e' },
1708 { "include", required_argument
, NULL
, 'I' },
1709 { "topology", no_argument
, NULL
, 't' },
1710 { "paths", no_argument
, NULL
, 'p' },
1711 { "pairs", no_argument
, NULL
, 'P' },
1712 { "scsi", no_argument
, NULL
, 'S' },
1713 { "sort", required_argument
, NULL
, 'x' },
1714 { "tree", no_argument
, NULL
, 'T' },
1715 { "version", no_argument
, NULL
, 'V' },
1716 { NULL
, 0, NULL
, 0 },
1719 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
1727 { 'P','T', 'l','r' },
1730 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1732 setlocale(LC_ALL
, "");
1733 bindtextdomain(PACKAGE
, LOCALEDIR
);
1734 textdomain(PACKAGE
);
1735 atexit(close_stdout
);
1741 while((c
= getopt_long(argc
, argv
,
1742 "abdDze:fhJlnmo:OpPiI:rstVSTx:", longopts
, NULL
)) != -1) {
1744 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1748 lsblk
->all_devices
= 1;
1757 add_uniq_column(COL_NAME
);
1758 add_uniq_column(COL_DALIGN
);
1759 add_uniq_column(COL_DGRAN
);
1760 add_uniq_column(COL_DMAX
);
1761 add_uniq_column(COL_DZERO
);
1764 add_uniq_column(COL_NAME
);
1765 add_uniq_column(COL_ZONED
);
1768 parse_excludes(optarg
);
1774 lsblk
->flags
|= LSBLK_JSON
;
1777 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
1780 lsblk
->flags
|= LSBLK_NOHEADINGS
;
1786 for (ncolumns
= 0 ; ncolumns
< ARRAY_SIZE(infos
); ncolumns
++)
1787 columns
[ncolumns
] = ncolumns
;
1793 lsblk
->flags
|= LSBLK_EXPORT
;
1794 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
1797 lsblk
->flags
|= LSBLK_ASCII
;
1800 parse_includes(optarg
);
1803 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
1804 lsblk
->flags
|= LSBLK_RAW
; /* enable raw */
1810 add_uniq_column(COL_NAME
);
1811 add_uniq_column(COL_FSTYPE
);
1812 add_uniq_column(COL_LABEL
);
1813 add_uniq_column(COL_UUID
);
1814 add_uniq_column(COL_TARGET
);
1817 add_uniq_column(COL_NAME
);
1818 add_uniq_column(COL_SIZE
);
1819 add_uniq_column(COL_OWNER
);
1820 add_uniq_column(COL_GROUP
);
1821 add_uniq_column(COL_MODE
);
1824 add_uniq_column(COL_NAME
);
1825 add_uniq_column(COL_ALIOFF
);
1826 add_uniq_column(COL_MINIO
);
1827 add_uniq_column(COL_OPTIO
);
1828 add_uniq_column(COL_PHYSEC
);
1829 add_uniq_column(COL_LOGSEC
);
1830 add_uniq_column(COL_ROTA
);
1831 add_uniq_column(COL_SCHED
);
1832 add_uniq_column(COL_RQ_SIZE
);
1833 add_uniq_column(COL_RA
);
1834 add_uniq_column(COL_WSAME
);
1839 add_uniq_column(COL_NAME
);
1840 add_uniq_column(COL_HCTL
);
1841 add_uniq_column(COL_TYPE
);
1842 add_uniq_column(COL_VENDOR
);
1843 add_uniq_column(COL_MODEL
);
1844 add_uniq_column(COL_REV
);
1845 add_uniq_column(COL_TRANSPORT
);
1851 printf(UTIL_LINUX_VERSION
);
1852 return EXIT_SUCCESS
;
1854 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
1855 lsblk
->sort_id
= column_name_to_id(optarg
, strlen(optarg
));
1856 if (lsblk
->sort_id
>= 0)
1860 errtryhelp(EXIT_FAILURE
);
1865 lsblk
->flags
|= LSBLK_TREE
;
1867 check_sysdevblock();
1870 add_column(COL_NAME
);
1871 add_column(COL_MAJMIN
);
1873 add_column(COL_SIZE
);
1875 add_column(COL_TYPE
);
1876 add_column(COL_TARGET
);
1879 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
1880 &ncolumns
, column_name_to_id
) < 0)
1881 return EXIT_FAILURE
;
1883 if (nexcludes
== 0 && nincludes
== 0)
1884 excludes
[nexcludes
++] = 1; /* default: ignore RAM disks */
1886 if (lsblk
->sort_id
< 0)
1887 /* Since Linux 4.8 we have sort devices by default, because
1888 * /sys is no more sorted */
1889 lsblk
->sort_id
= COL_MAJMIN
;
1891 /* For --inverse --list we still follow parent->child relation */
1892 if (lsblk
->inverse
&& !(lsblk
->flags
& LSBLK_TREE
))
1893 lsblk
->force_tree_order
= 1;
1895 if (lsblk
->sort_id
>= 0 && column_id_to_number(lsblk
->sort_id
) < 0) {
1896 /* the sort column is not between output columns -- add as hidden */
1897 add_column(lsblk
->sort_id
);
1898 lsblk
->sort_hidden
= 1;
1902 scols_init_debug(0);
1905 * initialize output columns
1907 if (!(lsblk
->table
= scols_new_table()))
1908 errx(EXIT_FAILURE
, _("failed to allocate output table"));
1909 scols_table_enable_raw(lsblk
->table
, !!(lsblk
->flags
& LSBLK_RAW
));
1910 scols_table_enable_export(lsblk
->table
, !!(lsblk
->flags
& LSBLK_EXPORT
));
1911 scols_table_enable_ascii(lsblk
->table
, !!(lsblk
->flags
& LSBLK_ASCII
));
1912 scols_table_enable_json(lsblk
->table
, !!(lsblk
->flags
& LSBLK_JSON
));
1913 scols_table_enable_noheadings(lsblk
->table
, !!(lsblk
->flags
& LSBLK_NOHEADINGS
));
1915 if (lsblk
->flags
& LSBLK_JSON
)
1916 scols_table_set_name(lsblk
->table
, "blockdevices");
1918 for (i
= 0; i
< ncolumns
; i
++) {
1919 struct colinfo
*ci
= get_column_info(i
);
1920 struct libscols_column
*cl
;
1921 int id
= get_column_id(i
), fl
= ci
->flags
;
1923 if (!(lsblk
->flags
& LSBLK_TREE
) && id
== COL_NAME
)
1924 fl
&= ~SCOLS_FL_TREE
;
1925 if (lsblk
->sort_hidden
&& lsblk
->sort_id
== id
)
1926 fl
|= SCOLS_FL_HIDDEN
;
1928 cl
= scols_table_new_column(lsblk
->table
, ci
->name
, ci
->whint
, fl
);
1930 warn(_("failed to allocate output column"));
1933 if (!lsblk
->sort_col
&& lsblk
->sort_id
== id
) {
1934 lsblk
->sort_col
= cl
;
1935 scols_column_set_cmpfunc(cl
,
1936 ci
->sort_type
== SORT_STRING
?
1937 scols_cmpstr_cells
: cmp_u64_cells
, NULL
);
1942 status
= iterate_block_devices() == 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
1944 int cnt
= 0, cnt_err
= 0;
1946 while (optind
< argc
) {
1947 if (process_one_device(argv
[optind
++]) != 0)
1951 status
= cnt
== 0 ? EXIT_FAILURE
: /* nothing */
1952 cnt
== cnt_err
? LSBLK_EXIT_ALLFAILED
:/* all failed */
1953 cnt_err
? LSBLK_EXIT_SOMEOK
: /* some ok */
1954 EXIT_SUCCESS
; /* all success */
1957 if (lsblk
->sort_col
)
1958 scols_sort_table(lsblk
->table
, lsblk
->sort_col
);
1959 if (lsblk
->force_tree_order
)
1960 scols_sort_table_by_tree(lsblk
->table
);
1962 scols_print_table(lsblk
->table
);
1965 if (lsblk
->sort_col
)
1966 unref_sortdata(lsblk
->table
);
1968 scols_unref_table(lsblk
->table
);
1970 mnt_unref_table(mtab
);
1971 mnt_unref_table(swaps
);
1972 mnt_unref_cache(mntcache
);