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