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