Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 1 | /* |
| 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 |
| 33 | extern char *optarg; |
| 34 | extern 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 | |
| 53 | static char * device_name; |
| 54 | static char * new_label, *new_last_mounted, *new_UUID; |
| 55 | static char * io_options; |
| 56 | static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag; |
| 57 | static int m_flag, M_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag; |
| 58 | static time_t last_check_time; |
| 59 | static int print_label; |
| 60 | static int max_mount_count, mount_count, mount_flags; |
| 61 | static unsigned long interval, reserved_ratio, reserved_blocks; |
| 62 | static unsigned long resgid, resuid; |
| 63 | static unsigned short errors; |
| 64 | static int open_flag; |
| 65 | static char *features_cmd; |
| 66 | static char *mntopts_cmd; |
| 67 | |
| 68 | static int journal_size, journal_flags; |
| 69 | static char *journal_device; |
| 70 | |
| 71 | static const char *please_fsck = "Please run e2fsck on the filesystem\n"; |
| 72 | |
| 73 | static __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 | */ |
| 82 | static 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 | |
| 157 | no_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 */ |
| 168 | static 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 | */ |
| 186 | static 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; |
| 191 | |
| 192 | retval = ext2fs_read_inode(fs, ino, &inode); |
| 193 | if (retval) |
| 194 | bb_error_msg_and_die("Failed to read journal inode"); |
| 195 | if (ino == EXT2_JOURNAL_INO) { |
| 196 | retval = ext2fs_read_bitmaps(fs); |
| 197 | if (retval) |
| 198 | bb_error_msg_and_die("Failed to read bitmaps"); |
| 199 | retval = ext2fs_block_iterate(fs, ino, 0, NULL, |
| 200 | release_blocks_proc, NULL); |
| 201 | if (retval) |
| 202 | bb_error_msg_and_die("Failed clearing journal inode"); |
| 203 | memset(&inode, 0, sizeof(inode)); |
| 204 | ext2fs_mark_bb_dirty(fs); |
| 205 | fs->flags &= ~EXT2_FLAG_SUPER_ONLY; |
| 206 | } else |
| 207 | inode.i_flags &= ~EXT2_IMMUTABLE_FL; |
| 208 | retval = ext2fs_write_inode(fs, ino, &inode); |
| 209 | if (retval) |
| 210 | bb_error_msg_and_die("Failed writing journal inode"); |
| 211 | fs->super->s_journal_inum = 0; |
| 212 | ext2fs_mark_super_dirty(fs); |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Update the default mount options |
| 217 | */ |
| 218 | static void update_mntopts(ext2_filsys fs, char *mntopts) |
| 219 | { |
| 220 | struct ext2_super_block *sb= fs->super; |
| 221 | |
| 222 | if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) |
| 223 | bb_error_msg_and_die("Invalid mount option set: %s", mntopts); |
| 224 | ext2fs_mark_super_dirty(fs); |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * Update the feature set as provided by the user. |
| 229 | */ |
| 230 | static void update_feature_set(ext2_filsys fs, char *features) |
| 231 | { |
| 232 | int sparse, old_sparse, filetype, old_filetype; |
| 233 | int journal, old_journal, dxdir, old_dxdir; |
| 234 | struct ext2_super_block *sb= fs->super; |
| 235 | __u32 old_compat, old_incompat, old_ro_compat; |
| 236 | |
| 237 | old_compat = sb->s_feature_compat; |
| 238 | old_ro_compat = sb->s_feature_ro_compat; |
| 239 | old_incompat = sb->s_feature_incompat; |
| 240 | |
| 241 | old_sparse = sb->s_feature_ro_compat & |
| 242 | EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; |
| 243 | old_filetype = sb->s_feature_incompat & |
| 244 | EXT2_FEATURE_INCOMPAT_FILETYPE; |
| 245 | old_journal = sb->s_feature_compat & |
| 246 | EXT3_FEATURE_COMPAT_HAS_JOURNAL; |
| 247 | old_dxdir = sb->s_feature_compat & |
| 248 | EXT2_FEATURE_COMPAT_DIR_INDEX; |
| 249 | if (e2p_edit_feature(features, &sb->s_feature_compat, ok_features)) |
| 250 | bb_error_msg_and_die("Invalid filesystem option set: %s", features); |
| 251 | sparse = sb->s_feature_ro_compat & |
| 252 | EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; |
| 253 | filetype = sb->s_feature_incompat & |
| 254 | EXT2_FEATURE_INCOMPAT_FILETYPE; |
| 255 | journal = sb->s_feature_compat & |
| 256 | EXT3_FEATURE_COMPAT_HAS_JOURNAL; |
| 257 | dxdir = sb->s_feature_compat & |
| 258 | EXT2_FEATURE_COMPAT_DIR_INDEX; |
| 259 | if (old_journal && !journal) { |
| 260 | if ((mount_flags & EXT2_MF_MOUNTED) && |
| 261 | !(mount_flags & EXT2_MF_READONLY)) { |
| 262 | bb_error_msg_and_die( |
| 263 | "The has_journal flag may only be " |
| 264 | "cleared when the filesystem is\n" |
| 265 | "unmounted or mounted " |
| 266 | "read-only"); |
| 267 | } |
| 268 | if (sb->s_feature_incompat & |
| 269 | EXT3_FEATURE_INCOMPAT_RECOVER) { |
| 270 | bb_error_msg_and_die( |
| 271 | "The needs_recovery flag is set. " |
| 272 | "Please run e2fsck before clearing\n" |
| 273 | "the has_journal flag."); |
| 274 | } |
| 275 | if (sb->s_journal_inum) { |
| 276 | remove_journal_inode(fs); |
| 277 | } |
| 278 | if (sb->s_journal_dev) { |
| 279 | remove_journal_device(fs); |
| 280 | } |
| 281 | } |
| 282 | if (journal && !old_journal) { |
| 283 | /* |
| 284 | * If adding a journal flag, let the create journal |
| 285 | * code below handle creating setting the flag and |
| 286 | * creating the journal. We supply a default size if |
| 287 | * necessary. |
| 288 | */ |
| 289 | if (!journal_size) |
| 290 | journal_size = -1; |
| 291 | sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL; |
| 292 | } |
| 293 | if (dxdir && !old_dxdir) { |
| 294 | if (!sb->s_def_hash_version) |
| 295 | sb->s_def_hash_version = EXT2_HASH_TEA; |
| 296 | if (uuid_is_null((unsigned char *) sb->s_hash_seed)) |
| 297 | uuid_generate((unsigned char *) sb->s_hash_seed); |
| 298 | } |
| 299 | |
| 300 | if (sb->s_rev_level == EXT2_GOOD_OLD_REV && |
| 301 | (sb->s_feature_compat || sb->s_feature_ro_compat || |
| 302 | sb->s_feature_incompat)) |
| 303 | ext2fs_update_dynamic_rev(fs); |
| 304 | if ((sparse != old_sparse) || |
| 305 | (filetype != old_filetype)) { |
| 306 | sb->s_state &= ~EXT2_VALID_FS; |
| 307 | printf("\n%s\n", please_fsck); |
| 308 | } |
| 309 | if ((old_compat != sb->s_feature_compat) || |
| 310 | (old_ro_compat != sb->s_feature_ro_compat) || |
| 311 | (old_incompat != sb->s_feature_incompat)) |
| 312 | ext2fs_mark_super_dirty(fs); |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * Add a journal to the filesystem. |
| 317 | */ |
| 318 | static void add_journal(ext2_filsys fs) |
| 319 | { |
| 320 | unsigned long journal_blocks; |
| 321 | errcode_t retval; |
| 322 | ext2_filsys jfs; |
| 323 | io_manager io_ptr; |
| 324 | |
| 325 | if (fs->super->s_feature_compat & |
| 326 | EXT3_FEATURE_COMPAT_HAS_JOURNAL) { |
| 327 | bb_error_msg("The filesystem already has a journal"); |
| 328 | goto err; |
| 329 | } |
| 330 | if (journal_device) { |
| 331 | check_plausibility(journal_device); |
| 332 | check_mount(journal_device, 0, "journal"); |
| 333 | io_ptr = unix_io_manager; |
| 334 | retval = ext2fs_open(journal_device, EXT2_FLAG_RW| |
| 335 | EXT2_FLAG_JOURNAL_DEV_OK, 0, |
| 336 | fs->blocksize, io_ptr, &jfs); |
| 337 | if (retval) { |
| 338 | bb_error_msg("Failed to open journal on %s", journal_device); |
| 339 | goto err; |
| 340 | } |
| 341 | printf("Creating journal on device %s: ", journal_device); |
| 342 | fflush(stdout); |
| 343 | |
| 344 | retval = ext2fs_add_journal_device(fs, jfs); |
| 345 | ext2fs_close(jfs); |
| 346 | if (retval) { |
| 347 | bb_error_msg("Failed to add filesystem to journal on %s", journal_device); |
| 348 | goto err; |
| 349 | } |
| 350 | puts("done"); |
| 351 | } else if (journal_size) { |
| 352 | fputs("Creating journal inode: ", stdout); |
| 353 | fflush(stdout); |
| 354 | journal_blocks = figure_journal_size(journal_size, fs); |
| 355 | |
| 356 | retval = ext2fs_add_journal_inode(fs, journal_blocks, |
| 357 | journal_flags); |
| 358 | if (retval) |
| 359 | bb_error_msg_and_die("Failed to create journal file"); |
| 360 | else |
| 361 | puts("done"); |
| 362 | /* |
| 363 | * If the filesystem wasn't mounted, we need to force |
| 364 | * the block group descriptors out. |
| 365 | */ |
| 366 | if ((mount_flags & EXT2_MF_MOUNTED) == 0) |
| 367 | fs->flags &= ~EXT2_FLAG_SUPER_ONLY; |
| 368 | } |
| 369 | print_check_message(fs); |
| 370 | return; |
| 371 | |
| 372 | err: |
| 373 | if (journal_device) |
| 374 | free(journal_device); |
| 375 | exit(1); |
| 376 | } |
| 377 | |
Mike Frysinger | a1c6a57 | 2005-09-24 05:55:03 +0000 | [diff] [blame] | 378 | #ifdef CONFIG_E2LABEL |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 379 | static void parse_e2label_options(int argc, char ** argv) |
| 380 | { |
| 381 | if ((argc < 2) || (argc > 3)) |
| 382 | bb_show_usage(); |
| 383 | io_options = strchr(argv[1], '?'); |
| 384 | if (io_options) |
| 385 | *io_options++ = 0; |
| 386 | device_name = blkid_get_devname(NULL, argv[1], NULL); |
| 387 | if (!device_name) |
| 388 | bb_error_msg_and_die("Unable to resolve '%s'", argv[1]); |
| 389 | if (argc == 3) { |
| 390 | open_flag = EXT2_FLAG_RW | EXT2_FLAG_JOURNAL_DEV_OK; |
| 391 | L_flag = 1; |
| 392 | new_label = argv[2]; |
| 393 | } else |
| 394 | print_label++; |
| 395 | } |
Mike Frysinger | a1c6a57 | 2005-09-24 05:55:03 +0000 | [diff] [blame] | 396 | #endif |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 397 | |
| 398 | static time_t parse_time(char *str) |
| 399 | { |
| 400 | struct tm ts; |
| 401 | |
| 402 | if (strcmp(str, "now") == 0) { |
| 403 | return (time(0)); |
| 404 | } |
| 405 | memset(&ts, 0, sizeof(ts)); |
| 406 | #ifdef HAVE_STRPTIME |
| 407 | strptime(str, "%Y%m%d%H%M%S", &ts); |
| 408 | #else |
| 409 | sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon, |
| 410 | &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec); |
| 411 | ts.tm_year -= 1900; |
| 412 | ts.tm_mon -= 1; |
| 413 | if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 || |
| 414 | ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 || |
| 415 | ts.tm_min > 59 || ts.tm_sec > 61) |
| 416 | ts.tm_mday = 0; |
| 417 | #endif |
| 418 | if (ts.tm_mday == 0) { |
| 419 | bb_error_msg_and_die("Couldn't parse date/time specifier: %s", str); |
| 420 | } |
| 421 | return (mktime(&ts)); |
| 422 | } |
| 423 | |
| 424 | static void parse_tune2fs_options(int argc, char **argv) |
| 425 | { |
| 426 | int c; |
| 427 | char * tmp; |
| 428 | struct group * gr; |
| 429 | struct passwd * pw; |
| 430 | |
| 431 | printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE); |
| 432 | while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:J:L:M:O:T:U:")) != EOF) |
| 433 | switch (c) |
| 434 | { |
| 435 | case 'c': |
| 436 | max_mount_count = strtol (optarg, &tmp, 0); |
| 437 | if (*tmp || max_mount_count > 16000) { |
| 438 | bb_error_msg_and_die("bad mounts count - %s", optarg); |
| 439 | } |
| 440 | if (max_mount_count == 0) |
| 441 | max_mount_count = -1; |
| 442 | c_flag = 1; |
| 443 | open_flag = EXT2_FLAG_RW; |
| 444 | break; |
| 445 | case 'C': |
| 446 | mount_count = strtoul (optarg, &tmp, 0); |
| 447 | if (*tmp || mount_count > 16000) { |
| 448 | bb_error_msg_and_die("bad mounts count - %s", optarg); |
| 449 | } |
| 450 | C_flag = 1; |
| 451 | open_flag = EXT2_FLAG_RW; |
| 452 | break; |
| 453 | case 'e': |
| 454 | if (strcmp (optarg, "continue") == 0) |
| 455 | errors = EXT2_ERRORS_CONTINUE; |
| 456 | else if (strcmp (optarg, "remount-ro") == 0) |
| 457 | errors = EXT2_ERRORS_RO; |
| 458 | else if (strcmp (optarg, "panic") == 0) |
| 459 | errors = EXT2_ERRORS_PANIC; |
| 460 | else { |
| 461 | bb_error_msg_and_die("bad error behavior - %s", optarg); |
| 462 | } |
| 463 | e_flag = 1; |
| 464 | open_flag = EXT2_FLAG_RW; |
| 465 | break; |
| 466 | case 'f': /* Force */ |
| 467 | f_flag = 1; |
| 468 | break; |
| 469 | case 'g': |
| 470 | resgid = strtoul (optarg, &tmp, 0); |
| 471 | if (*tmp) { |
| 472 | gr = getgrnam (optarg); |
| 473 | if (gr == NULL) |
| 474 | tmp = optarg; |
| 475 | else { |
| 476 | resgid = gr->gr_gid; |
| 477 | *tmp =0; |
| 478 | } |
| 479 | } |
| 480 | if (*tmp) { |
| 481 | bb_error_msg_and_die("bad gid/group name - %s", optarg); |
| 482 | } |
| 483 | g_flag = 1; |
| 484 | open_flag = EXT2_FLAG_RW; |
| 485 | break; |
| 486 | case 'i': |
| 487 | interval = strtoul (optarg, &tmp, 0); |
| 488 | switch (*tmp) { |
| 489 | case 's': |
| 490 | tmp++; |
| 491 | break; |
| 492 | case '\0': |
| 493 | case 'd': |
| 494 | case 'D': /* days */ |
| 495 | interval *= 86400; |
| 496 | if (*tmp != '\0') |
| 497 | tmp++; |
| 498 | break; |
| 499 | case 'm': |
| 500 | case 'M': /* months! */ |
| 501 | interval *= 86400 * 30; |
| 502 | tmp++; |
| 503 | break; |
| 504 | case 'w': |
| 505 | case 'W': /* weeks */ |
| 506 | interval *= 86400 * 7; |
| 507 | tmp++; |
| 508 | break; |
| 509 | } |
| 510 | if (*tmp || interval > (365 * 86400)) { |
| 511 | bb_error_msg_and_die("bad interval - %s", optarg); |
| 512 | } |
| 513 | i_flag = 1; |
| 514 | open_flag = EXT2_FLAG_RW; |
| 515 | break; |
| 516 | case 'j': |
| 517 | if (!journal_size) |
| 518 | journal_size = -1; |
| 519 | open_flag = EXT2_FLAG_RW; |
| 520 | break; |
| 521 | case 'J': |
| 522 | parse_journal_opts(&journal_device, &journal_flags, &journal_size, optarg); |
| 523 | open_flag = EXT2_FLAG_RW; |
| 524 | break; |
| 525 | case 'l': |
| 526 | l_flag = 1; |
| 527 | break; |
| 528 | case 'L': |
| 529 | new_label = optarg; |
| 530 | L_flag = 1; |
| 531 | open_flag = EXT2_FLAG_RW | |
| 532 | EXT2_FLAG_JOURNAL_DEV_OK; |
| 533 | break; |
| 534 | case 'm': |
| 535 | reserved_ratio = strtoul (optarg, &tmp, 0); |
| 536 | if (*tmp || reserved_ratio > 50) { |
| 537 | bb_error_msg_and_die("bad reserved block ratio - %s", optarg); |
| 538 | } |
| 539 | m_flag = 1; |
| 540 | open_flag = EXT2_FLAG_RW; |
| 541 | break; |
| 542 | case 'M': |
| 543 | new_last_mounted = optarg; |
| 544 | M_flag = 1; |
| 545 | open_flag = EXT2_FLAG_RW; |
| 546 | break; |
| 547 | case 'o': |
| 548 | if (mntopts_cmd) { |
| 549 | bb_error_msg_and_die("-o may only be specified once"); |
| 550 | } |
| 551 | mntopts_cmd = optarg; |
| 552 | open_flag = EXT2_FLAG_RW; |
| 553 | break; |
| 554 | |
| 555 | case 'O': |
| 556 | if (features_cmd) { |
| 557 | bb_error_msg_and_die("-O may only be specified once"); |
| 558 | } |
| 559 | features_cmd = optarg; |
| 560 | open_flag = EXT2_FLAG_RW; |
| 561 | break; |
| 562 | case 'r': |
| 563 | reserved_blocks = strtoul (optarg, &tmp, 0); |
| 564 | if (*tmp) { |
| 565 | bb_error_msg_and_die("bad reserved blocks count - %s", optarg); |
| 566 | } |
| 567 | r_flag = 1; |
| 568 | open_flag = EXT2_FLAG_RW; |
| 569 | break; |
| 570 | case 's': |
| 571 | s_flag = atoi(optarg); |
| 572 | open_flag = EXT2_FLAG_RW; |
| 573 | break; |
| 574 | case 'T': |
| 575 | T_flag = 1; |
| 576 | last_check_time = parse_time(optarg); |
| 577 | open_flag = EXT2_FLAG_RW; |
| 578 | break; |
| 579 | case 'u': |
| 580 | resuid = strtoul (optarg, &tmp, 0); |
| 581 | if (*tmp) { |
| 582 | pw = getpwnam (optarg); |
| 583 | if (pw == NULL) |
| 584 | tmp = optarg; |
| 585 | else { |
| 586 | resuid = pw->pw_uid; |
| 587 | *tmp = 0; |
| 588 | } |
| 589 | } |
| 590 | if (*tmp) { |
| 591 | bb_error_msg_and_die("bad uid/user name - %s", optarg); |
| 592 | } |
| 593 | u_flag = 1; |
| 594 | open_flag = EXT2_FLAG_RW; |
| 595 | break; |
| 596 | case 'U': |
| 597 | new_UUID = optarg; |
| 598 | U_flag = 1; |
| 599 | open_flag = EXT2_FLAG_RW | |
| 600 | EXT2_FLAG_JOURNAL_DEV_OK; |
| 601 | break; |
| 602 | default: |
| 603 | bb_show_usage(); |
| 604 | } |
| 605 | if (optind < argc - 1 || optind == argc) |
| 606 | bb_show_usage(); |
| 607 | if (!open_flag && !l_flag) |
| 608 | bb_show_usage(); |
| 609 | io_options = strchr(argv[optind], '?'); |
| 610 | if (io_options) |
| 611 | *io_options++ = 0; |
| 612 | device_name = blkid_get_devname(NULL, argv[optind], NULL); |
| 613 | if (!device_name) |
| 614 | bb_error_msg_and_die("Unable to resolve '%s'", argv[optind]); |
| 615 | } |
| 616 | |
Mike Frysinger | a1c6a57 | 2005-09-24 05:55:03 +0000 | [diff] [blame] | 617 | #ifdef CONFIG_FINDFS |
Mike Frysinger | 0f8a638 | 2005-09-24 06:07:34 +0000 | [diff] [blame] | 618 | static attribute_noreturn void do_findfs(int argc, char **argv) |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 619 | { |
| 620 | char *dev; |
| 621 | |
| 622 | if ((argc != 2) || |
| 623 | (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) |
| 624 | bb_show_usage(); |
| 625 | dev = blkid_get_devname(NULL, argv[1], NULL); |
| 626 | if (!dev) |
| 627 | bb_error_msg_and_die("Unable to resolve '%s'", argv[1]); |
| 628 | puts(dev); |
Mike Frysinger | 0f8a638 | 2005-09-24 06:07:34 +0000 | [diff] [blame] | 629 | exit(0); |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 630 | } |
Mike Frysinger | a1c6a57 | 2005-09-24 05:55:03 +0000 | [diff] [blame] | 631 | #endif |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 632 | |
| 633 | int tune2fs_main(int argc, char **argv) |
| 634 | { |
| 635 | errcode_t retval; |
| 636 | ext2_filsys fs; |
| 637 | struct ext2_super_block *sb; |
| 638 | io_manager io_ptr; |
Mike Frysinger | a1c6a57 | 2005-09-24 05:55:03 +0000 | [diff] [blame] | 639 | #if defined(CONFIG_FINDFS) || defined(CONFIG_E2LABEL) |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 640 | char *program_name = basename(argv[0]); |
Mike Frysinger | a1c6a57 | 2005-09-24 05:55:03 +0000 | [diff] [blame] | 641 | #endif |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 642 | |
Mike Frysinger | a1c6a57 | 2005-09-24 05:55:03 +0000 | [diff] [blame] | 643 | #ifdef CONFIG_FINDFS |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 644 | if (strcmp(program_name, "findfs") == 0) |
Mike Frysinger | 0f8a638 | 2005-09-24 06:07:34 +0000 | [diff] [blame] | 645 | do_findfs(argc, argv); |
Mike Frysinger | a1c6a57 | 2005-09-24 05:55:03 +0000 | [diff] [blame] | 646 | #endif |
| 647 | |
| 648 | #ifdef CONFIG_E2LABEL |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 649 | if (strcmp(program_name, "e2label") == 0) |
| 650 | parse_e2label_options(argc, argv); |
| 651 | else |
Mike Frysinger | a1c6a57 | 2005-09-24 05:55:03 +0000 | [diff] [blame] | 652 | #endif |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 653 | parse_tune2fs_options(argc, argv); |
| 654 | |
| 655 | io_ptr = unix_io_manager; |
| 656 | retval = ext2fs_open2(device_name, io_options, open_flag, |
| 657 | 0, 0, io_ptr, &fs); |
| 658 | if (retval) |
| 659 | bb_error_msg_and_die("No valid superblock on %s", device_name); |
| 660 | sb = fs->super; |
| 661 | if (print_label) { |
| 662 | /* For e2label emulation */ |
| 663 | printf("%.*s\n", (int) sizeof(sb->s_volume_name), |
| 664 | sb->s_volume_name); |
| 665 | exit(0); |
| 666 | } |
| 667 | retval = ext2fs_check_if_mounted(device_name, &mount_flags); |
| 668 | if (retval) |
| 669 | bb_error_msg_and_die("Could not determine if %s is mounted", device_name); |
| 670 | /* Normally we only need to write out the superblock */ |
| 671 | fs->flags |= EXT2_FLAG_SUPER_ONLY; |
| 672 | |
| 673 | if (c_flag) { |
| 674 | sb->s_max_mnt_count = max_mount_count; |
| 675 | ext2fs_mark_super_dirty(fs); |
| 676 | printf("Setting maximal mount count to %d\n", max_mount_count); |
| 677 | } |
| 678 | if (C_flag) { |
| 679 | sb->s_mnt_count = mount_count; |
| 680 | ext2fs_mark_super_dirty(fs); |
| 681 | printf("Setting current mount count to %d\n", mount_count); |
| 682 | } |
| 683 | if (e_flag) { |
| 684 | sb->s_errors = errors; |
| 685 | ext2fs_mark_super_dirty(fs); |
| 686 | printf("Setting error behavior to %d\n", errors); |
| 687 | } |
| 688 | if (g_flag) { |
| 689 | sb->s_def_resgid = resgid; |
| 690 | ext2fs_mark_super_dirty(fs); |
| 691 | printf("Setting reserved blocks gid to %lu\n", resgid); |
| 692 | } |
| 693 | if (i_flag) { |
| 694 | sb->s_checkinterval = interval; |
| 695 | ext2fs_mark_super_dirty(fs); |
| 696 | printf("Setting interval between check %lu seconds\n", interval); |
| 697 | } |
| 698 | if (m_flag) { |
| 699 | sb->s_r_blocks_count = (sb->s_blocks_count / 100) |
| 700 | * reserved_ratio; |
| 701 | ext2fs_mark_super_dirty(fs); |
| 702 | printf("Setting reserved blocks percentage to %lu (%u blocks)\n", |
| 703 | reserved_ratio, sb->s_r_blocks_count); |
| 704 | } |
| 705 | if (r_flag) { |
| 706 | if (reserved_blocks >= sb->s_blocks_count/2) |
| 707 | bb_error_msg_and_die("reserved blocks count is too big (%lu)", reserved_blocks); |
| 708 | sb->s_r_blocks_count = reserved_blocks; |
| 709 | ext2fs_mark_super_dirty(fs); |
| 710 | printf("Setting reserved blocks count to %lu\n", reserved_blocks); |
| 711 | } |
| 712 | if (s_flag == 1) { |
| 713 | if (sb->s_feature_ro_compat & |
| 714 | EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) |
| 715 | bb_error_msg("\nThe filesystem already has sparse superblocks\n"); |
| 716 | else { |
| 717 | sb->s_feature_ro_compat |= |
| 718 | EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; |
| 719 | sb->s_state &= ~EXT2_VALID_FS; |
| 720 | ext2fs_mark_super_dirty(fs); |
| 721 | printf("\nSparse superblock flag set. %s", please_fsck); |
| 722 | } |
| 723 | } |
| 724 | if (s_flag == 0) { |
| 725 | if (!(sb->s_feature_ro_compat & |
| 726 | EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) |
| 727 | bb_error_msg("\nThe filesystem already has sparse superblocks disabled\n"); |
| 728 | else { |
| 729 | sb->s_feature_ro_compat &= |
| 730 | ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; |
| 731 | sb->s_state &= ~EXT2_VALID_FS; |
| 732 | fs->flags |= EXT2_FLAG_MASTER_SB_ONLY; |
| 733 | ext2fs_mark_super_dirty(fs); |
| 734 | printf("\nSparse superblock flag cleared. %s", please_fsck); |
| 735 | } |
| 736 | } |
| 737 | if (T_flag) { |
| 738 | sb->s_lastcheck = last_check_time; |
| 739 | ext2fs_mark_super_dirty(fs); |
| 740 | printf("Setting time filesystem last checked to %s\n", |
| 741 | ctime(&last_check_time)); |
| 742 | } |
| 743 | if (u_flag) { |
| 744 | sb->s_def_resuid = resuid; |
| 745 | ext2fs_mark_super_dirty(fs); |
| 746 | printf("Setting reserved blocks uid to %lu\n", resuid); |
| 747 | } |
| 748 | if (L_flag) { |
| 749 | if (strlen(new_label) > sizeof(sb->s_volume_name)) |
| 750 | bb_error_msg("Warning: label too long, truncating\n"); |
| 751 | memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name)); |
| 752 | strncpy(sb->s_volume_name, new_label, |
| 753 | sizeof(sb->s_volume_name)); |
| 754 | ext2fs_mark_super_dirty(fs); |
| 755 | } |
| 756 | if (M_flag) { |
| 757 | memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted)); |
| 758 | strncpy(sb->s_last_mounted, new_last_mounted, |
| 759 | sizeof(sb->s_last_mounted)); |
| 760 | ext2fs_mark_super_dirty(fs); |
| 761 | } |
| 762 | if (mntopts_cmd) |
| 763 | update_mntopts(fs, mntopts_cmd); |
| 764 | if (features_cmd) |
| 765 | update_feature_set(fs, features_cmd); |
| 766 | if (journal_size || journal_device) |
| 767 | add_journal(fs); |
| 768 | |
| 769 | if (U_flag) { |
| 770 | if ((strcasecmp(new_UUID, "null") == 0) || |
| 771 | (strcasecmp(new_UUID, "clear") == 0)) { |
| 772 | uuid_clear(sb->s_uuid); |
| 773 | } else if (strcasecmp(new_UUID, "time") == 0) { |
| 774 | uuid_generate_time(sb->s_uuid); |
| 775 | } else if (strcasecmp(new_UUID, "random") == 0) { |
| 776 | uuid_generate(sb->s_uuid); |
| 777 | } else if (uuid_parse(new_UUID, sb->s_uuid)) { |
| 778 | bb_error_msg_and_die("Invalid UUID format"); |
| 779 | } |
| 780 | ext2fs_mark_super_dirty(fs); |
| 781 | } |
| 782 | |
| 783 | if (l_flag) |
| 784 | list_super (sb); |
| 785 | return (ext2fs_close (fs) ? 1 : 0); |
| 786 | } |