blob: cff2d0f69619caba86f92a5aebb692260781167f [file] [log] [blame]
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +00001/*
2 * main.c --- ext2 resizer main program
3 *
Theodore Ts'o0cee8a52000-04-06 21:38:34 +00004 * Copyright (C) 1997, 1998 by Theodore Ts'o and
5 * PowerQuest, Inc.
6 *
Theodore Ts'oe5b38a52001-01-01 16:17:12 +00007 * Copyright (C) 1999, 2000, 2001 by Theosore Ts'o
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +00008 *
9 * %Begin-Header%
Theodore Ts'o0cee8a52000-04-06 21:38:34 +000010 * This file may be redistributed under the terms of the GNU Public
11 * License.
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +000012 * %End-Header%
13 */
14
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000015#ifdef HAVE_GETOPT_H
16#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000017#else
18extern char *optarg;
19extern int optind;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000020#endif
21#include <fcntl.h>
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000022
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +000023#include "resize2fs.h"
24
Theodore Ts'o0cee8a52000-04-06 21:38:34 +000025#include "../version.h"
Theodore Ts'o2e561e01998-04-08 06:18:37 +000026
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +000027char *program_name, *device_name;
28
Theodore Ts'odfcdc322001-01-11 15:38:00 +000029static void usage (char *prog)
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +000030{
Theodore Ts'oa13575f2000-06-12 22:06:16 +000031 fprintf (stderr, _("usage: %s [-d debug_flags] [-f] [-F] [-p] device [new-size]\n\n"), prog);
Theodore Ts'o2e561e01998-04-08 06:18:37 +000032
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +000033 exit (1);
34}
35
Theodore Ts'o3b627e81998-02-24 20:24:49 +000036static errcode_t resize_progress_func(ext2_resize_t rfs, int pass,
37 unsigned long cur, unsigned long max)
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000038{
39 ext2_sim_progmeter progress;
40 const char *label;
41 errcode_t retval;
42
43 progress = (ext2_sim_progmeter) rfs->prog_data;
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +000044 if (max == 0)
Theodore Ts'o3b627e81998-02-24 20:24:49 +000045 return 0;
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000046 if (cur == 0) {
47 if (progress)
48 ext2fs_progress_close(progress);
49 progress = 0;
50 switch (pass) {
Theodore Ts'oa8519a21998-02-16 22:16:20 +000051 case E2_RSZ_EXTEND_ITABLE_PASS:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000052 label = _("Extending the inode table");
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000053 break;
54 case E2_RSZ_BLOCK_RELOC_PASS:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000055 label = _("Relocating blocks");
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000056 break;
Theodore Ts'oa8519a21998-02-16 22:16:20 +000057 case E2_RSZ_INODE_SCAN_PASS:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000058 label = _("Scanning inode table");
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000059 break;
60 case E2_RSZ_INODE_REF_UPD_PASS:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000061 label = _("Updating inode references");
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000062 break;
63 case E2_RSZ_MOVE_ITABLE_PASS:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000064 label = _("Moving inode table");
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000065 break;
Theodore Ts'oa8519a21998-02-16 22:16:20 +000066 default:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000067 label = _("Unknown pass?!?");
Theodore Ts'oa8519a21998-02-16 22:16:20 +000068 break;
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000069 }
Theodore Ts'oa13575f2000-06-12 22:06:16 +000070 printf(_("Begin pass %d (max = %lu)\n"), pass, max);
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000071 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'o3b627e81998-02-24 20:24:49 +000085 return 0;
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000086}
87
Theodore Ts'o2a3013b1998-03-30 01:08:41 +000088static void check_mount(char *device)
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +000089{
90 errcode_t retval;
91 int mount_flags;
92
Theodore Ts'o2a3013b1998-03-30 01:08:41 +000093 retval = ext2fs_check_if_mounted(device, &mount_flags);
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +000094 if (retval) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +000095 com_err(_("ext2fs_check_if_mount"), retval,
96 _("while determining whether %s is mounted."),
Theodore Ts'o2a3013b1998-03-30 01:08:41 +000097 device);
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +000098 return;
99 }
100 if (!(mount_flags & EXT2_MF_MOUNTED))
101 return;
102
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000103 fprintf(stderr, _("%s is mounted; can't resize a "
104 "mounted filesystem!\n\n"), device);
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000105 exit(1);
106}
107
108
Theodore Ts'o0a617312000-02-02 19:14:36 +0000109int main (int argc, char ** argv)
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000110{
111 errcode_t retval;
112 ext2_filsys fs;
113 int c;
Theodore Ts'o05e112a1997-06-14 07:28:44 +0000114 int flags = 0;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000115 int flush = 0;
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000116 int force = 0;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000117 int fd;
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000118 blk_t new_size = 0;
119 blk_t max_size = 0;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000120 io_manager io_ptr;
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000121 char *tmp;
Theodore Ts'o101c84f1998-03-24 16:27:11 +0000122
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000123 initialize_ext2_error_table();
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000124
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000125 fprintf (stderr, _("resize2fs %s (%s)\n"),
Theodore Ts'oba0af751998-03-09 17:41:53 +0000126 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000127 if (argc && *argv)
128 program_name = *argv;
Theodore Ts'o2e561e01998-04-08 06:18:37 +0000129
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000130 while ((c = getopt (argc, argv, "d:fFhp")) != EOF) {
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000131 switch (c) {
132 case 'h':
133 usage(program_name);
134 break;
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000135 case 'f':
136 force = 1;
137 break;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000138 case 'F':
139 flush = 1;
140 break;
Theodore Ts'o05e112a1997-06-14 07:28:44 +0000141 case 'd':
142 flags |= atoi(optarg);
143 break;
144 case 'p':
145 flags |= RESIZE_PERCENT_COMPLETE;
146 break;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000147 default:
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000148 usage(program_name);
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000149 }
150 }
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000151 if (optind == argc)
152 usage(program_name);
Theodore Ts'o2e561e01998-04-08 06:18:37 +0000153
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000154 device_name = argv[optind++];
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000155 if (optind < argc) {
156 new_size = strtoul(argv[optind++], &tmp, 0);
157 if (*tmp) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000158 com_err(program_name, 0, _("bad filesystem size - %s"),
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000159 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'oc762c8e1997-06-17 03:52:12 +0000168 if (flush) {
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000169 fd = open(device_name, O_RDONLY, 0);
170
171 if (fd < 0) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000172 com_err("open", errno,
173 _("while opening %s for flushing"),
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000174 device_name);
175 exit(1);
176 }
Theodore Ts'o48e08e02001-01-11 16:11:11 +0000177 retval = ext2fs_sync_device(fd, 1);
178 if (retval) {
179 com_err(argv[0], retval,
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000180 _("while trying to flush %s"),
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000181 device_name);
182 exit(1);
183 }
184 close(fd);
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000185 }
186
Theodore Ts'o05e112a1997-06-14 07:28:44 +0000187 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'o1e1da291997-06-09 14:51:29 +0000193 retval = ext2fs_open (device_name, EXT2_FLAG_RW, 0, 0,
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000194 io_ptr, &fs);
195 if (retval) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000196 com_err (program_name, retval, _("while trying to open %s"),
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000197 device_name);
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000198 printf (_("Couldn't find valid filesystem superblock.\n"));
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000199 exit (1);
200 }
Theodore Ts'o101c84f1998-03-24 16:27:11 +0000201 /*
202 * Check for compatibility with the feature sets. We need to
203 * be more stringent than ext2fs_open().
204 */
Theodore Ts'oe5b38a52001-01-01 16:17:12 +0000205 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'o101c84f1998-03-24 16:27:11 +0000207 com_err(program_name, EXT2_ET_UNSUPP_FEATURE,
208 "(%s)", device_name);
209 exit(1);
210 }
211
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000212 /*
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'oa13575f2000-06-12 22:06:16 +0000221 _("while trying to determine filesystem size"));
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000222 exit(1);
223 }
224 if (!new_size)
225 new_size = max_size;
226 if (!force && (new_size > max_size)) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000227 fprintf(stderr, _("The containing partition (or device)"
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000228 " is only %d blocks.\nYou requested a new size"
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000229 " of %d blocks.\n\n"), max_size,
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000230 new_size);
231 exit(1);
232 }
233 if (new_size == fs->super->s_blocks_count) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000234 fprintf(stderr, _("The filesystem is already %d blocks "
235 "long. Nothing to do!\n\n"), new_size);
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000236 exit(0);
237 }
238 if (!force && (fs->super->s_lastcheck < fs->super->s_mtime)) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000239 fprintf(stderr, _("Please run 'e2fsck -f %s' first.\n\n"),
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000240 device_name);
241 exit(1);
242 }
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000243 retval = resize_fs(fs, new_size, flags,
244 ((flags & RESIZE_PERCENT_COMPLETE) ?
245 resize_progress_func : 0));
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000246 if (retval) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000247 com_err(program_name, retval, _("while trying to resize %s"),
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000248 device_name);
249 ext2fs_close (fs);
250 }
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000251 printf(_("The filesystem on %s is now %d blocks long.\n\n"),
Theodore Ts'o7e71e4c1998-09-30 00:54:35 +0000252 device_name, new_size);
Theodore Ts'o0a617312000-02-02 19:14:36 +0000253 return (0);
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000254}