Denys Vlasenko | 3945bc1 | 2009-10-22 00:55:55 +0200 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * tune2fs: utility to modify EXT2 filesystem |
| 4 | * |
| 5 | * Busybox'ed (2009) by Vladimir Dronnikov <dronnikov@gmail.com> |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denys Vlasenko | 3945bc1 | 2009-10-22 00:55:55 +0200 | [diff] [blame] | 8 | */ |
| 9 | #include "libbb.h" |
| 10 | #include <linux/fs.h> |
Denys Vlasenko | 176bc34 | 2012-04-17 15:06:55 +0200 | [diff] [blame] | 11 | #include "bb_e2fs_defs.h" |
Denys Vlasenko | 3945bc1 | 2009-10-22 00:55:55 +0200 | [diff] [blame] | 12 | |
| 13 | // storage helpers |
| 14 | char BUG_wrong_field_size(void); |
| 15 | #define STORE_LE(field, value) \ |
| 16 | do { \ |
| 17 | if (sizeof(field) == 4) \ |
Denys Vlasenko | 6774386 | 2010-05-09 00:13:40 +0200 | [diff] [blame] | 18 | field = SWAP_LE32(value); \ |
Denys Vlasenko | 3945bc1 | 2009-10-22 00:55:55 +0200 | [diff] [blame] | 19 | else if (sizeof(field) == 2) \ |
Denys Vlasenko | 6774386 | 2010-05-09 00:13:40 +0200 | [diff] [blame] | 20 | field = SWAP_LE16(value); \ |
Denys Vlasenko | 3945bc1 | 2009-10-22 00:55:55 +0200 | [diff] [blame] | 21 | else if (sizeof(field) == 1) \ |
| 22 | field = (value); \ |
| 23 | else \ |
| 24 | BUG_wrong_field_size(); \ |
| 25 | } while (0) |
| 26 | |
| 27 | #define FETCH_LE32(field) \ |
Denys Vlasenko | 6774386 | 2010-05-09 00:13:40 +0200 | [diff] [blame] | 28 | (sizeof(field) == 4 ? SWAP_LE32(field) : BUG_wrong_field_size()) |
Denys Vlasenko | 3945bc1 | 2009-10-22 00:55:55 +0200 | [diff] [blame] | 29 | |
Richard Braun | 5aa4d53 | 2010-10-05 00:39:46 +0200 | [diff] [blame] | 30 | //usage:#define tune2fs_trivial_usage |
Denys Vlasenko | 46aa5e0 | 2011-09-11 20:08:12 +0200 | [diff] [blame] | 31 | //usage: "[-c MAX_MOUNT_COUNT] " |
Richard Braun | 5aa4d53 | 2010-10-05 00:39:46 +0200 | [diff] [blame] | 32 | ////usage: "[-e errors-behavior] [-g group] " |
| 33 | //usage: "[-i DAYS] " |
| 34 | ////usage: "[-j] [-J journal-options] [-l] [-s sparse-flag] " |
| 35 | ////usage: "[-m reserved-blocks-percent] [-o [^]mount-options[,...]] " |
Denys Vlasenko | 46aa5e0 | 2011-09-11 20:08:12 +0200 | [diff] [blame] | 36 | ////usage: "[-r reserved-blocks-count] [-u user] " |
| 37 | //usage: "[-C MOUNT_COUNT] " |
Richard Braun | 5aa4d53 | 2010-10-05 00:39:46 +0200 | [diff] [blame] | 38 | //usage: "[-L LABEL] " |
| 39 | ////usage: "[-M last-mounted-dir] [-O [^]feature[,...]] " |
| 40 | ////usage: "[-T last-check-time] [-U UUID] " |
| 41 | //usage: "BLOCKDEV" |
| 42 | //usage: |
| 43 | //usage:#define tune2fs_full_usage "\n\n" |
| 44 | //usage: "Adjust filesystem options on ext[23] filesystems" |
| 45 | |
Denys Vlasenko | 3945bc1 | 2009-10-22 00:55:55 +0200 | [diff] [blame] | 46 | enum { |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 47 | OPT_L = 1 << 0, // label |
Richard Braun | 5aa4d53 | 2010-10-05 00:39:46 +0200 | [diff] [blame] | 48 | OPT_c = 1 << 1, // max mount count |
| 49 | OPT_i = 1 << 2, // check interval |
Denys Vlasenko | 46aa5e0 | 2011-09-11 20:08:12 +0200 | [diff] [blame] | 50 | OPT_C = 1 << 3, // current mount count |
Denys Vlasenko | 3945bc1 | 2009-10-22 00:55:55 +0200 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | int tune2fs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 54 | int tune2fs_main(int argc UNUSED_PARAM, char **argv) |
| 55 | { |
| 56 | unsigned opts; |
Denys Vlasenko | 46aa5e0 | 2011-09-11 20:08:12 +0200 | [diff] [blame] | 57 | const char *label, *str_c, *str_i, *str_C; |
Denys Vlasenko | 3945bc1 | 2009-10-22 00:55:55 +0200 | [diff] [blame] | 58 | struct ext2_super_block *sb; |
| 59 | int fd; |
| 60 | |
| 61 | opt_complementary = "=1"; |
Denys Vlasenko | 46aa5e0 | 2011-09-11 20:08:12 +0200 | [diff] [blame] | 62 | opts = getopt32(argv, "L:c:i:C:", &label, &str_c, &str_i, &str_C); |
Denys Vlasenko | 3945bc1 | 2009-10-22 00:55:55 +0200 | [diff] [blame] | 63 | if (!opts) |
| 64 | bb_show_usage(); |
Richard Braun | 5aa4d53 | 2010-10-05 00:39:46 +0200 | [diff] [blame] | 65 | argv += optind; // argv[0] -- device |
Denys Vlasenko | 3945bc1 | 2009-10-22 00:55:55 +0200 | [diff] [blame] | 66 | |
| 67 | // read superblock |
| 68 | fd = xopen(argv[0], O_RDWR); |
| 69 | xlseek(fd, 1024, SEEK_SET); |
| 70 | sb = xzalloc(1024); |
| 71 | xread(fd, sb, 1024); |
| 72 | |
| 73 | // mangle superblock |
| 74 | //STORE_LE(sb->s_wtime, time(NULL)); - why bother? |
Richard Braun | 5aa4d53 | 2010-10-05 00:39:46 +0200 | [diff] [blame] | 75 | |
Denys Vlasenko | 46aa5e0 | 2011-09-11 20:08:12 +0200 | [diff] [blame] | 76 | if (opts & OPT_C) { |
| 77 | int n = xatoi_range(str_C, 1, 0xfffe); |
| 78 | STORE_LE(sb->s_mnt_count, (unsigned)n); |
| 79 | } |
| 80 | |
Denys Vlasenko | 3945bc1 | 2009-10-22 00:55:55 +0200 | [diff] [blame] | 81 | // set the label |
Richard Braun | 5aa4d53 | 2010-10-05 00:39:46 +0200 | [diff] [blame] | 82 | if (opts & OPT_L) |
Denys Vlasenko | 3945bc1 | 2009-10-22 00:55:55 +0200 | [diff] [blame] | 83 | safe_strncpy((char *)sb->s_volume_name, label, sizeof(sb->s_volume_name)); |
Richard Braun | 5aa4d53 | 2010-10-05 00:39:46 +0200 | [diff] [blame] | 84 | |
| 85 | if (opts & OPT_c) { |
| 86 | int n = xatoi_range(str_c, -1, 0xfffe); |
| 87 | if (n == 0) |
| 88 | n = -1; |
| 89 | STORE_LE(sb->s_max_mnt_count, (unsigned)n); |
| 90 | } |
| 91 | |
| 92 | if (opts & OPT_i) { |
| 93 | unsigned n = xatou_range(str_i, 0, (unsigned)0xffffffff / (24*60*60)) * 24*60*60; |
| 94 | STORE_LE(sb->s_checkinterval, n); |
| 95 | } |
| 96 | |
Denys Vlasenko | 3945bc1 | 2009-10-22 00:55:55 +0200 | [diff] [blame] | 97 | // write superblock |
| 98 | xlseek(fd, 1024, SEEK_SET); |
| 99 | xwrite(fd, sb, 1024); |
| 100 | |
| 101 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 102 | free(sb); |
| 103 | } |
| 104 | |
| 105 | xclose(fd); |
| 106 | return EXIT_SUCCESS; |
| 107 | } |