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"
57 UL_DEBUG_DEFINE_MASK(lsblk
);
58 UL_DEBUG_DEFINE_MASKNAMES(lsblk
) = UL_DEBUG_EMPTY_MASKNAMES
;
60 #define LSBLK_EXIT_SOMEOK 64
61 #define LSBLK_EXIT_ALLFAILED 32
63 static int column_id_to_number(int id
);
124 /* basic table settings */
126 LSBLK_ASCII
= (1 << 0),
127 LSBLK_RAW
= (1 << 1),
128 LSBLK_NOHEADINGS
= (1 << 2),
129 LSBLK_EXPORT
= (1 << 3),
130 LSBLK_TREE
= (1 << 4),
131 LSBLK_JSON
= (1 << 5),
134 /* Types used for qsort() and JSON */
136 COLTYPE_STR
= 0, /* default */
137 COLTYPE_NUM
= 1, /* always u64 number */
138 COLTYPE_SORTNUM
= 2, /* string on output, u64 for qsort() */
139 COLTYPE_SIZE
= 3, /* srring by default, number when --bytes */
140 COLTYPE_BOOL
= 4 /* 0 or 1 */
145 const char *name
; /* header */
146 double whint
; /* width hint (N < 1 is in percent of termwidth) */
147 int flags
; /* SCOLS_FL_* */
149 int type
; /* COLTYPE_* */
152 /* columns descriptions */
153 static struct colinfo infos
[] = {
154 [COL_NAME
] = { "NAME", 0.25, SCOLS_FL_NOEXTREMES
, N_("device name") },
155 [COL_KNAME
] = { "KNAME", 0.3, 0, N_("internal kernel device name") },
156 [COL_PKNAME
] = { "PKNAME", 0.3, 0, N_("internal parent kernel device name") },
157 [COL_PATH
] = { "PATH", 0.3, 0, N_("path to the device node") },
158 [COL_MAJMIN
] = { "MAJ:MIN", 6, 0, N_("major:minor device number"), COLTYPE_SORTNUM
},
160 [COL_FSAVAIL
] = { "FSAVAIL", 5, SCOLS_FL_RIGHT
, N_("filesystem size available") },
161 [COL_FSSIZE
] = { "FSSIZE", 5, SCOLS_FL_RIGHT
, N_("filesystem size") },
162 [COL_FSTYPE
] = { "FSTYPE", 0.1, SCOLS_FL_TRUNC
, N_("filesystem type") },
163 [COL_FSUSED
] = { "FSUSED", 5, SCOLS_FL_RIGHT
, N_("filesystem size used") },
164 [COL_FSUSEPERC
] = { "FSUSE%", 3, SCOLS_FL_RIGHT
, N_("filesystem use percentage") },
165 [COL_FSVERSION
] = { "FSVER", 0.1, SCOLS_FL_TRUNC
, N_("filesystem version") },
167 [COL_TARGET
] = { "MOUNTPOINT", 0.10, SCOLS_FL_TRUNC
, N_("where the device is mounted") },
168 [COL_LABEL
] = { "LABEL", 0.1, 0, N_("filesystem LABEL") },
169 [COL_UUID
] = { "UUID", 36, 0, N_("filesystem UUID") },
171 [COL_PTUUID
] = { "PTUUID", 36, 0, N_("partition table identifier (usually UUID)") },
172 [COL_PTTYPE
] = { "PTTYPE", 0.1, 0, N_("partition table type") },
174 [COL_PARTTYPE
] = { "PARTTYPE", 36, 0, N_("partition type code or UUID") },
175 [COL_PARTTYPENAME
] = { "PARTTYPENAME", 0.1, 0, N_("partition type name") },
176 [COL_PARTLABEL
] = { "PARTLABEL", 0.1, 0, N_("partition LABEL") },
177 [COL_PARTUUID
] = { "PARTUUID", 36, 0, N_("partition UUID") },
178 [COL_PARTFLAGS
] = { "PARTFLAGS", 36, 0, N_("partition flags") },
180 [COL_RA
] = { "RA", 3, SCOLS_FL_RIGHT
, N_("read-ahead of the device"), COLTYPE_NUM
},
181 [COL_RO
] = { "RO", 1, SCOLS_FL_RIGHT
, N_("read-only device"), COLTYPE_BOOL
},
182 [COL_RM
] = { "RM", 1, SCOLS_FL_RIGHT
, N_("removable device"), COLTYPE_BOOL
},
183 [COL_HOTPLUG
]= { "HOTPLUG", 1, SCOLS_FL_RIGHT
, N_("removable or hotplug device (usb, pcmcia, ...)"), COLTYPE_BOOL
},
184 [COL_ROTA
] = { "ROTA", 1, SCOLS_FL_RIGHT
, N_("rotational device"), COLTYPE_BOOL
},
185 [COL_RAND
] = { "RAND", 1, SCOLS_FL_RIGHT
, N_("adds randomness"), COLTYPE_BOOL
},
186 [COL_MODEL
] = { "MODEL", 0.1, SCOLS_FL_TRUNC
, N_("device identifier") },
187 [COL_SERIAL
] = { "SERIAL", 0.1, SCOLS_FL_TRUNC
, N_("disk serial number") },
188 [COL_SIZE
] = { "SIZE", 5, SCOLS_FL_RIGHT
, N_("size of the device"), COLTYPE_SIZE
},
189 [COL_STATE
] = { "STATE", 7, SCOLS_FL_TRUNC
, N_("state of the device") },
190 [COL_OWNER
] = { "OWNER", 0.1, SCOLS_FL_TRUNC
, N_("user name"), },
191 [COL_GROUP
] = { "GROUP", 0.1, SCOLS_FL_TRUNC
, N_("group name") },
192 [COL_MODE
] = { "MODE", 10, 0, N_("device node permissions") },
193 [COL_ALIOFF
] = { "ALIGNMENT", 6, SCOLS_FL_RIGHT
, N_("alignment offset"), COLTYPE_NUM
},
194 [COL_MINIO
] = { "MIN-IO", 6, SCOLS_FL_RIGHT
, N_("minimum I/O size"), COLTYPE_NUM
},
195 [COL_OPTIO
] = { "OPT-IO", 6, SCOLS_FL_RIGHT
, N_("optimal I/O size"), COLTYPE_NUM
},
196 [COL_PHYSEC
] = { "PHY-SEC", 7, SCOLS_FL_RIGHT
, N_("physical sector size"), COLTYPE_NUM
},
197 [COL_LOGSEC
] = { "LOG-SEC", 7, SCOLS_FL_RIGHT
, N_("logical sector size"), COLTYPE_NUM
},
198 [COL_SCHED
] = { "SCHED", 0.1, 0, N_("I/O scheduler name") },
199 [COL_RQ_SIZE
]= { "RQ-SIZE", 5, SCOLS_FL_RIGHT
, N_("request queue size"), COLTYPE_NUM
},
200 [COL_TYPE
] = { "TYPE", 4, 0, N_("device type") },
201 [COL_DALIGN
] = { "DISC-ALN", 6, SCOLS_FL_RIGHT
, N_("discard alignment offset"), COLTYPE_NUM
},
202 [COL_DGRAN
] = { "DISC-GRAN", 6, SCOLS_FL_RIGHT
, N_("discard granularity"), COLTYPE_SIZE
},
203 [COL_DMAX
] = { "DISC-MAX", 6, SCOLS_FL_RIGHT
, N_("discard max bytes"), COLTYPE_SIZE
},
204 [COL_DZERO
] = { "DISC-ZERO", 1, SCOLS_FL_RIGHT
, N_("discard zeroes data"), COLTYPE_BOOL
},
205 [COL_WSAME
] = { "WSAME", 6, SCOLS_FL_RIGHT
, N_("write same max bytes"), COLTYPE_SIZE
},
206 [COL_WWN
] = { "WWN", 18, 0, N_("unique storage identifier") },
207 [COL_HCTL
] = { "HCTL", 10, 0, N_("Host:Channel:Target:Lun for SCSI") },
208 [COL_TRANSPORT
] = { "TRAN", 6, 0, N_("device transport type") },
209 [COL_SUBSYS
] = { "SUBSYSTEMS", 0.1, SCOLS_FL_NOEXTREMES
, N_("de-duplicated chain of subsystems") },
210 [COL_REV
] = { "REV", 4, SCOLS_FL_RIGHT
, N_("device revision") },
211 [COL_VENDOR
] = { "VENDOR", 0.1, SCOLS_FL_TRUNC
, N_("device vendor") },
212 [COL_ZONED
] = { "ZONED", 0.3, 0, N_("zone model") },
213 [COL_DAX
] = { "DAX", 1, SCOLS_FL_RIGHT
, N_("dax-capable device"), COLTYPE_BOOL
},
216 struct lsblk
*lsblk
; /* global handler */
219 * columns[] array specifies all currently wanted output column. The columns
220 * are defined by infos[] array and you can specify (on command line) each
221 * column twice. That's enough, dynamically allocated array of the columns is
222 * unnecessary overkill and over-engineering in this case
224 static int columns
[ARRAY_SIZE(infos
) * 2];
225 static size_t ncolumns
;
227 static inline void add_column(int id
)
229 if (ncolumns
>= ARRAY_SIZE(columns
))
230 errx(EXIT_FAILURE
, _("too many columns specified, "
231 "the limit is %zu columns"),
232 ARRAY_SIZE(columns
) - 1);
233 columns
[ ncolumns
++ ] = id
;
236 static inline void add_uniq_column(int id
)
238 if (column_id_to_number(id
) < 0)
242 static void lsblk_init_debug(void)
244 __UL_INIT_DEBUG_FROM_ENV(lsblk
, LSBLK_DEBUG_
, 0, LSBLK_DEBUG
);
248 * exclude/include devices filter based on major device numbers
250 static int excludes
[256];
251 static size_t nexcludes
;
253 static int includes
[256];
254 static size_t nincludes
;
256 static int is_maj_excluded(int maj
)
260 assert(ARRAY_SIZE(excludes
) > nexcludes
);
263 return 0; /* filter not enabled, device not excluded */
265 for (i
= 0; i
< nexcludes
; i
++) {
266 if (excludes
[i
] == maj
) {
267 DBG(FILTER
, ul_debug("exclude: maj=%d", maj
));
274 static int is_maj_included(int maj
)
278 assert(ARRAY_SIZE(includes
) > nincludes
);
281 return 1; /* filter not enabled, device is included */
283 for (i
= 0; i
< nincludes
; i
++) {
284 if (includes
[i
] == maj
) {
285 DBG(FILTER
, ul_debug("include: maj=%d", maj
));
292 /* Converts column sequential number to column ID (COL_*) */
293 static int get_column_id(int num
)
296 assert((size_t) num
< ncolumns
);
297 assert(columns
[num
] < (int) ARRAY_SIZE(infos
));
301 /* Returns column description for the column sequential number */
302 static struct colinfo
*get_column_info(int num
)
304 return &infos
[ get_column_id(num
) ];
307 /* Converts column name (as defined in the infos[] to the column ID */
308 static int column_name_to_id(const char *name
, size_t namesz
)
312 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
313 const char *cn
= infos
[i
].name
;
315 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
318 warnx(_("unknown column: %s"), name
);
322 /* Converts column ID (COL_*) to column sequential number */
323 static int column_id_to_number(int id
)
327 for (i
= 0; i
< ncolumns
; i
++)
328 if (columns
[i
] == id
)
333 /* Checks for DM prefix in the device name */
334 static int is_dm(const char *name
)
336 return strncmp(name
, "dm-", 3) ? 0 : 1;
339 /* Returns full pat to the device node (TODO: what about sysfs_blkdev_get_path()) */
340 static char *get_device_path(struct lsblk_device
*dev
)
347 if (is_dm(dev
->name
))
348 return __canonicalize_dm_name(lsblk
->sysroot
, dev
->name
);
350 snprintf(path
, sizeof(path
), "/dev/%s", dev
->name
);
351 sysfs_devname_sys_to_dev(path
);
352 return xstrdup(path
);
355 static int is_readonly_device(struct lsblk_device
*dev
)
359 if (ul_path_scanf(dev
->sysfs
, "ro", "%d", &ro
) == 1)
362 /* fallback if "ro" attribute does not exist */
363 fd
= open(dev
->filename
, O_RDONLY
);
365 if (ioctl(fd
, BLKROGET
, &ro
) != 0)
372 static char *get_scheduler(struct lsblk_device
*dev
)
375 char *p
, *res
= NULL
;
377 if (ul_path_read_buffer(dev
->sysfs
, buf
, sizeof(buf
), "queue/scheduler") == 0)
379 p
= strchr(buf
, '[');
382 p
= strchr(res
, ']');
392 static char *get_type(struct lsblk_device
*dev
)
394 char *res
= NULL
, *p
;
396 if (device_is_partition(dev
))
397 return xstrdup("part");
399 if (is_dm(dev
->name
)) {
400 char *dm_uuid
= NULL
;
402 /* The DM_UUID prefix should be set to subsystem owning
403 * the device - LVM, CRYPT, DMRAID, MPATH, PART */
404 if (ul_path_read_string(dev
->sysfs
, &dm_uuid
, "dm/uuid") > 0
407 char *dm_uuid_prefix
= strsep(&tmp
, "-");
409 if (dm_uuid_prefix
) {
410 /* kpartx hack to remove partition number */
411 if (strncasecmp(dm_uuid_prefix
, "part", 4) == 0)
412 dm_uuid_prefix
[4] = '\0';
414 res
= xstrdup(dm_uuid_prefix
);
420 /* No UUID or no prefix - just mark it as DM device */
423 } else if (!strncmp(dev
->name
, "loop", 4)) {
424 res
= xstrdup("loop");
426 } else if (!strncmp(dev
->name
, "md", 2)) {
427 char *md_level
= NULL
;
429 ul_path_read_string(dev
->sysfs
, &md_level
, "md/level");
430 res
= md_level
? md_level
: xstrdup("md");
433 const char *type
= NULL
;
436 if (ul_path_read_s32(dev
->sysfs
, &x
, "device/type") == 0)
437 type
= blkdev_scsi_type_to_name(x
);
443 for (p
= res
; p
&& *p
; p
++)
444 *p
= tolower((unsigned char) *p
);
448 /* Thanks to lsscsi code for idea of detection logic used here */
449 static char *get_transport(struct lsblk_device
*dev
)
451 struct path_cxt
*sysfs
= dev
->sysfs
;
453 const char *trans
= NULL
;
456 /* SCSI - Serial Peripheral Interface */
457 if (sysfs_blkdev_scsi_host_is(sysfs
, "spi"))
460 /* FC/FCoE - Fibre Channel / Fibre Channel over Ethernet */
461 else if (sysfs_blkdev_scsi_host_is(sysfs
, "fc")) {
462 attr
= sysfs_blkdev_scsi_host_strdup_attribute(sysfs
, "fc", "symbolic_name");
465 trans
= strstr(attr
, " over ") ? "fcoe" : "fc";
469 /* SAS - Serial Attached SCSI */
470 else if (sysfs_blkdev_scsi_host_is(sysfs
, "sas") ||
471 sysfs_blkdev_scsi_has_attribute(sysfs
, "sas_device"))
475 /* SBP - Serial Bus Protocol (FireWire) */
476 else if (sysfs_blkdev_scsi_has_attribute(sysfs
, "ieee1394_id"))
480 else if (sysfs_blkdev_scsi_host_is(sysfs
, "iscsi"))
483 /* USB - Universal Serial Bus */
484 else if (sysfs_blkdev_scsi_path_contains(sysfs
, "usb"))
488 else if (sysfs_blkdev_scsi_host_is(sysfs
, "scsi")) {
489 attr
= sysfs_blkdev_scsi_host_strdup_attribute(sysfs
, "scsi", "proc_name");
492 if (!strncmp(attr
, "ahci", 4) || !strncmp(attr
, "sata", 4))
494 else if (strstr(attr
, "ata"))
498 } else if (strncmp(dev
->name
, "nvme", 4) == 0)
501 return trans
? xstrdup(trans
) : NULL
;
504 static char *get_subsystems(struct lsblk_device
*dev
)
507 char *sub
, *chain
, *res
= NULL
;
508 size_t len
= 0, last
= 0;
510 chain
= sysfs_blkdev_get_devchain(dev
->sysfs
, path
, sizeof(path
));
514 while (sysfs_blkdev_next_subsystem(dev
->sysfs
, chain
, &sub
) == 0) {
517 /* don't create "block:scsi:scsi", but "block:scsi" */
518 if (len
&& strcmp(res
+ last
, sub
) == 0) {
524 res
= xrealloc(res
, len
+ sz
+ 2);
528 memcpy(res
+ len
, sub
, sz
+ 1);
538 #define is_parsable(_l) (scols_table_is_raw((_l)->table) || \
539 scols_table_is_export((_l)->table) || \
540 scols_table_is_json((_l)->table))
542 static char *mk_name(const char *name
)
548 xasprintf(&p
, "/dev/%s", name
);
552 sysfs_devname_sys_to_dev(p
);
556 static char *mk_dm_name(const char *name
)
562 xasprintf(&p
, "/dev/mapper/%s", name
);
568 /* stores data to scols cell userdata (invisible and independent on output)
569 * to make the original values accessible for sort functions
571 static void set_sortdata_u64(struct libscols_line
*ln
, int col
, uint64_t x
)
573 struct libscols_cell
*ce
= scols_line_get_cell(ln
, col
);
578 data
= xmalloc(sizeof(uint64_t));
580 scols_cell_set_userdata(ce
, data
);
583 /* do not modify *data on any error */
584 static void str2u64(const char *str
, uint64_t *data
)
590 if (str
== NULL
|| *str
== '\0')
592 num
= strtoumax(str
, &end
, 10);
594 if (errno
|| str
== end
|| (end
&& *end
))
599 static void unref_sortdata(struct libscols_table
*tb
)
601 struct libscols_iter
*itr
;
602 struct libscols_line
*ln
;
604 if (!tb
|| !lsblk
->sort_col
)
606 itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
609 while (scols_table_next_line(tb
, itr
, &ln
) == 0) {
610 struct libscols_cell
*ce
= scols_line_get_column_cell(ln
,
612 void *data
= scols_cell_get_userdata(ce
);
616 scols_free_iter(itr
);
619 static char *get_vfs_attribute(struct lsblk_device
*dev
, int id
)
622 uint64_t vfs_attr
= 0;
625 if (!dev
->fsstat
.f_blocks
) {
626 mnt
= lsblk_device_get_mountpoint(dev
);
627 if (!mnt
|| dev
->is_swap
)
629 if (statvfs(mnt
, &dev
->fsstat
) != 0)
635 vfs_attr
= dev
->fsstat
.f_frsize
* dev
->fsstat
.f_blocks
;
638 vfs_attr
= dev
->fsstat
.f_frsize
* dev
->fsstat
.f_bavail
;
641 vfs_attr
= dev
->fsstat
.f_frsize
* (dev
->fsstat
.f_blocks
- dev
->fsstat
.f_bfree
);
644 if (dev
->fsstat
.f_blocks
== 0)
647 xasprintf(&sizestr
, "%.0f%%",
648 (double)(dev
->fsstat
.f_blocks
- dev
->fsstat
.f_bfree
) /
649 dev
->fsstat
.f_blocks
* 100);
654 sizestr
= xstrdup("0");
655 else if (lsblk
->bytes
)
656 xasprintf(&sizestr
, "%ju", vfs_attr
);
658 sizestr
= size_to_human_string(SIZE_SUFFIX_1LETTER
, vfs_attr
);
663 static struct stat
*device_get_stat(struct lsblk_device
*dev
)
666 && stat(dev
->filename
, &dev
->st
) != 0)
672 static int is_removable_device(struct lsblk_device
*dev
, struct lsblk_device
*parent
)
676 if (dev
->removable
!= -1)
678 if (ul_path_scanf(dev
->sysfs
, "removable", "%d", &dev
->removable
) == 1)
682 pc
= sysfs_blkdev_get_parent(dev
->sysfs
);
686 /* dev is partition and parent is whole-disk */
687 if (pc
== parent
->sysfs
)
688 dev
->removable
= is_removable_device(parent
, NULL
);
690 /* parent is something else, use sysfs parent */
691 else if (ul_path_scanf(pc
, "removable", "%d", &dev
->removable
) != 1)
695 if (dev
->removable
== -1)
697 return dev
->removable
;
700 static uint64_t device_get_discard_granularity(struct lsblk_device
*dev
)
702 if (dev
->discard_granularity
== (uint64_t) -1
703 && ul_path_read_u64(dev
->sysfs
, &dev
->discard_granularity
,
704 "queue/discard_granularity") != 0)
705 dev
->discard_granularity
= 0;
707 return dev
->discard_granularity
;
711 * Generates data (string) for column specified by column ID for specified device. If sortdata
712 * is not NULL then returns number usable to sort the column if the data are available for the
715 static char *device_get_data(
716 struct lsblk_device
*dev
, /* device */
717 struct lsblk_device
*parent
, /* device parent as defined in the tree */
718 int id
, /* column ID (COL_*) */
719 uint64_t *sortdata
) /* returns sort data as number */
721 struct lsblk_devprop
*prop
= NULL
;
726 str
= dev
->dm_name
? mk_dm_name(dev
->dm_name
) : mk_name(dev
->name
);
729 str
= mk_name(dev
->name
);
733 str
= mk_name(parent
->name
);
737 str
= xstrdup(dev
->filename
);
741 prop
= lsblk_device_get_properties(dev
);
742 if (prop
&& prop
->owner
) {
743 str
= xstrdup(prop
->owner
);
745 struct stat
*st
= device_get_stat(dev
);
746 struct passwd
*pw
= st
? getpwuid(st
->st_uid
) : NULL
;
748 str
= xstrdup(pw
->pw_name
);
753 prop
= lsblk_device_get_properties(dev
);
754 if (prop
&& prop
->group
) {
755 str
= xstrdup(prop
->group
);
757 struct stat
*st
= device_get_stat(dev
);
758 struct group
*gr
= st
? getgrgid(st
->st_gid
) : NULL
;
760 str
= xstrdup(gr
->gr_name
);
765 prop
= lsblk_device_get_properties(dev
);
766 if (prop
&& prop
->mode
) {
767 str
= xstrdup(prop
->mode
);
769 struct stat
*st
= device_get_stat(dev
);
770 char md
[11] = { '\0' };
773 str
= xstrdup(xstrmode(st
->st_mode
, md
));
777 if (is_parsable(lsblk
))
778 xasprintf(&str
, "%u:%u", dev
->maj
, dev
->min
);
780 xasprintf(&str
, "%3u:%-3u", dev
->maj
, dev
->min
);
782 *sortdata
= makedev(dev
->maj
, dev
->min
);
785 prop
= lsblk_device_get_properties(dev
);
786 if (prop
&& prop
->fstype
)
787 str
= xstrdup(prop
->fstype
);
793 str
= get_vfs_attribute(dev
, id
);
796 prop
= lsblk_device_get_properties(dev
);
797 if (prop
&& prop
->fsversion
)
798 str
= xstrdup(prop
->fsversion
);
802 char *s
= lsblk_device_get_mountpoint(dev
);
810 prop
= lsblk_device_get_properties(dev
);
811 if (prop
&& prop
->label
)
812 str
= xstrdup(prop
->label
);
815 prop
= lsblk_device_get_properties(dev
);
816 if (prop
&& prop
->uuid
)
817 str
= xstrdup(prop
->uuid
);
820 prop
= lsblk_device_get_properties(dev
);
821 if (prop
&& prop
->ptuuid
)
822 str
= xstrdup(prop
->ptuuid
);
825 prop
= lsblk_device_get_properties(dev
);
826 if (prop
&& prop
->pttype
)
827 str
= xstrdup(prop
->pttype
);
830 prop
= lsblk_device_get_properties(dev
);
831 if (prop
&& prop
->parttype
)
832 str
= xstrdup(prop
->parttype
);
834 case COL_PARTTYPENAME
:
835 prop
= lsblk_device_get_properties(dev
);
836 if (prop
&& prop
->parttype
&& prop
->pttype
) {
837 const char *x
= lsblk_parttype_code_to_string(
838 prop
->parttype
, prop
->pttype
);
844 prop
= lsblk_device_get_properties(dev
);
845 if (prop
&& prop
->partlabel
)
846 str
= xstrdup(prop
->partlabel
);
849 prop
= lsblk_device_get_properties(dev
);
850 if (prop
&& prop
->partuuid
)
851 str
= xstrdup(prop
->partuuid
);
854 prop
= lsblk_device_get_properties(dev
);
855 if (prop
&& prop
->partflags
)
856 str
= xstrdup(prop
->partflags
);
859 prop
= lsblk_device_get_properties(dev
);
860 if (prop
&& prop
->wwn
)
861 str
= xstrdup(prop
->wwn
);
864 ul_path_read_string(dev
->sysfs
, &str
, "queue/read_ahead_kb");
866 str2u64(str
, sortdata
);
869 str
= xstrdup(is_readonly_device(dev
) ? "1" : "0");
872 str
= xstrdup(is_removable_device(dev
, parent
) ? "1" : "0");
875 str
= sysfs_blkdev_is_hotpluggable(dev
->sysfs
) ? xstrdup("1") : xstrdup("0");
878 ul_path_read_string(dev
->sysfs
, &str
, "queue/rotational");
881 ul_path_read_string(dev
->sysfs
, &str
, "queue/add_random");
884 if (!device_is_partition(dev
) && dev
->nslaves
== 0) {
885 prop
= lsblk_device_get_properties(dev
);
886 if (prop
&& prop
->model
)
887 str
= xstrdup(prop
->model
);
889 ul_path_read_string(dev
->sysfs
, &str
, "device/model");
893 if (!device_is_partition(dev
) && dev
->nslaves
== 0) {
894 prop
= lsblk_device_get_properties(dev
);
895 if (prop
&& prop
->serial
)
896 str
= xstrdup(prop
->serial
);
898 ul_path_read_string(dev
->sysfs
, &str
, "device/serial");
902 if (!device_is_partition(dev
) && dev
->nslaves
== 0)
903 ul_path_read_string(dev
->sysfs
, &str
, "device/rev");
906 if (!device_is_partition(dev
) && dev
->nslaves
== 0)
907 ul_path_read_string(dev
->sysfs
, &str
, "device/vendor");
911 xasprintf(&str
, "%ju", dev
->size
);
913 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, dev
->size
);
915 *sortdata
= dev
->size
;
918 if (!device_is_partition(dev
) && !dev
->dm_name
)
919 ul_path_read_string(dev
->sysfs
, &str
, "device/state");
920 else if (dev
->dm_name
) {
922 if (ul_path_read_s32(dev
->sysfs
, &x
, "dm/suspended") == 0)
923 str
= xstrdup(x
? "suspended" : "running");
927 ul_path_read_string(dev
->sysfs
, &str
, "alignment_offset");
929 str2u64(str
, sortdata
);
932 ul_path_read_string(dev
->sysfs
, &str
, "queue/minimum_io_size");
934 str2u64(str
, sortdata
);
937 ul_path_read_string(dev
->sysfs
, &str
, "queue/optimal_io_size");
939 str2u64(str
, sortdata
);
942 ul_path_read_string(dev
->sysfs
, &str
, "queue/physical_block_size");
944 str2u64(str
, sortdata
);
947 ul_path_read_string(dev
->sysfs
, &str
, "queue/logical_block_size");
949 str2u64(str
, sortdata
);
952 str
= get_scheduler(dev
);
955 ul_path_read_string(dev
->sysfs
, &str
, "queue/nr_requests");
957 str2u64(str
, sortdata
);
965 if (sysfs_blkdev_scsi_get_hctl(dev
->sysfs
, &h
, &c
, &t
, &l
) == 0)
966 xasprintf(&str
, "%d:%d:%d:%d", h
, c
, t
, l
);
970 str
= get_transport(dev
);
973 str
= get_subsystems(dev
);
976 if (device_get_discard_granularity(dev
) > 0)
977 ul_path_read_string(dev
->sysfs
, &str
, "discard_alignment");
981 str2u64(str
, sortdata
);
985 ul_path_read_string(dev
->sysfs
, &str
, "queue/discard_granularity");
987 str2u64(str
, sortdata
);
989 uint64_t x
= device_get_discard_granularity(dev
);
990 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
997 ul_path_read_string(dev
->sysfs
, &str
, "queue/discard_max_bytes");
999 str2u64(str
, sortdata
);
1002 if (ul_path_read_u64(dev
->sysfs
, &x
, "queue/discard_max_bytes") == 0) {
1003 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1010 if (device_get_discard_granularity(dev
) > 0)
1011 ul_path_read_string(dev
->sysfs
, &str
, "queue/discard_zeroes_data");
1017 ul_path_read_string(dev
->sysfs
, &str
, "queue/write_same_max_bytes");
1019 str2u64(str
, sortdata
);
1023 if (ul_path_read_u64(dev
->sysfs
, &x
, "queue/write_same_max_bytes") == 0) {
1024 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1033 ul_path_read_string(dev
->sysfs
, &str
, "queue/zoned");
1036 ul_path_read_string(dev
->sysfs
, &str
, "queue/dax");
1044 * Adds data for all wanted columns about the device to the smartcols table
1046 static void device_to_scols(
1047 struct lsblk_device
*dev
,
1048 struct lsblk_device
*parent
,
1049 struct libscols_table
*tab
,
1050 struct libscols_line
*parent_line
)
1053 struct libscols_line
*ln
;
1054 struct lsblk_iter itr
;
1055 struct lsblk_device
*child
= NULL
;
1059 DBG(DEV
, ul_debugobj(dev
, "add '%s' to scols", dev
->name
));
1060 ON_DBG(DEV
, if (ul_path_isopen_dirfd(dev
->sysfs
)) ul_debugobj(dev
, " %s ---> is open!", dev
->name
));
1062 if (!parent
&& dev
->wholedisk
)
1063 parent
= dev
->wholedisk
;
1065 /* Do not print device more than once on --list if tree order is not requested */
1066 if (!(lsblk
->flags
& LSBLK_TREE
) && !lsblk
->force_tree_order
&& dev
->is_printed
)
1069 if (lsblk
->merge
&& list_count_entries(&dev
->parents
) > 1) {
1070 if (!lsblk_device_is_last_parent(dev
, parent
))
1075 ln
= scols_table_new_line(tab
, link_group
? NULL
: parent_line
);
1077 err(EXIT_FAILURE
, _("failed to allocate output line"));
1079 dev
->is_printed
= 1;
1082 struct lsblk_device
*p
;
1083 struct libscols_line
*gr
= parent_line
;
1085 /* Merge all my parents to the one group */
1086 DBG(DEV
, ul_debugobj(dev
, " grouping parents [--merge]"));
1087 lsblk_reset_iter(&itr
, LSBLK_ITER_FORWARD
);
1088 while (lsblk_device_next_parent(dev
, &itr
, &p
) == 0) {
1089 if (!p
->scols_line
) {
1090 DBG(DEV
, ul_debugobj(dev
, " *** ignore '%s' no scols line yet", p
->name
));
1093 DBG(DEV
, ul_debugobj(dev
, " group '%s'", p
->name
));
1094 scols_table_group_lines(tab
, p
->scols_line
, gr
, 0);
1097 /* Link the group -- this makes group->child connection */
1098 DBG(DEV
, ul_debugobj(dev
, " linking the group [--merge]"));
1099 scols_line_link_group(ln
, gr
, 0);
1102 /* read column specific data and set it to smartcols table line */
1103 for (i
= 0; i
< ncolumns
; i
++) {
1105 int id
= get_column_id(i
);
1107 if (lsblk
->sort_id
!= id
)
1108 data
= device_get_data(dev
, parent
, id
, NULL
);
1110 uint64_t sortdata
= (uint64_t) -1;
1112 data
= device_get_data(dev
, parent
, id
, &sortdata
);
1113 if (data
&& sortdata
!= (uint64_t) -1)
1114 set_sortdata_u64(ln
, i
, sortdata
);
1116 DBG(DEV
, ul_debugobj(dev
, " refer data[%zu]=\"%s\"", i
, data
));
1117 if (data
&& scols_line_refer_data(ln
, i
, data
))
1118 err(EXIT_FAILURE
, _("failed to add output data"));
1121 dev
->scols_line
= ln
;
1123 if (dev
->npartitions
== 0)
1124 /* For partitions we often read from parental whole-disk sysfs,
1125 * otherwise we can close */
1126 ul_path_close_dirfd(dev
->sysfs
);
1128 lsblk_reset_iter(&itr
, LSBLK_ITER_FORWARD
);
1129 while (lsblk_device_next_child(dev
, &itr
, &child
) == 0) {
1130 DBG(DEV
, ul_debugobj(dev
, "%s -> continue to child", dev
->name
));
1131 device_to_scols(child
, dev
, tab
, ln
);
1132 DBG(DEV
, ul_debugobj(dev
, "%s <- child done", dev
->name
));
1135 /* Let's be careful with number of open files */
1136 ul_path_close_dirfd(dev
->sysfs
);
1140 * Walks on tree and adds one line for each device to the smartcols table
1142 static void devtree_to_scols(struct lsblk_devtree
*tr
, struct libscols_table
*tab
)
1144 struct lsblk_iter itr
;
1145 struct lsblk_device
*dev
= NULL
;
1147 lsblk_reset_iter(&itr
, LSBLK_ITER_FORWARD
);
1149 while (lsblk_devtree_next_root(tr
, &itr
, &dev
) == 0)
1150 device_to_scols(dev
, NULL
, tab
, NULL
);
1153 static int ignore_empty(struct lsblk_device
*dev
)
1155 /* show all non-empty devices */
1159 /* ignore empty loop devices without backing file */
1160 if (dev
->maj
== LOOPDEV_MAJOR
&&
1161 !loopdev_has_backing_file(dev
->filename
))
1168 * Reads very basic information about the device from sysfs into the device struct
1170 static int initialize_device(struct lsblk_device
*dev
,
1171 struct lsblk_device
*wholedisk
,
1176 DBG(DEV
, ul_debugobj(dev
, "initialize %s [wholedisk=%p %s]",
1177 name
, wholedisk
, wholedisk
? wholedisk
->name
: ""));
1179 if (sysfs_devname_is_hidden(lsblk
->sysroot
, name
)) {
1180 DBG(DEV
, ul_debugobj(dev
, "%s: hidden, ignore", name
));
1184 dev
->name
= xstrdup(name
);
1187 dev
->wholedisk
= wholedisk
;
1188 lsblk_ref_device(wholedisk
);
1191 dev
->filename
= get_device_path(dev
);
1192 if (!dev
->filename
) {
1193 DBG(DEV
, ul_debugobj(dev
, "%s: failed to get device path", dev
->name
));
1196 DBG(DEV
, ul_debugobj(dev
, "%s: filename=%s", dev
->name
, dev
->filename
));
1198 devno
= __sysfs_devname_to_devno(lsblk
->sysroot
, dev
->name
, wholedisk
? wholedisk
->name
: NULL
);
1200 DBG(DEV
, ul_debugobj(dev
, "%s: unknown device name", dev
->name
));
1204 dev
->sysfs
= ul_new_sysfs_path(devno
, wholedisk
? wholedisk
->sysfs
: NULL
, lsblk
->sysroot
);
1206 DBG(DEV
, ul_debugobj(dev
, "%s: failed to initialize sysfs handler", dev
->name
));
1210 dev
->maj
= major(devno
);
1211 dev
->min
= minor(devno
);
1214 if (ul_path_read_u64(dev
->sysfs
, &dev
->size
, "size") == 0) /* in sectors */
1215 dev
->size
<<= 9; /* in bytes */
1217 /* Ignore devices of zero size */
1218 if (!lsblk
->all_devices
&& ignore_empty(dev
)) {
1219 DBG(DEV
, ul_debugobj(dev
, "zero size device -- ignore"));
1222 if (is_dm(dev
->name
)) {
1223 ul_path_read_string(dev
->sysfs
, &dev
->dm_name
, "dm/name");
1224 if (!dev
->dm_name
) {
1225 DBG(DEV
, ul_debugobj(dev
, "%s: failed to get dm name", dev
->name
));
1230 dev
->npartitions
= sysfs_blkdev_count_partitions(dev
->sysfs
, dev
->name
);
1231 dev
->nholders
= ul_path_count_dirents(dev
->sysfs
, "holders");
1232 dev
->nslaves
= ul_path_count_dirents(dev
->sysfs
, "slaves");
1234 DBG(DEV
, ul_debugobj(dev
, "%s: npartitions=%d, nholders=%d, nslaves=%d",
1235 dev
->name
, dev
->npartitions
, dev
->nholders
, dev
->nslaves
));
1237 /* ignore non-SCSI devices */
1238 if (lsblk
->scsi
&& sysfs_blkdev_scsi_get_hctl(dev
->sysfs
, NULL
, NULL
, NULL
, NULL
)) {
1239 DBG(DEV
, ul_debugobj(dev
, "non-scsi device -- ignore"));
1243 DBG(DEV
, ul_debugobj(dev
, "%s: context successfully initialized", dev
->name
));
1247 static struct lsblk_device
*devtree_get_device_or_new(struct lsblk_devtree
*tr
,
1248 struct lsblk_device
*disk
,
1251 struct lsblk_device
*dev
= lsblk_devtree_get_device(tr
, name
);
1254 dev
= lsblk_new_device();
1256 err(EXIT_FAILURE
, _("failed to allocate device"));
1258 if (initialize_device(dev
, disk
, name
) != 0) {
1259 lsblk_unref_device(dev
);
1262 lsblk_devtree_add_device(tr
, dev
);
1263 lsblk_unref_device(dev
); /* keep it referenced by devtree only */
1265 DBG(DEV
, ul_debugobj(dev
, "%s: already processed", name
));
1270 static int process_dependencies(
1271 struct lsblk_devtree
*tr
,
1272 struct lsblk_device
*dev
,
1276 * Read devices from whole-disk device into tree
1278 static int process_partitions(struct lsblk_devtree
*tr
, struct lsblk_device
*disk
)
1286 * Do not process further if there are no partitions for
1287 * this device or the device itself is a partition.
1289 if (!disk
->npartitions
|| device_is_partition(disk
))
1292 DBG(DEV
, ul_debugobj(disk
, "%s: probe whole-disk for partitions", disk
->name
));
1294 dir
= ul_path_opendir(disk
->sysfs
, NULL
);
1296 err(EXIT_FAILURE
, _("failed to open device directory in sysfs"));
1298 while ((d
= xreaddir(dir
))) {
1299 struct lsblk_device
*part
;
1301 if (!(sysfs_blkdev_is_partition_dirent(dir
, d
, disk
->name
)))
1304 DBG(DEV
, ul_debugobj(disk
, " checking %s", d
->d_name
));
1306 part
= devtree_get_device_or_new(tr
, disk
, d
->d_name
);
1310 if (lsblk_device_new_dependence(disk
, part
) == 0)
1311 process_dependencies(tr
, part
, 0);
1313 ul_path_close_dirfd(part
->sysfs
);
1316 /* For partitions we need parental (whole-disk) sysfs directory pretty
1317 * often, so close it now when all is done */
1318 ul_path_close_dirfd(disk
->sysfs
);
1320 DBG(DEV
, ul_debugobj(disk
, "probe whole-disk for partitions -- done"));
1325 static char *get_wholedisk_from_partition_dirent(DIR *dir
, struct dirent
*d
, char *buf
, size_t bufsz
)
1330 if ((len
= readlinkat(dirfd(dir
), d
->d_name
, buf
, bufsz
- 1)) < 0)
1335 /* The path ends with ".../<device>/<partition>" */
1336 p
= strrchr(buf
, '/');
1341 p
= strrchr(buf
, '/');
1350 * Reads slaves/holders and partitions for specified device into device tree
1352 static int process_dependencies(
1353 struct lsblk_devtree
*tr
,
1354 struct lsblk_device
*dev
,
1359 const char *depname
;
1366 /* read all or specified partition */
1367 if (do_partitions
&& dev
->npartitions
)
1368 process_partitions(tr
, dev
);
1370 DBG(DEV
, ul_debugobj(dev
, "%s: reading dependencies", dev
->name
));
1372 if (!(lsblk
->inverse
? dev
->nslaves
: dev
->nholders
)) {
1373 DBG(DEV
, ul_debugobj(dev
, " ignore (no slaves/holders)"));
1377 depname
= lsblk
->inverse
? "slaves" : "holders";
1378 dir
= ul_path_opendir(dev
->sysfs
, depname
);
1380 DBG(DEV
, ul_debugobj(dev
, " ignore (no slaves/holders directory)"));
1383 ul_path_close_dirfd(dev
->sysfs
);
1385 DBG(DEV
, ul_debugobj(dev
, " %s: checking for '%s' dependence", dev
->name
, depname
));
1387 while ((d
= xreaddir(dir
))) {
1388 struct lsblk_device
*dep
= NULL
;
1389 struct lsblk_device
*disk
= NULL
;
1391 /* Is the dependency a partition? */
1392 if (sysfs_blkdev_is_partition_dirent(dir
, d
, NULL
)) {
1397 DBG(DEV
, ul_debugobj(dev
, " %s: dependence is partition", d
->d_name
));
1399 diskname
= get_wholedisk_from_partition_dirent(dir
, d
, buf
, sizeof(buf
));
1401 disk
= devtree_get_device_or_new(tr
, NULL
, diskname
);
1403 DBG(DEV
, ul_debugobj(dev
, " ignore no wholedisk ???"));
1407 dep
= devtree_get_device_or_new(tr
, disk
, d
->d_name
);
1411 if (lsblk_device_new_dependence(dev
, dep
) == 0)
1412 process_dependencies(tr
, dep
, 1);
1415 && lsblk_device_new_dependence(dep
, disk
) == 0)
1416 process_dependencies(tr
, disk
, 0);
1418 /* The dependency is a whole device. */
1420 DBG(DEV
, ul_debugobj(dev
, " %s: %s: dependence is whole-disk",
1421 dev
->name
, d
->d_name
));
1423 dep
= devtree_get_device_or_new(tr
, NULL
, d
->d_name
);
1427 if (lsblk_device_new_dependence(dev
, dep
) == 0)
1428 /* For inverse tree we don't want to show partitions
1429 * if the dependence is on whole-disk */
1430 process_dependencies(tr
, dep
, lsblk
->inverse
? 0 : 1);
1433 if (dep
&& dep
->sysfs
)
1434 ul_path_close_dirfd(dep
->sysfs
);
1435 if (disk
&& disk
->sysfs
)
1436 ul_path_close_dirfd(disk
->sysfs
);
1440 DBG(DEV
, ul_debugobj(dev
, "%s: checking for '%s' -- done", dev
->name
, depname
));
1445 * Defines the device as root node in the device tree and walks on all dependencies of the device.
1447 static int __process_one_device(struct lsblk_devtree
*tr
, char *devname
, dev_t devno
)
1449 struct lsblk_device
*dev
= NULL
;
1450 struct lsblk_device
*disk
= NULL
;
1451 char buf
[PATH_MAX
+ 1], *name
= NULL
, *diskname
= NULL
;
1452 int real_part
= 0, rc
= -EINVAL
;
1454 if (devno
== 0 && devname
) {
1457 DBG(DEV
, ul_debug("%s: reading alone device", devname
));
1459 if (stat(devname
, &st
) || !S_ISBLK(st
.st_mode
)) {
1460 warnx(_("%s: not a block device"), devname
);
1465 DBG(DEV
, ul_debug("%d:%d: reading alone device", major(devno
), minor(devno
)));
1467 assert(devno
|| devname
);
1471 /* TODO: sysfs_devno_to_devname() internally initializes path_cxt, it
1472 * would be better to use ul_new_sysfs_path() + sysfs_blkdev_get_name()
1473 * and reuse path_cxt for initialize_device()
1475 name
= sysfs_devno_to_devname(devno
, buf
, sizeof(buf
));
1478 warn(_("%s: failed to get sysfs name"), devname
);
1481 name
= xstrdup(name
);
1483 if (!strncmp(name
, "dm-", 3)) {
1484 /* dm mapping is never a real partition! */
1489 if (blkid_devno_to_wholedisk(devno
, buf
, sizeof(buf
), &diskno
)) {
1490 warn(_("%s: failed to get whole-disk device number"), name
);
1494 real_part
= devno
!= diskno
;
1499 * Device is not a partition.
1501 DBG(DEV
, ul_debug(" non-partition"));
1503 dev
= devtree_get_device_or_new(tr
, NULL
, name
);
1507 lsblk_devtree_add_root(tr
, dev
);
1508 process_dependencies(tr
, dev
, !lsblk
->inverse
);
1511 * Partition, read sysfs name of the disk device
1513 DBG(DEV
, ul_debug(" partition"));
1515 disk
= devtree_get_device_or_new(tr
, NULL
, diskname
);
1519 dev
= devtree_get_device_or_new(tr
, disk
, name
);
1523 lsblk_devtree_add_root(tr
, dev
);
1524 process_dependencies(tr
, dev
, 1);
1527 && lsblk_device_new_dependence(dev
, disk
) == 0)
1528 process_dependencies(tr
, disk
, 0);
1530 ul_path_close_dirfd(disk
->sysfs
);
1535 if (dev
&& dev
->sysfs
)
1536 ul_path_close_dirfd(dev
->sysfs
);
1537 if (disk
&& disk
->sysfs
)
1538 ul_path_close_dirfd(disk
->sysfs
);
1543 static int process_one_device(struct lsblk_devtree
*tr
, char *devname
)
1546 return __process_one_device(tr
, devname
, 0);
1550 * The /sys/block contains only root devices, and no partitions. It seems more
1551 * simple to scan /sys/dev/block where are all devices without exceptions to get
1552 * top-level devices for the reverse tree.
1554 static int process_all_devices_inverse(struct lsblk_devtree
*tr
)
1558 struct path_cxt
*pc
= ul_new_path(_PATH_SYS_DEVBLOCK
);
1560 assert(lsblk
->inverse
);
1563 err(EXIT_FAILURE
, _("failed to allocate /sys handler"));
1565 ul_path_set_prefix(pc
, lsblk
->sysroot
);
1566 dir
= ul_path_opendir(pc
, NULL
);
1570 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_DEVBLOCK
));
1572 while ((d
= xreaddir(dir
))) {
1576 DBG(DEV
, ul_debug(" %s dentry", d
->d_name
));
1578 if (sscanf(d
->d_name
, "%d:%d", &maj
, &min
) != 2)
1580 devno
= makedev(maj
, min
);
1582 if (is_maj_excluded(maj
) || !is_maj_included(maj
))
1584 if (ul_path_countf_dirents(pc
, "%s/holders", d
->d_name
) != 0)
1586 if (sysfs_devno_count_partitions(devno
) != 0)
1588 __process_one_device(tr
, NULL
, devno
);
1594 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_DEVBLOCK
" -- done"));
1599 * Reads root nodes (devices) from /sys/block into devices tree
1601 static int process_all_devices(struct lsblk_devtree
*tr
)
1605 struct path_cxt
*pc
;
1607 assert(lsblk
->inverse
== 0);
1609 pc
= ul_new_path(_PATH_SYS_BLOCK
);
1611 err(EXIT_FAILURE
, _("failed to allocate /sys handler"));
1613 ul_path_set_prefix(pc
, lsblk
->sysroot
);
1614 dir
= ul_path_opendir(pc
, NULL
);
1618 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_BLOCK
));
1620 while ((d
= xreaddir(dir
))) {
1621 struct lsblk_device
*dev
= NULL
;
1623 DBG(DEV
, ul_debug(" %s dentry", d
->d_name
));
1624 dev
= devtree_get_device_or_new(tr
, NULL
, d
->d_name
);
1628 /* remove unwanted devices */
1629 if (is_maj_excluded(dev
->maj
) || !is_maj_included(dev
->maj
)) {
1630 DBG(DEV
, ul_debug(" %s: ignore (by filter)", d
->d_name
));
1631 lsblk_devtree_remove_device(tr
, dev
);
1637 DBG(DEV
, ul_debug(" %s: ignore (in-middle)", d
->d_name
));
1641 lsblk_devtree_add_root(tr
, dev
);
1642 process_dependencies(tr
, dev
, 1);
1644 /* Let's be careful with number of open files */
1645 if (dev
&& dev
->sysfs
)
1646 ul_path_close_dirfd(dev
->sysfs
);
1652 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_BLOCK
" -- done"));
1657 * Parses major numbers as specified on lsblk command line
1659 static void parse_excludes(const char *str0
)
1661 const char *str
= str0
;
1663 while (str
&& *str
) {
1668 n
= strtoul(str
, &end
, 10);
1670 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1671 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1672 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1673 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1674 excludes
[nexcludes
++] = n
;
1676 if (nexcludes
== ARRAY_SIZE(excludes
))
1677 /* TRANSLATORS: The standard value for %d is 256. */
1678 errx(EXIT_FAILURE
, _("the list of excluded devices is "
1679 "too large (limit is %d devices)"),
1680 (int)ARRAY_SIZE(excludes
));
1682 str
= end
&& *end
? end
+ 1 : NULL
;
1687 * Parses major numbers as specified on lsblk command line
1688 * (TODO: what about refactor and merge parse_excludes() and parse_includes().)
1690 static void parse_includes(const char *str0
)
1692 const char *str
= str0
;
1694 while (str
&& *str
) {
1699 n
= strtoul(str
, &end
, 10);
1701 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1702 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1703 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1704 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1705 includes
[nincludes
++] = n
;
1707 if (nincludes
== ARRAY_SIZE(includes
))
1708 /* TRANSLATORS: The standard value for %d is 256. */
1709 errx(EXIT_FAILURE
, _("the list of included devices is "
1710 "too large (limit is %d devices)"),
1711 (int)ARRAY_SIZE(includes
));
1712 str
= end
&& *end
? end
+ 1 : NULL
;
1717 * see set_sortdata_u64() and columns initialization in main()
1719 static int cmp_u64_cells(struct libscols_cell
*a
,
1720 struct libscols_cell
*b
,
1721 __attribute__((__unused__
)) void *data
)
1723 uint64_t *adata
= (uint64_t *) scols_cell_get_userdata(a
),
1724 *bdata
= (uint64_t *) scols_cell_get_userdata(b
);
1726 if (adata
== NULL
&& bdata
== NULL
)
1732 return *adata
== *bdata
? 0 : *adata
>= *bdata
? 1 : -1;
1735 static void device_set_dedupkey(
1736 struct lsblk_device
*dev
,
1737 struct lsblk_device
*parent
,
1740 struct lsblk_iter itr
;
1741 struct lsblk_device
*child
= NULL
;
1743 dev
->dedupkey
= device_get_data(dev
, parent
, id
, NULL
);
1745 DBG(DEV
, ul_debugobj(dev
, "%s: de-duplication key: %s", dev
->name
, dev
->dedupkey
));
1747 if (dev
->npartitions
== 0)
1748 /* For partitions we often read from parental whole-disk sysfs,
1749 * otherwise we can close */
1750 ul_path_close_dirfd(dev
->sysfs
);
1752 lsblk_reset_iter(&itr
, LSBLK_ITER_FORWARD
);
1754 while (lsblk_device_next_child(dev
, &itr
, &child
) == 0)
1755 device_set_dedupkey(child
, dev
, id
);
1757 /* Let's be careful with number of open files */
1758 ul_path_close_dirfd(dev
->sysfs
);
1761 static void devtree_set_dedupkeys(struct lsblk_devtree
*tr
, int id
)
1763 struct lsblk_iter itr
;
1764 struct lsblk_device
*dev
= NULL
;
1766 lsblk_reset_iter(&itr
, LSBLK_ITER_FORWARD
);
1768 while (lsblk_devtree_next_root(tr
, &itr
, &dev
) == 0)
1769 device_set_dedupkey(dev
, NULL
, id
);
1772 static void __attribute__((__noreturn__
)) usage(void)
1777 fputs(USAGE_HEADER
, out
);
1778 fprintf(out
, _(" %s [options] [<device> ...]\n"), program_invocation_short_name
);
1780 fputs(USAGE_SEPARATOR
, out
);
1781 fputs(_("List information about block devices.\n"), out
);
1783 fputs(USAGE_OPTIONS
, out
);
1784 fputs(_(" -D, --discard print discard capabilities\n"), out
);
1785 fputs(_(" -E, --dedup <column> de-duplicate output by <column>\n"), out
);
1786 fputs(_(" -I, --include <list> show only devices with specified major numbers\n"), out
);
1787 fputs(_(" -J, --json use JSON output format\n"), out
);
1788 fputs(_(" -O, --output-all output all columns\n"), out
);
1789 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out
);
1790 fputs(_(" -S, --scsi output info about SCSI devices\n"), out
);
1791 fputs(_(" -T, --tree[=<column>] use tree format output\n"), out
);
1792 fputs(_(" -a, --all print all devices\n"), out
);
1793 fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out
);
1794 fputs(_(" -d, --nodeps don't print slaves or holders\n"), out
);
1795 fputs(_(" -e, --exclude <list> exclude devices by major number (default: RAM disks)\n"), out
);
1796 fputs(_(" -f, --fs output info about filesystems\n"), out
);
1797 fputs(_(" -i, --ascii use ascii characters only\n"), out
);
1798 fputs(_(" -l, --list use list format output\n"), out
);
1799 fputs(_(" -M, --merge group parents of sub-trees (usable for RAIDs, Multi-path)\n"), out
);
1800 fputs(_(" -m, --perms output info about permissions\n"), out
);
1801 fputs(_(" -n, --noheadings don't print headings\n"), out
);
1802 fputs(_(" -o, --output <list> output columns\n"), out
);
1803 fputs(_(" -p, --paths print complete device path\n"), out
);
1804 fputs(_(" -r, --raw use raw output format\n"), out
);
1805 fputs(_(" -s, --inverse inverse dependencies\n"), out
);
1806 fputs(_(" -t, --topology output info about topology\n"), out
);
1807 fputs(_(" -w, --width <num> specifies output width as number of characters\n"), out
);
1808 fputs(_(" -x, --sort <column> sort output by <column>\n"), out
);
1809 fputs(_(" -z, --zoned print zone model\n"), out
);
1810 fputs(_(" --sysroot <dir> use specified directory as system root\n"), out
);
1811 fputs(USAGE_SEPARATOR
, out
);
1812 printf(USAGE_HELP_OPTIONS(22));
1814 fprintf(out
, USAGE_COLUMNS
);
1816 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
1817 fprintf(out
, " %12s %s\n", infos
[i
].name
, _(infos
[i
].help
));
1819 printf(USAGE_MAN_TAIL("lsblk(8)"));
1824 static void check_sysdevblock(void)
1826 if (access(_PATH_SYS_DEVBLOCK
, R_OK
) != 0)
1827 err(EXIT_FAILURE
, _("failed to access sysfs directory: %s"),
1828 _PATH_SYS_DEVBLOCK
);
1831 int main(int argc
, char *argv
[])
1833 struct lsblk _ls
= {
1836 .flags
= LSBLK_TREE
,
1839 struct lsblk_devtree
*tr
= NULL
;
1840 int c
, status
= EXIT_FAILURE
;
1841 char *outarg
= NULL
;
1843 unsigned int width
= 0;
1844 int force_tree
= 0, has_tree_col
= 0;
1847 OPT_SYSROOT
= CHAR_MAX
+ 1
1850 static const struct option longopts
[] = {
1851 { "all", no_argument
, NULL
, 'a' },
1852 { "bytes", no_argument
, NULL
, 'b' },
1853 { "nodeps", no_argument
, NULL
, 'd' },
1854 { "discard", no_argument
, NULL
, 'D' },
1855 { "dedup", required_argument
, NULL
, 'E' },
1856 { "zoned", no_argument
, NULL
, 'z' },
1857 { "help", no_argument
, NULL
, 'h' },
1858 { "json", no_argument
, NULL
, 'J' },
1859 { "output", required_argument
, NULL
, 'o' },
1860 { "output-all", no_argument
, NULL
, 'O' },
1861 { "merge", no_argument
, NULL
, 'M' },
1862 { "perms", no_argument
, NULL
, 'm' },
1863 { "noheadings", no_argument
, NULL
, 'n' },
1864 { "list", no_argument
, NULL
, 'l' },
1865 { "ascii", no_argument
, NULL
, 'i' },
1866 { "raw", no_argument
, NULL
, 'r' },
1867 { "inverse", no_argument
, NULL
, 's' },
1868 { "fs", no_argument
, NULL
, 'f' },
1869 { "exclude", required_argument
, NULL
, 'e' },
1870 { "include", required_argument
, NULL
, 'I' },
1871 { "topology", no_argument
, NULL
, 't' },
1872 { "paths", no_argument
, NULL
, 'p' },
1873 { "pairs", no_argument
, NULL
, 'P' },
1874 { "scsi", no_argument
, NULL
, 'S' },
1875 { "sort", required_argument
, NULL
, 'x' },
1876 { "sysroot", required_argument
, NULL
, OPT_SYSROOT
},
1877 { "tree", optional_argument
, NULL
, 'T' },
1878 { "version", no_argument
, NULL
, 'V' },
1879 { "width", required_argument
, NULL
, 'w' },
1880 { NULL
, 0, NULL
, 0 },
1883 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
1892 { 'P','T', 'l','r' },
1895 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1897 setlocale(LC_ALL
, "");
1898 bindtextdomain(PACKAGE
, LOCALEDIR
);
1899 textdomain(PACKAGE
);
1900 close_stdout_atexit();
1906 while((c
= getopt_long(argc
, argv
,
1907 "abdDzE:e:fhJlnMmo:OpPiI:rstVST::w:x:", longopts
, NULL
)) != -1) {
1909 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1913 lsblk
->all_devices
= 1;
1922 add_uniq_column(COL_NAME
);
1923 add_uniq_column(COL_DALIGN
);
1924 add_uniq_column(COL_DGRAN
);
1925 add_uniq_column(COL_DMAX
);
1926 add_uniq_column(COL_DZERO
);
1929 add_uniq_column(COL_NAME
);
1930 add_uniq_column(COL_ZONED
);
1933 parse_excludes(optarg
);
1936 lsblk
->flags
|= LSBLK_JSON
;
1939 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
1945 lsblk
->flags
|= LSBLK_NOHEADINGS
;
1951 for (ncolumns
= 0 ; ncolumns
< ARRAY_SIZE(infos
); ncolumns
++)
1952 columns
[ncolumns
] = ncolumns
;
1958 lsblk
->flags
|= LSBLK_EXPORT
;
1959 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
1962 lsblk
->flags
|= LSBLK_ASCII
;
1965 parse_includes(optarg
);
1968 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
1969 lsblk
->flags
|= LSBLK_RAW
; /* enable raw */
1975 add_uniq_column(COL_NAME
);
1976 add_uniq_column(COL_FSTYPE
);
1977 add_uniq_column(COL_FSVERSION
);
1978 add_uniq_column(COL_LABEL
);
1979 add_uniq_column(COL_UUID
);
1980 add_uniq_column(COL_FSAVAIL
);
1981 add_uniq_column(COL_FSUSEPERC
);
1982 add_uniq_column(COL_TARGET
);
1985 add_uniq_column(COL_NAME
);
1986 add_uniq_column(COL_SIZE
);
1987 add_uniq_column(COL_OWNER
);
1988 add_uniq_column(COL_GROUP
);
1989 add_uniq_column(COL_MODE
);
1992 add_uniq_column(COL_NAME
);
1993 add_uniq_column(COL_ALIOFF
);
1994 add_uniq_column(COL_MINIO
);
1995 add_uniq_column(COL_OPTIO
);
1996 add_uniq_column(COL_PHYSEC
);
1997 add_uniq_column(COL_LOGSEC
);
1998 add_uniq_column(COL_ROTA
);
1999 add_uniq_column(COL_SCHED
);
2000 add_uniq_column(COL_RQ_SIZE
);
2001 add_uniq_column(COL_RA
);
2002 add_uniq_column(COL_WSAME
);
2007 add_uniq_column(COL_NAME
);
2008 add_uniq_column(COL_HCTL
);
2009 add_uniq_column(COL_TYPE
);
2010 add_uniq_column(COL_VENDOR
);
2011 add_uniq_column(COL_MODEL
);
2012 add_uniq_column(COL_REV
);
2013 add_uniq_column(COL_SERIAL
);
2014 add_uniq_column(COL_TRANSPORT
);
2021 lsblk
->tree_id
= column_name_to_id(optarg
, strlen(optarg
));
2025 lsblk
->sysroot
= optarg
;
2028 lsblk
->dedup_id
= column_name_to_id(optarg
, strlen(optarg
));
2029 if (lsblk
->dedup_id
>= 0)
2031 errtryhelp(EXIT_FAILURE
);
2034 width
= strtou32_or_err(optarg
, _("invalid output width number argument"));
2037 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
2038 lsblk
->sort_id
= column_name_to_id(optarg
, strlen(optarg
));
2039 if (lsblk
->sort_id
>= 0)
2041 errtryhelp(EXIT_FAILURE
);
2047 print_version(EXIT_SUCCESS
);
2049 errtryhelp(EXIT_FAILURE
);
2054 lsblk
->flags
|= LSBLK_TREE
;
2056 check_sysdevblock();
2059 add_column(COL_NAME
);
2060 add_column(COL_MAJMIN
);
2062 add_column(COL_SIZE
);
2064 add_column(COL_TYPE
);
2065 add_column(COL_TARGET
);
2068 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
2069 &ncolumns
, column_name_to_id
) < 0)
2070 return EXIT_FAILURE
;
2072 if (lsblk
->all_devices
== 0 && nexcludes
== 0 && nincludes
== 0)
2073 excludes
[nexcludes
++] = 1; /* default: ignore RAM disks */
2075 if (lsblk
->sort_id
< 0)
2076 /* Since Linux 4.8 we have sort devices by default, because
2077 * /sys is no more sorted */
2078 lsblk
->sort_id
= COL_MAJMIN
;
2080 /* For --{inverse,raw,pairs} --list we still follow parent->child relation */
2081 if (!(lsblk
->flags
& LSBLK_TREE
)
2082 && (lsblk
->inverse
|| lsblk
->flags
& LSBLK_EXPORT
|| lsblk
->flags
& LSBLK_RAW
))
2083 lsblk
->force_tree_order
= 1;
2085 if (lsblk
->sort_id
>= 0 && column_id_to_number(lsblk
->sort_id
) < 0) {
2086 /* the sort column is not between output columns -- add as hidden */
2087 add_column(lsblk
->sort_id
);
2088 lsblk
->sort_hidden
= 1;
2091 if (lsblk
->dedup_id
>= 0 && column_id_to_number(lsblk
->dedup_id
) < 0) {
2092 /* the deduplication column is not between output columns -- add as hidden */
2093 add_column(lsblk
->dedup_id
);
2094 lsblk
->dedup_hidden
= 1;
2098 scols_init_debug(0);
2099 ul_path_init_debug();
2102 * initialize output columns
2104 if (!(lsblk
->table
= scols_new_table()))
2105 errx(EXIT_FAILURE
, _("failed to allocate output table"));
2106 scols_table_enable_raw(lsblk
->table
, !!(lsblk
->flags
& LSBLK_RAW
));
2107 scols_table_enable_export(lsblk
->table
, !!(lsblk
->flags
& LSBLK_EXPORT
));
2108 scols_table_enable_ascii(lsblk
->table
, !!(lsblk
->flags
& LSBLK_ASCII
));
2109 scols_table_enable_json(lsblk
->table
, !!(lsblk
->flags
& LSBLK_JSON
));
2110 scols_table_enable_noheadings(lsblk
->table
, !!(lsblk
->flags
& LSBLK_NOHEADINGS
));
2112 if (lsblk
->flags
& LSBLK_JSON
)
2113 scols_table_set_name(lsblk
->table
, "blockdevices");
2115 scols_table_set_termwidth(lsblk
->table
, width
);
2116 scols_table_set_termforce(lsblk
->table
, SCOLS_TERMFORCE_ALWAYS
);
2119 for (i
= 0; i
< ncolumns
; i
++) {
2120 struct colinfo
*ci
= get_column_info(i
);
2121 struct libscols_column
*cl
;
2122 int id
= get_column_id(i
), fl
= ci
->flags
;
2124 if ((lsblk
->flags
& LSBLK_TREE
)
2125 && has_tree_col
== 0
2126 && id
== lsblk
->tree_id
) {
2127 fl
|= SCOLS_FL_TREE
;
2128 fl
&= ~SCOLS_FL_RIGHT
;
2132 if (lsblk
->sort_hidden
&& lsblk
->sort_id
== id
)
2133 fl
|= SCOLS_FL_HIDDEN
;
2134 if (lsblk
->dedup_hidden
&& lsblk
->dedup_id
== id
)
2135 fl
|= SCOLS_FL_HIDDEN
;
2138 && lsblk
->flags
& LSBLK_JSON
2139 && has_tree_col
== 0
2140 && i
+ 1 == ncolumns
)
2141 /* The "--tree --json" specified, but no column with
2142 * SCOLS_FL_TREE yet; force it for the last column
2144 fl
|= SCOLS_FL_TREE
;
2146 cl
= scols_table_new_column(lsblk
->table
, ci
->name
, ci
->whint
, fl
);
2148 warn(_("failed to allocate output column"));
2151 if (!lsblk
->sort_col
&& lsblk
->sort_id
== id
) {
2152 lsblk
->sort_col
= cl
;
2153 scols_column_set_cmpfunc(cl
,
2154 ci
->type
== COLTYPE_NUM
? cmp_u64_cells
:
2155 ci
->type
== COLTYPE_SIZE
? cmp_u64_cells
:
2156 ci
->type
== COLTYPE_SORTNUM
? cmp_u64_cells
: scols_cmpstr_cells
,
2159 if (lsblk
->flags
& LSBLK_JSON
) {
2166 scols_column_set_json_type(cl
, SCOLS_JSON_NUMBER
);
2169 scols_column_set_json_type(cl
, SCOLS_JSON_BOOLEAN
);
2172 scols_column_set_json_type(cl
, SCOLS_JSON_STRING
);
2178 tr
= lsblk_new_devtree();
2180 err(EXIT_FAILURE
, _("failed to allocate device tree"));
2182 if (optind
== argc
) {
2183 int rc
= lsblk
->inverse
?
2184 process_all_devices_inverse(tr
) :
2185 process_all_devices(tr
);
2187 status
= rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
2189 int cnt
= 0, cnt_err
= 0;
2191 while (optind
< argc
) {
2192 if (process_one_device(tr
, argv
[optind
++]) != 0)
2196 status
= cnt
== 0 ? EXIT_FAILURE
: /* nothing */
2197 cnt
== cnt_err
? LSBLK_EXIT_ALLFAILED
:/* all failed */
2198 cnt_err
? LSBLK_EXIT_SOMEOK
: /* some ok */
2199 EXIT_SUCCESS
; /* all success */
2202 if (lsblk
->dedup_id
> -1) {
2203 devtree_set_dedupkeys(tr
, lsblk
->dedup_id
);
2204 lsblk_devtree_deduplicate_devices(tr
);
2207 devtree_to_scols(tr
, lsblk
->table
);
2209 if (lsblk
->sort_col
)
2210 scols_sort_table(lsblk
->table
, lsblk
->sort_col
);
2211 if (lsblk
->force_tree_order
)
2212 scols_sort_table_by_tree(lsblk
->table
);
2214 scols_print_table(lsblk
->table
);
2217 if (lsblk
->sort_col
)
2218 unref_sortdata(lsblk
->table
);
2220 scols_unref_table(lsblk
->table
);
2223 lsblk_properties_deinit();
2224 lsblk_unref_devtree(tr
);