]>
Commit | Line | Data |
---|---|---|
24b2c7a7 TT |
1 | /* |
2 | * main.c --- ext2 resizer main program | |
3 | * | |
0cee8a5c TT |
4 | * Copyright (C) 1997, 1998 by Theodore Ts'o and |
5 | * PowerQuest, Inc. | |
6 | * | |
55f4cbd9 | 7 | * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 by Theodore Ts'o |
24b2c7a7 TT |
8 | * |
9 | * %Begin-Header% | |
0cee8a5c TT |
10 | * This file may be redistributed under the terms of the GNU Public |
11 | * License. | |
24b2c7a7 TT |
12 | * %End-Header% |
13 | */ | |
14 | ||
fe07357f TT |
15 | #define _LARGEFILE_SOURCE |
16 | #define _LARGEFILE64_SOURCE | |
17 | ||
c762c8e6 TT |
18 | #ifdef HAVE_GETOPT_H |
19 | #include <getopt.h> | |
373b8337 TT |
20 | #else |
21 | extern char *optarg; | |
22 | extern int optind; | |
c762c8e6 | 23 | #endif |
fe07357f TT |
24 | #include <unistd.h> |
25 | #include <sys/types.h> | |
116db1b5 | 26 | #include <sys/stat.h> |
fe07357f | 27 | #include <fcntl.h> |
c762c8e6 | 28 | |
55f4cbd9 TT |
29 | #include "e2p/e2p.h" |
30 | ||
24b2c7a7 TT |
31 | #include "resize2fs.h" |
32 | ||
0cee8a5c | 33 | #include "../version.h" |
2e561e02 | 34 | |
2e8ca9a2 | 35 | char *program_name, *device_name, *io_options; |
24b2c7a7 | 36 | |
dfcdc32f | 37 | static void usage (char *prog) |
24b2c7a7 | 38 | { |
f35fd3d5 TT |
39 | fprintf (stderr, _("Usage: %s [-d debug_flags] [-f] [-F] [-p] " |
40 | "device [new_size]\n\n"), prog); | |
2e561e02 | 41 | |
24b2c7a7 TT |
42 | exit (1); |
43 | } | |
44 | ||
3b627e8d TT |
45 | static errcode_t resize_progress_func(ext2_resize_t rfs, int pass, |
46 | unsigned long cur, unsigned long max) | |
63b44fbe TT |
47 | { |
48 | ext2_sim_progmeter progress; | |
49 | const char *label; | |
50 | errcode_t retval; | |
51 | ||
52 | progress = (ext2_sim_progmeter) rfs->prog_data; | |
f4b2a6db | 53 | if (max == 0) |
3b627e8d | 54 | return 0; |
63b44fbe TT |
55 | if (cur == 0) { |
56 | if (progress) | |
57 | ext2fs_progress_close(progress); | |
58 | progress = 0; | |
59 | switch (pass) { | |
a8519a2d | 60 | case E2_RSZ_EXTEND_ITABLE_PASS: |
a13575f4 | 61 | label = _("Extending the inode table"); |
63b44fbe TT |
62 | break; |
63 | case E2_RSZ_BLOCK_RELOC_PASS: | |
a13575f4 | 64 | label = _("Relocating blocks"); |
63b44fbe | 65 | break; |
a8519a2d | 66 | case E2_RSZ_INODE_SCAN_PASS: |
a13575f4 | 67 | label = _("Scanning inode table"); |
63b44fbe TT |
68 | break; |
69 | case E2_RSZ_INODE_REF_UPD_PASS: | |
a13575f4 | 70 | label = _("Updating inode references"); |
63b44fbe TT |
71 | break; |
72 | case E2_RSZ_MOVE_ITABLE_PASS: | |
a13575f4 | 73 | label = _("Moving inode table"); |
63b44fbe | 74 | break; |
a8519a2d | 75 | default: |
a13575f4 | 76 | label = _("Unknown pass?!?"); |
a8519a2d | 77 | break; |
63b44fbe | 78 | } |
a13575f4 | 79 | printf(_("Begin pass %d (max = %lu)\n"), pass, max); |
63b44fbe TT |
80 | retval = ext2fs_progress_init(&progress, label, 30, |
81 | 40, max, 0); | |
82 | if (retval) | |
83 | progress = 0; | |
84 | rfs->prog_data = (void *) progress; | |
85 | } | |
86 | if (progress) | |
87 | ext2fs_progress_update(progress, cur); | |
88 | if (cur >= max) { | |
89 | if (progress) | |
90 | ext2fs_progress_close(progress); | |
91 | progress = 0; | |
92 | rfs->prog_data = 0; | |
93 | } | |
3b627e8d | 94 | return 0; |
63b44fbe TT |
95 | } |
96 | ||
2a3013b8 | 97 | static void check_mount(char *device) |
f4b2a6db TT |
98 | { |
99 | errcode_t retval; | |
100 | int mount_flags; | |
101 | ||
2a3013b8 | 102 | retval = ext2fs_check_if_mounted(device, &mount_flags); |
f4b2a6db | 103 | if (retval) { |
ddc32a04 | 104 | com_err("ext2fs_check_if_mount", retval, |
a13575f4 | 105 | _("while determining whether %s is mounted."), |
2a3013b8 | 106 | device); |
f4b2a6db TT |
107 | return; |
108 | } | |
109 | if (!(mount_flags & EXT2_MF_MOUNTED)) | |
110 | return; | |
111 | ||
a13575f4 TT |
112 | fprintf(stderr, _("%s is mounted; can't resize a " |
113 | "mounted filesystem!\n\n"), device); | |
f4b2a6db TT |
114 | exit(1); |
115 | } | |
116 | ||
0a617317 | 117 | int main (int argc, char ** argv) |
24b2c7a7 TT |
118 | { |
119 | errcode_t retval; | |
120 | ext2_filsys fs; | |
121 | int c; | |
05e112a1 | 122 | int flags = 0; |
c762c8e6 | 123 | int flush = 0; |
f4b2a6db | 124 | int force = 0; |
fe07357f | 125 | int fd, ret; |
f4b2a6db TT |
126 | blk_t new_size = 0; |
127 | blk_t max_size = 0; | |
24b2c7a7 | 128 | io_manager io_ptr; |
55f4cbd9 | 129 | char *new_size_str = 0; |
fe07357f TT |
130 | #ifdef HAVE_FSTAT64 |
131 | struct stat64 st_buf; | |
132 | #else | |
116db1b5 | 133 | struct stat st_buf; |
fe07357f TT |
134 | #endif |
135 | __s64 new_file_size; | |
54434927 | 136 | unsigned int sys_page_size = 4096; |
a7ccdff8 | 137 | long sysval; |
ddc32a04 TT |
138 | |
139 | #ifdef ENABLE_NLS | |
140 | setlocale(LC_MESSAGES, ""); | |
141 | setlocale(LC_CTYPE, ""); | |
142 | bindtextdomain(NLS_CAT_NAME, LOCALEDIR); | |
143 | textdomain(NLS_CAT_NAME); | |
144 | #endif | |
145 | ||
f4b2a6db | 146 | initialize_ext2_error_table(); |
24b2c7a7 | 147 | |
f35fd3d5 | 148 | fprintf (stderr, "resize2fs %s (%s)\n", |
ba0af756 | 149 | E2FSPROGS_VERSION, E2FSPROGS_DATE); |
24b2c7a7 TT |
150 | if (argc && *argv) |
151 | program_name = *argv; | |
2e561e02 | 152 | |
f4b2a6db | 153 | while ((c = getopt (argc, argv, "d:fFhp")) != EOF) { |
24b2c7a7 TT |
154 | switch (c) { |
155 | case 'h': | |
156 | usage(program_name); | |
157 | break; | |
f4b2a6db TT |
158 | case 'f': |
159 | force = 1; | |
160 | break; | |
c762c8e6 TT |
161 | case 'F': |
162 | flush = 1; | |
163 | break; | |
05e112a1 TT |
164 | case 'd': |
165 | flags |= atoi(optarg); | |
166 | break; | |
167 | case 'p': | |
168 | flags |= RESIZE_PERCENT_COMPLETE; | |
169 | break; | |
24b2c7a7 | 170 | default: |
f4b2a6db | 171 | usage(program_name); |
24b2c7a7 TT |
172 | } |
173 | } | |
f4b2a6db TT |
174 | if (optind == argc) |
175 | usage(program_name); | |
2e561e02 | 176 | |
24b2c7a7 | 177 | device_name = argv[optind++]; |
55f4cbd9 TT |
178 | if (optind < argc) |
179 | new_size_str = argv[optind++]; | |
f4b2a6db TT |
180 | if (optind < argc) |
181 | usage(program_name); | |
182 | ||
2e8ca9a2 TT |
183 | io_options = strchr(device_name, '?'); |
184 | if (io_options) | |
185 | *io_options++ = 0; | |
186 | ||
f4b2a6db | 187 | check_mount(device_name); |
fe07357f TT |
188 | |
189 | #ifdef HAVE_OPEN64 | |
190 | fd = open64(device_name, O_RDWR); | |
191 | #else | |
192 | fd = open(device_name, O_RDWR); | |
193 | #endif | |
194 | if (fd < 0) { | |
195 | com_err("open", errno, _("while opening %s"), | |
196 | device_name); | |
197 | exit(1); | |
198 | } | |
199 | ||
200 | #ifdef HAVE_FSTAT64 | |
201 | ret = fstat64(fd, &st_buf); | |
202 | #else | |
203 | ret = fstat(fd, &st_buf); | |
204 | #endif | |
205 | if (ret < 0) { | |
206 | com_err("open", errno, | |
207 | _("while getting stat information for %s"), | |
208 | device_name); | |
209 | exit(1); | |
210 | } | |
f4b2a6db | 211 | |
c762c8e6 | 212 | if (flush) { |
48e08e03 TT |
213 | retval = ext2fs_sync_device(fd, 1); |
214 | if (retval) { | |
215 | com_err(argv[0], retval, | |
a13575f4 | 216 | _("while trying to flush %s"), |
c762c8e6 TT |
217 | device_name); |
218 | exit(1); | |
219 | } | |
fe07357f TT |
220 | } |
221 | ||
222 | if (!S_ISREG(st_buf.st_mode )) { | |
c762c8e6 | 223 | close(fd); |
fe07357f | 224 | fd = -1; |
c762c8e6 TT |
225 | } |
226 | ||
05e112a1 TT |
227 | if (flags & RESIZE_DEBUG_IO) { |
228 | io_ptr = test_io_manager; | |
229 | test_io_backing_manager = unix_io_manager; | |
230 | } else | |
231 | io_ptr = unix_io_manager; | |
232 | ||
2e8ca9a2 TT |
233 | retval = ext2fs_open2(device_name, io_options, EXT2_FLAG_RW, |
234 | 0, 0, io_ptr, &fs); | |
24b2c7a7 | 235 | if (retval) { |
a13575f4 | 236 | com_err (program_name, retval, _("while trying to open %s"), |
24b2c7a7 | 237 | device_name); |
a13575f4 | 238 | printf (_("Couldn't find valid filesystem superblock.\n")); |
24b2c7a7 TT |
239 | exit (1); |
240 | } | |
101c84f2 TT |
241 | /* |
242 | * Check for compatibility with the feature sets. We need to | |
243 | * be more stringent than ext2fs_open(). | |
244 | */ | |
76dd5e5c | 245 | if (fs->super->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) { |
101c84f2 TT |
246 | com_err(program_name, EXT2_ET_UNSUPP_FEATURE, |
247 | "(%s)", device_name); | |
248 | exit(1); | |
249 | } | |
250 | ||
a7ccdff8 TT |
251 | /* Determine the system page size if possible */ |
252 | #ifdef HAVE_SYSCONF | |
253 | #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE)) | |
254 | #define _SC_PAGESIZE _SC_PAGE_SIZE | |
255 | #endif | |
256 | #ifdef _SC_PAGESIZE | |
257 | sysval = sysconf(_SC_PAGESIZE); | |
258 | if (sysval > 0) | |
259 | sys_page_size = sysval; | |
260 | #endif /* _SC_PAGESIZE */ | |
261 | #endif /* HAVE_SYSCONF */ | |
262 | ||
f4b2a6db TT |
263 | /* |
264 | * Get the size of the containing partition, and use this for | |
fe07357f | 265 | * defaults and for making sure the new filesystem doesn't |
f4b2a6db TT |
266 | * exceed the partition size. |
267 | */ | |
268 | retval = ext2fs_get_device_size(device_name, fs->blocksize, | |
269 | &max_size); | |
270 | if (retval) { | |
271 | com_err(program_name, retval, | |
a13575f4 | 272 | _("while trying to determine filesystem size")); |
f4b2a6db TT |
273 | exit(1); |
274 | } | |
55f4cbd9 TT |
275 | if (new_size_str) { |
276 | new_size = parse_num_blocks(new_size_str, | |
277 | fs->super->s_log_block_size); | |
278 | if (!new_size) { | |
279 | com_err(program_name, 0, _("bad filesystem size - %s"), | |
280 | new_size_str); | |
281 | exit(1); | |
282 | } | |
283 | } else { | |
f4b2a6db | 284 | new_size = max_size; |
a7ccdff8 TT |
285 | /* Round down to an even multiple of a pagesize */ |
286 | if (sys_page_size > fs->blocksize) | |
287 | new_size &= ~((sys_page_size / fs->blocksize)-1); | |
288 | } | |
792a0881 | 289 | |
116db1b5 TT |
290 | /* |
291 | * If we are resizing a plain file, and it's not big enough, | |
292 | * automatically extend it in a sparse fashion by writing the | |
293 | * last requested block. | |
294 | */ | |
fe07357f TT |
295 | new_file_size = ((__u64) new_size) * fs->blocksize; |
296 | if ((__u64) new_file_size > | |
297 | (((__u64) 1) << (sizeof(st_buf.st_size)*8 - 1)) - 1) | |
298 | fd = -1; | |
299 | if ((new_file_size > st_buf.st_size) && | |
300 | (fd > 0)) { | |
301 | if ((ext2fs_llseek(fd, new_file_size-1, SEEK_SET) >= 0) && | |
302 | (write(fd, "0", 1) == 1)) | |
116db1b5 | 303 | max_size = new_size; |
116db1b5 | 304 | } |
f4b2a6db | 305 | if (!force && (new_size > max_size)) { |
a13575f4 | 306 | fprintf(stderr, _("The containing partition (or device)" |
792a0881 | 307 | " is only %d (%dk) blocks.\nYou requested a new size" |
a13575f4 | 308 | " of %d blocks.\n\n"), max_size, |
792a0881 | 309 | fs->blocksize / 1024, new_size); |
f4b2a6db TT |
310 | exit(1); |
311 | } | |
312 | if (new_size == fs->super->s_blocks_count) { | |
a13575f4 TT |
313 | fprintf(stderr, _("The filesystem is already %d blocks " |
314 | "long. Nothing to do!\n\n"), new_size); | |
f4b2a6db TT |
315 | exit(0); |
316 | } | |
41cce580 TT |
317 | if (!force && ((fs->super->s_lastcheck < fs->super->s_mtime) || |
318 | (fs->super->s_state & EXT2_ERROR_FS) || | |
319 | ((fs->super->s_state & EXT2_VALID_FS) == 0))) { | |
a13575f4 | 320 | fprintf(stderr, _("Please run 'e2fsck -f %s' first.\n\n"), |
f4b2a6db TT |
321 | device_name); |
322 | exit(1); | |
323 | } | |
792a0881 TT |
324 | printf("Resizing the filesystem on %s to %d (%dk) blocks.\n", |
325 | device_name, new_size, fs->blocksize / 1024); | |
116db1b5 | 326 | retval = resize_fs(fs, &new_size, flags, |
a8519a2d TT |
327 | ((flags & RESIZE_PERCENT_COMPLETE) ? |
328 | resize_progress_func : 0)); | |
1e1da29f | 329 | if (retval) { |
a13575f4 | 330 | com_err(program_name, retval, _("while trying to resize %s"), |
1e1da29f TT |
331 | device_name); |
332 | ext2fs_close (fs); | |
16082112 | 333 | exit(1); |
1e1da29f | 334 | } |
a13575f4 | 335 | printf(_("The filesystem on %s is now %d blocks long.\n\n"), |
7e71e4c5 | 336 | device_name, new_size); |
fe07357f TT |
337 | |
338 | if ((st_buf.st_size > new_file_size) && | |
339 | (fd > 0)) { | |
340 | #ifdef HAVE_FSTAT64 | |
341 | ftruncate64(fd, new_file_size); | |
342 | #else | |
343 | ftruncate(fd, (off_t) new_file_size); | |
344 | #endif | |
345 | } | |
346 | if (fd > 0) | |
347 | close(fd); | |
0a617317 | 348 | return (0); |
24b2c7a7 | 349 | } |