2 * find matching entry in fstab and export it
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
28 #include "libudev-private.h"
32 static void log_fn(struct udev *udev, int priority,
33 const char *file, int line, const char *fn,
34 const char *format, va_list args)
37 fprintf(stderr, "%s: ", fn);
38 vfprintf(stderr, format, args);
40 vsyslog(priority, format, args);
44 static int matches_device_list(struct udev *udev, char **devices, const char *name)
48 for (i = 0; devices[i] != NULL; i++) {
49 info(udev, "compare '%s' == '%s'\n", name, devices[i]);
50 if (strcmp(devices[i], name) == 0)
56 static void print_fstab_entry(struct udev *udev, struct mntent *mnt)
58 printf("FSTAB_NAME=%s\n", mnt->mnt_fsname);
59 printf("FSTAB_DIR=%s\n", mnt->mnt_dir);
60 printf("FSTAB_TYPE=%s\n", mnt->mnt_type);
61 printf("FSTAB_OPTS=%s\n", mnt->mnt_opts);
62 printf("FSTAB_FREQ=%d\n", mnt->mnt_freq);
63 printf("FSTAB_PASSNO=%d\n", mnt->mnt_passno);
66 int main(int argc, char *argv[])
69 static const struct option options[] = {
70 { "export", no_argument, NULL, 'x' },
71 { "debug", no_argument, NULL, 'd' },
72 { "help", no_argument, NULL, 'h' },
84 udev_log_init("fstab_id");
85 udev_set_log_fn(udev, log_fn);
90 option = getopt_long(argc, argv, "dxh", options, NULL);
97 if (udev_get_log_priority(udev) < LOG_INFO)
98 udev_set_log_priority(udev, LOG_INFO);
101 printf("Usage: fstab_id [OPTIONS] name [...]\n"
102 " --export print environment keys\n"
103 " --debug debug to stderr\n"
104 " --help print this help text\n\n");
114 devices = &argv[optind];
115 if (devices[0] == NULL) {
116 fprintf(stderr, "error: missing device(s) to match\n");
121 fp = setmntent ("/etc/fstab", "r");
123 fprintf(stderr, "error: opening fstab: %s\n", strerror(errno));
133 info(udev, "found '%s'@'%s'\n", mnt->mnt_fsname, mnt->mnt_dir);
135 /* skip root device */
136 if (strcmp(mnt->mnt_dir, "/") == 0)
140 if (strncmp(mnt->mnt_fsname, "LABEL=", 6) == 0) {
144 label = &mnt->mnt_fsname[6];
145 if (label[0] == '"' || label[0] == '\'') {
148 util_strscpy(str, sizeof(str), &label[1]);
149 pos = strrchr(str, label[0]);
155 if (matches_device_list(udev, devices, str)) {
156 print_fstab_entry(udev, mnt);
164 if (strncmp(mnt->mnt_fsname, "UUID=", 5) == 0) {
168 uuid = &mnt->mnt_fsname[5];
169 if (uuid[0] == '"' || uuid[0] == '\'') {
172 util_strscpy(str, sizeof(str), &uuid[1]);
173 pos = strrchr(str, uuid[0]);
179 if (matches_device_list(udev, devices, str)) {
180 print_fstab_entry(udev, mnt);
188 if (strncmp(mnt->mnt_fsname, udev_get_dev_path(udev), strlen(udev_get_dev_path(udev))) != 0)
191 if (matches_device_list(udev, devices, &mnt->mnt_fsname[strlen(udev_get_dev_path(udev))+1])) {
192 print_fstab_entry(udev, mnt);