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_VALUE_ONLY (1 << 1)
23 #define OUTPUT_DEVICE_ONLY (1 << 2)
24 #define OUTPUT_PRETTY_LIST (1 << 3) /* deprecated */
25 #define OUTPUT_UDEV_LIST (1 << 4) /* deprecated */
26 #define OUTPUT_EXPORT_LIST (1 << 5)
28 #define LOWPROBE_TOPOLOGY (1 << 1)
29 #define LOWPROBE_SUPERBLOCKS (1 << 2)
31 #define BLKID_EXIT_NOTFOUND 2 /* token or device not found */
32 #define BLKID_EXIT_OTHER 4 /* bad usage or other error */
33 #define BLKID_EXIT_AMBIVAL 8 /* ambivalent low-level probing detected */
37 #include "ismounted.h"
39 #define STRTOXX_EXIT_CODE BLKID_EXIT_OTHER /* strtoxx_or_err() */
41 #define OPTUTILS_EXIT_CODE BLKID_EXIT_OTHER /* exclusive_option() */
43 #define CLOSE_EXIT_CODE BLKID_EXIT_OTHER /* close_stdout() */
44 #include "closestream.h"
52 static void print_version(FILE *out
)
54 fprintf(out
, _("%s from %s (libblkid %s, %s)\n"),
55 program_invocation_short_name
, PACKAGE_STRING
,
56 LIBBLKID_VERSION
, LIBBLKID_DATE
);
59 static void usage(int error
)
61 FILE *out
= error
? stderr
: stdout
;
63 fputs(USAGE_HEADER
, out
);
64 fprintf(out
, _( " %s -L <label> | -U <uuid>\n\n"), program_invocation_short_name
);
65 fprintf(out
, _( " %s [-c <file>] [-ghlLv] [-o <format>] [-s <tag>] \n"
66 " [-t <token>] [<dev> ...]\n\n"), program_invocation_short_name
);
67 fprintf(out
, _( " %s -p [-s <tag>] [-O <offset>] [-S <size>] \n"
68 " [-o <format>] <dev> ...\n\n"), program_invocation_short_name
);
69 fprintf(out
, _( " %s -i [-s <tag>] [-o <format>] <dev> ...\n"), program_invocation_short_name
);
70 fputs(USAGE_OPTIONS
, out
);
71 fputs(_( " -c <file> read from <file> instead of reading from the default\n"
72 " cache file (-c /dev/null means no cache)\n"), out
);
73 fputs(_( " -d don't encode non-printing characters\n"), out
);
74 fputs(_( " -h print this usage message and exit\n"), out
);
75 fputs(_( " -g garbage collect the blkid cache\n"), out
);
76 fputs(_( " -o <format> output format; can be one of:\n"
77 " value, device, export or full; (default: full)\n"), out
);
78 fputs(_( " -k list all known filesystems/RAIDs and exit\n"), out
);
79 fputs(_( " -s <tag> show specified tag(s) (default show all tags)\n"), out
);
80 fputs(_( " -t <token> find device with a specific token (NAME=value pair)\n"), out
);
81 fputs(_( " -l look up only first device with token specified by -t\n"), out
);
82 fputs(_( " -L <label> convert LABEL to device name\n"), out
);
83 fputs(_( " -U <uuid> convert UUID to device name\n"), out
);
84 fputs(_( " -V print version and exit\n"), out
);
85 fputs(_( " <dev> specify device(s) to probe (default: all devices)\n"), out
);
87 fputs(_( "Low-level probing options:\n"), out
);
88 fputs(_( " -p low-level superblocks probing (bypass cache)\n"), out
);
89 fputs(_( " -i gather information about I/O limits\n"), out
);
90 fputs(_( " -S <size> overwrite device size\n"), out
);
91 fputs(_( " -O <offset> probe at the given offset\n"), out
);
92 fputs(_( " -u <list> filter by \"usage\" (e.g. -u filesystem,raid)\n"), out
);
93 fputs(_( " -n <list> filter by filesystem type (e.g. -n vfat,ext3)\n"), out
);
95 fprintf(out
, USAGE_MAN_TAIL("blkid(8)"));
100 * This function does "safe" printing. It will convert non-printable
101 * ASCII characters using '^' and M- notation.
103 * If 'esc' is defined then escape all chars from esc by \.
105 static void safe_print(const char *cp
, int len
, const char *esc
)
119 if ((ch
< 32) || (ch
== 0x7f)) {
121 ch
^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
123 } else if (esc
&& strchr(esc
, ch
))
130 static int pretty_print_word(const char *str
, int max_len
,
131 int left_len
, int overflow_nl
)
133 int len
= strlen(str
) + left_len
;
137 if (overflow_nl
&& len
> max_len
) {
140 } else if (len
> max_len
)
144 } while (len
++ < max_len
);
148 static void pretty_print_line(const char *device
, const char *fs_type
,
149 const char *label
, const char *mtpt
,
152 static int device_len
= 10, fs_type_len
= 7;
153 static int label_len
= 8, mtpt_len
= 14;
154 static int term_width
= -1;
157 if (term_width
< 0) {
158 term_width
= get_terminal_width(80);
160 if (term_width
> 80) {
173 len
= pretty_print_word(device
, device_len
, 0, 1);
174 len
= pretty_print_word(fs_type
, fs_type_len
, len
, 0);
175 len
= pretty_print_word(label
, label_len
, len
, 0);
176 pretty_print_word(mtpt
, mtpt_len
, len
, 0);
182 static void pretty_print_dev(blkid_dev dev
)
184 blkid_tag_iterate iter
;
185 const char *type
, *value
, *devname
;
186 const char *uuid
= "", *fs_type
= "", *label
= "";
187 int len
, mount_flags
;
192 pretty_print_line("device", "fs_type", "label",
193 "mount point", "UUID");
194 for (len
=get_terminal_width(0)-1; len
> 0; len
--)
200 devname
= blkid_dev_devname(dev
);
201 if (access(devname
, F_OK
))
204 /* Get the uuid, label, type */
205 iter
= blkid_tag_iterate_begin(dev
);
206 while (blkid_tag_next(iter
, &type
, &value
) == 0) {
207 if (!strcmp(type
, "UUID"))
209 if (!strcmp(type
, "TYPE"))
211 if (!strcmp(type
, "LABEL"))
214 blkid_tag_iterate_end(iter
);
216 /* Get the mount point */
218 retval
= check_mount_point(devname
, &mount_flags
, mtpt
, sizeof(mtpt
));
220 if (mount_flags
& MF_MOUNTED
) {
222 strcpy(mtpt
, _("(mounted, mtpt unknown)"));
223 } else if (mount_flags
& MF_BUSY
)
224 strcpy(mtpt
, _("(in use)"));
226 strcpy(mtpt
, _("(not mounted)"));
229 pretty_print_line(devname
, fs_type
, label
, mtpt
, uuid
);
232 static void print_udev_format(const char *name
, const char *value
)
234 char enc
[265], safe
[256];
235 size_t namelen
= strlen(name
);
239 if (!strcmp(name
, "TYPE") || !strcmp(name
, "VERSION")) {
240 blkid_encode_string(value
, enc
, sizeof(enc
));
241 printf("ID_FS_%s=%s\n", name
, enc
);
243 } else if (!strcmp(name
, "UUID") ||
244 !strcmp(name
, "LABEL") ||
245 !strcmp(name
, "UUID_SUB")) {
247 blkid_safe_string(value
, safe
, sizeof(safe
));
248 printf("ID_FS_%s=%s\n", name
, safe
);
250 blkid_encode_string(value
, enc
, sizeof(enc
));
251 printf("ID_FS_%s_ENC=%s\n", name
, enc
);
253 } else if (!strcmp(name
, "PTUUID")) {
254 printf("ID_PART_TABLE_UUID=%s\n", value
);
256 } else if (!strcmp(name
, "PTTYPE")) {
257 printf("ID_PART_TABLE_TYPE=%s\n", value
);
259 } else if (!strcmp(name
, "PART_ENTRY_NAME") ||
260 !strcmp(name
, "PART_ENTRY_TYPE")) {
262 blkid_encode_string(value
, enc
, sizeof(enc
));
263 printf("ID_%s=%s\n", name
, enc
);
265 } else if (!strncmp(name
, "PART_ENTRY_", 11))
266 printf("ID_%s=%s\n", name
, value
);
268 else if (namelen
>= 15 && (
269 !strcmp(name
+ (namelen
- 12), "_SECTOR_SIZE") ||
270 !strcmp(name
+ (namelen
- 8), "_IO_SIZE") ||
271 !strcmp(name
, "ALIGNMENT_OFFSET")))
272 printf("ID_IOLIMIT_%s=%s\n", name
, value
);
274 printf("ID_FS_%s=%s\n", name
, value
);
277 static int has_item(char *ary
[], const char *item
)
281 for (p
= ary
; *p
!= NULL
; p
++)
282 if (!strcmp(item
, *p
))
287 static void print_value(int output
, int num
, const char *devname
,
288 const char *value
, const char *name
, size_t valsz
)
290 if (output
& OUTPUT_VALUE_ONLY
) {
291 fputs(value
, stdout
);
294 } else if (output
& OUTPUT_UDEV_LIST
) {
295 print_udev_format(name
, value
);
297 } else if (output
& OUTPUT_EXPORT_LIST
) {
298 if (num
== 1 && devname
)
299 printf("DEVNAME=%s\n", devname
);
302 safe_print(value
, valsz
, " \\\"'$`<>");
306 if (num
== 1 && devname
)
307 printf("%s:", devname
);
310 fputs("=\"", stdout
);
311 safe_print(value
, valsz
, "\"\\");
316 static void print_tags(blkid_dev dev
, char *show
[], int output
)
318 blkid_tag_iterate iter
;
319 const char *type
, *value
, *devname
;
321 static int first
= 1;
326 if (output
& OUTPUT_PRETTY_LIST
) {
327 pretty_print_dev(dev
);
331 devname
= blkid_dev_devname(dev
);
333 if (output
& OUTPUT_DEVICE_ONLY
) {
334 printf("%s\n", devname
);
338 iter
= blkid_tag_iterate_begin(dev
);
339 while (blkid_tag_next(iter
, &type
, &value
) == 0) {
340 if (show
[0] && !has_item(show
, type
))
343 if (num
== 1 && !first
&&
344 (output
& (OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
)))
345 /* add extra line between output from more devices */
348 print_value(output
, num
++, devname
, value
, type
, strlen(value
));
350 blkid_tag_iterate_end(iter
);
353 if (!(output
& (OUTPUT_VALUE_ONLY
| OUTPUT_UDEV_LIST
|
354 OUTPUT_EXPORT_LIST
)))
361 static int append_str(char **res
, size_t *sz
, const char *a
, const char *b
)
364 size_t asz
= a
? strlen(a
) : 0;
365 size_t bsz
= b
? strlen(b
) : 0;
366 size_t len
= *sz
+ asz
+ bsz
;
371 *res
= str
= xrealloc(str
, len
+ 1);
388 * Compose and print ID_FS_AMBIVALENT for udev
390 static int print_udev_ambivalent(blkid_probe pr
)
394 int count
= 0, rc
= -1;
396 while (!blkid_do_probe(pr
)) {
397 const char *usage_txt
= NULL
, *type
= NULL
, *version
= NULL
;
400 blkid_probe_lookup_value(pr
, "USAGE", &usage_txt
, NULL
);
401 blkid_probe_lookup_value(pr
, "TYPE", &type
, NULL
);
402 blkid_probe_lookup_value(pr
, "VERSION", &version
, NULL
);
404 if (!usage_txt
|| !type
)
407 blkid_encode_string(usage_txt
, enc
, sizeof(enc
));
408 if (append_str(&val
, &valsz
, enc
, ":"))
411 blkid_encode_string(type
, enc
, sizeof(enc
));
412 if (append_str(&val
, &valsz
, enc
, version
? ":" : " "))
416 blkid_encode_string(version
, enc
, sizeof(enc
));
417 if (append_str(&val
, &valsz
, enc
, " "))
424 *(val
+ valsz
- 1) = '\0'; /* rem tailing whitespace */
425 printf("ID_FS_AMBIVALENT=%s\n", val
);
433 static int lowprobe_superblocks(blkid_probe pr
)
436 int rc
, fd
= blkid_probe_get_fd(pr
);
438 if (fd
< 0 || fstat(fd
, &st
))
441 blkid_probe_enable_partitions(pr
, 1);
443 if (!S_ISCHR(st
.st_mode
) && blkid_probe_get_size(pr
) <= 1024 * 1440 &&
444 blkid_probe_is_wholedisk(pr
)) {
446 * check if the small disk is partitioned, if yes then
447 * don't probe for filesystems.
449 blkid_probe_enable_superblocks(pr
, 0);
451 rc
= blkid_do_fullprobe(pr
);
453 return rc
; /* -1 = error, 1 = nothing, 0 = success */
455 if (blkid_probe_lookup_value(pr
, "PTTYPE", NULL
, NULL
) == 0)
456 return 0; /* partition table detected */
459 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
460 blkid_probe_enable_superblocks(pr
, 1);
462 return blkid_do_safeprobe(pr
);
465 static int lowprobe_topology(blkid_probe pr
)
467 /* enable topology probing only */
468 blkid_probe_enable_topology(pr
, 1);
470 blkid_probe_enable_superblocks(pr
, 0);
471 blkid_probe_enable_partitions(pr
, 0);
473 return blkid_do_fullprobe(pr
);
476 static int lowprobe_device(blkid_probe pr
, const char *devname
,
477 int chain
, char *show
[], int output
,
478 uint64_t offset
, uint64_t size
)
482 int nvals
= 0, n
, num
= 1;
486 static int first
= 1;
488 fd
= open(devname
, O_RDONLY
|O_CLOEXEC
);
490 warn(_("error: %s"), devname
);
491 return BLKID_EXIT_NOTFOUND
;
493 if (blkid_probe_set_device(pr
, fd
, offset
, size
))
496 if (chain
& LOWPROBE_TOPOLOGY
)
497 rc
= lowprobe_topology(pr
);
498 if (rc
>= 0 && (chain
& LOWPROBE_SUPERBLOCKS
))
499 rc
= lowprobe_superblocks(pr
);
504 nvals
= blkid_probe_numof_values(pr
);
506 if (nvals
&& !first
&& output
& (OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
))
507 /* add extra line between output from devices */
510 if (nvals
&& (output
& OUTPUT_DEVICE_ONLY
)) {
511 printf("%s\n", devname
);
515 for (n
= 0; n
< nvals
; n
++) {
516 if (blkid_probe_get_value(pr
, n
, &name
, &data
, &len
))
518 if (show
[0] && !has_item(show
, name
))
520 len
= strnlen((char *) data
, len
);
521 print_value(output
, num
++, devname
, (char *) data
, name
, len
);
526 if (nvals
>= 1 && !(output
& (OUTPUT_VALUE_ONLY
|
527 OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
)))
531 if (output
& OUTPUT_UDEV_LIST
)
532 print_udev_ambivalent(pr
);
534 warnx(_("%s: ambivalent result (probably more "
535 "filesystems on the device, use wipefs(8) "
536 "to see more details)"),
542 return BLKID_EXIT_AMBIVAL
; /* ambivalent probing result */
544 return BLKID_EXIT_NOTFOUND
; /* nothing detected */
546 return 0; /* success */
549 /* converts comma separated list to BLKID_USAGE_* mask */
550 static int list_to_usage(const char *list
, int *flag
)
553 const char *word
= NULL
, *p
= list
;
555 if (p
&& strncmp(p
, "no", 2) == 0) {
556 *flag
= BLKID_FLTR_NOTIN
;
566 if (!strncmp(word
, "filesystem", 10))
567 mask
|= BLKID_USAGE_FILESYSTEM
;
568 else if (!strncmp(word
, "raid", 4))
569 mask
|= BLKID_USAGE_RAID
;
570 else if (!strncmp(word
, "crypto", 6))
571 mask
|= BLKID_USAGE_CRYPTO
;
572 else if (!strncmp(word
, "other", 5))
573 mask
|= BLKID_USAGE_OTHER
;
580 warnx(_("unknown keyword in -u <list> argument: '%s'"),
582 exit(BLKID_EXIT_OTHER
);
585 /* converts comma separated list to types[] */
586 static char **list_to_types(const char *list
, int *flag
)
589 const char *p
= list
;
592 if (p
&& strncmp(p
, "no", 2) == 0) {
593 *flag
= BLKID_FLTR_NOTIN
;
597 warnx(_("error: -u <list> argument is empty"));
600 for (i
= 1; p
&& (p
= strchr(p
, ',')); i
++, p
++);
602 res
= xcalloc(i
+ 1, sizeof(char *));
603 p
= *flag
& BLKID_FLTR_NOTIN
? list
+ 2 : list
;
607 const char *word
= p
;
609 res
[i
++] = p
? xstrndup(word
, p
- word
) : xstrdup(word
);
618 exit(BLKID_EXIT_OTHER
);
621 static void free_types_list(char *list
[])
627 for (n
= list
; *n
; n
++)
632 int main(int argc
, char **argv
)
634 blkid_cache cache
= NULL
;
635 char **devices
= NULL
;
636 char *show
[128] = { NULL
, };
637 char *search_type
= NULL
, *search_value
= NULL
;
640 char **fltr_type
= NULL
;
641 int fltr_flag
= BLKID_FLTR_ONLYIN
;
642 unsigned int numdev
= 0, numtag
= 0;
644 int err
= BLKID_EXIT_OTHER
;
646 int output_format
= 0;
647 int lookup
= 0, gc
= 0, lowprobe
= 0, eval
= 0;
649 uintmax_t offset
= 0, size
= 0;
651 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
655 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
658 setlocale(LC_ALL
, "");
659 bindtextdomain(PACKAGE
, LOCALEDIR
);
661 atexit(close_stdout
);
663 while ((c
= getopt (argc
, argv
,
664 "c:df:ghilL:n:ko:O:ps:S:t:u:U:w:Vv")) != EOF
) {
666 err_exclusive_options(c
, NULL
, excl
, excl_st
);
670 if (optarg
&& !*optarg
)
680 search_value
= xstrdup(optarg
);
681 search_type
= xstrdup("LABEL");
684 fltr_type
= list_to_types(optarg
, &fltr_flag
);
687 fltr_usage
= list_to_usage(optarg
, &fltr_flag
);
691 search_value
= xstrdup(optarg
);
692 search_type
= xstrdup("UUID");
695 lowprobe
|= LOWPROBE_TOPOLOGY
;
706 const char *name
= NULL
;
708 while (blkid_superblocks_get_name(idx
++, &name
, NULL
) == 0)
709 printf("%s\n", name
);
713 if (!strcmp(optarg
, "value"))
714 output_format
= OUTPUT_VALUE_ONLY
;
715 else if (!strcmp(optarg
, "device"))
716 output_format
= OUTPUT_DEVICE_ONLY
;
717 else if (!strcmp(optarg
, "list"))
718 output_format
= OUTPUT_PRETTY_LIST
; /* deprecated */
719 else if (!strcmp(optarg
, "udev"))
720 output_format
= OUTPUT_UDEV_LIST
;
721 else if (!strcmp(optarg
, "export"))
722 output_format
= OUTPUT_EXPORT_LIST
;
723 else if (!strcmp(optarg
, "full"))
726 errx(BLKID_EXIT_OTHER
, _("unsupported output format %s"), optarg
);
729 offset
= strtosize_or_err(optarg
, _("invalid offset argument"));
732 lowprobe
|= LOWPROBE_SUPERBLOCKS
;
735 if (numtag
+ 1 >= sizeof(show
) / sizeof(*show
)) {
736 warnx(_("Too many tags specified"));
739 show
[numtag
++] = optarg
;
743 size
= strtosize_or_err(optarg
, _("invalid size argument"));
747 warnx(_("Can only search for "
748 "one NAME=value pair"));
751 if (blkid_parse_tag_string(optarg
,
754 warnx(_("-t needs NAME=value pair"));
763 /* ignore - backward compatibility */
769 errtryh(EXIT_FAILURE
);
774 /* The rest of the args are device names */
776 devices
= xcalloc(argc
- optind
, sizeof(char *));
777 while (optind
< argc
)
778 devices
[numdev
++] = argv
[optind
++];
782 print_version(stdout
);
786 /* convert LABEL/UUID lookup to evaluate request */
787 if (lookup
&& output_format
== OUTPUT_DEVICE_ONLY
&& search_type
&&
788 (!strcmp(search_type
, "LABEL") || !strcmp(search_type
, "UUID"))) {
793 if (!lowprobe
&& !eval
&& blkid_get_cache(&cache
, read
) < 0)
797 blkid_gc_cache(cache
);
801 err
= BLKID_EXIT_NOTFOUND
;
803 if (eval
== 0 && (output_format
& OUTPUT_PRETTY_LIST
)) {
805 errx(BLKID_EXIT_OTHER
,
806 _("The low-level probing mode does not "
807 "support 'list' output format"));
808 pretty_print_dev(NULL
);
818 errx(BLKID_EXIT_OTHER
,
819 _("The low-level probing mode "
820 "requires a device"));
822 /* automatically enable 'export' format for I/O Limits */
823 if (!output_format
&& (lowprobe
& LOWPROBE_TOPOLOGY
))
824 output_format
= OUTPUT_EXPORT_LIST
;
826 pr
= blkid_new_probe();
830 if (lowprobe
& LOWPROBE_SUPERBLOCKS
) {
831 blkid_probe_set_superblocks_flags(pr
,
832 BLKID_SUBLKS_LABEL
| BLKID_SUBLKS_UUID
|
833 BLKID_SUBLKS_TYPE
| BLKID_SUBLKS_SECTYPE
|
834 BLKID_SUBLKS_USAGE
| BLKID_SUBLKS_VERSION
);
836 if (fltr_usage
&& blkid_probe_filter_superblocks_usage(
837 pr
, fltr_flag
, fltr_usage
))
840 else if (fltr_type
&& blkid_probe_filter_superblocks_type(
841 pr
, fltr_flag
, fltr_type
))
845 for (i
= 0; i
< numdev
; i
++) {
846 err
= lowprobe_device(pr
, devices
[i
], lowprobe
, show
,
853 blkid_free_probe(pr
);
858 char *res
= blkid_evaluate_tag(search_type
, search_value
, NULL
);
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(dev
, show
, output_format
);
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(dev
, show
, output_format
);
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(dev
, show
, output_format
);
917 free_types_list(fltr_type
);
918 if (!lowprobe
&& !eval
)
919 blkid_put_cache(cache
);