xref: /netbsd-src/usr.sbin/sysinst/install.c (revision 72694b5f3c62c99cad4e73ba476310b078ddbf90)
1*72694b5fSmartin /*	$NetBSD: install.c,v 1.25 2023/01/06 18:13:40 martin Exp $	*/
250dbef1aSdholland 
350dbef1aSdholland /*
450dbef1aSdholland  * Copyright 1997 Piermont Information Systems Inc.
550dbef1aSdholland  * All rights reserved.
650dbef1aSdholland  *
750dbef1aSdholland  * Written by Philip A. Nelson for Piermont Information Systems Inc.
850dbef1aSdholland  *
950dbef1aSdholland  * Redistribution and use in source and binary forms, with or without
1050dbef1aSdholland  * modification, are permitted provided that the following conditions
1150dbef1aSdholland  * are met:
1250dbef1aSdholland  * 1. Redistributions of source code must retain the above copyright
1350dbef1aSdholland  *    notice, this list of conditions and the following disclaimer.
1450dbef1aSdholland  * 2. Redistributions in binary form must reproduce the above copyright
1550dbef1aSdholland  *    notice, this list of conditions and the following disclaimer in the
1650dbef1aSdholland  *    documentation and/or other materials provided with the distribution.
1750dbef1aSdholland  * 3. The name of Piermont Information Systems Inc. may not be used to endorse
1850dbef1aSdholland  *    or promote products derived from this software without specific prior
1950dbef1aSdholland  *    written permission.
2050dbef1aSdholland  *
2150dbef1aSdholland  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
2250dbef1aSdholland  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2350dbef1aSdholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2450dbef1aSdholland  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
2550dbef1aSdholland  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2650dbef1aSdholland  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2750dbef1aSdholland  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2850dbef1aSdholland  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2950dbef1aSdholland  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3050dbef1aSdholland  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3150dbef1aSdholland  * THE POSSIBILITY OF SUCH DAMAGE.
3250dbef1aSdholland  *
3350dbef1aSdholland  */
3450dbef1aSdholland 
3550dbef1aSdholland /* install.c -- system installation. */
3650dbef1aSdholland 
37f77b58b1Smartin #include <sys/param.h>
3850dbef1aSdholland #include <stdio.h>
3950dbef1aSdholland #include <curses.h>
4050dbef1aSdholland #include "defs.h"
4150dbef1aSdholland #include "msg_defs.h"
4250dbef1aSdholland #include "menu_defs.h"
4350dbef1aSdholland 
444103857bSmartin /*
454103857bSmartin  * helper function: call the md pre/post disklabel callback for all involved
464103857bSmartin  * inner partitions and write them back.
474103857bSmartin  * return net result
484103857bSmartin  */
494103857bSmartin static bool
write_all_parts(struct install_partition_desc * install)504103857bSmartin write_all_parts(struct install_partition_desc *install)
514103857bSmartin {
524103857bSmartin 	struct disk_partitions **allparts, *parts;
53485d5309Smartin #ifndef NO_CLONES
54f77b58b1Smartin 	struct selected_partition *src;
55485d5309Smartin #endif
564103857bSmartin 	size_t num_parts, i, j;
574103857bSmartin 	bool found, res;
584103857bSmartin 
594103857bSmartin 	/* pessimistic assumption: all partitions on different devices */
60194d5a77Smartin 	allparts = calloc(install->num + install->num_write_back,
61194d5a77Smartin 	    sizeof(*allparts));
624103857bSmartin 	if (allparts == NULL)
634103857bSmartin 		return false;
644103857bSmartin 
654103857bSmartin 	/* collect all different partition sets */
664103857bSmartin 	num_parts = 0;
67194d5a77Smartin 	for (i = 0; i < install->num_write_back; i++) {
68194d5a77Smartin 		parts = install->write_back[i];
69194d5a77Smartin 		if (parts == NULL)
70194d5a77Smartin 			continue;
71194d5a77Smartin 		found = false;
72194d5a77Smartin 		for (j = 0; j < num_parts; j++) {
73194d5a77Smartin 			if (allparts[j] == parts) {
74194d5a77Smartin 				found = true;
75194d5a77Smartin 				break;
76194d5a77Smartin 			}
77194d5a77Smartin 		}
78194d5a77Smartin 		if (found)
79194d5a77Smartin 			continue;
80194d5a77Smartin 		allparts[num_parts++] = parts;
81194d5a77Smartin 	}
824103857bSmartin 	for (i = 0; i < install->num; i++) {
834103857bSmartin 		parts = install->infos[i].parts;
844103857bSmartin 		if (parts == NULL)
854103857bSmartin 			continue;
864103857bSmartin 		found = false;
874103857bSmartin 		for (j = 0; j < num_parts; j++) {
884103857bSmartin 			if (allparts[j] == parts) {
894103857bSmartin 				found = true;
904103857bSmartin 				break;
914103857bSmartin 			}
924103857bSmartin 		}
934103857bSmartin 		if (found)
944103857bSmartin 			continue;
954103857bSmartin 		allparts[num_parts++] = parts;
964103857bSmartin 	}
974103857bSmartin 
98f77b58b1Smartin 	/* do multiple phases, abort anytime and go out, returning res */
994103857bSmartin 
1004103857bSmartin 	res = true;
1014103857bSmartin 
1024103857bSmartin 	/* phase 1: pre disklabel - used to write MBR and similar */
1034103857bSmartin 	for (i = 0; i < num_parts; i++) {
1044103857bSmartin 		if (!md_pre_disklabel(install, allparts[i])) {
1054103857bSmartin 			res = false;
1064103857bSmartin 			goto out;
1074103857bSmartin 		}
1084103857bSmartin 	}
1094103857bSmartin 
1104103857bSmartin 	/* phase 2: write our partitions (used to be: disklabel) */
1114103857bSmartin 	for (i = 0; i < num_parts; i++) {
1124103857bSmartin 		if (!allparts[i]->pscheme->write_to_disk(allparts[i])) {
1134103857bSmartin 			res = false;
1144103857bSmartin 			goto out;
1154103857bSmartin 		}
1164103857bSmartin 	}
1174103857bSmartin 
1184103857bSmartin 	/* phase 3: now we may have a first chance to enable swap space */
1194103857bSmartin 	set_swap_if_low_ram(install);
1204103857bSmartin 
121485d5309Smartin #ifndef NO_CLONES
122f77b58b1Smartin 	/* phase 4: copy any cloned partitions data (if requested) */
123f77b58b1Smartin 	for (i = 0; i < install->num; i++) {
124f77b58b1Smartin 		if ((install->infos[i].flags & PUIFLG_CLONE_PARTS) == 0
125f77b58b1Smartin 		    || install->infos[i].clone_src == NULL
126f77b58b1Smartin 		    || !install->infos[i].clone_src->with_data)
127f77b58b1Smartin 			continue;
128f77b58b1Smartin 		src = &install->infos[i].clone_src
129f77b58b1Smartin 		    ->selection[install->infos[i].clone_ndx];
130f77b58b1Smartin 		clone_partition_data(install->infos[i].parts,
131f77b58b1Smartin 		    install->infos[i].cur_part_id,
132f77b58b1Smartin 		    src->parts, src->id);
133f77b58b1Smartin 	}
134485d5309Smartin #endif
135f77b58b1Smartin 
136f77b58b1Smartin 	/* phase 5: post disklabel (used for updating boot loaders) */
1374103857bSmartin 	for (i = 0; i < num_parts; i++) {
1384103857bSmartin 		if (!md_post_disklabel(install, allparts[i])) {
1394103857bSmartin 			res = false;
1404103857bSmartin 			goto out;
1414103857bSmartin 		}
1424103857bSmartin 	}
1434103857bSmartin 
1444103857bSmartin out:
1454103857bSmartin 	free(allparts);
1464103857bSmartin 
1474103857bSmartin 	return res;
1484103857bSmartin }
1494103857bSmartin 
15050dbef1aSdholland /* Do the system install. */
15150dbef1aSdholland 
15250dbef1aSdholland void
do_install(void)15350dbef1aSdholland do_install(void)
15450dbef1aSdholland {
1554b2364d9Smartin 	int find_disks_ret;
156957b5cd6Smartin 	int retcode = 0, res;
157b1fa9754Smartin 	struct install_partition_desc install = {};
1584103857bSmartin 
159d1110ccaSrin #ifndef NO_PARTMAN
1604b2364d9Smartin 	partman_go = -1;
161d1110ccaSrin #else
162d1110ccaSrin 	partman_go = 0;
163d1110ccaSrin #endif
16450dbef1aSdholland 
1654b2364d9Smartin #ifndef DEBUG
16650dbef1aSdholland 	msg_display(MSG_installusure);
167e21052b4Smartin 	if (!ask_noyes(NULL))
16850dbef1aSdholland 		return;
1694b2364d9Smartin #endif
17050dbef1aSdholland 
1714103857bSmartin 	memset(&install, 0, sizeof install);
1724103857bSmartin 
1734b2364d9Smartin 	/* Create and mount partitions */
174b1fa9754Smartin 	find_disks_ret = find_disks(msg_string(MSG_install), false);
1754b2364d9Smartin 	if (partman_go == 1) {
176c38850fbSmartin 		if (partman(&install) < 0) {
1774103857bSmartin 			hit_enter_to_continue(MSG_abort_part, NULL);
17850dbef1aSdholland 			return;
1794b2364d9Smartin 		}
1804b2364d9Smartin 	} else if (find_disks_ret < 0)
1814b2364d9Smartin 		return;
1824b2364d9Smartin 	else {
1834b2364d9Smartin 	/* Classical partitioning wizard */
1844b2364d9Smartin 		partman_go = 0;
18550dbef1aSdholland 		clear();
18650dbef1aSdholland 		refresh();
18750dbef1aSdholland 
1884b2364d9Smartin 		if (check_swap(pm->diskdev, 0) > 0) {
1894103857bSmartin 			hit_enter_to_continue(MSG_swapactive, NULL);
1904b2364d9Smartin 			if (check_swap(pm->diskdev, 1) < 0) {
1914103857bSmartin 				hit_enter_to_continue(MSG_swapdelfailed, NULL);
19250dbef1aSdholland 				if (!debug)
19350dbef1aSdholland 					return;
19450dbef1aSdholland 			}
19550dbef1aSdholland 		}
19650dbef1aSdholland 
197957b5cd6Smartin 		for (;;) {
198957b5cd6Smartin 			if (md_get_info(&install)) {
199957b5cd6Smartin 				res = md_make_bsd_partitions(&install);
200957b5cd6Smartin 				if (res == -1) {
201957b5cd6Smartin 					pm->parts = NULL;
202957b5cd6Smartin 					continue;
203957b5cd6Smartin 				} else if (res == 1) {
204957b5cd6Smartin 					break;
205957b5cd6Smartin 				}
206957b5cd6Smartin 			}
2074103857bSmartin 			hit_enter_to_continue(MSG_abort_inst, NULL);
20831a289cdSmartin 			goto error;
20950dbef1aSdholland 		}
21050dbef1aSdholland 
21150dbef1aSdholland 		/* Last chance ... do you really want to do this? */
21250dbef1aSdholland 		clear();
21350dbef1aSdholland 		refresh();
21424ecf24eSchristos 		msg_fmt_display(MSG_lastchance, "%s", pm->diskdev);
215e21052b4Smartin 		if (!ask_noyes(NULL))
21631a289cdSmartin 			goto error;
21750dbef1aSdholland 
2184103857bSmartin 		if ((!pm->no_part && !write_all_parts(&install)) ||
2194103857bSmartin 		    make_filesystems(&install) ||
2204103857bSmartin 		    make_fstab(&install) != 0 ||
2214103857bSmartin 		    md_post_newfs(&install) != 0)
22231a289cdSmartin 			goto error;
2234b2364d9Smartin 	}
22450dbef1aSdholland 
22550dbef1aSdholland 	/* Unpack the distribution. */
2264b2364d9Smartin 	process_menu(MENU_distset, &retcode);
2274b2364d9Smartin 	if (retcode == 0)
22831a289cdSmartin 		goto error;
22950dbef1aSdholland 	if (get_and_unpack_sets(0, MSG_disksetupdone,
23050dbef1aSdholland 	    MSG_extractcomplete, MSG_abortinst) != 0)
23131a289cdSmartin 		goto error;
23250dbef1aSdholland 
2334204f810Smartin 	if (md_post_extract(&install, false) != 0)
23431a289cdSmartin 		goto error;
23550dbef1aSdholland 
23618183f70Smartin 	root_pw_setup();
23718183f70Smartin #if CHECK_ENTROPY
23818183f70Smartin 	do_add_entropy();
23918183f70Smartin #endif
2404103857bSmartin 	do_configmenu(&install);
24150dbef1aSdholland 
24250dbef1aSdholland 	sanity_check();
24350dbef1aSdholland 
2444103857bSmartin 	md_cleanup_install(&install);
24550dbef1aSdholland 
2464103857bSmartin 	hit_enter_to_continue(MSG_instcomplete, NULL);
24731a289cdSmartin 
24831a289cdSmartin error:
249f77b58b1Smartin 	free_install_desc(&install);
25050dbef1aSdholland }
251