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