Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * main.c --- ext2 resizer main program |
| 3 | * |
Theodore Ts'o | 0cee8a5 | 2000-04-06 21:38:34 +0000 | [diff] [blame] | 4 | * Copyright (C) 1997, 1998 by Theodore Ts'o and |
| 5 | * PowerQuest, Inc. |
| 6 | * |
Theodore Ts'o | e5b38a5 | 2001-01-01 16:17:12 +0000 | [diff] [blame] | 7 | * Copyright (C) 1999, 2000, 2001 by Theosore Ts'o |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 8 | * |
| 9 | * %Begin-Header% |
Theodore Ts'o | 0cee8a5 | 2000-04-06 21:38:34 +0000 | [diff] [blame] | 10 | * This file may be redistributed under the terms of the GNU Public |
| 11 | * License. |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 12 | * %End-Header% |
| 13 | */ |
| 14 | |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 15 | #ifdef HAVE_GETOPT_H |
| 16 | #include <getopt.h> |
Theodore Ts'o | 373b833 | 2000-04-03 16:22:35 +0000 | [diff] [blame] | 17 | #else |
| 18 | extern char *optarg; |
| 19 | extern int optind; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 20 | #endif |
| 21 | #include <fcntl.h> |
Theodore Ts'o | 116db1b | 2002-04-01 01:28:30 -0500 | [diff] [blame^] | 22 | #include <sys/stat.h> |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 23 | |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 24 | #include "resize2fs.h" |
| 25 | |
Theodore Ts'o | 0cee8a5 | 2000-04-06 21:38:34 +0000 | [diff] [blame] | 26 | #include "../version.h" |
Theodore Ts'o | 2e561e0 | 1998-04-08 06:18:37 +0000 | [diff] [blame] | 27 | |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 28 | char *program_name, *device_name; |
| 29 | |
Theodore Ts'o | dfcdc32 | 2001-01-11 15:38:00 +0000 | [diff] [blame] | 30 | static void usage (char *prog) |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 31 | { |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 32 | fprintf (stderr, _("usage: %s [-d debug_flags] [-f] [-F] [-p] device [new-size]\n\n"), prog); |
Theodore Ts'o | 2e561e0 | 1998-04-08 06:18:37 +0000 | [diff] [blame] | 33 | |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 34 | exit (1); |
| 35 | } |
| 36 | |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 37 | static errcode_t resize_progress_func(ext2_resize_t rfs, int pass, |
| 38 | unsigned long cur, unsigned long max) |
Theodore Ts'o | 63b44fb | 1998-02-13 22:58:18 +0000 | [diff] [blame] | 39 | { |
| 40 | ext2_sim_progmeter progress; |
| 41 | const char *label; |
| 42 | errcode_t retval; |
| 43 | |
| 44 | progress = (ext2_sim_progmeter) rfs->prog_data; |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 45 | if (max == 0) |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 46 | return 0; |
Theodore Ts'o | 63b44fb | 1998-02-13 22:58:18 +0000 | [diff] [blame] | 47 | if (cur == 0) { |
| 48 | if (progress) |
| 49 | ext2fs_progress_close(progress); |
| 50 | progress = 0; |
| 51 | switch (pass) { |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 52 | case E2_RSZ_EXTEND_ITABLE_PASS: |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 53 | label = _("Extending the inode table"); |
Theodore Ts'o | 63b44fb | 1998-02-13 22:58:18 +0000 | [diff] [blame] | 54 | break; |
| 55 | case E2_RSZ_BLOCK_RELOC_PASS: |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 56 | label = _("Relocating blocks"); |
Theodore Ts'o | 63b44fb | 1998-02-13 22:58:18 +0000 | [diff] [blame] | 57 | break; |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 58 | case E2_RSZ_INODE_SCAN_PASS: |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 59 | label = _("Scanning inode table"); |
Theodore Ts'o | 63b44fb | 1998-02-13 22:58:18 +0000 | [diff] [blame] | 60 | break; |
| 61 | case E2_RSZ_INODE_REF_UPD_PASS: |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 62 | label = _("Updating inode references"); |
Theodore Ts'o | 63b44fb | 1998-02-13 22:58:18 +0000 | [diff] [blame] | 63 | break; |
| 64 | case E2_RSZ_MOVE_ITABLE_PASS: |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 65 | label = _("Moving inode table"); |
Theodore Ts'o | 63b44fb | 1998-02-13 22:58:18 +0000 | [diff] [blame] | 66 | break; |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 67 | default: |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 68 | label = _("Unknown pass?!?"); |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 69 | break; |
Theodore Ts'o | 63b44fb | 1998-02-13 22:58:18 +0000 | [diff] [blame] | 70 | } |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 71 | printf(_("Begin pass %d (max = %lu)\n"), pass, max); |
Theodore Ts'o | 63b44fb | 1998-02-13 22:58:18 +0000 | [diff] [blame] | 72 | retval = ext2fs_progress_init(&progress, label, 30, |
| 73 | 40, max, 0); |
| 74 | if (retval) |
| 75 | progress = 0; |
| 76 | rfs->prog_data = (void *) progress; |
| 77 | } |
| 78 | if (progress) |
| 79 | ext2fs_progress_update(progress, cur); |
| 80 | if (cur >= max) { |
| 81 | if (progress) |
| 82 | ext2fs_progress_close(progress); |
| 83 | progress = 0; |
| 84 | rfs->prog_data = 0; |
| 85 | } |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 86 | return 0; |
Theodore Ts'o | 63b44fb | 1998-02-13 22:58:18 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Theodore Ts'o | 2a3013b | 1998-03-30 01:08:41 +0000 | [diff] [blame] | 89 | static void check_mount(char *device) |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 90 | { |
| 91 | errcode_t retval; |
| 92 | int mount_flags; |
| 93 | |
Theodore Ts'o | 2a3013b | 1998-03-30 01:08:41 +0000 | [diff] [blame] | 94 | retval = ext2fs_check_if_mounted(device, &mount_flags); |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 95 | if (retval) { |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 96 | com_err(_("ext2fs_check_if_mount"), retval, |
| 97 | _("while determining whether %s is mounted."), |
Theodore Ts'o | 2a3013b | 1998-03-30 01:08:41 +0000 | [diff] [blame] | 98 | device); |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 99 | return; |
| 100 | } |
| 101 | if (!(mount_flags & EXT2_MF_MOUNTED)) |
| 102 | return; |
| 103 | |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 104 | fprintf(stderr, _("%s is mounted; can't resize a " |
| 105 | "mounted filesystem!\n\n"), device); |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 106 | exit(1); |
| 107 | } |
| 108 | |
| 109 | |
Theodore Ts'o | 0a61731 | 2000-02-02 19:14:36 +0000 | [diff] [blame] | 110 | int main (int argc, char ** argv) |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 111 | { |
| 112 | errcode_t retval; |
| 113 | ext2_filsys fs; |
| 114 | int c; |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 115 | int flags = 0; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 116 | int flush = 0; |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 117 | int force = 0; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 118 | int fd; |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 119 | blk_t new_size = 0; |
| 120 | blk_t max_size = 0; |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 121 | io_manager io_ptr; |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 122 | char *tmp; |
Theodore Ts'o | 116db1b | 2002-04-01 01:28:30 -0500 | [diff] [blame^] | 123 | struct stat st_buf; |
Theodore Ts'o | 101c84f | 1998-03-24 16:27:11 +0000 | [diff] [blame] | 124 | |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 125 | initialize_ext2_error_table(); |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 126 | |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 127 | fprintf (stderr, _("resize2fs %s (%s)\n"), |
Theodore Ts'o | ba0af75 | 1998-03-09 17:41:53 +0000 | [diff] [blame] | 128 | E2FSPROGS_VERSION, E2FSPROGS_DATE); |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 129 | if (argc && *argv) |
| 130 | program_name = *argv; |
Theodore Ts'o | 2e561e0 | 1998-04-08 06:18:37 +0000 | [diff] [blame] | 131 | |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 132 | while ((c = getopt (argc, argv, "d:fFhp")) != EOF) { |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 133 | switch (c) { |
| 134 | case 'h': |
| 135 | usage(program_name); |
| 136 | break; |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 137 | case 'f': |
| 138 | force = 1; |
| 139 | break; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 140 | case 'F': |
| 141 | flush = 1; |
| 142 | break; |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 143 | case 'd': |
| 144 | flags |= atoi(optarg); |
| 145 | break; |
| 146 | case 'p': |
| 147 | flags |= RESIZE_PERCENT_COMPLETE; |
| 148 | break; |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 149 | default: |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 150 | usage(program_name); |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 153 | if (optind == argc) |
| 154 | usage(program_name); |
Theodore Ts'o | 2e561e0 | 1998-04-08 06:18:37 +0000 | [diff] [blame] | 155 | |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 156 | device_name = argv[optind++]; |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 157 | if (optind < argc) { |
| 158 | new_size = strtoul(argv[optind++], &tmp, 0); |
| 159 | if (*tmp) { |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 160 | com_err(program_name, 0, _("bad filesystem size - %s"), |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 161 | argv[optind - 1]); |
| 162 | exit(1); |
| 163 | } |
| 164 | } |
| 165 | if (optind < argc) |
| 166 | usage(program_name); |
| 167 | |
| 168 | check_mount(device_name); |
| 169 | |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 170 | if (flush) { |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 171 | fd = open(device_name, O_RDONLY, 0); |
| 172 | |
| 173 | if (fd < 0) { |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 174 | com_err("open", errno, |
| 175 | _("while opening %s for flushing"), |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 176 | device_name); |
| 177 | exit(1); |
| 178 | } |
Theodore Ts'o | 48e08e0 | 2001-01-11 16:11:11 +0000 | [diff] [blame] | 179 | retval = ext2fs_sync_device(fd, 1); |
| 180 | if (retval) { |
| 181 | com_err(argv[0], retval, |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 182 | _("while trying to flush %s"), |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 183 | device_name); |
| 184 | exit(1); |
| 185 | } |
| 186 | close(fd); |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 189 | if (flags & RESIZE_DEBUG_IO) { |
| 190 | io_ptr = test_io_manager; |
| 191 | test_io_backing_manager = unix_io_manager; |
| 192 | } else |
| 193 | io_ptr = unix_io_manager; |
| 194 | |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 195 | retval = ext2fs_open (device_name, EXT2_FLAG_RW, 0, 0, |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 196 | io_ptr, &fs); |
| 197 | if (retval) { |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 198 | com_err (program_name, retval, _("while trying to open %s"), |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 199 | device_name); |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 200 | printf (_("Couldn't find valid filesystem superblock.\n")); |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 201 | exit (1); |
| 202 | } |
Theodore Ts'o | 101c84f | 1998-03-24 16:27:11 +0000 | [diff] [blame] | 203 | /* |
| 204 | * Check for compatibility with the feature sets. We need to |
| 205 | * be more stringent than ext2fs_open(). |
| 206 | */ |
Theodore Ts'o | e5b38a5 | 2001-01-01 16:17:12 +0000 | [diff] [blame] | 207 | if ((fs->super->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) || |
| 208 | (fs->super->s_feature_incompat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) { |
Theodore Ts'o | 101c84f | 1998-03-24 16:27:11 +0000 | [diff] [blame] | 209 | com_err(program_name, EXT2_ET_UNSUPP_FEATURE, |
| 210 | "(%s)", device_name); |
| 211 | exit(1); |
| 212 | } |
| 213 | |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 214 | /* |
| 215 | * Get the size of the containing partition, and use this for |
| 216 | * defaults and for making sure the new filesystme doesn't |
| 217 | * exceed the partition size. |
| 218 | */ |
| 219 | retval = ext2fs_get_device_size(device_name, fs->blocksize, |
| 220 | &max_size); |
| 221 | if (retval) { |
| 222 | com_err(program_name, retval, |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 223 | _("while trying to determine filesystem size")); |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 224 | exit(1); |
| 225 | } |
| 226 | if (!new_size) |
| 227 | new_size = max_size; |
Theodore Ts'o | 116db1b | 2002-04-01 01:28:30 -0500 | [diff] [blame^] | 228 | /* |
| 229 | * If we are resizing a plain file, and it's not big enough, |
| 230 | * automatically extend it in a sparse fashion by writing the |
| 231 | * last requested block. |
| 232 | */ |
| 233 | if ((new_size > max_size) && |
| 234 | (stat(device_name, &st_buf) == 0) && |
| 235 | S_ISREG(st_buf.st_mode) && |
| 236 | ((tmp = malloc(fs->blocksize)) != 0)) { |
| 237 | memset(tmp, 0, fs->blocksize); |
| 238 | retval = io_channel_write_blk(fs->io, new_size-1, 1, tmp); |
| 239 | if (retval == 0) |
| 240 | max_size = new_size; |
| 241 | free(tmp); |
| 242 | } |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 243 | if (!force && (new_size > max_size)) { |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 244 | fprintf(stderr, _("The containing partition (or device)" |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 245 | " is only %d blocks.\nYou requested a new size" |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 246 | " of %d blocks.\n\n"), max_size, |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 247 | new_size); |
| 248 | exit(1); |
| 249 | } |
| 250 | if (new_size == fs->super->s_blocks_count) { |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 251 | fprintf(stderr, _("The filesystem is already %d blocks " |
| 252 | "long. Nothing to do!\n\n"), new_size); |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 253 | exit(0); |
| 254 | } |
| 255 | if (!force && (fs->super->s_lastcheck < fs->super->s_mtime)) { |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 256 | fprintf(stderr, _("Please run 'e2fsck -f %s' first.\n\n"), |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 257 | device_name); |
| 258 | exit(1); |
| 259 | } |
Theodore Ts'o | 116db1b | 2002-04-01 01:28:30 -0500 | [diff] [blame^] | 260 | retval = resize_fs(fs, &new_size, flags, |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 261 | ((flags & RESIZE_PERCENT_COMPLETE) ? |
| 262 | resize_progress_func : 0)); |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 263 | if (retval) { |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 264 | com_err(program_name, retval, _("while trying to resize %s"), |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 265 | device_name); |
| 266 | ext2fs_close (fs); |
| 267 | } |
Theodore Ts'o | a13575f | 2000-06-12 22:06:16 +0000 | [diff] [blame] | 268 | printf(_("The filesystem on %s is now %d blocks long.\n\n"), |
Theodore Ts'o | 7e71e4c | 1998-09-30 00:54:35 +0000 | [diff] [blame] | 269 | device_name, new_size); |
Theodore Ts'o | 0a61731 | 2000-02-02 19:14:36 +0000 | [diff] [blame] | 270 | return (0); |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 271 | } |