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