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)
37 #define BLKID_EXIT_NOTFOUND 2 /* token or device not found */
38 #define BLKID_EXIT_OTHER 4 /* bad usage or other error */
39 #define BLKID_EXIT_AMBIVAL 8 /* ambivalent low-level probing detected */
43 #include "ismounted.h"
45 #define STRTOXX_EXIT_CODE BLKID_EXIT_OTHER /* strtoxx_or_err() */
47 #include "closestream.h"
50 const char *progname
= "blkid";
54 static void print_version(FILE *out
)
56 fprintf(out
, "%s from %s (libblkid %s, %s)\n",
57 progname
, PACKAGE_STRING
, LIBBLKID_VERSION
, LIBBLKID_DATE
);
60 static void usage(int error
)
62 FILE *out
= error
? stderr
: stdout
;
67 " %1$s -L <label> | -U <uuid>\n\n"
68 " %1$s [-c <file>] [-ghlLv] [-o <format>] [-s <tag>] \n"
69 " [-t <token>] [<dev> ...]\n\n"
70 " %1$s -p [-s <tag>] [-O <offset>] [-S <size>] \n"
71 " [-o <format>] <dev> ...\n\n"
72 " %1$s -i [-s <tag>] [-o <format>] <dev> ...\n\n"
74 " -c <file> read from <file> instead of reading from the default\n"
75 " cache file (-c /dev/null means no cache)\n"
76 " -d don't encode non-printing characters\n"
77 " -h print this usage message and exit\n"
78 " -g garbage collect the blkid cache\n"
79 " -o <format> output format; can be one of:\n"
80 " value, device, export or full; (default: full)\n"
81 " -k list all known filesystems/RAIDs and exit\n"
82 " -s <tag> show specified tag(s) (default show all tags)\n"
83 " -t <token> find device with a specific token (NAME=value pair)\n"
84 " -l look up only first device with token specified by -t\n"
85 " -L <label> convert LABEL to device name\n"
86 " -U <uuid> convert UUID to device name\n"
87 " -v print version and exit\n"
88 " <dev> specify device(s) to probe (default: all devices)\n\n"
89 "Low-level probing options:\n"
90 " -p low-level superblocks probing (bypass cache)\n"
91 " -i gather information about I/O limits\n"
92 " -S <size> overwrite device size\n"
93 " -O <offset> probe at the given offset\n"
94 " -u <list> filter by \"usage\" (e.g. -u filesystem,raid)\n"
95 " -n <list> filter by filesystem type (e.g. -n vfat,ext3)\n"
102 * This function does "safe" printing. It will convert non-printable
103 * ASCII characters using '^' and M- notation.
105 static void safe_print(const char *cp
, int len
)
119 if ((ch
< 32) || (ch
== 0x7f)) {
121 ch
^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
128 static int pretty_print_word(const char *str
, int max_len
,
129 int left_len
, int overflow_nl
)
131 int len
= strlen(str
) + left_len
;
135 if (overflow_nl
&& len
> max_len
) {
138 } else if (len
> max_len
)
142 while (len
++ < max_len
);
146 static void pretty_print_line(const char *device
, const char *fs_type
,
147 const char *label
, const char *mtpt
,
150 static int device_len
= 10, fs_type_len
= 7;
151 static int label_len
= 8, mtpt_len
= 14;
152 static int term_width
= -1;
155 if (term_width
< 0) {
156 term_width
= get_terminal_width();
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()-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
, "PTTYPE")) {
254 printf("ID_PART_TABLE_TYPE=%s\n", value
);
256 } else if (!strcmp(name
, "PART_ENTRY_NAME") ||
257 !strcmp(name
, "PART_ENTRY_TYPE")) {
259 blkid_encode_string(value
, enc
, sizeof(enc
));
260 printf("ID_%s=%s\n", name
, enc
);
262 } else if (!strncmp(name
, "PART_ENTRY_", 11))
263 printf("ID_%s=%s\n", name
, value
);
265 else if (namelen
>= 15 && (
266 !strcmp(name
+ (namelen
- 12), "_SECTOR_SIZE") ||
267 !strcmp(name
+ (namelen
- 8), "_IO_SIZE") ||
268 !strcmp(name
, "ALIGNMENT_OFFSET")))
269 printf("ID_IOLIMIT_%s=%s\n", name
, value
);
271 printf("ID_FS_%s=%s\n", name
, value
);
274 static int has_item(char *ary
[], const char *item
)
278 for (p
= ary
; *p
!= NULL
; p
++)
279 if (!strcmp(item
, *p
))
284 static void print_value(int output
, int num
, const char *devname
,
285 const char *value
, const char *name
, size_t valsz
)
287 if (output
& OUTPUT_VALUE_ONLY
) {
288 fputs(value
, stdout
);
291 } else if (output
& OUTPUT_UDEV_LIST
) {
292 print_udev_format(name
, value
);
294 } else if (output
& OUTPUT_EXPORT_LIST
) {
295 if (num
== 1 && devname
)
296 printf("DEVNAME=%s\n", devname
);
299 safe_print(value
, valsz
);
303 if (num
== 1 && devname
)
304 printf("%s: ", devname
);
306 fputs("=\"", stdout
);
307 safe_print(value
, valsz
);
308 fputs("\" ", stdout
);
312 static void print_tags(blkid_dev dev
, char *show
[], int output
)
314 blkid_tag_iterate iter
;
315 const char *type
, *value
, *devname
;
317 static int first
= 1;
322 if (output
& OUTPUT_PRETTY_LIST
) {
323 pretty_print_dev(dev
);
327 devname
= blkid_dev_devname(dev
);
329 if (output
& OUTPUT_DEVICE_ONLY
) {
330 printf("%s\n", devname
);
334 iter
= blkid_tag_iterate_begin(dev
);
335 while (blkid_tag_next(iter
, &type
, &value
) == 0) {
336 if (show
[0] && !has_item(show
, type
))
339 if (num
== 1 && !first
&&
340 (output
& (OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
)))
341 /* add extra line between output from more devices */
344 print_value(output
, num
++, devname
, value
, type
, strlen(value
));
346 blkid_tag_iterate_end(iter
);
349 if (!(output
& (OUTPUT_VALUE_ONLY
| OUTPUT_UDEV_LIST
|
350 OUTPUT_EXPORT_LIST
)))
357 static int append_str(char **res
, size_t *sz
, const char *a
, const char *b
)
360 size_t asz
= a
? strlen(a
) : 0;
361 size_t bsz
= b
? strlen(b
) : 0;
362 size_t len
= *sz
+ asz
+ bsz
;
367 str
= realloc(str
, len
+ 1);
390 * Compose and print ID_FS_AMBIVALENT for udev
392 static int print_udev_ambivalent(blkid_probe pr
)
396 int count
= 0, rc
= -1;
398 while (!blkid_do_probe(pr
)) {
399 const char *usage
= NULL
, *type
= NULL
, *version
= NULL
;
402 blkid_probe_lookup_value(pr
, "USAGE", &usage
, NULL
);
403 blkid_probe_lookup_value(pr
, "TYPE", &type
, NULL
);
404 blkid_probe_lookup_value(pr
, "VERSION", &version
, NULL
);
409 blkid_encode_string(usage
, enc
, sizeof(enc
));
410 if (append_str(&val
, &valsz
, enc
, ":"))
413 blkid_encode_string(type
, enc
, sizeof(enc
));
414 if (append_str(&val
, &valsz
, enc
, version
? ":" : " "))
418 blkid_encode_string(version
, enc
, sizeof(enc
));
419 if (append_str(&val
, &valsz
, enc
, " "))
426 *(val
+ valsz
- 1) = '\0'; /* rem tailing whitespace */
427 printf("ID_FS_AMBIVALEN=%s\n", val
);
435 static int lowprobe_superblocks(blkid_probe pr
)
438 int rc
, fd
= blkid_probe_get_fd(pr
);
440 if (fd
< 0 || fstat(fd
, &st
))
443 blkid_probe_enable_partitions(pr
, 1);
445 if (!S_ISCHR(st
.st_mode
) && blkid_probe_get_size(pr
) <= 1024 * 1440 &&
446 blkid_probe_is_wholedisk(pr
)) {
448 * check if the small disk is partitioned, if yes then
449 * don't probe for filesystems.
451 blkid_probe_enable_superblocks(pr
, 0);
453 rc
= blkid_do_fullprobe(pr
);
455 return rc
; /* -1 = error, 1 = nothing, 0 = succes */
457 if (blkid_probe_lookup_value(pr
, "PTTYPE", NULL
, NULL
) == 0)
458 return 0; /* partition table detected */
461 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
462 blkid_probe_enable_superblocks(pr
, 1);
464 return blkid_do_safeprobe(pr
);
467 static int lowprobe_topology(blkid_probe pr
)
469 /* enable topology probing only */
470 blkid_probe_enable_topology(pr
, 1);
472 blkid_probe_enable_superblocks(pr
, 0);
473 blkid_probe_enable_partitions(pr
, 0);
475 return blkid_do_fullprobe(pr
);
478 static int lowprobe_device(blkid_probe pr
, const char *devname
,
479 int chain
, char *show
[], int output
,
480 blkid_loff_t offset
, blkid_loff_t size
)
484 int nvals
= 0, n
, num
= 1;
488 static int first
= 1;
490 fd
= open(devname
, O_RDONLY
);
492 fprintf(stderr
, "error: %s: %m\n", devname
);
493 return BLKID_EXIT_NOTFOUND
;
495 if (blkid_probe_set_device(pr
, fd
, offset
, size
))
498 if (chain
& LOWPROBE_TOPOLOGY
)
499 rc
= lowprobe_topology(pr
);
500 if (rc
>= 0 && (chain
& LOWPROBE_SUPERBLOCKS
))
501 rc
= lowprobe_superblocks(pr
);
506 nvals
= blkid_probe_numof_values(pr
);
509 !(chain
& LOWPROBE_TOPOLOGY
) &&
510 !(output
& OUTPUT_UDEV_LIST
) &&
511 !blkid_probe_has_value(pr
, "TYPE") &&
512 !blkid_probe_has_value(pr
, "PTTYPE"))
514 * Ignore probing result if there is not any filesystem or
515 * partition table on the device and udev output is not
518 * The udev db stores information about partitions, so
519 * PART_ENTRY_* values are alway important.
523 if (nvals
&& !first
&& output
& (OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
))
524 /* add extra line between output from devices */
527 if (nvals
&& (output
& OUTPUT_DEVICE_ONLY
)) {
528 printf("%s\n", devname
);
532 for (n
= 0; n
< nvals
; n
++) {
533 if (blkid_probe_get_value(pr
, n
, &name
, &data
, &len
))
535 if (show
[0] && !has_item(show
, name
))
537 len
= strnlen((char *) data
, len
);
538 print_value(output
, num
++, devname
, (char *) data
, name
, len
);
543 if (nvals
>= 1 && !(output
& (OUTPUT_VALUE_ONLY
|
544 OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
)))
548 if (output
& OUTPUT_UDEV_LIST
)
549 print_udev_ambivalent(pr
);
552 "%s: ambivalent result (probably more "
553 "filesystems on the device, use wipefs(8) "
554 "to see more details)\n",
560 return BLKID_EXIT_AMBIVAL
; /* ambivalent probing result */
562 return BLKID_EXIT_NOTFOUND
; /* nothing detected */
564 return 0; /* success */
567 /* converts comma separated list to BLKID_USAGE_* mask */
568 static int list_to_usage(const char *list
, int *flag
)
571 const char *word
= NULL
, *p
= list
;
573 if (p
&& strncmp(p
, "no", 2) == 0) {
574 *flag
= BLKID_FLTR_NOTIN
;
584 if (!strncmp(word
, "filesystem", 10))
585 mask
|= BLKID_USAGE_FILESYSTEM
;
586 else if (!strncmp(word
, "raid", 4))
587 mask
|= BLKID_USAGE_RAID
;
588 else if (!strncmp(word
, "crypto", 6))
589 mask
|= BLKID_USAGE_CRYPTO
;
590 else if (!strncmp(word
, "other", 5))
591 mask
|= BLKID_USAGE_OTHER
;
598 fprintf(stderr
, "unknown kerword in -u <list> argument: '%s'\n",
600 exit(BLKID_EXIT_OTHER
);
603 /* converts comma separated list to types[] */
604 static char **list_to_types(const char *list
, int *flag
)
607 const char *p
= list
;
610 if (p
&& strncmp(p
, "no", 2) == 0) {
611 *flag
= BLKID_FLTR_NOTIN
;
615 fprintf(stderr
, "error: -u <list> argument is empty\n");
618 for (i
= 1; p
&& (p
= strchr(p
, ',')); i
++, p
++);
620 res
= calloc(i
+ 1, sizeof(char *));
623 p
= *flag
& BLKID_FLTR_NOTIN
? list
+ 2 : list
;
627 const char *word
= p
;
629 res
[i
] = p
? strndup(word
, p
- word
) : strdup(word
);
638 fprintf(stderr
, "out of memory\n");
642 exit(BLKID_EXIT_OTHER
);
645 static void free_types_list(char *list
[])
651 for (n
= list
; *n
; n
++)
656 int main(int argc
, char **argv
)
658 blkid_cache cache
= NULL
;
659 char **devices
= NULL
;
660 char *show
[128] = { NULL
, };
661 char *search_type
= NULL
, *search_value
= NULL
;
664 char **fltr_type
= NULL
;
665 int fltr_flag
= BLKID_FLTR_ONLYIN
;
666 unsigned int numdev
= 0, numtag
= 0;
668 int err
= BLKID_EXIT_OTHER
;
670 int output_format
= 0;
671 int lookup
= 0, gc
= 0, lowprobe
= 0, eval
= 0;
673 uintmax_t offset
= 0, size
= 0;
676 atexit(close_stdout
);
678 while ((c
= getopt (argc
, argv
, "c:df:ghilL:n:ko:O:ps:S:t:u:U:w:v")) != EOF
)
681 if (optarg
&& !*optarg
)
691 search_value
= strdup(optarg
);
692 search_type
= strdup("LABEL");
696 fprintf(stderr
, "error: -u and -n options are mutually exclusive\n");
697 exit(BLKID_EXIT_OTHER
);
699 fltr_type
= list_to_types(optarg
, &fltr_flag
);
703 fprintf(stderr
, "error: -u and -n options are mutually exclusive\n");
704 exit(BLKID_EXIT_OTHER
);
706 fltr_usage
= list_to_usage(optarg
, &fltr_flag
);
710 search_value
= strdup(optarg
);
711 search_type
= strdup("UUID");
714 lowprobe
|= LOWPROBE_TOPOLOGY
;
725 const char *name
= NULL
;
727 while (blkid_superblocks_get_name(idx
++, &name
, NULL
) == 0)
728 printf("%s\n", name
);
732 if (!strcmp(optarg
, "value"))
733 output_format
= OUTPUT_VALUE_ONLY
;
734 else if (!strcmp(optarg
, "device"))
735 output_format
= OUTPUT_DEVICE_ONLY
;
736 else if (!strcmp(optarg
, "list"))
737 output_format
= OUTPUT_PRETTY_LIST
; /* deprecated */
738 else if (!strcmp(optarg
, "udev"))
739 output_format
= OUTPUT_UDEV_LIST
;
740 else if (!strcmp(optarg
, "export"))
741 output_format
= OUTPUT_EXPORT_LIST
;
742 else if (!strcmp(optarg
, "full"))
745 fprintf(stderr
, "Invalid output format %s. "
746 "Choose from value,\n\t"
747 "device, list, udev or full\n", optarg
);
748 exit(BLKID_EXIT_OTHER
);
752 offset
= strtosize_or_err(optarg
, "invalid offset argument");
755 lowprobe
|= LOWPROBE_SUPERBLOCKS
;
758 if (numtag
+ 1 >= sizeof(show
) / sizeof(*show
)) {
759 fprintf(stderr
, "Too many tags specified\n");
762 show
[numtag
++] = optarg
;
766 size
= strtosize_or_err(optarg
, "invalid size argument");
770 fprintf(stderr
, "Can only search for "
771 "one NAME=value pair\n");
774 if (blkid_parse_tag_string(optarg
,
777 fprintf(stderr
, "-t needs NAME=value pair\n");
785 /* ignore - backward compatibility */
795 /* The rest of the args are device names */
797 devices
= calloc(argc
- optind
, sizeof(char *));
799 fprintf(stderr
, "Failed to allocate device name array\n");
803 while (optind
< argc
)
804 devices
[numdev
++] = argv
[optind
++];
808 print_version(stdout
);
812 /* convert LABEL/UUID lookup to evaluate request */
813 if (lookup
&& output_format
== OUTPUT_DEVICE_ONLY
&& search_type
&&
814 (!strcmp(search_type
, "LABEL") || !strcmp(search_type
, "UUID"))) {
819 if (!lowprobe
&& !eval
&& blkid_get_cache(&cache
, read
) < 0)
823 blkid_gc_cache(cache
);
827 err
= BLKID_EXIT_NOTFOUND
;
829 if (eval
== 0 && (output_format
& OUTPUT_PRETTY_LIST
)) {
831 fprintf(stderr
, "The low-level probing mode does not "
832 "support 'list' output format\n");
833 exit(BLKID_EXIT_OTHER
);
835 pretty_print_dev(NULL
);
845 fprintf(stderr
, "The low-level probing mode "
846 "requires a device\n");
847 exit(BLKID_EXIT_OTHER
);
850 /* automatically enable 'export' format for I/O Limits */
851 if (!output_format
&& (lowprobe
& LOWPROBE_TOPOLOGY
))
852 output_format
= OUTPUT_EXPORT_LIST
;
854 pr
= blkid_new_probe();
858 if (lowprobe
& LOWPROBE_SUPERBLOCKS
) {
859 blkid_probe_set_superblocks_flags(pr
,
860 BLKID_SUBLKS_LABEL
| BLKID_SUBLKS_UUID
|
861 BLKID_SUBLKS_TYPE
| BLKID_SUBLKS_SECTYPE
|
862 BLKID_SUBLKS_USAGE
| BLKID_SUBLKS_VERSION
);
864 if (fltr_usage
&& blkid_probe_filter_superblocks_usage(
865 pr
, fltr_flag
, fltr_usage
))
868 else if (fltr_type
&& blkid_probe_filter_superblocks_type(
869 pr
, fltr_flag
, fltr_type
))
873 for (i
= 0; i
< numdev
; i
++) {
874 err
= lowprobe_device(pr
, devices
[i
], lowprobe
, show
,
876 (blkid_loff_t
) offset
,
877 (blkid_loff_t
) size
);
881 blkid_free_probe(pr
);
886 char *res
= blkid_evaluate_tag(search_type
, search_value
, NULL
);
893 * Classic (cache based) API
898 fprintf(stderr
, "The lookup option requires a "
899 "search type specified using -t\n");
900 exit(BLKID_EXIT_OTHER
);
902 /* Load any additional devices not in the cache */
903 for (i
= 0; i
< numdev
; i
++)
904 blkid_get_dev(cache
, devices
[i
], BLKID_DEV_NORMAL
);
906 if ((dev
= blkid_find_dev_with_tag(cache
, search_type
,
908 print_tags(dev
, show
, output_format
);
911 /* If we didn't specify a single device, show all available devices */
912 } else if (!numdev
) {
913 blkid_dev_iterate iter
;
916 blkid_probe_all(cache
);
918 iter
= blkid_dev_iterate_begin(cache
);
919 blkid_dev_set_search(iter
, search_type
, search_value
);
920 while (blkid_dev_next(iter
, &dev
) == 0) {
921 dev
= blkid_verify(cache
, dev
);
924 print_tags(dev
, show
, output_format
);
927 blkid_dev_iterate_end(iter
);
928 /* Add all specified devices to cache (optionally display tags) */
929 } else for (i
= 0; i
< numdev
; i
++) {
930 blkid_dev dev
= blkid_get_dev(cache
, devices
[i
],
935 !blkid_dev_has_tag(dev
, search_type
,
938 print_tags(dev
, show
, output_format
);
946 free_types_list(fltr_type
);
947 if (!lowprobe
&& !eval
)
948 blkid_put_cache(cache
);