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