bsddisklabel.c revision 1.1 1 1.1 dholland /* $NetBSD: bsddisklabel.c,v 1.1 2014/07/26 19:30:44 dholland Exp $ */
2 1.1 dholland
3 1.1 dholland /*
4 1.1 dholland * Copyright 1997 Piermont Information Systems Inc.
5 1.1 dholland * All rights reserved.
6 1.1 dholland *
7 1.1 dholland * Based on code written by Philip A. Nelson for Piermont Information
8 1.1 dholland * Systems Inc.
9 1.1 dholland *
10 1.1 dholland * Redistribution and use in source and binary forms, with or without
11 1.1 dholland * modification, are permitted provided that the following conditions
12 1.1 dholland * are met:
13 1.1 dholland * 1. Redistributions of source code must retain the above copyright
14 1.1 dholland * notice, this list of conditions and the following disclaimer.
15 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 dholland * notice, this list of conditions and the following disclaimer in the
17 1.1 dholland * documentation and/or other materials provided with the distribution.
18 1.1 dholland * 3. The name of Piermont Information Systems Inc. may not be used to endorse
19 1.1 dholland * or promote products derived from this software without specific prior
20 1.1 dholland * written permission.
21 1.1 dholland *
22 1.1 dholland * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
23 1.1 dholland * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 dholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 dholland * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
26 1.1 dholland * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.1 dholland * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.1 dholland * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.1 dholland * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.1 dholland * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.1 dholland * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 1.1 dholland * THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 dholland */
34 1.1 dholland
35 1.1 dholland /* bsddisklabel.c -- generate standard BSD disklabel */
36 1.1 dholland /* Included by appropriate arch/XXXX/md.c */
37 1.1 dholland
38 1.1 dholland #include <sys/param.h>
39 1.1 dholland #include <sys/sysctl.h>
40 1.1 dholland #include <sys/exec.h>
41 1.1 dholland #include <sys/utsname.h>
42 1.1 dholland #include <sys/types.h>
43 1.1 dholland #include <sys/stat.h>
44 1.1 dholland #include <machine/cpu.h>
45 1.1 dholland #include <stdio.h>
46 1.1 dholland #include <stddef.h>
47 1.1 dholland #include <util.h>
48 1.1 dholland #include <dirent.h>
49 1.1 dholland #include "defs.h"
50 1.1 dholland #include "md.h"
51 1.1 dholland #include "endian.h"
52 1.1 dholland #include "msg_defs.h"
53 1.1 dholland #include "menu_defs.h"
54 1.1 dholland
55 1.1 dholland static int check_partitions(void);
56 1.1 dholland
57 1.1 dholland /* For the current state of this file blame abs (at) NetBSD.org */
58 1.1 dholland /* Even though he wasn't the last to hack it, but he did admit doing so :-) */
59 1.1 dholland
60 1.1 dholland #define PART_ANY -1
61 1.1 dholland #define PART_EXTRA -2
62 1.1 dholland #define PART_TMP_RAMDISK -3
63 1.1 dholland
64 1.1 dholland /* Defaults for things that might be defined in md.h */
65 1.1 dholland #ifndef PART_ROOT
66 1.1 dholland #define PART_ROOT PART_A
67 1.1 dholland #endif
68 1.1 dholland #ifndef PART_SWAP
69 1.1 dholland #define PART_SWAP PART_B
70 1.1 dholland #endif
71 1.1 dholland #ifndef PART_USR
72 1.1 dholland #define PART_USR PART_ANY
73 1.1 dholland #endif
74 1.1 dholland
75 1.1 dholland #ifndef DEFVARSIZE
76 1.1 dholland #define DEFVARSIZE 32
77 1.1 dholland #endif
78 1.1 dholland #ifndef DEFROOTSIZE
79 1.1 dholland #define DEFROOTSIZE 32
80 1.1 dholland #endif
81 1.1 dholland #ifndef DEFUSRSIZE
82 1.1 dholland #define DEFUSRSIZE 128
83 1.1 dholland #endif
84 1.1 dholland #ifndef DEFSWAPSIZE
85 1.1 dholland #define DEFSWAPSIZE 128
86 1.1 dholland #endif
87 1.1 dholland
88 1.1 dholland int
89 1.1 dholland save_ptn(int ptn, daddr_t start, daddr_t size, int fstype, const char *mountpt)
90 1.1 dholland {
91 1.1 dholland static int maxptn;
92 1.1 dholland partinfo *p;
93 1.1 dholland int pp;
94 1.1 dholland
95 1.1 dholland if (maxptn == 0)
96 1.1 dholland maxptn = getmaxpartitions();
97 1.1 dholland
98 1.1 dholland if (ptn < 0 || bsdlabel[ptn].pi_fstype != FS_UNUSED) {
99 1.1 dholland ptn = getrawpartition() + 1;
100 1.1 dholland #ifdef PART_FIRST_FREE
101 1.1 dholland if (ptn < PART_FIRST_FREE)
102 1.1 dholland ptn = PART_FIRST_FREE;
103 1.1 dholland #endif
104 1.1 dholland for (;; ptn++) {
105 1.1 dholland if (ptn >= maxptn)
106 1.1 dholland return -1;
107 1.1 dholland if (ptn == PART_USR)
108 1.1 dholland continue;
109 1.1 dholland if (bsdlabel[ptn].pi_fstype == FS_UNUSED)
110 1.1 dholland break;
111 1.1 dholland }
112 1.1 dholland }
113 1.1 dholland
114 1.1 dholland if (fstype == FS_UNUSED)
115 1.1 dholland return ptn;
116 1.1 dholland
117 1.1 dholland p = bsdlabel + ptn;
118 1.1 dholland p->pi_offset = start;
119 1.1 dholland p->pi_size = size;
120 1.1 dholland set_ptype(p, fstype, mountpt ? PIF_NEWFS : 0);
121 1.1 dholland
122 1.1 dholland if (mountpt != NULL) {
123 1.1 dholland for (pp = 0; pp < maxptn; pp++) {
124 1.1 dholland if (strcmp(bsdlabel[pp].pi_mount, mountpt) == 0)
125 1.1 dholland bsdlabel[pp].pi_flags &= ~PIF_MOUNT;
126 1.1 dholland }
127 1.1 dholland strlcpy(p->pi_mount, mountpt, sizeof p->pi_mount);
128 1.1 dholland p->pi_flags |= PIF_MOUNT;
129 1.1 dholland /* Default to UFS2. */
130 1.1 dholland if (p->pi_fstype == FS_BSDFFS) {
131 1.1 dholland #ifdef DEFAULT_UFS2
132 1.1 dholland #ifndef HAVE_UFS2_BOOT
133 1.1 dholland if (strcmp(mountpt, "/") != 0)
134 1.1 dholland #endif
135 1.1 dholland p->pi_flags |= PIF_FFSv2;
136 1.1 dholland #endif
137 1.1 dholland }
138 1.1 dholland }
139 1.1 dholland
140 1.1 dholland return ptn;
141 1.1 dholland }
142 1.1 dholland
143 1.1 dholland void
144 1.1 dholland set_ptn_titles(menudesc *m, int opt, void *arg)
145 1.1 dholland {
146 1.1 dholland struct ptn_info *pi = arg;
147 1.1 dholland struct ptn_size *p;
148 1.1 dholland int sm = MEG / sectorsize;
149 1.1 dholland daddr_t size;
150 1.1 dholland char inc_free[12];
151 1.1 dholland
152 1.1 dholland p = &pi->ptn_sizes[opt];
153 1.1 dholland if (p->mount[0] == 0) {
154 1.1 dholland wprintw(m->mw, "%s", msg_string(MSG_add_another_ptn));
155 1.1 dholland return;
156 1.1 dholland }
157 1.1 dholland size = p->size;
158 1.1 dholland if (p == pi->pool_part)
159 1.1 dholland snprintf(inc_free, sizeof inc_free, "(%" PRIi64 ")",
160 1.1 dholland (size + pi->free_space) / sm);
161 1.1 dholland else
162 1.1 dholland inc_free[0] = 0;
163 1.1 dholland wprintw(m->mw, "%6" PRIi64 "%8s%10" PRIi64 "%10" PRIi64 " %c %s",
164 1.1 dholland size / sm, inc_free, size / dlcylsize, size,
165 1.1 dholland p == pi->pool_part ? '+' : ' ', p->mount);
166 1.1 dholland }
167 1.1 dholland
168 1.1 dholland void
169 1.1 dholland set_ptn_menu(struct ptn_info *pi)
170 1.1 dholland {
171 1.1 dholland struct ptn_size *p;
172 1.1 dholland menu_ent *m;
173 1.1 dholland
174 1.1 dholland for (m = pi->ptn_menus, p = pi->ptn_sizes;; m++, p++) {
175 1.1 dholland m->opt_name = NULL;
176 1.1 dholland m->opt_menu = OPT_NOMENU;
177 1.1 dholland m->opt_flags = 0;
178 1.1 dholland m->opt_action = set_ptn_size;
179 1.1 dholland if (p->mount[0] == 0)
180 1.1 dholland break;
181 1.1 dholland }
182 1.1 dholland if (pi->free_parts != 0)
183 1.1 dholland m++;
184 1.1 dholland m->opt_name = MSG_askunits;
185 1.1 dholland m->opt_menu = MENU_sizechoice;
186 1.1 dholland m->opt_flags = OPT_SUB;
187 1.1 dholland m->opt_action = NULL;
188 1.1 dholland m++;
189 1.1 dholland
190 1.1 dholland if (pi->free_space >= 0)
191 1.1 dholland snprintf(pi->exit_msg, sizeof pi->exit_msg,
192 1.1 dholland msg_string(MSG_fssizesok),
193 1.1 dholland (int)(pi->free_space / sizemult), multname, pi->free_parts);
194 1.1 dholland else
195 1.1 dholland snprintf(pi->exit_msg, sizeof pi->exit_msg,
196 1.1 dholland msg_string(MSG_fssizesbad),
197 1.1 dholland (int)(-pi->free_space / sizemult), multname, (uint) -pi->free_space);
198 1.1 dholland
199 1.1 dholland set_menu_numopts(pi->menu_no, m - pi->ptn_menus);
200 1.1 dholland }
201 1.1 dholland
202 1.1 dholland int
203 1.1 dholland set_ptn_size(menudesc *m, void *arg)
204 1.1 dholland {
205 1.1 dholland struct ptn_info *pi = arg;
206 1.1 dholland struct ptn_size *p;
207 1.1 dholland char answer[10];
208 1.1 dholland char dflt[10];
209 1.1 dholland char *cp;
210 1.1 dholland daddr_t size, old_size;
211 1.1 dholland int mult;
212 1.1 dholland
213 1.1 dholland p = pi->ptn_sizes + m->cursel;
214 1.1 dholland
215 1.1 dholland if (pi->free_parts == 0 && p->size == 0)
216 1.1 dholland /* Don't allow 'free_parts' to go negative */
217 1.1 dholland return 0;
218 1.1 dholland
219 1.1 dholland if (p->mount[0] == 0) {
220 1.1 dholland msg_prompt_win(MSG_askfsmount, -1, 18, 0, 0,
221 1.1 dholland NULL, p->mount, sizeof p->mount);
222 1.1 dholland if (p->mount[0] == 0)
223 1.1 dholland return 0;
224 1.1 dholland }
225 1.1 dholland
226 1.1 dholland size = p->size;
227 1.1 dholland old_size = size;
228 1.1 dholland if (size == 0)
229 1.1 dholland size = p->dflt_size;
230 1.1 dholland size /= sizemult;
231 1.1 dholland snprintf(dflt, sizeof dflt, "%" PRIi64 "%s",
232 1.1 dholland size, p == pi->pool_part ? "+" : "");
233 1.1 dholland
234 1.1 dholland for (;;) {
235 1.1 dholland mult = sizemult;
236 1.1 dholland msg_prompt_win(MSG_askfssize, -1, 18, 0, 0,
237 1.1 dholland dflt, answer, sizeof answer,
238 1.1 dholland p->mount, multname);
239 1.1 dholland /* Some special cases when /usr is first given a size */
240 1.1 dholland if (old_size == 0 && !strcmp(p->mount, "/usr")) {
241 1.1 dholland /* Remove space for /usr from / */
242 1.1 dholland if (!pi->ptn_sizes[0].changed) {
243 1.1 dholland pi->ptn_sizes[0].size -= p->dflt_size;
244 1.1 dholland pi->free_space += p->dflt_size;
245 1.1 dholland pi->ptn_sizes[0].changed = 1;
246 1.1 dholland }
247 1.1 dholland /* hack to add free space to default sized /usr */
248 1.1 dholland if (!strcmp(answer, dflt)) {
249 1.1 dholland size = p->dflt_size;
250 1.1 dholland pi->pool_part = p;
251 1.1 dholland goto adjust_free;
252 1.1 dholland }
253 1.1 dholland }
254 1.1 dholland size = strtoul(answer, &cp, 0);
255 1.1 dholland switch (*cp++) {
256 1.1 dholland default:
257 1.1 dholland continue;
258 1.1 dholland case 's':
259 1.1 dholland mult = 1;
260 1.1 dholland break;
261 1.1 dholland case 'c':
262 1.1 dholland mult = dlcylsize;
263 1.1 dholland break;
264 1.1 dholland case 'm':
265 1.1 dholland case 'M':
266 1.1 dholland mult = MEG / sectorsize;
267 1.1 dholland break;
268 1.1 dholland case 'g':
269 1.1 dholland case 'G':
270 1.1 dholland mult = 1024 * MEG / sectorsize;
271 1.1 dholland break;
272 1.1 dholland case '+':
273 1.1 dholland cp--;
274 1.1 dholland if (cp != answer)
275 1.1 dholland break;
276 1.1 dholland mult = 1;
277 1.1 dholland size = old_size;
278 1.1 dholland break;
279 1.1 dholland case 0:
280 1.1 dholland cp--;
281 1.1 dholland break;
282 1.1 dholland }
283 1.1 dholland if (*cp == 0 || *cp == '+')
284 1.1 dholland break;
285 1.1 dholland }
286 1.1 dholland
287 1.1 dholland size = NUMSEC(size, mult, dlcylsize);
288 1.1 dholland if (p->ptn_id == PART_TMP_RAMDISK) {
289 1.1 dholland p->size = size;
290 1.1 dholland return 0;
291 1.1 dholland }
292 1.1 dholland if (p == pi->pool_part)
293 1.1 dholland pi->pool_part = NULL;
294 1.1 dholland if (*cp == '+' && p->limit == 0) {
295 1.1 dholland pi->pool_part = p;
296 1.1 dholland if (size == 0)
297 1.1 dholland size = dlcylsize;
298 1.1 dholland }
299 1.1 dholland if (p->limit != 0 && size > p->limit)
300 1.1 dholland size = p->limit;
301 1.1 dholland adjust_free:
302 1.1 dholland if (size != old_size)
303 1.1 dholland p->changed = 1;
304 1.1 dholland pi->free_space += old_size - size;
305 1.1 dholland p->size = size;
306 1.1 dholland if (size == 0) {
307 1.1 dholland if (old_size != 0)
308 1.1 dholland pi->free_parts++;
309 1.1 dholland if (p->ptn_id == PART_EXTRA)
310 1.1 dholland memmove(p, p + 1,
311 1.1 dholland (char *)&pi->ptn_sizes[MAXPARTITIONS]
312 1.1 dholland - (char *)p);
313 1.1 dholland } else {
314 1.1 dholland int f = pi->free_space;
315 1.1 dholland if (old_size == 0)
316 1.1 dholland pi->free_parts--;
317 1.1 dholland if (f < mult && -f < mult) {
318 1.1 dholland /*
319 1.1 dholland * Round size to end of available space,
320 1.1 dholland * but keep cylinder alignment
321 1.1 dholland */
322 1.1 dholland if (f < 0)
323 1.1 dholland f = -roundup(-f, dlcylsize);
324 1.1 dholland else
325 1.1 dholland f = rounddown(f, dlcylsize);
326 1.1 dholland size += f;
327 1.1 dholland if (size != 0) {
328 1.1 dholland pi->free_space -= f;
329 1.1 dholland p->size += f;
330 1.1 dholland }
331 1.1 dholland }
332 1.1 dholland }
333 1.1 dholland
334 1.1 dholland set_ptn_menu(pi);
335 1.1 dholland
336 1.1 dholland return 0;
337 1.1 dholland }
338 1.1 dholland
339 1.1 dholland void
340 1.1 dholland get_ptn_sizes(daddr_t part_start, daddr_t sectors, int no_swap)
341 1.1 dholland {
342 1.1 dholland int i;
343 1.1 dholland int maxpart = getmaxpartitions();
344 1.1 dholland int sm; /* sectors in 1MB */
345 1.1 dholland struct ptn_size *p;
346 1.1 dholland daddr_t size;
347 1.1 dholland
348 1.1 dholland static struct ptn_info pi = { -1, {
349 1.1 dholland #define PI_ROOT 0
350 1.1 dholland { PART_ROOT, { '/', '\0' },
351 1.1 dholland DEFROOTSIZE, DEFROOTSIZE , 0, 0},
352 1.1 dholland #define PI_SWAP 1
353 1.1 dholland { PART_SWAP, { 's', 'w', 'a', 'p', '\0' },
354 1.1 dholland DEFSWAPSIZE, DEFSWAPSIZE, 0, 0 },
355 1.1 dholland { PART_TMP_RAMDISK,
356 1.1 dholland #ifdef HAVE_TMPFS
357 1.1 dholland { '/', 't', 'm', 'p', ' ', '(', 't', 'm', 'p', 'f', 's', ')', '\0' },
358 1.1 dholland #else
359 1.1 dholland { '/', 't', 'm', 'p', ' ', '(', 'm', 'f', 's', ')', '\0' },
360 1.1 dholland #endif
361 1.1 dholland 64, 0, 0, 0 },
362 1.1 dholland #define PI_USR 3
363 1.1 dholland { PART_USR, { '/', 'u', 's', 'r', '\0' }, DEFUSRSIZE,
364 1.1 dholland 0, 0, 0 },
365 1.1 dholland { PART_ANY, { '/', 'v', 'a', 'r', '\0' }, DEFVARSIZE,
366 1.1 dholland 0, 0, 0 },
367 1.1 dholland { PART_ANY, { '/', 'h', 'o', 'm', 'e', '\0' }, 0,
368 1.1 dholland 0, 0, 0 },
369 1.1 dholland }, {
370 1.1 dholland { NULL, OPT_NOMENU, 0, set_ptn_size },
371 1.1 dholland { MSG_askunits, MENU_sizechoice, OPT_SUB, NULL },
372 1.1 dholland }, 0, 0, NULL, { 0 } };
373 1.1 dholland
374 1.1 dholland if (maxpart > MAXPARTITIONS)
375 1.1 dholland maxpart = MAXPARTITIONS; /* sanity */
376 1.1 dholland
377 1.1 dholland msg_display(MSG_ptnsizes);
378 1.1 dholland msg_table_add(MSG_ptnheaders);
379 1.1 dholland
380 1.1 dholland if (pi.menu_no < 0) {
381 1.1 dholland /* If there is a swap partition elsewhere, don't add one here.*/
382 1.1 dholland if (no_swap) {
383 1.1 dholland pi.ptn_sizes[PI_SWAP].size = 0;
384 1.1 dholland } else {
385 1.1 dholland #if DEFSWAPSIZE == -1
386 1.1 dholland /* Dynamic swap size. */
387 1.1 dholland pi.ptn_sizes[PI_SWAP].dflt_size = get_ramsize();
388 1.1 dholland pi.ptn_sizes[PI_SWAP].size =
389 1.1 dholland pi.ptn_sizes[PI_SWAP].dflt_size;
390 1.1 dholland #endif
391 1.1 dholland }
392 1.1 dholland
393 1.1 dholland /* If installing X increase default size of /usr */
394 1.1 dholland if (set_X11_selected())
395 1.1 dholland pi.ptn_sizes[PI_USR].dflt_size += XNEEDMB;
396 1.1 dholland
397 1.1 dholland /* Start of planning to give free space to / */
398 1.1 dholland pi.pool_part = &pi.ptn_sizes[PI_ROOT];
399 1.1 dholland /* Make size of root include default size of /usr */
400 1.1 dholland pi.ptn_sizes[PI_ROOT].size += pi.ptn_sizes[PI_USR].dflt_size;
401 1.1 dholland
402 1.1 dholland sm = MEG / sectorsize;
403 1.1 dholland
404 1.1 dholland if (root_limit != 0) {
405 1.1 dholland /* Bah - bios can not read all the disk, limit root */
406 1.1 dholland pi.ptn_sizes[PI_ROOT].limit = root_limit - part_start;
407 1.1 dholland /* Allocate a /usr partition if bios can't read
408 1.1 dholland * everything except swap.
409 1.1 dholland */
410 1.1 dholland if (pi.ptn_sizes[PI_ROOT].limit
411 1.1 dholland < sectors - pi.ptn_sizes[PI_SWAP].size * sm) {
412 1.1 dholland /* Root won't be able to access all the space */
413 1.1 dholland /* Claw back space for /usr */
414 1.1 dholland pi.ptn_sizes[PI_USR].size =
415 1.1 dholland pi.ptn_sizes[PI_USR].dflt_size;
416 1.1 dholland pi.ptn_sizes[PI_ROOT].size -=
417 1.1 dholland pi.ptn_sizes[PI_USR].dflt_size;
418 1.1 dholland pi.ptn_sizes[PI_ROOT].changed = 1;
419 1.1 dholland /* Give free space to /usr */
420 1.1 dholland pi.pool_part = &pi.ptn_sizes[PI_USR];
421 1.1 dholland }
422 1.1 dholland }
423 1.1 dholland
424 1.1 dholland /* Change preset sizes from MB to sectors */
425 1.1 dholland pi.free_space = sectors;
426 1.1 dholland for (p = pi.ptn_sizes; p->mount[0]; p++) {
427 1.1 dholland p->size = NUMSEC(p->size, sm, dlcylsize);
428 1.1 dholland p->dflt_size = NUMSEC(p->dflt_size, sm, dlcylsize);
429 1.1 dholland pi.free_space -= p->size;
430 1.1 dholland }
431 1.1 dholland
432 1.1 dholland /* Steal space from swap to make things fit.. */
433 1.1 dholland if (pi.free_space < 0) {
434 1.1 dholland i = roundup(-pi.free_space, dlcylsize);
435 1.1 dholland if (i > pi.ptn_sizes[PI_SWAP].size)
436 1.1 dholland i = pi.ptn_sizes[PI_SWAP].size;
437 1.1 dholland pi.ptn_sizes[PI_SWAP].size -= i;
438 1.1 dholland pi.free_space += i;
439 1.1 dholland }
440 1.1 dholland
441 1.1 dholland /* Add space for 2 system dumps to / (traditional) */
442 1.1 dholland i = get_ramsize() * sm;
443 1.1 dholland i = roundup(i, dlcylsize);
444 1.1 dholland if (pi.free_space > i * 2)
445 1.1 dholland i *= 2;
446 1.1 dholland if (pi.free_space > i) {
447 1.1 dholland pi.ptn_sizes[PI_ROOT].size += i;
448 1.1 dholland pi.free_space -= i;
449 1.1 dholland }
450 1.1 dholland
451 1.1 dholland /* Ensure all of / is readable by the system boot code */
452 1.1 dholland i = pi.ptn_sizes[PI_ROOT].limit;
453 1.1 dholland if (i != 0 && (i -= pi.ptn_sizes[PI_ROOT].size) < 0) {
454 1.1 dholland pi.ptn_sizes[PI_ROOT].size += i;
455 1.1 dholland pi.free_space -= i;
456 1.1 dholland }
457 1.1 dholland
458 1.1 dholland /* Count free partition slots */
459 1.1 dholland pi.free_parts = 0;
460 1.1 dholland for (i = 0; i < maxpart; i++) {
461 1.1 dholland if (bsdlabel[i].pi_size == 0)
462 1.1 dholland pi.free_parts++;
463 1.1 dholland }
464 1.1 dholland for (i = 0; i < MAXPARTITIONS; i++) {
465 1.1 dholland p = &pi.ptn_sizes[i];
466 1.1 dholland if (i != 0 && p->ptn_id == 0)
467 1.1 dholland p->ptn_id = PART_EXTRA;
468 1.1 dholland if (p->size != 0)
469 1.1 dholland pi.free_parts--;
470 1.1 dholland }
471 1.1 dholland
472 1.1 dholland pi.menu_no = new_menu(0, pi.ptn_menus, nelem(pi.ptn_menus),
473 1.1 dholland 3, -1, 12, 70,
474 1.1 dholland MC_NOSHORTCUT |
475 1.1 dholland MC_ALWAYS_SCROLL | MC_NOBOX | MC_NOCLEAR,
476 1.1 dholland NULL, set_ptn_titles, NULL,
477 1.1 dholland "help", pi.exit_msg);
478 1.1 dholland
479 1.1 dholland if (pi.menu_no < 0)
480 1.1 dholland return;
481 1.1 dholland }
482 1.1 dholland
483 1.1 dholland do {
484 1.1 dholland set_ptn_menu(&pi);
485 1.1 dholland current_cylsize = dlcylsize;
486 1.1 dholland process_menu(pi.menu_no, &pi);
487 1.1 dholland } while (pi.free_space < 0 || pi.free_parts < 0);
488 1.1 dholland
489 1.1 dholland /* Give any cylinder fragment to last partition */
490 1.1 dholland if (pi.pool_part != NULL || pi.free_space < dlcylsize) {
491 1.1 dholland for (p = pi.ptn_sizes + nelem(pi.ptn_sizes) - 1; ;p--) {
492 1.1 dholland if (p->size == 0) {
493 1.1 dholland if (p == pi.ptn_sizes)
494 1.1 dholland break;
495 1.1 dholland continue;
496 1.1 dholland }
497 1.1 dholland if (p->ptn_id == PART_TMP_RAMDISK)
498 1.1 dholland continue;
499 1.1 dholland p->size += pi.free_space % dlcylsize;
500 1.1 dholland break;
501 1.1 dholland }
502 1.1 dholland }
503 1.1 dholland
504 1.1 dholland for (p = pi.ptn_sizes; p->mount[0]; p++, part_start += size) {
505 1.1 dholland size = p->size;
506 1.1 dholland if (p == pi.pool_part) {
507 1.1 dholland size += rounddown(pi.free_space, dlcylsize);
508 1.1 dholland if (p->limit != 0 && size > p->limit)
509 1.1 dholland size = p->limit;
510 1.1 dholland }
511 1.1 dholland i = p->ptn_id;
512 1.1 dholland if (i == PART_TMP_RAMDISK) {
513 1.1 dholland tmp_ramdisk_size = size;
514 1.1 dholland size = 0;
515 1.1 dholland continue;
516 1.1 dholland }
517 1.1 dholland if (size == 0)
518 1.1 dholland continue;
519 1.1 dholland if (i == PART_SWAP) {
520 1.1 dholland save_ptn(i, part_start, size, FS_SWAP, NULL);
521 1.1 dholland continue;
522 1.1 dholland }
523 1.1 dholland save_ptn(i, part_start, size, FS_BSDFFS, p->mount);
524 1.1 dholland }
525 1.1 dholland }
526 1.1 dholland
527 1.1 dholland /*
528 1.1 dholland * md back-end code for menu-driven BSD disklabel editor.
529 1.1 dholland */
530 1.1 dholland int
531 1.1 dholland make_bsd_partitions(void)
532 1.1 dholland {
533 1.1 dholland int i;
534 1.1 dholland int part;
535 1.1 dholland int maxpart = getmaxpartitions();
536 1.1 dholland daddr_t partstart;
537 1.1 dholland int part_raw, part_bsd;
538 1.1 dholland daddr_t ptend;
539 1.1 dholland int no_swap = 0, valid_part = -1;
540 1.1 dholland partinfo *p;
541 1.1 dholland
542 1.1 dholland /*
543 1.1 dholland * Initialize global variables that track space used on this disk.
544 1.1 dholland * Standard 4.4BSD 8-partition labels always cover whole disk.
545 1.1 dholland */
546 1.1 dholland if (logfp) fprintf(logfp, "dlsize=%" PRId64 " ptsize=%" PRId64 " ptstart=%" PRId64 "\n", dlsize,ptsize,ptstart);
547 1.1 dholland if (ptsize == 0)
548 1.1 dholland ptsize = dlsize - ptstart;
549 1.1 dholland if (dlsize == 0)
550 1.1 dholland dlsize = ptstart + ptsize;
551 1.1 dholland
552 1.1 dholland partstart = ptstart;
553 1.1 dholland ptend = ptstart + ptsize;
554 1.1 dholland
555 1.1 dholland /* Ask for layout type -- standard or special */
556 1.1 dholland msg_display(MSG_layout,
557 1.1 dholland (int) (ptsize / (MEG / sectorsize)),
558 1.1 dholland DEFROOTSIZE + DEFSWAPSIZE + DEFUSRSIZE,
559 1.1 dholland DEFROOTSIZE + DEFSWAPSIZE + DEFUSRSIZE + XNEEDMB);
560 1.1 dholland
561 1.1 dholland process_menu(MENU_layout, NULL);
562 1.1 dholland
563 1.1 dholland /* Set so we use the 'real' geometry for rounding, input in MB */
564 1.1 dholland current_cylsize = dlcylsize;
565 1.1 dholland set_sizemultname_meg();
566 1.1 dholland
567 1.1 dholland /* Build standard partitions */
568 1.1 dholland memset(&bsdlabel, 0, sizeof bsdlabel);
569 1.1 dholland
570 1.1 dholland /* Set initial partition types to unused */
571 1.1 dholland for (part = 0 ; part < maxpart ; ++part)
572 1.1 dholland bsdlabel[part].pi_fstype = FS_UNUSED;
573 1.1 dholland
574 1.1 dholland /* Whole disk partition */
575 1.1 dholland part_raw = getrawpartition();
576 1.1 dholland if (part_raw == -1)
577 1.1 dholland part_raw = PART_C; /* for sanity... */
578 1.1 dholland bsdlabel[part_raw].pi_offset = 0;
579 1.1 dholland bsdlabel[part_raw].pi_size = dlsize;
580 1.1 dholland
581 1.1 dholland if (part_raw == PART_D) {
582 1.1 dholland /* Probably a system that expects an i386 style mbr */
583 1.1 dholland part_bsd = PART_C;
584 1.1 dholland bsdlabel[PART_C].pi_offset = ptstart;
585 1.1 dholland bsdlabel[PART_C].pi_size = ptsize;
586 1.1 dholland } else {
587 1.1 dholland part_bsd = part_raw;
588 1.1 dholland }
589 1.1 dholland
590 1.1 dholland #if defined(PART_BOOT) && defined(BOOT_SIZE)
591 1.1 dholland i = BOOT_SIZE;
592 1.1 dholland if (i >= 1024) {
593 1.1 dholland /* Treat big numbers as a byte count */
594 1.1 dholland i = (i + dlcylsize * sectorsize - 1) / (dlcylsize * sectorsize);
595 1.1 dholland i *= dlcylsize;
596 1.1 dholland }
597 1.1 dholland #if defined(PART_BOOT_FFS)
598 1.1 dholland bsdlabel[PART_BOOT].pi_fstype = FS_BSDFFS;
599 1.1 dholland bsdlabel[PART_BOOT].pi_flags = PIF_NEWFS;
600 1.1 dholland #else
601 1.1 dholland bsdlabel[PART_BOOT].pi_fstype = FS_BOOT;
602 1.1 dholland #endif
603 1.1 dholland bsdlabel[PART_BOOT].pi_size = i;
604 1.1 dholland #ifdef BOOT_HIGH
605 1.1 dholland bsdlabel[PART_BOOT].pi_offset = ptend - i;
606 1.1 dholland ptend -= i;
607 1.1 dholland #else
608 1.1 dholland bsdlabel[PART_BOOT].pi_offset = ptstart;
609 1.1 dholland partstart += i;
610 1.1 dholland #endif
611 1.1 dholland #elif defined(PART_BOOT)
612 1.1 dholland if (bootsize != 0) {
613 1.1 dholland #if defined(PART_BOOT_MSDOS)
614 1.1 dholland bsdlabel[PART_BOOT].pi_fstype = FS_MSDOS;
615 1.1 dholland #else
616 1.1 dholland bsdlabel[PART_BOOT].pi_fstype = FS_BOOT;
617 1.1 dholland #endif
618 1.1 dholland bsdlabel[PART_BOOT].pi_size = bootsize;
619 1.1 dholland bsdlabel[PART_BOOT].pi_offset = bootstart;
620 1.1 dholland #if defined(PART_BOOT_PI_FLAGS)
621 1.1 dholland bsdlabel[PART_BOOT].pi_flags |= PART_BOOT_PI_FLAGS;
622 1.1 dholland #endif
623 1.1 dholland #if defined(PART_BOOT_PI_MOUNT)
624 1.1 dholland strlcpy(bsdlabel[PART_BOOT].pi_mount, PART_BOOT_PI_MOUNT,
625 1.1 dholland sizeof bsdlabel[PART_BOOT].pi_mount);
626 1.1 dholland #endif
627 1.1 dholland }
628 1.1 dholland #endif /* PART_BOOT w/o BOOT_SIZE */
629 1.1 dholland
630 1.1 dholland #if defined(PART_SYSVBFS) && defined(SYSVBFS_SIZE)
631 1.1 dholland bsdlabel[PART_SYSVBFS].pi_offset = partstart;
632 1.1 dholland bsdlabel[PART_SYSVBFS].pi_fstype = FS_SYSVBFS;
633 1.1 dholland bsdlabel[PART_SYSVBFS].pi_size = SYSVBFS_SIZE;
634 1.1 dholland bsdlabel[PART_SYSVBFS].pi_flags |= PIF_NEWFS | PIF_MOUNT;
635 1.1 dholland strlcpy(bsdlabel[PART_SYSVBFS].pi_mount, "/stand",
636 1.1 dholland sizeof bsdlabel[PART_SYSVBFS].pi_mount);
637 1.1 dholland partstart += SYSVBFS_SIZE;
638 1.1 dholland #endif
639 1.1 dholland
640 1.1 dholland #ifdef PART_REST
641 1.1 dholland bsdlabel[PART_REST].pi_offset = 0;
642 1.1 dholland bsdlabel[PART_REST].pi_size = ptstart;
643 1.1 dholland #endif
644 1.1 dholland
645 1.1 dholland if (layoutkind == 4) {
646 1.1 dholland /*
647 1.1 dholland * If 'oldlabel' is a default label created by the kernel it
648 1.1 dholland * will have exactly one valid partition besides raw_part
649 1.1 dholland * which covers the whole disk - but might lie outside the
650 1.1 dholland * mbr partition we (by now) have offset by a few sectors.
651 1.1 dholland * Check for this and and fix ut up.
652 1.1 dholland */
653 1.1 dholland valid_part = -1;
654 1.1 dholland for (i = 0; i < maxpart; i++) {
655 1.1 dholland if (i == part_raw)
656 1.1 dholland continue;
657 1.1 dholland if (oldlabel[i].pi_size > 0 && PI_ISBSDFS(&oldlabel[i])) {
658 1.1 dholland if (valid_part >= 0) {
659 1.1 dholland /* nope, not the default case */
660 1.1 dholland valid_part = -1;
661 1.1 dholland break;
662 1.1 dholland }
663 1.1 dholland valid_part = i;
664 1.1 dholland }
665 1.1 dholland }
666 1.1 dholland if (valid_part >= 0 && oldlabel[valid_part].pi_offset < ptstart) {
667 1.1 dholland oldlabel[valid_part].pi_offset = ptstart;
668 1.1 dholland oldlabel[valid_part].pi_size -= ptstart;
669 1.1 dholland }
670 1.1 dholland }
671 1.1 dholland
672 1.1 dholland /*
673 1.1 dholland * Save any partitions that are outside the area we are
674 1.1 dholland * going to use.
675 1.1 dholland * In particular this saves details of the other MBR
676 1.1 dholland * partitions on a multiboot i386 system.
677 1.1 dholland */
678 1.1 dholland for (i = maxpart; i--;) {
679 1.1 dholland if (bsdlabel[i].pi_size != 0)
680 1.1 dholland /* Don't overwrite special partitions */
681 1.1 dholland continue;
682 1.1 dholland p = &oldlabel[i];
683 1.1 dholland if (p->pi_fstype == FS_UNUSED || p->pi_size == 0)
684 1.1 dholland continue;
685 1.1 dholland if (layoutkind == 4) {
686 1.1 dholland if (PI_ISBSDFS(p)) {
687 1.1 dholland p->pi_flags |= PIF_MOUNT;
688 1.1 dholland if (layoutkind == 4 && i == valid_part) {
689 1.1 dholland int fstype = p->pi_fstype;
690 1.1 dholland p->pi_fstype = 0;
691 1.1 dholland strcpy(p->pi_mount, "/");
692 1.1 dholland set_ptype(p, fstype, PIF_NEWFS);
693 1.1 dholland }
694 1.1 dholland }
695 1.1 dholland } else {
696 1.1 dholland if (p->pi_offset < ptstart + ptsize &&
697 1.1 dholland p->pi_offset + p->pi_size > ptstart)
698 1.1 dholland /* Not outside area we are allocating */
699 1.1 dholland continue;
700 1.1 dholland if (p->pi_fstype == FS_SWAP)
701 1.1 dholland no_swap = 1;
702 1.1 dholland }
703 1.1 dholland bsdlabel[i] = oldlabel[i];
704 1.1 dholland }
705 1.1 dholland
706 1.1 dholland if (layoutkind != 4)
707 1.1 dholland get_ptn_sizes(partstart, ptend - partstart, no_swap);
708 1.1 dholland
709 1.1 dholland /*
710 1.1 dholland * OK, we have a partition table. Give the user the chance to
711 1.1 dholland * edit it and verify it's OK, or abort altogether.
712 1.1 dholland */
713 1.1 dholland edit_check:
714 1.1 dholland if (edit_and_check_label(bsdlabel, maxpart, part_raw, part_bsd) == 0) {
715 1.1 dholland msg_display(MSG_abort);
716 1.1 dholland return 0;
717 1.1 dholland }
718 1.1 dholland if (check_partitions() == 0)
719 1.1 dholland goto edit_check;
720 1.1 dholland
721 1.1 dholland /* Disk name */
722 1.1 dholland msg_prompt(MSG_packname, bsddiskname, bsddiskname, sizeof bsddiskname);
723 1.1 dholland
724 1.1 dholland /* save label to disk for MI code to update. */
725 1.1 dholland (void) savenewlabel(bsdlabel, maxpart);
726 1.1 dholland
727 1.1 dholland /* Everything looks OK. */
728 1.1 dholland return (1);
729 1.1 dholland }
730 1.1 dholland
731 1.1 dholland /*
732 1.1 dholland * check that there is at least a / somewhere.
733 1.1 dholland */
734 1.1 dholland static int
735 1.1 dholland check_partitions(void)
736 1.1 dholland {
737 1.1 dholland #ifdef HAVE_BOOTXX_xFS
738 1.1 dholland int rv;
739 1.1 dholland char *bootxx;
740 1.1 dholland #endif
741 1.1 dholland #ifndef HAVE_UFS2_BOOT
742 1.1 dholland int fstype;
743 1.1 dholland #endif
744 1.1 dholland
745 1.1 dholland #ifdef HAVE_BOOTXX_xFS
746 1.1 dholland /* check if we have boot code for the root partition type */
747 1.1 dholland bootxx = bootxx_name();
748 1.1 dholland if (bootxx != NULL) {
749 1.1 dholland rv = access(bootxx, R_OK);
750 1.1 dholland free(bootxx);
751 1.1 dholland } else
752 1.1 dholland rv = -1;
753 1.1 dholland if (rv != 0) {
754 1.1 dholland process_menu(MENU_ok, deconst(MSG_No_Bootcode));
755 1.1 dholland return 0;
756 1.1 dholland }
757 1.1 dholland #endif
758 1.1 dholland #ifndef HAVE_UFS2_BOOT
759 1.1 dholland fstype = bsdlabel[rootpart].pi_fstype;
760 1.1 dholland if (fstype == FS_BSDFFS &&
761 1.1 dholland (bsdlabel[rootpart].pi_flags & PIF_FFSv2) != 0) {
762 1.1 dholland process_menu(MENU_ok, deconst(MSG_cannot_ufs2_root));
763 1.1 dholland return 0;
764 1.1 dholland }
765 1.1 dholland #endif
766 1.1 dholland
767 1.1 dholland return md_check_partitions();
768 1.1 dholland }
769