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