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>
52 #include "pathnames.h"
54 #include "canonicalize.h"
61 #include "closestream.h"
110 const char *name
; /* header */
111 double whint
; /* width hint (N < 1 is in percent of termwidth) */
112 int flags
; /* TT_FL_* */
116 /* columns descriptions */
117 static struct colinfo infos
[] = {
118 [COL_NAME
] = { "NAME", 0.25, TT_FL_TREE
| TT_FL_NOEXTREMES
, N_("device name") },
119 [COL_KNAME
] = { "KNAME", 0.3, 0, N_("internal kernel device name") },
120 [COL_PKNAME
] = { "PKNAME", 0.3, 0, N_("internal parent kernel device name") },
121 [COL_MAJMIN
] = { "MAJ:MIN", 6, 0, N_("major:minor device number") },
122 [COL_FSTYPE
] = { "FSTYPE", 0.1, TT_FL_TRUNC
, N_("filesystem type") },
123 [COL_TARGET
] = { "MOUNTPOINT", 0.10, TT_FL_TRUNC
, N_("where the device is mounted") },
124 [COL_LABEL
] = { "LABEL", 0.1, 0, N_("filesystem LABEL") },
125 [COL_UUID
] = { "UUID", 36, 0, N_("filesystem UUID") },
127 [COL_PARTLABEL
] = { "PARTLABEL", 0.1, 0, N_("partition LABEL") },
128 [COL_PARTUUID
] = { "PARTUUID", 36, 0, N_("partition UUID") },
130 [COL_RA
] = { "RA", 3, TT_FL_RIGHT
, N_("read-ahead of the device") },
131 [COL_RO
] = { "RO", 1, TT_FL_RIGHT
, N_("read-only device") },
132 [COL_RM
] = { "RM", 1, TT_FL_RIGHT
, N_("removable device") },
133 [COL_ROTA
] = { "ROTA", 1, TT_FL_RIGHT
, N_("rotational device") },
134 [COL_RAND
] = { "RAND", 1, TT_FL_RIGHT
, N_("adds randomness") },
135 [COL_MODEL
] = { "MODEL", 0.1, TT_FL_TRUNC
, N_("device identifier") },
136 [COL_SIZE
] = { "SIZE", 5, TT_FL_RIGHT
, N_("size of the device") },
137 [COL_STATE
] = { "STATE", 7, TT_FL_TRUNC
, N_("state of the device") },
138 [COL_OWNER
] = { "OWNER", 0.1, TT_FL_TRUNC
, N_("user name"), },
139 [COL_GROUP
] = { "GROUP", 0.1, TT_FL_TRUNC
, N_("group name") },
140 [COL_MODE
] = { "MODE", 10, 0, N_("device node permissions") },
141 [COL_ALIOFF
] = { "ALIGNMENT", 6, TT_FL_RIGHT
, N_("alignment offset") },
142 [COL_MINIO
] = { "MIN-IO", 6, TT_FL_RIGHT
, N_("minimum I/O size") },
143 [COL_OPTIO
] = { "OPT-IO", 6, TT_FL_RIGHT
, N_("optimal I/O size") },
144 [COL_PHYSEC
] = { "PHY-SEC", 7, TT_FL_RIGHT
, N_("physical sector size") },
145 [COL_LOGSEC
] = { "LOG-SEC", 7, TT_FL_RIGHT
, N_("logical sector size") },
146 [COL_SCHED
] = { "SCHED", 0.1, 0, N_("I/O scheduler name") },
147 [COL_RQ_SIZE
]= { "RQ-SIZE", 5, TT_FL_RIGHT
, N_("request queue size") },
148 [COL_TYPE
] = { "TYPE", 4, 0, N_("device type") },
149 [COL_DALIGN
] = { "DISC-ALN", 6, TT_FL_RIGHT
, N_("discard alignment offset") },
150 [COL_DGRAN
] = { "DISC-GRAN", 6, TT_FL_RIGHT
, N_("discard granularity") },
151 [COL_DMAX
] = { "DISC-MAX", 6, TT_FL_RIGHT
, N_("discard max bytes") },
152 [COL_DZERO
] = { "DISC-ZERO", 1, TT_FL_RIGHT
, N_("discard zeroes data") },
153 [COL_WSAME
] = { "WSAME", 6, TT_FL_RIGHT
, N_("write same max bytes") },
154 [COL_WWN
] = { "WWN", 18, 0, N_("unique storage identifier") },
155 [COL_HCTL
] = { "HCTL", 10, 0, N_("Host:Channel:Target:Lun for SCSI") },
156 [COL_TRANSPORT
] = { "TRAN", 6, 0, N_("device transport type") },
157 [COL_REV
] = { "REV", 4, TT_FL_RIGHT
, N_("device revision") },
158 [COL_VENDOR
] = { "VENDOR", 0.1, TT_FL_TRUNC
, N_("device vendor") },
162 struct tt
*tt
; /* output table */
163 unsigned int all_devices
:1; /* print all devices, including empty */
164 unsigned int bytes
:1; /* print SIZE in bytes */
165 unsigned int inverse
:1; /* print inverse dependencies */
166 unsigned int nodeps
:1; /* don't print slaves/holders */
167 unsigned int scsi
:1; /* print only device with HCTL (SCSI) */
170 struct lsblk
*lsblk
; /* global handler */
172 #define NCOLS ARRAY_SIZE(infos)
173 static int columns
[NCOLS
];/* enabled columns */
174 static int ncolumns
; /* number of enabled columns */
176 static int excludes
[256];
177 static size_t nexcludes
;
179 static int includes
[256];
180 static size_t nincludes
;
182 static struct libmnt_table
*mtab
, *swaps
;
183 static struct libmnt_cache
*mntcache
;
186 struct blkdev_cxt
*parent
;
188 struct tt_line
*tt_line
;
191 char *name
; /* kernel name in /sys/block */
192 char *dm_name
; /* DM name (dm/block) */
194 char *filename
; /* path to device node */
196 struct sysfs_cxt sysfs
;
198 int partition
; /* is partition? TRUE/FALSE */
200 int probed
; /* already probed */
201 char *fstype
; /* detected fs, NULL or "?" if cannot detect */
202 char *uuid
; /* filesystem UUID (or stack uuid) */
203 char *label
; /* filesystem label */
204 char *partuuid
; /* partition UUID */
205 char *partlabel
; /* partiton label */
206 char *wwn
; /* storage WWN */
208 int npartitions
; /* # of partitions this device has */
209 int nholders
; /* # of devices mapped directly to this device
210 * /sys/block/.../holders */
211 int nslaves
; /* # of devices this device maps to */
212 int maj
, min
; /* devno */
213 int discard
; /* supports discard */
215 uint64_t size
; /* device size */
218 static int is_maj_excluded(int maj
)
222 assert(ARRAY_SIZE(excludes
) > nexcludes
);
225 return 0; /* filter not enabled, device not exluded */
227 for (i
= 0; i
< nexcludes
; i
++)
228 if (excludes
[i
] == maj
)
233 static int is_maj_included(int maj
)
237 assert(ARRAY_SIZE(includes
) > nincludes
);
240 return 1; /* filter not enabled, device is included */
242 for (i
= 0; i
< nincludes
; i
++)
243 if (includes
[i
] == maj
)
248 /* array with IDs of enabled columns */
249 static int get_column_id(int num
)
251 assert(ARRAY_SIZE(columns
) == NCOLS
);
252 assert(num
< ncolumns
);
253 assert(columns
[num
] < (int) NCOLS
);
257 static struct colinfo
*get_column_info(int num
)
259 return &infos
[ get_column_id(num
) ];
262 static int column_name_to_id(const char *name
, size_t namesz
)
266 for (i
= 0; i
< NCOLS
; i
++) {
267 const char *cn
= infos
[i
].name
;
269 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
272 warnx(_("unknown column: %s"), name
);
276 static void reset_blkdev_cxt(struct blkdev_cxt
*cxt
)
287 free(cxt
->partlabel
);
290 sysfs_deinit(&cxt
->sysfs
);
292 memset(cxt
, 0, sizeof(*cxt
));
295 static int is_dm(const char *name
)
297 return strncmp(name
, "dm-", 3) ? 0 : 1;
300 static struct dirent
*xreaddir(DIR *dp
)
306 while ((d
= readdir(dp
))) {
307 if (!strcmp(d
->d_name
, ".") ||
308 !strcmp(d
->d_name
, ".."))
311 /* blacklist here? */
317 static char *get_device_path(struct blkdev_cxt
*cxt
)
324 if (is_dm(cxt
->name
))
325 return canonicalize_dm_name(cxt
->name
);
327 snprintf(path
, sizeof(path
), "/dev/%s", cxt
->name
);
328 return xstrdup(path
);
331 static int is_active_swap(const char *filename
)
334 swaps
= mnt_new_table();
338 mntcache
= mnt_new_cache();
340 mnt_table_set_cache(swaps
, mntcache
);
341 mnt_table_parse_swaps(swaps
, NULL
);
344 return mnt_table_find_srcpath(swaps
, filename
, MNT_ITER_BACKWARD
) != 0;
347 static char *get_device_mountpoint(struct blkdev_cxt
*cxt
)
349 struct libmnt_fs
*fs
;
353 assert(cxt
->filename
);
356 mtab
= mnt_new_table();
360 mntcache
= mnt_new_cache();
362 mnt_table_set_cache(mtab
, mntcache
);
363 mnt_table_parse_mtab(mtab
, NULL
);
366 /* try /etc/mtab or /proc/self/mountinfo */
367 fs
= mnt_table_find_srcpath(mtab
, cxt
->filename
, MNT_ITER_BACKWARD
);
369 return is_active_swap(cxt
->filename
) ? xstrdup("[SWAP]") : NULL
;
372 fsroot
= mnt_fs_get_root(fs
);
373 if (fsroot
&& strcmp(fsroot
, "/") != 0) {
374 /* hmm.. we found bind mount or btrfs subvolume, let's try to
375 * get real FS root mountpoint */
376 struct libmnt_fs
*rfs
;
377 struct libmnt_iter
*itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
379 mnt_table_set_iter(mtab
, itr
, fs
);
380 while (mnt_table_next_fs(mtab
, itr
, &rfs
) == 0) {
381 fsroot
= mnt_fs_get_root(rfs
);
382 if ((!fsroot
|| strcmp(fsroot
, "/") == 0)
383 && mnt_fs_match_source(rfs
, cxt
->filename
, mntcache
)) {
391 return xstrdup(mnt_fs_get_target(fs
));
395 static int get_udev_properties(struct blkdev_cxt
*cxt
396 __attribute__((__unused__
)))
401 static int get_udev_properties(struct blkdev_cxt
*cxt
)
404 struct udev_device
*dev
;
407 return 0; /* already done */
413 dev
= udev_device_new_from_subsystem_sysname(udev
, "block", cxt
->name
);
417 if ((data
= udev_device_get_property_value(dev
, "ID_FS_LABEL_ENC"))) {
418 cxt
->label
= xstrdup(data
);
419 unhexmangle_string(cxt
->label
);
421 if ((data
= udev_device_get_property_value(dev
, "ID_FS_UUID_ENC"))) {
422 cxt
->uuid
= xstrdup(data
);
423 unhexmangle_string(cxt
->uuid
);
425 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_NAME"))) {
426 cxt
->partlabel
= xstrdup(data
);
427 unhexmangle_string(cxt
->partlabel
);
429 if ((data
= udev_device_get_property_value(dev
, "ID_FS_TYPE")))
430 cxt
->fstype
= xstrdup(data
);
431 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_UUID")))
432 cxt
->partuuid
= xstrdup(data
);
433 if ((data
= udev_device_get_property_value(dev
, "ID_WWN")))
434 cxt
->wwn
= xstrdup(data
);
436 udev_device_unref(dev
);
442 return cxt
->probed
== 1 ? 0 : -1;
445 #endif /* HAVE_LIBUDEV */
447 static void probe_device(struct blkdev_cxt
*cxt
)
449 blkid_probe pr
= NULL
;
458 if (get_udev_properties(cxt
) == 0)
459 return; /* success */
463 /* try libblkid (fallback) */
465 return; /* no permissions to read from the device */
467 pr
= blkid_new_probe_from_filename(cxt
->filename
);
471 blkid_probe_enable_superblocks(pr
, 1);
472 blkid_probe_set_superblocks_flags(pr
, BLKID_SUBLKS_LABEL
|
475 blkid_probe_enable_partitions(pr
, 1);
476 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
478 if (!blkid_do_safeprobe(pr
)) {
479 const char *data
= NULL
;
481 if (!blkid_probe_lookup_value(pr
, "TYPE", &data
, NULL
))
482 cxt
->fstype
= xstrdup(data
);
483 if (!blkid_probe_lookup_value(pr
, "UUID", &data
, NULL
))
484 cxt
->uuid
= xstrdup(data
);
485 if (!blkid_probe_lookup_value(pr
, "LABEL", &data
, NULL
))
486 cxt
->label
= xstrdup(data
);
487 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_UUID", &data
, NULL
))
488 cxt
->partuuid
= xstrdup(data
);
489 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_NAME", &data
, NULL
))
490 cxt
->partlabel
= xstrdup(data
);
494 blkid_free_probe(pr
);
498 static int is_readonly_device(struct blkdev_cxt
*cxt
)
502 if (sysfs_scanf(&cxt
->sysfs
, "ro", "%d", &ro
) == 1)
505 /* fallback if "ro" attribute does not exist */
506 fd
= open(cxt
->filename
, O_RDONLY
);
508 if (ioctl(fd
, BLKROGET
, &ro
) != 0)
515 static char *get_scheduler(struct blkdev_cxt
*cxt
)
517 char *str
= sysfs_strdup(&cxt
->sysfs
, "queue/scheduler");
518 char *p
, *res
= NULL
;
522 p
= strchr(str
, '[');
525 p
= strchr(res
, ']');
536 static char *get_type(struct blkdev_cxt
*cxt
)
538 char *res
= NULL
, *p
;
540 if (is_dm(cxt
->name
)) {
541 char *dm_uuid
= sysfs_strdup(&cxt
->sysfs
, "dm/uuid");
543 /* The DM_UUID prefix should be set to subsystem owning
544 * the device - LVM, CRYPT, DMRAID, MPATH, PART */
547 char *dm_uuid_prefix
= strsep(&tmp
, "-");
549 if (dm_uuid_prefix
) {
550 /* kpartx hack to remove partition number */
551 if (strncasecmp(dm_uuid_prefix
, "part", 4) == 0)
552 dm_uuid_prefix
[4] = '\0';
554 res
= xstrdup(dm_uuid_prefix
);
560 /* No UUID or no prefix - just mark it as DM device */
563 } else if (!strncmp(cxt
->name
, "loop", 4)) {
564 res
= xstrdup("loop");
566 } else if (!strncmp(cxt
->name
, "md", 2)) {
567 char *md_level
= sysfs_strdup(&cxt
->sysfs
, "md/level");
568 res
= md_level
? md_level
: xstrdup("md");
571 const char *type
= NULL
;
574 if (!sysfs_read_int(&cxt
->sysfs
, "device/type", &x
))
575 type
= blkdev_scsi_type_to_name(x
);
578 type
= cxt
->partition
? "part" : "disk";
582 for (p
= res
; p
&& *p
; p
++)
583 *p
= tolower((unsigned char) *p
);
587 /* Thanks to lsscsi code for idea of detection logic used here */
588 static char *get_transport(struct blkdev_cxt
*cxt
)
590 struct sysfs_cxt
*sysfs
= &cxt
->sysfs
;
592 const char *trans
= NULL
;
594 /* SCSI - Serial Peripheral Interface */
595 if (sysfs_scsi_host_is(sysfs
, "spi"))
598 /* FC/FCoE - Fibre Channel / Fibre Channel over Ethernet */
599 else if (sysfs_scsi_host_is(sysfs
, "fc")) {
600 attr
= sysfs_scsi_host_strdup_attribute(sysfs
, "fc", "symbolic_name");
603 trans
= strstr(attr
, " over ") ? "fcoe" : "fc";
607 /* SAS - Serial Attached SCSI */
608 else if (sysfs_scsi_host_is(sysfs
, "sas") ||
609 sysfs_scsi_has_attribute(sysfs
, "sas_device"))
613 /* SBP - Serial Bus Protocol (FireWire) */
614 else if (sysfs_scsi_has_attribute(sysfs
, "ieee1394_id"))
618 else if (sysfs_scsi_host_is(sysfs
, "iscsi"))
621 /* USB - Universal Serial Bus */
622 else if (sysfs_scsi_path_contains(sysfs
, "usb"))
626 else if (sysfs_scsi_host_is(sysfs
, "scsi")) {
627 attr
= sysfs_scsi_host_strdup_attribute(sysfs
, "scsi", "proc_name");
630 if (!strncmp(attr
, "ahci", 4) || !strncmp(attr
, "sata", 4))
632 else if (strstr(attr
, "ata"))
637 return trans
? xstrdup(trans
) : NULL
;
640 #define is_parsable(_l) (((_l)->tt->flags & TT_FL_RAW) || \
641 ((_l)->tt->flags & TT_FL_EXPORT))
643 static void set_tt_data(struct blkdev_cxt
*cxt
, int col
, int id
, struct tt_line
*ln
)
649 if (!cxt
->st
.st_rdev
&& (id
== COL_OWNER
|| id
== COL_GROUP
||
651 st_rc
= stat(cxt
->filename
, &cxt
->st
);
656 if (is_parsable(lsblk
))
657 tt_line_set_data(ln
, col
, xstrdup(cxt
->dm_name
));
659 snprintf(buf
, sizeof(buf
), "%s (%s)",
660 cxt
->dm_name
, cxt
->name
);
661 tt_line_set_data(ln
, col
, xstrdup(buf
));
666 tt_line_set_data(ln
, col
, xstrdup(cxt
->name
));
670 tt_line_set_data(ln
, col
, xstrdup(cxt
->parent
->name
));
674 struct passwd
*pw
= st_rc
? NULL
: getpwuid(cxt
->st
.st_uid
);
676 tt_line_set_data(ln
, col
, xstrdup(pw
->pw_name
));
681 struct group
*gr
= st_rc
? NULL
: getgrgid(cxt
->st
.st_gid
);
683 tt_line_set_data(ln
, col
, xstrdup(gr
->gr_name
));
691 strmode(cxt
->st
.st_mode
, md
);
692 tt_line_set_data(ln
, col
, xstrdup(md
));
697 if (is_parsable(lsblk
))
698 snprintf(buf
, sizeof(buf
), "%u:%u", cxt
->maj
, cxt
->min
);
700 snprintf(buf
, sizeof(buf
), "%3u:%-3u", cxt
->maj
, cxt
->min
);
701 tt_line_set_data(ln
, col
, xstrdup(buf
));
706 tt_line_set_data(ln
, col
, xstrdup(cxt
->fstype
));
709 if (!(cxt
->nholders
+ cxt
->npartitions
)) {
710 if ((p
= get_device_mountpoint(cxt
)))
711 tt_line_set_data(ln
, col
, p
);
719 tt_line_set_data(ln
, col
, xstrdup(cxt
->label
));
724 tt_line_set_data(ln
, col
, xstrdup(cxt
->uuid
));
731 tt_line_set_data(ln
, col
, xstrdup(cxt
->partlabel
));
736 tt_line_set_data(ln
, col
, xstrdup(cxt
->partuuid
));
739 get_udev_properties(cxt
);
741 tt_line_set_data(ln
, col
, xstrdup(cxt
->wwn
));
744 p
= sysfs_strdup(&cxt
->sysfs
, "queue/read_ahead_kb");
746 tt_line_set_data(ln
, col
, p
);
749 tt_line_set_data(ln
, col
, is_readonly_device(cxt
) ?
750 xstrdup("1") : xstrdup("0"));
753 p
= sysfs_strdup(&cxt
->sysfs
, "removable");
754 if (!p
&& cxt
->sysfs
.parent
)
755 p
= sysfs_strdup(cxt
->sysfs
.parent
, "removable");
757 tt_line_set_data(ln
, col
, p
);
760 p
= sysfs_strdup(&cxt
->sysfs
, "queue/rotational");
762 tt_line_set_data(ln
, col
, p
);
765 p
= sysfs_strdup(&cxt
->sysfs
, "queue/add_random");
767 tt_line_set_data(ln
, col
, p
);
770 if (!cxt
->partition
&& cxt
->nslaves
== 0) {
771 p
= sysfs_strdup(&cxt
->sysfs
, "device/model");
773 tt_line_set_data(ln
, col
, p
);
777 if (!cxt
->partition
&& cxt
->nslaves
== 0) {
778 p
= sysfs_strdup(&cxt
->sysfs
, "device/rev");
780 tt_line_set_data(ln
, col
, p
);
784 if (!cxt
->partition
&& cxt
->nslaves
== 0) {
785 p
= sysfs_strdup(&cxt
->sysfs
, "device/vendor");
787 tt_line_set_data(ln
, col
, p
);
793 xasprintf(&p
, "%jd", cxt
->size
);
795 p
= size_to_human_string(SIZE_SUFFIX_1LETTER
, cxt
->size
);
797 tt_line_set_data(ln
, col
, p
);
801 if (!cxt
->partition
&& !cxt
->dm_name
) {
802 p
= sysfs_strdup(&cxt
->sysfs
, "device/state");
803 } else if (cxt
->dm_name
) {
805 if (sysfs_read_int(&cxt
->sysfs
, "dm/suspended", &x
) == 0)
806 p
= x
? xstrdup("suspended") : xstrdup("running");
809 tt_line_set_data(ln
, col
, p
);
812 p
= sysfs_strdup(&cxt
->sysfs
, "alignment_offset");
814 tt_line_set_data(ln
, col
, p
);
817 p
= sysfs_strdup(&cxt
->sysfs
, "queue/minimum_io_size");
819 tt_line_set_data(ln
, col
, p
);
822 p
= sysfs_strdup(&cxt
->sysfs
, "queue/optimal_io_size");
824 tt_line_set_data(ln
, col
, p
);
827 p
= sysfs_strdup(&cxt
->sysfs
, "queue/physical_block_size");
829 tt_line_set_data(ln
, col
, p
);
832 p
= sysfs_strdup(&cxt
->sysfs
, "queue/logical_block_size");
834 tt_line_set_data(ln
, col
, p
);
837 p
= get_scheduler(cxt
);
839 tt_line_set_data(ln
, col
, p
);
842 p
= sysfs_strdup(&cxt
->sysfs
, "queue/nr_requests");
844 tt_line_set_data(ln
, col
, p
);
849 tt_line_set_data(ln
, col
, p
);
854 if (sysfs_scsi_get_hctl(&cxt
->sysfs
, &h
, &c
, &t
, &l
) == 0) {
855 snprintf(buf
, sizeof(buf
), "%d:%d:%d:%d", h
, c
, t
, l
);
856 tt_line_set_data(ln
, col
, xstrdup(buf
));
861 p
= get_transport(cxt
);
863 tt_line_set_data(ln
, col
, p
);
866 p
= sysfs_strdup(&cxt
->sysfs
, "discard_alignment");
867 if (cxt
->discard
&& p
)
868 tt_line_set_data(ln
, col
, p
);
870 tt_line_set_data(ln
, col
, "0");
874 p
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_granularity");
878 if (sysfs_read_u64(&cxt
->sysfs
,
879 "queue/discard_granularity", &x
) == 0)
880 p
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
883 tt_line_set_data(ln
, col
, p
);
887 p
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_max_bytes");
891 if (sysfs_read_u64(&cxt
->sysfs
,
892 "queue/discard_max_bytes", &x
) == 0)
893 p
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
896 tt_line_set_data(ln
, col
, p
);
899 p
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_zeroes_data");
900 if (cxt
->discard
&& p
)
901 tt_line_set_data(ln
, col
, p
);
903 tt_line_set_data(ln
, col
, "0");
907 p
= sysfs_strdup(&cxt
->sysfs
, "queue/write_same_max_bytes");
911 if (sysfs_read_u64(&cxt
->sysfs
,
912 "queue/write_same_max_bytes", &x
) == 0)
913 p
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
915 tt_line_set_data(ln
, col
, p
? p
: "0");
920 static void print_device(struct blkdev_cxt
*cxt
, struct tt_line
*tt_parent
)
924 cxt
->tt_line
= tt_add_line(lsblk
->tt
, tt_parent
);
926 for (i
= 0; i
< ncolumns
; i
++)
927 set_tt_data(cxt
, i
, get_column_id(i
), cxt
->tt_line
);
930 static int set_cxt(struct blkdev_cxt
*cxt
,
931 struct blkdev_cxt
*parent
,
932 struct blkdev_cxt
*wholedisk
,
937 cxt
->parent
= parent
;
938 cxt
->name
= xstrdup(name
);
939 cxt
->partition
= wholedisk
!= NULL
;
941 cxt
->filename
= get_device_path(cxt
);
942 if (!cxt
->filename
) {
943 warnx(_("%s: failed to get device path"), name
);
947 devno
= sysfs_devname_to_devno(name
, wholedisk
? wholedisk
->name
: NULL
);
950 warnx(_("%s: unknown device name"), name
);
954 if (lsblk
->inverse
) {
955 if (sysfs_init(&cxt
->sysfs
, devno
, wholedisk
? &wholedisk
->sysfs
: NULL
)) {
956 warnx(_("%s: failed to initialize sysfs handler"), name
);
960 parent
->sysfs
.parent
= &cxt
->sysfs
;
962 if (sysfs_init(&cxt
->sysfs
, devno
, parent
? &parent
->sysfs
: NULL
)) {
963 warnx(_("%s: failed to initialize sysfs handler"), name
);
968 cxt
->maj
= major(devno
);
969 cxt
->min
= minor(devno
);
972 if (sysfs_read_u64(&cxt
->sysfs
, "size", &cxt
->size
) == 0) /* in sectors */
973 cxt
->size
<<= 9; /* in bytes */
975 sysfs_read_int(&cxt
->sysfs
, "queue/discard_granularity", &cxt
->discard
);
977 /* Ignore devices of zero size */
978 if (!lsblk
->all_devices
&& cxt
->size
== 0)
982 cxt
->dm_name
= sysfs_strdup(&cxt
->sysfs
, "dm/name");
984 warnx(_("%s: failed to get dm name"), name
);
989 cxt
->npartitions
= sysfs_count_partitions(&cxt
->sysfs
, name
);
990 cxt
->nholders
= sysfs_count_dirents(&cxt
->sysfs
, "holders");
991 cxt
->nslaves
= sysfs_count_dirents(&cxt
->sysfs
, "slaves");
993 /* ignore non-SCSI devices */
994 if (lsblk
->scsi
&& sysfs_scsi_get_hctl(&cxt
->sysfs
, NULL
, NULL
, NULL
, NULL
))
1000 static int process_blkdev(struct blkdev_cxt
*cxt
, struct blkdev_cxt
*parent
,
1001 int do_partitions
, const char *part_name
);
1004 * List device partitions if any.
1006 static int list_partitions(struct blkdev_cxt
*wholedisk_cxt
, struct blkdev_cxt
*parent_cxt
,
1007 const char *part_name
)
1011 struct blkdev_cxt part_cxt
= {};
1014 assert(wholedisk_cxt
);
1017 * Do not process further if there are no partitions for
1018 * this device or the device itself is a partition.
1020 if (!wholedisk_cxt
->npartitions
|| wholedisk_cxt
->partition
)
1023 dir
= sysfs_opendir(&wholedisk_cxt
->sysfs
, NULL
);
1025 err(EXIT_FAILURE
, _("failed to open device directory in sysfs"));
1027 while ((d
= xreaddir(dir
))) {
1028 /* Process particular partition only? */
1029 if (part_name
&& strcmp(part_name
, d
->d_name
))
1032 if (!(sysfs_is_partition_dirent(dir
, d
, wholedisk_cxt
->name
)))
1035 if (lsblk
->inverse
) {
1042 if (set_cxt(&part_cxt
, parent_cxt
, wholedisk_cxt
, d
->d_name
))
1045 if (!parent_cxt
&& part_cxt
.nholders
)
1048 wholedisk_cxt
->parent
= &part_cxt
;
1049 print_device(&part_cxt
, parent_cxt
? parent_cxt
->tt_line
: NULL
);
1051 process_blkdev(wholedisk_cxt
, &part_cxt
, 0, NULL
);
1059 int ps
= set_cxt(&part_cxt
, wholedisk_cxt
, wholedisk_cxt
, d
->d_name
);
1061 /* Print whole disk only once */
1063 print_device(wholedisk_cxt
, parent_cxt
? parent_cxt
->tt_line
: NULL
);
1064 if (ps
== 0 && !lsblk
->nodeps
)
1065 process_blkdev(&part_cxt
, wholedisk_cxt
, 0, NULL
);
1068 reset_blkdev_cxt(&part_cxt
);
1076 static int get_wholedisk_from_partition_dirent(DIR *dir
, const char *dirname
,
1077 struct dirent
*d
, struct blkdev_cxt
*cxt
)
1079 char path
[PATH_MAX
];
1083 if ((len
= readlink_at(dirfd(dir
), dirname
,
1084 d
->d_name
, path
, sizeof(path
) - 1)) < 0)
1089 /* The path ends with ".../<device>/<partition>" */
1090 p
= strrchr(path
, '/');
1095 p
= strrchr(path
, '/');
1100 return set_cxt(cxt
, NULL
, NULL
, p
);
1104 * List device dependencies: partitions, holders (inverse = 0) or slaves (inverse = 1).
1106 static int list_deps(struct blkdev_cxt
*cxt
)
1110 struct blkdev_cxt dep
= {};
1111 char dirname
[PATH_MAX
];
1112 const char *depname
;
1119 if (!(lsblk
->inverse
? cxt
->nslaves
: cxt
->nholders
))
1122 depname
= lsblk
->inverse
? "slaves" : "holders";
1123 dir
= sysfs_opendir(&cxt
->sysfs
, depname
);
1127 snprintf(dirname
, sizeof(dirname
), "%s/%s", cxt
->sysfs
.dir_path
, depname
);
1129 while ((d
= xreaddir(dir
))) {
1130 /* Is the dependency a partition? */
1131 if (sysfs_is_partition_dirent(dir
, d
, NULL
)) {
1132 if (!get_wholedisk_from_partition_dirent(dir
, dirname
, d
, &dep
))
1133 process_blkdev(&dep
, cxt
, 1, d
->d_name
);
1135 /* The dependency is a whole device. */
1136 else if (!set_cxt(&dep
, cxt
, NULL
, d
->d_name
))
1137 process_blkdev(&dep
, cxt
, 1, NULL
);
1139 reset_blkdev_cxt(&dep
);
1146 static int process_blkdev(struct blkdev_cxt
*cxt
, struct blkdev_cxt
*parent
,
1147 int do_partitions
, const char *part_name
)
1149 if (do_partitions
&& cxt
->npartitions
)
1150 return list_partitions(cxt
, parent
, part_name
);
1152 print_device(cxt
, parent
? parent
->tt_line
: NULL
);
1153 return list_deps(cxt
);
1156 /* Iterate devices in sysfs */
1157 static int iterate_block_devices(void)
1161 struct blkdev_cxt cxt
= {};
1163 if (!(dir
= opendir(_PATH_SYS_BLOCK
)))
1164 return EXIT_FAILURE
;
1166 while ((d
= xreaddir(dir
))) {
1167 if (set_cxt(&cxt
, NULL
, NULL
, d
->d_name
))
1170 if (is_maj_excluded(cxt
.maj
) || !is_maj_included(cxt
.maj
))
1173 /* Skip devices in the middle of dependency tree. */
1174 if ((lsblk
->inverse
? cxt
.nholders
: cxt
.nslaves
) > 0)
1177 process_blkdev(&cxt
, NULL
, 1, NULL
);
1179 reset_blkdev_cxt(&cxt
);
1184 return EXIT_SUCCESS
;
1187 static char *devno_to_sysfs_name(dev_t devno
, char *devname
, char *buf
, size_t buf_size
)
1189 char path
[PATH_MAX
];
1192 if (!sysfs_devno_path(devno
, path
, sizeof(path
))) {
1193 warn(_("%s: failed to compose sysfs path"), devname
);
1197 len
= readlink(path
, buf
, buf_size
- 1);
1199 warn(_("%s: failed to read link"), path
);
1204 return xstrdup(strrchr(buf
, '/') + 1);
1207 static int process_one_device(char *devname
)
1209 struct blkdev_cxt parent
= {}, cxt
= {};
1211 char buf
[PATH_MAX
+ 1], *name
, *diskname
= NULL
;
1214 int status
= EXIT_FAILURE
;
1216 if (stat(devname
, &st
) || !S_ISBLK(st
.st_mode
)) {
1217 warnx(_("%s: not a block device"), devname
);
1218 return EXIT_FAILURE
;
1221 if (!(name
= devno_to_sysfs_name(st
.st_rdev
, devname
, buf
, PATH_MAX
))) {
1222 warn(_("%s: failed to get sysfs name"), devname
);
1223 return EXIT_FAILURE
;
1226 if (!strncmp(name
, "dm-", 3)) {
1227 /* dm mapping is never a real partition! */
1230 if (blkid_devno_to_wholedisk(st
.st_rdev
, buf
, sizeof(buf
), &disk
)) {
1231 warn(_("%s: failed to get whole-disk device number"), devname
);
1232 return EXIT_FAILURE
;
1235 real_part
= st
.st_rdev
!= disk
;
1240 * Device is not a partition.
1242 if (set_cxt(&cxt
, NULL
, NULL
, name
))
1244 process_blkdev(&cxt
, NULL
, !lsblk
->inverse
, NULL
);
1247 * Partition, read sysfs name of the device.
1249 if (set_cxt(&parent
, NULL
, NULL
, diskname
))
1251 if (set_cxt(&cxt
, &parent
, &parent
, name
))
1255 process_blkdev(&parent
, &cxt
, 1, cxt
.name
);
1257 process_blkdev(&cxt
, &parent
, 1, NULL
);
1260 status
= EXIT_SUCCESS
;
1263 reset_blkdev_cxt(&cxt
);
1266 reset_blkdev_cxt(&parent
);
1271 static void parse_excludes(const char *str0
)
1273 const char *str
= str0
;
1275 while (str
&& *str
) {
1280 n
= strtoul(str
, &end
, 10);
1282 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1283 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1284 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1285 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1286 excludes
[nexcludes
++] = n
;
1288 if (nexcludes
== ARRAY_SIZE(excludes
))
1289 /* TRANSLATORS: The standard value for %d is 256. */
1290 errx(EXIT_FAILURE
, _("the list of excluded devices is "
1291 "too large (limit is %d devices)"),
1292 (int)ARRAY_SIZE(excludes
));
1294 str
= end
&& *end
? end
+ 1 : NULL
;
1298 static void parse_includes(const char *str0
)
1300 const char *str
= str0
;
1302 while (str
&& *str
) {
1307 n
= strtoul(str
, &end
, 10);
1309 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1310 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1311 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1312 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1313 includes
[nincludes
++] = n
;
1315 if (nincludes
== ARRAY_SIZE(includes
))
1316 /* TRANSLATORS: The standard value for %d is 256. */
1317 errx(EXIT_FAILURE
, _("the list of included devices is "
1318 "too large (limit is %d devices)"),
1319 (int)ARRAY_SIZE(includes
));
1320 str
= end
&& *end
? end
+ 1 : NULL
;
1324 static void __attribute__((__noreturn__
)) help(FILE *out
)
1330 " %s [options] [<device> ...]\n"), program_invocation_short_name
);
1334 " -a, --all print all devices\n"
1335 " -b, --bytes print SIZE in bytes rather than in human readable format\n"
1336 " -d, --nodeps don't print slaves or holders\n"
1337 " -D, --discard print discard capabilities\n"
1338 " -e, --exclude <list> exclude devices by major number (default: RAM disks)\n"
1339 " -I, --include <list> show only devices with specified major numbers\n"
1340 " -f, --fs output info about filesystems\n"
1341 " -h, --help usage information (this)\n"
1342 " -i, --ascii use ascii characters only\n"
1343 " -m, --perms output info about permissions\n"
1344 " -l, --list use list format output\n"
1345 " -n, --noheadings don't print headings\n"
1346 " -o, --output <list> output columns\n"
1347 " -P, --pairs use key=\"value\" output format\n"
1348 " -r, --raw use raw output format\n"
1349 " -s, --inverse inverse dependencies\n"
1350 " -t, --topology output info about topology\n"
1351 " -S, --scsi output info about SCSI devices\n"
1352 " -V, --version output version information and exit\n"));
1354 fprintf(out
, _("\nAvailable columns (for --output):\n"));
1356 for (i
= 0; i
< NCOLS
; i
++)
1357 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
1359 fprintf(out
, USAGE_MAN_TAIL("lsblk(8)"));
1361 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
1364 static void check_sysdevblock(void)
1366 if (access(_PATH_SYS_DEVBLOCK
, R_OK
) != 0)
1367 err(EXIT_FAILURE
, _("failed to access sysfs directory: %s"),
1368 _PATH_SYS_DEVBLOCK
);
1371 int main(int argc
, char *argv
[])
1374 int tt_flags
= TT_FL_TREE
;
1375 int i
, c
, status
= EXIT_FAILURE
;
1376 char *outarg
= NULL
;
1378 static const struct option longopts
[] = {
1379 { "all", 0, 0, 'a' },
1380 { "bytes", 0, 0, 'b' },
1381 { "nodeps", 0, 0, 'd' },
1382 { "discard", 0, 0, 'D' },
1383 { "help", 0, 0, 'h' },
1384 { "output", 1, 0, 'o' },
1385 { "perms", 0, 0, 'm' },
1386 { "noheadings", 0, 0, 'n' },
1387 { "list", 0, 0, 'l' },
1388 { "ascii", 0, 0, 'i' },
1389 { "raw", 0, 0, 'r' },
1390 { "inverse", 0, 0, 's' },
1391 { "fs", 0, 0, 'f' },
1392 { "exclude", 1, 0, 'e' },
1393 { "include", 1, 0, 'I' },
1394 { "topology", 0, 0, 't' },
1395 { "pairs", 0, 0, 'P' },
1396 { "scsi", 0, 0, 'S' },
1397 { "version", 0, 0, 'V' },
1401 static const ul_excl_t excl
[] = { /* rows and cols in in ASCII order */
1406 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1408 setlocale(LC_ALL
, "");
1409 bindtextdomain(PACKAGE
, LOCALEDIR
);
1410 textdomain(PACKAGE
);
1411 atexit(close_stdout
);
1414 memset(lsblk
, 0, sizeof(*lsblk
));
1416 while((c
= getopt_long(argc
, argv
,
1417 "abdDe:fhlnmo:PiI:rstVS", longopts
, NULL
)) != -1) {
1419 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1423 lsblk
->all_devices
= 1;
1432 columns
[ncolumns
++] = COL_NAME
;
1433 columns
[ncolumns
++] = COL_DALIGN
;
1434 columns
[ncolumns
++] = COL_DGRAN
;
1435 columns
[ncolumns
++] = COL_DMAX
;
1436 columns
[ncolumns
++] = COL_DZERO
;
1439 parse_excludes(optarg
);
1445 tt_flags
&= ~TT_FL_TREE
; /* disable the default */
1448 tt_flags
|= TT_FL_NOHEADINGS
;
1454 tt_flags
|= TT_FL_EXPORT
;
1455 tt_flags
&= ~TT_FL_TREE
; /* disable the default */
1458 tt_flags
|= TT_FL_ASCII
;
1461 parse_includes(optarg
);
1464 tt_flags
&= ~TT_FL_TREE
; /* disable the default */
1465 tt_flags
|= TT_FL_RAW
; /* enable raw */
1471 columns
[ncolumns
++] = COL_NAME
;
1472 columns
[ncolumns
++] = COL_FSTYPE
;
1473 columns
[ncolumns
++] = COL_LABEL
;
1474 columns
[ncolumns
++] = COL_UUID
;
1475 columns
[ncolumns
++] = COL_TARGET
;
1478 columns
[ncolumns
++] = COL_NAME
;
1479 columns
[ncolumns
++] = COL_SIZE
;
1480 columns
[ncolumns
++] = COL_OWNER
;
1481 columns
[ncolumns
++] = COL_GROUP
;
1482 columns
[ncolumns
++] = COL_MODE
;
1485 columns
[ncolumns
++] = COL_NAME
;
1486 columns
[ncolumns
++] = COL_ALIOFF
;
1487 columns
[ncolumns
++] = COL_MINIO
;
1488 columns
[ncolumns
++] = COL_OPTIO
;
1489 columns
[ncolumns
++] = COL_PHYSEC
;
1490 columns
[ncolumns
++] = COL_LOGSEC
;
1491 columns
[ncolumns
++] = COL_ROTA
;
1492 columns
[ncolumns
++] = COL_SCHED
;
1493 columns
[ncolumns
++] = COL_RQ_SIZE
;
1494 columns
[ncolumns
++] = COL_RA
;
1495 columns
[ncolumns
++] = COL_WSAME
;
1500 columns
[ncolumns
++] = COL_NAME
;
1501 columns
[ncolumns
++] = COL_HCTL
;
1502 columns
[ncolumns
++] = COL_TYPE
;
1503 columns
[ncolumns
++] = COL_VENDOR
;
1504 columns
[ncolumns
++] = COL_MODEL
;
1505 columns
[ncolumns
++] = COL_REV
;
1506 columns
[ncolumns
++] = COL_TRANSPORT
;
1509 printf(_("%s from %s\n"), program_invocation_short_name
,
1511 return EXIT_SUCCESS
;
1517 check_sysdevblock();
1520 columns
[ncolumns
++] = COL_NAME
;
1521 columns
[ncolumns
++] = COL_MAJMIN
;
1522 columns
[ncolumns
++] = COL_RM
;
1523 columns
[ncolumns
++] = COL_SIZE
;
1524 columns
[ncolumns
++] = COL_RO
;
1525 columns
[ncolumns
++] = COL_TYPE
;
1526 columns
[ncolumns
++] = COL_TARGET
;
1529 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
1530 &ncolumns
, column_name_to_id
) < 0)
1531 return EXIT_FAILURE
;
1533 if (nexcludes
== 0 && nincludes
== 0)
1534 excludes
[nexcludes
++] = 1; /* default: ignore RAM disks */
1539 * initialize output columns
1541 if (!(lsblk
->tt
= tt_new_table(tt_flags
)))
1542 errx(EXIT_FAILURE
, _("failed to initialize output table"));
1544 for (i
= 0; i
< ncolumns
; i
++) {
1545 struct colinfo
*ci
= get_column_info(i
);
1548 if (!(tt_flags
& TT_FL_TREE
) && get_column_id(i
) == COL_NAME
)
1551 if (!tt_define_column(lsblk
->tt
, ci
->name
, ci
->whint
, fl
)) {
1552 warn(_("failed to initialize output column"));
1558 status
= iterate_block_devices();
1559 else while (optind
< argc
)
1560 status
= process_one_device(argv
[optind
++]);
1562 tt_print_table(lsblk
->tt
);
1565 tt_free_table(lsblk
->tt
);
1566 mnt_free_table(mtab
);
1567 mnt_free_table(swaps
);
1568 mnt_free_cache(mntcache
);