2 * main.c --- ext2 resizer main program
4 * Copyright (C) 1997, 1998 by Theodore Ts'o and
7 * Copyright (C) 1999, 2000, 2001 by Theosore Ts'o
10 * This file may be redistributed under the terms of the GNU Public
24 #include "resize2fs.h"
26 #include "../version.h"
28 char *program_name, *device_name;
30 static void usage (char *prog)
32 fprintf (stderr, _("usage: %s [-d debug_flags] [-f] [-F] [-p] device [new-size]\n\n"), prog);
37 static errcode_t resize_progress_func(ext2_resize_t rfs, int pass,
38 unsigned long cur, unsigned long max)
40 ext2_sim_progmeter progress;
44 progress = (ext2_sim_progmeter) rfs->prog_data;
49 ext2fs_progress_close(progress);
52 case E2_RSZ_EXTEND_ITABLE_PASS:
53 label = _("Extending the inode table");
55 case E2_RSZ_BLOCK_RELOC_PASS:
56 label = _("Relocating blocks");
58 case E2_RSZ_INODE_SCAN_PASS:
59 label = _("Scanning inode table");
61 case E2_RSZ_INODE_REF_UPD_PASS:
62 label = _("Updating inode references");
64 case E2_RSZ_MOVE_ITABLE_PASS:
65 label = _("Moving inode table");
68 label = _("Unknown pass?!?");
71 printf(_("Begin pass %d (max = %lu)\n"), pass, max);
72 retval = ext2fs_progress_init(&progress, label, 30,
76 rfs->prog_data = (void *) progress;
79 ext2fs_progress_update(progress, cur);
82 ext2fs_progress_close(progress);
89 static void check_mount(char *device)
94 retval = ext2fs_check_if_mounted(device, &mount_flags);
96 com_err("ext2fs_check_if_mount", retval,
97 _("while determining whether %s is mounted."),
101 if (!(mount_flags & EXT2_MF_MOUNTED))
104 fprintf(stderr, _("%s is mounted; can't resize a "
105 "mounted filesystem!\n\n"), device);
109 static int get_units(const char *s)
121 return 1024*1024*1024;
126 int main (int argc, char ** argv)
141 unsigned int sys_page_size = 4096;
145 setlocale(LC_MESSAGES, "");
146 setlocale(LC_CTYPE, "");
147 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
148 textdomain(NLS_CAT_NAME);
151 initialize_ext2_error_table();
153 fprintf (stderr, _("resize2fs %s (%s)\n"),
154 E2FSPROGS_VERSION, E2FSPROGS_DATE);
156 program_name = *argv;
158 while ((c = getopt (argc, argv, "d:fFhp")) != EOF) {
170 flags |= atoi(optarg);
173 flags |= RESIZE_PERCENT_COMPLETE;
182 device_name = argv[optind++];
184 new_size = strtoul(argv[optind++], &tmp, 0);
186 units = get_units(tmp);
188 com_err(program_name, 0,
189 _("bad filesystem size - %s"),
198 check_mount(device_name);
201 fd = open(device_name, O_RDONLY, 0);
204 com_err("open", errno,
205 _("while opening %s for flushing"),
209 retval = ext2fs_sync_device(fd, 1);
211 com_err(argv[0], retval,
212 _("while trying to flush %s"),
219 if (flags & RESIZE_DEBUG_IO) {
220 io_ptr = test_io_manager;
221 test_io_backing_manager = unix_io_manager;
223 io_ptr = unix_io_manager;
225 retval = ext2fs_open (device_name, EXT2_FLAG_RW, 0, 0,
228 com_err (program_name, retval, _("while trying to open %s"),
230 printf (_("Couldn't find valid filesystem superblock.\n"));
234 * Check for compatibility with the feature sets. We need to
235 * be more stringent than ext2fs_open().
237 if (fs->super->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) {
238 com_err(program_name, EXT2_ET_UNSUPP_FEATURE,
239 "(%s)", device_name);
243 /* Determine the system page size if possible */
245 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
246 #define _SC_PAGESIZE _SC_PAGE_SIZE
249 sysval = sysconf(_SC_PAGESIZE);
251 sys_page_size = sysval;
252 #endif /* _SC_PAGESIZE */
253 #endif /* HAVE_SYSCONF */
256 * Get the size of the containing partition, and use this for
257 * defaults and for making sure the new filesystme doesn't
258 * exceed the partition size.
260 retval = ext2fs_get_device_size(device_name, fs->blocksize,
263 com_err(program_name, retval,
264 _("while trying to determine filesystem size"));
268 if ((unsigned) units < fs->blocksize)
269 new_size = (new_size * units) / fs->blocksize;
270 else if ((unsigned) units > fs->blocksize)
271 new_size = new_size * (units / fs->blocksize);
275 /* Round down to an even multiple of a pagesize */
276 if (sys_page_size > fs->blocksize)
277 new_size &= ~((sys_page_size / fs->blocksize)-1);
281 * If we are resizing a plain file, and it's not big enough,
282 * automatically extend it in a sparse fashion by writing the
283 * last requested block.
285 if ((new_size > max_size) &&
286 (stat(device_name, &st_buf) == 0) &&
287 S_ISREG(st_buf.st_mode) &&
288 ((tmp = malloc(fs->blocksize)) != 0)) {
289 memset(tmp, 0, fs->blocksize);
290 retval = io_channel_write_blk(fs->io, new_size-1, 1, tmp);
295 if (!force && (new_size > max_size)) {
296 fprintf(stderr, _("The containing partition (or device)"
297 " is only %d (%dk) blocks.\nYou requested a new size"
298 " of %d blocks.\n\n"), max_size,
299 fs->blocksize / 1024, new_size);
302 if (new_size == fs->super->s_blocks_count) {
303 fprintf(stderr, _("The filesystem is already %d blocks "
304 "long. Nothing to do!\n\n"), new_size);
307 if (!force && ((fs->super->s_lastcheck < fs->super->s_mtime) ||
308 (fs->super->s_state & EXT2_ERROR_FS) ||
309 ((fs->super->s_state & EXT2_VALID_FS) == 0))) {
310 fprintf(stderr, _("Please run 'e2fsck -f %s' first.\n\n"),
314 printf("Resizing the filesystem on %s to %d (%dk) blocks.\n",
315 device_name, new_size, fs->blocksize / 1024);
316 retval = resize_fs(fs, &new_size, flags,
317 ((flags & RESIZE_PERCENT_COMPLETE) ?
318 resize_progress_func : 0));
320 com_err(program_name, retval, _("while trying to resize %s"),
325 printf(_("The filesystem on %s is now %d blocks long.\n\n"),
326 device_name, new_size);