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