Commit | Line | Data |
---|---|---|
c018628f DF |
1 | /* |
2 | * Copyright (c) 1983 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
7 | #ifndef lint | |
8 | char copyright[] = | |
9 | "@(#) Copyright (c) 1983 Regents of the University of California.\n\ | |
10 | All rights reserved.\n"; | |
11 | #endif not lint | |
12 | ||
a54c0b3f | 13 | #ifndef lint |
75e589fc | 14 | static char sccsid[] = "@(#)newfs.c 5.2 (Berkeley) %G%"; |
c018628f | 15 | #endif not lint |
a54c0b3f SL |
16 | |
17 | /* | |
c6003316 | 18 | * newfs: friendly front end to mkfs |
a54c0b3f SL |
19 | */ |
20 | #include <sys/param.h> | |
21 | #include <sys/stat.h> | |
22 | #include <sys/fs.h> | |
14b4eb74 | 23 | #include <sys/dir.h> |
a54c0b3f SL |
24 | |
25 | #include <stdio.h> | |
a54c0b3f SL |
26 | #include <disktab.h> |
27 | ||
89241117 SL |
28 | #define BOOTDIR "/usr/mdec" /* directory for boot blocks */ |
29 | ||
28b1d98d | 30 | int Nflag; /* run mkfs without writing file system */ |
a54c0b3f | 31 | int verbose; /* show mkfs line before exec */ |
11b2fe08 | 32 | int noboot; /* do not fill boot blocks */ |
a54c0b3f SL |
33 | int fssize; /* file system size */ |
34 | int fsize; /* fragment size */ | |
35 | int bsize; /* block size */ | |
36 | int ntracks; /* # tracks/cylinder */ | |
37 | int nsectors; /* # sectors/track */ | |
38 | int sectorsize; /* bytes/sector */ | |
39 | int cpg; /* cylinders/cylinder group */ | |
11b2fe08 | 40 | int minfree = -1; /* free space threshold */ |
75e589fc | 41 | int opt; /* optimization preference (space or time) */ |
c6003316 | 42 | int rpm; /* revolutions/minute of drive */ |
685a1465 | 43 | int density; /* number of bytes per inode */ |
a54c0b3f SL |
44 | |
45 | char *av[20]; /* argv array and buffers for exec */ | |
46 | char a2[20]; | |
47 | char a3[20]; | |
48 | char a4[20]; | |
49 | char a5[20]; | |
50 | char a6[20]; | |
51 | char a7[20]; | |
c6003316 SL |
52 | char a8[20]; |
53 | char a9[20]; | |
685a1465 | 54 | char a10[20]; |
a54c0b3f SL |
55 | char device[MAXPATHLEN]; |
56 | char cmd[BUFSIZ]; | |
57 | ||
58 | char *index(); | |
59 | char *rindex(); | |
60 | char *sprintf(); | |
61 | ||
62 | main(argc, argv) | |
1b8b6e44 | 63 | int argc; |
a54c0b3f SL |
64 | char *argv[]; |
65 | { | |
66 | char *cp, *special; | |
67 | register struct disktab *dp; | |
68 | register struct partition *pp; | |
69 | struct stat st; | |
70 | register int i; | |
71 | int status; | |
72 | ||
73 | argc--, argv++; | |
74 | while (argc > 0 && argv[0][0] == '-') { | |
75 | for (cp = &argv[0][1]; *cp; cp++) | |
76 | switch (*cp) { | |
77 | ||
78 | case 'v': | |
79 | verbose++; | |
80 | break; | |
81 | ||
28b1d98d KM |
82 | case 'N': |
83 | Nflag++; | |
84 | /* fall through to */ | |
85 | ||
11b2fe08 HS |
86 | case 'n': |
87 | noboot++; | |
88 | break; | |
89 | ||
a54c0b3f SL |
90 | case 's': |
91 | if (argc < 1) | |
92 | fatal("-s: missing file system size"); | |
93 | argc--, argv++; | |
94 | fssize = atoi(*argv); | |
95 | if (fssize < 0) | |
96 | fatal("%s: bad file system size", | |
97 | *argv); | |
98 | goto next; | |
99 | ||
100 | case 't': | |
101 | if (argc < 1) | |
102 | fatal("-t: missing track total"); | |
103 | argc--, argv++; | |
104 | ntracks = atoi(*argv); | |
105 | if (ntracks < 0) | |
106 | fatal("%s: bad total tracks", *argv); | |
107 | goto next; | |
108 | ||
75e589fc KM |
109 | case 'o': |
110 | if (argc < 1) | |
111 | fatal("-o: missing optimization preference"); | |
112 | argc--, argv++; | |
113 | if (strcmp(*argv, "space") == 0) | |
114 | opt = FS_OPTSPACE; | |
115 | else if (strcmp(*argv, "time") == 0) | |
116 | opt = FS_OPTTIME; | |
117 | else | |
118 | fatal("%s: bad optimization preference %s", | |
119 | *argv, | |
120 | "(options are `space' or `time')"); | |
121 | goto next; | |
122 | ||
a54c0b3f SL |
123 | case 'b': |
124 | if (argc < 1) | |
125 | fatal("-b: missing block size"); | |
126 | argc--, argv++; | |
127 | bsize = atoi(*argv); | |
128 | if (bsize < 0 || bsize < MINBSIZE) | |
129 | fatal("%s: bad block size", *argv); | |
130 | goto next; | |
131 | ||
132 | case 'f': | |
133 | if (argc < 1) | |
134 | fatal("-f: missing frag size"); | |
135 | argc--, argv++; | |
136 | fsize = atoi(*argv); | |
137 | if (fsize < 0) | |
138 | fatal("%s: bad frag size", *argv); | |
139 | goto next; | |
140 | ||
141 | case 'S': | |
142 | if (argc < 1) | |
143 | fatal("-S: missing sector size"); | |
144 | argc--, argv++; | |
145 | sectorsize = atoi(*argv); | |
146 | if (sectorsize < 0) | |
147 | fatal("%s: bad sector size", *argv); | |
148 | goto next; | |
149 | ||
150 | case 'c': | |
151 | if (argc < 1) | |
152 | fatal("-c: missing cylinders/group"); | |
153 | argc--, argv++; | |
154 | cpg = atoi(*argv); | |
155 | if (cpg < 0) | |
156 | fatal("%s: bad cylinders/group", *argv); | |
157 | goto next; | |
158 | ||
c6003316 SL |
159 | case 'm': |
160 | if (argc < 1) | |
161 | fatal("-m: missing free space %%\n"); | |
162 | argc--, argv++; | |
163 | minfree = atoi(*argv); | |
164 | if (minfree < 0 || minfree > 99) | |
165 | fatal("%s: bad free space %%\n", | |
166 | *argv); | |
167 | goto next; | |
168 | ||
169 | case 'r': | |
170 | if (argc < 1) | |
171 | fatal("-r: missing revs/minute\n"); | |
172 | argc--, argv++; | |
173 | rpm = atoi(*argv); | |
174 | if (rpm < 0) | |
175 | fatal("%s: bad revs/minute\n", *argv); | |
176 | goto next; | |
177 | ||
685a1465 KM |
178 | case 'i': |
179 | if (argc < 1) | |
180 | fatal("-i: missing bytes per inode\n"); | |
181 | argc--, argv++; | |
182 | density = atoi(*argv); | |
183 | if (density < 0) | |
184 | fatal("%s: bad bytes per inode\n", | |
185 | *argv); | |
186 | goto next; | |
187 | ||
a54c0b3f SL |
188 | default: |
189 | fatal("-%c: unknown flag", cp); | |
190 | } | |
191 | next: | |
192 | argc--, argv++; | |
193 | } | |
194 | if (argc < 2) { | |
c6003316 | 195 | fprintf(stderr, "usage: newfs [ -v ] [ mkfs-options ] %s\n", |
a54c0b3f SL |
196 | "special-device device-type"); |
197 | fprintf(stderr, "where mkfs-options are:\n"); | |
28b1d98d KM |
198 | fprintf(stderr, "\t-N do not create file system, %s\n", |
199 | "just print out parameters"); | |
e6b4584b SL |
200 | fprintf(stderr, "\t-s file system size (sectors)\n"); |
201 | fprintf(stderr, "\t-b block size\n"); | |
202 | fprintf(stderr, "\t-f frag size\n"); | |
2d2a9370 SL |
203 | fprintf(stderr, "\t-t tracks/cylinder\n"); |
204 | fprintf(stderr, "\t-c cylinders/group\n"); | |
c6003316 | 205 | fprintf(stderr, "\t-m minimum free space %%\n"); |
75e589fc KM |
206 | fprintf(stderr, "\t-o optimization preference %s\n", |
207 | "(`space' or `time')"); | |
c6003316 | 208 | fprintf(stderr, "\t-r revolutions/minute\n"); |
e6b4584b | 209 | fprintf(stderr, "\t-S sector size\n"); |
685a1465 | 210 | fprintf(stderr, "\t-i number of bytes per inode\n"); |
a54c0b3f SL |
211 | exit(1); |
212 | } | |
213 | special = argv[0]; | |
1b8b6e44 KM |
214 | cp = rindex(special, '/'); |
215 | if (cp != 0) | |
216 | special = cp + 1; | |
2f56e34c | 217 | if (*special == 'r' && special[1] != 'a' && special[1] != 'b') |
1b8b6e44 KM |
218 | special++; |
219 | special = sprintf(device, "/dev/r%s", special); | |
a54c0b3f | 220 | if (stat(special, &st) < 0) { |
c6003316 | 221 | fprintf(stderr, "newfs: "); perror(special); |
a54c0b3f SL |
222 | exit(2); |
223 | } | |
1b8b6e44 KM |
224 | if ((st.st_mode & S_IFMT) != S_IFCHR) |
225 | fatal("%s: not a character device", special); | |
a54c0b3f SL |
226 | dp = getdiskbyname(argv[1]); |
227 | if (dp == 0) | |
228 | fatal("%s: unknown disk type", argv[1]); | |
229 | cp = index(argv[0], '\0') - 1; | |
230 | if (cp == 0 || *cp < 'a' || *cp > 'h') | |
231 | fatal("%s: can't figure out file system partition", argv[0]); | |
232 | pp = &dp->d_partitions[*cp - 'a']; | |
233 | if (fssize == 0) { | |
234 | fssize = pp->p_size; | |
235 | if (fssize < 0) | |
236 | fatal("%s: no default size for `%c' partition", | |
237 | argv[1], *cp); | |
238 | } | |
239 | if (nsectors == 0) { | |
240 | nsectors = dp->d_nsectors; | |
241 | if (nsectors < 0) | |
242 | fatal("%s: no default #sectors/track", argv[1]); | |
243 | } | |
244 | if (ntracks == 0) { | |
245 | ntracks = dp->d_ntracks; | |
246 | if (ntracks < 0) | |
247 | fatal("%s: no default #tracks", argv[1]); | |
248 | } | |
249 | if (sectorsize == 0) { | |
250 | sectorsize = dp->d_secsize; | |
251 | if (sectorsize < 0) | |
252 | fatal("%s: no default sector size", argv[1]); | |
253 | } | |
254 | if (bsize == 0) { | |
255 | bsize = pp->p_bsize; | |
256 | if (bsize < 0) | |
257 | fatal("%s: no default block size for `%c' partition", | |
258 | argv[1], *cp); | |
259 | } | |
260 | if (fsize == 0) { | |
261 | fsize = pp->p_fsize; | |
262 | if (fsize < 0) | |
263 | fatal("%s: no default frag size for `%c' partition", | |
264 | argv[1], *cp); | |
265 | } | |
c6003316 SL |
266 | if (rpm == 0) { |
267 | rpm = dp->d_rpm; | |
268 | if (rpm < 0) | |
269 | fatal("%s: no default revolutions/minute value", | |
270 | argv[1]); | |
271 | } | |
685a1465 KM |
272 | if (density <= 0) |
273 | density = 2048; | |
11b2fe08 | 274 | if (minfree < 0) |
c6003316 | 275 | minfree = 10; |
75e589fc KM |
276 | if (minfree < 10 && opt != FS_OPTSPACE) { |
277 | fprintf(stderr, "setting optimization for space "); | |
278 | fprintf(stderr, "with minfree less than 10%\n"); | |
279 | opt = FS_OPTSPACE; | |
280 | } | |
c6003316 SL |
281 | if (cpg == 0) |
282 | cpg = 16; | |
a54c0b3f | 283 | i = 0; |
28b1d98d KM |
284 | if (Nflag) |
285 | av[i++] = "-N"; | |
286 | av[i++] = special; | |
a54c0b3f SL |
287 | av[i++] = sprintf(a2, "%d", fssize); |
288 | av[i++] = sprintf(a3, "%d", nsectors); | |
289 | av[i++] = sprintf(a4, "%d", ntracks); | |
290 | av[i++] = sprintf(a5, "%d", bsize); | |
291 | av[i++] = sprintf(a6, "%d", fsize); | |
c6003316 SL |
292 | av[i++] = sprintf(a7, "%d", cpg); |
293 | av[i++] = sprintf(a8, "%d", minfree); | |
294 | av[i++] = sprintf(a9, "%d", rpm / 60); | |
685a1465 | 295 | av[i++] = sprintf(a10, "%d", density); |
75e589fc | 296 | av[i++] = opt == FS_OPTSPACE ? "s" : "t"; |
a54c0b3f | 297 | av[i++] = 0; |
28b1d98d | 298 | strcpy(cmd, "/etc/mkfs"); |
a54c0b3f SL |
299 | for (i = 0; av[i] != 0; i++) { |
300 | strcat(cmd, " "); | |
301 | strcat(cmd, av[i]); | |
302 | } | |
303 | if (verbose) | |
304 | printf("%s\n", cmd); | |
a54c0b3f | 305 | if (status = system(cmd)) |
b7069ef9 | 306 | exit(status >> 8); |
11b2fe08 | 307 | if (*cp == 'a' && !noboot) { |
a54c0b3f | 308 | char type[3]; |
d314bb68 | 309 | struct stat sb; |
a54c0b3f SL |
310 | |
311 | cp = rindex(special, '/'); | |
312 | if (cp == NULL) | |
313 | fatal("%s: can't figure out disk type from name", | |
314 | special); | |
d314bb68 | 315 | if (stat(special, &sb) >= 0 && (sb.st_mode & S_IFMT) == S_IFCHR) |
2d2a9370 | 316 | cp++; |
a54c0b3f SL |
317 | type[0] = *++cp; |
318 | type[1] = *++cp; | |
319 | type[2] = '\0'; | |
320 | installboot(special, type); | |
321 | } | |
322 | exit(0); | |
323 | } | |
324 | ||
325 | installboot(dev, type) | |
326 | char *dev, *type; | |
327 | { | |
328 | int fd; | |
329 | char bootblock[MAXPATHLEN], standalonecode[MAXPATHLEN]; | |
330 | char bootimage[BBSIZE]; | |
331 | ||
89241117 SL |
332 | sprintf(bootblock, "%s/%sboot", BOOTDIR, type); |
333 | sprintf(standalonecode, "%s/boot%s", BOOTDIR, type); | |
a54c0b3f SL |
334 | if (verbose) { |
335 | printf("installing boot code\n"); | |
336 | printf("sector 0 boot = %s\n", bootblock); | |
337 | printf("1st level boot = %s\n", standalonecode); | |
338 | } | |
339 | fd = open(bootblock, 0); | |
340 | if (fd < 0) { | |
c6003316 | 341 | fprintf(stderr, "newfs: "); perror(bootblock); |
a54c0b3f SL |
342 | exit(1); |
343 | } | |
344 | if (read(fd, bootimage, DEV_BSIZE) < 0) { | |
c6003316 | 345 | fprintf(stderr, "newfs: "); perror(bootblock); |
a54c0b3f SL |
346 | exit(2); |
347 | } | |
348 | close(fd); | |
349 | fd = open(standalonecode, 0); | |
350 | if (fd < 0) { | |
c6003316 | 351 | fprintf(stderr, "newfs: "); perror(standalonecode); |
a54c0b3f SL |
352 | exit(1); |
353 | } | |
354 | if (read(fd, &bootimage[DEV_BSIZE], BBSIZE - DEV_BSIZE) < 0) { | |
c6003316 | 355 | fprintf(stderr, "newfs: "); perror(standalonecode); |
a54c0b3f SL |
356 | exit(2); |
357 | } | |
358 | close(fd); | |
f55416c1 | 359 | fd = open(dev, 1); |
a54c0b3f | 360 | if (fd < 0) { |
c6003316 | 361 | fprintf(stderr, "newfs: "); perror(dev); |
a54c0b3f SL |
362 | exit(1); |
363 | } | |
364 | if (write(fd, bootimage, BBSIZE) != BBSIZE) { | |
c6003316 | 365 | fprintf(stderr, "newfs: "); perror(dev); |
a54c0b3f SL |
366 | exit(2); |
367 | } | |
368 | close(fd); | |
a54c0b3f SL |
369 | } |
370 | ||
371 | /*VARARGS*/ | |
372 | fatal(fmt, arg1, arg2) | |
373 | char *fmt; | |
374 | { | |
375 | ||
c6003316 | 376 | fprintf(stderr, "newfs: "); |
a54c0b3f SL |
377 | fprintf(stderr, fmt, arg1, arg2); |
378 | putc('\n', stderr); | |
379 | exit(10); | |
380 | } |