util.c 7.0 KB

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