]>
Commit | Line | Data |
---|---|---|
3839e657 TT |
1 | /* |
2 | * mke2fs.c - Make a ext2fs filesystem. | |
3 | * | |
19c78dc0 TT |
4 | * Copyright (C) 1994, 1995, 1996, 1997 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% | |
3839e657 TT |
10 | */ |
11 | ||
12 | /* Usage: mke2fs [options] device | |
13 | * | |
14 | * The device may be a block device or a image of one, but this isn't | |
15 | * enforced (but it's not much fun on a character device :-). | |
16 | */ | |
17 | ||
a418d3ad | 18 | #include <stdio.h> |
3839e657 TT |
19 | #include <string.h> |
20 | #include <fcntl.h> | |
21 | #include <ctype.h> | |
3839e657 | 22 | #include <time.h> |
2740156b TT |
23 | #ifdef linux |
24 | #include <sys/utsname.h> | |
25 | #endif | |
a418d3ad | 26 | #ifdef HAVE_GETOPT_H |
3839e657 | 27 | #include <getopt.h> |
373b8337 TT |
28 | #else |
29 | extern char *optarg; | |
30 | extern int optind; | |
a418d3ad TT |
31 | #endif |
32 | #ifdef HAVE_UNISTD_H | |
3839e657 | 33 | #include <unistd.h> |
a418d3ad TT |
34 | #endif |
35 | #ifdef HAVE_STDLIB_H | |
3839e657 | 36 | #include <stdlib.h> |
a418d3ad TT |
37 | #endif |
38 | #ifdef HAVE_ERRNO_H | |
39 | #include <errno.h> | |
40 | #endif | |
41 | #ifdef HAVE_MNTENT_H | |
3839e657 | 42 | #include <mntent.h> |
a418d3ad | 43 | #endif |
3839e657 | 44 | #include <sys/ioctl.h> |
f3db3566 TT |
45 | #include <sys/types.h> |
46 | ||
f3db3566 | 47 | #include <linux/ext2_fs.h> |
74becf3c TT |
48 | #ifdef HAVE_LINUX_MAJOR_H |
49 | #include <linux/major.h> | |
e6597048 | 50 | #include <sys/stat.h> /* Only need sys/stat.h for major nr test */ |
74becf3c | 51 | #endif |
3839e657 TT |
52 | |
53 | #include "et/com_err.h" | |
1e3472c5 | 54 | #include "uuid/uuid.h" |
896938d5 | 55 | #include "e2p/e2p.h" |
3839e657 TT |
56 | #include "ext2fs/ext2fs.h" |
57 | #include "../version.h" | |
d9c56d3c | 58 | #include "nls-enable.h" |
3839e657 | 59 | |
bbfa3aa9 TT |
60 | /* Everything is STDC, these days */ |
61 | #define NOARGS void | |
62 | ||
3839e657 TT |
63 | #define STRIDE_LENGTH 8 |
64 | ||
6733c2fd | 65 | #ifndef __sparc__ |
1e3472c5 TT |
66 | #define ZAP_BOOTBLOCK |
67 | #endif | |
68 | ||
3839e657 | 69 | extern int isatty(int); |
f3db3566 | 70 | extern FILE *fpopen(const char *cmd, const char *mode); |
3839e657 TT |
71 | |
72 | const char * program_name = "mke2fs"; | |
73 | const char * device_name = NULL; | |
74 | ||
75 | /* Command line options */ | |
76 | int cflag = 0; | |
77 | int verbose = 0; | |
78 | int quiet = 0; | |
f3db3566 | 79 | int super_only = 0; |
74becf3c | 80 | int force = 0; |
50787ea2 | 81 | int noaction = 0; |
85ef4ae8 | 82 | int journal_size = 0; |
3839e657 | 83 | char *bad_blocks_filename = 0; |
a29f4d30 | 84 | __u32 fs_stride = 0; |
3839e657 TT |
85 | |
86 | struct ext2_super_block param; | |
1e3472c5 TT |
87 | char *creator_os = NULL; |
88 | char *volume_label = NULL; | |
89 | char *mount_dir = NULL; | |
3839e657 | 90 | |
bbfa3aa9 TT |
91 | static void usage(NOARGS), check_plausibility(NOARGS), check_mount(NOARGS); |
92 | ||
3839e657 TT |
93 | static void usage(NOARGS) |
94 | { | |
d9c56d3c | 95 | fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] " |
1e3472c5 | 96 | "[-f fragment-size]\n\t[-i bytes-per-inode] " |
5515e6b4 TT |
97 | " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] " |
98 | "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] " | |
18160d26 TT |
99 | "[-M last-mounted-directory] [-O feature[,...]]\n\t" |
100 | "[-r fs-revision] [-R raid_opts] [-s sparse-super-flag]\n\t" | |
d9c56d3c | 101 | "[-qvSV] device [blocks-count]\n"), |
3839e657 TT |
102 | program_name); |
103 | exit(1); | |
104 | } | |
105 | ||
6733c2fd | 106 | static int int_log2(int arg) |
3839e657 TT |
107 | { |
108 | int l = 0; | |
109 | ||
110 | arg >>= 1; | |
111 | while (arg) { | |
112 | l++; | |
113 | arg >>= 1; | |
114 | } | |
115 | return l; | |
116 | } | |
117 | ||
6733c2fd | 118 | static int int_log10(unsigned int arg) |
1dde43f0 TT |
119 | { |
120 | int l; | |
121 | ||
122 | for (l=0; arg ; l++) | |
123 | arg = arg / 10; | |
124 | return l; | |
125 | } | |
126 | ||
a789d840 TT |
127 | static void proceed_question(NOARGS) |
128 | { | |
4ea0a110 | 129 | char buf[256]; |
d9c56d3c TT |
130 | char *short_yes = _("yY"); |
131 | ||
a789d840 TT |
132 | fflush(stdout); |
133 | fflush(stderr); | |
d9c56d3c | 134 | printf(_("Proceed anyway? (y,n) ")); |
4ea0a110 TT |
135 | buf[0] = 0; |
136 | fgets(buf, sizeof(buf), stdin); | |
137 | if (strchr(short_yes, buf[0]) == 0) | |
a789d840 TT |
138 | exit(1); |
139 | } | |
140 | ||
93ab9d78 TT |
141 | #ifndef SCSI_BLK_MAJOR |
142 | #define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR) | |
143 | #endif | |
144 | ||
74becf3c TT |
145 | static void check_plausibility(NOARGS) |
146 | { | |
147 | #ifdef HAVE_LINUX_MAJOR_H | |
b4ee1fb2 TT |
148 | #ifndef MAJOR |
149 | #define MAJOR(dev) ((dev)>>8) | |
150 | #define MINOR(dev) ((dev) & 0xff) | |
151 | #endif | |
152 | ||
74becf3c TT |
153 | int val; |
154 | struct stat s; | |
155 | ||
156 | val = stat(device_name, &s); | |
157 | ||
158 | if(val == -1) { | |
d9c56d3c | 159 | fprintf(stderr, _("Could not stat %s --- %s\n"), |
a789d840 | 160 | device_name, error_message(errno)); |
19c78dc0 | 161 | if (errno == ENOENT) |
d9c56d3c TT |
162 | fprintf(stderr, _("\nThe device apparently does " |
163 | "not exist; did you specify it correctly?\n")); | |
74becf3c TT |
164 | exit(1); |
165 | } | |
166 | if(!S_ISBLK(s.st_mode)) { | |
d9c56d3c | 167 | printf(_("%s is not a block special device.\n"), device_name); |
a789d840 | 168 | proceed_question(); |
74becf3c | 169 | return; |
a789d840 TT |
170 | } else if ((MAJOR(s.st_rdev) == HD_MAJOR && |
171 | MINOR(s.st_rdev)%64 == 0) || | |
1dde43f0 TT |
172 | (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) && |
173 | MINOR(s.st_rdev)%16 == 0)) { | |
d9c56d3c | 174 | printf(_("%s is entire device, not just one partition!\n"), |
74becf3c | 175 | device_name); |
a789d840 | 176 | proceed_question(); |
74becf3c TT |
177 | } |
178 | #endif | |
179 | } | |
180 | ||
3839e657 TT |
181 | static void check_mount(NOARGS) |
182 | { | |
a418d3ad TT |
183 | errcode_t retval; |
184 | int mount_flags; | |
3839e657 | 185 | |
a418d3ad TT |
186 | retval = ext2fs_check_if_mounted(device_name, &mount_flags); |
187 | if (retval) { | |
188 | com_err("ext2fs_check_if_mount", retval, | |
d9c56d3c | 189 | _("while determining whether %s is mounted."), |
a418d3ad | 190 | device_name); |
3839e657 | 191 | return; |
a418d3ad TT |
192 | } |
193 | if (!(mount_flags & EXT2_MF_MOUNTED)) | |
3839e657 | 194 | return; |
ca3c3285 | 195 | |
d9c56d3c | 196 | fprintf(stderr, _("%s is mounted; "), device_name); |
ca3c3285 | 197 | if (force) { |
d9c56d3c TT |
198 | fprintf(stderr, _("mke2fs forced anyway. " |
199 | "Hope /etc/mtab is incorrect.\n")); | |
ca3c3285 | 200 | } else { |
d9c56d3c | 201 | fprintf(stderr, _("will not make a filesystem here!\n")); |
ca3c3285 TT |
202 | exit(1); |
203 | } | |
3839e657 TT |
204 | } |
205 | ||
50787ea2 TT |
206 | /* |
207 | * This function sets the default parameters for a filesystem | |
208 | * | |
2740156b TT |
209 | * The type is specified by the user. The size is the maximum size |
210 | * (in megabytes) for which a set of parameters applies, with a size | |
211 | * of zero meaning that it is the default parameter for the type. | |
212 | * Note that order is important in the table below. | |
50787ea2 TT |
213 | */ |
214 | static char default_str[] = "default"; | |
215 | struct mke2fs_defaults { | |
4a600566 TT |
216 | const char *type; |
217 | int size; | |
218 | int blocksize; | |
219 | int inode_ratio; | |
50787ea2 TT |
220 | } settings[] = { |
221 | { default_str, 0, 4096, 8192 }, | |
222 | { default_str, 512, 1024, 4096 }, | |
223 | { default_str, 3, 1024, 8192 }, | |
224 | { "news", 0, 4096, 4096 }, | |
225 | { 0, 0, 0, 0}, | |
226 | }; | |
227 | ||
9094f284 | 228 | static void set_fs_defaults(char *fs_type, struct ext2fs_sb *super, |
50787ea2 TT |
229 | int blocksize, int *inode_ratio) |
230 | { | |
231 | int megs; | |
232 | int ratio = 0; | |
233 | struct mke2fs_defaults *p; | |
234 | ||
9094f284 | 235 | megs = (super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) / |
50787ea2 TT |
236 | 1024); |
237 | if (inode_ratio) | |
238 | ratio = *inode_ratio; | |
239 | if (!fs_type) | |
240 | fs_type = default_str; | |
241 | for (p = settings; p->type; p++) { | |
242 | if ((strcmp(p->type, fs_type) != 0) && | |
243 | (strcmp(p->type, default_str) != 0)) | |
244 | continue; | |
245 | if ((p->size != 0) && | |
246 | (megs > p->size)) | |
247 | continue; | |
248 | if (ratio == 0) | |
249 | *inode_ratio = p->inode_ratio; | |
250 | if (blocksize == 0) { | |
9094f284 | 251 | super->s_log_frag_size = super->s_log_block_size = |
6733c2fd | 252 | int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE); |
50787ea2 TT |
253 | } |
254 | } | |
255 | if (blocksize == 0) | |
9094f284 | 256 | super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024; |
50787ea2 TT |
257 | } |
258 | ||
3839e657 TT |
259 | /* |
260 | * Helper function for read_bb_file and test_disk | |
261 | */ | |
262 | static void invalid_block(ext2_filsys fs, blk_t blk) | |
263 | { | |
d9c56d3c | 264 | printf(_("Bad block %u out of range; ignored.\n"), blk); |
3839e657 TT |
265 | return; |
266 | } | |
267 | ||
268 | /* | |
269 | * Reads the bad blocks list from a file | |
270 | */ | |
271 | static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list, | |
272 | const char *bad_blocks_file) | |
273 | { | |
274 | FILE *f; | |
275 | errcode_t retval; | |
276 | ||
277 | f = fopen(bad_blocks_file, "r"); | |
278 | if (!f) { | |
279 | com_err("read_bad_blocks_file", errno, | |
d9c56d3c | 280 | _("while trying to open %s"), bad_blocks_file); |
3839e657 TT |
281 | exit(1); |
282 | } | |
283 | retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block); | |
284 | fclose (f); | |
285 | if (retval) { | |
286 | com_err("ext2fs_read_bb_FILE", retval, | |
d9c56d3c | 287 | _("while reading in list of bad blocks from file")); |
3839e657 TT |
288 | exit(1); |
289 | } | |
290 | } | |
291 | ||
292 | /* | |
293 | * Runs the badblocks program to test the disk | |
294 | */ | |
295 | static void test_disk(ext2_filsys fs, badblocks_list *bb_list) | |
296 | { | |
297 | FILE *f; | |
298 | errcode_t retval; | |
299 | char buf[1024]; | |
300 | ||
f635d7f6 TT |
301 | sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize, |
302 | quiet ? "" : "-s ", fs->device_name, | |
3839e657 TT |
303 | fs->super->s_blocks_count); |
304 | if (verbose) | |
d9c56d3c | 305 | printf(_("Running command: %s\n"), buf); |
3839e657 TT |
306 | f = popen(buf, "r"); |
307 | if (!f) { | |
308 | com_err("popen", errno, | |
d9c56d3c | 309 | _("while trying run '%s'"), buf); |
3839e657 TT |
310 | exit(1); |
311 | } | |
312 | retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block); | |
f3db3566 | 313 | pclose(f); |
3839e657 TT |
314 | if (retval) { |
315 | com_err("ext2fs_read_bb_FILE", retval, | |
d9c56d3c | 316 | _("while processing list of bad blocks from program")); |
3839e657 TT |
317 | exit(1); |
318 | } | |
319 | } | |
320 | ||
321 | static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list) | |
322 | { | |
f3db3566 | 323 | int i, j; |
3839e657 TT |
324 | int must_be_good; |
325 | blk_t blk; | |
326 | badblocks_iterate bb_iter; | |
327 | errcode_t retval; | |
f3db3566 TT |
328 | blk_t group_block; |
329 | int group; | |
330 | int group_bad; | |
3839e657 TT |
331 | |
332 | if (!bb_list) | |
333 | return; | |
334 | ||
335 | /* | |
336 | * The primary superblock and group descriptors *must* be | |
337 | * good; if not, abort. | |
338 | */ | |
339 | must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks; | |
340 | for (i = fs->super->s_first_data_block; i <= must_be_good; i++) { | |
341 | if (badblocks_list_test(bb_list, i)) { | |
d9c56d3c TT |
342 | fprintf(stderr, _("Block %d in primary " |
343 | "superblock/group descriptor area bad.\n"), i); | |
344 | fprintf(stderr, _("Blocks %d through %d must be good " | |
345 | "in order to build a filesystem.\n"), | |
3839e657 | 346 | fs->super->s_first_data_block, must_be_good); |
d9c56d3c | 347 | fprintf(stderr, _("Aborting....\n")); |
3839e657 TT |
348 | exit(1); |
349 | } | |
350 | } | |
f3db3566 TT |
351 | |
352 | /* | |
353 | * See if any of the bad blocks are showing up in the backup | |
354 | * superblocks and/or group descriptors. If so, issue a | |
355 | * warning and adjust the block counts appropriately. | |
356 | */ | |
357 | group_block = fs->super->s_first_data_block + | |
358 | fs->super->s_blocks_per_group; | |
f3db3566 TT |
359 | |
360 | for (i = 1; i < fs->group_desc_count; i++) { | |
92bcc595 | 361 | group_bad = 0; |
f3db3566 TT |
362 | for (j=0; j < fs->desc_blocks+1; j++) { |
363 | if (badblocks_list_test(bb_list, group_block + | |
364 | j)) { | |
365 | if (!group_bad) | |
366 | fprintf(stderr, | |
d9c56d3c TT |
367 | _("Warning: the backup superblock/group descriptors at block %d contain\n" |
368 | " bad blocks.\n\n"), | |
f3db3566 TT |
369 | group_block); |
370 | group_bad++; | |
371 | group = ext2fs_group_of_blk(fs, group_block+j); | |
372 | fs->group_desc[group].bg_free_blocks_count++; | |
373 | fs->super->s_free_blocks_count++; | |
374 | } | |
375 | } | |
376 | group_block += fs->super->s_blocks_per_group; | |
377 | } | |
3839e657 TT |
378 | |
379 | /* | |
380 | * Mark all the bad blocks as used... | |
381 | */ | |
382 | retval = badblocks_list_iterate_begin(bb_list, &bb_iter); | |
383 | if (retval) { | |
384 | com_err("badblocks_list_iterate_begin", retval, | |
d9c56d3c | 385 | _("while marking bad blocks as used")); |
3839e657 TT |
386 | exit(1); |
387 | } | |
388 | while (badblocks_list_iterate(bb_iter, &blk)) | |
f3db3566 | 389 | ext2fs_mark_block_bitmap(fs->block_map, blk); |
3839e657 TT |
390 | badblocks_list_iterate_end(bb_iter); |
391 | } | |
392 | ||
19c78dc0 | 393 | static void write_inode_tables(ext2_filsys fs) |
3839e657 TT |
394 | { |
395 | errcode_t retval; | |
396 | blk_t blk; | |
19c78dc0 TT |
397 | int i, j, num, count; |
398 | char *buf; | |
1dde43f0 | 399 | char format[20], backup[80]; |
7d5633cf TT |
400 | int sync_kludge = 0; |
401 | char *mke2fs_sync; | |
402 | ||
403 | mke2fs_sync = getenv("MKE2FS_SYNC"); | |
404 | if (mke2fs_sync) | |
405 | sync_kludge = atoi(mke2fs_sync); | |
3839e657 TT |
406 | |
407 | buf = malloc(fs->blocksize * STRIDE_LENGTH); | |
408 | if (!buf) { | |
d9c56d3c TT |
409 | com_err("malloc", ENOMEM, |
410 | _("while allocating zeroizing buffer")); | |
3839e657 TT |
411 | exit(1); |
412 | } | |
413 | memset(buf, 0, fs->blocksize * STRIDE_LENGTH); | |
1dde43f0 TT |
414 | |
415 | /* | |
416 | * Figure out how many digits we need | |
417 | */ | |
6733c2fd | 418 | i = int_log10(fs->group_desc_count); |
1dde43f0 TT |
419 | sprintf(format, "%%%dd/%%%dld", i, i); |
420 | memset(backup, '\b', sizeof(backup)-1); | |
421 | backup[sizeof(backup)-1] = 0; | |
422 | if ((2*i)+1 < sizeof(backup)) | |
423 | backup[(2*i)+1] = 0; | |
424 | ||
3839e657 | 425 | if (!quiet) |
d9c56d3c | 426 | printf(_("Writing inode tables: ")); |
3839e657 TT |
427 | for (i = 0; i < fs->group_desc_count; i++) { |
428 | if (!quiet) | |
1dde43f0 | 429 | printf(format, i, fs->group_desc_count); |
3839e657 | 430 | |
19c78dc0 TT |
431 | blk = fs->group_desc[i].bg_inode_table; |
432 | num = fs->inode_blocks_per_group; | |
433 | ||
434 | for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) { | |
435 | if (num-j > STRIDE_LENGTH) | |
436 | count = STRIDE_LENGTH; | |
437 | else | |
438 | count = num - j; | |
439 | retval = io_channel_write_blk(fs->io, blk, count, buf); | |
440 | if (retval) | |
d9c56d3c TT |
441 | printf(_("Warning: could not write %d blocks " |
442 | "in inode table starting at %d: %s\n"), | |
19c78dc0 TT |
443 | count, blk, error_message(retval)); |
444 | } | |
3839e657 | 445 | if (!quiet) |
1dde43f0 | 446 | fputs(backup, stdout); |
7d5633cf TT |
447 | if (sync_kludge) { |
448 | if (sync_kludge == 1) | |
449 | sync(); | |
450 | else if ((i % sync_kludge) == 0) | |
451 | sync(); | |
452 | } | |
3839e657 | 453 | } |
19c78dc0 | 454 | free(buf); |
3839e657 | 455 | if (!quiet) |
d9c56d3c | 456 | fputs(_("done \n"), stdout); |
3839e657 TT |
457 | } |
458 | ||
459 | static void create_root_dir(ext2_filsys fs) | |
460 | { | |
461 | errcode_t retval; | |
f3db3566 | 462 | struct ext2_inode inode; |
3839e657 TT |
463 | |
464 | retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0); | |
465 | if (retval) { | |
d9c56d3c | 466 | com_err("ext2fs_mkdir", retval, _("while creating root dir")); |
3839e657 TT |
467 | exit(1); |
468 | } | |
f3db3566 TT |
469 | if (geteuid()) { |
470 | retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode); | |
471 | if (retval) { | |
472 | com_err("ext2fs_read_inode", retval, | |
d9c56d3c | 473 | _("while reading root inode")); |
f3db3566 TT |
474 | exit(1); |
475 | } | |
19c78dc0 TT |
476 | inode.i_uid = getuid(); |
477 | if (inode.i_uid) | |
478 | inode.i_gid = getgid(); | |
f3db3566 TT |
479 | retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode); |
480 | if (retval) { | |
481 | com_err("ext2fs_write_inode", retval, | |
d9c56d3c | 482 | _("while setting root inode ownership")); |
f3db3566 TT |
483 | exit(1); |
484 | } | |
485 | } | |
3839e657 TT |
486 | } |
487 | ||
488 | static void create_lost_and_found(ext2_filsys fs) | |
489 | { | |
490 | errcode_t retval; | |
491 | ino_t ino; | |
492 | const char *name = "lost+found"; | |
493 | int i; | |
50787ea2 | 494 | int lpf_size = 0; |
3839e657 TT |
495 | |
496 | retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name); | |
497 | if (retval) { | |
d9c56d3c TT |
498 | com_err("ext2fs_mkdir", retval, |
499 | _("while creating /lost+found")); | |
3839e657 TT |
500 | exit(1); |
501 | } | |
502 | ||
503 | retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino); | |
504 | if (retval) { | |
d9c56d3c TT |
505 | com_err("ext2_lookup", retval, |
506 | _("while looking up /lost+found")); | |
3839e657 TT |
507 | exit(1); |
508 | } | |
509 | ||
510 | for (i=1; i < EXT2_NDIR_BLOCKS; i++) { | |
50787ea2 TT |
511 | if ((lpf_size += fs->blocksize) >= 16*1024) |
512 | break; | |
3839e657 TT |
513 | retval = ext2fs_expand_dir(fs, ino); |
514 | if (retval) { | |
515 | com_err("ext2fs_expand_dir", retval, | |
d9c56d3c | 516 | _("while expanding /lost+found")); |
3839e657 TT |
517 | exit(1); |
518 | } | |
519 | } | |
520 | } | |
521 | ||
522 | static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list) | |
523 | { | |
524 | errcode_t retval; | |
525 | ||
f3db3566 | 526 | ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO); |
3839e657 TT |
527 | fs->group_desc[0].bg_free_inodes_count--; |
528 | fs->super->s_free_inodes_count--; | |
529 | retval = ext2fs_update_bb_inode(fs, bb_list); | |
530 | if (retval) { | |
531 | com_err("ext2fs_update_bb_inode", retval, | |
d9c56d3c | 532 | _("while setting bad block inode")); |
3839e657 TT |
533 | exit(1); |
534 | } | |
535 | ||
536 | } | |
537 | ||
538 | static void reserve_inodes(ext2_filsys fs) | |
539 | { | |
540 | ino_t i; | |
541 | int group; | |
542 | ||
7f88b043 | 543 | for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) { |
f3db3566 | 544 | ext2fs_mark_inode_bitmap(fs->inode_map, i); |
3839e657 TT |
545 | group = ext2fs_group_of_ino(fs, i); |
546 | fs->group_desc[group].bg_free_inodes_count--; | |
547 | fs->super->s_free_inodes_count--; | |
548 | } | |
549 | ext2fs_mark_ib_dirty(fs); | |
550 | } | |
551 | ||
e41784eb | 552 | static void zap_sector(ext2_filsys fs, int sect) |
f3db3566 TT |
553 | { |
554 | char buf[512]; | |
555 | int retval; | |
556 | ||
557 | memset(buf, 0, 512); | |
558 | ||
e41784eb TT |
559 | io_channel_set_blksize(fs->io, 512); |
560 | retval = io_channel_write_blk(fs->io, sect, -512, buf); | |
561 | io_channel_set_blksize(fs->io, fs->blocksize); | |
f3db3566 | 562 | if (retval) |
e294cf2f | 563 | printf(_("Warning: could not erase sector %d: %s\n"), sect, |
f3db3566 TT |
564 | error_message(retval)); |
565 | } | |
566 | ||
567 | ||
3839e657 TT |
568 | static void show_stats(ext2_filsys fs) |
569 | { | |
1e3472c5 TT |
570 | struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super; |
571 | char buf[80]; | |
3839e657 | 572 | blk_t group_block; |
1dde43f0 | 573 | int i, need, col_left; |
3839e657 TT |
574 | |
575 | if (param.s_blocks_count != s->s_blocks_count) | |
d9c56d3c | 576 | printf(_("warning: %d blocks unused.\n\n"), |
3839e657 | 577 | param.s_blocks_count - s->s_blocks_count); |
50787ea2 TT |
578 | |
579 | memset(buf, 0, sizeof(buf)); | |
580 | strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name)); | |
d9c56d3c TT |
581 | printf(_("Filesystem label=%s\n"), buf); |
582 | printf(_("OS type: ")); | |
1e3472c5 TT |
583 | switch (fs->super->s_creator_os) { |
584 | case EXT2_OS_LINUX: printf ("Linux"); break; | |
ad6783df | 585 | case EXT2_OS_HURD: printf ("GNU/Hurd"); break; |
1e3472c5 | 586 | case EXT2_OS_MASIX: printf ("Masix"); break; |
d9c56d3c | 587 | default: printf (_("(unknown os)")); |
1e3472c5 | 588 | } |
50787ea2 | 589 | printf("\n"); |
d9c56d3c | 590 | printf(_("Block size=%u (log=%u)\n"), fs->blocksize, |
50787ea2 | 591 | s->s_log_block_size); |
d9c56d3c | 592 | printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize, |
50787ea2 | 593 | s->s_log_frag_size); |
d9c56d3c | 594 | printf(_("%u inodes, %u blocks\n"), s->s_inodes_count, |
3839e657 | 595 | s->s_blocks_count); |
d9c56d3c | 596 | printf(_("%u blocks (%2.2f%%) reserved for the super user\n"), |
3839e657 TT |
597 | s->s_r_blocks_count, |
598 | 100.0 * s->s_r_blocks_count / s->s_blocks_count); | |
d9c56d3c TT |
599 | printf(_("First data block=%u\n"), s->s_first_data_block); |
600 | if (fs->group_desc_count > 1) | |
601 | printf(_("%lu block groups\n"), fs->group_desc_count); | |
602 | else | |
603 | printf(_("%lu block group\n"), fs->group_desc_count); | |
604 | printf(_("%u blocks per group, %u fragments per group\n"), | |
3839e657 | 605 | s->s_blocks_per_group, s->s_frags_per_group); |
d9c56d3c | 606 | printf(_("%u inodes per group\n"), s->s_inodes_per_group); |
3839e657 TT |
607 | |
608 | if (fs->group_desc_count == 1) { | |
609 | printf("\n"); | |
610 | return; | |
611 | } | |
612 | ||
d9c56d3c | 613 | printf(_("Superblock backups stored on blocks: ")); |
3839e657 TT |
614 | group_block = s->s_first_data_block; |
615 | col_left = 0; | |
616 | for (i = 1; i < fs->group_desc_count; i++) { | |
617 | group_block += s->s_blocks_per_group; | |
521e3685 TT |
618 | if (!ext2fs_bg_has_super(fs, i)) |
619 | continue; | |
7671433a TT |
620 | if (i != 1) |
621 | printf(", "); | |
6733c2fd | 622 | need = int_log10(group_block) + 2; |
1dde43f0 | 623 | if (need > col_left) { |
3839e657 | 624 | printf("\n\t"); |
1dde43f0 | 625 | col_left = 72; |
3839e657 | 626 | } |
1dde43f0 | 627 | col_left -= need; |
a418d3ad | 628 | printf("%u", group_block); |
3839e657 TT |
629 | } |
630 | printf("\n\n"); | |
631 | } | |
632 | ||
1e3472c5 TT |
633 | #ifndef HAVE_STRCASECMP |
634 | static int strcasecmp (char *s1, char *s2) | |
635 | { | |
636 | while (*s1 && *s2) { | |
637 | int ch1 = *s1++, ch2 = *s2++; | |
638 | if (isupper (ch1)) | |
639 | ch1 = tolower (ch1); | |
640 | if (isupper (ch2)) | |
641 | ch2 = tolower (ch2); | |
642 | if (ch1 != ch2) | |
643 | return ch1 - ch2; | |
644 | } | |
645 | return *s1 ? 1 : *s2 ? -1 : 0; | |
646 | } | |
647 | #endif | |
648 | ||
649 | /* | |
650 | * Set the S_CREATOR_OS field. Return true if OS is known, | |
651 | * otherwise, 0. | |
652 | */ | |
653 | static int set_os(struct ext2_super_block *sb, char *os) | |
654 | { | |
655 | if (isdigit (*os)) | |
656 | sb->s_creator_os = atoi (os); | |
657 | else if (strcasecmp(os, "linux") == 0) | |
658 | sb->s_creator_os = EXT2_OS_LINUX; | |
659 | else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0) | |
660 | sb->s_creator_os = EXT2_OS_HURD; | |
661 | else if (strcasecmp(os, "masix") == 0) | |
662 | sb->s_creator_os = EXT2_OS_MASIX; | |
663 | else | |
664 | return 0; | |
665 | return 1; | |
666 | } | |
667 | ||
a418d3ad TT |
668 | #define PATH_SET "PATH=/sbin" |
669 | ||
d163b094 | 670 | static void parse_raid_opts(const char *opts) |
a29f4d30 TT |
671 | { |
672 | char *buf, *token, *next, *p, *arg; | |
673 | int len; | |
674 | int raid_usage = 0; | |
675 | ||
676 | len = strlen(opts); | |
677 | buf = malloc(len+1); | |
678 | if (!buf) { | |
d9c56d3c TT |
679 | fprintf(stderr, _("Couldn't allocate memory to parse " |
680 | "raid options!\n")); | |
a29f4d30 TT |
681 | exit(1); |
682 | } | |
683 | strcpy(buf, opts); | |
684 | for (token = buf; token && *token; token = next) { | |
685 | p = strchr(token, ','); | |
686 | next = 0; | |
687 | if (p) { | |
688 | *p = 0; | |
689 | next = p+1; | |
690 | } | |
691 | arg = strchr(token, '='); | |
692 | if (arg) { | |
693 | *arg = 0; | |
694 | arg++; | |
695 | } | |
696 | if (strcmp(token, "stride") == 0) { | |
697 | if (!arg) { | |
698 | raid_usage++; | |
699 | continue; | |
700 | } | |
701 | fs_stride = strtoul(arg, &p, 0); | |
702 | if (*p || (fs_stride == 0)) { | |
d9c56d3c TT |
703 | fprintf(stderr, |
704 | _("Invalid stride parameter.\n")); | |
a29f4d30 TT |
705 | raid_usage++; |
706 | continue; | |
707 | } | |
708 | } else | |
709 | raid_usage++; | |
710 | } | |
711 | if (raid_usage) { | |
d9c56d3c | 712 | fprintf(stderr, _("\nBad raid options specified.\n\n" |
a29f4d30 TT |
713 | "Raid options are separated by commas, " |
714 | "and may take an argument which\n" | |
715 | "\tis set off by an equals ('=') sign.\n\n" | |
716 | "Valid raid options are:\n" | |
d9c56d3c | 717 | "\tstride=<stride length in blocks>\n\n")); |
a29f4d30 TT |
718 | exit(1); |
719 | } | |
720 | } | |
721 | ||
896938d5 TT |
722 | static __u32 ok_features[3] = { |
723 | 0, /* Compat */ | |
724 | EXT2_FEATURE_INCOMPAT_FILETYPE, /* Incompat */ | |
725 | EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */ | |
726 | }; | |
a29f4d30 TT |
727 | |
728 | ||
3839e657 TT |
729 | static void PRS(int argc, char *argv[]) |
730 | { | |
4a600566 TT |
731 | int c; |
732 | int size; | |
733 | char * tmp; | |
80c22c90 | 734 | blk_t group_blk_max = 8192; |
4a600566 TT |
735 | int blocksize = 0; |
736 | int inode_ratio = 0; | |
737 | int reserved_ratio = 5; | |
738 | ino_t num_inodes = 0; | |
a418d3ad | 739 | errcode_t retval; |
f6f65832 | 740 | int sparse_option = 1; |
4a600566 | 741 | char * oldpath = getenv("PATH"); |
521e3685 | 742 | struct ext2fs_sb *param_ext2 = (struct ext2fs_sb *) ¶m; |
4a600566 TT |
743 | char * raid_opts = 0; |
744 | char * fs_type = 0; | |
f6f65832 | 745 | const char * feature_set = "filetype"; |
4a600566 | 746 | blk_t dev_size; |
2740156b | 747 | #ifdef linux |
4a600566 | 748 | struct utsname ut; |
2740156b TT |
749 | |
750 | if (uname(&ut)) { | |
751 | perror("uname"); | |
752 | exit(1); | |
753 | } | |
896938d5 TT |
754 | if ((ut.release[0] == '1') || |
755 | (ut.release[0] == '2' && ut.release[1] == '.' && | |
756 | ut.release[2] < '2' && ut.release[3] == '.')) | |
80c22c90 | 757 | feature_set = NULL; |
2740156b | 758 | #endif |
3839e657 | 759 | /* Update our PATH to include /sbin */ |
a418d3ad TT |
760 | if (oldpath) { |
761 | char *newpath; | |
762 | ||
763 | newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath)); | |
764 | strcpy (newpath, PATH_SET); | |
765 | strcat (newpath, ":"); | |
766 | strcat (newpath, oldpath); | |
767 | putenv (newpath); | |
768 | } else | |
769 | putenv (PATH_SET); | |
3839e657 TT |
770 | |
771 | setbuf(stdout, NULL); | |
772 | setbuf(stderr, NULL); | |
773 | initialize_ext2_error_table(); | |
774 | memset(¶m, 0, sizeof(struct ext2_super_block)); | |
50787ea2 | 775 | param.s_rev_level = 1; /* Create revision 1 filesystems now */ |
3839e657 | 776 | |
d9c56d3c | 777 | fprintf (stderr, _("mke2fs %s, %s for EXT2 FS %s, %s\n"), |
3839e657 TT |
778 | E2FSPROGS_VERSION, E2FSPROGS_DATE, |
779 | EXT2FS_VERSION, EXT2FS_DATE); | |
780 | if (argc && *argv) | |
781 | program_name = *argv; | |
1e3472c5 | 782 | while ((c = getopt (argc, argv, |
85ef4ae8 | 783 | "b:cf:g:i:j:l:m:no:qr:R:s:tvI:ST:FL:M:N:O:V")) != EOF) |
3839e657 TT |
784 | switch (c) { |
785 | case 'b': | |
50787ea2 TT |
786 | blocksize = strtoul(optarg, &tmp, 0); |
787 | if (blocksize < 1024 || blocksize > 4096 || *tmp) { | |
d9c56d3c TT |
788 | com_err(program_name, 0, |
789 | _("bad block size - %s"), optarg); | |
3839e657 TT |
790 | exit(1); |
791 | } | |
792 | param.s_log_block_size = | |
6733c2fd | 793 | int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE); |
80c22c90 | 794 | group_blk_max = blocksize * 8; |
3839e657 TT |
795 | break; |
796 | case 'c': | |
797 | case 't': /* Check for bad blocks */ | |
798 | cflag = 1; | |
799 | break; | |
800 | case 'f': | |
801 | size = strtoul(optarg, &tmp, 0); | |
802 | if (size < 1024 || size > 4096 || *tmp) { | |
d9c56d3c TT |
803 | com_err(program_name, 0, |
804 | _("bad fragment size - %s"), | |
3839e657 TT |
805 | optarg); |
806 | exit(1); | |
807 | } | |
808 | param.s_log_frag_size = | |
6733c2fd | 809 | int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE); |
d9c56d3c TT |
810 | printf(_("Warning: fragments not supported. " |
811 | "Ignoring -f option\n")); | |
3839e657 TT |
812 | break; |
813 | case 'g': | |
814 | param.s_blocks_per_group = strtoul(optarg, &tmp, 0); | |
f3db3566 TT |
815 | if (*tmp) { |
816 | com_err(program_name, 0, | |
d9c56d3c | 817 | _("Illegal number for blocks per group")); |
f3db3566 TT |
818 | exit(1); |
819 | } | |
f3db3566 TT |
820 | if ((param.s_blocks_per_group % 8) != 0) { |
821 | com_err(program_name, 0, | |
d9c56d3c | 822 | _("blocks per group must be multiple of 8")); |
3839e657 TT |
823 | exit(1); |
824 | } | |
825 | break; | |
826 | case 'i': | |
827 | inode_ratio = strtoul(optarg, &tmp, 0); | |
828 | if (inode_ratio < 1024 || inode_ratio > 256 * 1024 || | |
829 | *tmp) { | |
d9c56d3c TT |
830 | com_err(program_name, 0, |
831 | _("bad inode ratio - %s"), optarg); | |
3839e657 TT |
832 | exit(1); |
833 | } | |
834 | break; | |
85ef4ae8 TT |
835 | case 'j': |
836 | journal_size = strtoul(optarg, &tmp, 0); | |
837 | if (journal_size < 4 || journal_size > 100 || *tmp) { | |
838 | com_err(program_name, 0, | |
839 | _("bad journal size - %s"), optarg); | |
840 | exit(1); | |
841 | } | |
842 | break; | |
3839e657 | 843 | case 'l': |
f3db3566 TT |
844 | bad_blocks_filename = malloc(strlen(optarg)+1); |
845 | if (!bad_blocks_filename) { | |
846 | com_err(program_name, ENOMEM, | |
d9c56d3c | 847 | _("in malloc for bad_blocks_filename")); |
f3db3566 TT |
848 | exit(1); |
849 | } | |
850 | strcpy(bad_blocks_filename, optarg); | |
3839e657 TT |
851 | break; |
852 | case 'm': | |
853 | reserved_ratio = strtoul(optarg, &tmp, 0); | |
854 | if (reserved_ratio > 50 || *tmp) { | |
855 | com_err(program_name, 0, | |
d9c56d3c | 856 | _("bad reserved blocks percent - %s"), |
3839e657 TT |
857 | optarg); |
858 | exit(1); | |
859 | } | |
860 | break; | |
50787ea2 TT |
861 | case 'n': |
862 | noaction++; | |
863 | break; | |
1e3472c5 TT |
864 | case 'o': |
865 | creator_os = optarg; | |
866 | break; | |
7f88b043 TT |
867 | case 'r': |
868 | param.s_rev_level = atoi(optarg); | |
869 | break; | |
521e3685 TT |
870 | case 's': |
871 | sparse_option = atoi(optarg); | |
872 | break; | |
7f88b043 TT |
873 | #ifdef EXT2_DYNAMIC_REV |
874 | case 'I': | |
875 | param.s_inode_size = atoi(optarg); | |
876 | break; | |
877 | #endif | |
5515e6b4 TT |
878 | case 'N': |
879 | num_inodes = atoi(optarg); | |
880 | break; | |
3839e657 TT |
881 | case 'v': |
882 | verbose = 1; | |
883 | break; | |
884 | case 'q': | |
885 | quiet = 1; | |
886 | break; | |
74becf3c TT |
887 | case 'F': |
888 | force = 1; | |
889 | break; | |
1e3472c5 TT |
890 | case 'L': |
891 | volume_label = optarg; | |
892 | break; | |
893 | case 'M': | |
894 | mount_dir = optarg; | |
895 | break; | |
896938d5 TT |
896 | case 'O': |
897 | feature_set = optarg; | |
898 | break; | |
a29f4d30 TT |
899 | case 'R': |
900 | raid_opts = optarg; | |
901 | break; | |
f3db3566 TT |
902 | case 'S': |
903 | super_only = 1; | |
904 | break; | |
50787ea2 TT |
905 | case 'T': |
906 | fs_type = optarg; | |
907 | break; | |
818180cd TT |
908 | case 'V': |
909 | /* Print version number and exit */ | |
d9c56d3c | 910 | fprintf(stderr, _("\tUsing %s\n"), |
818180cd TT |
911 | error_message(EXT2_ET_BASE)); |
912 | exit(0); | |
3839e657 TT |
913 | default: |
914 | usage(); | |
915 | } | |
916 | if (optind == argc) | |
917 | usage(); | |
918 | device_name = argv[optind]; | |
919 | optind++; | |
920 | if (optind < argc) { | |
e1a0a3e3 TT |
921 | unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0); |
922 | ||
923 | if ((*tmp) || (tmp2 > 0xfffffffful)) { | |
d9c56d3c | 924 | com_err(program_name, 0, _("bad blocks count - %s"), |
3839e657 TT |
925 | argv[optind - 1]); |
926 | exit(1); | |
927 | } | |
e1a0a3e3 | 928 | param.s_blocks_count = tmp2; |
3839e657 TT |
929 | } |
930 | if (optind < argc) | |
931 | usage(); | |
a418d3ad | 932 | |
a29f4d30 TT |
933 | if (raid_opts) |
934 | parse_raid_opts(raid_opts); | |
935 | ||
74becf3c TT |
936 | if (!force) |
937 | check_plausibility(); | |
a418d3ad TT |
938 | check_mount(); |
939 | ||
3839e657 TT |
940 | param.s_log_frag_size = param.s_log_block_size; |
941 | ||
50787ea2 TT |
942 | if (noaction && param.s_blocks_count) { |
943 | dev_size = param.s_blocks_count; | |
944 | retval = 0; | |
945 | } else | |
946 | retval = ext2fs_get_device_size(device_name, | |
947 | EXT2_BLOCK_SIZE(¶m), | |
948 | &dev_size); | |
a789d840 TT |
949 | if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) { |
950 | com_err(program_name, retval, | |
d9c56d3c | 951 | _("while trying to determine filesystem size")); |
a789d840 TT |
952 | exit(1); |
953 | } | |
a418d3ad | 954 | if (!param.s_blocks_count) { |
a789d840 TT |
955 | if (retval == EXT2_ET_UNIMPLEMENTED) { |
956 | com_err(program_name, 0, | |
d9c56d3c | 957 | _("Couldn't determine device size; you " |
a789d840 | 958 | "must specify\nthe size of the " |
d9c56d3c | 959 | "filesystem\n")); |
a418d3ad | 960 | exit(1); |
26ab5315 TT |
961 | } else { |
962 | if (dev_size == 0) { | |
963 | com_err(program_name, 0, | |
964 | _("Device size reported to be zero. " | |
965 | "Invalid partition specified, or\n\t" | |
966 | "partition table wasn't reread " | |
967 | "after running fdisk, due to\n\t" | |
968 | "a modified partition being busy " | |
969 | "and in use. You may need to reboot\n\t" | |
970 | "to re-read your partition table.\n" | |
971 | )); | |
972 | exit(1); | |
973 | } | |
a789d840 | 974 | param.s_blocks_count = dev_size; |
26ab5315 TT |
975 | } |
976 | ||
a789d840 TT |
977 | } else if (!force && (param.s_blocks_count > dev_size)) { |
978 | com_err(program_name, 0, | |
d9c56d3c | 979 | _("Filesystem larger than apparent filesystem size.")); |
a789d840 | 980 | proceed_question(); |
a418d3ad | 981 | } |
3839e657 | 982 | |
50787ea2 TT |
983 | set_fs_defaults(fs_type, param_ext2, blocksize, &inode_ratio); |
984 | ||
521e3685 TT |
985 | if (param.s_blocks_per_group) { |
986 | if (param.s_blocks_per_group < 256 || | |
80c22c90 | 987 | param.s_blocks_per_group > group_blk_max || *tmp) { |
521e3685 | 988 | com_err(program_name, 0, |
d9c56d3c | 989 | _("blocks per group count out of range")); |
521e3685 TT |
990 | exit(1); |
991 | } | |
992 | } | |
993 | ||
3839e657 TT |
994 | /* |
995 | * Calculate number of inodes based on the inode ratio | |
996 | */ | |
5515e6b4 | 997 | param.s_inodes_count = num_inodes ? num_inodes : |
e6597048 | 998 | ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(¶m)) |
f3db3566 | 999 | / inode_ratio; |
3839e657 TT |
1000 | |
1001 | /* | |
1002 | * Calculate number of blocks to reserve | |
1003 | */ | |
1004 | param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100; | |
521e3685 | 1005 | |
f6f65832 TT |
1006 | /* Turn off features not supported by the earlier filesystem version */ |
1007 | if (param.s_rev_level == 0) { | |
1008 | sparse_option = 0; | |
1009 | feature_set = NULL; | |
1010 | } | |
2740156b | 1011 | if (sparse_option) |
521e3685 TT |
1012 | param_ext2->s_feature_ro_compat |= |
1013 | EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; | |
f6f65832 | 1014 | |
896938d5 | 1015 | if (feature_set && !strncasecmp(feature_set, "none", 4)) |
80c22c90 | 1016 | feature_set = NULL; |
896938d5 TT |
1017 | if (feature_set && e2p_edit_feature(feature_set, |
1018 | ¶m_ext2->s_feature_compat, | |
1019 | ok_features)) { | |
d9c56d3c | 1020 | fprintf(stderr, _("Invalid filesystem option set: %s\n"), |
896938d5 TT |
1021 | feature_set); |
1022 | exit(1); | |
1023 | } | |
3839e657 TT |
1024 | } |
1025 | ||
1026 | int main (int argc, char *argv[]) | |
1027 | { | |
1028 | errcode_t retval = 0; | |
1029 | ext2_filsys fs; | |
1030 | badblocks_list bb_list = 0; | |
85ef4ae8 | 1031 | int journal_blocks; |
1e3472c5 | 1032 | struct ext2fs_sb *s; |
d9c56d3c TT |
1033 | |
1034 | #ifdef ENABLE_NLS | |
1035 | setlocale(LC_MESSAGES, ""); | |
1036 | bindtextdomain(NLS_CAT_NAME, LOCALEDIR); | |
1037 | textdomain(NLS_CAT_NAME); | |
1038 | #endif | |
3839e657 TT |
1039 | PRS(argc, argv); |
1040 | ||
3839e657 TT |
1041 | /* |
1042 | * Initialize the superblock.... | |
1043 | */ | |
1044 | retval = ext2fs_initialize(device_name, 0, ¶m, | |
1045 | unix_io_manager, &fs); | |
1046 | if (retval) { | |
d9c56d3c | 1047 | com_err(device_name, retval, _("while setting up superblock")); |
3839e657 TT |
1048 | exit(1); |
1049 | } | |
1050 | ||
e41784eb TT |
1051 | /* |
1052 | * Wipe out the old on-disk superblock | |
1053 | */ | |
1054 | zap_sector(fs, 2); | |
1055 | ||
1e3472c5 TT |
1056 | /* |
1057 | * Generate a UUID for it... | |
1058 | */ | |
1059 | s = (struct ext2fs_sb *) fs->super; | |
1060 | uuid_generate(s->s_uuid); | |
1061 | ||
1062 | /* | |
1063 | * Override the creator OS, if applicable | |
1064 | */ | |
1065 | if (creator_os && !set_os(fs->super, creator_os)) { | |
d9c56d3c | 1066 | com_err (program_name, 0, _("unknown os - %s"), creator_os); |
1e3472c5 TT |
1067 | exit(1); |
1068 | } | |
1069 | ||
4ea0a110 TT |
1070 | /* |
1071 | * For the Hurd, we will turn off filetype since it doesn't | |
1072 | * support it. | |
1073 | */ | |
1074 | if (fs->super->s_creator_os == EXT2_OS_HURD) | |
1075 | fs->super->s_feature_incompat &= | |
1076 | ~EXT2_FEATURE_INCOMPAT_FILETYPE; | |
1077 | ||
1e3472c5 TT |
1078 | /* |
1079 | * Set the volume label... | |
1080 | */ | |
1081 | if (volume_label) { | |
1082 | memset(s->s_volume_name, 0, sizeof(s->s_volume_name)); | |
1083 | strncpy(s->s_volume_name, volume_label, | |
1084 | sizeof(s->s_volume_name)); | |
1085 | } | |
1086 | ||
1087 | /* | |
1088 | * Set the last mount directory | |
1089 | */ | |
1090 | if (mount_dir) { | |
1091 | memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted)); | |
1092 | strncpy(s->s_last_mounted, mount_dir, | |
1093 | sizeof(s->s_last_mounted)); | |
1094 | } | |
1095 | ||
50787ea2 | 1096 | if (!quiet || noaction) |
3839e657 TT |
1097 | show_stats(fs); |
1098 | ||
50787ea2 TT |
1099 | if (noaction) |
1100 | exit(0); | |
1101 | ||
3839e657 TT |
1102 | if (bad_blocks_filename) |
1103 | read_bb_file(fs, &bb_list, bad_blocks_filename); | |
1104 | if (cflag) | |
1105 | test_disk(fs, &bb_list); | |
1106 | ||
1107 | handle_bad_blocks(fs, bb_list); | |
a29f4d30 | 1108 | fs->stride = fs_stride; |
19c78dc0 TT |
1109 | retval = ext2fs_allocate_tables(fs); |
1110 | if (retval) { | |
1111 | com_err(program_name, retval, | |
d9c56d3c | 1112 | _("while trying to allocate filesystem tables")); |
19c78dc0 TT |
1113 | exit(1); |
1114 | } | |
f3db3566 TT |
1115 | if (super_only) { |
1116 | fs->super->s_state |= EXT2_ERROR_FS; | |
1117 | fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY); | |
1118 | } else { | |
19c78dc0 | 1119 | write_inode_tables(fs); |
f3db3566 TT |
1120 | create_root_dir(fs); |
1121 | create_lost_and_found(fs); | |
1122 | reserve_inodes(fs); | |
1123 | create_bad_block_inode(fs, bb_list); | |
1e3472c5 | 1124 | #ifdef ZAP_BOOTBLOCK |
e41784eb | 1125 | zap_sector(fs, 0); |
1e3472c5 | 1126 | #endif |
f3db3566 | 1127 | } |
85ef4ae8 TT |
1128 | if (journal_size) { |
1129 | if (!quiet) | |
1130 | printf(_("Creating journal: ")); | |
1131 | journal_blocks = journal_size * 1024 / | |
1132 | (fs->blocksize / 1024); | |
1133 | retval = ext2fs_add_journal_fs(fs, journal_blocks); | |
1134 | if (retval) { | |
1135 | com_err (program_name, retval, | |
1136 | _("while trying to create journal")); | |
1137 | exit(1); | |
1138 | } | |
1139 | if (!quiet) | |
1140 | printf(_("done\n")); | |
1141 | } | |
3839e657 TT |
1142 | |
1143 | if (!quiet) | |
d9c56d3c TT |
1144 | printf(_("Writing superblocks and " |
1145 | "filesystem accounting information: ")); | |
5d45d803 TT |
1146 | retval = ext2fs_flush(fs); |
1147 | if (retval) { | |
d9c56d3c | 1148 | printf(_("\nWarning, had trouble writing out superblocks.")); |
5d45d803 | 1149 | } |
3839e657 | 1150 | if (!quiet) |
d9c56d3c | 1151 | printf(_("done\n")); |
5d45d803 | 1152 | ext2fs_close(fs); |
3839e657 TT |
1153 | return 0; |
1154 | } |