blob: 0cdcc059174fce01902d3809947c9cb66d2fc67a [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * mke2fs.c - Make a ext2fs filesystem.
3 *
4 * Copyright (C) 1994 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
6 */
7
8/* Usage: mke2fs [options] device
9 *
10 * The device may be a block device or a image of one, but this isn't
11 * enforced (but it's not much fun on a character device :-).
12 */
13
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000014#include <stdio.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000015#include <string.h>
16#include <fcntl.h>
17#include <ctype.h>
18#include <termios.h>
19#include <time.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000020#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000021#include <getopt.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000022#endif
23#ifdef HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000024#include <unistd.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000025#endif
26#ifdef HAVE_STDLIB_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include <stdlib.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000028#endif
29#ifdef HAVE_ERRNO_H
30#include <errno.h>
31#endif
32#ifdef HAVE_MNTENT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000033#include <mntent.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000034#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000035#include <malloc.h>
36#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000037#include <sys/types.h>
Theodore Ts'o74becf31997-04-26 14:37:06 +000038#include <sys/stat.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000039#include <stdio.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000040
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000041#ifdef HAVE_LINUX_FS_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000042#include <linux/fs.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000043#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000044#include <linux/ext2_fs.h>
Theodore Ts'o74becf31997-04-26 14:37:06 +000045#ifdef HAVE_LINUX_MAJOR_H
46#include <linux/major.h>
47#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000048
49#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000050#include "uuid/uuid.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000051#include "ext2fs/ext2fs.h"
52#include "../version.h"
53
54#define STRIDE_LENGTH 8
55
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000056#ifndef sparc
57#define ZAP_BOOTBLOCK
58#endif
59
Theodore Ts'o3839e651997-04-26 13:21:57 +000060extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000061extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000062
63const char * program_name = "mke2fs";
64const char * device_name = NULL;
65
66/* Command line options */
67int cflag = 0;
68int verbose = 0;
69int quiet = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +000070int super_only = 0;
Theodore Ts'o74becf31997-04-26 14:37:06 +000071int force = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000072char *bad_blocks_filename = 0;
73
74struct ext2_super_block param;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000075char *creator_os = NULL;
76char *volume_label = NULL;
77char *mount_dir = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000078
79static void usage(NOARGS)
80{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000081 fprintf(stderr, "Usage: %s [-c|-t|-l filename] [-b block-size] "
82 "[-f fragment-size]\n\t[-i bytes-per-inode] "
83 "[-m reserved-blocks-percentage] [-qvS]\n\t"
84 "[-o creator-os] [-g blocks-per-group] [-L volume-label]\n\t"
85 "[-M last-mounted-directory] device [blocks-count]\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +000086 program_name);
87 exit(1);
88}
89
90static int log2(int arg)
91{
92 int l = 0;
93
94 arg >>= 1;
95 while (arg) {
96 l++;
97 arg >>= 1;
98 }
99 return l;
100}
101
Theodore Ts'o74becf31997-04-26 14:37:06 +0000102static void check_plausibility(NOARGS)
103{
104#ifdef HAVE_LINUX_MAJOR_H
105 int val;
106 struct stat s;
107
108 val = stat(device_name, &s);
109
110 if(val == -1) {
111 perror("stat");
112 exit(1);
113 }
114 if(!S_ISBLK(s.st_mode)) {
115 printf("%s is not a block special device.\n", device_name);
116 printf("Proceed anyway? (y,n) ");
117 if (getchar() != 'y')
118 exit(1);
119 return;
120 }
121 if ((MAJOR(s.st_rdev) == HD_MAJOR && MINOR(s.st_rdev)%64 == 0) ||
122 (MAJOR(s.st_rdev) == SCSI_DISK_MAJOR &&
123 MINOR(s.st_rdev)%16 == 0)) {
124 printf("%s is entire device, not just one partition!\n",
125 device_name);
126 printf("Proceed anyway? (y,n) ");
127 if (getchar() != 'y')
128 exit(1);
129 return;
130 }
131#endif
132}
133
Theodore Ts'o3839e651997-04-26 13:21:57 +0000134static void check_mount(NOARGS)
135{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000136 errcode_t retval;
137 int mount_flags;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000138
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000139 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
140 if (retval) {
141 com_err("ext2fs_check_if_mount", retval,
142 "while determining whether %s is mounted.",
143 device_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000144 return;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000145 }
146 if (!(mount_flags & EXT2_MF_MOUNTED))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000147 return;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000148
Theodore Ts'o3839e651997-04-26 13:21:57 +0000149 fprintf(stderr, "%s is mounted; will not make a filesystem here!\n",
150 device_name);
151 exit(1);
152}
153
154/*
155 * Helper function for read_bb_file and test_disk
156 */
157static void invalid_block(ext2_filsys fs, blk_t blk)
158{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000159 printf("Bad block %u out of range; ignored.\n", blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000160 return;
161}
162
163/*
164 * Reads the bad blocks list from a file
165 */
166static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
167 const char *bad_blocks_file)
168{
169 FILE *f;
170 errcode_t retval;
171
172 f = fopen(bad_blocks_file, "r");
173 if (!f) {
174 com_err("read_bad_blocks_file", errno,
175 "while trying to open %s", bad_blocks_file);
176 exit(1);
177 }
178 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
179 fclose (f);
180 if (retval) {
181 com_err("ext2fs_read_bb_FILE", retval,
182 "while reading in list of bad blocks from file");
183 exit(1);
184 }
185}
186
187/*
188 * Runs the badblocks program to test the disk
189 */
190static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
191{
192 FILE *f;
193 errcode_t retval;
194 char buf[1024];
195
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000196 sprintf(buf, "badblocks %s%s %d", quiet ? "" : "-s ",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000197 fs->device_name,
198 fs->super->s_blocks_count);
199 if (verbose)
200 printf("Running command: %s\n", buf);
201 f = popen(buf, "r");
202 if (!f) {
203 com_err("popen", errno,
204 "while trying run '%s'", buf);
205 exit(1);
206 }
207 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000208 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000209 if (retval) {
210 com_err("ext2fs_read_bb_FILE", retval,
211 "while processing list of bad blocks from program");
212 exit(1);
213 }
214}
215
216static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
217{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000218 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000219 int must_be_good;
220 blk_t blk;
221 badblocks_iterate bb_iter;
222 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000223 blk_t group_block;
224 int group;
225 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000226
227 if (!bb_list)
228 return;
229
230 /*
231 * The primary superblock and group descriptors *must* be
232 * good; if not, abort.
233 */
234 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
235 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
236 if (badblocks_list_test(bb_list, i)) {
237 fprintf(stderr, "Block %d in primary superblock/group "
238 "descriptor area bad.\n", i);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000239 fprintf(stderr, "Blocks %d through %d must be good "
Theodore Ts'o3839e651997-04-26 13:21:57 +0000240 "in order to build a filesystem.\n",
241 fs->super->s_first_data_block, must_be_good);
242 fprintf(stderr, "Aborting....\n");
243 exit(1);
244 }
245 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000246
247 /*
248 * See if any of the bad blocks are showing up in the backup
249 * superblocks and/or group descriptors. If so, issue a
250 * warning and adjust the block counts appropriately.
251 */
252 group_block = fs->super->s_first_data_block +
253 fs->super->s_blocks_per_group;
254 group_bad = 0;
255
256 for (i = 1; i < fs->group_desc_count; i++) {
257 for (j=0; j < fs->desc_blocks+1; j++) {
258 if (badblocks_list_test(bb_list, group_block +
259 j)) {
260 if (!group_bad)
261 fprintf(stderr,
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000262"Warning: the backup superblock/group descriptors at block %d contain\n"
Theodore Ts'of3db3561997-04-26 13:34:30 +0000263" bad blocks.\n\n",
264 group_block);
265 group_bad++;
266 group = ext2fs_group_of_blk(fs, group_block+j);
267 fs->group_desc[group].bg_free_blocks_count++;
268 fs->super->s_free_blocks_count++;
269 }
270 }
271 group_block += fs->super->s_blocks_per_group;
272 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000273
274 /*
275 * Mark all the bad blocks as used...
276 */
277 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
278 if (retval) {
279 com_err("badblocks_list_iterate_begin", retval,
280 "while marking bad blocks as used");
281 exit(1);
282 }
283 while (badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000284 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000285 badblocks_list_iterate_end(bb_iter);
286}
287
288static void new_table_block(ext2_filsys fs, blk_t first_block,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000289 const char *name, int num, int initialize,
290 const char *buf, blk_t *new_block)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000291{
292 errcode_t retval;
293 blk_t blk;
294 int i;
295 int count;
296
297 retval = ext2fs_get_free_blocks(fs, first_block,
298 first_block + fs->super->s_blocks_per_group,
299 num, fs->block_map, new_block);
300 if (retval) {
301 printf("Could not allocate %d block(s) for %s: %s\n",
302 num, name, error_message(retval));
303 ext2fs_unmark_valid(fs);
304 return;
305 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000306 if (initialize) {
307 blk = *new_block;
308 for (i=0; i < num; i += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
309 if (num-i > STRIDE_LENGTH)
310 count = STRIDE_LENGTH;
311 else
312 count = num - i;
313 retval = io_channel_write_blk(fs->io, blk, count, buf);
314 if (retval)
315 printf("Warning: could not write %d blocks "
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000316 "starting at %d for %s: %s\n",
Theodore Ts'of3db3561997-04-26 13:34:30 +0000317 count, blk, name,
318 error_message(retval));
319 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000320 }
321 blk = *new_block;
322 for (i = 0; i < num; i++, blk++)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000323 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000324}
325
326static void alloc_tables(ext2_filsys fs)
327{
328 blk_t group_blk;
329 int i;
330 char *buf;
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);
338
339 group_blk = fs->super->s_first_data_block;
340 if (!quiet)
341 printf("Writing inode tables: ");
342 for (i = 0; i < fs->group_desc_count; i++) {
343 if (!quiet)
344 printf("%4d/%4ld", i, fs->group_desc_count);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000345 new_table_block(fs, group_blk, "block bitmap", 1, 0, buf,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000346 &fs->group_desc[i].bg_block_bitmap);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000347 new_table_block(fs, group_blk, "inode bitmap", 1, 0, buf,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000348 &fs->group_desc[i].bg_inode_bitmap);
349 new_table_block(fs, group_blk, "inode table",
Theodore Ts'of3db3561997-04-26 13:34:30 +0000350 fs->inode_blocks_per_group,
351 !super_only, buf,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000352 &fs->group_desc[i].bg_inode_table);
353
Theodore Ts'o3839e651997-04-26 13:21:57 +0000354 group_blk += fs->super->s_blocks_per_group;
355 if (!quiet)
356 printf("\b\b\b\b\b\b\b\b\b");
357 }
358 if (!quiet)
359 printf("done \n");
360}
361
362static void create_root_dir(ext2_filsys fs)
363{
364 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000365 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000366
367 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
368 if (retval) {
369 com_err("ext2fs_mkdir", retval, "while creating root dir");
370 exit(1);
371 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000372 if (geteuid()) {
373 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
374 if (retval) {
375 com_err("ext2fs_read_inode", retval,
376 "while reading root inode");
377 exit(1);
378 }
379 inode.i_uid = geteuid();
380 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
381 if (retval) {
382 com_err("ext2fs_write_inode", retval,
383 "while setting root inode ownership");
384 exit(1);
385 }
386 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000387}
388
389static void create_lost_and_found(ext2_filsys fs)
390{
391 errcode_t retval;
392 ino_t ino;
393 const char *name = "lost+found";
394 int i;
395
396 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
397 if (retval) {
398 com_err("ext2fs_mkdir", retval, "while creating /lost+found");
399 exit(1);
400 }
401
402 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
403 if (retval) {
404 com_err("ext2_lookup", retval, "while looking up /lost+found");
405 exit(1);
406 }
407
408 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
409 retval = ext2fs_expand_dir(fs, ino);
410 if (retval) {
411 com_err("ext2fs_expand_dir", retval,
412 "while expanding /lost+found");
413 exit(1);
414 }
415 }
416}
417
418static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
419{
420 errcode_t retval;
421
Theodore Ts'of3db3561997-04-26 13:34:30 +0000422 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000423 fs->group_desc[0].bg_free_inodes_count--;
424 fs->super->s_free_inodes_count--;
425 retval = ext2fs_update_bb_inode(fs, bb_list);
426 if (retval) {
427 com_err("ext2fs_update_bb_inode", retval,
428 "while setting bad block inode");
429 exit(1);
430 }
431
432}
433
434static void reserve_inodes(ext2_filsys fs)
435{
436 ino_t i;
437 int group;
438
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000439 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000440 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000441 group = ext2fs_group_of_ino(fs, i);
442 fs->group_desc[group].bg_free_inodes_count--;
443 fs->super->s_free_inodes_count--;
444 }
445 ext2fs_mark_ib_dirty(fs);
446}
447
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000448#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +0000449static void zap_bootblock(ext2_filsys fs)
450{
451 char buf[512];
452 int retval;
453
454 memset(buf, 0, 512);
455
456 retval = io_channel_write_blk(fs->io, 0, -512, buf);
457 if (retval)
458 printf("Warning: could not erase block 0: %s\n",
459 error_message(retval));
460}
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000461#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +0000462
463
Theodore Ts'o3839e651997-04-26 13:21:57 +0000464static void show_stats(ext2_filsys fs)
465{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000466 struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super;
467 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000468 blk_t group_block;
469 int i, col_left;
470
471 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000472 printf("warning: %d blocks unused.\n\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000473 param.s_blocks_count - s->s_blocks_count);
474
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000475 switch (fs->super->s_creator_os) {
476 case EXT2_OS_LINUX: printf ("Linux"); break;
477 case EXT2_OS_HURD: printf ("GNU/hurd"); break;
478 case EXT2_OS_MASIX: printf ("Masix"); break;
479 default: printf ("(unknown os)");
480 }
481 printf (" ext2 filesystem format\n");
482 memset(buf, 0, sizeof(buf));
483 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
484 printf("Filesystem label=%s\n", buf);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000485 printf("%u inodes, %u blocks\n", s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000486 s->s_blocks_count);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000487 printf("%u blocks (%2.2f%%) reserved for the super user\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000488 s->s_r_blocks_count,
489 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000490 printf("First data block=%u\n", s->s_first_data_block);
491 printf("Block size=%u (log=%u)\n", fs->blocksize,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000492 s->s_log_block_size);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000493 printf("Fragment size=%u (log=%u)\n", fs->fragsize,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000494 s->s_log_frag_size);
495 printf("%lu block group%s\n", fs->group_desc_count,
496 (fs->group_desc_count > 1) ? "s" : "");
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000497 printf("%u blocks per group, %u fragments per group\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000498 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000499 printf("%u inodes per group\n", s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000500
501 if (fs->group_desc_count == 1) {
502 printf("\n");
503 return;
504 }
505
506 printf("Superblock backups stored on blocks: ");
507 group_block = s->s_first_data_block;
508 col_left = 0;
509 for (i = 1; i < fs->group_desc_count; i++) {
510 group_block += s->s_blocks_per_group;
511 if (!col_left--) {
512 printf("\n\t");
513 col_left = 8;
514 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000515 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000516 if (i != fs->group_desc_count - 1)
517 printf(", ");
518 }
519 printf("\n\n");
520}
521
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000522#ifndef HAVE_STRCASECMP
523static int strcasecmp (char *s1, char *s2)
524{
525 while (*s1 && *s2) {
526 int ch1 = *s1++, ch2 = *s2++;
527 if (isupper (ch1))
528 ch1 = tolower (ch1);
529 if (isupper (ch2))
530 ch2 = tolower (ch2);
531 if (ch1 != ch2)
532 return ch1 - ch2;
533 }
534 return *s1 ? 1 : *s2 ? -1 : 0;
535}
536#endif
537
538/*
539 * Set the S_CREATOR_OS field. Return true if OS is known,
540 * otherwise, 0.
541 */
542static int set_os(struct ext2_super_block *sb, char *os)
543{
544 if (isdigit (*os))
545 sb->s_creator_os = atoi (os);
546 else if (strcasecmp(os, "linux") == 0)
547 sb->s_creator_os = EXT2_OS_LINUX;
548 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
549 sb->s_creator_os = EXT2_OS_HURD;
550 else if (strcasecmp(os, "masix") == 0)
551 sb->s_creator_os = EXT2_OS_MASIX;
552 else
553 return 0;
554 return 1;
555}
556
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000557#define PATH_SET "PATH=/sbin"
558
Theodore Ts'o3839e651997-04-26 13:21:57 +0000559static void PRS(int argc, char *argv[])
560{
561 char c;
562 int size;
563 char * tmp;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000564 int inode_ratio = 4096;
565 int reserved_ratio = 5;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000566 errcode_t retval;
567 char *oldpath = getenv("PATH");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000568
569 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000570 if (oldpath) {
571 char *newpath;
572
573 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
574 strcpy (newpath, PATH_SET);
575 strcat (newpath, ":");
576 strcat (newpath, oldpath);
577 putenv (newpath);
578 } else
579 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000580
581 setbuf(stdout, NULL);
582 setbuf(stderr, NULL);
583 initialize_ext2_error_table();
584 memset(&param, 0, sizeof(struct ext2_super_block));
585
586 fprintf (stderr, "mke2fs %s, %s for EXT2 FS %s, %s\n",
587 E2FSPROGS_VERSION, E2FSPROGS_DATE,
588 EXT2FS_VERSION, EXT2FS_DATE);
589 if (argc && *argv)
590 program_name = *argv;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000591 while ((c = getopt (argc, argv,
592 "b:cf:g:i:l:m:o:qr:tvI:SFL:M:")) != EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000593 switch (c) {
594 case 'b':
595 size = strtoul(optarg, &tmp, 0);
596 if (size < 1024 || size > 4096 || *tmp) {
597 com_err(program_name, 0, "bad block size - %s",
598 optarg);
599 exit(1);
600 }
601 param.s_log_block_size =
602 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
603 break;
604 case 'c':
605 case 't': /* Check for bad blocks */
606 cflag = 1;
607 break;
608 case 'f':
609 size = strtoul(optarg, &tmp, 0);
610 if (size < 1024 || size > 4096 || *tmp) {
611 com_err(program_name, 0, "bad fragment size - %s",
612 optarg);
613 exit(1);
614 }
615 param.s_log_frag_size =
616 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
617 printf("Warning: fragments not supported. "
618 "Ignoring -f option\n");
619 break;
620 case 'g':
621 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000622 if (*tmp) {
623 com_err(program_name, 0,
624 "Illegal number for blocks per group");
625 exit(1);
626 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000627 if (param.s_blocks_per_group < 256 ||
628 param.s_blocks_per_group > 8192 || *tmp) {
629 com_err(program_name, 0,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000630 "blocks per group count out of range");
631 exit(1);
632 }
633 if ((param.s_blocks_per_group % 8) != 0) {
634 com_err(program_name, 0,
635 "blocks per group must be multiple of 8");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000636 exit(1);
637 }
638 break;
639 case 'i':
640 inode_ratio = strtoul(optarg, &tmp, 0);
641 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
642 *tmp) {
643 com_err(program_name, 0, "bad inode ratio - %s",
644 optarg);
645 exit(1);
646 }
647 break;
648 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000649 bad_blocks_filename = malloc(strlen(optarg)+1);
650 if (!bad_blocks_filename) {
651 com_err(program_name, ENOMEM,
652 "in malloc for bad_blocks_filename");
653 exit(1);
654 }
655 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000656 break;
657 case 'm':
658 reserved_ratio = strtoul(optarg, &tmp, 0);
659 if (reserved_ratio > 50 || *tmp) {
660 com_err(program_name, 0,
661 "bad reserved blocks percent - %s",
662 optarg);
663 exit(1);
664 }
665 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000666 case 'o':
667 creator_os = optarg;
668 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000669 case 'r':
670 param.s_rev_level = atoi(optarg);
671 break;
672#ifdef EXT2_DYNAMIC_REV
673 case 'I':
674 param.s_inode_size = atoi(optarg);
675 break;
676#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000677 case 'v':
678 verbose = 1;
679 break;
680 case 'q':
681 quiet = 1;
682 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +0000683 case 'F':
684 force = 1;
685 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000686 case 'L':
687 volume_label = optarg;
688 break;
689 case 'M':
690 mount_dir = optarg;
691 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000692 case 'S':
693 super_only = 1;
694 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000695 default:
696 usage();
697 }
698 if (optind == argc)
699 usage();
700 device_name = argv[optind];
701 optind++;
702 if (optind < argc) {
703 param.s_blocks_count = strtoul(argv[optind++], &tmp, 0);
704 if (*tmp) {
705 com_err(program_name, 0, "bad blocks count - %s",
706 argv[optind - 1]);
707 exit(1);
708 }
709 }
710 if (optind < argc)
711 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000712
Theodore Ts'o74becf31997-04-26 14:37:06 +0000713 if (!force)
714 check_plausibility();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000715 check_mount();
716
Theodore Ts'o3839e651997-04-26 13:21:57 +0000717 param.s_log_frag_size = param.s_log_block_size;
718
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000719 if (!param.s_blocks_count) {
720 retval = ext2fs_get_device_size(device_name,
721 EXT2_BLOCK_SIZE(&param),
722 &param.s_blocks_count);
723 if (retval) {
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000724 com_err(program_name, retval,
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000725 "while trying to determine filesystem size");
726 exit(1);
727 }
728 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000729
730 /*
731 * Calculate number of inodes based on the inode ratio
732 */
733 param.s_inodes_count =
Theodore Ts'of3db3561997-04-26 13:34:30 +0000734 ((long long) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
735 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000736
737 /*
738 * Calculate number of blocks to reserve
739 */
740 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
741}
742
743int main (int argc, char *argv[])
744{
745 errcode_t retval = 0;
746 ext2_filsys fs;
747 badblocks_list bb_list = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000748 struct ext2fs_sb *s;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000749
750 PRS(argc, argv);
751
Theodore Ts'o3839e651997-04-26 13:21:57 +0000752 /*
753 * Initialize the superblock....
754 */
755 retval = ext2fs_initialize(device_name, 0, &param,
756 unix_io_manager, &fs);
757 if (retval) {
758 com_err(device_name, retval, "while setting up superblock");
759 exit(1);
760 }
761
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000762 /*
763 * Generate a UUID for it...
764 */
765 s = (struct ext2fs_sb *) fs->super;
766 uuid_generate(s->s_uuid);
767
768 /*
769 * Override the creator OS, if applicable
770 */
771 if (creator_os && !set_os(fs->super, creator_os)) {
772 com_err (program_name, 0, "unknown os - %s", creator_os);
773 exit(1);
774 }
775
776 /*
777 * Set the volume label...
778 */
779 if (volume_label) {
780 memset(s->s_volume_name, 0, sizeof(s->s_volume_name));
781 strncpy(s->s_volume_name, volume_label,
782 sizeof(s->s_volume_name));
783 }
784
785 /*
786 * Set the last mount directory
787 */
788 if (mount_dir) {
789 memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted));
790 strncpy(s->s_last_mounted, mount_dir,
791 sizeof(s->s_last_mounted));
792 }
793
Theodore Ts'o3839e651997-04-26 13:21:57 +0000794 if (!quiet)
795 show_stats(fs);
796
797 if (bad_blocks_filename)
798 read_bb_file(fs, &bb_list, bad_blocks_filename);
799 if (cflag)
800 test_disk(fs, &bb_list);
801
802 handle_bad_blocks(fs, bb_list);
803 alloc_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000804 if (super_only) {
805 fs->super->s_state |= EXT2_ERROR_FS;
806 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
807 } else {
808 create_root_dir(fs);
809 create_lost_and_found(fs);
810 reserve_inodes(fs);
811 create_bad_block_inode(fs, bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000812#ifdef ZAP_BOOTBLOCK
Theodore Ts'of3db3561997-04-26 13:34:30 +0000813 zap_bootblock(fs);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000814#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +0000815 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000816
817 if (!quiet)
818 printf("Writing superblocks and "
819 "filesystem accounting information: ");
820 ext2fs_close(fs);
821 if (!quiet)
822 printf("done\n");
823 return 0;
824}