1 /* $NetBSD: makefs.c,v 1.20 2004/06/20 22:20:18 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.20 2004/06/20 22:20:18 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 int (*parse_options)(const char *, fsinfo_t *); 65 void (*make_fs)(const char *, const char *, fsnode *, 66 fsinfo_t *); 67 } fstype_t; 68 69 static fstype_t fstypes[] = { 70 { "ffs", ffs_parse_opts, ffs_makefs }, 71 { NULL }, 72 }; 73 74 u_int debug; 75 struct timespec start_time; 76 77 static fstype_t *get_fstype(const char *); 78 static void usage(void); 79 int main(int, char *[]); 80 81 int 82 main(int argc, char *argv[]) 83 { 84 struct timeval start; 85 fstype_t *fstype; 86 fsinfo_t fsoptions; 87 fsnode *root; 88 int ch, len; 89 char *specfile; 90 91 setprogname(argv[0]); 92 93 debug = 0; 94 if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL) 95 errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE); 96 97 /* set default fsoptions */ 98 (void)memset(&fsoptions, 0, sizeof(fsoptions)); 99 fsoptions.fd = -1; 100 fsoptions.sectorsize = -1; 101 fsoptions.bsize= -1; 102 fsoptions.fsize= -1; 103 fsoptions.cpg= -1; 104 fsoptions.density= -1; 105 fsoptions.minfree= -1; 106 fsoptions.optimization= -1; 107 fsoptions.maxcontig= -1; 108 fsoptions.maxbpg= -1; 109 fsoptions.avgfilesize= -1; 110 fsoptions.avgfpdir= -1; 111 fsoptions.version = 1; 112 113 specfile = NULL; 114 if (gettimeofday(&start, NULL) == -1) 115 err(1, "Unable to get system time"); 116 117 start_time.tv_sec = start.tv_sec; 118 start_time.tv_nsec = start.tv_usec * 1000; 119 120 while ((ch = getopt(argc, argv, "B:b:d:f:F:M:m:N:o:s:S:t:x")) != -1) { 121 switch (ch) { 122 123 case 'B': 124 if (strcmp(optarg, "be") == 0 || 125 strcmp(optarg, "4321") == 0 || 126 strcmp(optarg, "big") == 0) { 127 #if BYTE_ORDER == LITTLE_ENDIAN 128 fsoptions.needswap = 1; 129 #endif 130 } else if (strcmp(optarg, "le") == 0 || 131 strcmp(optarg, "1234") == 0 || 132 strcmp(optarg, "little") == 0) { 133 #if BYTE_ORDER == BIG_ENDIAN 134 fsoptions.needswap = 1; 135 #endif 136 } else { 137 warnx("Invalid endian `%s'.", optarg); 138 usage(); 139 } 140 break; 141 142 case 'b': 143 len = strlen(optarg) - 1; 144 if (optarg[len] == '%') { 145 optarg[len] = '\0'; 146 fsoptions.freeblockpc = 147 strsuftoll("free block percentage", 148 optarg, 0, 99); 149 } else { 150 fsoptions.freeblocks = 151 strsuftoll("free blocks", 152 optarg, 0, LLONG_MAX); 153 } 154 break; 155 156 case 'd': 157 debug = 158 (int)strsuftoll("debug mask", optarg, 0, UINT_MAX); 159 break; 160 161 case 'f': 162 len = strlen(optarg) - 1; 163 if (optarg[len] == '%') { 164 optarg[len] = '\0'; 165 fsoptions.freefilepc = 166 strsuftoll("free file percentage", 167 optarg, 0, 99); 168 } else { 169 fsoptions.freefiles = 170 strsuftoll("free files", 171 optarg, 0, LLONG_MAX); 172 } 173 break; 174 175 case 'F': 176 specfile = optarg; 177 break; 178 179 case 'M': 180 fsoptions.minsize = 181 strsuftoll("minimum size", optarg, 1LL, LLONG_MAX); 182 break; 183 184 case 'N': 185 if (! setup_getid(optarg)) 186 errx(1, 187 "Unable to use user and group databases in `%s'", 188 optarg); 189 break; 190 191 case 'm': 192 fsoptions.maxsize = 193 strsuftoll("maximum size", optarg, 1LL, LLONG_MAX); 194 break; 195 196 case 'o': 197 { 198 char *p; 199 200 while ((p = strsep(&optarg, ",")) != NULL) { 201 if (*p == '\0') 202 errx(1, "Empty option"); 203 if (! fstype->parse_options(p, &fsoptions)) 204 usage(); 205 } 206 break; 207 } 208 209 case 's': 210 fsoptions.minsize = fsoptions.maxsize = 211 strsuftoll("size", optarg, 1LL, LLONG_MAX); 212 break; 213 214 case 'S': 215 fsoptions.sectorsize = 216 (int)strsuftoll("sector size", optarg, 217 1LL, INT_MAX); 218 break; 219 220 case 't': 221 if ((fstype = get_fstype(optarg)) == NULL) 222 errx(1, "Unknown fs type `%s'.", optarg); 223 break; 224 225 case 'x': 226 fsoptions.onlyspec = 1; 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 /* -x must be accompanied by -F */ 249 if (fsoptions.onlyspec != 0 && specfile == NULL) 250 errx(1, "-x requires -F mtree-specfile."); 251 252 /* walk the tree */ 253 TIMER_START(start); 254 root = walk_dir(argv[1], NULL); 255 TIMER_RESULTS(start, "walk_dir"); 256 257 if (specfile) { /* apply a specfile */ 258 TIMER_START(start); 259 apply_specfile(specfile, argv[1], root); 260 TIMER_RESULTS(start, "apply_specfile"); 261 } 262 263 if (debug & DEBUG_DUMP_FSNODES) { 264 printf("\nparent: %s\n", argv[1]); 265 dump_fsnodes(".", root); 266 putchar('\n'); 267 } 268 269 /* build the file system */ 270 TIMER_START(start); 271 fstype->make_fs(argv[0], argv[1], root, &fsoptions); 272 TIMER_RESULTS(start, "make_fs"); 273 274 exit(0); 275 /* NOTREACHED */ 276 } 277 278 279 int 280 set_option(option_t *options, const char *var, const char *val) 281 { 282 int i; 283 284 for (i = 0; options[i].name != NULL; i++) { 285 if (strcmp(options[i].name, var) != 0) 286 continue; 287 *options[i].value = (int)strsuftoll(options[i].desc, val, 288 options[i].minimum, options[i].maximum); 289 return (1); 290 } 291 warnx("Unknown option `%s'", var); 292 return (0); 293 } 294 295 296 static fstype_t * 297 get_fstype(const char *type) 298 { 299 int i; 300 301 for (i = 0; fstypes[i].type != NULL; i++) 302 if (strcmp(fstypes[i].type, type) == 0) 303 return (&fstypes[i]); 304 return (NULL); 305 } 306 307 static void 308 usage(void) 309 { 310 const char *prog; 311 312 prog = getprogname(); 313 fprintf(stderr, 314 "usage: %s [-t fs-type] [-o fs-options] [-d debug-mask] [-B endian]\n" 315 "\t[-S sector-size] [-M minimum-size] [-m maximum-size] [-s image-size]\n" 316 "\t[-b free-blocks] [-f free-files] [-F mtree-specfile] [-x]\n" 317 "\t[-N userdb-dir] image-file directory\n", 318 prog); 319 exit(1); 320 } 321