blob: e96101aef59455143abccfc5b6c1dfec8d255dda [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
Theodore Ts'oefc6f622008-08-27 23:07:54 -04005 *
Theodore Ts'oa789d841998-03-30 01:20:55 +00006 * 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>
Theodore Ts'o373b8332000-04-03 16:22:35 +000022#else
23extern char *optarg;
24extern int optind;
Theodore Ts'o80c0fc31997-11-03 19:46:49 +000025#endif
26#ifdef HAVE_UNISTD_H
27#include <unistd.h>
28#endif
29#ifdef HAVE_STDLIB_H
30#include <stdlib.h>
31#endif
32#ifdef HAVE_ERRNO_H
33#include <errno.h>
34#endif
Theodore Ts'od9c56d32000-02-08 00:47:55 +000035#include "nls-enable.h"
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +000036
37#define EXT2_SUPER_MAGIC 0xEF53
38
39#define VOLNAMSZ 16
40
41struct ext2_super_block {
42 char s_dummy0[56];
43 unsigned char s_magic[2];
44 char s_dummy1[62];
45 char s_volume_name[VOLNAMSZ];
46 char s_last_mounted[64];
47 char s_dummy2[824];
48} sb;
49
Theodore Ts'oa789d841998-03-30 01:20:55 +000050static int open_e2fs (char *dev, int mode)
51{
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +000052 int fd;
53
54 fd = open(dev, mode);
55 if (fd < 0) {
56 perror(dev);
Theodore Ts'od9c56d32000-02-08 00:47:55 +000057 fprintf (stderr, _("e2label: cannot open %s\n"), dev);
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +000058 exit(1);
59 }
60 if (lseek(fd, 1024, SEEK_SET) != 1024) {
61 perror(dev);
Theodore Ts'od9c56d32000-02-08 00:47:55 +000062 fprintf (stderr, _("e2label: cannot seek to superblock\n"));
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +000063 exit(1);
64 }
65 if (read(fd, (char *) &sb, sizeof(sb)) != sizeof(sb)) {
66 perror(dev);
Theodore Ts'od9c56d32000-02-08 00:47:55 +000067 fprintf (stderr, _("e2label: error reading superblock\n"));
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +000068 exit(1);
69 }
70 if (sb.s_magic[0] + 256*sb.s_magic[1] != EXT2_SUPER_MAGIC) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +000071 fprintf (stderr, _("e2label: not an ext2 filesystem\n"));
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +000072 exit(1);
73 }
74
75 return fd;
76}
77
Theodore Ts'oa789d841998-03-30 01:20:55 +000078static void print_label (char *dev)
79{
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +000080 char label[VOLNAMSZ+1];
81
82 open_e2fs (dev, O_RDONLY);
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +000083 strncpy(label, sb.s_volume_name, VOLNAMSZ);
Theodore Ts'oa789d841998-03-30 01:20:55 +000084 label[VOLNAMSZ] = 0;
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +000085 printf("%s\n", label);
86}
87
Theodore Ts'oa789d841998-03-30 01:20:55 +000088static void change_label (char *dev, char *label)
89{
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +000090 int fd;
91
92 fd = open_e2fs(dev, O_RDWR);
93 memset(sb.s_volume_name, 0, VOLNAMSZ);
94 strncpy(sb.s_volume_name, label, VOLNAMSZ);
Theodore Ts'oa789d841998-03-30 01:20:55 +000095 if (strlen(label) > VOLNAMSZ)
Theodore Ts'od9c56d32000-02-08 00:47:55 +000096 fprintf(stderr, _("Warning: label too long, truncating.\n"));
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +000097 if (lseek(fd, 1024, SEEK_SET) != 1024) {
98 perror(dev);
Theodore Ts'od9c56d32000-02-08 00:47:55 +000099 fprintf (stderr, _("e2label: cannot seek to superblock again\n"));
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +0000100 exit(1);
101 }
102 if (write(fd, (char *) &sb, sizeof(sb)) != sizeof(sb)) {
103 perror(dev);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000104 fprintf (stderr, _("e2label: error writing superblock\n"));
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +0000105 exit(1);
106 }
107}
108
Theodore Ts'oa789d841998-03-30 01:20:55 +0000109int main (int argc, char ** argv)
110{
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +0000111 if (argc == 2)
112 print_label(argv[1]);
113 else if (argc == 3)
114 change_label(argv[1], argv[2]);
115 else {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000116 fprintf(stderr, _("Usage: e2label device [newlabel]\n"));
Theodore Ts'oab6b8ab1997-07-14 19:28:55 +0000117 exit(1);
118 }
119 return 0;
120}