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 0x0001
33 #define OUTPUT_DEVICE_ONLY 0x0002
34 #define OUTPUT_PRETTY_LIST 0x0004
35 #define OUTPUT_UDEV_LIST 0x0008
39 #include "ismounted.h"
41 const char *progname
= "blkid";
43 static void print_version(FILE *out
)
45 fprintf(out
, "%s from %s (libblkid %s, %s)\n",
46 progname
, PACKAGE_STRING
, LIBBLKID_VERSION
, LIBBLKID_DATE
);
49 static void usage(int error
)
51 FILE *out
= error
? stderr
: stdout
;
56 " %1$s -L <label> | -U <uuid>\n\n"
57 " %1$s [-c <file>] [-ghlLv] [-o format] [-s <tag>] \n"
58 " [-t <token>] [-w <file>] [dev ...]\n\n"
59 " %1$s -p [-O <offset>] [-S <size>] [-o format] <dev> [dev ...]\n\n"
61 " -c <file> cache file (default: /etc/blkid.tab, /dev/null = none)\n"
62 " -h print this usage message and exit\n"
63 " -g garbage collect the blkid cache\n"
64 " -o <format> output format; can be one of:\n"
65 " value, device, list, udev or full; (default: full)\n"
66 " -s <tag> show specified tag(s) (default show all tags)\n"
67 " -t <token> find device with a specific token (NAME=value pair)\n"
68 " -l lookup the the first device with arguments specified by -t\n"
69 " -L <label> convert LABEL to device name\n"
70 " -U <uuid> convert UUID to device name\n"
71 " -v print version and exit\n"
72 " -w <file> write cache to different file (/dev/null = no write)\n"
73 " <dev> specify device(s) to probe (default: all devices)\n\n"
74 "Low-level probing options:\n"
75 " -p switch to low-level mode (bypass cache)\n"
76 " -S <bytes> overwrite device size\n"
77 " -O <bytes> probe at the given offset\n"
78 " -u <list> filter by \"usage\" (e.g. -u filesystem,raid)\n"
79 " -n <list> filter by filesystem type (e.g. -n vfat,ext3)\n"
87 * This function does "safe" printing. It will convert non-printable
88 * ASCII characters using '^' and M- notation.
90 static void safe_print(const char *cp
, int len
)
103 if ((ch
< 32) || (ch
== 0x7f)) {
105 ch
^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
111 static int get_terminal_width(void)
114 struct ttysize t_win
;
117 struct winsize w_win
;
122 if (ioctl (0, TIOCGSIZE
, &t_win
) == 0)
123 return (t_win
.ts_cols
);
126 if (ioctl (0, TIOCGWINSZ
, &w_win
) == 0)
127 return (w_win
.ws_col
);
129 cp
= getenv("COLUMNS");
131 return strtol(cp
, NULL
, 10);
135 static int pretty_print_word(const char *str
, int max_len
,
136 int left_len
, int overflow_nl
)
138 int len
= strlen(str
) + left_len
;
142 if (overflow_nl
&& len
> max_len
) {
145 } else if (len
> max_len
)
149 while (len
++ < max_len
);
153 static void pretty_print_line(const char *device
, const char *fs_type
,
154 const char *label
, const char *mtpt
,
157 static int device_len
= 10, fs_type_len
= 7;
158 static int label_len
= 8, mtpt_len
= 14;
159 static int term_width
= -1;
163 term_width
= get_terminal_width();
165 if (term_width
> 80) {
178 len
= pretty_print_word(device
, device_len
, 0, 1);
179 len
= pretty_print_word(fs_type
, fs_type_len
, len
, 0);
180 len
= pretty_print_word(label
, label_len
, len
, 0);
181 len
= pretty_print_word(mtpt
, mtpt_len
, len
, 0);
186 static void pretty_print_dev(blkid_dev dev
)
188 blkid_tag_iterate iter
;
189 const char *type
, *value
, *devname
;
190 const char *uuid
= "", *fs_type
= "", *label
= "";
191 int len
, mount_flags
;
196 pretty_print_line("device", "fs_type", "label",
197 "mount point", "UUID");
198 for (len
=get_terminal_width()-1; len
> 0; len
--)
204 devname
= blkid_dev_devname(dev
);
205 if (access(devname
, F_OK
))
208 /* Get the uuid, label, type */
209 iter
= blkid_tag_iterate_begin(dev
);
210 while (blkid_tag_next(iter
, &type
, &value
) == 0) {
211 if (!strcmp(type
, "UUID"))
213 if (!strcmp(type
, "TYPE"))
215 if (!strcmp(type
, "LABEL"))
218 blkid_tag_iterate_end(iter
);
220 /* Get the mount point */
222 retval
= check_mount_point(devname
, &mount_flags
, mtpt
, sizeof(mtpt
));
224 if (mount_flags
& MF_MOUNTED
) {
226 strcpy(mtpt
, "(mounted, mtpt unknown)");
227 } else if (mount_flags
& MF_BUSY
)
228 strcpy(mtpt
, "(in use)");
230 strcpy(mtpt
, "(not mounted)");
233 pretty_print_line(devname
, fs_type
, label
, mtpt
, uuid
);
236 static void print_udev_format(const char *name
, const char *value
, size_t sz
)
238 char enc
[265], safe
[256];
242 if (!strcmp(name
, "TYPE") || !strcmp(name
, "VERSION")) {
243 blkid_encode_string(value
, enc
, sizeof(enc
));
244 printf("ID_FS_%s=%s\n", name
, enc
);
246 } else if (!strcmp(name
, "UUID") ||
247 !strcmp(name
, "LABEL") ||
248 !strcmp(name
, "UUID_SUB")) {
250 blkid_safe_string(value
, safe
, sizeof(safe
));
251 printf("ID_FS_%s=%s\n", name
, safe
);
253 blkid_encode_string(value
, enc
, sizeof(enc
));
254 printf("ID_FS_%s_ENC=%s\n", name
, enc
);
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_safe_string(value
, safe
, sizeof(safe
));
263 printf("ID_%s=%s\n", name
, safe
);
265 blkid_encode_string(value
, enc
, sizeof(enc
));
266 printf("ID_%s_ENC=%s\n", name
, enc
);
268 } else if (!strncmp(name
, "PART_ENTRY_", 11))
269 printf("ID_%s=%s\n", name
, value
);
272 printf("ID_FS_%s=%s\n", name
, value
);
275 static int has_item(char *ary
[], const char *item
)
279 for (p
= ary
; *p
!= NULL
; p
++)
280 if (!strcmp(item
, *p
))
285 static void print_value(int output
, int num
, const char *devname
,
286 const char *value
, const char *name
, size_t valsz
)
288 if (output
& OUTPUT_VALUE_ONLY
) {
289 fputs(value
, stdout
);
292 } else if (output
& OUTPUT_UDEV_LIST
) {
293 print_udev_format(name
, value
, valsz
);
296 if (num
== 1 && devname
)
297 printf("%s: ", devname
);
299 fputs("=\"", stdout
);
300 safe_print(value
, valsz
);
301 fputs("\" ", stdout
);
305 static void print_tags(blkid_dev dev
, char *show
[], int output
)
307 blkid_tag_iterate iter
;
308 const char *type
, *value
, *devname
;
314 if (output
& OUTPUT_PRETTY_LIST
) {
315 pretty_print_dev(dev
);
319 devname
= blkid_dev_devname(dev
);
321 if (output
& OUTPUT_DEVICE_ONLY
) {
322 printf("%s\n", devname
);
326 iter
= blkid_tag_iterate_begin(dev
);
327 while (blkid_tag_next(iter
, &type
, &value
) == 0) {
328 if (show
[0] && !has_item(show
, type
))
330 print_value(output
, num
++, devname
, value
, type
, strlen(value
));
332 blkid_tag_iterate_end(iter
);
334 if (num
> 1 && !(output
& (OUTPUT_VALUE_ONLY
| OUTPUT_UDEV_LIST
)))
339 static int append_str(char **res
, size_t *sz
, const char *a
, const char *b
)
342 size_t asz
= a
? strlen(a
) : 0;
343 size_t bsz
= b
? strlen(b
) : 0;
344 size_t len
= *sz
+ asz
+ bsz
;
349 str
= realloc(str
, len
+ 1);
371 * Compose and print ID_FS_AMBIVALENT for udev
373 static int print_udev_ambivalent(blkid_probe pr
)
377 int count
= 0, rc
= -1;
379 while (!blkid_do_probe(pr
)) {
380 const char *usage
= NULL
, *type
= NULL
, *version
= NULL
;
383 blkid_probe_lookup_value(pr
, "USAGE", &usage
, NULL
);
384 blkid_probe_lookup_value(pr
, "TYPE", &type
, NULL
);
385 blkid_probe_lookup_value(pr
, "VERSION", &version
, NULL
);
390 blkid_encode_string(usage
, enc
, sizeof(enc
));
391 if (append_str(&val
, &valsz
, enc
, ":"))
394 blkid_encode_string(type
, enc
, sizeof(enc
));
395 if (append_str(&val
, &valsz
, enc
, version
? ":" : " "))
399 blkid_encode_string(version
, enc
, sizeof(enc
));
400 if (append_str(&val
, &valsz
, enc
, " "))
407 *(val
+ valsz
- 1) = '\0'; /* rem tailing whitespace */
408 printf("ID_FS_AMBIVALEN=%s\n", val
);
416 static int lowprobe_device(blkid_probe pr
, const char *devname
, char *show
[],
417 int output
, blkid_loff_t offset
, blkid_loff_t size
)
421 int nvals
= 0, n
, num
= 1, has_pt
= 0;
427 fd
= open(devname
, O_RDONLY
);
429 fprintf(stderr
, "error: %s: %s\n", devname
, strerror(errno
));
432 if (blkid_probe_set_device(pr
, fd
, offset
, size
))
440 blkid_probe_enable_superblocks(pr
, 0); /* enabled by default ;-( */
442 blkid_probe_enable_partitions(pr
, 1);
444 /* This is for PART_ENTRY_{UUID,NAME,TYPE,...} values and it will
445 * open() whole-disk device.
447 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
449 rc
= blkid_do_fullprobe(pr
);
450 blkid_probe_enable_partitions(pr
, 0);
453 goto done
; /* -1 = error, 1 = nothing, 0 = succes */
455 if (blkid_probe_lookup_value(pr
, "PTTYPE", NULL
, NULL
) == 0)
456 /* partition table detected */
460 * filesystems/raids probing
462 * Q: Why do we try to probe for FS/RAID on device with partiton table?
464 * A: For some devices the kernel creates partitions because it is too
465 * dumb to find out, that the device contains a RAID signature or
466 * other metadata that signifies, that this device should not be
469 * Many RAID signatures are at the end of the device, or at least
470 * still allow that the volume contains a partition table at the
471 * beginning of the device, which the kernel will find. Udev needs
472 * to look at the disk, if there are such RAID signatures, and then
473 * it needs to handle it, and in many cases just kill all the
474 * wrongly kernel-created partitions.
477 * http://thread.gmane.org/gmane.linux.utilities.util-linux-ng/2888/focus=2890
479 * This all is unncecessary for very small devices (<= 1.44MiB).
481 if (has_pt
== 0 || S_ISCHR(st
.st_mode
) ||
482 blkid_probe_get_size(pr
) > 1024 * 1440) {
484 * filesystems/RAIDs probing
486 blkid_probe_enable_superblocks(pr
, 1);
488 rc
= blkid_do_safeprobe(pr
);
493 nvals
= blkid_probe_numof_values(pr
);
495 if (output
& OUTPUT_DEVICE_ONLY
) {
496 printf("%s\n", devname
);
500 for (n
= 0; n
< nvals
; n
++) {
501 if (blkid_probe_get_value(pr
, n
, &name
, &data
, &len
))
503 if (show
[0] && !has_item(show
, name
))
505 len
= strnlen((char *) data
, len
);
506 print_value(output
, num
++, devname
, (char *) data
, name
, len
);
509 if (nvals
>= 1 && !(output
& (OUTPUT_VALUE_ONLY
| OUTPUT_UDEV_LIST
)))
513 if (output
& OUTPUT_UDEV_LIST
)
514 print_udev_ambivalent(pr
);
517 "%s: ambivalent result (probably more "
518 "filesystems on the device, use wipefs(8) "
519 "to see more details)\n",
523 return !nvals
? 2 : 0;
526 /* converts comma separated list to BLKID_USAGE_* mask */
527 static int list_to_usage(const char *list
, int *flag
)
530 const char *word
= NULL
, *p
= list
;
532 if (p
&& strncmp(p
, "no", 2) == 0) {
533 *flag
= BLKID_FLTR_NOTIN
;
543 if (!strncmp(word
, "filesystem", 10))
544 mask
|= BLKID_USAGE_FILESYSTEM
;
545 else if (!strncmp(word
, "raid", 4))
546 mask
|= BLKID_USAGE_RAID
;
547 else if (!strncmp(word
, "crypto", 6))
548 mask
|= BLKID_USAGE_CRYPTO
;
549 else if (!strncmp(word
, "other", 5))
550 mask
|= BLKID_USAGE_OTHER
;
557 fprintf(stderr
, "unknown kerword in -u <list> argument: '%s'\n",
562 /* converts comma separated list to types[] */
563 static char **list_to_types(const char *list
, int *flag
)
566 const char *p
= list
;
569 if (p
&& strncmp(p
, "no", 2) == 0) {
570 *flag
= BLKID_FLTR_NOTIN
;
574 fprintf(stderr
, "error: -u <list> argument is empty\n");
577 for (i
= 1; p
&& (p
= strchr(p
, ',')); i
++, p
++);
579 res
= calloc(i
+ 1, sizeof(char *));
582 p
= *flag
& BLKID_FLTR_NOTIN
? list
+ 2 : list
;
586 const char *word
= p
;
588 res
[i
] = p
? strndup(word
, p
- word
) : strdup(word
);
597 fprintf(stderr
, "out of memory\n");
604 static void free_types_list(char *list
[])
610 for (n
= list
; *n
; n
++)
615 int main(int argc
, char **argv
)
617 blkid_cache cache
= NULL
;
618 char *devices
[128] = { NULL
, };
619 char *show
[128] = { NULL
, };
620 char *search_type
= NULL
, *search_value
= NULL
;
624 char **fltr_type
= NULL
;
625 int fltr_flag
= BLKID_FLTR_ONLYIN
;
626 unsigned int numdev
= 0, numtag
= 0;
630 int output_format
= 0;
631 int lookup
= 0, gc
= 0, lowprobe
= 0, eval
= 0;
633 blkid_loff_t offset
= 0, size
= 0;
637 while ((c
= getopt (argc
, argv
, "c:f:ghlL:n:o:O:ps:S:t:u:U:w:v")) != EOF
)
640 if (optarg
&& !*optarg
)
649 search_value
= strdup(optarg
);
650 search_type
= strdup("LABEL");
654 fprintf(stderr
, "error: -u and -n options are mutually exclusive\n");
657 fltr_type
= list_to_types(optarg
, &fltr_flag
);
661 fprintf(stderr
, "error: -u and -n options are mutually exclusive\n");
664 fltr_usage
= list_to_usage(optarg
, &fltr_flag
);
668 search_value
= strdup(optarg
);
669 search_type
= strdup("UUID");
678 if (!strcmp(optarg
, "value"))
679 output_format
= OUTPUT_VALUE_ONLY
;
680 else if (!strcmp(optarg
, "device"))
681 output_format
= OUTPUT_DEVICE_ONLY
;
682 else if (!strcmp(optarg
, "list"))
683 output_format
= OUTPUT_PRETTY_LIST
;
684 else if (!strcmp(optarg
, "udev"))
685 output_format
= OUTPUT_UDEV_LIST
;
686 else if (!strcmp(optarg
, "full"))
689 fprintf(stderr
, "Invalid output format %s. "
690 "Choose from value,\n\t"
691 "device, list, udev or full\n", optarg
);
696 offset
= strtoll(optarg
, NULL
, 10);
702 if (numtag
+ 1 >= sizeof(show
) / sizeof(*show
)) {
703 fprintf(stderr
, "Too many tags specified\n");
706 show
[numtag
++] = optarg
;
710 size
= strtoll(optarg
, NULL
, 10);
714 fprintf(stderr
, "Can only search for "
715 "one NAME=value pair\n");
718 if (blkid_parse_tag_string(optarg
,
721 fprintf(stderr
, "-t needs NAME=value pair\n");
729 if (optarg
&& !*optarg
)
740 while (optind
< argc
)
741 devices
[numdev
++] = argv
[optind
++];
744 print_version(stdout
);
748 /* convert LABEL/UUID lookup to evaluate request */
749 if (lookup
&& output_format
== OUTPUT_DEVICE_ONLY
&& search_type
&&
750 (!strcmp(search_type
, "LABEL") || !strcmp(search_type
, "UUID"))) {
755 if (!lowprobe
&& !eval
&& blkid_get_cache(&cache
, read
) < 0)
759 blkid_gc_cache(cache
);
765 if (eval
== 0 && output_format
& OUTPUT_PRETTY_LIST
) {
767 fprintf(stderr
, "The low-level probing mode does not "
768 "support 'list' output format\n");
771 pretty_print_dev(NULL
);
781 fprintf(stderr
, "The low-level probing mode "
782 "requires a device\n");
785 pr
= blkid_new_probe();
789 blkid_probe_set_superblocks_flags(pr
,
790 BLKID_SUBLKS_LABEL
| BLKID_SUBLKS_UUID
|
791 BLKID_SUBLKS_TYPE
| BLKID_SUBLKS_SECTYPE
|
792 BLKID_SUBLKS_USAGE
| BLKID_SUBLKS_VERSION
);
795 blkid_probe_filter_superblocks_usage(pr
, fltr_flag
, fltr_usage
))
797 else if (fltr_type
&&
798 blkid_probe_filter_superblocks_type(pr
, fltr_flag
, fltr_type
))
801 for (i
= 0; i
< numdev
; i
++)
802 err
= lowprobe_device(pr
, devices
[i
], show
,
803 output_format
, offset
, size
);
804 blkid_free_probe(pr
);
809 char *res
= blkid_evaluate_tag(search_type
, search_value
, NULL
);
816 * Classic (cache based) API
821 fprintf(stderr
, "The lookup option requires a "
822 "search type specified using -t\n");
825 /* Load any additional devices not in the cache */
826 for (i
= 0; i
< numdev
; i
++)
827 blkid_get_dev(cache
, devices
[i
], BLKID_DEV_NORMAL
);
829 if ((dev
= blkid_find_dev_with_tag(cache
, search_type
,
831 print_tags(dev
, show
, output_format
);
834 /* If we didn't specify a single device, show all available devices */
835 } else if (!numdev
) {
836 blkid_dev_iterate iter
;
839 blkid_probe_all(cache
);
841 iter
= blkid_dev_iterate_begin(cache
);
842 blkid_dev_set_search(iter
, search_type
, search_value
);
843 while (blkid_dev_next(iter
, &dev
) == 0) {
844 dev
= blkid_verify(cache
, dev
);
847 print_tags(dev
, show
, output_format
);
850 blkid_dev_iterate_end(iter
);
851 /* Add all specified devices to cache (optionally display tags) */
852 } else for (i
= 0; i
< numdev
; i
++) {
853 blkid_dev dev
= blkid_get_dev(cache
, devices
[i
],
858 !blkid_dev_has_tag(dev
, search_type
,
861 print_tags(dev
, show
, output_format
);
869 free_types_list(fltr_type
);
870 if (!lowprobe
&& !eval
)
871 blkid_put_cache(cache
);