2 * blkid.c - User command-line interface for libblkid
4 * Copyright (C) 2001 Andreas Dilger
7 * This file may be redistributed under the terms of the
8 * GNU Lesser General Public License.
16 #include <sys/types.h>
21 #ifdef HAVE_SYS_IOCTL_H
22 #include <sys/ioctl.h>
27 extern int getopt(int argc
, char * const argv
[], const char *optstring
);
32 #define OUTPUT_VALUE_ONLY (1 << 1)
33 #define OUTPUT_DEVICE_ONLY (1 << 2)
34 #define OUTPUT_PRETTY_LIST (1 << 3) /* deprecated */
35 #define OUTPUT_UDEV_LIST (1 << 4) /* deprecated */
36 #define OUTPUT_EXPORT_LIST (1 << 5)
38 #define LOWPROBE_TOPOLOGY (1 << 1)
39 #define LOWPROBE_SUPERBLOCKS (1 << 2)
43 #include "ismounted.h"
45 #define STRTOXX_EXIT_CODE 4 /* strtoxx_or_err() */
48 const char *progname
= "blkid";
52 static void print_version(FILE *out
)
54 fprintf(out
, "%s from %s (libblkid %s, %s)\n",
55 progname
, PACKAGE_STRING
, LIBBLKID_VERSION
, LIBBLKID_DATE
);
58 static void usage(int error
)
60 FILE *out
= error
? stderr
: stdout
;
65 " %1$s -L <label> | -U <uuid>\n\n"
66 " %1$s [-c <file>] [-ghlLv] [-o <format>] [-s <tag>] \n"
67 " [-t <token>] [<dev> ...]\n\n"
68 " %1$s -p [-s <tag>] [-O <offset>] [-S <size>] \n"
69 " [-o <format>] <dev> ...\n\n"
70 " %1$s -i [-s <tag>] [-o <format>] <dev> ...\n\n"
72 " -c <file> read from <file> instead of reading from the default\n"
73 " cache file (-c /dev/null means no cache)\n"
74 " -d don't encode non-printing characters\n"
75 " -h print this usage message and exit\n"
76 " -g garbage collect the blkid cache\n"
77 " -o <format> output format; can be one of:\n"
78 " value, device, export or full; (default: full)\n"
79 " -k list all known filesystems/RAIDs and exit\n"
80 " -s <tag> show specified tag(s) (default show all tags)\n"
81 " -t <token> find device with a specific token (NAME=value pair)\n"
82 " -l look up only first device with token specified by -t\n"
83 " -L <label> convert LABEL to device name\n"
84 " -U <uuid> convert UUID to device name\n"
85 " -v print version and exit\n"
86 " <dev> specify device(s) to probe (default: all devices)\n\n"
87 "Low-level probing options:\n"
88 " -p low-level superblocks probing (bypass cache)\n"
89 " -i gather information about I/O limits\n"
90 " -S <size> overwrite device size\n"
91 " -O <offset> probe at the given offset\n"
92 " -u <list> filter by \"usage\" (e.g. -u filesystem,raid)\n"
93 " -n <list> filter by filesystem type (e.g. -n vfat,ext3)\n"
100 * This function does "safe" printing. It will convert non-printable
101 * ASCII characters using '^' and M- notation.
103 static void safe_print(const char *cp
, int len
)
117 if ((ch
< 32) || (ch
== 0x7f)) {
119 ch
^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
126 static int get_terminal_width(void)
129 struct ttysize t_win
;
132 struct winsize w_win
;
137 if (ioctl (0, TIOCGSIZE
, &t_win
) == 0)
138 return (t_win
.ts_cols
);
141 if (ioctl (0, TIOCGWINSZ
, &w_win
) == 0)
142 return (w_win
.ws_col
);
144 cp
= getenv("COLUMNS");
146 return strtol(cp
, NULL
, 10);
150 static int pretty_print_word(const char *str
, int max_len
,
151 int left_len
, int overflow_nl
)
153 int len
= strlen(str
) + left_len
;
157 if (overflow_nl
&& len
> max_len
) {
160 } else if (len
> max_len
)
164 while (len
++ < max_len
);
168 static void pretty_print_line(const char *device
, const char *fs_type
,
169 const char *label
, const char *mtpt
,
172 static int device_len
= 10, fs_type_len
= 7;
173 static int label_len
= 8, mtpt_len
= 14;
174 static int term_width
= -1;
178 term_width
= get_terminal_width();
180 if (term_width
> 80) {
193 len
= pretty_print_word(device
, device_len
, 0, 1);
194 len
= pretty_print_word(fs_type
, fs_type_len
, len
, 0);
195 len
= pretty_print_word(label
, label_len
, len
, 0);
196 pretty_print_word(mtpt
, mtpt_len
, len
, 0);
202 static void pretty_print_dev(blkid_dev dev
)
204 blkid_tag_iterate iter
;
205 const char *type
, *value
, *devname
;
206 const char *uuid
= "", *fs_type
= "", *label
= "";
207 int len
, mount_flags
;
212 pretty_print_line("device", "fs_type", "label",
213 "mount point", "UUID");
214 for (len
=get_terminal_width()-1; len
> 0; len
--)
220 devname
= blkid_dev_devname(dev
);
221 if (access(devname
, F_OK
))
224 /* Get the uuid, label, type */
225 iter
= blkid_tag_iterate_begin(dev
);
226 while (blkid_tag_next(iter
, &type
, &value
) == 0) {
227 if (!strcmp(type
, "UUID"))
229 if (!strcmp(type
, "TYPE"))
231 if (!strcmp(type
, "LABEL"))
234 blkid_tag_iterate_end(iter
);
236 /* Get the mount point */
238 retval
= check_mount_point(devname
, &mount_flags
, mtpt
, sizeof(mtpt
));
240 if (mount_flags
& MF_MOUNTED
) {
242 strcpy(mtpt
, "(mounted, mtpt unknown)");
243 } else if (mount_flags
& MF_BUSY
)
244 strcpy(mtpt
, "(in use)");
246 strcpy(mtpt
, "(not mounted)");
249 pretty_print_line(devname
, fs_type
, label
, mtpt
, uuid
);
252 static void print_udev_format(const char *name
, const char *value
)
254 char enc
[265], safe
[256];
255 size_t namelen
= strlen(name
);
259 if (!strcmp(name
, "TYPE") || !strcmp(name
, "VERSION")) {
260 blkid_encode_string(value
, enc
, sizeof(enc
));
261 printf("ID_FS_%s=%s\n", name
, enc
);
263 } else if (!strcmp(name
, "UUID") ||
264 !strcmp(name
, "LABEL") ||
265 !strcmp(name
, "UUID_SUB")) {
267 blkid_safe_string(value
, safe
, sizeof(safe
));
268 printf("ID_FS_%s=%s\n", name
, safe
);
270 blkid_encode_string(value
, enc
, sizeof(enc
));
271 printf("ID_FS_%s_ENC=%s\n", name
, enc
);
273 } else if (!strcmp(name
, "PTTYPE")) {
274 printf("ID_PART_TABLE_TYPE=%s\n", value
);
276 } else if (!strcmp(name
, "PART_ENTRY_NAME") ||
277 !strcmp(name
, "PART_ENTRY_TYPE")) {
279 blkid_encode_string(value
, enc
, sizeof(enc
));
280 printf("ID_%s=%s\n", name
, enc
);
282 } else if (!strncmp(name
, "PART_ENTRY_", 11))
283 printf("ID_%s=%s\n", name
, value
);
285 else if (namelen
>= 15 && (
286 !strcmp(name
+ (namelen
- 12), "_SECTOR_SIZE") ||
287 !strcmp(name
+ (namelen
- 8), "_IO_SIZE") ||
288 !strcmp(name
, "ALIGNMENT_OFFSET")))
289 printf("ID_IOLIMIT_%s=%s\n", name
, value
);
291 printf("ID_FS_%s=%s\n", name
, value
);
294 static int has_item(char *ary
[], const char *item
)
298 for (p
= ary
; *p
!= NULL
; p
++)
299 if (!strcmp(item
, *p
))
304 static void print_value(int output
, int num
, const char *devname
,
305 const char *value
, const char *name
, size_t valsz
)
307 if (output
& OUTPUT_VALUE_ONLY
) {
308 fputs(value
, stdout
);
311 } else if (output
& OUTPUT_UDEV_LIST
) {
312 print_udev_format(name
, value
);
314 } else if (output
& OUTPUT_EXPORT_LIST
) {
315 if (num
== 1 && devname
)
316 printf("DEVNAME=%s\n", devname
);
319 safe_print(value
, valsz
);
323 if (num
== 1 && devname
)
324 printf("%s: ", devname
);
326 fputs("=\"", stdout
);
327 safe_print(value
, valsz
);
328 fputs("\" ", stdout
);
332 static void print_tags(blkid_dev dev
, char *show
[], int output
)
334 blkid_tag_iterate iter
;
335 const char *type
, *value
, *devname
;
337 static int first
= 1;
342 if (output
& OUTPUT_PRETTY_LIST
) {
343 pretty_print_dev(dev
);
347 devname
= blkid_dev_devname(dev
);
349 if (output
& OUTPUT_DEVICE_ONLY
) {
350 printf("%s\n", devname
);
354 iter
= blkid_tag_iterate_begin(dev
);
355 while (blkid_tag_next(iter
, &type
, &value
) == 0) {
356 if (show
[0] && !has_item(show
, type
))
359 if (num
== 1 && !first
&&
360 (output
& (OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
)))
361 /* add extra line between output from more devices */
364 print_value(output
, num
++, devname
, value
, type
, strlen(value
));
366 blkid_tag_iterate_end(iter
);
369 if (!(output
& (OUTPUT_VALUE_ONLY
| OUTPUT_UDEV_LIST
|
370 OUTPUT_EXPORT_LIST
)))
377 static int append_str(char **res
, size_t *sz
, const char *a
, const char *b
)
380 size_t asz
= a
? strlen(a
) : 0;
381 size_t bsz
= b
? strlen(b
) : 0;
382 size_t len
= *sz
+ asz
+ bsz
;
387 str
= realloc(str
, len
+ 1);
408 * Compose and print ID_FS_AMBIVALENT for udev
410 static int print_udev_ambivalent(blkid_probe pr
)
414 int count
= 0, rc
= -1;
416 while (!blkid_do_probe(pr
)) {
417 const char *usage
= NULL
, *type
= NULL
, *version
= NULL
;
420 blkid_probe_lookup_value(pr
, "USAGE", &usage
, NULL
);
421 blkid_probe_lookup_value(pr
, "TYPE", &type
, NULL
);
422 blkid_probe_lookup_value(pr
, "VERSION", &version
, NULL
);
427 blkid_encode_string(usage
, enc
, sizeof(enc
));
428 if (append_str(&val
, &valsz
, enc
, ":"))
431 blkid_encode_string(type
, enc
, sizeof(enc
));
432 if (append_str(&val
, &valsz
, enc
, version
? ":" : " "))
436 blkid_encode_string(version
, enc
, sizeof(enc
));
437 if (append_str(&val
, &valsz
, enc
, " "))
444 *(val
+ valsz
- 1) = '\0'; /* rem tailing whitespace */
445 printf("ID_FS_AMBIVALEN=%s\n", val
);
453 static int lowprobe_superblocks(blkid_probe pr
)
456 int rc
, fd
= blkid_probe_get_fd(pr
);
458 if (fd
< 0 || fstat(fd
, &st
))
461 blkid_probe_enable_partitions(pr
, 1);
463 if (!S_ISCHR(st
.st_mode
) && blkid_probe_get_size(pr
) <= 1024 * 1440 &&
464 blkid_probe_is_wholedisk(pr
)) {
466 * check if the small disk is partitioned, if yes then
467 * don't probe for filesystems.
469 blkid_probe_enable_superblocks(pr
, 0);
471 rc
= blkid_do_fullprobe(pr
);
473 return rc
; /* -1 = error, 1 = nothing, 0 = succes */
475 if (blkid_probe_lookup_value(pr
, "PTTYPE", NULL
, NULL
) == 0)
476 return 0; /* partition table detected */
479 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
480 blkid_probe_enable_superblocks(pr
, 1);
482 return blkid_do_safeprobe(pr
);
485 static int lowprobe_topology(blkid_probe pr
)
487 /* enable topology probing only */
488 blkid_probe_enable_topology(pr
, 1);
490 blkid_probe_enable_superblocks(pr
, 0);
491 blkid_probe_enable_partitions(pr
, 0);
493 return blkid_do_fullprobe(pr
);
496 static int lowprobe_device(blkid_probe pr
, const char *devname
,
497 int chain
, char *show
[], int output
,
498 blkid_loff_t offset
, blkid_loff_t size
)
502 int nvals
= 0, n
, num
= 1;
506 static int first
= 1;
508 fd
= open(devname
, O_RDONLY
);
510 fprintf(stderr
, "error: %s: %m\n", devname
);
513 if (blkid_probe_set_device(pr
, fd
, offset
, size
))
516 if (chain
& LOWPROBE_TOPOLOGY
)
517 rc
= lowprobe_topology(pr
);
518 if (rc
>= 0 && (chain
& LOWPROBE_SUPERBLOCKS
))
519 rc
= lowprobe_superblocks(pr
);
524 nvals
= blkid_probe_numof_values(pr
);
527 !(chain
& LOWPROBE_TOPOLOGY
) &&
528 !(output
& OUTPUT_UDEV_LIST
) &&
529 !blkid_probe_has_value(pr
, "TYPE") &&
530 !blkid_probe_has_value(pr
, "PTTYPE"))
532 * Ignore probing result if there is not any filesystem or
533 * partition table on the device and udev output is not
536 * The udev db stores information about partitions, so
537 * PART_ENTRY_* values are alway important.
541 if (nvals
&& !first
&& output
& (OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
))
542 /* add extra line between output from devices */
545 if (nvals
&& (output
& OUTPUT_DEVICE_ONLY
)) {
546 printf("%s\n", devname
);
550 for (n
= 0; n
< nvals
; n
++) {
551 if (blkid_probe_get_value(pr
, n
, &name
, &data
, &len
))
553 if (show
[0] && !has_item(show
, name
))
555 len
= strnlen((char *) data
, len
);
556 print_value(output
, num
++, devname
, (char *) data
, name
, len
);
561 if (nvals
>= 1 && !(output
& (OUTPUT_VALUE_ONLY
|
562 OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
)))
566 if (output
& OUTPUT_UDEV_LIST
)
567 print_udev_ambivalent(pr
);
570 "%s: ambivalent result (probably more "
571 "filesystems on the device, use wipefs(8) "
572 "to see more details)\n",
578 return 8; /* ambivalent probing result */
580 return 2; /* nothing detected */
582 return 0; /* sucess */
585 /* converts comma separated list to BLKID_USAGE_* mask */
586 static int list_to_usage(const char *list
, int *flag
)
589 const char *word
= NULL
, *p
= list
;
591 if (p
&& strncmp(p
, "no", 2) == 0) {
592 *flag
= BLKID_FLTR_NOTIN
;
602 if (!strncmp(word
, "filesystem", 10))
603 mask
|= BLKID_USAGE_FILESYSTEM
;
604 else if (!strncmp(word
, "raid", 4))
605 mask
|= BLKID_USAGE_RAID
;
606 else if (!strncmp(word
, "crypto", 6))
607 mask
|= BLKID_USAGE_CRYPTO
;
608 else if (!strncmp(word
, "other", 5))
609 mask
|= BLKID_USAGE_OTHER
;
616 fprintf(stderr
, "unknown kerword in -u <list> argument: '%s'\n",
621 /* converts comma separated list to types[] */
622 static char **list_to_types(const char *list
, int *flag
)
625 const char *p
= list
;
628 if (p
&& strncmp(p
, "no", 2) == 0) {
629 *flag
= BLKID_FLTR_NOTIN
;
633 fprintf(stderr
, "error: -u <list> argument is empty\n");
636 for (i
= 1; p
&& (p
= strchr(p
, ',')); i
++, p
++);
638 res
= calloc(i
+ 1, sizeof(char *));
641 p
= *flag
& BLKID_FLTR_NOTIN
? list
+ 2 : list
;
645 const char *word
= p
;
647 res
[i
] = p
? strndup(word
, p
- word
) : strdup(word
);
656 fprintf(stderr
, "out of memory\n");
663 static void free_types_list(char *list
[])
669 for (n
= list
; *n
; n
++)
674 int main(int argc
, char **argv
)
676 blkid_cache cache
= NULL
;
677 char **devices
= NULL
;
678 char *show
[128] = { NULL
, };
679 char *search_type
= NULL
, *search_value
= NULL
;
682 char **fltr_type
= NULL
;
683 int fltr_flag
= BLKID_FLTR_ONLYIN
;
684 unsigned int numdev
= 0, numtag
= 0;
688 int output_format
= 0;
689 int lookup
= 0, gc
= 0, lowprobe
= 0, eval
= 0;
691 uintmax_t offset
= 0, size
= 0;
695 while ((c
= getopt (argc
, argv
, "c:df:ghilL:n:ko:O:ps:S:t:u:U:w:v")) != EOF
)
698 if (optarg
&& !*optarg
)
708 search_value
= strdup(optarg
);
709 search_type
= strdup("LABEL");
713 fprintf(stderr
, "error: -u and -n options are mutually exclusive\n");
716 fltr_type
= list_to_types(optarg
, &fltr_flag
);
720 fprintf(stderr
, "error: -u and -n options are mutually exclusive\n");
723 fltr_usage
= list_to_usage(optarg
, &fltr_flag
);
727 search_value
= strdup(optarg
);
728 search_type
= strdup("UUID");
731 lowprobe
|= LOWPROBE_TOPOLOGY
;
742 const char *name
= NULL
;
744 while (blkid_superblocks_get_name(idx
++, &name
, NULL
) == 0)
745 printf("%s\n", name
);
749 if (!strcmp(optarg
, "value"))
750 output_format
= OUTPUT_VALUE_ONLY
;
751 else if (!strcmp(optarg
, "device"))
752 output_format
= OUTPUT_DEVICE_ONLY
;
753 else if (!strcmp(optarg
, "list"))
754 output_format
= OUTPUT_PRETTY_LIST
; /* deprecated */
755 else if (!strcmp(optarg
, "udev"))
756 output_format
= OUTPUT_UDEV_LIST
;
757 else if (!strcmp(optarg
, "export"))
758 output_format
= OUTPUT_EXPORT_LIST
;
759 else if (!strcmp(optarg
, "full"))
762 fprintf(stderr
, "Invalid output format %s. "
763 "Choose from value,\n\t"
764 "device, list, udev or full\n", optarg
);
769 offset
= strtosize_or_err(optarg
, "failed to parse offset");
772 lowprobe
|= LOWPROBE_SUPERBLOCKS
;
775 if (numtag
+ 1 >= sizeof(show
) / sizeof(*show
)) {
776 fprintf(stderr
, "Too many tags specified\n");
779 show
[numtag
++] = optarg
;
783 size
= strtosize_or_err(optarg
, "failed to parse size");
787 fprintf(stderr
, "Can only search for "
788 "one NAME=value pair\n");
791 if (blkid_parse_tag_string(optarg
,
794 fprintf(stderr
, "-t needs NAME=value pair\n");
802 /* ignore - backward compatibility */
812 /* The rest of the args are device names */
814 devices
= calloc(argc
- optind
, sizeof(char *));
816 fprintf(stderr
, "Failed to allocate device name array\n");
820 while (optind
< argc
)
821 devices
[numdev
++] = argv
[optind
++];
825 print_version(stdout
);
829 /* convert LABEL/UUID lookup to evaluate request */
830 if (lookup
&& output_format
== OUTPUT_DEVICE_ONLY
&& search_type
&&
831 (!strcmp(search_type
, "LABEL") || !strcmp(search_type
, "UUID"))) {
836 if (!lowprobe
&& !eval
&& blkid_get_cache(&cache
, read
) < 0)
840 blkid_gc_cache(cache
);
846 if (eval
== 0 && (output_format
& OUTPUT_PRETTY_LIST
)) {
848 fprintf(stderr
, "The low-level probing mode does not "
849 "support 'list' output format\n");
852 pretty_print_dev(NULL
);
862 fprintf(stderr
, "The low-level probing mode "
863 "requires a device\n");
867 /* automatically enable 'export' format for I/O Limits */
868 if (!output_format
&& (lowprobe
& LOWPROBE_TOPOLOGY
))
869 output_format
= OUTPUT_EXPORT_LIST
;
871 pr
= blkid_new_probe();
875 if (lowprobe
& LOWPROBE_SUPERBLOCKS
) {
876 blkid_probe_set_superblocks_flags(pr
,
877 BLKID_SUBLKS_LABEL
| BLKID_SUBLKS_UUID
|
878 BLKID_SUBLKS_TYPE
| BLKID_SUBLKS_SECTYPE
|
879 BLKID_SUBLKS_USAGE
| BLKID_SUBLKS_VERSION
);
881 if (fltr_usage
&& blkid_probe_filter_superblocks_usage(
882 pr
, fltr_flag
, fltr_usage
))
885 else if (fltr_type
&& blkid_probe_filter_superblocks_type(
886 pr
, fltr_flag
, fltr_type
))
890 for (i
= 0; i
< numdev
; i
++)
891 err
= lowprobe_device(pr
, devices
[i
], lowprobe
, show
,
893 (blkid_loff_t
) offset
,
894 (blkid_loff_t
) size
);
895 blkid_free_probe(pr
);
900 char *res
= blkid_evaluate_tag(search_type
, search_value
, NULL
);
907 * Classic (cache based) API
912 fprintf(stderr
, "The lookup option requires a "
913 "search type specified using -t\n");
916 /* Load any additional devices not in the cache */
917 for (i
= 0; i
< numdev
; i
++)
918 blkid_get_dev(cache
, devices
[i
], BLKID_DEV_NORMAL
);
920 if ((dev
= blkid_find_dev_with_tag(cache
, search_type
,
922 print_tags(dev
, show
, output_format
);
925 /* If we didn't specify a single device, show all available devices */
926 } else if (!numdev
) {
927 blkid_dev_iterate iter
;
930 blkid_probe_all(cache
);
932 iter
= blkid_dev_iterate_begin(cache
);
933 blkid_dev_set_search(iter
, search_type
, search_value
);
934 while (blkid_dev_next(iter
, &dev
) == 0) {
935 dev
= blkid_verify(cache
, dev
);
938 print_tags(dev
, show
, output_format
);
941 blkid_dev_iterate_end(iter
);
942 /* Add all specified devices to cache (optionally display tags) */
943 } else for (i
= 0; i
< numdev
; i
++) {
944 blkid_dev dev
= blkid_get_dev(cache
, devices
[i
],
949 !blkid_dev_has_tag(dev
, search_type
,
952 print_tags(dev
, show
, output_format
);
960 free_types_list(fltr_type
);
961 if (!lowprobe
&& !eval
)
962 blkid_put_cache(cache
);