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>
22 #define OUTPUT_FULL (1 << 0)
23 #define OUTPUT_VALUE_ONLY (1 << 1)
24 #define OUTPUT_DEVICE_ONLY (1 << 2)
25 #define OUTPUT_PRETTY_LIST (1 << 3) /* deprecated */
26 #define OUTPUT_UDEV_LIST (1 << 4) /* deprecated */
27 #define OUTPUT_EXPORT_LIST (1 << 5)
29 #define BLKID_EXIT_NOTFOUND 2 /* token or device not found */
30 #define BLKID_EXIT_OTHER 4 /* bad usage or other error */
31 #define BLKID_EXIT_AMBIVAL 8 /* ambivalent low-level probing detected */
35 #include "ismounted.h"
37 #define STRTOXX_EXIT_CODE BLKID_EXIT_OTHER /* strtoxx_or_err() */
39 #define OPTUTILS_EXIT_CODE BLKID_EXIT_OTHER /* exclusive_option() */
41 #define CLOSE_EXIT_CODE BLKID_EXIT_OTHER /* close_stdout() */
42 #include "closestream.h"
48 struct blkid_control
{
58 lowprobe_superblocks
:1,
63 static void print_version(FILE *out
)
65 fprintf(out
, _("%s from %s (libblkid %s, %s)\n"),
66 program_invocation_short_name
, PACKAGE_STRING
,
67 LIBBLKID_VERSION
, LIBBLKID_DATE
);
70 static void usage(int error
)
72 FILE *out
= error
? stderr
: stdout
;
74 fputs(USAGE_HEADER
, out
);
75 fprintf(out
, _( " %s -L <label> | -U <uuid>\n\n"), program_invocation_short_name
);
76 fprintf(out
, _( " %s [-c <file>] [-ghlLv] [-o <format>] [-s <tag>] \n"
77 " [-t <token>] [<dev> ...]\n\n"), program_invocation_short_name
);
78 fprintf(out
, _( " %s -p [-s <tag>] [-O <offset>] [-S <size>] \n"
79 " [-o <format>] <dev> ...\n\n"), program_invocation_short_name
);
80 fprintf(out
, _( " %s -i [-s <tag>] [-o <format>] <dev> ...\n"), program_invocation_short_name
);
81 fputs(USAGE_OPTIONS
, out
);
82 fputs(_( " -c <file> read from <file> instead of reading from the default\n"
83 " cache file (-c /dev/null means no cache)\n"), out
);
84 fputs(_( " -d don't encode non-printing characters\n"), out
);
85 fputs(_( " -h print this usage message and exit\n"), out
);
86 fputs(_( " -g garbage collect the blkid cache\n"), out
);
87 fputs(_( " -o <format> output format; can be one of:\n"
88 " value, device, export or full; (default: full)\n"), out
);
89 fputs(_( " -k list all known filesystems/RAIDs and exit\n"), out
);
90 fputs(_( " -s <tag> show specified tag(s) (default show all tags)\n"), out
);
91 fputs(_( " -t <token> find device with a specific token (NAME=value pair)\n"), out
);
92 fputs(_( " -l look up only first device with token specified by -t\n"), out
);
93 fputs(_( " -L <label> convert LABEL to device name\n"), out
);
94 fputs(_( " -U <uuid> convert UUID to device name\n"), out
);
95 fputs(_( " -V print version and exit\n"), out
);
96 fputs(_( " <dev> specify device(s) to probe (default: all devices)\n"), out
);
98 fputs(_( "Low-level probing options:\n"), out
);
99 fputs(_( " -p low-level superblocks probing (bypass cache)\n"), out
);
100 fputs(_( " -i gather information about I/O limits\n"), out
);
101 fputs(_( " -S <size> overwrite device size\n"), out
);
102 fputs(_( " -O <offset> probe at the given offset\n"), out
);
103 fputs(_( " -u <list> filter by \"usage\" (e.g. -u filesystem,raid)\n"), out
);
104 fputs(_( " -n <list> filter by filesystem type (e.g. -n vfat,ext3)\n"), out
);
106 fprintf(out
, USAGE_MAN_TAIL("blkid(8)"));
111 * This function does "safe" printing. It will convert non-printable
112 * ASCII characters using '^' and M- notation.
114 * If 'esc' is defined then escape all chars from esc by \.
116 static void safe_print(const struct blkid_control
*ctl
, const char *cp
, int len
,
126 if (!ctl
->raw_chars
) {
131 if ((ch
< 32) || (ch
== 0x7f)) {
133 ch
^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
135 } else if (esc
&& strchr(esc
, ch
))
142 static int pretty_print_word(const char *str
, int max_len
,
143 int left_len
, int overflow_nl
)
145 int len
= strlen(str
) + left_len
;
149 if (overflow_nl
&& len
> max_len
) {
152 } else if (len
> max_len
)
156 } while (len
++ < max_len
);
160 static void pretty_print_line(const char *device
, const char *fs_type
,
161 const char *label
, const char *mtpt
,
164 static int device_len
= 10, fs_type_len
= 7;
165 static int label_len
= 8, mtpt_len
= 14;
166 static int term_width
= -1;
169 if (term_width
< 0) {
170 term_width
= get_terminal_width(80);
172 if (term_width
> 80) {
185 len
= pretty_print_word(device
, device_len
, 0, 1);
186 len
= pretty_print_word(fs_type
, fs_type_len
, len
, 0);
187 len
= pretty_print_word(label
, label_len
, len
, 0);
188 pretty_print_word(mtpt
, mtpt_len
, len
, 0);
194 static void pretty_print_dev(blkid_dev dev
)
196 blkid_tag_iterate iter
;
197 const char *type
, *value
, *devname
;
198 const char *uuid
= "", *fs_type
= "", *label
= "";
199 int len
, mount_flags
;
204 pretty_print_line("device", "fs_type", "label",
205 "mount point", "UUID");
206 for (len
=get_terminal_width(0)-1; len
> 0; len
--)
212 devname
= blkid_dev_devname(dev
);
213 if (access(devname
, F_OK
))
216 /* Get the uuid, label, type */
217 iter
= blkid_tag_iterate_begin(dev
);
218 while (blkid_tag_next(iter
, &type
, &value
) == 0) {
219 if (!strcmp(type
, "UUID"))
221 if (!strcmp(type
, "TYPE"))
223 if (!strcmp(type
, "LABEL"))
226 blkid_tag_iterate_end(iter
);
228 /* Get the mount point */
230 retval
= check_mount_point(devname
, &mount_flags
, mtpt
, sizeof(mtpt
));
232 if (mount_flags
& MF_MOUNTED
) {
234 strcpy(mtpt
, _("(mounted, mtpt unknown)"));
235 } else if (mount_flags
& MF_BUSY
)
236 strcpy(mtpt
, _("(in use)"));
238 strcpy(mtpt
, _("(not mounted)"));
241 pretty_print_line(devname
, fs_type
, label
, mtpt
, uuid
);
244 static void print_udev_format(const char *name
, const char *value
)
246 char enc
[265], safe
[256];
247 size_t namelen
= strlen(name
);
251 if (!strcmp(name
, "TYPE") || !strcmp(name
, "VERSION")) {
252 blkid_encode_string(value
, enc
, sizeof(enc
));
253 printf("ID_FS_%s=%s\n", name
, enc
);
255 } else if (!strcmp(name
, "UUID") ||
256 !strcmp(name
, "LABEL") ||
257 !strcmp(name
, "UUID_SUB")) {
259 blkid_safe_string(value
, safe
, sizeof(safe
));
260 printf("ID_FS_%s=%s\n", name
, safe
);
262 blkid_encode_string(value
, enc
, sizeof(enc
));
263 printf("ID_FS_%s_ENC=%s\n", name
, enc
);
265 } else if (!strcmp(name
, "PTUUID")) {
266 printf("ID_PART_TABLE_UUID=%s\n", value
);
268 } else if (!strcmp(name
, "PTTYPE")) {
269 printf("ID_PART_TABLE_TYPE=%s\n", value
);
271 } else if (!strcmp(name
, "PART_ENTRY_NAME") ||
272 !strcmp(name
, "PART_ENTRY_TYPE")) {
274 blkid_encode_string(value
, enc
, sizeof(enc
));
275 printf("ID_%s=%s\n", name
, enc
);
277 } else if (!strncmp(name
, "PART_ENTRY_", 11))
278 printf("ID_%s=%s\n", name
, value
);
280 else if (namelen
>= 15 && (
281 !strcmp(name
+ (namelen
- 12), "_SECTOR_SIZE") ||
282 !strcmp(name
+ (namelen
- 8), "_IO_SIZE") ||
283 !strcmp(name
, "ALIGNMENT_OFFSET")))
284 printf("ID_IOLIMIT_%s=%s\n", name
, value
);
286 printf("ID_FS_%s=%s\n", name
, value
);
289 static int has_item(const struct blkid_control
*ctl
, const char *item
)
293 for (p
= ctl
->show
; *p
!= NULL
; p
++)
294 if (!strcmp(item
, *p
))
299 static void print_value(const struct blkid_control
*ctl
, int num
,
300 const char *devname
, const char *value
,
301 const char *name
, size_t valsz
)
303 if (ctl
->output
& OUTPUT_VALUE_ONLY
) {
304 fputs(value
, stdout
);
307 } else if (ctl
->output
& OUTPUT_UDEV_LIST
) {
308 print_udev_format(name
, value
);
310 } else if (ctl
->output
& OUTPUT_EXPORT_LIST
) {
311 if (num
== 1 && devname
)
312 printf("DEVNAME=%s\n", devname
);
315 safe_print(ctl
, value
, valsz
, " \\\"'$`<>");
319 if (num
== 1 && devname
)
320 printf("%s:", devname
);
323 fputs("=\"", stdout
);
324 safe_print(ctl
, value
, valsz
, "\"\\");
329 static void print_tags(const struct blkid_control
*ctl
, blkid_dev dev
)
331 blkid_tag_iterate iter
;
332 const char *type
, *value
, *devname
;
334 static int first
= 1;
339 if (ctl
->output
& OUTPUT_PRETTY_LIST
) {
340 pretty_print_dev(dev
);
344 devname
= blkid_dev_devname(dev
);
346 if (ctl
->output
& OUTPUT_DEVICE_ONLY
) {
347 printf("%s\n", devname
);
351 iter
= blkid_tag_iterate_begin(dev
);
352 while (blkid_tag_next(iter
, &type
, &value
) == 0) {
353 if (ctl
->show
[0] && !has_item(ctl
, type
))
356 if (num
== 1 && !first
&&
357 (ctl
->output
& (OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
)))
358 /* add extra line between output from more devices */
361 print_value(ctl
, num
++, devname
, value
, type
, strlen(value
));
363 blkid_tag_iterate_end(iter
);
366 if (!(ctl
->output
& (OUTPUT_VALUE_ONLY
| OUTPUT_UDEV_LIST
|
367 OUTPUT_EXPORT_LIST
)))
374 static int append_str(char **res
, size_t *sz
, const char *a
, const char *b
)
377 size_t asz
= a
? strlen(a
) : 0;
378 size_t bsz
= b
? strlen(b
) : 0;
379 size_t len
= *sz
+ asz
+ bsz
;
384 *res
= str
= xrealloc(str
, len
+ 1);
401 * Compose and print ID_FS_AMBIVALENT for udev
403 static int print_udev_ambivalent(blkid_probe pr
)
407 int count
= 0, rc
= -1;
409 while (!blkid_do_probe(pr
)) {
410 const char *usage_txt
= NULL
, *type
= NULL
, *version
= NULL
;
413 blkid_probe_lookup_value(pr
, "USAGE", &usage_txt
, NULL
);
414 blkid_probe_lookup_value(pr
, "TYPE", &type
, NULL
);
415 blkid_probe_lookup_value(pr
, "VERSION", &version
, NULL
);
417 if (!usage_txt
|| !type
)
420 blkid_encode_string(usage_txt
, enc
, sizeof(enc
));
421 if (append_str(&val
, &valsz
, enc
, ":"))
424 blkid_encode_string(type
, enc
, sizeof(enc
));
425 if (append_str(&val
, &valsz
, enc
, version
? ":" : " "))
429 blkid_encode_string(version
, enc
, sizeof(enc
));
430 if (append_str(&val
, &valsz
, enc
, " "))
437 *(val
+ valsz
- 1) = '\0'; /* rem tailing whitespace */
438 printf("ID_FS_AMBIVALENT=%s\n", val
);
446 static int lowprobe_superblocks(blkid_probe pr
)
449 int rc
, fd
= blkid_probe_get_fd(pr
);
451 if (fd
< 0 || fstat(fd
, &st
))
454 blkid_probe_enable_partitions(pr
, 1);
456 if (!S_ISCHR(st
.st_mode
) && blkid_probe_get_size(pr
) <= 1024 * 1440 &&
457 blkid_probe_is_wholedisk(pr
)) {
459 * check if the small disk is partitioned, if yes then
460 * don't probe for filesystems.
462 blkid_probe_enable_superblocks(pr
, 0);
464 rc
= blkid_do_fullprobe(pr
);
466 return rc
; /* -1 = error, 1 = nothing, 0 = success */
468 if (blkid_probe_lookup_value(pr
, "PTTYPE", NULL
, NULL
) == 0)
469 return 0; /* partition table detected */
472 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
473 blkid_probe_enable_superblocks(pr
, 1);
475 return blkid_do_safeprobe(pr
);
478 static int lowprobe_topology(blkid_probe pr
)
480 /* enable topology probing only */
481 blkid_probe_enable_topology(pr
, 1);
483 blkid_probe_enable_superblocks(pr
, 0);
484 blkid_probe_enable_partitions(pr
, 0);
486 return blkid_do_fullprobe(pr
);
489 static int lowprobe_device(blkid_probe pr
, const char *devname
,
490 struct blkid_control
*ctl
)
494 int nvals
= 0, n
, num
= 1;
498 static int first
= 1;
500 fd
= open(devname
, O_RDONLY
|O_CLOEXEC
);
502 warn(_("error: %s"), devname
);
503 return BLKID_EXIT_NOTFOUND
;
505 if (blkid_probe_set_device(pr
, fd
, ctl
->offset
, ctl
->size
))
508 if (ctl
->lowprobe_topology
)
509 rc
= lowprobe_topology(pr
);
510 if (rc
>= 0 && ctl
->lowprobe_superblocks
)
511 rc
= lowprobe_superblocks(pr
);
516 nvals
= blkid_probe_numof_values(pr
);
518 if (nvals
&& !first
&& ctl
->output
& (OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
))
519 /* add extra line between output from devices */
522 if (nvals
&& (ctl
->output
& OUTPUT_DEVICE_ONLY
)) {
523 printf("%s\n", devname
);
527 for (n
= 0; n
< nvals
; n
++) {
528 if (blkid_probe_get_value(pr
, n
, &name
, &data
, &len
))
530 if (ctl
->show
[0] && !has_item(ctl
, name
))
532 len
= strnlen((char *) data
, len
);
533 print_value(ctl
, num
++, devname
, (char *) data
, name
, len
);
539 if (nvals
>= 1 && !(ctl
->output
& (OUTPUT_VALUE_ONLY
|
540 OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
)))
544 if (ctl
->output
& OUTPUT_UDEV_LIST
)
545 print_udev_ambivalent(pr
);
547 warnx(_("%s: ambivalent result (probably more "
548 "filesystems on the device, use wipefs(8) "
549 "to see more details)"),
555 return BLKID_EXIT_AMBIVAL
; /* ambivalent probing result */
557 return BLKID_EXIT_NOTFOUND
; /* nothing detected */
559 return 0; /* success */
562 /* converts comma separated list to BLKID_USAGE_* mask */
563 static int list_to_usage(const char *list
, int *flag
)
566 const char *word
= NULL
, *p
= list
;
568 if (p
&& strncmp(p
, "no", 2) == 0) {
569 *flag
= BLKID_FLTR_NOTIN
;
579 if (!strncmp(word
, "filesystem", 10))
580 mask
|= BLKID_USAGE_FILESYSTEM
;
581 else if (!strncmp(word
, "raid", 4))
582 mask
|= BLKID_USAGE_RAID
;
583 else if (!strncmp(word
, "crypto", 6))
584 mask
|= BLKID_USAGE_CRYPTO
;
585 else if (!strncmp(word
, "other", 5))
586 mask
|= BLKID_USAGE_OTHER
;
593 warnx(_("unknown keyword in -u <list> argument: '%s'"),
595 exit(BLKID_EXIT_OTHER
);
598 /* converts comma separated list to types[] */
599 static char **list_to_types(const char *list
, int *flag
)
602 const char *p
= list
;
605 if (p
&& strncmp(p
, "no", 2) == 0) {
606 *flag
= BLKID_FLTR_NOTIN
;
610 warnx(_("error: -u <list> argument is empty"));
613 for (i
= 1; p
&& (p
= strchr(p
, ',')); i
++, p
++);
615 res
= xcalloc(i
+ 1, sizeof(char *));
616 p
= *flag
& BLKID_FLTR_NOTIN
? list
+ 2 : list
;
620 const char *word
= p
;
622 res
[i
++] = p
? xstrndup(word
, p
- word
) : xstrdup(word
);
631 exit(BLKID_EXIT_OTHER
);
634 static void free_types_list(char *list
[])
640 for (n
= list
; *n
; n
++)
645 int main(int argc
, char **argv
)
647 struct blkid_control ctl
= { .output
= OUTPUT_FULL
, 0 };
648 blkid_cache cache
= NULL
;
649 char **devices
= NULL
;
650 char *search_type
= NULL
, *search_value
= NULL
;
653 char **fltr_type
= NULL
;
654 int fltr_flag
= BLKID_FLTR_ONLYIN
;
655 unsigned int numdev
= 0, numtag
= 0;
656 int err
= BLKID_EXIT_OTHER
;
660 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
664 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
666 setlocale(LC_ALL
, "");
667 bindtextdomain(PACKAGE
, LOCALEDIR
);
669 atexit(close_stdout
);
671 while ((c
= getopt (argc
, argv
,
672 "c:df:ghilL:n:ko:O:ps:S:t:u:U:w:Vv")) != EOF
) {
674 err_exclusive_options(c
, NULL
, excl
, excl_st
);
685 search_value
= xstrdup(optarg
);
686 search_type
= xstrdup("LABEL");
689 fltr_type
= list_to_types(optarg
, &fltr_flag
);
692 fltr_usage
= list_to_usage(optarg
, &fltr_flag
);
696 search_value
= xstrdup(optarg
);
697 search_type
= xstrdup("UUID");
700 ctl
.lowprobe_topology
= 1;
711 const char *name
= NULL
;
713 while (blkid_superblocks_get_name(idx
++, &name
, NULL
) == 0)
714 printf("%s\n", name
);
718 if (!strcmp(optarg
, "value"))
719 ctl
.output
= OUTPUT_VALUE_ONLY
;
720 else if (!strcmp(optarg
, "device"))
721 ctl
.output
= OUTPUT_DEVICE_ONLY
;
722 else if (!strcmp(optarg
, "list"))
723 ctl
.output
= OUTPUT_PRETTY_LIST
; /* deprecated */
724 else if (!strcmp(optarg
, "udev"))
725 ctl
.output
= OUTPUT_UDEV_LIST
;
726 else if (!strcmp(optarg
, "export"))
727 ctl
.output
= OUTPUT_EXPORT_LIST
;
728 else if (!strcmp(optarg
, "full"))
731 errx(BLKID_EXIT_OTHER
, _("unsupported output format %s"), optarg
);
734 ctl
.offset
= strtosize_or_err(optarg
, _("invalid offset argument"));
737 ctl
.lowprobe_superblocks
= 1;
740 if (numtag
+ 1 >= sizeof(ctl
.show
) / sizeof(*ctl
.show
)) {
741 warnx(_("Too many tags specified"));
744 ctl
.show
[numtag
++] = optarg
;
747 ctl
.size
= strtosize_or_err(optarg
, _("invalid size argument"));
751 warnx(_("Can only search for "
752 "one NAME=value pair"));
755 if (blkid_parse_tag_string(optarg
,
758 warnx(_("-t needs NAME=value pair"));
764 print_version(stdout
);
768 /* ignore - backward compatibility */
774 errtryh(EXIT_FAILURE
);
778 if (ctl
.lowprobe_topology
|| ctl
.lowprobe_superblocks
)
781 /* The rest of the args are device names */
783 devices
= xcalloc(argc
- optind
, sizeof(char *));
784 while (optind
< argc
)
785 devices
[numdev
++] = argv
[optind
++];
788 /* convert LABEL/UUID lookup to evaluate request */
789 if (ctl
.lookup
&& ctl
.output
== OUTPUT_DEVICE_ONLY
&& search_type
&&
790 (!strcmp(search_type
, "LABEL") || !strcmp(search_type
, "UUID"))) {
795 if (!ctl
.lowprobe
&& !ctl
.eval
&& blkid_get_cache(&cache
, read
) < 0)
799 blkid_gc_cache(cache
);
803 err
= BLKID_EXIT_NOTFOUND
;
805 if (ctl
.eval
== 0 && (ctl
.output
& OUTPUT_PRETTY_LIST
)) {
807 errx(BLKID_EXIT_OTHER
,
808 _("The low-level probing mode does not "
809 "support 'list' output format"));
810 pretty_print_dev(NULL
);
820 errx(BLKID_EXIT_OTHER
,
821 _("The low-level probing mode "
822 "requires a device"));
824 /* automatically enable 'export' format for I/O Limits */
825 if (!ctl
.output
&& ctl
.lowprobe_topology
)
826 ctl
.output
= OUTPUT_EXPORT_LIST
;
828 pr
= blkid_new_probe();
832 if (ctl
.lowprobe_superblocks
) {
833 blkid_probe_set_superblocks_flags(pr
,
834 BLKID_SUBLKS_LABEL
| BLKID_SUBLKS_UUID
|
835 BLKID_SUBLKS_TYPE
| BLKID_SUBLKS_SECTYPE
|
836 BLKID_SUBLKS_USAGE
| BLKID_SUBLKS_VERSION
);
840 blkid_probe_filter_superblocks_usage(pr
, fltr_flag
, fltr_usage
))
843 else if (fltr_type
&&
844 blkid_probe_filter_superblocks_type(pr
, fltr_flag
, fltr_type
))
848 for (i
= 0; i
< numdev
; i
++) {
849 err
= lowprobe_device(pr
, devices
[i
], &ctl
);
853 blkid_free_probe(pr
);
854 } else if (ctl
.eval
) {
858 char *res
= blkid_evaluate_tag(search_type
, search_value
, NULL
);
863 } else if (ctl
.lookup
) {
865 * Classic (cache based) API
870 errx(BLKID_EXIT_OTHER
,
871 _("The lookup option requires a "
872 "search type specified using -t"));
873 /* Load any additional devices not in the cache */
874 for (i
= 0; i
< numdev
; i
++)
875 blkid_get_dev(cache
, devices
[i
], BLKID_DEV_NORMAL
);
877 if ((dev
= blkid_find_dev_with_tag(cache
, search_type
,
879 print_tags(&ctl
, dev
);
882 /* If we didn't specify a single device, show all available devices */
883 } else if (!numdev
) {
884 blkid_dev_iterate iter
;
887 blkid_probe_all(cache
);
889 iter
= blkid_dev_iterate_begin(cache
);
890 blkid_dev_set_search(iter
, search_type
, search_value
);
891 while (blkid_dev_next(iter
, &dev
) == 0) {
892 dev
= blkid_verify(cache
, dev
);
895 print_tags(&ctl
, dev
);
898 blkid_dev_iterate_end(iter
);
899 /* Add all specified devices to cache (optionally display tags) */
900 } else for (i
= 0; i
< numdev
; i
++) {
901 blkid_dev dev
= blkid_get_dev(cache
, devices
[i
],
906 !blkid_dev_has_tag(dev
, search_type
,
909 print_tags(&ctl
, dev
);
917 free_types_list(fltr_type
);
918 if (!ctl
.lowprobe
&& !ctl
.eval
)
919 blkid_put_cache(cache
);