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