diff options
author | kay.sievers@vrfy.org <kay.sievers@vrfy.org> | 2004-04-30 23:26:33 -0700 |
---|---|---|
committer | Greg KH <gregkh@suse.de> | 2005-04-26 21:35:17 -0700 |
commit | 3611d5820c0fc0863d15c04884b05c9e04492f45 (patch) | |
tree | 594698488dc499c4ba88afa07087d7c03efa93de /extras/volume_id/udev_volume_id.c | |
parent | e13fa59953d64e478025fd1200bb5fd0baf671b2 (diff) |
[PATCH] udev callout for reading filesystem labels
here is a small udev toy, which enables udev to name partitions by
its filesystem label or uuid's.
The following udev rule:
KERNEL="sd*", PROGRAM="/sbin/udev_volume_id -M%M -m%m -u", SYMLINK="%c"
creates a symlink with the uuid read from the filesystem. If no label or
uuid is found the program exits with nonzero and the rule will fail.
ext2, ext3, reiserfs, xfs, jfs, vfat, msdos volume labels are supported,
ntfs and swap partitions can be recognized.
It's possible to compile with klibc and the static binary takes 13kb.
Diffstat (limited to 'extras/volume_id/udev_volume_id.c')
-rw-r--r-- | extras/volume_id/udev_volume_id.c | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/extras/volume_id/udev_volume_id.c b/extras/volume_id/udev_volume_id.c new file mode 100644 index 0000000000..88779ccd36 --- /dev/null +++ b/extras/volume_id/udev_volume_id.c @@ -0,0 +1,120 @@ +/* + * udev_volume_id - udev callout to read filesystem label and uuid + * + * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org> + * + * sample udev rule for creation of a symlink with the filsystem uuid: + * KERNEL="sd*", PROGRAM="/sbin/udev_volume_id -M%M -m%m -u", SYMLINK="%c" + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include "volume_id.h" + +int main(int argc, char *argv[]) +{ + struct volume_id *vid; + const char help[] = "usage: udev_volume_id -m<minor> -M<major> [-t|-l|-u]\n"; + int major = -1; + int minor = -1; + char *tail; + static const char short_options[] = "M:m:htlu"; + int option; + char print = '\0'; + int rc; + + + while (1) { + option = getopt(argc, argv, short_options); + if (option == -1) + break; + + switch (option) { + case 'M': + major = (int) strtoul(optarg, &tail, 10); + if (tail[0] != '\0') { + printf("invalid major\n"); + exit(1); + } + break; + case 'm': + minor = (int) strtoul(optarg, &tail, 10); + if (tail[0] != '\0') { + printf("invalid minor\n"); + exit(1); + } + break; + case 't': + print = 't'; + break; + case 'l': + print = 'l'; + break; + case 'u': + print = 'u'; + break; + case 'h': + case '?': + default: + printf(help); + exit(1); + } + } + + if (major == -1 || minor == -1) { + printf(help); + exit(1); + } + + vid = volume_id_open_dev_t(makedev(major, minor)); + if (vid == NULL) { + printf("error open volume\n"); + exit(1); + } + + rc = volume_id_probe(vid, ALL); + if (rc != 0) { + printf("error probing volume\n"); + exit(1); + } + + switch (print) { + case 't': + printf("%s\n", vid->fs_name); + break; + case 'l': + if (vid->label_string[0] == '\0') + exit(2); + printf("%s\n", vid->label_string); + break; + case 'u': + if (vid->uuid_string[0] == '\0') + exit(2); + printf("%s\n", vid->uuid_string); + break; + default: + printf("T:%s\n", vid->fs_name); + printf("L:%s\n", vid->label_string); + printf("U:%s\n", vid->uuid_string); + } + + volume_id_close(vid); + + exit(0); +} |