tune2fs.c 2.7 KB

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