blob: 8c0df8b2af5528d8dba4ea340140e760580024a0 [file] [log] [blame]
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +00001/*
2 * e2label.c - Print or change the volume label on an ext2 fs
3 *
Theodore Ts'oa789d841998-03-30 01:20:55 +00004 * Written by Andries Brouwer (aeb@cwi.nl), 970714
5 *
6 * Copyright 1997, 1998 by Theodore Ts'o.
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +00007 *
Theodore Ts'oa789d841998-03-30 01:20:55 +00008 * %Begin-Header%
9 * This file may be redistributed under the terms of the GNU Public
10 * License.
11 * %End-Header%
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +000012 */
13
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +000014#include <stdio.h>
Theodore Ts'o80c0fc31997-11-03 19:46:49 +000015#include <string.h>
16#include <fcntl.h>
17#include <ctype.h>
18#include <termios.h>
19#include <time.h>
20#ifdef HAVE_GETOPT_H
21#include <getopt.h>
22#endif
23#ifdef HAVE_UNISTD_H
24#include <unistd.h>
25#endif
26#ifdef HAVE_STDLIB_H
27#include <stdlib.h>
28#endif
29#ifdef HAVE_ERRNO_H
30#include <errno.h>
31#endif
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +000032
33#define EXT2_SUPER_MAGIC 0xEF53
34
35#define VOLNAMSZ 16
36
37struct ext2_super_block {
38 char s_dummy0[56];
39 unsigned char s_magic[2];
40 char s_dummy1[62];
41 char s_volume_name[VOLNAMSZ];
42 char s_last_mounted[64];
43 char s_dummy2[824];
44} sb;
45
Theodore Ts'oa789d841998-03-30 01:20:55 +000046static int open_e2fs (char *dev, int mode)
47{
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +000048 int fd;
49
50 fd = open(dev, mode);
51 if (fd < 0) {
52 perror(dev);
53 fprintf (stderr, "e2label: cannot open %s\n", dev);
54 exit(1);
55 }
56 if (lseek(fd, 1024, SEEK_SET) != 1024) {
57 perror(dev);
58 fprintf (stderr, "e2label: cannot seek to superblock\n");
59 exit(1);
60 }
61 if (read(fd, (char *) &sb, sizeof(sb)) != sizeof(sb)) {
62 perror(dev);
63 fprintf (stderr, "e2label: error reading superblock\n");
64 exit(1);
65 }
66 if (sb.s_magic[0] + 256*sb.s_magic[1] != EXT2_SUPER_MAGIC) {
67 fprintf (stderr, "e2label: not an ext2 filesystem\n");
68 exit(1);
69 }
70
71 return fd;
72}
73
Theodore Ts'oa789d841998-03-30 01:20:55 +000074static void print_label (char *dev)
75{
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +000076 char label[VOLNAMSZ+1];
77
78 open_e2fs (dev, O_RDONLY);
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +000079 strncpy(label, sb.s_volume_name, VOLNAMSZ);
Theodore Ts'oa789d841998-03-30 01:20:55 +000080 label[VOLNAMSZ] = 0;
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +000081 printf("%s\n", label);
82}
83
Theodore Ts'oa789d841998-03-30 01:20:55 +000084static void change_label (char *dev, char *label)
85{
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +000086 int fd;
87
88 fd = open_e2fs(dev, O_RDWR);
89 memset(sb.s_volume_name, 0, VOLNAMSZ);
90 strncpy(sb.s_volume_name, label, VOLNAMSZ);
Theodore Ts'oa789d841998-03-30 01:20:55 +000091 if (strlen(label) > VOLNAMSZ)
92 fprintf(stderr, "Warning: label too long, truncating.\n");
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +000093 if (lseek(fd, 1024, SEEK_SET) != 1024) {
94 perror(dev);
95 fprintf (stderr, "e2label: cannot seek to superblock again\n");
96 exit(1);
97 }
98 if (write(fd, (char *) &sb, sizeof(sb)) != sizeof(sb)) {
99 perror(dev);
100 fprintf (stderr, "e2label: error writing superblock\n");
101 exit(1);
102 }
103}
104
Theodore Ts'oa789d841998-03-30 01:20:55 +0000105int main (int argc, char ** argv)
106{
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +0000107 if (argc == 2)
108 print_label(argv[1]);
109 else if (argc == 3)
110 change_label(argv[1], argv[2]);
111 else {
112 fprintf(stderr, "Usage: e2label device [newlabel]\n");
113 exit(1);
114 }
115 return 0;
116}