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