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