2 * lsblk(8) - list block devices
4 * Copyright (C) 2010,2011,2012 Red Hat, Inc. All rights reserved.
5 * Written by Milan Broz <mbroz@redhat.com>
6 * Karel Zak <kzak@redhat.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it would be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <sys/types.h>
33 #include <sys/ioctl.h>
44 #include "pathnames.h"
46 #include "canonicalize.h"
51 #include "closestream.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
);
120 /* basic table settings */
122 LSBLK_ASCII
= (1 << 0),
123 LSBLK_RAW
= (1 << 1),
124 LSBLK_NOHEADINGS
= (1 << 2),
125 LSBLK_EXPORT
= (1 << 3),
126 LSBLK_TREE
= (1 << 4),
127 LSBLK_JSON
= (1 << 5),
130 /* Types used for qsort() and JSON */
132 COLTYPE_STR
= 0, /* default */
133 COLTYPE_NUM
= 1, /* always u64 number */
134 COLTYPE_SORTNUM
= 2, /* string on output, u64 for qsort() */
135 COLTYPE_SIZE
= 3, /* srring by default, number when --bytes */
136 COLTYPE_BOOL
= 4 /* 0 or 1 */
141 const char *name
; /* header */
142 double whint
; /* width hint (N < 1 is in percent of termwidth) */
143 int flags
; /* SCOLS_FL_* */
146 int type
; /* COLTYPE_* */
149 /* columns descriptions */
150 static struct colinfo infos
[] = {
151 [COL_NAME
] = { "NAME", 0.25, SCOLS_FL_TREE
| SCOLS_FL_NOEXTREMES
, N_("device name") },
152 [COL_KNAME
] = { "KNAME", 0.3, 0, N_("internal kernel device name") },
153 [COL_PKNAME
] = { "PKNAME", 0.3, 0, N_("internal parent kernel device name") },
154 [COL_PATH
] = { "PATH", 0.3, 0, N_("path to the device node") },
155 [COL_MAJMIN
] = { "MAJ:MIN", 6, 0, N_("major:minor device number"), COLTYPE_SORTNUM
},
157 [COL_FSAVAIL
] = { "FSAVAIL", 5, SCOLS_FL_RIGHT
, N_("filesystem size available") },
158 [COL_FSSIZE
] = { "FSSIZE", 5, SCOLS_FL_RIGHT
, N_("filesystem size") },
159 [COL_FSTYPE
] = { "FSTYPE", 0.1, SCOLS_FL_TRUNC
, N_("filesystem type") },
160 [COL_FSUSED
] = { "FSUSED", 5, SCOLS_FL_RIGHT
, N_("filesystem size used") },
161 [COL_FSUSEPERC
] = { "FSUSE%", 3, SCOLS_FL_RIGHT
, N_("filesystem use percentage") },
163 [COL_TARGET
] = { "MOUNTPOINT", 0.10, SCOLS_FL_TRUNC
, N_("where the device is mounted") },
164 [COL_LABEL
] = { "LABEL", 0.1, 0, N_("filesystem LABEL") },
165 [COL_UUID
] = { "UUID", 36, 0, N_("filesystem UUID") },
167 [COL_PTUUID
] = { "PTUUID", 36, 0, N_("partition table identifier (usually UUID)") },
168 [COL_PTTYPE
] = { "PTTYPE", 0.1, 0, N_("partition table type") },
170 [COL_PARTTYPE
] = { "PARTTYPE", 36, 0, N_("partition type UUID") },
171 [COL_PARTLABEL
] = { "PARTLABEL", 0.1, 0, N_("partition LABEL") },
172 [COL_PARTUUID
] = { "PARTUUID", 36, 0, N_("partition UUID") },
173 [COL_PARTFLAGS
] = { "PARTFLAGS", 36, 0, N_("partition flags") },
175 [COL_RA
] = { "RA", 3, SCOLS_FL_RIGHT
, N_("read-ahead of the device"), COLTYPE_NUM
},
176 [COL_RO
] = { "RO", 1, SCOLS_FL_RIGHT
, N_("read-only device"), COLTYPE_BOOL
},
177 [COL_RM
] = { "RM", 1, SCOLS_FL_RIGHT
, N_("removable device"), COLTYPE_BOOL
},
178 [COL_HOTPLUG
]= { "HOTPLUG", 1, SCOLS_FL_RIGHT
, N_("removable or hotplug device (usb, pcmcia, ...)"), COLTYPE_BOOL
},
179 [COL_ROTA
] = { "ROTA", 1, SCOLS_FL_RIGHT
, N_("rotational device"), COLTYPE_BOOL
},
180 [COL_RAND
] = { "RAND", 1, SCOLS_FL_RIGHT
, N_("adds randomness"), COLTYPE_BOOL
},
181 [COL_MODEL
] = { "MODEL", 0.1, SCOLS_FL_TRUNC
, N_("device identifier") },
182 [COL_SERIAL
] = { "SERIAL", 0.1, SCOLS_FL_TRUNC
, N_("disk serial number") },
183 [COL_SIZE
] = { "SIZE", 5, SCOLS_FL_RIGHT
, N_("size of the device"), COLTYPE_SIZE
},
184 [COL_STATE
] = { "STATE", 7, SCOLS_FL_TRUNC
, N_("state of the device") },
185 [COL_OWNER
] = { "OWNER", 0.1, SCOLS_FL_TRUNC
, N_("user name"), },
186 [COL_GROUP
] = { "GROUP", 0.1, SCOLS_FL_TRUNC
, N_("group name") },
187 [COL_MODE
] = { "MODE", 10, 0, N_("device node permissions") },
188 [COL_ALIOFF
] = { "ALIGNMENT", 6, SCOLS_FL_RIGHT
, N_("alignment offset"), COLTYPE_NUM
},
189 [COL_MINIO
] = { "MIN-IO", 6, SCOLS_FL_RIGHT
, N_("minimum I/O size"), COLTYPE_NUM
},
190 [COL_OPTIO
] = { "OPT-IO", 6, SCOLS_FL_RIGHT
, N_("optimal I/O size"), COLTYPE_NUM
},
191 [COL_PHYSEC
] = { "PHY-SEC", 7, SCOLS_FL_RIGHT
, N_("physical sector size"), COLTYPE_NUM
},
192 [COL_LOGSEC
] = { "LOG-SEC", 7, SCOLS_FL_RIGHT
, N_("logical sector size"), COLTYPE_NUM
},
193 [COL_SCHED
] = { "SCHED", 0.1, 0, N_("I/O scheduler name") },
194 [COL_RQ_SIZE
]= { "RQ-SIZE", 5, SCOLS_FL_RIGHT
, N_("request queue size"), COLTYPE_NUM
},
195 [COL_TYPE
] = { "TYPE", 4, 0, N_("device type") },
196 [COL_DALIGN
] = { "DISC-ALN", 6, SCOLS_FL_RIGHT
, N_("discard alignment offset"), COLTYPE_NUM
},
197 [COL_DGRAN
] = { "DISC-GRAN", 6, SCOLS_FL_RIGHT
, N_("discard granularity"), COLTYPE_SIZE
},
198 [COL_DMAX
] = { "DISC-MAX", 6, SCOLS_FL_RIGHT
, N_("discard max bytes"), COLTYPE_SIZE
},
199 [COL_DZERO
] = { "DISC-ZERO", 1, SCOLS_FL_RIGHT
, N_("discard zeroes data"), COLTYPE_BOOL
},
200 [COL_WSAME
] = { "WSAME", 6, SCOLS_FL_RIGHT
, N_("write same max bytes"), COLTYPE_SIZE
},
201 [COL_WWN
] = { "WWN", 18, 0, N_("unique storage identifier") },
202 [COL_HCTL
] = { "HCTL", 10, 0, N_("Host:Channel:Target:Lun for SCSI") },
203 [COL_TRANSPORT
] = { "TRAN", 6, 0, N_("device transport type") },
204 [COL_SUBSYS
] = { "SUBSYSTEMS", 0.1, SCOLS_FL_NOEXTREMES
, N_("de-duplicated chain of subsystems") },
205 [COL_REV
] = { "REV", 4, SCOLS_FL_RIGHT
, N_("device revision") },
206 [COL_VENDOR
] = { "VENDOR", 0.1, SCOLS_FL_TRUNC
, N_("device vendor") },
207 [COL_ZONED
] = { "ZONED", 0.3, 0, N_("zone model") },
210 struct lsblk
*lsblk
; /* global handler */
212 /* columns[] array specifies all currently wanted output column. The columns
213 * are defined by infos[] array and you can specify (on command line) each
214 * column twice. That's enough, dynamically allocated array of the columns is
215 * unnecessary overkill and over-engineering in this case */
216 static int columns
[ARRAY_SIZE(infos
) * 2];
217 static size_t ncolumns
;
220 static inline void add_column(int id
)
222 if (ncolumns
>= ARRAY_SIZE(columns
))
223 errx(EXIT_FAILURE
, _("too many columns specified, "
224 "the limit is %zu columns"),
225 ARRAY_SIZE(columns
) - 1);
226 columns
[ ncolumns
++ ] = id
;
229 static inline void add_uniq_column(int id
)
231 if (column_id_to_number(id
) < 0)
235 static int excludes
[256];
236 static size_t nexcludes
;
238 static int includes
[256];
239 static size_t nincludes
;
241 static void lsblk_init_debug(void)
243 __UL_INIT_DEBUG_FROM_ENV(lsblk
, LSBLK_DEBUG_
, 0, LSBLK_DEBUG
);
246 static int is_maj_excluded(int maj
)
250 assert(ARRAY_SIZE(excludes
) > nexcludes
);
253 return 0; /* filter not enabled, device not excluded */
255 for (i
= 0; i
< nexcludes
; i
++) {
256 if (excludes
[i
] == maj
) {
257 DBG(FILTER
, ul_debug("exclude: maj=%d", maj
));
264 static int is_maj_included(int maj
)
268 assert(ARRAY_SIZE(includes
) > nincludes
);
271 return 1; /* filter not enabled, device is included */
273 for (i
= 0; i
< nincludes
; i
++) {
274 if (includes
[i
] == maj
) {
275 DBG(FILTER
, ul_debug("include: maj=%d", maj
));
282 /* array with IDs of enabled columns */
283 static int get_column_id(int num
)
286 assert((size_t) num
< ncolumns
);
287 assert(columns
[num
] < (int) ARRAY_SIZE(infos
));
291 static struct colinfo
*get_column_info(int num
)
293 return &infos
[ get_column_id(num
) ];
296 static int column_name_to_id(const char *name
, size_t namesz
)
300 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
301 const char *cn
= infos
[i
].name
;
303 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
306 warnx(_("unknown column: %s"), name
);
310 static int column_id_to_number(int id
)
314 for (i
= 0; i
< ncolumns
; i
++)
315 if (columns
[i
] == id
)
320 static int is_dm(const char *name
)
322 return strncmp(name
, "dm-", 3) ? 0 : 1;
325 static struct dirent
*xreaddir(DIR *dp
)
331 while ((d
= readdir(dp
))) {
332 if (!strcmp(d
->d_name
, ".") ||
333 !strcmp(d
->d_name
, ".."))
336 /* blacklist here? */
342 static char *get_device_path(struct lsblk_device
*dev
)
349 if (is_dm(dev
->name
))
350 return __canonicalize_dm_name(lsblk
->sysroot
, dev
->name
);
352 snprintf(path
, sizeof(path
), "/dev/%s", dev
->name
);
353 sysfs_devname_sys_to_dev(path
);
354 return xstrdup(path
);
357 static int is_readonly_device(struct lsblk_device
*dev
)
361 if (ul_path_scanf(dev
->sysfs
, "ro", "%d", &ro
) == 1)
364 /* fallback if "ro" attribute does not exist */
365 fd
= open(dev
->filename
, O_RDONLY
);
367 if (ioctl(fd
, BLKROGET
, &ro
) != 0)
374 static char *get_scheduler(struct lsblk_device
*dev
)
377 char *p
, *res
= NULL
;
379 if (ul_path_read_buffer(dev
->sysfs
, buf
, sizeof(buf
), "queue/scheduler") == 0)
381 p
= strchr(buf
, '[');
384 p
= strchr(res
, ']');
394 static char *get_type(struct lsblk_device
*dev
)
396 char *res
= NULL
, *p
;
399 return xstrdup("part");
401 if (is_dm(dev
->name
)) {
402 char *dm_uuid
= NULL
;
404 /* The DM_UUID prefix should be set to subsystem owning
405 * the device - LVM, CRYPT, DMRAID, MPATH, PART */
406 if (ul_path_read_string(dev
->sysfs
, &dm_uuid
, "dm/uuid") > 0
409 char *dm_uuid_prefix
= strsep(&tmp
, "-");
411 if (dm_uuid_prefix
) {
412 /* kpartx hack to remove partition number */
413 if (strncasecmp(dm_uuid_prefix
, "part", 4) == 0)
414 dm_uuid_prefix
[4] = '\0';
416 res
= xstrdup(dm_uuid_prefix
);
422 /* No UUID or no prefix - just mark it as DM device */
425 } else if (!strncmp(dev
->name
, "loop", 4)) {
426 res
= xstrdup("loop");
428 } else if (!strncmp(dev
->name
, "md", 2)) {
429 char *md_level
= NULL
;
431 ul_path_read_string(dev
->sysfs
, &md_level
, "md/level");
432 res
= md_level
? md_level
: xstrdup("md");
435 const char *type
= NULL
;
438 if (ul_path_read_s32(dev
->sysfs
, &x
, "device/type") == 0)
439 type
= blkdev_scsi_type_to_name(x
);
445 for (p
= res
; p
&& *p
; p
++)
446 *p
= tolower((unsigned char) *p
);
450 /* Thanks to lsscsi code for idea of detection logic used here */
451 static char *get_transport(struct lsblk_device
*dev
)
453 struct path_cxt
*sysfs
= dev
->sysfs
;
455 const char *trans
= NULL
;
458 /* SCSI - Serial Peripheral Interface */
459 if (sysfs_blkdev_scsi_host_is(sysfs
, "spi"))
462 /* FC/FCoE - Fibre Channel / Fibre Channel over Ethernet */
463 else if (sysfs_blkdev_scsi_host_is(sysfs
, "fc")) {
464 attr
= sysfs_blkdev_scsi_host_strdup_attribute(sysfs
, "fc", "symbolic_name");
467 trans
= strstr(attr
, " over ") ? "fcoe" : "fc";
471 /* SAS - Serial Attached SCSI */
472 else if (sysfs_blkdev_scsi_host_is(sysfs
, "sas") ||
473 sysfs_blkdev_scsi_has_attribute(sysfs
, "sas_device"))
477 /* SBP - Serial Bus Protocol (FireWire) */
478 else if (sysfs_blkdev_scsi_has_attribute(sysfs
, "ieee1394_id"))
482 else if (sysfs_blkdev_scsi_host_is(sysfs
, "iscsi"))
485 /* USB - Universal Serial Bus */
486 else if (sysfs_blkdev_scsi_path_contains(sysfs
, "usb"))
490 else if (sysfs_blkdev_scsi_host_is(sysfs
, "scsi")) {
491 attr
= sysfs_blkdev_scsi_host_strdup_attribute(sysfs
, "scsi", "proc_name");
494 if (!strncmp(attr
, "ahci", 4) || !strncmp(attr
, "sata", 4))
496 else if (strstr(attr
, "ata"))
500 } else if (strncmp(dev
->name
, "nvme", 4) == 0)
503 return trans
? xstrdup(trans
) : NULL
;
506 static char *get_subsystems(struct lsblk_device
*dev
)
509 char *sub
, *chain
, *res
= NULL
;
510 size_t len
= 0, last
= 0;
512 chain
= sysfs_blkdev_get_devchain(dev
->sysfs
, path
, sizeof(path
));
516 while (sysfs_blkdev_next_subsystem(dev
->sysfs
, chain
, &sub
) == 0) {
519 /* don't create "block:scsi:scsi", but "block:scsi" */
520 if (len
&& strcmp(res
+ last
, sub
) == 0) {
526 res
= xrealloc(res
, len
+ sz
+ 2);
530 memcpy(res
+ len
, sub
, sz
+ 1);
540 #define is_parsable(_l) (scols_table_is_raw((_l)->table) || \
541 scols_table_is_export((_l)->table) || \
542 scols_table_is_json((_l)->table))
544 static char *mk_name(const char *name
)
550 xasprintf(&p
, "/dev/%s", name
);
554 sysfs_devname_sys_to_dev(p
);
558 static char *mk_dm_name(const char *name
)
564 xasprintf(&p
, "/dev/mapper/%s", name
);
570 /* stores data to scols cell userdata (invisible and independent on output)
571 * to make the original values accessible for sort functions
573 static void set_sortdata_u64(struct libscols_line
*ln
, int col
, uint64_t x
)
575 struct libscols_cell
*ce
= scols_line_get_cell(ln
, col
);
580 data
= xmalloc(sizeof(uint64_t));
582 scols_cell_set_userdata(ce
, data
);
585 static void set_sortdata_u64_from_string(struct libscols_line
*ln
, int col
, const char *str
)
589 if (!str
|| sscanf(str
, "%"SCNu64
, &x
) != 1)
592 set_sortdata_u64(ln
, col
, x
);
595 static void unref_sortdata(struct libscols_table
*tb
)
597 struct libscols_iter
*itr
;
598 struct libscols_line
*ln
;
600 if (!tb
|| !lsblk
->sort_col
)
602 itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
605 while (scols_table_next_line(tb
, itr
, &ln
) == 0) {
606 struct libscols_cell
*ce
= scols_line_get_column_cell(ln
,
608 void *data
= scols_cell_get_userdata(ce
);
612 scols_free_iter(itr
);
615 static char *get_vfs_attribute(struct lsblk_device
*dev
, int id
)
618 uint64_t vfs_attr
= 0;
621 if (!dev
->fsstat
.f_blocks
) {
622 mnt
= lsblk_device_get_mountpoint(dev
);
623 if (!mnt
|| dev
->is_swap
)
625 if (statvfs(mnt
, &dev
->fsstat
) != 0)
631 vfs_attr
= dev
->fsstat
.f_frsize
* dev
->fsstat
.f_blocks
;
634 vfs_attr
= dev
->fsstat
.f_frsize
* dev
->fsstat
.f_bavail
;
637 vfs_attr
= dev
->fsstat
.f_frsize
* (dev
->fsstat
.f_blocks
- dev
->fsstat
.f_bfree
);
640 if (dev
->fsstat
.f_blocks
== 0)
643 xasprintf(&sizestr
, "%.0f%%",
644 (double)(dev
->fsstat
.f_blocks
- dev
->fsstat
.f_bfree
) /
645 dev
->fsstat
.f_blocks
* 100);
650 sizestr
= xstrdup("0");
651 else if (lsblk
->bytes
)
652 xasprintf(&sizestr
, "%ju", vfs_attr
);
654 sizestr
= size_to_human_string(SIZE_SUFFIX_1LETTER
, vfs_attr
);
659 static struct stat
*device_get_stat(struct lsblk_device
*dev
)
661 if (!dev
->st
.st_rdev
)
662 stat(dev
->filename
, &dev
->st
);
667 static void set_scols_data(struct lsblk_device
*dev
, int col
, int id
, struct libscols_line
*ln
)
669 struct lsblk_devprop
*prop
;
673 if (lsblk
->sort_id
== id
)
678 str
= dev
->dm_name
? mk_dm_name(dev
->dm_name
) : mk_name(dev
->name
);
681 str
= mk_name(dev
->name
);
685 str
= mk_name(dev
->parent
->name
);
689 str
= xstrdup(dev
->filename
);
693 struct stat
*st
= device_get_stat(dev
);
694 struct passwd
*pw
= st
? NULL
: getpwuid(st
->st_uid
);
696 str
= xstrdup(pw
->pw_name
);
701 struct stat
*st
= device_get_stat(dev
);
702 struct group
*gr
= st
? NULL
: getgrgid(st
->st_gid
);
704 str
= xstrdup(gr
->gr_name
);
709 struct stat
*st
= device_get_stat(dev
);
710 char md
[11] = { '\0' };
713 str
= xstrdup(xstrmode(st
->st_mode
, md
));
717 if (is_parsable(lsblk
))
718 xasprintf(&str
, "%u:%u", dev
->maj
, dev
->min
);
720 xasprintf(&str
, "%3u:%-3u", dev
->maj
, dev
->min
);
722 set_sortdata_u64(ln
, col
, makedev(dev
->maj
, dev
->min
));
725 prop
= lsblk_device_get_properties(dev
);
726 if (prop
&& prop
->fstype
)
727 str
= xstrdup(prop
->fstype
);
733 str
= get_vfs_attribute(dev
, id
);
736 str
= xstrdup(lsblk_device_get_mountpoint(dev
));
739 prop
= lsblk_device_get_properties(dev
);
740 if (prop
&& prop
->label
)
741 str
= xstrdup(prop
->label
);
744 prop
= lsblk_device_get_properties(dev
);
745 if (prop
&& prop
->uuid
)
746 str
= xstrdup(prop
->uuid
);
749 prop
= lsblk_device_get_properties(dev
);
750 if (prop
&& prop
->ptuuid
)
751 str
= xstrdup(prop
->ptuuid
);
754 prop
= lsblk_device_get_properties(dev
);
755 if (prop
&& prop
->pttype
)
756 str
= xstrdup(prop
->pttype
);
759 prop
= lsblk_device_get_properties(dev
);
760 if (prop
&& prop
->parttype
)
761 str
= xstrdup(prop
->parttype
);
764 prop
= lsblk_device_get_properties(dev
);
765 if (prop
&& prop
->partlabel
)
766 str
= xstrdup(prop
->partlabel
);
769 prop
= lsblk_device_get_properties(dev
);
770 if (prop
&& prop
->partuuid
)
771 str
= xstrdup(prop
->partuuid
);
774 prop
= lsblk_device_get_properties(dev
);
775 if (prop
&& prop
->partflags
)
776 str
= xstrdup(prop
->partflags
);
779 prop
= lsblk_device_get_properties(dev
);
780 if (prop
&& prop
->wwn
)
781 str
= xstrdup(prop
->wwn
);
784 ul_path_read_string(dev
->sysfs
, &str
, "queue/read_ahead_kb");
786 set_sortdata_u64_from_string(ln
, col
, str
);
789 str
= xstrdup(is_readonly_device(dev
) ? "1" : "0");
792 ul_path_read_string(dev
->sysfs
, &str
, "removable");
793 if (!str
&& sysfs_blkdev_get_parent(dev
->sysfs
))
794 ul_path_read_string(sysfs_blkdev_get_parent(dev
->sysfs
),
799 str
= sysfs_blkdev_is_hotpluggable(dev
->sysfs
) ? xstrdup("1") : xstrdup("0");
802 ul_path_read_string(dev
->sysfs
, &str
, "queue/rotational");
805 ul_path_read_string(dev
->sysfs
, &str
, "queue/add_random");
808 if (!dev
->partition
&& dev
->nslaves
== 0) {
809 prop
= lsblk_device_get_properties(dev
);
810 if (prop
&& prop
->model
)
811 str
= xstrdup(prop
->model
);
813 ul_path_read_string(dev
->sysfs
, &str
, "device/model");
817 if (!dev
->partition
&& dev
->nslaves
== 0) {
818 prop
= lsblk_device_get_properties(dev
);
819 if (prop
&& prop
->serial
)
820 str
= xstrdup(prop
->serial
);
822 ul_path_read_string(dev
->sysfs
, &str
, "device/serial");
826 if (!dev
->partition
&& dev
->nslaves
== 0)
827 ul_path_read_string(dev
->sysfs
, &str
, "device/rev");
830 if (!dev
->partition
&& dev
->nslaves
== 0)
831 ul_path_read_string(dev
->sysfs
, &str
, "device/vendor");
837 xasprintf(&str
, "%ju", dev
->size
);
839 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, dev
->size
);
841 set_sortdata_u64(ln
, col
, dev
->size
);
844 if (!dev
->partition
&& !dev
->dm_name
)
845 ul_path_read_string(dev
->sysfs
, &str
, "device/state");
846 else if (dev
->dm_name
) {
848 if (ul_path_read_s32(dev
->sysfs
, &x
, "dm/suspended") == 0)
849 str
= xstrdup(x
? "suspended" : "running");
853 ul_path_read_string(dev
->sysfs
, &str
, "alignment_offset");
855 set_sortdata_u64_from_string(ln
, col
, str
);
858 ul_path_read_string(dev
->sysfs
, &str
, "queue/minimum_io_size");
860 set_sortdata_u64_from_string(ln
, col
, str
);
863 ul_path_read_string(dev
->sysfs
, &str
, "queue/optimal_io_size");
865 set_sortdata_u64_from_string(ln
, col
, str
);
868 ul_path_read_string(dev
->sysfs
, &str
, "queue/physical_block_size");
870 set_sortdata_u64_from_string(ln
, col
, str
);
873 ul_path_read_string(dev
->sysfs
, &str
, "queue/logical_block_size");
875 set_sortdata_u64_from_string(ln
, col
, str
);
878 str
= get_scheduler(dev
);
881 ul_path_read_string(dev
->sysfs
, &str
, "queue/nr_requests");
883 set_sortdata_u64_from_string(ln
, col
, str
);
891 if (sysfs_blkdev_scsi_get_hctl(dev
->sysfs
, &h
, &c
, &t
, &l
) == 0)
892 xasprintf(&str
, "%d:%d:%d:%d", h
, c
, t
, l
);
896 str
= get_transport(dev
);
899 str
= get_subsystems(dev
);
903 ul_path_read_string(dev
->sysfs
, &str
, "discard_alignment");
907 set_sortdata_u64_from_string(ln
, col
, str
);
911 ul_path_read_string(dev
->sysfs
, &str
, "queue/discard_granularity");
913 set_sortdata_u64_from_string(ln
, col
, str
);
916 if (ul_path_read_u64(dev
->sysfs
, &x
, "queue/discard_granularity") == 0) {
917 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
919 set_sortdata_u64(ln
, col
, x
);
925 ul_path_read_string(dev
->sysfs
, &str
, "queue/discard_max_bytes");
927 set_sortdata_u64_from_string(ln
, col
, str
);
930 if (ul_path_read_u64(dev
->sysfs
, &x
, "queue/discard_max_bytes") == 0) {
931 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
933 set_sortdata_u64(ln
, col
, x
);
939 ul_path_read_string(dev
->sysfs
, &str
, "queue/discard_zeroes_data");
945 ul_path_read_string(dev
->sysfs
, &str
, "queue/write_same_max_bytes");
947 set_sortdata_u64_from_string(ln
, col
, str
);
951 if (ul_path_read_u64(dev
->sysfs
, &x
, "queue/write_same_max_bytes") == 0) {
952 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
954 set_sortdata_u64(ln
, col
, x
);
961 ul_path_read_string(dev
->sysfs
, &str
, "queue/zoned");
965 if (str
&& scols_line_refer_data(ln
, col
, str
))
966 err(EXIT_FAILURE
, _("failed to add output data"));
969 static void device_to_scols(struct lsblk_device
*dev
, struct lsblk_device
*parent
, struct libscols_table
*tab
)
972 struct lsblk_iter itr
;
973 struct lsblk_device
*child
= NULL
;
975 dev
->scols_line
= scols_table_new_line(tab
, parent
? parent
->scols_line
: NULL
);
976 if (!dev
->scols_line
)
977 err(EXIT_FAILURE
, _("failed to allocate output line"));
979 for (i
= 0; i
< ncolumns
; i
++)
980 set_scols_data(dev
, i
, get_column_id(i
), dev
->scols_line
);
982 lsblk_reset_iter(&itr
, LSBLK_ITER_FORWARD
);
984 while (lsblk_device_next_child(dev
, &itr
, &child
) == 0)
985 device_to_scols(child
, dev
, tab
);
988 static void devtree_to_scols(struct lsblk_devtree
*tr
, struct libscols_table
*tab
)
990 struct lsblk_iter itr
;
991 struct lsblk_device
*dev
= NULL
;
993 lsblk_reset_iter(&itr
, LSBLK_ITER_FORWARD
);
995 while (lsblk_devtree_next_root(tr
, &itr
, &dev
) == 0)
996 device_to_scols(dev
, NULL
, tab
);
999 static int set_device(struct lsblk_device
*dev
,
1000 struct lsblk_device
*parent
,
1001 struct lsblk_device
*wholedisk
,
1006 DBG(DEV
, ul_debugobj(dev
, "setting context for %s [parent=%p, wholedisk=%p]",
1007 name
, parent
, wholedisk
));
1009 dev
->parent
= parent
;
1010 dev
->name
= xstrdup(name
);
1011 dev
->partition
= wholedisk
!= NULL
;
1013 dev
->filename
= get_device_path(dev
);
1014 if (!dev
->filename
) {
1015 DBG(DEV
, ul_debugobj(dev
, "%s: failed to get device path", dev
->name
));
1018 DBG(DEV
, ul_debugobj(dev
, "%s: filename=%s", dev
->name
, dev
->filename
));
1020 devno
= __sysfs_devname_to_devno(lsblk
->sysroot
, dev
->name
, wholedisk
? wholedisk
->name
: NULL
);
1023 DBG(DEV
, ul_debugobj(dev
, "%s: unknown device name", dev
->name
));
1027 if (lsblk
->inverse
) {
1028 dev
->sysfs
= ul_new_sysfs_path(devno
, wholedisk
? wholedisk
->sysfs
: NULL
, lsblk
->sysroot
);
1030 DBG(DEV
, ul_debugobj(dev
, "%s: failed to initialize sysfs handler", dev
->name
));
1034 sysfs_blkdev_set_parent(parent
->sysfs
, dev
->sysfs
);
1036 dev
->sysfs
= ul_new_sysfs_path(devno
, parent
? parent
->sysfs
: NULL
, lsblk
->sysroot
);
1038 DBG(DEV
, ul_debugobj(dev
, "%s: failed to initialize sysfs handler", dev
->name
));
1043 dev
->maj
= major(devno
);
1044 dev
->min
= minor(devno
);
1047 if (ul_path_read_u64(dev
->sysfs
, &dev
->size
, "size") == 0) /* in sectors */
1048 dev
->size
<<= 9; /* in bytes */
1050 if (ul_path_read_s32(dev
->sysfs
, &dev
->discard
,
1051 "queue/discard_granularity") != 0)
1054 /* Ignore devices of zero size */
1055 if (!lsblk
->all_devices
&& dev
->size
== 0) {
1056 DBG(DEV
, ul_debugobj(dev
, "zero size device -- ignore"));
1059 if (is_dm(dev
->name
)) {
1060 ul_path_read_string(dev
->sysfs
, &dev
->dm_name
, "dm/name");
1061 if (!dev
->dm_name
) {
1062 DBG(DEV
, ul_debugobj(dev
, "%s: failed to get dm name", dev
->name
));
1067 dev
->npartitions
= sysfs_blkdev_count_partitions(dev
->sysfs
, dev
->name
);
1068 dev
->nholders
= ul_path_count_dirents(dev
->sysfs
, "holders");
1069 dev
->nslaves
= ul_path_count_dirents(dev
->sysfs
, "slaves");
1071 DBG(DEV
, ul_debugobj(dev
, "%s: npartitions=%d, nholders=%d, nslaves=%d",
1072 dev
->name
, dev
->npartitions
, dev
->nholders
, dev
->nslaves
));
1074 /* ignore non-SCSI devices */
1075 if (lsblk
->scsi
&& sysfs_blkdev_scsi_get_hctl(dev
->sysfs
, NULL
, NULL
, NULL
, NULL
)) {
1076 DBG(DEV
, ul_debugobj(dev
, "non-scsi device -- ignore"));
1080 DBG(DEV
, ul_debugobj(dev
, "%s: context successfully initialized", dev
->name
));
1084 struct lsblk_device
*devtree_get_device_or_new(struct lsblk_devtree
*tr
,
1085 struct lsblk_device
*parent
,
1086 struct lsblk_device
*disk
,
1089 struct lsblk_device
*dev
= lsblk_devtree_get_device(tr
, name
);
1092 dev
= lsblk_new_device(tr
);
1094 err(EXIT_FAILURE
, _("failed to allocate device"));
1096 if (set_device(dev
, parent
, disk
, name
) != 0) {
1097 lsblk_unref_device(dev
);
1100 lsblk_devtree_add_device(tr
, dev
);
1101 lsblk_unref_device(dev
); /* keep it referenced by devtree only */
1103 DBG(DEV
, ul_debugobj(dev
, "%s: already processed", name
));
1108 static int process_dependencies(struct lsblk_device
*dev
, struct lsblk_device
*parent
,
1109 int do_partitions
, const char *part_name
);
1112 * List device partitions if any.
1114 static int list_partitions(struct lsblk_device
*wholedisk_dev
, struct lsblk_device
*parent_dev
,
1115 const char *part_name
)
1119 struct lsblk_device part_dev
= { .parent
= NULL
};
1122 assert(wholedisk_dev
);
1125 * Do not process further if there are no partitions for
1126 * this device or the device itself is a partition.
1128 if (!wholedisk_dev
->npartitions
|| wholedisk_dev
->partition
)
1131 DBG(DEV
, ul_debugobj(wholedisk_dev
, "probe whole-disk for partitions"));
1133 dir
= ul_path_opendir(wholedisk_dev
->sysfs
, NULL
);
1135 err(EXIT_FAILURE
, _("failed to open device directory in sysfs"));
1137 while ((d
= xreaddir(dir
))) {
1138 /* Process particular partition only? */
1139 if (part_name
&& strcmp(part_name
, d
->d_name
))
1142 if (!(sysfs_blkdev_is_partition_dirent(dir
, d
, wholedisk_dev
->name
)))
1145 DBG(DEV
, ul_debugobj(wholedisk_dev
, " checking %s", d
->d_name
));
1147 if (lsblk
->inverse
) {
1154 if (set_device(&part_dev
, parent_dev
, wholedisk_dev
, d
->d_name
))
1157 if (!parent_dev
&& part_dev
.nholders
)
1160 wholedisk_dev
->parent
= &part_dev
;
1162 process_dependencies(wholedisk_dev
, &part_dev
, 0, NULL
);
1170 int ps
= set_device(&part_dev
, wholedisk_dev
, wholedisk_dev
, d
->d_name
);
1172 if (ps
== 0 && !lsblk
->nodeps
)
1173 process_dependencies(&part_dev
, wholedisk_dev
, 0, NULL
);
1179 DBG(DEV
, ul_debugobj(wholedisk_dev
, "probe whole-disk for partitions -- done"));
1184 static int get_wholedisk_from_partition_dirent(DIR *dir
,
1185 struct dirent
*d
, struct lsblk_device
*dev
)
1187 char path
[PATH_MAX
];
1191 if ((len
= readlinkat(dirfd(dir
), d
->d_name
, path
, sizeof(path
) - 1)) < 0)
1196 /* The path ends with ".../<device>/<partition>" */
1197 p
= strrchr(path
, '/');
1202 p
= strrchr(path
, '/');
1207 return set_device(dev
, NULL
, NULL
, p
);
1211 * List device dependencies: partitions, holders (inverse = 0) or slaves (inverse = 1).
1213 static int list_deps(struct lsblk_device
*dev
)
1217 struct lsblk_device dep
= { .parent
= NULL
};
1218 const char *depname
;
1225 DBG(DEV
, ul_debugobj(dev
, "%s: list dependencies", dev
->name
));
1227 if (!(lsblk
->inverse
? dev
->nslaves
: dev
->nholders
))
1230 depname
= lsblk
->inverse
? "slaves" : "holders";
1231 dir
= ul_path_opendir(dev
->sysfs
, depname
);
1235 DBG(DEV
, ul_debugobj(dev
, "%s: checking for '%s' dependence", dev
->name
, depname
));
1237 while ((d
= xreaddir(dir
))) {
1238 /* Is the dependency a partition? */
1239 if (sysfs_blkdev_is_partition_dirent(dir
, d
, NULL
)) {
1240 if (!get_wholedisk_from_partition_dirent(dir
, d
, &dep
)) {
1241 DBG(DEV
, ul_debugobj(dev
, "%s: %s: dependence is partition",
1242 dev
->name
, d
->d_name
));
1243 process_dependencies(&dep
, dev
, 1, d
->d_name
);
1246 /* The dependency is a whole device. */
1247 else if (!set_device(&dep
, dev
, NULL
, d
->d_name
)) {
1248 DBG(DEV
, ul_debugobj(dev
, "%s: %s: dependence is whole-disk",
1249 dev
->name
, d
->d_name
));
1250 /* For inverse tree we don't want to show partitions
1251 * if the dependence is on whole-disk */
1252 process_dependencies(&dep
, dev
, lsblk
->inverse
? 0 : 1, NULL
);
1257 DBG(DEV
, ul_debugobj(dev
, "%s: checking for '%s' -- done", dev
->name
, depname
));
1261 static int process_dependencies(struct lsblk_device
*dev
, struct lsblk_device
*parent
,
1262 int do_partitions
, const char *part_name
)
1265 if (do_partitions
&& dev
->npartitions
)
1266 process_partitions(dev
, parent
, part_name
); /* partitions + whole-disk */
1268 return list_deps(dev
);
1273 /* Iterate devices in sysfs */
1274 static int process_all_devices(struct lsblk_devtree
*tr
)
1278 struct path_cxt
*pc
= ul_new_path(_PATH_SYS_BLOCK
);
1281 err(EXIT_FAILURE
, _("failed to allocate /sys handler"));
1283 ul_path_set_prefix(pc
, lsblk
->sysroot
);
1285 /* TODO: reuse @pc in set_device(), etc. */
1286 dir
= ul_path_opendir(pc
, NULL
);
1290 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_BLOCK
));
1292 while ((d
= xreaddir(dir
))) {
1293 struct lsblk_device
*dev
;
1295 DBG(DEV
, ul_debug(" %s dentry", d
->d_name
));
1297 dev
= devtree_get_device_or_new(tr
, NULL
, NULL
, d
->d_name
);
1301 /* remove unwanted devices */
1302 if (is_maj_excluded(dev
->maj
) || !is_maj_included(dev
->maj
)) {
1303 DBG(DEV
, ul_debug(" %s: ignore (by filter)", d
->d_name
));
1304 lsblk_devtree_remove_device(tr
, dev
);
1308 /* process dependencies for top level devices */
1309 if ((lsblk
->inverse
? dev
->nholders
: dev
->nslaves
) <= 0
1310 && process_dependencies(dev
, NULL
, 1, NULL
) == 0)
1311 lsblk_devtree_add_root(tr
, dev
);
1317 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_BLOCK
" -- done"));
1321 static int process_one_device(struct lsblk_devtree
*tr
, char *devname
)
1323 struct lsblk_device
*dev
= NULL
;
1325 char buf
[PATH_MAX
+ 1], *name
= NULL
, *diskname
= NULL
;
1327 int real_part
= 0, rc
= -EINVAL
;
1329 if (stat(devname
, &st
) || !S_ISBLK(st
.st_mode
)) {
1330 warnx(_("%s: not a block device"), devname
);
1334 /* TODO: sysfs_devno_to_devname() internally initializes path_cxt, it
1335 * would be better to use ul_new_sysfs_path() + sysfs_blkdev_get_name()
1336 * and reuse path_cxt for set_device()
1338 name
= sysfs_devno_to_devname(st
.st_rdev
, buf
, sizeof(buf
));
1340 warn(_("%s: failed to get sysfs name"), devname
);
1343 name
= xstrdup(name
);
1345 if (!strncmp(name
, "dm-", 3)) {
1346 /* dm mapping is never a real partition! */
1349 if (blkid_devno_to_wholedisk(st
.st_rdev
, buf
, sizeof(buf
), &disk
)) {
1350 warn(_("%s: failed to get whole-disk device number"), devname
);
1354 real_part
= st
.st_rdev
!= disk
;
1359 * Device is not a partition.
1361 dev
= devtree_get_device_or_new(tr
, NULL
, NULL
, name
);
1365 if (process_dependencies(dev
, NULL
, !lsblk
->inverse
, NULL
) == 0)
1366 lsblk_devtree_add_root(tr
, dev
);
1369 * Partition, read sysfs name of the disk device
1371 struct lsblk_device
*parent
;
1373 parent
= devtree_get_device_or_new(tr
, NULL
, NULL
, diskname
);
1377 dev
= devtree_get_device_or_new(tr
, parent
, parent
, name
);
1381 if (process_dependencies(dev
, NULL
, 1, NULL
) == 0)
1382 lsblk_devtree_add_root(tr
, dev
);
1386 process_dependencies(parent, dev, 1, dev->name);
1388 process_dependencies(dev, parent, 1, NULL);
1398 static void parse_excludes(const char *str0
)
1400 const char *str
= str0
;
1402 while (str
&& *str
) {
1407 n
= strtoul(str
, &end
, 10);
1409 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1410 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1411 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1412 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1413 excludes
[nexcludes
++] = n
;
1415 if (nexcludes
== ARRAY_SIZE(excludes
))
1416 /* TRANSLATORS: The standard value for %d is 256. */
1417 errx(EXIT_FAILURE
, _("the list of excluded devices is "
1418 "too large (limit is %d devices)"),
1419 (int)ARRAY_SIZE(excludes
));
1421 str
= end
&& *end
? end
+ 1 : NULL
;
1425 static void parse_includes(const char *str0
)
1427 const char *str
= str0
;
1429 while (str
&& *str
) {
1434 n
= strtoul(str
, &end
, 10);
1436 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1437 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1438 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1439 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1440 includes
[nincludes
++] = n
;
1442 if (nincludes
== ARRAY_SIZE(includes
))
1443 /* TRANSLATORS: The standard value for %d is 256. */
1444 errx(EXIT_FAILURE
, _("the list of included devices is "
1445 "too large (limit is %d devices)"),
1446 (int)ARRAY_SIZE(includes
));
1447 str
= end
&& *end
? end
+ 1 : NULL
;
1452 * see set_sortdata_u64() and columns initialization in main()
1454 static int cmp_u64_cells(struct libscols_cell
*a
,
1455 struct libscols_cell
*b
,
1456 __attribute__((__unused__
)) void *data
)
1458 uint64_t *adata
= (uint64_t *) scols_cell_get_userdata(a
),
1459 *bdata
= (uint64_t *) scols_cell_get_userdata(b
);
1461 if (adata
== NULL
&& bdata
== NULL
)
1467 return *adata
== *bdata
? 0 : *adata
>= *bdata
? 1 : -1;
1470 static void __attribute__((__noreturn__
)) usage(void)
1475 fputs(USAGE_HEADER
, out
);
1476 fprintf(out
, _(" %s [options] [<device> ...]\n"), program_invocation_short_name
);
1478 fputs(USAGE_SEPARATOR
, out
);
1479 fputs(_("List information about block devices.\n"), out
);
1481 fputs(USAGE_OPTIONS
, out
);
1482 fputs(_(" -a, --all print all devices\n"), out
);
1483 fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out
);
1484 fputs(_(" -d, --nodeps don't print slaves or holders\n"), out
);
1485 fputs(_(" -D, --discard print discard capabilities\n"), out
);
1486 fputs(_(" -z, --zoned print zone model\n"), out
);
1487 fputs(_(" -e, --exclude <list> exclude devices by major number (default: RAM disks)\n"), out
);
1488 fputs(_(" -f, --fs output info about filesystems\n"), out
);
1489 fputs(_(" -i, --ascii use ascii characters only\n"), out
);
1490 fputs(_(" -I, --include <list> show only devices with specified major numbers\n"), out
);
1491 fputs(_(" -J, --json use JSON output format\n"), out
);
1492 fputs(_(" -l, --list use list format output\n"), out
);
1493 fputs(_(" -T, --tree use tree format output\n"), out
);
1494 fputs(_(" -m, --perms output info about permissions\n"), out
);
1495 fputs(_(" -n, --noheadings don't print headings\n"), out
);
1496 fputs(_(" -o, --output <list> output columns\n"), out
);
1497 fputs(_(" -O, --output-all output all columns\n"), out
);
1498 fputs(_(" -p, --paths print complete device path\n"), out
);
1499 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out
);
1500 fputs(_(" -r, --raw use raw output format\n"), out
);
1501 fputs(_(" -s, --inverse inverse dependencies\n"), out
);
1502 fputs(_(" -S, --scsi output info about SCSI devices\n"), out
);
1503 fputs(_(" -t, --topology output info about topology\n"), out
);
1504 fputs(_(" -x, --sort <column> sort output by <column>\n"), out
);
1505 fputs(_(" --sysroot <dir> use specified directory as system root\n"), out
);
1506 fputs(USAGE_SEPARATOR
, out
);
1507 printf(USAGE_HELP_OPTIONS(22));
1509 fprintf(out
, USAGE_COLUMNS
);
1511 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
1512 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
1514 printf(USAGE_MAN_TAIL("lsblk(8)"));
1519 static void check_sysdevblock(void)
1521 if (access(_PATH_SYS_DEVBLOCK
, R_OK
) != 0)
1522 err(EXIT_FAILURE
, _("failed to access sysfs directory: %s"),
1523 _PATH_SYS_DEVBLOCK
);
1526 int main(int argc
, char *argv
[])
1528 struct lsblk _ls
= { .sort_id
= -1, .flags
= LSBLK_TREE
};
1529 struct lsblk_devtree
*tr
= NULL
;
1530 int c
, status
= EXIT_FAILURE
;
1531 char *outarg
= NULL
;
1536 OPT_SYSROOT
= CHAR_MAX
+ 1
1539 static const struct option longopts
[] = {
1540 { "all", no_argument
, NULL
, 'a' },
1541 { "bytes", no_argument
, NULL
, 'b' },
1542 { "nodeps", no_argument
, NULL
, 'd' },
1543 { "discard", no_argument
, NULL
, 'D' },
1544 { "zoned", no_argument
, NULL
, 'z' },
1545 { "help", no_argument
, NULL
, 'h' },
1546 { "json", no_argument
, NULL
, 'J' },
1547 { "output", required_argument
, NULL
, 'o' },
1548 { "output-all", no_argument
, NULL
, 'O' },
1549 { "perms", no_argument
, NULL
, 'm' },
1550 { "noheadings", no_argument
, NULL
, 'n' },
1551 { "list", no_argument
, NULL
, 'l' },
1552 { "ascii", no_argument
, NULL
, 'i' },
1553 { "raw", no_argument
, NULL
, 'r' },
1554 { "inverse", no_argument
, NULL
, 's' },
1555 { "fs", no_argument
, NULL
, 'f' },
1556 { "exclude", required_argument
, NULL
, 'e' },
1557 { "include", required_argument
, NULL
, 'I' },
1558 { "topology", no_argument
, NULL
, 't' },
1559 { "paths", no_argument
, NULL
, 'p' },
1560 { "pairs", no_argument
, NULL
, 'P' },
1561 { "scsi", no_argument
, NULL
, 'S' },
1562 { "sort", required_argument
, NULL
, 'x' },
1563 { "sysroot", required_argument
, NULL
, OPT_SYSROOT
},
1564 { "tree", no_argument
, NULL
, 'T' },
1565 { "version", no_argument
, NULL
, 'V' },
1566 { NULL
, 0, NULL
, 0 },
1569 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
1577 { 'P','T', 'l','r' },
1580 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1582 setlocale(LC_ALL
, "");
1583 bindtextdomain(PACKAGE
, LOCALEDIR
);
1584 textdomain(PACKAGE
);
1585 atexit(close_stdout
);
1591 while((c
= getopt_long(argc
, argv
,
1592 "abdDze:fhJlnmo:OpPiI:rstVSTx:", longopts
, NULL
)) != -1) {
1594 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1598 lsblk
->all_devices
= 1;
1607 add_uniq_column(COL_NAME
);
1608 add_uniq_column(COL_DALIGN
);
1609 add_uniq_column(COL_DGRAN
);
1610 add_uniq_column(COL_DMAX
);
1611 add_uniq_column(COL_DZERO
);
1614 add_uniq_column(COL_NAME
);
1615 add_uniq_column(COL_ZONED
);
1618 parse_excludes(optarg
);
1624 lsblk
->flags
|= LSBLK_JSON
;
1627 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
1630 lsblk
->flags
|= LSBLK_NOHEADINGS
;
1636 for (ncolumns
= 0 ; ncolumns
< ARRAY_SIZE(infos
); ncolumns
++)
1637 columns
[ncolumns
] = ncolumns
;
1643 lsblk
->flags
|= LSBLK_EXPORT
;
1644 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
1647 lsblk
->flags
|= LSBLK_ASCII
;
1650 parse_includes(optarg
);
1653 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
1654 lsblk
->flags
|= LSBLK_RAW
; /* enable raw */
1660 add_uniq_column(COL_NAME
);
1661 add_uniq_column(COL_FSTYPE
);
1662 add_uniq_column(COL_LABEL
);
1663 add_uniq_column(COL_UUID
);
1664 add_uniq_column(COL_FSAVAIL
);
1665 add_uniq_column(COL_FSUSEPERC
);
1666 add_uniq_column(COL_TARGET
);
1669 add_uniq_column(COL_NAME
);
1670 add_uniq_column(COL_SIZE
);
1671 add_uniq_column(COL_OWNER
);
1672 add_uniq_column(COL_GROUP
);
1673 add_uniq_column(COL_MODE
);
1676 add_uniq_column(COL_NAME
);
1677 add_uniq_column(COL_ALIOFF
);
1678 add_uniq_column(COL_MINIO
);
1679 add_uniq_column(COL_OPTIO
);
1680 add_uniq_column(COL_PHYSEC
);
1681 add_uniq_column(COL_LOGSEC
);
1682 add_uniq_column(COL_ROTA
);
1683 add_uniq_column(COL_SCHED
);
1684 add_uniq_column(COL_RQ_SIZE
);
1685 add_uniq_column(COL_RA
);
1686 add_uniq_column(COL_WSAME
);
1691 add_uniq_column(COL_NAME
);
1692 add_uniq_column(COL_HCTL
);
1693 add_uniq_column(COL_TYPE
);
1694 add_uniq_column(COL_VENDOR
);
1695 add_uniq_column(COL_MODEL
);
1696 add_uniq_column(COL_REV
);
1697 add_uniq_column(COL_TRANSPORT
);
1704 lsblk
->sysroot
= optarg
;
1707 printf(UTIL_LINUX_VERSION
);
1708 return EXIT_SUCCESS
;
1710 lsblk
->flags
&= ~LSBLK_TREE
; /* disable the default */
1711 lsblk
->sort_id
= column_name_to_id(optarg
, strlen(optarg
));
1712 if (lsblk
->sort_id
>= 0)
1716 errtryhelp(EXIT_FAILURE
);
1721 lsblk
->flags
|= LSBLK_TREE
;
1723 check_sysdevblock();
1726 add_column(COL_NAME
);
1727 add_column(COL_MAJMIN
);
1729 add_column(COL_SIZE
);
1731 add_column(COL_TYPE
);
1732 add_column(COL_TARGET
);
1735 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
1736 &ncolumns
, column_name_to_id
) < 0)
1737 return EXIT_FAILURE
;
1739 if (lsblk
->all_devices
== 0 && nexcludes
== 0 && nincludes
== 0)
1740 excludes
[nexcludes
++] = 1; /* default: ignore RAM disks */
1742 if (lsblk
->sort_id
< 0)
1743 /* Since Linux 4.8 we have sort devices by default, because
1744 * /sys is no more sorted */
1745 lsblk
->sort_id
= COL_MAJMIN
;
1747 /* For --inverse --list we still follow parent->child relation */
1748 if (lsblk
->inverse
&& !(lsblk
->flags
& LSBLK_TREE
))
1749 lsblk
->force_tree_order
= 1;
1751 if (lsblk
->sort_id
>= 0 && column_id_to_number(lsblk
->sort_id
) < 0) {
1752 /* the sort column is not between output columns -- add as hidden */
1753 add_column(lsblk
->sort_id
);
1754 lsblk
->sort_hidden
= 1;
1758 scols_init_debug(0);
1759 ul_path_init_debug();
1762 * initialize output columns
1764 if (!(lsblk
->table
= scols_new_table()))
1765 errx(EXIT_FAILURE
, _("failed to allocate output table"));
1766 scols_table_enable_raw(lsblk
->table
, !!(lsblk
->flags
& LSBLK_RAW
));
1767 scols_table_enable_export(lsblk
->table
, !!(lsblk
->flags
& LSBLK_EXPORT
));
1768 scols_table_enable_ascii(lsblk
->table
, !!(lsblk
->flags
& LSBLK_ASCII
));
1769 scols_table_enable_json(lsblk
->table
, !!(lsblk
->flags
& LSBLK_JSON
));
1770 scols_table_enable_noheadings(lsblk
->table
, !!(lsblk
->flags
& LSBLK_NOHEADINGS
));
1772 if (lsblk
->flags
& LSBLK_JSON
)
1773 scols_table_set_name(lsblk
->table
, "blockdevices");
1775 for (i
= 0; i
< ncolumns
; i
++) {
1776 struct colinfo
*ci
= get_column_info(i
);
1777 struct libscols_column
*cl
;
1778 int id
= get_column_id(i
), fl
= ci
->flags
;
1780 if (!(lsblk
->flags
& LSBLK_TREE
) && id
== COL_NAME
)
1781 fl
&= ~SCOLS_FL_TREE
;
1782 if (lsblk
->sort_hidden
&& lsblk
->sort_id
== id
)
1783 fl
|= SCOLS_FL_HIDDEN
;
1785 cl
= scols_table_new_column(lsblk
->table
, ci
->name
, ci
->whint
, fl
);
1787 warn(_("failed to allocate output column"));
1790 if (!lsblk
->sort_col
&& lsblk
->sort_id
== id
) {
1791 lsblk
->sort_col
= cl
;
1792 scols_column_set_cmpfunc(cl
,
1793 ci
->type
== COLTYPE_NUM
? cmp_u64_cells
:
1794 ci
->type
== COLTYPE_SIZE
? cmp_u64_cells
:
1795 ci
->type
== COLTYPE_SORTNUM
? cmp_u64_cells
: scols_cmpstr_cells
,
1798 if (lsblk
->flags
& LSBLK_JSON
) {
1805 scols_column_set_json_type(cl
, SCOLS_JSON_NUMBER
);
1808 scols_column_set_json_type(cl
, SCOLS_JSON_BOOLEAN
);
1811 scols_column_set_json_type(cl
, SCOLS_JSON_STRING
);
1817 tr
= lsblk_new_devtree();
1819 err(EXIT_FAILURE
, _("failed to allocate device tree"));
1822 status
= process_all_devices(tr
) == 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
1824 int cnt
= 0, cnt_err
= 0;
1826 while (optind
< argc
) {
1827 if (process_one_device(tr
, argv
[optind
++]) != 0)
1831 status
= cnt
== 0 ? EXIT_FAILURE
: /* nothing */
1832 cnt
== cnt_err
? LSBLK_EXIT_ALLFAILED
:/* all failed */
1833 cnt_err
? LSBLK_EXIT_SOMEOK
: /* some ok */
1834 EXIT_SUCCESS
; /* all success */
1837 devtree_to_scols(tr
, lsblk
->table
);
1839 if (lsblk
->sort_col
)
1840 scols_sort_table(lsblk
->table
, lsblk
->sort_col
);
1841 if (lsblk
->force_tree_order
)
1842 scols_sort_table_by_tree(lsblk
->table
);
1844 scols_print_table(lsblk
->table
);
1847 if (lsblk
->sort_col
)
1848 unref_sortdata(lsblk
->table
);
1850 scols_unref_table(lsblk
->table
);
1853 lsblk_properties_deinit();
1854 lsblk_unref_devtree(tr
);