2 * e2label.c - Print or change the volume label on an ext2 fs
4 * Written by Andries Brouwer (aeb@cwi.nl), 970714
6 * Copyright 1997, 1998 by Theodore Ts'o.
9 * This file may be redistributed under the terms of the GNU Public
36 #include "nls-enable.h"
38 #define EXT2_SUPER_MAGIC 0xEF53
42 struct ext2_super_block {
44 unsigned char s_magic[2];
46 char s_volume_name[VOLNAMSZ];
47 char s_last_mounted[64];
51 static int open_e2fs (char *dev, int mode)
58 fprintf (stderr, _("e2label: cannot open %s\n"), dev);
61 if (lseek(fd, 1024, SEEK_SET) != 1024) {
63 fprintf (stderr, _("e2label: cannot seek to superblock\n"));
66 if (read(fd, (char *) &sb, sizeof(sb)) != sizeof(sb)) {
68 fprintf (stderr, _("e2label: error reading superblock\n"));
71 if (sb.s_magic[0] + 256*sb.s_magic[1] != EXT2_SUPER_MAGIC) {
72 fprintf (stderr, _("e2label: not an ext2 filesystem\n"));
79 static void print_label (char *dev)
81 char label[VOLNAMSZ+1];
83 open_e2fs (dev, O_RDONLY);
84 strncpy(label, sb.s_volume_name, VOLNAMSZ);
86 printf("%s\n", label);
89 static void change_label (char *dev, char *label)
93 fd = open_e2fs(dev, O_RDWR);
94 memset(sb.s_volume_name, 0, VOLNAMSZ);
95 strncpy(sb.s_volume_name, label, VOLNAMSZ);
96 if (strlen(label) > VOLNAMSZ)
97 fprintf(stderr, _("Warning: label too long, truncating.\n"));
98 if (lseek(fd, 1024, SEEK_SET) != 1024) {
100 fprintf (stderr, _("e2label: cannot seek to superblock again\n"));
103 if (write(fd, (char *) &sb, sizeof(sb)) != sizeof(sb)) {
105 fprintf (stderr, _("e2label: error writing superblock\n"));
110 int main (int argc, char ** argv)
113 print_label(argv[1]);
115 change_label(argv[1], argv[2]);
117 fprintf(stderr, _("Usage: e2label device [newlabel]\n"));