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