From 118d7dacc535fde4e2ed8d996de3919aa0eb2e98 Mon Sep 17 00:00:00 2001 From: Theodore Ts'o Date: Sat, 17 Aug 2002 23:01:22 -0400 Subject: [PATCH] Add support for searching /proc/evms/volumes when interpreting LABEL=xxx or UUID=xxx. Add new utility program /sbin/findfs, which interprets LABEL=xxx or UUID=xxx, and returns the device name. (Part of tune2fs). --- misc/ChangeLog | 12 +++++++ misc/Makefile.in | 7 +++- misc/get_device_by_label.c | 82 +++++++++++++++++++++++++++++++++++++++++----- misc/tune2fs.c | 22 ++++++++++++- 4 files changed, 112 insertions(+), 11 deletions(-) diff --git a/misc/ChangeLog b/misc/ChangeLog index 70a2d7d..607a903 100644 --- a/misc/ChangeLog +++ b/misc/ChangeLog @@ -1,5 +1,17 @@ 2002-08-17 Theodore Ts'o + * findfs.8.in: New man page. + + * tune2fs.c (do_findfs): If tune2fs is executed with argv[0] set + to findfs, then it will interpret argv[1] as one of + LABEL=xxx or UUID=xxx and translate it to a device name. + + * get_device_by_label.c (uuidcache_init): Moved code which + interpreted /proc/partitions into read_partitions(), and + then added support for interpreting /proc/evms/volumes in + read_evms(). uuidcache_init() calls both read_evms() and + read_partitions(). + * mke2fs.8.in: Add a common usage of the -n option. (Addresses Debian Bug #146437) diff --git a/misc/Makefile.in b/misc/Makefile.in index bb48ba1..bc2bb75 100644 --- a/misc/Makefile.in +++ b/misc/Makefile.in @@ -17,7 +17,7 @@ INSTALL = @INSTALL@ SPROGS= mke2fs badblocks tune2fs dumpe2fs $(E2IMAGE_PROG) @FSCK_PROG@ USPROGS= mklost+found SMANPAGES= tune2fs.8 mklost+found.8 mke2fs.8 dumpe2fs.8 badblocks.8 \ - e2label.8 $(E2IMAGE_MAN) @FSCK_MAN@ + e2label.8 findfs.8 $(E2IMAGE_MAN) @FSCK_MAN@ UPROGS= chattr lsattr uuidgen UMANPAGES= chattr.1 lsattr.1 uuidgen.1 @@ -116,6 +116,9 @@ mke2fs.8: $(DEP_SUBSTITUTE) $(srcdir)/mke2fs.8.in e2label.8: $(DEP_SUBSTITUTE) $(srcdir)/e2label.8.in $(SUBSTITUTE) $(srcdir)/e2label.8.in e2label.8 +findfs.8: $(DEP_SUBSTITUTE) $(srcdir)/findfs.8.in + $(SUBSTITUTE) $(srcdir)/findfs.8.in findfs.8 + e2image.8: $(DEP_SUBSTITUTE) $(srcdir)/e2image.8.in $(SUBSTITUTE) $(srcdir)/e2image.8.in e2image.8 @@ -157,6 +160,8 @@ install: all $(SMANPAGES) $(UMANPAGES) installdirs $(DESTDIR)$(root_sbindir)/mkfs.ext3 $(LN) -f $(DESTDIR)$(root_sbindir)/tune2fs \ $(DESTDIR)$(root_sbindir)/e2label + $(LN) -f $(DESTDIR)$(root_sbindir)/tune2fs \ + $(DESTDIR)$(root_sbindir)/findfs for i in $(UPROGS); do \ $(INSTALL_PROGRAM) $$i $(DESTDIR)$(bindir)/$$i; \ $(STRIP) $(DESTDIR)$(bindir)/$$i; \ diff --git a/misc/get_device_by_label.c b/misc/get_device_by_label.c index 3f68dd8..8a80428 100644 --- a/misc/get_device_by_label.c +++ b/misc/get_device_by_label.c @@ -37,6 +37,7 @@ extern char *ext2fs_find_block_device(dev_t device); #define PROC_PARTITIONS "/proc/partitions" +#define PROC_EVMS_VOLUMES "/proc/evms/volumes" #define DEVLABELDIR "/dev" #define VG_DIR "/proc/lvm/VGs" @@ -197,7 +198,8 @@ static void init_lvm(void) #endif static void -uuidcache_init(void) { +read_partitions(void) +{ char line[100]; char *s; int ma, mi, sz; @@ -210,13 +212,6 @@ uuidcache_init(void) { int firstPass; int handleOnFirst; - if (uuidCache) - return; - -#ifdef VG_DIR - init_lvm(); -#endif - procpt = fopen(PROC_PARTITIONS, "r"); if (!procpt) return; @@ -259,6 +254,10 @@ uuidcache_init(void) { devname = string_copy(device); if (!devname) continue; +#ifdef DEBUG + printf("Checking partition %s (%d, %d)\n", + devname, ma, mi); +#endif if (!get_label_uuid(devname, &label, uuid)) uuidcache_addentry(devname, label, uuid); else @@ -270,6 +269,64 @@ uuidcache_init(void) { fclose(procpt); } +static void +read_evms(void) +{ + char line[100]; + char *s; + int ma, mi, sz; + FILE *procpt; + char uuid[16], *label, *devname; + char device[110]; + dev_t dev; + struct stat statbuf; + + procpt = fopen(PROC_EVMS_VOLUMES, "r"); + if (!procpt) + return; + while (fgets(line, sizeof(line), procpt)) { + if (sscanf (line, " %d %d %d %*s %*s %[^\n ]", + &ma, &mi, &sz, device) != 4) + continue; + + /* + * We first look for the device in the named location, + * but if we don't find it, or if the stat information + * doesn't check out, we use ext2fs_find_block_device + * to find it. + */ + dev = makedev(ma, mi); + if ((stat(device, &statbuf) < 0) || (statbuf.st_rdev != dev)) { + devname = ext2fs_find_block_device(dev); + } else + devname = string_copy(device); + if (!devname) + continue; +#ifdef DEBUG + printf("Checking partition %s (%d, %d)\n", + devname, ma, mi); +#endif + if (!get_label_uuid(devname, &label, uuid)) + uuidcache_addentry(devname, label, uuid); + else + free(devname); + } + fclose(procpt); +} + +static void +uuidcache_init(void) +{ + if (uuidCache) + return; + +#ifdef VG_DIR + init_lvm(); +#endif + read_evms(); + read_partitions(); +} + #define UUID 1 #define VOL 2 @@ -328,7 +385,7 @@ get_spec_by_uuid(const char *s) return get_spec_by_x(UUID, uuid); bad_uuid: - fprintf(stderr, _("WARNING: %s: bad UUID"), s); + fprintf(stderr, _("WARNING: %s: bad UUID\n"), s); return NULL; } @@ -371,3 +428,10 @@ char *interpret_spec(char *spec) dev = string_copy(spec); return dev; } + +#ifdef DEBUG +main(int argc, char **argv) +{ + uuidcache_init(); +} +#endif diff --git a/misc/tune2fs.c b/misc/tune2fs.c index 7930c78..a644bb4 100644 --- a/misc/tune2fs.c +++ b/misc/tune2fs.c @@ -627,8 +627,26 @@ static void parse_tune2fs_options(int argc, char **argv) if (!open_flag && !l_flag) usage(); device_name = argv[optind]; -} +} + +do_findfs(int argc, char **argv) +{ + char *dev; + if ((argc != 2) || + (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) { + fprintf(stderr, "Usage: findfs LABEL=