This patch adds support for maximum mount count and interval checking of
ext2/ext3 file systems. The -i option however doesn't support d/w/m
suffixes.

Although tune2fs seems unused by the vast majority of busybox users
because of its only available option, I felt it was the right place to
introduce those changes. I'm currently developing an embedded
distribution on x86 hardware and a CompactFlash card is used as the
persistent storage device, so ext2 was chosen as the file system for
persistent configuration. But this file system is very small and almost
never mounted read-write, only when changes are actually committed. As a
result, there is no real need to regularly check that file system. And
because the "restore defaults" operation actually recreates an empty
file system, I needed a way to disable mount count/interval checking
from busybox. So here is the patch.
---
 e2fsprogs/tune2fs.c |   49 ++++++++++++++++++++++++++++++++++++++++++++-----
 include/usage.src.h |    6 ++++--
 2 files changed, 48 insertions(+), 7 deletions(-)

diff --git a/e2fsprogs/tune2fs.c b/e2fsprogs/tune2fs.c
index 311349f..9772bf0 100644
--- a/e2fsprogs/tune2fs.c
+++ b/e2fsprogs/tune2fs.c
@@ -27,25 +27,53 @@ do { \
 #define FETCH_LE32(field) \
        (sizeof(field) == 4 ? SWAP_LE32(field) : BUG_wrong_field_size())
 
+#define SECONDS_PER_DAY 86400
+#define MAX_CHECK_INTERVAL ((unsigned)0xffffffff / SECONDS_PER_DAY)
+
 enum {
        OPT_L = 1 << 0, // label
+       OPT_c = 1 << 1, // max mount count
+       OPT_i = 1 << 2, // check interval
 };
 
 int tune2fs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int tune2fs_main(int argc UNUSED_PARAM, char **argv)
 {
-       unsigned opts;
-       const char *label;
+       unsigned opts, check_interval;
+       const char *label, *max_mnt_count_str, *check_interval_str;
        struct ext2_super_block *sb;
-       int fd;
+       int fd, max_mnt_count;
 
        opt_complementary = "=1";
-       opts = getopt32(argv, "L:", &label);
+       opts = getopt32(argv, "L:c:i:", &label, &max_mnt_count_str, 
&check_interval_str);
        argv += optind; // argv[0] -- device
 
        if (!opts)
                bb_show_usage();
 
+       if (opts & OPT_c) {
+               errno = 0;
+               max_mnt_count = bb_strtoi(max_mnt_count_str, NULL, 10);
+
+               // max_mnt_count is 16-bits wide in the superblock. In addition,
+               // -1 marks mount count checking as disabled.
+               if (errno || (max_mnt_count < -1) || (max_mnt_count >= 0xffff))
+                       bb_error_msg_and_die("invalid max mount count");
+
+               if (max_mnt_count == 0)
+                       max_mnt_count = -1;
+       }
+
+       if (opts & OPT_i) {
+               errno = 0;
+               check_interval = bb_strtou(check_interval_str, NULL, 10);
+
+               if (errno || (check_interval > MAX_CHECK_INTERVAL))
+                       bb_error_msg_and_die("invalid check interval");
+
+               check_interval *= SECONDS_PER_DAY;
+       }
+
        // read superblock
        fd = xopen(argv[0], O_RDWR);
        xlseek(fd, 1024, SEEK_SET);
@@ -55,8 +83,19 @@ int tune2fs_main(int argc UNUSED_PARAM, char **argv)
        // mangle superblock
        //STORE_LE(sb->s_wtime, time(NULL)); - why bother?
        // set the label
-       if (1 /*opts & OPT_L*/)
+       if (opts & OPT_L)
                safe_strncpy((char *)sb->s_volume_name, label, 
sizeof(sb->s_volume_name));
+
+       if (opts & OPT_c) {
+               printf("Setting maximal mount count to %d\n", max_mnt_count);
+               STORE_LE(sb->s_max_mnt_count, (unsigned)max_mnt_count);
+       }
+
+       if (opts & OPT_i) {
+               printf("Setting interval between checks to %u seconds\n", 
check_interval);
+               STORE_LE(sb->s_checkinterval, check_interval);
+       }
+
        // write superblock
        xlseek(fd, 1024, SEEK_SET);
        xwrite(fd, sb, 1024);
diff --git a/include/usage.src.h b/include/usage.src.h
index 577eb57..feb3fe9 100644
--- a/include/usage.src.h
+++ b/include/usage.src.h
@@ -4460,8 +4460,10 @@ INSERT
        "# tunctl -d tun0\n"
 
 #define tune2fs_trivial_usage \
-/*     "[-c max-mounts-count] [-e errors-behavior] [-g group] " */ \
-/*     "[-i interval[d|m|w]] [-j] [-J journal-options] [-l] [-s sparse-flag] " 
*/ \
+       "[-c max-mounts-count] " \
+/*     "[-e errors-behavior] [-g group] " */ \
+       "[-i interval] " \
+/*     "[-j] [-J journal-options] [-l] [-s sparse-flag] " */ \
 /*     "[-m reserved-blocks-percent] [-o [^]mount-options[,...]] " */ \
 /*     "[-r reserved-blocks-count] [-u user] [-C mount-count] " */ \
        "[-L LABEL] " \
-- 
1.5.6.5

_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to