blob: 7f5e5e5eb8dc26e9546ca1df489510ed47a7cffa [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'oa418d3a1997-04-26 14:00:26 +0000178
Theodore Ts'o3839e651997-04-26 13:21:57 +0000179 fprintf(stderr, "%s is mounted; will not make a filesystem here!\n",
180 device_name);
181 exit(1);
182}
183
184/*
185 * Helper function for read_bb_file and test_disk
186 */
187static void invalid_block(ext2_filsys fs, blk_t blk)
188{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000189 printf("Bad block %u out of range; ignored.\n", blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000190 return;
191}
192
193/*
194 * Reads the bad blocks list from a file
195 */
196static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
197 const char *bad_blocks_file)
198{
199 FILE *f;
200 errcode_t retval;
201
202 f = fopen(bad_blocks_file, "r");
203 if (!f) {
204 com_err("read_bad_blocks_file", errno,
205 "while trying to open %s", bad_blocks_file);
206 exit(1);
207 }
208 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
209 fclose (f);
210 if (retval) {
211 com_err("ext2fs_read_bb_FILE", retval,
212 "while reading in list of bad blocks from file");
213 exit(1);
214 }
215}
216
217/*
218 * Runs the badblocks program to test the disk
219 */
220static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
221{
222 FILE *f;
223 errcode_t retval;
224 char buf[1024];
225
Theodore Ts'of635d7f1997-05-09 02:50:16 +0000226 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
227 quiet ? "" : "-s ", fs->device_name,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000228 fs->super->s_blocks_count);
229 if (verbose)
230 printf("Running command: %s\n", buf);
231 f = popen(buf, "r");
232 if (!f) {
233 com_err("popen", errno,
234 "while trying run '%s'", buf);
235 exit(1);
236 }
237 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000238 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000239 if (retval) {
240 com_err("ext2fs_read_bb_FILE", retval,
241 "while processing list of bad blocks from program");
242 exit(1);
243 }
244}
245
246static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
247{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000248 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000249 int must_be_good;
250 blk_t blk;
251 badblocks_iterate bb_iter;
252 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000253 blk_t group_block;
254 int group;
255 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000256
257 if (!bb_list)
258 return;
259
260 /*
261 * The primary superblock and group descriptors *must* be
262 * good; if not, abort.
263 */
264 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
265 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
266 if (badblocks_list_test(bb_list, i)) {
267 fprintf(stderr, "Block %d in primary superblock/group "
268 "descriptor area bad.\n", i);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000269 fprintf(stderr, "Blocks %d through %d must be good "
Theodore Ts'o3839e651997-04-26 13:21:57 +0000270 "in order to build a filesystem.\n",
271 fs->super->s_first_data_block, must_be_good);
272 fprintf(stderr, "Aborting....\n");
273 exit(1);
274 }
275 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000276
277 /*
278 * See if any of the bad blocks are showing up in the backup
279 * superblocks and/or group descriptors. If so, issue a
280 * warning and adjust the block counts appropriately.
281 */
282 group_block = fs->super->s_first_data_block +
283 fs->super->s_blocks_per_group;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000284
285 for (i = 1; i < fs->group_desc_count; i++) {
Theodore Ts'o92bcc591998-02-16 22:29:34 +0000286 group_bad = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000287 for (j=0; j < fs->desc_blocks+1; j++) {
288 if (badblocks_list_test(bb_list, group_block +
289 j)) {
290 if (!group_bad)
291 fprintf(stderr,
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000292"Warning: the backup superblock/group descriptors at block %d contain\n"
Theodore Ts'of3db3561997-04-26 13:34:30 +0000293" bad blocks.\n\n",
294 group_block);
295 group_bad++;
296 group = ext2fs_group_of_blk(fs, group_block+j);
297 fs->group_desc[group].bg_free_blocks_count++;
298 fs->super->s_free_blocks_count++;
299 }
300 }
301 group_block += fs->super->s_blocks_per_group;
302 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000303
304 /*
305 * Mark all the bad blocks as used...
306 */
307 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
308 if (retval) {
309 com_err("badblocks_list_iterate_begin", retval,
310 "while marking bad blocks as used");
311 exit(1);
312 }
313 while (badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000314 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000315 badblocks_list_iterate_end(bb_iter);
316}
317
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000318static void write_inode_tables(ext2_filsys fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000319{
320 errcode_t retval;
321 blk_t blk;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000322 int i, j, num, count;
323 char *buf;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000324 char format[20], backup[80];
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000325 int sync_kludge = 0;
326 char *mke2fs_sync;
327
328 mke2fs_sync = getenv("MKE2FS_SYNC");
329 if (mke2fs_sync)
330 sync_kludge = atoi(mke2fs_sync);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000331
332 buf = malloc(fs->blocksize * STRIDE_LENGTH);
333 if (!buf) {
334 com_err("malloc", ENOMEM, "while allocating zeroizing buffer");
335 exit(1);
336 }
337 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000338
339 /*
340 * Figure out how many digits we need
341 */
342 i = log10(fs->group_desc_count);
343 sprintf(format, "%%%dd/%%%dld", i, i);
344 memset(backup, '\b', sizeof(backup)-1);
345 backup[sizeof(backup)-1] = 0;
346 if ((2*i)+1 < sizeof(backup))
347 backup[(2*i)+1] = 0;
348
Theodore Ts'o3839e651997-04-26 13:21:57 +0000349 if (!quiet)
350 printf("Writing inode tables: ");
351 for (i = 0; i < fs->group_desc_count; i++) {
352 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000353 printf(format, i, fs->group_desc_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000354
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000355 blk = fs->group_desc[i].bg_inode_table;
356 num = fs->inode_blocks_per_group;
357
358 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
359 if (num-j > STRIDE_LENGTH)
360 count = STRIDE_LENGTH;
361 else
362 count = num - j;
363 retval = io_channel_write_blk(fs->io, blk, count, buf);
364 if (retval)
365 printf("Warning: could not write %d blocks "
366 "in inode table starting at %d: %s\n",
367 count, blk, error_message(retval));
368 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000369 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000370 fputs(backup, stdout);
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000371 if (sync_kludge) {
372 if (sync_kludge == 1)
373 sync();
374 else if ((i % sync_kludge) == 0)
375 sync();
376 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000377 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000378 free(buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000379 if (!quiet)
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000380 fputs("done \n", stdout);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000381}
382
383static void create_root_dir(ext2_filsys fs)
384{
385 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000386 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000387
388 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
389 if (retval) {
390 com_err("ext2fs_mkdir", retval, "while creating root dir");
391 exit(1);
392 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000393 if (geteuid()) {
394 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
395 if (retval) {
396 com_err("ext2fs_read_inode", retval,
397 "while reading root inode");
398 exit(1);
399 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000400 inode.i_uid = getuid();
401 if (inode.i_uid)
402 inode.i_gid = getgid();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000403 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
404 if (retval) {
405 com_err("ext2fs_write_inode", retval,
406 "while setting root inode ownership");
407 exit(1);
408 }
409 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000410}
411
412static void create_lost_and_found(ext2_filsys fs)
413{
414 errcode_t retval;
415 ino_t ino;
416 const char *name = "lost+found";
417 int i;
418
419 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
420 if (retval) {
421 com_err("ext2fs_mkdir", retval, "while creating /lost+found");
422 exit(1);
423 }
424
425 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
426 if (retval) {
427 com_err("ext2_lookup", retval, "while looking up /lost+found");
428 exit(1);
429 }
430
431 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
432 retval = ext2fs_expand_dir(fs, ino);
433 if (retval) {
434 com_err("ext2fs_expand_dir", retval,
435 "while expanding /lost+found");
436 exit(1);
437 }
438 }
439}
440
441static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
442{
443 errcode_t retval;
444
Theodore Ts'of3db3561997-04-26 13:34:30 +0000445 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000446 fs->group_desc[0].bg_free_inodes_count--;
447 fs->super->s_free_inodes_count--;
448 retval = ext2fs_update_bb_inode(fs, bb_list);
449 if (retval) {
450 com_err("ext2fs_update_bb_inode", retval,
451 "while setting bad block inode");
452 exit(1);
453 }
454
455}
456
457static void reserve_inodes(ext2_filsys fs)
458{
459 ino_t i;
460 int group;
461
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000462 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000463 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000464 group = ext2fs_group_of_ino(fs, i);
465 fs->group_desc[group].bg_free_inodes_count--;
466 fs->super->s_free_inodes_count--;
467 }
468 ext2fs_mark_ib_dirty(fs);
469}
470
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000471#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +0000472static void zap_bootblock(ext2_filsys fs)
473{
474 char buf[512];
475 int retval;
476
477 memset(buf, 0, 512);
478
479 retval = io_channel_write_blk(fs->io, 0, -512, buf);
480 if (retval)
481 printf("Warning: could not erase block 0: %s\n",
482 error_message(retval));
483}
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000484#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +0000485
486
Theodore Ts'o3839e651997-04-26 13:21:57 +0000487static void show_stats(ext2_filsys fs)
488{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000489 struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super;
490 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000491 blk_t group_block;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000492 int i, need, col_left;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000493
494 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000495 printf("warning: %d blocks unused.\n\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000496 param.s_blocks_count - s->s_blocks_count);
497
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000498 switch (fs->super->s_creator_os) {
499 case EXT2_OS_LINUX: printf ("Linux"); break;
500 case EXT2_OS_HURD: printf ("GNU/hurd"); break;
501 case EXT2_OS_MASIX: printf ("Masix"); break;
502 default: printf ("(unknown os)");
503 }
504 printf (" ext2 filesystem format\n");
505 memset(buf, 0, sizeof(buf));
506 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
507 printf("Filesystem label=%s\n", buf);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000508 printf("%u inodes, %u blocks\n", s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000509 s->s_blocks_count);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000510 printf("%u blocks (%2.2f%%) reserved for the super user\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000511 s->s_r_blocks_count,
512 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000513 printf("First data block=%u\n", s->s_first_data_block);
514 printf("Block size=%u (log=%u)\n", fs->blocksize,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000515 s->s_log_block_size);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000516 printf("Fragment size=%u (log=%u)\n", fs->fragsize,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000517 s->s_log_frag_size);
518 printf("%lu block group%s\n", fs->group_desc_count,
519 (fs->group_desc_count > 1) ? "s" : "");
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000520 printf("%u blocks per group, %u fragments per group\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000521 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000522 printf("%u inodes per group\n", s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000523
524 if (fs->group_desc_count == 1) {
525 printf("\n");
526 return;
527 }
528
529 printf("Superblock backups stored on blocks: ");
530 group_block = s->s_first_data_block;
531 col_left = 0;
532 for (i = 1; i < fs->group_desc_count; i++) {
533 group_block += s->s_blocks_per_group;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000534 if (!ext2fs_bg_has_super(fs, i))
535 continue;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000536 need = log10(group_block) + 2;
537 if (need > col_left) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000538 printf("\n\t");
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000539 col_left = 72;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000540 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000541 col_left -= need;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000542 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000543 if (i != fs->group_desc_count - 1)
544 printf(", ");
545 }
546 printf("\n\n");
547}
548
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000549#ifndef HAVE_STRCASECMP
550static int strcasecmp (char *s1, char *s2)
551{
552 while (*s1 && *s2) {
553 int ch1 = *s1++, ch2 = *s2++;
554 if (isupper (ch1))
555 ch1 = tolower (ch1);
556 if (isupper (ch2))
557 ch2 = tolower (ch2);
558 if (ch1 != ch2)
559 return ch1 - ch2;
560 }
561 return *s1 ? 1 : *s2 ? -1 : 0;
562}
563#endif
564
565/*
566 * Set the S_CREATOR_OS field. Return true if OS is known,
567 * otherwise, 0.
568 */
569static int set_os(struct ext2_super_block *sb, char *os)
570{
571 if (isdigit (*os))
572 sb->s_creator_os = atoi (os);
573 else if (strcasecmp(os, "linux") == 0)
574 sb->s_creator_os = EXT2_OS_LINUX;
575 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
576 sb->s_creator_os = EXT2_OS_HURD;
577 else if (strcasecmp(os, "masix") == 0)
578 sb->s_creator_os = EXT2_OS_MASIX;
579 else
580 return 0;
581 return 1;
582}
583
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000584#define PATH_SET "PATH=/sbin"
585
Theodore Ts'od163b091997-10-03 17:42:28 +0000586static void parse_raid_opts(const char *opts)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000587{
588 char *buf, *token, *next, *p, *arg;
589 int len;
590 int raid_usage = 0;
591
592 len = strlen(opts);
593 buf = malloc(len+1);
594 if (!buf) {
595 fprintf(stderr, "Couldn't allocate memory to parse "
596 "raid options!\n");
597 exit(1);
598 }
599 strcpy(buf, opts);
600 for (token = buf; token && *token; token = next) {
601 p = strchr(token, ',');
602 next = 0;
603 if (p) {
604 *p = 0;
605 next = p+1;
606 }
607 arg = strchr(token, '=');
608 if (arg) {
609 *arg = 0;
610 arg++;
611 }
612 if (strcmp(token, "stride") == 0) {
613 if (!arg) {
614 raid_usage++;
615 continue;
616 }
617 fs_stride = strtoul(arg, &p, 0);
618 if (*p || (fs_stride == 0)) {
619 fprintf(stderr, "Invalid stride parameter.\n");
620 raid_usage++;
621 continue;
622 }
623 } else
624 raid_usage++;
625 }
626 if (raid_usage) {
627 fprintf(stderr, "\nBad raid options specified.\n\n"
628 "Raid options are separated by commas, "
629 "and may take an argument which\n"
630 "\tis set off by an equals ('=') sign.\n\n"
631 "Valid raid options are:\n"
632 "\tstride=<stride length in blocks>\n\n");
633 exit(1);
634 }
635}
636
637
638
Theodore Ts'o3839e651997-04-26 13:21:57 +0000639static void PRS(int argc, char *argv[])
640{
Theodore Ts'o519149f1997-10-25 03:49:49 +0000641 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000642 int size;
643 char * tmp;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000644 blk_t max = 8192;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000645 int inode_ratio = 4096;
646 int reserved_ratio = 5;
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000647 ino_t num_inodes = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000648 errcode_t retval;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000649 int sparse_option = -1;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000650 char *oldpath = getenv("PATH");
Theodore Ts'o521e3681997-04-29 17:48:10 +0000651 struct ext2fs_sb *param_ext2 = (struct ext2fs_sb *) &param;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000652 char *raid_opts = 0;
Theodore Ts'oa789d841998-03-30 01:20:55 +0000653 blk_t dev_size;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000654
Theodore Ts'o3839e651997-04-26 13:21:57 +0000655 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000656 if (oldpath) {
657 char *newpath;
658
659 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
660 strcpy (newpath, PATH_SET);
661 strcat (newpath, ":");
662 strcat (newpath, oldpath);
663 putenv (newpath);
664 } else
665 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000666
667 setbuf(stdout, NULL);
668 setbuf(stderr, NULL);
669 initialize_ext2_error_table();
670 memset(&param, 0, sizeof(struct ext2_super_block));
671
672 fprintf (stderr, "mke2fs %s, %s for EXT2 FS %s, %s\n",
673 E2FSPROGS_VERSION, E2FSPROGS_DATE,
674 EXT2FS_VERSION, EXT2FS_DATE);
675 if (argc && *argv)
676 program_name = *argv;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000677 while ((c = getopt (argc, argv,
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000678 "b:cf:g:i:l:m:o:qr:R:s:tvI:SFL:M:N:V")) != EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000679 switch (c) {
680 case 'b':
681 size = strtoul(optarg, &tmp, 0);
682 if (size < 1024 || size > 4096 || *tmp) {
683 com_err(program_name, 0, "bad block size - %s",
684 optarg);
685 exit(1);
686 }
687 param.s_log_block_size =
688 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Theodore Ts'o521e3681997-04-29 17:48:10 +0000689 max = size * 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000690 break;
691 case 'c':
692 case 't': /* Check for bad blocks */
693 cflag = 1;
694 break;
695 case 'f':
696 size = strtoul(optarg, &tmp, 0);
697 if (size < 1024 || size > 4096 || *tmp) {
698 com_err(program_name, 0, "bad fragment size - %s",
699 optarg);
700 exit(1);
701 }
702 param.s_log_frag_size =
703 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
704 printf("Warning: fragments not supported. "
705 "Ignoring -f option\n");
706 break;
707 case 'g':
708 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000709 if (*tmp) {
710 com_err(program_name, 0,
711 "Illegal number for blocks per group");
712 exit(1);
713 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000714 if ((param.s_blocks_per_group % 8) != 0) {
715 com_err(program_name, 0,
716 "blocks per group must be multiple of 8");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000717 exit(1);
718 }
719 break;
720 case 'i':
721 inode_ratio = strtoul(optarg, &tmp, 0);
722 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
723 *tmp) {
724 com_err(program_name, 0, "bad inode ratio - %s",
725 optarg);
726 exit(1);
727 }
728 break;
729 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000730 bad_blocks_filename = malloc(strlen(optarg)+1);
731 if (!bad_blocks_filename) {
732 com_err(program_name, ENOMEM,
733 "in malloc for bad_blocks_filename");
734 exit(1);
735 }
736 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000737 break;
738 case 'm':
739 reserved_ratio = strtoul(optarg, &tmp, 0);
740 if (reserved_ratio > 50 || *tmp) {
741 com_err(program_name, 0,
742 "bad reserved blocks percent - %s",
743 optarg);
744 exit(1);
745 }
746 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000747 case 'o':
748 creator_os = optarg;
749 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000750 case 'r':
751 param.s_rev_level = atoi(optarg);
752 break;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000753 case 's':
754 sparse_option = atoi(optarg);
755 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000756#ifdef EXT2_DYNAMIC_REV
757 case 'I':
758 param.s_inode_size = atoi(optarg);
759 break;
760#endif
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000761 case 'N':
762 num_inodes = atoi(optarg);
763 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000764 case 'v':
765 verbose = 1;
766 break;
767 case 'q':
768 quiet = 1;
769 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +0000770 case 'F':
771 force = 1;
772 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000773 case 'L':
774 volume_label = optarg;
775 break;
776 case 'M':
777 mount_dir = optarg;
778 break;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000779 case 'R':
780 raid_opts = optarg;
781 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000782 case 'S':
783 super_only = 1;
784 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000785 case 'V':
786 /* Print version number and exit */
787 fprintf(stderr, "\tUsing %s\n",
788 error_message(EXT2_ET_BASE));
789 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000790 default:
791 usage();
792 }
793 if (optind == argc)
794 usage();
795 device_name = argv[optind];
796 optind++;
797 if (optind < argc) {
798 param.s_blocks_count = strtoul(argv[optind++], &tmp, 0);
799 if (*tmp) {
800 com_err(program_name, 0, "bad blocks count - %s",
801 argv[optind - 1]);
802 exit(1);
803 }
804 }
805 if (optind < argc)
806 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000807
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000808 if (raid_opts)
809 parse_raid_opts(raid_opts);
810
Theodore Ts'o74becf31997-04-26 14:37:06 +0000811 if (!force)
812 check_plausibility();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000813 check_mount();
814
Theodore Ts'o3839e651997-04-26 13:21:57 +0000815 param.s_log_frag_size = param.s_log_block_size;
816
Theodore Ts'oa789d841998-03-30 01:20:55 +0000817 retval = ext2fs_get_device_size(device_name,
818 EXT2_BLOCK_SIZE(&param),
819 &dev_size);
820 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
821 com_err(program_name, retval,
822 "while trying to determine filesystem size");
823 exit(1);
824 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000825 if (!param.s_blocks_count) {
Theodore Ts'oa789d841998-03-30 01:20:55 +0000826 if (retval == EXT2_ET_UNIMPLEMENTED) {
827 com_err(program_name, 0,
828 "Couldn't determine device size; you "
829 "must specify\nthe size of the "
830 "filesystem\n");
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000831 exit(1);
Theodore Ts'oa789d841998-03-30 01:20:55 +0000832 } else
833 param.s_blocks_count = dev_size;
834 } else if (!force && (param.s_blocks_count > dev_size)) {
835 com_err(program_name, 0,
836 "Filesystem larger than apparent filesystem size.");
837 proceed_question();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000838 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000839
Theodore Ts'o521e3681997-04-29 17:48:10 +0000840 if (param.s_blocks_per_group) {
841 if (param.s_blocks_per_group < 256 ||
842 param.s_blocks_per_group > max || *tmp) {
843 com_err(program_name, 0,
844 "blocks per group count out of range");
845 exit(1);
846 }
847 }
848
Theodore Ts'o3839e651997-04-26 13:21:57 +0000849 /*
850 * Calculate number of inodes based on the inode ratio
851 */
Theodore Ts'o5515e6b1999-01-05 07:25:06 +0000852 param.s_inodes_count = num_inodes ? num_inodes :
Theodore Ts'of3db3561997-04-26 13:34:30 +0000853 ((long long) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
854 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000855
856 /*
857 * Calculate number of blocks to reserve
858 */
859 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000860
861 /*
862 * If we are using revision #1, use the sparse super feature
863 * by default
864 */
865#ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
866 if ((sparse_option == 1)
867#ifdef EXT2_DYNAMIC_REV
Theodore Ts'obbfa3aa1998-03-21 07:12:46 +0000868 || ((param.s_rev_level >= EXT2_DYNAMIC_REV) && (!sparse_option))
Theodore Ts'o521e3681997-04-29 17:48:10 +0000869#endif
870 )
871 param_ext2->s_feature_ro_compat |=
872 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
873#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000874}
875
876int main (int argc, char *argv[])
877{
878 errcode_t retval = 0;
879 ext2_filsys fs;
880 badblocks_list bb_list = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000881 struct ext2fs_sb *s;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000882
883 PRS(argc, argv);
884
Theodore Ts'o3839e651997-04-26 13:21:57 +0000885 /*
886 * Initialize the superblock....
887 */
888 retval = ext2fs_initialize(device_name, 0, &param,
889 unix_io_manager, &fs);
890 if (retval) {
891 com_err(device_name, retval, "while setting up superblock");
892 exit(1);
893 }
894
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000895 /*
896 * Generate a UUID for it...
897 */
898 s = (struct ext2fs_sb *) fs->super;
899 uuid_generate(s->s_uuid);
900
901 /*
902 * Override the creator OS, if applicable
903 */
904 if (creator_os && !set_os(fs->super, creator_os)) {
905 com_err (program_name, 0, "unknown os - %s", creator_os);
906 exit(1);
907 }
908
909 /*
910 * Set the volume label...
911 */
912 if (volume_label) {
913 memset(s->s_volume_name, 0, sizeof(s->s_volume_name));
914 strncpy(s->s_volume_name, volume_label,
915 sizeof(s->s_volume_name));
916 }
917
918 /*
919 * Set the last mount directory
920 */
921 if (mount_dir) {
922 memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted));
923 strncpy(s->s_last_mounted, mount_dir,
924 sizeof(s->s_last_mounted));
925 }
926
Theodore Ts'o3839e651997-04-26 13:21:57 +0000927 if (!quiet)
928 show_stats(fs);
929
930 if (bad_blocks_filename)
931 read_bb_file(fs, &bb_list, bad_blocks_filename);
932 if (cflag)
933 test_disk(fs, &bb_list);
934
935 handle_bad_blocks(fs, bb_list);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000936 fs->stride = fs_stride;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000937 retval = ext2fs_allocate_tables(fs);
938 if (retval) {
939 com_err(program_name, retval,
940 "while trying to allocate filesystem tables");
941 exit(1);
942 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000943 if (super_only) {
944 fs->super->s_state |= EXT2_ERROR_FS;
945 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
946 } else {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000947 write_inode_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000948 create_root_dir(fs);
949 create_lost_and_found(fs);
950 reserve_inodes(fs);
951 create_bad_block_inode(fs, bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000952#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +0000953 zap_bootblock(fs);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000954#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +0000955 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000956
957 if (!quiet)
958 printf("Writing superblocks and "
959 "filesystem accounting information: ");
960 ext2fs_close(fs);
961 if (!quiet)
962 printf("done\n");
963 return 0;
964}