Home | History | Annotate | Line # | Download | only in sysinst
install.c revision 1.18
      1 /*	$NetBSD: install.c,v 1.18 2020/10/12 16:14:32 martin Exp $	*/
      2 
      3 /*
      4  * Copyright 1997 Piermont Information Systems Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Philip A. Nelson for Piermont Information Systems Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. The name of Piermont Information Systems Inc. may not be used to endorse
     18  *    or promote products derived from this software without specific prior
     19  *    written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
     22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
     25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     31  * THE POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  */
     34 
     35 /* install.c -- system installation. */
     36 
     37 #include <sys/param.h>
     38 #include <stdio.h>
     39 #include <curses.h>
     40 #include "defs.h"
     41 #include "msg_defs.h"
     42 #include "menu_defs.h"
     43 
     44 /*
     45  * helper function: call the md pre/post disklabel callback for all involved
     46  * inner partitions and write them back.
     47  * return net result
     48  */
     49 static bool
     50 write_all_parts(struct install_partition_desc *install)
     51 {
     52 	struct disk_partitions **allparts, *parts;
     53 #ifndef NO_CLONES
     54 	struct selected_partition *src;
     55 #endif
     56 	size_t num_parts, i, j;
     57 	bool found, res;
     58 
     59 	/* pessimistic assumption: all partitions on different devices */
     60 	allparts = calloc(install->num + install->num_write_back,
     61 	    sizeof(*allparts));
     62 	if (allparts == NULL)
     63 		return false;
     64 
     65 	/* collect all different partition sets */
     66 	num_parts = 0;
     67 	for (i = 0; i < install->num_write_back; i++) {
     68 		parts = install->write_back[i];
     69 		if (parts == NULL)
     70 			continue;
     71 		found = false;
     72 		for (j = 0; j < num_parts; j++) {
     73 			if (allparts[j] == parts) {
     74 				found = true;
     75 				break;
     76 			}
     77 		}
     78 		if (found)
     79 			continue;
     80 		allparts[num_parts++] = parts;
     81 	}
     82 	for (i = 0; i < install->num; i++) {
     83 		parts = install->infos[i].parts;
     84 		if (parts == NULL)
     85 			continue;
     86 		found = false;
     87 		for (j = 0; j < num_parts; j++) {
     88 			if (allparts[j] == parts) {
     89 				found = true;
     90 				break;
     91 			}
     92 		}
     93 		if (found)
     94 			continue;
     95 		allparts[num_parts++] = parts;
     96 	}
     97 
     98 	/* do multiple phases, abort anytime and go out, returning res */
     99 
    100 	res = true;
    101 
    102 	/* phase 1: pre disklabel - used to write MBR and similar */
    103 	for (i = 0; i < num_parts; i++) {
    104 		if (!md_pre_disklabel(install, allparts[i])) {
    105 			res = false;
    106 			goto out;
    107 		}
    108 	}
    109 
    110 	/* phase 2: write our partitions (used to be: disklabel) */
    111 	for (i = 0; i < num_parts; i++) {
    112 		if (!allparts[i]->pscheme->write_to_disk(allparts[i])) {
    113 			res = false;
    114 			goto out;
    115 		}
    116 	}
    117 
    118 	/* phase 3: now we may have a first chance to enable swap space */
    119 	set_swap_if_low_ram(install);
    120 
    121 #ifndef NO_CLONES
    122 	/* phase 4: copy any cloned partitions data (if requested) */
    123 	for (i = 0; i < install->num; i++) {
    124 		if ((install->infos[i].flags & PUIFLG_CLONE_PARTS) == 0
    125 		    || install->infos[i].clone_src == NULL
    126 		    || !install->infos[i].clone_src->with_data)
    127 			continue;
    128 		src = &install->infos[i].clone_src
    129 		    ->selection[install->infos[i].clone_ndx];
    130 		clone_partition_data(install->infos[i].parts,
    131 		    install->infos[i].cur_part_id,
    132 		    src->parts, src->id);
    133 	}
    134 #endif
    135 
    136 	/* phase 5: post disklabel (used for updating boot loaders) */
    137 	for (i = 0; i < num_parts; i++) {
    138 		if (!md_post_disklabel(install, allparts[i])) {
    139 			res = false;
    140 			goto out;
    141 		}
    142 	}
    143 
    144 out:
    145 	free(allparts);
    146 
    147 	return res;
    148 }
    149 
    150 /* Do the system install. */
    151 
    152 void
    153 do_install(void)
    154 {
    155 	int find_disks_ret;
    156 	int retcode = 0, res;
    157 	struct install_partition_desc install = {};
    158 	struct disk_partitions *parts;
    159 
    160 #ifndef NO_PARTMAN
    161 	partman_go = -1;
    162 #else
    163 	partman_go = 0;
    164 #endif
    165 
    166 #ifndef DEBUG
    167 	msg_display(MSG_installusure);
    168 	if (!ask_noyes(NULL))
    169 		return;
    170 #endif
    171 
    172 	memset(&install, 0, sizeof install);
    173 
    174 	/* Create and mount partitions */
    175 	find_disks_ret = find_disks(msg_string(MSG_install), false);
    176 	if (partman_go == 1) {
    177 		if (partman() < 0) {
    178 			hit_enter_to_continue(MSG_abort_part, NULL);
    179 			return;
    180 		}
    181 	} else if (find_disks_ret < 0)
    182 		return;
    183 	else {
    184 	/* Classical partitioning wizard */
    185 		partman_go = 0;
    186 		clear();
    187 		refresh();
    188 
    189 		if (check_swap(pm->diskdev, 0) > 0) {
    190 			hit_enter_to_continue(MSG_swapactive, NULL);
    191 			if (check_swap(pm->diskdev, 1) < 0) {
    192 				hit_enter_to_continue(MSG_swapdelfailed, NULL);
    193 				if (!debug)
    194 					return;
    195 			}
    196 		}
    197 
    198 		for (;;) {
    199 			if (md_get_info(&install)) {
    200 				res = md_make_bsd_partitions(&install);
    201 				if (res == -1) {
    202 					pm->parts = NULL;
    203 					continue;
    204 				} else if (res == 1) {
    205 					break;
    206 				}
    207 			}
    208 			hit_enter_to_continue(MSG_abort_inst, NULL);
    209 			goto error;
    210 		}
    211 
    212 		/* Last chance ... do you really want to do this? */
    213 		clear();
    214 		refresh();
    215 		msg_fmt_display(MSG_lastchance, "%s", pm->diskdev);
    216 		if (!ask_noyes(NULL))
    217 			goto error;
    218 
    219 		/*
    220 		 * Check if we have a secondary partitioning and
    221 		 * use that if available. The MD code will typically
    222 		 * have written the outer partitioning in md_pre_disklabel.
    223 		 */
    224 		parts = pm->parts;
    225 		if (!pm->no_part && parts != NULL) {
    226 			if (parts->pscheme->secondary_scheme != NULL &&
    227 			    parts->pscheme->secondary_partitions != NULL) {
    228 				parts = parts->pscheme->secondary_partitions(
    229 				    parts, pm->ptstart, false);
    230 				if (parts == NULL)
    231 					parts = pm->parts;
    232 			}
    233 		}
    234 		if ((!pm->no_part && !write_all_parts(&install)) ||
    235 		    make_filesystems(&install) ||
    236 		    make_fstab(&install) != 0 ||
    237 		    md_post_newfs(&install) != 0)
    238 		goto error;
    239 	}
    240 
    241 	/* Unpack the distribution. */
    242 	process_menu(MENU_distset, &retcode);
    243 	if (retcode == 0)
    244 		goto error;
    245 	if (get_and_unpack_sets(0, MSG_disksetupdone,
    246 	    MSG_extractcomplete, MSG_abortinst) != 0)
    247 		goto error;
    248 
    249 	if (md_post_extract(&install) != 0)
    250 		goto error;
    251 
    252 	do_configmenu(&install);
    253 
    254 	sanity_check();
    255 
    256 	md_cleanup_install(&install);
    257 
    258 	hit_enter_to_continue(MSG_instcomplete, NULL);
    259 
    260 error:
    261 	free_install_desc(&install);
    262 }
    263