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