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)
40 #include <sys/types.h>
42 #include <linux/ext2_fs.h>
44 #include "ext2fs/ext2fs.h"
45 #include "et/com_err.h"
46 #include "uuid/uuid.h"
49 #include "../version.h"
51 const char * program_name = "tune2fs";
52 char * device_name = NULL;
53 char * new_label = NULL;
54 char * new_last_mounted = NULL;
55 char * new_UUID = NULL;
69 int max_mount_count, mount_count;
70 unsigned long interval;
71 unsigned long reserved_ratio = 0;
72 unsigned long reserved_blocks = 0;
73 unsigned short errors;
74 unsigned long resgid = 0;
75 unsigned long resuid = 0;
77 #ifndef HAVE_STRCASECMP
78 static int strcasecmp (char *s1, char *s2)
81 int ch1 = *s1++, ch2 = *s2++;
89 return *s1 ? 1 : *s2 ? -1 : 0;
93 static void usage(void)
95 fprintf(stderr, "Usage: %s [-c max-mounts-count] [-e errors-behavior] "
97 "\t[-i interval[d|m|w]] [-l] [-s] [-m reserved-blocks-percent]\n"
98 "\t[-r reserved-blocks-count] [-u user] [-C mount-count]\n"
99 "\t[-L volume-label] [-M last-mounted-dir] [-U UUID] "
100 "device\n", program_name);
104 int main (int argc, char ** argv)
110 struct ext2fs_sb *sb;
115 fprintf (stderr, "tune2fs %s, %s for EXT2 FS %s, %s\n",
116 E2FSPROGS_VERSION, E2FSPROGS_DATE,
117 EXT2FS_VERSION, EXT2FS_DATE);
119 program_name = *argv;
120 initialize_ext2_error_table();
121 while ((c = getopt (argc, argv, "c:e:g:i:lm:r:s:u:C:L:M:U:")) != EOF)
125 max_mount_count = strtoul (optarg, &tmp, 0);
126 if (*tmp || max_mount_count > 16000) {
127 com_err (program_name, 0,
128 "bad mounts count - %s",
133 open_flag = EXT2_FLAG_RW;
136 mount_count = strtoul (optarg, &tmp, 0);
137 if (*tmp || mount_count > 16000) {
138 com_err (program_name, 0,
139 "bad mounts count - %s",
144 open_flag = EXT2_FLAG_RW;
147 if (strcmp (optarg, "continue") == 0)
148 errors = EXT2_ERRORS_CONTINUE;
149 else if (strcmp (optarg, "remount-ro") == 0)
150 errors = EXT2_ERRORS_RO;
151 else if (strcmp (optarg, "panic") == 0)
152 errors = EXT2_ERRORS_PANIC;
154 com_err (program_name, 0,
155 "bad error behavior - %s",
160 open_flag = EXT2_FLAG_RW;
163 resgid = strtoul (optarg, &tmp, 0);
165 gr = getgrnam (optarg);
174 com_err (program_name, 0,
175 "bad gid/group name - %s",
180 open_flag = EXT2_FLAG_RW;
183 interval = strtoul (optarg, &tmp, 0);
196 case 'M': /* months! */
197 interval *= 86400 * 30;
201 case 'W': /* weeks */
202 interval *= 86400 * 7;
206 if (*tmp || interval > (365 * 86400)) {
207 com_err (program_name, 0,
208 "bad interval - %s", optarg);
212 open_flag = EXT2_FLAG_RW;
220 open_flag = EXT2_FLAG_RW;
223 reserved_ratio = strtoul (optarg, &tmp, 0);
224 if (*tmp || reserved_ratio > 50) {
225 com_err (program_name, 0,
226 "bad reserved block ratio - %s",
231 open_flag = EXT2_FLAG_RW;
234 new_last_mounted = optarg;
236 open_flag = EXT2_FLAG_RW;
239 reserved_blocks = strtoul (optarg, &tmp, 0);
241 com_err (program_name, 0,
242 "bad reserved blocks count - %s",
247 open_flag = EXT2_FLAG_RW;
250 s_flag = atoi(optarg);
251 open_flag = EXT2_FLAG_RW;
254 resuid = strtoul (optarg, &tmp, 0);
256 pw = getpwnam (optarg);
265 com_err (program_name, 0,
266 "bad uid/user name - %s",
271 open_flag = EXT2_FLAG_RW;
276 open_flag = EXT2_FLAG_RW;
281 if (optind < argc - 1 || optind == argc)
283 if (!open_flag && !l_flag)
285 device_name = argv[optind];
286 retval = ext2fs_open (device_name, open_flag, 0, 0,
287 unix_io_manager, &fs);
289 com_err (program_name, retval, "while trying to open %s",
291 printf("Couldn't find valid filesystem superblock.\n");
294 sb = (struct ext2fs_sb *) fs->super;
297 fs->super->s_max_mnt_count = max_mount_count;
298 ext2fs_mark_super_dirty(fs);
299 printf ("Setting maximal mount count to %d\n",
303 fs->super->s_mnt_count = mount_count;
304 ext2fs_mark_super_dirty(fs);
305 printf ("Setting current mount count to %d\n", mount_count);
308 fs->super->s_errors = errors;
309 ext2fs_mark_super_dirty(fs);
310 printf ("Setting error behavior to %d\n", errors);
313 #ifdef EXT2_DEF_RESGID
315 fs->super->s_def_resgid = resgid;
316 ext2fs_mark_super_dirty(fs);
317 printf ("Setting reserved blocks gid to %lu\n", resgid);
320 com_err (program_name, 0,
321 "The -g option is not supported by this version -- "
322 "Recompile with a newer kernel");
325 fs->super->s_checkinterval = interval;
326 ext2fs_mark_super_dirty(fs);
327 printf ("Setting interval between check %lu seconds\n", interval);
330 fs->super->s_r_blocks_count = (fs->super->s_blocks_count / 100)
332 ext2fs_mark_super_dirty(fs);
333 printf ("Setting reserved blocks percentage to %lu (%u blocks)\n",
334 reserved_ratio, fs->super->s_r_blocks_count);
337 if (reserved_blocks >= fs->super->s_blocks_count) {
338 com_err (program_name, 0,
339 "reserved blocks count is too big (%ul)",
343 fs->super->s_r_blocks_count = reserved_blocks;
344 ext2fs_mark_super_dirty(fs);
345 printf ("Setting reserved blocks count to %lu\n",
349 #ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
350 if (sb->s_feature_ro_compat &
351 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
352 fprintf(stderr, "\nThe filesystem already "
353 " has spare superblocks.\n");
355 sb->s_feature_ro_compat |=
356 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
357 fs->super->s_state &= ~EXT2_VALID_FS;
358 ext2fs_mark_super_dirty(fs);
359 printf("\nSparse superblock flag set. "
360 "Please run e2fsck on the filesystem.\n");
362 #else /* !EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER */
363 com_err (program_name, 0,
364 "The -s option is not supported by this version -- "
365 "Recompile with a newer kernel");
366 #endif /* EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER */
369 #ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
370 if (!(sb->s_feature_ro_compat &
371 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
372 fprintf(stderr, "\nThe filesystem already "
373 " does not support spare superblocks.\n");
375 sb->s_feature_ro_compat &=
376 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
377 fs->super->s_state &= ~EXT2_VALID_FS;
378 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
379 ext2fs_mark_super_dirty(fs);
380 printf("\nSparse superblock flag cleared. "
381 "Please run e2fsck on the filesystem.\n");
383 #else /* !EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER */
384 com_err (program_name, 0,
385 "The -s option is not supported by this version -- "
386 "Recompile with a newer kernel");
387 #endif /* EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER */
391 #ifdef EXT2_DEF_RESUID
393 fs->super->s_def_resuid = resuid;
394 ext2fs_mark_super_dirty(fs);
395 printf ("Setting reserved blocks uid to %lu\n", resuid);
398 com_err (program_name, 0,
399 "The -u option is not supported by this version -- "
400 "Recompile with a newer kernel");
403 if (strlen(new_label) > sizeof(sb->s_volume_name))
404 fprintf(stderr, "Warning: label too "
405 "long, truncating.\n");
406 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
407 strncpy(sb->s_volume_name, new_label,
408 sizeof(sb->s_volume_name));
409 ext2fs_mark_super_dirty(fs);
412 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
413 strncpy(sb->s_last_mounted, new_last_mounted,
414 sizeof(sb->s_last_mounted));
415 ext2fs_mark_super_dirty(fs);
418 if (strcasecmp(new_UUID, "null") == 0) {
419 uuid_clear(sb->s_uuid);
420 } else if (strcasecmp(new_UUID, "random") == 0) {
421 uuid_generate(sb->s_uuid);
422 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
423 com_err(program_name, 0, "Invalid UUID format\n");
426 ext2fs_mark_super_dirty(fs);
430 list_super (fs->super);