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