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
);
123 /* basic table settings */
125 LSBLK_ASCII
= (1 << 0),
126 LSBLK_RAW
= (1 << 1),
127 LSBLK_NOHEADINGS
= (1 << 2),
128 LSBLK_EXPORT
= (1 << 3),
129 LSBLK_TREE
= (1 << 4),
130 LSBLK_JSON
= (1 << 5),
133 /* Types used for qsort() and JSON */
135 COLTYPE_STR
= 0, /* default */
136 COLTYPE_NUM
= 1, /* always u64 number */
137 COLTYPE_SORTNUM
= 2, /* string on output, u64 for qsort() */
138 COLTYPE_SIZE
= 3, /* srring by default, number when --bytes */
139 COLTYPE_BOOL
= 4 /* 0 or 1 */
144 const char *name
; /* header */
145 double whint
; /* width hint (N < 1 is in percent of termwidth) */
146 int flags
; /* SCOLS_FL_* */
148 int type
; /* COLTYPE_* */
151 /* columns descriptions */
152 static struct colinfo infos
[] = {
153 [COL_NAME
] = { "NAME", 0.25, SCOLS_FL_NOEXTREMES
, N_("device name") },
154 [COL_KNAME
] = { "KNAME", 0.3, 0, N_("internal kernel device name") },
155 [COL_PKNAME
] = { "PKNAME", 0.3, 0, N_("internal parent kernel device name") },
156 [COL_PATH
] = { "PATH", 0.3, 0, N_("path to the device node") },
157 [COL_MAJMIN
] = { "MAJ:MIN", 6, 0, N_("major:minor device number"), COLTYPE_SORTNUM
},
159 [COL_FSAVAIL
] = { "FSAVAIL", 5, SCOLS_FL_RIGHT
, N_("filesystem size available") },
160 [COL_FSSIZE
] = { "FSSIZE", 5, SCOLS_FL_RIGHT
, N_("filesystem size") },
161 [COL_FSTYPE
] = { "FSTYPE", 0.1, SCOLS_FL_TRUNC
, N_("filesystem type") },
162 [COL_FSUSED
] = { "FSUSED", 5, SCOLS_FL_RIGHT
, N_("filesystem size used") },
163 [COL_FSUSEPERC
] = { "FSUSE%", 3, SCOLS_FL_RIGHT
, N_("filesystem use percentage") },
164 [COL_FSVERSION
] = { "FSVER", 0.1, SCOLS_FL_TRUNC
, N_("filesystem version") },
166 [COL_TARGET
] = { "MOUNTPOINT", 0.10, SCOLS_FL_TRUNC
, N_("where the device is mounted") },
167 [COL_LABEL
] = { "LABEL", 0.1, 0, N_("filesystem LABEL") },
168 [COL_UUID
] = { "UUID", 36, 0, N_("filesystem UUID") },
170 [COL_PTUUID
] = { "PTUUID", 36, 0, N_("partition table identifier (usually UUID)") },
171 [COL_PTTYPE
] = { "PTTYPE", 0.1, 0, N_("partition table type") },
173 [COL_PARTTYPE
] = { "PARTTYPE", 36, 0, N_("partition type code or UUID") },
174 [COL_PARTTYPENAME
] = { "PARTTYPENAME", 0.1, 0, N_("partition type name") },
175 [COL_PARTLABEL
] = { "PARTLABEL", 0.1, 0, N_("partition LABEL") },
176 [COL_PARTUUID
] = { "PARTUUID", 36, 0, N_("partition UUID") },
177 [COL_PARTFLAGS
] = { "PARTFLAGS", 36, 0, N_("partition flags") },
179 [COL_RA
] = { "RA", 3, SCOLS_FL_RIGHT
, N_("read-ahead of the device"), COLTYPE_NUM
},
180 [COL_RO
] = { "RO", 1, SCOLS_FL_RIGHT
, N_("read-only device"), COLTYPE_BOOL
},
181 [COL_RM
] = { "RM", 1, SCOLS_FL_RIGHT
, N_("removable device"), COLTYPE_BOOL
},
182 [COL_HOTPLUG
]= { "HOTPLUG", 1, SCOLS_FL_RIGHT
, N_("removable or hotplug device (usb, pcmcia, ...)"), COLTYPE_BOOL
},
183 [COL_ROTA
] = { "ROTA", 1, SCOLS_FL_RIGHT
, N_("rotational device"), COLTYPE_BOOL
},
184 [COL_RAND
] = { "RAND", 1, SCOLS_FL_RIGHT
, N_("adds randomness"), COLTYPE_BOOL
},
185 [COL_MODEL
] = { "MODEL", 0.1, SCOLS_FL_TRUNC
, N_("device identifier") },
186 [COL_SERIAL
] = { "SERIAL", 0.1, SCOLS_FL_TRUNC
, N_("disk serial number") },
187 [COL_SIZE
] = { "SIZE", 5, SCOLS_FL_RIGHT
, N_("size of the device"), COLTYPE_SIZE
},
188 [COL_STATE
] = { "STATE", 7, SCOLS_FL_TRUNC
, N_("state of the device") },
189 [COL_OWNER
] = { "OWNER", 0.1, SCOLS_FL_TRUNC
, N_("user name"), },
190 [COL_GROUP
] = { "GROUP", 0.1, SCOLS_FL_TRUNC
, N_("group name") },
191 [COL_MODE
] = { "MODE", 10, 0, N_("device node permissions") },
192 [COL_ALIOFF
] = { "ALIGNMENT", 6, SCOLS_FL_RIGHT
, N_("alignment offset"), COLTYPE_NUM
},
193 [COL_MINIO
] = { "MIN-IO", 6, SCOLS_FL_RIGHT
, N_("minimum I/O size"), COLTYPE_NUM
},
194 [COL_OPTIO
] = { "OPT-IO", 6, SCOLS_FL_RIGHT
, N_("optimal I/O size"), COLTYPE_NUM
},
195 [COL_PHYSEC
] = { "PHY-SEC", 7, SCOLS_FL_RIGHT
, N_("physical sector size"), COLTYPE_NUM
},
196 [COL_LOGSEC
] = { "LOG-SEC", 7, SCOLS_FL_RIGHT
, N_("logical sector size"), COLTYPE_NUM
},
197 [COL_SCHED
] = { "SCHED", 0.1, 0, N_("I/O scheduler name") },
198 [COL_RQ_SIZE
]= { "RQ-SIZE", 5, SCOLS_FL_RIGHT
, N_("request queue size"), COLTYPE_NUM
},
199 [COL_TYPE
] = { "TYPE", 4, 0, N_("device type") },
200 [COL_DALIGN
] = { "DISC-ALN", 6, SCOLS_FL_RIGHT
, N_("discard alignment offset"), COLTYPE_NUM
},
201 [COL_DGRAN
] = { "DISC-GRAN", 6, SCOLS_FL_RIGHT
, N_("discard granularity"), COLTYPE_SIZE
},
202 [COL_DMAX
] = { "DISC-MAX", 6, SCOLS_FL_RIGHT
, N_("discard max bytes"), COLTYPE_SIZE
},
203 [COL_DZERO
] = { "DISC-ZERO", 1, SCOLS_FL_RIGHT
, N_("discard zeroes data"), COLTYPE_BOOL
},
204 [COL_WSAME
] = { "WSAME", 6, SCOLS_FL_RIGHT
, N_("write same max bytes"), COLTYPE_SIZE
},
205 [COL_WWN
] = { "WWN", 18, 0, N_("unique storage identifier") },
206 [COL_HCTL
] = { "HCTL", 10, 0, N_("Host:Channel:Target:Lun for SCSI") },
207 [COL_TRANSPORT
] = { "TRAN", 6, 0, N_("device transport type") },
208 [COL_SUBSYS
] = { "SUBSYSTEMS", 0.1, SCOLS_FL_NOEXTREMES
, N_("de-duplicated chain of subsystems") },
209 [COL_REV
] = { "REV", 4, SCOLS_FL_RIGHT
, N_("device revision") },
210 [COL_VENDOR
] = { "VENDOR", 0.1, SCOLS_FL_TRUNC
, N_("device vendor") },
211 [COL_ZONED
] = { "ZONED", 0.3, 0, N_("zone model") },
212 [COL_DAX
] = { "DAX", 1, SCOLS_FL_RIGHT
, N_("dax-capable device"), COLTYPE_BOOL
},
215 struct lsblk
*lsblk
; /* global handler */
218 * columns[] array specifies all currently wanted output column. The columns
219 * are defined by infos[] array and you can specify (on command line) each
220 * column twice. That's enough, dynamically allocated array of the columns is
221 * unnecessary overkill and over-engineering in this case
223 static int columns
[ARRAY_SIZE(infos
) * 2];
224 static size_t ncolumns
;
226 static inline void add_column(int id
)
228 if (ncolumns
>= ARRAY_SIZE(columns
))
229 errx(EXIT_FAILURE
, _("too many columns specified, "
230 "the limit is %zu columns"),
231 ARRAY_SIZE(columns
) - 1);
232 columns
[ ncolumns
++ ] = id
;
235 static inline void add_uniq_column(int id
)
237 if (column_id_to_number(id
) < 0)
241 static void lsblk_init_debug(void)
243 __UL_INIT_DEBUG_FROM_ENV(lsblk
, LSBLK_DEBUG_
, 0, LSBLK_DEBUG
);
247 * exclude/include devices filter based on major device numbers
249 static int excludes
[256];
250 static size_t nexcludes
;
252 static int includes
[256];
253 static size_t nincludes
;
255 static int is_maj_excluded(int maj
)
259 assert(ARRAY_SIZE(excludes
) > nexcludes
);
262 return 0; /* filter not enabled, device not excluded */
264 for (i
= 0; i
< nexcludes
; i
++) {
265 if (excludes
[i
] == maj
) {
266 DBG(FILTER
, ul_debug("exclude: maj=%d", maj
));
273 static int is_maj_included(int maj
)
277 assert(ARRAY_SIZE(includes
) > nincludes
);
280 return 1; /* filter not enabled, device is included */
282 for (i
= 0; i
< nincludes
; i
++) {
283 if (includes
[i
] == maj
) {
284 DBG(FILTER
, ul_debug("include: maj=%d", maj
));
291 /* Converts column sequential number to column ID (COL_*) */
292 static int get_column_id(int num
)
295 assert((size_t) num
< ncolumns
);
296 assert(columns
[num
] < (int) ARRAY_SIZE(infos
));
300 /* Returns column description for the column sequential number */
301 static struct colinfo
*get_column_info(int num
)
303 return &infos
[ get_column_id(num
) ];
306 /* Converts column name (as defined in the infos[] to the column ID */
307 static int column_name_to_id(const char *name
, size_t namesz
)
311 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
312 const char *cn
= infos
[i
].name
;
314 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
317 warnx(_("unknown column: %s"), name
);
321 /* Converts column ID (COL_*) to column sequential number */
322 static int column_id_to_number(int id
)
326 for (i
= 0; i
< ncolumns
; i
++)
327 if (columns
[i
] == id
)
332 /* Checks for DM prefix in the device name */
333 static int is_dm(const char *name
)
335 return strncmp(name
, "dm-", 3) ? 0 : 1;
338 /* Returns full pat to the device node (TODO: what about sysfs_blkdev_get_path()) */
339 static char *get_device_path(struct lsblk_device
*dev
)
346 if (is_dm(dev
->name
))
347 return __canonicalize_dm_name(lsblk
->sysroot
, dev
->name
);
349 snprintf(path
, sizeof(path
), "/dev/%s", dev
->name
);
350 sysfs_devname_sys_to_dev(path
);
351 return xstrdup(path
);
354 static int is_readonly_device(struct lsblk_device
*dev
)
358 if (ul_path_scanf(dev
->sysfs
, "ro", "%d", &ro
) == 1)
361 /* fallback if "ro" attribute does not exist */
362 fd
= open(dev
->filename
, O_RDONLY
);
364 if (ioctl(fd
, BLKROGET
, &ro
) != 0)
371 static char *get_scheduler(struct lsblk_device
*dev
)
374 char *p
, *res
= NULL
;
376 if (ul_path_read_buffer(dev
->sysfs
, buf
, sizeof(buf
), "queue/scheduler") == 0)
378 p
= strchr(buf
, '[');
381 p
= strchr(res
, ']');
391 static char *get_type(struct lsblk_device
*dev
)
393 char *res
= NULL
, *p
;
395 if (device_is_partition(dev
))
396 return xstrdup("part");
398 if (is_dm(dev
->name
)) {
399 char *dm_uuid
= NULL
;
401 /* The DM_UUID prefix should be set to subsystem owning
402 * the device - LVM, CRYPT, DMRAID, MPATH, PART */
403 if (ul_path_read_string(dev
->sysfs
, &dm_uuid
, "dm/uuid") > 0
406 char *dm_uuid_prefix
= strsep(&tmp
, "-");
408 if (dm_uuid_prefix
) {
409 /* kpartx hack to remove partition number */
410 if (strncasecmp(dm_uuid_prefix
, "part", 4) == 0)
411 dm_uuid_prefix
[4] = '\0';
413 res
= xstrdup(dm_uuid_prefix
);
419 /* No UUID or no prefix - just mark it as DM device */
422 } else if (!strncmp(dev
->name
, "loop", 4)) {
423 res
= xstrdup("loop");
425 } else if (!strncmp(dev
->name
, "md", 2)) {
426 char *md_level
= NULL
;
428 ul_path_read_string(dev
->sysfs
, &md_level
, "md/level");
429 res
= md_level
? md_level
: xstrdup("md");
432 const char *type
= NULL
;
435 if (ul_path_read_s32(dev
->sysfs
, &x
, "device/type") == 0)
436 type
= blkdev_scsi_type_to_name(x
);
442 for (p
= res
; p
&& *p
; p
++)
443 *p
= tolower((unsigned char) *p
);
447 /* Thanks to lsscsi code for idea of detection logic used here */
448 static char *get_transport(struct lsblk_device
*dev
)
450 struct path_cxt
*sysfs
= dev
->sysfs
;
452 const char *trans
= NULL
;
455 /* SCSI - Serial Peripheral Interface */
456 if (sysfs_blkdev_scsi_host_is(sysfs
, "spi"))
459 /* FC/FCoE - Fibre Channel / Fibre Channel over Ethernet */
460 else if (sysfs_blkdev_scsi_host_is(sysfs
, "fc")) {
461 attr
= sysfs_blkdev_scsi_host_strdup_attribute(sysfs
, "fc", "symbolic_name");
464 trans
= strstr(attr
, " over ") ? "fcoe" : "fc";
468 /* SAS - Serial Attached SCSI */
469 else if (sysfs_blkdev_scsi_host_is(sysfs
, "sas") ||
470 sysfs_blkdev_scsi_has_attribute(sysfs
, "sas_device"))
474 /* SBP - Serial Bus Protocol (FireWire) */
475 else if (sysfs_blkdev_scsi_has_attribute(sysfs
, "ieee1394_id"))
479 else if (sysfs_blkdev_scsi_host_is(sysfs
, "iscsi"))
482 /* USB - Universal Serial Bus */
483 else if (sysfs_blkdev_scsi_path_contains(sysfs
, "usb"))
487 else if (sysfs_blkdev_scsi_host_is(sysfs
, "scsi")) {
488 attr
= sysfs_blkdev_scsi_host_strdup_attribute(sysfs
, "scsi", "proc_name");
491 if (!strncmp(attr
, "ahci", 4) || !strncmp(attr
, "sata", 4))
493 else if (strstr(attr
, "ata"))
497 } else if (strncmp(dev
->name
, "nvme", 4) == 0)
500 return trans
? xstrdup(trans
) : NULL
;
503 static char *get_subsystems(struct lsblk_device
*dev
)
506 char *sub
, *chain
, *res
= NULL
;
507 size_t len
= 0, last
= 0;
509 chain
= sysfs_blkdev_get_devchain(dev
->sysfs
, path
, sizeof(path
));
513 while (sysfs_blkdev_next_subsystem(dev
->sysfs
, chain
, &sub
) == 0) {
516 /* don't create "block:scsi:scsi", but "block:scsi" */
517 if (len
&& strcmp(res
+ last
, sub
) == 0) {
523 res
= xrealloc(res
, len
+ sz
+ 2);
527 memcpy(res
+ len
, sub
, sz
+ 1);
537 #define is_parsable(_l) (scols_table_is_raw((_l)->table) || \
538 scols_table_is_export((_l)->table) || \
539 scols_table_is_json((_l)->table))
541 static char *mk_name(const char *name
)
547 xasprintf(&p
, "/dev/%s", name
);
551 sysfs_devname_sys_to_dev(p
);
555 static char *mk_dm_name(const char *name
)
561 xasprintf(&p
, "/dev/mapper/%s", name
);
567 /* stores data to scols cell userdata (invisible and independent on output)
568 * to make the original values accessible for sort functions
570 static void set_sortdata_u64(struct libscols_line
*ln
, int col
, uint64_t x
)
572 struct libscols_cell
*ce
= scols_line_get_cell(ln
, col
);
577 data
= xmalloc(sizeof(uint64_t));
579 scols_cell_set_userdata(ce
, data
);
582 /* do not modify *data on any error */
583 static void str2u64(const char *str
, uint64_t *data
)
589 if (str
== NULL
|| *str
== '\0')
591 num
= strtoumax(str
, &end
, 10);
593 if (errno
|| str
== end
|| (end
&& *end
))
598 static void unref_sortdata(struct libscols_table
*tb
)
600 struct libscols_iter
*itr
;
601 struct libscols_line
*ln
;
603 if (!tb
|| !lsblk
->sort_col
)
605 itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
608 while (scols_table_next_line(tb
, itr
, &ln
) == 0) {
609 struct libscols_cell
*ce
= scols_line_get_column_cell(ln
,
611 void *data
= scols_cell_get_userdata(ce
);
615 scols_free_iter(itr
);
618 static char *get_vfs_attribute(struct lsblk_device
*dev
, int id
)
621 uint64_t vfs_attr
= 0;
624 if (!dev
->fsstat
.f_blocks
) {
625 mnt
= lsblk_device_get_mountpoint(dev
);
626 if (!mnt
|| dev
->is_swap
)
628 if (statvfs(mnt
, &dev
->fsstat
) != 0)
634 vfs_attr
= dev
->fsstat
.f_frsize
* dev
->fsstat
.f_blocks
;
637 vfs_attr
= dev
->fsstat
.f_frsize
* dev
->fsstat
.f_bavail
;
640 vfs_attr
= dev
->fsstat
.f_frsize
* (dev
->fsstat
.f_blocks
- dev
->fsstat
.f_bfree
);
643 if (dev
->fsstat
.f_blocks
== 0)
646 xasprintf(&sizestr
, "%.0f%%",
647 (double)(dev
->fsstat
.f_blocks
- dev
->fsstat
.f_bfree
) /
648 dev
->fsstat
.f_blocks
* 100);
653 sizestr
= xstrdup("0");
654 else if (lsblk
->bytes
)
655 xasprintf(&sizestr
, "%ju", vfs_attr
);
657 sizestr
= size_to_human_string(SIZE_SUFFIX_1LETTER
, vfs_attr
);
662 static struct stat
*device_get_stat(struct lsblk_device
*dev
)
665 && stat(dev
->filename
, &dev
->st
) != 0)
671 static int is_removable_device(struct lsblk_device
*dev
, struct lsblk_device
*parent
)
675 if (dev
->removable
!= -1)
677 if (ul_path_scanf(dev
->sysfs
, "removable", "%d", &dev
->removable
) == 1)
681 pc
= sysfs_blkdev_get_parent(dev
->sysfs
);
685 /* dev is partition and parent is whole-disk */
686 if (pc
== parent
->sysfs
)
687 dev
->removable
= is_removable_device(parent
, NULL
);
689 /* parent is something else, use sysfs parent */
690 else if (ul_path_scanf(pc
, "removable", "%d", &dev
->removable
) != 1)
694 if (dev
->removable
== -1)
696 return dev
->removable
;
699 static uint64_t device_get_discard_granularity(struct lsblk_device
*dev
)
701 if (dev
->discard_granularity
== (uint64_t) -1
702 && ul_path_read_u64(dev
->sysfs
, &dev
->discard_granularity
,
703 "queue/discard_granularity") != 0)
704 dev
->discard_granularity
= 0;
706 return dev
->discard_granularity
;
710 * Generates data (string) for column specified by column ID for specified device. If sortdata
711 * is not NULL then returns number usable to sort the column if the data are available for the
714 static char *device_get_data(
715 struct lsblk_device
*dev
, /* device */
716 struct lsblk_device
*parent
, /* device parent as defined in the tree */
717 int id
, /* column ID (COL_*) */
718 uint64_t *sortdata
) /* returns sort data as number */
720 struct lsblk_devprop
*prop
= NULL
;
725 str
= dev
->dm_name
? mk_dm_name(dev
->dm_name
) : mk_name(dev
->name
);
728 str
= mk_name(dev
->name
);
732 str
= mk_name(parent
->name
);
736 str
= xstrdup(dev
->filename
);
740 prop
= lsblk_device_get_properties(dev
);
741 if (prop
&& prop
->owner
) {
742 str
= xstrdup(prop
->owner
);
744 struct stat
*st
= device_get_stat(dev
);
745 struct passwd
*pw
= st
? getpwuid(st
->st_uid
) : NULL
;
747 str
= xstrdup(pw
->pw_name
);
752 prop
= lsblk_device_get_properties(dev
);
753 if (prop
&& prop
->group
) {
754 str
= xstrdup(prop
->group
);
756 struct stat
*st
= device_get_stat(dev
);
757 struct group
*gr
= st
? getgrgid(st
->st_gid
) : NULL
;
759 str
= xstrdup(gr
->gr_name
);
764 prop
= lsblk_device_get_properties(dev
);
765 if (prop
&& prop
->mode
) {
766 str
= xstrdup(prop
->mode
);
768 struct stat
*st
= device_get_stat(dev
);
769 char md
[11] = { '\0' };
772 str
= xstrdup(xstrmode(st
->st_mode
, md
));
776 if (is_parsable(lsblk
))
777 xasprintf(&str
, "%u:%u", dev
->maj
, dev
->min
);
779 xasprintf(&str
, "%3u:%-3u", dev
->maj
, dev
->min
);
781 *sortdata
= makedev(dev
->maj
, dev
->min
);
784 prop
= lsblk_device_get_properties(dev
);
785 if (prop
&& prop
->fstype
)
786 str
= xstrdup(prop
->fstype
);
792 str
= get_vfs_attribute(dev
, id
);
795 prop
= lsblk_device_get_properties(dev
);
796 if (prop
&& prop
->fsversion
)
797 str
= xstrdup(prop
->fsversion
);
801 char *s
= lsblk_device_get_mountpoint(dev
);
809 prop
= lsblk_device_get_properties(dev
);
810 if (prop
&& prop
->label
)
811 str
= xstrdup(prop
->label
);
814 prop
= lsblk_device_get_properties(dev
);
815 if (prop
&& prop
->uuid
)
816 str
= xstrdup(prop
->uuid
);
819 prop
= lsblk_device_get_properties(dev
);
820 if (prop
&& prop
->ptuuid
)
821 str
= xstrdup(prop
->ptuuid
);
824 prop
= lsblk_device_get_properties(dev
);
825 if (prop
&& prop
->pttype
)
826 str
= xstrdup(prop
->pttype
);
829 prop
= lsblk_device_get_properties(dev
);
830 if (prop
&& prop
->parttype
)
831 str
= xstrdup(prop
->parttype
);
833 case COL_PARTTYPENAME
:
834 prop
= lsblk_device_get_properties(dev
);
835 if (prop
&& prop
->parttype
&& prop
->pttype
) {
836 const char *x
= lsblk_parttype_code_to_string(
837 prop
->parttype
, prop
->pttype
);
843 prop
= lsblk_device_get_properties(dev
);
844 if (prop
&& prop
->partlabel
)
845 str
= xstrdup(prop
->partlabel
);
848 prop
= lsblk_device_get_properties(dev
);
849 if (prop
&& prop
->partuuid
)
850 str
= xstrdup(prop
->partuuid
);
853 prop
= lsblk_device_get_properties(dev
);
854 if (prop
&& prop
->partflags
)
855 str
= xstrdup(prop
->partflags
);
858 prop
= lsblk_device_get_properties(dev
);
859 if (prop
&& prop
->wwn
)
860 str
= xstrdup(prop
->wwn
);
863 ul_path_read_string(dev
->sysfs
, &str
, "queue/read_ahead_kb");
865 str2u64(str
, sortdata
);
868 str
= xstrdup(is_readonly_device(dev
) ? "1" : "0");
871 str
= xstrdup(is_removable_device(dev
, parent
) ? "1" : "0");
874 str
= sysfs_blkdev_is_hotpluggable(dev
->sysfs
) ? xstrdup("1") : xstrdup("0");
877 ul_path_read_string(dev
->sysfs
, &str
, "queue/rotational");
880 ul_path_read_string(dev
->sysfs
, &str
, "queue/add_random");
883 if (!device_is_partition(dev
) && dev
->nslaves
== 0) {
884 prop
= lsblk_device_get_properties(dev
);
885 if (prop
&& prop
->model
)
886 str
= xstrdup(prop
->model
);
888 ul_path_read_string(dev
->sysfs
, &str
, "device/model");
892 if (!device_is_partition(dev
) && dev
->nslaves
== 0) {
893 prop
= lsblk_device_get_properties(dev
);
894 if (prop
&& prop
->serial
)
895 str
= xstrdup(prop
->serial
);
897 ul_path_read_string(dev
->sysfs
, &str
, "device/serial");
901 if (!device_is_partition(dev
) && dev
->nslaves
== 0)
902 ul_path_read_string(dev
->sysfs
, &str
, "device/rev");
905 if (!device_is_partition(dev
) && dev
->nslaves
== 0)
906 ul_path_read_string(dev
->sysfs
, &str
, "device/vendor");
910 xasprintf(&str
, "%ju", dev
->size
);
912 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, dev
->size
);
914 *sortdata
= dev
->size
;
917 if (!device_is_partition(dev
) && !dev
->dm_name
)
918 ul_path_read_string(dev
->sysfs
, &str
, "device/state");
919 else if (dev
->dm_name
) {
921 if (ul_path_read_s32(dev
->sysfs
, &x
, "dm/suspended") == 0)
922 str
= xstrdup(x
? "suspended" : "running");
926 ul_path_read_string(dev
->sysfs
, &str
, "alignment_offset");
928 str2u64(str
, sortdata
);
931 ul_path_read_string(dev
->sysfs
, &str
, "queue/minimum_io_size");
933 str2u64(str
, sortdata
);
936 ul_path_read_string(dev
->sysfs
, &str
, "queue/optimal_io_size");
938 str2u64(str
, sortdata
);
941 ul_path_read_string(dev
->sysfs
, &str
, "queue/physical_block_size");
943 str2u64(str
, sortdata
);
946 ul_path_read_string(dev
->sysfs
, &str
, "queue/logical_block_size");
948 str2u64(str
, sortdata
);
951 str
= get_scheduler(dev
);
954 ul_path_read_string(dev
->sysfs
, &str
, "queue/nr_requests");
956 str2u64(str
, sortdata
);
964 if (sysfs_blkdev_scsi_get_hctl(dev
->sysfs
, &h
, &c
, &t
, &l
) == 0)
965 xasprintf(&str
, "%d:%d:%d:%d", h
, c
, t
, l
);
969 str
= get_transport(dev
);
972 str
= get_subsystems(dev
);
975 if (device_get_discard_granularity(dev
) > 0)
976 ul_path_read_string(dev
->sysfs
, &str
, "discard_alignment");
980 str2u64(str
, sortdata
);
984 ul_path_read_string(dev
->sysfs
, &str
, "queue/discard_granularity");
986 str2u64(str
, sortdata
);
988 uint64_t x
= device_get_discard_granularity(dev
);
989 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
996 ul_path_read_string(dev
->sysfs
, &str
, "queue/discard_max_bytes");
998 str2u64(str
, sortdata
);
1001 if (ul_path_read_u64(dev
->sysfs
, &x
, "queue/discard_max_bytes") == 0) {
1002 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1009 if (device_get_discard_granularity(dev
) > 0)
1010 ul_path_read_string(dev
->sysfs
, &str
, "queue/discard_zeroes_data");
1016 ul_path_read_string(dev
->sysfs
, &str
, "queue/write_same_max_bytes");
1018 str2u64(str
, sortdata
);
1022 if (ul_path_read_u64(dev
->sysfs
, &x
, "queue/write_same_max_bytes") == 0) {
1023 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1032 ul_path_read_string(dev
->sysfs
, &str
, "queue/zoned");
1035 ul_path_read_string(dev
->sysfs
, &str
, "queue/dax");
1043 * Adds data for all wanted columns about the device to the smartcols table
1045 static void device_to_scols(
1046 struct lsblk_device
*dev
,
1047 struct lsblk_device
*parent
,
1048 struct libscols_table
*tab
,
1049 struct libscols_line
*parent_line
)
1052 struct libscols_line
*ln
;
1053 struct lsblk_iter itr
;
1054 struct lsblk_device
*child
= NULL
;
1058 DBG(DEV
, ul_debugobj(dev
, "add '%s' to scols", dev
->name
));
1059 ON_DBG(DEV
, if (ul_path_isopen_dirfd(dev
->sysfs
)) ul_debugobj(dev
, " %s ---> is open!", dev
->name
));
1061 if (!parent
&& dev
->wholedisk
)
1062 parent
= dev
->wholedisk
;
1064 /* Do not print device more than once on --list if tree order is not requested */
1065 if (!(lsblk
->flags
& LSBLK_TREE
) && !lsblk
->force_tree_order
&& dev
->is_printed
)
1068 if (lsblk
->merge
&& list_count_entries(&dev
->parents
) > 1) {
1069 if (!lsblk_device_is_last_parent(dev
, parent
))
1074 ln
= scols_table_new_line(tab
, link_group
? NULL
: parent_line
);
1076 err(EXIT_FAILURE
, _("failed to allocate output line"));
1078 dev
->is_printed
= 1;
1081 struct lsblk_device
*p
;
1082 struct libscols_line
*gr
= parent_line
;
1084 /* Merge all my parents to the one group */
1085 DBG(DEV
, ul_debugobj(dev
, " grouping parents [--merge]"));
1086 lsblk_reset_iter(&itr
, LSBLK_ITER_FORWARD
);
1087 while (lsblk_device_next_parent(dev
, &itr
, &p
) == 0) {
1088 if (!p
->scols_line
) {
1089 DBG(DEV
, ul_debugobj(dev
, " *** ignore '%s' no scols line yet", p
->name
));
1092 DBG(DEV
, ul_debugobj(dev
, " group '%s'", p
->name
));
1093 scols_table_group_lines(tab
, p
->scols_line
, gr
, 0);
1096 /* Link the group -- this makes group->child connection */
1097 DBG(DEV
, ul_debugobj(dev
, " linking the group [--merge]"));
1098 scols_line_link_group(ln
, gr
, 0);
1101 /* read column specific data and set it to smartcols table line */
1102 for (i
= 0; i
< ncolumns
; i
++) {
1104 int id
= get_column_id(i
);
1106 if (lsblk
->sort_id
!= id
)
1107 data
= device_get_data(dev
, parent
, id
, NULL
);
1109 uint64_t sortdata
= (uint64_t) -1;
1111 data
= device_get_data(dev
, parent
, id
, &sortdata
);
1112 if (data
&& sortdata
!= (uint64_t) -1)
1113 set_sortdata_u64(ln
, i
, sortdata
);
1115 DBG(DEV
, ul_debugobj(dev
, " refer data[%zu]=\"%s\"", i
, data
));
1116 if (data
&& scols_line_refer_data(ln
, i
, data
))
1117 err(EXIT_FAILURE
, _("failed to add output data"));
1120 dev
->scols_line
= ln
;
1122 if (dev
->npartitions
== 0)
1123 /* For partitions we often read from parental whole-disk sysfs,
1124 * otherwise we can close */
1125 ul_path_close_dirfd(dev
->sysfs
);
1127 lsblk_reset_iter(&itr
, LSBLK_ITER_FORWARD
);
1128 while (lsblk_device_next_child(dev
, &itr
, &child
) == 0) {
1129 DBG(DEV
, ul_debugobj(dev
, "%s -> continue to child", dev
->name
));
1130 device_to_scols(child
, dev
, tab
, ln
);
1131 DBG(DEV
, ul_debugobj(dev
, "%s <- child done", dev
->name
));
1134 /* Let's be careful with number of open files */
1135 ul_path_close_dirfd(dev
->sysfs
);
1139 * Walks on tree and adds one line for each device to the smartcols table
1141 static void devtree_to_scols(struct lsblk_devtree
*tr
, struct libscols_table
*tab
)
1143 struct lsblk_iter itr
;
1144 struct lsblk_device
*dev
= NULL
;
1146 lsblk_reset_iter(&itr
, LSBLK_ITER_FORWARD
);
1148 while (lsblk_devtree_next_root(tr
, &itr
, &dev
) == 0)
1149 device_to_scols(dev
, NULL
, tab
, NULL
);
1153 * Reads very basic information about the device from sysfs into the device struct
1155 static int initialize_device(struct lsblk_device
*dev
,
1156 struct lsblk_device
*wholedisk
,
1161 DBG(DEV
, ul_debugobj(dev
, "initialize %s [wholedisk=%p %s]",
1162 name
, wholedisk
, wholedisk
? wholedisk
->name
: ""));
1164 if (sysfs_devname_is_hidden(lsblk
->sysroot
, name
)) {
1165 DBG(DEV
, ul_debugobj(dev
, "%s: hidden, ignore", name
));
1169 dev
->name
= xstrdup(name
);
1172 dev
->wholedisk
= wholedisk
;
1173 lsblk_ref_device(wholedisk
);
1176 dev
->filename
= get_device_path(dev
);
1177 if (!dev
->filename
) {
1178 DBG(DEV
, ul_debugobj(dev
, "%s: failed to get device path", dev
->name
));
1181 DBG(DEV
, ul_debugobj(dev
, "%s: filename=%s", dev
->name
, dev
->filename
));
1183 devno
= __sysfs_devname_to_devno(lsblk
->sysroot
, dev
->name
, wholedisk
? wholedisk
->name
: NULL
);
1185 DBG(DEV
, ul_debugobj(dev
, "%s: unknown device name", dev
->name
));
1189 dev
->sysfs
= ul_new_sysfs_path(devno
, wholedisk
? wholedisk
->sysfs
: NULL
, lsblk
->sysroot
);
1191 DBG(DEV
, ul_debugobj(dev
, "%s: failed to initialize sysfs handler", dev
->name
));
1195 dev
->maj
= major(devno
);
1196 dev
->min
= minor(devno
);
1199 if (ul_path_read_u64(dev
->sysfs
, &dev
->size
, "size") == 0) /* in sectors */
1200 dev
->size
<<= 9; /* in bytes */
1202 /* Ignore devices of zero size */
1203 if (!lsblk
->all_devices
&& dev
->size
== 0) {
1204 DBG(DEV
, ul_debugobj(dev
, "zero size device -- ignore"));
1207 if (is_dm(dev
->name
)) {
1208 ul_path_read_string(dev
->sysfs
, &dev
->dm_name
, "dm/name");
1209 if (!dev
->dm_name
) {
1210 DBG(DEV
, ul_debugobj(dev
, "%s: failed to get dm name", dev
->name
));
1215 dev
->npartitions
= sysfs_blkdev_count_partitions(dev
->sysfs
, dev
->name
);
1216 dev
->nholders
= ul_path_count_dirents(dev
->sysfs
, "holders");
1217 dev
->nslaves
= ul_path_count_dirents(dev
->sysfs
, "slaves");
1219 DBG(DEV
, ul_debugobj(dev
, "%s: npartitions=%d, nholders=%d, nslaves=%d",
1220 dev
->name
, dev
->npartitions
, dev
->nholders
, dev
->nslaves
));
1222 /* ignore non-SCSI devices */
1223 if (lsblk
->scsi
&& sysfs_blkdev_scsi_get_hctl(dev
->sysfs
, NULL
, NULL
, NULL
, NULL
)) {
1224 DBG(DEV
, ul_debugobj(dev
, "non-scsi device -- ignore"));
1228 DBG(DEV
, ul_debugobj(dev
, "%s: context successfully initialized", dev
->name
));
1232 static struct lsblk_device
*devtree_get_device_or_new(struct lsblk_devtree
*tr
,
1233 struct lsblk_device
*disk
,
1236 struct lsblk_device
*dev
= lsblk_devtree_get_device(tr
, name
);
1239 dev
= lsblk_new_device();
1241 err(EXIT_FAILURE
, _("failed to allocate device"));
1243 if (initialize_device(dev
, disk
, name
) != 0) {
1244 lsblk_unref_device(dev
);
1247 lsblk_devtree_add_device(tr
, dev
);
1248 lsblk_unref_device(dev
); /* keep it referenced by devtree only */
1250 DBG(DEV
, ul_debugobj(dev
, "%s: already processed", name
));
1255 static int process_dependencies(
1256 struct lsblk_devtree
*tr
,
1257 struct lsblk_device
*dev
,
1261 * Read devices from whole-disk device into tree
1263 static int process_partitions(struct lsblk_devtree
*tr
, struct lsblk_device
*disk
)
1271 * Do not process further if there are no partitions for
1272 * this device or the device itself is a partition.
1274 if (!disk
->npartitions
|| device_is_partition(disk
))
1277 DBG(DEV
, ul_debugobj(disk
, "%s: probe whole-disk for partitions", disk
->name
));
1279 dir
= ul_path_opendir(disk
->sysfs
, NULL
);
1281 err(EXIT_FAILURE
, _("failed to open device directory in sysfs"));
1283 while ((d
= xreaddir(dir
))) {
1284 struct lsblk_device
*part
;
1286 if (!(sysfs_blkdev_is_partition_dirent(dir
, d
, disk
->name
)))
1289 DBG(DEV
, ul_debugobj(disk
, " checking %s", d
->d_name
));
1291 part
= devtree_get_device_or_new(tr
, disk
, d
->d_name
);
1295 if (lsblk_device_new_dependence(disk
, part
) == 0)
1296 process_dependencies(tr
, part
, 0);
1298 ul_path_close_dirfd(part
->sysfs
);
1301 /* For partitions we need parental (whole-disk) sysfs directory pretty
1302 * often, so close it now when all is done */
1303 ul_path_close_dirfd(disk
->sysfs
);
1305 DBG(DEV
, ul_debugobj(disk
, "probe whole-disk for partitions -- done"));
1310 static char *get_wholedisk_from_partition_dirent(DIR *dir
, struct dirent
*d
, char *buf
, size_t bufsz
)
1315 if ((len
= readlinkat(dirfd(dir
), d
->d_name
, buf
, bufsz
- 1)) < 0)
1320 /* The path ends with ".../<device>/<partition>" */
1321 p
= strrchr(buf
, '/');
1326 p
= strrchr(buf
, '/');
1335 * Reads slaves/holders and partitions for specified device into device tree
1337 static int process_dependencies(
1338 struct lsblk_devtree
*tr
,
1339 struct lsblk_device
*dev
,
1344 const char *depname
;
1351 /* read all or specified partition */
1352 if (do_partitions
&& dev
->npartitions
)
1353 process_partitions(tr
, dev
);
1355 DBG(DEV
, ul_debugobj(dev
, "%s: reading dependencies", dev
->name
));
1357 if (!(lsblk
->inverse
? dev
->nslaves
: dev
->nholders
)) {
1358 DBG(DEV
, ul_debugobj(dev
, " ignore (no slaves/holders)"));
1362 depname
= lsblk
->inverse
? "slaves" : "holders";
1363 dir
= ul_path_opendir(dev
->sysfs
, depname
);
1365 DBG(DEV
, ul_debugobj(dev
, " ignore (no slaves/holders directory)"));
1368 ul_path_close_dirfd(dev
->sysfs
);
1370 DBG(DEV
, ul_debugobj(dev
, " %s: checking for '%s' dependence", dev
->name
, depname
));
1372 while ((d
= xreaddir(dir
))) {
1373 struct lsblk_device
*dep
= NULL
;
1374 struct lsblk_device
*disk
= NULL
;
1376 /* Is the dependency a partition? */
1377 if (sysfs_blkdev_is_partition_dirent(dir
, d
, NULL
)) {
1382 DBG(DEV
, ul_debugobj(dev
, " %s: dependence is partition", d
->d_name
));
1384 diskname
= get_wholedisk_from_partition_dirent(dir
, d
, buf
, sizeof(buf
));
1386 disk
= devtree_get_device_or_new(tr
, NULL
, diskname
);
1388 DBG(DEV
, ul_debugobj(dev
, " ignore no wholedisk ???"));
1392 dep
= devtree_get_device_or_new(tr
, disk
, d
->d_name
);
1396 if (lsblk_device_new_dependence(dev
, dep
) == 0)
1397 process_dependencies(tr
, dep
, 1);
1400 && lsblk_device_new_dependence(dep
, disk
) == 0)
1401 process_dependencies(tr
, disk
, 0);
1403 /* The dependency is a whole device. */
1405 DBG(DEV
, ul_debugobj(dev
, " %s: %s: dependence is whole-disk",
1406 dev
->name
, d
->d_name
));
1408 dep
= devtree_get_device_or_new(tr
, NULL
, d
->d_name
);
1412 if (lsblk_device_new_dependence(dev
, dep
) == 0)
1413 /* For inverse tree we don't want to show partitions
1414 * if the dependence is on whole-disk */
1415 process_dependencies(tr
, dep
, lsblk
->inverse
? 0 : 1);
1418 if (dep
&& dep
->sysfs
)
1419 ul_path_close_dirfd(dep
->sysfs
);
1420 if (disk
&& disk
->sysfs
)
1421 ul_path_close_dirfd(disk
->sysfs
);
1425 DBG(DEV
, ul_debugobj(dev
, "%s: checking for '%s' -- done", dev
->name
, depname
));
1430 * Defines the device as root node in the device tree and walks on all dependencies of the device.
1432 static int __process_one_device(struct lsblk_devtree
*tr
, char *devname
, dev_t devno
)
1434 struct lsblk_device
*dev
= NULL
;
1435 struct lsblk_device
*disk
= NULL
;
1436 char buf
[PATH_MAX
+ 1], *name
= NULL
, *diskname
= NULL
;
1437 int real_part
= 0, rc
= -EINVAL
;
1439 if (devno
== 0 && devname
) {
1442 DBG(DEV
, ul_debug("%s: reading alone device", devname
));
1444 if (stat(devname
, &st
) || !S_ISBLK(st
.st_mode
)) {
1445 warnx(_("%s: not a block device"), devname
);
1450 DBG(DEV
, ul_debug("%d:%d: reading alone device", major(devno
), minor(devno
)));
1452 assert(devno
|| devname
);
1456 /* TODO: sysfs_devno_to_devname() internally initializes path_cxt, it
1457 * would be better to use ul_new_sysfs_path() + sysfs_blkdev_get_name()
1458 * and reuse path_cxt for initialize_device()
1460 name
= sysfs_devno_to_devname(devno
, buf
, sizeof(buf
));
1463 warn(_("%s: failed to get sysfs name"), devname
);
1466 name
= xstrdup(name
);
1468 if (!strncmp(name
, "dm-", 3)) {
1469 /* dm mapping is never a real partition! */
1474 if (blkid_devno_to_wholedisk(devno
, buf
, sizeof(buf
), &diskno
)) {
1475 warn(_("%s: failed to get whole-disk device number"), name
);
1479 real_part
= devno
!= diskno
;
1484 * Device is not a partition.
1486 DBG(DEV
, ul_debug(" non-partition"));
1488 dev
= devtree_get_device_or_new(tr
, NULL
, name
);
1492 lsblk_devtree_add_root(tr
, dev
);
1493 process_dependencies(tr
, dev
, !lsblk
->inverse
);
1496 * Partition, read sysfs name of the disk device
1498 DBG(DEV
, ul_debug(" partition"));
1500 disk
= devtree_get_device_or_new(tr
, NULL
, diskname
);
1504 dev
= devtree_get_device_or_new(tr
, disk
, name
);
1508 lsblk_devtree_add_root(tr
, dev
);
1509 process_dependencies(tr
, dev
, 1);
1512 && lsblk_device_new_dependence(dev
, disk
) == 0)
1513 process_dependencies(tr
, disk
, 0);
1515 ul_path_close_dirfd(disk
->sysfs
);
1520 if (dev
&& dev
->sysfs
)
1521 ul_path_close_dirfd(dev
->sysfs
);
1522 if (disk
&& disk
->sysfs
)
1523 ul_path_close_dirfd(disk
->sysfs
);
1528 static int process_one_device(struct lsblk_devtree
*tr
, char *devname
)
1531 return __process_one_device(tr
, devname
, 0);
1535 * The /sys/block contains only root devices, and no partitions. It seems more
1536 * simple to scan /sys/dev/block where are all devices without exceptions to get
1537 * top-level devices for the reverse tree.
1539 static int process_all_devices_inverse(struct lsblk_devtree
*tr
)
1543 struct path_cxt
*pc
= ul_new_path(_PATH_SYS_DEVBLOCK
);
1545 assert(lsblk
->inverse
);
1548 err(EXIT_FAILURE
, _("failed to allocate /sys handler"));
1550 ul_path_set_prefix(pc
, lsblk
->sysroot
);
1551 dir
= ul_path_opendir(pc
, NULL
);
1555 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_DEVBLOCK
));
1557 while ((d
= xreaddir(dir
))) {
1561 DBG(DEV
, ul_debug(" %s dentry", d
->d_name
));
1563 if (sscanf(d
->d_name
, "%d:%d", &maj
, &min
) != 2)
1565 devno
= makedev(maj
, min
);
1567 if (is_maj_excluded(maj
) || !is_maj_included(maj
))
1569 if (ul_path_countf_dirents(pc
, "%s/holders", d
->d_name
) != 0)
1571 if (sysfs_devno_count_partitions(devno
) != 0)
1573 __process_one_device(tr
, NULL
, devno
);
1579 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_DEVBLOCK
" -- done"));
1584 * Reads root nodes (devices) from /sys/block into devices tree
1586 static int process_all_devices(struct lsblk_devtree
*tr
)
1590 struct path_cxt
*pc
;
1592 assert(lsblk
->inverse
== 0);
1594 pc
= ul_new_path(_PATH_SYS_BLOCK
);
1596 err(EXIT_FAILURE
, _("failed to allocate /sys handler"));
1598 ul_path_set_prefix(pc
, lsblk
->sysroot
);
1599 dir
= ul_path_opendir(pc
, NULL
);
1603 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_BLOCK
));
1605 while ((d
= xreaddir(dir
))) {
1606 struct lsblk_device
*dev
= NULL
;
1608 DBG(DEV
, ul_debug(" %s dentry", d
->d_name
));
1609 dev
= devtree_get_device_or_new(tr
, NULL
, d
->d_name
);
1613 /* remove unwanted devices */
1614 if (is_maj_excluded(dev
->maj
) || !is_maj_included(dev
->maj
)) {
1615 DBG(DEV
, ul_debug(" %s: ignore (by filter)", d
->d_name
));
1616 lsblk_devtree_remove_device(tr
, dev
);
1622 DBG(DEV
, ul_debug(" %s: ignore (in-middle)", d
->d_name
));
1626 lsblk_devtree_add_root(tr
, dev
);
1627 process_dependencies(tr
, dev
, 1);
1629 /* Let's be careful with number of open files */
1630 if (dev
&& dev
->sysfs
)
1631 ul_path_close_dirfd(dev
->sysfs
);
1637 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_BLOCK
" -- done"));
1642 * Parses major numbers as specified on lsblk command line
1644 static void parse_excludes(const char *str0
)
1646 const char *str
= str0
;
1648 while (str
&& *str
) {
1653 n
= strtoul(str
, &end
, 10);
1655 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1656 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1657 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1658 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1659 excludes
[nexcludes
++] = n
;
1661 if (nexcludes
== ARRAY_SIZE(excludes
))
1662 /* TRANSLATORS: The standard value for %d is 256. */
1663 errx(EXIT_FAILURE
, _("the list of excluded devices is "
1664 "too large (limit is %d devices)"),
1665 (int)ARRAY_SIZE(excludes
));
1667 str
= end
&& *end
? end
+ 1 : NULL
;
1672 * Parses major numbers as specified on lsblk command line
1673 * (TODO: what about refactor and merge parse_excludes() and parse_includes().)
1675 static void parse_includes(const char *str0
)
1677 const char *str
= str0
;
1679 while (str
&& *str
) {
1684 n
= strtoul(str
, &end
, 10);
1686 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1687 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1688 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1689 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1690 includes
[nincludes
++] = n
;
1692 if (nincludes
== ARRAY_SIZE(includes
))
1693 /* TRANSLATORS: The standard value for %d is 256. */
1694 errx(EXIT_FAILURE
, _("the list of included devices is "
1695 "too large (limit is %d devices)"),
1696 (int)ARRAY_SIZE(includes
));
1697 str
= end
&& *end
? end
+ 1 : NULL
;
1702 * see set_sortdata_u64() and columns initialization in main()
1704 static int cmp_u64_cells(struct libscols_cell
*a
,
1705 struct libscols_cell
*b
,
1706 __attribute__((__unused__
)) void *data
)
1708 uint64_t *adata
= (uint64_t *) scols_cell_get_userdata(a
),
1709 *bdata
= (uint64_t *) scols_cell_get_userdata(b
);
1711 if (adata
== NULL
&& bdata
== NULL
)
1717 return *adata
== *bdata
? 0 : *adata
>= *bdata
? 1 : -1;
1720 static void device_set_dedupkey(
1721 struct lsblk_device
*dev
,
1722 struct lsblk_device
*parent
,
1725 struct lsblk_iter itr
;
1726 struct lsblk_device
*child
= NULL
;
1728 dev
->dedupkey
= device_get_data(dev
, parent
, id
, NULL
);
1730 DBG(DEV
, ul_debugobj(dev
, "%s: de-duplication key: %s", dev
->name
, dev
->dedupkey
));
1732 if (dev
->npartitions
== 0)
1733 /* For partitions we often read from parental whole-disk sysfs,
1734 * otherwise we can close */
1735 ul_path_close_dirfd(dev
->sysfs
);
1737 lsblk_reset_iter(&itr
, LSBLK_ITER_FORWARD
);
1739 while (lsblk_device_next_child(dev
, &itr
, &child
) == 0)
1740 device_set_dedupkey(child
, dev
, id
);
1742 /* Let's be careful with number of open files */
1743 ul_path_close_dirfd(dev
->sysfs
);
1746 static void devtree_set_dedupkeys(struct lsblk_devtree
*tr
, int id
)
1748 struct lsblk_iter itr
;
1749 struct lsblk_device
*dev
= NULL
;
1751 lsblk_reset_iter(&itr
, LSBLK_ITER_FORWARD
);
1753 while (lsblk_devtree_next_root(tr
, &itr
, &dev
) == 0)
1754 device_set_dedupkey(dev
, NULL
, id
);
1757 static void __attribute__((__noreturn__
)) usage(void)
1762 fputs(USAGE_HEADER
, out
);
1763 fprintf(out
, _(" %s [options] [<device> ...]\n"), program_invocation_short_name
);
1765 fputs(USAGE_SEPARATOR
, out
);
1766 fputs(_("List information about block devices.\n"), out
);
1768 fputs(USAGE_OPTIONS
, out
);
1769 fputs(_(" -D, --discard print discard capabilities\n"), out
);
1770 fputs(_(" -E, --dedup <column> de-duplicate output by <column>\n"), out
);
1771 fputs(_(" -I, --include <list> show only devices with specified major numbers\n"), out
);
1772 fputs(_(" -J, --json use JSON output format\n"), out
);
1773 fputs(_(" -O, --output-all output all columns\n"), out
);
1774 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out
);
1775 fputs(_(" -S, --scsi output info about SCSI devices\n"), out
);
1776 fputs(_(" -T, --tree[=<column>] use tree format output\n"), out
);
1777 fputs(_(" -a, --all print all devices\n"), out
);
1778 fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out
);
1779 fputs(_(" -d, --nodeps don't print slaves or holders\n"), out
);
1780 fputs(_(" -e, --exclude <list> exclude devices by major number (default: RAM disks)\n"), out
);
1781 fputs(_(" -f, --fs output info about filesystems\n"), out
);
1782 fputs(_(" -i, --ascii use ascii characters only\n"), out
);
1783 fputs(_(" -l, --list use list format output\n"), out
);
1784 fputs(_(" -M, --merge group parents of sub-trees (usable for RAIDs, Multi-path)\n"), out
);
1785 fputs(_(" -m, --perms output info about permissions\n"), out
);
1786 fputs(_(" -n, --noheadings don't print headings\n"), out
);
1787 fputs(_(" -o, --output <list> output columns\n"), out
);
1788 fputs(_(" -p, --paths print complete device path\n"), out
);
1789 fputs(_(" -r, --raw use raw output format\n"), out
);
1790 fputs(_(" -s, --inverse inverse dependencies\n"), out
);
1791 fputs(_(" -t, --topology output info about topology\n"), out
);
1792 fputs(_(" -z, --zoned print zone model\n"), out
);
1793 fputs(_(" -x, --sort <column> sort output by <column>\n"), out
);
1794 fputs(_(" --sysroot <dir> use specified directory as system root\n"), out
);
1795 fputs(USAGE_SEPARATOR
, out
);
1796 printf(USAGE_HELP_OPTIONS(22));
1798 fprintf(out
, USAGE_COLUMNS
);
1800 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
1801 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
1803 printf(USAGE_MAN_TAIL("lsblk(8)"));
1808 static void check_sysdevblock(void)
1810 if (access(_PATH_SYS_DEVBLOCK
, R_OK
) != 0)
1811 err(EXIT_FAILURE
, _("failed to access sysfs directory: %s"),
1812 _PATH_SYS_DEVBLOCK
);
1815 int main(int argc
, char *argv
[])
1817 struct lsblk _ls
= {
1820 .flags
= LSBLK_TREE
,
1823 struct lsblk_devtree
*tr
= NULL
;
1824 int c
, status
= EXIT_FAILURE
;
1825 char *outarg
= NULL
;
1827 int force_tree
= 0, has_tree_col
= 0;
1830 OPT_SYSROOT
= CHAR_MAX
+ 1
1833 static const struct option longopts
[] = {
1834 { "all", no_argument
, NULL
, 'a' },
1835 { "bytes", no_argument
, NULL
, 'b' },
1836 { "nodeps", no_argument
, NULL
, 'd' },
1837 { "discard", no_argument
, NULL
, 'D' },
1838 { "dedup", required_argument
, NULL
, 'E' },
1839 { "zoned", no_argument
, NULL
, 'z' },
1840 { "help", no_argument
, NULL
, 'h' },
1841 { "json", no_argument
, NULL
, 'J' },
1842 { "output", required_argument
, NULL
, 'o' },
1843 { "output-all", no_argument
, NULL
, 'O' },
1844 { "merge", no_argument
, NULL
, 'M' },
1845 { "perms", no_argument
, NULL
, 'm' },
1846 { "noheadings", no_argument
, NULL
, 'n' },
1847 { "list", no_argument
, NULL
, 'l' },
1848 { "ascii", no_argument
, NULL
, 'i' },
1849 { "raw", no_argument
, NULL
, 'r' },
1850 { "inverse", no_argument
, NULL
, 's' },
1851 { "fs", no_argument
, NULL
, 'f' },
1852 { "exclude", required_argument
, NULL
, 'e' },
1853 { "include", required_argument
, NULL
, 'I' },
1854 { "topology", no_argument
, NULL
, 't' },
1855 { "paths", no_argument
, NULL
, 'p' },
1856 { "pairs", no_argument
, NULL
, 'P' },
1857 { "scsi", no_argument
, NULL
, 'S' },
1858 { "sort", required_argument
, NULL
, 'x' },
1859 { "sysroot", required_argument
, NULL
, OPT_SYSROOT
},
1860 { "tree", optional_argument
, NULL
, 'T' },
1861 { "version", no_argument
, NULL
, 'V' },
1862 { NULL
, 0, NULL
, 0 },
1865 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
1874 { 'P','T', 'l','r' },
1877 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1879 setlocale(LC_ALL
, "");
1880 bindtextdomain(PACKAGE
, LOCALEDIR
);
1881 textdomain(PACKAGE
);
1882 close_stdout_atexit();
1888 while((c
= getopt_long(argc
, argv
,
1889 "abdDzE:e:fhJlnMmo:OpPiI:rstVST::x:", longopts
, NULL
)) != -1) {
1891 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1895 lsblk
->all_devices
= 1;
1904 add_uniq_column(COL_NAME
);
1905 add_uniq_column(COL_DALIGN
);
1906 add_uniq_column(COL_DGRAN
);
1907 add_uniq_column(COL_DMAX
);
1908 add_uniq_column(COL_DZERO
);
1911 add_uniq_column(COL_NAME
);
1912 add_uniq_column(COL_ZONED
);
1915 parse_excludes(optarg
);
1918 lsblk
->flags
|= LSBLK_JSON
;
1921 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
1927 lsblk
->flags
|= LSBLK_NOHEADINGS
;
1933 for (ncolumns
= 0 ; ncolumns
< ARRAY_SIZE(infos
); ncolumns
++)
1934 columns
[ncolumns
] = ncolumns
;
1940 lsblk
->flags
|= LSBLK_EXPORT
;
1941 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
1944 lsblk
->flags
|= LSBLK_ASCII
;
1947 parse_includes(optarg
);
1950 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
1951 lsblk
->flags
|= LSBLK_RAW
; /* enable raw */
1957 add_uniq_column(COL_NAME
);
1958 add_uniq_column(COL_FSTYPE
);
1959 add_uniq_column(COL_FSVERSION
);
1960 add_uniq_column(COL_LABEL
);
1961 add_uniq_column(COL_UUID
);
1962 add_uniq_column(COL_FSAVAIL
);
1963 add_uniq_column(COL_FSUSEPERC
);
1964 add_uniq_column(COL_TARGET
);
1967 add_uniq_column(COL_NAME
);
1968 add_uniq_column(COL_SIZE
);
1969 add_uniq_column(COL_OWNER
);
1970 add_uniq_column(COL_GROUP
);
1971 add_uniq_column(COL_MODE
);
1974 add_uniq_column(COL_NAME
);
1975 add_uniq_column(COL_ALIOFF
);
1976 add_uniq_column(COL_MINIO
);
1977 add_uniq_column(COL_OPTIO
);
1978 add_uniq_column(COL_PHYSEC
);
1979 add_uniq_column(COL_LOGSEC
);
1980 add_uniq_column(COL_ROTA
);
1981 add_uniq_column(COL_SCHED
);
1982 add_uniq_column(COL_RQ_SIZE
);
1983 add_uniq_column(COL_RA
);
1984 add_uniq_column(COL_WSAME
);
1989 add_uniq_column(COL_NAME
);
1990 add_uniq_column(COL_HCTL
);
1991 add_uniq_column(COL_TYPE
);
1992 add_uniq_column(COL_VENDOR
);
1993 add_uniq_column(COL_MODEL
);
1994 add_uniq_column(COL_REV
);
1995 add_uniq_column(COL_SERIAL
);
1996 add_uniq_column(COL_TRANSPORT
);
2003 lsblk
->tree_id
= column_name_to_id(optarg
, strlen(optarg
));
2007 lsblk
->sysroot
= optarg
;
2010 lsblk
->dedup_id
= column_name_to_id(optarg
, strlen(optarg
));
2011 if (lsblk
->dedup_id
>= 0)
2013 errtryhelp(EXIT_FAILURE
);
2016 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
2017 lsblk
->sort_id
= column_name_to_id(optarg
, strlen(optarg
));
2018 if (lsblk
->sort_id
>= 0)
2020 errtryhelp(EXIT_FAILURE
);
2026 print_version(EXIT_SUCCESS
);
2028 errtryhelp(EXIT_FAILURE
);
2033 lsblk
->flags
|= LSBLK_TREE
;
2035 check_sysdevblock();
2038 add_column(COL_NAME
);
2039 add_column(COL_MAJMIN
);
2041 add_column(COL_SIZE
);
2043 add_column(COL_TYPE
);
2044 add_column(COL_TARGET
);
2047 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
2048 &ncolumns
, column_name_to_id
) < 0)
2049 return EXIT_FAILURE
;
2051 if (lsblk
->all_devices
== 0 && nexcludes
== 0 && nincludes
== 0)
2052 excludes
[nexcludes
++] = 1; /* default: ignore RAM disks */
2054 if (lsblk
->sort_id
< 0)
2055 /* Since Linux 4.8 we have sort devices by default, because
2056 * /sys is no more sorted */
2057 lsblk
->sort_id
= COL_MAJMIN
;
2059 /* For --{inverse,raw,pairs} --list we still follow parent->child relation */
2060 if (!(lsblk
->flags
& LSBLK_TREE
)
2061 && (lsblk
->inverse
|| lsblk
->flags
& LSBLK_EXPORT
|| lsblk
->flags
& LSBLK_RAW
))
2062 lsblk
->force_tree_order
= 1;
2064 if (lsblk
->sort_id
>= 0 && column_id_to_number(lsblk
->sort_id
) < 0) {
2065 /* the sort column is not between output columns -- add as hidden */
2066 add_column(lsblk
->sort_id
);
2067 lsblk
->sort_hidden
= 1;
2070 if (lsblk
->dedup_id
>= 0 && column_id_to_number(lsblk
->dedup_id
) < 0) {
2071 /* the deduplication column is not between output columns -- add as hidden */
2072 add_column(lsblk
->dedup_id
);
2073 lsblk
->dedup_hidden
= 1;
2077 scols_init_debug(0);
2078 ul_path_init_debug();
2081 * initialize output columns
2083 if (!(lsblk
->table
= scols_new_table()))
2084 errx(EXIT_FAILURE
, _("failed to allocate output table"));
2085 scols_table_enable_raw(lsblk
->table
, !!(lsblk
->flags
& LSBLK_RAW
));
2086 scols_table_enable_export(lsblk
->table
, !!(lsblk
->flags
& LSBLK_EXPORT
));
2087 scols_table_enable_ascii(lsblk
->table
, !!(lsblk
->flags
& LSBLK_ASCII
));
2088 scols_table_enable_json(lsblk
->table
, !!(lsblk
->flags
& LSBLK_JSON
));
2089 scols_table_enable_noheadings(lsblk
->table
, !!(lsblk
->flags
& LSBLK_NOHEADINGS
));
2091 if (lsblk
->flags
& LSBLK_JSON
)
2092 scols_table_set_name(lsblk
->table
, "blockdevices");
2094 for (i
= 0; i
< ncolumns
; i
++) {
2095 struct colinfo
*ci
= get_column_info(i
);
2096 struct libscols_column
*cl
;
2097 int id
= get_column_id(i
), fl
= ci
->flags
;
2099 if ((lsblk
->flags
& LSBLK_TREE
)
2100 && has_tree_col
== 0
2101 && id
== lsblk
->tree_id
) {
2102 fl
|= SCOLS_FL_TREE
;
2103 fl
&= ~SCOLS_FL_RIGHT
;
2107 if (lsblk
->sort_hidden
&& lsblk
->sort_id
== id
)
2108 fl
|= SCOLS_FL_HIDDEN
;
2109 if (lsblk
->dedup_hidden
&& lsblk
->dedup_id
== id
)
2110 fl
|= SCOLS_FL_HIDDEN
;
2113 && lsblk
->flags
& LSBLK_JSON
2114 && has_tree_col
== 0
2115 && i
+ 1 == ncolumns
)
2116 /* The "--tree --json" specified, but no column with
2117 * SCOLS_FL_TREE yet; force it for the last column
2119 fl
|= SCOLS_FL_TREE
;
2121 cl
= scols_table_new_column(lsblk
->table
, ci
->name
, ci
->whint
, fl
);
2123 warn(_("failed to allocate output column"));
2126 if (!lsblk
->sort_col
&& lsblk
->sort_id
== id
) {
2127 lsblk
->sort_col
= cl
;
2128 scols_column_set_cmpfunc(cl
,
2129 ci
->type
== COLTYPE_NUM
? cmp_u64_cells
:
2130 ci
->type
== COLTYPE_SIZE
? cmp_u64_cells
:
2131 ci
->type
== COLTYPE_SORTNUM
? cmp_u64_cells
: scols_cmpstr_cells
,
2134 if (lsblk
->flags
& LSBLK_JSON
) {
2141 scols_column_set_json_type(cl
, SCOLS_JSON_NUMBER
);
2144 scols_column_set_json_type(cl
, SCOLS_JSON_BOOLEAN
);
2147 scols_column_set_json_type(cl
, SCOLS_JSON_STRING
);
2153 tr
= lsblk_new_devtree();
2155 err(EXIT_FAILURE
, _("failed to allocate device tree"));
2157 if (optind
== argc
) {
2158 int rc
= lsblk
->inverse
?
2159 process_all_devices_inverse(tr
) :
2160 process_all_devices(tr
);
2162 status
= rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
2164 int cnt
= 0, cnt_err
= 0;
2166 while (optind
< argc
) {
2167 if (process_one_device(tr
, argv
[optind
++]) != 0)
2171 status
= cnt
== 0 ? EXIT_FAILURE
: /* nothing */
2172 cnt
== cnt_err
? LSBLK_EXIT_ALLFAILED
:/* all failed */
2173 cnt_err
? LSBLK_EXIT_SOMEOK
: /* some ok */
2174 EXIT_SUCCESS
; /* all success */
2177 if (lsblk
->dedup_id
> -1) {
2178 devtree_set_dedupkeys(tr
, lsblk
->dedup_id
);
2179 lsblk_devtree_deduplicate_devices(tr
);
2182 devtree_to_scols(tr
, lsblk
->table
);
2184 if (lsblk
->sort_col
)
2185 scols_sort_table(lsblk
->table
, lsblk
->sort_col
);
2186 if (lsblk
->force_tree_order
)
2187 scols_sort_table_by_tree(lsblk
->table
);
2189 scols_print_table(lsblk
->table
);
2192 if (lsblk
->sort_col
)
2193 unref_sortdata(lsblk
->table
);
2195 scols_unref_table(lsblk
->table
);
2198 lsblk_properties_deinit();
2199 lsblk_unref_devtree(tr
);