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>] [<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> read from <file> instead of reading from the default\n"
71 " cache file (-c /dev/null means no cache)\n"
72 " -d don't encode non-printing characters\n"
73 " -h print this usage message and exit\n"
74 " -g garbage collect the blkid cache\n"
75 " -o <format> output format; can be one of:\n"
76 " value, device, list, udev, export or full; (default: full)\n"
77 " -k list all known filesystems/RAIDs and exit\n"
78 " -s <tag> show specified tag(s) (default show all tags)\n"
79 " -t <token> find device with a specific token (NAME=value pair)\n"
80 " -l look up only first device with token specified by -t\n"
81 " -L <label> convert LABEL to device name\n"
82 " -U <uuid> convert UUID to device name\n"
83 " -v print version and exit\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 pretty_print_word(mtpt
, mtpt_len
, len
, 0);
200 static void pretty_print_dev(blkid_dev dev
)
202 blkid_tag_iterate iter
;
203 const char *type
, *value
, *devname
;
204 const char *uuid
= "", *fs_type
= "", *label
= "";
205 int len
, mount_flags
;
210 pretty_print_line("device", "fs_type", "label",
211 "mount point", "UUID");
212 for (len
=get_terminal_width()-1; len
> 0; len
--)
218 devname
= blkid_dev_devname(dev
);
219 if (access(devname
, F_OK
))
222 /* Get the uuid, label, type */
223 iter
= blkid_tag_iterate_begin(dev
);
224 while (blkid_tag_next(iter
, &type
, &value
) == 0) {
225 if (!strcmp(type
, "UUID"))
227 if (!strcmp(type
, "TYPE"))
229 if (!strcmp(type
, "LABEL"))
232 blkid_tag_iterate_end(iter
);
234 /* Get the mount point */
236 retval
= check_mount_point(devname
, &mount_flags
, mtpt
, sizeof(mtpt
));
238 if (mount_flags
& MF_MOUNTED
) {
240 strcpy(mtpt
, "(mounted, mtpt unknown)");
241 } else if (mount_flags
& MF_BUSY
)
242 strcpy(mtpt
, "(in use)");
244 strcpy(mtpt
, "(not mounted)");
247 pretty_print_line(devname
, fs_type
, label
, mtpt
, uuid
);
250 static void print_udev_format(const char *name
, const char *value
)
252 char enc
[265], safe
[256];
253 size_t namelen
= strlen(name
);
257 if (!strcmp(name
, "TYPE") || !strcmp(name
, "VERSION")) {
258 blkid_encode_string(value
, enc
, sizeof(enc
));
259 printf("ID_FS_%s=%s\n", name
, enc
);
261 } else if (!strcmp(name
, "UUID") ||
262 !strcmp(name
, "LABEL") ||
263 !strcmp(name
, "UUID_SUB")) {
265 blkid_safe_string(value
, safe
, sizeof(safe
));
266 printf("ID_FS_%s=%s\n", name
, safe
);
268 blkid_encode_string(value
, enc
, sizeof(enc
));
269 printf("ID_FS_%s_ENC=%s\n", name
, enc
);
271 } else if (!strcmp(name
, "PTTYPE")) {
272 printf("ID_PART_TABLE_TYPE=%s\n", value
);
274 } else if (!strcmp(name
, "PART_ENTRY_NAME") ||
275 !strcmp(name
, "PART_ENTRY_TYPE")) {
277 blkid_encode_string(value
, enc
, sizeof(enc
));
278 printf("ID_%s=%s\n", name
, enc
);
280 } else if (!strncmp(name
, "PART_ENTRY_", 11))
281 printf("ID_%s=%s\n", name
, value
);
283 else if (namelen
>= 15 && (
284 !strcmp(name
+ (namelen
- 12), "_SECTOR_SIZE") ||
285 !strcmp(name
+ (namelen
- 8), "_IO_SIZE") ||
286 !strcmp(name
, "ALIGNMENT_OFFSET")))
287 printf("ID_IOLIMIT_%s=%s\n", name
, value
);
289 printf("ID_FS_%s=%s\n", name
, value
);
292 static int has_item(char *ary
[], const char *item
)
296 for (p
= ary
; *p
!= NULL
; p
++)
297 if (!strcmp(item
, *p
))
302 static void print_value(int output
, int num
, const char *devname
,
303 const char *value
, const char *name
, size_t valsz
)
305 if (output
& OUTPUT_VALUE_ONLY
) {
306 fputs(value
, stdout
);
309 } else if (output
& OUTPUT_UDEV_LIST
) {
310 print_udev_format(name
, value
);
312 } else if (output
& OUTPUT_EXPORT_LIST
) {
313 if (num
== 1 && devname
)
314 printf("DEVNAME=%s\n", devname
);
317 safe_print(value
, valsz
);
321 if (num
== 1 && devname
)
322 printf("%s: ", devname
);
324 fputs("=\"", stdout
);
325 safe_print(value
, valsz
);
326 fputs("\" ", stdout
);
330 static void print_tags(blkid_dev dev
, char *show
[], int output
)
332 blkid_tag_iterate iter
;
333 const char *type
, *value
, *devname
;
335 static int first
= 1;
340 if (output
& OUTPUT_PRETTY_LIST
) {
341 pretty_print_dev(dev
);
345 devname
= blkid_dev_devname(dev
);
347 if (output
& OUTPUT_DEVICE_ONLY
) {
348 printf("%s\n", devname
);
352 iter
= blkid_tag_iterate_begin(dev
);
353 while (blkid_tag_next(iter
, &type
, &value
) == 0) {
354 if (show
[0] && !has_item(show
, type
))
357 if (num
== 1 && !first
&&
358 (output
& (OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
)))
359 /* add extra line between output from more devices */
362 print_value(output
, num
++, devname
, value
, type
, strlen(value
));
364 blkid_tag_iterate_end(iter
);
367 if (!(output
& (OUTPUT_VALUE_ONLY
| OUTPUT_UDEV_LIST
|
368 OUTPUT_EXPORT_LIST
)))
375 static int append_str(char **res
, size_t *sz
, const char *a
, const char *b
)
378 size_t asz
= a
? strlen(a
) : 0;
379 size_t bsz
= b
? strlen(b
) : 0;
380 size_t len
= *sz
+ asz
+ bsz
;
385 str
= realloc(str
, len
+ 1);
406 * Compose and print ID_FS_AMBIVALENT for udev
408 static int print_udev_ambivalent(blkid_probe pr
)
412 int count
= 0, rc
= -1;
414 while (!blkid_do_probe(pr
)) {
415 const char *usage
= NULL
, *type
= NULL
, *version
= NULL
;
418 blkid_probe_lookup_value(pr
, "USAGE", &usage
, NULL
);
419 blkid_probe_lookup_value(pr
, "TYPE", &type
, NULL
);
420 blkid_probe_lookup_value(pr
, "VERSION", &version
, NULL
);
425 blkid_encode_string(usage
, enc
, sizeof(enc
));
426 if (append_str(&val
, &valsz
, enc
, ":"))
429 blkid_encode_string(type
, enc
, sizeof(enc
));
430 if (append_str(&val
, &valsz
, enc
, version
? ":" : " "))
434 blkid_encode_string(version
, enc
, sizeof(enc
));
435 if (append_str(&val
, &valsz
, enc
, " "))
442 *(val
+ valsz
- 1) = '\0'; /* rem tailing whitespace */
443 printf("ID_FS_AMBIVALEN=%s\n", val
);
451 static int lowprobe_superblocks(blkid_probe pr
)
454 int rc
, fd
= blkid_probe_get_fd(pr
);
456 if (fd
< 0 || fstat(fd
, &st
))
459 blkid_probe_enable_partitions(pr
, 1);
461 if (!S_ISCHR(st
.st_mode
) && blkid_probe_get_size(pr
) <= 1024 * 1440 &&
462 blkid_probe_is_wholedisk(pr
)) {
464 * check if the small disk is partitioned, if yes then
465 * don't probe for filesystems.
467 blkid_probe_enable_superblocks(pr
, 0);
469 rc
= blkid_do_fullprobe(pr
);
471 return rc
; /* -1 = error, 1 = nothing, 0 = succes */
473 if (blkid_probe_lookup_value(pr
, "PTTYPE", NULL
, NULL
) == 0)
474 return 0; /* partition table detected */
477 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
478 blkid_probe_enable_superblocks(pr
, 1);
480 return blkid_do_safeprobe(pr
);
483 static int lowprobe_topology(blkid_probe pr
)
485 /* enable topology probing only */
486 blkid_probe_enable_topology(pr
, 1);
488 blkid_probe_enable_superblocks(pr
, 0);
489 blkid_probe_enable_partitions(pr
, 0);
491 return blkid_do_fullprobe(pr
);
494 static int lowprobe_device(blkid_probe pr
, const char *devname
,
495 int chain
, char *show
[], int output
,
496 blkid_loff_t offset
, blkid_loff_t size
)
500 int nvals
= 0, n
, num
= 1;
504 static int first
= 1;
506 fd
= open(devname
, O_RDONLY
);
508 fprintf(stderr
, "error: %s: %m\n", devname
);
511 if (blkid_probe_set_device(pr
, fd
, offset
, size
))
514 if (chain
& LOWPROBE_TOPOLOGY
)
515 rc
= lowprobe_topology(pr
);
516 if (rc
>= 0 && (chain
& LOWPROBE_SUPERBLOCKS
))
517 rc
= lowprobe_superblocks(pr
);
522 nvals
= blkid_probe_numof_values(pr
);
525 !(chain
& LOWPROBE_TOPOLOGY
) &&
526 !(output
& OUTPUT_UDEV_LIST
) &&
527 !blkid_probe_has_value(pr
, "TYPE") &&
528 !blkid_probe_has_value(pr
, "PTTYPE"))
530 * Ignore probing result if there is not any filesystem or
531 * partition table on the device and udev output is not
534 * The udev db stores information about partitions, so
535 * PART_ENTRY_* values are alway important.
539 if (nvals
&& !first
&& output
& (OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
))
540 /* add extra line between output from devices */
543 if (nvals
&& (output
& OUTPUT_DEVICE_ONLY
)) {
544 printf("%s\n", devname
);
548 for (n
= 0; n
< nvals
; n
++) {
549 if (blkid_probe_get_value(pr
, n
, &name
, &data
, &len
))
551 if (show
[0] && !has_item(show
, name
))
553 len
= strnlen((char *) data
, len
);
554 print_value(output
, num
++, devname
, (char *) data
, name
, len
);
559 if (nvals
>= 1 && !(output
& (OUTPUT_VALUE_ONLY
|
560 OUTPUT_UDEV_LIST
| OUTPUT_EXPORT_LIST
)))
564 if (output
& OUTPUT_UDEV_LIST
)
565 print_udev_ambivalent(pr
);
568 "%s: ambivalent result (probably more "
569 "filesystems on the device, use wipefs(8) "
570 "to see more details)\n",
576 return 8; /* ambivalent probing result */
578 return 2; /* nothing detected */
580 return 0; /* sucess */
583 /* converts comma separated list to BLKID_USAGE_* mask */
584 static int list_to_usage(const char *list
, int *flag
)
587 const char *word
= NULL
, *p
= list
;
589 if (p
&& strncmp(p
, "no", 2) == 0) {
590 *flag
= BLKID_FLTR_NOTIN
;
600 if (!strncmp(word
, "filesystem", 10))
601 mask
|= BLKID_USAGE_FILESYSTEM
;
602 else if (!strncmp(word
, "raid", 4))
603 mask
|= BLKID_USAGE_RAID
;
604 else if (!strncmp(word
, "crypto", 6))
605 mask
|= BLKID_USAGE_CRYPTO
;
606 else if (!strncmp(word
, "other", 5))
607 mask
|= BLKID_USAGE_OTHER
;
614 fprintf(stderr
, "unknown kerword in -u <list> argument: '%s'\n",
619 /* converts comma separated list to types[] */
620 static char **list_to_types(const char *list
, int *flag
)
623 const char *p
= list
;
626 if (p
&& strncmp(p
, "no", 2) == 0) {
627 *flag
= BLKID_FLTR_NOTIN
;
631 fprintf(stderr
, "error: -u <list> argument is empty\n");
634 for (i
= 1; p
&& (p
= strchr(p
, ',')); i
++, p
++);
636 res
= calloc(i
+ 1, sizeof(char *));
639 p
= *flag
& BLKID_FLTR_NOTIN
? list
+ 2 : list
;
643 const char *word
= p
;
645 res
[i
] = p
? strndup(word
, p
- word
) : strdup(word
);
654 fprintf(stderr
, "out of memory\n");
661 static void free_types_list(char *list
[])
667 for (n
= list
; *n
; n
++)
672 int main(int argc
, char **argv
)
674 blkid_cache cache
= NULL
;
675 char **devices
= NULL
;
676 char *show
[128] = { NULL
, };
677 char *search_type
= NULL
, *search_value
= NULL
;
680 char **fltr_type
= NULL
;
681 int fltr_flag
= BLKID_FLTR_ONLYIN
;
682 unsigned int numdev
= 0, numtag
= 0;
686 int output_format
= 0;
687 int lookup
= 0, gc
= 0, lowprobe
= 0, eval
= 0;
689 uintmax_t offset
= 0, size
= 0;
693 while ((c
= getopt (argc
, argv
, "c:df:ghilL:n:ko:O:ps:S:t:u:U:w:v")) != EOF
)
696 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 /* ignore - backward compatibility */
816 /* The rest of the args are device names */
818 devices
= calloc(argc
- optind
, sizeof(char *));
820 fprintf(stderr
, "Failed to allocate device name array\n");
824 while (optind
< argc
)
825 devices
[numdev
++] = argv
[optind
++];
829 print_version(stdout
);
833 /* convert LABEL/UUID lookup to evaluate request */
834 if (lookup
&& output_format
== OUTPUT_DEVICE_ONLY
&& search_type
&&
835 (!strcmp(search_type
, "LABEL") || !strcmp(search_type
, "UUID"))) {
840 if (!lowprobe
&& !eval
&& blkid_get_cache(&cache
, read
) < 0)
844 blkid_gc_cache(cache
);
850 if (eval
== 0 && (output_format
& OUTPUT_PRETTY_LIST
)) {
852 fprintf(stderr
, "The low-level probing mode does not "
853 "support 'list' output format\n");
856 pretty_print_dev(NULL
);
866 fprintf(stderr
, "The low-level probing mode "
867 "requires a device\n");
871 /* automatically enable 'export' format for I/O Limits */
872 if (!output_format
&& (lowprobe
& LOWPROBE_TOPOLOGY
))
873 output_format
= OUTPUT_EXPORT_LIST
;
875 pr
= blkid_new_probe();
879 if (lowprobe
& LOWPROBE_SUPERBLOCKS
) {
880 blkid_probe_set_superblocks_flags(pr
,
881 BLKID_SUBLKS_LABEL
| BLKID_SUBLKS_UUID
|
882 BLKID_SUBLKS_TYPE
| BLKID_SUBLKS_SECTYPE
|
883 BLKID_SUBLKS_USAGE
| BLKID_SUBLKS_VERSION
);
885 if (fltr_usage
&& blkid_probe_filter_superblocks_usage(
886 pr
, fltr_flag
, fltr_usage
))
889 else if (fltr_type
&& blkid_probe_filter_superblocks_type(
890 pr
, fltr_flag
, fltr_type
))
894 for (i
= 0; i
< numdev
; i
++)
895 err
= lowprobe_device(pr
, devices
[i
], lowprobe
, show
,
897 (blkid_loff_t
) offset
,
898 (blkid_loff_t
) size
);
899 blkid_free_probe(pr
);
904 char *res
= blkid_evaluate_tag(search_type
, search_value
, NULL
);
911 * Classic (cache based) API
916 fprintf(stderr
, "The lookup option requires a "
917 "search type specified using -t\n");
920 /* Load any additional devices not in the cache */
921 for (i
= 0; i
< numdev
; i
++)
922 blkid_get_dev(cache
, devices
[i
], BLKID_DEV_NORMAL
);
924 if ((dev
= blkid_find_dev_with_tag(cache
, search_type
,
926 print_tags(dev
, show
, output_format
);
929 /* If we didn't specify a single device, show all available devices */
930 } else if (!numdev
) {
931 blkid_dev_iterate iter
;
934 blkid_probe_all(cache
);
936 iter
= blkid_dev_iterate_begin(cache
);
937 blkid_dev_set_search(iter
, search_type
, search_value
);
938 while (blkid_dev_next(iter
, &dev
) == 0) {
939 dev
= blkid_verify(cache
, dev
);
942 print_tags(dev
, show
, output_format
);
945 blkid_dev_iterate_end(iter
);
946 /* Add all specified devices to cache (optionally display tags) */
947 } else for (i
= 0; i
< numdev
; i
++) {
948 blkid_dev dev
= blkid_get_dev(cache
, devices
[i
],
953 !blkid_dev_has_tag(dev
, search_type
,
956 print_tags(dev
, show
, output_format
);
964 free_types_list(fltr_type
);
965 if (!lowprobe
&& !eval
)
966 blkid_put_cache(cache
);