Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * e2label.c - Print or change the volume label on an ext2 fs |
| 3 | * |
Theodore Ts'o | a789d84 | 1998-03-30 01:20:55 +0000 | [diff] [blame] | 4 | * Written by Andries Brouwer (aeb@cwi.nl), 970714 |
| 5 | * |
| 6 | * Copyright 1997, 1998 by Theodore Ts'o. |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 7 | * |
Theodore Ts'o | a789d84 | 1998-03-30 01:20:55 +0000 | [diff] [blame] | 8 | * %Begin-Header% |
| 9 | * This file may be redistributed under the terms of the GNU Public |
| 10 | * License. |
| 11 | * %End-Header% |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 14 | #include <stdio.h> |
Theodore Ts'o | 80c0fc3 | 1997-11-03 19:46:49 +0000 | [diff] [blame] | 15 | #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'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 32 | |
| 33 | #define EXT2_SUPER_MAGIC 0xEF53 |
| 34 | |
| 35 | #define VOLNAMSZ 16 |
| 36 | |
| 37 | struct 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'o | a789d84 | 1998-03-30 01:20:55 +0000 | [diff] [blame] | 46 | static int open_e2fs (char *dev, int mode) |
| 47 | { |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 48 | 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'o | a789d84 | 1998-03-30 01:20:55 +0000 | [diff] [blame] | 74 | static void print_label (char *dev) |
| 75 | { |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 76 | char label[VOLNAMSZ+1]; |
| 77 | |
| 78 | open_e2fs (dev, O_RDONLY); |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 79 | strncpy(label, sb.s_volume_name, VOLNAMSZ); |
Theodore Ts'o | a789d84 | 1998-03-30 01:20:55 +0000 | [diff] [blame] | 80 | label[VOLNAMSZ] = 0; |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 81 | printf("%s\n", label); |
| 82 | } |
| 83 | |
Theodore Ts'o | a789d84 | 1998-03-30 01:20:55 +0000 | [diff] [blame] | 84 | static void change_label (char *dev, char *label) |
| 85 | { |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 86 | 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'o | a789d84 | 1998-03-30 01:20:55 +0000 | [diff] [blame] | 91 | if (strlen(label) > VOLNAMSZ) |
| 92 | fprintf(stderr, "Warning: label too long, truncating.\n"); |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 93 | 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'o | a789d84 | 1998-03-30 01:20:55 +0000 | [diff] [blame] | 105 | int main (int argc, char ** argv) |
| 106 | { |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 107 | 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 | } |