tune2fs.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. enum {
  37. OPT_L = 1 << 0, // label
  38. OPT_c = 1 << 1, // max mount count
  39. OPT_i = 1 << 2, // check interval
  40. OPT_C = 1 << 3, // current mount count
  41. };
  42. int tune2fs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  43. int tune2fs_main(int argc UNUSED_PARAM, char **argv)
  44. {
  45. unsigned opts;
  46. const char *label, *str_c, *str_i, *str_C;
  47. struct ext2_super_block *sb;
  48. int fd;
  49. opts = getopt32(argv, "^" "L:c:i:C:" "\0" "=1", &label, &str_c, &str_i, &str_C);
  50. if (!opts)
  51. bb_show_usage();
  52. argv += optind; // argv[0] -- device
  53. // read superblock
  54. fd = xopen(argv[0], O_RDWR);
  55. xlseek(fd, 1024, SEEK_SET);
  56. sb = xzalloc(1024);
  57. xread(fd, sb, 1024);
  58. // mangle superblock
  59. //STORE_LE(sb->s_wtime, time(NULL)); - why bother?
  60. if (opts & OPT_C) {
  61. int n = xatoi_range(str_C, 1, 0xfffe);
  62. STORE_LE(sb->s_mnt_count, (unsigned)n);
  63. }
  64. // set the label
  65. if (opts & OPT_L)
  66. safe_strncpy((char *)sb->s_volume_name, label, sizeof(sb->s_volume_name));
  67. if (opts & OPT_c) {
  68. int n = xatoi_range(str_c, -1, 0xfffe);
  69. if (n == 0)
  70. n = -1;
  71. STORE_LE(sb->s_max_mnt_count, (unsigned)n);
  72. }
  73. if (opts & OPT_i) {
  74. unsigned n = xatou_range(str_i, 0, (unsigned)0xffffffff / (24*60*60)) * 24*60*60;
  75. STORE_LE(sb->s_checkinterval, n);
  76. }
  77. // write superblock
  78. xlseek(fd, 1024, SEEK_SET);
  79. xwrite(fd, sb, 1024);
  80. if (ENABLE_FEATURE_CLEAN_UP) {
  81. free(sb);
  82. }
  83. xclose(fd);
  84. return EXIT_SUCCESS;
  85. }