blob: d5529936cff62ee740ea08bd2dc52245945ca139 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * mke2fs.c - Make a ext2fs filesystem.
3 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * 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%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 */
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
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000018#include <stdio.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000019#include <string.h>
20#include <fcntl.h>
21#include <ctype.h>
22#include <termios.h>
23#include <time.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000024#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000025#include <getopt.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000026#endif
27#ifdef HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000028#include <unistd.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000029#endif
30#ifdef HAVE_STDLIB_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000031#include <stdlib.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000032#endif
33#ifdef HAVE_ERRNO_H
34#include <errno.h>
35#endif
36#ifdef HAVE_MNTENT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000037#include <mntent.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000038#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000039#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000040#include <sys/types.h>
Theodore Ts'o74becf31997-04-26 14:37:06 +000041#include <sys/stat.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000042
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000043#ifdef HAVE_LINUX_FS_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000044#include <linux/fs.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000045#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000046#include <linux/ext2_fs.h>
Theodore Ts'o74becf31997-04-26 14:37:06 +000047#ifdef HAVE_LINUX_MAJOR_H
48#include <linux/major.h>
49#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000050
51#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000052#include "uuid/uuid.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000053#include "ext2fs/ext2fs.h"
54#include "../version.h"
55
Theodore Ts'obbfa3aa1998-03-21 07:12:46 +000056/* Everything is STDC, these days */
57#define NOARGS void
58
Theodore Ts'o3839e651997-04-26 13:21:57 +000059#define STRIDE_LENGTH 8
60
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000061#ifndef sparc
62#define ZAP_BOOTBLOCK
63#endif
64
Theodore Ts'o3839e651997-04-26 13:21:57 +000065extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000066extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000067
68const char * program_name = "mke2fs";
69const char * device_name = NULL;
70
71/* Command line options */
72int cflag = 0;
73int verbose = 0;
74int quiet = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +000075int super_only = 0;
Theodore Ts'o74becf31997-04-26 14:37:06 +000076int force = 0;
Theodore Ts'o50787ea1999-07-19 15:30:21 +000077int noaction = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000078char *bad_blocks_filename = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000079__u32 fs_stride = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000080
81struct ext2_super_block param;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000082char *creator_os = NULL;
83char *volume_label = NULL;
84char *mount_dir = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000085
Theodore Ts'obbfa3aa1998-03-21 07:12:46 +000086static void usage(NOARGS), check_plausibility(NOARGS), check_mount(NOARGS);
87
Theodore Ts'o3839e651997-04-26 13:21:57 +000088static void usage(NOARGS)
89{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000090 fprintf(stderr, "Usage: %s [-c|-t|-l filename] [-b block-size] "
91 "[-f fragment-size]\n\t[-i bytes-per-inode] "
Theodore Ts'o5515e6b1999-01-05 07:25:06 +000092 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
93 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
94 "[-M last-mounted-directory] [-r fs-revision]\n\t[-R raid_opts]"
95 "[-s sparse-super-flag] [-qvSV] device [blocks-count]\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +000096 program_name);
97 exit(1);
98}
99
100static int log2(int arg)
101{
102 int l = 0;
103
104 arg >>= 1;
105 while (arg) {
106 l++;
107 arg >>= 1;
108 }
109 return l;
110}
111
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000112static int log10(unsigned int arg)
113{
114 int l;
115
116 for (l=0; arg ; l++)
117 arg = arg / 10;
118 return l;
119}
120
Theodore Ts'oa789d841998-03-30 01:20:55 +0000121static void proceed_question(NOARGS)
122{
123 fflush(stdout);
124 fflush(stderr);
125 printf("Proceed anyway? (y,n) ");
126 if (getchar() != 'y')
127 exit(1);
128}
129
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000130#ifndef SCSI_BLK_MAJOR
131#define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
132#endif
133
Theodore Ts'o74becf31997-04-26 14:37:06 +0000134static void check_plausibility(NOARGS)
135{
136#ifdef HAVE_LINUX_MAJOR_H
137 int val;
138 struct stat s;
139
140 val = stat(device_name, &s);
141
142 if(val == -1) {
Theodore Ts'oa789d841998-03-30 01:20:55 +0000143 fprintf(stderr, "Could not stat %s --- %s\n",
144 device_name, error_message(errno));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000145 if (errno == ENOENT)
Theodore Ts'oa789d841998-03-30 01:20:55 +0000146 fprintf(stderr, "\nThe device apparently does "
147 "not exist; did you specify it correctly?\n");
Theodore Ts'o74becf31997-04-26 14:37:06 +0000148 exit(1);
149 }
150 if(!S_ISBLK(s.st_mode)) {
151 printf("%s is not a block special device.\n", device_name);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000152 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000153 return;
Theodore Ts'oa789d841998-03-30 01:20:55 +0000154 } else if ((MAJOR(s.st_rdev) == HD_MAJOR &&
155 MINOR(s.st_rdev)%64 == 0) ||
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000156 (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
157 MINOR(s.st_rdev)%16 == 0)) {
Theodore Ts'o74becf31997-04-26 14:37:06 +0000158 printf("%s is entire device, not just one partition!\n",
159 device_name);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000160 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000161 }
162#endif
163}
164
Theodore Ts'o3839e651997-04-26 13:21:57 +0000165static void check_mount(NOARGS)
166{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000167 errcode_t retval;
168 int mount_flags;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000169
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000170 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
171 if (retval) {
172 com_err("ext2fs_check_if_mount", retval,
173 "while determining whether %s is mounted.",
174 device_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000175 return;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000176 }
177 if (!(mount_flags & EXT2_MF_MOUNTED))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000178 return;
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000179
180 fprintf(stderr, "%s is mounted; ", device_name);
181 if (force) {
182 fprintf(stderr, "mke2fs forced anyway. "
183 "Hope /etc/mtab is incorrect.\n");
184 } else {
185 fprintf(stderr, "will not make a filesystem here!\n");
186 exit(1);
187 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000188}
189
190/*
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000191 * This function sets the default parameters for a filesystem
192 *
193 * The type is specified by the user. The size is minimum size for
194 * which a set of parameters applies, with a size of zero meaning that
195 * it is the default parameter for the type. Note that order is
196 * important in the table below.
197 */
198static char default_str[] = "default";
199struct mke2fs_defaults {
200 char *type;
201 int size;
202 int blocksize;
203 int inode_ratio;
204} settings[] = {
205 { default_str, 0, 4096, 8192 },
206 { default_str, 512, 1024, 4096 },
207 { default_str, 3, 1024, 8192 },
208 { "news", 0, 4096, 4096 },
209 { 0, 0, 0, 0},
210};
211
212static void set_fs_defaults(char *fs_type, struct ext2fs_sb *param,
213 int blocksize, int *inode_ratio)
214{
215 int megs;
216 int ratio = 0;
217 struct mke2fs_defaults *p;
218
219 megs = (param->s_blocks_count * (EXT2_BLOCK_SIZE(param) / 1024) /
220 1024);
221 if (inode_ratio)
222 ratio = *inode_ratio;
223 if (!fs_type)
224 fs_type = default_str;
225 for (p = settings; p->type; p++) {
226 if ((strcmp(p->type, fs_type) != 0) &&
227 (strcmp(p->type, default_str) != 0))
228 continue;
229 if ((p->size != 0) &&
230 (megs > p->size))
231 continue;
232 if (ratio == 0)
233 *inode_ratio = p->inode_ratio;
234 if (blocksize == 0) {
235 param->s_log_frag_size = param->s_log_block_size =
236 log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
237 }
238 }
239 if (blocksize == 0)
240 param->s_blocks_count /= EXT2_BLOCK_SIZE(param) / 1024;
241}
242
243/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000244 * Helper function for read_bb_file and test_disk
245 */
246static void invalid_block(ext2_filsys fs, blk_t blk)
247{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000248 printf("Bad block %u out of range; ignored.\n", blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000249 return;
250}
251
252/*
253 * Reads the bad blocks list from a file
254 */
255static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
256 const char *bad_blocks_file)
257{
258 FILE *f;
259 errcode_t retval;
260
261 f = fopen(bad_blocks_file, "r");
262 if (!f) {
263 com_err("read_bad_blocks_file", errno,
264 "while trying to open %s", bad_blocks_file);
265 exit(1);
266 }
267 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
268 fclose (f);
269 if (retval) {
270 com_err("ext2fs_read_bb_FILE", retval,
271 "while reading in list of bad blocks from file");
272 exit(1);
273 }
274}
275
276/*
277 * Runs the badblocks program to test the disk
278 */
279static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
280{
281 FILE *f;
282 errcode_t retval;
283 char buf[1024];
284
Theodore Ts'of635d7f1997-05-09 02:50:16 +0000285 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
286 quiet ? "" : "-s ", fs->device_name,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000287 fs->super->s_blocks_count);
288 if (verbose)
289 printf("Running command: %s\n", buf);
290 f = popen(buf, "r");
291 if (!f) {
292 com_err("popen", errno,
293 "while trying run '%s'", buf);
294 exit(1);
295 }
296 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000297 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000298 if (retval) {
299 com_err("ext2fs_read_bb_FILE", retval,
300 "while processing list of bad blocks from program");
301 exit(1);
302 }
303}
304
305static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
306{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000307 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000308 int must_be_good;
309 blk_t blk;
310 badblocks_iterate bb_iter;
311 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000312 blk_t group_block;
313 int group;
314 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000315
316 if (!bb_list)
317 return;
318
319 /*
320 * The primary superblock and group descriptors *must* be
321 * good; if not, abort.
322 */
323 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
324 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
325 if (badblocks_list_test(bb_list, i)) {
326 fprintf(stderr, "Block %d in primary superblock/group "
327 "descriptor area bad.\n", i);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000328 fprintf(stderr, "Blocks %d through %d must be good "
Theodore Ts'o3839e651997-04-26 13:21:57 +0000329 "in order to build a filesystem.\n",
330 fs->super->s_first_data_block, must_be_good);
331 fprintf(stderr, "Aborting....\n");
332 exit(1);
333 }
334 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000335
336 /*
337 * See if any of the bad blocks are showing up in the backup
338 * superblocks and/or group descriptors. If so, issue a
339 * warning and adjust the block counts appropriately.
340 */
341 group_block = fs->super->s_first_data_block +
342 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000343
344 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000345 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000346 for (j=0; j < fs->desc_blocks+1; j++) {
347 if (badblocks_list_test(bb_list, group_block +
348 j)) {
349 if (!group_bad)
350 fprintf(stderr,
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000351"Warning: the backup superblock/group descriptors at block %d contain\n"
Theodore Ts'of3db3561997-04-26 13:34:30 +0000352" bad blocks.\n\n",
353 group_block);
354 group_bad++;
355 group = ext2fs_group_of_blk(fs, group_block+j);
356 fs->group_desc[group].bg_free_blocks_count++;
357 fs->super->s_free_blocks_count++;
358 }
359 }
360 group_block += fs->super->s_blocks_per_group;
361 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000362
363 /*
364 * Mark all the bad blocks as used...
365 */
366 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
367 if (retval) {
368 com_err("badblocks_list_iterate_begin", retval,
369 "while marking bad blocks as used");
370 exit(1);
371 }
372 while (badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000373 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000374 badblocks_list_iterate_end(bb_iter);
375}
376
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000377static void write_inode_tables(ext2_filsys fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000378{
379 errcode_t retval;
380 blk_t blk;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000381 int i, j, num, count;
382 char *buf;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000383 char format[20], backup[80];
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000384 int sync_kludge = 0;
385 char *mke2fs_sync;
386
387 mke2fs_sync = getenv("MKE2FS_SYNC");
388 if (mke2fs_sync)
389 sync_kludge = atoi(mke2fs_sync);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000390
391 buf = malloc(fs->blocksize * STRIDE_LENGTH);
392 if (!buf) {
393 com_err("malloc", ENOMEM, "while allocating zeroizing buffer");
394 exit(1);
395 }
396 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000397
398 /*
399 * Figure out how many digits we need
400 */
401 i = log10(fs->group_desc_count);
402 sprintf(format, "%%%dd/%%%dld", i, i);
403 memset(backup, '\b', sizeof(backup)-1);
404 backup[sizeof(backup)-1] = 0;
405 if ((2*i)+1 < sizeof(backup))
406 backup[(2*i)+1] = 0;
407
Theodore Ts'o3839e651997-04-26 13:21:57 +0000408 if (!quiet)
409 printf("Writing inode tables: ");
410 for (i = 0; i < fs->group_desc_count; i++) {
411 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000412 printf(format, i, fs->group_desc_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000413
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000414 blk = fs->group_desc[i].bg_inode_table;
415 num = fs->inode_blocks_per_group;
416
417 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
418 if (num-j > STRIDE_LENGTH)
419 count = STRIDE_LENGTH;
420 else
421 count = num - j;
422 retval = io_channel_write_blk(fs->io, blk, count, buf);
423 if (retval)
424 printf("Warning: could not write %d blocks "
425 "in inode table starting at %d: %s\n",
426 count, blk, error_message(retval));
427 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000428 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000429 fputs(backup, stdout);
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000430 if (sync_kludge) {
431 if (sync_kludge == 1)
432 sync();
433 else if ((i % sync_kludge) == 0)
434 sync();
435 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000436 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000437 free(buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000438 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000439 fputs("done \n", stdout);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000440}
441
442static void create_root_dir(ext2_filsys fs)
443{
444 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000445 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000446
447 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
448 if (retval) {
449 com_err("ext2fs_mkdir", retval, "while creating root dir");
450 exit(1);
451 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000452 if (geteuid()) {
453 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
454 if (retval) {
455 com_err("ext2fs_read_inode", retval,
456 "while reading root inode");
457 exit(1);
458 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000459 inode.i_uid = getuid();
460 if (inode.i_uid)
461 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000462 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
463 if (retval) {
464 com_err("ext2fs_write_inode", retval,
465 "while setting root inode ownership");
466 exit(1);
467 }
468 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000469}
470
471static void create_lost_and_found(ext2_filsys fs)
472{
473 errcode_t retval;
474 ino_t ino;
475 const char *name = "lost+found";
476 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000477 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000478
479 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
480 if (retval) {
481 com_err("ext2fs_mkdir", retval, "while creating /lost+found");
482 exit(1);
483 }
484
485 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
486 if (retval) {
487 com_err("ext2_lookup", retval, "while looking up /lost+found");
488 exit(1);
489 }
490
491 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000492 if ((lpf_size += fs->blocksize) >= 16*1024)
493 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000494 retval = ext2fs_expand_dir(fs, ino);
495 if (retval) {
496 com_err("ext2fs_expand_dir", retval,
497 "while expanding /lost+found");
498 exit(1);
499 }
500 }
501}
502
503static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
504{
505 errcode_t retval;
506
Theodore Ts'of3db3561997-04-26 13:34:30 +0000507 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000508 fs->group_desc[0].bg_free_inodes_count--;
509 fs->super->s_free_inodes_count--;
510 retval = ext2fs_update_bb_inode(fs, bb_list);
511 if (retval) {
512 com_err("ext2fs_update_bb_inode", retval,
513 "while setting bad block inode");
514 exit(1);
515 }
516
517}
518
519static void reserve_inodes(ext2_filsys fs)
520{
521 ino_t i;
522 int group;
523
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000524 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000525 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000526 group = ext2fs_group_of_ino(fs, i);
527 fs->group_desc[group].bg_free_inodes_count--;
528 fs->super->s_free_inodes_count--;
529 }
530 ext2fs_mark_ib_dirty(fs);
531}
532
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000533#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +0000534static void zap_bootblock(ext2_filsys fs)
535{
536 char buf[512];
537 int retval;
538
539 memset(buf, 0, 512);
540
541 retval = io_channel_write_blk(fs->io, 0, -512, buf);
542 if (retval)
543 printf("Warning: could not erase block 0: %s\n",
544 error_message(retval));
545}
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000546#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +0000547
548
Theodore Ts'o3839e651997-04-26 13:21:57 +0000549static void show_stats(ext2_filsys fs)
550{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000551 struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super;
552 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000553 blk_t group_block;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000554 int i, need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000555
556 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000557 printf("warning: %d blocks unused.\n\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000558 param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000559
560 memset(buf, 0, sizeof(buf));
561 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
562 printf("Filesystem label=%s\n", buf);
563 printf("OS type: ");
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000564 switch (fs->super->s_creator_os) {
565 case EXT2_OS_LINUX: printf ("Linux"); break;
566 case EXT2_OS_HURD: printf ("GNU/hurd"); break;
567 case EXT2_OS_MASIX: printf ("Masix"); break;
568 default: printf ("(unknown os)");
569 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000570 printf("\n");
571 printf("Block size=%u (log=%u)\n", fs->blocksize,
572 s->s_log_block_size);
573 printf("Fragment size=%u (log=%u)\n", fs->fragsize,
574 s->s_log_frag_size);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000575 printf("%u inodes, %u blocks\n", s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000576 s->s_blocks_count);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000577 printf("%u blocks (%2.2f%%) reserved for the super user\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000578 s->s_r_blocks_count,
579 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000580 printf("First data block=%u\n", s->s_first_data_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000581 printf("%lu block group%s\n", fs->group_desc_count,
582 (fs->group_desc_count > 1) ? "s" : "");
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000583 printf("%u blocks per group, %u fragments per group\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000584 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000585 printf("%u inodes per group\n", s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000586
587 if (fs->group_desc_count == 1) {
588 printf("\n");
589 return;
590 }
591
592 printf("Superblock backups stored on blocks: ");
593 group_block = s->s_first_data_block;
594 col_left = 0;
595 for (i = 1; i < fs->group_desc_count; i++) {
596 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000597 if (!ext2fs_bg_has_super(fs, i))
598 continue;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000599 need = log10(group_block) + 2;
600 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000601 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000602 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000603 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000604 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000605 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000606 if (i != fs->group_desc_count - 1)
607 printf(", ");
608 }
609 printf("\n\n");
610}
611
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000612#ifndef HAVE_STRCASECMP
613static int strcasecmp (char *s1, char *s2)
614{
615 while (*s1 && *s2) {
616 int ch1 = *s1++, ch2 = *s2++;
617 if (isupper (ch1))
618 ch1 = tolower (ch1);
619 if (isupper (ch2))
620 ch2 = tolower (ch2);
621 if (ch1 != ch2)
622 return ch1 - ch2;
623 }
624 return *s1 ? 1 : *s2 ? -1 : 0;
625}
626#endif
627
628/*
629 * Set the S_CREATOR_OS field. Return true if OS is known,
630 * otherwise, 0.
631 */
632static int set_os(struct ext2_super_block *sb, char *os)
633{
634 if (isdigit (*os))
635 sb->s_creator_os = atoi (os);
636 else if (strcasecmp(os, "linux") == 0)
637 sb->s_creator_os = EXT2_OS_LINUX;
638 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
639 sb->s_creator_os = EXT2_OS_HURD;
640 else if (strcasecmp(os, "masix") == 0)
641 sb->s_creator_os = EXT2_OS_MASIX;
642 else
643 return 0;
644 return 1;
645}
646
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000647#define PATH_SET "PATH=/sbin"
648
Theodore Ts'od163b091997-10-03 17:42:28 +0000649static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000650{
651 char *buf, *token, *next, *p, *arg;
652 int len;
653 int raid_usage = 0;
654
655 len = strlen(opts);
656 buf = malloc(len+1);
657 if (!buf) {
658 fprintf(stderr, "Couldn't allocate memory to parse "
659 "raid options!\n");
660 exit(1);
661 }
662 strcpy(buf, opts);
663 for (token = buf; token && *token; token = next) {
664 p = strchr(token, ',');
665 next = 0;
666 if (p) {
667 *p = 0;
668 next = p+1;
669 }
670 arg = strchr(token, '=');
671 if (arg) {
672 *arg = 0;
673 arg++;
674 }
675 if (strcmp(token, "stride") == 0) {
676 if (!arg) {
677 raid_usage++;
678 continue;
679 }
680 fs_stride = strtoul(arg, &p, 0);
681 if (*p || (fs_stride == 0)) {
682 fprintf(stderr, "Invalid stride parameter.\n");
683 raid_usage++;
684 continue;
685 }
686 } else
687 raid_usage++;
688 }
689 if (raid_usage) {
690 fprintf(stderr, "\nBad raid options specified.\n\n"
691 "Raid options are separated by commas, "
692 "and may take an argument which\n"
693 "\tis set off by an equals ('=') sign.\n\n"
694 "Valid raid options are:\n"
695 "\tstride=<stride length in blocks>\n\n");
696 exit(1);
697 }
698}
699
700
701
Theodore Ts'o3839e651997-04-26 13:21:57 +0000702static void PRS(int argc, char *argv[])
703{
Theodore Ts'o519149f1997-10-25 03:49:49 +0000704 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000705 int size;
706 char * tmp;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000707 blk_t max = 8192;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000708 int blocksize = 0;
709 int inode_ratio = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000710 int reserved_ratio = 5;
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000711 ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000712 errcode_t retval;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000713 int sparse_option = 1;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000714 char *oldpath = getenv("PATH");
Theodore Ts'o521e3681997-04-29 17:48:10 +0000715 struct ext2fs_sb *param_ext2 = (struct ext2fs_sb *) &param;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000716 char *raid_opts = 0;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000717 char *fs_type = 0;
Theodore Ts'oa789d841998-03-30 01:20:55 +0000718 blk_t dev_size;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000719
Theodore Ts'o3839e651997-04-26 13:21:57 +0000720 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000721 if (oldpath) {
722 char *newpath;
723
724 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
725 strcpy (newpath, PATH_SET);
726 strcat (newpath, ":");
727 strcat (newpath, oldpath);
728 putenv (newpath);
729 } else
730 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000731
732 setbuf(stdout, NULL);
733 setbuf(stderr, NULL);
734 initialize_ext2_error_table();
735 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000736 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000737
738 fprintf (stderr, "mke2fs %s, %s for EXT2 FS %s, %s\n",
739 E2FSPROGS_VERSION, E2FSPROGS_DATE,
740 EXT2FS_VERSION, EXT2FS_DATE);
741 if (argc && *argv)
742 program_name = *argv;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000743 while ((c = getopt (argc, argv,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000744 "b:cf:g:i:l:m:no:qr:R:s:tvI:ST:FL:M:N:V")) != EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000745 switch (c) {
746 case 'b':
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000747 blocksize = strtoul(optarg, &tmp, 0);
748 if (blocksize < 1024 || blocksize > 4096 || *tmp) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000749 com_err(program_name, 0, "bad block size - %s",
750 optarg);
751 exit(1);
752 }
753 param.s_log_block_size =
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000754 log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
755 max = blocksize * 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000756 break;
757 case 'c':
758 case 't': /* Check for bad blocks */
759 cflag = 1;
760 break;
761 case 'f':
762 size = strtoul(optarg, &tmp, 0);
763 if (size < 1024 || size > 4096 || *tmp) {
764 com_err(program_name, 0, "bad fragment size - %s",
765 optarg);
766 exit(1);
767 }
768 param.s_log_frag_size =
769 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
770 printf("Warning: fragments not supported. "
771 "Ignoring -f option\n");
772 break;
773 case 'g':
774 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000775 if (*tmp) {
776 com_err(program_name, 0,
777 "Illegal number for blocks per group");
778 exit(1);
779 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000780 if ((param.s_blocks_per_group % 8) != 0) {
781 com_err(program_name, 0,
782 "blocks per group must be multiple of 8");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000783 exit(1);
784 }
785 break;
786 case 'i':
787 inode_ratio = strtoul(optarg, &tmp, 0);
788 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
789 *tmp) {
790 com_err(program_name, 0, "bad inode ratio - %s",
791 optarg);
792 exit(1);
793 }
794 break;
795 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000796 bad_blocks_filename = malloc(strlen(optarg)+1);
797 if (!bad_blocks_filename) {
798 com_err(program_name, ENOMEM,
799 "in malloc for bad_blocks_filename");
800 exit(1);
801 }
802 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000803 break;
804 case 'm':
805 reserved_ratio = strtoul(optarg, &tmp, 0);
806 if (reserved_ratio > 50 || *tmp) {
807 com_err(program_name, 0,
808 "bad reserved blocks percent - %s",
809 optarg);
810 exit(1);
811 }
812 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000813 case 'n':
814 noaction++;
815 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000816 case 'o':
817 creator_os = optarg;
818 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000819 case 'r':
820 param.s_rev_level = atoi(optarg);
821 break;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000822 case 's':
823 sparse_option = atoi(optarg);
824 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000825#ifdef EXT2_DYNAMIC_REV
826 case 'I':
827 param.s_inode_size = atoi(optarg);
828 break;
829#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000830 case 'N':
831 num_inodes = atoi(optarg);
832 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000833 case 'v':
834 verbose = 1;
835 break;
836 case 'q':
837 quiet = 1;
838 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +0000839 case 'F':
840 force = 1;
841 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000842 case 'L':
843 volume_label = optarg;
844 break;
845 case 'M':
846 mount_dir = optarg;
847 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000848 case 'R':
849 raid_opts = optarg;
850 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000851 case 'S':
852 super_only = 1;
853 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000854 case 'T':
855 fs_type = optarg;
856 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000857 case 'V':
858 /* Print version number and exit */
859 fprintf(stderr, "\tUsing %s\n",
860 error_message(EXT2_ET_BASE));
861 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000862 default:
863 usage();
864 }
865 if (optind == argc)
866 usage();
867 device_name = argv[optind];
868 optind++;
869 if (optind < argc) {
870 param.s_blocks_count = strtoul(argv[optind++], &tmp, 0);
871 if (*tmp) {
872 com_err(program_name, 0, "bad blocks count - %s",
873 argv[optind - 1]);
874 exit(1);
875 }
876 }
877 if (optind < argc)
878 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000879
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000880 if (raid_opts)
881 parse_raid_opts(raid_opts);
882
Theodore Ts'o74becf31997-04-26 14:37:06 +0000883 if (!force)
884 check_plausibility();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000885 check_mount();
886
Theodore Ts'o3839e651997-04-26 13:21:57 +0000887 param.s_log_frag_size = param.s_log_block_size;
888
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000889 if (noaction && param.s_blocks_count) {
890 dev_size = param.s_blocks_count;
891 retval = 0;
892 } else
893 retval = ext2fs_get_device_size(device_name,
894 EXT2_BLOCK_SIZE(&param),
895 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000896 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
897 com_err(program_name, retval,
898 "while trying to determine filesystem size");
899 exit(1);
900 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000901 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +0000902 if (retval == EXT2_ET_UNIMPLEMENTED) {
903 com_err(program_name, 0,
904 "Couldn't determine device size; you "
905 "must specify\nthe size of the "
906 "filesystem\n");
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000907 exit(1);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000908 } else
909 param.s_blocks_count = dev_size;
910 } else if (!force && (param.s_blocks_count > dev_size)) {
911 com_err(program_name, 0,
912 "Filesystem larger than apparent filesystem size.");
913 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000914 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000915
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000916 set_fs_defaults(fs_type, param_ext2, blocksize, &inode_ratio);
917
Theodore Ts'o521e3681997-04-29 17:48:10 +0000918 if (param.s_blocks_per_group) {
919 if (param.s_blocks_per_group < 256 ||
920 param.s_blocks_per_group > max || *tmp) {
921 com_err(program_name, 0,
922 "blocks per group count out of range");
923 exit(1);
924 }
925 }
926
Theodore Ts'o3839e651997-04-26 13:21:57 +0000927 /*
928 * Calculate number of inodes based on the inode ratio
929 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000930 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'of3db3561997-04-26 13:34:30 +0000931 ((long long) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
932 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000933
934 /*
935 * Calculate number of blocks to reserve
936 */
937 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000938
939 /*
940 * If we are using revision #1, use the sparse super feature
941 * by default
942 */
943#ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
944 if ((sparse_option == 1)
945#ifdef EXT2_DYNAMIC_REV
Theodore Ts'obbfa3aa1998-03-21 07:12:46 +0000946 || ((param.s_rev_level >= EXT2_DYNAMIC_REV) && (!sparse_option))
Theodore Ts'o521e3681997-04-29 17:48:10 +0000947#endif
948 )
949 param_ext2->s_feature_ro_compat |=
950 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
951#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000952}
953
954int main (int argc, char *argv[])
955{
956 errcode_t retval = 0;
957 ext2_filsys fs;
958 badblocks_list bb_list = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000959 struct ext2fs_sb *s;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000960
961 PRS(argc, argv);
962
Theodore Ts'o3839e651997-04-26 13:21:57 +0000963 /*
964 * Initialize the superblock....
965 */
966 retval = ext2fs_initialize(device_name, 0, &param,
967 unix_io_manager, &fs);
968 if (retval) {
969 com_err(device_name, retval, "while setting up superblock");
970 exit(1);
971 }
972
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000973 /*
974 * Generate a UUID for it...
975 */
976 s = (struct ext2fs_sb *) fs->super;
977 uuid_generate(s->s_uuid);
978
979 /*
980 * Override the creator OS, if applicable
981 */
982 if (creator_os && !set_os(fs->super, creator_os)) {
983 com_err (program_name, 0, "unknown os - %s", creator_os);
984 exit(1);
985 }
986
987 /*
988 * Set the volume label...
989 */
990 if (volume_label) {
991 memset(s->s_volume_name, 0, sizeof(s->s_volume_name));
992 strncpy(s->s_volume_name, volume_label,
993 sizeof(s->s_volume_name));
994 }
995
996 /*
997 * Set the last mount directory
998 */
999 if (mount_dir) {
1000 memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted));
1001 strncpy(s->s_last_mounted, mount_dir,
1002 sizeof(s->s_last_mounted));
1003 }
1004
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001005 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001006 show_stats(fs);
1007
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001008 if (noaction)
1009 exit(0);
1010
Theodore Ts'o3839e651997-04-26 13:21:57 +00001011 if (bad_blocks_filename)
1012 read_bb_file(fs, &bb_list, bad_blocks_filename);
1013 if (cflag)
1014 test_disk(fs, &bb_list);
1015
1016 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001017 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001018 retval = ext2fs_allocate_tables(fs);
1019 if (retval) {
1020 com_err(program_name, retval,
1021 "while trying to allocate filesystem tables");
1022 exit(1);
1023 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001024 if (super_only) {
1025 fs->super->s_state |= EXT2_ERROR_FS;
1026 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1027 } else {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001028 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001029 create_root_dir(fs);
1030 create_lost_and_found(fs);
1031 reserve_inodes(fs);
1032 create_bad_block_inode(fs, bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001033#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +00001034 zap_bootblock(fs);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001035#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +00001036 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001037
1038 if (!quiet)
1039 printf("Writing superblocks and "
1040 "filesystem accounting information: ");
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001041 retval = ext2fs_flush(fs);
1042 if (retval) {
1043 printf("\nWarning, had trouble writing out superblocks.");
1044 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001045 if (!quiet)
1046 printf("done\n");
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001047 ext2fs_close(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001048 return 0;
1049}