1#!/bin/mksh
2# $MirOS: src/distrib/common/upgrade.sh,v 1.11 2010/07/13 12:49:26 tg Exp $
3# $OpenBSD: upgrade.sh,v 1.61 2005/04/02 14:27:08 krw Exp $
4# $NetBSD: upgrade.sh,v 1.2.4.5 1996/08/27 18:15:08 gwr Exp $
5#
6# Copyright © 2010
7#	Thorsten Glaser <tg@mirbsd.org>
8#
9# Copyright (c) 1997-2004 Todd Miller, Theo de Raadt, Ken Westerback
10# All rights reserved.
11#
12# Copyright (c) 1996 The NetBSD Foundation, Inc.
13# All rights reserved.
14#
15# This code is derived from software contributed to The NetBSD Foundation
16# by Jason R. Thorpe.
17#
18# Redistribution and use in source and binary forms, with or without
19# modification, are permitted provided that the following conditions
20# are met:
21# 1. Redistributions of source code must retain the above copyright
22#    notice, this list of conditions and the following disclaimer.
23# 2. Redistributions in binary form must reproduce the above copyright
24#    notice, this list of conditions and the following disclaimer in the
25#    documentation and/or other materials provided with the distribution.
26# 3. All advertising materials mentioning features or use of this software
27#    must display the following acknowledgement:
28#        This product includes software developed by the NetBSD
29#        Foundation, Inc. and its contributors.
30# 4. Neither the name of The NetBSD Foundation nor the names of its
31#    contributors may be used to endorse or promote products derived
32#    from this software without specific prior written permission.
33#
34# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
35# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
36# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
37# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
38# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44# POSSIBILITY OF SUCH DAMAGE.
45#
46
47#	OpenBSD installation script.
48#	In a perfect world, this would be a nice C program, with a reasonable
49#	user interface.
50
51# install.sub needs to know the MODE
52MODE=upgrade
53
54# include common subroutines and initialization code
55. install.sub
56
57# Have the user confirm that $ROOTDEV is the root filesystem.
58while :; do
59	ask "Root filesystem?" "$ROOTDEV"
60	resp=${resp##*/}
61	[[ -b /dev/$resp ]] && break
62
63	echo "Sorry, $resp is not a block device."
64done
65ROOTDEV=$resp
66
67echo -n "Checking root filesystem (fsck -fp /dev/${ROOTDEV}) ... "
68if ! fsck -fp /dev/$ROOTDEV >/dev/null 2>&1; then
69	echo	"FAILED.\nYou must fsck ${ROOTDEV} manually."
70	exit
71fi
72echo	"OK."
73
74echo -n "Mounting root filesystem..."
75if ! mount -o ro /dev/$ROOTDEV /mnt; then
76	echo	"ERROR: can't mount root filesystem!"
77	exit
78fi
79echo	"done."
80
81# The fstab, hosts and myname files are required.
82for _file in fstab hosts myname; do
83	if [ ! -f /mnt/etc/$_file ]; then
84		echo "ERROR: no /mnt/etc/${_file}!"
85		exit
86	fi
87	cp /mnt/etc/$_file /tmp/$_file
88done
89hostname $(</tmp/myname)
90
91if [[ -f /etc/ssh/ssh_host_rsa_key ]]; then
92	echo "Since sshd(8) is running, I assume you already have" \
93	    "set up the network."
94	echo 'I will *not* offer to use the configuration from the root fs!'
95else
96	ask_yn "Enable network using configuration stored on root filesystem?" yes
97	[[ $resp = y ]] && enable_network
98fi
99
100# Offer the user the opportunity to tweak, repair, or create the network
101# configuration by hand.
102manual_net_cfg
103
104cat <<__EOT
105
106The fstab is configured as follows:
107
108$(</tmp/fstab)
109
110For the $MODE, filesystems in the fstab will be automatically mounted if the
111'noauto' option is absent, and /sbin/mount_<fstype> is found, and the fstype is
112not nfs or mfs. Non-ffs filesystems will be mounted read-only.
113
114You can edit the fstab now, before it is used, but the edited fstab will only
115be used during the upgrade. It will not be copied back to disk.
116__EOT
117edit_tmp_file fstab
118
119# Create /etc/fstab.
120munge_fstab
121
122# fsck -p non-root filesystems in /etc/fstab.
123check_fs
124
125# Mount filesystems in /etc/fstab.
126if ! umount /mnt; then
127	echo	"ERROR: can't unmount previously mounted root!"
128	exit
129fi
130mount_fs
131
132# Upgrade helper for pkgutl*.ngz (pre-install part)
133rm -f /mnt/usr/mpkg/_{upgrade,flst}
134if [[ -d /mnt/usr/mpkg/db/. ]]; then (
135	cd /mnt/usr/mpkg
136	# fold in /usr/mpkg/db/pkg/{mirmake,pkgtools}-* from the database
137	# as well as the contents of the mirmake package (pkgtools is OK)
138	for i in db/pkg/mirmake-*; do
139		[[ -d $i/. ]] || continue
140		print -r -- "$i" >>_flst
141		grep -v '^[+@]' $i/+CONTENTS | grep -v '^share/mmake' >>_flst
142		print share/mmake >>_flst
143	done
144	for i in db/pkg/pkgtools-*; do
145		[[ -d $i/. ]] && print -r -- "$i" >>_flst
146	done
147	# XXX no sort here… # sort -uo _flst _flst
148	cpio -oC512 -Hnewc -Mset <_flst >_upgrade
149	rm -rf $(<_flst)
150	rm -f _flst
151); fi
152
153# Install sets.
154install_sets
155
156# Upgrade helper for pkgutl*.ngz (post-install part)
157if [[ -s /mnt/usr/mpkg/_upgrade ]]; then (
158	cd /mnt/usr/mpkg
159	found=0
160	for i in db/pkg/pkgtools-*; do
161		[[ -d $i/. ]] || continue
162		found=1
163		break
164	done
165	if (( !found )); then
166		# restore mirmake and pkgtools from before
167		tar xphf _upgrade
168	fi
169	rm -f _upgrade
170); fi
171
172# Little upgrade helpers
173[[ -e /mnt/etc/boot.conf && ! -e /mnt/boot.cfg ]] && \
174    cp /mnt/etc/boot.conf /mnt/boot.cfg
175[[ -e /mnt/etc/boot.cfg && ! -e /mnt/boot.cfg ]] && \
176    cp /mnt/etc/boot.cfg /mnt/boot.cfg
177[[ -e /mnt/etc/spamd.conf && ! -e /mnt/etc/mail/spamd.conf ]] && \
178    cp /mnt/etc/spamd.conf /mnt/etc/mail/spamd.conf
179[[ -d /mnt/usr/dbin/. || -d /mnt/usr/dsbin/. ]] && \
180    for f in /mnt/usr/dbin/* /mnt/usr/dsbin/*; do
181	[[ -f $f ]] || continue
182	for d in bin sbin usr/bin usr/sbin; do
183		if [[ -e /mnt/$d/${f##*/} ]]; then
184			rm -f $f
185			ln -sf ../../$d/${f##*/} $f
186			break
187		fi
188	done
189done
190if [[ -d /mnt/usr/include/gxx/. ]]; then
191	found=
192	for i in /mnt/usr/lib/gcc/*/3.4.6/include/c++/.; do
193		[[ -d $i ]] && found=$i
194	done
195	[[ -n $found ]] && rm -rf /mnt/usr/include/gxx
196fi
197if [[ -d /mnt/usr/include/objc/. ]]; then
198	found=
199	for i in /mnt/usr/lib/gcc/*/3.4.6/include/objc/.; do
200		[[ -d $i ]] && found=$i
201	done
202	[[ -n $found ]] && rm -rf /mnt/usr/include/objc
203fi
204[[ -s /mnt/etc/rc ]] && /mnt/bin/ed -s /mnt/etc/rc <<-'EOF'
205	,g/set -o arc4random/s/^/#disabled by $MirOS: src/distrib/common/upgrade.sh,v 1.11 2010/07/13 12:49:26 tg Exp $#/
206	wq
207EOF
208
209# Perform final steps common to both an install and an upgrade.
210finish_up
211
212( ( dd if=/dev/prandom bs=64 count=1; \
213    dd if=/dev/arandom bs=64 count=8; \
214    dd if=/dev/urandom bs=64 count=55; \
215  ) 2>/dev/wrandom | dd of=/mnt/var/db/host.random; \
216    chown 0:0 /mnt/var/db/host.random; \
217    chmod 600 /mnt/var/db/host.random) \
218    >/dev/wrandom 2>&1
219