blob: 841f4bd2b4dc62b6c6712c8e47ed43283db970f5 [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'o24b2c7a1997-06-07 20:42:58 +000028char *program_name, *device_name;
29
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'oa13575f2000-06-12 22:06:16 +000096 com_err(_("ext2fs_check_if_mount"), retval,
97 _("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
109
Theodore Ts'o0a617312000-02-02 19:14:36 +0000110int main (int argc, char ** argv)
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000111{
112 errcode_t retval;
113 ext2_filsys fs;
114 int c;
Theodore Ts'o05e112a1997-06-14 07:28:44 +0000115 int flags = 0;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000116 int flush = 0;
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000117 int force = 0;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000118 int fd;
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000119 blk_t new_size = 0;
120 blk_t max_size = 0;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000121 io_manager io_ptr;
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000122 char *tmp;
Theodore Ts'o116db1b2002-04-01 01:28:30 -0500123 struct stat st_buf;
Theodore Ts'o101c84f1998-03-24 16:27:11 +0000124
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000125 initialize_ext2_error_table();
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000126
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000127 fprintf (stderr, _("resize2fs %s (%s)\n"),
Theodore Ts'oba0af751998-03-09 17:41:53 +0000128 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000129 if (argc && *argv)
130 program_name = *argv;
Theodore Ts'o2e561e01998-04-08 06:18:37 +0000131
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000132 while ((c = getopt (argc, argv, "d:fFhp")) != EOF) {
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000133 switch (c) {
134 case 'h':
135 usage(program_name);
136 break;
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000137 case 'f':
138 force = 1;
139 break;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000140 case 'F':
141 flush = 1;
142 break;
Theodore Ts'o05e112a1997-06-14 07:28:44 +0000143 case 'd':
144 flags |= atoi(optarg);
145 break;
146 case 'p':
147 flags |= RESIZE_PERCENT_COMPLETE;
148 break;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000149 default:
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000150 usage(program_name);
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000151 }
152 }
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000153 if (optind == argc)
154 usage(program_name);
Theodore Ts'o2e561e01998-04-08 06:18:37 +0000155
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000156 device_name = argv[optind++];
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000157 if (optind < argc) {
158 new_size = strtoul(argv[optind++], &tmp, 0);
159 if (*tmp) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000160 com_err(program_name, 0, _("bad filesystem size - %s"),
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000161 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'oc762c8e1997-06-17 03:52:12 +0000170 if (flush) {
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000171 fd = open(device_name, O_RDONLY, 0);
172
173 if (fd < 0) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000174 com_err("open", errno,
175 _("while opening %s for flushing"),
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000176 device_name);
177 exit(1);
178 }
Theodore Ts'o48e08e02001-01-11 16:11:11 +0000179 retval = ext2fs_sync_device(fd, 1);
180 if (retval) {
181 com_err(argv[0], retval,
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000182 _("while trying to flush %s"),
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000183 device_name);
184 exit(1);
185 }
186 close(fd);
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000187 }
188
Theodore Ts'o05e112a1997-06-14 07:28:44 +0000189 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'o1e1da291997-06-09 14:51:29 +0000195 retval = ext2fs_open (device_name, EXT2_FLAG_RW, 0, 0,
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000196 io_ptr, &fs);
197 if (retval) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000198 com_err (program_name, retval, _("while trying to open %s"),
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000199 device_name);
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000200 printf (_("Couldn't find valid filesystem superblock.\n"));
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000201 exit (1);
202 }
Theodore Ts'o101c84f1998-03-24 16:27:11 +0000203 /*
204 * Check for compatibility with the feature sets. We need to
205 * be more stringent than ext2fs_open().
206 */
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500207 if (fs->super->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) {
Theodore Ts'o101c84f1998-03-24 16:27:11 +0000208 com_err(program_name, EXT2_ET_UNSUPP_FEATURE,
209 "(%s)", device_name);
210 exit(1);
211 }
212
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000213 /*
214 * Get the size of the containing partition, and use this for
215 * defaults and for making sure the new filesystme doesn't
216 * exceed the partition size.
217 */
218 retval = ext2fs_get_device_size(device_name, fs->blocksize,
219 &max_size);
220 if (retval) {
221 com_err(program_name, retval,
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000222 _("while trying to determine filesystem size"));
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000223 exit(1);
224 }
225 if (!new_size)
226 new_size = max_size;
Theodore Ts'o116db1b2002-04-01 01:28:30 -0500227 /*
228 * If we are resizing a plain file, and it's not big enough,
229 * automatically extend it in a sparse fashion by writing the
230 * last requested block.
231 */
232 if ((new_size > max_size) &&
233 (stat(device_name, &st_buf) == 0) &&
234 S_ISREG(st_buf.st_mode) &&
235 ((tmp = malloc(fs->blocksize)) != 0)) {
236 memset(tmp, 0, fs->blocksize);
237 retval = io_channel_write_blk(fs->io, new_size-1, 1, tmp);
238 if (retval == 0)
239 max_size = new_size;
240 free(tmp);
241 }
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000242 if (!force && (new_size > max_size)) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000243 fprintf(stderr, _("The containing partition (or device)"
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000244 " is only %d blocks.\nYou requested a new size"
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000245 " of %d blocks.\n\n"), max_size,
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000246 new_size);
247 exit(1);
248 }
249 if (new_size == fs->super->s_blocks_count) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000250 fprintf(stderr, _("The filesystem is already %d blocks "
251 "long. Nothing to do!\n\n"), new_size);
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000252 exit(0);
253 }
Theodore Ts'o41cce582002-05-28 23:19:14 -0400254 if (!force && ((fs->super->s_lastcheck < fs->super->s_mtime) ||
255 (fs->super->s_state & EXT2_ERROR_FS) ||
256 ((fs->super->s_state & EXT2_VALID_FS) == 0))) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000257 fprintf(stderr, _("Please run 'e2fsck -f %s' first.\n\n"),
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000258 device_name);
259 exit(1);
260 }
Theodore Ts'o116db1b2002-04-01 01:28:30 -0500261 retval = resize_fs(fs, &new_size, flags,
Theodore Ts'oa8519a21998-02-16 22:16:20 +0000262 ((flags & RESIZE_PERCENT_COMPLETE) ?
263 resize_progress_func : 0));
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000264 if (retval) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000265 com_err(program_name, retval, _("while trying to resize %s"),
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000266 device_name);
267 ext2fs_close (fs);
Theodore Ts'o16082112002-04-09 12:46:19 -0400268 exit(1);
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000269 }
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000270 printf(_("The filesystem on %s is now %d blocks long.\n\n"),
Theodore Ts'o7e71e4c1998-09-30 00:54:35 +0000271 device_name, new_size);
Theodore Ts'o0a617312000-02-02 19:14:36 +0000272 return (0);
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000273}