blob: 2a2f428bb4b67c7e2d602862519ccf1589965f74 [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] "
91 "[-m reserved-blocks-percentage] [-qvS]\n\t"
92 "[-o creator-os] [-g blocks-per-group] [-L volume-label]\n\t"
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000093 "[-M last-mounted-directory] [-r fs-revision] [-R raid_opts]\n\t"
94 "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'o74becf31997-04-26 14:37:06 +0000111static void check_plausibility(NOARGS)
112{
113#ifdef HAVE_LINUX_MAJOR_H
114 int val;
115 struct stat s;
116
117 val = stat(device_name, &s);
118
119 if(val == -1) {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000120 printf("Could not stat %s --- %s\n", device_name,
121 error_message(errno));
122 if (errno == ENOENT)
123 printf("\nThe device apparently does not exist; "
Theodore Ts'obbfa3aa1998-03-21 07:12:46 +0000124 "did you specify it correctly?\n");
Theodore Ts'o74becf31997-04-26 14:37:06 +0000125 exit(1);
126 }
127 if(!S_ISBLK(s.st_mode)) {
128 printf("%s is not a block special device.\n", device_name);
129 printf("Proceed anyway? (y,n) ");
130 if (getchar() != 'y')
131 exit(1);
132 return;
133 }
134 if ((MAJOR(s.st_rdev) == HD_MAJOR && MINOR(s.st_rdev)%64 == 0) ||
135 (MAJOR(s.st_rdev) == SCSI_DISK_MAJOR &&
136 MINOR(s.st_rdev)%16 == 0)) {
137 printf("%s is entire device, not just one partition!\n",
138 device_name);
139 printf("Proceed anyway? (y,n) ");
140 if (getchar() != 'y')
141 exit(1);
142 return;
143 }
144#endif
145}
146
Theodore Ts'o3839e651997-04-26 13:21:57 +0000147static void check_mount(NOARGS)
148{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000149 errcode_t retval;
150 int mount_flags;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000151
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000152 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
153 if (retval) {
154 com_err("ext2fs_check_if_mount", retval,
155 "while determining whether %s is mounted.",
156 device_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000157 return;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000158 }
159 if (!(mount_flags & EXT2_MF_MOUNTED))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000160 return;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000161
Theodore Ts'o3839e651997-04-26 13:21:57 +0000162 fprintf(stderr, "%s is mounted; will not make a filesystem here!\n",
163 device_name);
164 exit(1);
165}
166
167/*
168 * Helper function for read_bb_file and test_disk
169 */
170static void invalid_block(ext2_filsys fs, blk_t blk)
171{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000172 printf("Bad block %u out of range; ignored.\n", blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000173 return;
174}
175
176/*
177 * Reads the bad blocks list from a file
178 */
179static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
180 const char *bad_blocks_file)
181{
182 FILE *f;
183 errcode_t retval;
184
185 f = fopen(bad_blocks_file, "r");
186 if (!f) {
187 com_err("read_bad_blocks_file", errno,
188 "while trying to open %s", bad_blocks_file);
189 exit(1);
190 }
191 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
192 fclose (f);
193 if (retval) {
194 com_err("ext2fs_read_bb_FILE", retval,
195 "while reading in list of bad blocks from file");
196 exit(1);
197 }
198}
199
200/*
201 * Runs the badblocks program to test the disk
202 */
203static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
204{
205 FILE *f;
206 errcode_t retval;
207 char buf[1024];
208
Theodore Ts'of635d7f1997-05-09 02:50:16 +0000209 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
210 quiet ? "" : "-s ", fs->device_name,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000211 fs->super->s_blocks_count);
212 if (verbose)
213 printf("Running command: %s\n", buf);
214 f = popen(buf, "r");
215 if (!f) {
216 com_err("popen", errno,
217 "while trying run '%s'", buf);
218 exit(1);
219 }
220 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000221 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000222 if (retval) {
223 com_err("ext2fs_read_bb_FILE", retval,
224 "while processing list of bad blocks from program");
225 exit(1);
226 }
227}
228
229static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
230{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000231 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000232 int must_be_good;
233 blk_t blk;
234 badblocks_iterate bb_iter;
235 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000236 blk_t group_block;
237 int group;
238 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000239
240 if (!bb_list)
241 return;
242
243 /*
244 * The primary superblock and group descriptors *must* be
245 * good; if not, abort.
246 */
247 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
248 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
249 if (badblocks_list_test(bb_list, i)) {
250 fprintf(stderr, "Block %d in primary superblock/group "
251 "descriptor area bad.\n", i);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000252 fprintf(stderr, "Blocks %d through %d must be good "
Theodore Ts'o3839e651997-04-26 13:21:57 +0000253 "in order to build a filesystem.\n",
254 fs->super->s_first_data_block, must_be_good);
255 fprintf(stderr, "Aborting....\n");
256 exit(1);
257 }
258 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000259
260 /*
261 * See if any of the bad blocks are showing up in the backup
262 * superblocks and/or group descriptors. If so, issue a
263 * warning and adjust the block counts appropriately.
264 */
265 group_block = fs->super->s_first_data_block +
266 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000267
268 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000269 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000270 for (j=0; j < fs->desc_blocks+1; j++) {
271 if (badblocks_list_test(bb_list, group_block +
272 j)) {
273 if (!group_bad)
274 fprintf(stderr,
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000275"Warning: the backup superblock/group descriptors at block %d contain\n"
Theodore Ts'of3db3561997-04-26 13:34:30 +0000276" bad blocks.\n\n",
277 group_block);
278 group_bad++;
279 group = ext2fs_group_of_blk(fs, group_block+j);
280 fs->group_desc[group].bg_free_blocks_count++;
281 fs->super->s_free_blocks_count++;
282 }
283 }
284 group_block += fs->super->s_blocks_per_group;
285 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000286
287 /*
288 * Mark all the bad blocks as used...
289 */
290 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
291 if (retval) {
292 com_err("badblocks_list_iterate_begin", retval,
293 "while marking bad blocks as used");
294 exit(1);
295 }
296 while (badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000297 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000298 badblocks_list_iterate_end(bb_iter);
299}
300
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000301static void write_inode_tables(ext2_filsys fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000302{
303 errcode_t retval;
304 blk_t blk;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000305 int i, j, num, count;
306 char *buf;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000307
308 buf = malloc(fs->blocksize * STRIDE_LENGTH);
309 if (!buf) {
310 com_err("malloc", ENOMEM, "while allocating zeroizing buffer");
311 exit(1);
312 }
313 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
314
Theodore Ts'o3839e651997-04-26 13:21:57 +0000315 if (!quiet)
316 printf("Writing inode tables: ");
317 for (i = 0; i < fs->group_desc_count; i++) {
318 if (!quiet)
319 printf("%4d/%4ld", i, fs->group_desc_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000320
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000321 blk = fs->group_desc[i].bg_inode_table;
322 num = fs->inode_blocks_per_group;
323
324 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
325 if (num-j > STRIDE_LENGTH)
326 count = STRIDE_LENGTH;
327 else
328 count = num - j;
329 retval = io_channel_write_blk(fs->io, blk, count, buf);
330 if (retval)
331 printf("Warning: could not write %d blocks "
332 "in inode table starting at %d: %s\n",
333 count, blk, error_message(retval));
334 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000335 if (!quiet)
336 printf("\b\b\b\b\b\b\b\b\b");
337 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000338 free(buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000339 if (!quiet)
340 printf("done \n");
341}
342
343static void create_root_dir(ext2_filsys fs)
344{
345 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000346 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000347
348 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
349 if (retval) {
350 com_err("ext2fs_mkdir", retval, "while creating root dir");
351 exit(1);
352 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000353 if (geteuid()) {
354 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
355 if (retval) {
356 com_err("ext2fs_read_inode", retval,
357 "while reading root inode");
358 exit(1);
359 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000360 inode.i_uid = getuid();
361 if (inode.i_uid)
362 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000363 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
364 if (retval) {
365 com_err("ext2fs_write_inode", retval,
366 "while setting root inode ownership");
367 exit(1);
368 }
369 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000370}
371
372static void create_lost_and_found(ext2_filsys fs)
373{
374 errcode_t retval;
375 ino_t ino;
376 const char *name = "lost+found";
377 int i;
378
379 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
380 if (retval) {
381 com_err("ext2fs_mkdir", retval, "while creating /lost+found");
382 exit(1);
383 }
384
385 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
386 if (retval) {
387 com_err("ext2_lookup", retval, "while looking up /lost+found");
388 exit(1);
389 }
390
391 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
392 retval = ext2fs_expand_dir(fs, ino);
393 if (retval) {
394 com_err("ext2fs_expand_dir", retval,
395 "while expanding /lost+found");
396 exit(1);
397 }
398 }
399}
400
401static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
402{
403 errcode_t retval;
404
Theodore Ts'of3db3561997-04-26 13:34:30 +0000405 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000406 fs->group_desc[0].bg_free_inodes_count--;
407 fs->super->s_free_inodes_count--;
408 retval = ext2fs_update_bb_inode(fs, bb_list);
409 if (retval) {
410 com_err("ext2fs_update_bb_inode", retval,
411 "while setting bad block inode");
412 exit(1);
413 }
414
415}
416
417static void reserve_inodes(ext2_filsys fs)
418{
419 ino_t i;
420 int group;
421
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000422 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000423 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000424 group = ext2fs_group_of_ino(fs, i);
425 fs->group_desc[group].bg_free_inodes_count--;
426 fs->super->s_free_inodes_count--;
427 }
428 ext2fs_mark_ib_dirty(fs);
429}
430
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000431#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +0000432static void zap_bootblock(ext2_filsys fs)
433{
434 char buf[512];
435 int retval;
436
437 memset(buf, 0, 512);
438
439 retval = io_channel_write_blk(fs->io, 0, -512, buf);
440 if (retval)
441 printf("Warning: could not erase block 0: %s\n",
442 error_message(retval));
443}
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000444#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +0000445
446
Theodore Ts'o3839e651997-04-26 13:21:57 +0000447static void show_stats(ext2_filsys fs)
448{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000449 struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super;
450 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000451 blk_t group_block;
452 int i, col_left;
453
454 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000455 printf("warning: %d blocks unused.\n\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000456 param.s_blocks_count - s->s_blocks_count);
457
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000458 switch (fs->super->s_creator_os) {
459 case EXT2_OS_LINUX: printf ("Linux"); break;
460 case EXT2_OS_HURD: printf ("GNU/hurd"); break;
461 case EXT2_OS_MASIX: printf ("Masix"); break;
462 default: printf ("(unknown os)");
463 }
464 printf (" ext2 filesystem format\n");
465 memset(buf, 0, sizeof(buf));
466 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
467 printf("Filesystem label=%s\n", buf);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000468 printf("%u inodes, %u blocks\n", s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000469 s->s_blocks_count);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000470 printf("%u blocks (%2.2f%%) reserved for the super user\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000471 s->s_r_blocks_count,
472 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000473 printf("First data block=%u\n", s->s_first_data_block);
474 printf("Block size=%u (log=%u)\n", fs->blocksize,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000475 s->s_log_block_size);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000476 printf("Fragment size=%u (log=%u)\n", fs->fragsize,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000477 s->s_log_frag_size);
478 printf("%lu block group%s\n", fs->group_desc_count,
479 (fs->group_desc_count > 1) ? "s" : "");
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000480 printf("%u blocks per group, %u fragments per group\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000481 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000482 printf("%u inodes per group\n", s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000483
484 if (fs->group_desc_count == 1) {
485 printf("\n");
486 return;
487 }
488
489 printf("Superblock backups stored on blocks: ");
490 group_block = s->s_first_data_block;
491 col_left = 0;
492 for (i = 1; i < fs->group_desc_count; i++) {
493 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000494 if (!ext2fs_bg_has_super(fs, i))
495 continue;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000496 if (!col_left--) {
497 printf("\n\t");
Theodore Ts'oe22d4611997-06-17 03:57:27 +0000498 col_left = 6;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000499 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000500 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000501 if (i != fs->group_desc_count - 1)
502 printf(", ");
503 }
504 printf("\n\n");
505}
506
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000507#ifndef HAVE_STRCASECMP
508static int strcasecmp (char *s1, char *s2)
509{
510 while (*s1 && *s2) {
511 int ch1 = *s1++, ch2 = *s2++;
512 if (isupper (ch1))
513 ch1 = tolower (ch1);
514 if (isupper (ch2))
515 ch2 = tolower (ch2);
516 if (ch1 != ch2)
517 return ch1 - ch2;
518 }
519 return *s1 ? 1 : *s2 ? -1 : 0;
520}
521#endif
522
523/*
524 * Set the S_CREATOR_OS field. Return true if OS is known,
525 * otherwise, 0.
526 */
527static int set_os(struct ext2_super_block *sb, char *os)
528{
529 if (isdigit (*os))
530 sb->s_creator_os = atoi (os);
531 else if (strcasecmp(os, "linux") == 0)
532 sb->s_creator_os = EXT2_OS_LINUX;
533 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
534 sb->s_creator_os = EXT2_OS_HURD;
535 else if (strcasecmp(os, "masix") == 0)
536 sb->s_creator_os = EXT2_OS_MASIX;
537 else
538 return 0;
539 return 1;
540}
541
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000542#define PATH_SET "PATH=/sbin"
543
Theodore Ts'od163b091997-10-03 17:42:28 +0000544static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000545{
546 char *buf, *token, *next, *p, *arg;
547 int len;
548 int raid_usage = 0;
549
550 len = strlen(opts);
551 buf = malloc(len+1);
552 if (!buf) {
553 fprintf(stderr, "Couldn't allocate memory to parse "
554 "raid options!\n");
555 exit(1);
556 }
557 strcpy(buf, opts);
558 for (token = buf; token && *token; token = next) {
559 p = strchr(token, ',');
560 next = 0;
561 if (p) {
562 *p = 0;
563 next = p+1;
564 }
565 arg = strchr(token, '=');
566 if (arg) {
567 *arg = 0;
568 arg++;
569 }
570 if (strcmp(token, "stride") == 0) {
571 if (!arg) {
572 raid_usage++;
573 continue;
574 }
575 fs_stride = strtoul(arg, &p, 0);
576 if (*p || (fs_stride == 0)) {
577 fprintf(stderr, "Invalid stride parameter.\n");
578 raid_usage++;
579 continue;
580 }
581 } else
582 raid_usage++;
583 }
584 if (raid_usage) {
585 fprintf(stderr, "\nBad raid options specified.\n\n"
586 "Raid options are separated by commas, "
587 "and may take an argument which\n"
588 "\tis set off by an equals ('=') sign.\n\n"
589 "Valid raid options are:\n"
590 "\tstride=<stride length in blocks>\n\n");
591 exit(1);
592 }
593}
594
595
596
Theodore Ts'o3839e651997-04-26 13:21:57 +0000597static void PRS(int argc, char *argv[])
598{
Theodore Ts'o519149f1997-10-25 03:49:49 +0000599 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000600 int size;
601 char * tmp;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000602 blk_t max = 8192;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000603 int inode_ratio = 4096;
604 int reserved_ratio = 5;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000605 errcode_t retval;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000606 int sparse_option = -1;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000607 char *oldpath = getenv("PATH");
Theodore Ts'o521e3681997-04-29 17:48:10 +0000608 struct ext2fs_sb *param_ext2 = (struct ext2fs_sb *) &param;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000609 char *raid_opts = 0;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000610
Theodore Ts'o3839e651997-04-26 13:21:57 +0000611 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000612 if (oldpath) {
613 char *newpath;
614
615 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
616 strcpy (newpath, PATH_SET);
617 strcat (newpath, ":");
618 strcat (newpath, oldpath);
619 putenv (newpath);
620 } else
621 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000622
623 setbuf(stdout, NULL);
624 setbuf(stderr, NULL);
625 initialize_ext2_error_table();
626 memset(&param, 0, sizeof(struct ext2_super_block));
627
628 fprintf (stderr, "mke2fs %s, %s for EXT2 FS %s, %s\n",
629 E2FSPROGS_VERSION, E2FSPROGS_DATE,
630 EXT2FS_VERSION, EXT2FS_DATE);
631 if (argc && *argv)
632 program_name = *argv;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000633 while ((c = getopt (argc, argv,
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000634 "b:cf:g:i:l:m:o:qr:R:s:tvI:SFL:M:")) != EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000635 switch (c) {
636 case 'b':
637 size = strtoul(optarg, &tmp, 0);
638 if (size < 1024 || size > 4096 || *tmp) {
639 com_err(program_name, 0, "bad block size - %s",
640 optarg);
641 exit(1);
642 }
643 param.s_log_block_size =
644 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o521e3681997-04-29 17:48:10 +0000645 max = size * 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000646 break;
647 case 'c':
648 case 't': /* Check for bad blocks */
649 cflag = 1;
650 break;
651 case 'f':
652 size = strtoul(optarg, &tmp, 0);
653 if (size < 1024 || size > 4096 || *tmp) {
654 com_err(program_name, 0, "bad fragment size - %s",
655 optarg);
656 exit(1);
657 }
658 param.s_log_frag_size =
659 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
660 printf("Warning: fragments not supported. "
661 "Ignoring -f option\n");
662 break;
663 case 'g':
664 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000665 if (*tmp) {
666 com_err(program_name, 0,
667 "Illegal number for blocks per group");
668 exit(1);
669 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000670 if ((param.s_blocks_per_group % 8) != 0) {
671 com_err(program_name, 0,
672 "blocks per group must be multiple of 8");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000673 exit(1);
674 }
675 break;
676 case 'i':
677 inode_ratio = strtoul(optarg, &tmp, 0);
678 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
679 *tmp) {
680 com_err(program_name, 0, "bad inode ratio - %s",
681 optarg);
682 exit(1);
683 }
684 break;
685 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000686 bad_blocks_filename = malloc(strlen(optarg)+1);
687 if (!bad_blocks_filename) {
688 com_err(program_name, ENOMEM,
689 "in malloc for bad_blocks_filename");
690 exit(1);
691 }
692 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000693 break;
694 case 'm':
695 reserved_ratio = strtoul(optarg, &tmp, 0);
696 if (reserved_ratio > 50 || *tmp) {
697 com_err(program_name, 0,
698 "bad reserved blocks percent - %s",
699 optarg);
700 exit(1);
701 }
702 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000703 case 'o':
704 creator_os = optarg;
705 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000706 case 'r':
707 param.s_rev_level = atoi(optarg);
708 break;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000709 case 's':
710 sparse_option = atoi(optarg);
711 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000712#ifdef EXT2_DYNAMIC_REV
713 case 'I':
714 param.s_inode_size = atoi(optarg);
715 break;
716#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000717 case 'v':
718 verbose = 1;
719 break;
720 case 'q':
721 quiet = 1;
722 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +0000723 case 'F':
724 force = 1;
725 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000726 case 'L':
727 volume_label = optarg;
728 break;
729 case 'M':
730 mount_dir = optarg;
731 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000732 case 'R':
733 raid_opts = optarg;
734 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000735 case 'S':
736 super_only = 1;
737 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000738 default:
739 usage();
740 }
741 if (optind == argc)
742 usage();
743 device_name = argv[optind];
744 optind++;
745 if (optind < argc) {
746 param.s_blocks_count = strtoul(argv[optind++], &tmp, 0);
747 if (*tmp) {
748 com_err(program_name, 0, "bad blocks count - %s",
749 argv[optind - 1]);
750 exit(1);
751 }
752 }
753 if (optind < argc)
754 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000755
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000756 if (raid_opts)
757 parse_raid_opts(raid_opts);
758
Theodore Ts'o74becf31997-04-26 14:37:06 +0000759 if (!force)
760 check_plausibility();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000761 check_mount();
762
Theodore Ts'o3839e651997-04-26 13:21:57 +0000763 param.s_log_frag_size = param.s_log_block_size;
764
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000765 if (!param.s_blocks_count) {
766 retval = ext2fs_get_device_size(device_name,
767 EXT2_BLOCK_SIZE(&param),
768 &param.s_blocks_count);
769 if (retval) {
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000770 com_err(program_name, retval,
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000771 "while trying to determine filesystem size");
772 exit(1);
773 }
774 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000775
Theodore Ts'o521e3681997-04-29 17:48:10 +0000776 if (param.s_blocks_per_group) {
777 if (param.s_blocks_per_group < 256 ||
778 param.s_blocks_per_group > max || *tmp) {
779 com_err(program_name, 0,
780 "blocks per group count out of range");
781 exit(1);
782 }
783 }
784
Theodore Ts'o3839e651997-04-26 13:21:57 +0000785 /*
786 * Calculate number of inodes based on the inode ratio
787 */
788 param.s_inodes_count =
Theodore Ts'of3db3561997-04-26 13:34:30 +0000789 ((long long) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
790 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000791
792 /*
793 * Calculate number of blocks to reserve
794 */
795 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000796
797 /*
798 * If we are using revision #1, use the sparse super feature
799 * by default
800 */
801#ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
802 if ((sparse_option == 1)
803#ifdef EXT2_DYNAMIC_REV
Theodore Ts'obbfa3aa1998-03-21 07:12:46 +0000804 || ((param.s_rev_level >= EXT2_DYNAMIC_REV) && (!sparse_option))
Theodore Ts'o521e3681997-04-29 17:48:10 +0000805#endif
806 )
807 param_ext2->s_feature_ro_compat |=
808 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
809#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000810}
811
812int main (int argc, char *argv[])
813{
814 errcode_t retval = 0;
815 ext2_filsys fs;
816 badblocks_list bb_list = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000817 struct ext2fs_sb *s;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000818
819 PRS(argc, argv);
820
Theodore Ts'o3839e651997-04-26 13:21:57 +0000821 /*
822 * Initialize the superblock....
823 */
824 retval = ext2fs_initialize(device_name, 0, &param,
825 unix_io_manager, &fs);
826 if (retval) {
827 com_err(device_name, retval, "while setting up superblock");
828 exit(1);
829 }
830
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000831 /*
832 * Generate a UUID for it...
833 */
834 s = (struct ext2fs_sb *) fs->super;
835 uuid_generate(s->s_uuid);
836
837 /*
838 * Override the creator OS, if applicable
839 */
840 if (creator_os && !set_os(fs->super, creator_os)) {
841 com_err (program_name, 0, "unknown os - %s", creator_os);
842 exit(1);
843 }
844
845 /*
846 * Set the volume label...
847 */
848 if (volume_label) {
849 memset(s->s_volume_name, 0, sizeof(s->s_volume_name));
850 strncpy(s->s_volume_name, volume_label,
851 sizeof(s->s_volume_name));
852 }
853
854 /*
855 * Set the last mount directory
856 */
857 if (mount_dir) {
858 memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted));
859 strncpy(s->s_last_mounted, mount_dir,
860 sizeof(s->s_last_mounted));
861 }
862
Theodore Ts'o3839e651997-04-26 13:21:57 +0000863 if (!quiet)
864 show_stats(fs);
865
866 if (bad_blocks_filename)
867 read_bb_file(fs, &bb_list, bad_blocks_filename);
868 if (cflag)
869 test_disk(fs, &bb_list);
870
871 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000872 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000873 retval = ext2fs_allocate_tables(fs);
874 if (retval) {
875 com_err(program_name, retval,
876 "while trying to allocate filesystem tables");
877 exit(1);
878 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000879 if (super_only) {
880 fs->super->s_state |= EXT2_ERROR_FS;
881 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
882 } else {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000883 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000884 create_root_dir(fs);
885 create_lost_and_found(fs);
886 reserve_inodes(fs);
887 create_bad_block_inode(fs, bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000888#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +0000889 zap_bootblock(fs);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000890#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +0000891 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000892
893 if (!quiet)
894 printf("Writing superblocks and "
895 "filesystem accounting information: ");
896 ext2fs_close(fs);
897 if (!quiet)
898 printf("done\n");
899 return 0;
900}