From ab6b8ab64d5afb5393a9aa826fce26490127f785 Mon Sep 17 00:00:00 2001 From: Theodore Ts'o Date: Mon, 14 Jul 1997 19:28:55 +0000 Subject: [PATCH] ChangeLog, Makefile.in, e2label.c: e2label.c: New file contributed by Andries Brouwer which provides an easy-to-use interface to modify the filesystem label. --- misc/ChangeLog | 8 +++++ misc/Makefile.in | 6 +++- misc/e2label.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 misc/e2label.c diff --git a/misc/ChangeLog b/misc/ChangeLog index 37a9dec..e98d756 100644 --- a/misc/ChangeLog +++ b/misc/ChangeLog @@ -1,3 +1,11 @@ +Mon Jul 14 15:27:29 1997 Theodore Y. Ts'o + + * e2label.c: New file contributed by Andries Brouwer which + provides an easy-to-use interface to modify the filesystem + label. + + * Makefile.in (SPROGS): Add Makefile support to build e2label + Tue Jun 17 01:33:20 1997 Theodore Ts'o * Release of E2fsprogs 1.11 diff --git a/misc/Makefile.in b/misc/Makefile.in index b0d06a2..0c3126a 100644 --- a/misc/Makefile.in +++ b/misc/Makefile.in @@ -11,7 +11,7 @@ INSTALL = @INSTALL@ @MCONFIG@ -SPROGS= mke2fs badblocks tune2fs dumpe2fs @FSCK_PROG@ +SPROGS= mke2fs badblocks tune2fs dumpe2fs e2label @FSCK_PROG@ USPROGS= mklost+found SMANPAGES= tune2fs.8 mklost+found.8 mke2fs.8 dumpe2fs.8 badblocks.8 \ @FSCK_MAN@ @@ -26,6 +26,7 @@ CHATTR_OBJS= chattr.o LSATTR_OBJS= lsattr.o DUMPE2FS_OBJS= dumpe2fs.o BADBLOCKS_OBJS= badblocks.o +E2LABEL_OBJS= e2label.o FSCK_OBJS= fsck.o SRCS= $(srcdir)/tune2fs.c $(srcdir)/mklost+found.c $(srcdir)/mke2fs.c \ @@ -49,6 +50,9 @@ all:: $(SPROGS) $(UPROGS) $(USPROGS) fix_substitute $(SMANPAGES) $(UMANPAGES) tune2fs: $(TUNE2FS_OBJS) $(DEPLIBS_E2P) $(LIBUUID) $(CC) $(ALL_LDFLAGS) -o tune2fs $(TUNE2FS_OBJS) $(LIBS_E2P) $(LIBUUID) +e2label: $(E2LABEL_OBJS) + $(CC) $(ALL_LDFLAGS) -o e2label $(E2LABEL_OBJS) + mklost+found: $(MKLPF_OBJS) $(CC) $(ALL_LDFLAGS) -o mklost+found $(MKLPF_OBJS) diff --git a/misc/e2label.c b/misc/e2label.c new file mode 100644 index 0000000..2b4f171 --- /dev/null +++ b/misc/e2label.c @@ -0,0 +1,92 @@ +/* + * e2label.c - Print or change the volume label on an ext2 fs + * + * aeb, 970714 + * + */ + +#include +#include + +#define EXT2_SUPER_MAGIC 0xEF53 + +#define VOLNAMSZ 16 + +struct ext2_super_block { + char s_dummy0[56]; + unsigned char s_magic[2]; + char s_dummy1[62]; + char s_volume_name[VOLNAMSZ]; + char s_last_mounted[64]; + char s_dummy2[824]; +} sb; + +int +open_e2fs (char *dev, int mode) { + int fd; + + fd = open(dev, mode); + if (fd < 0) { + perror(dev); + fprintf (stderr, "e2label: cannot open %s\n", dev); + exit(1); + } + if (lseek(fd, 1024, SEEK_SET) != 1024) { + perror(dev); + fprintf (stderr, "e2label: cannot seek to superblock\n"); + exit(1); + } + if (read(fd, (char *) &sb, sizeof(sb)) != sizeof(sb)) { + perror(dev); + fprintf (stderr, "e2label: error reading superblock\n"); + exit(1); + } + if (sb.s_magic[0] + 256*sb.s_magic[1] != EXT2_SUPER_MAGIC) { + fprintf (stderr, "e2label: not an ext2 filesystem\n"); + exit(1); + } + + return fd; +} + +void +print_label (char *dev) { + char label[VOLNAMSZ+1]; + + open_e2fs (dev, O_RDONLY); + label[VOLNAMSZ] = 0; + strncpy(label, sb.s_volume_name, VOLNAMSZ); + printf("%s\n", label); +} + +void +change_label (char *dev, char *label) { + int fd; + + fd = open_e2fs(dev, O_RDWR); + memset(sb.s_volume_name, 0, VOLNAMSZ); + strncpy(sb.s_volume_name, label, VOLNAMSZ); + if (lseek(fd, 1024, SEEK_SET) != 1024) { + perror(dev); + fprintf (stderr, "e2label: cannot seek to superblock again\n"); + exit(1); + } + if (write(fd, (char *) &sb, sizeof(sb)) != sizeof(sb)) { + perror(dev); + fprintf (stderr, "e2label: error writing superblock\n"); + exit(1); + } +} + +int +main (int argc, char ** argv) { + if (argc == 2) + print_label(argv[1]); + else if (argc == 3) + change_label(argv[1], argv[2]); + else { + fprintf(stderr, "Usage: e2label device [newlabel]\n"); + exit(1); + } + return 0; +} -- 1.8.3.1