Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * mke2fs.c - Make a ext2fs filesystem. |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 3 | * |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 4 | * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 5 | * 2003, 2004, 2005 by Theodore Ts'o. |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 6 | * |
| 7 | * %Begin-Header% |
| 8 | * This file may be redistributed under the terms of the GNU Public |
| 9 | * License. |
| 10 | * %End-Header% |
| 11 | */ |
| 12 | |
| 13 | /* Usage: mke2fs [options] device |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 14 | * |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 15 | * The device may be a block device or a image of one, but this isn't |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 16 | * enforced (but it's not much fun on a character device :-). |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <string.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <ctype.h> |
| 23 | #include <time.h> |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 24 | #include <getopt.h> |
| 25 | #include <unistd.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <errno.h> |
| 28 | #include <mntent.h> |
| 29 | #include <sys/ioctl.h> |
| 30 | #include <sys/types.h> |
| 31 | |
| 32 | #include "e2fsbb.h" |
| 33 | #include "ext2fs/ext2_fs.h" |
| 34 | #include "uuid/uuid.h" |
| 35 | #include "e2p/e2p.h" |
| 36 | #include "ext2fs/ext2fs.h" |
| 37 | #include "util.h" |
| 38 | |
| 39 | #define STRIDE_LENGTH 8 |
| 40 | |
| 41 | #ifndef __sparc__ |
| 42 | #define ZAP_BOOTBLOCK |
| 43 | #endif |
| 44 | |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 45 | static const char * device_name /* = NULL */; |
| 46 | |
| 47 | /* Command line options */ |
| 48 | static int cflag; |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 49 | static int quiet; |
| 50 | static int super_only; |
| 51 | static int force; |
| 52 | static int noaction; |
| 53 | static int journal_size; |
| 54 | static int journal_flags; |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 55 | static const char *bad_blocks_filename; |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 56 | static __u32 fs_stride; |
| 57 | |
| 58 | static struct ext2_super_block param; |
| 59 | static char *creator_os; |
| 60 | static char *volume_label; |
| 61 | static char *mount_dir; |
| 62 | static char *journal_device; |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 63 | static int sync_kludge; /* Set using the MKE2FS_SYNC env. option */ |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 64 | |
| 65 | static int sys_page_size = 4096; |
| 66 | static int linux_version_code = 0; |
| 67 | |
| 68 | static int int_log2(int arg) |
| 69 | { |
| 70 | int l = 0; |
| 71 | |
| 72 | arg >>= 1; |
| 73 | while (arg) { |
| 74 | l++; |
| 75 | arg >>= 1; |
| 76 | } |
| 77 | return l; |
| 78 | } |
| 79 | |
| 80 | static int int_log10(unsigned int arg) |
| 81 | { |
| 82 | int l; |
| 83 | |
| 84 | for (l=0; arg ; l++) |
| 85 | arg = arg / 10; |
| 86 | return l; |
| 87 | } |
| 88 | |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 89 | /* |
| 90 | * This function sets the default parameters for a filesystem |
| 91 | * |
| 92 | * The type is specified by the user. The size is the maximum size |
| 93 | * (in megabytes) for which a set of parameters applies, with a size |
| 94 | * of zero meaning that it is the default parameter for the type. |
| 95 | * Note that order is important in the table below. |
| 96 | */ |
| 97 | #define DEF_MAX_BLOCKSIZE -1 |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 98 | static const char default_str[] = "default"; |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 99 | struct mke2fs_defaults { |
| 100 | const char *type; |
| 101 | int size; |
| 102 | int blocksize; |
| 103 | int inode_ratio; |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | static const struct mke2fs_defaults settings[] = { |
| 107 | { default_str, 0, 4096, 8192 }, |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 108 | { default_str, 512, 1024, 4096 }, |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 109 | { default_str, 3, 1024, 8192 }, |
| 110 | { "journal", 0, 4096, 8192 }, |
| 111 | { "news", 0, 4096, 4096 }, |
| 112 | { "largefile", 0, 4096, 1024 * 1024 }, |
| 113 | { "largefile4", 0, 4096, 4096 * 1024 }, |
| 114 | { 0, 0, 0, 0}, |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | static void set_fs_defaults(const char *fs_type, |
| 118 | struct ext2_super_block *super, |
| 119 | int blocksize, int sector_size, |
| 120 | int *inode_ratio) |
| 121 | { |
| 122 | int megs; |
| 123 | int ratio = 0; |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 124 | const struct mke2fs_defaults *p; |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 125 | int use_bsize = 1024; |
| 126 | |
| 127 | megs = super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) / 1024; |
| 128 | if (inode_ratio) |
| 129 | ratio = *inode_ratio; |
| 130 | if (!fs_type) |
| 131 | fs_type = default_str; |
| 132 | for (p = settings; p->type; p++) { |
| 133 | if ((strcmp(p->type, fs_type) != 0) && |
| 134 | (strcmp(p->type, default_str) != 0)) |
| 135 | continue; |
| 136 | if ((p->size != 0) && (megs > p->size)) |
| 137 | continue; |
| 138 | if (ratio == 0) |
| 139 | *inode_ratio = p->inode_ratio < blocksize ? |
| 140 | blocksize : p->inode_ratio; |
| 141 | use_bsize = p->blocksize; |
| 142 | } |
| 143 | if (blocksize <= 0) { |
| 144 | if (use_bsize == DEF_MAX_BLOCKSIZE) { |
| 145 | use_bsize = sys_page_size; |
| 146 | if ((linux_version_code < (2*65536 + 6*256)) && |
| 147 | (use_bsize > 4096)) |
| 148 | use_bsize = 4096; |
| 149 | } |
| 150 | if (sector_size && use_bsize < sector_size) |
| 151 | use_bsize = sector_size; |
| 152 | if ((blocksize < 0) && (use_bsize < (-blocksize))) |
| 153 | use_bsize = -blocksize; |
| 154 | blocksize = use_bsize; |
| 155 | super->s_blocks_count /= blocksize / 1024; |
| 156 | } |
| 157 | super->s_log_frag_size = super->s_log_block_size = |
| 158 | int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE); |
| 159 | } |
| 160 | |
| 161 | |
| 162 | /* |
| 163 | * Helper function for read_bb_file and test_disk |
| 164 | */ |
| 165 | static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk) |
| 166 | { |
| 167 | bb_error_msg("Bad block %u out of range; ignored", blk); |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Reads the bad blocks list from a file |
| 173 | */ |
| 174 | static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list, |
| 175 | const char *bad_blocks_file) |
| 176 | { |
| 177 | FILE *f; |
| 178 | errcode_t retval; |
| 179 | |
| 180 | f = fopen(bad_blocks_file, "r"); |
| 181 | if (!f) { |
| 182 | bb_perror_msg_and_die("Could not read bad blocks file %s", bad_blocks_file); |
| 183 | } |
| 184 | retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block); |
| 185 | fclose (f); |
| 186 | if (retval) { |
| 187 | bb_error_msg_and_die("Could not read bad blocks list"); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Runs the badblocks program to test the disk |
| 193 | */ |
| 194 | static void test_disk(ext2_filsys fs, badblocks_list *bb_list) |
| 195 | { |
| 196 | FILE *f; |
| 197 | errcode_t retval; |
| 198 | char buf[1024]; |
| 199 | |
| 200 | sprintf(buf, "badblocks -b %d %s%s%s %d", fs->blocksize, |
| 201 | quiet ? "" : "-s ", (cflag > 1) ? "-w " : "", |
| 202 | fs->device_name, fs->super->s_blocks_count); |
Mike Frysinger | df1eda8 | 2005-06-17 02:13:57 +0000 | [diff] [blame] | 203 | if (!quiet) |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 204 | printf("Running command: %s\n", buf); |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 205 | f = popen(buf, "r"); |
| 206 | if (!f) { |
| 207 | bb_perror_msg_and_die("Could not run '%s'", buf); |
| 208 | } |
| 209 | retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block); |
| 210 | pclose(f); |
| 211 | if (retval) { |
| 212 | bb_error_msg_and_die( |
| 213 | "Could not get list of bad blocks from program"); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list) |
| 218 | { |
| 219 | dgrp_t i; |
| 220 | blk_t j; |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 221 | unsigned must_be_good; |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 222 | blk_t blk; |
| 223 | badblocks_iterate bb_iter; |
| 224 | errcode_t retval; |
| 225 | blk_t group_block; |
| 226 | int group; |
| 227 | int group_bad; |
| 228 | |
| 229 | if (!bb_list) |
| 230 | return; |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 231 | |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 232 | /* |
| 233 | * The primary superblock and group descriptors *must* be |
| 234 | * good; if not, abort. |
| 235 | */ |
| 236 | must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks; |
| 237 | for (i = fs->super->s_first_data_block; i <= must_be_good; i++) { |
| 238 | if (ext2fs_badblocks_list_test(bb_list, i)) { |
| 239 | bb_error_msg_and_die( |
| 240 | "Block %d in primary superblock/group descriptor area bad\n" |
| 241 | "Blocks %d through %d must be good in order to build a filesystem\n" |
| 242 | "Aborting ...", i, fs->super->s_first_data_block, must_be_good); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * See if any of the bad blocks are showing up in the backup |
| 248 | * superblocks and/or group descriptors. If so, issue a |
| 249 | * warning and adjust the block counts appropriately. |
| 250 | */ |
| 251 | group_block = fs->super->s_first_data_block + |
| 252 | fs->super->s_blocks_per_group; |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 253 | |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 254 | for (i = 1; i < fs->group_desc_count; i++) { |
| 255 | group_bad = 0; |
| 256 | for (j=0; j < fs->desc_blocks+1; j++) { |
| 257 | if (ext2fs_badblocks_list_test(bb_list, |
| 258 | group_block + j)) { |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 259 | if (!group_bad) |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 260 | bb_error_msg( |
| 261 | "Warning: the backup superblock/group descriptors at block %d contain\n" |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 262 | " bad blocks\n", group_block); |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 263 | group_bad++; |
| 264 | group = ext2fs_group_of_blk(fs, group_block+j); |
| 265 | fs->group_desc[group].bg_free_blocks_count++; |
| 266 | fs->super->s_free_blocks_count++; |
| 267 | } |
| 268 | } |
| 269 | group_block += fs->super->s_blocks_per_group; |
| 270 | } |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 271 | |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 272 | /* |
| 273 | * Mark all the bad blocks as used... |
| 274 | */ |
| 275 | retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter); |
| 276 | if (retval) { |
| 277 | bb_error_msg_and_die("while marking bad blocks as used"); |
| 278 | } |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 279 | while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 280 | ext2fs_mark_block_bitmap(fs->block_map, blk); |
| 281 | ext2fs_badblocks_list_iterate_end(bb_iter); |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | * These functions implement a generalized progress meter. |
| 286 | */ |
| 287 | struct progress_struct { |
| 288 | char format[20]; |
| 289 | char backup[80]; |
| 290 | __u32 max; |
| 291 | int skip_progress; |
| 292 | }; |
| 293 | |
| 294 | static void progress_init(struct progress_struct *progress, |
| 295 | const char *label,__u32 max) |
| 296 | { |
| 297 | int i; |
| 298 | |
| 299 | memset(progress, 0, sizeof(struct progress_struct)); |
| 300 | if (quiet) |
| 301 | return; |
| 302 | |
| 303 | /* |
| 304 | * Figure out how many digits we need |
| 305 | */ |
| 306 | i = int_log10(max); |
| 307 | sprintf(progress->format, "%%%dd/%%%dld", i, i); |
| 308 | memset(progress->backup, '\b', sizeof(progress->backup)-1); |
| 309 | progress->backup[sizeof(progress->backup)-1] = 0; |
| 310 | if ((2*i)+1 < (int) sizeof(progress->backup)) |
| 311 | progress->backup[(2*i)+1] = 0; |
| 312 | progress->max = max; |
| 313 | |
| 314 | progress->skip_progress = 0; |
| 315 | if (getenv("MKE2FS_SKIP_PROGRESS")) |
| 316 | progress->skip_progress++; |
| 317 | |
| 318 | fputs(label, stdout); |
| 319 | fflush(stdout); |
| 320 | } |
| 321 | |
| 322 | static void progress_update(struct progress_struct *progress, __u32 val) |
| 323 | { |
| 324 | if ((progress->format[0] == 0) || progress->skip_progress) |
| 325 | return; |
| 326 | printf(progress->format, val, progress->max); |
| 327 | fputs(progress->backup, stdout); |
| 328 | } |
| 329 | |
| 330 | static void progress_close(struct progress_struct *progress) |
| 331 | { |
| 332 | if (progress->format[0] == 0) |
| 333 | return; |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 334 | fputs("done \n", stdout); |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | |
| 338 | /* |
| 339 | * Helper function which zeros out _num_ blocks starting at _blk_. In |
| 340 | * case of an error, the details of the error is returned via _ret_blk_ |
| 341 | * and _ret_count_ if they are non-NULL pointers. Returns 0 on |
| 342 | * success, and an error code on an error. |
| 343 | * |
| 344 | * As a special case, if the first argument is NULL, then it will |
| 345 | * attempt to free the static zeroizing buffer. (This is to keep |
| 346 | * programs that check for memory leaks happy.) |
| 347 | */ |
| 348 | static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num, |
| 349 | struct progress_struct *progress, |
| 350 | blk_t *ret_blk, int *ret_count) |
| 351 | { |
| 352 | int j, count, next_update, next_update_incr; |
| 353 | static char *buf; |
| 354 | errcode_t retval; |
| 355 | |
| 356 | /* If fs is null, clean up the static buffer and return */ |
| 357 | if (!fs) { |
| 358 | if (buf) { |
| 359 | free(buf); |
| 360 | buf = 0; |
| 361 | } |
| 362 | return 0; |
| 363 | } |
| 364 | /* Allocate the zeroizing buffer if necessary */ |
| 365 | if (!buf) { |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 366 | buf = xcalloc(fs->blocksize, STRIDE_LENGTH); |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 367 | } |
| 368 | /* OK, do the write loop */ |
| 369 | next_update = 0; |
| 370 | next_update_incr = num / 100; |
| 371 | if (next_update_incr < 1) |
| 372 | next_update_incr = 1; |
| 373 | for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) { |
| 374 | count = num - j; |
| 375 | if (count > STRIDE_LENGTH) |
| 376 | count = STRIDE_LENGTH; |
| 377 | retval = io_channel_write_blk(fs->io, blk, count, buf); |
| 378 | if (retval) { |
| 379 | if (ret_count) |
| 380 | *ret_count = count; |
| 381 | if (ret_blk) |
| 382 | *ret_blk = blk; |
| 383 | return retval; |
| 384 | } |
| 385 | if (progress && j > next_update) { |
| 386 | next_update += num / 100; |
| 387 | progress_update(progress, blk); |
| 388 | } |
| 389 | } |
| 390 | return 0; |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 391 | } |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 392 | |
| 393 | static void write_inode_tables(ext2_filsys fs) |
| 394 | { |
| 395 | errcode_t retval; |
| 396 | blk_t blk; |
| 397 | dgrp_t i; |
| 398 | int num; |
| 399 | struct progress_struct progress; |
| 400 | |
| 401 | if (quiet) |
| 402 | memset(&progress, 0, sizeof(progress)); |
| 403 | else |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 404 | progress_init(&progress, "Writing inode tables: ", |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 405 | fs->group_desc_count); |
| 406 | |
| 407 | for (i = 0; i < fs->group_desc_count; i++) { |
| 408 | progress_update(&progress, i); |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 409 | |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 410 | blk = fs->group_desc[i].bg_inode_table; |
| 411 | num = fs->inode_blocks_per_group; |
| 412 | |
| 413 | retval = zero_blocks(fs, blk, num, 0, &blk, &num); |
| 414 | if (retval) { |
| 415 | bb_error_msg_and_die( |
| 416 | "\nCould not write %d blocks " |
| 417 | "in inode table starting at %d.", |
| 418 | num, blk); |
| 419 | } |
| 420 | if (sync_kludge) { |
| 421 | if (sync_kludge == 1) |
| 422 | sync(); |
| 423 | else if ((i % sync_kludge) == 0) |
| 424 | sync(); |
| 425 | } |
| 426 | } |
| 427 | zero_blocks(0, 0, 0, 0, 0, 0); |
| 428 | progress_close(&progress); |
| 429 | } |
| 430 | |
| 431 | static void create_root_dir(ext2_filsys fs) |
| 432 | { |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 433 | errcode_t retval; |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 434 | struct ext2_inode inode; |
| 435 | |
| 436 | retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0); |
| 437 | if (retval) { |
| 438 | bb_error_msg_and_die("Could not create root dir"); |
| 439 | } |
| 440 | if (geteuid()) { |
| 441 | retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode); |
| 442 | if (retval) { |
| 443 | bb_error_msg_and_die("Could not read root inode"); |
| 444 | } |
| 445 | inode.i_uid = getuid(); |
| 446 | if (inode.i_uid) |
| 447 | inode.i_gid = getgid(); |
| 448 | retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode); |
| 449 | if (retval) { |
| 450 | bb_error_msg_and_die("Could not set root inode ownership"); |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | static void create_lost_and_found(ext2_filsys fs) |
| 456 | { |
| 457 | errcode_t retval; |
| 458 | ext2_ino_t ino; |
| 459 | const char *name = "lost+found"; |
| 460 | int i; |
| 461 | int lpf_size = 0; |
| 462 | |
| 463 | fs->umask = 077; |
| 464 | retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name); |
| 465 | if (retval) { |
| 466 | bb_error_msg_and_die("Could not create lost+found"); |
| 467 | } |
| 468 | |
| 469 | retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino); |
| 470 | if (retval) { |
| 471 | bb_error_msg_and_die("Could not look up lost+found"); |
| 472 | } |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 473 | |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 474 | for (i=1; i < EXT2_NDIR_BLOCKS; i++) { |
| 475 | if ((lpf_size += fs->blocksize) >= 16*1024) |
| 476 | break; |
| 477 | retval = ext2fs_expand_dir(fs, ino); |
| 478 | if (retval) { |
| 479 | bb_error_msg_and_die("Could not expand lost+found"); |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list) |
| 485 | { |
| 486 | errcode_t retval; |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 487 | |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 488 | ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO); |
| 489 | fs->group_desc[0].bg_free_inodes_count--; |
| 490 | fs->super->s_free_inodes_count--; |
| 491 | retval = ext2fs_update_bb_inode(fs, bb_list); |
| 492 | if (retval) { |
| 493 | bb_error_msg_and_die("Could not set bad block inode"); |
| 494 | } |
| 495 | |
| 496 | } |
| 497 | |
| 498 | static void reserve_inodes(ext2_filsys fs) |
| 499 | { |
| 500 | ext2_ino_t i; |
| 501 | int group; |
| 502 | |
| 503 | for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) { |
| 504 | ext2fs_mark_inode_bitmap(fs->inode_map, i); |
| 505 | group = ext2fs_group_of_ino(fs, i); |
| 506 | fs->group_desc[group].bg_free_inodes_count--; |
| 507 | fs->super->s_free_inodes_count--; |
| 508 | } |
| 509 | ext2fs_mark_ib_dirty(fs); |
| 510 | } |
| 511 | |
| 512 | #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */ |
| 513 | #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */ |
| 514 | #define BSD_LABEL_OFFSET 64 |
| 515 | |
| 516 | static void zap_sector(ext2_filsys fs, int sect, int nsect) |
| 517 | { |
| 518 | char *buf; |
| 519 | int retval; |
| 520 | unsigned int *magic; |
| 521 | |
Mike Frysinger | 2401ce5 | 2005-06-11 22:24:15 +0000 | [diff] [blame] | 522 | buf = xmalloc(512*nsect); |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 523 | |
| 524 | if (sect == 0) { |
| 525 | /* Check for a BSD disklabel, and don't erase it if so */ |
| 526 | retval = io_channel_read_blk(fs->io, 0, -512, buf); |
| 527 | if (retval) |
| 528 | bb_error_msg("Warning: could not read block 0"); |
| 529 | else { |
| 530 | magic = (unsigned int *) (buf + BSD_LABEL_OFFSET); |
| 531 | if ((*magic == BSD_DISKMAGIC) || |
| 532 | (*magic == BSD_MAGICDISK)) |
| 533 | return; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | memset(buf, 0, 512*nsect); |
| 538 | io_channel_set_blksize(fs->io, 512); |
| 539 | retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf); |
| 540 | io_channel_set_blksize(fs->io, fs->blocksize); |
| 541 | free(buf); |
| 542 | if (retval) |
| 543 | bb_error_msg("Warning: could not erase sector %d", sect); |
| 544 | } |
| 545 | |
| 546 | static void create_journal_dev(ext2_filsys fs) |
| 547 | { |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 548 | struct progress_struct progress; |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 549 | errcode_t retval; |
| 550 | char *buf; |
| 551 | blk_t blk; |
| 552 | int count; |
| 553 | |
| 554 | retval = ext2fs_create_journal_superblock(fs, |
| 555 | fs->super->s_blocks_count, 0, &buf); |
| 556 | if (retval) { |
| 557 | bb_error_msg_and_die("Could not init journal superblock"); |
| 558 | } |
| 559 | if (quiet) |
| 560 | memset(&progress, 0, sizeof(progress)); |
| 561 | else |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 562 | progress_init(&progress, "Zeroing journal device: ", |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 563 | fs->super->s_blocks_count); |
| 564 | |
| 565 | retval = zero_blocks(fs, 0, fs->super->s_blocks_count, |
| 566 | &progress, &blk, &count); |
| 567 | if (retval) { |
| 568 | bb_error_msg_and_die("Could not zero journal device (block %u, count %d)", |
| 569 | blk, count); |
| 570 | } |
| 571 | zero_blocks(0, 0, 0, 0, 0, 0); |
| 572 | |
| 573 | retval = io_channel_write_blk(fs->io, |
| 574 | fs->super->s_first_data_block+1, |
| 575 | 1, buf); |
| 576 | if (retval) { |
| 577 | bb_error_msg_and_die("Could not write journal superblock"); |
| 578 | } |
| 579 | progress_close(&progress); |
| 580 | } |
| 581 | |
| 582 | static void show_stats(ext2_filsys fs) |
| 583 | { |
| 584 | struct ext2_super_block *s = fs->super; |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 585 | char buf[80]; |
| 586 | char *os; |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 587 | blk_t group_block; |
| 588 | dgrp_t i; |
| 589 | int need, col_left; |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 590 | |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 591 | if (param.s_blocks_count != s->s_blocks_count) |
| 592 | bb_error_msg("warning: %d blocks unused\n", |
| 593 | param.s_blocks_count - s->s_blocks_count); |
| 594 | |
| 595 | memset(buf, 0, sizeof(buf)); |
| 596 | strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name)); |
| 597 | printf("Filesystem label=%s\n", buf); |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 598 | fputs("OS type: ", stdout); |
| 599 | os = e2p_os2string(fs->super->s_creator_os); |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 600 | fputs(os, stdout); |
| 601 | free(os); |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 602 | printf("\nBlock size=%u (log=%u)\n", fs->blocksize, |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 603 | s->s_log_block_size); |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 604 | printf("Fragment size=%u (log=%u)\n", fs->fragsize, |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 605 | s->s_log_frag_size); |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 606 | printf("%u inodes, %u blocks\n", s->s_inodes_count, |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 607 | s->s_blocks_count); |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 608 | printf("%u blocks (%2.2f%%) reserved for the super user\n", |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 609 | s->s_r_blocks_count, |
| 610 | 100.0 * s->s_r_blocks_count / s->s_blocks_count); |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 611 | printf("First data block=%u\n", s->s_first_data_block); |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 612 | if (s->s_reserved_gdt_blocks) |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 613 | printf("Maximum filesystem blocks=%lu\n", |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 614 | (s->s_reserved_gdt_blocks + fs->desc_blocks) * |
| 615 | (fs->blocksize / sizeof(struct ext2_group_desc)) * |
| 616 | s->s_blocks_per_group); |
| 617 | if (fs->group_desc_count > 1) |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 618 | printf("%u block groups\n", fs->group_desc_count); |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 619 | else |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 620 | printf("%u block group\n", fs->group_desc_count); |
| 621 | printf("%u blocks per group, %u fragments per group\n", |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 622 | s->s_blocks_per_group, s->s_frags_per_group); |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 623 | printf("%u inodes per group\n", s->s_inodes_per_group); |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 624 | |
| 625 | if (fs->group_desc_count == 1) { |
| 626 | printf("\n"); |
| 627 | return; |
| 628 | } |
| 629 | |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 630 | printf("Superblock backups stored on blocks: "); |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 631 | group_block = s->s_first_data_block; |
| 632 | col_left = 0; |
| 633 | for (i = 1; i < fs->group_desc_count; i++) { |
| 634 | group_block += s->s_blocks_per_group; |
| 635 | if (!ext2fs_bg_has_super(fs, i)) |
| 636 | continue; |
| 637 | if (i != 1) |
| 638 | printf(", "); |
| 639 | need = int_log10(group_block) + 2; |
| 640 | if (need > col_left) { |
| 641 | printf("\n\t"); |
| 642 | col_left = 72; |
| 643 | } |
| 644 | col_left -= need; |
| 645 | printf("%u", group_block); |
| 646 | } |
| 647 | printf("\n\n"); |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | * Set the S_CREATOR_OS field. Return true if OS is known, |
| 652 | * otherwise, 0. |
| 653 | */ |
| 654 | static int set_os(struct ext2_super_block *sb, char *os) |
| 655 | { |
| 656 | if (isdigit (*os)) |
| 657 | sb->s_creator_os = atoi (os); |
| 658 | else if (strcasecmp(os, "linux") == 0) |
| 659 | sb->s_creator_os = EXT2_OS_LINUX; |
| 660 | else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0) |
| 661 | sb->s_creator_os = EXT2_OS_HURD; |
| 662 | else if (strcasecmp(os, "masix") == 0) |
| 663 | sb->s_creator_os = EXT2_OS_MASIX; |
| 664 | else if (strcasecmp(os, "freebsd") == 0) |
| 665 | sb->s_creator_os = EXT2_OS_FREEBSD; |
| 666 | else if (strcasecmp(os, "lites") == 0) |
| 667 | sb->s_creator_os = EXT2_OS_LITES; |
| 668 | else |
| 669 | return 0; |
| 670 | |
| 671 | return 1; |
| 672 | } |
| 673 | |
| 674 | #define PATH_SET "PATH=/sbin" |
| 675 | |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 676 | static void parse_extended_opts(struct ext2_super_block *sb_param, |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 677 | const char *opts) |
| 678 | { |
| 679 | char *buf, *token, *next, *p, *arg; |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 680 | int r_usage = 0; |
| 681 | |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 682 | buf = bb_xstrdup(opts); |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 683 | for (token = buf; token && *token; token = next) { |
| 684 | p = strchr(token, ','); |
| 685 | next = 0; |
| 686 | if (p) { |
| 687 | *p = 0; |
| 688 | next = p+1; |
| 689 | } |
| 690 | arg = strchr(token, '='); |
| 691 | if (arg) { |
| 692 | *arg = 0; |
| 693 | arg++; |
| 694 | } |
| 695 | if (strcmp(token, "stride") == 0) { |
| 696 | if (!arg) { |
| 697 | r_usage++; |
| 698 | continue; |
| 699 | } |
| 700 | fs_stride = strtoul(arg, &p, 0); |
| 701 | if (*p || (fs_stride == 0)) { |
| 702 | bb_error_msg("Invalid stride parameter"); |
| 703 | r_usage++; |
| 704 | continue; |
| 705 | } |
| 706 | } else if (!strcmp(token, "resize")) { |
| 707 | unsigned long resize, bpg, rsv_groups; |
| 708 | unsigned long group_desc_count, desc_blocks; |
| 709 | unsigned int gdpb, blocksize; |
| 710 | int rsv_gdb; |
| 711 | |
| 712 | if (!arg) { |
| 713 | r_usage++; |
| 714 | continue; |
| 715 | } |
| 716 | |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 717 | resize = parse_num_blocks(arg, |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 718 | sb_param->s_log_block_size); |
| 719 | |
| 720 | if (resize == 0) { |
| 721 | bb_error_msg("Invalid resize parameter: %s", arg); |
| 722 | r_usage++; |
| 723 | continue; |
| 724 | } |
| 725 | if (resize <= sb_param->s_blocks_count) { |
| 726 | bb_error_msg("The resize maximum must be greater than the filesystem size"); |
| 727 | r_usage++; |
| 728 | continue; |
| 729 | } |
| 730 | |
| 731 | blocksize = EXT2_BLOCK_SIZE(sb_param); |
| 732 | bpg = sb_param->s_blocks_per_group; |
| 733 | if (!bpg) |
| 734 | bpg = blocksize * 8; |
| 735 | gdpb = blocksize / sizeof(struct ext2_group_desc); |
| 736 | group_desc_count = (sb_param->s_blocks_count + |
| 737 | bpg - 1) / bpg; |
| 738 | desc_blocks = (group_desc_count + |
| 739 | gdpb - 1) / gdpb; |
| 740 | rsv_groups = (resize + bpg - 1) / bpg; |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 741 | rsv_gdb = (rsv_groups + gdpb - 1) / gdpb - |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 742 | desc_blocks; |
| 743 | if (rsv_gdb > EXT2_ADDR_PER_BLOCK(sb_param)) |
| 744 | rsv_gdb = EXT2_ADDR_PER_BLOCK(sb_param); |
| 745 | |
| 746 | if (rsv_gdb > 0) { |
| 747 | sb_param->s_feature_compat |= |
| 748 | EXT2_FEATURE_COMPAT_RESIZE_INODE; |
| 749 | |
| 750 | sb_param->s_reserved_gdt_blocks = rsv_gdb; |
| 751 | } |
| 752 | } else |
| 753 | r_usage++; |
| 754 | } |
| 755 | if (r_usage) { |
| 756 | bb_error_msg_and_die( |
| 757 | "\nBad options specified.\n\n" |
| 758 | "Options are separated by commas, " |
| 759 | "and may take an argument which\n" |
| 760 | "\tis set off by an equals ('=') sign.\n\n" |
| 761 | "Valid raid options are:\n" |
| 762 | "\tstride=<stride length in blocks>\n" |
| 763 | "\tresize=<resize maximum size in blocks>\n"); |
| 764 | } |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 765 | } |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 766 | |
| 767 | static __u32 ok_features[3] = { |
| 768 | EXT3_FEATURE_COMPAT_HAS_JOURNAL | |
| 769 | EXT2_FEATURE_COMPAT_RESIZE_INODE | |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 770 | EXT2_FEATURE_COMPAT_DIR_INDEX, /* Compat */ |
| 771 | EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */ |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 772 | EXT3_FEATURE_INCOMPAT_JOURNAL_DEV| |
| 773 | EXT2_FEATURE_INCOMPAT_META_BG, |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 774 | EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */ |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 775 | }; |
| 776 | |
| 777 | |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 778 | static int PRS(int argc, char *argv[]) |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 779 | { |
| 780 | int b, c; |
| 781 | int size; |
| 782 | char * tmp; |
| 783 | int blocksize = 0; |
| 784 | int inode_ratio = 0; |
| 785 | int inode_size = 0; |
| 786 | int reserved_ratio = 5; |
| 787 | int sector_size = 0; |
| 788 | int show_version_only = 0; |
| 789 | ext2_ino_t num_inodes = 0; |
| 790 | errcode_t retval; |
| 791 | char * oldpath = getenv("PATH"); |
| 792 | char * extended_opts = 0; |
| 793 | const char * fs_type = 0; |
| 794 | blk_t dev_size; |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 795 | long sysval; |
| 796 | |
| 797 | /* Update our PATH to include /sbin */ |
| 798 | if (oldpath) { |
| 799 | char *newpath; |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 800 | |
| 801 | bb_xasprintf(&newpath, "%s:%s", PATH_SET, oldpath); |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 802 | } else |
| 803 | putenv (PATH_SET); |
| 804 | |
| 805 | tmp = getenv("MKE2FS_SYNC"); |
| 806 | if (tmp) |
| 807 | sync_kludge = atoi(tmp); |
| 808 | |
| 809 | /* Determine the system page size if possible */ |
| 810 | #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE)) |
| 811 | #define _SC_PAGESIZE _SC_PAGE_SIZE |
| 812 | #endif |
| 813 | #ifdef _SC_PAGESIZE |
| 814 | sysval = sysconf(_SC_PAGESIZE); |
| 815 | if (sysval > 0) |
| 816 | sys_page_size = sysval; |
| 817 | #endif /* _SC_PAGESIZE */ |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 818 | |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 819 | setbuf(stdout, NULL); |
| 820 | setbuf(stderr, NULL); |
| 821 | memset(¶m, 0, sizeof(struct ext2_super_block)); |
| 822 | param.s_rev_level = 1; /* Create revision 1 filesystems now */ |
| 823 | param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE; |
| 824 | param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; |
| 825 | #if 0 |
| 826 | param.s_feature_compat |= EXT2_FEATURE_COMPAT_DIR_INDEX; |
| 827 | #endif |
| 828 | |
| 829 | #ifdef __linux__ |
Mike Frysinger | d0615ae | 2005-06-17 01:35:52 +0000 | [diff] [blame] | 830 | linux_version_code = get_kernel_revision(); |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 831 | if (linux_version_code && linux_version_code < (2*65536 + 2*256)) { |
| 832 | param.s_rev_level = 0; |
| 833 | param.s_feature_incompat = 0; |
| 834 | param.s_feature_compat = 0; |
| 835 | param.s_feature_ro_compat = 0; |
| 836 | } |
| 837 | #endif |
| 838 | |
| 839 | if (argc && *argv) { |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 840 | /* If called as mkfs.ext3, create a journal inode */ |
| 841 | if (!strcmp(*argv + strlen(*argv) - 9, "mkfs.ext3")) |
| 842 | journal_size = -1; |
| 843 | } |
| 844 | |
| 845 | while ((c = getopt (argc, argv, |
| 846 | "b:cE:f:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF) { |
| 847 | switch (c) { |
| 848 | case 'b': |
| 849 | blocksize = strtol(optarg, &tmp, 0); |
| 850 | b = (blocksize > 0) ? blocksize : -blocksize; |
| 851 | if (b < EXT2_MIN_BLOCK_SIZE || |
| 852 | b > EXT2_MAX_BLOCK_SIZE || *tmp) { |
| 853 | bb_error_msg_and_die("bad block size - %s", optarg); |
| 854 | } |
| 855 | if (blocksize > 4096) |
| 856 | bb_error_msg( |
| 857 | "Warning: blocksize %d not usable on most systems", |
| 858 | blocksize); |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 859 | if (blocksize > 0) |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 860 | param.s_log_block_size = |
| 861 | int_log2(blocksize >> |
| 862 | EXT2_MIN_BLOCK_LOG_SIZE); |
| 863 | break; |
| 864 | case 'c': /* Check for bad blocks */ |
| 865 | case 't': /* deprecated */ |
| 866 | cflag++; |
| 867 | break; |
| 868 | case 'f': |
| 869 | size = strtoul(optarg, &tmp, 0); |
| 870 | if (size < EXT2_MIN_BLOCK_SIZE || |
| 871 | size > EXT2_MAX_BLOCK_SIZE || *tmp) { |
| 872 | bb_error_msg_and_die("bad fragment size - %s", optarg); |
| 873 | } |
| 874 | param.s_log_frag_size = |
| 875 | int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE); |
| 876 | bb_error_msg( |
| 877 | "Warning: fragments not supported. " |
| 878 | "Ignoring -f option"); |
| 879 | break; |
| 880 | case 'g': |
| 881 | param.s_blocks_per_group = strtoul(optarg, &tmp, 0); |
| 882 | if (*tmp) { |
| 883 | bb_error_msg_and_die("Illegal number for blocks per group"); |
| 884 | } |
| 885 | if ((param.s_blocks_per_group % 8) != 0) { |
| 886 | bb_error_msg_and_die("blocks per group must be multiple of 8"); |
| 887 | } |
| 888 | break; |
| 889 | case 'i': |
| 890 | inode_ratio = strtoul(optarg, &tmp, 0); |
| 891 | if (inode_ratio < EXT2_MIN_BLOCK_SIZE || |
| 892 | inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 || |
| 893 | *tmp) { |
| 894 | bb_error_msg_and_die("bad inode ratio %s (min %d/max %d", |
| 895 | optarg, EXT2_MIN_BLOCK_SIZE, |
| 896 | EXT2_MAX_BLOCK_SIZE); |
| 897 | } |
| 898 | break; |
| 899 | case 'J': |
| 900 | parse_journal_opts(&journal_device, &journal_flags, &journal_size, optarg); |
| 901 | break; |
| 902 | case 'j': |
| 903 | param.s_feature_compat |= |
| 904 | EXT3_FEATURE_COMPAT_HAS_JOURNAL; |
| 905 | if (!journal_size) |
| 906 | journal_size = -1; |
| 907 | break; |
| 908 | case 'l': |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 909 | bad_blocks_filename = optarg; |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 910 | break; |
| 911 | case 'm': |
| 912 | reserved_ratio = strtoul(optarg, &tmp, 0); |
| 913 | if (reserved_ratio > 50 || *tmp) { |
| 914 | bb_error_msg_and_die("bad reserved blocks percent - %s", optarg); |
| 915 | } |
| 916 | break; |
| 917 | case 'n': |
| 918 | noaction++; |
| 919 | break; |
| 920 | case 'o': |
| 921 | creator_os = optarg; |
| 922 | break; |
| 923 | case 'r': |
| 924 | param.s_rev_level = atoi(optarg); |
| 925 | if (param.s_rev_level == EXT2_GOOD_OLD_REV) { |
| 926 | param.s_feature_incompat = 0; |
| 927 | param.s_feature_compat = 0; |
| 928 | param.s_feature_ro_compat = 0; |
| 929 | } |
| 930 | break; |
| 931 | case 's': /* deprecated */ |
| 932 | if (atoi(optarg)) |
| 933 | param.s_feature_ro_compat |= |
| 934 | EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 935 | else |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 936 | param.s_feature_ro_compat &= |
| 937 | ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; |
| 938 | break; |
| 939 | #ifdef EXT2_DYNAMIC_REV |
| 940 | case 'I': |
| 941 | inode_size = strtoul(optarg, &tmp, 0); |
| 942 | if (*tmp) { |
| 943 | bb_error_msg_and_die("bad inode size - %s", optarg); |
| 944 | } |
| 945 | break; |
| 946 | #endif |
| 947 | case 'N': |
| 948 | num_inodes = atoi(optarg); |
| 949 | break; |
| 950 | case 'v': |
Mike Frysinger | df1eda8 | 2005-06-17 02:13:57 +0000 | [diff] [blame] | 951 | quiet = 0; |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 952 | break; |
| 953 | case 'q': |
| 954 | quiet = 1; |
| 955 | break; |
| 956 | case 'F': |
| 957 | force = 1; |
| 958 | break; |
| 959 | case 'L': |
| 960 | volume_label = optarg; |
| 961 | break; |
| 962 | case 'M': |
| 963 | mount_dir = optarg; |
| 964 | break; |
| 965 | case 'O': |
| 966 | if (!strcmp(optarg, "none")) { |
| 967 | param.s_feature_compat = 0; |
| 968 | param.s_feature_incompat = 0; |
| 969 | param.s_feature_ro_compat = 0; |
| 970 | break; |
| 971 | } |
| 972 | if (e2p_edit_feature(optarg, |
| 973 | ¶m.s_feature_compat, |
| 974 | ok_features)) { |
| 975 | bb_error_msg_and_die("Invalid filesystem option set: %s", optarg); |
| 976 | } |
| 977 | break; |
| 978 | case 'E': |
| 979 | case 'R': |
| 980 | extended_opts = optarg; |
| 981 | break; |
| 982 | case 'S': |
| 983 | super_only = 1; |
| 984 | break; |
| 985 | case 'T': |
| 986 | fs_type = optarg; |
| 987 | break; |
| 988 | case 'V': |
| 989 | /* Print version number and exit */ |
| 990 | show_version_only++; |
| 991 | break; |
| 992 | default: |
| 993 | bb_show_usage(); |
| 994 | } |
| 995 | } |
| 996 | if ((optind == argc) && !show_version_only) |
| 997 | bb_show_usage(); |
| 998 | device_name = argv[optind++]; |
| 999 | |
| 1000 | if (!quiet || show_version_only) |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 1001 | bb_error_msg("mke2fs %s (%s)", E2FSPROGS_VERSION, |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 1002 | E2FSPROGS_DATE); |
| 1003 | |
| 1004 | if (show_version_only) { |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 1005 | return 0; |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | /* |
| 1009 | * If there's no blocksize specified and there is a journal |
| 1010 | * device, use it to figure out the blocksize |
| 1011 | */ |
| 1012 | if (blocksize <= 0 && journal_device) { |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 1013 | ext2_filsys jfs; |
| 1014 | io_manager io_ptr; |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 1015 | |
| 1016 | #ifdef CONFIG_TESTIO_DEBUG |
| 1017 | io_ptr = test_io_manager; |
| 1018 | test_io_backing_manager = unix_io_manager; |
| 1019 | #else |
| 1020 | io_ptr = unix_io_manager; |
| 1021 | #endif |
| 1022 | retval = ext2fs_open(journal_device, |
| 1023 | EXT2_FLAG_JOURNAL_DEV_OK, 0, |
| 1024 | 0, io_ptr, &jfs); |
| 1025 | if (retval) { |
| 1026 | bb_error_msg_and_die("Could not open journal device %s", journal_device); |
| 1027 | } |
| 1028 | if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) { |
| 1029 | bb_error_msg_and_die( |
| 1030 | "Journal dev blocksize (%d) smaller than " |
| 1031 | "minimum blocksize %d\n", jfs->blocksize, |
| 1032 | -blocksize); |
| 1033 | } |
| 1034 | blocksize = jfs->blocksize; |
| 1035 | param.s_log_block_size = |
| 1036 | int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE); |
| 1037 | ext2fs_close(jfs); |
| 1038 | } |
| 1039 | |
| 1040 | if (blocksize > sys_page_size) { |
| 1041 | if (!force) { |
| 1042 | bb_error_msg("%d-byte blocks too big for system (max %d)", |
| 1043 | blocksize, sys_page_size); |
| 1044 | proceed_question(); |
| 1045 | } |
| 1046 | bb_error_msg( |
| 1047 | "Warning: %d-byte blocks too big for system " |
| 1048 | "(max %d), forced to continue", |
| 1049 | blocksize, sys_page_size); |
| 1050 | } |
| 1051 | if ((blocksize > 4096) && |
| 1052 | (param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) |
| 1053 | bb_error_msg( |
| 1054 | "\nWarning: some 2.4 kernels do not support " |
| 1055 | "blocksizes greater than 4096 \n\tusing ext3." |
| 1056 | " Use -b 4096 if this is an issue for you\n"); |
| 1057 | |
| 1058 | if (optind < argc) { |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 1059 | param.s_blocks_count = parse_num_blocks(argv[optind++], |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 1060 | param.s_log_block_size); |
| 1061 | if (!param.s_blocks_count) { |
| 1062 | bb_error_msg_and_die("bad blocks count - %s", argv[optind - 1]); |
| 1063 | } |
| 1064 | } |
| 1065 | if (optind < argc) |
| 1066 | bb_show_usage(); |
| 1067 | |
| 1068 | if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { |
| 1069 | if (!fs_type) |
| 1070 | fs_type = "journal"; |
| 1071 | reserved_ratio = 0; |
| 1072 | param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV; |
| 1073 | param.s_feature_compat = 0; |
| 1074 | param.s_feature_ro_compat = 0; |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 1075 | } |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 1076 | if (param.s_rev_level == EXT2_GOOD_OLD_REV && |
| 1077 | (param.s_feature_compat || param.s_feature_ro_compat || |
| 1078 | param.s_feature_incompat)) |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 1079 | param.s_rev_level = 1; /* Create a revision 1 filesystem */ |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 1080 | |
| 1081 | if (!force) |
| 1082 | check_plausibility(device_name); |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 1083 | check_mount(device_name, force, "filesystem"); |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 1084 | |
| 1085 | param.s_log_frag_size = param.s_log_block_size; |
| 1086 | |
| 1087 | if (noaction && param.s_blocks_count) { |
| 1088 | dev_size = param.s_blocks_count; |
| 1089 | retval = 0; |
| 1090 | } else { |
| 1091 | retry: |
| 1092 | retval = ext2fs_get_device_size(device_name, |
| 1093 | EXT2_BLOCK_SIZE(¶m), |
| 1094 | &dev_size); |
| 1095 | if ((retval == EFBIG) && |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 1096 | (blocksize == 0) && |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 1097 | (param.s_log_block_size == 0)) { |
| 1098 | param.s_log_block_size = 2; |
| 1099 | blocksize = 4096; |
| 1100 | goto retry; |
| 1101 | } |
| 1102 | } |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 1103 | |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 1104 | if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) { |
| 1105 | bb_error_msg_and_die("Could not determine filesystem size"); |
| 1106 | } |
| 1107 | if (!param.s_blocks_count) { |
| 1108 | if (retval == EXT2_ET_UNIMPLEMENTED) { |
| 1109 | bb_error_msg_and_die( |
| 1110 | "Couldn't determine device size; you " |
| 1111 | "must specify\nthe size of the " |
| 1112 | "filesystem"); |
| 1113 | } else { |
| 1114 | if (dev_size == 0) { |
| 1115 | bb_error_msg_and_die( |
| 1116 | "Device size reported to be zero. " |
| 1117 | "Invalid partition specified, or\n\t" |
| 1118 | "partition table wasn't reread " |
| 1119 | "after running fdisk, due to\n\t" |
| 1120 | "a modified partition being busy " |
| 1121 | "and in use. You may need to reboot\n\t" |
| 1122 | "to re-read your partition table.\n" |
| 1123 | ); |
| 1124 | } |
| 1125 | param.s_blocks_count = dev_size; |
| 1126 | if (sys_page_size > EXT2_BLOCK_SIZE(¶m)) |
| 1127 | param.s_blocks_count &= ~((sys_page_size / |
| 1128 | EXT2_BLOCK_SIZE(¶m))-1); |
| 1129 | } |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 1130 | |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 1131 | } else if (!force && (param.s_blocks_count > dev_size)) { |
| 1132 | bb_error_msg("Filesystem larger than apparent device size"); |
| 1133 | proceed_question(); |
| 1134 | } |
| 1135 | |
| 1136 | /* |
| 1137 | * If the user asked for HAS_JOURNAL, then make sure a journal |
| 1138 | * gets created. |
| 1139 | */ |
| 1140 | if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) && |
| 1141 | !journal_size) |
| 1142 | journal_size = -1; |
| 1143 | |
| 1144 | /* Set first meta blockgroup via an environment variable */ |
| 1145 | /* (this is mostly for debugging purposes) */ |
| 1146 | if ((param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) && |
| 1147 | ((tmp = getenv("MKE2FS_FIRST_META_BG")))) |
| 1148 | param.s_first_meta_bg = atoi(tmp); |
| 1149 | |
| 1150 | /* Get the hardware sector size, if available */ |
| 1151 | retval = ext2fs_get_device_sectsize(device_name, §or_size); |
| 1152 | if (retval) { |
| 1153 | bb_error_msg_and_die("Could not determine hardware sector size"); |
| 1154 | } |
| 1155 | |
| 1156 | if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL) |
| 1157 | sector_size = atoi(tmp); |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 1158 | |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 1159 | set_fs_defaults(fs_type, ¶m, blocksize, sector_size, &inode_ratio); |
| 1160 | blocksize = EXT2_BLOCK_SIZE(¶m); |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 1161 | |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 1162 | if (extended_opts) |
| 1163 | parse_extended_opts(¶m, extended_opts); |
| 1164 | |
| 1165 | /* Since sparse_super is the default, we would only have a problem |
| 1166 | * here if it was explicitly disabled. |
| 1167 | */ |
| 1168 | if ((param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) && |
| 1169 | !(param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) { |
| 1170 | bb_error_msg_and_die("reserved online resize blocks not supported " |
| 1171 | "on non-sparse filesystem"); |
| 1172 | } |
| 1173 | |
| 1174 | if (param.s_blocks_per_group) { |
| 1175 | if (param.s_blocks_per_group < 256 || |
| 1176 | param.s_blocks_per_group > 8 * (unsigned) blocksize) { |
| 1177 | bb_error_msg_and_die("blocks per group count out of range"); |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | if (inode_size) { |
| 1182 | if (inode_size < EXT2_GOOD_OLD_INODE_SIZE || |
| 1183 | inode_size > EXT2_BLOCK_SIZE(¶m) || |
| 1184 | inode_size & (inode_size - 1)) { |
| 1185 | bb_error_msg_and_die("bad inode size %d (min %d/max %d)", |
| 1186 | inode_size, EXT2_GOOD_OLD_INODE_SIZE, |
| 1187 | blocksize); |
| 1188 | } |
| 1189 | if (inode_size != EXT2_GOOD_OLD_INODE_SIZE) |
| 1190 | bb_error_msg( |
| 1191 | "Warning: %d-byte inodes not usable on most systems", |
| 1192 | inode_size); |
| 1193 | param.s_inode_size = inode_size; |
| 1194 | } |
| 1195 | |
| 1196 | /* |
| 1197 | * Calculate number of inodes based on the inode ratio |
| 1198 | */ |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 1199 | param.s_inodes_count = num_inodes ? num_inodes : |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 1200 | ((__u64) param.s_blocks_count * blocksize) |
| 1201 | / inode_ratio; |
| 1202 | |
| 1203 | /* |
| 1204 | * Calculate number of blocks to reserve |
| 1205 | */ |
| 1206 | param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100; |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 1207 | return 1; |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | int mke2fs_main (int argc, char *argv[]) |
| 1211 | { |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 1212 | errcode_t retval; |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 1213 | ext2_filsys fs; |
| 1214 | badblocks_list bb_list = 0; |
| 1215 | int journal_blocks; |
| 1216 | unsigned int i; |
| 1217 | int val; |
| 1218 | io_manager io_ptr; |
| 1219 | |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 1220 | if(!PRS(argc, argv)) |
| 1221 | return 0; |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 1222 | |
| 1223 | #ifdef CONFIG_TESTIO_DEBUG |
| 1224 | io_ptr = test_io_manager; |
| 1225 | test_io_backing_manager = unix_io_manager; |
| 1226 | #else |
| 1227 | io_ptr = unix_io_manager; |
| 1228 | #endif |
| 1229 | |
| 1230 | /* |
| 1231 | * Initialize the superblock.... |
| 1232 | */ |
| 1233 | retval = ext2fs_initialize(device_name, 0, ¶m, |
| 1234 | io_ptr, &fs); |
| 1235 | if (retval) { |
| 1236 | bb_error_msg_and_die("Could not set up superblock"); |
| 1237 | } |
| 1238 | |
| 1239 | /* |
| 1240 | * Wipe out the old on-disk superblock |
| 1241 | */ |
| 1242 | if (!noaction) |
| 1243 | zap_sector(fs, 2, 6); |
| 1244 | |
| 1245 | /* |
| 1246 | * Generate a UUID for it... |
| 1247 | */ |
| 1248 | uuid_generate(fs->super->s_uuid); |
| 1249 | |
| 1250 | /* |
| 1251 | * Initialize the directory index variables |
| 1252 | */ |
| 1253 | fs->super->s_def_hash_version = EXT2_HASH_TEA; |
| 1254 | uuid_generate((unsigned char *) fs->super->s_hash_seed); |
| 1255 | |
| 1256 | /* |
| 1257 | * Add "jitter" to the superblock's check interval so that we |
| 1258 | * don't check all the filesystems at the same time. We use a |
| 1259 | * kludgy hack of using the UUID to derive a random jitter value. |
| 1260 | */ |
| 1261 | for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++) |
| 1262 | val += fs->super->s_uuid[i]; |
| 1263 | fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT; |
| 1264 | |
| 1265 | /* |
| 1266 | * Override the creator OS, if applicable |
| 1267 | */ |
| 1268 | if (creator_os && !set_os(fs->super, creator_os)) { |
| 1269 | bb_error_msg_and_die("unknown os - %s", creator_os); |
| 1270 | } |
| 1271 | |
| 1272 | /* |
| 1273 | * For the Hurd, we will turn off filetype since it doesn't |
| 1274 | * support it. |
| 1275 | */ |
| 1276 | if (fs->super->s_creator_os == EXT2_OS_HURD) |
| 1277 | fs->super->s_feature_incompat &= |
| 1278 | ~EXT2_FEATURE_INCOMPAT_FILETYPE; |
| 1279 | |
| 1280 | /* |
| 1281 | * Set the volume label... |
| 1282 | */ |
| 1283 | if (volume_label) { |
| 1284 | memset(fs->super->s_volume_name, 0, |
| 1285 | sizeof(fs->super->s_volume_name)); |
| 1286 | strncpy(fs->super->s_volume_name, volume_label, |
| 1287 | sizeof(fs->super->s_volume_name)); |
| 1288 | } |
| 1289 | |
| 1290 | /* |
| 1291 | * Set the last mount directory |
| 1292 | */ |
| 1293 | if (mount_dir) { |
| 1294 | memset(fs->super->s_last_mounted, 0, |
| 1295 | sizeof(fs->super->s_last_mounted)); |
| 1296 | strncpy(fs->super->s_last_mounted, mount_dir, |
| 1297 | sizeof(fs->super->s_last_mounted)); |
| 1298 | } |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 1299 | |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 1300 | if (!quiet || noaction) |
| 1301 | show_stats(fs); |
| 1302 | |
| 1303 | if (noaction) |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 1304 | return 0; |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 1305 | |
| 1306 | if (fs->super->s_feature_incompat & |
| 1307 | EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { |
| 1308 | create_journal_dev(fs); |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 1309 | return (ext2fs_close(fs) ? 1 : 0); |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | if (bad_blocks_filename) |
| 1313 | read_bb_file(fs, &bb_list, bad_blocks_filename); |
| 1314 | if (cflag) |
| 1315 | test_disk(fs, &bb_list); |
| 1316 | |
| 1317 | handle_bad_blocks(fs, bb_list); |
| 1318 | fs->stride = fs_stride; |
| 1319 | retval = ext2fs_allocate_tables(fs); |
| 1320 | if (retval) { |
| 1321 | bb_error_msg_and_die("Could not allocate filesystem tables"); |
| 1322 | } |
| 1323 | if (super_only) { |
| 1324 | fs->super->s_state |= EXT2_ERROR_FS; |
| 1325 | fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY); |
| 1326 | } else { |
| 1327 | /* rsv must be a power of two (64kB is MD RAID sb alignment) */ |
| 1328 | unsigned int rsv = 65536 / fs->blocksize; |
| 1329 | unsigned long blocks = fs->super->s_blocks_count; |
| 1330 | unsigned long start; |
| 1331 | blk_t ret_blk; |
| 1332 | |
| 1333 | #ifdef ZAP_BOOTBLOCK |
| 1334 | zap_sector(fs, 0, 2); |
| 1335 | #endif |
| 1336 | |
| 1337 | /* |
| 1338 | * Wipe out any old MD RAID (or other) metadata at the end |
| 1339 | * of the device. This will also verify that the device is |
| 1340 | * as large as we think. Be careful with very small devices. |
| 1341 | */ |
| 1342 | start = (blocks & ~(rsv - 1)); |
| 1343 | if (start > rsv) |
| 1344 | start -= rsv; |
| 1345 | if (start > 0) |
| 1346 | retval = zero_blocks(fs, start, blocks - start, |
| 1347 | NULL, &ret_blk, NULL); |
| 1348 | |
| 1349 | if (retval) { |
| 1350 | bb_error_msg("Could not zero block %u at end of filesystem", ret_blk); |
| 1351 | } |
| 1352 | write_inode_tables(fs); |
| 1353 | create_root_dir(fs); |
| 1354 | create_lost_and_found(fs); |
| 1355 | reserve_inodes(fs); |
| 1356 | create_bad_block_inode(fs, bb_list); |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 1357 | if (fs->super->s_feature_compat & |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 1358 | EXT2_FEATURE_COMPAT_RESIZE_INODE) { |
| 1359 | retval = ext2fs_create_resize_inode(fs); |
| 1360 | if (retval) { |
| 1361 | bb_error_msg_and_die("Could not reserve blocks for online resize"); |
| 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | if (journal_device) { |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 1367 | ext2_filsys jfs; |
| 1368 | |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 1369 | if (!force) |
"Vladimir N. Oleynik" | b71e602 | 2005-09-19 13:48:39 +0000 | [diff] [blame] | 1370 | check_plausibility(journal_device); |
| 1371 | check_mount(journal_device, force, "journal"); |
Mike Frysinger | 6447ac0 | 2005-06-11 05:29:40 +0000 | [diff] [blame] | 1372 | |
| 1373 | retval = ext2fs_open(journal_device, EXT2_FLAG_RW| |
| 1374 | EXT2_FLAG_JOURNAL_DEV_OK, 0, |
| 1375 | fs->blocksize, unix_io_manager, &jfs); |
| 1376 | if (retval) { |
| 1377 | bb_error_msg_and_die("Could not open journal device %s", journal_device); |
| 1378 | } |
| 1379 | if (!quiet) { |
| 1380 | printf("Adding journal to device %s: ", journal_device); |
| 1381 | fflush(stdout); |
| 1382 | } |
| 1383 | retval = ext2fs_add_journal_device(fs, jfs); |
| 1384 | if(retval) { |
| 1385 | bb_error_msg_and_die("Could not add journal to device %s", journal_device); |
| 1386 | } |
| 1387 | if (!quiet) |
| 1388 | printf("done\n"); |
| 1389 | ext2fs_close(jfs); |
| 1390 | free(journal_device); |
| 1391 | } else if (journal_size) { |
| 1392 | journal_blocks = figure_journal_size(journal_size, fs); |
| 1393 | |
| 1394 | if (!journal_blocks) { |
| 1395 | fs->super->s_feature_compat &= |
| 1396 | ~EXT3_FEATURE_COMPAT_HAS_JOURNAL; |
| 1397 | goto no_journal; |
| 1398 | } |
| 1399 | if (!quiet) { |
| 1400 | printf("Creating journal (%d blocks): ", |
| 1401 | journal_blocks); |
| 1402 | fflush(stdout); |
| 1403 | } |
| 1404 | retval = ext2fs_add_journal_inode(fs, journal_blocks, |
| 1405 | journal_flags); |
| 1406 | if (retval) { |
| 1407 | bb_error_msg_and_die("Could not create journal"); |
| 1408 | } |
| 1409 | if (!quiet) |
| 1410 | printf("done\n"); |
| 1411 | } |
| 1412 | no_journal: |
| 1413 | |
| 1414 | if (!quiet) |
| 1415 | printf("Writing superblocks and " |
| 1416 | "filesystem accounting information: "); |
| 1417 | retval = ext2fs_flush(fs); |
| 1418 | if (retval) { |
| 1419 | bb_error_msg("\nWarning, had trouble writing out superblocks"); |
| 1420 | } |
| 1421 | if (!quiet) { |
| 1422 | printf("done\n\n"); |
| 1423 | if (!getenv("MKE2FS_SKIP_CHECK_MSG")) |
| 1424 | print_check_message(fs); |
| 1425 | } |
| 1426 | val = ext2fs_close(fs); |
| 1427 | return (retval || val) ? 1 : 0; |
| 1428 | } |