From: Theodore Ts'o Date: Fri, 20 Jul 2001 00:38:53 +0000 (-0400) Subject: fstype.c (identify_fs): New file which looks at the superblock X-Git-Tag: E2FSPROGS-1_23-WIP-0720~7 X-Git-Url: https://git.whamcloud.com/?a=commitdiff_plain;h=da376f5a13af0648159c575cf50bf78df34056f9;p=tools%2Fe2fsprogs.git fstype.c (identify_fs): New file which looks at the superblock of the filesystem to determines its type. fsck.c (fsck_device, ignore, interpret_type): Call identify_fs if the type specified in /etc/fstab is "auto". --- diff --git a/misc/ChangeLog b/misc/ChangeLog index dd6f113..9919f38 100644 --- a/misc/ChangeLog +++ b/misc/ChangeLog @@ -1,3 +1,11 @@ +2001-07-19 Theodore Tso + + * fstype.c (identify_fs): New file which looks at the superblock + of the filesystem to determines its type. + + * fsck.c (fsck_device, ignore, interpret_type): Call identify_fs + if the type specified in /etc/fstab is "auto". + 2001-06-23 Theodore Tso * Release of E2fsprogs 1.22 diff --git a/misc/Makefile.in b/misc/Makefile.in index 971ee95..d4421fc 100644 --- a/misc/Makefile.in +++ b/misc/Makefile.in @@ -31,12 +31,12 @@ UUIDGEN_OBJS= uuidgen.o DUMPE2FS_OBJS= dumpe2fs.o BADBLOCKS_OBJS= badblocks.o E2IMAGE_OBJS= e2image.o -FSCK_OBJS= fsck.o get_device_by_label.o base_device.o +FSCK_OBJS= fsck.o get_device_by_label.o base_device.o fstype.o SRCS= $(srcdir)/tune2fs.c $(srcdir)/mklost+found.c $(srcdir)/mke2fs.c \ $(srcdir)/chattr.c $(srcdir)/lsattr.c $(srcdir)/dumpe2fs.c \ $(srcdir)/badblocks.c $(srcdir)/fsck.c $(srcdir)/util.c \ - $(srcdir)/uuidgen.c + $(srcdir)/uuidgen.c $(srcdir)/fstype.c LIBS= $(LIBEXT2FS) $(LIBCOM_ERR) DEPLIBS= $(LIBEXT2FS) $(LIBCOM_ERR) diff --git a/misc/fsck.c b/misc/fsck.c index 17ac42d..6eefd55 100644 --- a/misc/fsck.c +++ b/misc/fsck.c @@ -252,6 +252,26 @@ static char *interpret_device(char *spec) } /* + * Interpret filesystem auto type if necessary + */ +static void interpret_type(struct fs_info *fs) +{ + const char *type; + + if (strcmp(fs->type, "auto") == 0) { + type = identify_fs(fs->device); + if (type) { + free(fs->type); + fs->type = string_copy(type); + } else + fprintf(stderr, _("Could not determine " + "filesystem type for %s\n"), + fs->device); + } +} + + +/* * Load the filesystem database from /etc/fstab */ static void load_fs_info(const char *filename) @@ -598,6 +618,7 @@ static void fsck_device(char *device, int interactive) if ((fsent = lookup(device))) { device = fsent->device; + interpret_type(fsent); if (!type) type = fsent->type; } @@ -764,6 +785,8 @@ static int ignore(struct fs_info *fs) if (fs->passno == 0) return 1; + interpret_type(fs); + /* * If a specific fstype is specified, and it doesn't match, * ignore it. diff --git a/misc/fsck.h b/misc/fsck.h index 9d9f6ea..71eb011 100644 --- a/misc/fsck.h +++ b/misc/fsck.h @@ -61,3 +61,4 @@ struct fsck_instance { extern char *base_device(char *device); extern char *string_copy(const char *s); +extern const char *identify_fs(const char *fs_name); diff --git a/misc/fstype.c b/misc/fstype.c new file mode 100644 index 0000000..31f3027 --- /dev/null +++ b/misc/fstype.c @@ -0,0 +1,72 @@ +/* + * fstype.c + * + * Copyright (C) 2001 Theodore Ts'o. + * + * %Begin-Header% + * This file may be redistributed under the terms of the GNU Public + * License. + * %End-Header% + */ + +#include +#if HAVE_UNISTD_H +#include +#endif +#include +#include + +struct fs_magic { + const char *fs_name; + int offset; + int len; + char *magic; +}; + +struct fs_magic type_array[] = { + { "ext2", 1024+56, 2, "\123\357" }, + { "minix", 1040, 2, "\177\023" }, + { "minix", 1040, 2, "\217\023" }, + { "minix", 1040, 2, "\150\044" }, + { "minix", 1040, 2, "\170\044" }, + { "xfs", 0, 4, "XFSB" }, + { 0, 0, 0, 0 } +}; + +const char *identify_fs(const char *fs_name) +{ + char buf[2048]; + struct fs_magic *p; + int fd; + + fd = open(fs_name, O_RDONLY); + if (fd < 0) + return NULL; + if (lseek(fd, 0, SEEK_SET) < 0) + return NULL; + if (read(fd, buf, sizeof(buf)) != sizeof(buf)) + return NULL; + for (p = type_array; p->fs_name; p++) { + if (memcmp(p->magic, buf+p->offset, p->len) == 0) + return p->fs_name; + } + return NULL; +} + +#ifdef TEST_PROGRAM +int main(int argc, char **argv) +{ + const char *type; + + if (argc != 2) { + fprintf(stderr, "Usage: %s device\n", argv[0]); + exit(1); + } + type = identify_fs(argv[1]); + printf("%s is a %s filesystem\n", argv[1], type); + return (0); +} +#endif + + +