xref: /netbsd-src/usr.sbin/sysinst/upgrade.c (revision 404ee5b9334f618040b6cdef96a0ff35a6fc4636)
1 /*	$NetBSD: upgrade.c,v 1.13 2019/08/27 14:11:00 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 /* upgrade.c -- upgrade an installation. */
36 
37 #include <sys/param.h>
38 #include <stdio.h>
39 #include <curses.h>
40 #include <errno.h>
41 #include "defs.h"
42 #include "msg_defs.h"
43 #include "menu_defs.h"
44 
45 /*
46  * local prototypes
47  */
48 static int save_X(const char *);
49 static int merge_X(const char *);
50 
51 /*
52  * Do the system upgrade.
53  */
54 void
55 do_upgrade(void)
56 {
57 	struct install_partition_desc install = {};
58 	int retcode = 0;
59 	partman_go = 0;
60 
61 	msg_display(MSG_upgradeusure);
62 	if (!ask_noyes(NULL))
63 		return;
64 
65 	get_ramsize();
66 
67 	if (find_disks(msg_string(MSG_upgrade), !root_is_read_only()) < 0)
68 		return;
69 
70 	if (pm->parts == NULL && !pm->cur_system) {
71 		hit_enter_to_continue(MSG_noroot, NULL);
72 		return;
73 	}
74 
75 	if (!pm->cur_system) {
76 		if (pm->parts->pscheme->pre_update_verify) {
77 			if (pm->parts->pscheme->pre_update_verify(pm->parts))
78 				pm->parts->pscheme->write_to_disk(pm->parts);
79 		}
80 
81 		install_desc_from_parts(&install, pm->parts);
82 	} else {
83 		install.cur_system = true;
84 	}
85 
86 	if (set_swap_if_low_ram(&install) < 0)
87 		return;
88 
89 	if (md_pre_update(&install) < 0)
90 		goto free_install;
91 
92 	if (mount_disks(&install) != 0)
93 		goto free_install;
94 
95 
96 	/*
97 	 * Save X symlink, ...
98 	 */
99 	if (save_X("/usr/X11R6"))
100 		goto free_install;
101 	if (save_X("/usr/X11R7"))
102 		goto free_install;
103 
104 #ifdef AOUT2ELF
105 	move_aout_libs();
106 #endif
107 	/* Do any md updating of the file systems ... e.g. bootblocks,
108 	   copy file systems ... */
109 	/* XXX pass install here too */
110 	if (!md_update(&install))
111 		goto free_install;
112 
113 	wrefresh(curscr);
114 	wmove(stdscr, 0, 0);
115 	wclear(stdscr);
116 	wrefresh(stdscr);
117 
118 	/* Done with disks. Ready to get and unpack tarballs. */
119 	process_menu(MENU_distset, &retcode);
120 	if (retcode == 0)
121 		goto free_install;
122 	if (get_and_unpack_sets(1, MSG_disksetupdoneupdate,
123 	    MSG_upgrcomplete, MSG_abortupgr) != 0)
124 		goto free_install;
125 
126 	if (md_post_extract(&install))
127 		goto free_install;
128 
129 	merge_X("/usr/X11R6");
130 	merge_X("/usr/X11R7");
131 
132 	sanity_check();
133 
134 free_install:
135 	free_install_desc(&install);
136 }
137 
138 /*
139  * Save X symlink to X.old so it can be recovered later
140  */
141 static int
142 save_X(const char *xroot)
143 {
144 	char newx[MAXPATHLEN], oldx[MAXPATHLEN];
145 
146 	strlcpy(newx, xroot, sizeof(newx));
147 	strlcat(newx, "/bin/X", sizeof(newx));
148 	strlcpy(oldx, newx, sizeof(oldx));
149 	strlcat(oldx, ".old", sizeof(oldx));
150 
151 	/* Only care for X if it's a symlink */
152 	if (target_symlink_exists_p(newx)) {
153 		if (target_symlink_exists_p(oldx)) {
154 			msg_fmt_display(MSG_X_oldexists,
155 			    "%s%s%s%s%s%s%s%s%s%s%s",
156 			    xroot, xroot, xroot, xroot, xroot, xroot, xroot,
157 			    xroot, xroot, xroot, xroot);
158 			hit_enter_to_continue(NULL, NULL);
159 			return EEXIST;
160 		}
161 
162 #ifdef DEBUG
163 		printf("saving %s as %s ...", newx, oldx);
164 #endif
165 
166 		/* Move target .../X to .../X.old.  Abort on error. */
167 		mv_within_target_or_die(newx, oldx);
168 	}
169 
170 	return 0;
171 }
172 
173 /*
174  * Merge back saved target X files after unpacking the new
175  * sets has completed.
176  */
177 static int
178 merge_X(const char *xroot)
179 {
180 	char newx[MAXPATHLEN], oldx[MAXPATHLEN];
181 
182 	strlcpy(newx, xroot, sizeof(newx));
183 	strlcat(newx, "/bin/X", sizeof(newx));
184 	strlcpy(oldx, newx, sizeof(oldx));
185 	strlcat(oldx, ".old", sizeof(oldx));
186 
187 	if (target_symlink_exists_p(oldx)) {
188 		/* Only move back X if it's a symlink - we don't want
189 		 * to restore old binaries */
190 		mv_within_target_or_die(oldx, newx);
191 	}
192 
193 	return 0;
194 }
195 
196 /*
197  * Unpacks sets,  clobbering existing contents.
198  */
199 void
200 do_reinstall_sets()
201 {
202 	struct install_partition_desc install = {};
203 	int retcode = 0;
204 	partman_go = 0;
205 
206 	unwind_mounts();
207 	msg_display(MSG_reinstallusure);
208 	if (!ask_noyes(NULL))
209 		return;
210 
211 	if (find_disks(msg_string(MSG_reinstall), !root_is_read_only()) < 0)
212 		return;
213 
214 	if (!pm->cur_system) {
215 		if (pm->parts == NULL) {
216 			hit_enter_to_continue(MSG_noroot, NULL);
217 			return;
218 		}
219 
220 		install_desc_from_parts(&install, pm->parts);
221 	} else {
222 		install.cur_system = true;
223 	}
224 
225 	if (mount_disks(&install) != 0)
226 		goto free_install;
227 
228 	/* Unpack the distribution. */
229 	process_menu(MENU_distset, &retcode);
230 	if (retcode == 0)
231 		goto free_install;
232 	if (get_and_unpack_sets(0, NULL, MSG_unpackcomplete, MSG_abortunpack) != 0)
233 		goto free_install;
234 
235 	sanity_check();
236 
237 free_install:
238 	free_install_desc(&install);
239 }
240