blob: ccbbc7868e26bf3520c829c1afc8defe328303af [file] [log] [blame]
Mike Frysingerd5624dc2005-06-11 00:40:20 +00001/*
2 * tune2fs.c - Change the file system parameters on an ext2 file system
3 *
4 * Copyright (C) 1992, 1993, 1994 Remy Card <card@masi.ibp.fr>
5 * Laboratoire MASI, Institut Blaise Pascal
6 * Universite Pierre et Marie Curie (Paris VI)
7 *
8 * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
9 *
10 * %Begin-Header%
11 * This file may be redistributed under the terms of the GNU Public
12 * License.
13 * %End-Header%
14 */
15
16/*
17 * History:
18 * 93/06/01 - Creation
19 * 93/10/31 - Added the -c option to change the maximal mount counts
20 * 93/12/14 - Added -l flag to list contents of superblock
21 * M.J.E. Mol (marcel@duteca.et.tudelft.nl)
22 * F.W. ten Wolde (franky@duteca.et.tudelft.nl)
23 * 93/12/29 - Added the -e option to change errors behavior
24 * 94/02/27 - Ported to use the ext2fs library
25 * 94/03/06 - Added the checks interval from Uwe Ohse (uwe@tirka.gun.de)
26 */
27
28#include <fcntl.h>
29#include <grp.h>
30#ifdef HAVE_GETOPT_H
31#include <getopt.h>
32#else
33extern char *optarg;
34extern int optind;
35#endif
36#include <pwd.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <time.h>
41#include <unistd.h>
42#include <sys/types.h>
43
44#include "e2fsbb.h"
45#include "ext2fs/ext2_fs.h"
46#include "ext2fs/ext2fs.h"
47#include "uuid/uuid.h"
48#include "e2p/e2p.h"
49#include "ext2fs/jfs_user.h"
50#include "util.h"
51#include "blkid/blkid.h"
52
Mike Frysinger7f782da2005-10-02 08:10:31 +000053static char * device_name = NULL;
Mike Frysingerd5624dc2005-06-11 00:40:20 +000054static char * new_label, *new_last_mounted, *new_UUID;
55static char * io_options;
56static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
57static int m_flag, M_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
58static time_t last_check_time;
59static int print_label;
60static int max_mount_count, mount_count, mount_flags;
61static unsigned long interval, reserved_ratio, reserved_blocks;
62static unsigned long resgid, resuid;
63static unsigned short errors;
64static int open_flag;
65static char *features_cmd;
66static char *mntopts_cmd;
67
68static int journal_size, journal_flags;
Mike Frysinger7f782da2005-10-02 08:10:31 +000069static char *journal_device = NULL;
Mike Frysingerd5624dc2005-06-11 00:40:20 +000070
71static const char *please_fsck = "Please run e2fsck on the filesystem\n";
72
73static __u32 ok_features[3] = {
74 EXT3_FEATURE_COMPAT_HAS_JOURNAL | EXT2_FEATURE_COMPAT_DIR_INDEX,
75 EXT2_FEATURE_INCOMPAT_FILETYPE,
76 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
77};
78
79/*
80 * Remove an external journal from the filesystem
81 */
82static void remove_journal_device(ext2_filsys fs)
83{
84 char *journal_path;
85 ext2_filsys jfs;
86 char buf[1024];
87 journal_superblock_t *jsb;
88 int i, nr_users;
89 errcode_t retval;
90 int commit_remove_journal = 0;
91 io_manager io_ptr;
92
93 if (f_flag)
94 commit_remove_journal = 1; /* force removal even if error */
95
96 uuid_unparse(fs->super->s_journal_uuid, buf);
97 journal_path = blkid_get_devname(NULL, "UUID", buf);
98
99 if (!journal_path) {
100 journal_path =
101 ext2fs_find_block_device(fs->super->s_journal_dev);
102 if (!journal_path)
103 return;
104 }
105
106 io_ptr = unix_io_manager;
107 retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
108 EXT2_FLAG_JOURNAL_DEV_OK, 0,
109 fs->blocksize, io_ptr, &jfs);
110 if (retval) {
111 bb_error_msg("Failed to open external journal");
112 goto no_valid_journal;
113 }
114 if (!(jfs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
115 bb_error_msg("%s is not a journal device", journal_path);
116 goto no_valid_journal;
117 }
118
119 /* Get the journal superblock */
120 if ((retval = io_channel_read_blk(jfs->io, 1, -1024, buf))) {
121 bb_error_msg("Failed to read journal superblock");
122 goto no_valid_journal;
123 }
124
125 jsb = (journal_superblock_t *) buf;
126 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
127 (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
128 bb_error_msg("Journal superblock not found!");
129 goto no_valid_journal;
130 }
131
132 /* Find the filesystem UUID */
133 nr_users = ntohl(jsb->s_nr_users);
134 for (i=0; i < nr_users; i++) {
135 if (memcmp(fs->super->s_uuid,
136 &jsb->s_users[i*16], 16) == 0)
137 break;
138 }
139 if (i >= nr_users) {
140 bb_error_msg("Filesystem's UUID not found on journal device");
141 commit_remove_journal = 1;
142 goto no_valid_journal;
143 }
144 nr_users--;
145 for (i=0; i < nr_users; i++)
146 memcpy(&jsb->s_users[i*16], &jsb->s_users[(i+1)*16], 16);
147 jsb->s_nr_users = htonl(nr_users);
148
149 /* Write back the journal superblock */
150 if ((retval = io_channel_write_blk(jfs->io, 1, -1024, buf))) {
151 bb_error_msg("Failed to write journal superblock");
152 goto no_valid_journal;
153 }
154
155 commit_remove_journal = 1;
156
157no_valid_journal:
158 if (commit_remove_journal == 0)
159 bb_error_msg_and_die("Journal NOT removed");
160 fs->super->s_journal_dev = 0;
161 uuid_clear(fs->super->s_journal_uuid);
162 ext2fs_mark_super_dirty(fs);
163 puts("Journal removed");
164 free(journal_path);
165}
166
167/* Helper function for remove_journal_inode */
168static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
169 int blockcnt EXT2FS_ATTR((unused)),
170 void *private EXT2FS_ATTR((unused)))
171{
172 blk_t block;
173 int group;
174
175 block = *blocknr;
176 ext2fs_unmark_block_bitmap(fs->block_map,block);
177 group = ext2fs_group_of_blk(fs, block);
178 fs->group_desc[group].bg_free_blocks_count++;
179 fs->super->s_free_blocks_count++;
180 return 0;
181}
182
183/*
184 * Remove the journal inode from the filesystem
185 */
186static void remove_journal_inode(ext2_filsys fs)
187{
188 struct ext2_inode inode;
189 errcode_t retval;
190 ino_t ino = fs->super->s_journal_inum;
Mike Frysinger7f782da2005-10-02 08:10:31 +0000191 char *msg = "to read";
192 char *s = "journal inode";
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000193
194 retval = ext2fs_read_inode(fs, ino, &inode);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000195 if (retval)
196 goto REMOVE_JOURNAL_INODE_ERROR;
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000197 if (ino == EXT2_JOURNAL_INO) {
198 retval = ext2fs_read_bitmaps(fs);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000199 if (retval) {
200 msg = "to read bitmaps";
201 s = "";
202 goto REMOVE_JOURNAL_INODE_ERROR;
203 }
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000204 retval = ext2fs_block_iterate(fs, ino, 0, NULL,
205 release_blocks_proc, NULL);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000206 if (retval) {
207 msg = "clearing";
208 goto REMOVE_JOURNAL_INODE_ERROR;
209 }
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000210 memset(&inode, 0, sizeof(inode));
211 ext2fs_mark_bb_dirty(fs);
212 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
213 } else
214 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
215 retval = ext2fs_write_inode(fs, ino, &inode);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000216 if (retval) {
217 msg = "writing";
218REMOVE_JOURNAL_INODE_ERROR:
219 bb_error_msg_and_die("Failed %s %s", msg, s);
220 }
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000221 fs->super->s_journal_inum = 0;
222 ext2fs_mark_super_dirty(fs);
223}
224
225/*
226 * Update the default mount options
227 */
228static void update_mntopts(ext2_filsys fs, char *mntopts)
229{
230 struct ext2_super_block *sb= fs->super;
231
232 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0))
233 bb_error_msg_and_die("Invalid mount option set: %s", mntopts);
234 ext2fs_mark_super_dirty(fs);
235}
236
237/*
238 * Update the feature set as provided by the user.
239 */
240static void update_feature_set(ext2_filsys fs, char *features)
241{
242 int sparse, old_sparse, filetype, old_filetype;
243 int journal, old_journal, dxdir, old_dxdir;
244 struct ext2_super_block *sb= fs->super;
245 __u32 old_compat, old_incompat, old_ro_compat;
246
247 old_compat = sb->s_feature_compat;
248 old_ro_compat = sb->s_feature_ro_compat;
249 old_incompat = sb->s_feature_incompat;
250
251 old_sparse = sb->s_feature_ro_compat &
252 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
253 old_filetype = sb->s_feature_incompat &
254 EXT2_FEATURE_INCOMPAT_FILETYPE;
255 old_journal = sb->s_feature_compat &
256 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
257 old_dxdir = sb->s_feature_compat &
258 EXT2_FEATURE_COMPAT_DIR_INDEX;
259 if (e2p_edit_feature(features, &sb->s_feature_compat, ok_features))
260 bb_error_msg_and_die("Invalid filesystem option set: %s", features);
261 sparse = sb->s_feature_ro_compat &
262 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
263 filetype = sb->s_feature_incompat &
264 EXT2_FEATURE_INCOMPAT_FILETYPE;
265 journal = sb->s_feature_compat &
266 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
267 dxdir = sb->s_feature_compat &
268 EXT2_FEATURE_COMPAT_DIR_INDEX;
269 if (old_journal && !journal) {
270 if ((mount_flags & EXT2_MF_MOUNTED) &&
271 !(mount_flags & EXT2_MF_READONLY)) {
272 bb_error_msg_and_die(
273 "The has_journal flag may only be "
274 "cleared when the filesystem is\n"
275 "unmounted or mounted "
276 "read-only");
277 }
278 if (sb->s_feature_incompat &
279 EXT3_FEATURE_INCOMPAT_RECOVER) {
280 bb_error_msg_and_die(
281 "The needs_recovery flag is set. "
Mike Frysinger7f782da2005-10-02 08:10:31 +0000282 "%s before clearing the has_journal flag.",
283 please_fsck);
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000284 }
285 if (sb->s_journal_inum) {
286 remove_journal_inode(fs);
287 }
288 if (sb->s_journal_dev) {
289 remove_journal_device(fs);
290 }
291 }
292 if (journal && !old_journal) {
293 /*
294 * If adding a journal flag, let the create journal
295 * code below handle creating setting the flag and
296 * creating the journal. We supply a default size if
297 * necessary.
298 */
299 if (!journal_size)
300 journal_size = -1;
301 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
302 }
303 if (dxdir && !old_dxdir) {
304 if (!sb->s_def_hash_version)
305 sb->s_def_hash_version = EXT2_HASH_TEA;
306 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
307 uuid_generate((unsigned char *) sb->s_hash_seed);
308 }
309
310 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
311 (sb->s_feature_compat || sb->s_feature_ro_compat ||
312 sb->s_feature_incompat))
313 ext2fs_update_dynamic_rev(fs);
314 if ((sparse != old_sparse) ||
315 (filetype != old_filetype)) {
316 sb->s_state &= ~EXT2_VALID_FS;
317 printf("\n%s\n", please_fsck);
318 }
319 if ((old_compat != sb->s_feature_compat) ||
320 (old_ro_compat != sb->s_feature_ro_compat) ||
321 (old_incompat != sb->s_feature_incompat))
322 ext2fs_mark_super_dirty(fs);
323}
324
325/*
326 * Add a journal to the filesystem.
327 */
328static void add_journal(ext2_filsys fs)
329{
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000330 if (fs->super->s_feature_compat &
331 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
Mike Frysinger7f782da2005-10-02 08:10:31 +0000332 bb_error_msg_and_die("The filesystem already has a journal");
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000333 }
334 if (journal_device) {
Mike Frysinger7f782da2005-10-02 08:10:31 +0000335 make_journal_device(journal_device, fs, 0, 0);
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000336 } else if (journal_size) {
Mike Frysinger7f782da2005-10-02 08:10:31 +0000337 make_journal_blocks(fs, journal_size, journal_flags, 0);
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000338 /*
339 * If the filesystem wasn't mounted, we need to force
340 * the block group descriptors out.
341 */
342 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
343 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
344 }
345 print_check_message(fs);
346 return;
Mike Frysinger7f782da2005-10-02 08:10:31 +0000347}
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000348
Mike Frysinger7f782da2005-10-02 08:10:31 +0000349/*
350 * Busybox stuff
351 */
352static char * x_blkid_get_devname(const char *token)
353{
354 char * dev_name;
355
356 if (!(dev_name = blkid_get_devname(NULL, token, NULL)))
357 bb_error_msg_and_die("Unable to resolve '%s'", token);
358 return dev_name;
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000359}
360
Mike Frysingera1c6a572005-09-24 05:55:03 +0000361#ifdef CONFIG_E2LABEL
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000362static void parse_e2label_options(int argc, char ** argv)
363{
364 if ((argc < 2) || (argc > 3))
365 bb_show_usage();
366 io_options = strchr(argv[1], '?');
367 if (io_options)
368 *io_options++ = 0;
Mike Frysinger7f782da2005-10-02 08:10:31 +0000369 device_name = x_blkid_get_devname(argv[1]);
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000370 if (argc == 3) {
371 open_flag = EXT2_FLAG_RW | EXT2_FLAG_JOURNAL_DEV_OK;
372 L_flag = 1;
373 new_label = argv[2];
374 } else
375 print_label++;
376}
Mike Frysinger7f782da2005-10-02 08:10:31 +0000377#else
378#define parse_e2label_options(x,y)
Mike Frysingera1c6a572005-09-24 05:55:03 +0000379#endif
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000380
381static time_t parse_time(char *str)
382{
383 struct tm ts;
384
385 if (strcmp(str, "now") == 0) {
386 return (time(0));
387 }
388 memset(&ts, 0, sizeof(ts));
389#ifdef HAVE_STRPTIME
390 strptime(str, "%Y%m%d%H%M%S", &ts);
391#else
392 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
393 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
394 ts.tm_year -= 1900;
395 ts.tm_mon -= 1;
396 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
397 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
398 ts.tm_min > 59 || ts.tm_sec > 61)
399 ts.tm_mday = 0;
400#endif
401 if (ts.tm_mday == 0) {
402 bb_error_msg_and_die("Couldn't parse date/time specifier: %s", str);
403 }
404 return (mktime(&ts));
405}
406
407static void parse_tune2fs_options(int argc, char **argv)
408{
409 int c;
410 char * tmp;
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000411
412 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
413 while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:J:L:M:O:T:U:")) != EOF)
414 switch (c)
415 {
416 case 'c':
Mike Frysinger7f782da2005-10-02 08:10:31 +0000417 if (safe_strtoi(optarg, &max_mount_count) || max_mount_count > 16000) {
418 goto MOUNTS_COUNT_ERROR;
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000419 }
420 if (max_mount_count == 0)
421 max_mount_count = -1;
422 c_flag = 1;
423 open_flag = EXT2_FLAG_RW;
424 break;
425 case 'C':
Mike Frysinger7f782da2005-10-02 08:10:31 +0000426 if (safe_strtoi(optarg, &mount_count) || mount_count > 16000) {
427MOUNTS_COUNT_ERROR:
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000428 bb_error_msg_and_die("bad mounts count - %s", optarg);
429 }
430 C_flag = 1;
431 open_flag = EXT2_FLAG_RW;
432 break;
433 case 'e':
434 if (strcmp (optarg, "continue") == 0)
435 errors = EXT2_ERRORS_CONTINUE;
436 else if (strcmp (optarg, "remount-ro") == 0)
437 errors = EXT2_ERRORS_RO;
438 else if (strcmp (optarg, "panic") == 0)
439 errors = EXT2_ERRORS_PANIC;
440 else {
441 bb_error_msg_and_die("bad error behavior - %s", optarg);
442 }
443 e_flag = 1;
444 open_flag = EXT2_FLAG_RW;
445 break;
446 case 'f': /* Force */
447 f_flag = 1;
448 break;
449 case 'g':
Mike Frysinger7f782da2005-10-02 08:10:31 +0000450 if (safe_strtoul(optarg, &resgid))
451 resgid = bb_xgetgrnam(optarg);
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000452 g_flag = 1;
453 open_flag = EXT2_FLAG_RW;
454 break;
455 case 'i':
456 interval = strtoul (optarg, &tmp, 0);
457 switch (*tmp) {
458 case 's':
459 tmp++;
460 break;
461 case '\0':
462 case 'd':
463 case 'D': /* days */
464 interval *= 86400;
465 if (*tmp != '\0')
466 tmp++;
467 break;
468 case 'm':
469 case 'M': /* months! */
470 interval *= 86400 * 30;
471 tmp++;
472 break;
473 case 'w':
474 case 'W': /* weeks */
475 interval *= 86400 * 7;
476 tmp++;
477 break;
478 }
479 if (*tmp || interval > (365 * 86400)) {
480 bb_error_msg_and_die("bad interval - %s", optarg);
481 }
482 i_flag = 1;
483 open_flag = EXT2_FLAG_RW;
484 break;
485 case 'j':
486 if (!journal_size)
487 journal_size = -1;
488 open_flag = EXT2_FLAG_RW;
489 break;
490 case 'J':
491 parse_journal_opts(&journal_device, &journal_flags, &journal_size, optarg);
492 open_flag = EXT2_FLAG_RW;
493 break;
494 case 'l':
495 l_flag = 1;
496 break;
497 case 'L':
498 new_label = optarg;
499 L_flag = 1;
500 open_flag = EXT2_FLAG_RW |
501 EXT2_FLAG_JOURNAL_DEV_OK;
502 break;
503 case 'm':
Mike Frysinger7f782da2005-10-02 08:10:31 +0000504 if(safe_strtoul(optarg, &reserved_ratio) || reserved_ratio > 50) {
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000505 bb_error_msg_and_die("bad reserved block ratio - %s", optarg);
506 }
507 m_flag = 1;
508 open_flag = EXT2_FLAG_RW;
509 break;
510 case 'M':
511 new_last_mounted = optarg;
512 M_flag = 1;
513 open_flag = EXT2_FLAG_RW;
514 break;
515 case 'o':
516 if (mntopts_cmd) {
517 bb_error_msg_and_die("-o may only be specified once");
518 }
519 mntopts_cmd = optarg;
520 open_flag = EXT2_FLAG_RW;
521 break;
522
523 case 'O':
524 if (features_cmd) {
525 bb_error_msg_and_die("-O may only be specified once");
526 }
527 features_cmd = optarg;
528 open_flag = EXT2_FLAG_RW;
529 break;
530 case 'r':
Mike Frysinger7f782da2005-10-02 08:10:31 +0000531 if(safe_strtoul(optarg, &reserved_blocks)) {
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000532 bb_error_msg_and_die("bad reserved blocks count - %s", optarg);
533 }
534 r_flag = 1;
535 open_flag = EXT2_FLAG_RW;
536 break;
537 case 's':
538 s_flag = atoi(optarg);
539 open_flag = EXT2_FLAG_RW;
540 break;
541 case 'T':
542 T_flag = 1;
543 last_check_time = parse_time(optarg);
544 open_flag = EXT2_FLAG_RW;
545 break;
546 case 'u':
Mike Frysinger7f782da2005-10-02 08:10:31 +0000547 if (safe_strtoul(optarg, &resuid))
548 resuid = bb_xgetpwnam(optarg);
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000549 u_flag = 1;
550 open_flag = EXT2_FLAG_RW;
551 break;
552 case 'U':
553 new_UUID = optarg;
554 U_flag = 1;
555 open_flag = EXT2_FLAG_RW |
556 EXT2_FLAG_JOURNAL_DEV_OK;
557 break;
558 default:
559 bb_show_usage();
560 }
561 if (optind < argc - 1 || optind == argc)
562 bb_show_usage();
563 if (!open_flag && !l_flag)
564 bb_show_usage();
565 io_options = strchr(argv[optind], '?');
566 if (io_options)
567 *io_options++ = 0;
Mike Frysinger7f782da2005-10-02 08:10:31 +0000568 device_name = x_blkid_get_devname(argv[optind]);
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000569}
570
Mike Frysingera1c6a572005-09-24 05:55:03 +0000571#ifdef CONFIG_FINDFS
Mike Frysinger0f8a6382005-09-24 06:07:34 +0000572static attribute_noreturn void do_findfs(int argc, char **argv)
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000573{
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000574 if ((argc != 2) ||
575 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5)))
576 bb_show_usage();
Mike Frysinger7f782da2005-10-02 08:10:31 +0000577 device_name = x_blkid_get_devname(argv[1]);
578 puts(device_name);
Mike Frysinger0f8a6382005-09-24 06:07:34 +0000579 exit(0);
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000580}
Mike Frysinger7f782da2005-10-02 08:10:31 +0000581#else
582#define do_findfs(x, y)
Mike Frysingera1c6a572005-09-24 05:55:03 +0000583#endif
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000584
Mike Frysinger7f782da2005-10-02 08:10:31 +0000585static void clean_up(void)
586{
587 if (ENABLE_FEATURE_CLEAN_UP && device_name) free(device_name);
588 if (ENABLE_FEATURE_CLEAN_UP && journal_device) free(journal_device);
589}
590
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000591int tune2fs_main(int argc, char **argv)
592{
593 errcode_t retval;
594 ext2_filsys fs;
595 struct ext2_super_block *sb;
596 io_manager io_ptr;
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000597
Mike Frysinger7f782da2005-10-02 08:10:31 +0000598 if (ENABLE_FEATURE_CLEAN_UP)
599 atexit(clean_up);
600
601 if (ENABLE_FINDFS && (bb_applet_name[0] == 'f')) /* findfs */
602 do_findfs(argc, argv); /* no return */
603 else if (ENABLE_E2LABEL && (bb_applet_name[0] == 'e')) /* e2label */
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000604 parse_e2label_options(argc, argv);
605 else
Mike Frysinger7f782da2005-10-02 08:10:31 +0000606 parse_tune2fs_options(argc, argv); /* tune2fs */
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000607
608 io_ptr = unix_io_manager;
609 retval = ext2fs_open2(device_name, io_options, open_flag,
610 0, 0, io_ptr, &fs);
611 if (retval)
612 bb_error_msg_and_die("No valid superblock on %s", device_name);
613 sb = fs->super;
614 if (print_label) {
615 /* For e2label emulation */
616 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
617 sb->s_volume_name);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000618 return 0;
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000619 }
620 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
621 if (retval)
622 bb_error_msg_and_die("Could not determine if %s is mounted", device_name);
623 /* Normally we only need to write out the superblock */
624 fs->flags |= EXT2_FLAG_SUPER_ONLY;
625
626 if (c_flag) {
627 sb->s_max_mnt_count = max_mount_count;
628 ext2fs_mark_super_dirty(fs);
629 printf("Setting maximal mount count to %d\n", max_mount_count);
630 }
631 if (C_flag) {
632 sb->s_mnt_count = mount_count;
633 ext2fs_mark_super_dirty(fs);
634 printf("Setting current mount count to %d\n", mount_count);
635 }
636 if (e_flag) {
637 sb->s_errors = errors;
638 ext2fs_mark_super_dirty(fs);
639 printf("Setting error behavior to %d\n", errors);
640 }
641 if (g_flag) {
642 sb->s_def_resgid = resgid;
643 ext2fs_mark_super_dirty(fs);
644 printf("Setting reserved blocks gid to %lu\n", resgid);
645 }
646 if (i_flag) {
647 sb->s_checkinterval = interval;
648 ext2fs_mark_super_dirty(fs);
649 printf("Setting interval between check %lu seconds\n", interval);
650 }
651 if (m_flag) {
652 sb->s_r_blocks_count = (sb->s_blocks_count / 100)
653 * reserved_ratio;
654 ext2fs_mark_super_dirty(fs);
655 printf("Setting reserved blocks percentage to %lu (%u blocks)\n",
656 reserved_ratio, sb->s_r_blocks_count);
657 }
658 if (r_flag) {
659 if (reserved_blocks >= sb->s_blocks_count/2)
660 bb_error_msg_and_die("reserved blocks count is too big (%lu)", reserved_blocks);
661 sb->s_r_blocks_count = reserved_blocks;
662 ext2fs_mark_super_dirty(fs);
663 printf("Setting reserved blocks count to %lu\n", reserved_blocks);
664 }
665 if (s_flag == 1) {
666 if (sb->s_feature_ro_compat &
667 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
668 bb_error_msg("\nThe filesystem already has sparse superblocks\n");
669 else {
670 sb->s_feature_ro_compat |=
671 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
672 sb->s_state &= ~EXT2_VALID_FS;
673 ext2fs_mark_super_dirty(fs);
674 printf("\nSparse superblock flag set. %s", please_fsck);
675 }
676 }
677 if (s_flag == 0) {
678 if (!(sb->s_feature_ro_compat &
679 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
680 bb_error_msg("\nThe filesystem already has sparse superblocks disabled\n");
681 else {
682 sb->s_feature_ro_compat &=
683 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
684 sb->s_state &= ~EXT2_VALID_FS;
685 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
686 ext2fs_mark_super_dirty(fs);
687 printf("\nSparse superblock flag cleared. %s", please_fsck);
688 }
689 }
690 if (T_flag) {
691 sb->s_lastcheck = last_check_time;
692 ext2fs_mark_super_dirty(fs);
693 printf("Setting time filesystem last checked to %s\n",
694 ctime(&last_check_time));
695 }
696 if (u_flag) {
697 sb->s_def_resuid = resuid;
698 ext2fs_mark_super_dirty(fs);
699 printf("Setting reserved blocks uid to %lu\n", resuid);
700 }
701 if (L_flag) {
702 if (strlen(new_label) > sizeof(sb->s_volume_name))
703 bb_error_msg("Warning: label too long, truncating\n");
704 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
705 strncpy(sb->s_volume_name, new_label,
706 sizeof(sb->s_volume_name));
707 ext2fs_mark_super_dirty(fs);
708 }
709 if (M_flag) {
710 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
711 strncpy(sb->s_last_mounted, new_last_mounted,
712 sizeof(sb->s_last_mounted));
713 ext2fs_mark_super_dirty(fs);
714 }
715 if (mntopts_cmd)
716 update_mntopts(fs, mntopts_cmd);
717 if (features_cmd)
718 update_feature_set(fs, features_cmd);
719 if (journal_size || journal_device)
720 add_journal(fs);
721
722 if (U_flag) {
723 if ((strcasecmp(new_UUID, "null") == 0) ||
724 (strcasecmp(new_UUID, "clear") == 0)) {
725 uuid_clear(sb->s_uuid);
726 } else if (strcasecmp(new_UUID, "time") == 0) {
727 uuid_generate_time(sb->s_uuid);
728 } else if (strcasecmp(new_UUID, "random") == 0) {
729 uuid_generate(sb->s_uuid);
730 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
731 bb_error_msg_and_die("Invalid UUID format");
732 }
733 ext2fs_mark_super_dirty(fs);
734 }
735
736 if (l_flag)
737 list_super (sb);
738 return (ext2fs_close (fs) ? 1 : 0);
739}