2 * tune2fs.c - Change the file system parameters on
3 * an unmounted second extended file system
5 * Copyright (C) 1992, 1993, 1994 Remy Card <card@masi.ibp.fr>
6 * Laboratoire MASI, Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
9 * Copyright 1995, 1996, 1997 by Theodore Ts'o.
12 * This file may be redistributed under the terms of the GNU Public
20 * 93/10/31 - Added the -c option to change the maximal mount counts
21 * 93/12/14 - Added -l flag to list contents of superblock
22 * M.J.E. Mol (marcel@duteca.et.tudelft.nl)
23 * F.W. ten Wolde (franky@duteca.et.tudelft.nl)
24 * 93/12/29 - Added the -e option to change errors behavior
25 * 94/02/27 - Ported to use the ext2fs library
26 * 94/03/06 - Added the checks interval from Uwe Ohse (uwe@tirka.gun.de)
43 #include <sys/types.h>
45 #include <linux/ext2_fs.h>
47 #include "ext2fs/ext2fs.h"
48 #include "et/com_err.h"
49 #include "uuid/uuid.h"
53 #include "../version.h"
54 #include "nls-enable.h"
56 const char * program_name = "tune2fs";
58 char * new_label, *new_last_mounted, *new_UUID;
59 const char *journal_opts;
60 static int c_flag, C_flag, e_flag, g_flag, i_flag, l_flag, L_flag;
61 static int m_flag, M_flag, r_flag, s_flag = -1, u_flag, U_flag;
62 static int print_label;
63 static int max_mount_count, mount_count, mount_flags;
64 static unsigned long interval, reserved_ratio, reserved_blocks;
65 static unsigned long resgid, resuid;
66 static unsigned short errors;
68 static char *features_cmd;
70 int journal_size, journal_flags;
73 static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
75 static void usage(void)
78 _("Usage: %s [-c max-mounts-count] [-e errors-behavior] "
80 "\t[-i interval[d|m|w]] [-j journal-options]\n"
81 "\t[-l] [-s sparse-flag] [-m reserved-blocks-percent]\n"
82 "\t[-r reserved-blocks-count] [-u user] [-C mount-count]\n"
83 "\t[-L volume-label] [-M last-mounted-dir] [-U UUID]\n"
84 "\t[-O [^]feature[,...]] device\n"), program_name);
88 static __u32 ok_features[3] = {
89 EXT3_FEATURE_COMPAT_HAS_JOURNAL, /* Compat */
90 EXT2_FEATURE_INCOMPAT_FILETYPE, /* Incompat */
91 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
95 * Update the feature set as provided by the user.
97 static void update_feature_set(ext2_filsys fs, char *features)
99 int sparse, old_sparse, filetype, old_filetype;
100 int journal, old_journal;
101 struct ext2_inode inode;
102 struct ext2_super_block *sb= fs->super;
105 old_sparse = sb->s_feature_ro_compat &
106 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
107 old_filetype = sb->s_feature_incompat &
108 EXT2_FEATURE_INCOMPAT_FILETYPE;
109 old_journal = sb->s_feature_compat &
110 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
111 if (e2p_edit_feature(features, &sb->s_feature_compat,
113 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
117 sparse = sb->s_feature_ro_compat &
118 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
119 filetype = sb->s_feature_incompat &
120 EXT2_FEATURE_INCOMPAT_FILETYPE;
121 journal = sb->s_feature_compat &
122 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
123 if (old_journal && !journal) {
124 if ((mount_flags & EXT2_MF_MOUNTED) &&
125 !(mount_flags & EXT2_MF_READONLY)) {
127 _("The HAS_JOURNAL flag may only be "
128 "cleared when the filesystem is\n"
129 "unmounted or mounted "
133 if (sb->s_feature_incompat &
134 EXT3_FEATURE_INCOMPAT_RECOVER) {
136 _("The NEEDS_RECOVERY flag is set. "
137 "Please run e2fsck before clearing\n"
138 "the HAS_JOURNAL flag.\n"));
142 * Remove the immutable flag on the journal inode
144 if (sb->s_journal_inum) {
145 retval = ext2fs_read_inode(fs, sb->s_journal_inum,
148 com_err(program_name, retval,
149 "while reading journal inode");
152 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
153 retval = ext2fs_write_inode(fs, sb->s_journal_inum,
156 com_err(program_name, retval,
157 "while write journal inode");
162 if (journal && !old_journal) {
164 * If adding a journal flag, let the create journal
165 * code below handle creating setting the flag and
166 * creating the journal. We supply a default size if
170 journal_opts = "size=16";
171 sb->s_feature_compat &=~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
172 journal = old_journal;
175 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
176 (sb->s_feature_compat || sb->s_feature_ro_compat ||
177 sb->s_feature_incompat))
178 ext2fs_update_dynamic_rev(fs);
179 if ((sparse != old_sparse) ||
180 (filetype != old_filetype) ||
181 (journal != old_journal)) {
182 sb->s_state &= ~EXT2_VALID_FS;
183 printf("\n%s\n", _(please_fsck));
185 ext2fs_mark_super_dirty(fs);
189 * Add a journal to the filesystem.
191 static void add_journal(ext2_filsys fs)
193 unsigned long journal_blocks;
196 if (fs->super->s_feature_compat &
197 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
198 fprintf(stderr, _("The filesystem already has a journal.\n"));
201 parse_journal_opts(journal_opts);
202 journal_blocks = journal_size * 1024 / (fs->blocksize / 1024);
203 if (journal_device) {
204 check_plausibility(journal_device);
205 check_mount(journal_device, 0, _("journal"));
206 printf(_("Creating journal on device %s: "),
208 retval = ext2fs_add_journal_device(fs, journal_device,
212 com_err (program_name, retval,
213 _("while trying to create journal on device %s"),
218 } else if (journal_size) {
219 printf(_("Creating journal inode: "));
221 retval = ext2fs_add_journal_inode(fs, journal_blocks,
225 com_err(program_name, retval,
226 _("while trying to create journal"));
231 * If the filesystem wasn't mounted, we need to force
232 * the block group descriptors out.
234 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
235 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
240 * Given argv[0], return the program name.
242 static char *get_progname(char *argv_zero)
246 cp = strrchr(argv_zero, '/');
254 static void parse_e2label_options(int argc, char ** argv)
256 if ((argc < 2) || (argc > 3)) {
257 fprintf(stderr, _("Usage: e2label device [newlabel]\n"));
260 device_name = argv[1];
262 open_flag = EXT2_FLAG_RW;
270 static void parse_tune2fs_options(int argc, char **argv)
277 fprintf (stderr, _("tune2fs %s, %s for EXT2 FS %s, %s\n"),
278 E2FSPROGS_VERSION, E2FSPROGS_DATE,
279 EXT2FS_VERSION, EXT2FS_DATE);
280 while ((c = getopt (argc, argv, "c:e:g:i:j:lm:r:s:u:C:L:M:O:U:")) != EOF)
284 max_mount_count = strtol (optarg, &tmp, 0);
285 if (*tmp || max_mount_count > 16000) {
286 com_err (program_name, 0,
287 _("bad mounts count - %s"),
292 open_flag = EXT2_FLAG_RW;
295 mount_count = strtoul (optarg, &tmp, 0);
296 if (*tmp || mount_count > 16000) {
297 com_err (program_name, 0,
298 _("bad mounts count - %s"),
303 open_flag = EXT2_FLAG_RW;
306 if (strcmp (optarg, "continue") == 0)
307 errors = EXT2_ERRORS_CONTINUE;
308 else if (strcmp (optarg, "remount-ro") == 0)
309 errors = EXT2_ERRORS_RO;
310 else if (strcmp (optarg, "panic") == 0)
311 errors = EXT2_ERRORS_PANIC;
313 com_err (program_name, 0,
314 _("bad error behavior - %s"),
319 open_flag = EXT2_FLAG_RW;
322 resgid = strtoul (optarg, &tmp, 0);
324 gr = getgrnam (optarg);
333 com_err (program_name, 0,
334 _("bad gid/group name - %s"),
339 open_flag = EXT2_FLAG_RW;
342 interval = strtoul (optarg, &tmp, 0);
355 case 'M': /* months! */
356 interval *= 86400 * 30;
360 case 'W': /* weeks */
361 interval *= 86400 * 7;
365 if (*tmp || interval > (365 * 86400)) {
366 com_err (program_name, 0,
367 _("bad interval - %s"), optarg);
371 open_flag = EXT2_FLAG_RW;
377 journal_opts = optarg;
378 open_flag = EXT2_FLAG_RW;
383 open_flag = EXT2_FLAG_RW;
386 reserved_ratio = strtoul (optarg, &tmp, 0);
387 if (*tmp || reserved_ratio > 50) {
388 com_err (program_name, 0,
389 _("bad reserved block ratio - %s"),
394 open_flag = EXT2_FLAG_RW;
397 new_last_mounted = optarg;
399 open_flag = EXT2_FLAG_RW;
402 features_cmd = optarg;
403 open_flag = EXT2_FLAG_RW;
406 reserved_blocks = strtoul (optarg, &tmp, 0);
408 com_err (program_name, 0,
409 _("bad reserved blocks count - %s"),
414 open_flag = EXT2_FLAG_RW;
417 s_flag = atoi(optarg);
418 open_flag = EXT2_FLAG_RW;
421 resuid = strtoul (optarg, &tmp, 0);
423 pw = getpwnam (optarg);
432 com_err (program_name, 0,
433 _("bad uid/user name - %s"),
438 open_flag = EXT2_FLAG_RW;
443 open_flag = EXT2_FLAG_RW;
448 if (optind < argc - 1 || optind == argc)
450 if (!open_flag && !l_flag)
452 device_name = argv[optind];
457 int main (int argc, char ** argv)
461 struct ext2_super_block *sb;
464 setlocale(LC_MESSAGES, "");
465 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
466 textdomain(NLS_CAT_NAME);
469 program_name = *argv;
470 initialize_ext2_error_table();
472 if (strcmp(get_progname(argv[0]), "e2label") == 0)
473 parse_e2label_options(argc, argv);
475 parse_tune2fs_options(argc, argv);
477 retval = ext2fs_open (device_name, open_flag, 0, 0,
478 unix_io_manager, &fs);
480 com_err (program_name, retval, _("while trying to open %s"),
482 printf(_("Couldn't find valid filesystem superblock.\n"));
487 /* For e2label emulation */
488 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
492 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
494 com_err("ext2fs_check_if_mount", retval,
495 _("while determining whether %s is mounted."),
499 /* Normally we only need to write out the superblock */
500 fs->flags |= EXT2_FLAG_SUPER_ONLY;
503 sb->s_max_mnt_count = max_mount_count;
504 ext2fs_mark_super_dirty(fs);
505 printf (_("Setting maximal mount count to %d\n"),
509 sb->s_mnt_count = mount_count;
510 ext2fs_mark_super_dirty(fs);
511 printf (_("Setting current mount count to %d\n"), mount_count);
514 sb->s_errors = errors;
515 ext2fs_mark_super_dirty(fs);
516 printf (_("Setting error behavior to %d\n"), errors);
519 sb->s_def_resgid = resgid;
520 ext2fs_mark_super_dirty(fs);
521 printf (_("Setting reserved blocks gid to %lu\n"), resgid);
524 sb->s_checkinterval = interval;
525 ext2fs_mark_super_dirty(fs);
526 printf (_("Setting interval between check %lu seconds\n"), interval);
529 sb->s_r_blocks_count = (sb->s_blocks_count / 100)
531 ext2fs_mark_super_dirty(fs);
532 printf (_("Setting reserved blocks percentage to %lu (%u blocks)\n"),
533 reserved_ratio, sb->s_r_blocks_count);
536 if (reserved_blocks >= sb->s_blocks_count) {
537 com_err (program_name, 0,
538 _("reserved blocks count is too big (%ul)"),
542 sb->s_r_blocks_count = reserved_blocks;
543 ext2fs_mark_super_dirty(fs);
544 printf (_("Setting reserved blocks count to %lu\n"),
548 if (sb->s_feature_ro_compat &
549 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
550 fprintf(stderr, _("\nThe filesystem already"
551 " has sparse superblocks.\n"));
553 sb->s_feature_ro_compat |=
554 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
555 sb->s_state &= ~EXT2_VALID_FS;
556 ext2fs_mark_super_dirty(fs);
557 printf(_("\nSparse superblock flag set. %s"),
562 if (!(sb->s_feature_ro_compat &
563 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
564 fprintf(stderr, _("\nThe filesystem already"
565 " has sparse superblocks disabled.\n"));
567 sb->s_feature_ro_compat &=
568 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
569 sb->s_state &= ~EXT2_VALID_FS;
570 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
571 ext2fs_mark_super_dirty(fs);
572 printf(_("\nSparse superblock flag cleared. %s"),
577 sb->s_def_resuid = resuid;
578 ext2fs_mark_super_dirty(fs);
579 printf (_("Setting reserved blocks uid to %lu\n"), resuid);
582 if (strlen(new_label) > sizeof(sb->s_volume_name))
583 fprintf(stderr, _("Warning: label too "
584 "long, truncating.\n"));
585 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
586 strncpy(sb->s_volume_name, new_label,
587 sizeof(sb->s_volume_name));
588 ext2fs_mark_super_dirty(fs);
591 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
592 strncpy(sb->s_last_mounted, new_last_mounted,
593 sizeof(sb->s_last_mounted));
594 ext2fs_mark_super_dirty(fs);
597 update_feature_set(fs, features_cmd);
602 if (strcasecmp(new_UUID, "null") == 0) {
603 uuid_clear(sb->s_uuid);
604 } else if (strcasecmp(new_UUID, "time") == 0) {
605 uuid_generate_time(sb->s_uuid);
606 } else if (strcasecmp(new_UUID, "random") == 0) {
607 uuid_generate(sb->s_uuid);
608 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
609 com_err(program_name, 0, _("Invalid UUID format\n"));
612 ext2fs_mark_super_dirty(fs);