1ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o/*
2ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o * e2label.c		- Print or change the volume label on an ext2 fs
3ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o *
4a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o * Written by Andries Brouwer (aeb@cwi.nl), 970714
5efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o *
6a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o * Copyright 1997, 1998 by Theodore Ts'o.
7ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o *
8a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o * %Begin-Header%
9a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o * This file may be redistributed under the terms of the GNU Public
10a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o * License.
11a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o * %End-Header%
12ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o */
13ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o
14ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o#include <stdio.h>
1580c0fc3492278168448017e79730905aa5b9b62bTheodore Ts'o#include <string.h>
1680c0fc3492278168448017e79730905aa5b9b62bTheodore Ts'o#include <fcntl.h>
1780c0fc3492278168448017e79730905aa5b9b62bTheodore Ts'o#include <ctype.h>
1880c0fc3492278168448017e79730905aa5b9b62bTheodore Ts'o#include <termios.h>
1980c0fc3492278168448017e79730905aa5b9b62bTheodore Ts'o#include <time.h>
2080c0fc3492278168448017e79730905aa5b9b62bTheodore Ts'o#ifdef HAVE_GETOPT_H
2180c0fc3492278168448017e79730905aa5b9b62bTheodore Ts'o#include <getopt.h>
22373b8337c7b6c6243810be250083fa4773891e92Theodore Ts'o#else
23373b8337c7b6c6243810be250083fa4773891e92Theodore Ts'oextern char *optarg;
24373b8337c7b6c6243810be250083fa4773891e92Theodore Ts'oextern int optind;
2580c0fc3492278168448017e79730905aa5b9b62bTheodore Ts'o#endif
2680c0fc3492278168448017e79730905aa5b9b62bTheodore Ts'o#ifdef HAVE_UNISTD_H
2780c0fc3492278168448017e79730905aa5b9b62bTheodore Ts'o#include <unistd.h>
2880c0fc3492278168448017e79730905aa5b9b62bTheodore Ts'o#endif
2980c0fc3492278168448017e79730905aa5b9b62bTheodore Ts'o#ifdef HAVE_STDLIB_H
3080c0fc3492278168448017e79730905aa5b9b62bTheodore Ts'o#include <stdlib.h>
3180c0fc3492278168448017e79730905aa5b9b62bTheodore Ts'o#endif
3280c0fc3492278168448017e79730905aa5b9b62bTheodore Ts'o#ifdef HAVE_ERRNO_H
3380c0fc3492278168448017e79730905aa5b9b62bTheodore Ts'o#include <errno.h>
3480c0fc3492278168448017e79730905aa5b9b62bTheodore Ts'o#endif
35d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o#include "nls-enable.h"
36ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o
37ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o#define EXT2_SUPER_MAGIC 0xEF53
38ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o
39ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o#define VOLNAMSZ 16
40ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o
41ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'ostruct ext2_super_block {
42ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	char  s_dummy0[56];
43ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	unsigned char  s_magic[2];
44ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	char  s_dummy1[62];
45ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	char  s_volume_name[VOLNAMSZ];
46ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	char  s_last_mounted[64];
47ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	char  s_dummy2[824];
48ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o} sb;
49ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o
50a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'ostatic int open_e2fs (char *dev, int mode)
51a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o{
52ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	int fd;
53ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o
54ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	fd = open(dev, mode);
55ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	if (fd < 0) {
56ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	     perror(dev);
57d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	     fprintf (stderr, _("e2label: cannot open %s\n"), dev);
58ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	     exit(1);
59ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	}
60ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	if (lseek(fd, 1024, SEEK_SET) != 1024) {
61ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	     perror(dev);
62d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	     fprintf (stderr, _("e2label: cannot seek to superblock\n"));
63ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	     exit(1);
64ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	}
65ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	if (read(fd, (char *) &sb, sizeof(sb)) != sizeof(sb)) {
66ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	     perror(dev);
67d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	     fprintf (stderr, _("e2label: error reading superblock\n"));
68ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	     exit(1);
69ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	}
70ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	if (sb.s_magic[0] + 256*sb.s_magic[1] != EXT2_SUPER_MAGIC) {
71d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	     fprintf (stderr, _("e2label: not an ext2 filesystem\n"));
72ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	     exit(1);
73ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	}
74ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o
75ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	return fd;
76ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o}
77ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o
78a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'ostatic void print_label (char *dev)
79a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o{
80ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	char label[VOLNAMSZ+1];
81ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o
82ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	open_e2fs (dev, O_RDONLY);
83ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	strncpy(label, sb.s_volume_name, VOLNAMSZ);
84a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o	label[VOLNAMSZ] = 0;
85ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	printf("%s\n", label);
86ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o}
87ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o
88a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'ostatic void change_label (char *dev, char *label)
89a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o{
90ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	int fd;
91ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o
92ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	fd = open_e2fs(dev, O_RDWR);
93ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	memset(sb.s_volume_name, 0, VOLNAMSZ);
94ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	strncpy(sb.s_volume_name, label, VOLNAMSZ);
95a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o	if (strlen(label) > VOLNAMSZ)
96d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o		fprintf(stderr, _("Warning: label too long, truncating.\n"));
97ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	if (lseek(fd, 1024, SEEK_SET) != 1024) {
98ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	     perror(dev);
99d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	     fprintf (stderr, _("e2label: cannot seek to superblock again\n"));
100ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	     exit(1);
101ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	}
102ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	if (write(fd, (char *) &sb, sizeof(sb)) != sizeof(sb)) {
103ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	     perror(dev);
104d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	     fprintf (stderr, _("e2label: error writing superblock\n"));
105ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	     exit(1);
106ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	}
107ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o}
108ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o
109a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'oint main (int argc, char ** argv)
110a789d8406070224503c9ab78040acc7ea80c65aeTheodore Ts'o{
111ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	if (argc == 2)
112ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	     print_label(argv[1]);
113ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	else if (argc == 3)
114ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	     change_label(argv[1], argv[2]);
115ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	else {
116d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	     fprintf(stderr, _("Usage: e2label device [newlabel]\n"));
117ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	     exit(1);
118ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	}
119ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o	return 0;
120ab6b8ab64d5afb5393a9aa826fce26490127f785Theodore Ts'o}
121