blob: 702c016b8f5519b5e9dceb81e19a05e0392fc059 [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>
Theodore Ts'o3839e651997-04-26 13:21:57 +000022#include <time.h>
Theodore Ts'o27401561999-09-14 20:11:19 +000023#ifdef linux
24#include <sys/utsname.h>
25#endif
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000026#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000028#else
29extern char *optarg;
30extern int optind;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000031#endif
32#ifdef HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000033#include <unistd.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000034#endif
35#ifdef HAVE_STDLIB_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000036#include <stdlib.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000037#endif
38#ifdef HAVE_ERRNO_H
39#include <errno.h>
40#endif
41#ifdef HAVE_MNTENT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000042#include <mntent.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000043#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000044#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000045#include <sys/types.h>
46
Theodore Ts'of3db3561997-04-26 13:34:30 +000047#include <linux/ext2_fs.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000048
49#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000050#include "uuid/uuid.h"
Theodore Ts'o896938d1999-10-23 01:04:50 +000051#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000052#include "ext2fs/ext2fs.h"
Theodore Ts'o63985322001-01-03 17:02:13 +000053#include "util.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000054#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000055#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000056
57#define STRIDE_LENGTH 8
58
Theodore Ts'o6733c2f1999-11-23 02:23:30 +000059#ifndef __sparc__
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000060#define ZAP_BOOTBLOCK
61#endif
62
Theodore Ts'o3839e651997-04-26 13:21:57 +000063extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000064extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000065
66const char * program_name = "mke2fs";
Theodore Ts'od48755e2000-12-09 14:36:04 +000067const char * device_name /* = NULL */;
Theodore Ts'o3839e651997-04-26 13:21:57 +000068
69/* Command line options */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000070int cflag;
71int verbose;
72int quiet;
73int super_only;
74int force;
75int noaction;
76int journal_size;
77int journal_flags;
78char *bad_blocks_filename;
79__u32 fs_stride;
Theodore Ts'o3839e651997-04-26 13:21:57 +000080
81struct ext2_super_block param;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000082char *creator_os;
83char *volume_label;
84char *mount_dir;
85char *journal_device;
86int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
Theodore Ts'o3839e651997-04-26 13:21:57 +000087
Theodore Ts'o63985322001-01-03 17:02:13 +000088static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000089{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000090 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
Theodore Ts'odc2ec522001-01-18 01:51:15 +000091 "[-f fragment-size]\n\t[-i bytes-per-inode] [-j] [-J journal-options]"
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] "
Theodore Ts'o18160d21999-10-23 01:22:17 +000094 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
95 "[-r fs-revision] [-R raid_opts] [-s sparse-super-flag]\n\t"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000096 "[-qvSV] device [blocks-count]\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +000097 program_name);
98 exit(1);
99}
100
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000101static int int_log2(int arg)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000102{
103 int l = 0;
104
105 arg >>= 1;
106 while (arg) {
107 l++;
108 arg >>= 1;
109 }
110 return l;
111}
112
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000113static int int_log10(unsigned int arg)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000114{
115 int l;
116
117 for (l=0; arg ; l++)
118 arg = arg / 10;
119 return l;
120}
121
Theodore Ts'o3839e651997-04-26 13:21:57 +0000122/*
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000123 * This function sets the default parameters for a filesystem
124 *
Theodore Ts'o27401561999-09-14 20:11:19 +0000125 * The type is specified by the user. The size is the maximum size
126 * (in megabytes) for which a set of parameters applies, with a size
127 * of zero meaning that it is the default parameter for the type.
128 * Note that order is important in the table below.
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000129 */
130static char default_str[] = "default";
131struct mke2fs_defaults {
Theodore Ts'o4a600561999-10-26 14:35:51 +0000132 const char *type;
133 int size;
134 int blocksize;
135 int inode_ratio;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000136} settings[] = {
137 { default_str, 0, 4096, 8192 },
138 { default_str, 512, 1024, 4096 },
139 { default_str, 3, 1024, 8192 },
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000140 { "journal", 0, 4096, 8192 },
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000141 { "news", 0, 4096, 4096 },
Theodore Ts'o44c09c02001-01-14 17:02:09 +0000142 { "largefile", 0, 4096, 1024 * 1024 },
143 { "largefile4", 0, 4096, 4096 * 1024 },
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000144 { 0, 0, 0, 0},
145};
146
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000147static void set_fs_defaults(char *fs_type, struct ext2_super_block *super,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000148 int blocksize, int *inode_ratio)
149{
150 int megs;
151 int ratio = 0;
152 struct mke2fs_defaults *p;
153
Theodore Ts'o9094f281999-10-26 16:42:50 +0000154 megs = (super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) /
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000155 1024);
156 if (inode_ratio)
157 ratio = *inode_ratio;
158 if (!fs_type)
159 fs_type = default_str;
160 for (p = settings; p->type; p++) {
161 if ((strcmp(p->type, fs_type) != 0) &&
162 (strcmp(p->type, default_str) != 0))
163 continue;
164 if ((p->size != 0) &&
165 (megs > p->size))
166 continue;
167 if (ratio == 0)
168 *inode_ratio = p->inode_ratio;
169 if (blocksize == 0) {
Theodore Ts'o9094f281999-10-26 16:42:50 +0000170 super->s_log_frag_size = super->s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000171 int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000172 }
173 }
174 if (blocksize == 0)
Theodore Ts'o9094f281999-10-26 16:42:50 +0000175 super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000176}
177
178/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000179 * Helper function for read_bb_file and test_disk
180 */
181static void invalid_block(ext2_filsys fs, blk_t blk)
182{
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000183 printf(_("Bad block %u out of range; ignored.\n"), blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000184 return;
185}
186
187/*
188 * Reads the bad blocks list from a file
189 */
190static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
191 const char *bad_blocks_file)
192{
193 FILE *f;
194 errcode_t retval;
195
196 f = fopen(bad_blocks_file, "r");
197 if (!f) {
198 com_err("read_bad_blocks_file", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000199 _("while trying to open %s"), bad_blocks_file);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000200 exit(1);
201 }
202 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
203 fclose (f);
204 if (retval) {
205 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000206 _("while reading in list of bad blocks from file"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000207 exit(1);
208 }
209}
210
211/*
212 * Runs the badblocks program to test the disk
213 */
214static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
215{
216 FILE *f;
217 errcode_t retval;
218 char buf[1024];
219
Theodore Ts'of635d7f1997-05-09 02:50:16 +0000220 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
221 quiet ? "" : "-s ", fs->device_name,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000222 fs->super->s_blocks_count);
223 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000224 printf(_("Running command: %s\n"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000225 f = popen(buf, "r");
226 if (!f) {
227 com_err("popen", errno,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000228 _("while trying run '%s'"), buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000229 exit(1);
230 }
231 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000232 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000233 if (retval) {
234 com_err("ext2fs_read_bb_FILE", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000235 _("while processing list of bad blocks from program"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000236 exit(1);
237 }
238}
239
240static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
241{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000242 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000243 int must_be_good;
244 blk_t blk;
245 badblocks_iterate bb_iter;
246 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000247 blk_t group_block;
248 int group;
249 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000250
251 if (!bb_list)
252 return;
253
254 /*
255 * The primary superblock and group descriptors *must* be
256 * good; if not, abort.
257 */
258 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
259 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
260 if (badblocks_list_test(bb_list, i)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000261 fprintf(stderr, _("Block %d in primary "
262 "superblock/group descriptor area bad.\n"), i);
263 fprintf(stderr, _("Blocks %d through %d must be good "
264 "in order to build a filesystem.\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000265 fs->super->s_first_data_block, must_be_good);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000266 fprintf(stderr, _("Aborting....\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000267 exit(1);
268 }
269 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000270
271 /*
272 * See if any of the bad blocks are showing up in the backup
273 * superblocks and/or group descriptors. If so, issue a
274 * warning and adjust the block counts appropriately.
275 */
276 group_block = fs->super->s_first_data_block +
277 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000278
279 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000280 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000281 for (j=0; j < fs->desc_blocks+1; j++) {
282 if (badblocks_list_test(bb_list, group_block +
283 j)) {
284 if (!group_bad)
285 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000286_("Warning: the backup superblock/group descriptors at block %d contain\n"
287" bad blocks.\n\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000288 group_block);
289 group_bad++;
290 group = ext2fs_group_of_blk(fs, group_block+j);
291 fs->group_desc[group].bg_free_blocks_count++;
292 fs->super->s_free_blocks_count++;
293 }
294 }
295 group_block += fs->super->s_blocks_per_group;
296 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000297
298 /*
299 * Mark all the bad blocks as used...
300 */
301 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
302 if (retval) {
303 com_err("badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000304 _("while marking bad blocks as used"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000305 exit(1);
306 }
307 while (badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000308 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000309 badblocks_list_iterate_end(bb_iter);
310}
311
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000312/*
313 * These functions implement a generalized progress meter.
314 */
315struct progress_struct {
316 char format[20];
317 char backup[80];
318 __u32 max;
319};
320
321static void progress_init(struct progress_struct *progress,
322 char *label,__u32 max)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000323{
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000324 int i;
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000325
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000326 memset(progress, 0, sizeof(struct progress_struct));
327 if (quiet)
328 return;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000329
330 /*
331 * Figure out how many digits we need
332 */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000333 i = int_log10(max);
334 sprintf(progress->format, "%%%dd/%%%dld", i, i);
335 memset(progress->backup, '\b', sizeof(progress->backup)-1);
336 progress->backup[sizeof(progress->backup)-1] = 0;
337 if ((2*i)+1 < sizeof(progress->backup))
338 progress->backup[(2*i)+1] = 0;
339 progress->max = max;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000340
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000341 fputs(label, stdout);
342 fflush(stdout);
343}
344
345static void progress_update(struct progress_struct *progress, __u32 val)
346{
347 if (progress->format[0] == 0)
348 return;
349 printf(progress->format, val, progress->max);
350 fputs(progress->backup, stdout);
351}
352
353static void progress_close(struct progress_struct *progress)
354{
355 if (progress->format[0] == 0)
356 return;
357 fputs(_("done \n"), stdout);
358}
359
360
361/*
362 * Helper function which zeros out _num_ blocks starting at _blk_. In
363 * case of an error, the details of the error is returned via _ret_blk_
364 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
365 * success, and an error code on an error.
366 *
367 * As a special case, if the first argument is NULL, then it will
368 * attempt to free the static zeroizing buffer. (This is to keep
369 * programs that check for memory leaks happy.)
370 */
371static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
372 struct progress_struct *progress,
373 blk_t *ret_blk, int *ret_count)
374{
375 int j, count, next_update, next_update_incr;
376 static char *buf;
377 errcode_t retval;
378
379 /* If fs is null, clean up the static buffer and return */
380 if (!fs) {
381 if (buf) {
382 free(buf);
383 buf = 0;
384 }
385 return 0;
386 }
387 /* Allocate the zeroizing buffer if necessary */
388 if (!buf) {
389 buf = malloc(fs->blocksize * STRIDE_LENGTH);
390 if (!buf) {
391 com_err("malloc", ENOMEM,
392 _("while allocating zeroizing buffer"));
393 exit(1);
394 }
395 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
396 }
397 /* OK, do the write loop */
398 next_update = 0;
399 next_update_incr = num / 100;
400 if (next_update_incr < 1)
401 next_update_incr = 1;
402 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
403 if (num-j > STRIDE_LENGTH)
404 count = STRIDE_LENGTH;
405 else
406 count = num - j;
407 retval = io_channel_write_blk(fs->io, blk, count, buf);
408 if (retval) {
409 if (ret_count)
410 *ret_count = count;
411 if (ret_blk)
412 *ret_blk = blk;
413 return retval;
414 }
415 if (progress && j > next_update) {
416 next_update += num / 100;
417 progress_update(progress, blk);
418 }
419 }
420 return 0;
421}
422
423static void write_inode_tables(ext2_filsys fs)
424{
425 errcode_t retval;
426 blk_t blk;
427 int i, num;
428 struct progress_struct progress;
429
430 if (quiet)
431 memset(&progress, 0, sizeof(progress));
432 else
433 progress_init(&progress, _("Writing inode tables: "),
434 fs->group_desc_count);
435
Theodore Ts'o3839e651997-04-26 13:21:57 +0000436 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000437 progress_update(&progress, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000438
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000439 blk = fs->group_desc[i].bg_inode_table;
440 num = fs->inode_blocks_per_group;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000441
442 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
443 if (retval) {
444 printf(_("\nCould not write %d blocks "
445 "in inode table starting at %d: %s\n"),
446 num, blk, error_message(retval));
447 exit(1);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000448 }
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000449 if (sync_kludge) {
450 if (sync_kludge == 1)
451 sync();
452 else if ((i % sync_kludge) == 0)
453 sync();
454 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000455 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000456 zero_blocks(0, 0, 0, 0, 0, 0);
457 progress_close(&progress);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000458}
459
460static void create_root_dir(ext2_filsys fs)
461{
462 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000463 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000464
465 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
466 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000467 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000468 exit(1);
469 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000470 if (geteuid()) {
471 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
472 if (retval) {
473 com_err("ext2fs_read_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000474 _("while reading root inode"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000475 exit(1);
476 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000477 inode.i_uid = getuid();
478 if (inode.i_uid)
479 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000480 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
481 if (retval) {
482 com_err("ext2fs_write_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000483 _("while setting root inode ownership"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000484 exit(1);
485 }
486 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000487}
488
489static void create_lost_and_found(ext2_filsys fs)
490{
491 errcode_t retval;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000492 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000493 const char *name = "lost+found";
494 int i;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000495 int lpf_size = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000496
497 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
498 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000499 com_err("ext2fs_mkdir", retval,
500 _("while creating /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000501 exit(1);
502 }
503
504 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
505 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000506 com_err("ext2_lookup", retval,
507 _("while looking up /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000508 exit(1);
509 }
510
511 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000512 if ((lpf_size += fs->blocksize) >= 16*1024)
513 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000514 retval = ext2fs_expand_dir(fs, ino);
515 if (retval) {
516 com_err("ext2fs_expand_dir", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000517 _("while expanding /lost+found"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000518 exit(1);
519 }
520 }
521}
522
523static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
524{
525 errcode_t retval;
526
Theodore Ts'of3db3561997-04-26 13:34:30 +0000527 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000528 fs->group_desc[0].bg_free_inodes_count--;
529 fs->super->s_free_inodes_count--;
530 retval = ext2fs_update_bb_inode(fs, bb_list);
531 if (retval) {
532 com_err("ext2fs_update_bb_inode", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000533 _("while setting bad block inode"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000534 exit(1);
535 }
536
537}
538
539static void reserve_inodes(ext2_filsys fs)
540{
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000541 ext2_ino_t i;
542 int group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000543
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000544 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000545 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000546 group = ext2fs_group_of_ino(fs, i);
547 fs->group_desc[group].bg_free_inodes_count--;
548 fs->super->s_free_inodes_count--;
549 }
550 ext2fs_mark_ib_dirty(fs);
551}
552
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000553static void zap_sector(ext2_filsys fs, int sect)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000554{
555 char buf[512];
556 int retval;
557
558 memset(buf, 0, 512);
559
Theodore Ts'oe41784e2000-08-14 14:44:15 +0000560 io_channel_set_blksize(fs->io, 512);
561 retval = io_channel_write_blk(fs->io, sect, -512, buf);
562 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000563 if (retval)
Theodore Ts'oe294cf22000-10-24 18:41:44 +0000564 printf(_("Warning: could not erase sector %d: %s\n"), sect,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000565 error_message(retval));
566}
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000567
568static void create_journal_dev(ext2_filsys fs)
569{
570 struct progress_struct progress;
571 errcode_t retval;
572 char *buf;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000573 blk_t blk;
574 int count;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000575
Theodore Ts'od6a27e02001-04-17 02:34:41 +0000576 retval = ext2fs_create_journal_superblock(fs,
577 fs->super->s_blocks_count, 0, &buf);
578 if (retval) {
579 com_err("create_journal_dev", retval,
580 _("while initializing journal superblock"));
581 exit(1);
582 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000583 if (quiet)
584 memset(&progress, 0, sizeof(progress));
585 else
586 progress_init(&progress, _("Zeroing journal device: "),
587 fs->super->s_blocks_count);
588
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000589 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
590 &progress, &blk, &count);
591 if (retval) {
592 com_err("create_journal_dev", retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +0000593 "while zeroing journal device (block %u, count %d)",
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000594 blk, count);
595 exit(1);
596 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000597 zero_blocks(0, 0, 0, 0, 0, 0);
598
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000599 retval = io_channel_write_blk(fs->io,
600 fs->super->s_first_data_block+1,
601 1, buf);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000602 if (retval) {
603 com_err("create_journal_dev", retval,
604 _("while writing journal superblock"));
605 exit(1);
606 }
607 progress_close(&progress);
608}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000609
Theodore Ts'o3839e651997-04-26 13:21:57 +0000610static void show_stats(ext2_filsys fs)
611{
Theodore Ts'oef9abe52001-01-01 15:31:53 +0000612 struct ext2_super_block *s = fs->super;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000613 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000614 blk_t group_block;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000615 int i, need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000616
617 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000618 printf(_("warning: %d blocks unused.\n\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000619 param.s_blocks_count - s->s_blocks_count);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000620
621 memset(buf, 0, sizeof(buf));
622 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000623 printf(_("Filesystem label=%s\n"), buf);
624 printf(_("OS type: "));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000625 switch (fs->super->s_creator_os) {
626 case EXT2_OS_LINUX: printf ("Linux"); break;
Theodore Ts'oad6783d1999-10-26 01:58:54 +0000627 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000628 case EXT2_OS_MASIX: printf ("Masix"); break;
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000629 default: printf (_("(unknown os)"));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000630 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000631 printf("\n");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000632 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000633 s->s_log_block_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000634 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000635 s->s_log_frag_size);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000636 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000637 s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000638 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000639 s->s_r_blocks_count,
640 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000641 printf(_("First data block=%u\n"), s->s_first_data_block);
642 if (fs->group_desc_count > 1)
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000643 printf(_("%u block groups\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000644 else
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000645 printf(_("%u block group\n"), fs->group_desc_count);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000646 printf(_("%u blocks per group, %u fragments per group\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000647 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000648 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000649
650 if (fs->group_desc_count == 1) {
651 printf("\n");
652 return;
653 }
654
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000655 printf(_("Superblock backups stored on blocks: "));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000656 group_block = s->s_first_data_block;
657 col_left = 0;
658 for (i = 1; i < fs->group_desc_count; i++) {
659 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000660 if (!ext2fs_bg_has_super(fs, i))
661 continue;
Theodore Ts'o76714331999-10-20 18:06:29 +0000662 if (i != 1)
663 printf(", ");
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000664 need = int_log10(group_block) + 2;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000665 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000666 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000667 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000668 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000669 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000670 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000671 }
672 printf("\n\n");
673}
674
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000675/*
676 * Set the S_CREATOR_OS field. Return true if OS is known,
677 * otherwise, 0.
678 */
679static int set_os(struct ext2_super_block *sb, char *os)
680{
681 if (isdigit (*os))
682 sb->s_creator_os = atoi (os);
683 else if (strcasecmp(os, "linux") == 0)
684 sb->s_creator_os = EXT2_OS_LINUX;
685 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
686 sb->s_creator_os = EXT2_OS_HURD;
687 else if (strcasecmp(os, "masix") == 0)
688 sb->s_creator_os = EXT2_OS_MASIX;
689 else
690 return 0;
691 return 1;
692}
693
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000694#define PATH_SET "PATH=/sbin"
695
Theodore Ts'od163b091997-10-03 17:42:28 +0000696static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000697{
698 char *buf, *token, *next, *p, *arg;
699 int len;
700 int raid_usage = 0;
701
702 len = strlen(opts);
703 buf = malloc(len+1);
704 if (!buf) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000705 fprintf(stderr, _("Couldn't allocate memory to parse "
706 "raid options!\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000707 exit(1);
708 }
709 strcpy(buf, opts);
710 for (token = buf; token && *token; token = next) {
711 p = strchr(token, ',');
712 next = 0;
713 if (p) {
714 *p = 0;
715 next = p+1;
716 }
717 arg = strchr(token, '=');
718 if (arg) {
719 *arg = 0;
720 arg++;
721 }
722 if (strcmp(token, "stride") == 0) {
723 if (!arg) {
724 raid_usage++;
725 continue;
726 }
727 fs_stride = strtoul(arg, &p, 0);
728 if (*p || (fs_stride == 0)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000729 fprintf(stderr,
730 _("Invalid stride parameter.\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000731 raid_usage++;
732 continue;
733 }
734 } else
735 raid_usage++;
736 }
737 if (raid_usage) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000738 fprintf(stderr, _("\nBad raid options specified.\n\n"
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000739 "Raid options are separated by commas, "
740 "and may take an argument which\n"
741 "\tis set off by an equals ('=') sign.\n\n"
742 "Valid raid options are:\n"
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000743 "\tstride=<stride length in blocks>\n\n"));
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000744 exit(1);
745 }
746}
747
Theodore Ts'o896938d1999-10-23 01:04:50 +0000748static __u32 ok_features[3] = {
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000749 EXT3_FEATURE_COMPAT_HAS_JOURNAL, /* Compat */
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000750 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
751 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV,
Theodore Ts'o896938d1999-10-23 01:04:50 +0000752 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
753};
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000754
755
Theodore Ts'o3839e651997-04-26 13:21:57 +0000756static void PRS(int argc, char *argv[])
757{
Theodore Ts'o4a600561999-10-26 14:35:51 +0000758 int c;
759 int size;
760 char * tmp;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000761 blk_t group_blk_max = 8192;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000762 int blocksize = 0;
763 int inode_ratio = 0;
764 int reserved_ratio = 5;
Theodore Ts'odfcdc322001-01-11 15:38:00 +0000765 ext2_ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000766 errcode_t retval;
Theodore Ts'of6f65832000-10-25 03:01:37 +0000767 int sparse_option = 1;
Theodore Ts'o4a600561999-10-26 14:35:51 +0000768 char * oldpath = getenv("PATH");
Theodore Ts'o4a600561999-10-26 14:35:51 +0000769 char * raid_opts = 0;
770 char * fs_type = 0;
Theodore Ts'of6f65832000-10-25 03:01:37 +0000771 const char * feature_set = "filetype";
Theodore Ts'o4a600561999-10-26 14:35:51 +0000772 blk_t dev_size;
Theodore Ts'o27401561999-09-14 20:11:19 +0000773#ifdef linux
Theodore Ts'o4a600561999-10-26 14:35:51 +0000774 struct utsname ut;
Theodore Ts'o27401561999-09-14 20:11:19 +0000775
776 if (uname(&ut)) {
777 perror("uname");
778 exit(1);
779 }
Theodore Ts'o896938d1999-10-23 01:04:50 +0000780 if ((ut.release[0] == '1') ||
781 (ut.release[0] == '2' && ut.release[1] == '.' &&
782 ut.release[2] < '2' && ut.release[3] == '.'))
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000783 feature_set = NULL;
Theodore Ts'o27401561999-09-14 20:11:19 +0000784#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000785 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000786 if (oldpath) {
787 char *newpath;
788
789 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
790 strcpy (newpath, PATH_SET);
791 strcat (newpath, ":");
792 strcat (newpath, oldpath);
793 putenv (newpath);
794 } else
795 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000796
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000797 tmp = getenv("MKE2FS_SYNC");
798 if (tmp)
799 sync_kludge = atoi(tmp);
800
Theodore Ts'o3839e651997-04-26 13:21:57 +0000801 setbuf(stdout, NULL);
802 setbuf(stderr, NULL);
803 initialize_ext2_error_table();
804 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000805 param.s_rev_level = 1; /* Create revision 1 filesystems now */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000806
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000807 fprintf (stderr, _("mke2fs %s, %s for EXT2 FS %s, %s\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000808 E2FSPROGS_VERSION, E2FSPROGS_DATE,
809 EXT2FS_VERSION, EXT2FS_DATE);
810 if (argc && *argv)
811 program_name = *argv;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000812 while ((c = getopt (argc, argv,
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000813 "b:cf:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000814 switch (c) {
815 case 'b':
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000816 blocksize = strtoul(optarg, &tmp, 0);
817 if (blocksize < 1024 || blocksize > 4096 || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000818 com_err(program_name, 0,
819 _("bad block size - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000820 exit(1);
821 }
822 param.s_log_block_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000823 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000824 group_blk_max = blocksize * 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000825 break;
826 case 'c':
827 case 't': /* Check for bad blocks */
828 cflag = 1;
829 break;
830 case 'f':
831 size = strtoul(optarg, &tmp, 0);
832 if (size < 1024 || size > 4096 || *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000833 com_err(program_name, 0,
834 _("bad fragment size - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000835 optarg);
836 exit(1);
837 }
838 param.s_log_frag_size =
Theodore Ts'o6733c2f1999-11-23 02:23:30 +0000839 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000840 printf(_("Warning: fragments not supported. "
841 "Ignoring -f option\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000842 break;
843 case 'g':
844 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000845 if (*tmp) {
846 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000847 _("Illegal number for blocks per group"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000848 exit(1);
849 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000850 if ((param.s_blocks_per_group % 8) != 0) {
851 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000852 _("blocks per group must be multiple of 8"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000853 exit(1);
854 }
855 break;
856 case 'i':
857 inode_ratio = strtoul(optarg, &tmp, 0);
Theodore Ts'o44c09c02001-01-14 17:02:09 +0000858 if (inode_ratio < 1024 || inode_ratio > 4096 * 1024 ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000859 *tmp) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000860 com_err(program_name, 0,
861 _("bad inode ratio - %s"), optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000862 exit(1);
863 }
864 break;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000865 case 'J':
866 parse_journal_opts(optarg);
867 break;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +0000868 case 'j':
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000869 if (!journal_size)
870 journal_size = -1;
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000871 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000872 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000873 bad_blocks_filename = malloc(strlen(optarg)+1);
874 if (!bad_blocks_filename) {
875 com_err(program_name, ENOMEM,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000876 _("in malloc for bad_blocks_filename"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000877 exit(1);
878 }
879 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000880 break;
881 case 'm':
882 reserved_ratio = strtoul(optarg, &tmp, 0);
883 if (reserved_ratio > 50 || *tmp) {
884 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000885 _("bad reserved blocks percent - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000886 optarg);
887 exit(1);
888 }
889 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000890 case 'n':
891 noaction++;
892 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000893 case 'o':
894 creator_os = optarg;
895 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000896 case 'r':
897 param.s_rev_level = atoi(optarg);
898 break;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000899 case 's':
900 sparse_option = atoi(optarg);
901 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000902#ifdef EXT2_DYNAMIC_REV
903 case 'I':
904 param.s_inode_size = atoi(optarg);
905 break;
906#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000907 case 'N':
908 num_inodes = atoi(optarg);
909 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000910 case 'v':
911 verbose = 1;
912 break;
913 case 'q':
914 quiet = 1;
915 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +0000916 case 'F':
917 force = 1;
918 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000919 case 'L':
920 volume_label = optarg;
921 break;
922 case 'M':
923 mount_dir = optarg;
924 break;
Theodore Ts'o896938d1999-10-23 01:04:50 +0000925 case 'O':
926 feature_set = optarg;
927 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000928 case 'R':
929 raid_opts = optarg;
930 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000931 case 'S':
932 super_only = 1;
933 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000934 case 'T':
935 fs_type = optarg;
936 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000937 case 'V':
938 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000939 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o818180c1998-06-27 05:11:14 +0000940 error_message(EXT2_ET_BASE));
941 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000942 default:
943 usage();
944 }
945 if (optind == argc)
946 usage();
947 device_name = argv[optind];
948 optind++;
949 if (optind < argc) {
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000950 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
951
952 if ((*tmp) || (tmp2 > 0xfffffffful)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000953 com_err(program_name, 0, _("bad blocks count - %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000954 argv[optind - 1]);
955 exit(1);
956 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000957 param.s_blocks_count = tmp2;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000958 }
959 if (optind < argc)
960 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000961
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000962 if (raid_opts)
963 parse_raid_opts(raid_opts);
964
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000965 /* Parse the user-supplied feature_set, if any. */
966 if (feature_set && !strncasecmp(feature_set, "none", 4))
967 feature_set = NULL;
968 if (feature_set && e2p_edit_feature(feature_set,
969 &param.s_feature_compat,
970 ok_features)) {
971 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
972 feature_set);
973 exit(1);
974 }
975
Theodore Ts'o74becf31997-04-26 14:37:06 +0000976 if (!force)
Theodore Ts'o8ddaa662000-11-17 04:55:24 +0000977 check_plausibility(device_name);
Theodore Ts'o63985322001-01-03 17:02:13 +0000978 check_mount(device_name, force, _("filesystem"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000979
Theodore Ts'o3839e651997-04-26 13:21:57 +0000980 param.s_log_frag_size = param.s_log_block_size;
981
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000982 if (noaction && param.s_blocks_count) {
983 dev_size = param.s_blocks_count;
984 retval = 0;
985 } else
986 retval = ext2fs_get_device_size(device_name,
987 EXT2_BLOCK_SIZE(&param),
988 &dev_size);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000989 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
990 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000991 _("while trying to determine filesystem size"));
Theodore Ts'oa789d841998-03-30 01:20:55 +0000992 exit(1);
993 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000994 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +0000995 if (retval == EXT2_ET_UNIMPLEMENTED) {
996 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000997 _("Couldn't determine device size; you "
Theodore Ts'oa789d841998-03-30 01:20:55 +0000998 "must specify\nthe size of the "
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000999 "filesystem\n"));
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001000 exit(1);
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001001 } else {
1002 if (dev_size == 0) {
1003 com_err(program_name, 0,
1004 _("Device size reported to be zero. "
1005 "Invalid partition specified, or\n\t"
1006 "partition table wasn't reread "
1007 "after running fdisk, due to\n\t"
1008 "a modified partition being busy "
1009 "and in use. You may need to reboot\n\t"
1010 "to re-read your partition table.\n"
1011 ));
1012 exit(1);
1013 }
Theodore Ts'oa789d841998-03-30 01:20:55 +00001014 param.s_blocks_count = dev_size;
Theodore Ts'o26ab5312000-05-29 15:05:42 +00001015 }
1016
Theodore Ts'oa789d841998-03-30 01:20:55 +00001017 } else if (!force && (param.s_blocks_count > dev_size)) {
1018 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001019 _("Filesystem larger than apparent filesystem size."));
Theodore Ts'oa789d841998-03-30 01:20:55 +00001020 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001021 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001022
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001023 /*
1024 * If the user asked for HAS_JOURNAL, then make sure a journal
1025 * gets created.
1026 */
1027 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1028 !journal_size)
1029 journal_size = -1;
1030 if (!fs_type &&
1031 (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
1032 fs_type = "journal";
1033 set_fs_defaults(fs_type, &param, blocksize, &inode_ratio);
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001034
Theodore Ts'o521e3681997-04-29 17:48:10 +00001035 if (param.s_blocks_per_group) {
1036 if (param.s_blocks_per_group < 256 ||
Theodore Ts'o80c22c92000-08-14 15:32:11 +00001037 param.s_blocks_per_group > group_blk_max || *tmp) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001038 com_err(program_name, 0,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001039 _("blocks per group count out of range"));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001040 exit(1);
1041 }
1042 }
1043
Theodore Ts'o3839e651997-04-26 13:21:57 +00001044 /*
1045 * Calculate number of inodes based on the inode ratio
1046 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +00001047 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'oe6597041999-10-26 02:30:16 +00001048 ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
Theodore Ts'of3db3561997-04-26 13:34:30 +00001049 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001050
1051 /*
1052 * Calculate number of blocks to reserve
1053 */
1054 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +00001055
Theodore Ts'of6f65832000-10-25 03:01:37 +00001056 /* Turn off features not supported by the earlier filesystem version */
1057 if (param.s_rev_level == 0) {
1058 sparse_option = 0;
1059 feature_set = NULL;
1060 }
Theodore Ts'o27401561999-09-14 20:11:19 +00001061 if (sparse_option)
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001062 param.s_feature_ro_compat |=
Theodore Ts'o521e3681997-04-29 17:48:10 +00001063 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'of6f65832000-10-25 03:01:37 +00001064
Theodore Ts'o3839e651997-04-26 13:21:57 +00001065}
1066
1067int main (int argc, char *argv[])
1068{
1069 errcode_t retval = 0;
1070 ext2_filsys fs;
1071 badblocks_list bb_list = 0;
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001072 int journal_blocks;
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001073 int i, val;
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001074
1075#ifdef ENABLE_NLS
1076 setlocale(LC_MESSAGES, "");
1077 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1078 textdomain(NLS_CAT_NAME);
1079#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +00001080 PRS(argc, argv);
1081
Theodore Ts'o3839e651997-04-26 13:21:57 +00001082 /*
1083 * Initialize the superblock....
1084 */
1085 retval = ext2fs_initialize(device_name, 0, &param,
1086 unix_io_manager, &fs);
1087 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001088 com_err(device_name, retval, _("while setting up superblock"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001089 exit(1);
1090 }
1091
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001092 /*
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001093 * Wipe out the old on-disk superblock
1094 */
1095 zap_sector(fs, 2);
1096
1097 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001098 * Generate a UUID for it...
1099 */
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001100 uuid_generate(fs->super->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001101
1102 /*
Theodore Ts'o44c09c02001-01-14 17:02:09 +00001103 * Add "jitter" to the superblock's check interval so that we
1104 * don't check all the filesystems at the same time. We use a
1105 * kludgy hack of using the UUID to derive a random jitter value.
1106 */
1107 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1108 val += fs->super->s_uuid[i];
1109 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1110
1111 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001112 * Override the creator OS, if applicable
1113 */
1114 if (creator_os && !set_os(fs->super, creator_os)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001115 com_err (program_name, 0, _("unknown os - %s"), creator_os);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001116 exit(1);
1117 }
1118
1119 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +00001120 * For the Hurd, we will turn off filetype since it doesn't
1121 * support it.
1122 */
1123 if (fs->super->s_creator_os == EXT2_OS_HURD)
1124 fs->super->s_feature_incompat &=
1125 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1126
1127 /*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001128 * Set the volume label...
1129 */
1130 if (volume_label) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001131 memset(fs->super->s_volume_name, 0,
1132 sizeof(fs->super->s_volume_name));
1133 strncpy(fs->super->s_volume_name, volume_label,
1134 sizeof(fs->super->s_volume_name));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001135 }
1136
1137 /*
1138 * Set the last mount directory
1139 */
1140 if (mount_dir) {
Theodore Ts'oef9abe52001-01-01 15:31:53 +00001141 memset(fs->super->s_last_mounted, 0,
1142 sizeof(fs->super->s_last_mounted));
1143 strncpy(fs->super->s_last_mounted, mount_dir,
1144 sizeof(fs->super->s_last_mounted));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001145 }
1146
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001147 if (!quiet || noaction)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001148 show_stats(fs);
1149
Theodore Ts'o50787ea1999-07-19 15:30:21 +00001150 if (noaction)
1151 exit(0);
1152
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001153 if (fs->super->s_feature_incompat &
1154 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1155 create_journal_dev(fs);
1156 ext2fs_close(fs);
1157 exit(0);
1158 }
1159
Theodore Ts'o3839e651997-04-26 13:21:57 +00001160 if (bad_blocks_filename)
1161 read_bb_file(fs, &bb_list, bad_blocks_filename);
1162 if (cflag)
1163 test_disk(fs, &bb_list);
1164
1165 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +00001166 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001167 retval = ext2fs_allocate_tables(fs);
1168 if (retval) {
1169 com_err(program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001170 _("while trying to allocate filesystem tables"));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001171 exit(1);
1172 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001173 if (super_only) {
1174 fs->super->s_state |= EXT2_ERROR_FS;
1175 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1176 } else {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001177 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001178 create_root_dir(fs);
1179 create_lost_and_found(fs);
1180 reserve_inodes(fs);
1181 create_bad_block_inode(fs, bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001182#ifdef ZAP_BOOTBLOCK
Theodore Ts'oe41784e2000-08-14 14:44:15 +00001183 zap_sector(fs, 0);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001184#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +00001185 }
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001186
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001187 if (journal_device) {
1188 ext2_filsys jfs;
1189
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001190 if (!force)
1191 check_plausibility(journal_device);
Theodore Ts'o63985322001-01-03 17:02:13 +00001192 check_mount(journal_device, force, _("journal"));
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001193
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001194 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1195 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1196 fs->blocksize, unix_io_manager, &jfs);
1197 if (retval) {
1198 com_err(program_name, retval,
1199 _("while trying to open journal device %s\n"),
1200 journal_device);
1201 exit(1);
1202 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001203 if (!quiet) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001204 printf(_("Creating journal on device %s: "),
1205 journal_device);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001206 fflush(stdout);
1207 }
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001208 retval = ext2fs_add_journal_device(fs, jfs);
1209 if(retval) {
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001210 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001211 _("\n\twhile trying to add journal to device %s"),
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001212 journal_device);
1213 exit(1);
1214 }
1215 if (!quiet)
1216 printf(_("done\n"));
Theodore Ts'o16ed5b32001-01-16 07:47:31 +00001217 ext2fs_close(jfs);
Theodore Ts'o8ddaa662000-11-17 04:55:24 +00001218 } else if (journal_size) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001219 journal_blocks = figure_journal_size(journal_size, fs);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001220
1221 if (!journal_blocks) {
1222 fs->super->s_feature_compat &=
1223 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1224 goto no_journal;
1225 }
1226 if (!quiet) {
Theodore Ts'o16ad3332000-12-31 03:21:56 +00001227 printf(_("Creating journal (%d blocks): "),
1228 journal_blocks);
Theodore Ts'o93345d12001-02-17 06:09:50 +00001229 fflush(stdout);
1230 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001231 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1232 journal_flags);
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001233 if (retval) {
1234 com_err (program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +00001235 _("\n\twhile trying to create journal"));
Theodore Ts'o85ef4ae2000-10-24 19:33:45 +00001236 exit(1);
1237 }
1238 if (!quiet)
1239 printf(_("done\n"));
1240 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001241no_journal:
1242
Theodore Ts'o3839e651997-04-26 13:21:57 +00001243 if (!quiet)
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001244 printf(_("Writing superblocks and "
1245 "filesystem accounting information: "));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001246 retval = ext2fs_flush(fs);
1247 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001248 printf(_("\nWarning, had trouble writing out superblocks."));
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001249 }
Theodore Ts'o93345d12001-02-17 06:09:50 +00001250 if (!quiet) {
Theodore Ts'o2537b6d2001-03-26 20:07:13 +00001251 printf(_("done\n\n"));
Theodore Ts'o2103ea72001-04-21 20:53:43 +00001252 printf(_("This filesystem will be automatically "
1253 "checked every %d mounts or\n"
1254 "%g days, whichever comes first. "
1255 "Use tune2fs -c or -i to override.\n"),
Theodore Ts'o93345d12001-02-17 06:09:50 +00001256 fs->super->s_max_mnt_count,
1257 (double)fs->super->s_checkinterval / (3600 * 24));
1258 }
Theodore Ts'o5d45d801999-03-16 19:35:19 +00001259 ext2fs_close(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001260 return 0;
1261}