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"
51 static void print_version(FILE *out
)
53 fprintf(out
, "%s from %s (libblkid %s, %s)\n",
54 program_invocation_short_name
, PACKAGE_STRING
,
55 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"
94 "\n", program_invocation_short_name
);
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 fprintf(stderr
, "error: %s: %m\n", 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
);
535 "%s: ambivalent result (probably more "
536 "filesystems on the device, use wipefs(8) "
537 "to see more details)\n",
543 return BLKID_EXIT_AMBIVAL
; /* ambivalent probing result */
545 return BLKID_EXIT_NOTFOUND
; /* nothing detected */
547 return 0; /* success */
550 /* converts comma separated list to BLKID_USAGE_* mask */
551 static int list_to_usage(const char *list
, int *flag
)
554 const char *word
= NULL
, *p
= list
;
556 if (p
&& strncmp(p
, "no", 2) == 0) {
557 *flag
= BLKID_FLTR_NOTIN
;
567 if (!strncmp(word
, "filesystem", 10))
568 mask
|= BLKID_USAGE_FILESYSTEM
;
569 else if (!strncmp(word
, "raid", 4))
570 mask
|= BLKID_USAGE_RAID
;
571 else if (!strncmp(word
, "crypto", 6))
572 mask
|= BLKID_USAGE_CRYPTO
;
573 else if (!strncmp(word
, "other", 5))
574 mask
|= BLKID_USAGE_OTHER
;
581 fprintf(stderr
, "unknown keyword in -u <list> argument: '%s'\n",
583 exit(BLKID_EXIT_OTHER
);
586 /* converts comma separated list to types[] */
587 static char **list_to_types(const char *list
, int *flag
)
590 const char *p
= list
;
593 if (p
&& strncmp(p
, "no", 2) == 0) {
594 *flag
= BLKID_FLTR_NOTIN
;
598 fprintf(stderr
, "error: -u <list> argument is empty\n");
601 for (i
= 1; p
&& (p
= strchr(p
, ',')); i
++, p
++);
603 res
= xcalloc(i
+ 1, sizeof(char *));
604 p
= *flag
& BLKID_FLTR_NOTIN
? list
+ 2 : list
;
608 const char *word
= p
;
610 res
[i
++] = p
? xstrndup(word
, p
- word
) : xstrdup(word
);
619 exit(BLKID_EXIT_OTHER
);
622 static void free_types_list(char *list
[])
628 for (n
= list
; *n
; n
++)
633 int main(int argc
, char **argv
)
635 blkid_cache cache
= NULL
;
636 char **devices
= NULL
;
637 char *show
[128] = { NULL
, };
638 char *search_type
= NULL
, *search_value
= NULL
;
641 char **fltr_type
= NULL
;
642 int fltr_flag
= BLKID_FLTR_ONLYIN
;
643 unsigned int numdev
= 0, numtag
= 0;
645 int err
= BLKID_EXIT_OTHER
;
647 int output_format
= 0;
648 int lookup
= 0, gc
= 0, lowprobe
= 0, eval
= 0;
650 uintmax_t offset
= 0, size
= 0;
652 static const ul_excl_t excl
[] = { /* rows and cols in in ASCII order */
656 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
659 atexit(close_stdout
);
661 while ((c
= getopt (argc
, argv
,
662 "c:df:ghilL:n:ko:O:ps:S:t:u:U:w:Vv")) != EOF
) {
664 err_exclusive_options(c
, NULL
, excl
, excl_st
);
668 if (optarg
&& !*optarg
)
678 search_value
= xstrdup(optarg
);
679 search_type
= xstrdup("LABEL");
682 fltr_type
= list_to_types(optarg
, &fltr_flag
);
685 fltr_usage
= list_to_usage(optarg
, &fltr_flag
);
689 search_value
= xstrdup(optarg
);
690 search_type
= xstrdup("UUID");
693 lowprobe
|= LOWPROBE_TOPOLOGY
;
704 const char *name
= NULL
;
706 while (blkid_superblocks_get_name(idx
++, &name
, NULL
) == 0)
707 printf("%s\n", name
);
711 if (!strcmp(optarg
, "value"))
712 output_format
= OUTPUT_VALUE_ONLY
;
713 else if (!strcmp(optarg
, "device"))
714 output_format
= OUTPUT_DEVICE_ONLY
;
715 else if (!strcmp(optarg
, "list"))
716 output_format
= OUTPUT_PRETTY_LIST
; /* deprecated */
717 else if (!strcmp(optarg
, "udev"))
718 output_format
= OUTPUT_UDEV_LIST
;
719 else if (!strcmp(optarg
, "export"))
720 output_format
= OUTPUT_EXPORT_LIST
;
721 else if (!strcmp(optarg
, "full"))
724 fprintf(stderr
, "Invalid output format %s. "
725 "Choose from value,\n\t"
726 "device, list, udev or full\n", optarg
);
727 exit(BLKID_EXIT_OTHER
);
731 offset
= strtosize_or_err(optarg
, "invalid offset argument");
734 lowprobe
|= LOWPROBE_SUPERBLOCKS
;
737 if (numtag
+ 1 >= sizeof(show
) / sizeof(*show
)) {
738 fprintf(stderr
, "Too many tags specified\n");
741 show
[numtag
++] = optarg
;
745 size
= strtosize_or_err(optarg
, "invalid size argument");
749 fprintf(stderr
, "Can only search for "
750 "one NAME=value pair\n");
753 if (blkid_parse_tag_string(optarg
,
756 fprintf(stderr
, "-t needs NAME=value pair\n");
765 /* ignore - backward compatibility */
776 /* The rest of the args are device names */
778 devices
= xcalloc(argc
- optind
, sizeof(char *));
779 while (optind
< argc
)
780 devices
[numdev
++] = argv
[optind
++];
784 print_version(stdout
);
788 /* convert LABEL/UUID lookup to evaluate request */
789 if (lookup
&& output_format
== OUTPUT_DEVICE_ONLY
&& search_type
&&
790 (!strcmp(search_type
, "LABEL") || !strcmp(search_type
, "UUID"))) {
795 if (!lowprobe
&& !eval
&& blkid_get_cache(&cache
, read
) < 0)
799 blkid_gc_cache(cache
);
803 err
= BLKID_EXIT_NOTFOUND
;
805 if (eval
== 0 && (output_format
& OUTPUT_PRETTY_LIST
)) {
807 fprintf(stderr
, "The low-level probing mode does not "
808 "support 'list' output format\n");
809 exit(BLKID_EXIT_OTHER
);
811 pretty_print_dev(NULL
);
821 fprintf(stderr
, "The low-level probing mode "
822 "requires a device\n");
823 exit(BLKID_EXIT_OTHER
);
826 /* automatically enable 'export' format for I/O Limits */
827 if (!output_format
&& (lowprobe
& LOWPROBE_TOPOLOGY
))
828 output_format
= OUTPUT_EXPORT_LIST
;
830 pr
= blkid_new_probe();
834 if (lowprobe
& LOWPROBE_SUPERBLOCKS
) {
835 blkid_probe_set_superblocks_flags(pr
,
836 BLKID_SUBLKS_LABEL
| BLKID_SUBLKS_UUID
|
837 BLKID_SUBLKS_TYPE
| BLKID_SUBLKS_SECTYPE
|
838 BLKID_SUBLKS_USAGE
| BLKID_SUBLKS_VERSION
);
840 if (fltr_usage
&& blkid_probe_filter_superblocks_usage(
841 pr
, fltr_flag
, fltr_usage
))
844 else if (fltr_type
&& blkid_probe_filter_superblocks_type(
845 pr
, fltr_flag
, fltr_type
))
849 for (i
= 0; i
< numdev
; i
++) {
850 err
= lowprobe_device(pr
, devices
[i
], lowprobe
, show
,
857 blkid_free_probe(pr
);
862 char *res
= blkid_evaluate_tag(search_type
, search_value
, NULL
);
869 * Classic (cache based) API
874 fprintf(stderr
, "The lookup option requires a "
875 "search type specified using -t\n");
876 exit(BLKID_EXIT_OTHER
);
878 /* Load any additional devices not in the cache */
879 for (i
= 0; i
< numdev
; i
++)
880 blkid_get_dev(cache
, devices
[i
], BLKID_DEV_NORMAL
);
882 if ((dev
= blkid_find_dev_with_tag(cache
, search_type
,
884 print_tags(dev
, show
, output_format
);
887 /* If we didn't specify a single device, show all available devices */
888 } else if (!numdev
) {
889 blkid_dev_iterate iter
;
892 blkid_probe_all(cache
);
894 iter
= blkid_dev_iterate_begin(cache
);
895 blkid_dev_set_search(iter
, search_type
, search_value
);
896 while (blkid_dev_next(iter
, &dev
) == 0) {
897 dev
= blkid_verify(cache
, dev
);
900 print_tags(dev
, show
, output_format
);
903 blkid_dev_iterate_end(iter
);
904 /* Add all specified devices to cache (optionally display tags) */
905 } else for (i
= 0; i
< numdev
; i
++) {
906 blkid_dev dev
= blkid_get_dev(cache
, devices
[i
],
911 !blkid_dev_has_tag(dev
, search_type
,
914 print_tags(dev
, show
, output_format
);
922 free_types_list(fltr_type
);
923 if (!lowprobe
&& !eval
)
924 blkid_put_cache(cache
);