]>
Commit | Line | Data |
---|---|---|
2a4c734b | 1 | /* |
f77fa578 | 2 | * lsblk(8) - list block devices |
2a4c734b | 3 | * |
b93585e8 | 4 | * Copyright (C) 2010-2018 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 | 21 | */ |
2a4c734b MB |
22 | #include <stdio.h> |
23 | #include <errno.h> | |
24 | #include <getopt.h> | |
2a4c734b MB |
25 | #include <stdlib.h> |
26 | #include <unistd.h> | |
2a4c734b MB |
27 | #include <sys/types.h> |
28 | #include <sys/stat.h> | |
29 | #include <dirent.h> | |
30 | #include <fcntl.h> | |
31 | #include <string.h> | |
2a4c734b | 32 | #include <sys/ioctl.h> |
2a4c734b MB |
33 | #include <stdarg.h> |
34 | #include <locale.h> | |
35 | #include <pwd.h> | |
36 | #include <grp.h> | |
18eac5ba | 37 | #include <ctype.h> |
ccafadb7 | 38 | #include <assert.h> |
2a4c734b MB |
39 | |
40 | #include <blkid.h> | |
2a4c734b | 41 | |
540d506f | 42 | #include "c.h" |
2a4c734b MB |
43 | #include "pathnames.h" |
44 | #include "blkdev.h" | |
45 | #include "canonicalize.h" | |
2a4c734b | 46 | #include "nls.h" |
2a4c734b MB |
47 | #include "xalloc.h" |
48 | #include "strutils.h" | |
766ab9b0 | 49 | #include "sysfs.h" |
c05a80ca | 50 | #include "closestream.h" |
05187653 | 51 | #include "optutils.h" |
2a4c734b | 52 | |
14560b7f | 53 | #include "lsblk.h" |
7e786eca | 54 | |
14560b7f | 55 | UL_DEBUG_DEFINE_MASK(lsblk); |
7e786eca KZ |
56 | UL_DEBUG_DEFINE_MASKNAMES(lsblk) = UL_DEBUG_EMPTY_MASKNAMES; |
57 | ||
1539750f KZ |
58 | #define LSBLK_EXIT_SOMEOK 64 |
59 | #define LSBLK_EXIT_ALLFAILED 32 | |
60 | ||
be685b98 KZ |
61 | static int column_id_to_number(int id); |
62 | ||
2a4c734b MB |
63 | /* column IDs */ |
64 | enum { | |
65 | COL_NAME = 0, | |
66 | COL_KNAME, | |
66638b0b | 67 | COL_PATH, |
2a4c734b | 68 | COL_MAJMIN, |
bc405d64 KZ |
69 | COL_FSAVAIL, |
70 | COL_FSSIZE, | |
2a4c734b | 71 | COL_FSTYPE, |
bc405d64 KZ |
72 | COL_FSUSED, |
73 | COL_FSUSEPERC, | |
2a4c734b MB |
74 | COL_TARGET, |
75 | COL_LABEL, | |
76 | COL_UUID, | |
1b06b33d MB |
77 | COL_PTUUID, |
78 | COL_PTTYPE, | |
4686ba17 | 79 | COL_PARTTYPE, |
aa0903e0 KZ |
80 | COL_PARTLABEL, |
81 | COL_PARTUUID, | |
5a2fd932 | 82 | COL_PARTFLAGS, |
150db7a9 | 83 | COL_RA, |
2a4c734b | 84 | COL_RO, |
627970a7 | 85 | COL_RM, |
483987c2 | 86 | COL_HOTPLUG, |
2a4c734b | 87 | COL_MODEL, |
460c7afb | 88 | COL_SERIAL, |
2a4c734b | 89 | COL_SIZE, |
01e487c0 | 90 | COL_STATE, |
2a4c734b MB |
91 | COL_OWNER, |
92 | COL_GROUP, | |
93 | COL_MODE, | |
94 | COL_ALIOFF, | |
95 | COL_MINIO, | |
96 | COL_OPTIO, | |
97 | COL_PHYSEC, | |
98 | COL_LOGSEC, | |
99 | COL_ROTA, | |
100 | COL_SCHED, | |
64c9e22a | 101 | COL_RQ_SIZE, |
18eac5ba | 102 | COL_TYPE, |
2d232246 MP |
103 | COL_DALIGN, |
104 | COL_DGRAN, | |
105 | COL_DMAX, | |
106 | COL_DZERO, | |
2adb1a44 | 107 | COL_WSAME, |
88ca32b3 | 108 | COL_WWN, |
12b06c3d | 109 | COL_RAND, |
310c0603 | 110 | COL_PKNAME, |
699e5c4f | 111 | COL_HCTL, |
a5fb4d23 | 112 | COL_TRANSPORT, |
7f14ee1b | 113 | COL_SUBSYS, |
d6681cee | 114 | COL_REV, |
f2df4365 DLM |
115 | COL_VENDOR, |
116 | COL_ZONED, | |
2a4c734b MB |
117 | }; |
118 | ||
9bd4e5c0 OO |
119 | /* basic table settings */ |
120 | enum { | |
121 | LSBLK_ASCII = (1 << 0), | |
122 | LSBLK_RAW = (1 << 1), | |
123 | LSBLK_NOHEADINGS = (1 << 2), | |
bb6e822a KZ |
124 | LSBLK_EXPORT = (1 << 3), |
125 | LSBLK_TREE = (1 << 4), | |
4a102a48 | 126 | LSBLK_JSON = (1 << 5), |
9bd4e5c0 OO |
127 | }; |
128 | ||
407e0ea6 | 129 | /* Types used for qsort() and JSON */ |
642048e4 | 130 | enum { |
407e0ea6 KZ |
131 | COLTYPE_STR = 0, /* default */ |
132 | COLTYPE_NUM = 1, /* always u64 number */ | |
133 | COLTYPE_SORTNUM = 2, /* string on output, u64 for qsort() */ | |
134 | COLTYPE_SIZE = 3, /* srring by default, number when --bytes */ | |
135 | COLTYPE_BOOL = 4 /* 0 or 1 */ | |
642048e4 KZ |
136 | }; |
137 | ||
2a4c734b MB |
138 | /* column names */ |
139 | struct colinfo { | |
140 | const char *name; /* header */ | |
141 | double whint; /* width hint (N < 1 is in percent of termwidth) */ | |
9bd4e5c0 | 142 | int flags; /* SCOLS_FL_* */ |
2a4c734b | 143 | const char *help; |
407e0ea6 | 144 | int type; /* COLTYPE_* */ |
2a4c734b MB |
145 | }; |
146 | ||
147 | /* columns descriptions */ | |
507d39c2 | 148 | static struct colinfo infos[] = { |
b9c088f2 | 149 | [COL_NAME] = { "NAME", 0.25, SCOLS_FL_NOEXTREMES, N_("device name") }, |
e22d8b95 | 150 | [COL_KNAME] = { "KNAME", 0.3, 0, N_("internal kernel device name") }, |
bc405d64 | 151 | [COL_PKNAME] = { "PKNAME", 0.3, 0, N_("internal parent kernel device name") }, |
66638b0b | 152 | [COL_PATH] = { "PATH", 0.3, 0, N_("path to the device node") }, |
407e0ea6 | 153 | [COL_MAJMIN] = { "MAJ:MIN", 6, 0, N_("major:minor device number"), COLTYPE_SORTNUM }, |
bc405d64 KZ |
154 | |
155 | [COL_FSAVAIL] = { "FSAVAIL", 5, SCOLS_FL_RIGHT, N_("filesystem size available") }, | |
156 | [COL_FSSIZE] = { "FSSIZE", 5, SCOLS_FL_RIGHT, N_("filesystem size") }, | |
157 | [COL_FSTYPE] = { "FSTYPE", 0.1, SCOLS_FL_TRUNC, N_("filesystem type") }, | |
158 | [COL_FSUSED] = { "FSUSED", 5, SCOLS_FL_RIGHT, N_("filesystem size used") }, | |
159 | [COL_FSUSEPERC] = { "FSUSE%", 3, SCOLS_FL_RIGHT, N_("filesystem use percentage") }, | |
160 | ||
9bd4e5c0 | 161 | [COL_TARGET] = { "MOUNTPOINT", 0.10, SCOLS_FL_TRUNC, N_("where the device is mounted") }, |
2a4c734b MB |
162 | [COL_LABEL] = { "LABEL", 0.1, 0, N_("filesystem LABEL") }, |
163 | [COL_UUID] = { "UUID", 36, 0, N_("filesystem UUID") }, | |
aa0903e0 | 164 | |
1b06b33d MB |
165 | [COL_PTUUID] = { "PTUUID", 36, 0, N_("partition table identifier (usually UUID)") }, |
166 | [COL_PTTYPE] = { "PTTYPE", 0.1, 0, N_("partition table type") }, | |
167 | ||
4686ba17 | 168 | [COL_PARTTYPE] = { "PARTTYPE", 36, 0, N_("partition type UUID") }, |
aa0903e0 KZ |
169 | [COL_PARTLABEL] = { "PARTLABEL", 0.1, 0, N_("partition LABEL") }, |
170 | [COL_PARTUUID] = { "PARTUUID", 36, 0, N_("partition UUID") }, | |
5a2fd932 | 171 | [COL_PARTFLAGS] = { "PARTFLAGS", 36, 0, N_("partition flags") }, |
aa0903e0 | 172 | |
407e0ea6 KZ |
173 | [COL_RA] = { "RA", 3, SCOLS_FL_RIGHT, N_("read-ahead of the device"), COLTYPE_NUM }, |
174 | [COL_RO] = { "RO", 1, SCOLS_FL_RIGHT, N_("read-only device"), COLTYPE_BOOL }, | |
175 | [COL_RM] = { "RM", 1, SCOLS_FL_RIGHT, N_("removable device"), COLTYPE_BOOL }, | |
176 | [COL_HOTPLUG]= { "HOTPLUG", 1, SCOLS_FL_RIGHT, N_("removable or hotplug device (usb, pcmcia, ...)"), COLTYPE_BOOL }, | |
177 | [COL_ROTA] = { "ROTA", 1, SCOLS_FL_RIGHT, N_("rotational device"), COLTYPE_BOOL }, | |
178 | [COL_RAND] = { "RAND", 1, SCOLS_FL_RIGHT, N_("adds randomness"), COLTYPE_BOOL }, | |
9bd4e5c0 OO |
179 | [COL_MODEL] = { "MODEL", 0.1, SCOLS_FL_TRUNC, N_("device identifier") }, |
180 | [COL_SERIAL] = { "SERIAL", 0.1, SCOLS_FL_TRUNC, N_("disk serial number") }, | |
407e0ea6 | 181 | [COL_SIZE] = { "SIZE", 5, SCOLS_FL_RIGHT, N_("size of the device"), COLTYPE_SIZE }, |
9bd4e5c0 OO |
182 | [COL_STATE] = { "STATE", 7, SCOLS_FL_TRUNC, N_("state of the device") }, |
183 | [COL_OWNER] = { "OWNER", 0.1, SCOLS_FL_TRUNC, N_("user name"), }, | |
184 | [COL_GROUP] = { "GROUP", 0.1, SCOLS_FL_TRUNC, N_("group name") }, | |
2a4c734b | 185 | [COL_MODE] = { "MODE", 10, 0, N_("device node permissions") }, |
407e0ea6 KZ |
186 | [COL_ALIOFF] = { "ALIGNMENT", 6, SCOLS_FL_RIGHT, N_("alignment offset"), COLTYPE_NUM }, |
187 | [COL_MINIO] = { "MIN-IO", 6, SCOLS_FL_RIGHT, N_("minimum I/O size"), COLTYPE_NUM }, | |
188 | [COL_OPTIO] = { "OPT-IO", 6, SCOLS_FL_RIGHT, N_("optimal I/O size"), COLTYPE_NUM }, | |
189 | [COL_PHYSEC] = { "PHY-SEC", 7, SCOLS_FL_RIGHT, N_("physical sector size"), COLTYPE_NUM }, | |
190 | [COL_LOGSEC] = { "LOG-SEC", 7, SCOLS_FL_RIGHT, N_("logical sector size"), COLTYPE_NUM }, | |
18eac5ba | 191 | [COL_SCHED] = { "SCHED", 0.1, 0, N_("I/O scheduler name") }, |
407e0ea6 | 192 | [COL_RQ_SIZE]= { "RQ-SIZE", 5, SCOLS_FL_RIGHT, N_("request queue size"), COLTYPE_NUM }, |
2d232246 | 193 | [COL_TYPE] = { "TYPE", 4, 0, N_("device type") }, |
407e0ea6 KZ |
194 | [COL_DALIGN] = { "DISC-ALN", 6, SCOLS_FL_RIGHT, N_("discard alignment offset"), COLTYPE_NUM }, |
195 | [COL_DGRAN] = { "DISC-GRAN", 6, SCOLS_FL_RIGHT, N_("discard granularity"), COLTYPE_SIZE }, | |
196 | [COL_DMAX] = { "DISC-MAX", 6, SCOLS_FL_RIGHT, N_("discard max bytes"), COLTYPE_SIZE }, | |
197 | [COL_DZERO] = { "DISC-ZERO", 1, SCOLS_FL_RIGHT, N_("discard zeroes data"), COLTYPE_BOOL }, | |
198 | [COL_WSAME] = { "WSAME", 6, SCOLS_FL_RIGHT, N_("write same max bytes"), COLTYPE_SIZE }, | |
88ca32b3 | 199 | [COL_WWN] = { "WWN", 18, 0, N_("unique storage identifier") }, |
699e5c4f | 200 | [COL_HCTL] = { "HCTL", 10, 0, N_("Host:Channel:Target:Lun for SCSI") }, |
a5fb4d23 | 201 | [COL_TRANSPORT] = { "TRAN", 6, 0, N_("device transport type") }, |
7f14ee1b | 202 | [COL_SUBSYS] = { "SUBSYSTEMS", 0.1, SCOLS_FL_NOEXTREMES, N_("de-duplicated chain of subsystems") }, |
9bd4e5c0 OO |
203 | [COL_REV] = { "REV", 4, SCOLS_FL_RIGHT, N_("device revision") }, |
204 | [COL_VENDOR] = { "VENDOR", 0.1, SCOLS_FL_TRUNC, N_("device vendor") }, | |
f2df4365 | 205 | [COL_ZONED] = { "ZONED", 0.3, 0, N_("zone model") }, |
2a4c734b MB |
206 | }; |
207 | ||
14560b7f | 208 | struct lsblk *lsblk; /* global handler */ |
507d39c2 | 209 | |
b93585e8 KZ |
210 | /* |
211 | * columns[] array specifies all currently wanted output column. The columns | |
b6327c6f KZ |
212 | * are defined by infos[] array and you can specify (on command line) each |
213 | * column twice. That's enough, dynamically allocated array of the columns is | |
b93585e8 KZ |
214 | * unnecessary overkill and over-engineering in this case |
215 | */ | |
b6327c6f | 216 | static int columns[ARRAY_SIZE(infos) * 2]; |
40b17508 | 217 | static size_t ncolumns; |
b6327c6f | 218 | |
be685b98 | 219 | static inline void add_column(int id) |
b6327c6f | 220 | { |
be685b98 | 221 | if (ncolumns >= ARRAY_SIZE(columns)) |
b6327c6f | 222 | errx(EXIT_FAILURE, _("too many columns specified, " |
1d231190 | 223 | "the limit is %zu columns"), |
be685b98 KZ |
224 | ARRAY_SIZE(columns) - 1); |
225 | columns[ ncolumns++ ] = id; | |
b6327c6f KZ |
226 | } |
227 | ||
be685b98 KZ |
228 | static inline void add_uniq_column(int id) |
229 | { | |
230 | if (column_id_to_number(id) < 0) | |
231 | add_column(id); | |
232 | } | |
2a4c734b | 233 | |
b93585e8 KZ |
234 | static void lsblk_init_debug(void) |
235 | { | |
236 | __UL_INIT_DEBUG_FROM_ENV(lsblk, LSBLK_DEBUG_, 0, LSBLK_DEBUG); | |
237 | } | |
238 | ||
239 | /* | |
240 | * exclude/include devices filter based on major device numbers | |
241 | */ | |
f6da50d4 BV |
242 | static int excludes[256]; |
243 | static size_t nexcludes; | |
2a4c734b | 244 | |
2ef4e8ba KZ |
245 | static int includes[256]; |
246 | static size_t nincludes; | |
247 | ||
2a4c734b MB |
248 | static int is_maj_excluded(int maj) |
249 | { | |
6aace32f | 250 | size_t i; |
2a4c734b MB |
251 | |
252 | assert(ARRAY_SIZE(excludes) > nexcludes); | |
253 | ||
2ef4e8ba | 254 | if (!nexcludes) |
9e930041 | 255 | return 0; /* filter not enabled, device not excluded */ |
2ef4e8ba | 256 | |
7e786eca KZ |
257 | for (i = 0; i < nexcludes; i++) { |
258 | if (excludes[i] == maj) { | |
259 | DBG(FILTER, ul_debug("exclude: maj=%d", maj)); | |
2a4c734b | 260 | return 1; |
7e786eca KZ |
261 | } |
262 | } | |
2a4c734b MB |
263 | return 0; |
264 | } | |
265 | ||
2ef4e8ba KZ |
266 | static 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 | ||
7e786eca KZ |
275 | for (i = 0; i < nincludes; i++) { |
276 | if (includes[i] == maj) { | |
277 | DBG(FILTER, ul_debug("include: maj=%d", maj)); | |
2ef4e8ba | 278 | return 1; |
7e786eca KZ |
279 | } |
280 | } | |
2ef4e8ba KZ |
281 | return 0; |
282 | } | |
283 | ||
b93585e8 | 284 | /* Converts column sequential number to column ID (COL_*) */ |
2a4c734b MB |
285 | static int get_column_id(int num) |
286 | { | |
40b17508 KZ |
287 | assert(num >= 0); |
288 | assert((size_t) num < ncolumns); | |
b6327c6f | 289 | assert(columns[num] < (int) ARRAY_SIZE(infos)); |
2a4c734b MB |
290 | return columns[num]; |
291 | } | |
292 | ||
b93585e8 | 293 | /* Returns column description for the column sequential number */ |
2a4c734b MB |
294 | static struct colinfo *get_column_info(int num) |
295 | { | |
296 | return &infos[ get_column_id(num) ]; | |
297 | } | |
298 | ||
b93585e8 | 299 | /* Converts column name (as defined in the infos[] to the column ID */ |
2a4c734b MB |
300 | static int column_name_to_id(const char *name, size_t namesz) |
301 | { | |
507d39c2 | 302 | size_t i; |
2a4c734b | 303 | |
03df0d11 | 304 | for (i = 0; i < ARRAY_SIZE(infos); i++) { |
2a4c734b MB |
305 | const char *cn = infos[i].name; |
306 | ||
307 | if (!strncasecmp(name, cn, namesz) && !*(cn + namesz)) | |
308 | return i; | |
309 | } | |
310 | warnx(_("unknown column: %s"), name); | |
311 | return -1; | |
312 | } | |
313 | ||
b93585e8 | 314 | /* Converts column ID (COL_*) to column sequential number */ |
642048e4 KZ |
315 | static int column_id_to_number(int id) |
316 | { | |
317 | size_t i; | |
318 | ||
40b17508 | 319 | for (i = 0; i < ncolumns; i++) |
642048e4 KZ |
320 | if (columns[i] == id) |
321 | return i; | |
322 | return -1; | |
323 | } | |
324 | ||
b93585e8 | 325 | /* Checks for DM prefix in the device name */ |
2a4c734b MB |
326 | static int is_dm(const char *name) |
327 | { | |
328 | return strncmp(name, "dm-", 3) ? 0 : 1; | |
329 | } | |
330 | ||
b93585e8 | 331 | /* This is readdir()-like function, but skips "." and ".." directory entries */ |
2a4c734b MB |
332 | static struct dirent *xreaddir(DIR *dp) |
333 | { | |
334 | struct dirent *d; | |
335 | ||
336 | assert(dp); | |
337 | ||
338 | while ((d = readdir(dp))) { | |
339 | if (!strcmp(d->d_name, ".") || | |
340 | !strcmp(d->d_name, "..")) | |
341 | continue; | |
342 | ||
343 | /* blacklist here? */ | |
344 | break; | |
345 | } | |
346 | return d; | |
347 | } | |
348 | ||
b158bd29 | 349 | /* Returns full pat to the device node (TODO: what about sysfs_blkdev_get_path()) */ |
fed34a1e | 350 | static char *get_device_path(struct lsblk_device *dev) |
2a4c734b MB |
351 | { |
352 | char path[PATH_MAX]; | |
353 | ||
fed34a1e KZ |
354 | assert(dev); |
355 | assert(dev->name); | |
2a4c734b | 356 | |
fed34a1e KZ |
357 | if (is_dm(dev->name)) |
358 | return __canonicalize_dm_name(lsblk->sysroot, dev->name); | |
2a4c734b | 359 | |
fed34a1e | 360 | snprintf(path, sizeof(path), "/dev/%s", dev->name); |
92441d5c | 361 | sysfs_devname_sys_to_dev(path); |
2a4c734b MB |
362 | return xstrdup(path); |
363 | } | |
364 | ||
fed34a1e | 365 | static int is_readonly_device(struct lsblk_device *dev) |
2a4c734b MB |
366 | { |
367 | int fd, ro = 0; | |
368 | ||
fed34a1e | 369 | if (ul_path_scanf(dev->sysfs, "ro", "%d", &ro) == 1) |
2a4c734b MB |
370 | return ro; |
371 | ||
372 | /* fallback if "ro" attribute does not exist */ | |
fed34a1e | 373 | fd = open(dev->filename, O_RDONLY); |
2a4c734b | 374 | if (fd != -1) { |
9c53a49c KZ |
375 | if (ioctl(fd, BLKROGET, &ro) != 0) |
376 | ro = 0; | |
2a4c734b MB |
377 | close(fd); |
378 | } | |
379 | return ro; | |
380 | } | |
381 | ||
fed34a1e | 382 | static char *get_scheduler(struct lsblk_device *dev) |
2a4c734b | 383 | { |
f153614c | 384 | char buf[128]; |
2a4c734b MB |
385 | char *p, *res = NULL; |
386 | ||
fed34a1e | 387 | if (ul_path_read_buffer(dev->sysfs, buf, sizeof(buf), "queue/scheduler") == 0) |
2a4c734b | 388 | return NULL; |
f153614c | 389 | p = strchr(buf, '['); |
2a4c734b MB |
390 | if (p) { |
391 | res = p + 1; | |
392 | p = strchr(res, ']'); | |
393 | if (p) { | |
394 | *p = '\0'; | |
395 | res = xstrdup(res); | |
396 | } else | |
397 | res = NULL; | |
398 | } | |
2a4c734b MB |
399 | return res; |
400 | } | |
401 | ||
fed34a1e | 402 | static char *get_type(struct lsblk_device *dev) |
18eac5ba MB |
403 | { |
404 | char *res = NULL, *p; | |
405 | ||
0bd05f5e | 406 | if (device_is_partition(dev)) |
ef2ce68b KZ |
407 | return xstrdup("part"); |
408 | ||
fed34a1e | 409 | if (is_dm(dev->name)) { |
f153614c | 410 | char *dm_uuid = NULL; |
18eac5ba MB |
411 | |
412 | /* The DM_UUID prefix should be set to subsystem owning | |
413 | * the device - LVM, CRYPT, DMRAID, MPATH, PART */ | |
fed34a1e | 414 | if (ul_path_read_string(dev->sysfs, &dm_uuid, "dm/uuid") > 0 |
f153614c | 415 | && dm_uuid) { |
18eac5ba MB |
416 | char *tmp = dm_uuid; |
417 | char *dm_uuid_prefix = strsep(&tmp, "-"); | |
418 | ||
419 | if (dm_uuid_prefix) { | |
420 | /* kpartx hack to remove partition number */ | |
421 | if (strncasecmp(dm_uuid_prefix, "part", 4) == 0) | |
422 | dm_uuid_prefix[4] = '\0'; | |
423 | ||
424 | res = xstrdup(dm_uuid_prefix); | |
425 | } | |
426 | } | |
427 | ||
428 | free(dm_uuid); | |
429 | if (!res) | |
430 | /* No UUID or no prefix - just mark it as DM device */ | |
431 | res = xstrdup("dm"); | |
432 | ||
fed34a1e | 433 | } else if (!strncmp(dev->name, "loop", 4)) { |
18eac5ba MB |
434 | res = xstrdup("loop"); |
435 | ||
fed34a1e | 436 | } else if (!strncmp(dev->name, "md", 2)) { |
f153614c KZ |
437 | char *md_level = NULL; |
438 | ||
fed34a1e | 439 | ul_path_read_string(dev->sysfs, &md_level, "md/level"); |
18eac5ba MB |
440 | res = md_level ? md_level : xstrdup("md"); |
441 | ||
442 | } else { | |
8d687180 | 443 | const char *type = NULL; |
90e9fcda | 444 | int x = 0; |
9d738ff8 | 445 | |
fed34a1e | 446 | if (ul_path_read_s32(dev->sysfs, &x, "device/type") == 0) |
8d687180 | 447 | type = blkdev_scsi_type_to_name(x); |
9d738ff8 | 448 | if (!type) |
ef2ce68b | 449 | type = "disk"; |
9d738ff8 | 450 | res = xstrdup(type); |
18eac5ba MB |
451 | } |
452 | ||
453 | for (p = res; p && *p; p++) | |
454 | *p = tolower((unsigned char) *p); | |
455 | return res; | |
456 | } | |
457 | ||
a5fb4d23 | 458 | /* Thanks to lsscsi code for idea of detection logic used here */ |
fed34a1e | 459 | static char *get_transport(struct lsblk_device *dev) |
a5fb4d23 | 460 | { |
fed34a1e | 461 | struct path_cxt *sysfs = dev->sysfs; |
2f2f7e80 KZ |
462 | char *attr = NULL; |
463 | const char *trans = NULL; | |
a5fb4d23 | 464 | |
f153614c | 465 | |
28ffc2b7 | 466 | /* SCSI - Serial Peripheral Interface */ |
f153614c | 467 | if (sysfs_blkdev_scsi_host_is(sysfs, "spi")) |
2f2f7e80 | 468 | trans = "spi"; |
a5fb4d23 MB |
469 | |
470 | /* FC/FCoE - Fibre Channel / Fibre Channel over Ethernet */ | |
f153614c KZ |
471 | else if (sysfs_blkdev_scsi_host_is(sysfs, "fc")) { |
472 | attr = sysfs_blkdev_scsi_host_strdup_attribute(sysfs, "fc", "symbolic_name"); | |
23a11c74 | 473 | if (!attr) |
a5fb4d23 | 474 | return NULL; |
2f2f7e80 KZ |
475 | trans = strstr(attr, " over ") ? "fcoe" : "fc"; |
476 | free(attr); | |
a5fb4d23 MB |
477 | } |
478 | ||
479 | /* SAS - Serial Attached SCSI */ | |
f153614c KZ |
480 | else if (sysfs_blkdev_scsi_host_is(sysfs, "sas") || |
481 | sysfs_blkdev_scsi_has_attribute(sysfs, "sas_device")) | |
2f2f7e80 | 482 | trans = "sas"; |
a5fb4d23 | 483 | |
a5fb4d23 MB |
484 | |
485 | /* SBP - Serial Bus Protocol (FireWire) */ | |
f153614c | 486 | else if (sysfs_blkdev_scsi_has_attribute(sysfs, "ieee1394_id")) |
2f2f7e80 | 487 | trans = "sbp"; |
a5fb4d23 MB |
488 | |
489 | /* iSCSI */ | |
f153614c | 490 | else if (sysfs_blkdev_scsi_host_is(sysfs, "iscsi")) |
2f2f7e80 | 491 | trans ="iscsi"; |
a5fb4d23 MB |
492 | |
493 | /* USB - Universal Serial Bus */ | |
f153614c | 494 | else if (sysfs_blkdev_scsi_path_contains(sysfs, "usb")) |
2f2f7e80 | 495 | trans = "usb"; |
a5fb4d23 MB |
496 | |
497 | /* ATA, SATA */ | |
f153614c KZ |
498 | else if (sysfs_blkdev_scsi_host_is(sysfs, "scsi")) { |
499 | attr = sysfs_blkdev_scsi_host_strdup_attribute(sysfs, "scsi", "proc_name"); | |
23a11c74 | 500 | if (!attr) |
a5fb4d23 | 501 | return NULL; |
2f2f7e80 KZ |
502 | if (!strncmp(attr, "ahci", 4) || !strncmp(attr, "sata", 4)) |
503 | trans = "sata"; | |
504 | else if (strstr(attr, "ata")) | |
505 | trans = "ata"; | |
506 | free(attr); | |
3d305170 | 507 | |
fed34a1e | 508 | } else if (strncmp(dev->name, "nvme", 4) == 0) |
3d305170 | 509 | trans = "nvme"; |
a5fb4d23 | 510 | |
2f2f7e80 | 511 | return trans ? xstrdup(trans) : NULL; |
a5fb4d23 MB |
512 | } |
513 | ||
fed34a1e | 514 | static char *get_subsystems(struct lsblk_device *dev) |
7f14ee1b KZ |
515 | { |
516 | char path[PATH_MAX]; | |
517 | char *sub, *chain, *res = NULL; | |
518 | size_t len = 0, last = 0; | |
519 | ||
fed34a1e | 520 | chain = sysfs_blkdev_get_devchain(dev->sysfs, path, sizeof(path)); |
7f14ee1b KZ |
521 | if (!chain) |
522 | return NULL; | |
523 | ||
fed34a1e | 524 | while (sysfs_blkdev_next_subsystem(dev->sysfs, chain, &sub) == 0) { |
7f14ee1b KZ |
525 | size_t sz; |
526 | ||
527 | /* don't create "block:scsi:scsi", but "block:scsi" */ | |
56e78cb5 AH |
528 | if (len && strcmp(res + last, sub) == 0) { |
529 | free(sub); | |
7f14ee1b | 530 | continue; |
56e78cb5 | 531 | } |
7f14ee1b KZ |
532 | |
533 | sz = strlen(sub); | |
534 | res = xrealloc(res, len + sz + 2); | |
535 | if (len) | |
536 | res[len++] = ':'; | |
537 | ||
538 | memcpy(res + len, sub, sz + 1); | |
539 | last = len; | |
540 | len += sz; | |
541 | free(sub); | |
542 | } | |
543 | ||
544 | return res; | |
545 | } | |
546 | ||
547 | ||
9bd4e5c0 | 548 | #define is_parsable(_l) (scols_table_is_raw((_l)->table) || \ |
4a102a48 KZ |
549 | scols_table_is_export((_l)->table) || \ |
550 | scols_table_is_json((_l)->table)) | |
7c9c872c | 551 | |
c7e76cd1 KZ |
552 | static char *mk_name(const char *name) |
553 | { | |
554 | char *p; | |
555 | if (!name) | |
556 | return NULL; | |
557 | if (lsblk->paths) | |
558 | xasprintf(&p, "/dev/%s", name); | |
559 | else | |
560 | p = xstrdup(name); | |
92441d5c KZ |
561 | if (p) |
562 | sysfs_devname_sys_to_dev(p); | |
c7e76cd1 KZ |
563 | return p; |
564 | } | |
565 | ||
566 | static char *mk_dm_name(const char *name) | |
567 | { | |
568 | char *p; | |
569 | if (!name) | |
570 | return NULL; | |
571 | if (lsblk->paths) | |
572 | xasprintf(&p, "/dev/mapper/%s", name); | |
573 | else | |
574 | p = xstrdup(name); | |
575 | return p; | |
576 | } | |
577 | ||
642048e4 KZ |
578 | /* stores data to scols cell userdata (invisible and independent on output) |
579 | * to make the original values accessible for sort functions | |
580 | */ | |
581 | static void set_sortdata_u64(struct libscols_line *ln, int col, uint64_t x) | |
582 | { | |
583 | struct libscols_cell *ce = scols_line_get_cell(ln, col); | |
584 | uint64_t *data; | |
585 | ||
586 | if (!ce) | |
587 | return; | |
588 | data = xmalloc(sizeof(uint64_t)); | |
589 | *data = x; | |
590 | scols_cell_set_userdata(ce, data); | |
591 | } | |
592 | ||
d81e32bd KZ |
593 | /* do not modify *data on any error */ |
594 | static void str2u64(const char *str, uint64_t *data) | |
642048e4 | 595 | { |
d81e32bd KZ |
596 | uintmax_t num; |
597 | char *end = NULL; | |
642048e4 | 598 | |
d81e32bd KZ |
599 | errno = 0; |
600 | if (str == NULL || *str == '\0') | |
642048e4 | 601 | return; |
d81e32bd | 602 | num = strtoumax(str, &end, 10); |
642048e4 | 603 | |
d81e32bd KZ |
604 | if (errno || str == end || (end && *end)) |
605 | return; | |
606 | *data = num; | |
642048e4 KZ |
607 | } |
608 | ||
609 | static void unref_sortdata(struct libscols_table *tb) | |
610 | { | |
611 | struct libscols_iter *itr; | |
612 | struct libscols_line *ln; | |
613 | ||
614 | if (!tb || !lsblk->sort_col) | |
615 | return; | |
616 | itr = scols_new_iter(SCOLS_ITER_FORWARD); | |
617 | if (!itr) | |
618 | return; | |
619 | while (scols_table_next_line(tb, itr, &ln) == 0) { | |
620 | struct libscols_cell *ce = scols_line_get_column_cell(ln, | |
621 | lsblk->sort_col); | |
622 | void *data = scols_cell_get_userdata(ce); | |
623 | free(data); | |
624 | } | |
625 | ||
626 | scols_free_iter(itr); | |
627 | } | |
628 | ||
fed34a1e | 629 | static char *get_vfs_attribute(struct lsblk_device *dev, int id) |
bc405d64 KZ |
630 | { |
631 | char *sizestr; | |
632 | uint64_t vfs_attr = 0; | |
633 | char *mnt; | |
634 | ||
fed34a1e KZ |
635 | if (!dev->fsstat.f_blocks) { |
636 | mnt = lsblk_device_get_mountpoint(dev); | |
637 | if (!mnt || dev->is_swap) | |
bc405d64 | 638 | return NULL; |
fed34a1e | 639 | if (statvfs(mnt, &dev->fsstat) != 0) |
bc405d64 KZ |
640 | return NULL; |
641 | } | |
642 | ||
643 | switch(id) { | |
644 | case COL_FSSIZE: | |
fed34a1e | 645 | vfs_attr = dev->fsstat.f_frsize * dev->fsstat.f_blocks; |
bc405d64 KZ |
646 | break; |
647 | case COL_FSAVAIL: | |
fed34a1e | 648 | vfs_attr = dev->fsstat.f_frsize * dev->fsstat.f_bavail; |
bc405d64 KZ |
649 | break; |
650 | case COL_FSUSED: | |
fed34a1e | 651 | vfs_attr = dev->fsstat.f_frsize * (dev->fsstat.f_blocks - dev->fsstat.f_bfree); |
bc405d64 KZ |
652 | break; |
653 | case COL_FSUSEPERC: | |
fed34a1e | 654 | if (dev->fsstat.f_blocks == 0) |
bc405d64 KZ |
655 | return xstrdup("-"); |
656 | ||
657 | xasprintf(&sizestr, "%.0f%%", | |
fed34a1e KZ |
658 | (double)(dev->fsstat.f_blocks - dev->fsstat.f_bfree) / |
659 | dev->fsstat.f_blocks * 100); | |
bc405d64 KZ |
660 | return sizestr; |
661 | } | |
662 | ||
663 | if (!vfs_attr) | |
664 | sizestr = xstrdup("0"); | |
665 | else if (lsblk->bytes) | |
666 | xasprintf(&sizestr, "%ju", vfs_attr); | |
667 | else | |
668 | sizestr = size_to_human_string(SIZE_SUFFIX_1LETTER, vfs_attr); | |
669 | ||
670 | return sizestr; | |
671 | } | |
672 | ||
fed34a1e | 673 | static struct stat *device_get_stat(struct lsblk_device *dev) |
652cb1cd | 674 | { |
fed34a1e KZ |
675 | if (!dev->st.st_rdev) |
676 | stat(dev->filename, &dev->st); | |
652cb1cd | 677 | |
fed34a1e | 678 | return &dev->st; |
652cb1cd | 679 | } |
bc405d64 | 680 | |
81a8936c KZ |
681 | static int is_removable_device(struct lsblk_device *dev, struct lsblk_device *parent) |
682 | { | |
683 | struct path_cxt *pc; | |
684 | ||
685 | if (dev->removable != -1) | |
686 | goto done; | |
687 | if (ul_path_scanf(dev->sysfs, "removable", "%d", &dev->removable) == 1) | |
688 | goto done; | |
689 | ||
690 | if (parent) { | |
691 | pc = sysfs_blkdev_get_parent(dev->sysfs); | |
692 | if (!pc) | |
693 | goto done; | |
694 | ||
695 | if (pc == parent->sysfs) | |
696 | /* dev is partition and parent is whole-disk */ | |
697 | dev->removable = is_removable_device(parent, NULL); | |
698 | else | |
699 | /* parent is something else, use sysfs parent */ | |
700 | ul_path_scanf(pc, "removable", "%d", &dev->removable); | |
701 | } | |
81a8936c KZ |
702 | done: |
703 | if (dev->removable == -1) | |
704 | dev->removable = 0; | |
705 | return dev->removable; | |
706 | } | |
707 | ||
8a2f175c KZ |
708 | static uint64_t device_get_discard_granularity(struct lsblk_device *dev) |
709 | { | |
710 | if (dev->discard_granularity == (uint64_t) -1 | |
711 | && ul_path_read_u64(dev->sysfs, &dev->discard_granularity, | |
712 | "queue/discard_granularity") != 0) | |
713 | dev->discard_granularity = 0; | |
714 | ||
715 | return dev->discard_granularity; | |
716 | } | |
717 | ||
b93585e8 | 718 | /* |
d81e32bd KZ |
719 | * Generates data (string) for column specified by column ID for specified device. If sortdata |
720 | * is not NULL then returns number usable to sort the column if the data are available for the | |
721 | * column. | |
b93585e8 | 722 | */ |
d81e32bd KZ |
723 | static char *device_get_data( |
724 | struct lsblk_device *dev, /* device */ | |
725 | struct lsblk_device *parent, /* device parent as defined in the tree */ | |
726 | int id, /* column ID (COL_*) */ | |
727 | uint64_t *sortdata) /* returns sort data as number */ | |
2a4c734b | 728 | { |
baad6dcc | 729 | struct lsblk_devprop *prop; |
bb6e822a | 730 | char *str = NULL; |
2a4c734b | 731 | |
2a4c734b MB |
732 | switch(id) { |
733 | case COL_NAME: | |
fed34a1e | 734 | str = dev->dm_name ? mk_dm_name(dev->dm_name) : mk_name(dev->name); |
c7e76cd1 | 735 | break; |
2a4c734b | 736 | case COL_KNAME: |
fed34a1e | 737 | str = mk_name(dev->name); |
2a4c734b | 738 | break; |
310c0603 | 739 | case COL_PKNAME: |
ae4c2c37 KZ |
740 | if (parent) |
741 | str = mk_name(parent->name); | |
310c0603 | 742 | break; |
66638b0b | 743 | case COL_PATH: |
fed34a1e KZ |
744 | if (dev->filename) |
745 | str = xstrdup(dev->filename); | |
66638b0b | 746 | break; |
2a4c734b MB |
747 | case COL_OWNER: |
748 | { | |
fed34a1e | 749 | struct stat *st = device_get_stat(dev); |
e361253e | 750 | struct passwd *pw = st ? getpwuid(st->st_uid) : NULL; |
2a4c734b | 751 | if (pw) |
bb6e822a | 752 | str = xstrdup(pw->pw_name); |
2a4c734b MB |
753 | break; |
754 | } | |
755 | case COL_GROUP: | |
756 | { | |
fed34a1e | 757 | struct stat *st = device_get_stat(dev); |
e361253e | 758 | struct group *gr = st ? getgrgid(st->st_gid) : NULL; |
2a4c734b | 759 | if (gr) |
bb6e822a | 760 | str = xstrdup(gr->gr_name); |
2a4c734b MB |
761 | break; |
762 | } | |
763 | case COL_MODE: | |
764 | { | |
fed34a1e | 765 | struct stat *st = device_get_stat(dev); |
652cb1cd | 766 | char md[11] = { '\0' }; |
1dc42eb6 | 767 | |
652cb1cd KZ |
768 | if (st) |
769 | str = xstrdup(xstrmode(st->st_mode, md)); | |
2a4c734b MB |
770 | break; |
771 | } | |
772 | case COL_MAJMIN: | |
7c9c872c | 773 | if (is_parsable(lsblk)) |
fed34a1e | 774 | xasprintf(&str, "%u:%u", dev->maj, dev->min); |
2a4c734b | 775 | else |
fed34a1e | 776 | xasprintf(&str, "%3u:%-3u", dev->maj, dev->min); |
d81e32bd KZ |
777 | if (sortdata) |
778 | *sortdata = makedev(dev->maj, dev->min); | |
2a4c734b MB |
779 | break; |
780 | case COL_FSTYPE: | |
fed34a1e | 781 | prop = lsblk_device_get_properties(dev); |
baad6dcc KZ |
782 | if (prop && prop->fstype) |
783 | str = xstrdup(prop->fstype); | |
2a4c734b | 784 | break; |
bc405d64 KZ |
785 | case COL_FSSIZE: |
786 | case COL_FSAVAIL: | |
787 | case COL_FSUSED: | |
788 | case COL_FSUSEPERC: | |
fed34a1e | 789 | str = get_vfs_attribute(dev, id); |
bc405d64 | 790 | break; |
2a4c734b | 791 | case COL_TARGET: |
fed34a1e | 792 | str = xstrdup(lsblk_device_get_mountpoint(dev)); |
2a4c734b MB |
793 | break; |
794 | case COL_LABEL: | |
fed34a1e | 795 | prop = lsblk_device_get_properties(dev); |
baad6dcc KZ |
796 | if (prop && prop->label) |
797 | str = xstrdup(prop->label); | |
2a4c734b MB |
798 | break; |
799 | case COL_UUID: | |
fed34a1e | 800 | prop = lsblk_device_get_properties(dev); |
baad6dcc KZ |
801 | if (prop && prop->uuid) |
802 | str = xstrdup(prop->uuid); | |
2a4c734b | 803 | break; |
1b06b33d | 804 | case COL_PTUUID: |
fed34a1e | 805 | prop = lsblk_device_get_properties(dev); |
baad6dcc KZ |
806 | if (prop && prop->ptuuid) |
807 | str = xstrdup(prop->ptuuid); | |
1b06b33d MB |
808 | break; |
809 | case COL_PTTYPE: | |
fed34a1e | 810 | prop = lsblk_device_get_properties(dev); |
baad6dcc KZ |
811 | if (prop && prop->pttype) |
812 | str = xstrdup(prop->pttype); | |
1b06b33d | 813 | break; |
4686ba17 | 814 | case COL_PARTTYPE: |
fed34a1e | 815 | prop = lsblk_device_get_properties(dev); |
baad6dcc KZ |
816 | if (prop && prop->parttype) |
817 | str = xstrdup(prop->parttype); | |
4686ba17 | 818 | break; |
aa0903e0 | 819 | case COL_PARTLABEL: |
fed34a1e | 820 | prop = lsblk_device_get_properties(dev); |
baad6dcc KZ |
821 | if (prop && prop->partlabel) |
822 | str = xstrdup(prop->partlabel); | |
aa0903e0 KZ |
823 | break; |
824 | case COL_PARTUUID: | |
fed34a1e | 825 | prop = lsblk_device_get_properties(dev); |
baad6dcc KZ |
826 | if (prop && prop->partuuid) |
827 | str = xstrdup(prop->partuuid); | |
aa0903e0 | 828 | break; |
5a2fd932 | 829 | case COL_PARTFLAGS: |
fed34a1e | 830 | prop = lsblk_device_get_properties(dev); |
baad6dcc KZ |
831 | if (prop && prop->partflags) |
832 | str = xstrdup(prop->partflags); | |
5a2fd932 | 833 | break; |
88ca32b3 | 834 | case COL_WWN: |
fed34a1e | 835 | prop = lsblk_device_get_properties(dev); |
baad6dcc KZ |
836 | if (prop && prop->wwn) |
837 | str = xstrdup(prop->wwn); | |
88ca32b3 | 838 | break; |
150db7a9 | 839 | case COL_RA: |
fed34a1e | 840 | ul_path_read_string(dev->sysfs, &str, "queue/read_ahead_kb"); |
d81e32bd KZ |
841 | if (sortdata) |
842 | str2u64(str, sortdata); | |
150db7a9 | 843 | break; |
2a4c734b | 844 | case COL_RO: |
fed34a1e | 845 | str = xstrdup(is_readonly_device(dev) ? "1" : "0"); |
2a4c734b | 846 | break; |
627970a7 | 847 | case COL_RM: |
81a8936c | 848 | str = xstrdup(is_removable_device(dev, parent) ? "1" : "0"); |
2a4c734b | 849 | break; |
483987c2 | 850 | case COL_HOTPLUG: |
fed34a1e | 851 | str = sysfs_blkdev_is_hotpluggable(dev->sysfs) ? xstrdup("1") : xstrdup("0"); |
483987c2 | 852 | break; |
2a4c734b | 853 | case COL_ROTA: |
fed34a1e | 854 | ul_path_read_string(dev->sysfs, &str, "queue/rotational"); |
2a4c734b | 855 | break; |
12b06c3d | 856 | case COL_RAND: |
fed34a1e | 857 | ul_path_read_string(dev->sysfs, &str, "queue/add_random"); |
12b06c3d | 858 | break; |
2a4c734b | 859 | case COL_MODEL: |
10501add | 860 | if (!device_is_partition(dev) && dev->nslaves == 0) { |
fed34a1e | 861 | prop = lsblk_device_get_properties(dev); |
baad6dcc KZ |
862 | if (prop && prop->model) |
863 | str = xstrdup(prop->model); | |
13cbc6f2 | 864 | else |
fed34a1e | 865 | ul_path_read_string(dev->sysfs, &str, "device/model"); |
13cbc6f2 | 866 | } |
d6681cee | 867 | break; |
460c7afb | 868 | case COL_SERIAL: |
10501add | 869 | if (!device_is_partition(dev) && dev->nslaves == 0) { |
fed34a1e | 870 | prop = lsblk_device_get_properties(dev); |
baad6dcc KZ |
871 | if (prop && prop->serial) |
872 | str = xstrdup(prop->serial); | |
3d305170 | 873 | else |
fed34a1e | 874 | ul_path_read_string(dev->sysfs, &str, "device/serial"); |
460c7afb KZ |
875 | } |
876 | break; | |
d6681cee | 877 | case COL_REV: |
10501add | 878 | if (!device_is_partition(dev) && dev->nslaves == 0) |
fed34a1e | 879 | ul_path_read_string(dev->sysfs, &str, "device/rev"); |
99f43b72 MB |
880 | break; |
881 | case COL_VENDOR: | |
10501add | 882 | if (!device_is_partition(dev) && dev->nslaves == 0) |
fed34a1e | 883 | ul_path_read_string(dev->sysfs, &str, "device/vendor"); |
2a4c734b MB |
884 | break; |
885 | case COL_SIZE: | |
fed34a1e | 886 | if (!dev->size) |
bb6e822a KZ |
887 | break; |
888 | if (lsblk->bytes) | |
fed34a1e | 889 | xasprintf(&str, "%ju", dev->size); |
bb6e822a | 890 | else |
fed34a1e | 891 | str = size_to_human_string(SIZE_SUFFIX_1LETTER, dev->size); |
d81e32bd KZ |
892 | if (sortdata) |
893 | *sortdata = dev->size; | |
2a4c734b | 894 | break; |
01e487c0 | 895 | case COL_STATE: |
10501add | 896 | if (!device_is_partition(dev) && !dev->dm_name) |
fed34a1e KZ |
897 | ul_path_read_string(dev->sysfs, &str, "device/state"); |
898 | else if (dev->dm_name) { | |
01e487c0 | 899 | int x = 0; |
fed34a1e | 900 | if (ul_path_read_s32(dev->sysfs, &x, "dm/suspended") == 0) |
bb6e822a | 901 | str = xstrdup(x ? "suspended" : "running"); |
01e487c0 | 902 | } |
01e487c0 | 903 | break; |
2a4c734b | 904 | case COL_ALIOFF: |
fed34a1e | 905 | ul_path_read_string(dev->sysfs, &str, "alignment_offset"); |
d81e32bd KZ |
906 | if (sortdata) |
907 | str2u64(str, sortdata); | |
2a4c734b MB |
908 | break; |
909 | case COL_MINIO: | |
fed34a1e | 910 | ul_path_read_string(dev->sysfs, &str, "queue/minimum_io_size"); |
d81e32bd KZ |
911 | if (sortdata) |
912 | str2u64(str, sortdata); | |
2a4c734b MB |
913 | break; |
914 | case COL_OPTIO: | |
fed34a1e | 915 | ul_path_read_string(dev->sysfs, &str, "queue/optimal_io_size"); |
d81e32bd KZ |
916 | if (sortdata) |
917 | str2u64(str, sortdata); | |
2a4c734b MB |
918 | break; |
919 | case COL_PHYSEC: | |
fed34a1e | 920 | ul_path_read_string(dev->sysfs, &str, "queue/physical_block_size"); |
d81e32bd KZ |
921 | if (sortdata) |
922 | str2u64(str, sortdata); | |
2a4c734b MB |
923 | break; |
924 | case COL_LOGSEC: | |
fed34a1e | 925 | ul_path_read_string(dev->sysfs, &str, "queue/logical_block_size"); |
d81e32bd KZ |
926 | if (sortdata) |
927 | str2u64(str, sortdata); | |
2a4c734b MB |
928 | break; |
929 | case COL_SCHED: | |
fed34a1e | 930 | str = get_scheduler(dev); |
2a4c734b | 931 | break; |
64c9e22a | 932 | case COL_RQ_SIZE: |
fed34a1e | 933 | ul_path_read_string(dev->sysfs, &str, "queue/nr_requests"); |
d81e32bd KZ |
934 | if (sortdata) |
935 | str2u64(str, sortdata); | |
64c9e22a | 936 | break; |
18eac5ba | 937 | case COL_TYPE: |
fed34a1e | 938 | str = get_type(dev); |
18eac5ba | 939 | break; |
699e5c4f MB |
940 | case COL_HCTL: |
941 | { | |
942 | int h, c, t, l; | |
fed34a1e | 943 | if (sysfs_blkdev_scsi_get_hctl(dev->sysfs, &h, &c, &t, &l) == 0) |
bb6e822a | 944 | xasprintf(&str, "%d:%d:%d:%d", h, c, t, l); |
699e5c4f MB |
945 | break; |
946 | } | |
a5fb4d23 | 947 | case COL_TRANSPORT: |
fed34a1e | 948 | str = get_transport(dev); |
a5fb4d23 | 949 | break; |
7f14ee1b | 950 | case COL_SUBSYS: |
fed34a1e | 951 | str = get_subsystems(dev); |
7f14ee1b | 952 | break; |
2d232246 | 953 | case COL_DALIGN: |
8a2f175c | 954 | if (device_get_discard_granularity(dev) > 0) |
fed34a1e | 955 | ul_path_read_string(dev->sysfs, &str, "discard_alignment"); |
bb6e822a KZ |
956 | if (!str) |
957 | str = xstrdup("0"); | |
d81e32bd KZ |
958 | if (sortdata) |
959 | str2u64(str, sortdata); | |
2d232246 MP |
960 | break; |
961 | case COL_DGRAN: | |
642048e4 | 962 | if (lsblk->bytes) { |
fed34a1e | 963 | ul_path_read_string(dev->sysfs, &str, "queue/discard_granularity"); |
d81e32bd KZ |
964 | if (sortdata) |
965 | str2u64(str, sortdata); | |
642048e4 | 966 | } else { |
8a2f175c KZ |
967 | uint64_t x = device_get_discard_granularity(dev); |
968 | str = size_to_human_string(SIZE_SUFFIX_1LETTER, x); | |
d81e32bd KZ |
969 | if (sortdata) |
970 | *sortdata = x; | |
9cfe1b9c | 971 | } |
2d232246 MP |
972 | break; |
973 | case COL_DMAX: | |
642048e4 | 974 | if (lsblk->bytes) { |
fed34a1e | 975 | ul_path_read_string(dev->sysfs, &str, "queue/discard_max_bytes"); |
d81e32bd KZ |
976 | if (sortdata) |
977 | str2u64(str, sortdata); | |
642048e4 | 978 | } else { |
9cfe1b9c | 979 | uint64_t x; |
fed34a1e | 980 | if (ul_path_read_u64(dev->sysfs, &x, "queue/discard_max_bytes") == 0) { |
bb6e822a | 981 | str = size_to_human_string(SIZE_SUFFIX_1LETTER, x); |
d81e32bd KZ |
982 | if (sortdata) |
983 | *sortdata = x; | |
642048e4 | 984 | } |
9cfe1b9c | 985 | } |
2d232246 MP |
986 | break; |
987 | case COL_DZERO: | |
8a2f175c | 988 | if (device_get_discard_granularity(dev) > 0) |
fed34a1e | 989 | ul_path_read_string(dev->sysfs, &str, "queue/discard_zeroes_data"); |
bb6e822a KZ |
990 | if (!str) |
991 | str = xstrdup("0"); | |
2d232246 | 992 | break; |
2adb1a44 | 993 | case COL_WSAME: |
642048e4 | 994 | if (lsblk->bytes) { |
fed34a1e | 995 | ul_path_read_string(dev->sysfs, &str, "queue/write_same_max_bytes"); |
d81e32bd KZ |
996 | if (sortdata) |
997 | str2u64(str, sortdata); | |
642048e4 | 998 | } else { |
2adb1a44 MB |
999 | uint64_t x; |
1000 | ||
fed34a1e | 1001 | if (ul_path_read_u64(dev->sysfs, &x, "queue/write_same_max_bytes") == 0) { |
bb6e822a | 1002 | str = size_to_human_string(SIZE_SUFFIX_1LETTER, x); |
d81e32bd KZ |
1003 | if (sortdata) |
1004 | *sortdata = x; | |
642048e4 | 1005 | } |
2adb1a44 | 1006 | } |
bb6e822a KZ |
1007 | if (!str) |
1008 | str = xstrdup("0"); | |
2adb1a44 | 1009 | break; |
f2df4365 | 1010 | case COL_ZONED: |
fed34a1e | 1011 | ul_path_read_string(dev->sysfs, &str, "queue/zoned"); |
f2df4365 | 1012 | break; |
2a4c734b | 1013 | }; |
bb6e822a | 1014 | |
d81e32bd | 1015 | return str; |
2a4c734b MB |
1016 | } |
1017 | ||
b93585e8 KZ |
1018 | /* |
1019 | * Adds data for all wanted columns about the device to the smartcols table | |
1020 | */ | |
d81e32bd KZ |
1021 | static void device_to_scols( |
1022 | struct lsblk_device *dev, | |
1023 | struct lsblk_device *parent, | |
1024 | struct libscols_table *tab, | |
1025 | struct libscols_line *parent_line) | |
2a4c734b | 1026 | { |
40b17508 | 1027 | size_t i; |
d81e32bd | 1028 | struct libscols_line *ln; |
f43b8297 KZ |
1029 | struct lsblk_iter itr; |
1030 | struct lsblk_device *child = NULL; | |
0bd05f5e | 1031 | int link_group = 0; |
2a4c734b | 1032 | |
ff7992d6 KZ |
1033 | ON_DBG(DEV, if (ul_path_isopen_dirfd(dev->sysfs)) ul_debugobj(dev, "%s ---> is open!", dev->name)); |
1034 | ||
0bd05f5e KZ |
1035 | /* Do not print device more than one in --list mode */ |
1036 | if (!(lsblk->flags & LSBLK_TREE) && dev->is_printed) | |
1037 | return; | |
1038 | ||
1039 | if (lsblk->merge && list_count_entries(&dev->parents) > 1) { | |
1040 | if (!lsblk_device_is_last_parent(dev, parent)) | |
1041 | return; | |
1042 | link_group = 1; | |
1043 | } | |
1044 | ||
1045 | ln = scols_table_new_line(tab, link_group ? NULL : parent_line); | |
d81e32bd | 1046 | if (!ln) |
780ce22c | 1047 | err(EXIT_FAILURE, _("failed to allocate output line")); |
2a4c734b | 1048 | |
0bd05f5e KZ |
1049 | dev->is_printed = 1; |
1050 | ||
1051 | if (link_group) { | |
1052 | struct lsblk_device *p; | |
1053 | struct libscols_line *gr = parent_line; | |
1054 | ||
1055 | /* Merge all my parents to the one group */ | |
1056 | lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD); | |
1057 | while (lsblk_device_next_parent(dev, &itr, &p) == 0) { | |
1058 | if (!p->scols_line) | |
1059 | continue; | |
1060 | scols_table_group_lines(tab, gr, p->scols_line, 0); | |
1061 | } | |
1062 | ||
1063 | /* Link the group -- this makes group->child connection */ | |
1064 | scols_line_link_group(ln, gr, 0); | |
1065 | } | |
1066 | ||
d81e32bd KZ |
1067 | /* read column specific data and set it to smartcols table line */ |
1068 | for (i = 0; i < ncolumns; i++) { | |
1069 | char *data; | |
1070 | int id = get_column_id(i); | |
1071 | ||
1072 | if (lsblk->sort_id != id) | |
1073 | data = device_get_data(dev, parent, id, NULL); | |
1074 | else { | |
1075 | uint64_t sortdata = (uint64_t) -1; | |
1076 | ||
1077 | data = device_get_data(dev, parent, id, &sortdata); | |
1078 | if (data && sortdata != (uint64_t) -1) | |
1079 | set_sortdata_u64(ln, i, sortdata); | |
1080 | } | |
1081 | if (data && scols_line_refer_data(ln, i, data)) | |
1082 | err(EXIT_FAILURE, _("failed to add output data")); | |
1083 | } | |
f43b8297 | 1084 | |
0bd05f5e KZ |
1085 | dev->scols_line = ln; |
1086 | ||
ff7992d6 KZ |
1087 | if (dev->npartitions == 0) |
1088 | /* For partitions we often read from parental whole-disk sysfs, | |
1089 | * otherwise we can close */ | |
1090 | ul_path_close_dirfd(dev->sysfs); | |
1091 | ||
f43b8297 | 1092 | |
0bd05f5e | 1093 | lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD); |
f43b8297 | 1094 | while (lsblk_device_next_child(dev, &itr, &child) == 0) |
d81e32bd | 1095 | device_to_scols(child, dev, tab, ln); |
ff7992d6 KZ |
1096 | |
1097 | /* Let's be careful with number of open files */ | |
1098 | ul_path_close_dirfd(dev->sysfs); | |
f43b8297 KZ |
1099 | } |
1100 | ||
b93585e8 KZ |
1101 | /* |
1102 | * Walks on tree and adds one line for each device to the smartcols table | |
1103 | */ | |
f43b8297 KZ |
1104 | static void devtree_to_scols(struct lsblk_devtree *tr, struct libscols_table *tab) |
1105 | { | |
1106 | struct lsblk_iter itr; | |
1107 | struct lsblk_device *dev = NULL; | |
1108 | ||
1109 | lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD); | |
1110 | ||
1111 | while (lsblk_devtree_next_root(tr, &itr, &dev) == 0) | |
d81e32bd | 1112 | device_to_scols(dev, NULL, tab, NULL); |
2a4c734b MB |
1113 | } |
1114 | ||
b93585e8 KZ |
1115 | /* |
1116 | * Reads very basic information about the device from sysfs into the device struct | |
1117 | */ | |
b158bd29 | 1118 | static int initialize_device(struct lsblk_device *dev, |
fed34a1e | 1119 | struct lsblk_device *wholedisk, |
09a71aa1 | 1120 | const char *name) |
2a4c734b | 1121 | { |
766ab9b0 | 1122 | dev_t devno; |
2a4c734b | 1123 | |
b158bd29 KZ |
1124 | DBG(DEV, ul_debugobj(dev, "initialize %s [wholedisk=%p %s]", |
1125 | name, wholedisk, wholedisk ? wholedisk->name : "")); | |
7e786eca | 1126 | |
fed34a1e | 1127 | dev->name = xstrdup(name); |
10501add KZ |
1128 | |
1129 | if (wholedisk) { | |
1130 | dev->wholedisk = wholedisk; | |
1131 | lsblk_ref_device(wholedisk); | |
1132 | } | |
2a4c734b | 1133 | |
fed34a1e KZ |
1134 | dev->filename = get_device_path(dev); |
1135 | if (!dev->filename) { | |
3bf2a36c | 1136 | DBG(DEV, ul_debugobj(dev, "%s: failed to get device path", dev->name)); |
270e66ec MB |
1137 | return -1; |
1138 | } | |
3bf2a36c | 1139 | DBG(DEV, ul_debugobj(dev, "%s: filename=%s", dev->name, dev->filename)); |
2a4c734b | 1140 | |
fed34a1e | 1141 | devno = __sysfs_devname_to_devno(lsblk->sysroot, dev->name, wholedisk ? wholedisk->name : NULL); |
270e66ec | 1142 | if (!devno) { |
3bf2a36c | 1143 | DBG(DEV, ul_debugobj(dev, "%s: unknown device name", dev->name)); |
270e66ec MB |
1144 | return -1; |
1145 | } | |
2a4c734b | 1146 | |
0e086f82 KZ |
1147 | dev->sysfs = ul_new_sysfs_path(devno, wholedisk ? wholedisk->sysfs : NULL, lsblk->sysroot); |
1148 | if (!dev->sysfs) { | |
1149 | DBG(DEV, ul_debugobj(dev, "%s: failed to initialize sysfs handler", dev->name)); | |
1150 | return -1; | |
270e66ec | 1151 | } |
766ab9b0 | 1152 | |
fed34a1e KZ |
1153 | dev->maj = major(devno); |
1154 | dev->min = minor(devno); | |
1155 | dev->size = 0; | |
2a4c734b | 1156 | |
fed34a1e KZ |
1157 | if (ul_path_read_u64(dev->sysfs, &dev->size, "size") == 0) /* in sectors */ |
1158 | dev->size <<= 9; /* in bytes */ | |
90e9fcda | 1159 | |
2a4c734b | 1160 | /* Ignore devices of zero size */ |
fed34a1e | 1161 | if (!lsblk->all_devices && dev->size == 0) { |
3bf2a36c | 1162 | DBG(DEV, ul_debugobj(dev, "zero size device -- ignore")); |
2a4c734b | 1163 | return -1; |
7e786eca | 1164 | } |
fed34a1e KZ |
1165 | if (is_dm(dev->name)) { |
1166 | ul_path_read_string(dev->sysfs, &dev->dm_name, "dm/name"); | |
1167 | if (!dev->dm_name) { | |
3bf2a36c | 1168 | DBG(DEV, ul_debugobj(dev, "%s: failed to get dm name", dev->name)); |
270e66ec MB |
1169 | return -1; |
1170 | } | |
766ab9b0 | 1171 | } |
2a4c734b | 1172 | |
fed34a1e KZ |
1173 | dev->npartitions = sysfs_blkdev_count_partitions(dev->sysfs, dev->name); |
1174 | dev->nholders = ul_path_count_dirents(dev->sysfs, "holders"); | |
1175 | dev->nslaves = ul_path_count_dirents(dev->sysfs, "slaves"); | |
2a4c734b | 1176 | |
3bf2a36c | 1177 | DBG(DEV, ul_debugobj(dev, "%s: npartitions=%d, nholders=%d, nslaves=%d", |
fed34a1e | 1178 | dev->name, dev->npartitions, dev->nholders, dev->nslaves)); |
7e786eca | 1179 | |
28ffc2b7 | 1180 | /* ignore non-SCSI devices */ |
fed34a1e | 1181 | if (lsblk->scsi && sysfs_blkdev_scsi_get_hctl(dev->sysfs, NULL, NULL, NULL, NULL)) { |
3bf2a36c | 1182 | DBG(DEV, ul_debugobj(dev, "non-scsi device -- ignore")); |
28ffc2b7 | 1183 | return -1; |
7e786eca | 1184 | } |
28ffc2b7 | 1185 | |
3bf2a36c | 1186 | DBG(DEV, ul_debugobj(dev, "%s: context successfully initialized", dev->name)); |
2a4c734b MB |
1187 | return 0; |
1188 | } | |
1189 | ||
4ca3c472 | 1190 | static struct lsblk_device *devtree_get_device_or_new(struct lsblk_devtree *tr, |
583b6489 KZ |
1191 | struct lsblk_device *disk, |
1192 | const char *name) | |
1193 | { | |
1194 | struct lsblk_device *dev = lsblk_devtree_get_device(tr, name); | |
1195 | ||
1196 | if (!dev) { | |
10501add | 1197 | dev = lsblk_new_device(); |
583b6489 KZ |
1198 | if (!dev) |
1199 | err(EXIT_FAILURE, _("failed to allocate device")); | |
1200 | ||
b158bd29 | 1201 | if (initialize_device(dev, disk, name) != 0) { |
583b6489 KZ |
1202 | lsblk_unref_device(dev); |
1203 | return NULL; | |
1204 | } | |
1205 | lsblk_devtree_add_device(tr, dev); | |
1206 | lsblk_unref_device(dev); /* keep it referenced by devtree only */ | |
1207 | } else | |
1208 | DBG(DEV, ul_debugobj(dev, "%s: already processed", name)); | |
1209 | ||
1210 | return dev; | |
1211 | } | |
a0a76f42 | 1212 | |
4ca3c472 KZ |
1213 | static int process_dependencies( |
1214 | struct lsblk_devtree *tr, | |
1215 | struct lsblk_device *dev, | |
1216 | int do_partitions); | |
09a71aa1 | 1217 | |
2a4c734b | 1218 | /* |
4ca3c472 | 1219 | * Read devices from whole-disk device into tree |
2a4c734b | 1220 | */ |
cf3c1bc7 | 1221 | static int process_partitions(struct lsblk_devtree *tr, struct lsblk_device *disk) |
2a4c734b MB |
1222 | { |
1223 | DIR *dir; | |
1224 | struct dirent *d; | |
2a4c734b | 1225 | |
cf3c1bc7 | 1226 | assert(disk); |
f31505fe | 1227 | |
09a71aa1 PR |
1228 | /* |
1229 | * Do not process further if there are no partitions for | |
1230 | * this device or the device itself is a partition. | |
1231 | */ | |
10501add | 1232 | if (!disk->npartitions || device_is_partition(disk)) |
cf3c1bc7 | 1233 | return -EINVAL; |
2a4c734b | 1234 | |
cf3c1bc7 | 1235 | DBG(DEV, ul_debugobj(disk, "%s: probe whole-disk for partitions", disk->name)); |
7e786eca | 1236 | |
cf3c1bc7 | 1237 | dir = ul_path_opendir(disk->sysfs, NULL); |
2a4c734b MB |
1238 | if (!dir) |
1239 | err(EXIT_FAILURE, _("failed to open device directory in sysfs")); | |
1240 | ||
1241 | while ((d = xreaddir(dir))) { | |
4ca3c472 | 1242 | struct lsblk_device *part; |
2a4c734b | 1243 | |
cf3c1bc7 | 1244 | if (!(sysfs_blkdev_is_partition_dirent(dir, d, disk->name))) |
270e66ec | 1245 | continue; |
09a71aa1 | 1246 | |
cf3c1bc7 | 1247 | DBG(DEV, ul_debugobj(disk, " checking %s", d->d_name)); |
7e786eca | 1248 | |
cf3c1bc7 | 1249 | part = devtree_get_device_or_new(tr, disk, d->d_name); |
4ca3c472 KZ |
1250 | if (!part) |
1251 | continue; | |
1252 | ||
cf3c1bc7 | 1253 | if (lsblk_device_new_dependence(disk, part) == 0) |
4ca3c472 | 1254 | process_dependencies(tr, part, 0); |
ff7992d6 KZ |
1255 | |
1256 | ul_path_close_dirfd(part->sysfs); | |
2a4c734b | 1257 | } |
09a71aa1 | 1258 | |
ff7992d6 KZ |
1259 | /* For partitions we need parental (whole-disk) sysfs directory pretty |
1260 | * often, so close it now when all is done */ | |
1261 | ul_path_close_dirfd(disk->sysfs); | |
1262 | ||
cf3c1bc7 | 1263 | DBG(DEV, ul_debugobj(disk, "probe whole-disk for partitions -- done")); |
2a4c734b | 1264 | closedir(dir); |
4ca3c472 | 1265 | return 0; |
09a71aa1 PR |
1266 | } |
1267 | ||
4ca3c472 | 1268 | static char *get_wholedisk_from_partition_dirent(DIR *dir, struct dirent *d, char *buf, size_t bufsz) |
09a71aa1 | 1269 | { |
09a71aa1 PR |
1270 | char *p; |
1271 | int len; | |
1272 | ||
4ca3c472 | 1273 | if ((len = readlinkat(dirfd(dir), d->d_name, buf, bufsz - 1)) < 0) |
09a71aa1 | 1274 | return 0; |
540d506f | 1275 | |
4ca3c472 | 1276 | buf[len] = '\0'; |
09a71aa1 PR |
1277 | |
1278 | /* The path ends with ".../<device>/<partition>" */ | |
4ca3c472 | 1279 | p = strrchr(buf, '/'); |
540d506f | 1280 | if (!p) |
4ca3c472 | 1281 | return NULL; |
540d506f KZ |
1282 | *p = '\0'; |
1283 | ||
4ca3c472 | 1284 | p = strrchr(buf, '/'); |
540d506f | 1285 | if (!p) |
4ca3c472 | 1286 | return NULL; |
540d506f | 1287 | p++; |
2a4c734b | 1288 | |
4ca3c472 | 1289 | return p; |
09a71aa1 PR |
1290 | } |
1291 | ||
1292 | /* | |
b93585e8 | 1293 | * Reads slaves/holders and partitions for specified device into device tree |
09a71aa1 | 1294 | */ |
4ca3c472 KZ |
1295 | static int process_dependencies( |
1296 | struct lsblk_devtree *tr, | |
1297 | struct lsblk_device *dev, | |
1298 | int do_partitions) | |
09a71aa1 PR |
1299 | { |
1300 | DIR *dir; | |
1301 | struct dirent *d; | |
540d506f | 1302 | const char *depname; |
09a71aa1 | 1303 | |
fed34a1e | 1304 | assert(dev); |
09a71aa1 | 1305 | |
57a06133 KZ |
1306 | if (lsblk->nodeps) |
1307 | return 0; | |
1308 | ||
4ca3c472 KZ |
1309 | /* read all or specified partition */ |
1310 | if (do_partitions && dev->npartitions) | |
1311 | process_partitions(tr, dev); | |
1312 | ||
4ca3c472 | 1313 | DBG(DEV, ul_debugobj(dev, "%s: reading dependencies", dev->name)); |
7e786eca | 1314 | |
4ca3c472 KZ |
1315 | if (!(lsblk->inverse ? dev->nslaves : dev->nholders)) { |
1316 | DBG(DEV, ul_debugobj(dev, " ignore (no slaves/holders)")); | |
09a71aa1 | 1317 | return 0; |
4ca3c472 | 1318 | } |
09a71aa1 | 1319 | |
540d506f | 1320 | depname = lsblk->inverse ? "slaves" : "holders"; |
fed34a1e | 1321 | dir = ul_path_opendir(dev->sysfs, depname); |
4ca3c472 KZ |
1322 | if (!dir) { |
1323 | DBG(DEV, ul_debugobj(dev, " ignore (no slaves/holders directory)")); | |
2a4c734b | 1324 | return 0; |
4ca3c472 | 1325 | } |
ff7992d6 | 1326 | ul_path_close_dirfd(dev->sysfs); |
2a4c734b | 1327 | |
4ca3c472 | 1328 | DBG(DEV, ul_debugobj(dev, " %s: checking for '%s' dependence", dev->name, depname)); |
7e786eca | 1329 | |
2a4c734b | 1330 | while ((d = xreaddir(dir))) { |
ff7992d6 KZ |
1331 | struct lsblk_device *dep = NULL; |
1332 | struct lsblk_device *disk = NULL; | |
4ca3c472 | 1333 | |
09a71aa1 | 1334 | /* Is the dependency a partition? */ |
f153614c | 1335 | if (sysfs_blkdev_is_partition_dirent(dir, d, NULL)) { |
4ca3c472 KZ |
1336 | |
1337 | char buf[PATH_MAX]; | |
1338 | char *diskname; | |
4ca3c472 KZ |
1339 | |
1340 | DBG(DEV, ul_debugobj(dev, " %s: dependence is partition", d->d_name)); | |
1341 | ||
1342 | diskname = get_wholedisk_from_partition_dirent(dir, d, buf, sizeof(buf)); | |
1343 | if (diskname) | |
0e086f82 | 1344 | disk = devtree_get_device_or_new(tr, NULL, diskname); |
4ca3c472 KZ |
1345 | if (!disk) { |
1346 | DBG(DEV, ul_debugobj(dev, " ignore no wholedisk ???")); | |
ff7992d6 | 1347 | goto next; |
7e786eca | 1348 | } |
4ca3c472 | 1349 | |
0e086f82 | 1350 | dep = devtree_get_device_or_new(tr, disk, d->d_name); |
4ca3c472 | 1351 | if (!dep) |
ff7992d6 | 1352 | goto next; |
4ca3c472 KZ |
1353 | |
1354 | if (lsblk_device_new_dependence(dev, dep) == 0) | |
1355 | process_dependencies(tr, dep, 1); | |
1356 | ||
1357 | if (lsblk->inverse | |
1358 | && lsblk_device_new_dependence(dep, disk) == 0) | |
1359 | process_dependencies(tr, disk, 0); | |
270e66ec | 1360 | } |
09a71aa1 | 1361 | /* The dependency is a whole device. */ |
4ca3c472 KZ |
1362 | else { |
1363 | DBG(DEV, ul_debugobj(dev, " %s: %s: dependence is whole-disk", | |
fed34a1e | 1364 | dev->name, d->d_name)); |
4ca3c472 | 1365 | |
0e086f82 | 1366 | dep = devtree_get_device_or_new(tr, NULL, d->d_name); |
4ca3c472 | 1367 | if (!dep) |
ff7992d6 | 1368 | goto next; |
4ca3c472 KZ |
1369 | |
1370 | if (lsblk_device_new_dependence(dev, dep) == 0) | |
1371 | /* For inverse tree we don't want to show partitions | |
1372 | * if the dependence is on whole-disk */ | |
1373 | process_dependencies(tr, dep, lsblk->inverse ? 0 : 1); | |
7e786eca | 1374 | } |
ff7992d6 KZ |
1375 | next: |
1376 | if (dep && dep->sysfs) | |
1377 | ul_path_close_dirfd(dep->sysfs); | |
1378 | if (disk && disk->sysfs) | |
1379 | ul_path_close_dirfd(disk->sysfs); | |
2a4c734b MB |
1380 | } |
1381 | closedir(dir); | |
1382 | ||
3bf2a36c | 1383 | DBG(DEV, ul_debugobj(dev, "%s: checking for '%s' -- done", dev->name, depname)); |
2a4c734b MB |
1384 | return 0; |
1385 | } | |
1386 | ||
b93585e8 KZ |
1387 | /* |
1388 | * Defines the device as root node in the device tree and walks on all dependencies of the device. | |
1389 | */ | |
e7b5353e | 1390 | static int __process_one_device(struct lsblk_devtree *tr, char *devname, dev_t devno) |
2a4c734b | 1391 | { |
291606fa | 1392 | struct lsblk_device *dev = NULL; |
ff7992d6 | 1393 | struct lsblk_device *disk = NULL; |
1539750f | 1394 | char buf[PATH_MAX + 1], *name = NULL, *diskname = NULL; |
1539750f | 1395 | int real_part = 0, rc = -EINVAL; |
2a4c734b | 1396 | |
e7b5353e KZ |
1397 | if (devno == 0) { |
1398 | struct stat st; | |
1399 | ||
1400 | DBG(DEV, ul_debug("%s: reading alone device", devname)); | |
1401 | ||
1402 | if (stat(devname, &st) || !S_ISBLK(st.st_mode)) { | |
1403 | warnx(_("%s: not a block device"), devname); | |
1404 | goto leave; | |
1405 | } | |
1406 | devno = st.st_rdev; | |
1407 | } else | |
1408 | DBG(DEV, ul_debug("%d:%d: reading alone device", major(devno), minor(devno))); | |
da30cb2a | 1409 | |
f153614c KZ |
1410 | /* TODO: sysfs_devno_to_devname() internally initializes path_cxt, it |
1411 | * would be better to use ul_new_sysfs_path() + sysfs_blkdev_get_name() | |
b158bd29 | 1412 | * and reuse path_cxt for initialize_device() |
f153614c | 1413 | */ |
e7b5353e | 1414 | name = sysfs_devno_to_devname(devno, buf, sizeof(buf)); |
f153614c | 1415 | if (!name) { |
e7b5353e KZ |
1416 | if (devname) |
1417 | warn(_("%s: failed to get sysfs name"), devname); | |
1539750f | 1418 | goto leave; |
2a4c734b | 1419 | } |
3273ce07 | 1420 | name = xstrdup(name); |
da30cb2a PR |
1421 | |
1422 | if (!strncmp(name, "dm-", 3)) { | |
1423 | /* dm mapping is never a real partition! */ | |
1424 | real_part = 0; | |
1425 | } else { | |
ff7992d6 KZ |
1426 | dev_t diskno = 0; |
1427 | ||
1428 | if (blkid_devno_to_wholedisk(devno, buf, sizeof(buf), &diskno)) { | |
e7b5353e | 1429 | warn(_("%s: failed to get whole-disk device number"), name); |
1539750f | 1430 | goto leave; |
da30cb2a PR |
1431 | } |
1432 | diskname = buf; | |
ff7992d6 | 1433 | real_part = devno != diskno; |
da30cb2a PR |
1434 | } |
1435 | ||
1436 | if (!real_part) { | |
2a4c734b | 1437 | /* |
09a71aa1 | 1438 | * Device is not a partition. |
2a4c734b | 1439 | */ |
4ca3c472 KZ |
1440 | DBG(DEV, ul_debug(" non-partition")); |
1441 | ||
0e086f82 | 1442 | dev = devtree_get_device_or_new(tr, NULL, name); |
291606fa | 1443 | if (!dev) |
270e66ec | 1444 | goto leave; |
291606fa | 1445 | |
4ca3c472 KZ |
1446 | lsblk_devtree_add_root(tr, dev); |
1447 | process_dependencies(tr, dev, !lsblk->inverse); | |
270e66ec | 1448 | } else { |
2a4c734b | 1449 | /* |
291606fa | 1450 | * Partition, read sysfs name of the disk device |
2a4c734b | 1451 | */ |
4ca3c472 KZ |
1452 | DBG(DEV, ul_debug(" partition")); |
1453 | ||
0e086f82 KZ |
1454 | disk = devtree_get_device_or_new(tr, NULL, diskname); |
1455 | if (!disk) | |
270e66ec | 1456 | goto leave; |
291606fa | 1457 | |
0e086f82 | 1458 | dev = devtree_get_device_or_new(tr, disk, name); |
291606fa | 1459 | if (!dev) |
270e66ec | 1460 | goto leave; |
09a71aa1 | 1461 | |
4ca3c472 KZ |
1462 | lsblk_devtree_add_root(tr, dev); |
1463 | process_dependencies(tr, dev, 1); | |
291606fa | 1464 | |
4ca3c472 | 1465 | if (lsblk->inverse |
0e086f82 KZ |
1466 | && lsblk_device_new_dependence(dev, disk) == 0) |
1467 | process_dependencies(tr, disk, 0); | |
ff7992d6 KZ |
1468 | else |
1469 | ul_path_close_dirfd(disk->sysfs); | |
2a4c734b MB |
1470 | } |
1471 | ||
1539750f | 1472 | rc = 0; |
270e66ec | 1473 | leave: |
ff7992d6 KZ |
1474 | if (dev && dev->sysfs) |
1475 | ul_path_close_dirfd(dev->sysfs); | |
1476 | if (disk && disk->sysfs) | |
1477 | ul_path_close_dirfd(disk->sysfs); | |
da30cb2a | 1478 | free(name); |
1539750f | 1479 | return rc; |
2a4c734b MB |
1480 | } |
1481 | ||
e7b5353e KZ |
1482 | static int process_one_device(struct lsblk_devtree *tr, char *devname) |
1483 | { | |
1484 | return __process_one_device(tr, devname, 0); | |
1485 | } | |
1486 | ||
1487 | /* | |
1488 | * The /sys/block contains only root devices, and no partitions. It seems more | |
1489 | * simple to scan /sys/dev/block where are all devices without exceptions to get | |
1490 | * top-level devices for the reverse tree. | |
1491 | */ | |
1492 | static int process_all_devices_inverse(struct lsblk_devtree *tr) | |
e15c9e3d KZ |
1493 | { |
1494 | DIR *dir; | |
1495 | struct dirent *d; | |
e7b5353e KZ |
1496 | struct path_cxt *pc = ul_new_path(_PATH_SYS_DEVBLOCK); |
1497 | ||
1498 | assert(lsblk->inverse); | |
e15c9e3d KZ |
1499 | |
1500 | if (!pc) | |
1501 | err(EXIT_FAILURE, _("failed to allocate /sys handler")); | |
1502 | ||
1503 | ul_path_set_prefix(pc, lsblk->sysroot); | |
e7b5353e KZ |
1504 | dir = ul_path_opendir(pc, NULL); |
1505 | if (!dir) | |
1506 | goto done; | |
1507 | ||
1508 | DBG(DEV, ul_debug("iterate on " _PATH_SYS_DEVBLOCK)); | |
1509 | ||
1510 | while ((d = xreaddir(dir))) { | |
1511 | dev_t devno; | |
1512 | int maj, min; | |
e15c9e3d | 1513 | |
e7b5353e KZ |
1514 | DBG(DEV, ul_debug(" %s dentry", d->d_name)); |
1515 | ||
1516 | if (sscanf(d->d_name, "%d:%d", &maj, &min) != 2) | |
1517 | continue; | |
1518 | devno = makedev(maj, min); | |
1519 | ||
1520 | if (is_maj_excluded(maj) || !is_maj_included(maj)) | |
1521 | continue; | |
1522 | if (ul_path_countf_dirents(pc, "%s/holders", d->d_name) != 0) | |
1523 | continue; | |
1524 | if (sysfs_devno_count_partitions(devno) != 0) | |
1525 | continue; | |
1526 | __process_one_device(tr, NULL, devno); | |
1527 | } | |
1528 | ||
1529 | closedir(dir); | |
1530 | done: | |
1531 | ul_unref_path(pc); | |
1532 | DBG(DEV, ul_debug("iterate on " _PATH_SYS_DEVBLOCK " -- done")); | |
1533 | return 0; | |
1534 | } | |
1535 | ||
b93585e8 KZ |
1536 | /* |
1537 | * Reads root nodes (devices) from /sys/block into devices tree | |
1538 | */ | |
e7b5353e KZ |
1539 | static int process_all_devices(struct lsblk_devtree *tr) |
1540 | { | |
1541 | DIR *dir; | |
1542 | struct dirent *d; | |
1543 | struct path_cxt *pc; | |
1544 | ||
1545 | assert(lsblk->inverse == 0); | |
1546 | ||
1547 | pc = ul_new_path(_PATH_SYS_BLOCK); | |
1548 | if (!pc) | |
1549 | err(EXIT_FAILURE, _("failed to allocate /sys handler")); | |
1550 | ||
1551 | ul_path_set_prefix(pc, lsblk->sysroot); | |
e15c9e3d KZ |
1552 | dir = ul_path_opendir(pc, NULL); |
1553 | if (!dir) | |
1554 | goto done; | |
1555 | ||
e7b5353e | 1556 | DBG(DEV, ul_debug("iterate on " _PATH_SYS_BLOCK)); |
e15c9e3d KZ |
1557 | |
1558 | while ((d = xreaddir(dir))) { | |
ff7992d6 | 1559 | struct lsblk_device *dev = NULL; |
e15c9e3d KZ |
1560 | |
1561 | DBG(DEV, ul_debug(" %s dentry", d->d_name)); | |
e15c9e3d KZ |
1562 | dev = devtree_get_device_or_new(tr, NULL, d->d_name); |
1563 | if (!dev) | |
ff7992d6 | 1564 | goto next; |
e15c9e3d KZ |
1565 | |
1566 | /* remove unwanted devices */ | |
1567 | if (is_maj_excluded(dev->maj) || !is_maj_included(dev->maj)) { | |
1568 | DBG(DEV, ul_debug(" %s: ignore (by filter)", d->d_name)); | |
1569 | lsblk_devtree_remove_device(tr, dev); | |
ff7992d6 | 1570 | goto next; |
e15c9e3d KZ |
1571 | } |
1572 | ||
e7b5353e KZ |
1573 | if (dev->nslaves) { |
1574 | DBG(DEV, ul_debug(" %s: ignore (in-middle)", d->d_name)); | |
ff7992d6 | 1575 | goto next; |
e15c9e3d | 1576 | } |
0bd05f5e | 1577 | |
e7b5353e KZ |
1578 | lsblk_devtree_add_root(tr, dev); |
1579 | process_dependencies(tr, dev, 1); | |
ff7992d6 KZ |
1580 | next: |
1581 | /* Let's be careful with number of open files */ | |
1582 | if (dev && dev->sysfs) | |
1583 | ul_path_close_dirfd(dev->sysfs); | |
e15c9e3d KZ |
1584 | } |
1585 | ||
1586 | closedir(dir); | |
1587 | done: | |
1588 | ul_unref_path(pc); | |
1589 | DBG(DEV, ul_debug("iterate on " _PATH_SYS_BLOCK " -- done")); | |
1590 | return 0; | |
1591 | } | |
1592 | ||
b93585e8 KZ |
1593 | /* |
1594 | * Parses major numbers as specified on lsblk command line | |
1595 | */ | |
2dc03af7 | 1596 | static void parse_excludes(const char *str0) |
2a4c734b | 1597 | { |
2dc03af7 KZ |
1598 | const char *str = str0; |
1599 | ||
2a4c734b MB |
1600 | while (str && *str) { |
1601 | char *end = NULL; | |
ed34643c | 1602 | unsigned long n; |
2a4c734b MB |
1603 | |
1604 | errno = 0; | |
1605 | n = strtoul(str, &end, 10); | |
1606 | ||
2dc03af7 KZ |
1607 | if (end == str || (end && *end && *end != ',')) |
1608 | errx(EXIT_FAILURE, _("failed to parse list '%s'"), str0); | |
1609 | if (errno != 0 && (n == ULONG_MAX || n == 0)) | |
1610 | err(EXIT_FAILURE, _("failed to parse list '%s'"), str0); | |
2a4c734b MB |
1611 | excludes[nexcludes++] = n; |
1612 | ||
1613 | if (nexcludes == ARRAY_SIZE(excludes)) | |
f14f02f5 | 1614 | /* TRANSLATORS: The standard value for %d is 256. */ |
2a4c734b MB |
1615 | errx(EXIT_FAILURE, _("the list of excluded devices is " |
1616 | "too large (limit is %d devices)"), | |
1617 | (int)ARRAY_SIZE(excludes)); | |
2dc03af7 | 1618 | |
2a4c734b MB |
1619 | str = end && *end ? end + 1 : NULL; |
1620 | } | |
1621 | } | |
1622 | ||
b93585e8 KZ |
1623 | /* |
1624 | * Parses major numbers as specified on lsblk command line | |
1625 | * (TODO: what about refactor and merge parse_excludes() and parse_includes().) | |
1626 | */ | |
2dc03af7 | 1627 | static void parse_includes(const char *str0) |
2ef4e8ba | 1628 | { |
2dc03af7 KZ |
1629 | const char *str = str0; |
1630 | ||
2ef4e8ba KZ |
1631 | while (str && *str) { |
1632 | char *end = NULL; | |
1633 | unsigned long n; | |
1634 | ||
1635 | errno = 0; | |
1636 | n = strtoul(str, &end, 10); | |
1637 | ||
2dc03af7 KZ |
1638 | if (end == str || (end && *end && *end != ',')) |
1639 | errx(EXIT_FAILURE, _("failed to parse list '%s'"), str0); | |
1640 | if (errno != 0 && (n == ULONG_MAX || n == 0)) | |
1641 | err(EXIT_FAILURE, _("failed to parse list '%s'"), str0); | |
2ef4e8ba KZ |
1642 | includes[nincludes++] = n; |
1643 | ||
1644 | if (nincludes == ARRAY_SIZE(includes)) | |
1645 | /* TRANSLATORS: The standard value for %d is 256. */ | |
1646 | errx(EXIT_FAILURE, _("the list of included devices is " | |
1647 | "too large (limit is %d devices)"), | |
1648 | (int)ARRAY_SIZE(includes)); | |
1649 | str = end && *end ? end + 1 : NULL; | |
1650 | } | |
1651 | } | |
1652 | ||
642048e4 KZ |
1653 | /* |
1654 | * see set_sortdata_u64() and columns initialization in main() | |
1655 | */ | |
1656 | static int cmp_u64_cells(struct libscols_cell *a, | |
1657 | struct libscols_cell *b, | |
1658 | __attribute__((__unused__)) void *data) | |
1659 | { | |
1660 | uint64_t *adata = (uint64_t *) scols_cell_get_userdata(a), | |
1661 | *bdata = (uint64_t *) scols_cell_get_userdata(b); | |
1662 | ||
1663 | if (adata == NULL && bdata == NULL) | |
1664 | return 0; | |
1665 | if (adata == NULL) | |
1666 | return -1; | |
1667 | if (bdata == NULL) | |
1668 | return 1; | |
1669 | return *adata == *bdata ? 0 : *adata >= *bdata ? 1 : -1; | |
1670 | } | |
1671 | ||
dc4662f0 KZ |
1672 | static void device_set_dedupkey( |
1673 | struct lsblk_device *dev, | |
1674 | struct lsblk_device *parent, | |
1675 | int id) | |
1676 | { | |
1677 | struct lsblk_iter itr; | |
1678 | struct lsblk_device *child = NULL; | |
1679 | ||
1680 | dev->dedupkey = device_get_data(dev, parent, id, NULL); | |
1681 | if (dev->dedupkey) | |
1682 | DBG(DEV, ul_debugobj(dev, "%s: de-duplication key: %s", dev->name, dev->dedupkey)); | |
1683 | ||
1684 | if (dev->npartitions == 0) | |
1685 | /* For partitions we often read from parental whole-disk sysfs, | |
1686 | * otherwise we can close */ | |
1687 | ul_path_close_dirfd(dev->sysfs); | |
1688 | ||
1689 | lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD); | |
1690 | ||
1691 | while (lsblk_device_next_child(dev, &itr, &child) == 0) | |
1692 | device_set_dedupkey(child, dev, id); | |
1693 | ||
1694 | /* Let's be careful with number of open files */ | |
1695 | ul_path_close_dirfd(dev->sysfs); | |
1696 | } | |
1697 | ||
1698 | static void devtree_set_dedupkeys(struct lsblk_devtree *tr, int id) | |
1699 | { | |
1700 | struct lsblk_iter itr; | |
1701 | struct lsblk_device *dev = NULL; | |
1702 | ||
1703 | lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD); | |
1704 | ||
1705 | while (lsblk_devtree_next_root(tr, &itr, &dev) == 0) | |
1706 | device_set_dedupkey(dev, NULL, id); | |
1707 | } | |
1708 | ||
86be6a32 | 1709 | static void __attribute__((__noreturn__)) usage(void) |
2a4c734b | 1710 | { |
86be6a32 | 1711 | FILE *out = stdout; |
507d39c2 | 1712 | size_t i; |
2a4c734b | 1713 | |
39b232c1 SK |
1714 | fputs(USAGE_HEADER, out); |
1715 | fprintf(out, _(" %s [options] [<device> ...]\n"), program_invocation_short_name); | |
451dbcfa BS |
1716 | |
1717 | fputs(USAGE_SEPARATOR, out); | |
1718 | fputs(_("List information about block devices.\n"), out); | |
1719 | ||
39b232c1 | 1720 | fputs(USAGE_OPTIONS, out); |
0bd05f5e KZ |
1721 | fputs(_(" -D, --discard print discard capabilities\n"), out); |
1722 | fputs(_(" -E, --dedup <column> de-duplicate output by <column>\n"), out); | |
1723 | fputs(_(" -I, --include <list> show only devices with specified major numbers\n"), out); | |
1724 | fputs(_(" -J, --json use JSON output format\n"), out); | |
1725 | fputs(_(" -O, --output-all output all columns\n"), out); | |
1726 | fputs(_(" -P, --pairs use key=\"value\" output format\n"), out); | |
1727 | fputs(_(" -S, --scsi output info about SCSI devices\n"), out); | |
b9c088f2 | 1728 | fputs(_(" -T, --tree[=<column>] use tree format output\n"), out); |
39b232c1 SK |
1729 | fputs(_(" -a, --all print all devices\n"), out); |
1730 | fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out); | |
1731 | fputs(_(" -d, --nodeps don't print slaves or holders\n"), out); | |
39b232c1 | 1732 | fputs(_(" -e, --exclude <list> exclude devices by major number (default: RAM disks)\n"), out); |
39b232c1 | 1733 | fputs(_(" -f, --fs output info about filesystems\n"), out); |
39b232c1 | 1734 | fputs(_(" -i, --ascii use ascii characters only\n"), out); |
39b232c1 | 1735 | fputs(_(" -l, --list use list format output\n"), out); |
0bd05f5e | 1736 | fputs(_(" -M, --merge group parents of sub-trees (usable for RAIDs, Multi-path)\n"), out); |
3e59c481 | 1737 | fputs(_(" -m, --perms output info about permissions\n"), out); |
39b232c1 SK |
1738 | fputs(_(" -n, --noheadings don't print headings\n"), out); |
1739 | fputs(_(" -o, --output <list> output columns\n"), out); | |
ef75bc88 | 1740 | fputs(_(" -p, --paths print complete device path\n"), out); |
39b232c1 SK |
1741 | fputs(_(" -r, --raw use raw output format\n"), out); |
1742 | fputs(_(" -s, --inverse inverse dependencies\n"), out); | |
3e59c481 | 1743 | fputs(_(" -t, --topology output info about topology\n"), out); |
0bd05f5e | 1744 | fputs(_(" -z, --zoned print zone model\n"), out); |
b5af3ee8 | 1745 | fputs(_(" -x, --sort <column> sort output by <column>\n"), out); |
498d1486 | 1746 | fputs(_(" --sysroot <dir> use specified directory as system root\n"), out); |
39b232c1 | 1747 | fputs(USAGE_SEPARATOR, out); |
f45f3ec3 | 1748 | printf(USAGE_HELP_OPTIONS(22)); |
2a4c734b | 1749 | |
c3a4cfc5 | 1750 | fprintf(out, USAGE_COLUMNS); |
2a4c734b | 1751 | |
03df0d11 | 1752 | for (i = 0; i < ARRAY_SIZE(infos); i++) |
57912387 | 1753 | fprintf(out, " %11s %s\n", infos[i].name, _(infos[i].help)); |
2a4c734b | 1754 | |
f45f3ec3 | 1755 | printf(USAGE_MAN_TAIL("lsblk(8)")); |
2a4c734b | 1756 | |
86be6a32 | 1757 | exit(EXIT_SUCCESS); |
2a4c734b MB |
1758 | } |
1759 | ||
f65b3bb6 SK |
1760 | static void check_sysdevblock(void) |
1761 | { | |
1762 | if (access(_PATH_SYS_DEVBLOCK, R_OK) != 0) | |
1763 | err(EXIT_FAILURE, _("failed to access sysfs directory: %s"), | |
1764 | _PATH_SYS_DEVBLOCK); | |
1765 | } | |
1766 | ||
2a4c734b MB |
1767 | int main(int argc, char *argv[]) |
1768 | { | |
dc4662f0 KZ |
1769 | struct lsblk _ls = { |
1770 | .sort_id = -1, | |
1771 | .dedup_id = -1, | |
b9c088f2 KZ |
1772 | .flags = LSBLK_TREE, |
1773 | .tree_id = COL_NAME | |
dc4662f0 | 1774 | }; |
57502dd3 | 1775 | struct lsblk_devtree *tr = NULL; |
40b17508 | 1776 | int c, status = EXIT_FAILURE; |
43bca827 | 1777 | char *outarg = NULL; |
40b17508 | 1778 | size_t i; |
b9c088f2 | 1779 | int force_tree = 0, has_tree_col = 0; |
2a4c734b | 1780 | |
ffc27623 KZ |
1781 | enum { |
1782 | OPT_SYSROOT = CHAR_MAX + 1 | |
1783 | }; | |
1784 | ||
6c7d5ae9 | 1785 | static const struct option longopts[] = { |
87918040 SK |
1786 | { "all", no_argument, NULL, 'a' }, |
1787 | { "bytes", no_argument, NULL, 'b' }, | |
1788 | { "nodeps", no_argument, NULL, 'd' }, | |
1789 | { "discard", no_argument, NULL, 'D' }, | |
0bd05f5e | 1790 | { "dedup", required_argument, NULL, 'E' }, |
f2df4365 | 1791 | { "zoned", no_argument, NULL, 'z' }, |
87918040 SK |
1792 | { "help", no_argument, NULL, 'h' }, |
1793 | { "json", no_argument, NULL, 'J' }, | |
1794 | { "output", required_argument, NULL, 'o' }, | |
1795 | { "output-all", no_argument, NULL, 'O' }, | |
0bd05f5e | 1796 | { "merge", no_argument, NULL, 'M' }, |
87918040 SK |
1797 | { "perms", no_argument, NULL, 'm' }, |
1798 | { "noheadings", no_argument, NULL, 'n' }, | |
1799 | { "list", no_argument, NULL, 'l' }, | |
1800 | { "ascii", no_argument, NULL, 'i' }, | |
1801 | { "raw", no_argument, NULL, 'r' }, | |
1802 | { "inverse", no_argument, NULL, 's' }, | |
1803 | { "fs", no_argument, NULL, 'f' }, | |
1804 | { "exclude", required_argument, NULL, 'e' }, | |
1805 | { "include", required_argument, NULL, 'I' }, | |
1806 | { "topology", no_argument, NULL, 't' }, | |
1807 | { "paths", no_argument, NULL, 'p' }, | |
1808 | { "pairs", no_argument, NULL, 'P' }, | |
1809 | { "scsi", no_argument, NULL, 'S' }, | |
1810 | { "sort", required_argument, NULL, 'x' }, | |
ffc27623 | 1811 | { "sysroot", required_argument, NULL, OPT_SYSROOT }, |
b9c088f2 | 1812 | { "tree", optional_argument, NULL, 'T' }, |
87918040 SK |
1813 | { "version", no_argument, NULL, 'V' }, |
1814 | { NULL, 0, NULL, 0 }, | |
2a4c734b MB |
1815 | }; |
1816 | ||
a7349ee3 | 1817 | static const ul_excl_t excl[] = { /* rows and cols in ASCII order */ |
1b4d2a4a | 1818 | { 'D','O' }, |
98f2dc7a | 1819 | { 'I','e' }, |
4a102a48 | 1820 | { 'J', 'P', 'r' }, |
1b4d2a4a MY |
1821 | { 'O','S' }, |
1822 | { 'O','f' }, | |
1823 | { 'O','m' }, | |
1824 | { 'O','t' }, | |
1d9e35cc | 1825 | { 'P','T', 'l','r' }, |
98f2dc7a KZ |
1826 | { 0 } |
1827 | }; | |
1828 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
1829 | ||
2a4c734b MB |
1830 | setlocale(LC_ALL, ""); |
1831 | bindtextdomain(PACKAGE, LOCALEDIR); | |
1832 | textdomain(PACKAGE); | |
c05a80ca | 1833 | atexit(close_stdout); |
2a4c734b MB |
1834 | |
1835 | lsblk = &_ls; | |
2a4c734b | 1836 | |
7e786eca KZ |
1837 | lsblk_init_debug(); |
1838 | ||
98f2dc7a | 1839 | while((c = getopt_long(argc, argv, |
b9c088f2 | 1840 | "abdDzE:e:fhJlnMmo:OpPiI:rstVST:x:", longopts, NULL)) != -1) { |
98f2dc7a KZ |
1841 | |
1842 | err_exclusive_options(c, longopts, excl, excl_st); | |
1843 | ||
2a4c734b MB |
1844 | switch(c) { |
1845 | case 'a': | |
1846 | lsblk->all_devices = 1; | |
1847 | break; | |
1848 | case 'b': | |
1849 | lsblk->bytes = 1; | |
1850 | break; | |
f31505fe KZ |
1851 | case 'd': |
1852 | lsblk->nodeps = 1; | |
1853 | break; | |
2d232246 | 1854 | case 'D': |
be685b98 KZ |
1855 | add_uniq_column(COL_NAME); |
1856 | add_uniq_column(COL_DALIGN); | |
1857 | add_uniq_column(COL_DGRAN); | |
1858 | add_uniq_column(COL_DMAX); | |
1859 | add_uniq_column(COL_DZERO); | |
2d232246 | 1860 | break; |
f2df4365 | 1861 | case 'z': |
be685b98 KZ |
1862 | add_uniq_column(COL_NAME); |
1863 | add_uniq_column(COL_ZONED); | |
f2df4365 | 1864 | break; |
2a4c734b MB |
1865 | case 'e': |
1866 | parse_excludes(optarg); | |
1867 | break; | |
1868 | case 'h': | |
86be6a32 | 1869 | usage(); |
2a4c734b | 1870 | break; |
4a102a48 | 1871 | case 'J': |
341c4ae2 | 1872 | lsblk->flags |= LSBLK_JSON; |
4a102a48 | 1873 | break; |
2a4c734b | 1874 | case 'l': |
341c4ae2 | 1875 | lsblk->flags &= ~LSBLK_TREE; /* disable the default */ |
2a4c734b | 1876 | break; |
0bd05f5e KZ |
1877 | case 'M': |
1878 | lsblk->merge = 1; | |
1879 | break; | |
2a4c734b | 1880 | case 'n': |
341c4ae2 | 1881 | lsblk->flags |= LSBLK_NOHEADINGS; |
2a4c734b MB |
1882 | break; |
1883 | case 'o': | |
43bca827 | 1884 | outarg = optarg; |
2a4c734b | 1885 | break; |
1b4d2a4a | 1886 | case 'O': |
40b17508 | 1887 | for (ncolumns = 0 ; ncolumns < ARRAY_SIZE(infos); ncolumns++) |
1b4d2a4a MY |
1888 | columns[ncolumns] = ncolumns; |
1889 | break; | |
c7e76cd1 KZ |
1890 | case 'p': |
1891 | lsblk->paths = 1; | |
1892 | break; | |
6ea1bdd3 | 1893 | case 'P': |
341c4ae2 KZ |
1894 | lsblk->flags |= LSBLK_EXPORT; |
1895 | lsblk->flags &= ~LSBLK_TREE; /* disable the default */ | |
6ea1bdd3 | 1896 | break; |
2a4c734b | 1897 | case 'i': |
341c4ae2 | 1898 | lsblk->flags |= LSBLK_ASCII; |
2a4c734b | 1899 | break; |
2ef4e8ba | 1900 | case 'I': |
2ef4e8ba KZ |
1901 | parse_includes(optarg); |
1902 | break; | |
2a4c734b | 1903 | case 'r': |
341c4ae2 KZ |
1904 | lsblk->flags &= ~LSBLK_TREE; /* disable the default */ |
1905 | lsblk->flags |= LSBLK_RAW; /* enable raw */ | |
2a4c734b | 1906 | break; |
09a71aa1 PR |
1907 | case 's': |
1908 | lsblk->inverse = 1; | |
1909 | break; | |
2a4c734b | 1910 | case 'f': |
be685b98 KZ |
1911 | add_uniq_column(COL_NAME); |
1912 | add_uniq_column(COL_FSTYPE); | |
1913 | add_uniq_column(COL_LABEL); | |
1914 | add_uniq_column(COL_UUID); | |
bc405d64 KZ |
1915 | add_uniq_column(COL_FSAVAIL); |
1916 | add_uniq_column(COL_FSUSEPERC); | |
be685b98 | 1917 | add_uniq_column(COL_TARGET); |
2a4c734b MB |
1918 | break; |
1919 | case 'm': | |
be685b98 KZ |
1920 | add_uniq_column(COL_NAME); |
1921 | add_uniq_column(COL_SIZE); | |
1922 | add_uniq_column(COL_OWNER); | |
1923 | add_uniq_column(COL_GROUP); | |
1924 | add_uniq_column(COL_MODE); | |
2a4c734b MB |
1925 | break; |
1926 | case 't': | |
be685b98 KZ |
1927 | add_uniq_column(COL_NAME); |
1928 | add_uniq_column(COL_ALIOFF); | |
1929 | add_uniq_column(COL_MINIO); | |
1930 | add_uniq_column(COL_OPTIO); | |
1931 | add_uniq_column(COL_PHYSEC); | |
1932 | add_uniq_column(COL_LOGSEC); | |
1933 | add_uniq_column(COL_ROTA); | |
1934 | add_uniq_column(COL_SCHED); | |
1935 | add_uniq_column(COL_RQ_SIZE); | |
1936 | add_uniq_column(COL_RA); | |
1937 | add_uniq_column(COL_WSAME); | |
2a4c734b | 1938 | break; |
28ffc2b7 MB |
1939 | case 'S': |
1940 | lsblk->nodeps = 1; | |
1941 | lsblk->scsi = 1; | |
be685b98 KZ |
1942 | add_uniq_column(COL_NAME); |
1943 | add_uniq_column(COL_HCTL); | |
1944 | add_uniq_column(COL_TYPE); | |
1945 | add_uniq_column(COL_VENDOR); | |
1946 | add_uniq_column(COL_MODEL); | |
1947 | add_uniq_column(COL_REV); | |
1948 | add_uniq_column(COL_TRANSPORT); | |
28ffc2b7 | 1949 | break; |
1d9e35cc KZ |
1950 | case 'T': |
1951 | force_tree = 1; | |
b9c088f2 KZ |
1952 | if (optarg) |
1953 | lsblk->tree_id = column_name_to_id(optarg, strlen(optarg)); | |
1d9e35cc | 1954 | break; |
ffc27623 KZ |
1955 | case OPT_SYSROOT: |
1956 | lsblk->sysroot = optarg; | |
1957 | break; | |
57912387 | 1958 | case 'V': |
e421313d | 1959 | printf(UTIL_LINUX_VERSION); |
57912387 | 1960 | return EXIT_SUCCESS; |
0bd05f5e | 1961 | case 'E': |
dc4662f0 KZ |
1962 | lsblk->dedup_id = column_name_to_id(optarg, strlen(optarg)); |
1963 | if (lsblk->dedup_id >= 0) | |
1964 | break; | |
1965 | errtryhelp(EXIT_FAILURE); | |
1966 | break; | |
642048e4 | 1967 | case 'x': |
341c4ae2 | 1968 | lsblk->flags &= ~LSBLK_TREE; /* disable the default */ |
642048e4 KZ |
1969 | lsblk->sort_id = column_name_to_id(optarg, strlen(optarg)); |
1970 | if (lsblk->sort_id >= 0) | |
1971 | break; | |
1972 | /* fallthrough */ | |
2a4c734b | 1973 | default: |
193b3239 | 1974 | errtryhelp(EXIT_FAILURE); |
2a4c734b MB |
1975 | } |
1976 | } | |
1977 | ||
1d9e35cc KZ |
1978 | if (force_tree) |
1979 | lsblk->flags |= LSBLK_TREE; | |
1980 | ||
f65b3bb6 SK |
1981 | check_sysdevblock(); |
1982 | ||
2a4c734b | 1983 | if (!ncolumns) { |
be685b98 KZ |
1984 | add_column(COL_NAME); |
1985 | add_column(COL_MAJMIN); | |
1986 | add_column(COL_RM); | |
1987 | add_column(COL_SIZE); | |
1988 | add_column(COL_RO); | |
1989 | add_column(COL_TYPE); | |
1990 | add_column(COL_TARGET); | |
2a4c734b MB |
1991 | } |
1992 | ||
43bca827 MB |
1993 | if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns), |
1994 | &ncolumns, column_name_to_id) < 0) | |
1995 | return EXIT_FAILURE; | |
1996 | ||
b9dd5721 | 1997 | if (lsblk->all_devices == 0 && nexcludes == 0 && nincludes == 0) |
2a4c734b | 1998 | excludes[nexcludes++] = 1; /* default: ignore RAM disks */ |
9554f7ab | 1999 | |
5e2305ca KZ |
2000 | if (lsblk->sort_id < 0) |
2001 | /* Since Linux 4.8 we have sort devices by default, because | |
2002 | * /sys is no more sorted */ | |
2003 | lsblk->sort_id = COL_MAJMIN; | |
2004 | ||
091683a8 KZ |
2005 | /* For --inverse --list we still follow parent->child relation */ |
2006 | if (lsblk->inverse && !(lsblk->flags & LSBLK_TREE)) | |
2007 | lsblk->force_tree_order = 1; | |
2008 | ||
a6dc8dcd KZ |
2009 | if (lsblk->sort_id >= 0 && column_id_to_number(lsblk->sort_id) < 0) { |
2010 | /* the sort column is not between output columns -- add as hidden */ | |
be685b98 | 2011 | add_column(lsblk->sort_id); |
a6dc8dcd KZ |
2012 | lsblk->sort_hidden = 1; |
2013 | } | |
642048e4 | 2014 | |
dc4662f0 KZ |
2015 | if (lsblk->dedup_id >= 0 && column_id_to_number(lsblk->dedup_id) < 0) { |
2016 | /* the deduplication column is not between output columns -- add as hidden */ | |
2017 | add_column(lsblk->dedup_id); | |
2018 | lsblk->dedup_hidden = 1; | |
2019 | } | |
2020 | ||
e0c016f1 | 2021 | lsblk_mnt_init(); |
710ed55d | 2022 | scols_init_debug(0); |
5a89aa99 | 2023 | ul_path_init_debug(); |
9554f7ab | 2024 | |
2a4c734b MB |
2025 | /* |
2026 | * initialize output columns | |
2027 | */ | |
0925a9dd | 2028 | if (!(lsblk->table = scols_new_table())) |
780ce22c | 2029 | errx(EXIT_FAILURE, _("failed to allocate output table")); |
341c4ae2 KZ |
2030 | scols_table_enable_raw(lsblk->table, !!(lsblk->flags & LSBLK_RAW)); |
2031 | scols_table_enable_export(lsblk->table, !!(lsblk->flags & LSBLK_EXPORT)); | |
2032 | scols_table_enable_ascii(lsblk->table, !!(lsblk->flags & LSBLK_ASCII)); | |
2033 | scols_table_enable_json(lsblk->table, !!(lsblk->flags & LSBLK_JSON)); | |
2034 | scols_table_enable_noheadings(lsblk->table, !!(lsblk->flags & LSBLK_NOHEADINGS)); | |
2a4c734b | 2035 | |
341c4ae2 | 2036 | if (lsblk->flags & LSBLK_JSON) |
4a102a48 KZ |
2037 | scols_table_set_name(lsblk->table, "blockdevices"); |
2038 | ||
2a4c734b MB |
2039 | for (i = 0; i < ncolumns; i++) { |
2040 | struct colinfo *ci = get_column_info(i); | |
642048e4 KZ |
2041 | struct libscols_column *cl; |
2042 | int id = get_column_id(i), fl = ci->flags; | |
2a4c734b | 2043 | |
b9c088f2 KZ |
2044 | if ((lsblk->flags & LSBLK_TREE) |
2045 | && has_tree_col == 0 | |
2046 | && id == lsblk->tree_id) { | |
2047 | fl |= SCOLS_FL_TREE; | |
2048 | fl &= ~SCOLS_FL_RIGHT; | |
2049 | has_tree_col = 1; | |
2050 | } | |
2051 | ||
a6dc8dcd KZ |
2052 | if (lsblk->sort_hidden && lsblk->sort_id == id) |
2053 | fl |= SCOLS_FL_HIDDEN; | |
dc4662f0 KZ |
2054 | if (lsblk->dedup_hidden && lsblk->dedup_id == id) |
2055 | fl |= SCOLS_FL_HIDDEN; | |
2a4c734b | 2056 | |
fb376180 KZ |
2057 | if (force_tree |
2058 | && lsblk->flags & LSBLK_JSON | |
2059 | && has_tree_col == 0 | |
2060 | && i + 1 == ncolumns) | |
2061 | /* The "--tree --json" specified, but no column with | |
2062 | * SCOLS_FL_TREE yet; force it for the last column | |
2063 | */ | |
2064 | fl |= SCOLS_FL_TREE; | |
2065 | ||
642048e4 KZ |
2066 | cl = scols_table_new_column(lsblk->table, ci->name, ci->whint, fl); |
2067 | if (!cl) { | |
780ce22c | 2068 | warn(_("failed to allocate output column")); |
2a4c734b MB |
2069 | goto leave; |
2070 | } | |
642048e4 KZ |
2071 | if (!lsblk->sort_col && lsblk->sort_id == id) { |
2072 | lsblk->sort_col = cl; | |
2073 | scols_column_set_cmpfunc(cl, | |
407e0ea6 KZ |
2074 | ci->type == COLTYPE_NUM ? cmp_u64_cells : |
2075 | ci->type == COLTYPE_SIZE ? cmp_u64_cells : | |
2076 | ci->type == COLTYPE_SORTNUM ? cmp_u64_cells : scols_cmpstr_cells, | |
2077 | NULL); | |
2078 | } | |
2079 | if (lsblk->flags & LSBLK_JSON) { | |
2080 | switch (ci->type) { | |
2081 | case COLTYPE_SIZE: | |
2082 | if (!lsblk->bytes) | |
2083 | break; | |
2084 | /* fallthrough */ | |
2085 | case COLTYPE_NUM: | |
2086 | scols_column_set_json_type(cl, SCOLS_JSON_NUMBER); | |
2087 | break; | |
2088 | case COLTYPE_BOOL: | |
2089 | scols_column_set_json_type(cl, SCOLS_JSON_BOOLEAN); | |
2090 | break; | |
2091 | default: | |
2092 | scols_column_set_json_type(cl, SCOLS_JSON_STRING); | |
2093 | break; | |
2094 | } | |
642048e4 | 2095 | } |
2a4c734b MB |
2096 | } |
2097 | ||
f43b8297 KZ |
2098 | tr = lsblk_new_devtree(); |
2099 | if (!tr) | |
2100 | err(EXIT_FAILURE, _("failed to allocate device tree")); | |
2101 | ||
e7b5353e KZ |
2102 | if (optind == argc) { |
2103 | int rc = lsblk->inverse ? | |
2104 | process_all_devices_inverse(tr) : | |
2105 | process_all_devices(tr); | |
2106 | ||
2107 | status = rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE; | |
2108 | } else { | |
1539750f KZ |
2109 | int cnt = 0, cnt_err = 0; |
2110 | ||
2111 | while (optind < argc) { | |
291606fa | 2112 | if (process_one_device(tr, argv[optind++]) != 0) |
1539750f KZ |
2113 | cnt_err++; |
2114 | cnt++; | |
2115 | } | |
2116 | status = cnt == 0 ? EXIT_FAILURE : /* nothing */ | |
2117 | cnt == cnt_err ? LSBLK_EXIT_ALLFAILED :/* all failed */ | |
2118 | cnt_err ? LSBLK_EXIT_SOMEOK : /* some ok */ | |
2119 | EXIT_SUCCESS; /* all success */ | |
2120 | } | |
2a4c734b | 2121 | |
dc4662f0 KZ |
2122 | if (lsblk->dedup_id > -1) { |
2123 | devtree_set_dedupkeys(tr, lsblk->dedup_id); | |
2124 | lsblk_devtree_deduplicate_devices(tr); | |
2125 | } | |
2126 | ||
f43b8297 KZ |
2127 | devtree_to_scols(tr, lsblk->table); |
2128 | ||
642048e4 KZ |
2129 | if (lsblk->sort_col) |
2130 | scols_sort_table(lsblk->table, lsblk->sort_col); | |
091683a8 KZ |
2131 | if (lsblk->force_tree_order) |
2132 | scols_sort_table_by_tree(lsblk->table); | |
642048e4 | 2133 | |
9bd4e5c0 | 2134 | scols_print_table(lsblk->table); |
2a4c734b MB |
2135 | |
2136 | leave: | |
642048e4 KZ |
2137 | if (lsblk->sort_col) |
2138 | unref_sortdata(lsblk->table); | |
2139 | ||
9bd4e5c0 | 2140 | scols_unref_table(lsblk->table); |
50fccba1 | 2141 | |
e0c016f1 | 2142 | lsblk_mnt_deinit(); |
ccafadb7 | 2143 | lsblk_properties_deinit(); |
57502dd3 | 2144 | lsblk_unref_devtree(tr); |
ccafadb7 | 2145 | |
2a4c734b MB |
2146 | return status; |
2147 | } |