bsddisklabel.c revision 1.23 1 /* $NetBSD: bsddisklabel.c,v 1.23 2019/07/28 16:30:36 martin Exp $ */
2
3 /*
4 * Copyright 1997 Piermont Information Systems Inc.
5 * All rights reserved.
6 *
7 * Based on code written by Philip A. Nelson for Piermont Information
8 * Systems Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of Piermont Information Systems Inc. may not be used to endorse
19 * or promote products derived from this software without specific prior
20 * written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /* bsddisklabel.c -- generate standard BSD disklabel */
36 /* Included by appropriate arch/XXXX/md.c */
37
38 #include <sys/param.h>
39 #include <sys/sysctl.h>
40 #include <sys/exec.h>
41 #include <sys/utsname.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <machine/cpu.h>
45 #include <assert.h>
46 #include <stdio.h>
47 #include <stddef.h>
48 #include <util.h>
49 #include <dirent.h>
50 #include "defs.h"
51 #include "md.h"
52 #include "defsizes.h"
53 #include "endian.h"
54 #include "msg_defs.h"
55 #include "menu_defs.h"
56
57 static size_t fill_ptn_menu(struct partition_usage_set *pset);
58
59 /*
60 * The default partition layout.
61 */
62 static const struct part_usage_info
63 default_parts_init[] =
64 {
65 /*
66 * Pretty complex setup for boot partitions.
67 * This is copy&pasted below, please keep in sync!
68 */
69 #ifdef PART_BOOT
70 { .size = PART_BOOT/512, /* PART_BOOT is in BYTE, not MB! */
71 #ifdef PART_BOOT_MOUNT
72 .mount = PART_BOOT_MOUNT,
73 .instflags = PUIINST_MOUNT|PUIINST_BOOT,
74 #else
75 .instflags = PUIINST_BOOT,
76 #endif
77 #ifdef PART_BOOT_TYPE
78 .fs_type = PART_BOOT_TYPE,
79 #if PART_BOOT_TYPE == FS_MSDOS
80 .flags = PUIFLAG_ADD_OUTER,
81 #endif
82 #endif
83 #ifdef PART_BOOT_SUBT
84 .fs_version = PART_BOOT_SUBT,
85 #endif
86 },
87 #endif
88
89 /*
90 * Two more copies of above for _BOOT1 and _BOOT2, please
91 * keep in sync!
92 */
93 #ifdef PART_BOOT1
94 { .size = PART_BOOT1/512, /* PART_BOOT1 is in BYTE, not MB! */
95 #ifdef PART_BOOT1_MOUNT
96 .mount = PART_BOOT1_MOUNT,
97 .instflags = PUIINST_MOUNT|PUIINST_BOOT,
98 #else
99 .instflags = PUIINST_MOUNT|PUIINST_BOOT,
100 #endif
101 #ifdef PART_BOOT1_TYPE
102 .fs_type = PART_BOOT1_TYPE,
103 #if PART_BOOT1_TYPE == FS_MSDOS
104 .flags = PUIFLAG_ADD_OUTER,
105 #endif
106 #endif
107 #ifdef PART_BOOT1_SUBT
108 .fs_version = PART_BOOT1_SUBT,
109 #endif
110 },
111 #endif
112 #ifdef PART_BOOT2
113 { .size = PART_BOOT2/512, /* PART_BOOT2 is in BYTE, not MB! */
114 #ifdef PART_BOOT2_MOUNT
115 .mount = PART_BOOT2_MOUNT,
116 .instflags = PUIINST_MOUNT|PUIINST_BOOT,
117 #else
118 .instflags = PUIINST_MOUNT|PUIINST_BOOT,
119 #endif
120 #ifdef PART_BOOT2_TYPE
121 .fs_type = PART_BOOT2_TYPE,
122 #if PART_BOOT2_TYPE == FS_MSDOS
123 .flags = PUIFLAG_ADD_OUTER,
124 #endif
125 #endif
126 #ifdef PART_BOOT2_SUBT
127 .fs_version = PART_BOOT1_SUBT,
128 #endif
129 },
130 #endif
131
132 { .size = DEFROOTSIZE*(MEG/512), .mount = "/", .type = PT_root,
133 .flags = PUIFLAG_EXTEND },
134 {
135 #if DEFSWAPSIZE > 0
136 .size = DEFSWAPSIZE*(MEG/512),
137 #endif
138 .type = PT_swap, .fs_type = FS_SWAP },
139 #ifdef HAVE_TMPFS
140 { .type = PT_root, .mount = "/tmp", .fs_type = FS_TMPFS,
141 .flags = PUIFLG_JUST_MOUNTPOINT },
142 #else
143 { .type = PT_root, .mount = "/tmp", .fs_type = FS_MFS,
144 .flags = PUIFLG_JUST_MOUNTPOINT },
145 #endif
146 { .def_size = DEFUSRSIZE*(MEG/512), .mount = "/usr", .type = PT_root },
147 { .def_size = DEFVARSIZE*(MEG/512), .mount = "/var", .type = PT_root },
148 };
149
150 #if 0 // XXX
151 static int
152 save_ptn(int ptn, daddr_t start, daddr_t size, int fstype, const char *mountpt)
153 {
154 static int maxptn;
155 partinfo *p;
156 int pp;
157 char *buf;
158
159 if (maxptn == 0)
160 maxptn = getmaxpartitions();
161
162 if (ptn < 0 || PI_FSTYPE(&pm->bsdlabel[ptn]) != FS_UNUSED) {
163 ptn = getrawpartition() + 1;
164 #ifdef PART_FIRST_FREE
165 if (ptn < PART_FIRST_FREE)
166 ptn = PART_FIRST_FREE;
167 #endif
168 for (;; ptn++) {
169 if (ptn >= maxptn)
170 return -1;
171 // XXX if (ptn == PART_USR)
172 // continue;
173 if (PI_FSTYPE(&pm->bsdlabel[ptn]) == FS_UNUSED)
174 break;
175 }
176 }
177
178 if (fstype == FS_UNUSED)
179 return ptn;
180
181 p = pm->bsdlabel + ptn;
182 PI_SET_OFFSET(p, start);
183 PI_SET_SIZE(p, size);
184 set_ptype(p, fstype, mountpt ? PIF_NEWFS : 0);
185
186 /* Hack because we does not have something like FS_LVMPV */
187 p->lvmpv = 0;
188 if (mountpt != NULL && strcmp(mountpt, "lvm") == 0)
189 p->lvmpv = 1;
190 else if (mountpt != NULL) {
191 for (pp = 0; pp < maxptn; pp++) {
192 if (strcmp(pm->bsdlabel[pp].pi_mount, mountpt) == 0)
193 pm->bsdlabel[pp].pi_flags &= ~PIF_MOUNT;
194 }
195 if (mountpt[0] != '/')
196 asprintf(&buf, "/%s", mountpt);
197 else
198 asprintf(&buf, "%s", mountpt);
199 strlcpy(p->pi_mount, buf, sizeof p->pi_mount);
200 p->pi_flags |= PIF_MOUNT;
201 /* Default to UFS2. */
202 if (PI_FSTYPE(p) == FS_BSDFFS) {
203 #ifdef DEFAULT_UFS2
204 #ifndef HAVE_UFS2_BOOT
205 if (strcmp(mountpt, "/") != 0)
206 #endif
207 p->pi_flags |= PIF_FFSv2;
208 #endif
209 }
210 free(buf);
211 }
212 return ptn;
213 }
214 #endif
215
216
217 static const char size_separator[] =
218 "----------------------------------- - --------------------";
219 static char size_menu_title[STRSIZE];
220 static char size_menu_exit[MENUSTRSIZE];
221
222 static void
223 set_pset_exit_str(struct partition_usage_set *pset)
224 {
225 char *str, num[25];
226 const char *args[2];
227 bool overrun;
228 daddr_t free_space = pset->cur_free_space;
229
230 /* format exit string */
231 overrun = free_space < 0;
232 if (overrun)
233 free_space = -free_space;
234
235 snprintf(num, sizeof(num), "%" PRIu64, free_space / sizemult);
236 args[0] = num;
237 args[1] = multname;
238 str = str_arg_subst(
239 msg_string(overrun ? MSG_fssizesbad : MSG_fssizesok),
240 2, args);
241 strlcpy(size_menu_exit, str, sizeof(size_menu_exit));
242 free(str);
243 }
244
245 static void
246 draw_size_menu_header(menudesc *m, void *arg)
247 {
248 struct partition_usage_set *pset = arg;
249 size_t i;
250 char col1[70], desc[MENUSTRSIZE];
251 bool need_ext = false, need_existing = false;
252
253 msg_display(MSG_ptnsizes);
254
255 for (i = 0; i < pset->num; i++) {
256 if (pset->infos[i].flags & PUIFLG_IS_OUTER)
257 need_ext = true;
258 else if (pset->infos[i].cur_part_id != NO_PART)
259 need_existing = true;
260 }
261 if (need_ext && need_existing)
262 snprintf(desc, sizeof desc, "%s, %s",
263 msg_string(MSG_ptnsizes_mark_existing),
264 msg_string(MSG_ptnsizes_mark_external));
265 else if (need_existing)
266 strlcpy(desc, msg_string(MSG_ptnsizes_mark_existing),
267 sizeof desc);
268 else if (need_ext)
269 strlcpy(desc, msg_string(MSG_ptnsizes_mark_external),
270 sizeof desc);
271 if (need_ext || need_existing) {
272 msg_printf("\n");
273 msg_display_add_subst(msg_string(MSG_ptnsizes_markers),
274 1, &desc);
275 }
276 msg_printf("\n\n");
277
278 /* update menu title */
279 snprintf(col1, sizeof col1, "%s (%s)", msg_string(MSG_ptnheaders_size),
280 multname);
281 snprintf(size_menu_title, sizeof size_menu_title,
282 " %-37.37s %s\n %s", col1,
283 msg_string(MSG_ptnheaders_filesystem), size_separator);
284 }
285
286 static void
287 draw_size_menu_line(menudesc *m, int opt, void *arg)
288 {
289 struct partition_usage_set *pset = arg;
290 daddr_t size;
291 char psize[38], inc_free[16], flag, swap[40];
292 const char *mount;
293 bool free_mount = false;
294
295 if (opt < 0 || (size_t)opt >= pset->num)
296 return;
297
298 inc_free[0] = 0;
299 if ((pset->infos[opt].flags & PUIFLAG_EXTEND) &&
300 pset->cur_free_space > 0) {
301 size = pset->infos[opt].size + pset->cur_free_space;
302 snprintf(inc_free, sizeof inc_free, " (%" PRIu64 ")",
303 size / sizemult);
304 }
305 size = pset->infos[opt].size;
306 snprintf(psize, sizeof psize, "%" PRIu64 "%s",
307 size / sizemult, inc_free);
308
309 if (pset->infos[opt].type == PT_swap) {
310 snprintf(swap, sizeof swap, "<%s>",
311 msg_string(MSG_swap_display));
312 mount = swap;
313 } else if (pset->infos[opt].flags & PUIFLG_JUST_MOUNTPOINT) {
314 snprintf(swap, sizeof swap, "%s (%s)",
315 pset->infos[opt].mount,
316 getfslabelname(pset->infos[opt].fs_type,
317 pset->infos[opt].fs_version));
318 mount = swap;
319 } else if (pset->infos[opt].mount[0]) {
320 mount = pset->infos[opt].mount;
321 } else {
322 mount = NULL;
323 if (pset->infos[opt].parts->pscheme->other_partition_identifier
324 && pset->infos[opt].cur_part_id != NO_PART)
325 mount = pset->infos[opt].parts->pscheme->
326 other_partition_identifier(pset->infos[opt].parts,
327 pset->infos[opt].cur_part_id);
328 if (mount == NULL)
329 mount = getfslabelname(pset->infos[opt].fs_type,
330 pset->infos[opt].fs_version);
331 mount = str_arg_subst(msg_string(MSG_size_ptn_not_mounted),
332 1, &mount);
333 free_mount = true;
334 }
335 flag = ' ';
336 if (pset->infos[opt].flags & PUIFLAG_EXTEND)
337 flag = '+';
338 else if (pset->infos[opt].flags & PUIFLG_IS_OUTER)
339 flag = '@';
340 else if (pset->infos[opt].cur_part_id != NO_PART)
341 flag = '=';
342 wprintw(m->mw, "%-35.35s %c %s", psize, flag, mount);
343 if (free_mount)
344 free(__UNCONST(mount));
345
346 if (opt == 0)
347 set_pset_exit_str(pset);
348 }
349
350 static int
351 add_other_ptn_size(menudesc *menu, void *arg)
352 {
353 struct partition_usage_set *pset = arg;
354 struct part_usage_info *p;
355 struct menu_ent *m;
356 char new_mp[MOUNTLEN], *err;
357 const char *args;
358
359 for (;;) {
360 msg_prompt_win(partman_go?MSG_askfsmountadv:MSG_askfsmount,
361 -1, 18, 0, 0, NULL, new_mp, sizeof(new_mp));
362 if (new_mp[0] == 0)
363 return 0;
364 if (new_mp[0] != '/') {
365 /* we need absolute mount paths */
366 memmove(new_mp+1, new_mp, sizeof(new_mp)-1);
367 new_mp[0] = '/';
368 /* duplicates? */
369 bool duplicate = false;
370 for (size_t i = 0; i < pset->num; i++) {
371 if (strcmp(pset->infos[i].mount,
372 new_mp) == 0) {
373 args = new_mp;
374 err = str_arg_subst(
375 msg_string(MSG_mp_already_exists),
376 1, &args);
377 err_msg_win(err);
378 free(err);
379 duplicate = true;
380 break;
381 }
382 }
383 if (!duplicate)
384 break;
385 }
386 }
387
388 m = realloc(pset->menu_opts, (pset->num+4)*sizeof(*pset->menu_opts));
389 if (m == NULL)
390 return 0;
391 p = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
392 if (p == NULL)
393 return 0;
394
395 pset->infos = p;
396 pset->menu_opts = m;
397 menu->opts = m;
398 menu->numopts = pset->num+4;
399 m += pset->num;
400 p += pset->num;
401 memset(m, 0, sizeof(*m));
402 memset(p, 0, sizeof(*p));
403 strncpy(p->mount, new_mp, sizeof(p->mount));
404
405 menu->cursel = pset->num;
406 pset->num++;
407 fill_ptn_menu(pset);
408
409 return -1;
410 }
411
412 static size_t
413 fill_ptn_menu(struct partition_usage_set *pset)
414 {
415 struct part_usage_info *p;
416 struct disk_part_info info;
417 menu_ent *m;
418 size_t i;
419 daddr_t free_space;
420
421 memset(pset->menu_opts, 0, (pset->num+3)*sizeof(*pset->menu_opts));
422 for (m = pset->menu_opts, p = pset->infos, i = 0; i < pset->num;
423 m++, p++, i++) {
424 m->opt_action = set_ptn_size;
425 }
426
427 m->opt_name = size_separator;
428 m->opt_flags = OPT_IGNORE|OPT_NOSHORT;
429 m++;
430
431 m->opt_name = MSG_add_another_ptn;
432 m->opt_action = add_other_ptn_size;
433 m++;
434
435 m->opt_name = MSG_askunits;
436 m->opt_menu = MENU_sizechoice;
437 m->opt_flags = OPT_SUB;
438 m++;
439
440 /* calculate free space */
441 free_space = pset->parts->free_space;
442 for (i = 0; i < pset->parts->num_part; i++) {
443 if (!pset->parts->pscheme->get_part_info(pset->parts, i,
444 &info))
445 continue;
446 if (info.flags & (PTI_SEC_CONTAINER|PTI_WHOLE_DISK|
447 PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
448 continue;
449 free_space += info.size;
450 }
451 for (i = 0; i < pset->num; i++) {
452 if (pset->infos[i].flags &
453 (PUIFLG_IS_OUTER|PUIFLG_JUST_MOUNTPOINT))
454 continue;
455 free_space -= pset->infos[i].size;
456 }
457 pset->cur_free_space = free_space;
458 set_pset_exit_str(pset);
459
460 if (pset->menu >= 0)
461 set_menu_numopts(pset->menu, m - pset->menu_opts);
462
463 return m - pset->menu_opts;
464 }
465
466 static part_id
467 find_part_at(struct disk_partitions *parts, daddr_t start)
468 {
469 size_t i;
470 struct disk_part_info info;
471
472 for (i = 0; i < parts->num_part; i++) {
473 if (!parts->pscheme->get_part_info(parts, i, &info))
474 continue;
475 if (info.start == start)
476 return i;
477 }
478
479 return NO_PART;
480 }
481
482 int
483 set_ptn_size(menudesc *m, void *arg)
484 {
485 struct partition_usage_set *pset = arg;
486 struct part_usage_info *p = &pset->infos[m->cursel];
487 char answer[16], dflt[16];
488 const char *err_msg;
489 size_t i, root = ~0U;
490 daddr_t size, old_size, new_size_val, mult;
491 int rv;
492 bool non_zero, extend;
493
494 if (pset->cur_free_space == 0 && p->size == 0 &&
495 !(p->flags & PUIFLG_JUST_MOUNTPOINT))
496 /* Don't allow 'free_parts' to go negative */
497 return 0;
498
499 if (p->cur_part_id != NO_PART) {
500 rv = 0;
501 process_menu(MENU_ptnsize_replace_existing_partition, &rv);
502 if (rv == 0)
503 return 0;
504 if (!pset->parts->pscheme->delete_partition(pset->parts,
505 p->cur_part_id, &err_msg)) {
506 if (err_msg)
507 err_msg_win(err_msg);
508 return 0;
509 }
510 p->cur_part_id = NO_PART;
511 /*
512 * All other part ids are invalid now too - update them!
513 */
514 for (i = 0; i < pset->num; i++) {
515 if (pset->infos[i].cur_part_id == NO_PART)
516 continue;
517 pset->infos[i].cur_part_id =
518 find_part_at(pset->parts, pset->infos[i].cur_start);
519 }
520 }
521
522 size = p->size;
523 old_size = size;
524 if (size == 0)
525 size = p->def_size;
526 size /= sizemult;
527 snprintf(dflt, sizeof dflt, "%" PRIu64 "%s",
528 size, p->flags & PUIFLAG_EXTEND ? "+" : "");
529
530 for (;;) {
531 msg_fmt_prompt_win(MSG_askfssize, -1, 18, 0, 0,
532 dflt, answer, sizeof answer, "%s%s", p->mount, multname);
533
534 /* cp will be checked below */
535 mult = sizemult;
536 new_size_val = parse_disk_pos(answer, &mult, pm->dlcylsize,
537 &extend);
538
539 if (strcmp(answer, dflt) == 0)
540 non_zero = p->def_size > 0;
541 else
542 non_zero = new_size_val > 0;
543
544 /* Some special cases when /usr is first given a size */
545 if (old_size == 0 && non_zero &&
546 strcmp(p->mount, "/usr") == 0) {
547 for (i = 0; i < pset->num; i++) {
548 if (strcmp(pset->infos[i].mount, "/") == 0) {
549 root = i;
550 break;
551 }
552 }
553 /* Remove space for /usr from / */
554 if (root < pset->num && pset->infos[i].cur_part_id ==
555 NO_PART) {
556 pset->infos[root].size -= p->def_size;
557 pset->cur_free_space += p->def_size;
558 }
559 /* hack to add free space to default sized /usr */
560 if (strcmp(answer, dflt) == 0) {
561 size = p->def_size;
562 pset->infos[root].flags &= ~PUIFLAG_EXTEND;
563 p->flags |= PUIFLAG_EXTEND;
564 goto adjust_free;
565 }
566 }
567 if (new_size_val < 0)
568 continue;
569 size = new_size_val;
570 break;
571 }
572
573 daddr_t align = pset->parts->pscheme->get_part_alignment(pset->parts);
574 size = NUMSEC(size, mult, align);
575 if (p->flags & PUIFLAG_EXTEND)
576 p->flags &= ~PUIFLAG_EXTEND;
577 if (extend && (p->limit == 0 || p->limit > p->size)) {
578 p->flags |= PUIFLAG_EXTEND;
579 if (size == 0)
580 size = align;
581 }
582 if (p->limit != 0 && size > p->limit)
583 size = p->limit;
584 adjust_free:
585 if ((p->flags & (PUIFLG_IS_OUTER|PUIFLG_JUST_MOUNTPOINT)) == 0)
586 pset->cur_free_space += p->size - size;
587 p->size = size;
588 set_pset_exit_str(pset);
589
590 return 0;
591 }
592
593 /*
594 * User interface to edit a "wanted" partition layout "pset" as first
595 * abstract phase (not concrete partitions).
596 * Make sure to have everything (at least theoretically) fit the
597 * available space.
598 * During editing we keep the part_usage_info and the menu_opts
599 * in pset in sync, that is: we always allocate just enough entries
600 * in pset->infos as we have usage infos in the list (pset->num),
601 * and two additional menu entries ("add a partition" and "select units").
602 * The menu exit string changes depending on content, and implies
603 * abort while the partition set is not valid (does not fit).
604 * Return true when the user wants to continue (by editing the concrete
605 * partitions), return false to abort.
606 */
607 bool
608 get_ptn_sizes(struct partition_usage_set *pset)
609 {
610 size_t num;
611
612 wclear(stdscr);
613 wrefresh(stdscr);
614
615 if (pset->menu_opts == NULL)
616 pset->menu_opts = calloc(pset->num+3, sizeof(*pset->menu_opts));
617
618 pset->menu = -1;
619 num = fill_ptn_menu(pset);
620
621 pset->menu = new_menu(size_menu_title, pset->menu_opts, num,
622 3, -1, 12, 70,
623 MC_ALWAYS_SCROLL|MC_NOBOX|MC_NOCLEAR|MC_CONTINUOUS,
624 draw_size_menu_header, draw_size_menu_line, NULL,
625 NULL, size_menu_exit);
626
627 if (pset->menu < 0) {
628 free(pset->menu_opts);
629 pset->menu_opts = NULL;
630 return false;
631 }
632
633 pset->ok = true;
634 process_menu(pset->menu, pset);
635
636 free_menu(pset->menu);
637 free(pset->menu_opts);
638 pset->menu = -1;
639 pset->menu_opts = NULL;
640
641 if (!pset->ok)
642 return false;
643
644 #if 0
645 if (cur_ptns.menu_no < 0) {
646 /* If there is a swap partition elsewhere, don't add one here.*/
647 if (no_swap || (swap_created && partman_go)) {
648 cur_ptns.ptn_sizes[PI_SWAP].size = 0;
649 } else {
650 #if DEFSWAPSIZE == -1
651 /* Dynamic swap size. */
652 cur_ptns.ptn_sizes[PI_SWAP].dflt_size = get_ramsize();
653 cur_ptns.ptn_sizes[PI_SWAP].size =
654 cur_ptns.ptn_sizes[PI_SWAP].dflt_size;
655 #endif
656 }
657
658 /* If installing X increase default size of /usr */
659 if (set_X11_selected())
660 cur_ptns.ptn_sizes[PI_USR].dflt_size += XNEEDMB;
661
662 /* Start of planning to give free space to / */
663 cur_ptns.pool_part = &cur_ptns.ptn_sizes[PI_ROOT];
664 /* Make size of root include default size of /usr */
665 cur_ptns.ptn_sizes[PI_ROOT].size += cur_ptns.ptn_sizes[PI_USR].dflt_size;
666
667 sm = MEG / pm->sectorsize;
668
669 if (root_limit != 0) {
670 /* Bah - bios can not read all the disk, limit root */
671 cur_ptns.ptn_sizes[PI_ROOT].limit = root_limit -
672 part_start;
673 /* Allocate a /usr partition if bios can't read
674 * everything except swap.
675 */
676 if (cur_ptns.ptn_sizes[PI_ROOT].limit
677 < sectors - cur_ptns.ptn_sizes[PI_SWAP].size * sm) {
678 /* Root won't be able to access all the space */
679 /* Claw back space for /usr */
680 cur_ptns.ptn_sizes[PI_USR].size =
681 cur_ptns.ptn_sizes[PI_USR].dflt_size;
682 cur_ptns.ptn_sizes[PI_ROOT].size -=
683 cur_ptns.ptn_sizes[PI_USR].dflt_size;
684 cur_ptns.ptn_sizes[PI_ROOT].changed = 1;
685 /* Give free space to /usr */
686 cur_ptns.pool_part = &cur_ptns.ptn_sizes[PI_USR];
687 }
688 }
689
690 /* Change preset sizes from MB to sectors */
691 cur_ptns.free_space = sectors;
692 for (p = cur_ptns.ptn_sizes; p->mount[0]; p++) {
693 p->size = NUMSEC(p->size, sm, pm->dlcylsize);
694 p->dflt_size = NUMSEC(p->dflt_size, sm, pm->dlcylsize);
695 cur_ptns.free_space -= p->size;
696 }
697
698 /* Steal space from swap to make things fit.. */
699 if (cur_ptns.free_space < 0) {
700 i = roundup(-cur_ptns.free_space, pm->dlcylsize);
701 if (i > cur_ptns.ptn_sizes[PI_SWAP].size)
702 i = cur_ptns.ptn_sizes[PI_SWAP].size;
703 cur_ptns.ptn_sizes[PI_SWAP].size -= i;
704 cur_ptns.free_space += i;
705 }
706
707 /* Add space for 2 system dumps to / (traditional) */
708 i = get_ramsize() * sm;
709 i = roundup(i, pm->dlcylsize);
710 if (cur_ptns.free_space > i * 2)
711 i *= 2;
712 if (cur_ptns.free_space > i) {
713 cur_ptns.ptn_sizes[PI_ROOT].size += i;
714 cur_ptns.free_space -= i;
715 }
716
717 if (root_created && partman_go) {
718 cur_ptns.ptn_sizes[PI_ROOT].size = 0;
719 cur_ptns.pool_part = 0;
720 }
721
722 /* Ensure all of / is readable by the system boot code */
723 i = cur_ptns.ptn_sizes[PI_ROOT].limit;
724 if (i != 0 && (i -= cur_ptns.ptn_sizes[PI_ROOT].size) < 0) {
725 cur_ptns.ptn_sizes[PI_ROOT].size += i;
726 cur_ptns.free_space -= i;
727 }
728
729 /* Count free partition slots */
730 cur_ptns.free_parts = 0;
731 #if 0 // XXX
732 for (i = 0; i < maxpart; i++) {
733 if (pm->bsdlabel[i].pi_size == 0)
734 cur_ptns.free_parts++;
735 }
736 #endif
737 for (i = 0; i < MAXPARTITIONS; i++) {
738 p = &cur_ptns.ptn_sizes[i];
739 if (i != 0 && p->use == 0)
740 p->use = PART_EXTRA;
741 if (p->size != 0)
742 cur_ptns.free_parts--;
743 }
744
745 cur_ptns.menu_no = new_menu(0, cur_ptns.ptn_menus,
746 __arraycount(cur_ptns.ptn_menus),
747 3, -1, 12, 70,
748 MC_ALWAYS_SCROLL | MC_NOBOX | MC_NOCLEAR,
749 NULL, set_ptn_titles, NULL,
750 "help", cur_ptns.exit_msg);
751
752 if (cur_ptns.menu_no < 0)
753 return;
754 }
755
756 do {
757 set_ptn_menu(&cur_ptns);
758 pm->current_cylsize = pm->dlcylsize;
759 process_menu(cur_ptns.menu_no, &cur_ptns);
760 } while (cur_ptns.free_space < 0 || cur_ptns.free_parts < 0);
761
762 /* Give any cylinder fragment to last partition */
763 if (cur_ptns.pool_part != NULL || cur_ptns.free_space < pm->dlcylsize) {
764 for (p = cur_ptns.ptn_sizes + __arraycount(cur_ptns.ptn_sizes) - 1; ;p--) {
765 if (p->size == 0) {
766 if (p == cur_ptns.ptn_sizes)
767 break;
768 continue;
769 }
770 if (p->use == PART_TMP_RAMDISK)
771 continue;
772 p->size += cur_ptns.free_space % pm->dlcylsize;
773 cur_ptns.free_space -= cur_ptns.free_space % pm->dlcylsize;
774 break;
775 }
776 }
777
778 for (p = cur_ptns.ptn_sizes; p->mount[0]; p++, part_start += size) {
779 size = p->size;
780 if (p == cur_ptns.pool_part) {
781 size += rounddown(cur_ptns.free_space, pm->dlcylsize);
782 if (p->limit != 0 && size > p->limit)
783 size = p->limit;
784 }
785 i = p->use;
786 if (i == PART_TMP_RAMDISK) {
787 tmp_ramdisk_size = size;
788 size = 0;
789 continue;
790 }
791 if (size == 0)
792 continue;
793 if (i == PART_ROOT && size > 0)
794 root_created = 1;
795 if (i == PART_SWAP) {
796 if (size > 0)
797 swap_created = 1;
798 save_ptn(i, part_start, size, FS_SWAP, NULL);
799 continue;
800 }
801 if (!strcmp(p->mount, "raid")) {
802 save_ptn(i, part_start, size, FS_RAID, NULL);
803 continue;
804 } else if (!strcmp(p->mount, "cgd")) {
805 save_ptn(i, part_start, size, FS_CGD, NULL);
806 continue;
807 }
808 save_ptn(i, part_start, size, FS_BSDFFS, p->mount);
809 }
810 #endif
811 return true;
812 }
813
814 static int
815 set_keep_existing(menudesc *m, void *arg)
816 {
817 ((arg_rep_int*)arg)->rv = LY_KEEPEXISTING;
818 return 0;
819 }
820
821 static int
822 set_edit_part_sizes(menudesc *m, void *arg)
823 {
824 ((arg_rep_int*)arg)->rv = LY_SETSIZES;
825 return 0;
826 }
827
828 static int
829 set_use_default_sizes(menudesc *m, void *arg)
830 {
831 ((arg_rep_int*)arg)->rv = LY_USEDEFAULT;
832 return 0;
833 }
834
835 /*
836 * Check if there is a reasonable pre-existing partition for
837 * NetBSD.
838 */
839 static bool
840 check_existing_netbsd(struct disk_partitions *parts)
841 {
842 size_t nbsd_parts;
843 struct disk_part_info info;
844
845 nbsd_parts = 0;
846 for (part_id p = 0; p < parts->num_part; p++) {
847 if (!parts->pscheme->get_part_info(parts, p, &info))
848 continue;
849 if (info.flags & (PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
850 continue;
851 if (info.nat_type && info.nat_type->generic_ptype == PT_root)
852 nbsd_parts++;
853 }
854
855 return nbsd_parts > 0;
856 }
857
858 /*
859 * Query a partition layout type (with available options depending on
860 * pre-existing partitions).
861 */
862 static enum layout_type
863 ask_layout(struct disk_partitions *parts, bool have_existing)
864 {
865 arg_rep_int ai;
866 const char *args[2];
867 int menu;
868 size_t num_opts;
869 menu_ent options[3], *opt;
870
871 args[0] = msg_string(parts->pscheme->name);
872 args[1] = msg_string(parts->pscheme->short_name);
873 ai.args.argv = args;
874 ai.args.argc = 2;
875 ai.rv = LY_SETSIZES;
876
877 memset(options, 0, sizeof(options));
878 num_opts = 0;
879 opt = &options[0];
880
881 if (have_existing) {
882 opt->opt_name = MSG_Keep_existing_partitions;
883 opt->opt_flags = OPT_EXIT;
884 opt->opt_action = set_keep_existing;
885 opt++;
886 num_opts++;
887 }
888 opt->opt_name = MSG_Set_Sizes;
889 opt->opt_flags = OPT_EXIT;
890 opt->opt_action = set_edit_part_sizes;
891 opt++;
892 num_opts++;
893
894 opt->opt_name = MSG_Use_Default_Parts;
895 opt->opt_flags = OPT_EXIT;
896 opt->opt_action = set_use_default_sizes;
897 opt++;
898 num_opts++;
899
900 menu = new_menu(MSG_Select_your_choice, options, num_opts,
901 -1, -10, 0, 0, MC_NOEXITOPT, NULL, NULL, NULL, NULL, NULL);
902 if (menu != -1) {
903 get_menudesc(menu)->expand_act = expand_all_option_texts;
904 process_menu(menu, &ai);
905 free_menu(menu);
906 }
907
908 return ai.rv;
909 }
910
911 static void
912 merge_part_with_wanted(struct disk_partitions *parts, part_id pno,
913 const struct disk_part_info *info, struct partition_usage_set *wanted,
914 size_t wanted_num, bool is_outer)
915 {
916 struct part_usage_info *infos;
917
918 /*
919 * does this partition match something in the wanted set?
920 */
921 for (size_t i = 0; i < wanted_num; i++) {
922 if (wanted->infos[i].type != info->nat_type->generic_ptype)
923 continue;
924 if (wanted->infos[i].type == PT_root &&
925 info->last_mounted != NULL && info->last_mounted[0] != 0 &&
926 strcmp(info->last_mounted, wanted->infos[i].mount) != 0)
927 continue;
928 if (wanted->infos[i].cur_part_id != NO_PART)
929 continue;
930 wanted->infos[i].cur_part_id = pno;
931 wanted->infos[i].parts = parts;
932 wanted->infos[i].size = info->size;
933 wanted->infos[i].cur_start = info->start;
934 wanted->infos[i].flags &= ~PUIFLAG_EXTEND;
935 if (wanted->infos[i].fs_type != FS_UNUSED &&
936 wanted->infos[i].type != PT_swap)
937 wanted->infos[i].instflags |= PUIINST_MOUNT;
938 if (is_outer)
939 wanted->infos[i].flags |= PUIFLG_IS_OUTER;
940 else
941 wanted->infos[i].flags &= ~PUIFLG_IS_OUTER;
942 return;
943 }
944
945 /*
946 * no match - if this is fromt the outer scheme, we are done.
947 * otherwise it must be inserted into the wanted set.
948 */
949 if (is_outer)
950 return;
951
952 /*
953 * create a new entry for this
954 */
955 infos = realloc(wanted->infos, sizeof(*infos)*(wanted->num+1));
956 if (infos == NULL)
957 return;
958 wanted->infos = infos;
959 infos += wanted->num;
960 wanted->num++;
961 memset(infos, 0, sizeof(*infos));
962 if (info->last_mounted != NULL && info->last_mounted[0] != 0)
963 strlcpy(infos->mount, info->last_mounted,
964 sizeof(infos->mount));
965 infos->type = info->nat_type->generic_ptype;
966 infos->cur_part_id = pno;
967 infos->parts = parts;
968 infos->size = info->size;
969 infos->cur_start = info->start;
970 infos->fs_type = info->fs_type;
971 infos->fs_version = info->fs_sub_type;
972 if (is_outer)
973 infos->flags |= PUIFLG_IS_OUTER;
974 }
975
976 static bool
977 have_x11_by_default(void)
978 {
979 static const uint8_t def_sets[] = { MD_SETS_SELECTED };
980
981 for (size_t i = 0; i < __arraycount(def_sets); i++)
982 if (def_sets[i] >= SET_X11_FIRST &&
983 def_sets[i] <= SET_X11_LAST)
984 return true;
985
986 return false;
987 }
988
989 static void
990 fill_defaults(struct partition_usage_set *wanted, struct disk_partitions *parts,
991 daddr_t ptstart, daddr_t ptsize)
992 {
993 size_t i, root = ~0U, usr = ~0U, swap = ~0U, def_usr = ~0U;
994 daddr_t free_space, dump_space, required;
995 #if defined(DEFAULT_UFS2) && !defined(HAVE_UFS2_BOOT)
996 size_t boot = ~0U;
997 #endif
998
999 memset(wanted, 0, sizeof(*wanted));
1000 wanted->parts = parts;
1001 wanted->num = __arraycount(default_parts_init);
1002 wanted->infos = calloc(wanted->num, sizeof(*wanted->infos));
1003 if (wanted->infos == NULL) {
1004 err_msg_win(err_outofmem);
1005 return;
1006 }
1007
1008 memcpy(wanted->infos, default_parts_init, sizeof(default_parts_init));
1009
1010 #ifdef MD_PART_DEFAULTS
1011 MD_PART_DEFAULTS(pm, wanted->infos, wanted->num);
1012 #endif
1013
1014 for (i = 0; i < wanted->num; i++) {
1015 wanted->infos[i].parts = parts;
1016 wanted->infos[i].cur_part_id = NO_PART;
1017
1018 #if DEFSWAPSIZE == -1
1019 if (wanted->infos[i].type == PT_swap)
1020 wanted->infos[i].size = get_ramsize() * (MEG / 512);
1021 #endif
1022 if (wanted->infos[i].type == PT_swap && swap > wanted->num)
1023 swap = i;
1024 #if defined(DEFAULT_UFS2) && !defined(HAVE_UFS2_BOOT)
1025 if (wanted->infos[i].instflags & PUIINST_BOOT)
1026 boot = i;
1027 #endif
1028 if (wanted->infos[i].type == PT_root) {
1029 if (strcmp(wanted->infos[i].mount, "/") == 0) {
1030 root = i;
1031 } else if (
1032 strcmp(wanted->infos[i].mount, "/usr") == 0) {
1033 if (wanted->infos[i].size > 0)
1034 usr = i;
1035 else
1036 def_usr = i;
1037 }
1038 if (wanted->infos[i].fs_type == FS_UNUSED)
1039 wanted->infos[i].fs_type = FS_BSDFFS;
1040 if (wanted->infos[i].fs_type == FS_BSDFFS) {
1041 #ifdef DEFAULT_UFS2
1042 #ifndef HAVE_UFS2_BOOT
1043 if (boot < wanted->num || i != root)
1044 #endif
1045 wanted->infos[i].fs_version = 2;
1046 #endif
1047 }
1048 }
1049 if ((wanted->infos[i].flags & PUIFLG_JUST_MOUNTPOINT) &&
1050 wanted->infos[i].size == 0)
1051 /* default tmpfs to 1/4 RAM */
1052 wanted->infos[i].def_size =
1053 get_ramsize() * (MEG/512/4);
1054 }
1055
1056 /*
1057 * Now we have the defaults as if we were installing to an
1058 * empty disk. Merge the partitions in target range that are already
1059 * there (match with wanted) or are there additionaly.
1060 * The only thing outside of target range that we care for
1061 * is a potential swap partition - we assume one is enough.
1062 */
1063 size_t num = wanted->num;
1064 if (parts->parent) {
1065 for (part_id pno = 0; pno < parts->parent->num_part; pno++) {
1066 struct disk_part_info info;
1067
1068 if (!parts->parent->pscheme->get_part_info(
1069 parts->parent, pno, &info))
1070 continue;
1071 if (info.nat_type->generic_ptype != PT_swap)
1072 continue;
1073 merge_part_with_wanted(parts->parent, pno, &info,
1074 wanted, num, true);
1075 break;
1076 }
1077 }
1078 for (part_id pno = 0; pno < parts->num_part; pno++) {
1079 struct disk_part_info info;
1080
1081 if (!parts->pscheme->get_part_info(parts, pno, &info))
1082 continue;
1083
1084 if (info.flags & PTI_PSCHEME_INTERNAL)
1085 continue;
1086
1087 if (info.nat_type->generic_ptype != PT_swap &&
1088 (info.start < ptstart ||
1089 (info.start + info.size) > (ptstart+ptsize)))
1090 continue;
1091
1092 merge_part_with_wanted(parts, pno, &info,
1093 wanted, num, false);
1094 }
1095
1096 daddr_t align = parts->pscheme->get_part_alignment(parts);
1097
1098 if (root < wanted->num && wanted->infos[root].cur_part_id == NO_PART) {
1099 daddr_t max_root_size = parts->disk_start + parts->disk_size;
1100 if (root_limit > 0) {
1101 /* Bah - bios can not read all the disk, limit root */
1102 max_root_size = root_limit - parts->disk_start;
1103 }
1104 wanted->infos[root].limit = max_root_size;
1105 }
1106
1107 if (have_x11_by_default()) {
1108 daddr_t xsize = XNEEDMB * (MEG / 512);
1109 if (usr < wanted->num) {
1110 if (wanted->infos[usr].cur_part_id == NO_PART) {
1111 wanted->infos[usr].size += xsize;
1112 wanted->infos[usr].def_size += xsize;
1113 }
1114 } else if (root < wanted->num &&
1115 wanted->infos[root].cur_part_id == NO_PART &&
1116 (wanted->infos[root].limit == 0 ||
1117 (wanted->infos[root].size + xsize) <=
1118 wanted->infos[root].limit)) {
1119 wanted->infos[root].size += xsize;
1120 }
1121 }
1122 if (wanted->infos[root].limit > 0 &&
1123 wanted->infos[root].size > wanted->infos[root].limit) {
1124 if (usr < wanted->num) {
1125 /* move space from root to usr */
1126 daddr_t spill = wanted->infos[root].size -
1127 wanted->infos[root].limit;
1128 spill = roundup(spill, align);
1129 wanted->infos[root].size =
1130 wanted->infos[root].limit;
1131 wanted->infos[usr].size = spill;
1132 } else {
1133 wanted->infos[root].size =
1134 wanted->infos[root].limit;
1135 }
1136 }
1137
1138 /*
1139 * Preliminary calc additional space to allocate and how much
1140 * we likely will have left over. Use that to do further
1141 * adjustments, so we don't present the user inherently
1142 * impossible defaults.
1143 */
1144 free_space = parts->free_space;
1145 required = 0;
1146 if (root < wanted->num)
1147 required += wanted->infos[root].size;
1148 if (usr < wanted->num)
1149 required += wanted->infos[usr].size;
1150 else if (def_usr < wanted->num)
1151 required += wanted->infos[def_usr].def_size;
1152 free_space -= required;
1153 for (i = 0; i < wanted->num; i++) {
1154 if (i == root || i == usr)
1155 continue; /* already accounted above */
1156 if (wanted->infos[i].cur_part_id != NO_PART)
1157 continue;
1158 if (wanted->infos[i].size == 0)
1159 continue;
1160 if (wanted->infos[i].flags
1161 & (PUIFLG_IS_OUTER|PUIFLG_JUST_MOUNTPOINT))
1162 continue;
1163 free_space -= wanted->infos[i].size;
1164 }
1165 if (free_space < 0 && swap < wanted->num) {
1166 /* steel from swap partition */
1167 daddr_t d = wanted->infos[swap].size;
1168 daddr_t inc = roundup(-free_space, align);
1169 if (inc > d)
1170 inc = d;
1171 free_space += inc;
1172 wanted->infos[swap].size -= inc;
1173 }
1174 if (root < wanted->num) {
1175 /* Add space for 2 system dumps to / (traditional) */
1176 dump_space = get_ramsize() * (MEG/512);
1177 dump_space = roundup(dump_space, align);
1178 if (free_space > dump_space*2)
1179 dump_space *= 2;
1180 if (free_space > dump_space)
1181 wanted->infos[root].size += dump_space;
1182 }
1183 }
1184
1185 /*
1186 * We sort pset->infos to sync with pset->parts and
1187 * the cur_part_id, to allow using the same index into both
1188 * "array" in later phases. This may include inserting
1189 * dummy entries (when we do not actually want the
1190 * partition, but it is forced upon us, like RAW_PART in
1191 * disklabel).
1192 */
1193 static void
1194 sort_and_sync_parts(struct partition_usage_set *pset)
1195 {
1196 struct part_usage_info *infos;
1197 size_t i, j, no;
1198 part_id pno;
1199
1200 pset->cur_free_space = pset->parts->free_space;
1201
1202 /* count non-empty entries that are not in pset->parts */
1203 no = pset->parts->num_part;
1204 for (i = 0; i < pset->num; i++) {
1205 if (pset->infos[i].size == 0)
1206 continue;
1207 if (pset->infos[i].cur_part_id != NO_PART)
1208 continue;
1209 no++;
1210 }
1211
1212 /* allocate new infos */
1213 infos = calloc(no, sizeof *infos);
1214 if (infos == NULL)
1215 return;
1216
1217 /* pre-initialize the first entires as dummy entries */
1218 for (i = 0; i < pset->parts->num_part; i++) {
1219 infos[i].cur_part_id = NO_PART;
1220 infos[i].cur_flags = PTI_PSCHEME_INTERNAL;
1221 }
1222 /*
1223 * Now copy over eveything from our old entries that points to
1224 * a real partition.
1225 */
1226 for (i = 0; i < pset->num; i++) {
1227 pno = pset->infos[i].cur_part_id;
1228 if (pno == NO_PART)
1229 continue;
1230 if (pset->parts != pset->infos[i].parts)
1231 continue;
1232 if (pset->infos[i].flags & PUIFLG_JUST_MOUNTPOINT)
1233 continue;
1234 if ((pset->infos[i].flags & (PUIFLG_IS_OUTER|PUIFLAG_ADD_INNER))
1235 == PUIFLG_IS_OUTER)
1236 continue;
1237 if (pno >= pset->parts->num_part)
1238 continue;
1239 memcpy(infos+pno, pset->infos+i, sizeof(*infos));
1240 }
1241 /* Fill in the infos for real partitions where we had no data */
1242 for (pno = 0; pno < pset->parts->num_part; pno++) {
1243 struct disk_part_info info;
1244
1245 if (infos[pno].cur_part_id != NO_PART)
1246 continue;
1247
1248 if (!pset->parts->pscheme->get_part_info(pset->parts, pno,
1249 &info))
1250 continue;
1251
1252 infos[pno].parts = pset->parts;
1253 infos[pno].cur_part_id = pno;
1254 infos[pno].cur_flags = info.flags;
1255 infos[pno].size = info.size;
1256 infos[pno].type = info.nat_type->generic_ptype;
1257 infos[pno].cur_start = info.start;
1258 infos[pno].fs_type = info.fs_type;
1259 infos[pno].fs_version = info.fs_sub_type;
1260 }
1261 /* Add the non-partition entires after that */
1262 j = pset->num;
1263 for (i = 0; i < pset->num; i++) {
1264 if (j >= no)
1265 break;
1266 if (pset->infos[i].size == 0)
1267 continue;
1268 if (pset->infos[i].cur_part_id != NO_PART)
1269 continue;
1270 memcpy(infos+j, pset->infos+i, sizeof(*infos));
1271 j++;
1272 }
1273
1274 /* done, replace infos */
1275 free(pset->infos);
1276 pset->num = no;
1277 pset->infos = infos;
1278 }
1279
1280 static void
1281 apply_settings_to_partitions(struct pm_devs *p, struct disk_partitions *parts,
1282 struct partition_usage_set *wanted, daddr_t start, daddr_t size)
1283 {
1284 size_t i, exp_ndx = ~0U;
1285 daddr_t planned_space = 0, nsp, from, align;
1286 struct disk_part_info *infos;
1287 struct disk_part_free_space space;
1288 struct disk_partitions *ps = NULL;
1289 part_id pno, new_part_id;
1290
1291 infos = calloc(wanted->num, sizeof(*infos));
1292 if (infos == NULL) {
1293 err_msg_win(err_outofmem);
1294 return;
1295 }
1296
1297 align = wanted->parts->pscheme->get_part_alignment(wanted->parts);
1298
1299 /*
1300 * Pass one: calculate space available for expanding
1301 * the marked partition.
1302 */
1303 for (i = 0; i < wanted->num; i++) {
1304 if ((wanted->infos[i].flags & PUIFLAG_EXTEND) &&
1305 exp_ndx == ~0U)
1306 exp_ndx = i;
1307 if (wanted->infos[i].flags &
1308 (PUIFLG_JUST_MOUNTPOINT|PUIFLG_IS_OUTER))
1309 continue;
1310 nsp = wanted->infos[i].size;
1311 if (wanted->infos[i].cur_part_id != NO_PART) {
1312 ps = wanted->infos[i].flags & PUIFLG_IS_OUTER ?
1313 parts->parent : parts;
1314
1315 if (ps->pscheme->get_part_info(ps,
1316 wanted->infos[i].cur_part_id, &infos[i]))
1317 nsp -= infos[i].size;
1318 }
1319 if (nsp > 0)
1320 planned_space += roundup(nsp, align);
1321 }
1322
1323 /*
1324 * Expand the pool partition (or shrink, if we overran),
1325 */
1326 if (exp_ndx < wanted->num)
1327 wanted->infos[exp_ndx].size +=
1328 parts->free_space - planned_space;
1329
1330 /*
1331 * Now it gets tricky: we want the wanted partitions in order
1332 * as defined, but any already existing partitions should not
1333 * be moved. We allow them to change size though.
1334 * To keep it simple, we just assign in order and skip blocked
1335 * spaces. This may shuffle the order of the resulting partitions
1336 * compared to the wanted list.
1337 */
1338
1339 /* Adjust sizes of existing partitions */
1340 for (i = 0; i < wanted->num; i++) {
1341 ps = wanted->infos[i].flags & PUIFLG_IS_OUTER ?
1342 parts->parent : parts;
1343 const struct part_usage_info *want = &wanted->infos[i];
1344
1345 if (want->cur_part_id == NO_PART)
1346 continue;
1347 if (i == exp_ndx) /* the exp. part. can not exist yet */
1348 continue;
1349 daddr_t free_size = ps->pscheme->max_free_space_at(ps,
1350 infos[i].start);
1351 if (free_size < wanted->infos[i].size)
1352 continue;
1353 infos[i].size = wanted->infos[i].size;
1354 ps->pscheme->set_part_info(ps, want->cur_part_id,
1355 &infos[i], NULL);
1356 }
1357
1358 from = -1;
1359 /*
1360 * First add all outer partitions - we need to align those exactly
1361 * with the inner counterpart later.
1362 */
1363 if (parts->parent) {
1364 ps = parts->parent;
1365 daddr_t outer_align = ps->pscheme->get_part_alignment(ps);
1366
1367 for (i = 0; i < wanted->num; i++) {
1368 struct part_usage_info *want = &wanted->infos[i];
1369
1370 if (want->cur_part_id != NO_PART)
1371 continue;
1372 if (!(want->flags & PUIFLAG_ADD_OUTER))
1373 continue;
1374 if (want->size <= 0)
1375 continue;
1376
1377 size_t cnt = ps->pscheme->get_free_spaces(ps,
1378 &space, 1, want->size-2*outer_align,
1379 outer_align, from, -1);
1380
1381 if (cnt == 0) /* no free space for this partition */
1382 continue;
1383
1384 infos[i].start = space.start;
1385 infos[i].size = min(want->size, space.size);
1386 infos[i].nat_type =
1387 ps->pscheme->get_fs_part_type(
1388 want->fs_type, want->fs_version);
1389 infos[i].last_mounted = want->mount;
1390 infos[i].fs_type = want->fs_type;
1391 infos[i].fs_sub_type = want->fs_version;
1392 new_part_id = ps->pscheme->add_partition(ps,
1393 &infos[i], NULL);
1394 if (new_part_id == NO_PART)
1395 continue; /* failed to add, skip */
1396
1397 ps->pscheme->get_part_info(ps,
1398 new_part_id, &infos[i]);
1399 want->cur_part_id = new_part_id;
1400
1401 want->flags |= PUIFLAG_ADD_INNER|PUIFLG_IS_OUTER;
1402 from = rounddown(infos[i].start +
1403 infos[i].size+outer_align, outer_align);
1404 }
1405 }
1406
1407 /*
1408 * Now add new inner partitions
1409 */
1410 for (i = 0; i < wanted->num && from < wanted->parts->disk_size; i++) {
1411 struct part_usage_info *want = &wanted->infos[i];
1412
1413 if (want->cur_part_id != NO_PART)
1414 continue;
1415 if (want->flags & (PUIFLG_JUST_MOUNTPOINT|PUIFLG_IS_OUTER))
1416 continue;
1417 if (want->size <= 0)
1418 continue;
1419
1420 size_t cnt = wanted->parts->pscheme->get_free_spaces(
1421 wanted->parts, &space, 1, want->size-2*align, align, from,
1422 -1);
1423
1424 if (cnt == 0)
1425 continue; /* no free space for this partition */
1426
1427 infos[i].start = space.start;
1428 infos[i].size = min(want->size, space.size);
1429 infos[i].nat_type =
1430 wanted->parts->pscheme->get_fs_part_type(want->fs_type,
1431 want->fs_version);
1432 infos[i].last_mounted = want->mount;
1433 infos[i].fs_type = want->fs_type;
1434 infos[i].fs_sub_type = want->fs_version;
1435 if (want->fs_type != FS_UNUSED && want->type != PT_swap) {
1436 want->instflags |= PUIINST_NEWFS;
1437 if (want->mount[0] != 0)
1438 want->instflags |= PUIINST_MOUNT;
1439 }
1440 new_part_id = wanted->parts->pscheme->add_partition(
1441 wanted->parts, &infos[i], NULL);
1442 if (new_part_id == NO_PART)
1443 continue; /* failed to add, skip */
1444
1445 wanted->parts->pscheme->get_part_info(
1446 wanted->parts, new_part_id, &infos[i]);
1447 from = rounddown(infos[i].start+infos[i].size+align, align);
1448 }
1449
1450
1451 /*
1452 * If there are any outer partitions that we need as inner ones
1453 * too, add them to the inner partitioning scheme.
1454 */
1455 for (i = 0; i < wanted->num; i++) {
1456 struct part_usage_info *want = &wanted->infos[i];
1457
1458 if (want->cur_part_id != NO_PART)
1459 continue;
1460 if (want->flags & PUIFLG_JUST_MOUNTPOINT)
1461 continue;
1462 if (want->size <= 0)
1463 continue;
1464
1465 if ((want->flags & (PUIFLAG_ADD_INNER|PUIFLG_IS_OUTER)) !=
1466 (PUIFLAG_ADD_INNER|PUIFLG_IS_OUTER))
1467 continue;
1468
1469 infos[i].start = want->cur_start;
1470 infos[i].size = want->size;
1471 infos[i].nat_type = wanted->parts->pscheme->get_fs_part_type(
1472 want->fs_type, want->fs_version);
1473 infos[i].last_mounted = want->mount;
1474 infos[i].fs_type = want->fs_type;
1475 infos[i].fs_sub_type = want->fs_version;
1476
1477 if (wanted->parts->pscheme->add_outer_partition
1478 != NULL)
1479 new_part_id = wanted->parts->pscheme->
1480 add_outer_partition(
1481 wanted->parts, &infos[i], NULL);
1482 else
1483 new_part_id = wanted->parts->pscheme->
1484 add_partition(
1485 wanted->parts, &infos[i], NULL);
1486
1487 if (new_part_id == NO_PART)
1488 continue; /* failed to add, skip */
1489
1490 wanted->parts->pscheme->get_part_info(
1491 wanted->parts, new_part_id, &infos[i]);
1492 }
1493
1494 /*
1495 * Note: all part_ids are invalid now, as we have added things!
1496 */
1497 for (i = 0; i < wanted->num; i++)
1498 wanted->infos[i].cur_part_id = NO_PART;
1499 for (pno = 0; pno < parts->num_part; pno++) {
1500 struct disk_part_info t;
1501
1502 if (!parts->pscheme->get_part_info(parts, pno, &t))
1503 continue;
1504
1505 for (i = 0; i < wanted->num; i++) {
1506 if (wanted->infos[i].cur_part_id != NO_PART)
1507 continue;
1508 if (t.start == infos[i].start) {
1509 wanted->infos[i].cur_part_id = pno;
1510 wanted->infos[i].cur_start = infos[i].start;
1511 wanted->infos[i].cur_flags = infos[i].flags;
1512 break;
1513 }
1514 }
1515 }
1516 free(infos);
1517
1518 /* sort, and sync part ids and wanted->infos[] indices */
1519 sort_and_sync_parts(wanted);
1520 }
1521
1522 static void
1523 replace_by_default(struct pm_devs *p, struct disk_partitions *parts,
1524 daddr_t start, daddr_t size, struct partition_usage_set *wanted)
1525 {
1526
1527 if (start == 0 && size == parts->disk_size)
1528 parts->pscheme->delete_all_partitions(parts);
1529 else if (parts->pscheme->delete_partitions_in_range != NULL)
1530 parts->pscheme->delete_partitions_in_range(parts, start, size);
1531 else
1532 assert(parts->num_part == 0);
1533
1534 fill_defaults(wanted, parts, start, size);
1535 apply_settings_to_partitions(p, parts, wanted, start, size);
1536 }
1537
1538 static bool
1539 edit_with_defaults(struct pm_devs *p, struct disk_partitions *parts,
1540 daddr_t start, daddr_t size, struct partition_usage_set *wanted)
1541 {
1542 bool ok;
1543
1544 fill_defaults(wanted, parts, start, size);
1545 ok = get_ptn_sizes(wanted);
1546 if (ok)
1547 apply_settings_to_partitions(p, parts, wanted, start, size);
1548 return ok;
1549 }
1550
1551 /*
1552 * md back-end code for menu-driven BSD disklabel editor.
1553 * returns 0 on failure, 1 on success.
1554 * fills the install target with a list for newfs/fstab.
1555 */
1556 bool
1557 make_bsd_partitions(struct install_partition_desc *install)
1558 {
1559 struct disk_partitions *parts = pm->parts;
1560 const struct disk_partitioning_scheme *pscheme;
1561 struct partition_usage_set wanted;
1562 enum layout_type layoutkind = LY_SETSIZES;
1563 bool have_existing;
1564
1565 if (pm && pm->no_part && parts == NULL)
1566 return true;
1567
1568 if (parts == NULL) {
1569 pscheme = select_part_scheme(pm, NULL, true, NULL);
1570 if (pscheme == NULL)
1571 return false;
1572 parts = pscheme->create_new_for_disk(pm->diskdev,
1573 0, pm->dlsize, pm->dlsize, true);
1574 if (parts == NULL)
1575 return false;
1576 pm->parts = parts;
1577 } else {
1578 pscheme = parts->pscheme;
1579 }
1580
1581 if (pscheme->secondary_partitions) {
1582 struct disk_partitions *p;
1583
1584 p = pscheme->secondary_partitions(parts, pm->ptstart, false);
1585 if (p) {
1586 parts = p;
1587 pscheme = parts->pscheme;
1588 }
1589 }
1590
1591 have_existing = check_existing_netbsd(parts);
1592
1593 /*
1594 * Initialize global variables that track space used on this disk.
1595 */
1596 if (pm->ptsize == 0)
1597 pm->ptsize = pm->dlsize - pm->ptstart;
1598 if (pm->dlsize == 0)
1599 pm->dlsize = pm->ptstart + pm->ptsize;
1600
1601 if (logfp) fprintf(logfp, "dlsize=%" PRId64 " ptsize=%" PRId64
1602 " ptstart=%" PRId64 "\n",
1603 pm->dlsize, pm->ptsize, pm->ptstart);
1604
1605 if (pm->current_cylsize == 0)
1606 pm->current_cylsize = pm->dlcylsize;
1607
1608 /* Ask for layout type -- standard or special */
1609 if (partman_go == 0) {
1610 char bsd_size[6], min_size[6], x_size[6];
1611
1612 humanize_number(bsd_size, sizeof(bsd_size),
1613 (uint64_t)pm->ptsize*pm->sectorsize,
1614 "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1615 humanize_number(min_size, sizeof(min_size),
1616 (uint64_t)(DEFROOTSIZE + DEFSWAPSIZE + DEFUSRSIZE)*MEG,
1617 "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1618 humanize_number(x_size, sizeof(x_size),
1619 (uint64_t)(DEFROOTSIZE + DEFSWAPSIZE + DEFUSRSIZE
1620 + XNEEDMB)*MEG,
1621 "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1622
1623 msg_display_subst(
1624 have_existing ? MSG_layout_prologue_existing
1625 : MSG_layout_prologue_none, 6, pm->diskdev,
1626 msg_string(parts->pscheme->name),
1627 msg_string(parts->pscheme->short_name),
1628 bsd_size, min_size, x_size);
1629 msg_display_add_subst(MSG_layout_main, 6,
1630 pm->diskdev,
1631 msg_string(parts->pscheme->name),
1632 msg_string(parts->pscheme->short_name),
1633 bsd_size, min_size, x_size);
1634 msg_display_add("\n\n");
1635 layoutkind = ask_layout(parts, have_existing);
1636 }
1637
1638 if (layoutkind == LY_USEDEFAULT) {
1639 replace_by_default(pm, parts, pm->ptstart, pm->ptsize,
1640 &wanted);
1641 } else if (layoutkind == LY_SETSIZES) {
1642 if (!edit_with_defaults(pm, parts, pm->ptstart, pm->ptsize,
1643 &wanted)) {
1644 free_usage_set(&wanted);
1645 return false;
1646 }
1647 } else {
1648 usage_set_from_parts(&wanted, parts);
1649 }
1650
1651 /*
1652 * OK, we have a partition table. Give the user the chance to
1653 * edit it and verify it's OK, or abort altogether.
1654 */
1655 for (;;) {
1656 int rv = edit_and_check_label(pm, &wanted);
1657 if (rv == 0) {
1658 msg_display(MSG_abort_part);
1659 free_usage_set(&wanted);
1660 return false;
1661 }
1662 /* update install infos */
1663 install->num = wanted.num;
1664 install->infos = wanted.infos;
1665 /* and check them */
1666 if (check_partitions(install))
1667 break;
1668 }
1669
1670 /* we moved infos from wanted to install target */
1671 wanted.infos = NULL;
1672 free_usage_set(&wanted);
1673
1674 /* Everything looks OK. */
1675 return true;
1676 }
1677
1678 #ifndef MD_NEED_BOOTBLOCK
1679 #define MD_NEED_BOOTBLOCK(A) true
1680 #endif
1681
1682 /*
1683 * check that there is at least a / somewhere.
1684 */
1685 bool
1686 check_partitions(struct install_partition_desc *install)
1687 {
1688 #ifdef HAVE_BOOTXX_xFS
1689 int rv = 1;
1690 char *bootxx;
1691 #endif
1692 #ifndef HAVE_UFS2_BOOT
1693 size_t i;
1694 #endif
1695
1696 #ifdef HAVE_BOOTXX_xFS
1697 if (MD_NEED_BOOTBLOCK(install)) {
1698 /* check if we have boot code for the root partition type */
1699 bootxx = bootxx_name(install);
1700 if (bootxx != NULL) {
1701 rv = access(bootxx, R_OK);
1702 free(bootxx);
1703 } else
1704 rv = -1;
1705 if (rv != 0) {
1706 hit_enter_to_continue(NULL, MSG_No_Bootcode);
1707 return false;
1708 }
1709 }
1710 #endif
1711 #ifndef HAVE_UFS2_BOOT
1712 if (MD_NEED_BOOTBLOCK(install)) {
1713 for (i = 0; i < install->num; i++) {
1714 if (install->infos[i].type != PT_root)
1715 continue;
1716 if (strcmp(install->infos[i].mount, "/") != 0)
1717 continue;
1718 if (install->infos[i].fs_type != FS_BSDFFS)
1719 continue;
1720 if (install->infos[i].fs_version != 2)
1721 continue;
1722 hit_enter_to_continue(NULL, MSG_cannot_ufs2_root);
1723 return false;
1724 }
1725 }
1726 #endif
1727
1728 return md_check_partitions(install);
1729 }
1730