blob: d6eb6fb99f234e218b5a3fcfc22668a63109997a [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'o818180c1998-06-27 05:11:14 +000091 "[-m reserved-blocks-percentage] [-qvSV]\n\t"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000092 "[-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"
Theodore Ts'o1dde43f1998-11-14 04:18:28 +000094 "[-s sparse-super-flag] 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'o74becf31997-04-26 14:37:06 +0000129static void check_plausibility(NOARGS)
130{
131#ifdef HAVE_LINUX_MAJOR_H
132 int val;
133 struct stat s;
134
135 val = stat(device_name, &s);
136
137 if(val == -1) {
Theodore Ts'oa789d841998-03-30 01:20:55 +0000138 fprintf(stderr, "Could not stat %s --- %s\n",
139 device_name, error_message(errno));
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000140 if (errno == ENOENT)
Theodore Ts'oa789d841998-03-30 01:20:55 +0000141 fprintf(stderr, "\nThe device apparently does "
142 "not exist; did you specify it correctly?\n");
Theodore Ts'o74becf31997-04-26 14:37:06 +0000143 exit(1);
144 }
145 if(!S_ISBLK(s.st_mode)) {
146 printf("%s is not a block special device.\n", device_name);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000147 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000148 return;
Theodore Ts'oa789d841998-03-30 01:20:55 +0000149 } else if ((MAJOR(s.st_rdev) == HD_MAJOR &&
150 MINOR(s.st_rdev)%64 == 0) ||
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000151 (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
152 MINOR(s.st_rdev)%16 == 0)) {
Theodore Ts'o74becf31997-04-26 14:37:06 +0000153 printf("%s is entire device, not just one partition!\n",
154 device_name);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000155 proceed_question();
Theodore Ts'o74becf31997-04-26 14:37:06 +0000156 }
157#endif
158}
159
Theodore Ts'o3839e651997-04-26 13:21:57 +0000160static void check_mount(NOARGS)
161{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000162 errcode_t retval;
163 int mount_flags;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000164
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000165 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
166 if (retval) {
167 com_err("ext2fs_check_if_mount", retval,
168 "while determining whether %s is mounted.",
169 device_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000170 return;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000171 }
172 if (!(mount_flags & EXT2_MF_MOUNTED))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000173 return;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000174
Theodore Ts'o3839e651997-04-26 13:21:57 +0000175 fprintf(stderr, "%s is mounted; will not make a filesystem here!\n",
176 device_name);
177 exit(1);
178}
179
180/*
181 * Helper function for read_bb_file and test_disk
182 */
183static void invalid_block(ext2_filsys fs, blk_t blk)
184{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000185 printf("Bad block %u out of range; ignored.\n", blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000186 return;
187}
188
189/*
190 * Reads the bad blocks list from a file
191 */
192static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
193 const char *bad_blocks_file)
194{
195 FILE *f;
196 errcode_t retval;
197
198 f = fopen(bad_blocks_file, "r");
199 if (!f) {
200 com_err("read_bad_blocks_file", errno,
201 "while trying to open %s", bad_blocks_file);
202 exit(1);
203 }
204 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
205 fclose (f);
206 if (retval) {
207 com_err("ext2fs_read_bb_FILE", retval,
208 "while reading in list of bad blocks from file");
209 exit(1);
210 }
211}
212
213/*
214 * Runs the badblocks program to test the disk
215 */
216static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
217{
218 FILE *f;
219 errcode_t retval;
220 char buf[1024];
221
Theodore Ts'of635d7f1997-05-09 02:50:16 +0000222 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
223 quiet ? "" : "-s ", fs->device_name,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000224 fs->super->s_blocks_count);
225 if (verbose)
226 printf("Running command: %s\n", buf);
227 f = popen(buf, "r");
228 if (!f) {
229 com_err("popen", errno,
230 "while trying run '%s'", buf);
231 exit(1);
232 }
233 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000234 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000235 if (retval) {
236 com_err("ext2fs_read_bb_FILE", retval,
237 "while processing list of bad blocks from program");
238 exit(1);
239 }
240}
241
242static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
243{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000244 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000245 int must_be_good;
246 blk_t blk;
247 badblocks_iterate bb_iter;
248 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000249 blk_t group_block;
250 int group;
251 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000252
253 if (!bb_list)
254 return;
255
256 /*
257 * The primary superblock and group descriptors *must* be
258 * good; if not, abort.
259 */
260 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
261 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
262 if (badblocks_list_test(bb_list, i)) {
263 fprintf(stderr, "Block %d in primary superblock/group "
264 "descriptor area bad.\n", i);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000265 fprintf(stderr, "Blocks %d through %d must be good "
Theodore Ts'o3839e651997-04-26 13:21:57 +0000266 "in order to build a filesystem.\n",
267 fs->super->s_first_data_block, must_be_good);
268 fprintf(stderr, "Aborting....\n");
269 exit(1);
270 }
271 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000272
273 /*
274 * See if any of the bad blocks are showing up in the backup
275 * superblocks and/or group descriptors. If so, issue a
276 * warning and adjust the block counts appropriately.
277 */
278 group_block = fs->super->s_first_data_block +
279 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000280
281 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000282 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000283 for (j=0; j < fs->desc_blocks+1; j++) {
284 if (badblocks_list_test(bb_list, group_block +
285 j)) {
286 if (!group_bad)
287 fprintf(stderr,
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000288"Warning: the backup superblock/group descriptors at block %d contain\n"
Theodore Ts'of3db3561997-04-26 13:34:30 +0000289" bad blocks.\n\n",
290 group_block);
291 group_bad++;
292 group = ext2fs_group_of_blk(fs, group_block+j);
293 fs->group_desc[group].bg_free_blocks_count++;
294 fs->super->s_free_blocks_count++;
295 }
296 }
297 group_block += fs->super->s_blocks_per_group;
298 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000299
300 /*
301 * Mark all the bad blocks as used...
302 */
303 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
304 if (retval) {
305 com_err("badblocks_list_iterate_begin", retval,
306 "while marking bad blocks as used");
307 exit(1);
308 }
309 while (badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000310 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000311 badblocks_list_iterate_end(bb_iter);
312}
313
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000314static void write_inode_tables(ext2_filsys fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000315{
316 errcode_t retval;
317 blk_t blk;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000318 int i, j, num, count;
319 char *buf;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000320 char format[20], backup[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000321
322 buf = malloc(fs->blocksize * STRIDE_LENGTH);
323 if (!buf) {
324 com_err("malloc", ENOMEM, "while allocating zeroizing buffer");
325 exit(1);
326 }
327 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000328
329 /*
330 * Figure out how many digits we need
331 */
332 i = log10(fs->group_desc_count);
333 sprintf(format, "%%%dd/%%%dld", i, i);
334 memset(backup, '\b', sizeof(backup)-1);
335 backup[sizeof(backup)-1] = 0;
336 if ((2*i)+1 < sizeof(backup))
337 backup[(2*i)+1] = 0;
338
Theodore Ts'o3839e651997-04-26 13:21:57 +0000339 if (!quiet)
340 printf("Writing inode tables: ");
341 for (i = 0; i < fs->group_desc_count; i++) {
342 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000343 printf(format, i, fs->group_desc_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000344
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000345 blk = fs->group_desc[i].bg_inode_table;
346 num = fs->inode_blocks_per_group;
347
348 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
349 if (num-j > STRIDE_LENGTH)
350 count = STRIDE_LENGTH;
351 else
352 count = num - j;
353 retval = io_channel_write_blk(fs->io, blk, count, buf);
354 if (retval)
355 printf("Warning: could not write %d blocks "
356 "in inode table starting at %d: %s\n",
357 count, blk, error_message(retval));
358 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000359 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000360 fputs(backup, stdout);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000361 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000362 free(buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000363 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000364 fputs("done \n", stdout);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000365}
366
367static void create_root_dir(ext2_filsys fs)
368{
369 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000370 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000371
372 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
373 if (retval) {
374 com_err("ext2fs_mkdir", retval, "while creating root dir");
375 exit(1);
376 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000377 if (geteuid()) {
378 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
379 if (retval) {
380 com_err("ext2fs_read_inode", retval,
381 "while reading root inode");
382 exit(1);
383 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000384 inode.i_uid = getuid();
385 if (inode.i_uid)
386 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000387 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
388 if (retval) {
389 com_err("ext2fs_write_inode", retval,
390 "while setting root inode ownership");
391 exit(1);
392 }
393 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000394}
395
396static void create_lost_and_found(ext2_filsys fs)
397{
398 errcode_t retval;
399 ino_t ino;
400 const char *name = "lost+found";
401 int i;
402
403 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
404 if (retval) {
405 com_err("ext2fs_mkdir", retval, "while creating /lost+found");
406 exit(1);
407 }
408
409 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
410 if (retval) {
411 com_err("ext2_lookup", retval, "while looking up /lost+found");
412 exit(1);
413 }
414
415 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
416 retval = ext2fs_expand_dir(fs, ino);
417 if (retval) {
418 com_err("ext2fs_expand_dir", retval,
419 "while expanding /lost+found");
420 exit(1);
421 }
422 }
423}
424
425static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
426{
427 errcode_t retval;
428
Theodore Ts'of3db3561997-04-26 13:34:30 +0000429 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000430 fs->group_desc[0].bg_free_inodes_count--;
431 fs->super->s_free_inodes_count--;
432 retval = ext2fs_update_bb_inode(fs, bb_list);
433 if (retval) {
434 com_err("ext2fs_update_bb_inode", retval,
435 "while setting bad block inode");
436 exit(1);
437 }
438
439}
440
441static void reserve_inodes(ext2_filsys fs)
442{
443 ino_t i;
444 int group;
445
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000446 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000447 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000448 group = ext2fs_group_of_ino(fs, i);
449 fs->group_desc[group].bg_free_inodes_count--;
450 fs->super->s_free_inodes_count--;
451 }
452 ext2fs_mark_ib_dirty(fs);
453}
454
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000455#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +0000456static void zap_bootblock(ext2_filsys fs)
457{
458 char buf[512];
459 int retval;
460
461 memset(buf, 0, 512);
462
463 retval = io_channel_write_blk(fs->io, 0, -512, buf);
464 if (retval)
465 printf("Warning: could not erase block 0: %s\n",
466 error_message(retval));
467}
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000468#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +0000469
470
Theodore Ts'o3839e651997-04-26 13:21:57 +0000471static void show_stats(ext2_filsys fs)
472{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000473 struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super;
474 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000475 blk_t group_block;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000476 int i, need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000477
478 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000479 printf("warning: %d blocks unused.\n\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000480 param.s_blocks_count - s->s_blocks_count);
481
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000482 switch (fs->super->s_creator_os) {
483 case EXT2_OS_LINUX: printf ("Linux"); break;
484 case EXT2_OS_HURD: printf ("GNU/hurd"); break;
485 case EXT2_OS_MASIX: printf ("Masix"); break;
486 default: printf ("(unknown os)");
487 }
488 printf (" ext2 filesystem format\n");
489 memset(buf, 0, sizeof(buf));
490 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
491 printf("Filesystem label=%s\n", buf);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000492 printf("%u inodes, %u blocks\n", s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000493 s->s_blocks_count);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000494 printf("%u blocks (%2.2f%%) reserved for the super user\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000495 s->s_r_blocks_count,
496 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000497 printf("First data block=%u\n", s->s_first_data_block);
498 printf("Block size=%u (log=%u)\n", fs->blocksize,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000499 s->s_log_block_size);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000500 printf("Fragment size=%u (log=%u)\n", fs->fragsize,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000501 s->s_log_frag_size);
502 printf("%lu block group%s\n", fs->group_desc_count,
503 (fs->group_desc_count > 1) ? "s" : "");
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000504 printf("%u blocks per group, %u fragments per group\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000505 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000506 printf("%u inodes per group\n", s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000507
508 if (fs->group_desc_count == 1) {
509 printf("\n");
510 return;
511 }
512
513 printf("Superblock backups stored on blocks: ");
514 group_block = s->s_first_data_block;
515 col_left = 0;
516 for (i = 1; i < fs->group_desc_count; i++) {
517 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000518 if (!ext2fs_bg_has_super(fs, i))
519 continue;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000520 need = log10(group_block) + 2;
521 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000522 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000523 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000524 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000525 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000526 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000527 if (i != fs->group_desc_count - 1)
528 printf(", ");
529 }
530 printf("\n\n");
531}
532
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000533#ifndef HAVE_STRCASECMP
534static int strcasecmp (char *s1, char *s2)
535{
536 while (*s1 && *s2) {
537 int ch1 = *s1++, ch2 = *s2++;
538 if (isupper (ch1))
539 ch1 = tolower (ch1);
540 if (isupper (ch2))
541 ch2 = tolower (ch2);
542 if (ch1 != ch2)
543 return ch1 - ch2;
544 }
545 return *s1 ? 1 : *s2 ? -1 : 0;
546}
547#endif
548
549/*
550 * Set the S_CREATOR_OS field. Return true if OS is known,
551 * otherwise, 0.
552 */
553static int set_os(struct ext2_super_block *sb, char *os)
554{
555 if (isdigit (*os))
556 sb->s_creator_os = atoi (os);
557 else if (strcasecmp(os, "linux") == 0)
558 sb->s_creator_os = EXT2_OS_LINUX;
559 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
560 sb->s_creator_os = EXT2_OS_HURD;
561 else if (strcasecmp(os, "masix") == 0)
562 sb->s_creator_os = EXT2_OS_MASIX;
563 else
564 return 0;
565 return 1;
566}
567
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000568#define PATH_SET "PATH=/sbin"
569
Theodore Ts'od163b091997-10-03 17:42:28 +0000570static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000571{
572 char *buf, *token, *next, *p, *arg;
573 int len;
574 int raid_usage = 0;
575
576 len = strlen(opts);
577 buf = malloc(len+1);
578 if (!buf) {
579 fprintf(stderr, "Couldn't allocate memory to parse "
580 "raid options!\n");
581 exit(1);
582 }
583 strcpy(buf, opts);
584 for (token = buf; token && *token; token = next) {
585 p = strchr(token, ',');
586 next = 0;
587 if (p) {
588 *p = 0;
589 next = p+1;
590 }
591 arg = strchr(token, '=');
592 if (arg) {
593 *arg = 0;
594 arg++;
595 }
596 if (strcmp(token, "stride") == 0) {
597 if (!arg) {
598 raid_usage++;
599 continue;
600 }
601 fs_stride = strtoul(arg, &p, 0);
602 if (*p || (fs_stride == 0)) {
603 fprintf(stderr, "Invalid stride parameter.\n");
604 raid_usage++;
605 continue;
606 }
607 } else
608 raid_usage++;
609 }
610 if (raid_usage) {
611 fprintf(stderr, "\nBad raid options specified.\n\n"
612 "Raid options are separated by commas, "
613 "and may take an argument which\n"
614 "\tis set off by an equals ('=') sign.\n\n"
615 "Valid raid options are:\n"
616 "\tstride=<stride length in blocks>\n\n");
617 exit(1);
618 }
619}
620
621
622
Theodore Ts'o3839e651997-04-26 13:21:57 +0000623static void PRS(int argc, char *argv[])
624{
Theodore Ts'o519149f1997-10-25 03:49:49 +0000625 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000626 int size;
627 char * tmp;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000628 blk_t max = 8192;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000629 int inode_ratio = 4096;
630 int reserved_ratio = 5;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000631 errcode_t retval;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000632 int sparse_option = -1;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000633 char *oldpath = getenv("PATH");
Theodore Ts'o521e3681997-04-29 17:48:10 +0000634 struct ext2fs_sb *param_ext2 = (struct ext2fs_sb *) &param;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000635 char *raid_opts = 0;
Theodore Ts'oa789d841998-03-30 01:20:55 +0000636 blk_t dev_size;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000637
Theodore Ts'o3839e651997-04-26 13:21:57 +0000638 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000639 if (oldpath) {
640 char *newpath;
641
642 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
643 strcpy (newpath, PATH_SET);
644 strcat (newpath, ":");
645 strcat (newpath, oldpath);
646 putenv (newpath);
647 } else
648 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000649
650 setbuf(stdout, NULL);
651 setbuf(stderr, NULL);
652 initialize_ext2_error_table();
653 memset(&param, 0, sizeof(struct ext2_super_block));
654
655 fprintf (stderr, "mke2fs %s, %s for EXT2 FS %s, %s\n",
656 E2FSPROGS_VERSION, E2FSPROGS_DATE,
657 EXT2FS_VERSION, EXT2FS_DATE);
658 if (argc && *argv)
659 program_name = *argv;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000660 while ((c = getopt (argc, argv,
Theodore Ts'o818180c1998-06-27 05:11:14 +0000661 "b:cf:g:i:l:m:o:qr:R:s:tvI:SFL:M:V")) != EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000662 switch (c) {
663 case 'b':
664 size = strtoul(optarg, &tmp, 0);
665 if (size < 1024 || size > 4096 || *tmp) {
666 com_err(program_name, 0, "bad block size - %s",
667 optarg);
668 exit(1);
669 }
670 param.s_log_block_size =
671 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o521e3681997-04-29 17:48:10 +0000672 max = size * 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000673 break;
674 case 'c':
675 case 't': /* Check for bad blocks */
676 cflag = 1;
677 break;
678 case 'f':
679 size = strtoul(optarg, &tmp, 0);
680 if (size < 1024 || size > 4096 || *tmp) {
681 com_err(program_name, 0, "bad fragment size - %s",
682 optarg);
683 exit(1);
684 }
685 param.s_log_frag_size =
686 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
687 printf("Warning: fragments not supported. "
688 "Ignoring -f option\n");
689 break;
690 case 'g':
691 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000692 if (*tmp) {
693 com_err(program_name, 0,
694 "Illegal number for blocks per group");
695 exit(1);
696 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000697 if ((param.s_blocks_per_group % 8) != 0) {
698 com_err(program_name, 0,
699 "blocks per group must be multiple of 8");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000700 exit(1);
701 }
702 break;
703 case 'i':
704 inode_ratio = strtoul(optarg, &tmp, 0);
705 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
706 *tmp) {
707 com_err(program_name, 0, "bad inode ratio - %s",
708 optarg);
709 exit(1);
710 }
711 break;
712 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000713 bad_blocks_filename = malloc(strlen(optarg)+1);
714 if (!bad_blocks_filename) {
715 com_err(program_name, ENOMEM,
716 "in malloc for bad_blocks_filename");
717 exit(1);
718 }
719 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000720 break;
721 case 'm':
722 reserved_ratio = strtoul(optarg, &tmp, 0);
723 if (reserved_ratio > 50 || *tmp) {
724 com_err(program_name, 0,
725 "bad reserved blocks percent - %s",
726 optarg);
727 exit(1);
728 }
729 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000730 case 'o':
731 creator_os = optarg;
732 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000733 case 'r':
734 param.s_rev_level = atoi(optarg);
735 break;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000736 case 's':
737 sparse_option = atoi(optarg);
738 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000739#ifdef EXT2_DYNAMIC_REV
740 case 'I':
741 param.s_inode_size = atoi(optarg);
742 break;
743#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000744 case 'v':
745 verbose = 1;
746 break;
747 case 'q':
748 quiet = 1;
749 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +0000750 case 'F':
751 force = 1;
752 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000753 case 'L':
754 volume_label = optarg;
755 break;
756 case 'M':
757 mount_dir = optarg;
758 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000759 case 'R':
760 raid_opts = optarg;
761 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000762 case 'S':
763 super_only = 1;
764 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000765 case 'V':
766 /* Print version number and exit */
767 fprintf(stderr, "\tUsing %s\n",
768 error_message(EXT2_ET_BASE));
769 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000770 default:
771 usage();
772 }
773 if (optind == argc)
774 usage();
775 device_name = argv[optind];
776 optind++;
777 if (optind < argc) {
778 param.s_blocks_count = strtoul(argv[optind++], &tmp, 0);
779 if (*tmp) {
780 com_err(program_name, 0, "bad blocks count - %s",
781 argv[optind - 1]);
782 exit(1);
783 }
784 }
785 if (optind < argc)
786 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000787
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000788 if (raid_opts)
789 parse_raid_opts(raid_opts);
790
Theodore Ts'o74becf31997-04-26 14:37:06 +0000791 if (!force)
792 check_plausibility();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000793 check_mount();
794
Theodore Ts'o3839e651997-04-26 13:21:57 +0000795 param.s_log_frag_size = param.s_log_block_size;
796
Theodore Ts'oa789d841998-03-30 01:20:55 +0000797 retval = ext2fs_get_device_size(device_name,
798 EXT2_BLOCK_SIZE(&param),
799 &dev_size);
800 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
801 com_err(program_name, retval,
802 "while trying to determine filesystem size");
803 exit(1);
804 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000805 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +0000806 if (retval == EXT2_ET_UNIMPLEMENTED) {
807 com_err(program_name, 0,
808 "Couldn't determine device size; you "
809 "must specify\nthe size of the "
810 "filesystem\n");
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000811 exit(1);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000812 } else
813 param.s_blocks_count = dev_size;
814 } else if (!force && (param.s_blocks_count > dev_size)) {
815 com_err(program_name, 0,
816 "Filesystem larger than apparent filesystem size.");
817 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000818 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000819
Theodore Ts'o521e3681997-04-29 17:48:10 +0000820 if (param.s_blocks_per_group) {
821 if (param.s_blocks_per_group < 256 ||
822 param.s_blocks_per_group > max || *tmp) {
823 com_err(program_name, 0,
824 "blocks per group count out of range");
825 exit(1);
826 }
827 }
828
Theodore Ts'o3839e651997-04-26 13:21:57 +0000829 /*
830 * Calculate number of inodes based on the inode ratio
831 */
832 param.s_inodes_count =
Theodore Ts'of3db3561997-04-26 13:34:30 +0000833 ((long long) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
834 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000835
836 /*
837 * Calculate number of blocks to reserve
838 */
839 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000840
841 /*
842 * If we are using revision #1, use the sparse super feature
843 * by default
844 */
845#ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
846 if ((sparse_option == 1)
847#ifdef EXT2_DYNAMIC_REV
Theodore Ts'obbfa3aa1998-03-21 07:12:46 +0000848 || ((param.s_rev_level >= EXT2_DYNAMIC_REV) && (!sparse_option))
Theodore Ts'o521e3681997-04-29 17:48:10 +0000849#endif
850 )
851 param_ext2->s_feature_ro_compat |=
852 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
853#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000854}
855
856int main (int argc, char *argv[])
857{
858 errcode_t retval = 0;
859 ext2_filsys fs;
860 badblocks_list bb_list = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000861 struct ext2fs_sb *s;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000862
863 PRS(argc, argv);
864
Theodore Ts'o3839e651997-04-26 13:21:57 +0000865 /*
866 * Initialize the superblock....
867 */
868 retval = ext2fs_initialize(device_name, 0, &param,
869 unix_io_manager, &fs);
870 if (retval) {
871 com_err(device_name, retval, "while setting up superblock");
872 exit(1);
873 }
874
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000875 /*
876 * Generate a UUID for it...
877 */
878 s = (struct ext2fs_sb *) fs->super;
879 uuid_generate(s->s_uuid);
880
881 /*
882 * Override the creator OS, if applicable
883 */
884 if (creator_os && !set_os(fs->super, creator_os)) {
885 com_err (program_name, 0, "unknown os - %s", creator_os);
886 exit(1);
887 }
888
889 /*
890 * Set the volume label...
891 */
892 if (volume_label) {
893 memset(s->s_volume_name, 0, sizeof(s->s_volume_name));
894 strncpy(s->s_volume_name, volume_label,
895 sizeof(s->s_volume_name));
896 }
897
898 /*
899 * Set the last mount directory
900 */
901 if (mount_dir) {
902 memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted));
903 strncpy(s->s_last_mounted, mount_dir,
904 sizeof(s->s_last_mounted));
905 }
906
Theodore Ts'o3839e651997-04-26 13:21:57 +0000907 if (!quiet)
908 show_stats(fs);
909
910 if (bad_blocks_filename)
911 read_bb_file(fs, &bb_list, bad_blocks_filename);
912 if (cflag)
913 test_disk(fs, &bb_list);
914
915 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000916 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000917 retval = ext2fs_allocate_tables(fs);
918 if (retval) {
919 com_err(program_name, retval,
920 "while trying to allocate filesystem tables");
921 exit(1);
922 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000923 if (super_only) {
924 fs->super->s_state |= EXT2_ERROR_FS;
925 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
926 } else {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000927 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000928 create_root_dir(fs);
929 create_lost_and_found(fs);
930 reserve_inodes(fs);
931 create_bad_block_inode(fs, bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000932#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +0000933 zap_bootblock(fs);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000934#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +0000935 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000936
937 if (!quiet)
938 printf("Writing superblocks and "
939 "filesystem accounting information: ");
940 ext2fs_close(fs);
941 if (!quiet)
942 printf("done\n");
943 return 0;
944}