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