3
0

tune2fs.c 2.5 KB

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