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
; /* partition type UUID */
273 char *partuuid
; /* partition UUID */
274 char *partlabel
; /* partition 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 excluded */
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 at line %d -- ignored"), filename
, line
);
437 static int is_active_swap(const char *filename
)
440 swaps
= mnt_new_table();
444 mntcache
= mnt_new_cache();
446 mnt_table_set_parser_errcb(swaps
, table_parser_errcb
);
447 mnt_table_set_cache(swaps
, mntcache
);
448 mnt_table_parse_swaps(swaps
, NULL
);
451 return mnt_table_find_srcpath(swaps
, filename
, MNT_ITER_BACKWARD
) != 0;
454 static char *get_device_mountpoint(struct blkdev_cxt
*cxt
)
456 struct libmnt_fs
*fs
;
460 assert(cxt
->filename
);
463 mtab
= mnt_new_table();
467 mntcache
= mnt_new_cache();
469 mnt_table_set_parser_errcb(mtab
, table_parser_errcb
);
470 mnt_table_set_cache(mtab
, mntcache
);
471 mnt_table_parse_mtab(mtab
, NULL
);
474 /* Note that maj:min in /proc/self/mountinfo does not have to match with
475 * devno as returned by stat(), so we have to try devname too
477 fs
= mnt_table_find_devno(mtab
, makedev(cxt
->maj
, cxt
->min
), MNT_ITER_BACKWARD
);
479 fs
= mnt_table_find_srcpath(mtab
, cxt
->filename
, MNT_ITER_BACKWARD
);
481 return is_active_swap(cxt
->filename
) ? xstrdup("[SWAP]") : NULL
;
484 fsroot
= mnt_fs_get_root(fs
);
485 if (fsroot
&& strcmp(fsroot
, "/") != 0) {
486 /* hmm.. we found bind mount or btrfs subvolume, let's try to
487 * get real FS root mountpoint */
488 struct libmnt_fs
*rfs
;
489 struct libmnt_iter
*itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
491 mnt_table_set_iter(mtab
, itr
, fs
);
492 while (mnt_table_next_fs(mtab
, itr
, &rfs
) == 0) {
493 fsroot
= mnt_fs_get_root(rfs
);
494 if ((!fsroot
|| strcmp(fsroot
, "/") == 0)
495 && mnt_fs_match_source(rfs
, cxt
->filename
, mntcache
)) {
503 DBG(DEV
, ul_debugobj(cxt
, "mountpoint: %s", mnt_fs_get_target(fs
)));
504 return xstrdup(mnt_fs_get_target(fs
));
508 static int get_udev_properties(struct blkdev_cxt
*cxt
509 __attribute__((__unused__
)))
514 static int get_udev_properties(struct blkdev_cxt
*cxt
)
516 struct udev_device
*dev
;
519 return 0; /* already done */
526 dev
= udev_device_new_from_subsystem_sysname(udev
, "block", cxt
->name
);
530 if ((data
= udev_device_get_property_value(dev
, "ID_FS_LABEL_ENC"))) {
531 cxt
->label
= xstrdup(data
);
532 unhexmangle_string(cxt
->label
);
534 if ((data
= udev_device_get_property_value(dev
, "ID_FS_UUID_ENC"))) {
535 cxt
->uuid
= xstrdup(data
);
536 unhexmangle_string(cxt
->uuid
);
538 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_NAME"))) {
539 cxt
->partlabel
= xstrdup(data
);
540 unhexmangle_string(cxt
->partlabel
);
542 if ((data
= udev_device_get_property_value(dev
, "ID_FS_TYPE")))
543 cxt
->fstype
= xstrdup(data
);
544 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_TYPE")))
545 cxt
->parttype
= xstrdup(data
);
546 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_UUID")))
547 cxt
->partuuid
= xstrdup(data
);
548 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_FLAGS")))
549 cxt
->partflags
= xstrdup(data
);
551 data
= udev_device_get_property_value(dev
, "ID_WWN_WITH_EXTENSION");
553 data
= udev_device_get_property_value(dev
, "ID_WWN");
555 cxt
->wwn
= xstrdup(data
);
557 if ((data
= udev_device_get_property_value(dev
, "ID_SERIAL_SHORT")))
558 cxt
->serial
= xstrdup(data
);
559 udev_device_unref(dev
);
561 DBG(DEV
, ul_debugobj(cxt
, "%s: found udev properties", cxt
->name
));
564 return cxt
->probed
== 1 ? 0 : -1;
567 #endif /* HAVE_LIBUDEV */
569 static void probe_device(struct blkdev_cxt
*cxt
)
571 blkid_probe pr
= NULL
;
580 if (get_udev_properties(cxt
) == 0)
581 return; /* success */
585 /* try libblkid (fallback) */
587 return; /* no permissions to read from the device */
589 pr
= blkid_new_probe_from_filename(cxt
->filename
);
593 blkid_probe_enable_superblocks(pr
, 1);
594 blkid_probe_set_superblocks_flags(pr
, BLKID_SUBLKS_LABEL
|
597 blkid_probe_enable_partitions(pr
, 1);
598 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
600 if (!blkid_do_safeprobe(pr
)) {
601 const char *data
= NULL
;
603 if (!blkid_probe_lookup_value(pr
, "TYPE", &data
, NULL
))
604 cxt
->fstype
= xstrdup(data
);
605 if (!blkid_probe_lookup_value(pr
, "UUID", &data
, NULL
))
606 cxt
->uuid
= xstrdup(data
);
607 if (!blkid_probe_lookup_value(pr
, "LABEL", &data
, NULL
))
608 cxt
->label
= xstrdup(data
);
609 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_TYPE", &data
, NULL
))
610 cxt
->parttype
= xstrdup(data
);
611 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_UUID", &data
, NULL
))
612 cxt
->partuuid
= xstrdup(data
);
613 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_NAME", &data
, NULL
))
614 cxt
->partlabel
= xstrdup(data
);
615 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_FLAGS", &data
, NULL
))
616 cxt
->partflags
= xstrdup(data
);
617 DBG(DEV
, ul_debugobj(cxt
, "%s: found blkid properties", cxt
->name
));
620 blkid_free_probe(pr
);
624 static int is_readonly_device(struct blkdev_cxt
*cxt
)
628 if (sysfs_scanf(&cxt
->sysfs
, "ro", "%d", &ro
) == 1)
631 /* fallback if "ro" attribute does not exist */
632 fd
= open(cxt
->filename
, O_RDONLY
);
634 if (ioctl(fd
, BLKROGET
, &ro
) != 0)
641 static char *get_scheduler(struct blkdev_cxt
*cxt
)
643 char *str
= sysfs_strdup(&cxt
->sysfs
, "queue/scheduler");
644 char *p
, *res
= NULL
;
648 p
= strchr(str
, '[');
651 p
= strchr(res
, ']');
662 static char *get_type(struct blkdev_cxt
*cxt
)
664 char *res
= NULL
, *p
;
666 if (is_dm(cxt
->name
)) {
667 char *dm_uuid
= sysfs_strdup(&cxt
->sysfs
, "dm/uuid");
669 /* The DM_UUID prefix should be set to subsystem owning
670 * the device - LVM, CRYPT, DMRAID, MPATH, PART */
673 char *dm_uuid_prefix
= strsep(&tmp
, "-");
675 if (dm_uuid_prefix
) {
676 /* kpartx hack to remove partition number */
677 if (strncasecmp(dm_uuid_prefix
, "part", 4) == 0)
678 dm_uuid_prefix
[4] = '\0';
680 res
= xstrdup(dm_uuid_prefix
);
686 /* No UUID or no prefix - just mark it as DM device */
689 } else if (!strncmp(cxt
->name
, "loop", 4)) {
690 res
= xstrdup("loop");
692 } else if (!strncmp(cxt
->name
, "md", 2)) {
693 char *md_level
= sysfs_strdup(&cxt
->sysfs
, "md/level");
694 res
= md_level
? md_level
: xstrdup("md");
697 const char *type
= NULL
;
700 if (!sysfs_read_int(&cxt
->sysfs
, "device/type", &x
))
701 type
= blkdev_scsi_type_to_name(x
);
704 type
= cxt
->partition
? "part" : "disk";
708 for (p
= res
; p
&& *p
; p
++)
709 *p
= tolower((unsigned char) *p
);
713 /* Thanks to lsscsi code for idea of detection logic used here */
714 static char *get_transport(struct blkdev_cxt
*cxt
)
716 struct sysfs_cxt
*sysfs
= &cxt
->sysfs
;
718 const char *trans
= NULL
;
720 /* SCSI - Serial Peripheral Interface */
721 if (sysfs_scsi_host_is(sysfs
, "spi"))
724 /* FC/FCoE - Fibre Channel / Fibre Channel over Ethernet */
725 else if (sysfs_scsi_host_is(sysfs
, "fc")) {
726 attr
= sysfs_scsi_host_strdup_attribute(sysfs
, "fc", "symbolic_name");
729 trans
= strstr(attr
, " over ") ? "fcoe" : "fc";
733 /* SAS - Serial Attached SCSI */
734 else if (sysfs_scsi_host_is(sysfs
, "sas") ||
735 sysfs_scsi_has_attribute(sysfs
, "sas_device"))
739 /* SBP - Serial Bus Protocol (FireWire) */
740 else if (sysfs_scsi_has_attribute(sysfs
, "ieee1394_id"))
744 else if (sysfs_scsi_host_is(sysfs
, "iscsi"))
747 /* USB - Universal Serial Bus */
748 else if (sysfs_scsi_path_contains(sysfs
, "usb"))
752 else if (sysfs_scsi_host_is(sysfs
, "scsi")) {
753 attr
= sysfs_scsi_host_strdup_attribute(sysfs
, "scsi", "proc_name");
756 if (!strncmp(attr
, "ahci", 4) || !strncmp(attr
, "sata", 4))
758 else if (strstr(attr
, "ata"))
762 } else if (strncmp(cxt
->name
, "nvme", 4) == 0)
765 return trans
? xstrdup(trans
) : NULL
;
768 static char *get_subsystems(struct blkdev_cxt
*cxt
)
771 char *sub
, *chain
, *res
= NULL
;
772 size_t len
= 0, last
= 0;
774 chain
= sysfs_get_devchain(&cxt
->sysfs
, path
, sizeof(path
));
778 while (sysfs_next_subsystem(&cxt
->sysfs
, chain
, &sub
) == 0) {
781 /* don't create "block:scsi:scsi", but "block:scsi" */
782 if (len
&& strcmp(res
+ last
, sub
) == 0) {
788 res
= xrealloc(res
, len
+ sz
+ 2);
792 memcpy(res
+ len
, sub
, sz
+ 1);
802 #define is_parsable(_l) (scols_table_is_raw((_l)->table) || \
803 scols_table_is_export((_l)->table) || \
804 scols_table_is_json((_l)->table))
806 static char *mk_name(const char *name
)
812 xasprintf(&p
, "/dev/%s", name
);
816 sysfs_devname_sys_to_dev(p
);
820 static char *mk_dm_name(const char *name
)
826 xasprintf(&p
, "/dev/mapper/%s", name
);
832 /* stores data to scols cell userdata (invisible and independent on output)
833 * to make the original values accessible for sort functions
835 static void set_sortdata_u64(struct libscols_line
*ln
, int col
, uint64_t x
)
837 struct libscols_cell
*ce
= scols_line_get_cell(ln
, col
);
842 data
= xmalloc(sizeof(uint64_t));
844 scols_cell_set_userdata(ce
, data
);
847 static void set_sortdata_u64_from_string(struct libscols_line
*ln
, int col
, const char *str
)
851 if (!str
|| sscanf(str
, "%"SCNu64
, &x
) != 1)
854 set_sortdata_u64(ln
, col
, x
);
857 static void unref_sortdata(struct libscols_table
*tb
)
859 struct libscols_iter
*itr
;
860 struct libscols_line
*ln
;
862 if (!tb
|| !lsblk
->sort_col
)
864 itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
867 while (scols_table_next_line(tb
, itr
, &ln
) == 0) {
868 struct libscols_cell
*ce
= scols_line_get_column_cell(ln
,
870 void *data
= scols_cell_get_userdata(ce
);
874 scols_free_iter(itr
);
877 static void set_scols_data(struct blkdev_cxt
*cxt
, int col
, int id
, struct libscols_line
*ln
)
879 int sort
= 0, st_rc
= 0;
882 if (!cxt
->st
.st_rdev
&& (id
== COL_OWNER
|| id
== COL_GROUP
||
884 st_rc
= stat(cxt
->filename
, &cxt
->st
);
886 if (lsblk
->sort_id
== id
)
891 str
= cxt
->dm_name
? mk_dm_name(cxt
->dm_name
) : mk_name(cxt
->name
);
894 str
= mk_name(cxt
->name
);
898 str
= mk_name(cxt
->parent
->name
);
902 struct passwd
*pw
= st_rc
? NULL
: getpwuid(cxt
->st
.st_uid
);
904 str
= xstrdup(pw
->pw_name
);
909 struct group
*gr
= st_rc
? NULL
: getgrgid(cxt
->st
.st_gid
);
911 str
= xstrdup(gr
->gr_name
);
919 xstrmode(cxt
->st
.st_mode
, md
);
925 if (is_parsable(lsblk
))
926 xasprintf(&str
, "%u:%u", cxt
->maj
, cxt
->min
);
928 xasprintf(&str
, "%3u:%-3u", cxt
->maj
, cxt
->min
);
930 set_sortdata_u64(ln
, col
, makedev(cxt
->maj
, cxt
->min
));
935 str
= xstrdup(cxt
->fstype
);
938 str
= get_device_mountpoint(cxt
);
943 str
= xstrdup(cxt
->label
);
948 str
= xstrdup(cxt
->uuid
);
953 str
= xstrdup(cxt
->parttype
);
958 str
= xstrdup(cxt
->partlabel
);
963 str
= xstrdup(cxt
->partuuid
);
968 str
= xstrdup(cxt
->partflags
);
971 get_udev_properties(cxt
);
973 str
= xstrdup(cxt
->wwn
);
976 str
= sysfs_strdup(&cxt
->sysfs
, "queue/read_ahead_kb");
978 set_sortdata_u64_from_string(ln
, col
, str
);
981 str
= xstrdup(is_readonly_device(cxt
) ? "1" : "0");
984 str
= sysfs_strdup(&cxt
->sysfs
, "removable");
985 if (!str
&& cxt
->sysfs
.parent
)
986 str
= sysfs_strdup(cxt
->sysfs
.parent
, "removable");
989 str
= sysfs_is_hotpluggable(&cxt
->sysfs
) ? xstrdup("1") : xstrdup("0");
992 str
= sysfs_strdup(&cxt
->sysfs
, "queue/rotational");
995 str
= sysfs_strdup(&cxt
->sysfs
, "queue/add_random");
998 if (!cxt
->partition
&& cxt
->nslaves
== 0)
999 str
= sysfs_strdup(&cxt
->sysfs
, "device/model");
1002 if (!cxt
->partition
&& cxt
->nslaves
== 0) {
1003 get_udev_properties(cxt
);
1005 str
= xstrdup(cxt
->serial
);
1007 str
= sysfs_strdup(&cxt
->sysfs
, "device/serial");
1011 if (!cxt
->partition
&& cxt
->nslaves
== 0)
1012 str
= sysfs_strdup(&cxt
->sysfs
, "device/rev");
1015 if (!cxt
->partition
&& cxt
->nslaves
== 0)
1016 str
= sysfs_strdup(&cxt
->sysfs
, "device/vendor");
1022 xasprintf(&str
, "%ju", cxt
->size
);
1024 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, cxt
->size
);
1026 set_sortdata_u64(ln
, col
, cxt
->size
);
1029 if (!cxt
->partition
&& !cxt
->dm_name
)
1030 str
= sysfs_strdup(&cxt
->sysfs
, "device/state");
1031 else if (cxt
->dm_name
) {
1033 if (sysfs_read_int(&cxt
->sysfs
, "dm/suspended", &x
) == 0)
1034 str
= xstrdup(x
? "suspended" : "running");
1038 str
= sysfs_strdup(&cxt
->sysfs
, "alignment_offset");
1040 set_sortdata_u64_from_string(ln
, col
, str
);
1043 str
= sysfs_strdup(&cxt
->sysfs
, "queue/minimum_io_size");
1045 set_sortdata_u64_from_string(ln
, col
, str
);
1048 str
= sysfs_strdup(&cxt
->sysfs
, "queue/optimal_io_size");
1050 set_sortdata_u64_from_string(ln
, col
, str
);
1053 str
= sysfs_strdup(&cxt
->sysfs
, "queue/physical_block_size");
1055 set_sortdata_u64_from_string(ln
, col
, str
);
1058 str
= sysfs_strdup(&cxt
->sysfs
, "queue/logical_block_size");
1060 set_sortdata_u64_from_string(ln
, col
, str
);
1063 str
= get_scheduler(cxt
);
1066 str
= sysfs_strdup(&cxt
->sysfs
, "queue/nr_requests");
1068 set_sortdata_u64_from_string(ln
, col
, str
);
1071 str
= get_type(cxt
);
1076 if (sysfs_scsi_get_hctl(&cxt
->sysfs
, &h
, &c
, &t
, &l
) == 0)
1077 xasprintf(&str
, "%d:%d:%d:%d", h
, c
, t
, l
);
1081 str
= get_transport(cxt
);
1084 str
= get_subsystems(cxt
);
1088 str
= sysfs_strdup(&cxt
->sysfs
, "discard_alignment");
1092 set_sortdata_u64_from_string(ln
, col
, str
);
1096 str
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_granularity");
1098 set_sortdata_u64_from_string(ln
, col
, str
);
1101 if (sysfs_read_u64(&cxt
->sysfs
,
1102 "queue/discard_granularity", &x
) == 0) {
1103 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1105 set_sortdata_u64(ln
, col
, x
);
1111 str
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_max_bytes");
1113 set_sortdata_u64_from_string(ln
, col
, str
);
1116 if (sysfs_read_u64(&cxt
->sysfs
,
1117 "queue/discard_max_bytes", &x
) == 0) {
1118 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1120 set_sortdata_u64(ln
, col
, x
);
1126 str
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_zeroes_data");
1132 str
= sysfs_strdup(&cxt
->sysfs
, "queue/write_same_max_bytes");
1134 set_sortdata_u64_from_string(ln
, col
, str
);
1138 if (sysfs_read_u64(&cxt
->sysfs
,
1139 "queue/write_same_max_bytes", &x
) == 0) {
1140 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1142 set_sortdata_u64(ln
, col
, x
);
1151 scols_line_refer_data(ln
, col
, str
);
1154 static void fill_table_line(struct blkdev_cxt
*cxt
, struct libscols_line
*scols_parent
)
1158 cxt
->scols_line
= scols_table_new_line(lsblk
->table
, scols_parent
);
1159 if (!cxt
->scols_line
)
1162 for (i
= 0; i
< ncolumns
; i
++)
1163 set_scols_data(cxt
, i
, get_column_id(i
), cxt
->scols_line
);
1166 static int set_cxt(struct blkdev_cxt
*cxt
,
1167 struct blkdev_cxt
*parent
,
1168 struct blkdev_cxt
*wholedisk
,
1173 DBG(CXT
, ul_debugobj(cxt
, "setting context for %s [parent=%p, wholedisk=%p]",
1174 name
, parent
, wholedisk
));
1176 cxt
->parent
= parent
;
1177 cxt
->name
= xstrdup(name
);
1178 cxt
->partition
= wholedisk
!= NULL
;
1180 cxt
->filename
= get_device_path(cxt
);
1181 if (!cxt
->filename
) {
1182 DBG(CXT
, ul_debugobj(cxt
, "%s: failed to get device path", cxt
->name
));
1185 DBG(CXT
, ul_debugobj(cxt
, "%s: filename=%s", cxt
->name
, cxt
->filename
));
1187 devno
= sysfs_devname_to_devno(cxt
->name
, wholedisk
? wholedisk
->name
: NULL
);
1190 DBG(CXT
, ul_debugobj(cxt
, "%s: unknown device name", cxt
->name
));
1194 if (lsblk
->inverse
) {
1195 if (sysfs_init(&cxt
->sysfs
, devno
, wholedisk
? &wholedisk
->sysfs
: NULL
)) {
1196 DBG(CXT
, ul_debugobj(cxt
, "%s: failed to initialize sysfs handler", cxt
->name
));
1200 parent
->sysfs
.parent
= &cxt
->sysfs
;
1202 if (sysfs_init(&cxt
->sysfs
, devno
, parent
? &parent
->sysfs
: NULL
)) {
1203 DBG(CXT
, ul_debugobj(cxt
, "%s: failed to initialize sysfs handler", cxt
->name
));
1208 cxt
->maj
= major(devno
);
1209 cxt
->min
= minor(devno
);
1212 if (sysfs_read_u64(&cxt
->sysfs
, "size", &cxt
->size
) == 0) /* in sectors */
1213 cxt
->size
<<= 9; /* in bytes */
1215 if (sysfs_read_int(&cxt
->sysfs
,
1216 "queue/discard_granularity", &cxt
->discard
) != 0)
1219 /* Ignore devices of zero size */
1220 if (!lsblk
->all_devices
&& cxt
->size
== 0) {
1221 DBG(CXT
, ul_debugobj(cxt
, "zero size device -- ignore"));
1224 if (is_dm(cxt
->name
)) {
1225 cxt
->dm_name
= sysfs_strdup(&cxt
->sysfs
, "dm/name");
1226 if (!cxt
->dm_name
) {
1227 DBG(CXT
, ul_debugobj(cxt
, "%s: failed to get dm name", cxt
->name
));
1232 cxt
->npartitions
= sysfs_count_partitions(&cxt
->sysfs
, cxt
->name
);
1233 cxt
->nholders
= sysfs_count_dirents(&cxt
->sysfs
, "holders");
1234 cxt
->nslaves
= sysfs_count_dirents(&cxt
->sysfs
, "slaves");
1236 DBG(CXT
, ul_debugobj(cxt
, "%s: npartitions=%d, nholders=%d, nslaves=%d",
1237 cxt
->name
, cxt
->npartitions
, cxt
->nholders
, cxt
->nslaves
));
1239 /* ignore non-SCSI devices */
1240 if (lsblk
->scsi
&& sysfs_scsi_get_hctl(&cxt
->sysfs
, NULL
, NULL
, NULL
, NULL
)) {
1241 DBG(CXT
, ul_debugobj(cxt
, "non-scsi device -- ignore"));
1245 DBG(CXT
, ul_debugobj(cxt
, "%s: context successfully initialized", cxt
->name
));
1249 static int process_blkdev(struct blkdev_cxt
*cxt
, struct blkdev_cxt
*parent
,
1250 int do_partitions
, const char *part_name
);
1253 * List device partitions if any.
1255 static int list_partitions(struct blkdev_cxt
*wholedisk_cxt
, struct blkdev_cxt
*parent_cxt
,
1256 const char *part_name
)
1260 struct blkdev_cxt part_cxt
= { 0 };
1263 assert(wholedisk_cxt
);
1266 * Do not process further if there are no partitions for
1267 * this device or the device itself is a partition.
1269 if (!wholedisk_cxt
->npartitions
|| wholedisk_cxt
->partition
)
1272 DBG(CXT
, ul_debugobj(wholedisk_cxt
, "probe whole-disk for partitions"));
1274 dir
= sysfs_opendir(&wholedisk_cxt
->sysfs
, NULL
);
1276 err(EXIT_FAILURE
, _("failed to open device directory in sysfs"));
1278 while ((d
= xreaddir(dir
))) {
1279 /* Process particular partition only? */
1280 if (part_name
&& strcmp(part_name
, d
->d_name
))
1283 if (!(sysfs_is_partition_dirent(dir
, d
, wholedisk_cxt
->name
)))
1286 DBG(CXT
, ul_debugobj(wholedisk_cxt
, " checking %s", d
->d_name
));
1288 if (lsblk
->inverse
) {
1295 if (set_cxt(&part_cxt
, parent_cxt
, wholedisk_cxt
, d
->d_name
))
1298 if (!parent_cxt
&& part_cxt
.nholders
)
1301 wholedisk_cxt
->parent
= &part_cxt
;
1302 fill_table_line(&part_cxt
, parent_cxt
? parent_cxt
->scols_line
: NULL
);
1304 process_blkdev(wholedisk_cxt
, &part_cxt
, 0, NULL
);
1312 int ps
= set_cxt(&part_cxt
, wholedisk_cxt
, wholedisk_cxt
, d
->d_name
);
1314 /* Print whole disk only once */
1316 fill_table_line(wholedisk_cxt
, parent_cxt
? parent_cxt
->scols_line
: NULL
);
1317 if (ps
== 0 && !lsblk
->nodeps
)
1318 process_blkdev(&part_cxt
, wholedisk_cxt
, 0, NULL
);
1321 reset_blkdev_cxt(&part_cxt
);
1325 DBG(CXT
, ul_debugobj(wholedisk_cxt
, "probe whole-disk for partitions -- done"));
1330 static int get_wholedisk_from_partition_dirent(DIR *dir
,
1331 struct dirent
*d
, struct blkdev_cxt
*cxt
)
1333 char path
[PATH_MAX
];
1337 if ((len
= readlinkat(dirfd(dir
), d
->d_name
, path
, sizeof(path
) - 1)) < 0)
1342 /* The path ends with ".../<device>/<partition>" */
1343 p
= strrchr(path
, '/');
1348 p
= strrchr(path
, '/');
1353 return set_cxt(cxt
, NULL
, NULL
, p
);
1357 * List device dependencies: partitions, holders (inverse = 0) or slaves (inverse = 1).
1359 static int list_deps(struct blkdev_cxt
*cxt
)
1363 struct blkdev_cxt dep
= { 0 };
1364 const char *depname
;
1371 DBG(CXT
, ul_debugobj(cxt
, "%s: list dependencies", cxt
->name
));
1373 if (!(lsblk
->inverse
? cxt
->nslaves
: cxt
->nholders
))
1376 depname
= lsblk
->inverse
? "slaves" : "holders";
1377 dir
= sysfs_opendir(&cxt
->sysfs
, depname
);
1381 DBG(CXT
, ul_debugobj(cxt
, "%s: checking for '%s' dependence", cxt
->name
, depname
));
1383 while ((d
= xreaddir(dir
))) {
1384 /* Is the dependency a partition? */
1385 if (sysfs_is_partition_dirent(dir
, d
, NULL
)) {
1386 if (!get_wholedisk_from_partition_dirent(dir
, d
, &dep
)) {
1387 DBG(CXT
, ul_debugobj(cxt
, "%s: %s: dependence is partition",
1388 cxt
->name
, d
->d_name
));
1389 process_blkdev(&dep
, cxt
, 1, d
->d_name
);
1392 /* The dependency is a whole device. */
1393 else if (!set_cxt(&dep
, cxt
, NULL
, d
->d_name
)) {
1394 DBG(CXT
, ul_debugobj(cxt
, "%s: %s: dependence is whole-disk",
1395 cxt
->name
, d
->d_name
));
1396 /* For inverse tree we don't want to show partitions
1397 * if the dependence is on whole-disk */
1398 process_blkdev(&dep
, cxt
, lsblk
->inverse
? 0 : 1, NULL
);
1400 reset_blkdev_cxt(&dep
);
1404 DBG(CXT
, ul_debugobj(cxt
, "%s: checking for '%s' -- done", cxt
->name
, depname
));
1408 static int process_blkdev(struct blkdev_cxt
*cxt
, struct blkdev_cxt
*parent
,
1409 int do_partitions
, const char *part_name
)
1411 if (do_partitions
&& cxt
->npartitions
)
1412 list_partitions(cxt
, parent
, part_name
); /* partitions + whole-disk */
1414 fill_table_line(cxt
, parent
? parent
->scols_line
: NULL
); /* whole-disk only */
1416 return list_deps(cxt
);
1419 /* Iterate devices in sysfs */
1420 static int iterate_block_devices(void)
1424 struct blkdev_cxt cxt
= { 0 };
1426 if (!(dir
= opendir(_PATH_SYS_BLOCK
)))
1429 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_BLOCK
));
1431 while ((d
= xreaddir(dir
))) {
1433 DBG(DEV
, ul_debug(" %s dentry", d
->d_name
));
1435 if (set_cxt(&cxt
, NULL
, NULL
, d
->d_name
))
1438 if (is_maj_excluded(cxt
.maj
) || !is_maj_included(cxt
.maj
))
1441 /* Skip devices in the middle of dependency tree. */
1442 if ((lsblk
->inverse
? cxt
.nholders
: cxt
.nslaves
) > 0)
1445 process_blkdev(&cxt
, NULL
, 1, NULL
);
1447 reset_blkdev_cxt(&cxt
);
1452 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_BLOCK
" -- done"));
1456 static char *devno_to_sysfs_name(dev_t devno
, char *devname
, char *buf
, size_t buf_size
)
1458 char path
[PATH_MAX
];
1461 if (!sysfs_devno_path(devno
, path
, sizeof(path
))) {
1462 warn(_("%s: failed to compose sysfs path"), devname
);
1466 len
= readlink(path
, buf
, buf_size
- 1);
1468 warn(_("%s: failed to read link"), path
);
1473 return xstrdup(strrchr(buf
, '/') + 1);
1476 static int process_one_device(char *devname
)
1478 struct blkdev_cxt parent
= { 0 }, cxt
= { 0 };
1480 char buf
[PATH_MAX
+ 1], *name
= NULL
, *diskname
= NULL
;
1482 int real_part
= 0, rc
= -EINVAL
;
1484 if (stat(devname
, &st
) || !S_ISBLK(st
.st_mode
)) {
1485 warnx(_("%s: not a block device"), devname
);
1489 if (!(name
= devno_to_sysfs_name(st
.st_rdev
, devname
, buf
, PATH_MAX
))) {
1490 warn(_("%s: failed to get sysfs name"), devname
);
1494 if (!strncmp(name
, "dm-", 3)) {
1495 /* dm mapping is never a real partition! */
1498 if (blkid_devno_to_wholedisk(st
.st_rdev
, buf
, sizeof(buf
), &disk
)) {
1499 warn(_("%s: failed to get whole-disk device number"), devname
);
1503 real_part
= st
.st_rdev
!= disk
;
1508 * Device is not a partition.
1510 if (set_cxt(&cxt
, NULL
, NULL
, name
))
1512 process_blkdev(&cxt
, NULL
, !lsblk
->inverse
, NULL
);
1515 * Partition, read sysfs name of the device.
1517 if (set_cxt(&parent
, NULL
, NULL
, diskname
))
1519 if (set_cxt(&cxt
, &parent
, &parent
, name
))
1523 process_blkdev(&parent
, &cxt
, 1, cxt
.name
);
1525 process_blkdev(&cxt
, &parent
, 1, NULL
);
1531 reset_blkdev_cxt(&cxt
);
1534 reset_blkdev_cxt(&parent
);
1539 static void parse_excludes(const char *str0
)
1541 const char *str
= str0
;
1543 while (str
&& *str
) {
1548 n
= strtoul(str
, &end
, 10);
1550 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1551 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1552 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1553 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1554 excludes
[nexcludes
++] = n
;
1556 if (nexcludes
== ARRAY_SIZE(excludes
))
1557 /* TRANSLATORS: The standard value for %d is 256. */
1558 errx(EXIT_FAILURE
, _("the list of excluded devices is "
1559 "too large (limit is %d devices)"),
1560 (int)ARRAY_SIZE(excludes
));
1562 str
= end
&& *end
? end
+ 1 : NULL
;
1566 static void parse_includes(const char *str0
)
1568 const char *str
= str0
;
1570 while (str
&& *str
) {
1575 n
= strtoul(str
, &end
, 10);
1577 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1578 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1579 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1580 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1581 includes
[nincludes
++] = n
;
1583 if (nincludes
== ARRAY_SIZE(includes
))
1584 /* TRANSLATORS: The standard value for %d is 256. */
1585 errx(EXIT_FAILURE
, _("the list of included devices is "
1586 "too large (limit is %d devices)"),
1587 (int)ARRAY_SIZE(includes
));
1588 str
= end
&& *end
? end
+ 1 : NULL
;
1593 * see set_sortdata_u64() and columns initialization in main()
1595 static int cmp_u64_cells(struct libscols_cell
*a
,
1596 struct libscols_cell
*b
,
1597 __attribute__((__unused__
)) void *data
)
1599 uint64_t *adata
= (uint64_t *) scols_cell_get_userdata(a
),
1600 *bdata
= (uint64_t *) scols_cell_get_userdata(b
);
1602 if (adata
== NULL
&& bdata
== NULL
)
1608 return *adata
== *bdata
? 0 : *adata
>= *bdata
? 1 : -1;
1611 static void __attribute__((__noreturn__
)) help(FILE *out
)
1615 fputs(USAGE_HEADER
, out
);
1616 fprintf(out
, _(" %s [options] [<device> ...]\n"), program_invocation_short_name
);
1618 fputs(USAGE_SEPARATOR
, out
);
1619 fputs(_("List information about block devices.\n"), out
);
1621 fputs(USAGE_OPTIONS
, out
);
1622 fputs(_(" -a, --all print all devices\n"), out
);
1623 fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out
);
1624 fputs(_(" -d, --nodeps don't print slaves or holders\n"), out
);
1625 fputs(_(" -D, --discard print discard capabilities\n"), out
);
1626 fputs(_(" -e, --exclude <list> exclude devices by major number (default: RAM disks)\n"), out
);
1627 fputs(_(" -f, --fs output info about filesystems\n"), out
);
1628 fputs(_(" -i, --ascii use ascii characters only\n"), out
);
1629 fputs(_(" -I, --include <list> show only devices with specified major numbers\n"), out
);
1630 fputs(_(" -J, --json use JSON output format\n"), out
);
1631 fputs(_(" -l, --list use list format output\n"), out
);
1632 fputs(_(" -m, --perms output info about permissions\n"), out
);
1633 fputs(_(" -n, --noheadings don't print headings\n"), out
);
1634 fputs(_(" -o, --output <list> output columns\n"), out
);
1635 fputs(_(" -O, --output-all output all columns\n"), out
);
1636 fputs(_(" -p, --paths print complete device path\n"), out
);
1637 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out
);
1638 fputs(_(" -r, --raw use raw output format\n"), out
);
1639 fputs(_(" -s, --inverse inverse dependencies\n"), out
);
1640 fputs(_(" -S, --scsi output info about SCSI devices\n"), out
);
1641 fputs(_(" -t, --topology output info about topology\n"), out
);
1642 fputs(_(" -x, --sort <column> sort output by <column>\n"), out
);
1643 fputs(USAGE_SEPARATOR
, out
);
1644 fputs(USAGE_HELP
, out
);
1645 fputs(USAGE_VERSION
, out
);
1647 fprintf(out
, _("\nAvailable columns (for --output):\n"));
1649 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
1650 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
1652 fprintf(out
, USAGE_MAN_TAIL("lsblk(8)"));
1654 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
1657 static void check_sysdevblock(void)
1659 if (access(_PATH_SYS_DEVBLOCK
, R_OK
) != 0)
1660 err(EXIT_FAILURE
, _("failed to access sysfs directory: %s"),
1661 _PATH_SYS_DEVBLOCK
);
1664 int main(int argc
, char *argv
[])
1666 struct lsblk _ls
= { .sort_id
= -1 };
1667 int scols_flags
= LSBLK_TREE
;
1668 int c
, status
= EXIT_FAILURE
;
1669 char *outarg
= NULL
;
1672 static const struct option longopts
[] = {
1673 { "all", 0, 0, 'a' },
1674 { "bytes", 0, 0, 'b' },
1675 { "nodeps", 0, 0, 'd' },
1676 { "discard", 0, 0, 'D' },
1677 { "help", 0, 0, 'h' },
1678 { "json", 0, 0, 'J' },
1679 { "output", 1, 0, 'o' },
1680 { "output-all", 0, 0, 'O' },
1681 { "perms", 0, 0, 'm' },
1682 { "noheadings", 0, 0, 'n' },
1683 { "list", 0, 0, 'l' },
1684 { "ascii", 0, 0, 'i' },
1685 { "raw", 0, 0, 'r' },
1686 { "inverse", 0, 0, 's' },
1687 { "fs", 0, 0, 'f' },
1688 { "exclude", 1, 0, 'e' },
1689 { "include", 1, 0, 'I' },
1690 { "topology", 0, 0, 't' },
1691 { "paths", 0, 0, 'p' },
1692 { "pairs", 0, 0, 'P' },
1693 { "scsi", 0, 0, 'S' },
1694 { "sort", 1, 0, 'x' },
1695 { "version", 0, 0, 'V' },
1699 static const ul_excl_t excl
[] = { /* rows and cols in in ASCII order */
1710 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1712 setlocale(LC_ALL
, "");
1713 bindtextdomain(PACKAGE
, LOCALEDIR
);
1714 textdomain(PACKAGE
);
1715 atexit(close_stdout
);
1721 while((c
= getopt_long(argc
, argv
,
1722 "abdDe:fhJlnmo:OpPiI:rstVSx:", longopts
, NULL
)) != -1) {
1724 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1728 lsblk
->all_devices
= 1;
1737 add_column(columns
, ncolumns
++, COL_NAME
);
1738 add_column(columns
, ncolumns
++, COL_DALIGN
);
1739 add_column(columns
, ncolumns
++, COL_DGRAN
);
1740 add_column(columns
, ncolumns
++, COL_DMAX
);
1741 add_column(columns
, ncolumns
++, COL_DZERO
);
1744 parse_excludes(optarg
);
1750 scols_flags
|= LSBLK_JSON
;
1753 scols_flags
&= ~LSBLK_TREE
; /* disable the default */
1756 scols_flags
|= LSBLK_NOHEADINGS
;
1762 for (ncolumns
= 0 ; ncolumns
< ARRAY_SIZE(infos
); ncolumns
++)
1763 columns
[ncolumns
] = ncolumns
;
1769 scols_flags
|= LSBLK_EXPORT
;
1770 scols_flags
&= ~LSBLK_TREE
; /* disable the default */
1773 scols_flags
|= LSBLK_ASCII
;
1776 parse_includes(optarg
);
1779 scols_flags
&= ~LSBLK_TREE
; /* disable the default */
1780 scols_flags
|= LSBLK_RAW
; /* enable raw */
1786 add_column(columns
, ncolumns
++, COL_NAME
);
1787 add_column(columns
, ncolumns
++, COL_FSTYPE
);
1788 add_column(columns
, ncolumns
++, COL_LABEL
);
1789 add_column(columns
, ncolumns
++, COL_UUID
);
1790 add_column(columns
, ncolumns
++, COL_TARGET
);
1793 add_column(columns
, ncolumns
++, COL_NAME
);
1794 add_column(columns
, ncolumns
++, COL_SIZE
);
1795 add_column(columns
, ncolumns
++, COL_OWNER
);
1796 add_column(columns
, ncolumns
++, COL_GROUP
);
1797 add_column(columns
, ncolumns
++, COL_MODE
);
1800 add_column(columns
, ncolumns
++, COL_NAME
);
1801 add_column(columns
, ncolumns
++, COL_ALIOFF
);
1802 add_column(columns
, ncolumns
++, COL_MINIO
);
1803 add_column(columns
, ncolumns
++, COL_OPTIO
);
1804 add_column(columns
, ncolumns
++, COL_PHYSEC
);
1805 add_column(columns
, ncolumns
++, COL_LOGSEC
);
1806 add_column(columns
, ncolumns
++, COL_ROTA
);
1807 add_column(columns
, ncolumns
++, COL_SCHED
);
1808 add_column(columns
, ncolumns
++, COL_RQ_SIZE
);
1809 add_column(columns
, ncolumns
++, COL_RA
);
1810 add_column(columns
, ncolumns
++, COL_WSAME
);
1815 add_column(columns
, ncolumns
++, COL_NAME
);
1816 add_column(columns
, ncolumns
++, COL_HCTL
);
1817 add_column(columns
, ncolumns
++, COL_TYPE
);
1818 add_column(columns
, ncolumns
++, COL_VENDOR
);
1819 add_column(columns
, ncolumns
++, COL_MODEL
);
1820 add_column(columns
, ncolumns
++, COL_REV
);
1821 add_column(columns
, ncolumns
++, COL_TRANSPORT
);
1824 printf(UTIL_LINUX_VERSION
);
1825 return EXIT_SUCCESS
;
1827 scols_flags
&= ~LSBLK_TREE
; /* disable the default */
1828 lsblk
->sort_id
= column_name_to_id(optarg
, strlen(optarg
));
1829 if (lsblk
->sort_id
>= 0)
1833 errtryhelp(EXIT_FAILURE
);
1837 check_sysdevblock();
1840 add_column(columns
, ncolumns
++, COL_NAME
);
1841 add_column(columns
, ncolumns
++, COL_MAJMIN
);
1842 add_column(columns
, ncolumns
++, COL_RM
);
1843 add_column(columns
, ncolumns
++, COL_SIZE
);
1844 add_column(columns
, ncolumns
++, COL_RO
);
1845 add_column(columns
, ncolumns
++, COL_TYPE
);
1846 add_column(columns
, ncolumns
++, COL_TARGET
);
1849 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
1850 &ncolumns
, column_name_to_id
) < 0)
1851 return EXIT_FAILURE
;
1853 if (nexcludes
== 0 && nincludes
== 0)
1854 excludes
[nexcludes
++] = 1; /* default: ignore RAM disks */
1856 if (lsblk
->sort_id
< 0)
1857 /* Since Linux 4.8 we have sort devices by default, because
1858 * /sys is no more sorted */
1859 lsblk
->sort_id
= COL_MAJMIN
;
1861 if (lsblk
->sort_id
>= 0 && column_id_to_number(lsblk
->sort_id
) < 0) {
1862 /* the sort column is not between output columns -- add as hidden */
1863 add_column(columns
, ncolumns
++, lsblk
->sort_id
);
1864 lsblk
->sort_hidden
= 1;
1868 scols_init_debug(0);
1871 * initialize output columns
1873 if (!(lsblk
->table
= scols_new_table()))
1874 errx(EXIT_FAILURE
, _("failed to initialize output table"));
1875 scols_table_enable_raw(lsblk
->table
, !!(scols_flags
& LSBLK_RAW
));
1876 scols_table_enable_export(lsblk
->table
, !!(scols_flags
& LSBLK_EXPORT
));
1877 scols_table_enable_ascii(lsblk
->table
, !!(scols_flags
& LSBLK_ASCII
));
1878 scols_table_enable_json(lsblk
->table
, !!(scols_flags
& LSBLK_JSON
));
1879 scols_table_enable_noheadings(lsblk
->table
, !!(scols_flags
& LSBLK_NOHEADINGS
));
1881 if (scols_flags
& LSBLK_JSON
)
1882 scols_table_set_name(lsblk
->table
, "blockdevices");
1884 for (i
= 0; i
< ncolumns
; i
++) {
1885 struct colinfo
*ci
= get_column_info(i
);
1886 struct libscols_column
*cl
;
1887 int id
= get_column_id(i
), fl
= ci
->flags
;
1889 if (!(scols_flags
& LSBLK_TREE
) && id
== COL_NAME
)
1890 fl
&= ~SCOLS_FL_TREE
;
1891 if (lsblk
->sort_hidden
&& lsblk
->sort_id
== id
)
1892 fl
|= SCOLS_FL_HIDDEN
;
1894 cl
= scols_table_new_column(lsblk
->table
, ci
->name
, ci
->whint
, fl
);
1896 warn(_("failed to initialize output column"));
1899 if (!lsblk
->sort_col
&& lsblk
->sort_id
== id
) {
1900 lsblk
->sort_col
= cl
;
1901 scols_column_set_cmpfunc(cl
,
1902 ci
->sort_type
== SORT_STRING
?
1903 scols_cmpstr_cells
: cmp_u64_cells
, NULL
);
1908 status
= iterate_block_devices() == 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
1910 int cnt
= 0, cnt_err
= 0;
1912 while (optind
< argc
) {
1913 if (process_one_device(argv
[optind
++]) != 0)
1917 status
= cnt
== 0 ? EXIT_FAILURE
: /* nothing */
1918 cnt
== cnt_err
? LSBLK_EXIT_ALLFAILED
:/* all failed */
1919 cnt_err
? LSBLK_EXIT_SOMEOK
: /* some ok */
1920 EXIT_SUCCESS
; /* all success */
1923 if (lsblk
->sort_col
)
1924 scols_sort_table(lsblk
->table
, lsblk
->sort_col
);
1926 scols_print_table(lsblk
->table
);
1929 if (lsblk
->sort_col
)
1930 unref_sortdata(lsblk
->table
);
1932 scols_unref_table(lsblk
->table
);
1934 mnt_unref_table(mtab
);
1935 mnt_unref_table(swaps
);
1936 mnt_unref_cache(mntcache
);