tune2fs.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 (4.4 kb)"
  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_NOEXEC(tune2fs, tune2fs, BB_DIR_SBIN, BB_SUID_DROP, tune2fs))
  16. //TODO alias to "tune2fs -L LABEL": //applet:IF_E2LABEL(APPLET_ODDNAME(e2label, tune2fs, BB_DIR_SBIN, BB_SUID_DROP, e2label))
  17. //kbuild:lib-$(CONFIG_TUNE2FS) += tune2fs.o
  18. //usage:#define tune2fs_trivial_usage
  19. //usage: "[-c MAX_MOUNT_COUNT] "
  20. ////usage: "[-e errors-behavior] [-g group] "
  21. //usage: "[-i DAYS] "
  22. ////usage: "[-j] [-J journal-options] [-l] [-s sparse-flag] "
  23. ////usage: "[-m reserved-blocks-percent] [-o [^]mount-options[,...]] "
  24. ////usage: "[-r reserved-blocks-count] [-u user] "
  25. //usage: "[-C MOUNT_COUNT] "
  26. //usage: "[-L LABEL] "
  27. ////usage: "[-M last-mounted-dir] [-O [^]feature[,...]] "
  28. ////usage: "[-T last-check-time] [-U UUID] "
  29. //usage: "BLOCKDEV"
  30. //usage:
  31. //usage:#define tune2fs_full_usage "\n\n"
  32. //usage: "Adjust filesystem options on ext[23] filesystems"
  33. #include "libbb.h"
  34. #include <linux/fs.h>
  35. #include "bb_e2fs_defs.h"
  36. // storage helpers
  37. char BUG_wrong_field_size(void);
  38. #define STORE_LE(field, value) \
  39. do { \
  40. if (sizeof(field) == 4) \
  41. field = SWAP_LE32(value); \
  42. else if (sizeof(field) == 2) \
  43. field = SWAP_LE16(value); \
  44. else if (sizeof(field) == 1) \
  45. field = (value); \
  46. else \
  47. BUG_wrong_field_size(); \
  48. } while (0)
  49. #define FETCH_LE32(field) \
  50. (sizeof(field) == 4 ? SWAP_LE32(field) : BUG_wrong_field_size())
  51. enum {
  52. OPT_L = 1 << 0, // label
  53. OPT_c = 1 << 1, // max mount count
  54. OPT_i = 1 << 2, // check interval
  55. OPT_C = 1 << 3, // current mount count
  56. };
  57. int tune2fs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  58. int tune2fs_main(int argc UNUSED_PARAM, char **argv)
  59. {
  60. unsigned opts;
  61. const char *label, *str_c, *str_i, *str_C;
  62. struct ext2_super_block *sb;
  63. int fd;
  64. opts = getopt32(argv, "^" "L:c:i:C:" "\0" "=1", &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. }