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 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
130 /* basic table settings */
132 LSBLK_ASCII
= (1 << 0),
133 LSBLK_RAW
= (1 << 1),
134 LSBLK_NOHEADINGS
= (1 << 2),
135 LSBLK_EXPORT
= (1 << 3),
136 LSBLK_TREE
= (1 << 4),
137 LSBLK_JSON
= (1 << 5),
141 SORT_STRING
= 0, /* default is to use scols_cell_get_data() */
142 SORT_U64
= 1 /* use private pointer from scols_cell_get_userdata() */
147 const char *name
; /* header */
148 double whint
; /* width hint (N < 1 is in percent of termwidth) */
149 int flags
; /* SCOLS_FL_* */
152 int sort_type
; /* SORT_* */
155 /* columns descriptions */
156 static struct colinfo infos
[] = {
157 [COL_NAME
] = { "NAME", 0.25, SCOLS_FL_TREE
| SCOLS_FL_NOEXTREMES
, N_("device name") },
158 [COL_KNAME
] = { "KNAME", 0.3, 0, N_("internal kernel device name") },
159 [COL_PKNAME
] = { "PKNAME", 0.3, 0, N_("internal parent kernel device name") },
160 [COL_MAJMIN
] = { "MAJ:MIN", 6, 0, N_("major:minor device number"), SORT_U64
},
161 [COL_FSTYPE
] = { "FSTYPE", 0.1, SCOLS_FL_TRUNC
, N_("filesystem type") },
162 [COL_TARGET
] = { "MOUNTPOINT", 0.10, SCOLS_FL_TRUNC
, N_("where the device is mounted") },
163 [COL_LABEL
] = { "LABEL", 0.1, 0, N_("filesystem LABEL") },
164 [COL_UUID
] = { "UUID", 36, 0, N_("filesystem UUID") },
166 [COL_PARTTYPE
] = { "PARTTYPE", 36, 0, N_("partition type UUID") },
167 [COL_PARTLABEL
] = { "PARTLABEL", 0.1, 0, N_("partition LABEL") },
168 [COL_PARTUUID
] = { "PARTUUID", 36, 0, N_("partition UUID") },
169 [COL_PARTFLAGS
] = { "PARTFLAGS", 36, 0, N_("partition flags") },
171 [COL_RA
] = { "RA", 3, SCOLS_FL_RIGHT
, N_("read-ahead of the device"), SORT_U64
},
172 [COL_RO
] = { "RO", 1, SCOLS_FL_RIGHT
, N_("read-only device") },
173 [COL_RM
] = { "RM", 1, SCOLS_FL_RIGHT
, N_("removable device") },
174 [COL_HOTPLUG
]= { "HOTPLUG", 1, SCOLS_FL_RIGHT
, N_("removable or hotplug device (usb, pcmcia, ...)") },
175 [COL_ROTA
] = { "ROTA", 1, SCOLS_FL_RIGHT
, N_("rotational device") },
176 [COL_RAND
] = { "RAND", 1, SCOLS_FL_RIGHT
, N_("adds randomness") },
177 [COL_MODEL
] = { "MODEL", 0.1, SCOLS_FL_TRUNC
, N_("device identifier") },
178 [COL_SERIAL
] = { "SERIAL", 0.1, SCOLS_FL_TRUNC
, N_("disk serial number") },
179 [COL_SIZE
] = { "SIZE", 5, SCOLS_FL_RIGHT
, N_("size of the device"), SORT_U64
},
180 [COL_STATE
] = { "STATE", 7, SCOLS_FL_TRUNC
, N_("state of the device") },
181 [COL_OWNER
] = { "OWNER", 0.1, SCOLS_FL_TRUNC
, N_("user name"), },
182 [COL_GROUP
] = { "GROUP", 0.1, SCOLS_FL_TRUNC
, N_("group name") },
183 [COL_MODE
] = { "MODE", 10, 0, N_("device node permissions") },
184 [COL_ALIOFF
] = { "ALIGNMENT", 6, SCOLS_FL_RIGHT
, N_("alignment offset"), SORT_U64
},
185 [COL_MINIO
] = { "MIN-IO", 6, SCOLS_FL_RIGHT
, N_("minimum I/O size"), SORT_U64
},
186 [COL_OPTIO
] = { "OPT-IO", 6, SCOLS_FL_RIGHT
, N_("optimal I/O size"), SORT_U64
},
187 [COL_PHYSEC
] = { "PHY-SEC", 7, SCOLS_FL_RIGHT
, N_("physical sector size"), SORT_U64
},
188 [COL_LOGSEC
] = { "LOG-SEC", 7, SCOLS_FL_RIGHT
, N_("logical sector size"), SORT_U64
},
189 [COL_SCHED
] = { "SCHED", 0.1, 0, N_("I/O scheduler name") },
190 [COL_RQ_SIZE
]= { "RQ-SIZE", 5, SCOLS_FL_RIGHT
, N_("request queue size"), SORT_U64
},
191 [COL_TYPE
] = { "TYPE", 4, 0, N_("device type") },
192 [COL_DALIGN
] = { "DISC-ALN", 6, SCOLS_FL_RIGHT
, N_("discard alignment offset"), SORT_U64
},
193 [COL_DGRAN
] = { "DISC-GRAN", 6, SCOLS_FL_RIGHT
, N_("discard granularity"), SORT_U64
},
194 [COL_DMAX
] = { "DISC-MAX", 6, SCOLS_FL_RIGHT
, N_("discard max bytes"), SORT_U64
},
195 [COL_DZERO
] = { "DISC-ZERO", 1, SCOLS_FL_RIGHT
, N_("discard zeroes data") },
196 [COL_WSAME
] = { "WSAME", 6, SCOLS_FL_RIGHT
, N_("write same max bytes"), SORT_U64
},
197 [COL_WWN
] = { "WWN", 18, 0, N_("unique storage identifier") },
198 [COL_HCTL
] = { "HCTL", 10, 0, N_("Host:Channel:Target:Lun for SCSI") },
199 [COL_TRANSPORT
] = { "TRAN", 6, 0, N_("device transport type") },
200 [COL_SUBSYS
] = { "SUBSYSTEMS", 0.1, SCOLS_FL_NOEXTREMES
, N_("de-duplicated chain of subsystems") },
201 [COL_REV
] = { "REV", 4, SCOLS_FL_RIGHT
, N_("device revision") },
202 [COL_VENDOR
] = { "VENDOR", 0.1, SCOLS_FL_TRUNC
, N_("device vendor") },
206 struct libscols_table
*table
; /* output table */
207 struct libscols_column
*sort_col
;/* sort output by this column */
210 unsigned int all_devices
:1; /* print all devices, including empty */
211 unsigned int bytes
:1; /* print SIZE in bytes */
212 unsigned int inverse
:1; /* print inverse dependencies */
213 unsigned int nodeps
:1; /* don't print slaves/holders */
214 unsigned int scsi
:1; /* print only device with HCTL (SCSI) */
215 unsigned int paths
:1; /* print devnames with "/dev" prefix */
216 unsigned int sort_hidden
:1; /* sort column not between output columns */
219 struct lsblk
*lsblk
; /* global handler */
221 /* columns[] array specifies all currently wanted output column. The columns
222 * are defined by infos[] array and you can specify (on command line) each
223 * column twice. That's enough, dynamically allocated array of the columns is
224 * unnecessary overkill and over-engineering in this case */
225 static int columns
[ARRAY_SIZE(infos
) * 2];
226 static size_t ncolumns
;
228 static inline size_t err_columns_index(size_t arysz
, size_t idx
)
231 errx(EXIT_FAILURE
, _("too many columns specified, "
232 "the limit is %zu columns"),
237 #define add_column(ary, n, id) \
238 ((ary)[ err_columns_index(ARRAY_SIZE(ary), (n)) ] = (id))
240 static int excludes
[256];
241 static size_t nexcludes
;
243 static int includes
[256];
244 static size_t nincludes
;
246 static struct libmnt_table
*mtab
, *swaps
;
247 static struct libmnt_cache
*mntcache
;
254 struct blkdev_cxt
*parent
;
256 struct libscols_line
*scols_line
;
259 char *name
; /* kernel name in /sys/block */
260 char *dm_name
; /* DM name (dm/block) */
262 char *filename
; /* path to device node */
264 struct sysfs_cxt sysfs
;
266 int partition
; /* is partition? TRUE/FALSE */
268 int probed
; /* already probed */
269 char *fstype
; /* detected fs, NULL or "?" if cannot detect */
270 char *uuid
; /* filesystem UUID (or stack uuid) */
271 char *label
; /* filesystem label */
272 char *parttype
; /* partiton type UUID */
273 char *partuuid
; /* partition UUID */
274 char *partlabel
; /* partiton label */
275 char *partflags
; /* partition flags */
276 char *wwn
; /* storage WWN */
277 char *serial
; /* disk serial number */
279 int npartitions
; /* # of partitions this device has */
280 int nholders
; /* # of devices mapped directly to this device
281 * /sys/block/.../holders */
282 int nslaves
; /* # of devices this device maps to */
283 int maj
, min
; /* devno */
284 int discard
; /* supports discard */
286 uint64_t size
; /* device size */
289 static void lsblk_init_debug(void)
291 __UL_INIT_DEBUG(lsblk
, LSBLK_DEBUG_
, 0, LSBLK_DEBUG
);
294 static int is_maj_excluded(int maj
)
298 assert(ARRAY_SIZE(excludes
) > nexcludes
);
301 return 0; /* filter not enabled, device not exluded */
303 for (i
= 0; i
< nexcludes
; i
++) {
304 if (excludes
[i
] == maj
) {
305 DBG(FILTER
, ul_debug("exclude: maj=%d", maj
));
312 static int is_maj_included(int maj
)
316 assert(ARRAY_SIZE(includes
) > nincludes
);
319 return 1; /* filter not enabled, device is included */
321 for (i
= 0; i
< nincludes
; i
++) {
322 if (includes
[i
] == maj
) {
323 DBG(FILTER
, ul_debug("include: maj=%d", maj
));
330 /* array with IDs of enabled columns */
331 static int get_column_id(int num
)
334 assert((size_t) num
< ncolumns
);
335 assert(columns
[num
] < (int) ARRAY_SIZE(infos
));
339 static struct colinfo
*get_column_info(int num
)
341 return &infos
[ get_column_id(num
) ];
344 static int column_name_to_id(const char *name
, size_t namesz
)
348 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
349 const char *cn
= infos
[i
].name
;
351 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
354 warnx(_("unknown column: %s"), name
);
358 static int column_id_to_number(int id
)
362 for (i
= 0; i
< ncolumns
; i
++)
363 if (columns
[i
] == id
)
368 static void reset_blkdev_cxt(struct blkdev_cxt
*cxt
)
373 DBG(CXT
, ul_debugobj(cxt
, "reset"));
383 free(cxt
->partlabel
);
387 sysfs_deinit(&cxt
->sysfs
);
389 memset(cxt
, 0, sizeof(*cxt
));
392 static int is_dm(const char *name
)
394 return strncmp(name
, "dm-", 3) ? 0 : 1;
397 static struct dirent
*xreaddir(DIR *dp
)
403 while ((d
= readdir(dp
))) {
404 if (!strcmp(d
->d_name
, ".") ||
405 !strcmp(d
->d_name
, ".."))
408 /* blacklist here? */
414 static char *get_device_path(struct blkdev_cxt
*cxt
)
421 if (is_dm(cxt
->name
))
422 return canonicalize_dm_name(cxt
->name
);
424 snprintf(path
, sizeof(path
), "/dev/%s", cxt
->name
);
425 sysfs_devname_sys_to_dev(path
);
426 return xstrdup(path
);
429 static int table_parser_errcb(struct libmnt_table
*tb
__attribute__((__unused__
)),
430 const char *filename
, int line
)
433 warnx(_("%s: parse error: ignore entry at line %d."),
438 static int is_active_swap(const char *filename
)
441 swaps
= mnt_new_table();
445 mntcache
= mnt_new_cache();
447 mnt_table_set_parser_errcb(swaps
, table_parser_errcb
);
448 mnt_table_set_cache(swaps
, mntcache
);
449 mnt_table_parse_swaps(swaps
, NULL
);
452 return mnt_table_find_srcpath(swaps
, filename
, MNT_ITER_BACKWARD
) != 0;
455 static char *get_device_mountpoint(struct blkdev_cxt
*cxt
)
457 struct libmnt_fs
*fs
;
461 assert(cxt
->filename
);
464 mtab
= mnt_new_table();
468 mntcache
= mnt_new_cache();
470 mnt_table_set_parser_errcb(mtab
, table_parser_errcb
);
471 mnt_table_set_cache(mtab
, mntcache
);
472 mnt_table_parse_mtab(mtab
, NULL
);
475 /* Note that maj:min in /proc/self/mouninfo does not have to match with
476 * devno as returned by stat(), so we have to try devname too
478 fs
= mnt_table_find_devno(mtab
, makedev(cxt
->maj
, cxt
->min
), MNT_ITER_BACKWARD
);
480 fs
= mnt_table_find_srcpath(mtab
, cxt
->filename
, MNT_ITER_BACKWARD
);
482 return is_active_swap(cxt
->filename
) ? xstrdup("[SWAP]") : NULL
;
485 fsroot
= mnt_fs_get_root(fs
);
486 if (fsroot
&& strcmp(fsroot
, "/") != 0) {
487 /* hmm.. we found bind mount or btrfs subvolume, let's try to
488 * get real FS root mountpoint */
489 struct libmnt_fs
*rfs
;
490 struct libmnt_iter
*itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
492 mnt_table_set_iter(mtab
, itr
, fs
);
493 while (mnt_table_next_fs(mtab
, itr
, &rfs
) == 0) {
494 fsroot
= mnt_fs_get_root(rfs
);
495 if ((!fsroot
|| strcmp(fsroot
, "/") == 0)
496 && mnt_fs_match_source(rfs
, cxt
->filename
, mntcache
)) {
504 DBG(DEV
, ul_debugobj(cxt
, "mountpoint: %s", mnt_fs_get_target(fs
)));
505 return xstrdup(mnt_fs_get_target(fs
));
509 static int get_udev_properties(struct blkdev_cxt
*cxt
510 __attribute__((__unused__
)))
515 static int get_udev_properties(struct blkdev_cxt
*cxt
)
517 struct udev_device
*dev
;
520 return 0; /* already done */
527 dev
= udev_device_new_from_subsystem_sysname(udev
, "block", cxt
->name
);
531 if ((data
= udev_device_get_property_value(dev
, "ID_FS_LABEL_ENC"))) {
532 cxt
->label
= xstrdup(data
);
533 unhexmangle_string(cxt
->label
);
535 if ((data
= udev_device_get_property_value(dev
, "ID_FS_UUID_ENC"))) {
536 cxt
->uuid
= xstrdup(data
);
537 unhexmangle_string(cxt
->uuid
);
539 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_NAME"))) {
540 cxt
->partlabel
= xstrdup(data
);
541 unhexmangle_string(cxt
->partlabel
);
543 if ((data
= udev_device_get_property_value(dev
, "ID_FS_TYPE")))
544 cxt
->fstype
= xstrdup(data
);
545 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_TYPE")))
546 cxt
->parttype
= xstrdup(data
);
547 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_UUID")))
548 cxt
->partuuid
= xstrdup(data
);
549 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_FLAGS")))
550 cxt
->partflags
= xstrdup(data
);
551 if ((data
= udev_device_get_property_value(dev
, "ID_WWN")))
552 cxt
->wwn
= xstrdup(data
);
553 if ((data
= udev_device_get_property_value(dev
, "ID_SERIAL_SHORT")))
554 cxt
->serial
= xstrdup(data
);
555 udev_device_unref(dev
);
557 DBG(DEV
, ul_debugobj(cxt
, "%s: found udev properties", cxt
->name
));
560 return cxt
->probed
== 1 ? 0 : -1;
563 #endif /* HAVE_LIBUDEV */
565 static void probe_device(struct blkdev_cxt
*cxt
)
567 blkid_probe pr
= NULL
;
576 if (get_udev_properties(cxt
) == 0)
577 return; /* success */
581 /* try libblkid (fallback) */
583 return; /* no permissions to read from the device */
585 pr
= blkid_new_probe_from_filename(cxt
->filename
);
589 blkid_probe_enable_superblocks(pr
, 1);
590 blkid_probe_set_superblocks_flags(pr
, BLKID_SUBLKS_LABEL
|
593 blkid_probe_enable_partitions(pr
, 1);
594 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
596 if (!blkid_do_safeprobe(pr
)) {
597 const char *data
= NULL
;
599 if (!blkid_probe_lookup_value(pr
, "TYPE", &data
, NULL
))
600 cxt
->fstype
= xstrdup(data
);
601 if (!blkid_probe_lookup_value(pr
, "UUID", &data
, NULL
))
602 cxt
->uuid
= xstrdup(data
);
603 if (!blkid_probe_lookup_value(pr
, "LABEL", &data
, NULL
))
604 cxt
->label
= xstrdup(data
);
605 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_TYPE", &data
, NULL
))
606 cxt
->parttype
= xstrdup(data
);
607 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_UUID", &data
, NULL
))
608 cxt
->partuuid
= xstrdup(data
);
609 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_NAME", &data
, NULL
))
610 cxt
->partlabel
= xstrdup(data
);
611 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_FLAGS", &data
, NULL
))
612 cxt
->partflags
= xstrdup(data
);
613 DBG(DEV
, ul_debugobj(cxt
, "%s: found blkid properties", cxt
->name
));
616 blkid_free_probe(pr
);
620 static int is_readonly_device(struct blkdev_cxt
*cxt
)
624 if (sysfs_scanf(&cxt
->sysfs
, "ro", "%d", &ro
) == 1)
627 /* fallback if "ro" attribute does not exist */
628 fd
= open(cxt
->filename
, O_RDONLY
);
630 if (ioctl(fd
, BLKROGET
, &ro
) != 0)
637 static char *get_scheduler(struct blkdev_cxt
*cxt
)
639 char *str
= sysfs_strdup(&cxt
->sysfs
, "queue/scheduler");
640 char *p
, *res
= NULL
;
644 p
= strchr(str
, '[');
647 p
= strchr(res
, ']');
658 static char *get_type(struct blkdev_cxt
*cxt
)
660 char *res
= NULL
, *p
;
662 if (is_dm(cxt
->name
)) {
663 char *dm_uuid
= sysfs_strdup(&cxt
->sysfs
, "dm/uuid");
665 /* The DM_UUID prefix should be set to subsystem owning
666 * the device - LVM, CRYPT, DMRAID, MPATH, PART */
669 char *dm_uuid_prefix
= strsep(&tmp
, "-");
671 if (dm_uuid_prefix
) {
672 /* kpartx hack to remove partition number */
673 if (strncasecmp(dm_uuid_prefix
, "part", 4) == 0)
674 dm_uuid_prefix
[4] = '\0';
676 res
= xstrdup(dm_uuid_prefix
);
682 /* No UUID or no prefix - just mark it as DM device */
685 } else if (!strncmp(cxt
->name
, "loop", 4)) {
686 res
= xstrdup("loop");
688 } else if (!strncmp(cxt
->name
, "md", 2)) {
689 char *md_level
= sysfs_strdup(&cxt
->sysfs
, "md/level");
690 res
= md_level
? md_level
: xstrdup("md");
693 const char *type
= NULL
;
696 if (!sysfs_read_int(&cxt
->sysfs
, "device/type", &x
))
697 type
= blkdev_scsi_type_to_name(x
);
700 type
= cxt
->partition
? "part" : "disk";
704 for (p
= res
; p
&& *p
; p
++)
705 *p
= tolower((unsigned char) *p
);
709 /* Thanks to lsscsi code for idea of detection logic used here */
710 static char *get_transport(struct blkdev_cxt
*cxt
)
712 struct sysfs_cxt
*sysfs
= &cxt
->sysfs
;
714 const char *trans
= NULL
;
716 /* SCSI - Serial Peripheral Interface */
717 if (sysfs_scsi_host_is(sysfs
, "spi"))
720 /* FC/FCoE - Fibre Channel / Fibre Channel over Ethernet */
721 else if (sysfs_scsi_host_is(sysfs
, "fc")) {
722 attr
= sysfs_scsi_host_strdup_attribute(sysfs
, "fc", "symbolic_name");
725 trans
= strstr(attr
, " over ") ? "fcoe" : "fc";
729 /* SAS - Serial Attached SCSI */
730 else if (sysfs_scsi_host_is(sysfs
, "sas") ||
731 sysfs_scsi_has_attribute(sysfs
, "sas_device"))
735 /* SBP - Serial Bus Protocol (FireWire) */
736 else if (sysfs_scsi_has_attribute(sysfs
, "ieee1394_id"))
740 else if (sysfs_scsi_host_is(sysfs
, "iscsi"))
743 /* USB - Universal Serial Bus */
744 else if (sysfs_scsi_path_contains(sysfs
, "usb"))
748 else if (sysfs_scsi_host_is(sysfs
, "scsi")) {
749 attr
= sysfs_scsi_host_strdup_attribute(sysfs
, "scsi", "proc_name");
752 if (!strncmp(attr
, "ahci", 4) || !strncmp(attr
, "sata", 4))
754 else if (strstr(attr
, "ata"))
759 return trans
? xstrdup(trans
) : NULL
;
762 static char *get_subsystems(struct blkdev_cxt
*cxt
)
765 char *sub
, *chain
, *res
= NULL
;
766 size_t len
= 0, last
= 0;
768 chain
= sysfs_get_devchain(&cxt
->sysfs
, path
, sizeof(path
));
772 while (sysfs_next_subsystem(&cxt
->sysfs
, chain
, &sub
) == 0) {
775 /* don't create "block:scsi:scsi", but "block:scsi" */
776 if (len
&& strcmp(res
+ last
, sub
) == 0) {
782 res
= xrealloc(res
, len
+ sz
+ 2);
786 memcpy(res
+ len
, sub
, sz
+ 1);
796 #define is_parsable(_l) (scols_table_is_raw((_l)->table) || \
797 scols_table_is_export((_l)->table) || \
798 scols_table_is_json((_l)->table))
800 static char *mk_name(const char *name
)
806 xasprintf(&p
, "/dev/%s", name
);
810 sysfs_devname_sys_to_dev(p
);
814 static char *mk_dm_name(const char *name
)
820 xasprintf(&p
, "/dev/mapper/%s", name
);
826 /* stores data to scols cell userdata (invisible and independent on output)
827 * to make the original values accessible for sort functions
829 static void set_sortdata_u64(struct libscols_line
*ln
, int col
, uint64_t x
)
831 struct libscols_cell
*ce
= scols_line_get_cell(ln
, col
);
836 data
= xmalloc(sizeof(uint64_t));
838 scols_cell_set_userdata(ce
, data
);
841 static void set_sortdata_u64_from_string(struct libscols_line
*ln
, int col
, const char *str
)
845 if (!str
|| sscanf(str
, "%"SCNu64
, &x
) != 1)
848 set_sortdata_u64(ln
, col
, x
);
851 static void unref_sortdata(struct libscols_table
*tb
)
853 struct libscols_iter
*itr
;
854 struct libscols_line
*ln
;
856 if (!tb
|| !lsblk
->sort_col
)
858 itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
861 while (scols_table_next_line(tb
, itr
, &ln
) == 0) {
862 struct libscols_cell
*ce
= scols_line_get_column_cell(ln
,
864 void *data
= scols_cell_get_userdata(ce
);
868 scols_free_iter(itr
);
871 static void set_scols_data(struct blkdev_cxt
*cxt
, int col
, int id
, struct libscols_line
*ln
)
873 int sort
= 0, st_rc
= 0;
876 if (!cxt
->st
.st_rdev
&& (id
== COL_OWNER
|| id
== COL_GROUP
||
878 st_rc
= stat(cxt
->filename
, &cxt
->st
);
880 if (lsblk
->sort_id
== id
)
885 str
= cxt
->dm_name
? mk_dm_name(cxt
->dm_name
) : mk_name(cxt
->name
);
888 str
= mk_name(cxt
->name
);
892 str
= mk_name(cxt
->parent
->name
);
896 struct passwd
*pw
= st_rc
? NULL
: getpwuid(cxt
->st
.st_uid
);
898 str
= xstrdup(pw
->pw_name
);
903 struct group
*gr
= st_rc
? NULL
: getgrgid(cxt
->st
.st_gid
);
905 str
= xstrdup(gr
->gr_name
);
913 xstrmode(cxt
->st
.st_mode
, md
);
919 if (is_parsable(lsblk
))
920 xasprintf(&str
, "%u:%u", cxt
->maj
, cxt
->min
);
922 xasprintf(&str
, "%3u:%-3u", cxt
->maj
, cxt
->min
);
924 set_sortdata_u64(ln
, col
, makedev(cxt
->maj
, cxt
->min
));
929 str
= xstrdup(cxt
->fstype
);
932 str
= get_device_mountpoint(cxt
);
937 str
= xstrdup(cxt
->label
);
942 str
= xstrdup(cxt
->uuid
);
947 str
= xstrdup(cxt
->parttype
);
952 str
= xstrdup(cxt
->partlabel
);
957 str
= xstrdup(cxt
->partuuid
);
962 str
= xstrdup(cxt
->partflags
);
965 get_udev_properties(cxt
);
967 str
= xstrdup(cxt
->wwn
);
970 str
= sysfs_strdup(&cxt
->sysfs
, "queue/read_ahead_kb");
972 set_sortdata_u64_from_string(ln
, col
, str
);
975 str
= xstrdup(is_readonly_device(cxt
) ? "1" : "0");
978 str
= sysfs_strdup(&cxt
->sysfs
, "removable");
979 if (!str
&& cxt
->sysfs
.parent
)
980 str
= sysfs_strdup(cxt
->sysfs
.parent
, "removable");
983 str
= sysfs_is_hotpluggable(&cxt
->sysfs
) ? xstrdup("1") : xstrdup("0");
986 str
= sysfs_strdup(&cxt
->sysfs
, "queue/rotational");
989 str
= sysfs_strdup(&cxt
->sysfs
, "queue/add_random");
992 if (!cxt
->partition
&& cxt
->nslaves
== 0)
993 str
= sysfs_strdup(&cxt
->sysfs
, "device/model");
996 if (!cxt
->partition
&& cxt
->nslaves
== 0) {
997 get_udev_properties(cxt
);
999 str
= xstrdup(cxt
->serial
);
1003 if (!cxt
->partition
&& cxt
->nslaves
== 0)
1004 str
= sysfs_strdup(&cxt
->sysfs
, "device/rev");
1007 if (!cxt
->partition
&& cxt
->nslaves
== 0)
1008 str
= sysfs_strdup(&cxt
->sysfs
, "device/vendor");
1014 xasprintf(&str
, "%ju", cxt
->size
);
1016 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, cxt
->size
);
1018 set_sortdata_u64(ln
, col
, cxt
->size
);
1021 if (!cxt
->partition
&& !cxt
->dm_name
)
1022 str
= sysfs_strdup(&cxt
->sysfs
, "device/state");
1023 else if (cxt
->dm_name
) {
1025 if (sysfs_read_int(&cxt
->sysfs
, "dm/suspended", &x
) == 0)
1026 str
= xstrdup(x
? "suspended" : "running");
1030 str
= sysfs_strdup(&cxt
->sysfs
, "alignment_offset");
1032 set_sortdata_u64_from_string(ln
, col
, str
);
1035 str
= sysfs_strdup(&cxt
->sysfs
, "queue/minimum_io_size");
1037 set_sortdata_u64_from_string(ln
, col
, str
);
1040 str
= sysfs_strdup(&cxt
->sysfs
, "queue/optimal_io_size");
1042 set_sortdata_u64_from_string(ln
, col
, str
);
1045 str
= sysfs_strdup(&cxt
->sysfs
, "queue/physical_block_size");
1047 set_sortdata_u64_from_string(ln
, col
, str
);
1050 str
= sysfs_strdup(&cxt
->sysfs
, "queue/logical_block_size");
1052 set_sortdata_u64_from_string(ln
, col
, str
);
1055 str
= get_scheduler(cxt
);
1058 str
= sysfs_strdup(&cxt
->sysfs
, "queue/nr_requests");
1060 set_sortdata_u64_from_string(ln
, col
, str
);
1063 str
= get_type(cxt
);
1068 if (sysfs_scsi_get_hctl(&cxt
->sysfs
, &h
, &c
, &t
, &l
) == 0)
1069 xasprintf(&str
, "%d:%d:%d:%d", h
, c
, t
, l
);
1073 str
= get_transport(cxt
);
1076 str
= get_subsystems(cxt
);
1080 str
= sysfs_strdup(&cxt
->sysfs
, "discard_alignment");
1084 set_sortdata_u64_from_string(ln
, col
, str
);
1088 str
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_granularity");
1090 set_sortdata_u64_from_string(ln
, col
, str
);
1093 if (sysfs_read_u64(&cxt
->sysfs
,
1094 "queue/discard_granularity", &x
) == 0) {
1095 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1097 set_sortdata_u64(ln
, col
, x
);
1103 str
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_max_bytes");
1105 set_sortdata_u64_from_string(ln
, col
, str
);
1108 if (sysfs_read_u64(&cxt
->sysfs
,
1109 "queue/discard_max_bytes", &x
) == 0) {
1110 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1112 set_sortdata_u64(ln
, col
, x
);
1118 str
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_zeroes_data");
1124 str
= sysfs_strdup(&cxt
->sysfs
, "queue/write_same_max_bytes");
1126 set_sortdata_u64_from_string(ln
, col
, str
);
1130 if (sysfs_read_u64(&cxt
->sysfs
,
1131 "queue/write_same_max_bytes", &x
) == 0) {
1132 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1134 set_sortdata_u64(ln
, col
, x
);
1143 scols_line_refer_data(ln
, col
, str
);
1146 static void fill_table_line(struct blkdev_cxt
*cxt
, struct libscols_line
*scols_parent
)
1150 cxt
->scols_line
= scols_table_new_line(lsblk
->table
, scols_parent
);
1151 if (!cxt
->scols_line
)
1154 for (i
= 0; i
< ncolumns
; i
++)
1155 set_scols_data(cxt
, i
, get_column_id(i
), cxt
->scols_line
);
1158 static int set_cxt(struct blkdev_cxt
*cxt
,
1159 struct blkdev_cxt
*parent
,
1160 struct blkdev_cxt
*wholedisk
,
1165 DBG(CXT
, ul_debugobj(cxt
, "setting context for %s [parent=%p, wholedisk=%p]",
1166 name
, parent
, wholedisk
));
1168 cxt
->parent
= parent
;
1169 cxt
->name
= xstrdup(name
);
1170 cxt
->partition
= wholedisk
!= NULL
;
1172 cxt
->filename
= get_device_path(cxt
);
1173 if (!cxt
->filename
) {
1174 DBG(CXT
, ul_debugobj(cxt
, "%s: failed to get device path", cxt
->name
));
1177 DBG(CXT
, ul_debugobj(cxt
, "%s: filename=%s", cxt
->name
, cxt
->filename
));
1179 devno
= sysfs_devname_to_devno(cxt
->name
, wholedisk
? wholedisk
->name
: NULL
);
1182 DBG(CXT
, ul_debugobj(cxt
, "%s: unknown device name", cxt
->name
));
1186 if (lsblk
->inverse
) {
1187 if (sysfs_init(&cxt
->sysfs
, devno
, wholedisk
? &wholedisk
->sysfs
: NULL
)) {
1188 DBG(CXT
, ul_debugobj(cxt
, "%s: failed to initialize sysfs handler", cxt
->name
));
1192 parent
->sysfs
.parent
= &cxt
->sysfs
;
1194 if (sysfs_init(&cxt
->sysfs
, devno
, parent
? &parent
->sysfs
: NULL
)) {
1195 DBG(CXT
, ul_debugobj(cxt
, "%s: failed to initialize sysfs handler", cxt
->name
));
1200 cxt
->maj
= major(devno
);
1201 cxt
->min
= minor(devno
);
1204 if (sysfs_read_u64(&cxt
->sysfs
, "size", &cxt
->size
) == 0) /* in sectors */
1205 cxt
->size
<<= 9; /* in bytes */
1207 if (sysfs_read_int(&cxt
->sysfs
,
1208 "queue/discard_granularity", &cxt
->discard
) != 0)
1211 /* Ignore devices of zero size */
1212 if (!lsblk
->all_devices
&& cxt
->size
== 0) {
1213 DBG(CXT
, ul_debugobj(cxt
, "zero size device -- ignore"));
1216 if (is_dm(cxt
->name
)) {
1217 cxt
->dm_name
= sysfs_strdup(&cxt
->sysfs
, "dm/name");
1218 if (!cxt
->dm_name
) {
1219 DBG(CXT
, ul_debugobj(cxt
, "%s: failed to get dm name", cxt
->name
));
1224 cxt
->npartitions
= sysfs_count_partitions(&cxt
->sysfs
, cxt
->name
);
1225 cxt
->nholders
= sysfs_count_dirents(&cxt
->sysfs
, "holders");
1226 cxt
->nslaves
= sysfs_count_dirents(&cxt
->sysfs
, "slaves");
1228 DBG(CXT
, ul_debugobj(cxt
, "%s: npartitions=%d, nholders=%d, nslaves=%d",
1229 cxt
->name
, cxt
->npartitions
, cxt
->nholders
, cxt
->nslaves
));
1231 /* ignore non-SCSI devices */
1232 if (lsblk
->scsi
&& sysfs_scsi_get_hctl(&cxt
->sysfs
, NULL
, NULL
, NULL
, NULL
)) {
1233 DBG(CXT
, ul_debugobj(cxt
, "non-scsi device -- ignore"));
1237 DBG(CXT
, ul_debugobj(cxt
, "%s: context successfully initialized", cxt
->name
));
1241 static int process_blkdev(struct blkdev_cxt
*cxt
, struct blkdev_cxt
*parent
,
1242 int do_partitions
, const char *part_name
);
1245 * List device partitions if any.
1247 static int list_partitions(struct blkdev_cxt
*wholedisk_cxt
, struct blkdev_cxt
*parent_cxt
,
1248 const char *part_name
)
1252 struct blkdev_cxt part_cxt
= { 0 };
1255 assert(wholedisk_cxt
);
1258 * Do not process further if there are no partitions for
1259 * this device or the device itself is a partition.
1261 if (!wholedisk_cxt
->npartitions
|| wholedisk_cxt
->partition
)
1264 DBG(CXT
, ul_debugobj(wholedisk_cxt
, "probe whole-disk for partitions"));
1266 dir
= sysfs_opendir(&wholedisk_cxt
->sysfs
, NULL
);
1268 err(EXIT_FAILURE
, _("failed to open device directory in sysfs"));
1270 while ((d
= xreaddir(dir
))) {
1271 /* Process particular partition only? */
1272 if (part_name
&& strcmp(part_name
, d
->d_name
))
1275 if (!(sysfs_is_partition_dirent(dir
, d
, wholedisk_cxt
->name
)))
1278 DBG(CXT
, ul_debugobj(wholedisk_cxt
, " checking %s", d
->d_name
));
1280 if (lsblk
->inverse
) {
1287 if (set_cxt(&part_cxt
, parent_cxt
, wholedisk_cxt
, d
->d_name
))
1290 if (!parent_cxt
&& part_cxt
.nholders
)
1293 wholedisk_cxt
->parent
= &part_cxt
;
1294 fill_table_line(&part_cxt
, parent_cxt
? parent_cxt
->scols_line
: NULL
);
1296 process_blkdev(wholedisk_cxt
, &part_cxt
, 0, NULL
);
1304 int ps
= set_cxt(&part_cxt
, wholedisk_cxt
, wholedisk_cxt
, d
->d_name
);
1306 /* Print whole disk only once */
1308 fill_table_line(wholedisk_cxt
, parent_cxt
? parent_cxt
->scols_line
: NULL
);
1309 if (ps
== 0 && !lsblk
->nodeps
)
1310 process_blkdev(&part_cxt
, wholedisk_cxt
, 0, NULL
);
1313 reset_blkdev_cxt(&part_cxt
);
1317 DBG(CXT
, ul_debugobj(wholedisk_cxt
, "probe whole-disk for partitions -- done"));
1322 static int get_wholedisk_from_partition_dirent(DIR *dir
,
1323 struct dirent
*d
, struct blkdev_cxt
*cxt
)
1325 char path
[PATH_MAX
];
1329 if ((len
= readlinkat(dirfd(dir
), d
->d_name
, path
, sizeof(path
) - 1)) < 0)
1334 /* The path ends with ".../<device>/<partition>" */
1335 p
= strrchr(path
, '/');
1340 p
= strrchr(path
, '/');
1345 return set_cxt(cxt
, NULL
, NULL
, p
);
1349 * List device dependencies: partitions, holders (inverse = 0) or slaves (inverse = 1).
1351 static int list_deps(struct blkdev_cxt
*cxt
)
1355 struct blkdev_cxt dep
= { 0 };
1356 const char *depname
;
1363 DBG(CXT
, ul_debugobj(cxt
, "%s: list dependencies", cxt
->name
));
1365 if (!(lsblk
->inverse
? cxt
->nslaves
: cxt
->nholders
))
1368 depname
= lsblk
->inverse
? "slaves" : "holders";
1369 dir
= sysfs_opendir(&cxt
->sysfs
, depname
);
1373 DBG(CXT
, ul_debugobj(cxt
, "%s: checking for '%s' dependence", cxt
->name
, depname
));
1375 while ((d
= xreaddir(dir
))) {
1376 /* Is the dependency a partition? */
1377 if (sysfs_is_partition_dirent(dir
, d
, NULL
)) {
1378 if (!get_wholedisk_from_partition_dirent(dir
, d
, &dep
)) {
1379 DBG(CXT
, ul_debugobj(cxt
, "%s: %s: dependence is partition",
1380 cxt
->name
, d
->d_name
));
1381 process_blkdev(&dep
, cxt
, 1, d
->d_name
);
1384 /* The dependency is a whole device. */
1385 else if (!set_cxt(&dep
, cxt
, NULL
, d
->d_name
)) {
1386 DBG(CXT
, ul_debugobj(cxt
, "%s: %s: dependence is whole-disk",
1387 cxt
->name
, d
->d_name
));
1388 /* For inverse tree we don't want to show partitions
1389 * if the dependence is pn whle-disk */
1390 process_blkdev(&dep
, cxt
, lsblk
->inverse
? 0 : 1, NULL
);
1392 reset_blkdev_cxt(&dep
);
1396 DBG(CXT
, ul_debugobj(cxt
, "%s: checking for '%s' -- done", cxt
->name
, depname
));
1400 static int process_blkdev(struct blkdev_cxt
*cxt
, struct blkdev_cxt
*parent
,
1401 int do_partitions
, const char *part_name
)
1403 if (do_partitions
&& cxt
->npartitions
)
1404 list_partitions(cxt
, parent
, part_name
); /* partitoins + whole-disk */
1406 fill_table_line(cxt
, parent
? parent
->scols_line
: NULL
); /* whole-disk only */
1408 return list_deps(cxt
);
1411 /* Iterate devices in sysfs */
1412 static int iterate_block_devices(void)
1416 struct blkdev_cxt cxt
= { 0 };
1418 if (!(dir
= opendir(_PATH_SYS_BLOCK
)))
1421 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_BLOCK
));
1423 while ((d
= xreaddir(dir
))) {
1425 DBG(DEV
, ul_debug(" %s dentry", d
->d_name
));
1427 if (set_cxt(&cxt
, NULL
, NULL
, d
->d_name
))
1430 if (is_maj_excluded(cxt
.maj
) || !is_maj_included(cxt
.maj
))
1433 /* Skip devices in the middle of dependency tree. */
1434 if ((lsblk
->inverse
? cxt
.nholders
: cxt
.nslaves
) > 0)
1437 process_blkdev(&cxt
, NULL
, 1, NULL
);
1439 reset_blkdev_cxt(&cxt
);
1444 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_BLOCK
" -- done"));
1448 static char *devno_to_sysfs_name(dev_t devno
, char *devname
, char *buf
, size_t buf_size
)
1450 char path
[PATH_MAX
];
1453 if (!sysfs_devno_path(devno
, path
, sizeof(path
))) {
1454 warn(_("%s: failed to compose sysfs path"), devname
);
1458 len
= readlink(path
, buf
, buf_size
- 1);
1460 warn(_("%s: failed to read link"), path
);
1465 return xstrdup(strrchr(buf
, '/') + 1);
1468 static int process_one_device(char *devname
)
1470 struct blkdev_cxt parent
= { 0 }, cxt
= { 0 };
1472 char buf
[PATH_MAX
+ 1], *name
= NULL
, *diskname
= NULL
;
1474 int real_part
= 0, rc
= -EINVAL
;
1476 if (stat(devname
, &st
) || !S_ISBLK(st
.st_mode
)) {
1477 warnx(_("%s: not a block device"), devname
);
1481 if (!(name
= devno_to_sysfs_name(st
.st_rdev
, devname
, buf
, PATH_MAX
))) {
1482 warn(_("%s: failed to get sysfs name"), devname
);
1486 if (!strncmp(name
, "dm-", 3)) {
1487 /* dm mapping is never a real partition! */
1490 if (blkid_devno_to_wholedisk(st
.st_rdev
, buf
, sizeof(buf
), &disk
)) {
1491 warn(_("%s: failed to get whole-disk device number"), devname
);
1495 real_part
= st
.st_rdev
!= disk
;
1500 * Device is not a partition.
1502 if (set_cxt(&cxt
, NULL
, NULL
, name
))
1504 process_blkdev(&cxt
, NULL
, !lsblk
->inverse
, NULL
);
1507 * Partition, read sysfs name of the device.
1509 if (set_cxt(&parent
, NULL
, NULL
, diskname
))
1511 if (set_cxt(&cxt
, &parent
, &parent
, name
))
1515 process_blkdev(&parent
, &cxt
, 1, cxt
.name
);
1517 process_blkdev(&cxt
, &parent
, 1, NULL
);
1523 reset_blkdev_cxt(&cxt
);
1526 reset_blkdev_cxt(&parent
);
1531 static void parse_excludes(const char *str0
)
1533 const char *str
= str0
;
1535 while (str
&& *str
) {
1540 n
= strtoul(str
, &end
, 10);
1542 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1543 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1544 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1545 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1546 excludes
[nexcludes
++] = n
;
1548 if (nexcludes
== ARRAY_SIZE(excludes
))
1549 /* TRANSLATORS: The standard value for %d is 256. */
1550 errx(EXIT_FAILURE
, _("the list of excluded devices is "
1551 "too large (limit is %d devices)"),
1552 (int)ARRAY_SIZE(excludes
));
1554 str
= end
&& *end
? end
+ 1 : NULL
;
1558 static void parse_includes(const char *str0
)
1560 const char *str
= str0
;
1562 while (str
&& *str
) {
1567 n
= strtoul(str
, &end
, 10);
1569 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1570 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1571 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1572 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1573 includes
[nincludes
++] = n
;
1575 if (nincludes
== ARRAY_SIZE(includes
))
1576 /* TRANSLATORS: The standard value for %d is 256. */
1577 errx(EXIT_FAILURE
, _("the list of included devices is "
1578 "too large (limit is %d devices)"),
1579 (int)ARRAY_SIZE(includes
));
1580 str
= end
&& *end
? end
+ 1 : NULL
;
1585 * see set_sortdata_u64() and columns initialization in main()
1587 static int cmp_u64_cells(struct libscols_cell
*a
,
1588 struct libscols_cell
*b
,
1589 __attribute__((__unused__
)) void *data
)
1591 uint64_t *adata
= (uint64_t *) scols_cell_get_userdata(a
),
1592 *bdata
= (uint64_t *) scols_cell_get_userdata(b
);
1594 if (adata
== NULL
&& bdata
== NULL
)
1600 return *adata
== *bdata
? 0 : *adata
>= *bdata
? 1 : -1;
1603 static void __attribute__((__noreturn__
)) help(FILE *out
)
1607 fputs(USAGE_HEADER
, out
);
1608 fprintf(out
, _(" %s [options] [<device> ...]\n"), program_invocation_short_name
);
1610 fputs(USAGE_SEPARATOR
, out
);
1611 fputs(_("List information about block devices.\n"), out
);
1613 fputs(USAGE_OPTIONS
, out
);
1614 fputs(_(" -a, --all print all devices\n"), out
);
1615 fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out
);
1616 fputs(_(" -d, --nodeps don't print slaves or holders\n"), out
);
1617 fputs(_(" -D, --discard print discard capabilities\n"), out
);
1618 fputs(_(" -e, --exclude <list> exclude devices by major number (default: RAM disks)\n"), out
);
1619 fputs(_(" -f, --fs output info about filesystems\n"), out
);
1620 fputs(_(" -i, --ascii use ascii characters only\n"), out
);
1621 fputs(_(" -I, --include <list> show only devices with specified major numbers\n"), out
);
1622 fputs(_(" -J, --json use JSON output format\n"), out
);
1623 fputs(_(" -l, --list use list format output\n"), out
);
1624 fputs(_(" -m, --perms output info about permissions\n"), out
);
1625 fputs(_(" -n, --noheadings don't print headings\n"), out
);
1626 fputs(_(" -o, --output <list> output columns\n"), out
);
1627 fputs(_(" -O, --output-all output all columns\n"), out
);
1628 fputs(_(" -p, --paths print complete device path\n"), out
);
1629 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out
);
1630 fputs(_(" -r, --raw use raw output format\n"), out
);
1631 fputs(_(" -s, --inverse inverse dependencies\n"), out
);
1632 fputs(_(" -S, --scsi output info about SCSI devices\n"), out
);
1633 fputs(_(" -t, --topology output info about topology\n"), out
);
1634 fputs(_(" -x, --sort <column> sort output by <column>\n"), out
);
1635 fputs(USAGE_SEPARATOR
, out
);
1636 fputs(USAGE_HELP
, out
);
1637 fputs(USAGE_VERSION
, out
);
1639 fprintf(out
, _("\nAvailable columns (for --output):\n"));
1641 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
1642 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
1644 fprintf(out
, USAGE_MAN_TAIL("lsblk(8)"));
1646 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
1649 static void check_sysdevblock(void)
1651 if (access(_PATH_SYS_DEVBLOCK
, R_OK
) != 0)
1652 err(EXIT_FAILURE
, _("failed to access sysfs directory: %s"),
1653 _PATH_SYS_DEVBLOCK
);
1656 int main(int argc
, char *argv
[])
1658 struct lsblk _ls
= { .sort_id
= -1 };
1659 int scols_flags
= LSBLK_TREE
;
1660 int c
, status
= EXIT_FAILURE
;
1661 char *outarg
= NULL
;
1664 static const struct option longopts
[] = {
1665 { "all", 0, 0, 'a' },
1666 { "bytes", 0, 0, 'b' },
1667 { "nodeps", 0, 0, 'd' },
1668 { "discard", 0, 0, 'D' },
1669 { "help", 0, 0, 'h' },
1670 { "json", 0, 0, 'J' },
1671 { "output", 1, 0, 'o' },
1672 { "output-all", 0, 0, 'O' },
1673 { "perms", 0, 0, 'm' },
1674 { "noheadings", 0, 0, 'n' },
1675 { "list", 0, 0, 'l' },
1676 { "ascii", 0, 0, 'i' },
1677 { "raw", 0, 0, 'r' },
1678 { "inverse", 0, 0, 's' },
1679 { "fs", 0, 0, 'f' },
1680 { "exclude", 1, 0, 'e' },
1681 { "include", 1, 0, 'I' },
1682 { "topology", 0, 0, 't' },
1683 { "paths", 0, 0, 'p' },
1684 { "pairs", 0, 0, 'P' },
1685 { "scsi", 0, 0, 'S' },
1686 { "sort", 1, 0, 'x' },
1687 { "version", 0, 0, 'V' },
1691 static const ul_excl_t excl
[] = { /* rows and cols in in ASCII order */
1702 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1704 setlocale(LC_ALL
, "");
1705 bindtextdomain(PACKAGE
, LOCALEDIR
);
1706 textdomain(PACKAGE
);
1707 atexit(close_stdout
);
1713 while((c
= getopt_long(argc
, argv
,
1714 "abdDe:fhJlnmo:OpPiI:rstVSx:", longopts
, NULL
)) != -1) {
1716 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1720 lsblk
->all_devices
= 1;
1729 add_column(columns
, ncolumns
++, COL_NAME
);
1730 add_column(columns
, ncolumns
++, COL_DALIGN
);
1731 add_column(columns
, ncolumns
++, COL_DGRAN
);
1732 add_column(columns
, ncolumns
++, COL_DMAX
);
1733 add_column(columns
, ncolumns
++, COL_DZERO
);
1736 parse_excludes(optarg
);
1742 scols_flags
|= LSBLK_JSON
;
1745 scols_flags
&= ~LSBLK_TREE
; /* disable the default */
1748 scols_flags
|= LSBLK_NOHEADINGS
;
1754 for (ncolumns
= 0 ; ncolumns
< ARRAY_SIZE(infos
); ncolumns
++)
1755 columns
[ncolumns
] = ncolumns
;
1761 scols_flags
|= LSBLK_EXPORT
;
1762 scols_flags
&= ~LSBLK_TREE
; /* disable the default */
1765 scols_flags
|= LSBLK_ASCII
;
1768 parse_includes(optarg
);
1771 scols_flags
&= ~LSBLK_TREE
; /* disable the default */
1772 scols_flags
|= LSBLK_RAW
; /* enable raw */
1778 add_column(columns
, ncolumns
++, COL_NAME
);
1779 add_column(columns
, ncolumns
++, COL_FSTYPE
);
1780 add_column(columns
, ncolumns
++, COL_LABEL
);
1781 add_column(columns
, ncolumns
++, COL_UUID
);
1782 add_column(columns
, ncolumns
++, COL_TARGET
);
1785 add_column(columns
, ncolumns
++, COL_NAME
);
1786 add_column(columns
, ncolumns
++, COL_SIZE
);
1787 add_column(columns
, ncolumns
++, COL_OWNER
);
1788 add_column(columns
, ncolumns
++, COL_GROUP
);
1789 add_column(columns
, ncolumns
++, COL_MODE
);
1792 add_column(columns
, ncolumns
++, COL_NAME
);
1793 add_column(columns
, ncolumns
++, COL_ALIOFF
);
1794 add_column(columns
, ncolumns
++, COL_MINIO
);
1795 add_column(columns
, ncolumns
++, COL_OPTIO
);
1796 add_column(columns
, ncolumns
++, COL_PHYSEC
);
1797 add_column(columns
, ncolumns
++, COL_LOGSEC
);
1798 add_column(columns
, ncolumns
++, COL_ROTA
);
1799 add_column(columns
, ncolumns
++, COL_SCHED
);
1800 add_column(columns
, ncolumns
++, COL_RQ_SIZE
);
1801 add_column(columns
, ncolumns
++, COL_RA
);
1802 add_column(columns
, ncolumns
++, COL_WSAME
);
1807 add_column(columns
, ncolumns
++, COL_NAME
);
1808 add_column(columns
, ncolumns
++, COL_HCTL
);
1809 add_column(columns
, ncolumns
++, COL_TYPE
);
1810 add_column(columns
, ncolumns
++, COL_VENDOR
);
1811 add_column(columns
, ncolumns
++, COL_MODEL
);
1812 add_column(columns
, ncolumns
++, COL_REV
);
1813 add_column(columns
, ncolumns
++, COL_TRANSPORT
);
1816 printf(UTIL_LINUX_VERSION
);
1817 return EXIT_SUCCESS
;
1819 scols_flags
&= ~LSBLK_TREE
; /* disable the default */
1820 lsblk
->sort_id
= column_name_to_id(optarg
, strlen(optarg
));
1821 if (lsblk
->sort_id
>= 0)
1829 check_sysdevblock();
1832 add_column(columns
, ncolumns
++, COL_NAME
);
1833 add_column(columns
, ncolumns
++, COL_MAJMIN
);
1834 add_column(columns
, ncolumns
++, COL_RM
);
1835 add_column(columns
, ncolumns
++, COL_SIZE
);
1836 add_column(columns
, ncolumns
++, COL_RO
);
1837 add_column(columns
, ncolumns
++, COL_TYPE
);
1838 add_column(columns
, ncolumns
++, COL_TARGET
);
1841 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
1842 &ncolumns
, column_name_to_id
) < 0)
1843 return EXIT_FAILURE
;
1845 if (nexcludes
== 0 && nincludes
== 0)
1846 excludes
[nexcludes
++] = 1; /* default: ignore RAM disks */
1848 if (lsblk
->sort_id
>= 0 && column_id_to_number(lsblk
->sort_id
) < 0) {
1849 /* the sort column is not between output columns -- add as hidden */
1850 add_column(columns
, ncolumns
++, lsblk
->sort_id
);
1851 lsblk
->sort_hidden
= 1;
1855 scols_init_debug(0);
1858 * initialize output columns
1860 if (!(lsblk
->table
= scols_new_table()))
1861 errx(EXIT_FAILURE
, _("failed to initialize output table"));
1862 scols_table_enable_raw(lsblk
->table
, !!(scols_flags
& LSBLK_RAW
));
1863 scols_table_enable_export(lsblk
->table
, !!(scols_flags
& LSBLK_EXPORT
));
1864 scols_table_enable_ascii(lsblk
->table
, !!(scols_flags
& LSBLK_ASCII
));
1865 scols_table_enable_json(lsblk
->table
, !!(scols_flags
& LSBLK_JSON
));
1866 scols_table_enable_noheadings(lsblk
->table
, !!(scols_flags
& LSBLK_NOHEADINGS
));
1868 if (scols_flags
& LSBLK_JSON
)
1869 scols_table_set_name(lsblk
->table
, "blockdevices");
1871 for (i
= 0; i
< ncolumns
; i
++) {
1872 struct colinfo
*ci
= get_column_info(i
);
1873 struct libscols_column
*cl
;
1874 int id
= get_column_id(i
), fl
= ci
->flags
;
1876 if (!(scols_flags
& LSBLK_TREE
) && id
== COL_NAME
)
1877 fl
&= ~SCOLS_FL_TREE
;
1878 if (lsblk
->sort_hidden
&& lsblk
->sort_id
== id
)
1879 fl
|= SCOLS_FL_HIDDEN
;
1881 cl
= scols_table_new_column(lsblk
->table
, ci
->name
, ci
->whint
, fl
);
1883 warn(_("failed to initialize output column"));
1886 if (!lsblk
->sort_col
&& lsblk
->sort_id
== id
) {
1887 lsblk
->sort_col
= cl
;
1888 scols_column_set_cmpfunc(cl
,
1889 ci
->sort_type
== SORT_STRING
?
1890 scols_cmpstr_cells
: cmp_u64_cells
, NULL
);
1895 status
= iterate_block_devices() == 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
1897 int cnt
= 0, cnt_err
= 0;
1899 while (optind
< argc
) {
1900 if (process_one_device(argv
[optind
++]) != 0)
1904 status
= cnt
== 0 ? EXIT_FAILURE
: /* nothing */
1905 cnt
== cnt_err
? LSBLK_EXIT_ALLFAILED
:/* all failed */
1906 cnt_err
? LSBLK_EXIT_SOMEOK
: /* some ok */
1907 EXIT_SUCCESS
; /* all success */
1910 if (lsblk
->sort_col
)
1911 scols_sort_table(lsblk
->table
, lsblk
->sort_col
);
1913 scols_print_table(lsblk
->table
);
1916 if (lsblk
->sort_col
)
1917 unref_sortdata(lsblk
->table
);
1919 scols_unref_table(lsblk
->table
);
1921 mnt_unref_table(mtab
);
1922 mnt_unref_table(swaps
);
1923 mnt_unref_cache(mntcache
);