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