Annotation of src/distrib/utils/sysinst/label.c, Revision 1.6
1.6 ! jonathan 1: /* $NetBSD: label.c,v 1.5 1998/06/20 13:05:49 mrg Exp $ */
1.1 jonathan 2:
3: /*
4: * Copyright 1997 Jonathan Stone
5: * All rights reserved.
6: *
7:
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software develooped for the NetBSD Project by
19: * Jonathan Stone.
20: * 4. The name of Jonathan Stone may not be used to endorse
21: * or promote products derived from this software without specific prior
22: * written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY JONATHAN STONE ``AS IS''
25: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
28: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
34: * THE POSSIBILITY OF SUCH DAMAGE.
35: *
36: */
37:
38: #include <sys/cdefs.h>
39: #if defined(LIBC_SCCS) && !defined(lint)
1.6 ! jonathan 40: __RCSID("$NetBSD: label.c,v 1.5 1998/06/20 13:05:49 mrg Exp $");
1.1 jonathan 41: #endif
42:
1.2 jonathan 43: #include <sys/types.h>
44: #include <errno.h>
1.1 jonathan 45: #include <stdio.h>
1.2 jonathan 46: #include <fcntl.h>
1.1 jonathan 47: #include <util.h>
1.2 jonathan 48: #include <unistd.h>
49: #include <sys/dkio.h>
50: #include <sys/ioctl.h>
1.1 jonathan 51:
52: #include "defs.h"
53: #include "msg_defs.h"
54: #include "menu_defs.h"
55:
56: /*
57: * local prototypes
58: */
59: static int boringpart __P((partinfo *lp, int i, int rawpart, int bsdpart));
60:
1.2 jonathan 61: int checklabel __P((partinfo *lp, int nparts, int rawpart, int bsdpart,
1.1 jonathan 62: int *bad1, int *bad2));
1.2 jonathan 63: void translate_partinfo __P((partinfo *lp, struct partition *pp));
64:
1.1 jonathan 65:
66: /*
67: * Return 1 iff partition i in lp should be ignored when checking
68: * for overlapping partitions.
69: */
70: static int
1.5 mrg 71: boringpart(lp, i, rawpart, bsdpart)
72: partinfo *lp;
73: int i;
74: int rawpart;
75: int bsdpart;
1.1 jonathan 76: {
77:
78: if (i == rawpart || i == bsdpart ||
79: lp[i][D_FSTYPE] == T_UNUSED || lp[i][D_SIZE] == 0)
80: return 1;
81: return 0;
82: }
83:
84:
85:
86: /*
87: * Check a sysinst label structure for overlapping partitions.
88: * Returns 0 if no overlapping partition found, nonzero otherwise.
89: * Sets reference arguments ovly1 and ovly2 to the indices of
90: * overlapping partitions if any are found.
91: */
92: int
1.5 mrg 93: checklabel(lp, nparts, rawpart, bsdpart, ovly1, ovly2)
94: partinfo *lp;
95: int nparts;
96: int rawpart;
97: int bsdpart;
98: int *ovly1;
99: int *ovly2;
1.1 jonathan 100: {
101: int i;
102: int j;
103:
104: *ovly1 = -1;
105: *ovly2 = -1;
106:
107: for (i = 0; i < nparts - 1; i ++ ) {
1.5 mrg 108: int *ip = lp[i];
1.1 jonathan 109: int istart, istop;
110:
111: /* skip unused or reserved partitions */
112: if (boringpart(lp, i, rawpart, bsdpart))
113: continue;
114:
115: /*
116: * check succeding partitions for overlap.
117: * O(n^2), but n is small (currently <= 16).
118: */
119: istart = ip[D_OFFSET];
120: istop = istart + ip[D_SIZE];
121:
122: for (j = i+1; j < nparts; j++) {
123: int *jp = lp[j];
124: int jstart, jstop;
125:
126: /* skip unused or reserved partitions */
1.3 simonb 127: if (boringpart(lp, j, rawpart, bsdpart))
1.1 jonathan 128: continue;
129:
130: jstart = jp[D_OFFSET];
1.4 fvdl 131: jstop = jstart + jp[D_SIZE];
1.1 jonathan 132:
133: /* overlap? */
134: if ((istart <= jstart && jstart < istop) ||
135: (jstart <= istart && istart < jstop)) {
136: *ovly1 = i;
137: *ovly2 = j;
1.5 mrg 138: return (1);
1.1 jonathan 139: }
140: }
141: }
142:
1.5 mrg 143: return (0);
1.1 jonathan 144: }
145:
146:
147: /*
148: * Check a disklabel.
149: * If there are overlapping active parititons,
150: * Ask the user if they want to edit the parittion or give up.
151: */
152: int
1.5 mrg 153: edit_and_check_label(lp, nparts, rawpart, bsdpart)
154: partinfo *lp;
155: int nparts;
156: int rawpart;
157: int bsdpart;
1.1 jonathan 158: {
159: while (1) {
160: int i, j;
161:
162: /* first give the user the option to edit the label... */
1.5 mrg 163: process_menu(MENU_fspartok);
1.1 jonathan 164:
165: /* User thinks the label is OK. check for overlaps.*/
166: if (checklabel(lp, nparts, rawpart, bsdpart, &i, &j) == 0) {
167: /* partitions are OK. */
168: return (1);
169: }
170:
171: /* partitions overlap. */
172: msg_display(MSG_partitions_overlap, 'a' + i, 'a' + j);
173: /*XXX*/
174: msg_display_add(MSG_edit_partitions_again);
175: process_menu(MENU_yesno);
176: if (!yesno)
177: return(0);
178: }
179:
180: /*NOTREACHED*/
181: }
182:
183:
1.5 mrg 184: void
185: emptylabel(lp)
186: partinfo *lp;
1.1 jonathan 187: {
188: register int i, maxpart;
189:
190: maxpart = getmaxpartitions();
191:
192: for (i = 0; i < maxpart; i++) {
193: lp[i][D_FSTYPE] = T_UNUSED;
194: lp[i][D_OFFSET] = 0;
195: lp[i][D_SIZE] = 0;
196: lp[i][D_BSIZE] = 0;
197: lp[i][D_FSIZE] = 0;
198: }
199: }
200:
1.2 jonathan 201: int
1.5 mrg 202: savenewlabel(lp, nparts)
203: partinfo *lp;
204: int nparts;
1.2 jonathan 205: {
206: FILE *f;
207: int i;
208:
209: #ifdef DEBUG
1.5 mrg 210: f = fopen("/tmp/disktab", "a");
1.2 jonathan 211: #else
1.6 ! jonathan 212: /* Create the disktab.preinstall */
! 213: run_prog("cp /etc/disktab.preinstall /etc/disktab");
1.5 mrg 214: f = fopen("/etc/disktab", "a");
1.2 jonathan 215: #endif
216: if (f == NULL) {
217: endwin();
1.5 mrg 218: (void)fprintf(stderr, "Could not open /etc/disktab");
1.2 jonathan 219: exit (1);
220: }
1.5 mrg 221: (void)fprintf(f, "%s|NetBSD installation generated:\\\n", bsddiskname);
222: (void)fprintf(f, "\t:dt=%s:ty=winchester:\\\n", disktype);
223: (void)fprintf(f, "\t:nc#%d:nt#%d:ns#%d:\\\n", dlcyl, dlhead, dlsec);
224: (void)fprintf(f, "\t:sc#%d:su#%d:\\\n", dlhead*dlsec, dlsize);
225: (void)fprintf(f, "\t:se#%d:%s\\\n", sectorsize, doessf);
226: for (i = 0; i < nparts; i++) {
227: (void)fprintf(f, "\t:p%c#%d:o%c#%d:t%c=%s:",
228: 'a'+i, bsdlabel[i][D_SIZE],
229: 'a'+i, bsdlabel[i][D_OFFSET],
230: 'a'+i, fstype[bsdlabel[i][D_FSTYPE]]);
1.2 jonathan 231: if (bsdlabel[i][D_FSTYPE] == T_42BSD)
1.6 ! jonathan 232: (void)fprintf (f, "b%c#%d:f%c#%d:ta=4.2BSD:",,
1.2 jonathan 233: 'a'+i, bsdlabel[i][D_BSIZE],
234: 'a'+i, bsdlabel[i][D_FSIZE]);
235: if (i < 7)
1.5 mrg 236: (void)fprintf(f, "\\\n");
1.2 jonathan 237: else
1.5 mrg 238: (void)fprintf(f, "\n");
1.2 jonathan 239: }
240: fclose (f);
241:
242: return(0);
243: }
244:
245:
246: void
1.5 mrg 247: translate_partinfo(lp, pp)
248: partinfo *lp;
249: struct partition *pp;
1.2 jonathan 250: {
1.5 mrg 251:
1.2 jonathan 252: switch (pp->p_fstype) {
253:
254: case FS_UNUSED: /* XXX */
255: (*lp)[D_FSTYPE] = T_UNUSED;
256: break;
257:
258: case FS_SWAP:
259: (*lp)[D_FSTYPE] = T_SWAP;
260: break;
261:
262: case FS_BSDFFS:
263: (*lp)[D_FSTYPE] = T_42BSD;
264: (*lp)[D_OFFSET] = 0;
265: (*lp)[D_SIZE] = 0;
266: (*lp)[D_BSIZE] = pp->p_fsize * pp->p_frag;
267: (*lp)[D_FSIZE] = pp->p_fsize;
268: break;
269:
270: case FS_EX2FS:
271: (*lp)[D_FSTYPE] = T_UNUSED; /* XXX */
272: (*lp)[D_BSIZE] = pp->p_fsize * pp->p_frag;
273: (*lp)[D_FSIZE] = pp->p_fsize;
274: break;
275:
276: default:
277: (*lp)[D_FSTYPE] = T_UNUSED;
278: break;
279: }
280: }
281:
282: /*
283: * Read a label from disk into a sysist label structure.
284: */
1.5 mrg 285: int
286: incorelabel(dkname, lp)
287: const char *dkname;
288: partinfo *lp;
1.2 jonathan 289: {
290: struct disklabel lab;
291: int fd;
292: int i, maxpart;
293: struct partition *pp;
294: char nambuf[STRSIZE];
295:
296: fd = opendisk(dkname, O_RDONLY, nambuf, STRSIZE, 0);
297:
298: if (ioctl(fd, DIOCGDINFO, &lab) < 0) {
299: /*XXX err(4, "ioctl DIOCGDINFO");*/
300: return(errno);
301: }
302: close(fd);
303:
304: maxpart = getmaxpartitions();
1.5 mrg 305: if (maxpart > 16)
306: maxpart = 16;
1.2 jonathan 307:
308:
309: /* XXX set globals used by MD code to compute disk size? */
310:
311: pp = &lab.d_partitions[0];
312: emptylabel(lp);
313: for (i = 0; i < maxpart; i++) {
314: translate_partinfo(lp+i, pp+i);
315: }
316:
317: return (0);
318: }
CVSweb <webmaster@jp.NetBSD.org>