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