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)
35 #define OUTPUT_UDEV_LIST (1 << 4)
36 #define OUTPUT_EXPORT_LIST (1 << 5)
38 #define LOWPROBE_TOPOLOGY (1 << 1)
39 #define LOWPROBE_SUPERBLOCKS (1 << 2)
43 #include "ismounted.h"
46 const char *progname
= "blkid";
50 static void print_version(FILE *out
)
52 fprintf(out
, "%s from %s (libblkid %s, %s)\n",
53 progname
, PACKAGE_STRING
, LIBBLKID_VERSION
, LIBBLKID_DATE
);
56 static void usage(int error
)
58 FILE *out
= error
? stderr
: stdout
;
63 " %1$s -L <label> | -U <uuid>\n\n"
64 " %1$s [-c <file>] [-ghlLv] [-o <format>] [-s <tag>] \n"
65 " [-t <token>] [-w <file>] [<dev> ...]\n\n"
66 " %1$s -p [-s <tag>] [-O <offset>] [-S <size>] \n"
67 " [-o <format>] <dev> ...\n\n"
68 " %1$s -i [-s <tag>] [-o <format>] <dev> ...\n\n"
70 " -c <file> cache file (default: /etc/blkid.tab, /dev/null = none)\n"
71 " -d don't encode non-printing characters\n"
72 " -h print this usage message and exit\n"
73 " -g garbage collect the blkid cache\n"
74 " -o <format> output format; can be one of:\n"
75 " value, device, list, udev, export or full; (default: full)\n"
76 " -k list all known filesystems/RAIDs and exit\n"
77 " -s <tag> show specified tag(s) (default show all tags)\n"
78 " -t <token> find device with a specific token (NAME=value pair)\n"
79 " -l look up only first device with token specified by -t\n"
80 " -L <label> convert LABEL to device name\n"
81 " -U <uuid> convert UUID to device name\n"
82 " -v print version and exit\n"
83 " -w <file> write cache to different file (/dev/null = no write)\n"
84 " <dev> specify device(s) to probe (default: all devices)\n\n"
85 "Low-level probing options:\n"
86 " -p low-level superblocks probing (bypass cache)\n"
87 " -i gather information about I/O limits\n"
88 " -S <size> overwrite device size\n"
89 " -O <offset> probe at the given offset\n"
90 " -u <list> filter by \"usage\" (e.g. -u filesystem,raid)\n"
91 " -n <list> filter by filesystem type (e.g. -n vfat,ext3)\n"
98 * This function does "safe" printing. It will convert non-printable
99 * ASCII characters using '^' and M- notation.
101 static void safe_print(const char *cp
, int len
)
115 if ((ch
< 32) || (ch
== 0x7f)) {
117 ch
^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
124 static int get_terminal_width(void)
127 struct ttysize t_win
;
130 struct winsize w_win
;
135 if (ioctl (0, TIOCGSIZE
, &t_win
) == 0)
136 return (t_win
.ts_cols
);
139 if (ioctl (0, TIOCGWINSZ
, &w_win
) == 0)
140 return (w_win
.ws_col
);
142 cp
= getenv("COLUMNS");
144 return strtol(cp
, NULL
, 10);
148 static int pretty_print_word(const char *str
, int max_len
,
149 int left_len
, int overflow_nl
)
151 int len
= strlen(str
) + left_len
;
155 if (overflow_nl
&& len
> max_len
) {
158 } else if (len
> max_len
)
162 while (len
++ < max_len
);
166 static void pretty_print_line(const char *device
, const char *fs_type
,
167 const char *label
, const char *mtpt
,
170 static int device_len
= 10, fs_type_len
= 7;
171 static int label_len
= 8, mtpt_len
= 14;
172 static int term_width
= -1;
176 term_width
= get_terminal_width();
178 if (term_width
> 80) {
191 len
= pretty_print_word(device
, device_len
, 0, 1);
192 len
= pretty_print_word(fs_type
, fs_type_len
, len
, 0);
193 len
= pretty_print_word(label
, label_len
, len
, 0);
194 len
= pretty_print_word(mtpt
, mtpt_len
, len
, 0);
199 static void pretty_print_dev(blkid_dev dev
)
201 blkid_tag_iterate iter
;
202 const char *type
, *value
, *devname
;
203 const char *uuid
= "", *fs_type
= "", *label
= "";
204 int len
, mount_flags
;
209 pretty_print_line("device", "fs_type", "label",
210 "mount point", "UUID");
211 for (len
=get_terminal_width()-1; len
> 0; len
--)
217 devname
= blkid_dev_devname(dev
);
218 if (access(devname
, F_OK
))
221 /* Get the uuid, label, type */
222 iter
= blkid_tag_iterate_begin(dev
);
223 while (blkid_tag_next(iter
, &type
, &value
) == 0) {
224 if (!strcmp(type
, "UUID"))
226 if (!strcmp(type
, "TYPE"))
228 if (!strcmp(type
, "LABEL"))
231 blkid_tag_iterate_end(iter
);
233 /* Get the mount point */
235 retval
= check_mount_point(devname
, &mount_flags
, mtpt
, sizeof(mtpt
));
237 if (mount_flags
& MF_MOUNTED
) {
239 strcpy(mtpt
, "(mounted, mtpt unknown)");
240 } else if (mount_flags
& MF_BUSY
)
241 strcpy(mtpt
, "(in use)");
243 strcpy(mtpt
, "(not mounted)");
246 pretty_print_line(devname
, fs_type
, label
, mtpt
, uuid
);
249 static void print_udev_format(const char *name
, const char *value
)
251 char enc
[265], safe
[256];
252 size_t namelen
= strlen(name
);
256 if (!strcmp(name
, "TYPE") || !strcmp(name
, "VERSION")) {
257 blkid_encode_string(value
, enc
, sizeof(enc
));
258 printf("ID_FS_%s=%s\n", name
, enc
);
260 } else if (!strcmp(name
, "UUID") ||
261 !strcmp(name
, "LABEL") ||
262 !strcmp(name
, "UUID_SUB")) {
264 blkid_safe_string(value
, safe
, sizeof(safe
));
265 printf("ID_FS_%s=%s\n", name
, safe
);
267 blkid_encode_string(value
, enc
, sizeof(enc
));
268 printf("ID_FS_%s_ENC=%s\n", name
, enc
);
270 } else if (!strcmp(name
, "PTTYPE")) {
271 printf("ID_PART_TABLE_TYPE=%s\n", value
);
273 } else if (!strcmp(name
, "PART_ENTRY_NAME") ||
274 !strcmp(name
, "PART_ENTRY_TYPE")) {
276 blkid_encode_string(value
, enc
, sizeof(enc
));
277 printf("ID_%s=%s\n", name
, enc
);
279 } else if (!strncmp(name
, "PART_ENTRY_", 11))
280 printf("ID_%s=%s\n", name
, value
);
282 else if (namelen
>= 15 && (
283 !strcmp(name
+ (namelen
- 12), "_SECTOR_SIZE") ||
284 !strcmp(name
+ (namelen
- 8), "_IO_SIZE") ||
285 !strcmp(name
, "ALIGNMENT_OFFSET")))
286 printf("ID_IOLIMIT_%s=%s\n", name
, value
);
288 printf("ID_FS_%s=%s\n", name
, value
);
291 static int has_item(char *ary
[], const char *item
)
295 for (p
= ary
; *p
!= NULL
; p
++)
296 if (!strcmp(item
, *p
))
301 static void print_value(int output
, int num
, const char *devname
,
302 const char *value
, const char *name
, size_t valsz
)
304 if (output
& OUTPUT_VALUE_ONLY
) {
305 fputs(value
, stdout
);
308 } else if (output
& OUTPUT_UDEV_LIST
) {
309 print_udev_format(name
, value
);
311 } else if (output
& OUTPUT_EXPORT_LIST
) {
314 safe_print(value
, valsz
);
318 if (num
== 1 && devname
)
319 printf("%s: ", devname
);
321 fputs("=\"", stdout
);
322 safe_print(value
, valsz
);
323 fputs("\" ", stdout
);
327 static void print_tags(blkid_dev dev
, char *show
[], int output
)
329 blkid_tag_iterate iter
;
330 const char *type
, *value
, *devname
;
332 static int first
= 1;
337 if (output
& OUTPUT_PRETTY_LIST
) {
338 pretty_print_dev(dev
);
342 devname
= blkid_dev_devname(dev
);
344 if (output
& OUTPUT_DEVICE_ONLY
) {
345 printf("%s\n", devname
);
349 iter
= blkid_tag_iterate_begin(dev
);
350 while (blkid_tag_next(iter
, &type
, &value
) == 0) {
351 if (show
[0] && !has_item(show
, type
))
354 if (num
== 1 && !first
&&
355 (output
& (OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
)))
356 /* add extra line between output from more devices */
359 print_value(output
, num
++, devname
, value
, type
, strlen(value
));
361 blkid_tag_iterate_end(iter
);
364 if (!(output
& (OUTPUT_VALUE_ONLY
| OUTPUT_UDEV_LIST
|
365 OUTPUT_EXPORT_LIST
)))
372 static int append_str(char **res
, size_t *sz
, const char *a
, const char *b
)
375 size_t asz
= a
? strlen(a
) : 0;
376 size_t bsz
= b
? strlen(b
) : 0;
377 size_t len
= *sz
+ asz
+ bsz
;
382 str
= realloc(str
, len
+ 1);
403 * Compose and print ID_FS_AMBIVALENT for udev
405 static int print_udev_ambivalent(blkid_probe pr
)
409 int count
= 0, rc
= -1;
411 while (!blkid_do_probe(pr
)) {
412 const char *usage
= NULL
, *type
= NULL
, *version
= NULL
;
415 blkid_probe_lookup_value(pr
, "USAGE", &usage
, NULL
);
416 blkid_probe_lookup_value(pr
, "TYPE", &type
, NULL
);
417 blkid_probe_lookup_value(pr
, "VERSION", &version
, NULL
);
422 blkid_encode_string(usage
, enc
, sizeof(enc
));
423 if (append_str(&val
, &valsz
, enc
, ":"))
426 blkid_encode_string(type
, enc
, sizeof(enc
));
427 if (append_str(&val
, &valsz
, enc
, version
? ":" : " "))
431 blkid_encode_string(version
, enc
, sizeof(enc
));
432 if (append_str(&val
, &valsz
, enc
, " "))
439 *(val
+ valsz
- 1) = '\0'; /* rem tailing whitespace */
440 printf("ID_FS_AMBIVALEN=%s\n", val
);
448 static int lowprobe_superblocks(blkid_probe pr
)
453 if (fstat(blkid_probe_get_fd(pr
), &st
))
456 blkid_probe_enable_partitions(pr
, 1);
458 if (!S_ISCHR(st
.st_mode
) && blkid_probe_get_size(pr
) <= 1024 * 1440 &&
459 blkid_probe_is_wholedisk(pr
)) {
461 * check if the small disk is partitioned, if yes then
462 * don't probe for filesystems.
464 blkid_probe_enable_superblocks(pr
, 0);
466 rc
= blkid_do_fullprobe(pr
);
468 return rc
; /* -1 = error, 1 = nothing, 0 = succes */
470 if (blkid_probe_lookup_value(pr
, "PTTYPE", NULL
, NULL
) == 0)
471 return 0; /* partition table detected */
474 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
475 blkid_probe_enable_superblocks(pr
, 1);
477 return blkid_do_safeprobe(pr
);
480 static int lowprobe_topology(blkid_probe pr
)
482 /* enable topology probing only */
483 blkid_probe_enable_topology(pr
, 1);
485 blkid_probe_enable_superblocks(pr
, 0);
486 blkid_probe_enable_partitions(pr
, 0);
488 return blkid_do_fullprobe(pr
);
491 static int lowprobe_device(blkid_probe pr
, const char *devname
,
492 int chain
, char *show
[], int output
,
493 blkid_loff_t offset
, blkid_loff_t size
)
497 int nvals
= 0, n
, num
= 1;
501 static int first
= 1;
503 fd
= open(devname
, O_RDONLY
);
505 fprintf(stderr
, "error: %s: %m\n", devname
);
508 if (blkid_probe_set_device(pr
, fd
, offset
, size
))
511 if (chain
& LOWPROBE_TOPOLOGY
)
512 rc
= lowprobe_topology(pr
);
513 if (rc
>= 0 && (chain
& LOWPROBE_SUPERBLOCKS
))
514 rc
= lowprobe_superblocks(pr
);
519 nvals
= blkid_probe_numof_values(pr
);
522 !(chain
& LOWPROBE_TOPOLOGY
) &&
523 !(output
& OUTPUT_UDEV_LIST
) &&
524 !blkid_probe_has_value(pr
, "TYPE") &&
525 !blkid_probe_has_value(pr
, "PTTYPE"))
527 * Ignore probing result if there is not any filesystem or
528 * partition table on the device and udev output is not
531 * The udev db stores information about partitions, so
532 * PART_ENTRY_* values are alway important.
536 if (nvals
&& !first
&& output
& (OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
))
537 /* add extra line between output from devices */
540 if (nvals
&& (output
& OUTPUT_DEVICE_ONLY
)) {
541 printf("%s\n", devname
);
545 for (n
= 0; n
< nvals
; n
++) {
546 if (blkid_probe_get_value(pr
, n
, &name
, &data
, &len
))
548 if (show
[0] && !has_item(show
, name
))
550 len
= strnlen((char *) data
, len
);
551 print_value(output
, num
++, devname
, (char *) data
, name
, len
);
556 if (nvals
>= 1 && !(output
& (OUTPUT_VALUE_ONLY
|
557 OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
)))
561 if (output
& OUTPUT_UDEV_LIST
)
562 print_udev_ambivalent(pr
);
565 "%s: ambivalent result (probably more "
566 "filesystems on the device, use wipefs(8) "
567 "to see more details)\n",
573 return 8; /* ambivalent probing result */
575 return 2; /* nothing detected */
577 return 0; /* sucess */
580 /* converts comma separated list to BLKID_USAGE_* mask */
581 static int list_to_usage(const char *list
, int *flag
)
584 const char *word
= NULL
, *p
= list
;
586 if (p
&& strncmp(p
, "no", 2) == 0) {
587 *flag
= BLKID_FLTR_NOTIN
;
597 if (!strncmp(word
, "filesystem", 10))
598 mask
|= BLKID_USAGE_FILESYSTEM
;
599 else if (!strncmp(word
, "raid", 4))
600 mask
|= BLKID_USAGE_RAID
;
601 else if (!strncmp(word
, "crypto", 6))
602 mask
|= BLKID_USAGE_CRYPTO
;
603 else if (!strncmp(word
, "other", 5))
604 mask
|= BLKID_USAGE_OTHER
;
611 fprintf(stderr
, "unknown kerword in -u <list> argument: '%s'\n",
616 /* converts comma separated list to types[] */
617 static char **list_to_types(const char *list
, int *flag
)
620 const char *p
= list
;
623 if (p
&& strncmp(p
, "no", 2) == 0) {
624 *flag
= BLKID_FLTR_NOTIN
;
628 fprintf(stderr
, "error: -u <list> argument is empty\n");
631 for (i
= 1; p
&& (p
= strchr(p
, ',')); i
++, p
++);
633 res
= calloc(i
+ 1, sizeof(char *));
636 p
= *flag
& BLKID_FLTR_NOTIN
? list
+ 2 : list
;
640 const char *word
= p
;
642 res
[i
] = p
? strndup(word
, p
- word
) : strdup(word
);
651 fprintf(stderr
, "out of memory\n");
658 static void free_types_list(char *list
[])
664 for (n
= list
; *n
; n
++)
669 int main(int argc
, char **argv
)
671 blkid_cache cache
= NULL
;
672 char **devices
= NULL
;
673 char *show
[128] = { NULL
, };
674 char *search_type
= NULL
, *search_value
= NULL
;
678 char **fltr_type
= NULL
;
679 int fltr_flag
= BLKID_FLTR_ONLYIN
;
680 unsigned int numdev
= 0, numtag
= 0;
684 int output_format
= 0;
685 int lookup
= 0, gc
= 0, lowprobe
= 0, eval
= 0;
687 uintmax_t offset
= 0, size
= 0;
691 while ((c
= getopt (argc
, argv
, "c:df:ghilL:n:ko:O:ps:S:t:u:U:w:v")) != EOF
)
694 if (optarg
&& !*optarg
)
706 search_value
= strdup(optarg
);
707 search_type
= strdup("LABEL");
711 fprintf(stderr
, "error: -u and -n options are mutually exclusive\n");
714 fltr_type
= list_to_types(optarg
, &fltr_flag
);
718 fprintf(stderr
, "error: -u and -n options are mutually exclusive\n");
721 fltr_usage
= list_to_usage(optarg
, &fltr_flag
);
725 search_value
= strdup(optarg
);
726 search_type
= strdup("UUID");
729 lowprobe
|= LOWPROBE_TOPOLOGY
;
740 const char *name
= NULL
;
742 while (blkid_superblocks_get_name(idx
++, &name
, NULL
) == 0)
743 printf("%s\n", name
);
747 if (!strcmp(optarg
, "value"))
748 output_format
= OUTPUT_VALUE_ONLY
;
749 else if (!strcmp(optarg
, "device"))
750 output_format
= OUTPUT_DEVICE_ONLY
;
751 else if (!strcmp(optarg
, "list"))
752 output_format
= OUTPUT_PRETTY_LIST
;
753 else if (!strcmp(optarg
, "udev"))
754 output_format
= OUTPUT_UDEV_LIST
;
755 else if (!strcmp(optarg
, "export"))
756 output_format
= OUTPUT_EXPORT_LIST
;
757 else if (!strcmp(optarg
, "full"))
760 fprintf(stderr
, "Invalid output format %s. "
761 "Choose from value,\n\t"
762 "device, list, udev or full\n", optarg
);
767 if (strtosize(optarg
, &offset
))
769 "Invalid offset '%s' specified\n",
773 lowprobe
|= LOWPROBE_SUPERBLOCKS
;
776 if (numtag
+ 1 >= sizeof(show
) / sizeof(*show
)) {
777 fprintf(stderr
, "Too many tags specified\n");
780 show
[numtag
++] = optarg
;
784 if (strtosize(optarg
, &size
))
786 "Invalid size '%s' specified\n",
791 fprintf(stderr
, "Can only search for "
792 "one NAME=value pair\n");
795 if (blkid_parse_tag_string(optarg
,
798 fprintf(stderr
, "-t needs NAME=value pair\n");
806 if (optarg
&& !*optarg
)
818 /* The rest of the args are device names */
820 devices
= calloc(argc
- optind
, sizeof(char *));
822 fprintf(stderr
, "Failed to allocate device name array\n");
826 while (optind
< argc
)
827 devices
[numdev
++] = argv
[optind
++];
831 print_version(stdout
);
835 /* convert LABEL/UUID lookup to evaluate request */
836 if (lookup
&& output_format
== OUTPUT_DEVICE_ONLY
&& search_type
&&
837 (!strcmp(search_type
, "LABEL") || !strcmp(search_type
, "UUID"))) {
842 if (!lowprobe
&& !eval
&& blkid_get_cache(&cache
, read
) < 0)
846 blkid_gc_cache(cache
);
852 if (eval
== 0 && (output_format
& OUTPUT_PRETTY_LIST
)) {
854 fprintf(stderr
, "The low-level probing mode does not "
855 "support 'list' output format\n");
858 pretty_print_dev(NULL
);
868 fprintf(stderr
, "The low-level probing mode "
869 "requires a device\n");
873 /* automatically enable 'export' format for I/O Limits */
874 if (!output_format
&& (lowprobe
& LOWPROBE_TOPOLOGY
))
875 output_format
= OUTPUT_EXPORT_LIST
;
877 pr
= blkid_new_probe();
881 if (lowprobe
& LOWPROBE_SUPERBLOCKS
) {
882 blkid_probe_set_superblocks_flags(pr
,
883 BLKID_SUBLKS_LABEL
| BLKID_SUBLKS_UUID
|
884 BLKID_SUBLKS_TYPE
| BLKID_SUBLKS_SECTYPE
|
885 BLKID_SUBLKS_USAGE
| BLKID_SUBLKS_VERSION
);
887 if (fltr_usage
&& blkid_probe_filter_superblocks_usage(
888 pr
, fltr_flag
, fltr_usage
))
891 else if (fltr_type
&& blkid_probe_filter_superblocks_type(
892 pr
, fltr_flag
, fltr_type
))
896 for (i
= 0; i
< numdev
; i
++)
897 err
= lowprobe_device(pr
, devices
[i
], lowprobe
, show
,
899 (blkid_loff_t
) offset
,
900 (blkid_loff_t
) size
);
901 blkid_free_probe(pr
);
906 char *res
= blkid_evaluate_tag(search_type
, search_value
, NULL
);
913 * Classic (cache based) API
918 fprintf(stderr
, "The lookup option requires a "
919 "search type specified using -t\n");
922 /* Load any additional devices not in the cache */
923 for (i
= 0; i
< numdev
; i
++)
924 blkid_get_dev(cache
, devices
[i
], BLKID_DEV_NORMAL
);
926 if ((dev
= blkid_find_dev_with_tag(cache
, search_type
,
928 print_tags(dev
, show
, output_format
);
931 /* If we didn't specify a single device, show all available devices */
932 } else if (!numdev
) {
933 blkid_dev_iterate iter
;
936 blkid_probe_all(cache
);
938 iter
= blkid_dev_iterate_begin(cache
);
939 blkid_dev_set_search(iter
, search_type
, search_value
);
940 while (blkid_dev_next(iter
, &dev
) == 0) {
941 dev
= blkid_verify(cache
, dev
);
944 print_tags(dev
, show
, output_format
);
947 blkid_dev_iterate_end(iter
);
948 /* Add all specified devices to cache (optionally display tags) */
949 } else for (i
= 0; i
< numdev
; i
++) {
950 blkid_dev dev
= blkid_get_dev(cache
, devices
[i
],
955 !blkid_dev_has_tag(dev
, search_type
,
958 print_tags(dev
, show
, output_format
);
966 free_types_list(fltr_type
);
967 if (!lowprobe
&& !eval
)
968 blkid_put_cache(cache
);