2 * lsblk(8) - list block devices
4 * Copyright (C) 2010,2011,2012 Red Hat, Inc. All rights reserved.
5 * Written by Milan Broz <mbroz@redhat.com>
6 * Karel Zak <kzak@redhat.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it would be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <sys/types.h>
34 #include <sys/ioctl.h>
44 #include <libsmartcols.h>
53 #include "pathnames.h"
55 #include "canonicalize.h"
61 #include "closestream.h"
67 UL_DEBUG_DEFINE_MASK(lsblk
);
68 UL_DEBUG_DEFINE_MASKNAMES(lsblk
) = UL_DEBUG_EMPTY_MASKNAMES
;
70 #define LSBLK_DEBUG_INIT (1 << 1)
71 #define LSBLK_DEBUG_FILTER (1 << 2)
72 #define LSBLK_DEBUG_DEV (1 << 3)
73 #define LSBLK_DEBUG_CXT (1 << 4)
74 #define LSBLK_DEBUG_ALL 0xFFFF
76 #define DBG(m, x) __UL_DBG(lsblk, LSBLK_DEBUG_, m, x)
77 #define ON_DBG(m, x) __UL_DBG_CALL(lsblk, LSBLK_DEBUG_, m, x)
80 #define LSBLK_EXIT_SOMEOK 64
81 #define LSBLK_EXIT_ALLFAILED 32
131 /* basic table settings */
133 LSBLK_ASCII
= (1 << 0),
134 LSBLK_RAW
= (1 << 1),
135 LSBLK_NOHEADINGS
= (1 << 2),
136 LSBLK_EXPORT
= (1 << 3),
137 LSBLK_TREE
= (1 << 4),
141 SORT_STRING
= 0, /* default is to use scols_cell_get_data() */
142 SORT_U64
= 1 /* use private pointer from scols_cell_get_userdata() */
147 const char *name
; /* header */
148 double whint
; /* width hint (N < 1 is in percent of termwidth) */
149 int flags
; /* SCOLS_FL_* */
152 int sort_type
; /* SORT_* */
155 /* columns descriptions */
156 static struct colinfo infos
[] = {
157 [COL_NAME
] = { "NAME", 0.25, SCOLS_FL_TREE
| SCOLS_FL_NOEXTREMES
, N_("device name") },
158 [COL_KNAME
] = { "KNAME", 0.3, 0, N_("internal kernel device name") },
159 [COL_PKNAME
] = { "PKNAME", 0.3, 0, N_("internal parent kernel device name") },
160 [COL_MAJMIN
] = { "MAJ:MIN", 6, 0, N_("major:minor device number"), SORT_U64
},
161 [COL_FSTYPE
] = { "FSTYPE", 0.1, SCOLS_FL_TRUNC
, N_("filesystem type") },
162 [COL_TARGET
] = { "MOUNTPOINT", 0.10, SCOLS_FL_TRUNC
, N_("where the device is mounted") },
163 [COL_LABEL
] = { "LABEL", 0.1, 0, N_("filesystem LABEL") },
164 [COL_UUID
] = { "UUID", 36, 0, N_("filesystem UUID") },
166 [COL_PARTTYPE
] = { "PARTTYPE", 36, 0, N_("partition type UUID") },
167 [COL_PARTLABEL
] = { "PARTLABEL", 0.1, 0, N_("partition LABEL") },
168 [COL_PARTUUID
] = { "PARTUUID", 36, 0, N_("partition UUID") },
169 [COL_PARTFLAGS
] = { "PARTFLAGS", 36, 0, N_("partition flags") },
171 [COL_RA
] = { "RA", 3, SCOLS_FL_RIGHT
, N_("read-ahead of the device"), SORT_U64
},
172 [COL_RO
] = { "RO", 1, SCOLS_FL_RIGHT
, N_("read-only device") },
173 [COL_RM
] = { "RM", 1, SCOLS_FL_RIGHT
, N_("removable device") },
174 [COL_HOTPLUG
]= { "HOTPLUG", 1, SCOLS_FL_RIGHT
, N_("removable or hotplug device (usb, pcmcia, ...)") },
175 [COL_ROTA
] = { "ROTA", 1, SCOLS_FL_RIGHT
, N_("rotational device") },
176 [COL_RAND
] = { "RAND", 1, SCOLS_FL_RIGHT
, N_("adds randomness") },
177 [COL_MODEL
] = { "MODEL", 0.1, SCOLS_FL_TRUNC
, N_("device identifier") },
178 [COL_SERIAL
] = { "SERIAL", 0.1, SCOLS_FL_TRUNC
, N_("disk serial number") },
179 [COL_SIZE
] = { "SIZE", 5, SCOLS_FL_RIGHT
, N_("size of the device"), SORT_U64
},
180 [COL_STATE
] = { "STATE", 7, SCOLS_FL_TRUNC
, N_("state of the device") },
181 [COL_OWNER
] = { "OWNER", 0.1, SCOLS_FL_TRUNC
, N_("user name"), },
182 [COL_GROUP
] = { "GROUP", 0.1, SCOLS_FL_TRUNC
, N_("group name") },
183 [COL_MODE
] = { "MODE", 10, 0, N_("device node permissions") },
184 [COL_ALIOFF
] = { "ALIGNMENT", 6, SCOLS_FL_RIGHT
, N_("alignment offset"), SORT_U64
},
185 [COL_MINIO
] = { "MIN-IO", 6, SCOLS_FL_RIGHT
, N_("minimum I/O size"), SORT_U64
},
186 [COL_OPTIO
] = { "OPT-IO", 6, SCOLS_FL_RIGHT
, N_("optimal I/O size"), SORT_U64
},
187 [COL_PHYSEC
] = { "PHY-SEC", 7, SCOLS_FL_RIGHT
, N_("physical sector size"), SORT_U64
},
188 [COL_LOGSEC
] = { "LOG-SEC", 7, SCOLS_FL_RIGHT
, N_("logical sector size"), SORT_U64
},
189 [COL_SCHED
] = { "SCHED", 0.1, 0, N_("I/O scheduler name") },
190 [COL_RQ_SIZE
]= { "RQ-SIZE", 5, SCOLS_FL_RIGHT
, N_("request queue size"), SORT_U64
},
191 [COL_TYPE
] = { "TYPE", 4, 0, N_("device type") },
192 [COL_DALIGN
] = { "DISC-ALN", 6, SCOLS_FL_RIGHT
, N_("discard alignment offset"), SORT_U64
},
193 [COL_DGRAN
] = { "DISC-GRAN", 6, SCOLS_FL_RIGHT
, N_("discard granularity"), SORT_U64
},
194 [COL_DMAX
] = { "DISC-MAX", 6, SCOLS_FL_RIGHT
, N_("discard max bytes"), SORT_U64
},
195 [COL_DZERO
] = { "DISC-ZERO", 1, SCOLS_FL_RIGHT
, N_("discard zeroes data") },
196 [COL_WSAME
] = { "WSAME", 6, SCOLS_FL_RIGHT
, N_("write same max bytes"), SORT_U64
},
197 [COL_WWN
] = { "WWN", 18, 0, N_("unique storage identifier") },
198 [COL_HCTL
] = { "HCTL", 10, 0, N_("Host:Channel:Target:Lun for SCSI") },
199 [COL_TRANSPORT
] = { "TRAN", 6, 0, N_("device transport type") },
200 [COL_SUBSYS
] = { "SUBSYSTEMS", 0.1, SCOLS_FL_NOEXTREMES
, N_("de-duplicated chain of subsystems") },
201 [COL_REV
] = { "REV", 4, SCOLS_FL_RIGHT
, N_("device revision") },
202 [COL_VENDOR
] = { "VENDOR", 0.1, SCOLS_FL_TRUNC
, N_("device vendor") },
206 struct libscols_table
*table
; /* output table */
207 struct libscols_column
*sort_col
;/* sort output by this colum */
210 unsigned int all_devices
:1; /* print all devices, including empty */
211 unsigned int bytes
:1; /* print SIZE in bytes */
212 unsigned int inverse
:1; /* print inverse dependencies */
213 unsigned int nodeps
:1; /* don't print slaves/holders */
214 unsigned int scsi
:1; /* print only device with HCTL (SCSI) */
215 unsigned int paths
:1; /* print devnames with "/dev" prefix */
218 struct lsblk
*lsblk
; /* global handler */
220 /* columns[] array specifies all currently wanted output column. The columns
221 * are defined by infos[] array and you can specify (on command line) each
222 * column twice. That's enough, dynamically allocated array of the columns is
223 * unnecessary overkill and over-engineering in this case */
224 static int columns
[ARRAY_SIZE(infos
) * 2];
227 static inline size_t err_columns_index(size_t arysz
, size_t idx
)
230 errx(EXIT_FAILURE
, _("too many columns specified, "
231 "the limit is %zu columns"),
236 #define add_column(ary, n, id) \
237 ((ary)[ err_columns_index(ARRAY_SIZE(ary), (n)) ] = (id))
239 static int excludes
[256];
240 static size_t nexcludes
;
242 static int includes
[256];
243 static size_t nincludes
;
245 static struct libmnt_table
*mtab
, *swaps
;
246 static struct libmnt_cache
*mntcache
;
253 struct blkdev_cxt
*parent
;
255 struct libscols_line
*scols_line
;
258 char *name
; /* kernel name in /sys/block */
259 char *dm_name
; /* DM name (dm/block) */
261 char *filename
; /* path to device node */
263 struct sysfs_cxt sysfs
;
265 int partition
; /* is partition? TRUE/FALSE */
267 int probed
; /* already probed */
268 char *fstype
; /* detected fs, NULL or "?" if cannot detect */
269 char *uuid
; /* filesystem UUID (or stack uuid) */
270 char *label
; /* filesystem label */
271 char *parttype
; /* partiton type UUID */
272 char *partuuid
; /* partition UUID */
273 char *partlabel
; /* partiton label */
274 char *partflags
; /* partition flags */
275 char *wwn
; /* storage WWN */
276 char *serial
; /* disk serial number */
278 int npartitions
; /* # of partitions this device has */
279 int nholders
; /* # of devices mapped directly to this device
280 * /sys/block/.../holders */
281 int nslaves
; /* # of devices this device maps to */
282 int maj
, min
; /* devno */
283 int discard
; /* supports discard */
285 uint64_t size
; /* device size */
288 static void lsblk_init_debug(void)
290 __UL_INIT_DEBUG(lsblk
, LSBLK_DEBUG_
, 0, LSBLK_DEBUG
);
293 static int is_maj_excluded(int maj
)
297 assert(ARRAY_SIZE(excludes
) > nexcludes
);
300 return 0; /* filter not enabled, device not exluded */
302 for (i
= 0; i
< nexcludes
; i
++) {
303 if (excludes
[i
] == maj
) {
304 DBG(FILTER
, ul_debug("exclude: maj=%d", maj
));
311 static int is_maj_included(int maj
)
315 assert(ARRAY_SIZE(includes
) > nincludes
);
318 return 1; /* filter not enabled, device is included */
320 for (i
= 0; i
< nincludes
; i
++) {
321 if (includes
[i
] == maj
) {
322 DBG(FILTER
, ul_debug("include: maj=%d", maj
));
329 /* array with IDs of enabled columns */
330 static int get_column_id(int num
)
332 assert(num
< ncolumns
);
333 assert(columns
[num
] < (int) ARRAY_SIZE(infos
));
337 static struct colinfo
*get_column_info(int num
)
339 return &infos
[ get_column_id(num
) ];
342 static int column_name_to_id(const char *name
, size_t namesz
)
346 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
347 const char *cn
= infos
[i
].name
;
349 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
352 warnx(_("unknown column: %s"), name
);
356 static int column_id_to_number(int id
)
360 for (i
= 0; i
< (size_t) ncolumns
; i
++)
361 if (columns
[i
] == id
)
366 static void reset_blkdev_cxt(struct blkdev_cxt
*cxt
)
371 DBG(CXT
, ul_debugobj(cxt
, "reset"));
381 free(cxt
->partlabel
);
385 sysfs_deinit(&cxt
->sysfs
);
387 memset(cxt
, 0, sizeof(*cxt
));
390 static int is_dm(const char *name
)
392 return strncmp(name
, "dm-", 3) ? 0 : 1;
395 static struct dirent
*xreaddir(DIR *dp
)
401 while ((d
= readdir(dp
))) {
402 if (!strcmp(d
->d_name
, ".") ||
403 !strcmp(d
->d_name
, ".."))
406 /* blacklist here? */
412 static char *get_device_path(struct blkdev_cxt
*cxt
)
419 if (is_dm(cxt
->name
))
420 return canonicalize_dm_name(cxt
->name
);
422 snprintf(path
, sizeof(path
), "/dev/%s", cxt
->name
);
423 return xstrdup(path
);
426 static int is_active_swap(const char *filename
)
429 swaps
= mnt_new_table();
433 mntcache
= mnt_new_cache();
435 mnt_table_set_cache(swaps
, mntcache
);
436 mnt_table_parse_swaps(swaps
, NULL
);
439 return mnt_table_find_srcpath(swaps
, filename
, MNT_ITER_BACKWARD
) != 0;
442 static char *get_device_mountpoint(struct blkdev_cxt
*cxt
)
444 struct libmnt_fs
*fs
;
448 assert(cxt
->filename
);
451 mtab
= mnt_new_table();
455 mntcache
= mnt_new_cache();
457 mnt_table_set_cache(mtab
, mntcache
);
458 mnt_table_parse_mtab(mtab
, NULL
);
461 /* Note that maj:min in /proc/self/mouninfo does not have to match with
462 * devno as returned by stat(), so we have to try devname too
464 fs
= mnt_table_find_devno(mtab
, makedev(cxt
->maj
, cxt
->min
), MNT_ITER_BACKWARD
);
466 fs
= mnt_table_find_srcpath(mtab
, cxt
->filename
, MNT_ITER_BACKWARD
);
468 return is_active_swap(cxt
->filename
) ? xstrdup("[SWAP]") : NULL
;
471 fsroot
= mnt_fs_get_root(fs
);
472 if (fsroot
&& strcmp(fsroot
, "/") != 0) {
473 /* hmm.. we found bind mount or btrfs subvolume, let's try to
474 * get real FS root mountpoint */
475 struct libmnt_fs
*rfs
;
476 struct libmnt_iter
*itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
478 mnt_table_set_iter(mtab
, itr
, fs
);
479 while (mnt_table_next_fs(mtab
, itr
, &rfs
) == 0) {
480 fsroot
= mnt_fs_get_root(rfs
);
481 if ((!fsroot
|| strcmp(fsroot
, "/") == 0)
482 && mnt_fs_match_source(rfs
, cxt
->filename
, mntcache
)) {
490 DBG(DEV
, ul_debugobj(cxt
, "mountpoint: %s", mnt_fs_get_target(fs
)));
491 return xstrdup(mnt_fs_get_target(fs
));
495 static int get_udev_properties(struct blkdev_cxt
*cxt
496 __attribute__((__unused__
)))
501 static int get_udev_properties(struct blkdev_cxt
*cxt
)
503 struct udev_device
*dev
;
506 return 0; /* already done */
513 dev
= udev_device_new_from_subsystem_sysname(udev
, "block", cxt
->name
);
517 if ((data
= udev_device_get_property_value(dev
, "ID_FS_LABEL_ENC"))) {
518 cxt
->label
= xstrdup(data
);
519 unhexmangle_string(cxt
->label
);
521 if ((data
= udev_device_get_property_value(dev
, "ID_FS_UUID_ENC"))) {
522 cxt
->uuid
= xstrdup(data
);
523 unhexmangle_string(cxt
->uuid
);
525 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_NAME"))) {
526 cxt
->partlabel
= xstrdup(data
);
527 unhexmangle_string(cxt
->partlabel
);
529 if ((data
= udev_device_get_property_value(dev
, "ID_FS_TYPE")))
530 cxt
->fstype
= xstrdup(data
);
531 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_TYPE")))
532 cxt
->parttype
= xstrdup(data
);
533 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_UUID")))
534 cxt
->partuuid
= xstrdup(data
);
535 if ((data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_FLAGS")))
536 cxt
->partflags
= xstrdup(data
);
537 if ((data
= udev_device_get_property_value(dev
, "ID_WWN")))
538 cxt
->wwn
= xstrdup(data
);
539 if ((data
= udev_device_get_property_value(dev
, "ID_SERIAL_SHORT")))
540 cxt
->serial
= xstrdup(data
);
541 udev_device_unref(dev
);
543 DBG(DEV
, ul_debugobj(cxt
, "%s: found udev properties", cxt
->name
));
546 return cxt
->probed
== 1 ? 0 : -1;
549 #endif /* HAVE_LIBUDEV */
551 static void probe_device(struct blkdev_cxt
*cxt
)
553 blkid_probe pr
= NULL
;
562 if (get_udev_properties(cxt
) == 0)
563 return; /* success */
567 /* try libblkid (fallback) */
569 return; /* no permissions to read from the device */
571 pr
= blkid_new_probe_from_filename(cxt
->filename
);
575 blkid_probe_enable_superblocks(pr
, 1);
576 blkid_probe_set_superblocks_flags(pr
, BLKID_SUBLKS_LABEL
|
579 blkid_probe_enable_partitions(pr
, 1);
580 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
582 if (!blkid_do_safeprobe(pr
)) {
583 const char *data
= NULL
;
585 if (!blkid_probe_lookup_value(pr
, "TYPE", &data
, NULL
))
586 cxt
->fstype
= xstrdup(data
);
587 if (!blkid_probe_lookup_value(pr
, "UUID", &data
, NULL
))
588 cxt
->uuid
= xstrdup(data
);
589 if (!blkid_probe_lookup_value(pr
, "LABEL", &data
, NULL
))
590 cxt
->label
= xstrdup(data
);
591 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_TYPE", &data
, NULL
))
592 cxt
->parttype
= xstrdup(data
);
593 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_UUID", &data
, NULL
))
594 cxt
->partuuid
= xstrdup(data
);
595 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_NAME", &data
, NULL
))
596 cxt
->partlabel
= xstrdup(data
);
597 if (!blkid_probe_lookup_value(pr
, "PART_ENTRY_FLAGS", &data
, NULL
))
598 cxt
->partflags
= xstrdup(data
);
599 DBG(DEV
, ul_debugobj(cxt
, "%s: found blkid properties", cxt
->name
));
602 blkid_free_probe(pr
);
606 static int is_readonly_device(struct blkdev_cxt
*cxt
)
610 if (sysfs_scanf(&cxt
->sysfs
, "ro", "%d", &ro
) == 1)
613 /* fallback if "ro" attribute does not exist */
614 fd
= open(cxt
->filename
, O_RDONLY
);
616 if (ioctl(fd
, BLKROGET
, &ro
) != 0)
623 static char *get_scheduler(struct blkdev_cxt
*cxt
)
625 char *str
= sysfs_strdup(&cxt
->sysfs
, "queue/scheduler");
626 char *p
, *res
= NULL
;
630 p
= strchr(str
, '[');
633 p
= strchr(res
, ']');
644 static char *get_type(struct blkdev_cxt
*cxt
)
646 char *res
= NULL
, *p
;
648 if (is_dm(cxt
->name
)) {
649 char *dm_uuid
= sysfs_strdup(&cxt
->sysfs
, "dm/uuid");
651 /* The DM_UUID prefix should be set to subsystem owning
652 * the device - LVM, CRYPT, DMRAID, MPATH, PART */
655 char *dm_uuid_prefix
= strsep(&tmp
, "-");
657 if (dm_uuid_prefix
) {
658 /* kpartx hack to remove partition number */
659 if (strncasecmp(dm_uuid_prefix
, "part", 4) == 0)
660 dm_uuid_prefix
[4] = '\0';
662 res
= xstrdup(dm_uuid_prefix
);
668 /* No UUID or no prefix - just mark it as DM device */
671 } else if (!strncmp(cxt
->name
, "loop", 4)) {
672 res
= xstrdup("loop");
674 } else if (!strncmp(cxt
->name
, "md", 2)) {
675 char *md_level
= sysfs_strdup(&cxt
->sysfs
, "md/level");
676 res
= md_level
? md_level
: xstrdup("md");
679 const char *type
= NULL
;
682 if (!sysfs_read_int(&cxt
->sysfs
, "device/type", &x
))
683 type
= blkdev_scsi_type_to_name(x
);
686 type
= cxt
->partition
? "part" : "disk";
690 for (p
= res
; p
&& *p
; p
++)
691 *p
= tolower((unsigned char) *p
);
695 /* Thanks to lsscsi code for idea of detection logic used here */
696 static char *get_transport(struct blkdev_cxt
*cxt
)
698 struct sysfs_cxt
*sysfs
= &cxt
->sysfs
;
700 const char *trans
= NULL
;
702 /* SCSI - Serial Peripheral Interface */
703 if (sysfs_scsi_host_is(sysfs
, "spi"))
706 /* FC/FCoE - Fibre Channel / Fibre Channel over Ethernet */
707 else if (sysfs_scsi_host_is(sysfs
, "fc")) {
708 attr
= sysfs_scsi_host_strdup_attribute(sysfs
, "fc", "symbolic_name");
711 trans
= strstr(attr
, " over ") ? "fcoe" : "fc";
715 /* SAS - Serial Attached SCSI */
716 else if (sysfs_scsi_host_is(sysfs
, "sas") ||
717 sysfs_scsi_has_attribute(sysfs
, "sas_device"))
721 /* SBP - Serial Bus Protocol (FireWire) */
722 else if (sysfs_scsi_has_attribute(sysfs
, "ieee1394_id"))
726 else if (sysfs_scsi_host_is(sysfs
, "iscsi"))
729 /* USB - Universal Serial Bus */
730 else if (sysfs_scsi_path_contains(sysfs
, "usb"))
734 else if (sysfs_scsi_host_is(sysfs
, "scsi")) {
735 attr
= sysfs_scsi_host_strdup_attribute(sysfs
, "scsi", "proc_name");
738 if (!strncmp(attr
, "ahci", 4) || !strncmp(attr
, "sata", 4))
740 else if (strstr(attr
, "ata"))
745 return trans
? xstrdup(trans
) : NULL
;
748 static char *get_subsystems(struct blkdev_cxt
*cxt
)
751 char *sub
, *chain
, *res
= NULL
;
752 size_t len
= 0, last
= 0;
754 chain
= sysfs_get_devchain(&cxt
->sysfs
, path
, sizeof(path
));
758 while (sysfs_next_subsystem(&cxt
->sysfs
, chain
, &sub
) == 0) {
761 /* don't create "block:scsi:scsi", but "block:scsi" */
762 if (len
&& strcmp(res
+ last
, sub
) == 0)
766 res
= xrealloc(res
, len
+ sz
+ 2);
770 memcpy(res
+ len
, sub
, sz
+ 1);
780 #define is_parsable(_l) (scols_table_is_raw((_l)->table) || \
781 scols_table_is_export((_l)->table))
783 static char *mk_name(const char *name
)
789 xasprintf(&p
, "/dev/%s", name
);
795 static char *mk_dm_name(const char *name
)
801 xasprintf(&p
, "/dev/mapper/%s", name
);
807 /* stores data to scols cell userdata (invisible and independent on output)
808 * to make the original values accessible for sort functions
810 static void set_sortdata_u64(struct libscols_line
*ln
, int col
, uint64_t x
)
812 struct libscols_cell
*ce
= scols_line_get_cell(ln
, col
);
817 data
= xmalloc(sizeof(uint64_t));
819 scols_cell_set_userdata(ce
, data
);
822 static void set_sortdata_u64_from_string(struct libscols_line
*ln
, int col
, const char *str
)
826 if (!str
|| sscanf(str
, "%"SCNu64
, &x
) != 1)
829 set_sortdata_u64(ln
, col
, x
);
832 static void unref_sortdata(struct libscols_table
*tb
)
834 struct libscols_iter
*itr
;
835 struct libscols_line
*ln
;
837 if (!tb
|| !lsblk
->sort_col
)
839 itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
842 while (scols_table_next_line(tb
, itr
, &ln
) == 0) {
843 struct libscols_cell
*ce
= scols_line_get_column_cell(ln
,
845 void *data
= scols_cell_get_userdata(ce
);
849 scols_free_iter(itr
);
852 static void set_scols_data(struct blkdev_cxt
*cxt
, int col
, int id
, struct libscols_line
*ln
)
854 int sort
= 0, st_rc
= 0;
857 if (!cxt
->st
.st_rdev
&& (id
== COL_OWNER
|| id
== COL_GROUP
||
859 st_rc
= stat(cxt
->filename
, &cxt
->st
);
861 if (lsblk
->sort_id
== id
)
866 str
= cxt
->dm_name
? mk_dm_name(cxt
->dm_name
) : mk_name(cxt
->name
);
869 str
= mk_name(cxt
->name
);
873 str
= mk_name(cxt
->parent
->name
);
877 struct passwd
*pw
= st_rc
? NULL
: getpwuid(cxt
->st
.st_uid
);
879 str
= xstrdup(pw
->pw_name
);
884 struct group
*gr
= st_rc
? NULL
: getgrgid(cxt
->st
.st_gid
);
886 str
= xstrdup(gr
->gr_name
);
894 strmode(cxt
->st
.st_mode
, md
);
900 if (is_parsable(lsblk
))
901 xasprintf(&str
, "%u:%u", cxt
->maj
, cxt
->min
);
903 xasprintf(&str
, "%3u:%-3u", cxt
->maj
, cxt
->min
);
905 set_sortdata_u64(ln
, col
, makedev(cxt
->maj
, cxt
->min
));
910 str
= xstrdup(cxt
->fstype
);
913 if (!(cxt
->nholders
+ cxt
->npartitions
))
914 str
= get_device_mountpoint(cxt
);
919 str
= xstrdup(cxt
->label
);
924 str
= xstrdup(cxt
->uuid
);
929 str
= xstrdup(cxt
->parttype
);
934 str
= xstrdup(cxt
->partlabel
);
939 str
= xstrdup(cxt
->partuuid
);
944 str
= xstrdup(cxt
->partflags
);
947 get_udev_properties(cxt
);
949 str
= xstrdup(cxt
->wwn
);
952 str
= sysfs_strdup(&cxt
->sysfs
, "queue/read_ahead_kb");
954 set_sortdata_u64_from_string(ln
, col
, str
);
957 str
= xstrdup(is_readonly_device(cxt
) ? "1" : "0");
960 str
= sysfs_strdup(&cxt
->sysfs
, "removable");
961 if (!str
&& cxt
->sysfs
.parent
)
962 str
= sysfs_strdup(cxt
->sysfs
.parent
, "removable");
965 str
= sysfs_is_hotpluggable(&cxt
->sysfs
) ? xstrdup("1") : xstrdup("0");
968 str
= sysfs_strdup(&cxt
->sysfs
, "queue/rotational");
971 str
= sysfs_strdup(&cxt
->sysfs
, "queue/add_random");
974 if (!cxt
->partition
&& cxt
->nslaves
== 0)
975 str
= sysfs_strdup(&cxt
->sysfs
, "device/model");
978 if (!cxt
->partition
&& cxt
->nslaves
== 0) {
979 get_udev_properties(cxt
);
981 str
= xstrdup(cxt
->serial
);
985 if (!cxt
->partition
&& cxt
->nslaves
== 0)
986 str
= sysfs_strdup(&cxt
->sysfs
, "device/rev");
989 if (!cxt
->partition
&& cxt
->nslaves
== 0)
990 str
= sysfs_strdup(&cxt
->sysfs
, "device/vendor");
996 xasprintf(&str
, "%jd", cxt
->size
);
998 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, cxt
->size
);
1000 set_sortdata_u64(ln
, col
, cxt
->size
);
1003 if (!cxt
->partition
&& !cxt
->dm_name
)
1004 str
= sysfs_strdup(&cxt
->sysfs
, "device/state");
1005 else if (cxt
->dm_name
) {
1007 if (sysfs_read_int(&cxt
->sysfs
, "dm/suspended", &x
) == 0)
1008 str
= xstrdup(x
? "suspended" : "running");
1012 str
= sysfs_strdup(&cxt
->sysfs
, "alignment_offset");
1014 set_sortdata_u64_from_string(ln
, col
, str
);
1017 str
= sysfs_strdup(&cxt
->sysfs
, "queue/minimum_io_size");
1019 set_sortdata_u64_from_string(ln
, col
, str
);
1022 str
= sysfs_strdup(&cxt
->sysfs
, "queue/optimal_io_size");
1024 set_sortdata_u64_from_string(ln
, col
, str
);
1027 str
= sysfs_strdup(&cxt
->sysfs
, "queue/physical_block_size");
1029 set_sortdata_u64_from_string(ln
, col
, str
);
1032 str
= sysfs_strdup(&cxt
->sysfs
, "queue/logical_block_size");
1034 set_sortdata_u64_from_string(ln
, col
, str
);
1037 str
= get_scheduler(cxt
);
1040 str
= sysfs_strdup(&cxt
->sysfs
, "queue/nr_requests");
1042 set_sortdata_u64_from_string(ln
, col
, str
);
1045 str
= get_type(cxt
);
1050 if (sysfs_scsi_get_hctl(&cxt
->sysfs
, &h
, &c
, &t
, &l
) == 0)
1051 xasprintf(&str
, "%d:%d:%d:%d", h
, c
, t
, l
);
1055 str
= get_transport(cxt
);
1058 str
= get_subsystems(cxt
);
1062 str
= sysfs_strdup(&cxt
->sysfs
, "discard_alignment");
1066 set_sortdata_u64_from_string(ln
, col
, str
);
1070 str
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_granularity");
1072 set_sortdata_u64_from_string(ln
, col
, str
);
1075 if (sysfs_read_u64(&cxt
->sysfs
,
1076 "queue/discard_granularity", &x
) == 0) {
1077 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1079 set_sortdata_u64(ln
, col
, x
);
1085 str
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_max_bytes");
1087 set_sortdata_u64_from_string(ln
, col
, str
);
1090 if (sysfs_read_u64(&cxt
->sysfs
,
1091 "queue/discard_max_bytes", &x
) == 0) {
1092 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1094 set_sortdata_u64(ln
, col
, x
);
1100 str
= sysfs_strdup(&cxt
->sysfs
, "queue/discard_zeroes_data");
1106 str
= sysfs_strdup(&cxt
->sysfs
, "queue/write_same_max_bytes");
1108 set_sortdata_u64_from_string(ln
, col
, str
);
1112 if (sysfs_read_u64(&cxt
->sysfs
,
1113 "queue/write_same_max_bytes", &x
) == 0) {
1114 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, x
);
1116 set_sortdata_u64(ln
, col
, x
);
1125 scols_line_refer_data(ln
, col
, str
);
1128 static void fill_table_line(struct blkdev_cxt
*cxt
, struct libscols_line
*scols_parent
)
1132 cxt
->scols_line
= scols_table_new_line(lsblk
->table
, scols_parent
);
1133 if (!cxt
->scols_line
)
1136 for (i
= 0; i
< ncolumns
; i
++)
1137 set_scols_data(cxt
, i
, get_column_id(i
), cxt
->scols_line
);
1140 static int set_cxt(struct blkdev_cxt
*cxt
,
1141 struct blkdev_cxt
*parent
,
1142 struct blkdev_cxt
*wholedisk
,
1147 DBG(CXT
, ul_debugobj(cxt
, "setting context for %s [parent=%p, wholedisk=%p]",
1148 name
, parent
, wholedisk
));
1150 cxt
->parent
= parent
;
1151 cxt
->name
= xstrdup(name
);
1152 cxt
->partition
= wholedisk
!= NULL
;
1154 cxt
->filename
= get_device_path(cxt
);
1155 if (!cxt
->filename
) {
1156 warnx(_("%s: failed to get device path"), name
);
1159 DBG(CXT
, ul_debugobj(cxt
, "%s: filename=%s", cxt
->name
, cxt
->filename
));
1161 devno
= sysfs_devname_to_devno(name
, wholedisk
? wholedisk
->name
: NULL
);
1164 warnx(_("%s: unknown device name"), name
);
1168 if (lsblk
->inverse
) {
1169 if (sysfs_init(&cxt
->sysfs
, devno
, wholedisk
? &wholedisk
->sysfs
: NULL
)) {
1170 warnx(_("%s: failed to initialize sysfs handler"), name
);
1174 parent
->sysfs
.parent
= &cxt
->sysfs
;
1176 if (sysfs_init(&cxt
->sysfs
, devno
, parent
? &parent
->sysfs
: NULL
)) {
1177 warnx(_("%s: failed to initialize sysfs handler"), name
);
1182 cxt
->maj
= major(devno
);
1183 cxt
->min
= minor(devno
);
1186 if (sysfs_read_u64(&cxt
->sysfs
, "size", &cxt
->size
) == 0) /* in sectors */
1187 cxt
->size
<<= 9; /* in bytes */
1189 if (sysfs_read_int(&cxt
->sysfs
,
1190 "queue/discard_granularity", &cxt
->discard
) != 0)
1193 /* Ignore devices of zero size */
1194 if (!lsblk
->all_devices
&& cxt
->size
== 0) {
1195 DBG(CXT
, ul_debugobj(cxt
, "zero size device -- ignore"));
1199 cxt
->dm_name
= sysfs_strdup(&cxt
->sysfs
, "dm/name");
1200 if (!cxt
->dm_name
) {
1201 warnx(_("%s: failed to get dm name"), name
);
1206 cxt
->npartitions
= sysfs_count_partitions(&cxt
->sysfs
, name
);
1207 cxt
->nholders
= sysfs_count_dirents(&cxt
->sysfs
, "holders");
1208 cxt
->nslaves
= sysfs_count_dirents(&cxt
->sysfs
, "slaves");
1210 DBG(CXT
, ul_debugobj(cxt
, "%s: npartitions=%d, nholders=%d, nslaves=%d",
1211 cxt
->name
, cxt
->npartitions
, cxt
->nholders
, cxt
->nslaves
));
1213 /* ignore non-SCSI devices */
1214 if (lsblk
->scsi
&& sysfs_scsi_get_hctl(&cxt
->sysfs
, NULL
, NULL
, NULL
, NULL
)) {
1215 DBG(CXT
, ul_debugobj(cxt
, "non-scsi device -- ignore"));
1219 DBG(CXT
, ul_debugobj(cxt
, "%s: context successfully initialized", cxt
->name
));
1223 static int process_blkdev(struct blkdev_cxt
*cxt
, struct blkdev_cxt
*parent
,
1224 int do_partitions
, const char *part_name
);
1227 * List device partitions if any.
1229 static int list_partitions(struct blkdev_cxt
*wholedisk_cxt
, struct blkdev_cxt
*parent_cxt
,
1230 const char *part_name
)
1234 struct blkdev_cxt part_cxt
= { 0 };
1237 assert(wholedisk_cxt
);
1240 * Do not process further if there are no partitions for
1241 * this device or the device itself is a partition.
1243 if (!wholedisk_cxt
->npartitions
|| wholedisk_cxt
->partition
)
1246 DBG(CXT
, ul_debugobj(wholedisk_cxt
, "probe whole-disk for partitions"));
1248 dir
= sysfs_opendir(&wholedisk_cxt
->sysfs
, NULL
);
1250 err(EXIT_FAILURE
, _("failed to open device directory in sysfs"));
1252 while ((d
= xreaddir(dir
))) {
1253 /* Process particular partition only? */
1254 if (part_name
&& strcmp(part_name
, d
->d_name
))
1257 if (!(sysfs_is_partition_dirent(dir
, d
, wholedisk_cxt
->name
)))
1260 DBG(CXT
, ul_debugobj(wholedisk_cxt
, " checking %s", d
->d_name
));
1262 if (lsblk
->inverse
) {
1269 if (set_cxt(&part_cxt
, parent_cxt
, wholedisk_cxt
, d
->d_name
))
1272 if (!parent_cxt
&& part_cxt
.nholders
)
1275 wholedisk_cxt
->parent
= &part_cxt
;
1276 fill_table_line(&part_cxt
, parent_cxt
? parent_cxt
->scols_line
: NULL
);
1278 process_blkdev(wholedisk_cxt
, &part_cxt
, 0, NULL
);
1286 int ps
= set_cxt(&part_cxt
, wholedisk_cxt
, wholedisk_cxt
, d
->d_name
);
1288 /* Print whole disk only once */
1290 fill_table_line(wholedisk_cxt
, parent_cxt
? parent_cxt
->scols_line
: NULL
);
1291 if (ps
== 0 && !lsblk
->nodeps
)
1292 process_blkdev(&part_cxt
, wholedisk_cxt
, 0, NULL
);
1295 reset_blkdev_cxt(&part_cxt
);
1299 DBG(CXT
, ul_debugobj(wholedisk_cxt
, "probe whole-disk for partitions -- done"));
1304 static int get_wholedisk_from_partition_dirent(DIR *dir
, const char *dirname
,
1305 struct dirent
*d
, struct blkdev_cxt
*cxt
)
1307 char path
[PATH_MAX
];
1311 if ((len
= readlink_at(dirfd(dir
), dirname
,
1312 d
->d_name
, path
, sizeof(path
) - 1)) < 0)
1317 /* The path ends with ".../<device>/<partition>" */
1318 p
= strrchr(path
, '/');
1323 p
= strrchr(path
, '/');
1328 return set_cxt(cxt
, NULL
, NULL
, p
);
1332 * List device dependencies: partitions, holders (inverse = 0) or slaves (inverse = 1).
1334 static int list_deps(struct blkdev_cxt
*cxt
)
1338 struct blkdev_cxt dep
= { 0 };
1339 char dirname
[PATH_MAX
];
1340 const char *depname
;
1347 DBG(CXT
, ul_debugobj(cxt
, "%s: list dependencies", cxt
->name
));
1349 if (!(lsblk
->inverse
? cxt
->nslaves
: cxt
->nholders
))
1352 depname
= lsblk
->inverse
? "slaves" : "holders";
1353 dir
= sysfs_opendir(&cxt
->sysfs
, depname
);
1357 DBG(CXT
, ul_debugobj(cxt
, "%s: checking for '%s' dependence", cxt
->name
, depname
));
1359 snprintf(dirname
, sizeof(dirname
), "%s/%s", cxt
->sysfs
.dir_path
, depname
);
1361 while ((d
= xreaddir(dir
))) {
1362 /* Is the dependency a partition? */
1363 if (sysfs_is_partition_dirent(dir
, d
, NULL
)) {
1364 if (!get_wholedisk_from_partition_dirent(dir
, dirname
, d
, &dep
)) {
1365 DBG(CXT
, ul_debugobj(cxt
, "%s: %s: dependence is partition",
1366 cxt
->name
, d
->d_name
));
1367 process_blkdev(&dep
, cxt
, 1, d
->d_name
);
1370 /* The dependency is a whole device. */
1371 else if (!set_cxt(&dep
, cxt
, NULL
, d
->d_name
)) {
1372 DBG(CXT
, ul_debugobj(cxt
, "%s: %s: dependence is whole-disk",
1373 cxt
->name
, d
->d_name
));
1374 /* For inverse tree we don't want to show partitions
1375 * if the dependence is pn whle-disk */
1376 process_blkdev(&dep
, cxt
, lsblk
->inverse
? 0 : 1, NULL
);
1378 reset_blkdev_cxt(&dep
);
1382 DBG(CXT
, ul_debugobj(cxt
, "%s: checking for '%s' -- done", cxt
->name
, depname
));
1386 static int process_blkdev(struct blkdev_cxt
*cxt
, struct blkdev_cxt
*parent
,
1387 int do_partitions
, const char *part_name
)
1389 if (do_partitions
&& cxt
->npartitions
)
1390 list_partitions(cxt
, parent
, part_name
); /* partitoins + whole-disk */
1392 fill_table_line(cxt
, parent
? parent
->scols_line
: NULL
); /* whole-disk only */
1394 return list_deps(cxt
);
1397 /* Iterate devices in sysfs */
1398 static int iterate_block_devices(void)
1402 struct blkdev_cxt cxt
= { 0 };
1404 if (!(dir
= opendir(_PATH_SYS_BLOCK
)))
1407 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_BLOCK
));
1409 while ((d
= xreaddir(dir
))) {
1411 DBG(DEV
, ul_debug(" %s dentry", d
->d_name
));
1413 if (set_cxt(&cxt
, NULL
, NULL
, d
->d_name
))
1416 if (is_maj_excluded(cxt
.maj
) || !is_maj_included(cxt
.maj
))
1419 /* Skip devices in the middle of dependency tree. */
1420 if ((lsblk
->inverse
? cxt
.nholders
: cxt
.nslaves
) > 0)
1423 process_blkdev(&cxt
, NULL
, 1, NULL
);
1425 reset_blkdev_cxt(&cxt
);
1430 DBG(DEV
, ul_debug("iterate on " _PATH_SYS_BLOCK
" -- done"));
1434 static char *devno_to_sysfs_name(dev_t devno
, char *devname
, char *buf
, size_t buf_size
)
1436 char path
[PATH_MAX
];
1439 if (!sysfs_devno_path(devno
, path
, sizeof(path
))) {
1440 warn(_("%s: failed to compose sysfs path"), devname
);
1444 len
= readlink(path
, buf
, buf_size
- 1);
1446 warn(_("%s: failed to read link"), path
);
1451 return xstrdup(strrchr(buf
, '/') + 1);
1454 static int process_one_device(char *devname
)
1456 struct blkdev_cxt parent
= { 0 }, cxt
= { 0 };
1458 char buf
[PATH_MAX
+ 1], *name
= NULL
, *diskname
= NULL
;
1460 int real_part
= 0, rc
= -EINVAL
;
1462 if (stat(devname
, &st
) || !S_ISBLK(st
.st_mode
)) {
1463 warnx(_("%s: not a block device"), devname
);
1467 if (!(name
= devno_to_sysfs_name(st
.st_rdev
, devname
, buf
, PATH_MAX
))) {
1468 warn(_("%s: failed to get sysfs name"), devname
);
1472 if (!strncmp(name
, "dm-", 3)) {
1473 /* dm mapping is never a real partition! */
1476 if (blkid_devno_to_wholedisk(st
.st_rdev
, buf
, sizeof(buf
), &disk
)) {
1477 warn(_("%s: failed to get whole-disk device number"), devname
);
1481 real_part
= st
.st_rdev
!= disk
;
1486 * Device is not a partition.
1488 if (set_cxt(&cxt
, NULL
, NULL
, name
))
1490 process_blkdev(&cxt
, NULL
, !lsblk
->inverse
, NULL
);
1493 * Partition, read sysfs name of the device.
1495 if (set_cxt(&parent
, NULL
, NULL
, diskname
))
1497 if (set_cxt(&cxt
, &parent
, &parent
, name
))
1501 process_blkdev(&parent
, &cxt
, 1, cxt
.name
);
1503 process_blkdev(&cxt
, &parent
, 1, NULL
);
1509 reset_blkdev_cxt(&cxt
);
1512 reset_blkdev_cxt(&parent
);
1517 static void parse_excludes(const char *str0
)
1519 const char *str
= str0
;
1521 while (str
&& *str
) {
1526 n
= strtoul(str
, &end
, 10);
1528 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1529 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1530 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1531 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1532 excludes
[nexcludes
++] = n
;
1534 if (nexcludes
== ARRAY_SIZE(excludes
))
1535 /* TRANSLATORS: The standard value for %d is 256. */
1536 errx(EXIT_FAILURE
, _("the list of excluded devices is "
1537 "too large (limit is %d devices)"),
1538 (int)ARRAY_SIZE(excludes
));
1540 str
= end
&& *end
? end
+ 1 : NULL
;
1544 static void parse_includes(const char *str0
)
1546 const char *str
= str0
;
1548 while (str
&& *str
) {
1553 n
= strtoul(str
, &end
, 10);
1555 if (end
== str
|| (end
&& *end
&& *end
!= ','))
1556 errx(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1557 if (errno
!= 0 && (n
== ULONG_MAX
|| n
== 0))
1558 err(EXIT_FAILURE
, _("failed to parse list '%s'"), str0
);
1559 includes
[nincludes
++] = n
;
1561 if (nincludes
== ARRAY_SIZE(includes
))
1562 /* TRANSLATORS: The standard value for %d is 256. */
1563 errx(EXIT_FAILURE
, _("the list of included devices is "
1564 "too large (limit is %d devices)"),
1565 (int)ARRAY_SIZE(includes
));
1566 str
= end
&& *end
? end
+ 1 : NULL
;
1571 * see set_sortdata_u64() and columns initialization in main()
1573 static int cmp_u64_cells(struct libscols_cell
*a
,
1574 struct libscols_cell
*b
,
1575 __attribute__((__unused__
)) void *data
)
1577 uint64_t *adata
= (uint64_t *) scols_cell_get_userdata(a
),
1578 *bdata
= (uint64_t *) scols_cell_get_userdata(b
);
1580 if (adata
== NULL
&& bdata
== NULL
)
1586 return *adata
== *bdata
? 0 : *adata
>= *bdata
? 1 : -1;
1589 static void __attribute__((__noreturn__
)) help(FILE *out
)
1593 fputs(USAGE_HEADER
, out
);
1594 fprintf(out
, _(" %s [options] [<device> ...]\n"), program_invocation_short_name
);
1596 fputs(USAGE_SEPARATOR
, out
);
1597 fputs(_("List information about block devices.\n"), out
);
1599 fputs(USAGE_OPTIONS
, out
);
1600 fputs(_(" -a, --all print all devices\n"), out
);
1601 fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out
);
1602 fputs(_(" -d, --nodeps don't print slaves or holders\n"), out
);
1603 fputs(_(" -D, --discard print discard capabilities\n"), out
);
1604 fputs(_(" -e, --exclude <list> exclude devices by major number (default: RAM disks)\n"), out
);
1605 fputs(_(" -f, --fs output info about filesystems\n"), out
);
1606 fputs(_(" -i, --ascii use ascii characters only\n"), out
);
1607 fputs(_(" -I, --include <list> show only devices with specified major numbers\n"), out
);
1608 fputs(_(" -l, --list use list format output\n"), out
);
1609 fputs(_(" -m, --perms output info about permissions\n"), out
);
1610 fputs(_(" -n, --noheadings don't print headings\n"), out
);
1611 fputs(_(" -o, --output <list> output columns\n"), out
);
1612 fputs(_(" -O, --output-all output all columns\n"), out
);
1613 fputs(_(" -p, --paths print complete device path\n"), out
);
1614 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out
);
1615 fputs(_(" -r, --raw use raw output format\n"), out
);
1616 fputs(_(" -s, --inverse inverse dependencies\n"), out
);
1617 fputs(_(" -S, --scsi output info about SCSI devices\n"), out
);
1618 fputs(_(" -t, --topology output info about topology\n"), out
);
1619 fputs(_(" -x, --sort <column> sort output by <column>\n"), out
);
1620 fputs(USAGE_SEPARATOR
, out
);
1621 fputs(USAGE_HELP
, out
);
1622 fputs(USAGE_VERSION
, out
);
1624 fprintf(out
, _("\nAvailable columns (for --output):\n"));
1626 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
1627 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
1629 fprintf(out
, USAGE_MAN_TAIL("lsblk(8)"));
1631 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
1634 static void check_sysdevblock(void)
1636 if (access(_PATH_SYS_DEVBLOCK
, R_OK
) != 0)
1637 err(EXIT_FAILURE
, _("failed to access sysfs directory: %s"),
1638 _PATH_SYS_DEVBLOCK
);
1641 int main(int argc
, char *argv
[])
1643 struct lsblk _ls
= { .sort_id
= -1 };
1644 int scols_flags
= LSBLK_TREE
;
1645 int i
, c
, status
= EXIT_FAILURE
;
1646 char *outarg
= NULL
;
1648 static const struct option longopts
[] = {
1649 { "all", 0, 0, 'a' },
1650 { "bytes", 0, 0, 'b' },
1651 { "nodeps", 0, 0, 'd' },
1652 { "discard", 0, 0, 'D' },
1653 { "help", 0, 0, 'h' },
1654 { "output", 1, 0, 'o' },
1655 { "output-all", 0, 0, 'O' },
1656 { "perms", 0, 0, 'm' },
1657 { "noheadings", 0, 0, 'n' },
1658 { "list", 0, 0, 'l' },
1659 { "ascii", 0, 0, 'i' },
1660 { "raw", 0, 0, 'r' },
1661 { "inverse", 0, 0, 's' },
1662 { "fs", 0, 0, 'f' },
1663 { "exclude", 1, 0, 'e' },
1664 { "include", 1, 0, 'I' },
1665 { "topology", 0, 0, 't' },
1666 { "paths", 0, 0, 'p' },
1667 { "pairs", 0, 0, 'P' },
1668 { "scsi", 0, 0, 'S' },
1669 { "sort", 1, 0, 'x' },
1670 { "version", 0, 0, 'V' },
1674 static const ul_excl_t excl
[] = { /* rows and cols in in ASCII order */
1684 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1686 setlocale(LC_ALL
, "");
1687 bindtextdomain(PACKAGE
, LOCALEDIR
);
1688 textdomain(PACKAGE
);
1689 atexit(close_stdout
);
1695 while((c
= getopt_long(argc
, argv
,
1696 "abdDe:fhlnmo:OpPiI:rstVSx:", longopts
, NULL
)) != -1) {
1698 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1702 lsblk
->all_devices
= 1;
1711 add_column(columns
, ncolumns
++, COL_NAME
);
1712 add_column(columns
, ncolumns
++, COL_DALIGN
);
1713 add_column(columns
, ncolumns
++, COL_DGRAN
);
1714 add_column(columns
, ncolumns
++, COL_DMAX
);
1715 add_column(columns
, ncolumns
++, COL_DZERO
);
1718 parse_excludes(optarg
);
1724 scols_flags
&= ~LSBLK_TREE
; /* disable the default */
1727 scols_flags
|= LSBLK_NOHEADINGS
;
1733 for (ncolumns
= 0 ; ncolumns
< (int) ARRAY_SIZE(infos
); ncolumns
++)
1734 columns
[ncolumns
] = ncolumns
;
1740 scols_flags
|= LSBLK_EXPORT
;
1741 scols_flags
&= ~LSBLK_TREE
; /* disable the default */
1744 scols_flags
|= LSBLK_ASCII
;
1747 parse_includes(optarg
);
1750 scols_flags
&= ~LSBLK_TREE
; /* disable the default */
1751 scols_flags
|= LSBLK_RAW
; /* enable raw */
1757 add_column(columns
, ncolumns
++, COL_NAME
);
1758 add_column(columns
, ncolumns
++, COL_FSTYPE
);
1759 add_column(columns
, ncolumns
++, COL_LABEL
);
1760 add_column(columns
, ncolumns
++, COL_UUID
);
1761 add_column(columns
, ncolumns
++, COL_TARGET
);
1764 add_column(columns
, ncolumns
++, COL_NAME
);
1765 add_column(columns
, ncolumns
++, COL_SIZE
);
1766 add_column(columns
, ncolumns
++, COL_OWNER
);
1767 add_column(columns
, ncolumns
++, COL_GROUP
);
1768 add_column(columns
, ncolumns
++, COL_MODE
);
1771 add_column(columns
, ncolumns
++, COL_NAME
);
1772 add_column(columns
, ncolumns
++, COL_ALIOFF
);
1773 add_column(columns
, ncolumns
++, COL_MINIO
);
1774 add_column(columns
, ncolumns
++, COL_OPTIO
);
1775 add_column(columns
, ncolumns
++, COL_PHYSEC
);
1776 add_column(columns
, ncolumns
++, COL_LOGSEC
);
1777 add_column(columns
, ncolumns
++, COL_ROTA
);
1778 add_column(columns
, ncolumns
++, COL_SCHED
);
1779 add_column(columns
, ncolumns
++, COL_RQ_SIZE
);
1780 add_column(columns
, ncolumns
++, COL_RA
);
1781 add_column(columns
, ncolumns
++, COL_WSAME
);
1786 add_column(columns
, ncolumns
++, COL_NAME
);
1787 add_column(columns
, ncolumns
++, COL_HCTL
);
1788 add_column(columns
, ncolumns
++, COL_TYPE
);
1789 add_column(columns
, ncolumns
++, COL_VENDOR
);
1790 add_column(columns
, ncolumns
++, COL_MODEL
);
1791 add_column(columns
, ncolumns
++, COL_REV
);
1792 add_column(columns
, ncolumns
++, COL_TRANSPORT
);
1795 printf(UTIL_LINUX_VERSION
);
1796 return EXIT_SUCCESS
;
1798 scols_flags
&= ~LSBLK_TREE
; /* disable the default */
1799 lsblk
->sort_id
= column_name_to_id(optarg
, strlen(optarg
));
1800 if (lsblk
->sort_id
>= 0)
1808 check_sysdevblock();
1811 add_column(columns
, ncolumns
++, COL_NAME
);
1812 add_column(columns
, ncolumns
++, COL_MAJMIN
);
1813 add_column(columns
, ncolumns
++, COL_RM
);
1814 add_column(columns
, ncolumns
++, COL_SIZE
);
1815 add_column(columns
, ncolumns
++, COL_RO
);
1816 add_column(columns
, ncolumns
++, COL_TYPE
);
1817 add_column(columns
, ncolumns
++, COL_TARGET
);
1820 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
1821 &ncolumns
, column_name_to_id
) < 0)
1822 return EXIT_FAILURE
;
1824 if (nexcludes
== 0 && nincludes
== 0)
1825 excludes
[nexcludes
++] = 1; /* default: ignore RAM disks */
1827 if (lsblk
->sort_id
>= 0 && column_id_to_number(lsblk
->sort_id
) < 0)
1828 errx(EXIT_FAILURE
, _("the sort column has to be among the output columns"));
1831 scols_init_debug(0);
1834 * initialize output columns
1836 if (!(lsblk
->table
= scols_new_table()))
1837 errx(EXIT_FAILURE
, _("failed to initialize output table"));
1838 scols_table_enable_raw(lsblk
->table
, !!(scols_flags
& LSBLK_RAW
));
1839 scols_table_enable_export(lsblk
->table
, !!(scols_flags
& LSBLK_EXPORT
));
1840 scols_table_enable_ascii(lsblk
->table
, !!(scols_flags
& LSBLK_ASCII
));
1841 scols_table_enable_noheadings(lsblk
->table
, !!(scols_flags
& LSBLK_NOHEADINGS
));
1843 for (i
= 0; i
< ncolumns
; i
++) {
1844 struct colinfo
*ci
= get_column_info(i
);
1845 struct libscols_column
*cl
;
1846 int id
= get_column_id(i
), fl
= ci
->flags
;
1848 if (!(scols_flags
& LSBLK_TREE
) && id
== COL_NAME
)
1849 fl
&= ~SCOLS_FL_TREE
;
1851 cl
= scols_table_new_column(lsblk
->table
, ci
->name
, ci
->whint
, fl
);
1853 warn(_("failed to initialize output column"));
1856 if (!lsblk
->sort_col
&& lsblk
->sort_id
== id
) {
1857 lsblk
->sort_col
= cl
;
1858 scols_column_set_cmpfunc(cl
,
1859 ci
->sort_type
== SORT_STRING
?
1860 scols_cmpstr_cells
: cmp_u64_cells
, NULL
);
1865 status
= iterate_block_devices() == 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
1867 int cnt
= 0, cnt_err
= 0;
1869 while (optind
< argc
) {
1870 if (process_one_device(argv
[optind
++]) != 0)
1874 status
= cnt
== 0 ? EXIT_FAILURE
: /* nothing */
1875 cnt
== cnt_err
? LSBLK_EXIT_ALLFAILED
:/* all failed */
1876 cnt_err
? LSBLK_EXIT_SOMEOK
: /* some ok */
1877 EXIT_SUCCESS
; /* all success */
1880 if (lsblk
->sort_col
)
1881 scols_sort_table(lsblk
->table
, lsblk
->sort_col
);
1883 scols_print_table(lsblk
->table
);
1886 if (lsblk
->sort_col
)
1887 unref_sortdata(lsblk
->table
);
1889 scols_unref_table(lsblk
->table
);
1891 mnt_unref_table(mtab
);
1892 mnt_unref_table(swaps
);
1893 mnt_unref_cache(mntcache
);