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