]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/lsblk.c
libsmartcols: clean up flags usage
[thirdparty/util-linux.git] / misc-utils / lsblk.c
1 /*
2 * lsblk(8) - list block devices
3 *
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>
7 *
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.
12 *
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.
17 *
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.
21 */
22
23 #include <stdio.h>
24 #include <errno.h>
25 #include <getopt.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <stdint.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <dirent.h>
32 #include <fcntl.h>
33 #include <string.h>
34 #include <sys/ioctl.h>
35 #include <inttypes.h>
36 #include <stdarg.h>
37 #include <locale.h>
38 #include <pwd.h>
39 #include <grp.h>
40 #include <ctype.h>
41
42 #include <blkid.h>
43 #include <libmount.h>
44 #include <libsmartcols.h>
45
46 #ifdef HAVE_LIBUDEV
47 #include <libudev.h>
48 #endif
49
50 #include <assert.h>
51
52 #include "c.h"
53 #include "pathnames.h"
54 #include "blkdev.h"
55 #include "canonicalize.h"
56 #include "nls.h"
57 #include "xalloc.h"
58 #include "strutils.h"
59 #include "at.h"
60 #include "sysfs.h"
61 #include "closestream.h"
62 #include "mangle.h"
63 #include "optutils.h"
64
65 /* column IDs */
66 enum {
67 COL_NAME = 0,
68 COL_KNAME,
69 COL_MAJMIN,
70 COL_FSTYPE,
71 COL_TARGET,
72 COL_LABEL,
73 COL_UUID,
74 COL_PARTTYPE,
75 COL_PARTLABEL,
76 COL_PARTUUID,
77 COL_PARTFLAGS,
78 COL_RA,
79 COL_RO,
80 COL_RM,
81 COL_MODEL,
82 COL_SERIAL,
83 COL_SIZE,
84 COL_STATE,
85 COL_OWNER,
86 COL_GROUP,
87 COL_MODE,
88 COL_ALIOFF,
89 COL_MINIO,
90 COL_OPTIO,
91 COL_PHYSEC,
92 COL_LOGSEC,
93 COL_ROTA,
94 COL_SCHED,
95 COL_RQ_SIZE,
96 COL_TYPE,
97 COL_DALIGN,
98 COL_DGRAN,
99 COL_DMAX,
100 COL_DZERO,
101 COL_WSAME,
102 COL_WWN,
103 COL_RAND,
104 COL_PKNAME,
105 COL_HCTL,
106 COL_TRANSPORT,
107 COL_REV,
108 COL_VENDOR,
109 };
110
111 /* basic table settings */
112 enum {
113 LSBLK_ASCII = (1 << 0),
114 LSBLK_RAW = (1 << 1),
115 LSBLK_NOHEADINGS = (1 << 2),
116 LSBLK_EXPORT = (1 << 3),
117 LSBLK_TREE = (1 << 4),
118 };
119
120 /* column names */
121 struct colinfo {
122 const char *name; /* header */
123 double whint; /* width hint (N < 1 is in percent of termwidth) */
124 int flags; /* SCOLS_FL_* */
125 const char *help;
126 };
127
128 /* columns descriptions */
129 static struct colinfo infos[] = {
130 [COL_NAME] = { "NAME", 0.25, SCOLS_FL_TREE | SCOLS_FL_NOEXTREMES, N_("device name") },
131 [COL_KNAME] = { "KNAME", 0.3, 0, N_("internal kernel device name") },
132 [COL_PKNAME] = { "PKNAME", 0.3, 0, N_("internal parent kernel device name") },
133 [COL_MAJMIN] = { "MAJ:MIN", 6, 0, N_("major:minor device number") },
134 [COL_FSTYPE] = { "FSTYPE", 0.1, SCOLS_FL_TRUNC, N_("filesystem type") },
135 [COL_TARGET] = { "MOUNTPOINT", 0.10, SCOLS_FL_TRUNC, N_("where the device is mounted") },
136 [COL_LABEL] = { "LABEL", 0.1, 0, N_("filesystem LABEL") },
137 [COL_UUID] = { "UUID", 36, 0, N_("filesystem UUID") },
138
139 [COL_PARTTYPE] = { "PARTTYPE", 36, 0, N_("partition type UUID") },
140 [COL_PARTLABEL] = { "PARTLABEL", 0.1, 0, N_("partition LABEL") },
141 [COL_PARTUUID] = { "PARTUUID", 36, 0, N_("partition UUID") },
142 [COL_PARTFLAGS] = { "PARTFLAGS", 36, 0, N_("partition flags") },
143
144 [COL_RA] = { "RA", 3, SCOLS_FL_RIGHT, N_("read-ahead of the device") },
145 [COL_RO] = { "RO", 1, SCOLS_FL_RIGHT, N_("read-only device") },
146 [COL_RM] = { "RM", 1, SCOLS_FL_RIGHT, N_("removable device") },
147 [COL_ROTA] = { "ROTA", 1, SCOLS_FL_RIGHT, N_("rotational device") },
148 [COL_RAND] = { "RAND", 1, SCOLS_FL_RIGHT, N_("adds randomness") },
149 [COL_MODEL] = { "MODEL", 0.1, SCOLS_FL_TRUNC, N_("device identifier") },
150 [COL_SERIAL] = { "SERIAL", 0.1, SCOLS_FL_TRUNC, N_("disk serial number") },
151 [COL_SIZE] = { "SIZE", 5, SCOLS_FL_RIGHT, N_("size of the device") },
152 [COL_STATE] = { "STATE", 7, SCOLS_FL_TRUNC, N_("state of the device") },
153 [COL_OWNER] = { "OWNER", 0.1, SCOLS_FL_TRUNC, N_("user name"), },
154 [COL_GROUP] = { "GROUP", 0.1, SCOLS_FL_TRUNC, N_("group name") },
155 [COL_MODE] = { "MODE", 10, 0, N_("device node permissions") },
156 [COL_ALIOFF] = { "ALIGNMENT", 6, SCOLS_FL_RIGHT, N_("alignment offset") },
157 [COL_MINIO] = { "MIN-IO", 6, SCOLS_FL_RIGHT, N_("minimum I/O size") },
158 [COL_OPTIO] = { "OPT-IO", 6, SCOLS_FL_RIGHT, N_("optimal I/O size") },
159 [COL_PHYSEC] = { "PHY-SEC", 7, SCOLS_FL_RIGHT, N_("physical sector size") },
160 [COL_LOGSEC] = { "LOG-SEC", 7, SCOLS_FL_RIGHT, N_("logical sector size") },
161 [COL_SCHED] = { "SCHED", 0.1, 0, N_("I/O scheduler name") },
162 [COL_RQ_SIZE]= { "RQ-SIZE", 5, SCOLS_FL_RIGHT, N_("request queue size") },
163 [COL_TYPE] = { "TYPE", 4, 0, N_("device type") },
164 [COL_DALIGN] = { "DISC-ALN", 6, SCOLS_FL_RIGHT, N_("discard alignment offset") },
165 [COL_DGRAN] = { "DISC-GRAN", 6, SCOLS_FL_RIGHT, N_("discard granularity") },
166 [COL_DMAX] = { "DISC-MAX", 6, SCOLS_FL_RIGHT, N_("discard max bytes") },
167 [COL_DZERO] = { "DISC-ZERO", 1, SCOLS_FL_RIGHT, N_("discard zeroes data") },
168 [COL_WSAME] = { "WSAME", 6, SCOLS_FL_RIGHT, N_("write same max bytes") },
169 [COL_WWN] = { "WWN", 18, 0, N_("unique storage identifier") },
170 [COL_HCTL] = { "HCTL", 10, 0, N_("Host:Channel:Target:Lun for SCSI") },
171 [COL_TRANSPORT] = { "TRAN", 6, 0, N_("device transport type") },
172 [COL_REV] = { "REV", 4, SCOLS_FL_RIGHT, N_("device revision") },
173 [COL_VENDOR] = { "VENDOR", 0.1, SCOLS_FL_TRUNC, N_("device vendor") },
174 };
175
176 struct lsblk {
177 struct libscols_table *table; /* output table */
178 unsigned int all_devices:1; /* print all devices, including empty */
179 unsigned int bytes:1; /* print SIZE in bytes */
180 unsigned int inverse:1; /* print inverse dependencies */
181 unsigned int nodeps:1; /* don't print slaves/holders */
182 unsigned int scsi:1; /* print only device with HCTL (SCSI) */
183 unsigned int paths:1; /* print devnames with "/dev" prefix */
184 };
185
186 struct lsblk *lsblk; /* global handler */
187
188 #define NCOLS ARRAY_SIZE(infos)
189 static int columns[NCOLS];/* enabled columns */
190 static int ncolumns; /* number of enabled columns */
191
192 static int excludes[256];
193 static size_t nexcludes;
194
195 static int includes[256];
196 static size_t nincludes;
197
198 static struct libmnt_table *mtab, *swaps;
199 static struct libmnt_cache *mntcache;
200
201 #ifdef HAVE_LIBUDEV
202 struct udev *udev;
203 #endif
204
205 struct blkdev_cxt {
206 struct blkdev_cxt *parent;
207
208 struct libscols_line *scols_line;
209 struct stat st;
210
211 char *name; /* kernel name in /sys/block */
212 char *dm_name; /* DM name (dm/block) */
213
214 char *filename; /* path to device node */
215
216 struct sysfs_cxt sysfs;
217
218 int partition; /* is partition? TRUE/FALSE */
219
220 int probed; /* already probed */
221 char *fstype; /* detected fs, NULL or "?" if cannot detect */
222 char *uuid; /* filesystem UUID (or stack uuid) */
223 char *label; /* filesystem label */
224 char *parttype; /* partiton type UUID */
225 char *partuuid; /* partition UUID */
226 char *partlabel; /* partiton label */
227 char *partflags; /* partition flags */
228 char *wwn; /* storage WWN */
229 char *serial; /* disk serial number */
230
231 int npartitions; /* # of partitions this device has */
232 int nholders; /* # of devices mapped directly to this device
233 * /sys/block/.../holders */
234 int nslaves; /* # of devices this device maps to */
235 int maj, min; /* devno */
236 int discard; /* supports discard */
237
238 uint64_t size; /* device size */
239 };
240
241 static int is_maj_excluded(int maj)
242 {
243 size_t i;
244
245 assert(ARRAY_SIZE(excludes) > nexcludes);
246
247 if (!nexcludes)
248 return 0; /* filter not enabled, device not exluded */
249
250 for (i = 0; i < nexcludes; i++)
251 if (excludes[i] == maj)
252 return 1;
253 return 0;
254 }
255
256 static int is_maj_included(int maj)
257 {
258 size_t i;
259
260 assert(ARRAY_SIZE(includes) > nincludes);
261
262 if (!nincludes)
263 return 1; /* filter not enabled, device is included */
264
265 for (i = 0; i < nincludes; i++)
266 if (includes[i] == maj)
267 return 1;
268 return 0;
269 }
270
271 /* array with IDs of enabled columns */
272 static int get_column_id(int num)
273 {
274 assert(ARRAY_SIZE(columns) == NCOLS);
275 assert(num < ncolumns);
276 assert(columns[num] < (int) NCOLS);
277 return columns[num];
278 }
279
280 static struct colinfo *get_column_info(int num)
281 {
282 return &infos[ get_column_id(num) ];
283 }
284
285 static int column_name_to_id(const char *name, size_t namesz)
286 {
287 size_t i;
288
289 for (i = 0; i < NCOLS; i++) {
290 const char *cn = infos[i].name;
291
292 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
293 return i;
294 }
295 warnx(_("unknown column: %s"), name);
296 return -1;
297 }
298
299 static void reset_blkdev_cxt(struct blkdev_cxt *cxt)
300 {
301 if (!cxt)
302 return;
303 free(cxt->name);
304 free(cxt->dm_name);
305 free(cxt->filename);
306 free(cxt->fstype);
307 free(cxt->uuid);
308 free(cxt->label);
309 free(cxt->parttype);
310 free(cxt->partuuid);
311 free(cxt->partlabel);
312 free(cxt->wwn);
313 free(cxt->serial);
314
315 sysfs_deinit(&cxt->sysfs);
316
317 memset(cxt, 0, sizeof(*cxt));
318 }
319
320 static int is_dm(const char *name)
321 {
322 return strncmp(name, "dm-", 3) ? 0 : 1;
323 }
324
325 static struct dirent *xreaddir(DIR *dp)
326 {
327 struct dirent *d;
328
329 assert(dp);
330
331 while ((d = readdir(dp))) {
332 if (!strcmp(d->d_name, ".") ||
333 !strcmp(d->d_name, ".."))
334 continue;
335
336 /* blacklist here? */
337 break;
338 }
339 return d;
340 }
341
342 static char *get_device_path(struct blkdev_cxt *cxt)
343 {
344 char path[PATH_MAX];
345
346 assert(cxt);
347 assert(cxt->name);
348
349 if (is_dm(cxt->name))
350 return canonicalize_dm_name(cxt->name);
351
352 snprintf(path, sizeof(path), "/dev/%s", cxt->name);
353 return xstrdup(path);
354 }
355
356 static int is_active_swap(const char *filename)
357 {
358 if (!swaps) {
359 swaps = mnt_new_table();
360 if (!swaps)
361 return 0;
362 if (!mntcache)
363 mntcache = mnt_new_cache();
364
365 mnt_table_set_cache(swaps, mntcache);
366 mnt_table_parse_swaps(swaps, NULL);
367 }
368
369 return mnt_table_find_srcpath(swaps, filename, MNT_ITER_BACKWARD) != 0;
370 }
371
372 static char *get_device_mountpoint(struct blkdev_cxt *cxt)
373 {
374 struct libmnt_fs *fs;
375 const char *fsroot;
376
377 assert(cxt);
378 assert(cxt->filename);
379
380 if (!mtab) {
381 mtab = mnt_new_table();
382 if (!mtab)
383 return NULL;
384 if (!mntcache)
385 mntcache = mnt_new_cache();
386
387 mnt_table_set_cache(mtab, mntcache);
388 mnt_table_parse_mtab(mtab, NULL);
389 }
390
391 /* Note that maj:min in /proc/self/mouninfo does not have to match with
392 * devno as returned by stat(), so we have to try devname too
393 */
394 fs = mnt_table_find_devno(mtab, makedev(cxt->maj, cxt->min), MNT_ITER_BACKWARD);
395 if (!fs)
396 fs = mnt_table_find_srcpath(mtab, cxt->filename, MNT_ITER_BACKWARD);
397 if (!fs)
398 return is_active_swap(cxt->filename) ? xstrdup("[SWAP]") : NULL;
399
400 /* found */
401 fsroot = mnt_fs_get_root(fs);
402 if (fsroot && strcmp(fsroot, "/") != 0) {
403 /* hmm.. we found bind mount or btrfs subvolume, let's try to
404 * get real FS root mountpoint */
405 struct libmnt_fs *rfs;
406 struct libmnt_iter *itr = mnt_new_iter(MNT_ITER_BACKWARD);
407
408 mnt_table_set_iter(mtab, itr, fs);
409 while (mnt_table_next_fs(mtab, itr, &rfs) == 0) {
410 fsroot = mnt_fs_get_root(rfs);
411 if ((!fsroot || strcmp(fsroot, "/") == 0)
412 && mnt_fs_match_source(rfs, cxt->filename, mntcache)) {
413 fs = rfs;
414 break;
415 }
416 }
417 mnt_free_iter(itr);
418 }
419
420 return xstrdup(mnt_fs_get_target(fs));
421 }
422
423 #ifndef HAVE_LIBUDEV
424 static int get_udev_properties(struct blkdev_cxt *cxt
425 __attribute__((__unused__)))
426 {
427 return -1;
428 }
429 #else
430 static int get_udev_properties(struct blkdev_cxt *cxt)
431 {
432 struct udev_device *dev;
433
434 if (cxt->probed)
435 return 0; /* already done */
436
437 if (!udev)
438 udev = udev_new();
439 if (!udev)
440 return -1;
441
442 dev = udev_device_new_from_subsystem_sysname(udev, "block", cxt->name);
443 if (dev) {
444 const char *data;
445
446 if ((data = udev_device_get_property_value(dev, "ID_FS_LABEL_ENC"))) {
447 cxt->label = xstrdup(data);
448 unhexmangle_string(cxt->label);
449 }
450 if ((data = udev_device_get_property_value(dev, "ID_FS_UUID_ENC"))) {
451 cxt->uuid = xstrdup(data);
452 unhexmangle_string(cxt->uuid);
453 }
454 if ((data = udev_device_get_property_value(dev, "ID_PART_ENTRY_NAME"))) {
455 cxt->partlabel = xstrdup(data);
456 unhexmangle_string(cxt->partlabel);
457 }
458 if ((data = udev_device_get_property_value(dev, "ID_FS_TYPE")))
459 cxt->fstype = xstrdup(data);
460 if ((data = udev_device_get_property_value(dev, "ID_PART_ENTRY_TYPE")))
461 cxt->parttype = xstrdup(data);
462 if ((data = udev_device_get_property_value(dev, "ID_PART_ENTRY_UUID")))
463 cxt->partuuid = xstrdup(data);
464 if ((data = udev_device_get_property_value(dev, "ID_PART_ENTRY_FLAGS")))
465 cxt->partflags = xstrdup(data);
466 if ((data = udev_device_get_property_value(dev, "ID_WWN")))
467 cxt->wwn = xstrdup(data);
468 if ((data = udev_device_get_property_value(dev, "ID_SERIAL_SHORT")))
469 cxt->serial = xstrdup(data);
470 udev_device_unref(dev);
471 cxt->probed = 1;
472 }
473
474 return cxt->probed == 1 ? 0 : -1;
475
476 }
477 #endif /* HAVE_LIBUDEV */
478
479 static void probe_device(struct blkdev_cxt *cxt)
480 {
481 blkid_probe pr = NULL;
482
483 if (cxt->probed)
484 return;
485
486 if (!cxt->size)
487 return;
488
489 /* try udev DB */
490 if (get_udev_properties(cxt) == 0)
491 return; /* success */
492
493 cxt->probed = 1;
494
495 /* try libblkid (fallback) */
496 if (getuid() != 0)
497 return; /* no permissions to read from the device */
498
499 pr = blkid_new_probe_from_filename(cxt->filename);
500 if (!pr)
501 return;
502
503 blkid_probe_enable_superblocks(pr, 1);
504 blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_LABEL |
505 BLKID_SUBLKS_UUID |
506 BLKID_SUBLKS_TYPE);
507 blkid_probe_enable_partitions(pr, 1);
508 blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS);
509
510 if (!blkid_do_safeprobe(pr)) {
511 const char *data = NULL;
512
513 if (!blkid_probe_lookup_value(pr, "TYPE", &data, NULL))
514 cxt->fstype = xstrdup(data);
515 if (!blkid_probe_lookup_value(pr, "UUID", &data, NULL))
516 cxt->uuid = xstrdup(data);
517 if (!blkid_probe_lookup_value(pr, "LABEL", &data, NULL))
518 cxt->label = xstrdup(data);
519 if (!blkid_probe_lookup_value(pr, "PART_ENTRY_TYPE", &data, NULL))
520 cxt->parttype = xstrdup(data);
521 if (!blkid_probe_lookup_value(pr, "PART_ENTRY_UUID", &data, NULL))
522 cxt->partuuid = xstrdup(data);
523 if (!blkid_probe_lookup_value(pr, "PART_ENTRY_NAME", &data, NULL))
524 cxt->partlabel = xstrdup(data);
525 if (!blkid_probe_lookup_value(pr, "PART_ENTRY_FLAGS", &data, NULL))
526 cxt->partflags = xstrdup(data);
527 }
528
529 blkid_free_probe(pr);
530 return;
531 }
532
533 static int is_readonly_device(struct blkdev_cxt *cxt)
534 {
535 int fd, ro = 0;
536
537 if (sysfs_scanf(&cxt->sysfs, "ro", "%d", &ro) == 1)
538 return ro;
539
540 /* fallback if "ro" attribute does not exist */
541 fd = open(cxt->filename, O_RDONLY);
542 if (fd != -1) {
543 if (ioctl(fd, BLKROGET, &ro) != 0)
544 ro = 0;
545 close(fd);
546 }
547 return ro;
548 }
549
550 static char *get_scheduler(struct blkdev_cxt *cxt)
551 {
552 char *str = sysfs_strdup(&cxt->sysfs, "queue/scheduler");
553 char *p, *res = NULL;
554
555 if (!str)
556 return NULL;
557 p = strchr(str, '[');
558 if (p) {
559 res = p + 1;
560 p = strchr(res, ']');
561 if (p) {
562 *p = '\0';
563 res = xstrdup(res);
564 } else
565 res = NULL;
566 }
567 free(str);
568 return res;
569 }
570
571 static char *get_type(struct blkdev_cxt *cxt)
572 {
573 char *res = NULL, *p;
574
575 if (is_dm(cxt->name)) {
576 char *dm_uuid = sysfs_strdup(&cxt->sysfs, "dm/uuid");
577
578 /* The DM_UUID prefix should be set to subsystem owning
579 * the device - LVM, CRYPT, DMRAID, MPATH, PART */
580 if (dm_uuid) {
581 char *tmp = dm_uuid;
582 char *dm_uuid_prefix = strsep(&tmp, "-");
583
584 if (dm_uuid_prefix) {
585 /* kpartx hack to remove partition number */
586 if (strncasecmp(dm_uuid_prefix, "part", 4) == 0)
587 dm_uuid_prefix[4] = '\0';
588
589 res = xstrdup(dm_uuid_prefix);
590 }
591 }
592
593 free(dm_uuid);
594 if (!res)
595 /* No UUID or no prefix - just mark it as DM device */
596 res = xstrdup("dm");
597
598 } else if (!strncmp(cxt->name, "loop", 4)) {
599 res = xstrdup("loop");
600
601 } else if (!strncmp(cxt->name, "md", 2)) {
602 char *md_level = sysfs_strdup(&cxt->sysfs, "md/level");
603 res = md_level ? md_level : xstrdup("md");
604
605 } else {
606 const char *type = NULL;
607 int x = 0;
608
609 if (!sysfs_read_int(&cxt->sysfs, "device/type", &x))
610 type = blkdev_scsi_type_to_name(x);
611
612 if (!type)
613 type = cxt->partition ? "part" : "disk";
614 res = xstrdup(type);
615 }
616
617 for (p = res; p && *p; p++)
618 *p = tolower((unsigned char) *p);
619 return res;
620 }
621
622 /* Thanks to lsscsi code for idea of detection logic used here */
623 static char *get_transport(struct blkdev_cxt *cxt)
624 {
625 struct sysfs_cxt *sysfs = &cxt->sysfs;
626 char *attr = NULL;
627 const char *trans = NULL;
628
629 /* SCSI - Serial Peripheral Interface */
630 if (sysfs_scsi_host_is(sysfs, "spi"))
631 trans = "spi";
632
633 /* FC/FCoE - Fibre Channel / Fibre Channel over Ethernet */
634 else if (sysfs_scsi_host_is(sysfs, "fc")) {
635 attr = sysfs_scsi_host_strdup_attribute(sysfs, "fc", "symbolic_name");
636 if (!attr)
637 return NULL;
638 trans = strstr(attr, " over ") ? "fcoe" : "fc";
639 free(attr);
640 }
641
642 /* SAS - Serial Attached SCSI */
643 else if (sysfs_scsi_host_is(sysfs, "sas") ||
644 sysfs_scsi_has_attribute(sysfs, "sas_device"))
645 trans = "sas";
646
647
648 /* SBP - Serial Bus Protocol (FireWire) */
649 else if (sysfs_scsi_has_attribute(sysfs, "ieee1394_id"))
650 trans = "sbp";
651
652 /* iSCSI */
653 else if (sysfs_scsi_host_is(sysfs, "iscsi"))
654 trans ="iscsi";
655
656 /* USB - Universal Serial Bus */
657 else if (sysfs_scsi_path_contains(sysfs, "usb"))
658 trans = "usb";
659
660 /* ATA, SATA */
661 else if (sysfs_scsi_host_is(sysfs, "scsi")) {
662 attr = sysfs_scsi_host_strdup_attribute(sysfs, "scsi", "proc_name");
663 if (!attr)
664 return NULL;
665 if (!strncmp(attr, "ahci", 4) || !strncmp(attr, "sata", 4))
666 trans = "sata";
667 else if (strstr(attr, "ata"))
668 trans = "ata";
669 free(attr);
670 }
671
672 return trans ? xstrdup(trans) : NULL;
673 }
674
675 #define is_parsable(_l) (scols_table_is_raw((_l)->table) || \
676 scols_table_is_export((_l)->table))
677
678 static char *mk_name(const char *name)
679 {
680 char *p;
681 if (!name)
682 return NULL;
683 if (lsblk->paths)
684 xasprintf(&p, "/dev/%s", name);
685 else
686 p = xstrdup(name);
687 return p;
688 }
689
690 static char *mk_dm_name(const char *name)
691 {
692 char *p;
693 if (!name)
694 return NULL;
695 if (lsblk->paths)
696 xasprintf(&p, "/dev/mapper/%s", name);
697 else
698 p = xstrdup(name);
699 return p;
700 }
701
702 static void set_scols_data(struct blkdev_cxt *cxt, int col, int id, struct libscols_line *ln)
703 {
704 int st_rc = 0;
705 char *str = NULL;
706
707 if (!cxt->st.st_rdev && (id == COL_OWNER || id == COL_GROUP ||
708 id == COL_MODE))
709 st_rc = stat(cxt->filename, &cxt->st);
710
711 switch(id) {
712 case COL_NAME:
713 str = cxt->dm_name ? mk_dm_name(cxt->dm_name) : mk_name(cxt->name);
714 break;
715 case COL_KNAME:
716 str = mk_name(cxt->name);
717 break;
718 case COL_PKNAME:
719 if (cxt->parent)
720 str = mk_name(cxt->parent->name);
721 break;
722 case COL_OWNER:
723 {
724 struct passwd *pw = st_rc ? NULL : getpwuid(cxt->st.st_uid);
725 if (pw)
726 str = xstrdup(pw->pw_name);
727 break;
728 }
729 case COL_GROUP:
730 {
731 struct group *gr = st_rc ? NULL : getgrgid(cxt->st.st_gid);
732 if (gr)
733 str = xstrdup(gr->gr_name);
734 break;
735 }
736 case COL_MODE:
737 {
738 char md[11];
739
740 if (!st_rc) {
741 strmode(cxt->st.st_mode, md);
742 str = xstrdup(md);
743 }
744 break;
745 }
746 case COL_MAJMIN:
747 if (is_parsable(lsblk))
748 xasprintf(&str, "%u:%u", cxt->maj, cxt->min);
749 else
750 xasprintf(&str, "%3u:%-3u", cxt->maj, cxt->min);
751 break;
752 case COL_FSTYPE:
753 probe_device(cxt);
754 if (cxt->fstype)
755 str = xstrdup(cxt->fstype);
756 break;
757 case COL_TARGET:
758 if (!(cxt->nholders + cxt->npartitions))
759 str = get_device_mountpoint(cxt);
760 break;
761 case COL_LABEL:
762 probe_device(cxt);
763 if (cxt->label)
764 str = xstrdup(cxt->label);
765 break;
766 case COL_UUID:
767 probe_device(cxt);
768 if (cxt->uuid)
769 str = xstrdup(cxt->uuid);
770 break;
771 case COL_PARTTYPE:
772 probe_device(cxt);
773 if (cxt->parttype)
774 str = xstrdup(cxt->parttype);
775 break;
776 case COL_PARTLABEL:
777 probe_device(cxt);
778 if (cxt->partlabel)
779 str = xstrdup(cxt->partlabel);
780 break;
781 case COL_PARTUUID:
782 probe_device(cxt);
783 if (cxt->partuuid)
784 str = xstrdup(cxt->partuuid);
785 break;
786 case COL_PARTFLAGS:
787 probe_device(cxt);
788 if (cxt->partflags)
789 str = xstrdup(cxt->partflags);
790 break;
791 case COL_WWN:
792 get_udev_properties(cxt);
793 if (cxt->wwn)
794 str = xstrdup(cxt->wwn);
795 break;
796 case COL_RA:
797 str = sysfs_strdup(&cxt->sysfs, "queue/read_ahead_kb");
798 break;
799 case COL_RO:
800 str = xstrdup(is_readonly_device(cxt) ? "1" : "0");
801 break;
802 case COL_RM:
803 str = sysfs_strdup(&cxt->sysfs, "removable");
804 if (!str && cxt->sysfs.parent)
805 str = sysfs_strdup(cxt->sysfs.parent, "removable");
806 break;
807 case COL_ROTA:
808 str = sysfs_strdup(&cxt->sysfs, "queue/rotational");
809 break;
810 case COL_RAND:
811 str = sysfs_strdup(&cxt->sysfs, "queue/add_random");
812 break;
813 case COL_MODEL:
814 if (!cxt->partition && cxt->nslaves == 0)
815 str = sysfs_strdup(&cxt->sysfs, "device/model");
816 break;
817 case COL_SERIAL:
818 if (!cxt->partition && cxt->nslaves == 0) {
819 get_udev_properties(cxt);
820 if (cxt->serial)
821 str = xstrdup(cxt->serial);
822 }
823 break;
824 case COL_REV:
825 if (!cxt->partition && cxt->nslaves == 0)
826 str = sysfs_strdup(&cxt->sysfs, "device/rev");
827 break;
828 case COL_VENDOR:
829 if (!cxt->partition && cxt->nslaves == 0)
830 str = sysfs_strdup(&cxt->sysfs, "device/vendor");
831 break;
832 case COL_SIZE:
833 if (!cxt->size)
834 break;
835 if (lsblk->bytes)
836 xasprintf(&str, "%jd", cxt->size);
837 else
838 str = size_to_human_string(SIZE_SUFFIX_1LETTER, cxt->size);
839 break;
840 case COL_STATE:
841 if (!cxt->partition && !cxt->dm_name)
842 str = sysfs_strdup(&cxt->sysfs, "device/state");
843 else if (cxt->dm_name) {
844 int x = 0;
845 if (sysfs_read_int(&cxt->sysfs, "dm/suspended", &x) == 0)
846 str = xstrdup(x ? "suspended" : "running");
847 }
848 break;
849 case COL_ALIOFF:
850 str = sysfs_strdup(&cxt->sysfs, "alignment_offset");
851 break;
852 case COL_MINIO:
853 str = sysfs_strdup(&cxt->sysfs, "queue/minimum_io_size");
854 break;
855 case COL_OPTIO:
856 str = sysfs_strdup(&cxt->sysfs, "queue/optimal_io_size");
857 break;
858 case COL_PHYSEC:
859 str = sysfs_strdup(&cxt->sysfs, "queue/physical_block_size");
860 break;
861 case COL_LOGSEC:
862 str = sysfs_strdup(&cxt->sysfs, "queue/logical_block_size");
863 break;
864 case COL_SCHED:
865 str = get_scheduler(cxt);
866 break;
867 case COL_RQ_SIZE:
868 str = sysfs_strdup(&cxt->sysfs, "queue/nr_requests");
869 break;
870 case COL_TYPE:
871 str = get_type(cxt);
872 break;
873 case COL_HCTL:
874 {
875 int h, c, t, l;
876 if (sysfs_scsi_get_hctl(&cxt->sysfs, &h, &c, &t, &l) == 0)
877 xasprintf(&str, "%d:%d:%d:%d", h, c, t, l);
878 break;
879 }
880 case COL_TRANSPORT:
881 str = get_transport(cxt);
882 break;
883 case COL_DALIGN:
884 if (cxt->discard)
885 str = sysfs_strdup(&cxt->sysfs, "discard_alignment");
886 if (!str)
887 str = xstrdup("0");
888 break;
889 case COL_DGRAN:
890 if (lsblk->bytes)
891 str = sysfs_strdup(&cxt->sysfs, "queue/discard_granularity");
892 else {
893 uint64_t x;
894 if (sysfs_read_u64(&cxt->sysfs,
895 "queue/discard_granularity", &x) == 0)
896 str = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
897 }
898 break;
899 case COL_DMAX:
900 if (lsblk->bytes)
901 str = sysfs_strdup(&cxt->sysfs, "queue/discard_max_bytes");
902 else {
903 uint64_t x;
904 if (sysfs_read_u64(&cxt->sysfs,
905 "queue/discard_max_bytes", &x) == 0)
906 str = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
907 }
908 break;
909 case COL_DZERO:
910 if (cxt->discard)
911 str = sysfs_strdup(&cxt->sysfs, "queue/discard_zeroes_data");
912 if (!str)
913 str = xstrdup("0");
914 break;
915 case COL_WSAME:
916 if (lsblk->bytes)
917 str = sysfs_strdup(&cxt->sysfs, "queue/write_same_max_bytes");
918 else {
919 uint64_t x;
920
921 if (sysfs_read_u64(&cxt->sysfs,
922 "queue/write_same_max_bytes", &x) == 0)
923 str = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
924 }
925 if (!str)
926 str = xstrdup("0");
927 break;
928 };
929
930 if (str)
931 scols_line_refer_data(ln, col, str);
932 }
933
934 static void print_device(struct blkdev_cxt *cxt, struct libscols_line *scols_parent)
935 {
936 int i;
937
938 cxt->scols_line = scols_table_new_line(lsblk->table, scols_parent);
939 if (!cxt->scols_line)
940 return;
941
942 for (i = 0; i < ncolumns; i++)
943 set_scols_data(cxt, i, get_column_id(i), cxt->scols_line);
944 }
945
946 static int set_cxt(struct blkdev_cxt *cxt,
947 struct blkdev_cxt *parent,
948 struct blkdev_cxt *wholedisk,
949 const char *name)
950 {
951 dev_t devno;
952
953 cxt->parent = parent;
954 cxt->name = xstrdup(name);
955 cxt->partition = wholedisk != NULL;
956
957 cxt->filename = get_device_path(cxt);
958 if (!cxt->filename) {
959 warnx(_("%s: failed to get device path"), name);
960 return -1;
961 }
962
963 devno = sysfs_devname_to_devno(name, wholedisk ? wholedisk->name : NULL);
964
965 if (!devno) {
966 warnx(_("%s: unknown device name"), name);
967 return -1;
968 }
969
970 if (lsblk->inverse) {
971 if (sysfs_init(&cxt->sysfs, devno, wholedisk ? &wholedisk->sysfs : NULL)) {
972 warnx(_("%s: failed to initialize sysfs handler"), name);
973 return -1;
974 }
975 if (parent)
976 parent->sysfs.parent = &cxt->sysfs;
977 } else {
978 if (sysfs_init(&cxt->sysfs, devno, parent ? &parent->sysfs : NULL)) {
979 warnx(_("%s: failed to initialize sysfs handler"), name);
980 return -1;
981 }
982 }
983
984 cxt->maj = major(devno);
985 cxt->min = minor(devno);
986 cxt->size = 0;
987
988 if (sysfs_read_u64(&cxt->sysfs, "size", &cxt->size) == 0) /* in sectors */
989 cxt->size <<= 9; /* in bytes */
990
991 if (sysfs_read_int(&cxt->sysfs,
992 "queue/discard_granularity", &cxt->discard) != 0)
993 cxt->discard = 0;
994
995 /* Ignore devices of zero size */
996 if (!lsblk->all_devices && cxt->size == 0)
997 return -1;
998
999 if (is_dm(name)) {
1000 cxt->dm_name = sysfs_strdup(&cxt->sysfs, "dm/name");
1001 if (!cxt->dm_name) {
1002 warnx(_("%s: failed to get dm name"), name);
1003 return -1;
1004 }
1005 }
1006
1007 cxt->npartitions = sysfs_count_partitions(&cxt->sysfs, name);
1008 cxt->nholders = sysfs_count_dirents(&cxt->sysfs, "holders");
1009 cxt->nslaves = sysfs_count_dirents(&cxt->sysfs, "slaves");
1010
1011 /* ignore non-SCSI devices */
1012 if (lsblk->scsi && sysfs_scsi_get_hctl(&cxt->sysfs, NULL, NULL, NULL, NULL))
1013 return -1;
1014
1015 return 0;
1016 }
1017
1018 static int process_blkdev(struct blkdev_cxt *cxt, struct blkdev_cxt *parent,
1019 int do_partitions, const char *part_name);
1020
1021 /*
1022 * List device partitions if any.
1023 */
1024 static int list_partitions(struct blkdev_cxt *wholedisk_cxt, struct blkdev_cxt *parent_cxt,
1025 const char *part_name)
1026 {
1027 DIR *dir;
1028 struct dirent *d;
1029 struct blkdev_cxt part_cxt = {};
1030 int r = -1;
1031
1032 assert(wholedisk_cxt);
1033
1034 /*
1035 * Do not process further if there are no partitions for
1036 * this device or the device itself is a partition.
1037 */
1038 if (!wholedisk_cxt->npartitions || wholedisk_cxt->partition)
1039 return -1;
1040
1041 dir = sysfs_opendir(&wholedisk_cxt->sysfs, NULL);
1042 if (!dir)
1043 err(EXIT_FAILURE, _("failed to open device directory in sysfs"));
1044
1045 while ((d = xreaddir(dir))) {
1046 /* Process particular partition only? */
1047 if (part_name && strcmp(part_name, d->d_name))
1048 continue;
1049
1050 if (!(sysfs_is_partition_dirent(dir, d, wholedisk_cxt->name)))
1051 continue;
1052
1053 if (lsblk->inverse) {
1054 /*
1055 * <parent_cxt>
1056 * `-<part_cxt>
1057 * `-<wholedisk_cxt>
1058 * `-...
1059 */
1060 if (set_cxt(&part_cxt, parent_cxt, wholedisk_cxt, d->d_name))
1061 goto next;
1062
1063 if (!parent_cxt && part_cxt.nholders)
1064 goto next;
1065
1066 wholedisk_cxt->parent = &part_cxt;
1067 print_device(&part_cxt, parent_cxt ? parent_cxt->scols_line : NULL);
1068 if (!lsblk->nodeps)
1069 process_blkdev(wholedisk_cxt, &part_cxt, 0, NULL);
1070 } else {
1071 /*
1072 * <parent_cxt>
1073 * `-<wholedisk_cxt>
1074 * `-<part_cxt>
1075 * `-...
1076 */
1077 int ps = set_cxt(&part_cxt, wholedisk_cxt, wholedisk_cxt, d->d_name);
1078
1079 /* Print whole disk only once */
1080 if (r)
1081 print_device(wholedisk_cxt, parent_cxt ? parent_cxt->scols_line : NULL);
1082 if (ps == 0 && !lsblk->nodeps)
1083 process_blkdev(&part_cxt, wholedisk_cxt, 0, NULL);
1084 }
1085 next:
1086 reset_blkdev_cxt(&part_cxt);
1087 r = 0;
1088 }
1089
1090 closedir(dir);
1091 return r;
1092 }
1093
1094 static int get_wholedisk_from_partition_dirent(DIR *dir, const char *dirname,
1095 struct dirent *d, struct blkdev_cxt *cxt)
1096 {
1097 char path[PATH_MAX];
1098 char *p;
1099 int len;
1100
1101 if ((len = readlink_at(dirfd(dir), dirname,
1102 d->d_name, path, sizeof(path) - 1)) < 0)
1103 return 0;
1104
1105 path[len] = '\0';
1106
1107 /* The path ends with ".../<device>/<partition>" */
1108 p = strrchr(path, '/');
1109 if (!p)
1110 return 0;
1111 *p = '\0';
1112
1113 p = strrchr(path, '/');
1114 if (!p)
1115 return 0;
1116 p++;
1117
1118 return set_cxt(cxt, NULL, NULL, p);
1119 }
1120
1121 /*
1122 * List device dependencies: partitions, holders (inverse = 0) or slaves (inverse = 1).
1123 */
1124 static int list_deps(struct blkdev_cxt *cxt)
1125 {
1126 DIR *dir;
1127 struct dirent *d;
1128 struct blkdev_cxt dep = {};
1129 char dirname[PATH_MAX];
1130 const char *depname;
1131
1132 assert(cxt);
1133
1134 if (lsblk->nodeps)
1135 return 0;
1136
1137 if (!(lsblk->inverse ? cxt->nslaves : cxt->nholders))
1138 return 0;
1139
1140 depname = lsblk->inverse ? "slaves" : "holders";
1141 dir = sysfs_opendir(&cxt->sysfs, depname);
1142 if (!dir)
1143 return 0;
1144
1145 snprintf(dirname, sizeof(dirname), "%s/%s", cxt->sysfs.dir_path, depname);
1146
1147 while ((d = xreaddir(dir))) {
1148 /* Is the dependency a partition? */
1149 if (sysfs_is_partition_dirent(dir, d, NULL)) {
1150 if (!get_wholedisk_from_partition_dirent(dir, dirname, d, &dep))
1151 process_blkdev(&dep, cxt, 1, d->d_name);
1152 }
1153 /* The dependency is a whole device. */
1154 else if (!set_cxt(&dep, cxt, NULL, d->d_name))
1155 process_blkdev(&dep, cxt, 1, NULL);
1156
1157 reset_blkdev_cxt(&dep);
1158 }
1159 closedir(dir);
1160
1161 return 0;
1162 }
1163
1164 static int process_blkdev(struct blkdev_cxt *cxt, struct blkdev_cxt *parent,
1165 int do_partitions, const char *part_name)
1166 {
1167 if (do_partitions && cxt->npartitions)
1168 return list_partitions(cxt, parent, part_name);
1169
1170 print_device(cxt, parent ? parent->scols_line : NULL);
1171 return list_deps(cxt);
1172 }
1173
1174 /* Iterate devices in sysfs */
1175 static int iterate_block_devices(void)
1176 {
1177 DIR *dir;
1178 struct dirent *d;
1179 struct blkdev_cxt cxt = {};
1180
1181 if (!(dir = opendir(_PATH_SYS_BLOCK)))
1182 return EXIT_FAILURE;
1183
1184 while ((d = xreaddir(dir))) {
1185 if (set_cxt(&cxt, NULL, NULL, d->d_name))
1186 goto next;
1187
1188 if (is_maj_excluded(cxt.maj) || !is_maj_included(cxt.maj))
1189 goto next;
1190
1191 /* Skip devices in the middle of dependency tree. */
1192 if ((lsblk->inverse ? cxt.nholders : cxt.nslaves) > 0)
1193 goto next;
1194
1195 process_blkdev(&cxt, NULL, 1, NULL);
1196 next:
1197 reset_blkdev_cxt(&cxt);
1198 }
1199
1200 closedir(dir);
1201
1202 return EXIT_SUCCESS;
1203 }
1204
1205 static char *devno_to_sysfs_name(dev_t devno, char *devname, char *buf, size_t buf_size)
1206 {
1207 char path[PATH_MAX];
1208 ssize_t len;
1209
1210 if (!sysfs_devno_path(devno, path, sizeof(path))) {
1211 warn(_("%s: failed to compose sysfs path"), devname);
1212 return NULL;
1213 }
1214
1215 len = readlink(path, buf, buf_size - 1);
1216 if (len < 0) {
1217 warn(_("%s: failed to read link"), path);
1218 return NULL;
1219 }
1220 buf[len] = '\0';
1221
1222 return xstrdup(strrchr(buf, '/') + 1);
1223 }
1224
1225 static int process_one_device(char *devname)
1226 {
1227 struct blkdev_cxt parent = {}, cxt = {};
1228 struct stat st;
1229 char buf[PATH_MAX + 1], *name, *diskname = NULL;
1230 dev_t disk = 0;
1231 int real_part = 0;
1232 int status = EXIT_FAILURE;
1233
1234 if (stat(devname, &st) || !S_ISBLK(st.st_mode)) {
1235 warnx(_("%s: not a block device"), devname);
1236 return EXIT_FAILURE;
1237 }
1238
1239 if (!(name = devno_to_sysfs_name(st.st_rdev, devname, buf, PATH_MAX))) {
1240 warn(_("%s: failed to get sysfs name"), devname);
1241 return EXIT_FAILURE;
1242 }
1243
1244 if (!strncmp(name, "dm-", 3)) {
1245 /* dm mapping is never a real partition! */
1246 real_part = 0;
1247 } else {
1248 if (blkid_devno_to_wholedisk(st.st_rdev, buf, sizeof(buf), &disk)) {
1249 warn(_("%s: failed to get whole-disk device number"), devname);
1250 return EXIT_FAILURE;
1251 }
1252 diskname = buf;
1253 real_part = st.st_rdev != disk;
1254 }
1255
1256 if (!real_part) {
1257 /*
1258 * Device is not a partition.
1259 */
1260 if (set_cxt(&cxt, NULL, NULL, name))
1261 goto leave;
1262 process_blkdev(&cxt, NULL, !lsblk->inverse, NULL);
1263 } else {
1264 /*
1265 * Partition, read sysfs name of the device.
1266 */
1267 if (set_cxt(&parent, NULL, NULL, diskname))
1268 goto leave;
1269 if (set_cxt(&cxt, &parent, &parent, name))
1270 goto leave;
1271
1272 if (lsblk->inverse)
1273 process_blkdev(&parent, &cxt, 1, cxt.name);
1274 else
1275 process_blkdev(&cxt, &parent, 1, NULL);
1276 }
1277
1278 status = EXIT_SUCCESS;
1279 leave:
1280 free(name);
1281 reset_blkdev_cxt(&cxt);
1282
1283 if (real_part)
1284 reset_blkdev_cxt(&parent);
1285
1286 return status;
1287 }
1288
1289 static void parse_excludes(const char *str0)
1290 {
1291 const char *str = str0;
1292
1293 while (str && *str) {
1294 char *end = NULL;
1295 unsigned long n;
1296
1297 errno = 0;
1298 n = strtoul(str, &end, 10);
1299
1300 if (end == str || (end && *end && *end != ','))
1301 errx(EXIT_FAILURE, _("failed to parse list '%s'"), str0);
1302 if (errno != 0 && (n == ULONG_MAX || n == 0))
1303 err(EXIT_FAILURE, _("failed to parse list '%s'"), str0);
1304 excludes[nexcludes++] = n;
1305
1306 if (nexcludes == ARRAY_SIZE(excludes))
1307 /* TRANSLATORS: The standard value for %d is 256. */
1308 errx(EXIT_FAILURE, _("the list of excluded devices is "
1309 "too large (limit is %d devices)"),
1310 (int)ARRAY_SIZE(excludes));
1311
1312 str = end && *end ? end + 1 : NULL;
1313 }
1314 }
1315
1316 static void parse_includes(const char *str0)
1317 {
1318 const char *str = str0;
1319
1320 while (str && *str) {
1321 char *end = NULL;
1322 unsigned long n;
1323
1324 errno = 0;
1325 n = strtoul(str, &end, 10);
1326
1327 if (end == str || (end && *end && *end != ','))
1328 errx(EXIT_FAILURE, _("failed to parse list '%s'"), str0);
1329 if (errno != 0 && (n == ULONG_MAX || n == 0))
1330 err(EXIT_FAILURE, _("failed to parse list '%s'"), str0);
1331 includes[nincludes++] = n;
1332
1333 if (nincludes == ARRAY_SIZE(includes))
1334 /* TRANSLATORS: The standard value for %d is 256. */
1335 errx(EXIT_FAILURE, _("the list of included devices is "
1336 "too large (limit is %d devices)"),
1337 (int)ARRAY_SIZE(includes));
1338 str = end && *end ? end + 1 : NULL;
1339 }
1340 }
1341
1342 static void __attribute__((__noreturn__)) help(FILE *out)
1343 {
1344 size_t i;
1345
1346 fputs(USAGE_HEADER, out);
1347 fprintf(out, _(" %s [options] [<device> ...]\n"), program_invocation_short_name);
1348 fputs(USAGE_OPTIONS, out);
1349 fputs(_(" -a, --all print all devices\n"), out);
1350 fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out);
1351 fputs(_(" -d, --nodeps don't print slaves or holders\n"), out);
1352 fputs(_(" -D, --discard print discard capabilities\n"), out);
1353 fputs(_(" -e, --exclude <list> exclude devices by major number (default: RAM disks)\n"), out);
1354 fputs(_(" -f, --fs output info about filesystems\n"), out);
1355 fputs(_(" -i, --ascii use ascii characters only\n"), out);
1356 fputs(_(" -I, --include <list> show only devices with specified major numbers\n"), out);
1357 fputs(_(" -l, --list use list format output\n"), out);
1358 fputs(_(" -m, --perms output info about permissions\n"), out);
1359 fputs(_(" -n, --noheadings don't print headings\n"), out);
1360 fputs(_(" -o, --output <list> output columns\n"), out);
1361 fputs(_(" -p, --paths print complete device path\n"), out);
1362 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out);
1363 fputs(_(" -r, --raw use raw output format\n"), out);
1364 fputs(_(" -s, --inverse inverse dependencies\n"), out);
1365 fputs(_(" -S, --scsi output info about SCSI devices\n"), out);
1366 fputs(_(" -t, --topology output info about topology\n"), out);
1367 fputs(USAGE_SEPARATOR, out);
1368 fputs(USAGE_HELP, out);
1369 fputs(USAGE_VERSION, out);
1370
1371 fprintf(out, _("\nAvailable columns (for --output):\n"));
1372
1373 for (i = 0; i < NCOLS; i++)
1374 fprintf(out, " %11s %s\n", infos[i].name, _(infos[i].help));
1375
1376 fprintf(out, USAGE_MAN_TAIL("lsblk(8)"));
1377
1378 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
1379 }
1380
1381 static void check_sysdevblock(void)
1382 {
1383 if (access(_PATH_SYS_DEVBLOCK, R_OK) != 0)
1384 err(EXIT_FAILURE, _("failed to access sysfs directory: %s"),
1385 _PATH_SYS_DEVBLOCK);
1386 }
1387
1388 int main(int argc, char *argv[])
1389 {
1390 struct lsblk _ls;
1391 int scols_flags = LSBLK_TREE;
1392 int i, c, status = EXIT_FAILURE;
1393 char *outarg = NULL;
1394
1395 static const struct option longopts[] = {
1396 { "all", 0, 0, 'a' },
1397 { "bytes", 0, 0, 'b' },
1398 { "nodeps", 0, 0, 'd' },
1399 { "discard", 0, 0, 'D' },
1400 { "help", 0, 0, 'h' },
1401 { "output", 1, 0, 'o' },
1402 { "perms", 0, 0, 'm' },
1403 { "noheadings", 0, 0, 'n' },
1404 { "list", 0, 0, 'l' },
1405 { "ascii", 0, 0, 'i' },
1406 { "raw", 0, 0, 'r' },
1407 { "inverse", 0, 0, 's' },
1408 { "fs", 0, 0, 'f' },
1409 { "exclude", 1, 0, 'e' },
1410 { "include", 1, 0, 'I' },
1411 { "topology", 0, 0, 't' },
1412 { "paths", 0, 0, 'p' },
1413 { "pairs", 0, 0, 'P' },
1414 { "scsi", 0, 0, 'S' },
1415 { "version", 0, 0, 'V' },
1416 { NULL, 0, 0, 0 },
1417 };
1418
1419 static const ul_excl_t excl[] = { /* rows and cols in in ASCII order */
1420 { 'I','e' },
1421 { 'P','l','r' },
1422 { 0 }
1423 };
1424 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
1425
1426 setlocale(LC_ALL, "");
1427 bindtextdomain(PACKAGE, LOCALEDIR);
1428 textdomain(PACKAGE);
1429 atexit(close_stdout);
1430
1431 lsblk = &_ls;
1432 memset(lsblk, 0, sizeof(*lsblk));
1433
1434 while((c = getopt_long(argc, argv,
1435 "abdDe:fhlnmo:pPiI:rstVS", longopts, NULL)) != -1) {
1436
1437 err_exclusive_options(c, longopts, excl, excl_st);
1438
1439 switch(c) {
1440 case 'a':
1441 lsblk->all_devices = 1;
1442 break;
1443 case 'b':
1444 lsblk->bytes = 1;
1445 break;
1446 case 'd':
1447 lsblk->nodeps = 1;
1448 break;
1449 case 'D':
1450 columns[ncolumns++] = COL_NAME;
1451 columns[ncolumns++] = COL_DALIGN;
1452 columns[ncolumns++] = COL_DGRAN;
1453 columns[ncolumns++] = COL_DMAX;
1454 columns[ncolumns++] = COL_DZERO;
1455 break;
1456 case 'e':
1457 parse_excludes(optarg);
1458 break;
1459 case 'h':
1460 help(stdout);
1461 break;
1462 case 'l':
1463 scols_flags &= ~LSBLK_TREE; /* disable the default */
1464 break;
1465 case 'n':
1466 scols_flags |= LSBLK_NOHEADINGS;
1467 break;
1468 case 'o':
1469 outarg = optarg;
1470 break;
1471 case 'p':
1472 lsblk->paths = 1;
1473 break;
1474 case 'P':
1475 scols_flags |= LSBLK_EXPORT;
1476 scols_flags &= ~LSBLK_TREE; /* disable the default */
1477 break;
1478 case 'i':
1479 scols_flags |= LSBLK_ASCII;
1480 break;
1481 case 'I':
1482 parse_includes(optarg);
1483 break;
1484 case 'r':
1485 scols_flags &= ~LSBLK_TREE; /* disable the default */
1486 scols_flags |= LSBLK_RAW; /* enable raw */
1487 break;
1488 case 's':
1489 lsblk->inverse = 1;
1490 break;
1491 case 'f':
1492 columns[ncolumns++] = COL_NAME;
1493 columns[ncolumns++] = COL_FSTYPE;
1494 columns[ncolumns++] = COL_LABEL;
1495 columns[ncolumns++] = COL_UUID;
1496 columns[ncolumns++] = COL_TARGET;
1497 break;
1498 case 'm':
1499 columns[ncolumns++] = COL_NAME;
1500 columns[ncolumns++] = COL_SIZE;
1501 columns[ncolumns++] = COL_OWNER;
1502 columns[ncolumns++] = COL_GROUP;
1503 columns[ncolumns++] = COL_MODE;
1504 break;
1505 case 't':
1506 columns[ncolumns++] = COL_NAME;
1507 columns[ncolumns++] = COL_ALIOFF;
1508 columns[ncolumns++] = COL_MINIO;
1509 columns[ncolumns++] = COL_OPTIO;
1510 columns[ncolumns++] = COL_PHYSEC;
1511 columns[ncolumns++] = COL_LOGSEC;
1512 columns[ncolumns++] = COL_ROTA;
1513 columns[ncolumns++] = COL_SCHED;
1514 columns[ncolumns++] = COL_RQ_SIZE;
1515 columns[ncolumns++] = COL_RA;
1516 columns[ncolumns++] = COL_WSAME;
1517 break;
1518 case 'S':
1519 lsblk->nodeps = 1;
1520 lsblk->scsi = 1;
1521 columns[ncolumns++] = COL_NAME;
1522 columns[ncolumns++] = COL_HCTL;
1523 columns[ncolumns++] = COL_TYPE;
1524 columns[ncolumns++] = COL_VENDOR;
1525 columns[ncolumns++] = COL_MODEL;
1526 columns[ncolumns++] = COL_REV;
1527 columns[ncolumns++] = COL_TRANSPORT;
1528 break;
1529 case 'V':
1530 printf(UTIL_LINUX_VERSION);
1531 return EXIT_SUCCESS;
1532 default:
1533 help(stderr);
1534 }
1535 }
1536
1537 check_sysdevblock();
1538
1539 if (!ncolumns) {
1540 columns[ncolumns++] = COL_NAME;
1541 columns[ncolumns++] = COL_MAJMIN;
1542 columns[ncolumns++] = COL_RM;
1543 columns[ncolumns++] = COL_SIZE;
1544 columns[ncolumns++] = COL_RO;
1545 columns[ncolumns++] = COL_TYPE;
1546 columns[ncolumns++] = COL_TARGET;
1547 }
1548
1549 if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns),
1550 &ncolumns, column_name_to_id) < 0)
1551 return EXIT_FAILURE;
1552
1553 if (nexcludes == 0 && nincludes == 0)
1554 excludes[nexcludes++] = 1; /* default: ignore RAM disks */
1555
1556 mnt_init_debug(0);
1557
1558 /*
1559 * initialize output columns
1560 */
1561 if (!(lsblk->table = scols_new_table()))
1562 errx(EXIT_FAILURE, _("failed to initialize output table"));
1563 scols_table_enable_raw(lsblk->table, !!(scols_flags & LSBLK_RAW));
1564 scols_table_enable_export(lsblk->table, !!(scols_flags & LSBLK_EXPORT));
1565 scols_table_enable_ascii(lsblk->table, !!(scols_flags & LSBLK_ASCII));
1566 scols_table_enable_noheadings(lsblk->table, !!(scols_flags & LSBLK_NOHEADINGS));
1567
1568 for (i = 0; i < ncolumns; i++) {
1569 struct colinfo *ci = get_column_info(i);
1570 int fl = ci->flags;
1571
1572 if (!(scols_flags & LSBLK_TREE) && get_column_id(i) == COL_NAME)
1573 fl &= ~SCOLS_FL_TREE;
1574
1575 if (!scols_table_new_column(lsblk->table, ci->name, ci->whint, fl)) {
1576 warn(_("failed to initialize output column"));
1577 goto leave;
1578 }
1579 }
1580
1581 if (optind == argc)
1582 status = iterate_block_devices();
1583 else while (optind < argc)
1584 status = process_one_device(argv[optind++]);
1585
1586 scols_print_table(lsblk->table);
1587
1588 leave:
1589 scols_unref_table(lsblk->table);
1590
1591 mnt_unref_table(mtab);
1592 mnt_unref_table(swaps);
1593 mnt_unref_cache(mntcache);
1594 #ifdef HAVE_LIBUDEV
1595 udev_unref(udev);
1596 #endif
1597 return status;
1598 }