blob: 9daec542a2036585e98482beb048c68af5d1adef [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>
11#include <linux/ext2_fs.h>
Denys Vlasenko3945bc12009-10-22 00:55:55 +020012
13// storage helpers
14char BUG_wrong_field_size(void);
15#define STORE_LE(field, value) \
16do { \
17 if (sizeof(field) == 4) \
Denys Vlasenko67743862010-05-09 00:13:40 +020018 field = SWAP_LE32(value); \
Denys Vlasenko3945bc12009-10-22 00:55:55 +020019 else if (sizeof(field) == 2) \
Denys Vlasenko67743862010-05-09 00:13:40 +020020 field = SWAP_LE16(value); \
Denys Vlasenko3945bc12009-10-22 00:55:55 +020021 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 Vlasenko67743862010-05-09 00:13:40 +020028 (sizeof(field) == 4 ? SWAP_LE32(field) : BUG_wrong_field_size())
Denys Vlasenko3945bc12009-10-22 00:55:55 +020029
Richard Braun5aa4d532010-10-05 00:39:46 +020030//usage:#define tune2fs_trivial_usage
31//usage: "[-c MOUNT_CNT] "
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[,...]] "
36////usage: "[-r reserved-blocks-count] [-u user] [-C mount-count] "
37//usage: "[-L LABEL] "
38////usage: "[-M last-mounted-dir] [-O [^]feature[,...]] "
39////usage: "[-T last-check-time] [-U UUID] "
40//usage: "BLOCKDEV"
41//usage:
42//usage:#define tune2fs_full_usage "\n\n"
43//usage: "Adjust filesystem options on ext[23] filesystems"
44
Denys Vlasenko3945bc12009-10-22 00:55:55 +020045enum {
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020046 OPT_L = 1 << 0, // label
Richard Braun5aa4d532010-10-05 00:39:46 +020047 OPT_c = 1 << 1, // max mount count
48 OPT_i = 1 << 2, // check interval
Denys Vlasenko3945bc12009-10-22 00:55:55 +020049};
50
51int tune2fs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
52int tune2fs_main(int argc UNUSED_PARAM, char **argv)
53{
54 unsigned opts;
Richard Braun5aa4d532010-10-05 00:39:46 +020055 const char *label, *str_c, *str_i;
Denys Vlasenko3945bc12009-10-22 00:55:55 +020056 struct ext2_super_block *sb;
57 int fd;
58
59 opt_complementary = "=1";
Richard Braun5aa4d532010-10-05 00:39:46 +020060 opts = getopt32(argv, "L:c:i:", &label, &str_c, &str_i);
Denys Vlasenko3945bc12009-10-22 00:55:55 +020061 if (!opts)
62 bb_show_usage();
Richard Braun5aa4d532010-10-05 00:39:46 +020063 argv += optind; // argv[0] -- device
Denys Vlasenko3945bc12009-10-22 00:55:55 +020064
65 // read superblock
66 fd = xopen(argv[0], O_RDWR);
67 xlseek(fd, 1024, SEEK_SET);
68 sb = xzalloc(1024);
69 xread(fd, sb, 1024);
70
71 // mangle superblock
72 //STORE_LE(sb->s_wtime, time(NULL)); - why bother?
Richard Braun5aa4d532010-10-05 00:39:46 +020073
Denys Vlasenko3945bc12009-10-22 00:55:55 +020074 // set the label
Richard Braun5aa4d532010-10-05 00:39:46 +020075 if (opts & OPT_L)
Denys Vlasenko3945bc12009-10-22 00:55:55 +020076 safe_strncpy((char *)sb->s_volume_name, label, sizeof(sb->s_volume_name));
Richard Braun5aa4d532010-10-05 00:39:46 +020077
78 if (opts & OPT_c) {
79 int n = xatoi_range(str_c, -1, 0xfffe);
80 if (n == 0)
81 n = -1;
82 STORE_LE(sb->s_max_mnt_count, (unsigned)n);
83 }
84
85 if (opts & OPT_i) {
86 unsigned n = xatou_range(str_i, 0, (unsigned)0xffffffff / (24*60*60)) * 24*60*60;
87 STORE_LE(sb->s_checkinterval, n);
88 }
89
Denys Vlasenko3945bc12009-10-22 00:55:55 +020090 // write superblock
91 xlseek(fd, 1024, SEEK_SET);
92 xwrite(fd, sb, 1024);
93
94 if (ENABLE_FEATURE_CLEAN_UP) {
95 free(sb);
96 }
97
98 xclose(fd);
99 return EXIT_SUCCESS;
100}