1#!/bin/sh
2# $NetBSD: upgrade.sh,v 1.8 2003/07/26 17:06:31 salo Exp $
3#
4# Copyright (c) 1994 Christopher G. Demetriou
5# All rights reserved.
6# 
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15# 3. All advertising materials mentioning features or use of this software
16#    must display the following acknowledgement:
17#          This product includes software developed for the
18#          NetBSD Project.  See http://www.NetBSD.org/ for
19#          information about NetBSD.
20# 4. The name of the author may not be used to endorse or promote products
21#    derived from this software without specific prior written permission.
22# 
23# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33# 
34# <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
35
36#	NetBSD upgrade script.
37#	In a perfect world, this would be a nice C program, with a reasonable
38#	user interface.
39
40#DONTDOIT=echo
41
42VERSION=1.2
43
44getresp() {
45	read resp
46	if [ "X$resp" = "X" ]; then
47		resp=$1
48	fi
49}
50
51getvar() {
52	echo $(eval $(echo "echo \$$1"))
53}
54
55shiftvar() {
56	local - var
57	var="$1"
58	list="$(getvar $var)"
59	set -- $list
60	shift
61	setvar $var "$*"
62}
63
64getparts() {
65	disklabel $1 2>/dev/null | sed -e '/^[ ][ ][ad-p]/!d' |
66	sed -e 's,^[ ]*\([a-p]\):[ ]*[0-9]*[ ]*[0-9]*[ ][ ]*\([a-zA-Z0-9.]*\).*,\1 \2,' |
67	sed -e ':a
68		N;${s/\n/ /g;p;d;}
69		ba'
70}
71
72getdrives() {
73	local du thispart
74	for du in /dev/rsd?a; do
75		dd if=$du of=/dev/null bs=1b count=1 >/dev/null 2>&1
76		if [ $? -eq 0 ]; then
77			thisunit=`echo $du | sed -e 's,/dev/r\(...\)a,\1,g'`
78			driveunits="$driveunits $thisunit"
79		else
80			continue;
81		fi
82		setvar $thisunit "$(getparts $thisunit)"
83		export $thisunit
84	done
85	export drivenunits
86}
87
88Convert_fstab() {
89	if [ ! -e /mnt/etc/fstab.ufs ]; then
90		mv /mnt/etc/fstab /mnt/etc/fstab.ufs
91	fi
92	sed "s/ufs/ffs/" /mnt/etc/fstab.ufs >/mnt/etc/fstab
93}
94
95echo	"Welcome to the NetBSD ${VERSION} upgrade program."
96echo	""
97echo	"This program is designed to help you put the new version of NetBSD"
98echo	"on your hard disk, in a simple and rational way.  To upgrade, you"
99echo	"must have plenty of free space on all partitions which will be"
100echo	"upgraded.  If you have at least 1MB free on your root partition,"
101echo	"and several free on your /usr patition, you should be fine."
102echo	""
103echo	"As with anything which modifies your hard drive's contents, this"
104echo	"program can cause SIGNIFICANT data loss, and you are advised"
105echo	"to make sure your hard drive is backed up before beginning the"
106echo	"upgrade process."
107echo	""
108echo	"Default answers are displayed in brackets after the questions."
109echo	"You can hit Control-C at any time to quit, but if you do so at a"
110echo	"prompt, you may have to hit return.  Also, quitting in the middle of"
111echo	"the upgrade may leave your system in an inconsistent (and unusable)"
112echo	"state."
113echo	""
114echo -n "Proceed with upgrade? [n] "
115getresp "n"
116case "$resp" in
117	y*|Y*)
118		echo	"Cool!  Let's get to it..."
119		;;
120	*)
121		echo	""
122		echo	"OK, then.  Enter 'halt' at the prompt to halt the"
123		echo	"machine.  Once the machine has halted, remove the"
124		echo	"floppy and press any key to reboot."
125		exit
126		;;
127esac
128
129# find out what units are possible, and query the user.
130
131getdrives
132
133if [ "X${driveunits}" = "X" ]; then
134	echo	"FATAL ERROR:"
135	echo	"No disk devices."
136	echo	"This is probably a bug in the install disks."
137	echo	"Exiting install program."
138	exit
139fi
140
141echo	""
142echo	"The following disks are supported by this upgrade procedure:"
143echo	"	"${driveunits}
144echo	""
145echo	"If your system was previously completely contained within the"
146echo	"disks listed above (i.e. if your system didn't occupy any space"
147echo	"on disks NOT listed above), this upgrade disk can upgrade your"
148echo	"system.  If it cannot, hit Control-C at the prompt."
149echo	""
150while [ "X${drivename}" = "X" ]; do
151	echo -n	"Which disk contains your root partition? "
152	getresp
153	otherdrives=`echo "${driveunits}" | sed -e s,${resp},,`
154	if [ "X${driveunits}" = "X${otherdrives}" ]; then
155		echo	""
156		echo	"\"${resp}\" is an invalid drive name.  Valid choices"
157		echo	"are: "${driveunits}
158		echo	""
159	else
160		drivename=${resp}
161	fi
162done
163
164echo	""
165echo	"Root partition is on ${drivename}a."
166
167echo	""
168echo	"If you've still installed 0.9 or earlier on your machine or you"
169echo	"haven't upgraded your pre-1.0 filesystems, then you might want to"
170echo	"upgrade the filesystem to the version introduced with 1.0."
171echo	""
172echo	"Would you like to upgrade your file systems to the new file system"
173echo -n	"format? [y] "
174getresp "y"
175case "$resp" in
176	n*|N*)
177		echo	""
178		echo	"You should upgrade your file systems with 'fsck -c 2'"
179		echo	"as soon as is feasible, because the new file system"
180		echo	"code is better-tested and more performant."
181		upgradefs=NO
182		;;
183	*)
184		upgradefs=YES
185		;;
186esac
187
188if [ $upgradefs = YES ]; then
189	echo	""
190	echo	"Upgrading the file system on ${drivename}a..."
191	
192	$DONTDOIT fsck -p -c 2 /dev/r${drivename}a
193	if [ $? != 0 ]; then
194		echo	"FATAL ERROR: FILE SYSTEM UPGRADE FAILED."
195		echo	"You should probably reboot the machine, fsck your"
196		echo	"disk(s), and try the upgrade procedure again."
197		exit 1
198	fi
199	echo	"Done."
200fi
201
202echo	""
203echo	"Mounting root partition on /mnt..."
204$DONTDOIT mount /dev/${drivename}a /mnt
205if [ $? != 0 ]; then
206	echo	"FATAL ERROR: MOUNT FAILED."
207	echo	"You should verify that your system is set up as you"
208	echo	"described, and re-attempt the upgrade procedure."
209	exit 1
210fi
211echo	"Done."
212
213#<<<<<<<<<<<<<<<<<<<<<<<< update etc/fstab to ffs? >>>>>>>>>>>>>>>>>>>>>>>>
214echo	""
215echo -n	"Converting ufs entries in fstab to ffs..."
216$DONTDOIT Convert_fstab
217echo	"Done."
218
219if [ $upgradefs = YES ]; then
220	echo	""
221	echo -n	"Copying new fsck binary to your hard disk..."
222	if [ ! -d /mnt/sbin ]; then
223		$DONTDOIT mkdir /mnt/sbin
224	fi
225	$DONTDOIT cp /sbin/fsck /mnt/sbin/fsck
226	if [ $? != 0 ]; then
227		echo	"FATAL ERROR: COPY FAILED."
228		echo	"It in unclear why this error would occur.  It looks"
229		echo	"like you may end up having to upgrade by hand."
230		exit 1
231	fi
232	$DONTDOIT sync
233	echo	" Done."
234
235	echo	""
236	echo    "Re-mounting root partition read-only..."
237	$DONTDOIT mount -u -o ro /dev/${drivename}a /mnt
238	if [ $? != 0 ]; then
239		echo	"FATAL ERROR: RE-MOUNT FAILED."
240		echo	"It in unclear why this error would occur.  It looks"
241		echo	"like you may end up having to upgrade by hand."
242		exit 1
243	fi
244	echo	"Done."
245
246	echo	""
247	echo	"Upgrading the rest of your file systems..."
248	$DONTDOIT chroot /mnt fsck -p -c 2
249	if [ $? != 0 ]; then
250		echo	"FATAL ERROR: FILE SYSTEM UPGRADE(S) FAILED."
251		echo	"You should probably reboot the machine, fsck your"
252		echo	"file system(s), and try the upgrade procedure"
253		echo	"again."
254		exit 1
255	fi
256	echo	"Done."
257
258	echo	""
259	echo    "Re-mounting root partition read-write..."
260	$DONTDOIT mount -u -o rw /dev/${drivename}a /mnt
261	if [ $? != 0 ]; then
262		echo	"FATAL ERROR: RE-MOUNT FAILED."
263		echo	"It in unclear why this error would occur.  It looks"
264		echo	"like you may end up having to upgrade by hand."
265		exit 1
266	fi
267	echo	"Done."
268fi
269
270echo	""
271echo	"Copying bootstrapping binaries and config files to the hard drive..."
272$DONTDOIT pax -rwpe sbin/mount_ffs /mnt
273
274echo	""
275echo	"Mounting remaining partitions..."
276$DONTDOIT chroot /mnt mount -at ffs > /dev/null 2>&1
277echo	"Done."
278
279echo    ""
280echo    ""
281echo	"OK!  The preliminary work of setting up your disk is now complete,"
282echo	"and you can now upgrade the actual NetBSD software."
283echo	""
284echo	"Right now, your hard disk is mounted on /mnt.  You should consult"
285echo	"the installation notes to determine how to load and install the new"
286echo	"NetBSD distribution sets, and how to clean up after the upgrade"
287echo	"software, when you are done."
288echo	""
289echo	"GOOD LUCK!"
290echo	""
291