blob: c9f88b39d3a165e5d19af62027e986c95cbcb7de [file] [log] [blame]
Denys Vlasenko3945bc12009-10-22 00:55:55 +02001/* 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 Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Denys Vlasenko3945bc12009-10-22 00:55:55 +02008 */
Denys Vlasenko000eda42015-10-18 22:40:23 +02009//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//kbuild:lib-$(CONFIG_TUNE2FS) += tune2fs.o
19
20//usage:#define tune2fs_trivial_usage
21//usage: "[-c MAX_MOUNT_COUNT] "
22////usage: "[-e errors-behavior] [-g group] "
23//usage: "[-i DAYS] "
24////usage: "[-j] [-J journal-options] [-l] [-s sparse-flag] "
25////usage: "[-m reserved-blocks-percent] [-o [^]mount-options[,...]] "
26////usage: "[-r reserved-blocks-count] [-u user] "
27//usage: "[-C MOUNT_COUNT] "
28//usage: "[-L LABEL] "
29////usage: "[-M last-mounted-dir] [-O [^]feature[,...]] "
30////usage: "[-T last-check-time] [-U UUID] "
31//usage: "BLOCKDEV"
32//usage:
33//usage:#define tune2fs_full_usage "\n\n"
34//usage: "Adjust filesystem options on ext[23] filesystems"
35
Denys Vlasenko3945bc12009-10-22 00:55:55 +020036#include "libbb.h"
37#include <linux/fs.h>
Denys Vlasenko176bc342012-04-17 15:06:55 +020038#include "bb_e2fs_defs.h"
Denys Vlasenko3945bc12009-10-22 00:55:55 +020039
40// storage helpers
41char BUG_wrong_field_size(void);
42#define STORE_LE(field, value) \
43do { \
44 if (sizeof(field) == 4) \
Denys Vlasenko67743862010-05-09 00:13:40 +020045 field = SWAP_LE32(value); \
Denys Vlasenko3945bc12009-10-22 00:55:55 +020046 else if (sizeof(field) == 2) \
Denys Vlasenko67743862010-05-09 00:13:40 +020047 field = SWAP_LE16(value); \
Denys Vlasenko3945bc12009-10-22 00:55:55 +020048 else if (sizeof(field) == 1) \
49 field = (value); \
50 else \
51 BUG_wrong_field_size(); \
52} while (0)
53
54#define FETCH_LE32(field) \
Denys Vlasenko67743862010-05-09 00:13:40 +020055 (sizeof(field) == 4 ? SWAP_LE32(field) : BUG_wrong_field_size())
Denys Vlasenko3945bc12009-10-22 00:55:55 +020056
57enum {
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020058 OPT_L = 1 << 0, // label
Richard Braun5aa4d532010-10-05 00:39:46 +020059 OPT_c = 1 << 1, // max mount count
60 OPT_i = 1 << 2, // check interval
Denys Vlasenko46aa5e02011-09-11 20:08:12 +020061 OPT_C = 1 << 3, // current mount count
Denys Vlasenko3945bc12009-10-22 00:55:55 +020062};
63
64int tune2fs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
65int tune2fs_main(int argc UNUSED_PARAM, char **argv)
66{
67 unsigned opts;
Denys Vlasenko46aa5e02011-09-11 20:08:12 +020068 const char *label, *str_c, *str_i, *str_C;
Denys Vlasenko3945bc12009-10-22 00:55:55 +020069 struct ext2_super_block *sb;
70 int fd;
71
72 opt_complementary = "=1";
Denys Vlasenko46aa5e02011-09-11 20:08:12 +020073 opts = getopt32(argv, "L:c:i:C:", &label, &str_c, &str_i, &str_C);
Denys Vlasenko3945bc12009-10-22 00:55:55 +020074 if (!opts)
75 bb_show_usage();
Richard Braun5aa4d532010-10-05 00:39:46 +020076 argv += optind; // argv[0] -- device
Denys Vlasenko3945bc12009-10-22 00:55:55 +020077
78 // read superblock
79 fd = xopen(argv[0], O_RDWR);
80 xlseek(fd, 1024, SEEK_SET);
81 sb = xzalloc(1024);
82 xread(fd, sb, 1024);
83
84 // mangle superblock
85 //STORE_LE(sb->s_wtime, time(NULL)); - why bother?
Richard Braun5aa4d532010-10-05 00:39:46 +020086
Denys Vlasenko46aa5e02011-09-11 20:08:12 +020087 if (opts & OPT_C) {
88 int n = xatoi_range(str_C, 1, 0xfffe);
89 STORE_LE(sb->s_mnt_count, (unsigned)n);
90 }
91
Denys Vlasenko3945bc12009-10-22 00:55:55 +020092 // set the label
Richard Braun5aa4d532010-10-05 00:39:46 +020093 if (opts & OPT_L)
Denys Vlasenko3945bc12009-10-22 00:55:55 +020094 safe_strncpy((char *)sb->s_volume_name, label, sizeof(sb->s_volume_name));
Richard Braun5aa4d532010-10-05 00:39:46 +020095
96 if (opts & OPT_c) {
97 int n = xatoi_range(str_c, -1, 0xfffe);
98 if (n == 0)
99 n = -1;
100 STORE_LE(sb->s_max_mnt_count, (unsigned)n);
101 }
102
103 if (opts & OPT_i) {
104 unsigned n = xatou_range(str_i, 0, (unsigned)0xffffffff / (24*60*60)) * 24*60*60;
105 STORE_LE(sb->s_checkinterval, n);
106 }
107
Denys Vlasenko3945bc12009-10-22 00:55:55 +0200108 // write superblock
109 xlseek(fd, 1024, SEEK_SET);
110 xwrite(fd, sb, 1024);
111
112 if (ENABLE_FEATURE_CLEAN_UP) {
113 free(sb);
114 }
115
116 xclose(fd);
117 return EXIT_SUCCESS;
118}