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