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