blob: 1eb7a6e79dec11716385ce266dfe21f4c5eb9183 [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'o3839e651997-04-26 13:21:57 +000077char *bad_blocks_filename = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000078__u32 fs_stride = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000079
80struct ext2_super_block param;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000081char *creator_os = NULL;
82char *volume_label = NULL;
83char *mount_dir = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000084
Theodore Ts'obbfa3aa1998-03-21 07:12:46 +000085static void usage(NOARGS), check_plausibility(NOARGS), check_mount(NOARGS);
86
Theodore Ts'o3839e651997-04-26 13:21:57 +000087static void usage(NOARGS)
88{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000089 fprintf(stderr, "Usage: %s [-c|-t|-l filename] [-b block-size] "
90 "[-f fragment-size]\n\t[-i bytes-per-inode] "
Theodore Ts'o5515e6b1999-01-05 07:25:06 +000091 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
92 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
93 "[-M last-mounted-directory] [-r fs-revision]\n\t[-R raid_opts]"
94 "[-s sparse-super-flag] [-qvSV] device [blocks-count]\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +000095 program_name);
96 exit(1);
97}
98
99static int log2(int arg)
100{
101 int l = 0;
102
103 arg >>= 1;
104 while (arg) {
105 l++;
106 arg >>= 1;
107 }
108 return l;
109}
110
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000111static int log10(unsigned int arg)
112{
113 int l;
114
115 for (l=0; arg ; l++)
116 arg = arg / 10;
117 return l;
118}
119
Theodore Ts'oa789d841998-03-30 01:20:55 +0000120static void proceed_question(NOARGS)
121{
122 fflush(stdout);
123 fflush(stderr);
124 printf("Proceed anyway? (y,n) ");
125 if (getchar() != 'y')
126 exit(1);
127}
128
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000129#ifndef SCSI_BLK_MAJOR
130#define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
131#endif
132
Theodore Ts'o74becf31997-04-26 14:37:06 +0000133static void check_plausibility(NOARGS)
134{
135#ifdef HAVE_LINUX_MAJOR_H
136 int val;
137 struct stat s;
138
139 val = stat(device_name, &s);
140
141 if(val == -1) {
Theodore Ts'oa789d841998-03-30 01:20:55 +0000142 fprintf(stderr, "Could not stat %s --- %s\n",
143 device_name, error_message(errno));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000144 if (errno == ENOENT)
Theodore Ts'oa789d841998-03-30 01:20:55 +0000145 fprintf(stderr, "\nThe device apparently does "
146 "not exist; did you specify it correctly?\n");
Theodore Ts'o74becf31997-04-26 14:37:06 +0000147 exit(1);
148 }
149 if(!S_ISBLK(s.st_mode)) {
150 printf("%s is not a block special device.\n", device_name);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000151 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000152 return;
Theodore Ts'oa789d841998-03-30 01:20:55 +0000153 } else if ((MAJOR(s.st_rdev) == HD_MAJOR &&
154 MINOR(s.st_rdev)%64 == 0) ||
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000155 (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
156 MINOR(s.st_rdev)%16 == 0)) {
Theodore Ts'o74becf31997-04-26 14:37:06 +0000157 printf("%s is entire device, not just one partition!\n",
158 device_name);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000159 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000160 }
161#endif
162}
163
Theodore Ts'o3839e651997-04-26 13:21:57 +0000164static void check_mount(NOARGS)
165{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000166 errcode_t retval;
167 int mount_flags;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000168
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000169 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
170 if (retval) {
171 com_err("ext2fs_check_if_mount", retval,
172 "while determining whether %s is mounted.",
173 device_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000174 return;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000175 }
176 if (!(mount_flags & EXT2_MF_MOUNTED))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000177 return;
Theodore Ts'oca3c3281999-06-29 14:37:35 +0000178
179 fprintf(stderr, "%s is mounted; ", device_name);
180 if (force) {
181 fprintf(stderr, "mke2fs forced anyway. "
182 "Hope /etc/mtab is incorrect.\n");
183 } else {
184 fprintf(stderr, "will not make a filesystem here!\n");
185 exit(1);
186 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000187}
188
189/*
190 * Helper function for read_bb_file and test_disk
191 */
192static void invalid_block(ext2_filsys fs, blk_t blk)
193{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000194 printf("Bad block %u out of range; ignored.\n", blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000195 return;
196}
197
198/*
199 * Reads the bad blocks list from a file
200 */
201static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
202 const char *bad_blocks_file)
203{
204 FILE *f;
205 errcode_t retval;
206
207 f = fopen(bad_blocks_file, "r");
208 if (!f) {
209 com_err("read_bad_blocks_file", errno,
210 "while trying to open %s", bad_blocks_file);
211 exit(1);
212 }
213 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
214 fclose (f);
215 if (retval) {
216 com_err("ext2fs_read_bb_FILE", retval,
217 "while reading in list of bad blocks from file");
218 exit(1);
219 }
220}
221
222/*
223 * Runs the badblocks program to test the disk
224 */
225static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
226{
227 FILE *f;
228 errcode_t retval;
229 char buf[1024];
230
Theodore Ts'of635d7f1997-05-09 02:50:16 +0000231 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
232 quiet ? "" : "-s ", fs->device_name,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000233 fs->super->s_blocks_count);
234 if (verbose)
235 printf("Running command: %s\n", buf);
236 f = popen(buf, "r");
237 if (!f) {
238 com_err("popen", errno,
239 "while trying run '%s'", buf);
240 exit(1);
241 }
242 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000243 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000244 if (retval) {
245 com_err("ext2fs_read_bb_FILE", retval,
246 "while processing list of bad blocks from program");
247 exit(1);
248 }
249}
250
251static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
252{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000253 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000254 int must_be_good;
255 blk_t blk;
256 badblocks_iterate bb_iter;
257 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000258 blk_t group_block;
259 int group;
260 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000261
262 if (!bb_list)
263 return;
264
265 /*
266 * The primary superblock and group descriptors *must* be
267 * good; if not, abort.
268 */
269 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
270 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
271 if (badblocks_list_test(bb_list, i)) {
272 fprintf(stderr, "Block %d in primary superblock/group "
273 "descriptor area bad.\n", i);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000274 fprintf(stderr, "Blocks %d through %d must be good "
Theodore Ts'o3839e651997-04-26 13:21:57 +0000275 "in order to build a filesystem.\n",
276 fs->super->s_first_data_block, must_be_good);
277 fprintf(stderr, "Aborting....\n");
278 exit(1);
279 }
280 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000281
282 /*
283 * See if any of the bad blocks are showing up in the backup
284 * superblocks and/or group descriptors. If so, issue a
285 * warning and adjust the block counts appropriately.
286 */
287 group_block = fs->super->s_first_data_block +
288 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000289
290 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000291 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000292 for (j=0; j < fs->desc_blocks+1; j++) {
293 if (badblocks_list_test(bb_list, group_block +
294 j)) {
295 if (!group_bad)
296 fprintf(stderr,
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000297"Warning: the backup superblock/group descriptors at block %d contain\n"
Theodore Ts'of3db3561997-04-26 13:34:30 +0000298" bad blocks.\n\n",
299 group_block);
300 group_bad++;
301 group = ext2fs_group_of_blk(fs, group_block+j);
302 fs->group_desc[group].bg_free_blocks_count++;
303 fs->super->s_free_blocks_count++;
304 }
305 }
306 group_block += fs->super->s_blocks_per_group;
307 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000308
309 /*
310 * Mark all the bad blocks as used...
311 */
312 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
313 if (retval) {
314 com_err("badblocks_list_iterate_begin", retval,
315 "while marking bad blocks as used");
316 exit(1);
317 }
318 while (badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000319 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000320 badblocks_list_iterate_end(bb_iter);
321}
322
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000323static void write_inode_tables(ext2_filsys fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000324{
325 errcode_t retval;
326 blk_t blk;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000327 int i, j, num, count;
328 char *buf;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000329 char format[20], backup[80];
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000330 int sync_kludge = 0;
331 char *mke2fs_sync;
332
333 mke2fs_sync = getenv("MKE2FS_SYNC");
334 if (mke2fs_sync)
335 sync_kludge = atoi(mke2fs_sync);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000336
337 buf = malloc(fs->blocksize * STRIDE_LENGTH);
338 if (!buf) {
339 com_err("malloc", ENOMEM, "while allocating zeroizing buffer");
340 exit(1);
341 }
342 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000343
344 /*
345 * Figure out how many digits we need
346 */
347 i = log10(fs->group_desc_count);
348 sprintf(format, "%%%dd/%%%dld", i, i);
349 memset(backup, '\b', sizeof(backup)-1);
350 backup[sizeof(backup)-1] = 0;
351 if ((2*i)+1 < sizeof(backup))
352 backup[(2*i)+1] = 0;
353
Theodore Ts'o3839e651997-04-26 13:21:57 +0000354 if (!quiet)
355 printf("Writing inode tables: ");
356 for (i = 0; i < fs->group_desc_count; i++) {
357 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000358 printf(format, i, fs->group_desc_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000359
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000360 blk = fs->group_desc[i].bg_inode_table;
361 num = fs->inode_blocks_per_group;
362
363 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
364 if (num-j > STRIDE_LENGTH)
365 count = STRIDE_LENGTH;
366 else
367 count = num - j;
368 retval = io_channel_write_blk(fs->io, blk, count, buf);
369 if (retval)
370 printf("Warning: could not write %d blocks "
371 "in inode table starting at %d: %s\n",
372 count, blk, error_message(retval));
373 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000374 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000375 fputs(backup, stdout);
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000376 if (sync_kludge) {
377 if (sync_kludge == 1)
378 sync();
379 else if ((i % sync_kludge) == 0)
380 sync();
381 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000382 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000383 free(buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000384 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000385 fputs("done \n", stdout);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000386}
387
388static void create_root_dir(ext2_filsys fs)
389{
390 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000391 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000392
393 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
394 if (retval) {
395 com_err("ext2fs_mkdir", retval, "while creating root dir");
396 exit(1);
397 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000398 if (geteuid()) {
399 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
400 if (retval) {
401 com_err("ext2fs_read_inode", retval,
402 "while reading root inode");
403 exit(1);
404 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000405 inode.i_uid = getuid();
406 if (inode.i_uid)
407 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000408 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
409 if (retval) {
410 com_err("ext2fs_write_inode", retval,
411 "while setting root inode ownership");
412 exit(1);
413 }
414 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000415}
416
417static void create_lost_and_found(ext2_filsys fs)
418{
419 errcode_t retval;
420 ino_t ino;
421 const char *name = "lost+found";
422 int i;
423
424 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
425 if (retval) {
426 com_err("ext2fs_mkdir", retval, "while creating /lost+found");
427 exit(1);
428 }
429
430 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
431 if (retval) {
432 com_err("ext2_lookup", retval, "while looking up /lost+found");
433 exit(1);
434 }
435
436 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
437 retval = ext2fs_expand_dir(fs, ino);
438 if (retval) {
439 com_err("ext2fs_expand_dir", retval,
440 "while expanding /lost+found");
441 exit(1);
442 }
443 }
444}
445
446static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
447{
448 errcode_t retval;
449
Theodore Ts'of3db3561997-04-26 13:34:30 +0000450 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000451 fs->group_desc[0].bg_free_inodes_count--;
452 fs->super->s_free_inodes_count--;
453 retval = ext2fs_update_bb_inode(fs, bb_list);
454 if (retval) {
455 com_err("ext2fs_update_bb_inode", retval,
456 "while setting bad block inode");
457 exit(1);
458 }
459
460}
461
462static void reserve_inodes(ext2_filsys fs)
463{
464 ino_t i;
465 int group;
466
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000467 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000468 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000469 group = ext2fs_group_of_ino(fs, i);
470 fs->group_desc[group].bg_free_inodes_count--;
471 fs->super->s_free_inodes_count--;
472 }
473 ext2fs_mark_ib_dirty(fs);
474}
475
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000476#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +0000477static void zap_bootblock(ext2_filsys fs)
478{
479 char buf[512];
480 int retval;
481
482 memset(buf, 0, 512);
483
484 retval = io_channel_write_blk(fs->io, 0, -512, buf);
485 if (retval)
486 printf("Warning: could not erase block 0: %s\n",
487 error_message(retval));
488}
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000489#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +0000490
491
Theodore Ts'o3839e651997-04-26 13:21:57 +0000492static void show_stats(ext2_filsys fs)
493{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000494 struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super;
495 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000496 blk_t group_block;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000497 int i, need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000498
499 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000500 printf("warning: %d blocks unused.\n\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000501 param.s_blocks_count - s->s_blocks_count);
502
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000503 switch (fs->super->s_creator_os) {
504 case EXT2_OS_LINUX: printf ("Linux"); break;
505 case EXT2_OS_HURD: printf ("GNU/hurd"); break;
506 case EXT2_OS_MASIX: printf ("Masix"); break;
507 default: printf ("(unknown os)");
508 }
509 printf (" ext2 filesystem format\n");
510 memset(buf, 0, sizeof(buf));
511 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
512 printf("Filesystem label=%s\n", buf);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000513 printf("%u inodes, %u blocks\n", s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000514 s->s_blocks_count);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000515 printf("%u blocks (%2.2f%%) reserved for the super user\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000516 s->s_r_blocks_count,
517 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000518 printf("First data block=%u\n", s->s_first_data_block);
519 printf("Block size=%u (log=%u)\n", fs->blocksize,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000520 s->s_log_block_size);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000521 printf("Fragment size=%u (log=%u)\n", fs->fragsize,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000522 s->s_log_frag_size);
523 printf("%lu block group%s\n", fs->group_desc_count,
524 (fs->group_desc_count > 1) ? "s" : "");
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000525 printf("%u blocks per group, %u fragments per group\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000526 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000527 printf("%u inodes per group\n", s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000528
529 if (fs->group_desc_count == 1) {
530 printf("\n");
531 return;
532 }
533
534 printf("Superblock backups stored on blocks: ");
535 group_block = s->s_first_data_block;
536 col_left = 0;
537 for (i = 1; i < fs->group_desc_count; i++) {
538 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000539 if (!ext2fs_bg_has_super(fs, i))
540 continue;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000541 need = log10(group_block) + 2;
542 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000543 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000544 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000545 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000546 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000547 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000548 if (i != fs->group_desc_count - 1)
549 printf(", ");
550 }
551 printf("\n\n");
552}
553
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000554#ifndef HAVE_STRCASECMP
555static int strcasecmp (char *s1, char *s2)
556{
557 while (*s1 && *s2) {
558 int ch1 = *s1++, ch2 = *s2++;
559 if (isupper (ch1))
560 ch1 = tolower (ch1);
561 if (isupper (ch2))
562 ch2 = tolower (ch2);
563 if (ch1 != ch2)
564 return ch1 - ch2;
565 }
566 return *s1 ? 1 : *s2 ? -1 : 0;
567}
568#endif
569
570/*
571 * Set the S_CREATOR_OS field. Return true if OS is known,
572 * otherwise, 0.
573 */
574static int set_os(struct ext2_super_block *sb, char *os)
575{
576 if (isdigit (*os))
577 sb->s_creator_os = atoi (os);
578 else if (strcasecmp(os, "linux") == 0)
579 sb->s_creator_os = EXT2_OS_LINUX;
580 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
581 sb->s_creator_os = EXT2_OS_HURD;
582 else if (strcasecmp(os, "masix") == 0)
583 sb->s_creator_os = EXT2_OS_MASIX;
584 else
585 return 0;
586 return 1;
587}
588
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000589#define PATH_SET "PATH=/sbin"
590
Theodore Ts'od163b091997-10-03 17:42:28 +0000591static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000592{
593 char *buf, *token, *next, *p, *arg;
594 int len;
595 int raid_usage = 0;
596
597 len = strlen(opts);
598 buf = malloc(len+1);
599 if (!buf) {
600 fprintf(stderr, "Couldn't allocate memory to parse "
601 "raid options!\n");
602 exit(1);
603 }
604 strcpy(buf, opts);
605 for (token = buf; token && *token; token = next) {
606 p = strchr(token, ',');
607 next = 0;
608 if (p) {
609 *p = 0;
610 next = p+1;
611 }
612 arg = strchr(token, '=');
613 if (arg) {
614 *arg = 0;
615 arg++;
616 }
617 if (strcmp(token, "stride") == 0) {
618 if (!arg) {
619 raid_usage++;
620 continue;
621 }
622 fs_stride = strtoul(arg, &p, 0);
623 if (*p || (fs_stride == 0)) {
624 fprintf(stderr, "Invalid stride parameter.\n");
625 raid_usage++;
626 continue;
627 }
628 } else
629 raid_usage++;
630 }
631 if (raid_usage) {
632 fprintf(stderr, "\nBad raid options specified.\n\n"
633 "Raid options are separated by commas, "
634 "and may take an argument which\n"
635 "\tis set off by an equals ('=') sign.\n\n"
636 "Valid raid options are:\n"
637 "\tstride=<stride length in blocks>\n\n");
638 exit(1);
639 }
640}
641
642
643
Theodore Ts'o3839e651997-04-26 13:21:57 +0000644static void PRS(int argc, char *argv[])
645{
Theodore Ts'o519149f1997-10-25 03:49:49 +0000646 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000647 int size;
648 char * tmp;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000649 blk_t max = 8192;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000650 int inode_ratio = 4096;
651 int reserved_ratio = 5;
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000652 ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000653 errcode_t retval;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000654 int sparse_option = -1;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000655 char *oldpath = getenv("PATH");
Theodore Ts'o521e3681997-04-29 17:48:10 +0000656 struct ext2fs_sb *param_ext2 = (struct ext2fs_sb *) &param;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000657 char *raid_opts = 0;
Theodore Ts'oa789d841998-03-30 01:20:55 +0000658 blk_t dev_size;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000659
Theodore Ts'o3839e651997-04-26 13:21:57 +0000660 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000661 if (oldpath) {
662 char *newpath;
663
664 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
665 strcpy (newpath, PATH_SET);
666 strcat (newpath, ":");
667 strcat (newpath, oldpath);
668 putenv (newpath);
669 } else
670 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000671
672 setbuf(stdout, NULL);
673 setbuf(stderr, NULL);
674 initialize_ext2_error_table();
675 memset(&param, 0, sizeof(struct ext2_super_block));
676
677 fprintf (stderr, "mke2fs %s, %s for EXT2 FS %s, %s\n",
678 E2FSPROGS_VERSION, E2FSPROGS_DATE,
679 EXT2FS_VERSION, EXT2FS_DATE);
680 if (argc && *argv)
681 program_name = *argv;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000682 while ((c = getopt (argc, argv,
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000683 "b:cf:g:i:l:m:o:qr:R:s:tvI:SFL:M:N:V")) != EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000684 switch (c) {
685 case 'b':
686 size = strtoul(optarg, &tmp, 0);
687 if (size < 1024 || size > 4096 || *tmp) {
688 com_err(program_name, 0, "bad block size - %s",
689 optarg);
690 exit(1);
691 }
692 param.s_log_block_size =
693 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o521e3681997-04-29 17:48:10 +0000694 max = size * 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000695 break;
696 case 'c':
697 case 't': /* Check for bad blocks */
698 cflag = 1;
699 break;
700 case 'f':
701 size = strtoul(optarg, &tmp, 0);
702 if (size < 1024 || size > 4096 || *tmp) {
703 com_err(program_name, 0, "bad fragment size - %s",
704 optarg);
705 exit(1);
706 }
707 param.s_log_frag_size =
708 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
709 printf("Warning: fragments not supported. "
710 "Ignoring -f option\n");
711 break;
712 case 'g':
713 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000714 if (*tmp) {
715 com_err(program_name, 0,
716 "Illegal number for blocks per group");
717 exit(1);
718 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000719 if ((param.s_blocks_per_group % 8) != 0) {
720 com_err(program_name, 0,
721 "blocks per group must be multiple of 8");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000722 exit(1);
723 }
724 break;
725 case 'i':
726 inode_ratio = strtoul(optarg, &tmp, 0);
727 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
728 *tmp) {
729 com_err(program_name, 0, "bad inode ratio - %s",
730 optarg);
731 exit(1);
732 }
733 break;
734 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000735 bad_blocks_filename = malloc(strlen(optarg)+1);
736 if (!bad_blocks_filename) {
737 com_err(program_name, ENOMEM,
738 "in malloc for bad_blocks_filename");
739 exit(1);
740 }
741 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000742 break;
743 case 'm':
744 reserved_ratio = strtoul(optarg, &tmp, 0);
745 if (reserved_ratio > 50 || *tmp) {
746 com_err(program_name, 0,
747 "bad reserved blocks percent - %s",
748 optarg);
749 exit(1);
750 }
751 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000752 case 'o':
753 creator_os = optarg;
754 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000755 case 'r':
756 param.s_rev_level = atoi(optarg);
757 break;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000758 case 's':
759 sparse_option = atoi(optarg);
760 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000761#ifdef EXT2_DYNAMIC_REV
762 case 'I':
763 param.s_inode_size = atoi(optarg);
764 break;
765#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000766 case 'N':
767 num_inodes = atoi(optarg);
768 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000769 case 'v':
770 verbose = 1;
771 break;
772 case 'q':
773 quiet = 1;
774 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +0000775 case 'F':
776 force = 1;
777 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000778 case 'L':
779 volume_label = optarg;
780 break;
781 case 'M':
782 mount_dir = optarg;
783 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000784 case 'R':
785 raid_opts = optarg;
786 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000787 case 'S':
788 super_only = 1;
789 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000790 case 'V':
791 /* Print version number and exit */
792 fprintf(stderr, "\tUsing %s\n",
793 error_message(EXT2_ET_BASE));
794 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000795 default:
796 usage();
797 }
798 if (optind == argc)
799 usage();
800 device_name = argv[optind];
801 optind++;
802 if (optind < argc) {
803 param.s_blocks_count = strtoul(argv[optind++], &tmp, 0);
804 if (*tmp) {
805 com_err(program_name, 0, "bad blocks count - %s",
806 argv[optind - 1]);
807 exit(1);
808 }
809 }
810 if (optind < argc)
811 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000812
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000813 if (raid_opts)
814 parse_raid_opts(raid_opts);
815
Theodore Ts'o74becf31997-04-26 14:37:06 +0000816 if (!force)
817 check_plausibility();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000818 check_mount();
819
Theodore Ts'o3839e651997-04-26 13:21:57 +0000820 param.s_log_frag_size = param.s_log_block_size;
821
Theodore Ts'oa789d841998-03-30 01:20:55 +0000822 retval = ext2fs_get_device_size(device_name,
823 EXT2_BLOCK_SIZE(&param),
824 &dev_size);
825 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
826 com_err(program_name, retval,
827 "while trying to determine filesystem size");
828 exit(1);
829 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000830 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +0000831 if (retval == EXT2_ET_UNIMPLEMENTED) {
832 com_err(program_name, 0,
833 "Couldn't determine device size; you "
834 "must specify\nthe size of the "
835 "filesystem\n");
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000836 exit(1);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000837 } else
838 param.s_blocks_count = dev_size;
839 } else if (!force && (param.s_blocks_count > dev_size)) {
840 com_err(program_name, 0,
841 "Filesystem larger than apparent filesystem size.");
842 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000843 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000844
Theodore Ts'o521e3681997-04-29 17:48:10 +0000845 if (param.s_blocks_per_group) {
846 if (param.s_blocks_per_group < 256 ||
847 param.s_blocks_per_group > max || *tmp) {
848 com_err(program_name, 0,
849 "blocks per group count out of range");
850 exit(1);
851 }
852 }
853
Theodore Ts'o3839e651997-04-26 13:21:57 +0000854 /*
855 * Calculate number of inodes based on the inode ratio
856 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000857 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'of3db3561997-04-26 13:34:30 +0000858 ((long long) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
859 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000860
861 /*
862 * Calculate number of blocks to reserve
863 */
864 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000865
866 /*
867 * If we are using revision #1, use the sparse super feature
868 * by default
869 */
870#ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
871 if ((sparse_option == 1)
872#ifdef EXT2_DYNAMIC_REV
Theodore Ts'obbfa3aa1998-03-21 07:12:46 +0000873 || ((param.s_rev_level >= EXT2_DYNAMIC_REV) && (!sparse_option))
Theodore Ts'o521e3681997-04-29 17:48:10 +0000874#endif
875 )
876 param_ext2->s_feature_ro_compat |=
877 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
878#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000879}
880
881int main (int argc, char *argv[])
882{
883 errcode_t retval = 0;
884 ext2_filsys fs;
885 badblocks_list bb_list = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000886 struct ext2fs_sb *s;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000887
888 PRS(argc, argv);
889
Theodore Ts'o3839e651997-04-26 13:21:57 +0000890 /*
891 * Initialize the superblock....
892 */
893 retval = ext2fs_initialize(device_name, 0, &param,
894 unix_io_manager, &fs);
895 if (retval) {
896 com_err(device_name, retval, "while setting up superblock");
897 exit(1);
898 }
899
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000900 /*
901 * Generate a UUID for it...
902 */
903 s = (struct ext2fs_sb *) fs->super;
904 uuid_generate(s->s_uuid);
905
906 /*
907 * Override the creator OS, if applicable
908 */
909 if (creator_os && !set_os(fs->super, creator_os)) {
910 com_err (program_name, 0, "unknown os - %s", creator_os);
911 exit(1);
912 }
913
914 /*
915 * Set the volume label...
916 */
917 if (volume_label) {
918 memset(s->s_volume_name, 0, sizeof(s->s_volume_name));
919 strncpy(s->s_volume_name, volume_label,
920 sizeof(s->s_volume_name));
921 }
922
923 /*
924 * Set the last mount directory
925 */
926 if (mount_dir) {
927 memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted));
928 strncpy(s->s_last_mounted, mount_dir,
929 sizeof(s->s_last_mounted));
930 }
931
Theodore Ts'o3839e651997-04-26 13:21:57 +0000932 if (!quiet)
933 show_stats(fs);
934
935 if (bad_blocks_filename)
936 read_bb_file(fs, &bb_list, bad_blocks_filename);
937 if (cflag)
938 test_disk(fs, &bb_list);
939
940 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000941 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000942 retval = ext2fs_allocate_tables(fs);
943 if (retval) {
944 com_err(program_name, retval,
945 "while trying to allocate filesystem tables");
946 exit(1);
947 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000948 if (super_only) {
949 fs->super->s_state |= EXT2_ERROR_FS;
950 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
951 } else {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000952 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000953 create_root_dir(fs);
954 create_lost_and_found(fs);
955 reserve_inodes(fs);
956 create_bad_block_inode(fs, bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000957#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +0000958 zap_bootblock(fs);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000959#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +0000960 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000961
962 if (!quiet)
963 printf("Writing superblocks and "
964 "filesystem accounting information: ");
Theodore Ts'o5d45d801999-03-16 19:35:19 +0000965 retval = ext2fs_flush(fs);
966 if (retval) {
967 printf("\nWarning, had trouble writing out superblocks.");
968 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000969 if (!quiet)
970 printf("done\n");
Theodore Ts'o5d45d801999-03-16 19:35:19 +0000971 ext2fs_close(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000972 return 0;
973}