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
22 #include <sys/ioctl.h>
24 #include "resize2fs.h"
26 #include "../version.h"
28 char *program_name, *device_name;
30 static volatile 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);
110 int main (int argc, char ** argv)
124 initialize_ext2_error_table();
126 fprintf (stderr, _("resize2fs %s (%s)\n"),
127 E2FSPROGS_VERSION, E2FSPROGS_DATE);
129 program_name = *argv;
131 while ((c = getopt (argc, argv, "d:fFhp")) != EOF) {
143 flags |= atoi(optarg);
146 flags |= RESIZE_PERCENT_COMPLETE;
155 device_name = argv[optind++];
157 new_size = strtoul(argv[optind++], &tmp, 0);
159 com_err(program_name, 0, _("bad filesystem size - %s"),
167 check_mount(device_name);
171 fd = open(device_name, O_RDONLY, 0);
174 com_err("open", errno,
175 _("while opening %s for flushing"),
179 if (ioctl(fd, BLKFLSBUF, 0) < 0) {
180 com_err("BLKFLSBUF", errno,
181 _("while trying to flush %s"),
187 fprintf(stderr, _("BLKFLSBUF not supported"));
189 #endif /* BLKFLSBUF */
192 if (flags & RESIZE_DEBUG_IO) {
193 io_ptr = test_io_manager;
194 test_io_backing_manager = unix_io_manager;
196 io_ptr = unix_io_manager;
198 retval = ext2fs_open (device_name, EXT2_FLAG_RW, 0, 0,
201 com_err (program_name, retval, _("while trying to open %s"),
203 printf (_("Couldn't find valid filesystem superblock.\n"));
207 * Check for compatibility with the feature sets. We need to
208 * be more stringent than ext2fs_open().
210 if ((fs->super->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
211 (fs->super->s_feature_incompat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
212 com_err(program_name, EXT2_ET_UNSUPP_FEATURE,
213 "(%s)", device_name);
218 * Get the size of the containing partition, and use this for
219 * defaults and for making sure the new filesystme doesn't
220 * exceed the partition size.
222 retval = ext2fs_get_device_size(device_name, fs->blocksize,
225 com_err(program_name, retval,
226 _("while trying to determine filesystem size"));
231 if (!force && (new_size > max_size)) {
232 fprintf(stderr, _("The containing partition (or device)"
233 " is only %d blocks.\nYou requested a new size"
234 " of %d blocks.\n\n"), max_size,
238 if (new_size == fs->super->s_blocks_count) {
239 fprintf(stderr, _("The filesystem is already %d blocks "
240 "long. Nothing to do!\n\n"), new_size);
243 if (!force && (fs->super->s_lastcheck < fs->super->s_mtime)) {
244 fprintf(stderr, _("Please run 'e2fsck -f %s' first.\n\n"),
248 retval = resize_fs(fs, new_size, flags,
249 ((flags & RESIZE_PERCENT_COMPLETE) ?
250 resize_progress_func : 0));
252 com_err(program_name, retval, _("while trying to resize %s"),
256 printf(_("The filesystem on %s is now %d blocks long.\n\n"),
257 device_name, new_size);