tune2fs.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. //applet:IF_TUNE2FS(APPLET(tune2fs, BB_DIR_SBIN, BB_SUID_DROP))
  16. //kbuild:lib-$(CONFIG_TUNE2FS) += tune2fs.o
  17. //usage:#define tune2fs_trivial_usage
  18. //usage: "[-c MAX_MOUNT_COUNT] "
  19. ////usage: "[-e errors-behavior] [-g group] "
  20. //usage: "[-i DAYS] "
  21. ////usage: "[-j] [-J journal-options] [-l] [-s sparse-flag] "
  22. ////usage: "[-m reserved-blocks-percent] [-o [^]mount-options[,...]] "
  23. ////usage: "[-r reserved-blocks-count] [-u user] "
  24. //usage: "[-C MOUNT_COUNT] "
  25. //usage: "[-L LABEL] "
  26. ////usage: "[-M last-mounted-dir] [-O [^]feature[,...]] "
  27. ////usage: "[-T last-check-time] [-U UUID] "
  28. //usage: "BLOCKDEV"
  29. //usage:
  30. //usage:#define tune2fs_full_usage "\n\n"
  31. //usage: "Adjust filesystem options on ext[23] filesystems"
  32. #include "libbb.h"
  33. #include <linux/fs.h>
  34. #include "bb_e2fs_defs.h"
  35. // storage helpers
  36. char BUG_wrong_field_size(void);
  37. #define STORE_LE(field, value) \
  38. do { \
  39. if (sizeof(field) == 4) \
  40. field = SWAP_LE32(value); \
  41. else if (sizeof(field) == 2) \
  42. field = SWAP_LE16(value); \
  43. else if (sizeof(field) == 1) \
  44. field = (value); \
  45. else \
  46. BUG_wrong_field_size(); \
  47. } while (0)
  48. #define FETCH_LE32(field) \
  49. (sizeof(field) == 4 ? SWAP_LE32(field) : BUG_wrong_field_size())
  50. enum {
  51. OPT_L = 1 << 0, // label
  52. OPT_c = 1 << 1, // max mount count
  53. OPT_i = 1 << 2, // check interval
  54. OPT_C = 1 << 3, // current mount count
  55. };
  56. int tune2fs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  57. int tune2fs_main(int argc UNUSED_PARAM, char **argv)
  58. {
  59. unsigned opts;
  60. const char *label, *str_c, *str_i, *str_C;
  61. struct ext2_super_block *sb;
  62. int fd;
  63. opt_complementary = "=1";
  64. opts = getopt32(argv, "L:c:i:C:", &label, &str_c, &str_i, &str_C);
  65. if (!opts)
  66. bb_show_usage();
  67. argv += optind; // argv[0] -- device
  68. // read superblock
  69. fd = xopen(argv[0], O_RDWR);
  70. xlseek(fd, 1024, SEEK_SET);
  71. sb = xzalloc(1024);
  72. xread(fd, sb, 1024);
  73. // mangle superblock
  74. //STORE_LE(sb->s_wtime, time(NULL)); - why bother?
  75. if (opts & OPT_C) {
  76. int n = xatoi_range(str_C, 1, 0xfffe);
  77. STORE_LE(sb->s_mnt_count, (unsigned)n);
  78. }
  79. // set the label
  80. if (opts & OPT_L)
  81. safe_strncpy((char *)sb->s_volume_name, label, sizeof(sb->s_volume_name));
  82. if (opts & OPT_c) {
  83. int n = xatoi_range(str_c, -1, 0xfffe);
  84. if (n == 0)
  85. n = -1;
  86. STORE_LE(sb->s_max_mnt_count, (unsigned)n);
  87. }
  88. if (opts & OPT_i) {
  89. unsigned n = xatou_range(str_i, 0, (unsigned)0xffffffff / (24*60*60)) * 24*60*60;
  90. STORE_LE(sb->s_checkinterval, n);
  91. }
  92. // write superblock
  93. xlseek(fd, 1024, SEEK_SET);
  94. xwrite(fd, sb, 1024);
  95. if (ENABLE_FEATURE_CLEAN_UP) {
  96. free(sb);
  97. }
  98. xclose(fd);
  99. return EXIT_SUCCESS;
  100. }