summaryrefslogtreecommitdiff
path: root/e2fsprogs/tune2fs.c (plain)
blob: 0485e82ce5e08b29fb1eb00bc65c73e557200cc8
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 *
7 * Licensed under GPLv2, see file LICENSE in this source tree.
8 */
9//config:config TUNE2FS
10//config: bool "tune2fs"
11//config: default n # off: it is too limited compared to upstream version
12//config: help
13//config: tune2fs allows the system administrator to adjust various tunable
14//config: filesystem parameters on Linux ext2/ext3 filesystems.
15
16//applet:IF_TUNE2FS(APPLET(tune2fs, BB_DIR_SBIN, BB_SUID_DROP))
17
18//TODO alias to "tune2fs -L LABEL": //applet:IF_E2LABEL(APPLET_ODDNAME(e2label, tune2fs, BB_DIR_SBIN, BB_SUID_DROP, e2label))
19
20//kbuild:lib-$(CONFIG_TUNE2FS) += tune2fs.o
21
22//usage:#define tune2fs_trivial_usage
23//usage: "[-c MAX_MOUNT_COUNT] "
24////usage: "[-e errors-behavior] [-g group] "
25//usage: "[-i DAYS] "
26////usage: "[-j] [-J journal-options] [-l] [-s sparse-flag] "
27////usage: "[-m reserved-blocks-percent] [-o [^]mount-options[,...]] "
28////usage: "[-r reserved-blocks-count] [-u user] "
29//usage: "[-C MOUNT_COUNT] "
30//usage: "[-L LABEL] "
31////usage: "[-M last-mounted-dir] [-O [^]feature[,...]] "
32////usage: "[-T last-check-time] [-U UUID] "
33//usage: "BLOCKDEV"
34//usage:
35//usage:#define tune2fs_full_usage "\n\n"
36//usage: "Adjust filesystem options on ext[23] filesystems"
37
38#include "libbb.h"
39#include <linux/fs.h>
40#include "bb_e2fs_defs.h"
41
42// storage helpers
43char BUG_wrong_field_size(void);
44#define STORE_LE(field, value) \
45do { \
46 if (sizeof(field) == 4) \
47 field = SWAP_LE32(value); \
48 else if (sizeof(field) == 2) \
49 field = SWAP_LE16(value); \
50 else if (sizeof(field) == 1) \
51 field = (value); \
52 else \
53 BUG_wrong_field_size(); \
54} while (0)
55
56#define FETCH_LE32(field) \
57 (sizeof(field) == 4 ? SWAP_LE32(field) : BUG_wrong_field_size())
58
59enum {
60 OPT_L = 1 << 0, // label
61 OPT_c = 1 << 1, // max mount count
62 OPT_i = 1 << 2, // check interval
63 OPT_C = 1 << 3, // current mount count
64};
65
66int tune2fs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
67int tune2fs_main(int argc UNUSED_PARAM, char **argv)
68{
69 unsigned opts;
70 const char *label, *str_c, *str_i, *str_C;
71 struct ext2_super_block *sb;
72 int fd;
73
74 opt_complementary = "=1";
75 opts = getopt32(argv, "L:c:i:C:", &label, &str_c, &str_i, &str_C);
76 if (!opts)
77 bb_show_usage();
78 argv += optind; // argv[0] -- device
79
80 // read superblock
81 fd = xopen(argv[0], O_RDWR);
82 xlseek(fd, 1024, SEEK_SET);
83 sb = xzalloc(1024);
84 xread(fd, sb, 1024);
85
86 // mangle superblock
87 //STORE_LE(sb->s_wtime, time(NULL)); - why bother?
88
89 if (opts & OPT_C) {
90 int n = xatoi_range(str_C, 1, 0xfffe);
91 if (n == 0) n = 1;
92 STORE_LE(sb->s_mnt_count, (unsigned)n);
93 }
94
95 // set the label
96 if (opts & OPT_L)
97 safe_strncpy((char *)sb->s_volume_name, label, sizeof(sb->s_volume_name));
98
99 if (opts & OPT_c) {
100 int n = xatoi_range(str_c, -1, 0xfffe);
101 if (n == 0)
102 n = -1;
103 STORE_LE(sb->s_max_mnt_count, (unsigned)n);
104 }
105
106 if (opts & OPT_i) {
107 unsigned n = xatou_range(str_i, 0, (unsigned)0xffffffff / (24*60*60)) * 24*60*60;
108 STORE_LE(sb->s_checkinterval, n);
109 }
110
111 // write superblock
112 xlseek(fd, 1024, SEEK_SET);
113 xwrite(fd, sb, 1024);
114
115 if (ENABLE_FEATURE_CLEAN_UP) {
116 free(sb);
117 }
118
119 xclose(fd);
120 return EXIT_SUCCESS;
121}
122