2 * main.c --- ext2 resizer main program
4 * Copyright (C) 1997 Theodore Ts'o
15 #include <sys/ioctl.h>
17 #include "resize2fs.h"
19 #include "./version.h"
21 /* #define EXPIRE_TIME 905835600 */
24 static void check_expire_time(const char *progname);
27 char *program_name, *device_name;
29 static volatile void usage (char *prog)
31 fprintf (stderr, "usage: %s [-d debug_flags] [-f] [-F] [-p] device [new-size]\n\n", prog);
34 check_expire_time(program_name);
39 static errcode_t resize_progress_func(ext2_resize_t rfs, int pass,
40 unsigned long cur, unsigned long max)
42 ext2_sim_progmeter progress;
46 progress = (ext2_sim_progmeter) rfs->prog_data;
51 ext2fs_progress_close(progress);
54 case E2_RSZ_EXTEND_ITABLE_PASS:
55 label = "Extending the inode table";
57 case E2_RSZ_BLOCK_RELOC_PASS:
58 label = "Relocating blocks";
60 case E2_RSZ_INODE_SCAN_PASS:
61 label = "Scanning inode table";
63 case E2_RSZ_INODE_REF_UPD_PASS:
64 label = "Updating inode references";
66 case E2_RSZ_MOVE_ITABLE_PASS:
67 label = "Moving inode table";
70 label = "Unknown pass?!?";
73 printf("Begin pass %d (max = %lu)\n", pass, max);
74 retval = ext2fs_progress_init(&progress, label, 30,
78 rfs->prog_data = (void *) progress;
81 ext2fs_progress_update(progress, cur);
84 ext2fs_progress_close(progress);
91 static void check_mount(char *device)
96 retval = ext2fs_check_if_mounted(device, &mount_flags);
98 com_err("ext2fs_check_if_mount", retval,
99 "while determining whether %s is mounted.",
103 if (!(mount_flags & EXT2_MF_MOUNTED))
106 fprintf(stderr, "%s is mounted; can't resize a "
107 "mounted filesystem!\n\n", device);
112 static void check_expire_time(const char *progname)
118 if (timenow > EXPIRE_TIME) {
119 fprintf(stderr, "This beta-test version of %s is expired.\n"
120 "Please contact PowerQuest to get an updated version "
121 "of this program.\n\n", progname);
124 fprintf(stderr, "Please note this is a beta-test version of "
125 "%s which will\nexpire in %d days.\n\n", progname,
126 (EXPIRE_TIME - timenow) / (60*60*24));
133 void main (int argc, char ** argv)
148 initialize_ext2_error_table();
150 fprintf (stderr, "resize2fs %s (%s)\n",
151 E2FSPROGS_VERSION, E2FSPROGS_DATE);
152 fprintf(stderr, "Copyright 1998 by Theodore Ts'o and PowerQuest, Inc. All Rights Reserved.\n\n");
154 program_name = *argv;
156 while ((c = getopt (argc, argv, "d:fFhp")) != EOF) {
168 flags |= atoi(optarg);
171 flags |= RESIZE_PERCENT_COMPLETE;
181 check_expire_time(program_name);
184 device_name = argv[optind++];
186 new_size = strtoul(argv[optind++], &tmp, 0);
188 com_err(program_name, 0, "bad filesystem size - %s",
196 check_mount(device_name);
200 fd = open(device_name, O_RDONLY, 0);
203 com_err("open", errno, "while opening %s for flushing",
207 if (ioctl(fd, BLKFLSBUF, 0) < 0) {
208 com_err("BLKFLSBUF", errno, "while trying to flush %s",
214 fprintf(stderr, "BLKFLSBUF not supported");
216 #endif /* BLKFLSBUF */
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 s = (struct ext2fs_sb *) fs->super;
238 if ((s->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
239 (s->s_feature_incompat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
240 com_err(program_name, EXT2_ET_UNSUPP_FEATURE,
241 "(%s)", device_name);
246 * Get the size of the containing partition, and use this for
247 * defaults and for making sure the new filesystme doesn't
248 * exceed the partition size.
250 retval = ext2fs_get_device_size(device_name, fs->blocksize,
253 com_err(program_name, retval,
254 "while trying to determine filesystem size");
259 if (!force && (new_size > max_size)) {
260 fprintf(stderr, "The containing partition (or device)"
261 " is only %d blocks.\nYou requested a new size"
262 " of %d blocks.\n\n", max_size,
266 if (new_size == fs->super->s_blocks_count) {
267 fprintf(stderr, "The filesystem is already %d blocks "
268 "long. Nothing to do!\n\n", new_size);
271 if (!force && (fs->super->s_lastcheck < fs->super->s_mtime)) {
272 fprintf(stderr, "Please run 'e2fsck -f %s' first.\n\n",
276 retval = resize_fs(fs, new_size, flags,
277 ((flags & RESIZE_PERCENT_COMPLETE) ?
278 resize_progress_func : 0));
280 com_err(program_name, retval, "while trying to resize %s",
284 printf("The filesystem on %s is now %d blocks long.\n\n",
285 device_name, new_size);