]>
Commit | Line | Data |
---|---|---|
2a4c734b | 1 | /* |
f77fa578 | 2 | * lsblk(8) - list block devices |
2a4c734b | 3 | * |
270e66ec | 4 | * Copyright (C) 2010,2011 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> |
2a4c734b | 44 | |
f17f5f48 IM |
45 | #ifdef HAVE_LIBUDEV |
46 | #include <libudev.h> | |
47 | #endif | |
48 | ||
2a4c734b MB |
49 | #include <assert.h> |
50 | ||
540d506f | 51 | #include "c.h" |
2a4c734b MB |
52 | #include "pathnames.h" |
53 | #include "blkdev.h" | |
54 | #include "canonicalize.h" | |
2a4c734b MB |
55 | #include "nls.h" |
56 | #include "tt.h" | |
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 MB |
64 | |
65 | /* column IDs */ | |
66 | enum { | |
67 | COL_NAME = 0, | |
68 | COL_KNAME, | |
69 | COL_MAJMIN, | |
70 | COL_FSTYPE, | |
71 | COL_TARGET, | |
72 | COL_LABEL, | |
73 | COL_UUID, | |
aa0903e0 KZ |
74 | COL_PARTLABEL, |
75 | COL_PARTUUID, | |
150db7a9 | 76 | COL_RA, |
2a4c734b | 77 | COL_RO, |
627970a7 | 78 | COL_RM, |
2a4c734b MB |
79 | COL_MODEL, |
80 | COL_SIZE, | |
01e487c0 | 81 | COL_STATE, |
2a4c734b MB |
82 | COL_OWNER, |
83 | COL_GROUP, | |
84 | COL_MODE, | |
85 | COL_ALIOFF, | |
86 | COL_MINIO, | |
87 | COL_OPTIO, | |
88 | COL_PHYSEC, | |
89 | COL_LOGSEC, | |
90 | COL_ROTA, | |
91 | COL_SCHED, | |
64c9e22a | 92 | COL_RQ_SIZE, |
18eac5ba | 93 | COL_TYPE, |
2d232246 MP |
94 | COL_DALIGN, |
95 | COL_DGRAN, | |
96 | COL_DMAX, | |
97 | COL_DZERO, | |
88ca32b3 | 98 | COL_WWN, |
12b06c3d | 99 | COL_RAND, |
310c0603 | 100 | COL_PKNAME, |
2a4c734b MB |
101 | }; |
102 | ||
103 | /* column names */ | |
104 | struct colinfo { | |
105 | const char *name; /* header */ | |
106 | double whint; /* width hint (N < 1 is in percent of termwidth) */ | |
107 | int flags; /* TT_FL_* */ | |
108 | const char *help; | |
109 | }; | |
110 | ||
111 | /* columns descriptions */ | |
507d39c2 | 112 | static struct colinfo infos[] = { |
7aa69cf2 | 113 | [COL_NAME] = { "NAME", 0.25, TT_FL_TREE | TT_FL_NOEXTREMES, N_("device name") }, |
e22d8b95 | 114 | [COL_KNAME] = { "KNAME", 0.3, 0, N_("internal kernel device name") }, |
310c0603 | 115 | [COL_PKNAME] = { "PKNAME", 0.3, 0, N_("internal parent kernel device name") }, |
2a4c734b MB |
116 | [COL_MAJMIN] = { "MAJ:MIN", 6, 0, N_("major:minor device number") }, |
117 | [COL_FSTYPE] = { "FSTYPE", 0.1, TT_FL_TRUNC, N_("filesystem type") }, | |
118 | [COL_TARGET] = { "MOUNTPOINT", 0.10, TT_FL_TRUNC, N_("where the device is mounted") }, | |
119 | [COL_LABEL] = { "LABEL", 0.1, 0, N_("filesystem LABEL") }, | |
120 | [COL_UUID] = { "UUID", 36, 0, N_("filesystem UUID") }, | |
aa0903e0 KZ |
121 | |
122 | [COL_PARTLABEL] = { "PARTLABEL", 0.1, 0, N_("partition LABEL") }, | |
123 | [COL_PARTUUID] = { "PARTUUID", 36, 0, N_("partition UUID") }, | |
124 | ||
150db7a9 | 125 | [COL_RA] = { "RA", 4, TT_FL_RIGHT, N_("read-ahead of the device") }, |
2a4c734b | 126 | [COL_RO] = { "RO", 1, TT_FL_RIGHT, N_("read-only device") }, |
627970a7 | 127 | [COL_RM] = { "RM", 1, TT_FL_RIGHT, N_("removable device") }, |
2a4c734b | 128 | [COL_ROTA] = { "ROTA", 1, TT_FL_RIGHT, N_("rotational device") }, |
12b06c3d | 129 | [COL_RAND] = { "RAND", 1, TT_FL_RIGHT, N_("adds randomness") }, |
2a4c734b | 130 | [COL_MODEL] = { "MODEL", 0.1, TT_FL_TRUNC, N_("device identifier") }, |
451d7646 | 131 | [COL_SIZE] = { "SIZE", 5, TT_FL_RIGHT, N_("size of the device") }, |
01e487c0 | 132 | [COL_STATE] = { "STATE", 7, TT_FL_TRUNC, N_("state of the device") }, |
2a4c734b MB |
133 | [COL_OWNER] = { "OWNER", 0.1, TT_FL_TRUNC, N_("user name"), }, |
134 | [COL_GROUP] = { "GROUP", 0.1, TT_FL_TRUNC, N_("group name") }, | |
135 | [COL_MODE] = { "MODE", 10, 0, N_("device node permissions") }, | |
136 | [COL_ALIOFF] = { "ALIGNMENT", 6, TT_FL_RIGHT, N_("alignment offset") }, | |
137 | [COL_MINIO] = { "MIN-IO", 6, TT_FL_RIGHT, N_("minimum I/O size") }, | |
138 | [COL_OPTIO] = { "OPT-IO", 6, TT_FL_RIGHT, N_("optimal I/O size") }, | |
139 | [COL_PHYSEC] = { "PHY-SEC", 7, TT_FL_RIGHT, N_("physical sector size") }, | |
140 | [COL_LOGSEC] = { "LOG-SEC", 7, TT_FL_RIGHT, N_("logical sector size") }, | |
18eac5ba | 141 | [COL_SCHED] = { "SCHED", 0.1, 0, N_("I/O scheduler name") }, |
64c9e22a | 142 | [COL_RQ_SIZE]= { "RQ-SIZE", 5, TT_FL_RIGHT, N_("request queue size") }, |
2d232246 MP |
143 | [COL_TYPE] = { "TYPE", 4, 0, N_("device type") }, |
144 | [COL_DALIGN] = { "DISC-ALN", 6, TT_FL_RIGHT, N_("discard alignment offset") }, | |
145 | [COL_DGRAN] = { "DISC-GRAN", 6, TT_FL_RIGHT, N_("discard granularity") }, | |
146 | [COL_DMAX] = { "DISC-MAX", 6, TT_FL_RIGHT, N_("discard max bytes") }, | |
147 | [COL_DZERO] = { "DISC-ZERO", 1, TT_FL_RIGHT, N_("discard zeroes data") }, | |
88ca32b3 | 148 | [COL_WWN] = { "WWN", 18, 0, N_("unique storage identifier") }, |
2a4c734b MB |
149 | }; |
150 | ||
151 | struct lsblk { | |
9feec79c | 152 | struct tt *tt; /* output table */ |
2dc03af7 | 153 | unsigned int all_devices:1; /* print all devices, including empty */ |
9feec79c | 154 | unsigned int bytes:1; /* print SIZE in bytes */ |
09a71aa1 | 155 | unsigned int inverse:1; /* print inverse dependencies */ |
9feec79c | 156 | unsigned int nodeps:1; /* don't print slaves/holders */ |
2a4c734b MB |
157 | }; |
158 | ||
159 | struct lsblk *lsblk; /* global handler */ | |
507d39c2 DB |
160 | |
161 | #define NCOLS ARRAY_SIZE(infos) | |
2ef4e8ba KZ |
162 | static int columns[NCOLS];/* enabled columns */ |
163 | static int ncolumns; /* number of enabled columns */ | |
2a4c734b | 164 | |
f6da50d4 BV |
165 | static int excludes[256]; |
166 | static size_t nexcludes; | |
2a4c734b | 167 | |
2ef4e8ba KZ |
168 | static int includes[256]; |
169 | static size_t nincludes; | |
170 | ||
9554f7ab KZ |
171 | static struct libmnt_table *mtab, *swaps; |
172 | static struct libmnt_cache *mntcache; | |
173 | ||
2a4c734b MB |
174 | struct blkdev_cxt { |
175 | struct blkdev_cxt *parent; | |
176 | ||
177 | struct tt_line *tt_line; | |
178 | struct stat st; | |
179 | ||
180 | char *name; /* kernel name in /sys/block */ | |
181 | char *dm_name; /* DM name (dm/block) */ | |
182 | ||
183 | char *filename; /* path to device node */ | |
766ab9b0 KZ |
184 | |
185 | struct sysfs_cxt sysfs; | |
2a4c734b MB |
186 | |
187 | int partition; /* is partition? TRUE/FALSE */ | |
188 | ||
189 | int probed; /* already probed */ | |
190 | char *fstype; /* detected fs, NULL or "?" if cannot detect */ | |
aa0903e0 KZ |
191 | char *uuid; /* filesystem UUID (or stack uuid) */ |
192 | char *label; /* filesystem label */ | |
193 | char *partuuid; /* partition UUID */ | |
194 | char *partlabel; /* partiton label */ | |
88ca32b3 | 195 | char *wwn; /* storage WWN */ |
2a4c734b | 196 | |
09a71aa1 | 197 | int npartitions; /* # of partitions this device has */ |
2a4c734b | 198 | int nholders; /* # of devices mapped directly to this device |
09a71aa1 | 199 | * /sys/block/.../holders */ |
2a4c734b MB |
200 | int nslaves; /* # of devices this device maps to */ |
201 | int maj, min; /* devno */ | |
98055888 | 202 | int discard; /* supports discard */ |
2a4c734b MB |
203 | |
204 | uint64_t size; /* device size */ | |
205 | }; | |
206 | ||
207 | static int is_maj_excluded(int maj) | |
208 | { | |
6aace32f | 209 | size_t i; |
2a4c734b MB |
210 | |
211 | assert(ARRAY_SIZE(excludes) > nexcludes); | |
212 | ||
2ef4e8ba KZ |
213 | if (!nexcludes) |
214 | return 0; /* filter not enabled, device not exluded */ | |
215 | ||
2a4c734b MB |
216 | for (i = 0; i < nexcludes; i++) |
217 | if (excludes[i] == maj) | |
218 | return 1; | |
219 | return 0; | |
220 | } | |
221 | ||
2ef4e8ba KZ |
222 | static int is_maj_included(int maj) |
223 | { | |
224 | size_t i; | |
225 | ||
226 | assert(ARRAY_SIZE(includes) > nincludes); | |
227 | ||
228 | if (!nincludes) | |
229 | return 1; /* filter not enabled, device is included */ | |
230 | ||
231 | for (i = 0; i < nincludes; i++) | |
232 | if (includes[i] == maj) | |
233 | return 1; | |
234 | return 0; | |
235 | } | |
236 | ||
2a4c734b MB |
237 | /* array with IDs of enabled columns */ |
238 | static int get_column_id(int num) | |
239 | { | |
507d39c2 | 240 | assert(ARRAY_SIZE(columns) == NCOLS); |
2a4c734b | 241 | assert(num < ncolumns); |
507d39c2 | 242 | assert(columns[num] < (int) NCOLS); |
2a4c734b MB |
243 | return columns[num]; |
244 | } | |
245 | ||
246 | static struct colinfo *get_column_info(int num) | |
247 | { | |
248 | return &infos[ get_column_id(num) ]; | |
249 | } | |
250 | ||
2a4c734b MB |
251 | static int column_name_to_id(const char *name, size_t namesz) |
252 | { | |
507d39c2 | 253 | size_t i; |
2a4c734b | 254 | |
507d39c2 | 255 | for (i = 0; i < NCOLS; i++) { |
2a4c734b MB |
256 | const char *cn = infos[i].name; |
257 | ||
258 | if (!strncasecmp(name, cn, namesz) && !*(cn + namesz)) | |
259 | return i; | |
260 | } | |
261 | warnx(_("unknown column: %s"), name); | |
262 | return -1; | |
263 | } | |
264 | ||
265 | static void reset_blkdev_cxt(struct blkdev_cxt *cxt) | |
266 | { | |
267 | if (!cxt) | |
268 | return; | |
269 | free(cxt->name); | |
270 | free(cxt->dm_name); | |
271 | free(cxt->filename); | |
272 | free(cxt->fstype); | |
273 | free(cxt->uuid); | |
274 | free(cxt->label); | |
aa0903e0 KZ |
275 | free(cxt->partuuid); |
276 | free(cxt->partlabel); | |
88ca32b3 | 277 | free(cxt->wwn); |
2a4c734b | 278 | |
766ab9b0 | 279 | sysfs_deinit(&cxt->sysfs); |
2a4c734b MB |
280 | |
281 | memset(cxt, 0, sizeof(*cxt)); | |
282 | } | |
283 | ||
284 | static int is_dm(const char *name) | |
285 | { | |
286 | return strncmp(name, "dm-", 3) ? 0 : 1; | |
287 | } | |
288 | ||
289 | static struct dirent *xreaddir(DIR *dp) | |
290 | { | |
291 | struct dirent *d; | |
292 | ||
293 | assert(dp); | |
294 | ||
295 | while ((d = readdir(dp))) { | |
296 | if (!strcmp(d->d_name, ".") || | |
297 | !strcmp(d->d_name, "..")) | |
298 | continue; | |
299 | ||
300 | /* blacklist here? */ | |
301 | break; | |
302 | } | |
303 | return d; | |
304 | } | |
305 | ||
2a4c734b MB |
306 | static char *get_device_path(struct blkdev_cxt *cxt) |
307 | { | |
308 | char path[PATH_MAX]; | |
309 | ||
310 | assert(cxt); | |
311 | assert(cxt->name); | |
312 | ||
313 | if (is_dm(cxt->name)) | |
314 | return canonicalize_dm_name(cxt->name); | |
315 | ||
316 | snprintf(path, sizeof(path), "/dev/%s", cxt->name); | |
317 | return xstrdup(path); | |
318 | } | |
319 | ||
9554f7ab KZ |
320 | static int is_active_swap(const char *filename) |
321 | { | |
322 | if (!swaps) { | |
323 | swaps = mnt_new_table(); | |
324 | if (!swaps) | |
325 | return 0; | |
326 | if (!mntcache) | |
327 | mntcache = mnt_new_cache(); | |
328 | ||
329 | mnt_table_set_cache(swaps, mntcache); | |
330 | mnt_table_parse_swaps(swaps, NULL); | |
331 | } | |
332 | ||
333 | return mnt_table_find_srcpath(swaps, filename, MNT_ITER_BACKWARD) != 0; | |
334 | } | |
335 | ||
2a4c734b MB |
336 | static char *get_device_mountpoint(struct blkdev_cxt *cxt) |
337 | { | |
9554f7ab KZ |
338 | struct libmnt_fs *fs; |
339 | const char *fsroot; | |
2a4c734b | 340 | |
270e66ec MB |
341 | assert(cxt); |
342 | assert(cxt->filename); | |
343 | ||
9554f7ab KZ |
344 | if (!mtab) { |
345 | mtab = mnt_new_table(); | |
346 | if (!mtab) | |
347 | return NULL; | |
348 | if (!mntcache) | |
349 | mntcache = mnt_new_cache(); | |
2a4c734b | 350 | |
9554f7ab KZ |
351 | mnt_table_set_cache(mtab, mntcache); |
352 | mnt_table_parse_mtab(mtab, NULL); | |
2a4c734b | 353 | } |
9554f7ab KZ |
354 | |
355 | /* try /etc/mtab or /proc/self/mountinfo */ | |
356 | fs = mnt_table_find_srcpath(mtab, cxt->filename, MNT_ITER_BACKWARD); | |
357 | if (!fs) | |
358 | return is_active_swap(cxt->filename) ? xstrdup("[SWAP]") : NULL; | |
359 | ||
360 | /* found */ | |
361 | fsroot = mnt_fs_get_root(fs); | |
362 | if (fsroot && strcmp(fsroot, "/") != 0) { | |
363 | /* hmm.. we found bind mount or btrfs subvolume, let's try to | |
364 | * get real FS root mountpoint */ | |
365 | struct libmnt_fs *rfs; | |
366 | struct libmnt_iter *itr = mnt_new_iter(MNT_ITER_BACKWARD); | |
367 | ||
368 | mnt_table_set_iter(mtab, itr, fs); | |
369 | while (mnt_table_next_fs(mtab, itr, &rfs) == 0) { | |
370 | fsroot = mnt_fs_get_root(rfs); | |
371 | if ((!fsroot || strcmp(fsroot, "/") == 0) | |
372 | && mnt_fs_match_source(rfs, cxt->filename, mntcache)) { | |
373 | fs = rfs; | |
374 | break; | |
375 | } | |
376 | } | |
377 | mnt_free_iter(itr); | |
378 | } | |
379 | ||
380 | return xstrdup(mnt_fs_get_target(fs)); | |
2a4c734b MB |
381 | } |
382 | ||
f17f5f48 | 383 | #ifndef HAVE_LIBUDEV |
88ca32b3 | 384 | static int get_udev_properties(struct blkdev_cxt *cxt |
f17f5f48 IM |
385 | __attribute__((__unused__))) |
386 | { | |
387 | return -1; | |
388 | } | |
389 | #else | |
88ca32b3 | 390 | static int get_udev_properties(struct blkdev_cxt *cxt) |
f17f5f48 IM |
391 | { |
392 | struct udev *udev; | |
393 | struct udev_device *dev; | |
394 | ||
88ca32b3 KZ |
395 | if (cxt->probed) |
396 | return 0; /* already done */ | |
397 | ||
f17f5f48 IM |
398 | udev = udev_new(); |
399 | if (!udev) | |
400 | return -1; | |
401 | ||
402 | dev = udev_device_new_from_subsystem_sysname(udev, "block", cxt->name); | |
403 | if (dev) { | |
404 | const char *data; | |
405 | ||
88ca32b3 | 406 | if ((data = udev_device_get_property_value(dev, "ID_FS_LABEL_ENC"))) { |
f17f5f48 | 407 | cxt->label = xstrdup(data); |
d324270e KZ |
408 | unhexmangle_string(cxt->label); |
409 | } | |
88ca32b3 | 410 | if ((data = udev_device_get_property_value(dev, "ID_FS_UUID_ENC"))) { |
f17f5f48 | 411 | cxt->uuid = xstrdup(data); |
88ca32b3 KZ |
412 | unhexmangle_string(cxt->uuid); |
413 | } | |
d324270e | 414 | if ((data = udev_device_get_property_value(dev, "ID_PART_ENTRY_NAME"))) { |
aa0903e0 | 415 | cxt->partlabel = xstrdup(data); |
d324270e KZ |
416 | unhexmangle_string(cxt->partlabel); |
417 | } | |
88ca32b3 KZ |
418 | if ((data = udev_device_get_property_value(dev, "ID_FS_TYPE"))) |
419 | cxt->fstype = xstrdup(data); | |
420 | if ((data = udev_device_get_property_value(dev, "ID_PART_ENTRY_UUID"))) | |
421 | cxt->partuuid = xstrdup(data); | |
422 | if ((data = udev_device_get_property_value(dev, "ID_WWN"))) | |
423 | cxt->wwn = xstrdup(data); | |
424 | ||
f17f5f48 | 425 | udev_device_unref(dev); |
88ca32b3 | 426 | cxt->probed = 1; |
f17f5f48 IM |
427 | } |
428 | ||
429 | udev_unref(udev); | |
88ca32b3 KZ |
430 | |
431 | return cxt->probed == 1 ? 0 : -1; | |
432 | ||
f17f5f48 IM |
433 | } |
434 | #endif /* HAVE_LIBUDEV */ | |
435 | ||
2a4c734b MB |
436 | static void probe_device(struct blkdev_cxt *cxt) |
437 | { | |
2a4c734b MB |
438 | blkid_probe pr = NULL; |
439 | ||
440 | if (cxt->probed) | |
441 | return; | |
f17f5f48 | 442 | |
2a4c734b MB |
443 | if (!cxt->size) |
444 | return; | |
445 | ||
f17f5f48 | 446 | /* try udev DB */ |
88ca32b3 | 447 | if (get_udev_properties(cxt) == 0) |
f17f5f48 IM |
448 | return; /* success */ |
449 | ||
88ca32b3 KZ |
450 | cxt->probed = 1; |
451 | ||
452 | /* try libblkid (fallback) */ | |
453 | if (getuid() != 0) | |
454 | return; /* no permissions to read from the device */ | |
455 | ||
2a4c734b MB |
456 | pr = blkid_new_probe_from_filename(cxt->filename); |
457 | if (!pr) | |
458 | return; | |
459 | ||
2a4c734b MB |
460 | blkid_probe_enable_superblocks(pr, 1); |
461 | blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_LABEL | | |
462 | BLKID_SUBLKS_UUID | | |
463 | BLKID_SUBLKS_TYPE); | |
aa0903e0 KZ |
464 | blkid_probe_enable_partitions(pr, 1); |
465 | blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS); | |
466 | ||
2a4c734b MB |
467 | if (!blkid_do_safeprobe(pr)) { |
468 | const char *data = NULL; | |
469 | ||
470 | if (!blkid_probe_lookup_value(pr, "TYPE", &data, NULL)) | |
471 | cxt->fstype = xstrdup(data); | |
472 | if (!blkid_probe_lookup_value(pr, "UUID", &data, NULL)) | |
473 | cxt->uuid = xstrdup(data); | |
474 | if (!blkid_probe_lookup_value(pr, "LABEL", &data, NULL)) | |
475 | cxt->label = xstrdup(data); | |
aa0903e0 KZ |
476 | if (!blkid_probe_lookup_value(pr, "PART_ENTRY_UUID", &data, NULL)) |
477 | cxt->partuuid = xstrdup(data); | |
478 | if (!blkid_probe_lookup_value(pr, "PART_ENTRY_NAME", &data, NULL)) | |
479 | cxt->partlabel = xstrdup(data); | |
480 | ||
2a4c734b MB |
481 | } |
482 | ||
2a4c734b MB |
483 | blkid_free_probe(pr); |
484 | return; | |
485 | } | |
486 | ||
487 | static int is_readonly_device(struct blkdev_cxt *cxt) | |
488 | { | |
489 | int fd, ro = 0; | |
490 | ||
1ea962b2 | 491 | if (sysfs_scanf(&cxt->sysfs, "ro", "%d", &ro) == 1) |
2a4c734b MB |
492 | return ro; |
493 | ||
494 | /* fallback if "ro" attribute does not exist */ | |
495 | fd = open(cxt->filename, O_RDONLY); | |
496 | if (fd != -1) { | |
9c53a49c KZ |
497 | if (ioctl(fd, BLKROGET, &ro) != 0) |
498 | ro = 0; | |
2a4c734b MB |
499 | close(fd); |
500 | } | |
501 | return ro; | |
502 | } | |
503 | ||
504 | static char *get_scheduler(struct blkdev_cxt *cxt) | |
505 | { | |
766ab9b0 | 506 | char *str = sysfs_strdup(&cxt->sysfs, "queue/scheduler"); |
2a4c734b MB |
507 | char *p, *res = NULL; |
508 | ||
509 | if (!str) | |
510 | return NULL; | |
511 | p = strchr(str, '['); | |
512 | if (p) { | |
513 | res = p + 1; | |
514 | p = strchr(res, ']'); | |
515 | if (p) { | |
516 | *p = '\0'; | |
517 | res = xstrdup(res); | |
518 | } else | |
519 | res = NULL; | |
520 | } | |
521 | free(str); | |
522 | return res; | |
523 | } | |
524 | ||
18eac5ba MB |
525 | static char *get_type(struct blkdev_cxt *cxt) |
526 | { | |
527 | char *res = NULL, *p; | |
528 | ||
529 | if (is_dm(cxt->name)) { | |
766ab9b0 | 530 | char *dm_uuid = sysfs_strdup(&cxt->sysfs, "dm/uuid"); |
18eac5ba MB |
531 | |
532 | /* The DM_UUID prefix should be set to subsystem owning | |
533 | * the device - LVM, CRYPT, DMRAID, MPATH, PART */ | |
534 | if (dm_uuid) { | |
535 | char *tmp = dm_uuid; | |
536 | char *dm_uuid_prefix = strsep(&tmp, "-"); | |
537 | ||
538 | if (dm_uuid_prefix) { | |
539 | /* kpartx hack to remove partition number */ | |
540 | if (strncasecmp(dm_uuid_prefix, "part", 4) == 0) | |
541 | dm_uuid_prefix[4] = '\0'; | |
542 | ||
543 | res = xstrdup(dm_uuid_prefix); | |
544 | } | |
545 | } | |
546 | ||
547 | free(dm_uuid); | |
548 | if (!res) | |
549 | /* No UUID or no prefix - just mark it as DM device */ | |
550 | res = xstrdup("dm"); | |
551 | ||
552 | } else if (!strncmp(cxt->name, "loop", 4)) { | |
553 | res = xstrdup("loop"); | |
554 | ||
555 | } else if (!strncmp(cxt->name, "md", 2)) { | |
766ab9b0 | 556 | char *md_level = sysfs_strdup(&cxt->sysfs, "md/level"); |
18eac5ba MB |
557 | res = md_level ? md_level : xstrdup("md"); |
558 | ||
559 | } else { | |
8d687180 | 560 | const char *type = NULL; |
90e9fcda | 561 | int x = 0; |
9d738ff8 | 562 | |
8d687180 PR |
563 | if (!sysfs_read_int(&cxt->sysfs, "device/type", &x)) |
564 | type = blkdev_scsi_type_to_name(x); | |
9d738ff8 | 565 | |
9d738ff8 KZ |
566 | if (!type) |
567 | type = cxt->partition ? "part" : "disk"; | |
568 | res = xstrdup(type); | |
18eac5ba MB |
569 | } |
570 | ||
571 | for (p = res; p && *p; p++) | |
572 | *p = tolower((unsigned char) *p); | |
573 | return res; | |
574 | } | |
575 | ||
7c9c872c KZ |
576 | #define is_parsable(_l) (((_l)->tt->flags & TT_FL_RAW) || \ |
577 | ((_l)->tt->flags & TT_FL_EXPORT)) | |
578 | ||
2a4c734b MB |
579 | static void set_tt_data(struct blkdev_cxt *cxt, int col, int id, struct tt_line *ln) |
580 | { | |
581 | char buf[1024]; | |
9cfe1b9c | 582 | char *p = NULL; |
1dc42eb6 | 583 | int st_rc = 0; |
2a4c734b MB |
584 | |
585 | if (!cxt->st.st_rdev && (id == COL_OWNER || id == COL_GROUP || | |
586 | id == COL_MODE)) | |
1dc42eb6 | 587 | st_rc = stat(cxt->filename, &cxt->st); |
2a4c734b MB |
588 | |
589 | switch(id) { | |
590 | case COL_NAME: | |
591 | if (cxt->dm_name) { | |
7c9c872c | 592 | if (is_parsable(lsblk)) |
9e9bceeb | 593 | tt_line_set_data(ln, col, xstrdup(cxt->dm_name)); |
205eb002 KZ |
594 | else { |
595 | snprintf(buf, sizeof(buf), "%s (%s)", | |
2a4c734b | 596 | cxt->dm_name, cxt->name); |
205eb002 KZ |
597 | tt_line_set_data(ln, col, xstrdup(buf)); |
598 | } | |
2a4c734b MB |
599 | break; |
600 | } | |
601 | case COL_KNAME: | |
d324270e | 602 | tt_line_set_data(ln, col, xstrdup(cxt->name)); |
2a4c734b | 603 | break; |
310c0603 MB |
604 | case COL_PKNAME: |
605 | if (cxt->parent) | |
606 | tt_line_set_data(ln, col, xstrdup(cxt->parent->name)); | |
607 | break; | |
2a4c734b MB |
608 | case COL_OWNER: |
609 | { | |
1dc42eb6 | 610 | struct passwd *pw = st_rc ? NULL : getpwuid(cxt->st.st_uid); |
2a4c734b MB |
611 | if (pw) |
612 | tt_line_set_data(ln, col, xstrdup(pw->pw_name)); | |
613 | break; | |
614 | } | |
615 | case COL_GROUP: | |
616 | { | |
1dc42eb6 | 617 | struct group *gr = st_rc ? NULL : getgrgid(cxt->st.st_gid); |
2a4c734b MB |
618 | if (gr) |
619 | tt_line_set_data(ln, col, xstrdup(gr->gr_name)); | |
620 | break; | |
621 | } | |
622 | case COL_MODE: | |
623 | { | |
624 | char md[11]; | |
1dc42eb6 KZ |
625 | |
626 | if (!st_rc) { | |
627 | strmode(cxt->st.st_mode, md); | |
628 | tt_line_set_data(ln, col, xstrdup(md)); | |
629 | } | |
2a4c734b MB |
630 | break; |
631 | } | |
632 | case COL_MAJMIN: | |
7c9c872c | 633 | if (is_parsable(lsblk)) |
2a4c734b MB |
634 | snprintf(buf, sizeof(buf), "%u:%u", cxt->maj, cxt->min); |
635 | else | |
636 | snprintf(buf, sizeof(buf), "%3u:%-3u", cxt->maj, cxt->min); | |
637 | tt_line_set_data(ln, col, xstrdup(buf)); | |
638 | break; | |
639 | case COL_FSTYPE: | |
640 | probe_device(cxt); | |
641 | if (cxt->fstype) | |
642 | tt_line_set_data(ln, col, xstrdup(cxt->fstype)); | |
643 | break; | |
644 | case COL_TARGET: | |
09a71aa1 | 645 | if (!(cxt->nholders + cxt->npartitions)) { |
d324270e | 646 | if ((p = get_device_mountpoint(cxt))) |
2a4c734b MB |
647 | tt_line_set_data(ln, col, p); |
648 | } | |
649 | break; | |
650 | case COL_LABEL: | |
651 | probe_device(cxt); | |
7c9c872c KZ |
652 | if (!cxt->label) |
653 | break; | |
654 | ||
d324270e | 655 | tt_line_set_data(ln, col, xstrdup(cxt->label)); |
2a4c734b MB |
656 | break; |
657 | case COL_UUID: | |
658 | probe_device(cxt); | |
659 | if (cxt->uuid) | |
660 | tt_line_set_data(ln, col, xstrdup(cxt->uuid)); | |
661 | break; | |
aa0903e0 KZ |
662 | case COL_PARTLABEL: |
663 | probe_device(cxt); | |
664 | if (!cxt->partlabel) | |
665 | break; | |
666 | ||
d324270e | 667 | tt_line_set_data(ln, col, xstrdup(cxt->partlabel)); |
aa0903e0 KZ |
668 | break; |
669 | case COL_PARTUUID: | |
670 | probe_device(cxt); | |
671 | if (cxt->uuid) | |
672 | tt_line_set_data(ln, col, xstrdup(cxt->partuuid)); | |
673 | break; | |
88ca32b3 KZ |
674 | case COL_WWN: |
675 | get_udev_properties(cxt); | |
676 | if (cxt->wwn) | |
677 | tt_line_set_data(ln, col, xstrdup(cxt->wwn)); | |
678 | break; | |
150db7a9 MB |
679 | case COL_RA: |
680 | p = sysfs_strdup(&cxt->sysfs, "queue/read_ahead_kb"); | |
681 | if (p) | |
682 | tt_line_set_data(ln, col, p); | |
683 | break; | |
2a4c734b MB |
684 | case COL_RO: |
685 | tt_line_set_data(ln, col, is_readonly_device(cxt) ? | |
686 | xstrdup("1") : xstrdup("0")); | |
687 | break; | |
627970a7 | 688 | case COL_RM: |
766ab9b0 | 689 | p = sysfs_strdup(&cxt->sysfs, "removable"); |
09a71aa1 PR |
690 | if (!p && cxt->sysfs.parent) |
691 | p = sysfs_strdup(cxt->sysfs.parent, "removable"); | |
2a4c734b MB |
692 | if (p) |
693 | tt_line_set_data(ln, col, p); | |
694 | break; | |
695 | case COL_ROTA: | |
766ab9b0 | 696 | p = sysfs_strdup(&cxt->sysfs, "queue/rotational"); |
2a4c734b MB |
697 | if (p) |
698 | tt_line_set_data(ln, col, p); | |
699 | break; | |
12b06c3d MB |
700 | case COL_RAND: |
701 | p = sysfs_strdup(&cxt->sysfs, "queue/add_random"); | |
702 | if (p) | |
703 | tt_line_set_data(ln, col, p); | |
704 | break; | |
2a4c734b MB |
705 | case COL_MODEL: |
706 | if (!cxt->partition && cxt->nslaves == 0) { | |
766ab9b0 | 707 | p = sysfs_strdup(&cxt->sysfs, "device/model"); |
2a4c734b MB |
708 | if (p) |
709 | tt_line_set_data(ln, col, p); | |
710 | } | |
711 | break; | |
712 | case COL_SIZE: | |
713 | if (cxt->size) { | |
714 | if (lsblk->bytes) { | |
6f312c89 | 715 | if (xasprintf(&p, "%jd", cxt->size) < 0) |
2a4c734b MB |
716 | p = NULL; |
717 | } else | |
5d2a9849 | 718 | p = size_to_human_string(SIZE_SUFFIX_1LETTER, cxt->size); |
2a4c734b MB |
719 | if (p) |
720 | tt_line_set_data(ln, col, p); | |
721 | } | |
722 | break; | |
01e487c0 MB |
723 | case COL_STATE: |
724 | if (!cxt->partition && !cxt->dm_name) { | |
725 | p = sysfs_strdup(&cxt->sysfs, "device/state"); | |
726 | } else if (cxt->dm_name) { | |
727 | int x = 0; | |
728 | if (sysfs_read_int(&cxt->sysfs, "dm/suspended", &x) == 0) | |
729 | p = x ? xstrdup("suspended") : xstrdup("running"); | |
730 | } | |
731 | if (p) | |
732 | tt_line_set_data(ln, col, p); | |
733 | break; | |
2a4c734b | 734 | case COL_ALIOFF: |
766ab9b0 | 735 | p = sysfs_strdup(&cxt->sysfs, "alignment_offset"); |
2a4c734b MB |
736 | if (p) |
737 | tt_line_set_data(ln, col, p); | |
738 | break; | |
739 | case COL_MINIO: | |
766ab9b0 | 740 | p = sysfs_strdup(&cxt->sysfs, "queue/minimum_io_size"); |
2a4c734b MB |
741 | if (p) |
742 | tt_line_set_data(ln, col, p); | |
743 | break; | |
744 | case COL_OPTIO: | |
766ab9b0 | 745 | p = sysfs_strdup(&cxt->sysfs, "queue/optimal_io_size"); |
2a4c734b MB |
746 | if (p) |
747 | tt_line_set_data(ln, col, p); | |
748 | break; | |
749 | case COL_PHYSEC: | |
766ab9b0 | 750 | p = sysfs_strdup(&cxt->sysfs, "queue/physical_block_size"); |
2a4c734b MB |
751 | if (p) |
752 | tt_line_set_data(ln, col, p); | |
753 | break; | |
754 | case COL_LOGSEC: | |
766ab9b0 | 755 | p = sysfs_strdup(&cxt->sysfs, "queue/logical_block_size"); |
2a4c734b MB |
756 | if (p) |
757 | tt_line_set_data(ln, col, p); | |
758 | break; | |
759 | case COL_SCHED: | |
760 | p = get_scheduler(cxt); | |
761 | if (p) | |
762 | tt_line_set_data(ln, col, p); | |
763 | break; | |
64c9e22a MB |
764 | case COL_RQ_SIZE: |
765 | p = sysfs_strdup(&cxt->sysfs, "queue/nr_requests"); | |
766 | if (p) | |
767 | tt_line_set_data(ln, col, p); | |
768 | break; | |
18eac5ba MB |
769 | case COL_TYPE: |
770 | p = get_type(cxt); | |
771 | if (p) | |
772 | tt_line_set_data(ln, col, p); | |
773 | break; | |
2d232246 | 774 | case COL_DALIGN: |
766ab9b0 | 775 | p = sysfs_strdup(&cxt->sysfs, "discard_alignment"); |
98055888 | 776 | if (cxt->discard && p) |
2d232246 | 777 | tt_line_set_data(ln, col, p); |
98055888 MP |
778 | else |
779 | tt_line_set_data(ln, col, "0"); | |
2d232246 MP |
780 | break; |
781 | case COL_DGRAN: | |
9cfe1b9c KZ |
782 | if (lsblk->bytes) |
783 | p = sysfs_strdup(&cxt->sysfs, "queue/discard_granularity"); | |
784 | else { | |
785 | uint64_t x; | |
786 | ||
787 | if (sysfs_read_u64(&cxt->sysfs, | |
788 | "queue/discard_granularity", &x) == 0) | |
5d2a9849 | 789 | p = size_to_human_string(SIZE_SUFFIX_1LETTER, x); |
9cfe1b9c | 790 | } |
2d232246 MP |
791 | if (p) |
792 | tt_line_set_data(ln, col, p); | |
793 | break; | |
794 | case COL_DMAX: | |
9cfe1b9c KZ |
795 | if (lsblk->bytes) |
796 | p = sysfs_strdup(&cxt->sysfs, "queue/discard_max_bytes"); | |
797 | else { | |
798 | uint64_t x; | |
799 | ||
800 | if (sysfs_read_u64(&cxt->sysfs, | |
801 | "queue/discard_max_bytes", &x) == 0) | |
5d2a9849 | 802 | p = size_to_human_string(SIZE_SUFFIX_1LETTER, x); |
9cfe1b9c | 803 | } |
2d232246 MP |
804 | if (p) |
805 | tt_line_set_data(ln, col, p); | |
806 | break; | |
807 | case COL_DZERO: | |
766ab9b0 | 808 | p = sysfs_strdup(&cxt->sysfs, "queue/discard_zeroes_data"); |
98055888 | 809 | if (cxt->discard && p) |
2d232246 | 810 | tt_line_set_data(ln, col, p); |
98055888 MP |
811 | else |
812 | tt_line_set_data(ln, col, "0"); | |
2d232246 | 813 | break; |
2a4c734b MB |
814 | }; |
815 | } | |
816 | ||
817 | static void print_device(struct blkdev_cxt *cxt, struct tt_line *tt_parent) | |
818 | { | |
819 | int i; | |
820 | ||
821 | cxt->tt_line = tt_add_line(lsblk->tt, tt_parent); | |
822 | ||
823 | for (i = 0; i < ncolumns; i++) | |
824 | set_tt_data(cxt, i, get_column_id(i), cxt->tt_line); | |
825 | } | |
826 | ||
827 | static int set_cxt(struct blkdev_cxt *cxt, | |
828 | struct blkdev_cxt *parent, | |
09a71aa1 PR |
829 | struct blkdev_cxt *wholedisk, |
830 | const char *name) | |
2a4c734b | 831 | { |
766ab9b0 | 832 | dev_t devno; |
2a4c734b MB |
833 | |
834 | cxt->parent = parent; | |
835 | cxt->name = xstrdup(name); | |
09a71aa1 | 836 | cxt->partition = wholedisk != NULL; |
2a4c734b MB |
837 | |
838 | cxt->filename = get_device_path(cxt); | |
270e66ec MB |
839 | if (!cxt->filename) { |
840 | warnx(_("%s: failed to get device path"), name); | |
841 | return -1; | |
842 | } | |
2a4c734b | 843 | |
09a71aa1 PR |
844 | devno = sysfs_devname_to_devno(name, wholedisk ? wholedisk->name : NULL); |
845 | ||
270e66ec MB |
846 | if (!devno) { |
847 | warnx(_("%s: unknown device name"), name); | |
848 | return -1; | |
849 | } | |
2a4c734b | 850 | |
09a71aa1 PR |
851 | if (lsblk->inverse) { |
852 | if (sysfs_init(&cxt->sysfs, devno, wholedisk ? &wholedisk->sysfs : NULL)) { | |
853 | warnx(_("%s: failed to initialize sysfs handler"), name); | |
854 | return -1; | |
855 | } | |
856 | if (parent) | |
857 | parent->sysfs.parent = &cxt->sysfs; | |
858 | } else { | |
859 | if (sysfs_init(&cxt->sysfs, devno, parent ? &parent->sysfs : NULL)) { | |
860 | warnx(_("%s: failed to initialize sysfs handler"), name); | |
861 | return -1; | |
862 | } | |
270e66ec | 863 | } |
766ab9b0 KZ |
864 | |
865 | cxt->maj = major(devno); | |
866 | cxt->min = minor(devno); | |
32c67d2c | 867 | cxt->size = 0; |
2a4c734b | 868 | |
32c67d2c KZ |
869 | if (sysfs_read_u64(&cxt->sysfs, "size", &cxt->size) == 0) /* in sectors */ |
870 | cxt->size <<= 9; /* in bytes */ | |
90e9fcda KZ |
871 | |
872 | sysfs_read_int(&cxt->sysfs, "queue/discard_granularity", &cxt->discard); | |
2a4c734b MB |
873 | |
874 | /* Ignore devices of zero size */ | |
875 | if (!lsblk->all_devices && cxt->size == 0) | |
876 | return -1; | |
877 | ||
766ab9b0 KZ |
878 | if (is_dm(name)) { |
879 | cxt->dm_name = sysfs_strdup(&cxt->sysfs, "dm/name"); | |
270e66ec MB |
880 | if (!cxt->dm_name) { |
881 | warnx(_("%s: failed to get dm name"), name); | |
882 | return -1; | |
883 | } | |
766ab9b0 | 884 | } |
2a4c734b | 885 | |
09a71aa1 PR |
886 | cxt->npartitions = sysfs_count_partitions(&cxt->sysfs, name); |
887 | cxt->nholders = sysfs_count_dirents(&cxt->sysfs, "holders"); | |
766ab9b0 | 888 | cxt->nslaves = sysfs_count_dirents(&cxt->sysfs, "slaves"); |
2a4c734b MB |
889 | |
890 | return 0; | |
891 | } | |
892 | ||
09a71aa1 PR |
893 | static int process_blkdev(struct blkdev_cxt *cxt, struct blkdev_cxt *parent, |
894 | int do_partitions, const char *part_name); | |
895 | ||
2a4c734b | 896 | /* |
09a71aa1 | 897 | * List device partitions if any. |
2a4c734b | 898 | */ |
09a71aa1 PR |
899 | static int list_partitions(struct blkdev_cxt *wholedisk_cxt, struct blkdev_cxt *parent_cxt, |
900 | const char *part_name) | |
2a4c734b MB |
901 | { |
902 | DIR *dir; | |
903 | struct dirent *d; | |
09a71aa1 PR |
904 | struct blkdev_cxt part_cxt = {}; |
905 | int r = -1; | |
2a4c734b | 906 | |
09a71aa1 | 907 | assert(wholedisk_cxt); |
f31505fe | 908 | |
09a71aa1 PR |
909 | /* |
910 | * Do not process further if there are no partitions for | |
911 | * this device or the device itself is a partition. | |
912 | */ | |
913 | if (!wholedisk_cxt->npartitions || wholedisk_cxt->partition) | |
914 | return -1; | |
2a4c734b | 915 | |
09a71aa1 | 916 | dir = sysfs_opendir(&wholedisk_cxt->sysfs, NULL); |
2a4c734b MB |
917 | if (!dir) |
918 | err(EXIT_FAILURE, _("failed to open device directory in sysfs")); | |
919 | ||
920 | while ((d = xreaddir(dir))) { | |
09a71aa1 PR |
921 | /* Process particular partition only? */ |
922 | if (part_name && strcmp(part_name, d->d_name)) | |
2a4c734b MB |
923 | continue; |
924 | ||
09a71aa1 | 925 | if (!(sysfs_is_partition_dirent(dir, d, wholedisk_cxt->name))) |
270e66ec | 926 | continue; |
09a71aa1 PR |
927 | |
928 | if (lsblk->inverse) { | |
929 | /* | |
930 | * <parent_cxt> | |
931 | * `-<part_cxt> | |
932 | * `-<wholedisk_cxt> | |
933 | * `-... | |
934 | */ | |
935 | if (set_cxt(&part_cxt, parent_cxt, wholedisk_cxt, d->d_name)) | |
936 | goto next; | |
937 | ||
938 | if (!parent_cxt && part_cxt.nholders) | |
939 | goto next; | |
940 | ||
941 | wholedisk_cxt->parent = &part_cxt; | |
942 | print_device(&part_cxt, parent_cxt ? parent_cxt->tt_line : NULL); | |
943 | if (!lsblk->nodeps) | |
944 | process_blkdev(wholedisk_cxt, &part_cxt, 0, NULL); | |
945 | } else { | |
946 | /* | |
947 | * <parent_cxt> | |
948 | * `-<wholedisk_cxt> | |
949 | * `-<part_cxt> | |
950 | * `-... | |
951 | */ | |
952 | if (set_cxt(&part_cxt, wholedisk_cxt, wholedisk_cxt, d->d_name)) | |
953 | goto next; | |
954 | /* Print whole disk only once */ | |
955 | if (r) | |
956 | print_device(wholedisk_cxt, parent_cxt ? parent_cxt->tt_line : NULL); | |
957 | if (!lsblk->nodeps) | |
958 | process_blkdev(&part_cxt, wholedisk_cxt, 0, NULL); | |
270e66ec | 959 | } |
09a71aa1 PR |
960 | next: |
961 | reset_blkdev_cxt(&part_cxt); | |
962 | r = 0; | |
2a4c734b | 963 | } |
09a71aa1 | 964 | |
2a4c734b | 965 | closedir(dir); |
09a71aa1 PR |
966 | return r; |
967 | } | |
968 | ||
540d506f KZ |
969 | static int get_wholedisk_from_partition_dirent(DIR *dir, const char *dirname, |
970 | struct dirent *d, struct blkdev_cxt *cxt) | |
09a71aa1 PR |
971 | { |
972 | char path[PATH_MAX]; | |
973 | char *p; | |
974 | int len; | |
975 | ||
540d506f KZ |
976 | if ((len = readlink_at(dirfd(dir), dirname, |
977 | d->d_name, path, sizeof(path))) < 0) | |
09a71aa1 | 978 | return 0; |
540d506f KZ |
979 | |
980 | path[len] = '\0'; | |
09a71aa1 PR |
981 | |
982 | /* The path ends with ".../<device>/<partition>" */ | |
540d506f KZ |
983 | p = strrchr(path, '/'); |
984 | if (!p) | |
985 | return 0; | |
986 | *p = '\0'; | |
987 | ||
988 | p = strrchr(path, '/'); | |
989 | if (!p) | |
990 | return 0; | |
991 | p++; | |
2a4c734b | 992 | |
09a71aa1 PR |
993 | return set_cxt(cxt, NULL, NULL, p); |
994 | } | |
995 | ||
996 | /* | |
997 | * List device dependencies: partitions, holders (inverse = 0) or slaves (inverse = 1). | |
998 | */ | |
999 | static int list_deps(struct blkdev_cxt *cxt) | |
1000 | { | |
1001 | DIR *dir; | |
1002 | struct dirent *d; | |
1003 | struct blkdev_cxt dep = {}; | |
540d506f KZ |
1004 | char dirname[PATH_MAX]; |
1005 | const char *depname; | |
09a71aa1 PR |
1006 | |
1007 | assert(cxt); | |
1008 | ||
1009 | if (lsblk->nodeps) | |
1010 | return 0; | |
1011 | ||
1012 | if (!(lsblk->inverse ? cxt->nslaves : cxt->nholders)) | |
1013 | return 0; | |
1014 | ||
540d506f KZ |
1015 | depname = lsblk->inverse ? "slaves" : "holders"; |
1016 | dir = sysfs_opendir(&cxt->sysfs, depname); | |
2a4c734b MB |
1017 | if (!dir) |
1018 | return 0; | |
1019 | ||
540d506f KZ |
1020 | snprintf(dirname, sizeof(dirname), "%s/%s", cxt->sysfs.dir_path, depname); |
1021 | ||
2a4c734b | 1022 | while ((d = xreaddir(dir))) { |
09a71aa1 PR |
1023 | /* Is the dependency a partition? */ |
1024 | if (sysfs_is_partition_dirent(dir, d, NULL)) { | |
540d506f | 1025 | if (!get_wholedisk_from_partition_dirent(dir, dirname, d, &dep)) |
09a71aa1 | 1026 | process_blkdev(&dep, cxt, 1, d->d_name); |
270e66ec | 1027 | } |
09a71aa1 PR |
1028 | /* The dependency is a whole device. */ |
1029 | else if (!set_cxt(&dep, cxt, NULL, d->d_name)) | |
1030 | process_blkdev(&dep, cxt, 1, NULL); | |
1031 | ||
1032 | reset_blkdev_cxt(&dep); | |
2a4c734b MB |
1033 | } |
1034 | closedir(dir); | |
1035 | ||
1036 | return 0; | |
1037 | } | |
1038 | ||
09a71aa1 PR |
1039 | static int process_blkdev(struct blkdev_cxt *cxt, struct blkdev_cxt *parent, |
1040 | int do_partitions, const char *part_name) | |
1041 | { | |
1042 | if (do_partitions && cxt->npartitions) | |
1043 | return list_partitions(cxt, parent, part_name); | |
1044 | ||
1045 | print_device(cxt, parent ? parent->tt_line : NULL); | |
1046 | return list_deps(cxt); | |
1047 | } | |
1048 | ||
1049 | /* Iterate devices in sysfs */ | |
2a4c734b MB |
1050 | static int iterate_block_devices(void) |
1051 | { | |
1052 | DIR *dir; | |
1053 | struct dirent *d; | |
1054 | struct blkdev_cxt cxt = {}; | |
1055 | ||
1056 | if (!(dir = opendir(_PATH_SYS_BLOCK))) | |
1057 | return EXIT_FAILURE; | |
1058 | ||
1059 | while ((d = xreaddir(dir))) { | |
09a71aa1 | 1060 | if (set_cxt(&cxt, NULL, NULL, d->d_name)) |
2a4c734b MB |
1061 | goto next; |
1062 | ||
2ef4e8ba | 1063 | if (is_maj_excluded(cxt.maj) || !is_maj_included(cxt.maj)) |
2a4c734b MB |
1064 | goto next; |
1065 | ||
09a71aa1 PR |
1066 | /* Skip devices in the middle of dependency tree. */ |
1067 | if ((lsblk->inverse ? cxt.nholders : cxt.nslaves) > 0) | |
2a4c734b MB |
1068 | goto next; |
1069 | ||
09a71aa1 | 1070 | process_blkdev(&cxt, NULL, 1, NULL); |
2a4c734b MB |
1071 | next: |
1072 | reset_blkdev_cxt(&cxt); | |
1073 | } | |
1074 | ||
1075 | closedir(dir); | |
1076 | ||
1077 | return EXIT_SUCCESS; | |
1078 | } | |
1079 | ||
da30cb2a PR |
1080 | static char *devno_to_sysfs_name(dev_t devno, char *devname, char *buf, size_t buf_size) |
1081 | { | |
1082 | char path[PATH_MAX]; | |
1083 | ssize_t len; | |
1084 | ||
1085 | if (!sysfs_devno_path(devno, path, sizeof(path))) { | |
1086 | warn(_("%s: failed to compose sysfs path"), devname); | |
1087 | return NULL; | |
1088 | } | |
1089 | ||
1090 | len = readlink(path, buf, buf_size); | |
1091 | if (len < 0) { | |
1092 | warn(_("%s: failed to read link"), path); | |
1093 | return NULL; | |
1094 | } | |
1095 | buf[len] = '\0'; | |
1096 | ||
1097 | return xstrdup(strrchr(buf, '/') + 1); | |
1098 | } | |
1099 | ||
2a4c734b MB |
1100 | static int process_one_device(char *devname) |
1101 | { | |
1102 | struct blkdev_cxt parent = {}, cxt = {}; | |
1103 | struct stat st; | |
da30cb2a | 1104 | char buf[PATH_MAX + 1], *name, *diskname = NULL; |
2a4c734b | 1105 | dev_t disk = 0; |
da30cb2a | 1106 | int real_part = 0; |
270e66ec | 1107 | int status = EXIT_FAILURE; |
2a4c734b MB |
1108 | |
1109 | if (stat(devname, &st) || !S_ISBLK(st.st_mode)) { | |
1110 | warnx(_("%s: not a block device"), devname); | |
1111 | return EXIT_FAILURE; | |
1112 | } | |
da30cb2a PR |
1113 | |
1114 | if (!(name = devno_to_sysfs_name(st.st_rdev, devname, buf, PATH_MAX))) { | |
1115 | warn(_("%s: failed to get sysfs name"), devname); | |
2a4c734b MB |
1116 | return EXIT_FAILURE; |
1117 | } | |
da30cb2a PR |
1118 | |
1119 | if (!strncmp(name, "dm-", 3)) { | |
1120 | /* dm mapping is never a real partition! */ | |
1121 | real_part = 0; | |
1122 | } else { | |
1123 | if (blkid_devno_to_wholedisk(st.st_rdev, buf, sizeof(buf), &disk)) { | |
1124 | warn(_("%s: failed to get whole-disk device number"), devname); | |
1125 | return EXIT_FAILURE; | |
1126 | } | |
1127 | diskname = buf; | |
1128 | real_part = st.st_rdev != disk; | |
1129 | } | |
1130 | ||
1131 | if (!real_part) { | |
2a4c734b | 1132 | /* |
09a71aa1 | 1133 | * Device is not a partition. |
2a4c734b | 1134 | */ |
da30cb2a | 1135 | if (set_cxt(&cxt, NULL, NULL, name)) |
270e66ec | 1136 | goto leave; |
09a71aa1 | 1137 | process_blkdev(&cxt, NULL, !lsblk->inverse, NULL); |
270e66ec | 1138 | } else { |
2a4c734b | 1139 | /* |
09a71aa1 | 1140 | * Partition, read sysfs name of the device. |
2a4c734b | 1141 | */ |
09a71aa1 | 1142 | if (set_cxt(&parent, NULL, NULL, diskname)) |
270e66ec | 1143 | goto leave; |
09a71aa1 | 1144 | if (set_cxt(&cxt, &parent, &parent, name)) |
270e66ec | 1145 | goto leave; |
09a71aa1 PR |
1146 | |
1147 | if (lsblk->inverse) | |
1148 | process_blkdev(&parent, &cxt, 1, cxt.name); | |
1149 | else | |
1150 | process_blkdev(&cxt, &parent, 1, NULL); | |
2a4c734b MB |
1151 | } |
1152 | ||
270e66ec MB |
1153 | status = EXIT_SUCCESS; |
1154 | leave: | |
da30cb2a | 1155 | free(name); |
2a4c734b MB |
1156 | reset_blkdev_cxt(&cxt); |
1157 | ||
da30cb2a | 1158 | if (real_part) |
2a4c734b MB |
1159 | reset_blkdev_cxt(&parent); |
1160 | ||
270e66ec | 1161 | return status; |
2a4c734b MB |
1162 | } |
1163 | ||
2dc03af7 | 1164 | static void parse_excludes(const char *str0) |
2a4c734b | 1165 | { |
2dc03af7 KZ |
1166 | const char *str = str0; |
1167 | ||
2a4c734b MB |
1168 | while (str && *str) { |
1169 | char *end = NULL; | |
ed34643c | 1170 | unsigned long n; |
2a4c734b MB |
1171 | |
1172 | errno = 0; | |
1173 | n = strtoul(str, &end, 10); | |
1174 | ||
2dc03af7 KZ |
1175 | if (end == str || (end && *end && *end != ',')) |
1176 | errx(EXIT_FAILURE, _("failed to parse list '%s'"), str0); | |
1177 | if (errno != 0 && (n == ULONG_MAX || n == 0)) | |
1178 | err(EXIT_FAILURE, _("failed to parse list '%s'"), str0); | |
2a4c734b MB |
1179 | excludes[nexcludes++] = n; |
1180 | ||
1181 | if (nexcludes == ARRAY_SIZE(excludes)) | |
f14f02f5 | 1182 | /* TRANSLATORS: The standard value for %d is 256. */ |
2a4c734b MB |
1183 | errx(EXIT_FAILURE, _("the list of excluded devices is " |
1184 | "too large (limit is %d devices)"), | |
1185 | (int)ARRAY_SIZE(excludes)); | |
2dc03af7 | 1186 | |
2a4c734b MB |
1187 | str = end && *end ? end + 1 : NULL; |
1188 | } | |
1189 | } | |
1190 | ||
2dc03af7 | 1191 | static void parse_includes(const char *str0) |
2ef4e8ba | 1192 | { |
2dc03af7 KZ |
1193 | const char *str = str0; |
1194 | ||
2ef4e8ba KZ |
1195 | while (str && *str) { |
1196 | char *end = NULL; | |
1197 | unsigned long n; | |
1198 | ||
1199 | errno = 0; | |
1200 | n = strtoul(str, &end, 10); | |
1201 | ||
2dc03af7 KZ |
1202 | if (end == str || (end && *end && *end != ',')) |
1203 | errx(EXIT_FAILURE, _("failed to parse list '%s'"), str0); | |
1204 | if (errno != 0 && (n == ULONG_MAX || n == 0)) | |
1205 | err(EXIT_FAILURE, _("failed to parse list '%s'"), str0); | |
2ef4e8ba KZ |
1206 | includes[nincludes++] = n; |
1207 | ||
1208 | if (nincludes == ARRAY_SIZE(includes)) | |
1209 | /* TRANSLATORS: The standard value for %d is 256. */ | |
1210 | errx(EXIT_FAILURE, _("the list of included devices is " | |
1211 | "too large (limit is %d devices)"), | |
1212 | (int)ARRAY_SIZE(includes)); | |
1213 | str = end && *end ? end + 1 : NULL; | |
1214 | } | |
1215 | } | |
1216 | ||
abafd686 | 1217 | static void __attribute__((__noreturn__)) help(FILE *out) |
2a4c734b | 1218 | { |
507d39c2 | 1219 | size_t i; |
2a4c734b MB |
1220 | |
1221 | fprintf(out, _( | |
1222 | "\nUsage:\n" | |
1223 | " %s [options] [<device> ...]\n"), program_invocation_short_name); | |
1224 | ||
1225 | fprintf(out, _( | |
1226 | "\nOptions:\n" | |
1227 | " -a, --all print all devices\n" | |
1228 | " -b, --bytes print SIZE in bytes rather than in human readable format\n" | |
f31505fe | 1229 | " -d, --nodeps don't print slaves or holders\n" |
2d232246 | 1230 | " -D, --discard print discard capabilities\n" |
2a4c734b | 1231 | " -e, --exclude <list> exclude devices by major number (default: RAM disks)\n" |
2dc03af7 | 1232 | " -I, --include <list> show only devices with specified major numbers\n" |
2a4c734b MB |
1233 | " -f, --fs output info about filesystems\n" |
1234 | " -h, --help usage information (this)\n" | |
1235 | " -i, --ascii use ascii characters only\n" | |
1236 | " -m, --perms output info about permissions\n" | |
bbac757b | 1237 | " -l, --list use list format output\n" |
2a4c734b MB |
1238 | " -n, --noheadings don't print headings\n" |
1239 | " -o, --output <list> output columns\n" | |
6ea1bdd3 KZ |
1240 | " -P, --pairs use key=\"value\" output format\n" |
1241 | " -r, --raw use raw output format\n" | |
09a71aa1 | 1242 | " -s, --inverse inverse dependencies\n" |
57912387 MB |
1243 | " -t, --topology output info about topology\n" |
1244 | " -V, --version output version information and exit\n")); | |
2a4c734b | 1245 | |
57912387 | 1246 | fprintf(out, _("\nAvailable columns (for --output):\n")); |
2a4c734b | 1247 | |
507d39c2 | 1248 | for (i = 0; i < NCOLS; i++) |
57912387 | 1249 | fprintf(out, " %11s %s\n", infos[i].name, _(infos[i].help)); |
2a4c734b | 1250 | |
57912387 | 1251 | fprintf(out, USAGE_MAN_TAIL("lsblk(8)")); |
2a4c734b MB |
1252 | |
1253 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); | |
1254 | } | |
1255 | ||
f65b3bb6 SK |
1256 | static void check_sysdevblock(void) |
1257 | { | |
1258 | if (access(_PATH_SYS_DEVBLOCK, R_OK) != 0) | |
1259 | err(EXIT_FAILURE, _("failed to access sysfs directory: %s"), | |
1260 | _PATH_SYS_DEVBLOCK); | |
1261 | } | |
1262 | ||
2a4c734b MB |
1263 | int main(int argc, char *argv[]) |
1264 | { | |
1265 | struct lsblk _ls; | |
1266 | int tt_flags = TT_FL_TREE; | |
1267 | int i, c, status = EXIT_FAILURE; | |
43bca827 | 1268 | char *outarg = NULL; |
2a4c734b | 1269 | |
6c7d5ae9 | 1270 | static const struct option longopts[] = { |
2a4c734b | 1271 | { "all", 0, 0, 'a' }, |
f31505fe KZ |
1272 | { "bytes", 0, 0, 'b' }, |
1273 | { "nodeps", 0, 0, 'd' }, | |
2d232246 | 1274 | { "discard", 0, 0, 'D' }, |
2a4c734b MB |
1275 | { "help", 0, 0, 'h' }, |
1276 | { "output", 1, 0, 'o' }, | |
1277 | { "perms", 0, 0, 'm' }, | |
1278 | { "noheadings", 0, 0, 'n' }, | |
1279 | { "list", 0, 0, 'l' }, | |
1280 | { "ascii", 0, 0, 'i' }, | |
1281 | { "raw", 0, 0, 'r' }, | |
09a71aa1 | 1282 | { "inverse", 0, 0, 's' }, |
2a4c734b MB |
1283 | { "fs", 0, 0, 'f' }, |
1284 | { "exclude", 1, 0, 'e' }, | |
2ef4e8ba | 1285 | { "include", 1, 0, 'I' }, |
2a4c734b | 1286 | { "topology", 0, 0, 't' }, |
6ea1bdd3 | 1287 | { "pairs", 0, 0, 'P' }, |
57912387 | 1288 | { "version", 0, 0, 'V' }, |
2a4c734b MB |
1289 | { NULL, 0, 0, 0 }, |
1290 | }; | |
1291 | ||
98f2dc7a KZ |
1292 | static const ul_excl_t excl[] = { /* rows and cols in in ASCII order */ |
1293 | { 'I','e' }, | |
1294 | { 'P','l','r' }, | |
1295 | { 0 } | |
1296 | }; | |
1297 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
1298 | ||
2a4c734b MB |
1299 | setlocale(LC_ALL, ""); |
1300 | bindtextdomain(PACKAGE, LOCALEDIR); | |
1301 | textdomain(PACKAGE); | |
c05a80ca | 1302 | atexit(close_stdout); |
2a4c734b MB |
1303 | |
1304 | lsblk = &_ls; | |
1305 | memset(lsblk, 0, sizeof(*lsblk)); | |
1306 | ||
98f2dc7a KZ |
1307 | while((c = getopt_long(argc, argv, |
1308 | "abdDe:fhlnmo:PiI:rstV", longopts, NULL)) != -1) { | |
1309 | ||
1310 | err_exclusive_options(c, longopts, excl, excl_st); | |
1311 | ||
2a4c734b MB |
1312 | switch(c) { |
1313 | case 'a': | |
1314 | lsblk->all_devices = 1; | |
1315 | break; | |
1316 | case 'b': | |
1317 | lsblk->bytes = 1; | |
1318 | break; | |
f31505fe KZ |
1319 | case 'd': |
1320 | lsblk->nodeps = 1; | |
1321 | break; | |
2d232246 MP |
1322 | case 'D': |
1323 | columns[ncolumns++] = COL_NAME; | |
1324 | columns[ncolumns++] = COL_DALIGN; | |
1325 | columns[ncolumns++] = COL_DGRAN; | |
1326 | columns[ncolumns++] = COL_DMAX; | |
1327 | columns[ncolumns++] = COL_DZERO; | |
1328 | break; | |
2a4c734b MB |
1329 | case 'e': |
1330 | parse_excludes(optarg); | |
1331 | break; | |
1332 | case 'h': | |
1333 | help(stdout); | |
1334 | break; | |
1335 | case 'l': | |
2a4c734b MB |
1336 | tt_flags &= ~TT_FL_TREE; /* disable the default */ |
1337 | break; | |
1338 | case 'n': | |
1339 | tt_flags |= TT_FL_NOHEADINGS; | |
1340 | break; | |
1341 | case 'o': | |
43bca827 | 1342 | outarg = optarg; |
2a4c734b | 1343 | break; |
6ea1bdd3 KZ |
1344 | case 'P': |
1345 | tt_flags |= TT_FL_EXPORT; | |
1346 | tt_flags &= ~TT_FL_TREE; /* disable the default */ | |
1347 | break; | |
2a4c734b | 1348 | case 'i': |
449f9336 | 1349 | tt_flags |= TT_FL_ASCII; |
2a4c734b | 1350 | break; |
2ef4e8ba | 1351 | case 'I': |
2ef4e8ba KZ |
1352 | parse_includes(optarg); |
1353 | break; | |
2a4c734b MB |
1354 | case 'r': |
1355 | tt_flags &= ~TT_FL_TREE; /* disable the default */ | |
1356 | tt_flags |= TT_FL_RAW; /* enable raw */ | |
1357 | break; | |
09a71aa1 PR |
1358 | case 's': |
1359 | lsblk->inverse = 1; | |
1360 | break; | |
2a4c734b MB |
1361 | case 'f': |
1362 | columns[ncolumns++] = COL_NAME; | |
1363 | columns[ncolumns++] = COL_FSTYPE; | |
1364 | columns[ncolumns++] = COL_LABEL; | |
38a8d69e | 1365 | columns[ncolumns++] = COL_UUID; |
2a4c734b MB |
1366 | columns[ncolumns++] = COL_TARGET; |
1367 | break; | |
1368 | case 'm': | |
1369 | columns[ncolumns++] = COL_NAME; | |
1370 | columns[ncolumns++] = COL_SIZE; | |
1371 | columns[ncolumns++] = COL_OWNER; | |
1372 | columns[ncolumns++] = COL_GROUP; | |
1373 | columns[ncolumns++] = COL_MODE; | |
1374 | break; | |
1375 | case 't': | |
1376 | columns[ncolumns++] = COL_NAME; | |
1377 | columns[ncolumns++] = COL_ALIOFF; | |
1378 | columns[ncolumns++] = COL_MINIO; | |
1379 | columns[ncolumns++] = COL_OPTIO; | |
1380 | columns[ncolumns++] = COL_PHYSEC; | |
1381 | columns[ncolumns++] = COL_LOGSEC; | |
1382 | columns[ncolumns++] = COL_ROTA; | |
1383 | columns[ncolumns++] = COL_SCHED; | |
64c9e22a | 1384 | columns[ncolumns++] = COL_RQ_SIZE; |
150db7a9 | 1385 | columns[ncolumns++] = COL_RA; |
2a4c734b | 1386 | break; |
57912387 MB |
1387 | case 'V': |
1388 | printf(_("%s from %s\n"), program_invocation_short_name, | |
1389 | PACKAGE_STRING); | |
1390 | return EXIT_SUCCESS; | |
2a4c734b MB |
1391 | default: |
1392 | help(stderr); | |
1393 | } | |
1394 | } | |
1395 | ||
f65b3bb6 SK |
1396 | check_sysdevblock(); |
1397 | ||
2a4c734b MB |
1398 | if (!ncolumns) { |
1399 | columns[ncolumns++] = COL_NAME; | |
1400 | columns[ncolumns++] = COL_MAJMIN; | |
627970a7 | 1401 | columns[ncolumns++] = COL_RM; |
2a4c734b MB |
1402 | columns[ncolumns++] = COL_SIZE; |
1403 | columns[ncolumns++] = COL_RO; | |
18eac5ba | 1404 | columns[ncolumns++] = COL_TYPE; |
2a4c734b MB |
1405 | columns[ncolumns++] = COL_TARGET; |
1406 | } | |
1407 | ||
43bca827 MB |
1408 | if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns), |
1409 | &ncolumns, column_name_to_id) < 0) | |
1410 | return EXIT_FAILURE; | |
1411 | ||
2ef4e8ba | 1412 | if (nexcludes == 0 && nincludes == 0) |
2a4c734b | 1413 | excludes[nexcludes++] = 1; /* default: ignore RAM disks */ |
9554f7ab KZ |
1414 | |
1415 | mnt_init_debug(0); | |
1416 | ||
2a4c734b MB |
1417 | /* |
1418 | * initialize output columns | |
1419 | */ | |
1420 | if (!(lsblk->tt = tt_new_table(tt_flags))) | |
1421 | errx(EXIT_FAILURE, _("failed to initialize output table")); | |
1422 | ||
1423 | for (i = 0; i < ncolumns; i++) { | |
1424 | struct colinfo *ci = get_column_info(i); | |
1425 | int fl = ci->flags; | |
1426 | ||
1427 | if (!(tt_flags & TT_FL_TREE) && get_column_id(i) == COL_NAME) | |
1428 | fl &= ~TT_FL_TREE; | |
1429 | ||
1430 | if (!tt_define_column(lsblk->tt, ci->name, ci->whint, fl)) { | |
1431 | warn(_("failed to initialize output column")); | |
1432 | goto leave; | |
1433 | } | |
1434 | } | |
1435 | ||
1436 | if (optind == argc) | |
1437 | status = iterate_block_devices(); | |
1438 | else while (optind < argc) | |
1439 | status = process_one_device(argv[optind++]); | |
1440 | ||
1441 | tt_print_table(lsblk->tt); | |
1442 | ||
1443 | leave: | |
1444 | tt_free_table(lsblk->tt); | |
9554f7ab KZ |
1445 | mnt_free_table(mtab); |
1446 | mnt_free_table(swaps); | |
1447 | mnt_free_cache(mntcache); | |
2a4c734b MB |
1448 | return status; |
1449 | } |