install.c revision 1.12 1 /* $NetBSD: install.c,v 1.12 2019/11/12 16:33:14 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 struct selected_partition *src;
54 size_t num_parts, i, j;
55 bool found, res;
56
57 /* pessimistic assumption: all partitions on different devices */
58 allparts = calloc(install->num, sizeof(*allparts));
59 if (allparts == NULL)
60 return false;
61
62 /* collect all different partition sets */
63 num_parts = 0;
64 for (i = 0; i < install->num; i++) {
65 parts = install->infos[i].parts;
66 if (parts == NULL)
67 continue;
68 found = false;
69 for (j = 0; j < num_parts; j++) {
70 if (allparts[j] == parts) {
71 found = true;
72 break;
73 }
74 }
75 if (found)
76 continue;
77 allparts[num_parts++] = parts;
78 }
79
80 /* do multiple phases, abort anytime and go out, returning res */
81
82 res = true;
83
84 /* phase 1: pre disklabel - used to write MBR and similar */
85 for (i = 0; i < num_parts; i++) {
86 if (!md_pre_disklabel(install, allparts[i])) {
87 res = false;
88 goto out;
89 }
90 }
91
92 /* phase 2: write our partitions (used to be: disklabel) */
93 for (i = 0; i < num_parts; i++) {
94 if (!allparts[i]->pscheme->write_to_disk(allparts[i])) {
95 res = false;
96 goto out;
97 }
98 }
99
100 /* phase 3: now we may have a first chance to enable swap space */
101 set_swap_if_low_ram(install);
102
103 /* phase 4: copy any cloned partitions data (if requested) */
104 for (i = 0; i < install->num; i++) {
105 if ((install->infos[i].flags & PUIFLG_CLONE_PARTS) == 0
106 || install->infos[i].clone_src == NULL
107 || !install->infos[i].clone_src->with_data)
108 continue;
109 src = &install->infos[i].clone_src
110 ->selection[install->infos[i].clone_ndx];
111 clone_partition_data(install->infos[i].parts,
112 install->infos[i].cur_part_id,
113 src->parts, src->id);
114 }
115
116 /* phase 5: post disklabel (used for updating boot loaders) */
117 for (i = 0; i < num_parts; i++) {
118 if (!md_post_disklabel(install, allparts[i])) {
119 res = false;
120 goto out;
121 }
122 }
123
124 out:
125 free(allparts);
126
127 return res;
128 }
129
130 /* Do the system install. */
131
132 void
133 do_install(void)
134 {
135 int find_disks_ret;
136 int retcode = 0;
137 struct install_partition_desc install = {};
138 struct disk_partitions *parts;
139
140 #ifndef NO_PARTMAN
141 partman_go = -1;
142 #else
143 partman_go = 0;
144 #endif
145
146 #ifndef DEBUG
147 msg_display(MSG_installusure);
148 if (!ask_noyes(NULL))
149 return;
150 #endif
151
152 memset(&install, 0, sizeof install);
153
154 get_ramsize();
155
156 /* Create and mount partitions */
157 find_disks_ret = find_disks(msg_string(MSG_install), false);
158 if (partman_go == 1) {
159 if (partman() < 0) {
160 hit_enter_to_continue(MSG_abort_part, NULL);
161 return;
162 }
163 } else if (find_disks_ret < 0)
164 return;
165 else {
166 /* Classical partitioning wizard */
167 partman_go = 0;
168 clear();
169 refresh();
170
171 if (check_swap(pm->diskdev, 0) > 0) {
172 hit_enter_to_continue(MSG_swapactive, NULL);
173 if (check_swap(pm->diskdev, 1) < 0) {
174 hit_enter_to_continue(MSG_swapdelfailed, NULL);
175 if (!debug)
176 return;
177 }
178 }
179
180 if (!md_get_info(&install) ||
181 !md_make_bsd_partitions(&install)) {
182 hit_enter_to_continue(MSG_abort_inst, NULL);
183 goto error;
184 }
185
186 /* Last chance ... do you really want to do this? */
187 clear();
188 refresh();
189 msg_fmt_display(MSG_lastchance, "%s", pm->diskdev);
190 if (!ask_noyes(NULL))
191 goto error;
192
193 /*
194 * Check if we have a secondary partitioning and
195 * use that if available. The MD code will typically
196 * have written the outer partitioning in md_pre_disklabel.
197 */
198 parts = pm->parts;
199 if (!pm->no_part && parts != NULL) {
200 if (parts->pscheme->secondary_scheme != NULL &&
201 parts->pscheme->secondary_partitions != NULL) {
202 parts = parts->pscheme->secondary_partitions(
203 parts, pm->ptstart, false);
204 if (parts == NULL)
205 parts = pm->parts;
206 }
207 }
208 if ((!pm->no_part && !write_all_parts(&install)) ||
209 make_filesystems(&install) ||
210 make_fstab(&install) != 0 ||
211 md_post_newfs(&install) != 0)
212 goto error;
213 }
214
215 /* Unpack the distribution. */
216 process_menu(MENU_distset, &retcode);
217 if (retcode == 0)
218 goto error;
219 if (get_and_unpack_sets(0, MSG_disksetupdone,
220 MSG_extractcomplete, MSG_abortinst) != 0)
221 goto error;
222
223 if (md_post_extract(&install) != 0)
224 goto error;
225
226 do_configmenu(&install);
227
228 sanity_check();
229
230 md_cleanup_install(&install);
231
232 hit_enter_to_continue(MSG_instcomplete, NULL);
233
234 error:
235 free_install_desc(&install);
236 }
237