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>
21 #ifdef HAVE_SYS_IOCTL_H
22 #include <sys/ioctl.h>
27 extern int getopt(int argc
, char * const argv
[], const char *optstring
);
32 #define OUTPUT_VALUE_ONLY (1 << 1)
33 #define OUTPUT_DEVICE_ONLY (1 << 2)
34 #define OUTPUT_PRETTY_LIST (1 << 3) /* deprecated */
35 #define OUTPUT_UDEV_LIST (1 << 4) /* deprecated */
36 #define OUTPUT_EXPORT_LIST (1 << 5)
38 #define LOWPROBE_TOPOLOGY (1 << 1)
39 #define LOWPROBE_SUPERBLOCKS (1 << 2)
43 #include "ismounted.h"
45 #define STRTOXX_EXIT_CODE 4 /* strtoxx_or_err() */
47 #include "closestream.h"
49 const char *progname
= "blkid";
53 static void print_version(FILE *out
)
55 fprintf(out
, "%s from %s (libblkid %s, %s)\n",
56 progname
, PACKAGE_STRING
, LIBBLKID_VERSION
, LIBBLKID_DATE
);
59 static void usage(int error
)
61 FILE *out
= error
? stderr
: stdout
;
66 " %1$s -L <label> | -U <uuid>\n\n"
67 " %1$s [-c <file>] [-ghlLv] [-o <format>] [-s <tag>] \n"
68 " [-t <token>] [<dev> ...]\n\n"
69 " %1$s -p [-s <tag>] [-O <offset>] [-S <size>] \n"
70 " [-o <format>] <dev> ...\n\n"
71 " %1$s -i [-s <tag>] [-o <format>] <dev> ...\n\n"
73 " -c <file> read from <file> instead of reading from the default\n"
74 " cache file (-c /dev/null means no cache)\n"
75 " -d don't encode non-printing characters\n"
76 " -h print this usage message and exit\n"
77 " -g garbage collect the blkid cache\n"
78 " -o <format> output format; can be one of:\n"
79 " value, device, export or full; (default: full)\n"
80 " -k list all known filesystems/RAIDs and exit\n"
81 " -s <tag> show specified tag(s) (default show all tags)\n"
82 " -t <token> find device with a specific token (NAME=value pair)\n"
83 " -l look up only first device with token specified by -t\n"
84 " -L <label> convert LABEL to device name\n"
85 " -U <uuid> convert UUID to device name\n"
86 " -v print version and exit\n"
87 " <dev> specify device(s) to probe (default: all devices)\n\n"
88 "Low-level probing options:\n"
89 " -p low-level superblocks probing (bypass cache)\n"
90 " -i gather information about I/O limits\n"
91 " -S <size> overwrite device size\n"
92 " -O <offset> probe at the given offset\n"
93 " -u <list> filter by \"usage\" (e.g. -u filesystem,raid)\n"
94 " -n <list> filter by filesystem type (e.g. -n vfat,ext3)\n"
101 * This function does "safe" printing. It will convert non-printable
102 * ASCII characters using '^' and M- notation.
104 static void safe_print(const char *cp
, int len
)
118 if ((ch
< 32) || (ch
== 0x7f)) {
120 ch
^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
127 static int get_terminal_width(void)
130 struct ttysize t_win
;
133 struct winsize w_win
;
138 if (ioctl (0, TIOCGSIZE
, &t_win
) == 0)
139 return (t_win
.ts_cols
);
142 if (ioctl (0, TIOCGWINSZ
, &w_win
) == 0)
143 return (w_win
.ws_col
);
145 cp
= getenv("COLUMNS");
147 return strtol(cp
, NULL
, 10);
151 static int pretty_print_word(const char *str
, int max_len
,
152 int left_len
, int overflow_nl
)
154 int len
= strlen(str
) + left_len
;
158 if (overflow_nl
&& len
> max_len
) {
161 } else if (len
> max_len
)
165 while (len
++ < max_len
);
169 static void pretty_print_line(const char *device
, const char *fs_type
,
170 const char *label
, const char *mtpt
,
173 static int device_len
= 10, fs_type_len
= 7;
174 static int label_len
= 8, mtpt_len
= 14;
175 static int term_width
= -1;
179 term_width
= get_terminal_width();
181 if (term_width
> 80) {
194 len
= pretty_print_word(device
, device_len
, 0, 1);
195 len
= pretty_print_word(fs_type
, fs_type_len
, len
, 0);
196 len
= pretty_print_word(label
, label_len
, len
, 0);
197 pretty_print_word(mtpt
, mtpt_len
, len
, 0);
203 static void pretty_print_dev(blkid_dev dev
)
205 blkid_tag_iterate iter
;
206 const char *type
, *value
, *devname
;
207 const char *uuid
= "", *fs_type
= "", *label
= "";
208 int len
, mount_flags
;
213 pretty_print_line("device", "fs_type", "label",
214 "mount point", "UUID");
215 for (len
=get_terminal_width()-1; len
> 0; len
--)
221 devname
= blkid_dev_devname(dev
);
222 if (access(devname
, F_OK
))
225 /* Get the uuid, label, type */
226 iter
= blkid_tag_iterate_begin(dev
);
227 while (blkid_tag_next(iter
, &type
, &value
) == 0) {
228 if (!strcmp(type
, "UUID"))
230 if (!strcmp(type
, "TYPE"))
232 if (!strcmp(type
, "LABEL"))
235 blkid_tag_iterate_end(iter
);
237 /* Get the mount point */
239 retval
= check_mount_point(devname
, &mount_flags
, mtpt
, sizeof(mtpt
));
241 if (mount_flags
& MF_MOUNTED
) {
243 strcpy(mtpt
, "(mounted, mtpt unknown)");
244 } else if (mount_flags
& MF_BUSY
)
245 strcpy(mtpt
, "(in use)");
247 strcpy(mtpt
, "(not mounted)");
250 pretty_print_line(devname
, fs_type
, label
, mtpt
, uuid
);
253 static void print_udev_format(const char *name
, const char *value
)
255 char enc
[265], safe
[256];
256 size_t namelen
= strlen(name
);
260 if (!strcmp(name
, "TYPE") || !strcmp(name
, "VERSION")) {
261 blkid_encode_string(value
, enc
, sizeof(enc
));
262 printf("ID_FS_%s=%s\n", name
, enc
);
264 } else if (!strcmp(name
, "UUID") ||
265 !strcmp(name
, "LABEL") ||
266 !strcmp(name
, "UUID_SUB")) {
268 blkid_safe_string(value
, safe
, sizeof(safe
));
269 printf("ID_FS_%s=%s\n", name
, safe
);
271 blkid_encode_string(value
, enc
, sizeof(enc
));
272 printf("ID_FS_%s_ENC=%s\n", name
, enc
);
274 } else if (!strcmp(name
, "PTTYPE")) {
275 printf("ID_PART_TABLE_TYPE=%s\n", value
);
277 } else if (!strcmp(name
, "PART_ENTRY_NAME") ||
278 !strcmp(name
, "PART_ENTRY_TYPE")) {
280 blkid_encode_string(value
, enc
, sizeof(enc
));
281 printf("ID_%s=%s\n", name
, enc
);
283 } else if (!strncmp(name
, "PART_ENTRY_", 11))
284 printf("ID_%s=%s\n", name
, value
);
286 else if (namelen
>= 15 && (
287 !strcmp(name
+ (namelen
- 12), "_SECTOR_SIZE") ||
288 !strcmp(name
+ (namelen
- 8), "_IO_SIZE") ||
289 !strcmp(name
, "ALIGNMENT_OFFSET")))
290 printf("ID_IOLIMIT_%s=%s\n", name
, value
);
292 printf("ID_FS_%s=%s\n", name
, value
);
295 static int has_item(char *ary
[], const char *item
)
299 for (p
= ary
; *p
!= NULL
; p
++)
300 if (!strcmp(item
, *p
))
305 static void print_value(int output
, int num
, const char *devname
,
306 const char *value
, const char *name
, size_t valsz
)
308 if (output
& OUTPUT_VALUE_ONLY
) {
309 fputs(value
, stdout
);
312 } else if (output
& OUTPUT_UDEV_LIST
) {
313 print_udev_format(name
, value
);
315 } else if (output
& OUTPUT_EXPORT_LIST
) {
316 if (num
== 1 && devname
)
317 printf("DEVNAME=%s\n", devname
);
320 safe_print(value
, valsz
);
324 if (num
== 1 && devname
)
325 printf("%s: ", devname
);
327 fputs("=\"", stdout
);
328 safe_print(value
, valsz
);
329 fputs("\" ", stdout
);
333 static void print_tags(blkid_dev dev
, char *show
[], int output
)
335 blkid_tag_iterate iter
;
336 const char *type
, *value
, *devname
;
338 static int first
= 1;
343 if (output
& OUTPUT_PRETTY_LIST
) {
344 pretty_print_dev(dev
);
348 devname
= blkid_dev_devname(dev
);
350 if (output
& OUTPUT_DEVICE_ONLY
) {
351 printf("%s\n", devname
);
355 iter
= blkid_tag_iterate_begin(dev
);
356 while (blkid_tag_next(iter
, &type
, &value
) == 0) {
357 if (show
[0] && !has_item(show
, type
))
360 if (num
== 1 && !first
&&
361 (output
& (OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
)))
362 /* add extra line between output from more devices */
365 print_value(output
, num
++, devname
, value
, type
, strlen(value
));
367 blkid_tag_iterate_end(iter
);
370 if (!(output
& (OUTPUT_VALUE_ONLY
| OUTPUT_UDEV_LIST
|
371 OUTPUT_EXPORT_LIST
)))
378 static int append_str(char **res
, size_t *sz
, const char *a
, const char *b
)
381 size_t asz
= a
? strlen(a
) : 0;
382 size_t bsz
= b
? strlen(b
) : 0;
383 size_t len
= *sz
+ asz
+ bsz
;
388 str
= realloc(str
, len
+ 1);
409 * Compose and print ID_FS_AMBIVALENT for udev
411 static int print_udev_ambivalent(blkid_probe pr
)
415 int count
= 0, rc
= -1;
417 while (!blkid_do_probe(pr
)) {
418 const char *usage
= NULL
, *type
= NULL
, *version
= NULL
;
421 blkid_probe_lookup_value(pr
, "USAGE", &usage
, NULL
);
422 blkid_probe_lookup_value(pr
, "TYPE", &type
, NULL
);
423 blkid_probe_lookup_value(pr
, "VERSION", &version
, NULL
);
428 blkid_encode_string(usage
, enc
, sizeof(enc
));
429 if (append_str(&val
, &valsz
, enc
, ":"))
432 blkid_encode_string(type
, enc
, sizeof(enc
));
433 if (append_str(&val
, &valsz
, enc
, version
? ":" : " "))
437 blkid_encode_string(version
, enc
, sizeof(enc
));
438 if (append_str(&val
, &valsz
, enc
, " "))
445 *(val
+ valsz
- 1) = '\0'; /* rem tailing whitespace */
446 printf("ID_FS_AMBIVALEN=%s\n", val
);
454 static int lowprobe_superblocks(blkid_probe pr
)
457 int rc
, fd
= blkid_probe_get_fd(pr
);
459 if (fd
< 0 || fstat(fd
, &st
))
462 blkid_probe_enable_partitions(pr
, 1);
464 if (!S_ISCHR(st
.st_mode
) && blkid_probe_get_size(pr
) <= 1024 * 1440 &&
465 blkid_probe_is_wholedisk(pr
)) {
467 * check if the small disk is partitioned, if yes then
468 * don't probe for filesystems.
470 blkid_probe_enable_superblocks(pr
, 0);
472 rc
= blkid_do_fullprobe(pr
);
474 return rc
; /* -1 = error, 1 = nothing, 0 = succes */
476 if (blkid_probe_lookup_value(pr
, "PTTYPE", NULL
, NULL
) == 0)
477 return 0; /* partition table detected */
480 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
481 blkid_probe_enable_superblocks(pr
, 1);
483 return blkid_do_safeprobe(pr
);
486 static int lowprobe_topology(blkid_probe pr
)
488 /* enable topology probing only */
489 blkid_probe_enable_topology(pr
, 1);
491 blkid_probe_enable_superblocks(pr
, 0);
492 blkid_probe_enable_partitions(pr
, 0);
494 return blkid_do_fullprobe(pr
);
497 static int lowprobe_device(blkid_probe pr
, const char *devname
,
498 int chain
, char *show
[], int output
,
499 blkid_loff_t offset
, blkid_loff_t size
)
503 int nvals
= 0, n
, num
= 1;
507 static int first
= 1;
509 fd
= open(devname
, O_RDONLY
);
511 fprintf(stderr
, "error: %s: %m\n", devname
);
514 if (blkid_probe_set_device(pr
, fd
, offset
, size
))
517 if (chain
& LOWPROBE_TOPOLOGY
)
518 rc
= lowprobe_topology(pr
);
519 if (rc
>= 0 && (chain
& LOWPROBE_SUPERBLOCKS
))
520 rc
= lowprobe_superblocks(pr
);
525 nvals
= blkid_probe_numof_values(pr
);
528 !(chain
& LOWPROBE_TOPOLOGY
) &&
529 !(output
& OUTPUT_UDEV_LIST
) &&
530 !blkid_probe_has_value(pr
, "TYPE") &&
531 !blkid_probe_has_value(pr
, "PTTYPE"))
533 * Ignore probing result if there is not any filesystem or
534 * partition table on the device and udev output is not
537 * The udev db stores information about partitions, so
538 * PART_ENTRY_* values are alway important.
542 if (nvals
&& !first
&& output
& (OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
))
543 /* add extra line between output from devices */
546 if (nvals
&& (output
& OUTPUT_DEVICE_ONLY
)) {
547 printf("%s\n", devname
);
551 for (n
= 0; n
< nvals
; n
++) {
552 if (blkid_probe_get_value(pr
, n
, &name
, &data
, &len
))
554 if (show
[0] && !has_item(show
, name
))
556 len
= strnlen((char *) data
, len
);
557 print_value(output
, num
++, devname
, (char *) data
, name
, len
);
562 if (nvals
>= 1 && !(output
& (OUTPUT_VALUE_ONLY
|
563 OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
)))
567 if (output
& OUTPUT_UDEV_LIST
)
568 print_udev_ambivalent(pr
);
571 "%s: ambivalent result (probably more "
572 "filesystems on the device, use wipefs(8) "
573 "to see more details)\n",
579 return 8; /* ambivalent probing result */
581 return 2; /* nothing detected */
583 return 0; /* sucess */
586 /* converts comma separated list to BLKID_USAGE_* mask */
587 static int list_to_usage(const char *list
, int *flag
)
590 const char *word
= NULL
, *p
= list
;
592 if (p
&& strncmp(p
, "no", 2) == 0) {
593 *flag
= BLKID_FLTR_NOTIN
;
603 if (!strncmp(word
, "filesystem", 10))
604 mask
|= BLKID_USAGE_FILESYSTEM
;
605 else if (!strncmp(word
, "raid", 4))
606 mask
|= BLKID_USAGE_RAID
;
607 else if (!strncmp(word
, "crypto", 6))
608 mask
|= BLKID_USAGE_CRYPTO
;
609 else if (!strncmp(word
, "other", 5))
610 mask
|= BLKID_USAGE_OTHER
;
617 fprintf(stderr
, "unknown kerword in -u <list> argument: '%s'\n",
622 /* converts comma separated list to types[] */
623 static char **list_to_types(const char *list
, int *flag
)
626 const char *p
= list
;
629 if (p
&& strncmp(p
, "no", 2) == 0) {
630 *flag
= BLKID_FLTR_NOTIN
;
634 fprintf(stderr
, "error: -u <list> argument is empty\n");
637 for (i
= 1; p
&& (p
= strchr(p
, ',')); i
++, p
++);
639 res
= calloc(i
+ 1, sizeof(char *));
642 p
= *flag
& BLKID_FLTR_NOTIN
? list
+ 2 : list
;
646 const char *word
= p
;
648 res
[i
] = p
? strndup(word
, p
- word
) : strdup(word
);
657 fprintf(stderr
, "out of memory\n");
664 static void free_types_list(char *list
[])
670 for (n
= list
; *n
; n
++)
675 int main(int argc
, char **argv
)
677 blkid_cache cache
= NULL
;
678 char **devices
= NULL
;
679 char *show
[128] = { NULL
, };
680 char *search_type
= NULL
, *search_value
= NULL
;
683 char **fltr_type
= NULL
;
684 int fltr_flag
= BLKID_FLTR_ONLYIN
;
685 unsigned int numdev
= 0, numtag
= 0;
689 int output_format
= 0;
690 int lookup
= 0, gc
= 0, lowprobe
= 0, eval
= 0;
692 uintmax_t offset
= 0, size
= 0;
695 atexit(close_stdout
);
697 while ((c
= getopt (argc
, argv
, "c:df:ghilL:n:ko:O:ps:S:t:u:U:w:v")) != EOF
)
700 if (optarg
&& !*optarg
)
710 search_value
= strdup(optarg
);
711 search_type
= strdup("LABEL");
715 fprintf(stderr
, "error: -u and -n options are mutually exclusive\n");
718 fltr_type
= list_to_types(optarg
, &fltr_flag
);
722 fprintf(stderr
, "error: -u and -n options are mutually exclusive\n");
725 fltr_usage
= list_to_usage(optarg
, &fltr_flag
);
729 search_value
= strdup(optarg
);
730 search_type
= strdup("UUID");
733 lowprobe
|= LOWPROBE_TOPOLOGY
;
744 const char *name
= NULL
;
746 while (blkid_superblocks_get_name(idx
++, &name
, NULL
) == 0)
747 printf("%s\n", name
);
751 if (!strcmp(optarg
, "value"))
752 output_format
= OUTPUT_VALUE_ONLY
;
753 else if (!strcmp(optarg
, "device"))
754 output_format
= OUTPUT_DEVICE_ONLY
;
755 else if (!strcmp(optarg
, "list"))
756 output_format
= OUTPUT_PRETTY_LIST
; /* deprecated */
757 else if (!strcmp(optarg
, "udev"))
758 output_format
= OUTPUT_UDEV_LIST
;
759 else if (!strcmp(optarg
, "export"))
760 output_format
= OUTPUT_EXPORT_LIST
;
761 else if (!strcmp(optarg
, "full"))
764 fprintf(stderr
, "Invalid output format %s. "
765 "Choose from value,\n\t"
766 "device, list, udev or full\n", optarg
);
771 offset
= strtosize_or_err(optarg
, "failed to parse offset");
774 lowprobe
|= LOWPROBE_SUPERBLOCKS
;
777 if (numtag
+ 1 >= sizeof(show
) / sizeof(*show
)) {
778 fprintf(stderr
, "Too many tags specified\n");
781 show
[numtag
++] = optarg
;
785 size
= strtosize_or_err(optarg
, "failed to parse size");
789 fprintf(stderr
, "Can only search for "
790 "one NAME=value pair\n");
793 if (blkid_parse_tag_string(optarg
,
796 fprintf(stderr
, "-t needs NAME=value pair\n");
804 /* ignore - backward compatibility */
814 /* The rest of the args are device names */
816 devices
= calloc(argc
- optind
, sizeof(char *));
818 fprintf(stderr
, "Failed to allocate device name array\n");
822 while (optind
< argc
)
823 devices
[numdev
++] = argv
[optind
++];
827 print_version(stdout
);
831 /* convert LABEL/UUID lookup to evaluate request */
832 if (lookup
&& output_format
== OUTPUT_DEVICE_ONLY
&& search_type
&&
833 (!strcmp(search_type
, "LABEL") || !strcmp(search_type
, "UUID"))) {
838 if (!lowprobe
&& !eval
&& blkid_get_cache(&cache
, read
) < 0)
842 blkid_gc_cache(cache
);
848 if (eval
== 0 && (output_format
& OUTPUT_PRETTY_LIST
)) {
850 fprintf(stderr
, "The low-level probing mode does not "
851 "support 'list' output format\n");
854 pretty_print_dev(NULL
);
864 fprintf(stderr
, "The low-level probing mode "
865 "requires a device\n");
869 /* automatically enable 'export' format for I/O Limits */
870 if (!output_format
&& (lowprobe
& LOWPROBE_TOPOLOGY
))
871 output_format
= OUTPUT_EXPORT_LIST
;
873 pr
= blkid_new_probe();
877 if (lowprobe
& LOWPROBE_SUPERBLOCKS
) {
878 blkid_probe_set_superblocks_flags(pr
,
879 BLKID_SUBLKS_LABEL
| BLKID_SUBLKS_UUID
|
880 BLKID_SUBLKS_TYPE
| BLKID_SUBLKS_SECTYPE
|
881 BLKID_SUBLKS_USAGE
| BLKID_SUBLKS_VERSION
);
883 if (fltr_usage
&& blkid_probe_filter_superblocks_usage(
884 pr
, fltr_flag
, fltr_usage
))
887 else if (fltr_type
&& blkid_probe_filter_superblocks_type(
888 pr
, fltr_flag
, fltr_type
))
892 for (i
= 0; i
< numdev
; i
++)
893 err
= lowprobe_device(pr
, devices
[i
], lowprobe
, show
,
895 (blkid_loff_t
) offset
,
896 (blkid_loff_t
) size
);
897 blkid_free_probe(pr
);
902 char *res
= blkid_evaluate_tag(search_type
, search_value
, NULL
);
909 * Classic (cache based) API
914 fprintf(stderr
, "The lookup option requires a "
915 "search type specified using -t\n");
918 /* Load any additional devices not in the cache */
919 for (i
= 0; i
< numdev
; i
++)
920 blkid_get_dev(cache
, devices
[i
], BLKID_DEV_NORMAL
);
922 if ((dev
= blkid_find_dev_with_tag(cache
, search_type
,
924 print_tags(dev
, show
, output_format
);
927 /* If we didn't specify a single device, show all available devices */
928 } else if (!numdev
) {
929 blkid_dev_iterate iter
;
932 blkid_probe_all(cache
);
934 iter
= blkid_dev_iterate_begin(cache
);
935 blkid_dev_set_search(iter
, search_type
, search_value
);
936 while (blkid_dev_next(iter
, &dev
) == 0) {
937 dev
= blkid_verify(cache
, dev
);
940 print_tags(dev
, show
, output_format
);
943 blkid_dev_iterate_end(iter
);
944 /* Add all specified devices to cache (optionally display tags) */
945 } else for (i
= 0; i
< numdev
; i
++) {
946 blkid_dev dev
= blkid_get_dev(cache
, devices
[i
],
951 !blkid_dev_has_tag(dev
, search_type
,
954 print_tags(dev
, show
, output_format
);
962 free_types_list(fltr_type
);
963 if (!lowprobe
&& !eval
)
964 blkid_put_cache(cache
);