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>
23 extern int getopt(int argc
, char * const argv
[], const char *optstring
);
28 #define OUTPUT_VALUE_ONLY (1 << 1)
29 #define OUTPUT_DEVICE_ONLY (1 << 2)
30 #define OUTPUT_PRETTY_LIST (1 << 3) /* deprecated */
31 #define OUTPUT_UDEV_LIST (1 << 4) /* deprecated */
32 #define OUTPUT_EXPORT_LIST (1 << 5)
34 #define LOWPROBE_TOPOLOGY (1 << 1)
35 #define LOWPROBE_SUPERBLOCKS (1 << 2)
39 #include "ismounted.h"
41 #define STRTOXX_EXIT_CODE 4 /* strtoxx_or_err() */
43 #include "closestream.h"
46 const char *progname
= "blkid";
50 static void print_version(FILE *out
)
52 fprintf(out
, "%s from %s (libblkid %s, %s)\n",
53 progname
, PACKAGE_STRING
, LIBBLKID_VERSION
, LIBBLKID_DATE
);
56 static void usage(int error
)
58 FILE *out
= error
? stderr
: stdout
;
63 " %1$s -L <label> | -U <uuid>\n\n"
64 " %1$s [-c <file>] [-ghlLv] [-o <format>] [-s <tag>] \n"
65 " [-t <token>] [<dev> ...]\n\n"
66 " %1$s -p [-s <tag>] [-O <offset>] [-S <size>] \n"
67 " [-o <format>] <dev> ...\n\n"
68 " %1$s -i [-s <tag>] [-o <format>] <dev> ...\n\n"
70 " -c <file> read from <file> instead of reading from the default\n"
71 " cache file (-c /dev/null means no cache)\n"
72 " -d don't encode non-printing characters\n"
73 " -h print this usage message and exit\n"
74 " -g garbage collect the blkid cache\n"
75 " -o <format> output format; can be one of:\n"
76 " value, device, export or full; (default: full)\n"
77 " -k list all known filesystems/RAIDs and exit\n"
78 " -s <tag> show specified tag(s) (default show all tags)\n"
79 " -t <token> find device with a specific token (NAME=value pair)\n"
80 " -l look up only first device with token specified by -t\n"
81 " -L <label> convert LABEL to device name\n"
82 " -U <uuid> convert UUID to device name\n"
83 " -v print version and exit\n"
84 " <dev> specify device(s) to probe (default: all devices)\n\n"
85 "Low-level probing options:\n"
86 " -p low-level superblocks probing (bypass cache)\n"
87 " -i gather information about I/O limits\n"
88 " -S <size> overwrite device size\n"
89 " -O <offset> probe at the given offset\n"
90 " -u <list> filter by \"usage\" (e.g. -u filesystem,raid)\n"
91 " -n <list> filter by filesystem type (e.g. -n vfat,ext3)\n"
98 * This function does "safe" printing. It will convert non-printable
99 * ASCII characters using '^' and M- notation.
101 static void safe_print(const char *cp
, int len
)
115 if ((ch
< 32) || (ch
== 0x7f)) {
117 ch
^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
124 static int pretty_print_word(const char *str
, int max_len
,
125 int left_len
, int overflow_nl
)
127 int len
= strlen(str
) + left_len
;
131 if (overflow_nl
&& len
> max_len
) {
134 } else if (len
> max_len
)
138 while (len
++ < max_len
);
142 static void pretty_print_line(const char *device
, const char *fs_type
,
143 const char *label
, const char *mtpt
,
146 static int device_len
= 10, fs_type_len
= 7;
147 static int label_len
= 8, mtpt_len
= 14;
148 static int term_width
= -1;
151 if (term_width
< 0) {
152 term_width
= get_terminal_width();
156 if (term_width
> 80) {
169 len
= pretty_print_word(device
, device_len
, 0, 1);
170 len
= pretty_print_word(fs_type
, fs_type_len
, len
, 0);
171 len
= pretty_print_word(label
, label_len
, len
, 0);
172 pretty_print_word(mtpt
, mtpt_len
, len
, 0);
178 static void pretty_print_dev(blkid_dev dev
)
180 blkid_tag_iterate iter
;
181 const char *type
, *value
, *devname
;
182 const char *uuid
= "", *fs_type
= "", *label
= "";
183 int len
, mount_flags
;
188 pretty_print_line("device", "fs_type", "label",
189 "mount point", "UUID");
190 for (len
=get_terminal_width()-1; len
> 0; len
--)
196 devname
= blkid_dev_devname(dev
);
197 if (access(devname
, F_OK
))
200 /* Get the uuid, label, type */
201 iter
= blkid_tag_iterate_begin(dev
);
202 while (blkid_tag_next(iter
, &type
, &value
) == 0) {
203 if (!strcmp(type
, "UUID"))
205 if (!strcmp(type
, "TYPE"))
207 if (!strcmp(type
, "LABEL"))
210 blkid_tag_iterate_end(iter
);
212 /* Get the mount point */
214 retval
= check_mount_point(devname
, &mount_flags
, mtpt
, sizeof(mtpt
));
216 if (mount_flags
& MF_MOUNTED
) {
218 strcpy(mtpt
, "(mounted, mtpt unknown)");
219 } else if (mount_flags
& MF_BUSY
)
220 strcpy(mtpt
, "(in use)");
222 strcpy(mtpt
, "(not mounted)");
225 pretty_print_line(devname
, fs_type
, label
, mtpt
, uuid
);
228 static void print_udev_format(const char *name
, const char *value
)
230 char enc
[265], safe
[256];
231 size_t namelen
= strlen(name
);
235 if (!strcmp(name
, "TYPE") || !strcmp(name
, "VERSION")) {
236 blkid_encode_string(value
, enc
, sizeof(enc
));
237 printf("ID_FS_%s=%s\n", name
, enc
);
239 } else if (!strcmp(name
, "UUID") ||
240 !strcmp(name
, "LABEL") ||
241 !strcmp(name
, "UUID_SUB")) {
243 blkid_safe_string(value
, safe
, sizeof(safe
));
244 printf("ID_FS_%s=%s\n", name
, safe
);
246 blkid_encode_string(value
, enc
, sizeof(enc
));
247 printf("ID_FS_%s_ENC=%s\n", name
, enc
);
249 } else if (!strcmp(name
, "PTTYPE")) {
250 printf("ID_PART_TABLE_TYPE=%s\n", value
);
252 } else if (!strcmp(name
, "PART_ENTRY_NAME") ||
253 !strcmp(name
, "PART_ENTRY_TYPE")) {
255 blkid_encode_string(value
, enc
, sizeof(enc
));
256 printf("ID_%s=%s\n", name
, enc
);
258 } else if (!strncmp(name
, "PART_ENTRY_", 11))
259 printf("ID_%s=%s\n", name
, value
);
261 else if (namelen
>= 15 && (
262 !strcmp(name
+ (namelen
- 12), "_SECTOR_SIZE") ||
263 !strcmp(name
+ (namelen
- 8), "_IO_SIZE") ||
264 !strcmp(name
, "ALIGNMENT_OFFSET")))
265 printf("ID_IOLIMIT_%s=%s\n", name
, value
);
267 printf("ID_FS_%s=%s\n", name
, value
);
270 static int has_item(char *ary
[], const char *item
)
274 for (p
= ary
; *p
!= NULL
; p
++)
275 if (!strcmp(item
, *p
))
280 static void print_value(int output
, int num
, const char *devname
,
281 const char *value
, const char *name
, size_t valsz
)
283 if (output
& OUTPUT_VALUE_ONLY
) {
284 fputs(value
, stdout
);
287 } else if (output
& OUTPUT_UDEV_LIST
) {
288 print_udev_format(name
, value
);
290 } else if (output
& OUTPUT_EXPORT_LIST
) {
291 if (num
== 1 && devname
)
292 printf("DEVNAME=%s\n", devname
);
295 safe_print(value
, valsz
);
299 if (num
== 1 && devname
)
300 printf("%s: ", devname
);
302 fputs("=\"", stdout
);
303 safe_print(value
, valsz
);
304 fputs("\" ", stdout
);
308 static void print_tags(blkid_dev dev
, char *show
[], int output
)
310 blkid_tag_iterate iter
;
311 const char *type
, *value
, *devname
;
313 static int first
= 1;
318 if (output
& OUTPUT_PRETTY_LIST
) {
319 pretty_print_dev(dev
);
323 devname
= blkid_dev_devname(dev
);
325 if (output
& OUTPUT_DEVICE_ONLY
) {
326 printf("%s\n", devname
);
330 iter
= blkid_tag_iterate_begin(dev
);
331 while (blkid_tag_next(iter
, &type
, &value
) == 0) {
332 if (show
[0] && !has_item(show
, type
))
335 if (num
== 1 && !first
&&
336 (output
& (OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
)))
337 /* add extra line between output from more devices */
340 print_value(output
, num
++, devname
, value
, type
, strlen(value
));
342 blkid_tag_iterate_end(iter
);
345 if (!(output
& (OUTPUT_VALUE_ONLY
| OUTPUT_UDEV_LIST
|
346 OUTPUT_EXPORT_LIST
)))
353 static int append_str(char **res
, size_t *sz
, const char *a
, const char *b
)
356 size_t asz
= a
? strlen(a
) : 0;
357 size_t bsz
= b
? strlen(b
) : 0;
358 size_t len
= *sz
+ asz
+ bsz
;
363 str
= realloc(str
, len
+ 1);
384 * Compose and print ID_FS_AMBIVALENT for udev
386 static int print_udev_ambivalent(blkid_probe pr
)
390 int count
= 0, rc
= -1;
392 while (!blkid_do_probe(pr
)) {
393 const char *usage
= NULL
, *type
= NULL
, *version
= NULL
;
396 blkid_probe_lookup_value(pr
, "USAGE", &usage
, NULL
);
397 blkid_probe_lookup_value(pr
, "TYPE", &type
, NULL
);
398 blkid_probe_lookup_value(pr
, "VERSION", &version
, NULL
);
403 blkid_encode_string(usage
, enc
, sizeof(enc
));
404 if (append_str(&val
, &valsz
, enc
, ":"))
407 blkid_encode_string(type
, enc
, sizeof(enc
));
408 if (append_str(&val
, &valsz
, enc
, version
? ":" : " "))
412 blkid_encode_string(version
, enc
, sizeof(enc
));
413 if (append_str(&val
, &valsz
, enc
, " "))
420 *(val
+ valsz
- 1) = '\0'; /* rem tailing whitespace */
421 printf("ID_FS_AMBIVALEN=%s\n", val
);
429 static int lowprobe_superblocks(blkid_probe pr
)
432 int rc
, fd
= blkid_probe_get_fd(pr
);
434 if (fd
< 0 || fstat(fd
, &st
))
437 blkid_probe_enable_partitions(pr
, 1);
439 if (!S_ISCHR(st
.st_mode
) && blkid_probe_get_size(pr
) <= 1024 * 1440 &&
440 blkid_probe_is_wholedisk(pr
)) {
442 * check if the small disk is partitioned, if yes then
443 * don't probe for filesystems.
445 blkid_probe_enable_superblocks(pr
, 0);
447 rc
= blkid_do_fullprobe(pr
);
449 return rc
; /* -1 = error, 1 = nothing, 0 = succes */
451 if (blkid_probe_lookup_value(pr
, "PTTYPE", NULL
, NULL
) == 0)
452 return 0; /* partition table detected */
455 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
456 blkid_probe_enable_superblocks(pr
, 1);
458 return blkid_do_safeprobe(pr
);
461 static int lowprobe_topology(blkid_probe pr
)
463 /* enable topology probing only */
464 blkid_probe_enable_topology(pr
, 1);
466 blkid_probe_enable_superblocks(pr
, 0);
467 blkid_probe_enable_partitions(pr
, 0);
469 return blkid_do_fullprobe(pr
);
472 static int lowprobe_device(blkid_probe pr
, const char *devname
,
473 int chain
, char *show
[], int output
,
474 blkid_loff_t offset
, blkid_loff_t size
)
478 int nvals
= 0, n
, num
= 1;
482 static int first
= 1;
484 fd
= open(devname
, O_RDONLY
);
486 fprintf(stderr
, "error: %s: %m\n", devname
);
489 if (blkid_probe_set_device(pr
, fd
, offset
, size
))
492 if (chain
& LOWPROBE_TOPOLOGY
)
493 rc
= lowprobe_topology(pr
);
494 if (rc
>= 0 && (chain
& LOWPROBE_SUPERBLOCKS
))
495 rc
= lowprobe_superblocks(pr
);
500 nvals
= blkid_probe_numof_values(pr
);
503 !(chain
& LOWPROBE_TOPOLOGY
) &&
504 !(output
& OUTPUT_UDEV_LIST
) &&
505 !blkid_probe_has_value(pr
, "TYPE") &&
506 !blkid_probe_has_value(pr
, "PTTYPE"))
508 * Ignore probing result if there is not any filesystem or
509 * partition table on the device and udev output is not
512 * The udev db stores information about partitions, so
513 * PART_ENTRY_* values are alway important.
517 if (nvals
&& !first
&& output
& (OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
))
518 /* add extra line between output from devices */
521 if (nvals
&& (output
& OUTPUT_DEVICE_ONLY
)) {
522 printf("%s\n", devname
);
526 for (n
= 0; n
< nvals
; n
++) {
527 if (blkid_probe_get_value(pr
, n
, &name
, &data
, &len
))
529 if (show
[0] && !has_item(show
, name
))
531 len
= strnlen((char *) data
, len
);
532 print_value(output
, num
++, devname
, (char *) data
, name
, len
);
537 if (nvals
>= 1 && !(output
& (OUTPUT_VALUE_ONLY
|
538 OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
)))
542 if (output
& OUTPUT_UDEV_LIST
)
543 print_udev_ambivalent(pr
);
546 "%s: ambivalent result (probably more "
547 "filesystems on the device, use wipefs(8) "
548 "to see more details)\n",
554 return 8; /* ambivalent probing result */
556 return 2; /* nothing detected */
558 return 0; /* success */
561 /* converts comma separated list to BLKID_USAGE_* mask */
562 static int list_to_usage(const char *list
, int *flag
)
565 const char *word
= NULL
, *p
= list
;
567 if (p
&& strncmp(p
, "no", 2) == 0) {
568 *flag
= BLKID_FLTR_NOTIN
;
578 if (!strncmp(word
, "filesystem", 10))
579 mask
|= BLKID_USAGE_FILESYSTEM
;
580 else if (!strncmp(word
, "raid", 4))
581 mask
|= BLKID_USAGE_RAID
;
582 else if (!strncmp(word
, "crypto", 6))
583 mask
|= BLKID_USAGE_CRYPTO
;
584 else if (!strncmp(word
, "other", 5))
585 mask
|= BLKID_USAGE_OTHER
;
592 fprintf(stderr
, "unknown kerword in -u <list> argument: '%s'\n",
597 /* converts comma separated list to types[] */
598 static char **list_to_types(const char *list
, int *flag
)
601 const char *p
= list
;
604 if (p
&& strncmp(p
, "no", 2) == 0) {
605 *flag
= BLKID_FLTR_NOTIN
;
609 fprintf(stderr
, "error: -u <list> argument is empty\n");
612 for (i
= 1; p
&& (p
= strchr(p
, ',')); i
++, p
++);
614 res
= calloc(i
+ 1, sizeof(char *));
617 p
= *flag
& BLKID_FLTR_NOTIN
? list
+ 2 : list
;
621 const char *word
= p
;
623 res
[i
] = p
? strndup(word
, p
- word
) : strdup(word
);
632 fprintf(stderr
, "out of memory\n");
639 static void free_types_list(char *list
[])
645 for (n
= list
; *n
; n
++)
650 int main(int argc
, char **argv
)
652 blkid_cache cache
= NULL
;
653 char **devices
= NULL
;
654 char *show
[128] = { NULL
, };
655 char *search_type
= NULL
, *search_value
= NULL
;
658 char **fltr_type
= NULL
;
659 int fltr_flag
= BLKID_FLTR_ONLYIN
;
660 unsigned int numdev
= 0, numtag
= 0;
664 int output_format
= 0;
665 int lookup
= 0, gc
= 0, lowprobe
= 0, eval
= 0;
667 uintmax_t offset
= 0, size
= 0;
670 atexit(close_stdout
);
672 while ((c
= getopt (argc
, argv
, "c:df:ghilL:n:ko:O:ps:S:t:u:U:w:v")) != EOF
)
675 if (optarg
&& !*optarg
)
685 search_value
= strdup(optarg
);
686 search_type
= strdup("LABEL");
690 fprintf(stderr
, "error: -u and -n options are mutually exclusive\n");
693 fltr_type
= list_to_types(optarg
, &fltr_flag
);
697 fprintf(stderr
, "error: -u and -n options are mutually exclusive\n");
700 fltr_usage
= list_to_usage(optarg
, &fltr_flag
);
704 search_value
= strdup(optarg
);
705 search_type
= strdup("UUID");
708 lowprobe
|= LOWPROBE_TOPOLOGY
;
719 const char *name
= NULL
;
721 while (blkid_superblocks_get_name(idx
++, &name
, NULL
) == 0)
722 printf("%s\n", name
);
726 if (!strcmp(optarg
, "value"))
727 output_format
= OUTPUT_VALUE_ONLY
;
728 else if (!strcmp(optarg
, "device"))
729 output_format
= OUTPUT_DEVICE_ONLY
;
730 else if (!strcmp(optarg
, "list"))
731 output_format
= OUTPUT_PRETTY_LIST
; /* deprecated */
732 else if (!strcmp(optarg
, "udev"))
733 output_format
= OUTPUT_UDEV_LIST
;
734 else if (!strcmp(optarg
, "export"))
735 output_format
= OUTPUT_EXPORT_LIST
;
736 else if (!strcmp(optarg
, "full"))
739 fprintf(stderr
, "Invalid output format %s. "
740 "Choose from value,\n\t"
741 "device, list, udev or full\n", optarg
);
746 offset
= strtosize_or_err(optarg
, "failed to parse offset");
749 lowprobe
|= LOWPROBE_SUPERBLOCKS
;
752 if (numtag
+ 1 >= sizeof(show
) / sizeof(*show
)) {
753 fprintf(stderr
, "Too many tags specified\n");
756 show
[numtag
++] = optarg
;
760 size
= strtosize_or_err(optarg
, "failed to parse size");
764 fprintf(stderr
, "Can only search for "
765 "one NAME=value pair\n");
768 if (blkid_parse_tag_string(optarg
,
771 fprintf(stderr
, "-t needs NAME=value pair\n");
779 /* ignore - backward compatibility */
789 /* The rest of the args are device names */
791 devices
= calloc(argc
- optind
, sizeof(char *));
793 fprintf(stderr
, "Failed to allocate device name array\n");
797 while (optind
< argc
)
798 devices
[numdev
++] = argv
[optind
++];
802 print_version(stdout
);
806 /* convert LABEL/UUID lookup to evaluate request */
807 if (lookup
&& output_format
== OUTPUT_DEVICE_ONLY
&& search_type
&&
808 (!strcmp(search_type
, "LABEL") || !strcmp(search_type
, "UUID"))) {
813 if (!lowprobe
&& !eval
&& blkid_get_cache(&cache
, read
) < 0)
817 blkid_gc_cache(cache
);
823 if (eval
== 0 && (output_format
& OUTPUT_PRETTY_LIST
)) {
825 fprintf(stderr
, "The low-level probing mode does not "
826 "support 'list' output format\n");
829 pretty_print_dev(NULL
);
839 fprintf(stderr
, "The low-level probing mode "
840 "requires a device\n");
844 /* automatically enable 'export' format for I/O Limits */
845 if (!output_format
&& (lowprobe
& LOWPROBE_TOPOLOGY
))
846 output_format
= OUTPUT_EXPORT_LIST
;
848 pr
= blkid_new_probe();
852 if (lowprobe
& LOWPROBE_SUPERBLOCKS
) {
853 blkid_probe_set_superblocks_flags(pr
,
854 BLKID_SUBLKS_LABEL
| BLKID_SUBLKS_UUID
|
855 BLKID_SUBLKS_TYPE
| BLKID_SUBLKS_SECTYPE
|
856 BLKID_SUBLKS_USAGE
| BLKID_SUBLKS_VERSION
);
858 if (fltr_usage
&& blkid_probe_filter_superblocks_usage(
859 pr
, fltr_flag
, fltr_usage
))
862 else if (fltr_type
&& blkid_probe_filter_superblocks_type(
863 pr
, fltr_flag
, fltr_type
))
867 for (i
= 0; i
< numdev
; i
++)
868 err
= lowprobe_device(pr
, devices
[i
], lowprobe
, show
,
870 (blkid_loff_t
) offset
,
871 (blkid_loff_t
) size
);
872 blkid_free_probe(pr
);
877 char *res
= blkid_evaluate_tag(search_type
, search_value
, NULL
);
884 * Classic (cache based) API
889 fprintf(stderr
, "The lookup option requires a "
890 "search type specified using -t\n");
893 /* Load any additional devices not in the cache */
894 for (i
= 0; i
< numdev
; i
++)
895 blkid_get_dev(cache
, devices
[i
], BLKID_DEV_NORMAL
);
897 if ((dev
= blkid_find_dev_with_tag(cache
, search_type
,
899 print_tags(dev
, show
, output_format
);
902 /* If we didn't specify a single device, show all available devices */
903 } else if (!numdev
) {
904 blkid_dev_iterate iter
;
907 blkid_probe_all(cache
);
909 iter
= blkid_dev_iterate_begin(cache
);
910 blkid_dev_set_search(iter
, search_type
, search_value
);
911 while (blkid_dev_next(iter
, &dev
) == 0) {
912 dev
= blkid_verify(cache
, dev
);
915 print_tags(dev
, show
, output_format
);
918 blkid_dev_iterate_end(iter
);
919 /* Add all specified devices to cache (optionally display tags) */
920 } else for (i
= 0; i
< numdev
; i
++) {
921 blkid_dev dev
= blkid_get_dev(cache
, devices
[i
],
926 !blkid_dev_has_tag(dev
, search_type
,
929 print_tags(dev
, show
, output_format
);
937 free_types_list(fltr_type
);
938 if (!lowprobe
&& !eval
)
939 blkid_put_cache(cache
);