Home | History | Annotate | Line # | Download | only in makefs
makefs.c revision 1.12
      1  1.12   lukem /*	$NetBSD: makefs.c,v 1.12 2002/01/26 13:22:16 lukem Exp $	*/
      2   1.1   lukem 
      3   1.1   lukem /*
      4  1.11   lukem  * Copyright (c) 2001-2002 Wasabi Systems, Inc.
      5   1.1   lukem  * All rights reserved.
      6   1.1   lukem  *
      7   1.1   lukem  * Written by Luke Mewburn for Wasabi Systems, Inc.
      8   1.1   lukem  *
      9   1.1   lukem  * Redistribution and use in source and binary forms, with or without
     10   1.1   lukem  * modification, are permitted provided that the following conditions
     11   1.1   lukem  * are met:
     12   1.1   lukem  * 1. Redistributions of source code must retain the above copyright
     13   1.1   lukem  *    notice, this list of conditions and the following disclaimer.
     14   1.1   lukem  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1   lukem  *    notice, this list of conditions and the following disclaimer in the
     16   1.1   lukem  *    documentation and/or other materials provided with the distribution.
     17   1.1   lukem  * 3. All advertising materials mentioning features or use of this software
     18   1.1   lukem  *    must display the following acknowledgement:
     19   1.1   lukem  *      This product includes software developed for the NetBSD Project by
     20   1.1   lukem  *      Wasabi Systems, Inc.
     21   1.1   lukem  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22   1.1   lukem  *    or promote products derived from this software without specific prior
     23   1.1   lukem  *    written permission.
     24   1.1   lukem  *
     25   1.1   lukem  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26   1.1   lukem  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27   1.1   lukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28   1.1   lukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29   1.1   lukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30   1.1   lukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31   1.1   lukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32   1.1   lukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33   1.1   lukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34   1.1   lukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35   1.1   lukem  * POSSIBILITY OF SUCH DAMAGE.
     36   1.1   lukem  */
     37   1.2   lukem 
     38   1.2   lukem #include <sys/cdefs.h>
     39   1.2   lukem #ifndef __lint
     40  1.12   lukem __RCSID("$NetBSD: makefs.c,v 1.12 2002/01/26 13:22:16 lukem Exp $");
     41   1.2   lukem #endif	/* !__lint */
     42  1.10      is 
     43  1.10      is #ifdef HAVE_CONFIG_H
     44  1.10      is #include <config.h>
     45  1.10      is #endif
     46   1.1   lukem 
     47   1.1   lukem #include <assert.h>
     48   1.1   lukem #include <ctype.h>
     49   1.1   lukem #include <err.h>
     50   1.1   lukem #include <errno.h>
     51   1.5  simonb #include <limits.h>
     52   1.1   lukem #include <stdio.h>
     53   1.1   lukem #include <stdlib.h>
     54   1.1   lukem #include <string.h>
     55   1.1   lukem #include <unistd.h>
     56   1.1   lukem 
     57   1.1   lukem #include "makefs.h"
     58  1.11   lukem #include "mtree.h"
     59   1.6   lukem #include "strsuftoull.h"
     60   1.1   lukem 
     61   1.1   lukem 
     62   1.1   lukem /*
     63   1.1   lukem  * list of supported file systems and dispatch functions
     64   1.1   lukem  */
     65   1.1   lukem typedef struct {
     66   1.1   lukem 	const char	*type;
     67   1.1   lukem 	int		(*parse_options)(const char *, fsinfo_t *);
     68   1.1   lukem 	void		(*make_fs)(const char *, const char *, fsnode *,
     69   1.1   lukem 				fsinfo_t *);
     70   1.1   lukem } fstype_t;
     71   1.1   lukem 
     72   1.1   lukem static fstype_t fstypes[] = {
     73   1.1   lukem 	{ "ffs",	ffs_parse_opts,		ffs_makefs },
     74   1.1   lukem 	{ NULL	},
     75   1.1   lukem };
     76   1.1   lukem 
     77   1.7   lukem uint		debug;
     78   1.1   lukem struct timespec	start_time;
     79   1.1   lukem 
     80   1.1   lukem 
     81   1.1   lukem static	fstype_t *get_fstype(const char *);
     82   1.1   lukem static	void	usage(void);
     83   1.1   lukem int		main(int, char *[]);
     84   1.1   lukem 
     85   1.1   lukem int
     86   1.1   lukem main(int argc, char *argv[])
     87   1.1   lukem {
     88   1.1   lukem 	struct timeval	 start;
     89   1.1   lukem 	fstype_t	*fstype;
     90   1.1   lukem 	fsinfo_t	 fsoptions;
     91   1.1   lukem 	fsnode		*root;
     92   1.1   lukem 	int	 	 ch, len;
     93   1.1   lukem 	char		*specfile;
     94   1.8   lukem 
     95   1.8   lukem 	setprogname(argv[0]);
     96   1.1   lukem 
     97   1.1   lukem 	debug = 0;
     98   1.1   lukem 	if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL)
     99   1.1   lukem 		errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE);
    100   1.9   lukem 
    101   1.9   lukem 		/* set default fsoptions */
    102   1.1   lukem 	(void)memset(&fsoptions, 0, sizeof(fsoptions));
    103   1.1   lukem 	fsoptions.fd = -1;
    104   1.9   lukem 	fsoptions.sectorsize = -1;
    105   1.9   lukem 	fsoptions.bsize= -1;
    106   1.9   lukem 	fsoptions.fsize= -1;
    107   1.9   lukem 	fsoptions.cpg= -1;
    108   1.9   lukem 	fsoptions.density= -1;
    109   1.9   lukem 	fsoptions.ntracks= -1;
    110   1.9   lukem 	fsoptions.nsectors= -1;
    111   1.9   lukem 	fsoptions.rpm= -1;
    112   1.9   lukem 	fsoptions.minfree= -1;
    113   1.9   lukem 	fsoptions.optimization= -1;
    114   1.9   lukem 	fsoptions.maxcontig= -1;
    115   1.9   lukem 	fsoptions.rotdelay= -1;
    116   1.9   lukem 	fsoptions.maxbpg= -1;
    117   1.9   lukem 	fsoptions.nrpos= -1;
    118   1.9   lukem 	fsoptions.avgfilesize= -1;
    119   1.9   lukem 	fsoptions.avgfpdir= -1;
    120   1.9   lukem 
    121   1.1   lukem 	specfile = NULL;
    122   1.1   lukem 	if (gettimeofday(&start, NULL) == -1)
    123   1.1   lukem 		err(1, "Unable to get system time");
    124   1.1   lukem 	TIMEVAL_TO_TIMESPEC(&start, &start_time);
    125   1.1   lukem 
    126  1.11   lukem 	while ((ch = getopt(argc, argv, "B:b:d:f:F:M:m:N:o:s:S:t:")) != -1) {
    127   1.1   lukem 		switch (ch) {
    128   1.1   lukem 
    129   1.1   lukem 		case 'B':
    130   1.1   lukem 			if (strcmp(optarg, "be") == 0 ||
    131   1.1   lukem 			    strcmp(optarg, "big") == 0) {
    132   1.1   lukem #if BYTE_ORDER == LITTLE_ENDIAN
    133   1.1   lukem 				fsoptions.needswap = 1;
    134   1.1   lukem #endif
    135   1.1   lukem 			} else if (strcmp(optarg, "le") == 0 ||
    136   1.1   lukem 			    strcmp(optarg, "little") == 0) {
    137   1.1   lukem #if BYTE_ORDER == BIG_ENDIAN
    138   1.1   lukem 				fsoptions.needswap = 1;
    139   1.1   lukem #endif
    140   1.1   lukem 			} else {
    141   1.1   lukem 				warnx("Invalid endian `%s'.", optarg);
    142   1.1   lukem 				usage();
    143   1.1   lukem 			}
    144   1.1   lukem 			break;
    145   1.1   lukem 
    146   1.1   lukem 		case 'b':
    147   1.1   lukem 			len = strlen(optarg) - 1;
    148   1.1   lukem 			if (optarg[len] == '%') {
    149   1.1   lukem 				optarg[len] = '\0';
    150   1.1   lukem 				fsoptions.freeblockpc =
    151   1.6   lukem 				    strsuftoull("free block percentage",
    152   1.6   lukem 					optarg, 0, 99);
    153   1.1   lukem 			} else {
    154   1.6   lukem 				fsoptions.freeblocks =
    155   1.6   lukem 				    strsuftoull("free blocks",
    156   1.6   lukem 					optarg, 0, LLONG_MAX);
    157   1.1   lukem 			}
    158   1.1   lukem 			break;
    159   1.1   lukem 
    160   1.1   lukem 		case 'd':
    161   1.6   lukem 			debug =
    162   1.6   lukem 			    (int)strsuftoull("debug mask", optarg, 0, UINT_MAX);
    163   1.1   lukem 			break;
    164   1.1   lukem 
    165   1.1   lukem 		case 'f':
    166   1.1   lukem 			len = strlen(optarg) - 1;
    167   1.1   lukem 			if (optarg[len] == '%') {
    168   1.1   lukem 				optarg[len] = '\0';
    169   1.1   lukem 				fsoptions.freefilepc =
    170   1.6   lukem 				    strsuftoull("free file percentage",
    171   1.6   lukem 					optarg, 0, 99);
    172   1.1   lukem 			} else {
    173   1.6   lukem 				fsoptions.freefiles =
    174   1.6   lukem 				    strsuftoull("free files",
    175   1.6   lukem 					optarg, 0, LLONG_MAX);
    176   1.1   lukem 			}
    177   1.1   lukem 			break;
    178   1.1   lukem 
    179   1.1   lukem 		case 'F':
    180   1.1   lukem 			specfile = optarg;
    181   1.1   lukem 			break;
    182   1.1   lukem 
    183   1.1   lukem 		case 'M':
    184   1.6   lukem 			fsoptions.minsize =
    185   1.6   lukem 			    strsuftoull("minimum size", optarg, 1LL, LLONG_MAX);
    186   1.1   lukem 			break;
    187   1.1   lukem 
    188  1.11   lukem 		case 'N':
    189  1.11   lukem 			if (! setup_getid(optarg))
    190  1.11   lukem 				errx(1,
    191  1.11   lukem 			    "Unable to use user and group databases in `%s'",
    192  1.11   lukem 				    optarg);
    193  1.11   lukem 			break;
    194  1.11   lukem 
    195   1.1   lukem 		case 'm':
    196   1.6   lukem 			fsoptions.maxsize =
    197   1.6   lukem 			    strsuftoull("maximum size", optarg, 1LL, LLONG_MAX);
    198   1.1   lukem 			break;
    199   1.1   lukem 
    200   1.1   lukem 		case 'o':
    201   1.1   lukem 		{
    202   1.1   lukem 			char *p;
    203   1.1   lukem 
    204   1.1   lukem 			while ((p = strsep(&optarg, ",")) != NULL) {
    205   1.1   lukem 				if (*p == '\0')
    206   1.1   lukem 					errx(1, "Empty option");
    207   1.1   lukem 				if (! fstype->parse_options(p, &fsoptions))
    208   1.1   lukem 					usage();
    209   1.1   lukem 			}
    210   1.1   lukem 			break;
    211   1.1   lukem 		}
    212   1.1   lukem 
    213   1.1   lukem 		case 's':
    214   1.1   lukem 			fsoptions.minsize = fsoptions.maxsize =
    215   1.6   lukem 			    strsuftoull("size", optarg, 1LL, LLONG_MAX);
    216   1.1   lukem 			break;
    217   1.1   lukem 
    218   1.1   lukem 		case 'S':
    219   1.6   lukem 			fsoptions.sectorsize =
    220   1.6   lukem 			    (int)strsuftoull("sector size", optarg,
    221   1.6   lukem 				1LL, INT_MAX);
    222   1.1   lukem 			break;
    223   1.1   lukem 
    224   1.1   lukem 		case 't':
    225   1.1   lukem 			if ((fstype = get_fstype(optarg)) == NULL)
    226   1.1   lukem 				errx(1, "Unknown fs type `%s'.", optarg);
    227   1.1   lukem 			break;
    228   1.1   lukem 
    229   1.1   lukem 		case '?':
    230   1.1   lukem 		default:
    231   1.1   lukem 			usage();
    232   1.1   lukem 			/* NOTREACHED */
    233   1.1   lukem 
    234   1.1   lukem 		}
    235   1.1   lukem 	}
    236   1.1   lukem 	if (debug) {
    237   1.1   lukem 		printf("debug mask: 0x%08x\n", debug);
    238   1.1   lukem 		printf("start time: %ld.%ld, %s",
    239   1.3   lukem 		    (long)start_time.tv_sec, (long)start_time.tv_nsec,
    240   1.1   lukem 		    ctime(&start_time.tv_sec));
    241   1.1   lukem 	}
    242   1.1   lukem 	argc -= optind;
    243   1.1   lukem 	argv += optind;
    244   1.1   lukem 
    245   1.1   lukem 	if (argc != 2)
    246   1.1   lukem 		usage();
    247   1.1   lukem 
    248   1.1   lukem 				/* walk the tree */
    249   1.1   lukem 	TIMER_START(start);
    250   1.1   lukem 	root = walk_dir(argv[1], NULL);
    251   1.1   lukem 	TIMER_RESULTS(start, "walk_dir");
    252   1.1   lukem 
    253   1.1   lukem 	if (specfile) {		/* apply a specfile */
    254   1.1   lukem 		TIMER_START(start);
    255   1.1   lukem 		apply_specfile(specfile, argv[1], root);
    256   1.1   lukem 		TIMER_RESULTS(start, "apply_specfile");
    257   1.1   lukem 	}
    258   1.1   lukem 
    259   1.1   lukem 	if (debug & DEBUG_DUMP_FSNODES) {
    260  1.12   lukem 		printf("\nparent: %s\n", argv[1]);
    261  1.12   lukem 		dump_fsnodes(".", root);
    262   1.1   lukem 		putchar('\n');
    263   1.1   lukem 	}
    264   1.1   lukem 
    265   1.1   lukem 				/* build the file system */
    266   1.1   lukem 	TIMER_START(start);
    267   1.1   lukem 	fstype->make_fs(argv[0], argv[1], root, &fsoptions);
    268   1.1   lukem 	TIMER_RESULTS(start, "make_fs");
    269   1.1   lukem 
    270   1.1   lukem 	exit(0);
    271   1.1   lukem 	/* NOTREACHED */
    272   1.1   lukem }
    273   1.1   lukem 
    274   1.1   lukem 
    275   1.1   lukem int
    276   1.1   lukem set_option(option_t *options, const char *var, const char *val)
    277   1.1   lukem {
    278   1.1   lukem 	int	i;
    279   1.1   lukem 
    280   1.1   lukem 	for (i = 0; options[i].name != NULL; i++) {
    281   1.1   lukem 		if (strcmp(options[i].name, var) != 0)
    282   1.1   lukem 			continue;
    283   1.6   lukem 		*options[i].value = (int)strsuftoull(options[i].desc, val,
    284   1.1   lukem 		    options[i].minimum, options[i].maximum);
    285   1.1   lukem 		return (1);
    286   1.1   lukem 	}
    287   1.1   lukem 	warnx("Unknown option `%s'", var);
    288   1.1   lukem 	return (0);
    289   1.1   lukem }
    290   1.1   lukem 
    291   1.1   lukem 
    292   1.1   lukem static fstype_t *
    293   1.1   lukem get_fstype(const char *type)
    294   1.1   lukem {
    295   1.1   lukem 	int i;
    296   1.1   lukem 
    297   1.1   lukem 	for (i = 0; fstypes[i].type != NULL; i++)
    298   1.1   lukem 		if (strcmp(fstypes[i].type, type) == 0)
    299   1.1   lukem 			return (&fstypes[i]);
    300   1.1   lukem 	return (NULL);
    301   1.1   lukem }
    302   1.1   lukem 
    303   1.1   lukem static void
    304   1.1   lukem usage(void)
    305   1.1   lukem {
    306   1.1   lukem 	const char *prog;
    307   1.1   lukem 
    308   1.1   lukem 	prog = getprogname();
    309   1.1   lukem 	fprintf(stderr,
    310   1.1   lukem "Usage: %s [-t fs-type] [-o fs-options] [-d debug-mask] [-B endian]\n"
    311   1.1   lukem "\t[-S sector-size] [-M minimum-size] [-m maximum-size] [-s image-size]\n"
    312  1.11   lukem "\t[-b free-blocks] [-f free-files] [-F mtree-specfile] [-N userdb-dir]\n"
    313   1.1   lukem "\timage-file directory\n",
    314   1.1   lukem 	    prog);
    315   1.1   lukem 	exit(1);
    316   1.1   lukem }
    317