makefs.c revision 1.21 1 1.21 jmc /* $NetBSD: makefs.c,v 1.21 2004/12/20 20:51:42 jmc Exp $ */
2 1.1 lukem
3 1.1 lukem /*
4 1.15 lukem * Copyright (c) 2001-2003 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.20 jmc #if HAVE_NBTOOL_CONFIG_H
39 1.20 jmc #include "nbtool_config.h"
40 1.20 jmc #endif
41 1.20 jmc
42 1.2 lukem #include <sys/cdefs.h>
43 1.13 tv #if defined(__RCSID) && !defined(__lint)
44 1.21 jmc __RCSID("$NetBSD: makefs.c,v 1.21 2004/12/20 20:51:42 jmc Exp $");
45 1.2 lukem #endif /* !__lint */
46 1.10 is
47 1.1 lukem #include <assert.h>
48 1.1 lukem #include <ctype.h>
49 1.1 lukem #include <errno.h>
50 1.5 simonb #include <limits.h>
51 1.1 lukem #include <stdio.h>
52 1.1 lukem #include <stdlib.h>
53 1.1 lukem #include <string.h>
54 1.1 lukem #include <unistd.h>
55 1.1 lukem
56 1.1 lukem #include "makefs.h"
57 1.11 lukem #include "mtree.h"
58 1.1 lukem
59 1.1 lukem /*
60 1.1 lukem * list of supported file systems and dispatch functions
61 1.1 lukem */
62 1.1 lukem typedef struct {
63 1.1 lukem const char *type;
64 1.21 jmc void (*prepare_options)(fsinfo_t *);
65 1.1 lukem int (*parse_options)(const char *, fsinfo_t *);
66 1.21 jmc void (*cleanup_options)(fsinfo_t *);
67 1.1 lukem void (*make_fs)(const char *, const char *, fsnode *,
68 1.1 lukem fsinfo_t *);
69 1.1 lukem } fstype_t;
70 1.1 lukem
71 1.1 lukem static fstype_t fstypes[] = {
72 1.21 jmc { "ffs", ffs_prep_opts, ffs_parse_opts, ffs_cleanup_opts, ffs_makefs },
73 1.1 lukem { NULL },
74 1.1 lukem };
75 1.1 lukem
76 1.20 jmc u_int debug;
77 1.1 lukem struct timespec start_time;
78 1.1 lukem
79 1.1 lukem static fstype_t *get_fstype(const char *);
80 1.1 lukem static void usage(void);
81 1.1 lukem int main(int, char *[]);
82 1.1 lukem
83 1.1 lukem int
84 1.1 lukem main(int argc, char *argv[])
85 1.1 lukem {
86 1.1 lukem struct timeval start;
87 1.1 lukem fstype_t *fstype;
88 1.1 lukem fsinfo_t fsoptions;
89 1.1 lukem fsnode *root;
90 1.1 lukem int ch, len;
91 1.1 lukem char *specfile;
92 1.8 lukem
93 1.8 lukem setprogname(argv[0]);
94 1.1 lukem
95 1.1 lukem debug = 0;
96 1.1 lukem if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL)
97 1.1 lukem errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE);
98 1.9 lukem
99 1.9 lukem /* set default fsoptions */
100 1.1 lukem (void)memset(&fsoptions, 0, sizeof(fsoptions));
101 1.1 lukem fsoptions.fd = -1;
102 1.9 lukem fsoptions.sectorsize = -1;
103 1.21 jmc
104 1.21 jmc if (fstype->prepare_options)
105 1.21 jmc fstype->prepare_options(&fsoptions);
106 1.9 lukem
107 1.1 lukem specfile = NULL;
108 1.1 lukem if (gettimeofday(&start, NULL) == -1)
109 1.1 lukem err(1, "Unable to get system time");
110 1.13 tv
111 1.13 tv start_time.tv_sec = start.tv_sec;
112 1.13 tv start_time.tv_nsec = start.tv_usec * 1000;
113 1.1 lukem
114 1.16 thorpej while ((ch = getopt(argc, argv, "B:b:d:f:F:M:m:N:o:s:S:t:x")) != -1) {
115 1.1 lukem switch (ch) {
116 1.1 lukem
117 1.1 lukem case 'B':
118 1.1 lukem if (strcmp(optarg, "be") == 0 ||
119 1.15 lukem strcmp(optarg, "4321") == 0 ||
120 1.1 lukem strcmp(optarg, "big") == 0) {
121 1.1 lukem #if BYTE_ORDER == LITTLE_ENDIAN
122 1.1 lukem fsoptions.needswap = 1;
123 1.1 lukem #endif
124 1.1 lukem } else if (strcmp(optarg, "le") == 0 ||
125 1.15 lukem strcmp(optarg, "1234") == 0 ||
126 1.1 lukem strcmp(optarg, "little") == 0) {
127 1.1 lukem #if BYTE_ORDER == BIG_ENDIAN
128 1.1 lukem fsoptions.needswap = 1;
129 1.1 lukem #endif
130 1.1 lukem } else {
131 1.1 lukem warnx("Invalid endian `%s'.", optarg);
132 1.1 lukem usage();
133 1.1 lukem }
134 1.1 lukem break;
135 1.1 lukem
136 1.1 lukem case 'b':
137 1.1 lukem len = strlen(optarg) - 1;
138 1.1 lukem if (optarg[len] == '%') {
139 1.1 lukem optarg[len] = '\0';
140 1.1 lukem fsoptions.freeblockpc =
141 1.14 lukem strsuftoll("free block percentage",
142 1.6 lukem optarg, 0, 99);
143 1.1 lukem } else {
144 1.6 lukem fsoptions.freeblocks =
145 1.14 lukem strsuftoll("free blocks",
146 1.6 lukem optarg, 0, LLONG_MAX);
147 1.1 lukem }
148 1.1 lukem break;
149 1.1 lukem
150 1.1 lukem case 'd':
151 1.6 lukem debug =
152 1.14 lukem (int)strsuftoll("debug mask", optarg, 0, UINT_MAX);
153 1.1 lukem break;
154 1.1 lukem
155 1.1 lukem case 'f':
156 1.1 lukem len = strlen(optarg) - 1;
157 1.1 lukem if (optarg[len] == '%') {
158 1.1 lukem optarg[len] = '\0';
159 1.1 lukem fsoptions.freefilepc =
160 1.14 lukem strsuftoll("free file percentage",
161 1.6 lukem optarg, 0, 99);
162 1.1 lukem } else {
163 1.6 lukem fsoptions.freefiles =
164 1.14 lukem strsuftoll("free files",
165 1.6 lukem optarg, 0, LLONG_MAX);
166 1.1 lukem }
167 1.1 lukem break;
168 1.1 lukem
169 1.1 lukem case 'F':
170 1.1 lukem specfile = optarg;
171 1.1 lukem break;
172 1.1 lukem
173 1.1 lukem case 'M':
174 1.6 lukem fsoptions.minsize =
175 1.14 lukem strsuftoll("minimum size", optarg, 1LL, LLONG_MAX);
176 1.1 lukem break;
177 1.1 lukem
178 1.11 lukem case 'N':
179 1.11 lukem if (! setup_getid(optarg))
180 1.11 lukem errx(1,
181 1.11 lukem "Unable to use user and group databases in `%s'",
182 1.11 lukem optarg);
183 1.11 lukem break;
184 1.11 lukem
185 1.1 lukem case 'm':
186 1.6 lukem fsoptions.maxsize =
187 1.14 lukem strsuftoll("maximum size", optarg, 1LL, LLONG_MAX);
188 1.1 lukem break;
189 1.1 lukem
190 1.1 lukem case 'o':
191 1.1 lukem {
192 1.1 lukem char *p;
193 1.1 lukem
194 1.1 lukem while ((p = strsep(&optarg, ",")) != NULL) {
195 1.1 lukem if (*p == '\0')
196 1.1 lukem errx(1, "Empty option");
197 1.1 lukem if (! fstype->parse_options(p, &fsoptions))
198 1.1 lukem usage();
199 1.1 lukem }
200 1.1 lukem break;
201 1.1 lukem }
202 1.1 lukem
203 1.1 lukem case 's':
204 1.1 lukem fsoptions.minsize = fsoptions.maxsize =
205 1.14 lukem strsuftoll("size", optarg, 1LL, LLONG_MAX);
206 1.1 lukem break;
207 1.1 lukem
208 1.1 lukem case 'S':
209 1.6 lukem fsoptions.sectorsize =
210 1.14 lukem (int)strsuftoll("sector size", optarg,
211 1.6 lukem 1LL, INT_MAX);
212 1.1 lukem break;
213 1.1 lukem
214 1.1 lukem case 't':
215 1.21 jmc /* Check current one and cleanup if necessary. */
216 1.21 jmc if (fstype->cleanup_options)
217 1.21 jmc fstype->cleanup_options(&fsoptions);
218 1.21 jmc fsoptions.fs_specific = NULL;
219 1.1 lukem if ((fstype = get_fstype(optarg)) == NULL)
220 1.1 lukem errx(1, "Unknown fs type `%s'.", optarg);
221 1.21 jmc fstype->prepare_options(&fsoptions);
222 1.1 lukem break;
223 1.1 lukem
224 1.16 thorpej case 'x':
225 1.17 lukem fsoptions.onlyspec = 1;
226 1.16 thorpej break;
227 1.16 thorpej
228 1.1 lukem case '?':
229 1.1 lukem default:
230 1.1 lukem usage();
231 1.1 lukem /* NOTREACHED */
232 1.1 lukem
233 1.1 lukem }
234 1.1 lukem }
235 1.1 lukem if (debug) {
236 1.1 lukem printf("debug mask: 0x%08x\n", debug);
237 1.1 lukem printf("start time: %ld.%ld, %s",
238 1.3 lukem (long)start_time.tv_sec, (long)start_time.tv_nsec,
239 1.1 lukem ctime(&start_time.tv_sec));
240 1.1 lukem }
241 1.1 lukem argc -= optind;
242 1.1 lukem argv += optind;
243 1.1 lukem
244 1.1 lukem if (argc != 2)
245 1.1 lukem usage();
246 1.16 thorpej
247 1.16 thorpej /* -x must be accompanied by -F */
248 1.17 lukem if (fsoptions.onlyspec != 0 && specfile == NULL)
249 1.16 thorpej errx(1, "-x requires -F mtree-specfile.");
250 1.1 lukem
251 1.1 lukem /* walk the tree */
252 1.1 lukem TIMER_START(start);
253 1.1 lukem root = walk_dir(argv[1], NULL);
254 1.1 lukem TIMER_RESULTS(start, "walk_dir");
255 1.1 lukem
256 1.1 lukem if (specfile) { /* apply a specfile */
257 1.1 lukem TIMER_START(start);
258 1.1 lukem apply_specfile(specfile, argv[1], root);
259 1.1 lukem TIMER_RESULTS(start, "apply_specfile");
260 1.1 lukem }
261 1.1 lukem
262 1.1 lukem if (debug & DEBUG_DUMP_FSNODES) {
263 1.12 lukem printf("\nparent: %s\n", argv[1]);
264 1.12 lukem dump_fsnodes(".", root);
265 1.1 lukem putchar('\n');
266 1.1 lukem }
267 1.1 lukem
268 1.1 lukem /* build the file system */
269 1.1 lukem TIMER_START(start);
270 1.1 lukem fstype->make_fs(argv[0], argv[1], root, &fsoptions);
271 1.1 lukem TIMER_RESULTS(start, "make_fs");
272 1.1 lukem
273 1.1 lukem exit(0);
274 1.1 lukem /* NOTREACHED */
275 1.1 lukem }
276 1.1 lukem
277 1.1 lukem
278 1.1 lukem int
279 1.1 lukem set_option(option_t *options, const char *var, const char *val)
280 1.1 lukem {
281 1.1 lukem int i;
282 1.1 lukem
283 1.1 lukem for (i = 0; options[i].name != NULL; i++) {
284 1.1 lukem if (strcmp(options[i].name, var) != 0)
285 1.1 lukem continue;
286 1.14 lukem *options[i].value = (int)strsuftoll(options[i].desc, val,
287 1.1 lukem options[i].minimum, options[i].maximum);
288 1.1 lukem return (1);
289 1.1 lukem }
290 1.1 lukem warnx("Unknown option `%s'", var);
291 1.1 lukem return (0);
292 1.1 lukem }
293 1.1 lukem
294 1.1 lukem
295 1.1 lukem static fstype_t *
296 1.1 lukem get_fstype(const char *type)
297 1.1 lukem {
298 1.1 lukem int i;
299 1.1 lukem
300 1.1 lukem for (i = 0; fstypes[i].type != NULL; i++)
301 1.1 lukem if (strcmp(fstypes[i].type, type) == 0)
302 1.1 lukem return (&fstypes[i]);
303 1.1 lukem return (NULL);
304 1.1 lukem }
305 1.1 lukem
306 1.1 lukem static void
307 1.1 lukem usage(void)
308 1.1 lukem {
309 1.1 lukem const char *prog;
310 1.1 lukem
311 1.1 lukem prog = getprogname();
312 1.1 lukem fprintf(stderr,
313 1.19 jmmv "usage: %s [-t fs-type] [-o fs-options] [-d debug-mask] [-B endian]\n"
314 1.1 lukem "\t[-S sector-size] [-M minimum-size] [-m maximum-size] [-s image-size]\n"
315 1.17 lukem "\t[-b free-blocks] [-f free-files] [-F mtree-specfile] [-x]\n"
316 1.17 lukem "\t[-N userdb-dir] image-file directory\n",
317 1.1 lukem prog);
318 1.1 lukem exit(1);
319 1.1 lukem }
320