blob: 3d137ef641e34bc980e8082bc33ba9ef11ddef4c [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'o116db1b2002-04-01 01:28:30 -050022#include <sys/stat.h>
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000023
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +000024#include "resize2fs.h"
25
Theodore Ts'o0cee8a52000-04-06 21:38:34 +000026#include "../version.h"
Theodore Ts'o2e561e01998-04-08 06:18:37 +000027
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -050028char *program_name, *device_name, *io_options;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +000029
Theodore Ts'odfcdc322001-01-11 15:38:00 +000030static void usage (char *prog)
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +000031{
Theodore Ts'oa13575f2000-06-12 22:06:16 +000032 fprintf (stderr, _("usage: %s [-d debug_flags] [-f] [-F] [-p] device [new-size]\n\n"), prog);
Theodore Ts'o2e561e01998-04-08 06:18:37 +000033
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +000034 exit (1);
35}
36
Theodore Ts'o3b627e81998-02-24 20:24:49 +000037static errcode_t resize_progress_func(ext2_resize_t rfs, int pass,
38 unsigned long cur, unsigned long max)
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000039{
40 ext2_sim_progmeter progress;
41 const char *label;
42 errcode_t retval;
43
44 progress = (ext2_sim_progmeter) rfs->prog_data;
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +000045 if (max == 0)
Theodore Ts'o3b627e81998-02-24 20:24:49 +000046 return 0;
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000047 if (cur == 0) {
48 if (progress)
49 ext2fs_progress_close(progress);
50 progress = 0;
51 switch (pass) {
Theodore Ts'oa8519a21998-02-16 22:16:20 +000052 case E2_RSZ_EXTEND_ITABLE_PASS:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000053 label = _("Extending the inode table");
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000054 break;
55 case E2_RSZ_BLOCK_RELOC_PASS:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000056 label = _("Relocating blocks");
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000057 break;
Theodore Ts'oa8519a21998-02-16 22:16:20 +000058 case E2_RSZ_INODE_SCAN_PASS:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000059 label = _("Scanning inode table");
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000060 break;
61 case E2_RSZ_INODE_REF_UPD_PASS:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000062 label = _("Updating inode references");
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000063 break;
64 case E2_RSZ_MOVE_ITABLE_PASS:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000065 label = _("Moving inode table");
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000066 break;
Theodore Ts'oa8519a21998-02-16 22:16:20 +000067 default:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000068 label = _("Unknown pass?!?");
Theodore Ts'oa8519a21998-02-16 22:16:20 +000069 break;
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000070 }
Theodore Ts'oa13575f2000-06-12 22:06:16 +000071 printf(_("Begin pass %d (max = %lu)\n"), pass, max);
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000072 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'o3b627e81998-02-24 20:24:49 +000086 return 0;
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000087}
88
Theodore Ts'o2a3013b1998-03-30 01:08:41 +000089static void check_mount(char *device)
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +000090{
91 errcode_t retval;
92 int mount_flags;
93
Theodore Ts'o2a3013b1998-03-30 01:08:41 +000094 retval = ext2fs_check_if_mounted(device, &mount_flags);
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +000095 if (retval) {
Theodore Ts'oddc32a02003-05-03 18:45:55 -040096 com_err("ext2fs_check_if_mount", retval,
Theodore Ts'oa13575f2000-06-12 22:06:16 +000097 _("while determining whether %s is mounted."),
Theodore Ts'o2a3013b1998-03-30 01:08:41 +000098 device);
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +000099 return;
100 }
101 if (!(mount_flags & EXT2_MF_MOUNTED))
102 return;
103
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000104 fprintf(stderr, _("%s is mounted; can't resize a "
105 "mounted filesystem!\n\n"), device);
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000106 exit(1);
107}
108
Theodore Ts'o792a0882003-05-13 23:32:59 -0400109static int get_units(const char *s)
110{
111 if (strlen(s) != 1)
112 return -1;
113 switch(s[0]) {
114 case 's':
115 return 512;
116 case 'K':
117 return 1024;
118 case 'M':
119 return 1024*1024;
120 case 'G':
121 return 1024*1024*1024;
122 }
123 return -1;
124}
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000125
Theodore Ts'o0a617312000-02-02 19:14:36 +0000126int main (int argc, char ** argv)
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000127{
128 errcode_t retval;
129 ext2_filsys fs;
130 int c;
Theodore Ts'o05e112a1997-06-14 07:28:44 +0000131 int flags = 0;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000132 int flush = 0;
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000133 int force = 0;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000134 int fd;
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000135 blk_t new_size = 0;
136 blk_t max_size = 0;
Theodore Ts'o792a0882003-05-13 23:32:59 -0400137 int units = 0;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000138 io_manager io_ptr;
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000139 char *tmp;
Theodore Ts'o116db1b2002-04-01 01:28:30 -0500140 struct stat st_buf;
Theodore Ts'o54434922003-12-07 01:28:50 -0500141 unsigned int sys_page_size = 4096;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -0400142 long sysval;
Theodore Ts'oddc32a02003-05-03 18:45:55 -0400143
144#ifdef ENABLE_NLS
145 setlocale(LC_MESSAGES, "");
146 setlocale(LC_CTYPE, "");
147 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
148 textdomain(NLS_CAT_NAME);
149#endif
150
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000151 initialize_ext2_error_table();
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000152
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000153 fprintf (stderr, _("resize2fs %s (%s)\n"),
Theodore Ts'oba0af751998-03-09 17:41:53 +0000154 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000155 if (argc && *argv)
156 program_name = *argv;
Theodore Ts'o2e561e01998-04-08 06:18:37 +0000157
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000158 while ((c = getopt (argc, argv, "d:fFhp")) != EOF) {
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000159 switch (c) {
160 case 'h':
161 usage(program_name);
162 break;
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000163 case 'f':
164 force = 1;
165 break;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000166 case 'F':
167 flush = 1;
168 break;
Theodore Ts'o05e112a1997-06-14 07:28:44 +0000169 case 'd':
170 flags |= atoi(optarg);
171 break;
172 case 'p':
173 flags |= RESIZE_PERCENT_COMPLETE;
174 break;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000175 default:
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000176 usage(program_name);
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000177 }
178 }
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000179 if (optind == argc)
180 usage(program_name);
Theodore Ts'o2e561e01998-04-08 06:18:37 +0000181
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000182 device_name = argv[optind++];
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000183 if (optind < argc) {
184 new_size = strtoul(argv[optind++], &tmp, 0);
185 if (*tmp) {
Theodore Ts'o792a0882003-05-13 23:32:59 -0400186 units = get_units(tmp);
187 if (units < 0) {
188 com_err(program_name, 0,
189 _("bad filesystem size - %s"),
190 argv[optind - 1]);
191 exit(1);
192 }
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000193 }
194 }
195 if (optind < argc)
196 usage(program_name);
197
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500198 io_options = strchr(device_name, '?');
199 if (io_options)
200 *io_options++ = 0;
201
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000202 check_mount(device_name);
203
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000204 if (flush) {
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000205 fd = open(device_name, O_RDONLY, 0);
206
207 if (fd < 0) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000208 com_err("open", errno,
209 _("while opening %s for flushing"),
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000210 device_name);
211 exit(1);
212 }
Theodore Ts'o48e08e02001-01-11 16:11:11 +0000213 retval = ext2fs_sync_device(fd, 1);
214 if (retval) {
215 com_err(argv[0], retval,
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000216 _("while trying to flush %s"),
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000217 device_name);
218 exit(1);
219 }
220 close(fd);
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000221 }
222
Theodore Ts'o05e112a1997-06-14 07:28:44 +0000223 if (flags & RESIZE_DEBUG_IO) {
224 io_ptr = test_io_manager;
225 test_io_backing_manager = unix_io_manager;
226 } else
227 io_ptr = unix_io_manager;
228
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500229 retval = ext2fs_open2(device_name, io_options, EXT2_FLAG_RW,
230 0, 0, io_ptr, &fs);
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000231 if (retval) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000232 com_err (program_name, retval, _("while trying to open %s"),
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000233 device_name);
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000234 printf (_("Couldn't find valid filesystem superblock.\n"));
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000235 exit (1);
236 }
Theodore Ts'o101c84f1998-03-24 16:27:11 +0000237 /*
238 * Check for compatibility with the feature sets. We need to
239 * be more stringent than ext2fs_open().
240 */
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500241 if (fs->super->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) {
Theodore Ts'o101c84f1998-03-24 16:27:11 +0000242 com_err(program_name, EXT2_ET_UNSUPP_FEATURE,
243 "(%s)", device_name);
244 exit(1);
245 }
246
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -0400247 /* Determine the system page size if possible */
248#ifdef HAVE_SYSCONF
249#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
250#define _SC_PAGESIZE _SC_PAGE_SIZE
251#endif
252#ifdef _SC_PAGESIZE
253 sysval = sysconf(_SC_PAGESIZE);
254 if (sysval > 0)
255 sys_page_size = sysval;
256#endif /* _SC_PAGESIZE */
257#endif /* HAVE_SYSCONF */
258
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000259 /*
260 * Get the size of the containing partition, and use this for
261 * defaults and for making sure the new filesystme doesn't
262 * exceed the partition size.
263 */
264 retval = ext2fs_get_device_size(device_name, fs->blocksize,
265 &max_size);
266 if (retval) {
267 com_err(program_name, retval,
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000268 _("while trying to determine filesystem size"));
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000269 exit(1);
270 }
Theodore Ts'o792a0882003-05-13 23:32:59 -0400271 if (units) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500272 if ((unsigned) units < fs->blocksize)
Theodore Ts'o792a0882003-05-13 23:32:59 -0400273 new_size = (new_size * units) / fs->blocksize;
Theodore Ts'o54434922003-12-07 01:28:50 -0500274 else if ((unsigned) units > fs->blocksize)
Theodore Ts'o792a0882003-05-13 23:32:59 -0400275 new_size = new_size * (units / fs->blocksize);
276 }
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -0400277 if (!new_size) {
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000278 new_size = max_size;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -0400279 /* Round down to an even multiple of a pagesize */
280 if (sys_page_size > fs->blocksize)
281 new_size &= ~((sys_page_size / fs->blocksize)-1);
282 }
Theodore Ts'o792a0882003-05-13 23:32:59 -0400283
Theodore Ts'o116db1b2002-04-01 01:28:30 -0500284 /*
285 * If we are resizing a plain file, and it's not big enough,
286 * automatically extend it in a sparse fashion by writing the
287 * last requested block.
288 */
289 if ((new_size > max_size) &&
290 (stat(device_name, &st_buf) == 0) &&
291 S_ISREG(st_buf.st_mode) &&
292 ((tmp = malloc(fs->blocksize)) != 0)) {
293 memset(tmp, 0, fs->blocksize);
294 retval = io_channel_write_blk(fs->io, new_size-1, 1, tmp);
295 if (retval == 0)
296 max_size = new_size;
297 free(tmp);
298 }
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000299 if (!force && (new_size > max_size)) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000300 fprintf(stderr, _("The containing partition (or device)"
Theodore Ts'o792a0882003-05-13 23:32:59 -0400301 " is only %d (%dk) blocks.\nYou requested a new size"
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000302 " of %d blocks.\n\n"), max_size,
Theodore Ts'o792a0882003-05-13 23:32:59 -0400303 fs->blocksize / 1024, new_size);
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000304 exit(1);
305 }
306 if (new_size == fs->super->s_blocks_count) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000307 fprintf(stderr, _("The filesystem is already %d blocks "
308 "long. Nothing to do!\n\n"), new_size);
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000309 exit(0);
310 }
Theodore Ts'o41cce582002-05-28 23:19:14 -0400311 if (!force && ((fs->super->s_lastcheck < fs->super->s_mtime) ||
312 (fs->super->s_state & EXT2_ERROR_FS) ||
313 ((fs->super->s_state & EXT2_VALID_FS) == 0))) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000314 fprintf(stderr, _("Please run 'e2fsck -f %s' first.\n\n"),
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000315 device_name);
316 exit(1);
317 }
Theodore Ts'o792a0882003-05-13 23:32:59 -0400318 printf("Resizing the filesystem on %s to %d (%dk) blocks.\n",
319 device_name, new_size, fs->blocksize / 1024);
Theodore Ts'o116db1b2002-04-01 01:28:30 -0500320 retval = resize_fs(fs, &new_size, flags,
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000321 ((flags & RESIZE_PERCENT_COMPLETE) ?
322 resize_progress_func : 0));
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000323 if (retval) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000324 com_err(program_name, retval, _("while trying to resize %s"),
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000325 device_name);
326 ext2fs_close (fs);
Theodore Ts'o16082112002-04-09 12:46:19 -0400327 exit(1);
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000328 }
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000329 printf(_("The filesystem on %s is now %d blocks long.\n\n"),
Theodore Ts'o7e71e4c1998-09-30 00:54:35 +0000330 device_name, new_size);
Theodore Ts'o0a617312000-02-02 19:14:36 +0000331 return (0);
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000332}