2 * lsblk(8) - list block devices
4 * Copyright (C) 2010-2018 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.
27 #include <sys/types.h>
32 #include <sys/ioctl.h>
43 #include "pathnames.h"
45 #include "canonicalize.h"
50 #include "closestream.h"
52 #include "fileutils.h"
56 UL_DEBUG_DEFINE_MASK(lsblk
);
57 UL_DEBUG_DEFINE_MASKNAMES(lsblk
) = UL_DEBUG_EMPTY_MASKNAMES
;
59 #define LSBLK_EXIT_SOMEOK 64
60 #define LSBLK_EXIT_ALLFAILED 32
62 static int column_id_to_number(int id
);
121 /* basic table settings */
123 LSBLK_ASCII
= (1 << 0),
124 LSBLK_RAW
= (1 << 1),
125 LSBLK_NOHEADINGS
= (1 << 2),
126 LSBLK_EXPORT
= (1 << 3),
127 LSBLK_TREE
= (1 << 4),
128 LSBLK_JSON
= (1 << 5),
131 /* Types used for qsort() and JSON */
133 COLTYPE_STR
= 0, /* default */
134 COLTYPE_NUM
= 1, /* always u64 number */
135 COLTYPE_SORTNUM
= 2, /* string on output, u64 for qsort() */
136 COLTYPE_SIZE
= 3, /* srring by default, number when --bytes */
137 COLTYPE_BOOL
= 4 /* 0 or 1 */
142 const char *name
; /* header */
143 double whint
; /* width hint (N < 1 is in percent of termwidth) */
144 int flags
; /* SCOLS_FL_* */
146 int type
; /* COLTYPE_* */
149 /* columns descriptions */
150 static struct colinfo infos
[] = {
151 [COL_NAME
] = { "NAME", 0.25, SCOLS_FL_NOEXTREMES
, N_("device name") },
152 [COL_KNAME
] = { "KNAME", 0.3, 0, N_("internal kernel device name") },
153 [COL_PKNAME
] = { "PKNAME", 0.3, 0, N_("internal parent kernel device name") },
154 [COL_PATH
] = { "PATH", 0.3, 0, N_("path to the device node") },
155 [COL_MAJMIN
] = { "MAJ:MIN", 6, 0, N_("major:minor device number"), COLTYPE_SORTNUM
},
157 [COL_FSAVAIL
] = { "FSAVAIL", 5, SCOLS_FL_RIGHT
, N_("filesystem size available") },
158 [COL_FSSIZE
] = { "FSSIZE", 5, SCOLS_FL_RIGHT
, N_("filesystem size") },
159 [COL_FSTYPE
] = { "FSTYPE", 0.1, SCOLS_FL_TRUNC
, N_("filesystem type") },
160 [COL_FSUSED
] = { "FSUSED", 5, SCOLS_FL_RIGHT
, N_("filesystem size used") },
161 [COL_FSUSEPERC
] = { "FSUSE%", 3, SCOLS_FL_RIGHT
, N_("filesystem use percentage") },
162 [COL_FSVERSION
] = { "FSVER", 0.1, SCOLS_FL_TRUNC
, N_("filesystem version") },
164 [COL_TARGET
] = { "MOUNTPOINT", 0.10, SCOLS_FL_TRUNC
, N_("where the device is mounted") },
165 [COL_LABEL
] = { "LABEL", 0.1, 0, N_("filesystem LABEL") },
166 [COL_UUID
] = { "UUID", 36, 0, N_("filesystem UUID") },
168 [COL_PTUUID
] = { "PTUUID", 36, 0, N_("partition table identifier (usually UUID)") },
169 [COL_PTTYPE
] = { "PTTYPE", 0.1, 0, N_("partition table type") },
171 [COL_PARTTYPE
] = { "PARTTYPE", 36, 0, N_("partition type UUID") },
172 [COL_PARTLABEL
] = { "PARTLABEL", 0.1, 0, N_("partition LABEL") },
173 [COL_PARTUUID
] = { "PARTUUID", 36, 0, N_("partition UUID") },
174 [COL_PARTFLAGS
] = { "PARTFLAGS", 36, 0, N_("partition flags") },
176 [COL_RA
] = { "RA", 3, SCOLS_FL_RIGHT
, N_("read-ahead of the device"), COLTYPE_NUM
},
177 [COL_RO
] = { "RO", 1, SCOLS_FL_RIGHT
, N_("read-only device"), COLTYPE_BOOL
},
178 [COL_RM
] = { "RM", 1, SCOLS_FL_RIGHT
, N_("removable device"), COLTYPE_BOOL
},
179 [COL_HOTPLUG
]= { "HOTPLUG", 1, SCOLS_FL_RIGHT
, N_("removable or hotplug device (usb, pcmcia, ...)"), COLTYPE_BOOL
},
180 [COL_ROTA
] = { "ROTA", 1, SCOLS_FL_RIGHT
, N_("rotational device"), COLTYPE_BOOL
},
181 [COL_RAND
] = { "RAND", 1, SCOLS_FL_RIGHT
, N_("adds randomness"), COLTYPE_BOOL
},
182 [COL_MODEL
] = { "MODEL", 0.1, SCOLS_FL_TRUNC
, N_("device identifier") },
183 [COL_SERIAL
] = { "SERIAL", 0.1, SCOLS_FL_TRUNC
, N_("disk serial number") },
184 [COL_SIZE
] = { "SIZE", 5, SCOLS_FL_RIGHT
, N_("size of the device"), COLTYPE_SIZE
},
185 [COL_STATE
] = { "STATE", 7, SCOLS_FL_TRUNC
, N_("state of the device") },
186 [COL_OWNER
] = { "OWNER", 0.1, SCOLS_FL_TRUNC
, N_("user name"), },
187 [COL_GROUP
] = { "GROUP", 0.1, SCOLS_FL_TRUNC
, N_("group name") },
188 [COL_MODE
] = { "MODE", 10, 0, N_("device node permissions") },
189 [COL_ALIOFF
] = { "ALIGNMENT", 6, SCOLS_FL_RIGHT
, N_("alignment offset"), COLTYPE_NUM
},
190 [COL_MINIO
] = { "MIN-IO", 6, SCOLS_FL_RIGHT
, N_("minimum I/O size"), COLTYPE_NUM
},
191 [COL_OPTIO
] = { "OPT-IO", 6, SCOLS_FL_RIGHT
, N_("optimal I/O size"), COLTYPE_NUM
},
192 [COL_PHYSEC
] = { "PHY-SEC", 7, SCOLS_FL_RIGHT
, N_("physical sector size"), COLTYPE_NUM
},
193 [COL_LOGSEC
] = { "LOG-SEC", 7, SCOLS_FL_RIGHT
, N_("logical sector size"), COLTYPE_NUM
},
194 [COL_SCHED
] = { "SCHED", 0.1, 0, N_("I/O scheduler name") },
195 [COL_RQ_SIZE
]= { "RQ-SIZE", 5, SCOLS_FL_RIGHT
, N_("request queue size"), COLTYPE_NUM
},
196 [COL_TYPE
] = { "TYPE", 4, 0, N_("device type") },
197 [COL_DALIGN
] = { "DISC-ALN", 6, SCOLS_FL_RIGHT
, N_("discard alignment offset"), COLTYPE_NUM
},
198 [COL_DGRAN
] = { "DISC-GRAN", 6, SCOLS_FL_RIGHT
, N_("discard granularity"), COLTYPE_SIZE
},
199 [COL_DMAX
] = { "DISC-MAX", 6, SCOLS_FL_RIGHT
, N_("discard max bytes"), COLTYPE_SIZE
},
200 [COL_DZERO
] = { "DISC-ZERO", 1, SCOLS_FL_RIGHT
, N_("discard zeroes data"), COLTYPE_BOOL
},
201 [COL_WSAME
] = { "WSAME", 6, SCOLS_FL_RIGHT
, N_("write same max bytes"), COLTYPE_SIZE
},
202 [COL_WWN
] = { "WWN", 18, 0, N_("unique storage identifier") },
203 [COL_HCTL
] = { "HCTL", 10, 0, N_("Host:Channel:Target:Lun for SCSI") },
204 [COL_TRANSPORT
] = { "TRAN", 6, 0, N_("device transport type") },
205 [COL_SUBSYS
] = { "SUBSYSTEMS", 0.1, SCOLS_FL_NOEXTREMES
, N_("de-duplicated chain of subsystems") },
206 [COL_REV
] = { "REV", 4, SCOLS_FL_RIGHT
, N_("device revision") },
207 [COL_VENDOR
] = { "VENDOR", 0.1, SCOLS_FL_TRUNC
, N_("device vendor") },
208 [COL_ZONED
] = { "ZONED", 0.3, 0, N_("zone model") },
211 struct lsblk
*lsblk
; /* global handler */
214 * columns[] array specifies all currently wanted output column. The columns
215 * are defined by infos[] array and you can specify (on command line) each
216 * column twice. That's enough, dynamically allocated array of the columns is
217 * unnecessary overkill and over-engineering in this case
219 static int columns
[ARRAY_SIZE(infos
) * 2];
220 static size_t ncolumns
;
222 static inline void add_column(int id
)
224 if (ncolumns
>= ARRAY_SIZE(columns
))
225 errx(EXIT_FAILURE
, _("too many columns specified, "
226 "the limit is %zu columns"),
227 ARRAY_SIZE(columns
) - 1);
228 columns
[ ncolumns
++ ] = id
;
231 static inline void add_uniq_column(int id
)
233 if (column_id_to_number(id
) < 0)
237 static void lsblk_init_debug(void)
239 __UL_INIT_DEBUG_FROM_ENV(lsblk
, LSBLK_DEBUG_
, 0, LSBLK_DEBUG
);
243 * exclude/include devices filter based on major device numbers
245 static int excludes
[256];
246 static size_t nexcludes
;
248 static int includes
[256];
249 static size_t nincludes
;
251 static int is_maj_excluded(int maj
)
255 assert(ARRAY_SIZE(excludes
) > nexcludes
);
258 return 0; /* filter not enabled, device not excluded */
260 for (i
= 0; i
< nexcludes
; i
++) {
261 if (excludes
[i
] == maj
) {
262 DBG(FILTER
, ul_debug("exclude: maj=%d", maj
));
269 static int is_maj_included(int maj
)
273 assert(ARRAY_SIZE(includes
) > nincludes
);
276 return 1; /* filter not enabled, device is included */
278 for (i
= 0; i
< nincludes
; i
++) {
279 if (includes
[i
] == maj
) {
280 DBG(FILTER
, ul_debug("include: maj=%d", maj
));
287 /* Converts column sequential number to column ID (COL_*) */
288 static int get_column_id(int num
)
291 assert((size_t) num
< ncolumns
);
292 assert(columns
[num
] < (int) ARRAY_SIZE(infos
));
296 /* Returns column description for the column sequential number */
297 static struct colinfo
*get_column_info(int num
)
299 return &infos
[ get_column_id(num
) ];
302 /* Converts column name (as defined in the infos[] to the column ID */
303 static int column_name_to_id(const char *name
, size_t namesz
)
307 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
308 const char *cn
= infos
[i
].name
;
310 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
313 warnx(_("unknown column: %s"), name
);
317 /* Converts column ID (COL_*) to column sequential number */
318 static int column_id_to_number(int id
)
322 for (i
= 0; i
< ncolumns
; i
++)
323 if (columns
[i
] == id
)
328 /* Checks for DM prefix in the device name */
329 static int is_dm(const char *name
)
331 return strncmp(name
, "dm-", 3) ? 0 : 1;
334 /* Returns full pat to the device node (TODO: what about sysfs_blkdev_get_path()) */
335 static char *get_device_path(struct lsblk_device
*dev
)
342 if (is_dm(dev
->name
))
343 return __canonicalize_dm_name(lsblk
->sysroot
, dev
->name
);
345 snprintf(path
, sizeof(path
), "/dev/%s", dev
->name
);
346 sysfs_devname_sys_to_dev(path
);
347 return xstrdup(path
);
350 static int is_readonly_device(struct lsblk_device
*dev
)
354 if (ul_path_scanf(dev
->sysfs
, "ro", "%d", &ro
) == 1)
357 /* fallback if "ro" attribute does not exist */
358 fd
= open(dev
->filename
, O_RDONLY
);
360 if (ioctl(fd
, BLKROGET
, &ro
) != 0)
367 static char *get_scheduler(struct lsblk_device
*dev
)
370 char *p
, *res
= NULL
;
372 if (ul_path_read_buffer(dev
->sysfs
, buf
, sizeof(buf
), "queue/scheduler") == 0)
374 p
= strchr(buf
, '[');
377 p
= strchr(res
, ']');
387 static char *get_type(struct lsblk_device
*dev
)
389 char *res
= NULL
, *p
;
391 if (device_is_partition(dev
))
392 return xstrdup("part");
394 if (is_dm(dev
->name
)) {
395 char *dm_uuid
= NULL
;
397 /* The DM_UUID prefix should be set to subsystem owning
398 * the device - LVM, CRYPT, DMRAID, MPATH, PART */
399 if (ul_path_read_string(dev
->sysfs
, &dm_uuid
, "dm/uuid") > 0
402 char *dm_uuid_prefix
= strsep(&tmp
, "-");
404 if (dm_uuid_prefix
) {
405 /* kpartx hack to remove partition number */
406 if (strncasecmp(dm_uuid_prefix
, "part", 4) == 0)
407 dm_uuid_prefix
[4] = '\0';
409 res
= xstrdup(dm_uuid_prefix
);
415 /* No UUID or no prefix - just mark it as DM device */
418 } else if (!strncmp(dev
->name
, "loop", 4)) {
419 res
= xstrdup("loop");
421 } else if (!strncmp(dev
->name
, "md", 2)) {
422 char *md_level
= NULL
;
424 ul_path_read_string(dev
->sysfs
, &md_level
, "md/level");
425 res
= md_level
? md_level
: xstrdup("md");
428 const char *type
= NULL
;
431 if (ul_path_read_s32(dev
->sysfs
, &x
, "device/type") == 0)
432 type
= blkdev_scsi_type_to_name(x
);
438 for (p
= res
; p
&& *p
; p
++)
439 *p
= tolower((unsigned char) *p
);
443 /* Thanks to lsscsi code for idea of detection logic used here */
444 static char *get_transport(struct lsblk_device
*dev
)
446 struct path_cxt
*sysfs
= dev
->sysfs
;
448 const char *trans
= NULL
;
451 /* SCSI - Serial Peripheral Interface */
452 if (sysfs_blkdev_scsi_host_is(sysfs
, "spi"))
455 /* FC/FCoE - Fibre Channel / Fibre Channel over Ethernet */
456 else if (sysfs_blkdev_scsi_host_is(sysfs
, "fc")) {
457 attr
= sysfs_blkdev_scsi_host_strdup_attribute(sysfs
, "fc", "symbolic_name");
460 trans
= strstr(attr
, " over ") ? "fcoe" : "fc";
464 /* SAS - Serial Attached SCSI */
465 else if (sysfs_blkdev_scsi_host_is(sysfs
, "sas") ||
466 sysfs_blkdev_scsi_has_attribute(sysfs
, "sas_device"))
470 /* SBP - Serial Bus Protocol (FireWire) */
471 else if (sysfs_blkdev_scsi_has_attribute(sysfs
, "ieee1394_id"))
475 else if (sysfs_blkdev_scsi_host_is(sysfs
, "iscsi"))
478 /* USB - Universal Serial Bus */
479 else if (sysfs_blkdev_scsi_path_contains(sysfs
, "usb"))
483 else if (sysfs_blkdev_scsi_host_is(sysfs
, "scsi")) {
484 attr
= sysfs_blkdev_scsi_host_strdup_attribute(sysfs
, "scsi", "proc_name");
487 if (!strncmp(attr
, "ahci", 4) || !strncmp(attr
, "sata", 4))
489 else if (strstr(attr
, "ata"))
493 } else if (strncmp(dev
->name
, "nvme", 4) == 0)
496 return trans
? xstrdup(trans
) : NULL
;
499 static char *get_subsystems(struct lsblk_device
*dev
)
502 char *sub
, *chain
, *res
= NULL
;
503 size_t len
= 0, last
= 0;
505 chain
= sysfs_blkdev_get_devchain(dev
->sysfs
, path
, sizeof(path
));
509 while (sysfs_blkdev_next_subsystem(dev
->sysfs
, chain
, &sub
) == 0) {
512 /* don't create "block:scsi:scsi", but "block:scsi" */
513 if (len
&& strcmp(res
+ last
, sub
) == 0) {
519 res
= xrealloc(res
, len
+ sz
+ 2);
523 memcpy(res
+ len
, sub
, sz
+ 1);
533 #define is_parsable(_l) (scols_table_is_raw((_l)->table) || \
534 scols_table_is_export((_l)->table) || \
535 scols_table_is_json((_l)->table))
537 static char *mk_name(const char *name
)
543 xasprintf(&p
, "/dev/%s", name
);
547 sysfs_devname_sys_to_dev(p
);
551 static char *mk_dm_name(const char *name
)
557 xasprintf(&p
, "/dev/mapper/%s", name
);
563 /* stores data to scols cell userdata (invisible and independent on output)
564 * to make the original values accessible for sort functions
566 static void set_sortdata_u64(struct libscols_line
*ln
, int col
, uint64_t x
)
568 struct libscols_cell
*ce
= scols_line_get_cell(ln
, col
);
573 data
= xmalloc(sizeof(uint64_t));
575 scols_cell_set_userdata(ce
, data
);
578 /* do not modify *data on any error */
579 static void str2u64(const char *str
, uint64_t *data
)
585 if (str
== NULL
|| *str
== '\0')
587 num
= strtoumax(str
, &end
, 10);
589 if (errno
|| str
== end
|| (end
&& *end
))
594 static void unref_sortdata(struct libscols_table
*tb
)
596 struct libscols_iter
*itr
;
597 struct libscols_line
*ln
;
599 if (!tb
|| !lsblk
->sort_col
)
601 itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
604 while (scols_table_next_line(tb
, itr
, &ln
) == 0) {
605 struct libscols_cell
*ce
= scols_line_get_column_cell(ln
,
607 void *data
= scols_cell_get_userdata(ce
);
611 scols_free_iter(itr
);
614 static char *get_vfs_attribute(struct lsblk_device
*dev
, int id
)
617 uint64_t vfs_attr
= 0;
620 if (!dev
->fsstat
.f_blocks
) {
621 mnt
= lsblk_device_get_mountpoint(dev
);
622 if (!mnt
|| dev
->is_swap
)
624 if (statvfs(mnt
, &dev
->fsstat
) != 0)
630 vfs_attr
= dev
->fsstat
.f_frsize
* dev
->fsstat
.f_blocks
;
633 vfs_attr
= dev
->fsstat
.f_frsize
* dev
->fsstat
.f_bavail
;
636 vfs_attr
= dev
->fsstat
.f_frsize
* (dev
->fsstat
.f_blocks
- dev
->fsstat
.f_bfree
);
639 if (dev
->fsstat
.f_blocks
== 0)
642 xasprintf(&sizestr
, "%.0f%%",
643 (double)(dev
->fsstat
.f_blocks
- dev
->fsstat
.f_bfree
) /
644 dev
->fsstat
.f_blocks
* 100);
649 sizestr
= xstrdup("0");
650 else if (lsblk
->bytes
)
651 xasprintf(&sizestr
, "%ju", vfs_attr
);
653 sizestr
= size_to_human_string(SIZE_SUFFIX_1LETTER
, vfs_attr
);
658 static struct stat
*device_get_stat(struct lsblk_device
*dev
)
661 && stat(dev
->filename
, &dev
->st
) != 0)
667 static int is_removable_device(struct lsblk_device
*dev
, struct lsblk_device
*parent
)
671 if (dev
->removable
!= -1)
673 if (ul_path_scanf(dev
->sysfs
, "removable", "%d", &dev
->removable
) == 1)
677 pc
= sysfs_blkdev_get_parent(dev
->sysfs
);
681 /* dev is partition and parent is whole-disk */
682 if (pc
== parent
->sysfs
)
683 dev
->removable
= is_removable_device(parent
, NULL
);
685 /* parent is something else, use sysfs parent */
686 else if (ul_path_scanf(pc
, "removable", "%d", &dev
->removable
) != 1)
690 if (dev
->removable
== -1)
692 return dev
->removable
;
695 static uint64_t device_get_discard_granularity(struct lsblk_device
*dev
)
697 if (dev
->discard_granularity
== (uint64_t) -1
698 && ul_path_read_u64(dev
->sysfs
, &dev
->discard_granularity
,
699 "queue/discard_granularity") != 0)
700 dev
->discard_granularity
= 0;
702 return dev
->discard_granularity
;
706 * Generates data (string) for column specified by column ID for specified device. If sortdata
707 * is not NULL then returns number usable to sort the column if the data are available for the
710 static char *device_get_data(
711 struct lsblk_device
*dev
, /* device */
712 struct lsblk_device
*parent
, /* device parent as defined in the tree */
713 int id
, /* column ID (COL_*) */
714 uint64_t *sortdata
) /* returns sort data as number */
716 struct lsblk_devprop
*prop
= NULL
;
721 str
= dev
->dm_name
? mk_dm_name(dev
->dm_name
) : mk_name(dev
->name
);
724 str
= mk_name(dev
->name
);
728 str
= mk_name(parent
->name
);
732 str
= xstrdup(dev
->filename
);
736 prop
= lsblk_device_get_properties(dev
);
737 if (prop
&& prop
->owner
) {
738 str
= xstrdup(prop
->owner
);
740 struct stat
*st
= device_get_stat(dev
);
741 struct passwd
*pw
= st
? getpwuid(st
->st_uid
) : NULL
;
743 str
= xstrdup(pw
->pw_name
);
748 prop
= lsblk_device_get_properties(dev
);
749 if (prop
&& prop
->group
) {
750 str
= xstrdup(prop
->group
);
752 struct stat
*st
= device_get_stat(dev
);
753 struct group
*gr
= st
? getgrgid(st
->st_gid
) : NULL
;
755 str
= xstrdup(gr
->gr_name
);
760 prop
= lsblk_device_get_properties(dev
);
761 if (prop
&& prop
->mode
) {
762 str
= xstrdup(prop
->mode
);
764 struct stat
*st
= device_get_stat(dev
);
765 char md
[11] = { '\0' };
768 str
= xstrdup(xstrmode(st
->st_mode
, md
));
772 if (is_parsable(lsblk
))
773 xasprintf(&str
, "%u:%u", dev
->maj
, dev
->min
);
775 xasprintf(&str
, "%3u:%-3u", dev
->maj
, dev
->min
);
777 *sortdata
= makedev(dev
->maj
, dev
->min
);
780 prop
= lsblk_device_get_properties(dev
);
781 if (prop
&& prop
->fstype
)
782 str
= xstrdup(prop
->fstype
);
788 str
= get_vfs_attribute(dev
, id
);
791 prop
= lsblk_device_get_properties(dev
);
792 if (prop
&& prop
->fsversion
)
793 str
= xstrdup(prop
->fsversion
);
797 char *s
= lsblk_device_get_mountpoint(dev
);
805 prop
= lsblk_device_get_properties(dev
);
806 if (prop
&& prop
->label
)
807 str
= xstrdup(prop
->label
);
810 prop
= lsblk_device_get_properties(dev
);
811 if (prop
&& prop
->uuid
)
812 str
= xstrdup(prop
->uuid
);
815 prop
= lsblk_device_get_properties(dev
);
816 if (prop
&& prop
->ptuuid
)
817 str
= xstrdup(prop
->ptuuid
);
820 prop
= lsblk_device_get_properties(dev
);
821 if (prop
&& prop
->pttype
)
822 str
= xstrdup(prop
->pttype
);
825 prop
= lsblk_device_get_properties(dev
);
826 if (prop
&& prop
->parttype
)
827 str
= xstrdup(prop
->parttype
);
830 prop
= lsblk_device_get_properties(dev
);
831 if (prop
&& prop
->partlabel
)
832 str
= xstrdup(prop
->partlabel
);
835 prop
= lsblk_device_get_properties(dev
);
836 if (prop
&& prop
->partuuid
)
837 str
= xstrdup(prop
->partuuid
);
840 prop
= lsblk_device_get_properties(dev
);
841 if (prop
&& prop
->partflags
)
842 str
= xstrdup(prop
->partflags
);
845 prop
= lsblk_device_get_properties(dev
);
846 if (prop
&& prop
->wwn
)
847 str
= xstrdup(prop
->wwn
);
850 ul_path_read_string(dev
->sysfs
, &str
, "queue/read_ahead_kb");
852 str2u64(str
, sortdata
);
855 str
= xstrdup(is_readonly_device(dev
) ? "1" : "0");
858 str
= xstrdup(is_removable_device(dev
, parent
) ? "1" : "0");
861 str
= sysfs_blkdev_is_hotpluggable(dev
->sysfs
) ? xstrdup("1") : xstrdup("0");
864 ul_path_read_string(dev
->sysfs
, &str
, "queue/rotational");
867 ul_path_read_string(dev
->sysfs
, &str
, "queue/add_random");
870 if (!device_is_partition(dev
) && dev
->nslaves
== 0) {
871 prop
= lsblk_device_get_properties(dev
);
872 if (prop
&& prop
->model
)
873 str
= xstrdup(prop
->model
);
875 ul_path_read_string(dev
->sysfs
, &str
, "device/model");
879 if (!device_is_partition(dev
) && dev
->nslaves
== 0) {
880 prop
= lsblk_device_get_properties(dev
);
881 if (prop
&& prop
->serial
)
882 str
= xstrdup(prop
->serial
);
884 ul_path_read_string(dev
->sysfs
, &str
, "device/serial");
888 if (!device_is_partition(dev
) && dev
->nslaves
== 0)
889 ul_path_read_string(dev
->sysfs
, &str
, "device/rev");
892 if (!device_is_partition(dev
) && dev
->nslaves
== 0)
893 ul_path_read_string(dev
->sysfs
, &str
, "device/vendor");
899 xasprintf(&str
, "%ju", dev
->size
);
901 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, dev
->size
);
903 *sortdata
= dev
->size
;
906 if (!device_is_partition(dev
) && !dev
->dm_name
)
907 ul_path_read_string(dev
->sysfs
, &str
, "device/state");
908 else if (dev
->dm_name
) {
910 if (ul_path_read_s32(dev
->sysfs
, &x
, "dm/suspended") == 0)
911 str
= xstrdup(x
? "suspended" : "running");
915 ul_path_read_string(dev
->sysfs
, &str
, "alignment_offset");
917 str2u64(str
, sortdata
);
920 ul_path_read_string(dev
->sysfs
, &str
, "queue/minimum_io_size");
922 str2u64(str
, sortdata
);
925 ul_path_read_string(dev
->sysfs
, &str
, "queue/optimal_io_size");
927 str2u64(str
, sortdata
);
930 ul_path_read_string(dev
->sysfs
, &str
, "queue/physical_block_size");
932 str2u64(str
, sortdata
);
935 ul_path_read_string(dev
->sysfs
, &str
, "queue/logical_block_size");
937 str2u64(str
, sortdata
);
940 str
= get_scheduler(dev
);
943 ul_path_read_string(dev
->sysfs
, &str
, "queue/nr_requests");
945 str2u64(str
, sortdata
);
953 if (sysfs_blkdev_scsi_get_hctl(dev
->sysfs
, &h
, &c
, &t
, &l
) == 0)
954 xasprintf(&str
, "%d:%d:%d:%d", h
, c
, t
, l
);
958 str
= get_transport(dev
);
961 str
= get_subsystems(dev
);
964 if (device_get_discard_granularity(dev
) > 0)
965 ul_path_read_string(dev
->sysfs
, &str
, "discard_alignment");
969 str2u64(str
, sortdata
);
973 ul_path_read_string(dev
->sysfs
, &str
, "queue/discard_granularity");
975 str2u64(str
, sortdata
);
977 uint64_t x
= device_get_discard_granularity(dev
);
978 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
985 ul_path_read_string(dev
->sysfs
, &str
, "queue/discard_max_bytes");
987 str2u64(str
, sortdata
);
990 if (ul_path_read_u64(dev
->sysfs
, &x
, "queue/discard_max_bytes") == 0) {
991 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
998 if (device_get_discard_granularity(dev
) > 0)
999 ul_path_read_string(dev
->sysfs
, &str
, "queue/discard_zeroes_data");
1005 ul_path_read_string(dev
->sysfs
, &str
, "queue/write_same_max_bytes");
1007 str2u64(str
, sortdata
);
1011 if (ul_path_read_u64(dev
->sysfs
, &x
, "queue/write_same_max_bytes") == 0) {
1012 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1021 ul_path_read_string(dev
->sysfs
, &str
, "queue/zoned");
1029 * Adds data for all wanted columns about the device to the smartcols table
1031 static void device_to_scols(
1032 struct lsblk_device
*dev
,
1033 struct lsblk_device
*parent
,
1034 struct libscols_table
*tab
,
1035 struct libscols_line
*parent_line
)
1038 struct libscols_line
*ln
;
1039 struct lsblk_iter itr
;
1040 struct lsblk_device
*child
= NULL
;
1044 DBG(DEV
, ul_debugobj(dev
, "add '%s' to scols", dev
->name
));
1045 ON_DBG(DEV
, if (ul_path_isopen_dirfd(dev
->sysfs
)) ul_debugobj(dev
, " %s ---> is open!", dev
->name
));
1047 if (!parent
&& dev
->wholedisk
)
1048 parent
= dev
->wholedisk
;
1050 /* Do not print device more than one in --list mode */
1051 if (!(lsblk
->flags
& LSBLK_TREE
) && dev
->is_printed
)
1054 if (lsblk
->merge
&& list_count_entries(&dev
->parents
) > 1) {
1055 if (!lsblk_device_is_last_parent(dev
, parent
))
1060 ln
= scols_table_new_line(tab
, link_group
? NULL
: parent_line
);
1062 err(EXIT_FAILURE
, _("failed to allocate output line"));
1064 dev
->is_printed
= 1;
1067 struct lsblk_device
*p
;
1068 struct libscols_line
*gr
= parent_line
;
1070 /* Merge all my parents to the one group */
1071 DBG(DEV
, ul_debugobj(dev
, " grouping parents [--merge]"));
1072 lsblk_reset_iter(&itr
, LSBLK_ITER_FORWARD
);
1073 while (lsblk_device_next_parent(dev
, &itr
, &p
) == 0) {
1074 if (!p
->scols_line
) {
1075 DBG(DEV
, ul_debugobj(dev
, " *** ignore '%s' no scols line yet", p
->name
));
1078 DBG(DEV
, ul_debugobj(dev
, " group '%s'", p
->name
));
1079 scols_table_group_lines(tab
, p
->scols_line
, gr
, 0);
1082 /* Link the group -- this makes group->child connection */
1083 DBG(DEV
, ul_debugobj(dev
, " linking the group [--merge]"));
1084 scols_line_link_group(ln
, gr
, 0);
1087 /* read column specific data and set it to smartcols table line */
1088 for (i
= 0; i
< ncolumns
; i
++) {
1090 int id
= get_column_id(i
);
1092 if (lsblk
->sort_id
!= id
)
1093 data
= device_get_data(dev
, parent
, id
, NULL
);
1095 uint64_t sortdata
= (uint64_t) -1;
1097 data
= device_get_data(dev
, parent
, id
, &sortdata
);
1098 if (data
&& sortdata
!= (uint64_t) -1)
1099 set_sortdata_u64(ln
, i
, sortdata
);
1101 DBG(DEV
, ul_debugobj(dev
, " refer data[%zu]=\"%s\"", i
, data
));
1102 if (data
&& scols_line_refer_data(ln
, i
, data
))
1103 err(EXIT_FAILURE
, _("failed to add output data"));
1106 dev
->scols_line
= ln
;
1108 if (dev
->npartitions
== 0)
1109 /* For partitions we often read from parental whole-disk sysfs,
1110 * otherwise we can close */
1111 ul_path_close_dirfd(dev
->sysfs
);
1113 lsblk_reset_iter(&itr
, LSBLK_ITER_FORWARD
);
1114 while (lsblk_device_next_child(dev
, &itr
, &child
) == 0) {
1115 DBG(DEV
, ul_debugobj(dev
, "%s -> continue to child", dev
->name
));
1116 device_to_scols(child
, dev
, tab
, ln
);
1117 DBG(DEV
, ul_debugobj(dev
, "%s <- child done", dev
->name
));
1120 /* Let's be careful with number of open files */
1121 ul_path_close_dirfd(dev
->sysfs
);
1125 * Walks on tree and adds one line for each device to the smartcols table
1127 static void devtree_to_scols(struct lsblk_devtree
*tr
, struct libscols_table
*tab
)
1129 struct lsblk_iter itr
;
1130 struct lsblk_device
*dev
= NULL
;
1132 lsblk_reset_iter(&itr
, LSBLK_ITER_FORWARD
);
1134 while (lsblk_devtree_next_root(tr
, &itr
, &dev
) == 0)
1135 device_to_scols(dev
, NULL
, tab
, NULL
);
1139 * Reads very basic information about the device from sysfs into the device struct
1141 static int initialize_device(struct lsblk_device
*dev
,
1142 struct lsblk_device
*wholedisk
,
1147 DBG(DEV
, ul_debugobj(dev
, "initialize %s [wholedisk=%p %s]",
1148 name
, wholedisk
, wholedisk
? wholedisk
->name
: ""));
1150 dev
->name
= xstrdup(name
);
1153 dev
->wholedisk
= wholedisk
;
1154 lsblk_ref_device(wholedisk
);
1157 dev
->filename
= get_device_path(dev
);
1158 if (!dev
->filename
) {
1159 DBG(DEV
, ul_debugobj(dev
, "%s: failed to get device path", dev
->name
));
1162 DBG(DEV
, ul_debugobj(dev
, "%s: filename=%s", dev
->name
, dev
->filename
));
1164 devno
= __sysfs_devname_to_devno(lsblk
->sysroot
, dev
->name
, wholedisk
? wholedisk
->name
: NULL
);
1166 DBG(DEV
, ul_debugobj(dev
, "%s: unknown device name", dev
->name
));
1170 dev
->sysfs
= ul_new_sysfs_path(devno
, wholedisk
? wholedisk
->sysfs
: NULL
, lsblk
->sysroot
);
1172 DBG(DEV
, ul_debugobj(dev
, "%s: failed to initialize sysfs handler", dev
->name
));
1176 dev
->maj
= major(devno
);
1177 dev
->min
= minor(devno
);
1180 if (ul_path_read_u64(dev
->sysfs
, &dev
->size
, "size") == 0) /* in sectors */
1181 dev
->size
<<= 9; /* in bytes */
1183 /* Ignore devices of zero size */
1184 if (!lsblk
->all_devices
&& dev
->size
== 0) {
1185 DBG(DEV
, ul_debugobj(dev
, "zero size device -- ignore"));
1188 if (is_dm(dev
->name
)) {
1189 ul_path_read_string(dev
->sysfs
, &dev
->dm_name
, "dm/name");
1190 if (!dev
->dm_name
) {
1191 DBG(DEV
, ul_debugobj(dev
, "%s: failed to get dm name", dev
->name
));
1196 dev
->npartitions
= sysfs_blkdev_count_partitions(dev
->sysfs
, dev
->name
);
1197 dev
->nholders
= ul_path_count_dirents(dev
->sysfs
, "holders");
1198 dev
->nslaves
= ul_path_count_dirents(dev
->sysfs
, "slaves");
1200 DBG(DEV
, ul_debugobj(dev
, "%s: npartitions=%d, nholders=%d, nslaves=%d",
1201 dev
->name
, dev
->npartitions
, dev
->nholders
, dev
->nslaves
));
1203 /* ignore non-SCSI devices */
1204 if (lsblk
->scsi
&& sysfs_blkdev_scsi_get_hctl(dev
->sysfs
, NULL
, NULL
, NULL
, NULL
)) {
1205 DBG(DEV
, ul_debugobj(dev
, "non-scsi device -- ignore"));
1209 DBG(DEV
, ul_debugobj(dev
, "%s: context successfully initialized", dev
->name
));
1213 static struct lsblk_device
*devtree_get_device_or_new(struct lsblk_devtree
*tr
,
1214 struct lsblk_device
*disk
,
1217 struct lsblk_device
*dev
= lsblk_devtree_get_device(tr
, name
);
1220 dev
= lsblk_new_device();
1222 err(EXIT_FAILURE
, _("failed to allocate device"));
1224 if (initialize_device(dev
, disk
, name
) != 0) {
1225 lsblk_unref_device(dev
);
1228 lsblk_devtree_add_device(tr
, dev
);
1229 lsblk_unref_device(dev
); /* keep it referenced by devtree only */
1231 DBG(DEV
, ul_debugobj(dev
, "%s: already processed", name
));
1236 static int process_dependencies(
1237 struct lsblk_devtree
*tr
,
1238 struct lsblk_device
*dev
,
1242 * Read devices from whole-disk device into tree
1244 static int process_partitions(struct lsblk_devtree
*tr
, struct lsblk_device
*disk
)
1252 * Do not process further if there are no partitions for
1253 * this device or the device itself is a partition.
1255 if (!disk
->npartitions
|| device_is_partition(disk
))
1258 DBG(DEV
, ul_debugobj(disk
, "%s: probe whole-disk for partitions", disk
->name
));
1260 dir
= ul_path_opendir(disk
->sysfs
, NULL
);
1262 err(EXIT_FAILURE
, _("failed to open device directory in sysfs"));
1264 while ((d
= xreaddir(dir
))) {
1265 struct lsblk_device
*part
;
1267 if (!(sysfs_blkdev_is_partition_dirent(dir
, d
, disk
->name
)))
1270 DBG(DEV
, ul_debugobj(disk
, " checking %s", d
->d_name
));
1272 part
= devtree_get_device_or_new(tr
, disk
, d
->d_name
);
1276 if (lsblk_device_new_dependence(disk
, part
) == 0)
1277 process_dependencies(tr
, part
, 0);
1279 ul_path_close_dirfd(part
->sysfs
);
1282 /* For partitions we need parental (whole-disk) sysfs directory pretty
1283 * often, so close it now when all is done */
1284 ul_path_close_dirfd(disk
->sysfs
);
1286 DBG(DEV
, ul_debugobj(disk
, "probe whole-disk for partitions -- done"));
1291 static char *get_wholedisk_from_partition_dirent(DIR *dir
, struct dirent
*d
, char *buf
, size_t bufsz
)
1296 if ((len
= readlinkat(dirfd(dir
), d
->d_name
, buf
, bufsz
- 1)) < 0)
1301 /* The path ends with ".../<device>/<partition>" */
1302 p
= strrchr(buf
, '/');
1307 p
= strrchr(buf
, '/');
1316 * Reads slaves/holders and partitions for specified device into device tree
1318 static int process_dependencies(
1319 struct lsblk_devtree
*tr
,
1320 struct lsblk_device
*dev
,
1325 const char *depname
;
1332 /* read all or specified partition */
1333 if (do_partitions
&& dev
->npartitions
)
1334 process_partitions(tr
, dev
);
1336 DBG(DEV
, ul_debugobj(dev
, "%s: reading dependencies", dev
->name
));
1338 if (!(lsblk
->inverse
? dev
->nslaves
: dev
->nholders
)) {
1339 DBG(DEV
, ul_debugobj(dev
, " ignore (no slaves/holders)"));
1343 depname
= lsblk
->inverse
? "slaves" : "holders";
1344 dir
= ul_path_opendir(dev
->sysfs
, depname
);
1346 DBG(DEV
, ul_debugobj(dev
, " ignore (no slaves/holders directory)"));
1349 ul_path_close_dirfd(dev
->sysfs
);
1351 DBG(DEV
, ul_debugobj(dev
, " %s: checking for '%s' dependence", dev
->name
, depname
));
1353 while ((d
= xreaddir(dir
))) {
1354 struct lsblk_device
*dep
= NULL
;
1355 struct lsblk_device
*disk
= NULL
;
1357 /* Is the dependency a partition? */
1358 if (sysfs_blkdev_is_partition_dirent(dir
, d
, NULL
)) {
1363 DBG(DEV
, ul_debugobj(dev
, " %s: dependence is partition", d
->d_name
));
1365 diskname
= get_wholedisk_from_partition_dirent(dir
, d
, buf
, sizeof(buf
));
1367 disk
= devtree_get_device_or_new(tr
, NULL
, diskname
);
1369 DBG(DEV
, ul_debugobj(dev
, " ignore no wholedisk ???"));
1373 dep
= devtree_get_device_or_new(tr
, disk
, d
->d_name
);
1377 if (lsblk_device_new_dependence(dev
, dep
) == 0)
1378 process_dependencies(tr
, dep
, 1);
1381 && lsblk_device_new_dependence(dep
, disk
) == 0)
1382 process_dependencies(tr
, disk
, 0);
1384 /* The dependency is a whole device. */
1386 DBG(DEV
, ul_debugobj(dev
, " %s: %s: dependence is whole-disk",
1387 dev
->name
, d
->d_name
));
1389 dep
= devtree_get_device_or_new(tr
, NULL
, d
->d_name
);
1393 if (lsblk_device_new_dependence(dev
, dep
) == 0)
1394 /* For inverse tree we don't want to show partitions
1395 * if the dependence is on whole-disk */
1396 process_dependencies(tr
, dep
, lsblk
->inverse
? 0 : 1);
1399 if (dep
&& dep
->sysfs
)
1400 ul_path_close_dirfd(dep
->sysfs
);
1401 if (disk
&& disk
->sysfs
)
1402 ul_path_close_dirfd(disk
->sysfs
);
1406 DBG(DEV
, ul_debugobj(dev
, "%s: checking for '%s' -- done", dev
->name
, depname
));
1411 * Defines the device as root node in the device tree and walks on all dependencies of the device.
1413 static int __process_one_device(struct lsblk_devtree
*tr
, char *devname
, dev_t devno
)
1415 struct lsblk_device
*dev
= NULL
;
1416 struct lsblk_device
*disk
= NULL
;
1417 char buf
[PATH_MAX
+ 1], *name
= NULL
, *diskname
= NULL
;
1418 int real_part
= 0, rc
= -EINVAL
;
1420 if (devno
== 0 && devname
) {
1423 DBG(DEV
, ul_debug("%s: reading alone device", devname
));
1425 if (stat(devname
, &st
) || !S_ISBLK(st
.st_mode
)) {
1426 warnx(_("%s: not a block device"), devname
);
1431 DBG(DEV
, ul_debug("%d:%d: reading alone device", major(devno
), minor(devno
)));
1433 assert(devno
|| devname
);
1437 /* TODO: sysfs_devno_to_devname() internally initializes path_cxt, it
1438 * would be better to use ul_new_sysfs_path() + sysfs_blkdev_get_name()
1439 * and reuse path_cxt for initialize_device()
1441 name
= sysfs_devno_to_devname(devno
, buf
, sizeof(buf
));
1444 warn(_("%s: failed to get sysfs name"), devname
);
1447 name
= xstrdup(name
);
1449 if (!strncmp(name
, "dm-", 3)) {
1450 /* dm mapping is never a real partition! */
1455 if (blkid_devno_to_wholedisk(devno
, buf
, sizeof(buf
), &diskno
)) {
1456 warn(_("%s: failed to get whole-disk device number"), name
);
1460 real_part
= devno
!= diskno
;
1465 * Device is not a partition.
1467 DBG(DEV
, ul_debug(" non-partition"));
1469 dev
= devtree_get_device_or_new(tr
, NULL
, name
);
1473 lsblk_devtree_add_root(tr
, dev
);
1474 process_dependencies(tr
, dev
, !lsblk
->inverse
);
1477 * Partition, read sysfs name of the disk device
1479 DBG(DEV
, ul_debug(" partition"));
1481 disk
= devtree_get_device_or_new(tr
, NULL
, diskname
);
1485 dev
= devtree_get_device_or_new(tr
, disk
, name
);
1489 lsblk_devtree_add_root(tr
, dev
);
1490 process_dependencies(tr
, dev
, 1);
1493 && lsblk_device_new_dependence(dev
, disk
) == 0)
1494 process_dependencies(tr
, disk
, 0);
1496 ul_path_close_dirfd(disk
->sysfs
);
1501 if (dev
&& dev
->sysfs
)
1502 ul_path_close_dirfd(dev
->sysfs
);
1503 if (disk
&& disk
->sysfs
)
1504 ul_path_close_dirfd(disk
->sysfs
);
1509 static int process_one_device(struct lsblk_devtree
*tr
, char *devname
)
1512 return __process_one_device(tr
, devname
, 0);
1516 * The /sys/block contains only root devices, and no partitions. It seems more
1517 * simple to scan /sys/dev/block where are all devices without exceptions to get
1518 * top-level devices for the reverse tree.
1520 static int process_all_devices_inverse(struct lsblk_devtree
*tr
)
1524 struct path_cxt
*pc
= ul_new_path(_PATH_SYS_DEVBLOCK
);
1526 assert(lsblk
->inverse
);
1529 err(EXIT_FAILURE
, _("failed to allocate /sys handler"));
1531 ul_path_set_prefix(pc
, lsblk
->sysroot
);
1532 dir
= ul_path_opendir(pc
, NULL
);
1536 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_DEVBLOCK
));
1538 while ((d
= xreaddir(dir
))) {
1542 DBG(DEV
, ul_debug(" %s dentry", d
->d_name
));
1544 if (sscanf(d
->d_name
, "%d:%d", &maj
, &min
) != 2)
1546 devno
= makedev(maj
, min
);
1548 if (is_maj_excluded(maj
) || !is_maj_included(maj
))
1550 if (ul_path_countf_dirents(pc
, "%s/holders", d
->d_name
) != 0)
1552 if (sysfs_devno_count_partitions(devno
) != 0)
1554 __process_one_device(tr
, NULL
, devno
);
1560 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_DEVBLOCK
" -- done"));
1565 * Reads root nodes (devices) from /sys/block into devices tree
1567 static int process_all_devices(struct lsblk_devtree
*tr
)
1571 struct path_cxt
*pc
;
1573 assert(lsblk
->inverse
== 0);
1575 pc
= ul_new_path(_PATH_SYS_BLOCK
);
1577 err(EXIT_FAILURE
, _("failed to allocate /sys handler"));
1579 ul_path_set_prefix(pc
, lsblk
->sysroot
);
1580 dir
= ul_path_opendir(pc
, NULL
);
1584 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_BLOCK
));
1586 while ((d
= xreaddir(dir
))) {
1587 struct lsblk_device
*dev
= NULL
;
1589 DBG(DEV
, ul_debug(" %s dentry", d
->d_name
));
1590 dev
= devtree_get_device_or_new(tr
, NULL
, d
->d_name
);
1594 /* remove unwanted devices */
1595 if (is_maj_excluded(dev
->maj
) || !is_maj_included(dev
->maj
)) {
1596 DBG(DEV
, ul_debug(" %s: ignore (by filter)", d
->d_name
));
1597 lsblk_devtree_remove_device(tr
, dev
);
1603 DBG(DEV
, ul_debug(" %s: ignore (in-middle)", d
->d_name
));
1607 lsblk_devtree_add_root(tr
, dev
);
1608 process_dependencies(tr
, dev
, 1);
1610 /* Let's be careful with number of open files */
1611 if (dev
&& dev
->sysfs
)
1612 ul_path_close_dirfd(dev
->sysfs
);
1618 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_BLOCK
" -- done"));
1623 * Parses major numbers as specified on lsblk command line
1625 static void parse_excludes(const char *str0
)
1627 const char *str
= str0
;
1629 while (str
&& *str
) {
1634 n
= strtoul(str
, &end
, 10);
1636 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1637 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1638 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1639 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1640 excludes
[nexcludes
++] = n
;
1642 if (nexcludes
== ARRAY_SIZE(excludes
))
1643 /* TRANSLATORS: The standard value for %d is 256. */
1644 errx(EXIT_FAILURE
, _("the list of excluded devices is "
1645 "too large (limit is %d devices)"),
1646 (int)ARRAY_SIZE(excludes
));
1648 str
= end
&& *end
? end
+ 1 : NULL
;
1653 * Parses major numbers as specified on lsblk command line
1654 * (TODO: what about refactor and merge parse_excludes() and parse_includes().)
1656 static void parse_includes(const char *str0
)
1658 const char *str
= str0
;
1660 while (str
&& *str
) {
1665 n
= strtoul(str
, &end
, 10);
1667 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1668 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1669 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1670 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1671 includes
[nincludes
++] = n
;
1673 if (nincludes
== ARRAY_SIZE(includes
))
1674 /* TRANSLATORS: The standard value for %d is 256. */
1675 errx(EXIT_FAILURE
, _("the list of included devices is "
1676 "too large (limit is %d devices)"),
1677 (int)ARRAY_SIZE(includes
));
1678 str
= end
&& *end
? end
+ 1 : NULL
;
1683 * see set_sortdata_u64() and columns initialization in main()
1685 static int cmp_u64_cells(struct libscols_cell
*a
,
1686 struct libscols_cell
*b
,
1687 __attribute__((__unused__
)) void *data
)
1689 uint64_t *adata
= (uint64_t *) scols_cell_get_userdata(a
),
1690 *bdata
= (uint64_t *) scols_cell_get_userdata(b
);
1692 if (adata
== NULL
&& bdata
== NULL
)
1698 return *adata
== *bdata
? 0 : *adata
>= *bdata
? 1 : -1;
1701 static void device_set_dedupkey(
1702 struct lsblk_device
*dev
,
1703 struct lsblk_device
*parent
,
1706 struct lsblk_iter itr
;
1707 struct lsblk_device
*child
= NULL
;
1709 dev
->dedupkey
= device_get_data(dev
, parent
, id
, NULL
);
1711 DBG(DEV
, ul_debugobj(dev
, "%s: de-duplication key: %s", dev
->name
, dev
->dedupkey
));
1713 if (dev
->npartitions
== 0)
1714 /* For partitions we often read from parental whole-disk sysfs,
1715 * otherwise we can close */
1716 ul_path_close_dirfd(dev
->sysfs
);
1718 lsblk_reset_iter(&itr
, LSBLK_ITER_FORWARD
);
1720 while (lsblk_device_next_child(dev
, &itr
, &child
) == 0)
1721 device_set_dedupkey(child
, dev
, id
);
1723 /* Let's be careful with number of open files */
1724 ul_path_close_dirfd(dev
->sysfs
);
1727 static void devtree_set_dedupkeys(struct lsblk_devtree
*tr
, int id
)
1729 struct lsblk_iter itr
;
1730 struct lsblk_device
*dev
= NULL
;
1732 lsblk_reset_iter(&itr
, LSBLK_ITER_FORWARD
);
1734 while (lsblk_devtree_next_root(tr
, &itr
, &dev
) == 0)
1735 device_set_dedupkey(dev
, NULL
, id
);
1738 static void __attribute__((__noreturn__
)) usage(void)
1743 fputs(USAGE_HEADER
, out
);
1744 fprintf(out
, _(" %s [options] [<device> ...]\n"), program_invocation_short_name
);
1746 fputs(USAGE_SEPARATOR
, out
);
1747 fputs(_("List information about block devices.\n"), out
);
1749 fputs(USAGE_OPTIONS
, out
);
1750 fputs(_(" -D, --discard print discard capabilities\n"), out
);
1751 fputs(_(" -E, --dedup <column> de-duplicate output by <column>\n"), out
);
1752 fputs(_(" -I, --include <list> show only devices with specified major numbers\n"), out
);
1753 fputs(_(" -J, --json use JSON output format\n"), out
);
1754 fputs(_(" -O, --output-all output all columns\n"), out
);
1755 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out
);
1756 fputs(_(" -S, --scsi output info about SCSI devices\n"), out
);
1757 fputs(_(" -T, --tree[=<column>] use tree format output\n"), out
);
1758 fputs(_(" -a, --all print all devices\n"), out
);
1759 fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out
);
1760 fputs(_(" -d, --nodeps don't print slaves or holders\n"), out
);
1761 fputs(_(" -e, --exclude <list> exclude devices by major number (default: RAM disks)\n"), out
);
1762 fputs(_(" -f, --fs output info about filesystems\n"), out
);
1763 fputs(_(" -i, --ascii use ascii characters only\n"), out
);
1764 fputs(_(" -l, --list use list format output\n"), out
);
1765 fputs(_(" -M, --merge group parents of sub-trees (usable for RAIDs, Multi-path)\n"), out
);
1766 fputs(_(" -m, --perms output info about permissions\n"), out
);
1767 fputs(_(" -n, --noheadings don't print headings\n"), out
);
1768 fputs(_(" -o, --output <list> output columns\n"), out
);
1769 fputs(_(" -p, --paths print complete device path\n"), out
);
1770 fputs(_(" -r, --raw use raw output format\n"), out
);
1771 fputs(_(" -s, --inverse inverse dependencies\n"), out
);
1772 fputs(_(" -t, --topology output info about topology\n"), out
);
1773 fputs(_(" -z, --zoned print zone model\n"), out
);
1774 fputs(_(" -x, --sort <column> sort output by <column>\n"), out
);
1775 fputs(_(" --sysroot <dir> use specified directory as system root\n"), out
);
1776 fputs(USAGE_SEPARATOR
, out
);
1777 printf(USAGE_HELP_OPTIONS(22));
1779 fprintf(out
, USAGE_COLUMNS
);
1781 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
1782 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
1784 printf(USAGE_MAN_TAIL("lsblk(8)"));
1789 static void check_sysdevblock(void)
1791 if (access(_PATH_SYS_DEVBLOCK
, R_OK
) != 0)
1792 err(EXIT_FAILURE
, _("failed to access sysfs directory: %s"),
1793 _PATH_SYS_DEVBLOCK
);
1796 int main(int argc
, char *argv
[])
1798 struct lsblk _ls
= {
1801 .flags
= LSBLK_TREE
,
1804 struct lsblk_devtree
*tr
= NULL
;
1805 int c
, status
= EXIT_FAILURE
;
1806 char *outarg
= NULL
;
1808 int force_tree
= 0, has_tree_col
= 0;
1811 OPT_SYSROOT
= CHAR_MAX
+ 1
1814 static const struct option longopts
[] = {
1815 { "all", no_argument
, NULL
, 'a' },
1816 { "bytes", no_argument
, NULL
, 'b' },
1817 { "nodeps", no_argument
, NULL
, 'd' },
1818 { "discard", no_argument
, NULL
, 'D' },
1819 { "dedup", required_argument
, NULL
, 'E' },
1820 { "zoned", no_argument
, NULL
, 'z' },
1821 { "help", no_argument
, NULL
, 'h' },
1822 { "json", no_argument
, NULL
, 'J' },
1823 { "output", required_argument
, NULL
, 'o' },
1824 { "output-all", no_argument
, NULL
, 'O' },
1825 { "merge", no_argument
, NULL
, 'M' },
1826 { "perms", no_argument
, NULL
, 'm' },
1827 { "noheadings", no_argument
, NULL
, 'n' },
1828 { "list", no_argument
, NULL
, 'l' },
1829 { "ascii", no_argument
, NULL
, 'i' },
1830 { "raw", no_argument
, NULL
, 'r' },
1831 { "inverse", no_argument
, NULL
, 's' },
1832 { "fs", no_argument
, NULL
, 'f' },
1833 { "exclude", required_argument
, NULL
, 'e' },
1834 { "include", required_argument
, NULL
, 'I' },
1835 { "topology", no_argument
, NULL
, 't' },
1836 { "paths", no_argument
, NULL
, 'p' },
1837 { "pairs", no_argument
, NULL
, 'P' },
1838 { "scsi", no_argument
, NULL
, 'S' },
1839 { "sort", required_argument
, NULL
, 'x' },
1840 { "sysroot", required_argument
, NULL
, OPT_SYSROOT
},
1841 { "tree", optional_argument
, NULL
, 'T' },
1842 { "version", no_argument
, NULL
, 'V' },
1843 { NULL
, 0, NULL
, 0 },
1846 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
1855 { 'P','T', 'l','r' },
1858 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1860 setlocale(LC_ALL
, "");
1861 bindtextdomain(PACKAGE
, LOCALEDIR
);
1862 textdomain(PACKAGE
);
1863 close_stdout_atexit();
1869 while((c
= getopt_long(argc
, argv
,
1870 "abdDzE:e:fhJlnMmo:OpPiI:rstVST:x:", longopts
, NULL
)) != -1) {
1872 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1876 lsblk
->all_devices
= 1;
1885 add_uniq_column(COL_NAME
);
1886 add_uniq_column(COL_DALIGN
);
1887 add_uniq_column(COL_DGRAN
);
1888 add_uniq_column(COL_DMAX
);
1889 add_uniq_column(COL_DZERO
);
1892 add_uniq_column(COL_NAME
);
1893 add_uniq_column(COL_ZONED
);
1896 parse_excludes(optarg
);
1899 lsblk
->flags
|= LSBLK_JSON
;
1902 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
1908 lsblk
->flags
|= LSBLK_NOHEADINGS
;
1914 for (ncolumns
= 0 ; ncolumns
< ARRAY_SIZE(infos
); ncolumns
++)
1915 columns
[ncolumns
] = ncolumns
;
1921 lsblk
->flags
|= LSBLK_EXPORT
;
1922 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
1925 lsblk
->flags
|= LSBLK_ASCII
;
1928 parse_includes(optarg
);
1931 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
1932 lsblk
->flags
|= LSBLK_RAW
; /* enable raw */
1938 add_uniq_column(COL_NAME
);
1939 add_uniq_column(COL_FSTYPE
);
1940 add_uniq_column(COL_LABEL
);
1941 add_uniq_column(COL_UUID
);
1942 add_uniq_column(COL_FSAVAIL
);
1943 add_uniq_column(COL_FSUSEPERC
);
1944 add_uniq_column(COL_TARGET
);
1947 add_uniq_column(COL_NAME
);
1948 add_uniq_column(COL_SIZE
);
1949 add_uniq_column(COL_OWNER
);
1950 add_uniq_column(COL_GROUP
);
1951 add_uniq_column(COL_MODE
);
1954 add_uniq_column(COL_NAME
);
1955 add_uniq_column(COL_ALIOFF
);
1956 add_uniq_column(COL_MINIO
);
1957 add_uniq_column(COL_OPTIO
);
1958 add_uniq_column(COL_PHYSEC
);
1959 add_uniq_column(COL_LOGSEC
);
1960 add_uniq_column(COL_ROTA
);
1961 add_uniq_column(COL_SCHED
);
1962 add_uniq_column(COL_RQ_SIZE
);
1963 add_uniq_column(COL_RA
);
1964 add_uniq_column(COL_WSAME
);
1969 add_uniq_column(COL_NAME
);
1970 add_uniq_column(COL_HCTL
);
1971 add_uniq_column(COL_TYPE
);
1972 add_uniq_column(COL_VENDOR
);
1973 add_uniq_column(COL_MODEL
);
1974 add_uniq_column(COL_REV
);
1975 add_uniq_column(COL_TRANSPORT
);
1980 lsblk
->tree_id
= column_name_to_id(optarg
, strlen(optarg
));
1983 lsblk
->sysroot
= optarg
;
1986 lsblk
->dedup_id
= column_name_to_id(optarg
, strlen(optarg
));
1987 if (lsblk
->dedup_id
>= 0)
1989 errtryhelp(EXIT_FAILURE
);
1992 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
1993 lsblk
->sort_id
= column_name_to_id(optarg
, strlen(optarg
));
1994 if (lsblk
->sort_id
>= 0)
1996 errtryhelp(EXIT_FAILURE
);
2002 print_version(EXIT_SUCCESS
);
2004 errtryhelp(EXIT_FAILURE
);
2009 lsblk
->flags
|= LSBLK_TREE
;
2011 check_sysdevblock();
2014 add_column(COL_NAME
);
2015 add_column(COL_MAJMIN
);
2017 add_column(COL_SIZE
);
2019 add_column(COL_TYPE
);
2020 add_column(COL_TARGET
);
2023 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
2024 &ncolumns
, column_name_to_id
) < 0)
2025 return EXIT_FAILURE
;
2027 if (lsblk
->all_devices
== 0 && nexcludes
== 0 && nincludes
== 0)
2028 excludes
[nexcludes
++] = 1; /* default: ignore RAM disks */
2030 if (lsblk
->sort_id
< 0)
2031 /* Since Linux 4.8 we have sort devices by default, because
2032 * /sys is no more sorted */
2033 lsblk
->sort_id
= COL_MAJMIN
;
2035 /* For --inverse --list we still follow parent->child relation */
2036 if (lsblk
->inverse
&& !(lsblk
->flags
& LSBLK_TREE
))
2037 lsblk
->force_tree_order
= 1;
2039 if (lsblk
->sort_id
>= 0 && column_id_to_number(lsblk
->sort_id
) < 0) {
2040 /* the sort column is not between output columns -- add as hidden */
2041 add_column(lsblk
->sort_id
);
2042 lsblk
->sort_hidden
= 1;
2045 if (lsblk
->dedup_id
>= 0 && column_id_to_number(lsblk
->dedup_id
) < 0) {
2046 /* the deduplication column is not between output columns -- add as hidden */
2047 add_column(lsblk
->dedup_id
);
2048 lsblk
->dedup_hidden
= 1;
2052 scols_init_debug(0);
2053 ul_path_init_debug();
2056 * initialize output columns
2058 if (!(lsblk
->table
= scols_new_table()))
2059 errx(EXIT_FAILURE
, _("failed to allocate output table"));
2060 scols_table_enable_raw(lsblk
->table
, !!(lsblk
->flags
& LSBLK_RAW
));
2061 scols_table_enable_export(lsblk
->table
, !!(lsblk
->flags
& LSBLK_EXPORT
));
2062 scols_table_enable_ascii(lsblk
->table
, !!(lsblk
->flags
& LSBLK_ASCII
));
2063 scols_table_enable_json(lsblk
->table
, !!(lsblk
->flags
& LSBLK_JSON
));
2064 scols_table_enable_noheadings(lsblk
->table
, !!(lsblk
->flags
& LSBLK_NOHEADINGS
));
2066 if (lsblk
->flags
& LSBLK_JSON
)
2067 scols_table_set_name(lsblk
->table
, "blockdevices");
2069 for (i
= 0; i
< ncolumns
; i
++) {
2070 struct colinfo
*ci
= get_column_info(i
);
2071 struct libscols_column
*cl
;
2072 int id
= get_column_id(i
), fl
= ci
->flags
;
2074 if ((lsblk
->flags
& LSBLK_TREE
)
2075 && has_tree_col
== 0
2076 && id
== lsblk
->tree_id
) {
2077 fl
|= SCOLS_FL_TREE
;
2078 fl
&= ~SCOLS_FL_RIGHT
;
2082 if (lsblk
->sort_hidden
&& lsblk
->sort_id
== id
)
2083 fl
|= SCOLS_FL_HIDDEN
;
2084 if (lsblk
->dedup_hidden
&& lsblk
->dedup_id
== id
)
2085 fl
|= SCOLS_FL_HIDDEN
;
2088 && lsblk
->flags
& LSBLK_JSON
2089 && has_tree_col
== 0
2090 && i
+ 1 == ncolumns
)
2091 /* The "--tree --json" specified, but no column with
2092 * SCOLS_FL_TREE yet; force it for the last column
2094 fl
|= SCOLS_FL_TREE
;
2096 cl
= scols_table_new_column(lsblk
->table
, ci
->name
, ci
->whint
, fl
);
2098 warn(_("failed to allocate output column"));
2101 if (!lsblk
->sort_col
&& lsblk
->sort_id
== id
) {
2102 lsblk
->sort_col
= cl
;
2103 scols_column_set_cmpfunc(cl
,
2104 ci
->type
== COLTYPE_NUM
? cmp_u64_cells
:
2105 ci
->type
== COLTYPE_SIZE
? cmp_u64_cells
:
2106 ci
->type
== COLTYPE_SORTNUM
? cmp_u64_cells
: scols_cmpstr_cells
,
2109 if (lsblk
->flags
& LSBLK_JSON
) {
2116 scols_column_set_json_type(cl
, SCOLS_JSON_NUMBER
);
2119 scols_column_set_json_type(cl
, SCOLS_JSON_BOOLEAN
);
2122 scols_column_set_json_type(cl
, SCOLS_JSON_STRING
);
2128 tr
= lsblk_new_devtree();
2130 err(EXIT_FAILURE
, _("failed to allocate device tree"));
2132 if (optind
== argc
) {
2133 int rc
= lsblk
->inverse
?
2134 process_all_devices_inverse(tr
) :
2135 process_all_devices(tr
);
2137 status
= rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
2139 int cnt
= 0, cnt_err
= 0;
2141 while (optind
< argc
) {
2142 if (process_one_device(tr
, argv
[optind
++]) != 0)
2146 status
= cnt
== 0 ? EXIT_FAILURE
: /* nothing */
2147 cnt
== cnt_err
? LSBLK_EXIT_ALLFAILED
:/* all failed */
2148 cnt_err
? LSBLK_EXIT_SOMEOK
: /* some ok */
2149 EXIT_SUCCESS
; /* all success */
2152 if (lsblk
->dedup_id
> -1) {
2153 devtree_set_dedupkeys(tr
, lsblk
->dedup_id
);
2154 lsblk_devtree_deduplicate_devices(tr
);
2157 devtree_to_scols(tr
, lsblk
->table
);
2159 if (lsblk
->sort_col
)
2160 scols_sort_table(lsblk
->table
, lsblk
->sort_col
);
2161 if (lsblk
->force_tree_order
)
2162 scols_sort_table_by_tree(lsblk
->table
);
2164 scols_print_table(lsblk
->table
);
2167 if (lsblk
->sort_col
)
2168 unref_sortdata(lsblk
->table
);
2170 scols_unref_table(lsblk
->table
);
2173 lsblk_properties_deinit();
2174 lsblk_unref_devtree(tr
);