util.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * util.c --- helper functions used by tune2fs and mke2fs
  4. *
  5. * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this tarball for details.
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <errno.h>
  12. #include <linux/major.h>
  13. #include <sys/stat.h>
  14. #include "e2fsbb.h"
  15. #include "e2p/e2p.h"
  16. #include "ext2fs/ext2_fs.h"
  17. #include "ext2fs/ext2fs.h"
  18. #include "blkid/blkid.h"
  19. #include "util.h"
  20. void proceed_question(void)
  21. {
  22. fputs("Proceed anyway? (y,n) ", stdout);
  23. if (bb_ask_confirmation() == 0)
  24. exit(1);
  25. }
  26. void check_plausibility(const char *device, int force)
  27. {
  28. int val;
  29. struct stat s;
  30. val = stat(device, &s);
  31. if (force)
  32. return;
  33. if (val == -1)
  34. bb_perror_msg_and_die("can't stat '%s'", device);
  35. if (!S_ISBLK(s.st_mode)) {
  36. printf("%s is not a block special device.\n", device);
  37. proceed_question();
  38. return;
  39. }
  40. #ifdef HAVE_LINUX_MAJOR_H
  41. #ifndef MAJOR
  42. #define MAJOR(dev) ((dev)>>8)
  43. #define MINOR(dev) ((dev) & 0xff)
  44. #endif
  45. #ifndef SCSI_BLK_MAJOR
  46. #ifdef SCSI_DISK0_MAJOR
  47. #ifdef SCSI_DISK8_MAJOR
  48. #define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
  49. ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR) || \
  50. ((M) >= SCSI_DISK8_MAJOR && (M) <= SCSI_DISK15_MAJOR))
  51. #else
  52. #define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
  53. ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR))
  54. #endif /* defined(SCSI_DISK8_MAJOR) */
  55. #define SCSI_BLK_MAJOR(M) (SCSI_DISK_MAJOR((M)) || (M) == SCSI_CDROM_MAJOR)
  56. #else
  57. #define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
  58. #endif /* defined(SCSI_DISK0_MAJOR) */
  59. #endif /* defined(SCSI_BLK_MAJOR) */
  60. if (((MAJOR(s.st_rdev) == HD_MAJOR &&
  61. MINOR(s.st_rdev)%64 == 0) ||
  62. (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
  63. MINOR(s.st_rdev)%16 == 0))) {
  64. printf("%s is entire device, not just one partition!\n", device);
  65. proceed_question();
  66. }
  67. #endif
  68. }
  69. void check_mount(const char *device, int force, const char *type)
  70. {
  71. errcode_t retval;
  72. int mount_flags;
  73. retval = ext2fs_check_if_mounted(device, &mount_flags);
  74. if (retval) {
  75. bb_error_msg("can't determine if %s is mounted", device);
  76. return;
  77. }
  78. if (mount_flags & EXT2_MF_MOUNTED) {
  79. bb_error_msg("%s is mounted !", device);
  80. force_check:
  81. if (force)
  82. bb_error_msg("badblocks forced anyways");
  83. else
  84. bb_error_msg_and_die("it's not safe to run badblocks!");
  85. }
  86. if (mount_flags & EXT2_MF_BUSY) {
  87. bb_error_msg("%s is apparently in use by the system", device);
  88. goto force_check;
  89. }
  90. }
  91. void parse_journal_opts(char **journal_device, int *journal_flags,
  92. int *journal_size, const char *opts)
  93. {
  94. char *buf, *token, *next, *p, *arg;
  95. int journal_usage = 0;
  96. buf = xstrdup(opts);
  97. for (token = buf; token && *token; token = next) {
  98. p = strchr(token, ',');
  99. next = 0;
  100. if (p) {
  101. *p = 0;
  102. next = p+1;
  103. }
  104. arg = strchr(token, '=');
  105. if (arg) {
  106. *arg = 0;
  107. arg++;
  108. }
  109. if (strcmp(token, "device") == 0) {
  110. *journal_device = blkid_get_devname(NULL, arg, NULL);
  111. if (!journal_device) {
  112. journal_usage++;
  113. continue;
  114. }
  115. } else if (strcmp(token, "size") == 0) {
  116. if (!arg) {
  117. journal_usage++;
  118. continue;
  119. }
  120. (*journal_size) = strtoul(arg, &p, 0);
  121. if (*p)
  122. journal_usage++;
  123. } else if (strcmp(token, "v1_superblock") == 0) {
  124. (*journal_flags) |= EXT2_MKJOURNAL_V1_SUPER;
  125. continue;
  126. } else
  127. journal_usage++;
  128. }
  129. if (journal_usage)
  130. bb_error_msg_and_die(
  131. "\nBad journal options specified.\n\n"
  132. "Journal options are separated by commas, "
  133. "and may take an argument which\n"
  134. "\tis set off by an equals ('=') sign.\n\n"
  135. "Valid journal options are:\n"
  136. "\tsize=<journal size in megabytes>\n"
  137. "\tdevice=<journal device>\n\n"
  138. "The journal size must be between "
  139. "1024 and 102400 filesystem blocks.\n\n");
  140. }
  141. /*
  142. * Determine the number of journal blocks to use, either via
  143. * user-specified # of megabytes, or via some intelligently selected
  144. * defaults.
  145. *
  146. * Find a reasonable journal file size (in blocks) given the number of blocks
  147. * in the filesystem. For very small filesystems, it is not reasonable to
  148. * have a journal that fills more than half of the filesystem.
  149. */
  150. int figure_journal_size(int size, ext2_filsys fs)
  151. {
  152. blk_t j_blocks;
  153. if (fs->super->s_blocks_count < 2048) {
  154. bb_error_msg("Filesystem too small for a journal");
  155. return 0;
  156. }
  157. if (size >= 0) {
  158. j_blocks = size * 1024 / (fs->blocksize / 1024);
  159. if (j_blocks < 1024 || j_blocks > 102400)
  160. bb_error_msg_and_die("\nThe requested journal "
  161. "size is %d blocks;\n it must be "
  162. "between 1024 and 102400 blocks; Aborting",
  163. j_blocks);
  164. if (j_blocks > fs->super->s_free_blocks_count)
  165. bb_error_msg_and_die("Journal size too big for filesystem");
  166. return j_blocks;
  167. }
  168. if (fs->super->s_blocks_count < 32768)
  169. j_blocks = 1024;
  170. else if (fs->super->s_blocks_count < 256*1024)
  171. j_blocks = 4096;
  172. else if (fs->super->s_blocks_count < 512*1024)
  173. j_blocks = 8192;
  174. else if (fs->super->s_blocks_count < 1024*1024)
  175. j_blocks = 16384;
  176. else
  177. j_blocks = 32768;
  178. return j_blocks;
  179. }
  180. void print_check_message(ext2_filsys fs)
  181. {
  182. printf("This filesystem will be automatically "
  183. "checked every %d mounts or\n"
  184. "%g days, whichever comes first. "
  185. "Use tune2fs -c or -i to override.\n",
  186. fs->super->s_max_mnt_count,
  187. (double)fs->super->s_checkinterval / (3600 * 24));
  188. }
  189. void make_journal_device(char *journal_device, ext2_filsys fs, int quiet, int force)
  190. {
  191. errcode_t retval;
  192. ext2_filsys jfs;
  193. io_manager io_ptr;
  194. check_plausibility(journal_device, force);
  195. check_mount(journal_device, force, "journal");
  196. io_ptr = unix_io_manager;
  197. retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
  198. EXT2_FLAG_JOURNAL_DEV_OK, 0,
  199. fs->blocksize, io_ptr, &jfs);
  200. if (retval)
  201. bb_error_msg_and_die("can't journal device %s", journal_device);
  202. if (!quiet)
  203. printf("Adding journal to device %s: ", journal_device);
  204. fflush(stdout);
  205. retval = ext2fs_add_journal_device(fs, jfs);
  206. if (retval)
  207. bb_error_msg_and_die("\nFailed to add journal to device %s", journal_device);
  208. if (!quiet)
  209. puts("done");
  210. ext2fs_close(jfs);
  211. }
  212. void make_journal_blocks(ext2_filsys fs, int journal_size, int journal_flags, int quiet)
  213. {
  214. unsigned long journal_blocks;
  215. errcode_t retval;
  216. journal_blocks = figure_journal_size(journal_size, fs);
  217. if (!journal_blocks) {
  218. fs->super->s_feature_compat &=
  219. ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
  220. return;
  221. }
  222. if (!quiet)
  223. printf("Creating journal (%ld blocks): ", journal_blocks);
  224. fflush(stdout);
  225. retval = ext2fs_add_journal_inode(fs, journal_blocks,
  226. journal_flags);
  227. if (retval)
  228. bb_error_msg_and_die("can't create journal");
  229. if (!quiet)
  230. puts("done");
  231. }
  232. char *e2fs_set_sbin_path(void)
  233. {
  234. char *oldpath = getenv("PATH");
  235. /* Update our PATH to include /sbin */
  236. #define PATH_SET "/sbin"
  237. if (oldpath)
  238. oldpath = xasprintf("%s:%s", PATH_SET, oldpath);
  239. else
  240. oldpath = PATH_SET;
  241. putenv(oldpath);
  242. return oldpath;
  243. }