lwext4_mkfs.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * - The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include <getopt.h>
  33. #include <stdbool.h>
  34. #include <inttypes.h>
  35. #include <time.h>
  36. #include <unistd.h>
  37. #include <sys/time.h>
  38. #include <ext4.h>
  39. #include <ext4_mkfs.h>
  40. #include "../blockdev/linux/file_dev.h"
  41. #include "../blockdev/windows/file_windows.h"
  42. /**@brief Input stream name.*/
  43. const char *input_name = NULL;
  44. /**@brief Block device handle.*/
  45. static struct ext4_blockdev *bd;
  46. /**@brief Indicates that input is windows partition.*/
  47. static bool winpart = false;
  48. static int fs_type = F_SET_EXT4;
  49. static struct ext4_fs fs;
  50. static struct ext4_mkfs_info info = {
  51. .block_size = 1024,
  52. .journal = true,
  53. };
  54. static bool verbose = false;
  55. static const char *usage = " \n\
  56. Welcome in lwext4_mkfs tool . \n\
  57. Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com) \n\
  58. Usage: \n\
  59. [-i] --input - input file name (or blockdevice) \n\
  60. [-w] --wpart - windows partition mode \n\
  61. [-v] --verbose - verbose mode \n\
  62. [-b] --block - block size: 1024, 2048, 4096 (default 1024) \n\
  63. [-e] --ext - fs type (ext2: 2, ext3: 3 ext4: 4)) \n\
  64. \n";
  65. static bool open_linux(void)
  66. {
  67. file_dev_name_set(input_name);
  68. bd = file_dev_get();
  69. if (!bd) {
  70. printf("open_filedev: fail\n");
  71. return false;
  72. }
  73. return true;
  74. }
  75. static bool open_windows(void)
  76. {
  77. #ifdef WIN32
  78. file_windows_name_set(input_name);
  79. bd = file_windows_dev_get();
  80. if (!bd) {
  81. printf("open_winpartition: fail\n");
  82. return false;
  83. }
  84. return true;
  85. #else
  86. printf("open_winpartition: this mode should be used only under windows "
  87. "!\n");
  88. return false;
  89. #endif
  90. }
  91. static bool open_filedev(void)
  92. {
  93. return winpart ? open_windows() : open_linux();
  94. }
  95. static bool parse_opt(int argc, char **argv)
  96. {
  97. int option_index = 0;
  98. int c;
  99. static struct option long_options[] = {
  100. {"input", required_argument, 0, 'i'},
  101. {"block", required_argument, 0, 'b'},
  102. {"ext", required_argument, 0, 'e'},
  103. {"wpart", no_argument, 0, 'w'},
  104. {"verbose", no_argument, 0, 'v'},
  105. {"version", no_argument, 0, 'x'},
  106. {0, 0, 0, 0}};
  107. while (-1 != (c = getopt_long(argc, argv, "i:b:e:wvx",
  108. long_options, &option_index))) {
  109. switch (c) {
  110. case 'i':
  111. input_name = optarg;
  112. break;
  113. case 'b':
  114. info.block_size = atoi(optarg);
  115. break;
  116. case 'e':
  117. fs_type = atoi(optarg);
  118. break;
  119. case 'w':
  120. winpart = true;
  121. break;
  122. case 'v':
  123. verbose = true;
  124. break;
  125. case 'x':
  126. puts(VERSION);
  127. exit(0);
  128. break;
  129. default:
  130. printf("%s", usage);
  131. return false;
  132. }
  133. }
  134. switch (info.block_size) {
  135. case 1024:
  136. case 2048:
  137. case 4096:
  138. break;
  139. default:
  140. printf("parse_opt: block_size = %"PRIu32" unsupported\n",
  141. info.block_size);
  142. return false;
  143. }
  144. switch (fs_type) {
  145. case F_SET_EXT2:
  146. case F_SET_EXT3:
  147. case F_SET_EXT4:
  148. break;
  149. default:
  150. printf("parse_opt: fs_type = %"PRIu32" unsupported\n", fs_type);
  151. return false;
  152. }
  153. return true;
  154. }
  155. int main(int argc, char **argv)
  156. {
  157. int r;
  158. if (!parse_opt(argc, argv)){
  159. printf("parse_opt error\n");
  160. return EXIT_FAILURE;
  161. }
  162. if (!open_filedev()) {
  163. printf("open_filedev error\n");
  164. return EXIT_FAILURE;
  165. }
  166. if (verbose)
  167. ext4_dmask_set(DEBUG_ALL);
  168. printf("ext4_mkfs: ext%d\n", fs_type);
  169. r = ext4_mkfs(&fs, bd, &info, fs_type);
  170. if (r != EOK) {
  171. printf("ext4_mkfs error: %d\n", r);
  172. return EXIT_FAILURE;
  173. }
  174. memset(&info, 0, sizeof(struct ext4_mkfs_info));
  175. r = ext4_mkfs_read_info(bd, &info);
  176. if (r != EOK) {
  177. printf("ext4_mkfs_read_info error: %d\n", r);
  178. return EXIT_FAILURE;
  179. }
  180. printf("Created filesystem with parameters:\n");
  181. printf("Size: %"PRIu64"\n", info.len);
  182. printf("Block size: %"PRIu32"\n", info.block_size);
  183. printf("Blocks per group: %"PRIu32"\n", info.blocks_per_group);
  184. printf("Inodes per group: %"PRIu32"\n", info.inodes_per_group);
  185. printf("Inode size: %"PRIu32"\n", info.inode_size);
  186. printf("Inodes: %"PRIu32"\n", info.inodes);
  187. printf("Journal blocks: %"PRIu32"\n", info.journal_blocks);
  188. printf("Features ro_compat: 0x%x\n", info.feat_ro_compat);
  189. printf("Features compat: 0x%x\n", info.feat_compat);
  190. printf("Features incompat: 0x%x\n", info.feat_incompat);
  191. printf("BG desc reserve: %"PRIu32"\n", info.bg_desc_reserve_blocks);
  192. printf("Descriptor size: %"PRIu32"\n",info.dsc_size);
  193. printf("Label: %s\n", info.label);
  194. printf("\nDone ...\n");
  195. return EXIT_SUCCESS;
  196. }