blob: 8da7432d9425d17da707e1a2e8e0c24e70fe0b50 [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 */
9#include "libbb.h"
10#include <linux/fs.h>
Denys Vlasenkocaf57682012-04-12 14:01:10 +020011/*
12 * Work around linux/ext2_fs.h breakage.
13 * See https://bugzilla.kernel.org/show_bug.cgi?id=42986.
14 */
15typedef mode_t bb__umode_t;
16#define umode_t bb__umode_t
Denys Vlasenko3945bc12009-10-22 00:55:55 +020017#include <linux/ext2_fs.h>
Denys Vlasenko3945bc12009-10-22 00:55:55 +020018
19// storage helpers
20char BUG_wrong_field_size(void);
21#define STORE_LE(field, value) \
22do { \
23 if (sizeof(field) == 4) \
Denys Vlasenko67743862010-05-09 00:13:40 +020024 field = SWAP_LE32(value); \
Denys Vlasenko3945bc12009-10-22 00:55:55 +020025 else if (sizeof(field) == 2) \
Denys Vlasenko67743862010-05-09 00:13:40 +020026 field = SWAP_LE16(value); \
Denys Vlasenko3945bc12009-10-22 00:55:55 +020027 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 Vlasenko67743862010-05-09 00:13:40 +020034 (sizeof(field) == 4 ? SWAP_LE32(field) : BUG_wrong_field_size())
Denys Vlasenko3945bc12009-10-22 00:55:55 +020035
Richard Braun5aa4d532010-10-05 00:39:46 +020036//usage:#define tune2fs_trivial_usage
Denys Vlasenko46aa5e02011-09-11 20:08:12 +020037//usage: "[-c MAX_MOUNT_COUNT] "
Richard Braun5aa4d532010-10-05 00:39:46 +020038////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 Vlasenko46aa5e02011-09-11 20:08:12 +020042////usage: "[-r reserved-blocks-count] [-u user] "
43//usage: "[-C MOUNT_COUNT] "
Richard Braun5aa4d532010-10-05 00:39:46 +020044//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 Vlasenko3945bc12009-10-22 00:55:55 +020052enum {
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020053 OPT_L = 1 << 0, // label
Richard Braun5aa4d532010-10-05 00:39:46 +020054 OPT_c = 1 << 1, // max mount count
55 OPT_i = 1 << 2, // check interval
Denys Vlasenko46aa5e02011-09-11 20:08:12 +020056 OPT_C = 1 << 3, // current mount count
Denys Vlasenko3945bc12009-10-22 00:55:55 +020057};
58
59int tune2fs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
60int tune2fs_main(int argc UNUSED_PARAM, char **argv)
61{
62 unsigned opts;
Denys Vlasenko46aa5e02011-09-11 20:08:12 +020063 const char *label, *str_c, *str_i, *str_C;
Denys Vlasenko3945bc12009-10-22 00:55:55 +020064 struct ext2_super_block *sb;
65 int fd;
66
67 opt_complementary = "=1";
Denys Vlasenko46aa5e02011-09-11 20:08:12 +020068 opts = getopt32(argv, "L:c:i:C:", &label, &str_c, &str_i, &str_C);
Denys Vlasenko3945bc12009-10-22 00:55:55 +020069 if (!opts)
70 bb_show_usage();
Richard Braun5aa4d532010-10-05 00:39:46 +020071 argv += optind; // argv[0] -- device
Denys Vlasenko3945bc12009-10-22 00:55:55 +020072
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 Braun5aa4d532010-10-05 00:39:46 +020081
Denys Vlasenko46aa5e02011-09-11 20:08:12 +020082 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 Vlasenko3945bc12009-10-22 00:55:55 +020087 // set the label
Richard Braun5aa4d532010-10-05 00:39:46 +020088 if (opts & OPT_L)
Denys Vlasenko3945bc12009-10-22 00:55:55 +020089 safe_strncpy((char *)sb->s_volume_name, label, sizeof(sb->s_volume_name));
Richard Braun5aa4d532010-10-05 00:39:46 +020090
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 Vlasenko3945bc12009-10-22 00:55:55 +0200103 // 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}