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