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