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