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