2 * lsblk(8) - list block devices
4 * Copyright (C) 2010,2011,2012 Red Hat, Inc. All rights reserved.
5 * Written by Milan Broz <mbroz@redhat.com>
6 * Karel Zak <kzak@redhat.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it would be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <sys/types.h>
34 #include <sys/ioctl.h>
44 #include <libsmartcols.h>
53 #include "pathnames.h"
55 #include "canonicalize.h"
61 #include "closestream.h"
111 /* basic table settings */
113 LSBLK_ASCII
= (1 << 0),
114 LSBLK_RAW
= (1 << 1),
115 LSBLK_NOHEADINGS
= (1 << 2),
116 LSBLK_EXPORT
= (1 << 3),
117 LSBLK_TREE
= (1 << 4),
122 const char *name
; /* header */
123 double whint
; /* width hint (N < 1 is in percent of termwidth) */
124 int flags
; /* SCOLS_FL_* */
128 /* columns descriptions */
129 static struct colinfo infos
[] = {
130 [COL_NAME
] = { "NAME", 0.25, SCOLS_FL_TREE
| SCOLS_FL_NOEXTREMES
, N_("device name") },
131 [COL_KNAME
] = { "KNAME", 0.3, 0, N_("internal kernel device name") },
132 [COL_PKNAME
] = { "PKNAME", 0.3, 0, N_("internal parent kernel device name") },
133 [COL_MAJMIN
] = { "MAJ:MIN", 6, 0, N_("major:minor device number") },
134 [COL_FSTYPE
] = { "FSTYPE", 0.1, SCOLS_FL_TRUNC
, N_("filesystem type") },
135 [COL_TARGET
] = { "MOUNTPOINT", 0.10, SCOLS_FL_TRUNC
, N_("where the device is mounted") },
136 [COL_LABEL
] = { "LABEL", 0.1, 0, N_("filesystem LABEL") },
137 [COL_UUID
] = { "UUID", 36, 0, N_("filesystem UUID") },
139 [COL_PARTTYPE
] = { "PARTTYPE", 36, 0, N_("partition type UUID") },
140 [COL_PARTLABEL
] = { "PARTLABEL", 0.1, 0, N_("partition LABEL") },
141 [COL_PARTUUID
] = { "PARTUUID", 36, 0, N_("partition UUID") },
142 [COL_PARTFLAGS
] = { "PARTFLAGS", 36, 0, N_("partition flags") },
144 [COL_RA
] = { "RA", 3, SCOLS_FL_RIGHT
, N_("read-ahead of the device") },
145 [COL_RO
] = { "RO", 1, SCOLS_FL_RIGHT
, N_("read-only device") },
146 [COL_RM
] = { "RM", 1, SCOLS_FL_RIGHT
, N_("removable device") },
147 [COL_ROTA
] = { "ROTA", 1, SCOLS_FL_RIGHT
, N_("rotational device") },
148 [COL_RAND
] = { "RAND", 1, SCOLS_FL_RIGHT
, N_("adds randomness") },
149 [COL_MODEL
] = { "MODEL", 0.1, SCOLS_FL_TRUNC
, N_("device identifier") },
150 [COL_SERIAL
] = { "SERIAL", 0.1, SCOLS_FL_TRUNC
, N_("disk serial number") },
151 [COL_SIZE
] = { "SIZE", 5, SCOLS_FL_RIGHT
, N_("size of the device") },
152 [COL_STATE
] = { "STATE", 7, SCOLS_FL_TRUNC
, N_("state of the device") },
153 [COL_OWNER
] = { "OWNER", 0.1, SCOLS_FL_TRUNC
, N_("user name"), },
154 [COL_GROUP
] = { "GROUP", 0.1, SCOLS_FL_TRUNC
, N_("group name") },
155 [COL_MODE
] = { "MODE", 10, 0, N_("device node permissions") },
156 [COL_ALIOFF
] = { "ALIGNMENT", 6, SCOLS_FL_RIGHT
, N_("alignment offset") },
157 [COL_MINIO
] = { "MIN-IO", 6, SCOLS_FL_RIGHT
, N_("minimum I/O size") },
158 [COL_OPTIO
] = { "OPT-IO", 6, SCOLS_FL_RIGHT
, N_("optimal I/O size") },
159 [COL_PHYSEC
] = { "PHY-SEC", 7, SCOLS_FL_RIGHT
, N_("physical sector size") },
160 [COL_LOGSEC
] = { "LOG-SEC", 7, SCOLS_FL_RIGHT
, N_("logical sector size") },
161 [COL_SCHED
] = { "SCHED", 0.1, 0, N_("I/O scheduler name") },
162 [COL_RQ_SIZE
]= { "RQ-SIZE", 5, SCOLS_FL_RIGHT
, N_("request queue size") },
163 [COL_TYPE
] = { "TYPE", 4, 0, N_("device type") },
164 [COL_DALIGN
] = { "DISC-ALN", 6, SCOLS_FL_RIGHT
, N_("discard alignment offset") },
165 [COL_DGRAN
] = { "DISC-GRAN", 6, SCOLS_FL_RIGHT
, N_("discard granularity") },
166 [COL_DMAX
] = { "DISC-MAX", 6, SCOLS_FL_RIGHT
, N_("discard max bytes") },
167 [COL_DZERO
] = { "DISC-ZERO", 1, SCOLS_FL_RIGHT
, N_("discard zeroes data") },
168 [COL_WSAME
] = { "WSAME", 6, SCOLS_FL_RIGHT
, N_("write same max bytes") },
169 [COL_WWN
] = { "WWN", 18, 0, N_("unique storage identifier") },
170 [COL_HCTL
] = { "HCTL", 10, 0, N_("Host:Channel:Target:Lun for SCSI") },
171 [COL_TRANSPORT
] = { "TRAN", 6, 0, N_("device transport type") },
172 [COL_REV
] = { "REV", 4, SCOLS_FL_RIGHT
, N_("device revision") },
173 [COL_VENDOR
] = { "VENDOR", 0.1, SCOLS_FL_TRUNC
, N_("device vendor") },
177 struct libscols_table
*table
; /* output table */
178 unsigned int all_devices
:1; /* print all devices, including empty */
179 unsigned int bytes
:1; /* print SIZE in bytes */
180 unsigned int inverse
:1; /* print inverse dependencies */
181 unsigned int nodeps
:1; /* don't print slaves/holders */
182 unsigned int scsi
:1; /* print only device with HCTL (SCSI) */
183 unsigned int paths
:1; /* print devnames with "/dev" prefix */
186 struct lsblk
*lsblk
; /* global handler */
188 #define NCOLS ARRAY_SIZE(infos)
189 static int columns
[NCOLS
];/* enabled columns */
190 static int ncolumns
; /* number of enabled columns */
192 static int excludes
[256];
193 static size_t nexcludes
;
195 static int includes
[256];
196 static size_t nincludes
;
198 static struct libmnt_table
*mtab
, *swaps
;
199 static struct libmnt_cache
*mntcache
;
206 struct blkdev_cxt
*parent
;
208 struct libscols_line
*scols_line
;
211 char *name
; /* kernel name in /sys/block */
212 char *dm_name
; /* DM name (dm/block) */
214 char *filename
; /* path to device node */
216 struct sysfs_cxt sysfs
;
218 int partition
; /* is partition? TRUE/FALSE */
220 int probed
; /* already probed */
221 char *fstype
; /* detected fs, NULL or "?" if cannot detect */
222 char *uuid
; /* filesystem UUID (or stack uuid) */
223 char *label
; /* filesystem label */
224 char *parttype
; /* partiton type UUID */
225 char *partuuid
; /* partition UUID */
226 char *partlabel
; /* partiton label */
227 char *partflags
; /* partition flags */
228 char *wwn
; /* storage WWN */
229 char *serial
; /* disk serial number */
231 int npartitions
; /* # of partitions this device has */
232 int nholders
; /* # of devices mapped directly to this device
233 * /sys/block/.../holders */
234 int nslaves
; /* # of devices this device maps to */
235 int maj
, min
; /* devno */
236 int discard
; /* supports discard */
238 uint64_t size
; /* device size */
241 static int is_maj_excluded(int maj
)
245 assert(ARRAY_SIZE(excludes
) > nexcludes
);
248 return 0; /* filter not enabled, device not exluded */
250 for (i
= 0; i
< nexcludes
; i
++)
251 if (excludes
[i
] == maj
)
256 static int is_maj_included(int maj
)
260 assert(ARRAY_SIZE(includes
) > nincludes
);
263 return 1; /* filter not enabled, device is included */
265 for (i
= 0; i
< nincludes
; i
++)
266 if (includes
[i
] == maj
)
271 /* array with IDs of enabled columns */
272 static int get_column_id(int num
)
274 assert(ARRAY_SIZE(columns
) == NCOLS
);
275 assert(num
< ncolumns
);
276 assert(columns
[num
] < (int) NCOLS
);
280 static struct colinfo
*get_column_info(int num
)
282 return &infos
[ get_column_id(num
) ];
285 static int column_name_to_id(const char *name
, size_t namesz
)
289 for (i
= 0; i
< NCOLS
; i
++) {
290 const char *cn
= infos
[i
].name
;
292 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
295 warnx(_("unknown column: %s"), name
);
299 static void reset_blkdev_cxt(struct blkdev_cxt
*cxt
)
311 free(cxt
->partlabel
);
315 sysfs_deinit(&cxt
->sysfs
);
317 memset(cxt
, 0, sizeof(*cxt
));
320 static int is_dm(const char *name
)
322 return strncmp(name
, "dm-", 3) ? 0 : 1;
325 static struct dirent
*xreaddir(DIR *dp
)
331 while ((d
= readdir(dp
))) {
332 if (!strcmp(d
->d_name
, ".") ||
333 !strcmp(d
->d_name
, ".."))
336 /* blacklist here? */
342 static char *get_device_path(struct blkdev_cxt
*cxt
)
349 if (is_dm(cxt
->name
))
350 return canonicalize_dm_name(cxt
->name
);
352 snprintf(path
, sizeof(path
), "/dev/%s", cxt
->name
);
353 return xstrdup(path
);
356 static int is_active_swap(const char *filename
)
359 swaps
= mnt_new_table();
363 mntcache
= mnt_new_cache();
365 mnt_table_set_cache(swaps
, mntcache
);
366 mnt_table_parse_swaps(swaps
, NULL
);
369 return mnt_table_find_srcpath(swaps
, filename
, MNT_ITER_BACKWARD
) != 0;
372 static char *get_device_mountpoint(struct blkdev_cxt
*cxt
)
374 struct libmnt_fs
*fs
;
378 assert(cxt
->filename
);
381 mtab
= mnt_new_table();
385 mntcache
= mnt_new_cache();
387 mnt_table_set_cache(mtab
, mntcache
);
388 mnt_table_parse_mtab(mtab
, NULL
);
391 /* Note that maj:min in /proc/self/mouninfo does not have to match with
392 * devno as returned by stat(), so we have to try devname too
394 fs
= mnt_table_find_devno(mtab
, makedev(cxt
->maj
, cxt
->min
), MNT_ITER_BACKWARD
);
396 fs
= mnt_table_find_srcpath(mtab
, cxt
->filename
, MNT_ITER_BACKWARD
);
398 return is_active_swap(cxt
->filename
) ? xstrdup("[SWAP]") : NULL
;
401 fsroot
= mnt_fs_get_root(fs
);
402 if (fsroot
&& strcmp(fsroot
, "/") != 0) {
403 /* hmm.. we found bind mount or btrfs subvolume, let's try to
404 * get real FS root mountpoint */
405 struct libmnt_fs
*rfs
;
406 struct libmnt_iter
*itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
408 mnt_table_set_iter(mtab
, itr
, fs
);
409 while (mnt_table_next_fs(mtab
, itr
, &rfs
) == 0) {
410 fsroot
= mnt_fs_get_root(rfs
);
411 if ((!fsroot
|| strcmp(fsroot
, "/") == 0)
412 && mnt_fs_match_source(rfs
, cxt
->filename
, mntcache
)) {
420 return xstrdup(mnt_fs_get_target(fs
));
424 static int get_udev_properties(struct blkdev_cxt
*cxt
425 __attribute__((__unused__
)))
430 static int get_udev_properties(struct blkdev_cxt
*cxt
)
432 struct udev_device
*dev
;
435 return 0; /* already done */
442 dev
= udev_device_new_from_subsystem_sysname(udev
, "block", cxt
->name
);
446 if ((data
= udev_device_get_property_value(dev
, "ID_FS_LABEL_ENC"))) {
447 cxt
->label
= xstrdup(data
);
448 unhexmangle_string(cxt
->label
);
450 if ((data
= udev_device_get_property_value(dev
, "ID_FS_UUID_ENC"))) {
451 cxt
->uuid
= xstrdup(data
);
452 unhexmangle_string(cxt
->uuid
);
454 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_NAME"))) {
455 cxt
->partlabel
= xstrdup(data
);
456 unhexmangle_string(cxt
->partlabel
);
458 if ((data
= udev_device_get_property_value(dev
, "ID_FS_TYPE")))
459 cxt
->fstype
= xstrdup(data
);
460 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_TYPE")))
461 cxt
->parttype
= xstrdup(data
);
462 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_UUID")))
463 cxt
->partuuid
= xstrdup(data
);
464 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_FLAGS")))
465 cxt
->partflags
= xstrdup(data
);
466 if ((data
= udev_device_get_property_value(dev
, "ID_WWN")))
467 cxt
->wwn
= xstrdup(data
);
468 if ((data
= udev_device_get_property_value(dev
, "ID_SERIAL_SHORT")))
469 cxt
->serial
= xstrdup(data
);
470 udev_device_unref(dev
);
474 return cxt
->probed
== 1 ? 0 : -1;
477 #endif /* HAVE_LIBUDEV */
479 static void probe_device(struct blkdev_cxt
*cxt
)
481 blkid_probe pr
= NULL
;
490 if (get_udev_properties(cxt
) == 0)
491 return; /* success */
495 /* try libblkid (fallback) */
497 return; /* no permissions to read from the device */
499 pr
= blkid_new_probe_from_filename(cxt
->filename
);
503 blkid_probe_enable_superblocks(pr
, 1);
504 blkid_probe_set_superblocks_flags(pr
, BLKID_SUBLKS_LABEL
|
507 blkid_probe_enable_partitions(pr
, 1);
508 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
510 if (!blkid_do_safeprobe(pr
)) {
511 const char *data
= NULL
;
513 if (!blkid_probe_lookup_value(pr
, "TYPE", &data
, NULL
))
514 cxt
->fstype
= xstrdup(data
);
515 if (!blkid_probe_lookup_value(pr
, "UUID", &data
, NULL
))
516 cxt
->uuid
= xstrdup(data
);
517 if (!blkid_probe_lookup_value(pr
, "LABEL", &data
, NULL
))
518 cxt
->label
= xstrdup(data
);
519 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_TYPE", &data
, NULL
))
520 cxt
->parttype
= xstrdup(data
);
521 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_UUID", &data
, NULL
))
522 cxt
->partuuid
= xstrdup(data
);
523 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_NAME", &data
, NULL
))
524 cxt
->partlabel
= xstrdup(data
);
525 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_FLAGS", &data
, NULL
))
526 cxt
->partflags
= xstrdup(data
);
529 blkid_free_probe(pr
);
533 static int is_readonly_device(struct blkdev_cxt
*cxt
)
537 if (sysfs_scanf(&cxt
->sysfs
, "ro", "%d", &ro
) == 1)
540 /* fallback if "ro" attribute does not exist */
541 fd
= open(cxt
->filename
, O_RDONLY
);
543 if (ioctl(fd
, BLKROGET
, &ro
) != 0)
550 static char *get_scheduler(struct blkdev_cxt
*cxt
)
552 char *str
= sysfs_strdup(&cxt
->sysfs
, "queue/scheduler");
553 char *p
, *res
= NULL
;
557 p
= strchr(str
, '[');
560 p
= strchr(res
, ']');
571 static char *get_type(struct blkdev_cxt
*cxt
)
573 char *res
= NULL
, *p
;
575 if (is_dm(cxt
->name
)) {
576 char *dm_uuid
= sysfs_strdup(&cxt
->sysfs
, "dm/uuid");
578 /* The DM_UUID prefix should be set to subsystem owning
579 * the device - LVM, CRYPT, DMRAID, MPATH, PART */
582 char *dm_uuid_prefix
= strsep(&tmp
, "-");
584 if (dm_uuid_prefix
) {
585 /* kpartx hack to remove partition number */
586 if (strncasecmp(dm_uuid_prefix
, "part", 4) == 0)
587 dm_uuid_prefix
[4] = '\0';
589 res
= xstrdup(dm_uuid_prefix
);
595 /* No UUID or no prefix - just mark it as DM device */
598 } else if (!strncmp(cxt
->name
, "loop", 4)) {
599 res
= xstrdup("loop");
601 } else if (!strncmp(cxt
->name
, "md", 2)) {
602 char *md_level
= sysfs_strdup(&cxt
->sysfs
, "md/level");
603 res
= md_level
? md_level
: xstrdup("md");
606 const char *type
= NULL
;
609 if (!sysfs_read_int(&cxt
->sysfs
, "device/type", &x
))
610 type
= blkdev_scsi_type_to_name(x
);
613 type
= cxt
->partition
? "part" : "disk";
617 for (p
= res
; p
&& *p
; p
++)
618 *p
= tolower((unsigned char) *p
);
622 /* Thanks to lsscsi code for idea of detection logic used here */
623 static char *get_transport(struct blkdev_cxt
*cxt
)
625 struct sysfs_cxt
*sysfs
= &cxt
->sysfs
;
627 const char *trans
= NULL
;
629 /* SCSI - Serial Peripheral Interface */
630 if (sysfs_scsi_host_is(sysfs
, "spi"))
633 /* FC/FCoE - Fibre Channel / Fibre Channel over Ethernet */
634 else if (sysfs_scsi_host_is(sysfs
, "fc")) {
635 attr
= sysfs_scsi_host_strdup_attribute(sysfs
, "fc", "symbolic_name");
638 trans
= strstr(attr
, " over ") ? "fcoe" : "fc";
642 /* SAS - Serial Attached SCSI */
643 else if (sysfs_scsi_host_is(sysfs
, "sas") ||
644 sysfs_scsi_has_attribute(sysfs
, "sas_device"))
648 /* SBP - Serial Bus Protocol (FireWire) */
649 else if (sysfs_scsi_has_attribute(sysfs
, "ieee1394_id"))
653 else if (sysfs_scsi_host_is(sysfs
, "iscsi"))
656 /* USB - Universal Serial Bus */
657 else if (sysfs_scsi_path_contains(sysfs
, "usb"))
661 else if (sysfs_scsi_host_is(sysfs
, "scsi")) {
662 attr
= sysfs_scsi_host_strdup_attribute(sysfs
, "scsi", "proc_name");
665 if (!strncmp(attr
, "ahci", 4) || !strncmp(attr
, "sata", 4))
667 else if (strstr(attr
, "ata"))
672 return trans
? xstrdup(trans
) : NULL
;
675 #define is_parsable(_l) (scols_table_is_raw((_l)->table) || \
676 scols_table_is_export((_l)->table))
678 static char *mk_name(const char *name
)
684 xasprintf(&p
, "/dev/%s", name
);
690 static char *mk_dm_name(const char *name
)
696 xasprintf(&p
, "/dev/mapper/%s", name
);
702 static void set_scols_data(struct blkdev_cxt
*cxt
, int col
, int id
, struct libscols_line
*ln
)
707 if (!cxt
->st
.st_rdev
&& (id
== COL_OWNER
|| id
== COL_GROUP
||
709 st_rc
= stat(cxt
->filename
, &cxt
->st
);
713 str
= cxt
->dm_name
? mk_dm_name(cxt
->dm_name
) : mk_name(cxt
->name
);
716 str
= mk_name(cxt
->name
);
720 str
= mk_name(cxt
->parent
->name
);
724 struct passwd
*pw
= st_rc
? NULL
: getpwuid(cxt
->st
.st_uid
);
726 str
= xstrdup(pw
->pw_name
);
731 struct group
*gr
= st_rc
? NULL
: getgrgid(cxt
->st
.st_gid
);
733 str
= xstrdup(gr
->gr_name
);
741 strmode(cxt
->st
.st_mode
, md
);
747 if (is_parsable(lsblk
))
748 xasprintf(&str
, "%u:%u", cxt
->maj
, cxt
->min
);
750 xasprintf(&str
, "%3u:%-3u", cxt
->maj
, cxt
->min
);
755 str
= xstrdup(cxt
->fstype
);
758 if (!(cxt
->nholders
+ cxt
->npartitions
))
759 str
= get_device_mountpoint(cxt
);
764 str
= xstrdup(cxt
->label
);
769 str
= xstrdup(cxt
->uuid
);
774 str
= xstrdup(cxt
->parttype
);
779 str
= xstrdup(cxt
->partlabel
);
784 str
= xstrdup(cxt
->partuuid
);
789 str
= xstrdup(cxt
->partflags
);
792 get_udev_properties(cxt
);
794 str
= xstrdup(cxt
->wwn
);
797 str
= sysfs_strdup(&cxt
->sysfs
, "queue/read_ahead_kb");
800 str
= xstrdup(is_readonly_device(cxt
) ? "1" : "0");
803 str
= sysfs_strdup(&cxt
->sysfs
, "removable");
804 if (!str
&& cxt
->sysfs
.parent
)
805 str
= sysfs_strdup(cxt
->sysfs
.parent
, "removable");
808 str
= sysfs_strdup(&cxt
->sysfs
, "queue/rotational");
811 str
= sysfs_strdup(&cxt
->sysfs
, "queue/add_random");
814 if (!cxt
->partition
&& cxt
->nslaves
== 0)
815 str
= sysfs_strdup(&cxt
->sysfs
, "device/model");
818 if (!cxt
->partition
&& cxt
->nslaves
== 0) {
819 get_udev_properties(cxt
);
821 str
= xstrdup(cxt
->serial
);
825 if (!cxt
->partition
&& cxt
->nslaves
== 0)
826 str
= sysfs_strdup(&cxt
->sysfs
, "device/rev");
829 if (!cxt
->partition
&& cxt
->nslaves
== 0)
830 str
= sysfs_strdup(&cxt
->sysfs
, "device/vendor");
836 xasprintf(&str
, "%jd", cxt
->size
);
838 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, cxt
->size
);
841 if (!cxt
->partition
&& !cxt
->dm_name
)
842 str
= sysfs_strdup(&cxt
->sysfs
, "device/state");
843 else if (cxt
->dm_name
) {
845 if (sysfs_read_int(&cxt
->sysfs
, "dm/suspended", &x
) == 0)
846 str
= xstrdup(x
? "suspended" : "running");
850 str
= sysfs_strdup(&cxt
->sysfs
, "alignment_offset");
853 str
= sysfs_strdup(&cxt
->sysfs
, "queue/minimum_io_size");
856 str
= sysfs_strdup(&cxt
->sysfs
, "queue/optimal_io_size");
859 str
= sysfs_strdup(&cxt
->sysfs
, "queue/physical_block_size");
862 str
= sysfs_strdup(&cxt
->sysfs
, "queue/logical_block_size");
865 str
= get_scheduler(cxt
);
868 str
= sysfs_strdup(&cxt
->sysfs
, "queue/nr_requests");
876 if (sysfs_scsi_get_hctl(&cxt
->sysfs
, &h
, &c
, &t
, &l
) == 0)
877 xasprintf(&str
, "%d:%d:%d:%d", h
, c
, t
, l
);
881 str
= get_transport(cxt
);
885 str
= sysfs_strdup(&cxt
->sysfs
, "discard_alignment");
891 str
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_granularity");
894 if (sysfs_read_u64(&cxt
->sysfs
,
895 "queue/discard_granularity", &x
) == 0)
896 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
901 str
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_max_bytes");
904 if (sysfs_read_u64(&cxt
->sysfs
,
905 "queue/discard_max_bytes", &x
) == 0)
906 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
911 str
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_zeroes_data");
917 str
= sysfs_strdup(&cxt
->sysfs
, "queue/write_same_max_bytes");
921 if (sysfs_read_u64(&cxt
->sysfs
,
922 "queue/write_same_max_bytes", &x
) == 0)
923 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
931 scols_line_refer_data(ln
, col
, str
);
934 static void print_device(struct blkdev_cxt
*cxt
, struct libscols_line
*scols_parent
)
938 cxt
->scols_line
= scols_table_new_line(lsblk
->table
, scols_parent
);
939 if (!cxt
->scols_line
)
942 for (i
= 0; i
< ncolumns
; i
++)
943 set_scols_data(cxt
, i
, get_column_id(i
), cxt
->scols_line
);
946 static int set_cxt(struct blkdev_cxt
*cxt
,
947 struct blkdev_cxt
*parent
,
948 struct blkdev_cxt
*wholedisk
,
953 cxt
->parent
= parent
;
954 cxt
->name
= xstrdup(name
);
955 cxt
->partition
= wholedisk
!= NULL
;
957 cxt
->filename
= get_device_path(cxt
);
958 if (!cxt
->filename
) {
959 warnx(_("%s: failed to get device path"), name
);
963 devno
= sysfs_devname_to_devno(name
, wholedisk
? wholedisk
->name
: NULL
);
966 warnx(_("%s: unknown device name"), name
);
970 if (lsblk
->inverse
) {
971 if (sysfs_init(&cxt
->sysfs
, devno
, wholedisk
? &wholedisk
->sysfs
: NULL
)) {
972 warnx(_("%s: failed to initialize sysfs handler"), name
);
976 parent
->sysfs
.parent
= &cxt
->sysfs
;
978 if (sysfs_init(&cxt
->sysfs
, devno
, parent
? &parent
->sysfs
: NULL
)) {
979 warnx(_("%s: failed to initialize sysfs handler"), name
);
984 cxt
->maj
= major(devno
);
985 cxt
->min
= minor(devno
);
988 if (sysfs_read_u64(&cxt
->sysfs
, "size", &cxt
->size
) == 0) /* in sectors */
989 cxt
->size
<<= 9; /* in bytes */
991 if (sysfs_read_int(&cxt
->sysfs
,
992 "queue/discard_granularity", &cxt
->discard
) != 0)
995 /* Ignore devices of zero size */
996 if (!lsblk
->all_devices
&& cxt
->size
== 0)
1000 cxt
->dm_name
= sysfs_strdup(&cxt
->sysfs
, "dm/name");
1001 if (!cxt
->dm_name
) {
1002 warnx(_("%s: failed to get dm name"), name
);
1007 cxt
->npartitions
= sysfs_count_partitions(&cxt
->sysfs
, name
);
1008 cxt
->nholders
= sysfs_count_dirents(&cxt
->sysfs
, "holders");
1009 cxt
->nslaves
= sysfs_count_dirents(&cxt
->sysfs
, "slaves");
1011 /* ignore non-SCSI devices */
1012 if (lsblk
->scsi
&& sysfs_scsi_get_hctl(&cxt
->sysfs
, NULL
, NULL
, NULL
, NULL
))
1018 static int process_blkdev(struct blkdev_cxt
*cxt
, struct blkdev_cxt
*parent
,
1019 int do_partitions
, const char *part_name
);
1022 * List device partitions if any.
1024 static int list_partitions(struct blkdev_cxt
*wholedisk_cxt
, struct blkdev_cxt
*parent_cxt
,
1025 const char *part_name
)
1029 struct blkdev_cxt part_cxt
= {};
1032 assert(wholedisk_cxt
);
1035 * Do not process further if there are no partitions for
1036 * this device or the device itself is a partition.
1038 if (!wholedisk_cxt
->npartitions
|| wholedisk_cxt
->partition
)
1041 dir
= sysfs_opendir(&wholedisk_cxt
->sysfs
, NULL
);
1043 err(EXIT_FAILURE
, _("failed to open device directory in sysfs"));
1045 while ((d
= xreaddir(dir
))) {
1046 /* Process particular partition only? */
1047 if (part_name
&& strcmp(part_name
, d
->d_name
))
1050 if (!(sysfs_is_partition_dirent(dir
, d
, wholedisk_cxt
->name
)))
1053 if (lsblk
->inverse
) {
1060 if (set_cxt(&part_cxt
, parent_cxt
, wholedisk_cxt
, d
->d_name
))
1063 if (!parent_cxt
&& part_cxt
.nholders
)
1066 wholedisk_cxt
->parent
= &part_cxt
;
1067 print_device(&part_cxt
, parent_cxt
? parent_cxt
->scols_line
: NULL
);
1069 process_blkdev(wholedisk_cxt
, &part_cxt
, 0, NULL
);
1077 int ps
= set_cxt(&part_cxt
, wholedisk_cxt
, wholedisk_cxt
, d
->d_name
);
1079 /* Print whole disk only once */
1081 print_device(wholedisk_cxt
, parent_cxt
? parent_cxt
->scols_line
: NULL
);
1082 if (ps
== 0 && !lsblk
->nodeps
)
1083 process_blkdev(&part_cxt
, wholedisk_cxt
, 0, NULL
);
1086 reset_blkdev_cxt(&part_cxt
);
1094 static int get_wholedisk_from_partition_dirent(DIR *dir
, const char *dirname
,
1095 struct dirent
*d
, struct blkdev_cxt
*cxt
)
1097 char path
[PATH_MAX
];
1101 if ((len
= readlink_at(dirfd(dir
), dirname
,
1102 d
->d_name
, path
, sizeof(path
) - 1)) < 0)
1107 /* The path ends with ".../<device>/<partition>" */
1108 p
= strrchr(path
, '/');
1113 p
= strrchr(path
, '/');
1118 return set_cxt(cxt
, NULL
, NULL
, p
);
1122 * List device dependencies: partitions, holders (inverse = 0) or slaves (inverse = 1).
1124 static int list_deps(struct blkdev_cxt
*cxt
)
1128 struct blkdev_cxt dep
= {};
1129 char dirname
[PATH_MAX
];
1130 const char *depname
;
1137 if (!(lsblk
->inverse
? cxt
->nslaves
: cxt
->nholders
))
1140 depname
= lsblk
->inverse
? "slaves" : "holders";
1141 dir
= sysfs_opendir(&cxt
->sysfs
, depname
);
1145 snprintf(dirname
, sizeof(dirname
), "%s/%s", cxt
->sysfs
.dir_path
, depname
);
1147 while ((d
= xreaddir(dir
))) {
1148 /* Is the dependency a partition? */
1149 if (sysfs_is_partition_dirent(dir
, d
, NULL
)) {
1150 if (!get_wholedisk_from_partition_dirent(dir
, dirname
, d
, &dep
))
1151 process_blkdev(&dep
, cxt
, 1, d
->d_name
);
1153 /* The dependency is a whole device. */
1154 else if (!set_cxt(&dep
, cxt
, NULL
, d
->d_name
))
1155 process_blkdev(&dep
, cxt
, 1, NULL
);
1157 reset_blkdev_cxt(&dep
);
1164 static int process_blkdev(struct blkdev_cxt
*cxt
, struct blkdev_cxt
*parent
,
1165 int do_partitions
, const char *part_name
)
1167 if (do_partitions
&& cxt
->npartitions
)
1168 return list_partitions(cxt
, parent
, part_name
);
1170 print_device(cxt
, parent
? parent
->scols_line
: NULL
);
1171 return list_deps(cxt
);
1174 /* Iterate devices in sysfs */
1175 static int iterate_block_devices(void)
1179 struct blkdev_cxt cxt
= {};
1181 if (!(dir
= opendir(_PATH_SYS_BLOCK
)))
1182 return EXIT_FAILURE
;
1184 while ((d
= xreaddir(dir
))) {
1185 if (set_cxt(&cxt
, NULL
, NULL
, d
->d_name
))
1188 if (is_maj_excluded(cxt
.maj
) || !is_maj_included(cxt
.maj
))
1191 /* Skip devices in the middle of dependency tree. */
1192 if ((lsblk
->inverse
? cxt
.nholders
: cxt
.nslaves
) > 0)
1195 process_blkdev(&cxt
, NULL
, 1, NULL
);
1197 reset_blkdev_cxt(&cxt
);
1202 return EXIT_SUCCESS
;
1205 static char *devno_to_sysfs_name(dev_t devno
, char *devname
, char *buf
, size_t buf_size
)
1207 char path
[PATH_MAX
];
1210 if (!sysfs_devno_path(devno
, path
, sizeof(path
))) {
1211 warn(_("%s: failed to compose sysfs path"), devname
);
1215 len
= readlink(path
, buf
, buf_size
- 1);
1217 warn(_("%s: failed to read link"), path
);
1222 return xstrdup(strrchr(buf
, '/') + 1);
1225 static int process_one_device(char *devname
)
1227 struct blkdev_cxt parent
= {}, cxt
= {};
1229 char buf
[PATH_MAX
+ 1], *name
, *diskname
= NULL
;
1232 int status
= EXIT_FAILURE
;
1234 if (stat(devname
, &st
) || !S_ISBLK(st
.st_mode
)) {
1235 warnx(_("%s: not a block device"), devname
);
1236 return EXIT_FAILURE
;
1239 if (!(name
= devno_to_sysfs_name(st
.st_rdev
, devname
, buf
, PATH_MAX
))) {
1240 warn(_("%s: failed to get sysfs name"), devname
);
1241 return EXIT_FAILURE
;
1244 if (!strncmp(name
, "dm-", 3)) {
1245 /* dm mapping is never a real partition! */
1248 if (blkid_devno_to_wholedisk(st
.st_rdev
, buf
, sizeof(buf
), &disk
)) {
1249 warn(_("%s: failed to get whole-disk device number"), devname
);
1250 return EXIT_FAILURE
;
1253 real_part
= st
.st_rdev
!= disk
;
1258 * Device is not a partition.
1260 if (set_cxt(&cxt
, NULL
, NULL
, name
))
1262 process_blkdev(&cxt
, NULL
, !lsblk
->inverse
, NULL
);
1265 * Partition, read sysfs name of the device.
1267 if (set_cxt(&parent
, NULL
, NULL
, diskname
))
1269 if (set_cxt(&cxt
, &parent
, &parent
, name
))
1273 process_blkdev(&parent
, &cxt
, 1, cxt
.name
);
1275 process_blkdev(&cxt
, &parent
, 1, NULL
);
1278 status
= EXIT_SUCCESS
;
1281 reset_blkdev_cxt(&cxt
);
1284 reset_blkdev_cxt(&parent
);
1289 static void parse_excludes(const char *str0
)
1291 const char *str
= str0
;
1293 while (str
&& *str
) {
1298 n
= strtoul(str
, &end
, 10);
1300 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1301 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1302 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1303 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1304 excludes
[nexcludes
++] = n
;
1306 if (nexcludes
== ARRAY_SIZE(excludes
))
1307 /* TRANSLATORS: The standard value for %d is 256. */
1308 errx(EXIT_FAILURE
, _("the list of excluded devices is "
1309 "too large (limit is %d devices)"),
1310 (int)ARRAY_SIZE(excludes
));
1312 str
= end
&& *end
? end
+ 1 : NULL
;
1316 static void parse_includes(const char *str0
)
1318 const char *str
= str0
;
1320 while (str
&& *str
) {
1325 n
= strtoul(str
, &end
, 10);
1327 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1328 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1329 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1330 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1331 includes
[nincludes
++] = n
;
1333 if (nincludes
== ARRAY_SIZE(includes
))
1334 /* TRANSLATORS: The standard value for %d is 256. */
1335 errx(EXIT_FAILURE
, _("the list of included devices is "
1336 "too large (limit is %d devices)"),
1337 (int)ARRAY_SIZE(includes
));
1338 str
= end
&& *end
? end
+ 1 : NULL
;
1342 static void __attribute__((__noreturn__
)) help(FILE *out
)
1346 fputs(USAGE_HEADER
, out
);
1347 fprintf(out
, _(" %s [options] [<device> ...]\n"), program_invocation_short_name
);
1348 fputs(USAGE_OPTIONS
, out
);
1349 fputs(_(" -a, --all print all devices\n"), out
);
1350 fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out
);
1351 fputs(_(" -d, --nodeps don't print slaves or holders\n"), out
);
1352 fputs(_(" -D, --discard print discard capabilities\n"), out
);
1353 fputs(_(" -e, --exclude <list> exclude devices by major number (default: RAM disks)\n"), out
);
1354 fputs(_(" -f, --fs output info about filesystems\n"), out
);
1355 fputs(_(" -i, --ascii use ascii characters only\n"), out
);
1356 fputs(_(" -I, --include <list> show only devices with specified major numbers\n"), out
);
1357 fputs(_(" -l, --list use list format output\n"), out
);
1358 fputs(_(" -m, --perms output info about permissions\n"), out
);
1359 fputs(_(" -n, --noheadings don't print headings\n"), out
);
1360 fputs(_(" -o, --output <list> output columns\n"), out
);
1361 fputs(_(" -p, --paths print complete device path\n"), out
);
1362 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out
);
1363 fputs(_(" -r, --raw use raw output format\n"), out
);
1364 fputs(_(" -s, --inverse inverse dependencies\n"), out
);
1365 fputs(_(" -S, --scsi output info about SCSI devices\n"), out
);
1366 fputs(_(" -t, --topology output info about topology\n"), out
);
1367 fputs(USAGE_SEPARATOR
, out
);
1368 fputs(USAGE_HELP
, out
);
1369 fputs(USAGE_VERSION
, out
);
1371 fprintf(out
, _("\nAvailable columns (for --output):\n"));
1373 for (i
= 0; i
< NCOLS
; i
++)
1374 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
1376 fprintf(out
, USAGE_MAN_TAIL("lsblk(8)"));
1378 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
1381 static void check_sysdevblock(void)
1383 if (access(_PATH_SYS_DEVBLOCK
, R_OK
) != 0)
1384 err(EXIT_FAILURE
, _("failed to access sysfs directory: %s"),
1385 _PATH_SYS_DEVBLOCK
);
1388 int main(int argc
, char *argv
[])
1391 int scols_flags
= LSBLK_TREE
;
1392 int i
, c
, status
= EXIT_FAILURE
;
1393 char *outarg
= NULL
;
1395 static const struct option longopts
[] = {
1396 { "all", 0, 0, 'a' },
1397 { "bytes", 0, 0, 'b' },
1398 { "nodeps", 0, 0, 'd' },
1399 { "discard", 0, 0, 'D' },
1400 { "help", 0, 0, 'h' },
1401 { "output", 1, 0, 'o' },
1402 { "perms", 0, 0, 'm' },
1403 { "noheadings", 0, 0, 'n' },
1404 { "list", 0, 0, 'l' },
1405 { "ascii", 0, 0, 'i' },
1406 { "raw", 0, 0, 'r' },
1407 { "inverse", 0, 0, 's' },
1408 { "fs", 0, 0, 'f' },
1409 { "exclude", 1, 0, 'e' },
1410 { "include", 1, 0, 'I' },
1411 { "topology", 0, 0, 't' },
1412 { "paths", 0, 0, 'p' },
1413 { "pairs", 0, 0, 'P' },
1414 { "scsi", 0, 0, 'S' },
1415 { "version", 0, 0, 'V' },
1419 static const ul_excl_t excl
[] = { /* rows and cols in in ASCII order */
1424 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1426 setlocale(LC_ALL
, "");
1427 bindtextdomain(PACKAGE
, LOCALEDIR
);
1428 textdomain(PACKAGE
);
1429 atexit(close_stdout
);
1432 memset(lsblk
, 0, sizeof(*lsblk
));
1434 while((c
= getopt_long(argc
, argv
,
1435 "abdDe:fhlnmo:pPiI:rstVS", longopts
, NULL
)) != -1) {
1437 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1441 lsblk
->all_devices
= 1;
1450 columns
[ncolumns
++] = COL_NAME
;
1451 columns
[ncolumns
++] = COL_DALIGN
;
1452 columns
[ncolumns
++] = COL_DGRAN
;
1453 columns
[ncolumns
++] = COL_DMAX
;
1454 columns
[ncolumns
++] = COL_DZERO
;
1457 parse_excludes(optarg
);
1463 scols_flags
&= ~LSBLK_TREE
; /* disable the default */
1466 scols_flags
|= LSBLK_NOHEADINGS
;
1475 scols_flags
|= LSBLK_EXPORT
;
1476 scols_flags
&= ~LSBLK_TREE
; /* disable the default */
1479 scols_flags
|= LSBLK_ASCII
;
1482 parse_includes(optarg
);
1485 scols_flags
&= ~LSBLK_TREE
; /* disable the default */
1486 scols_flags
|= LSBLK_RAW
; /* enable raw */
1492 columns
[ncolumns
++] = COL_NAME
;
1493 columns
[ncolumns
++] = COL_FSTYPE
;
1494 columns
[ncolumns
++] = COL_LABEL
;
1495 columns
[ncolumns
++] = COL_UUID
;
1496 columns
[ncolumns
++] = COL_TARGET
;
1499 columns
[ncolumns
++] = COL_NAME
;
1500 columns
[ncolumns
++] = COL_SIZE
;
1501 columns
[ncolumns
++] = COL_OWNER
;
1502 columns
[ncolumns
++] = COL_GROUP
;
1503 columns
[ncolumns
++] = COL_MODE
;
1506 columns
[ncolumns
++] = COL_NAME
;
1507 columns
[ncolumns
++] = COL_ALIOFF
;
1508 columns
[ncolumns
++] = COL_MINIO
;
1509 columns
[ncolumns
++] = COL_OPTIO
;
1510 columns
[ncolumns
++] = COL_PHYSEC
;
1511 columns
[ncolumns
++] = COL_LOGSEC
;
1512 columns
[ncolumns
++] = COL_ROTA
;
1513 columns
[ncolumns
++] = COL_SCHED
;
1514 columns
[ncolumns
++] = COL_RQ_SIZE
;
1515 columns
[ncolumns
++] = COL_RA
;
1516 columns
[ncolumns
++] = COL_WSAME
;
1521 columns
[ncolumns
++] = COL_NAME
;
1522 columns
[ncolumns
++] = COL_HCTL
;
1523 columns
[ncolumns
++] = COL_TYPE
;
1524 columns
[ncolumns
++] = COL_VENDOR
;
1525 columns
[ncolumns
++] = COL_MODEL
;
1526 columns
[ncolumns
++] = COL_REV
;
1527 columns
[ncolumns
++] = COL_TRANSPORT
;
1530 printf(UTIL_LINUX_VERSION
);
1531 return EXIT_SUCCESS
;
1537 check_sysdevblock();
1540 columns
[ncolumns
++] = COL_NAME
;
1541 columns
[ncolumns
++] = COL_MAJMIN
;
1542 columns
[ncolumns
++] = COL_RM
;
1543 columns
[ncolumns
++] = COL_SIZE
;
1544 columns
[ncolumns
++] = COL_RO
;
1545 columns
[ncolumns
++] = COL_TYPE
;
1546 columns
[ncolumns
++] = COL_TARGET
;
1549 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
1550 &ncolumns
, column_name_to_id
) < 0)
1551 return EXIT_FAILURE
;
1553 if (nexcludes
== 0 && nincludes
== 0)
1554 excludes
[nexcludes
++] = 1; /* default: ignore RAM disks */
1559 * initialize output columns
1561 if (!(lsblk
->table
= scols_new_table()))
1562 errx(EXIT_FAILURE
, _("failed to initialize output table"));
1563 scols_table_enable_raw(lsblk
->table
, !!(scols_flags
& LSBLK_RAW
));
1564 scols_table_enable_export(lsblk
->table
, !!(scols_flags
& LSBLK_EXPORT
));
1565 scols_table_enable_ascii(lsblk
->table
, !!(scols_flags
& LSBLK_ASCII
));
1566 scols_table_enable_noheadings(lsblk
->table
, !!(scols_flags
& LSBLK_NOHEADINGS
));
1568 for (i
= 0; i
< ncolumns
; i
++) {
1569 struct colinfo
*ci
= get_column_info(i
);
1572 if (!(scols_flags
& LSBLK_TREE
) && get_column_id(i
) == COL_NAME
)
1573 fl
&= ~SCOLS_FL_TREE
;
1575 if (!scols_table_new_column(lsblk
->table
, ci
->name
, ci
->whint
, fl
)) {
1576 warn(_("failed to initialize output column"));
1582 status
= iterate_block_devices();
1583 else while (optind
< argc
)
1584 status
= process_one_device(argv
[optind
++]);
1586 scols_print_table(lsblk
->table
);
1589 scols_unref_table(lsblk
->table
);
1591 mnt_unref_table(mtab
);
1592 mnt_unref_table(swaps
);
1593 mnt_unref_cache(mntcache
);