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>
25 #ifdef HAVE_SYS_IOCTL_H
26 #include <sys/ioctl.h>
31 extern int getopt(int argc, char * const argv[], const char *optstring);
36 #define OUTPUT_VALUE_ONLY 0x0001
37 #define OUTPUT_DEVICE_ONLY 0x0002
38 #define OUTPUT_PRETTY_LIST 0x0004
39 #define OUTPUT_UDEV_LIST 0x0008
43 const char *progname = "blkid";
45 static void print_version(FILE *out)
47 fprintf(out, "%s from %s (libblkid %s, %s)\n",
48 progname, PACKAGE_STRING, LIBBLKID_VERSION, LIBBLKID_DATE);
51 static void usage(int error)
53 FILE *out = error ? stderr : stdout;
58 " %1$s -L <label> | -U <uuid>\n\n"
59 " %1$s [-c <file>] [-ghlLv] [-o format] [-s <tag>] \n"
60 " [-t <token>] [-w <file>] [dev ...]\n\n"
61 " %1$s -p [-O <offset>] [-S <size>] [-o format] <dev> [dev ...]\n\n"
63 " -c <file> cache file (default: /etc/blkid.tab, /dev/null = none)\n"
64 " -h print this usage message and exit\n"
65 " -g garbage collect the blkid cache\n"
66 " -o <format> output format; can be one of:\n"
67 " value, device, list, udev or full; (default: full)\n"
68 " -s <tag> show specified tag(s) (default show all tags)\n"
69 " -t <token> find device with a specific token (NAME=value pair)\n"
70 " -l lookup the the first device with arguments specified by -t\n"
71 " -L <label> convert LABEL to device name\n"
72 " -U <uuid> convert UUID to device name\n"
73 " -v print version and exit\n"
74 " -w <file> write cache to different file (/dev/null = no write)\n"
75 " <dev> specify device(s) to probe (default: all devices)\n\n"
76 "Low-level probing options:\n"
77 " -p switch to low-level mode (bypass cache)\n"
78 " -S <bytes> overwrite device size\n"
79 " -O <bytes> probe at the given offset\n"
80 " -u <list> filter by \"usage\" (e.g. -u filesystem,raid)\n"
88 * This function does "safe" printing. It will convert non-printable
89 * ASCII characters using '^' and M- notation.
91 static void safe_print(const char *cp, int len)
104 if ((ch < 32) || (ch == 0x7f)) {
106 ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
112 static int get_terminal_width(void)
115 struct ttysize t_win;
118 struct winsize w_win;
123 if (ioctl (0, TIOCGSIZE, &t_win) == 0)
124 return (t_win.ts_cols);
127 if (ioctl (0, TIOCGWINSZ, &w_win) == 0)
128 return (w_win.ws_col);
130 cp = getenv("COLUMNS");
132 return strtol(cp, NULL, 10);
136 static int pretty_print_word(const char *str, int max_len,
137 int left_len, int overflow_nl)
139 int len = strlen(str) + left_len;
143 if (overflow_nl && len > max_len) {
146 } else if (len > max_len)
150 while (len++ < max_len);
154 static void pretty_print_line(const char *device, const char *fs_type,
155 const char *label, const char *mtpt,
158 static int device_len = 10, fs_type_len = 7;
159 static int label_len = 8, mtpt_len = 14;
160 static int term_width = -1;
164 term_width = get_terminal_width();
166 if (term_width > 80) {
179 len = pretty_print_word(device, device_len, 0, 1);
180 len = pretty_print_word(fs_type, fs_type_len, len, 0);
181 len = pretty_print_word(label, label_len, len, 0);
182 len = pretty_print_word(mtpt, mtpt_len, len, 0);
187 static void pretty_print_dev(blkid_dev dev)
189 fprintf(stderr, "pretty print not implemented yet\n");
191 #ifdef NOT_IMPLEMENTED
192 blkid_tag_iterate iter;
193 const char *type, *value, *devname;
194 const char *uuid = "", *fs_type = "", *label = "";
196 int len, mount_flags;
201 pretty_print_line("device", "fs_type", "label",
202 "mount point", "UUID");
203 for (len=get_terminal_width()-1; len > 0; len--)
209 devname = blkid_dev_devname(dev);
210 if (access(devname, F_OK))
213 /* Get the uuid, label, type */
214 iter = blkid_tag_iterate_begin(dev);
215 while (blkid_tag_next(iter, &type, &value) == 0) {
216 if (!strcmp(type, "UUID"))
218 if (!strcmp(type, "TYPE"))
220 if (!strcmp(type, "LABEL"))
223 blkid_tag_iterate_end(iter);
225 /* Get the mount point */
227 retval = ext2fs_check_mount_point(devname, &mount_flags,
230 if (mount_flags & EXT2_MF_MOUNTED) {
232 strcpy(mtpt, "(mounted, mtpt unknown)");
233 } else if (mount_flags & EXT2_MF_BUSY)
234 strcpy(mtpt, "(in use)");
236 strcpy(mtpt, "(not mounted)");
239 pretty_print_line(devname, fs_type, label, mtpt, uuid);
243 static void print_udev_format(const char *name, const char *value, size_t sz)
245 char enc[265], safe[256];
249 if (!strcmp(name, "TYPE") || !strcmp(name, "VERSION")) {
250 blkid_encode_string(value, enc, sizeof(enc));
251 printf("ID_FS_%s=%s\n", name, enc);
253 } else if (!strcmp(name, "UUID") ||
254 !strcmp(name, "LABEL") ||
255 !strcmp(name, "UUID_SUB")) {
257 blkid_safe_string(value, safe, sizeof(safe));
258 printf("ID_FS_%s=%s\n", name, safe);
260 blkid_encode_string(value, enc, sizeof(enc));
261 printf("ID_FS_%s_ENC=%s\n", name, enc);
264 printf("ID_FS_%s=%s\n", name, value);
267 static void print_value(int output, int num, const char *devname,
268 const char *value, const char *name, size_t valsz)
270 if (output & OUTPUT_VALUE_ONLY) {
271 fputs(value, stdout);
274 } else if (output & OUTPUT_UDEV_LIST) {
275 print_udev_format(name, value, valsz);
278 if (num == 1 && devname)
279 printf("%s: ", devname);
281 fputs("=\"", stdout);
282 safe_print(value, valsz);
283 fputs("\" ", stdout);
287 static void print_tags(blkid_dev dev, char *show[], int numtag, int output)
289 blkid_tag_iterate iter;
290 const char *type, *value, *devname;
296 if (output & OUTPUT_PRETTY_LIST) {
297 pretty_print_dev(dev);
301 devname = blkid_dev_devname(dev);
303 if (output & OUTPUT_DEVICE_ONLY) {
304 printf("%s\n", devname);
308 iter = blkid_tag_iterate_begin(dev);
309 while (blkid_tag_next(iter, &type, &value) == 0) {
310 if (numtag && show) {
311 for (i=0; i < numtag; i++)
312 if (!strcmp(type, show[i]))
317 print_value(output, num++, devname, value, type, strlen(value));
319 blkid_tag_iterate_end(iter);
321 if (num > 1 && !(output & (OUTPUT_VALUE_ONLY | OUTPUT_UDEV_LIST)))
325 static int lowprobe_device(blkid_probe pr, const char *devname, int output,
326 blkid_loff_t offset, blkid_loff_t size)
335 fd = open(devname, O_RDONLY);
339 if (blkid_probe_set_device(pr, fd, offset, size))
341 rc = blkid_do_safeprobe(pr);
345 nvals = blkid_probe_numof_values(pr);
347 if (output & OUTPUT_DEVICE_ONLY) {
348 printf("%s\n", devname);
352 for (n = 0; n < nvals; n++) {
353 if (blkid_probe_get_value(pr, n, &name, &data, &len))
356 len = strnlen((char *) data, len);
357 print_value(output, n + 1, devname, (char *) data, name, len);
360 if (nvals > 1 && !(output & (OUTPUT_VALUE_ONLY | OUTPUT_UDEV_LIST)))
364 fprintf(stderr, "%s: ambivalent result "
365 "(probably more filesystems on the device)\n",
368 return !nvals ? 2 : 0;
371 /* converts comma separated list to BLKID_USAGE_* mask */
372 static int list_to_usage(const char *list, int *flag)
375 const char *word, *p = list;
377 if (p && strncmp(p, "no", 2) == 0) {
378 *flag = BLKID_FLTR_NOTIN;
382 for (word = p; p && *p; p++) {
383 if (*p == ',' || *(p + 1) == '\0') {
384 if (!strncmp(word, "filesystem", 10))
385 mask |= BLKID_USAGE_FILESYSTEM;
386 else if (!strncmp(word, "raid", 4))
387 mask |= BLKID_USAGE_RAID;
388 else if (!strncmp(word, "crypto", 6))
389 mask |= BLKID_USAGE_CRYPTO;
390 else if (!strncmp(word, "other", 5))
391 mask |= BLKID_USAGE_OTHER;
393 fprintf(stderr, "unknown usage keyword '%*s'\n",
394 (int) (p - word), word);
403 int main(int argc, char **argv)
405 blkid_cache cache = NULL;
406 char *devices[128] = { NULL, };
407 char *show[128] = { NULL, };
408 char *search_type = NULL, *search_value = NULL;
412 int fltr_flag = BLKID_FLTR_ONLYIN;
413 unsigned int numdev = 0, numtag = 0;
417 int output_format = 0;
418 int lookup = 0, gc = 0, lowprobe = 0, eval = 0;
420 blkid_loff_t offset = 0, size = 0;
422 while ((c = getopt (argc, argv, "c:f:ghlL:o:O:ps:S:t:u:U:w:v")) != EOF)
425 if (optarg && !*optarg)
434 search_value = strdup(optarg);
435 search_type = strdup("LABEL");
438 fltr_usage = list_to_usage(optarg, &fltr_flag);
442 search_value = strdup(optarg);
443 search_type = strdup("UUID");
452 if (!strcmp(optarg, "value"))
453 output_format = OUTPUT_VALUE_ONLY;
454 else if (!strcmp(optarg, "device"))
455 output_format = OUTPUT_DEVICE_ONLY;
456 else if (!strcmp(optarg, "list"))
457 output_format = OUTPUT_PRETTY_LIST;
458 else if (!strcmp(optarg, "udev"))
459 output_format = OUTPUT_UDEV_LIST;
460 else if (!strcmp(optarg, "full"))
463 fprintf(stderr, "Invalid output format %s. "
464 "Choose from value,\n\t"
465 "device, list, udev or full\n", optarg);
470 offset = strtoll(optarg, NULL, 10);
476 if (numtag >= sizeof(show) / sizeof(*show)) {
477 fprintf(stderr, "Too many tags specified\n");
480 show[numtag++] = optarg;
483 size = strtoll(optarg, NULL, 10);
487 fprintf(stderr, "Can only search for "
488 "one NAME=value pair\n");
491 if (blkid_parse_tag_string(optarg,
494 fprintf(stderr, "-t needs NAME=value pair\n");
502 if (optarg && !*optarg)
513 while (optind < argc)
514 devices[numdev++] = argv[optind++];
517 print_version(stdout);
521 /* convert LABEL/UUID lookup to evaluate request */
522 if (lookup && output_format == OUTPUT_DEVICE_ONLY && search_type &&
523 (!strcmp(search_type, "LABEL") || !strcmp(search_type, "UUID"))) {
528 if (!lowprobe && !eval && blkid_get_cache(&cache, read) < 0)
532 blkid_gc_cache(cache);
538 if (output_format & OUTPUT_PRETTY_LIST)
539 pretty_print_dev(NULL);
548 fprintf(stderr, "The low-probe option requires a device\n");
551 pr = blkid_new_probe();
554 blkid_probe_set_request(pr,
555 BLKID_PROBREQ_LABEL | BLKID_PROBREQ_UUID |
556 BLKID_PROBREQ_TYPE | BLKID_PROBREQ_SECTYPE |
557 BLKID_PROBREQ_USAGE | BLKID_PROBREQ_VERSION);
559 blkid_probe_filter_usage(pr, fltr_flag, fltr_usage))
562 for (i = 0; i < numdev; i++)
563 err = lowprobe_device(pr, devices[i],
564 output_format, offset, size);
565 blkid_free_probe(pr);
570 char *res = blkid_evaluate_tag(search_type, search_value, NULL);
577 * Classic (cache based) API
582 fprintf(stderr, "The lookup option requires a "
583 "search type specified using -t\n");
586 /* Load any additional devices not in the cache */
587 for (i = 0; i < numdev; i++)
588 blkid_get_dev(cache, devices[i], BLKID_DEV_NORMAL);
590 if ((dev = blkid_find_dev_with_tag(cache, search_type,
592 print_tags(dev, show, numtag, output_format);
595 /* If we didn't specify a single device, show all available devices */
596 } else if (!numdev) {
597 blkid_dev_iterate iter;
600 blkid_probe_all(cache);
602 iter = blkid_dev_iterate_begin(cache);
603 blkid_dev_set_search(iter, search_type, search_value);
604 while (blkid_dev_next(iter, &dev) == 0) {
605 dev = blkid_verify(cache, dev);
608 print_tags(dev, show, numtag, output_format);
611 blkid_dev_iterate_end(iter);
612 /* Add all specified devices to cache (optionally display tags) */
613 } else for (i = 0; i < numdev; i++) {
614 blkid_dev dev = blkid_get_dev(cache, devices[i],
619 !blkid_dev_has_tag(dev, search_type,
622 print_tags(dev, show, numtag, output_format);
630 if (!lowprobe && !eval)
631 blkid_put_cache(cache);