xref: /freebsd/usr.sbin/bsdinstall/scripts/auto (revision b740c88bfb6453416926271c089262e7164dace3)
1#!/bin/sh
2#-
3# Copyright (c) 2011 Nathan Whitehorn
4# Copyright (c) 2013 Devin Teske
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#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28# $FreeBSD$
29#
30############################################################ INCLUDES
31
32BSDCFG_SHARE="/usr/share/bsdconfig"
33. $BSDCFG_SHARE/common.subr || exit 1
34
35############################################################ FUNCTIONS
36
37error() {
38	local msg
39	if [ -n "$1" ]; then
40		msg="$1\n\n"
41	fi
42	test -n "$DISTDIR_IS_UNIONFS" && umount -f $BSDINSTALL_DISTDIR
43	test -f $PATH_FSTAB && bsdinstall umount
44	dialog --backtitle "FreeBSD Installer" --title "Abort" \
45	    --no-label "Exit" --yes-label "Restart" --yesno \
46	    "${msg}An installation step has been aborted. Would you like to restart the installation or exit the installer?" 0 0
47	if [ $? -ne 0 ]; then
48		exit 1
49	else
50		exec $0
51	fi
52}
53
54############################################################ MAIN
55
56f_dprintf "Began Installation at %s" "$( date )"
57
58rm -rf $BSDINSTALL_TMPETC
59mkdir $BSDINSTALL_TMPETC
60
61trap true SIGINT	# This section is optional
62bsdinstall keymap
63
64trap error SIGINT	# Catch cntrl-C here
65bsdinstall hostname || error "Set hostname failed"
66
67export DISTRIBUTIONS="base.txz kernel.txz"
68if [ -f $BSDINSTALL_DISTDIR/MANIFEST ]; then
69	DISTMENU=`awk -F'\t' '!/^(kernel|base)/{print $4,$5,$6}' $BSDINSTALL_DISTDIR/MANIFEST`
70
71	exec 3>&1
72	EXTRA_DISTS=$( eval dialog \
73	    --backtitle \"FreeBSD Installer\" \
74	    --title \"Distribution Select\" --nocancel --separate-output \
75	    --checklist \"Choose optional system components to install:\" \
76	    0 0 0 $DISTMENU \
77	2>&1 1>&3 )
78	for dist in $EXTRA_DISTS; do
79		export DISTRIBUTIONS="$DISTRIBUTIONS $dist.txz"
80	done
81fi
82
83FETCH_DISTRIBUTIONS=""
84for dist in $DISTRIBUTIONS; do
85	if [ ! -f $BSDINSTALL_DISTDIR/$dist ]; then
86		FETCH_DISTRIBUTIONS="$FETCH_DISTRIBUTIONS $dist"
87	fi
88done
89FETCH_DISTRIBUTIONS=`echo $FETCH_DISTRIBUTIONS`	# Trim white space
90
91if [ -n "$FETCH_DISTRIBUTIONS" -a -n "$BSDINSTALL_CONFIGCURRENT" ]; then
92	dialog --backtitle "FreeBSD Installer" --title "Network Installation" --msgbox "No installation files were found on the boot volume. The next few screens will allow you to configure networking so that they can be downloaded from the Internet." 0 0
93	bsdinstall netconfig || error
94	NETCONFIG_DONE=yes
95fi
96
97if [ -n "$FETCH_DISTRIBUTIONS" ]; then
98	exec 3>&1
99	BSDINSTALL_DISTSITE=$(`dirname $0`/mirrorselect 2>&1 1>&3)
100	MIRROR_BUTTON=$?
101	exec 3>&-
102	test $MIRROR_BUTTON -eq 0 || error "No mirror selected"
103	export BSDINSTALL_DISTSITE
104fi
105
106rm -f $PATH_FSTAB
107touch $PATH_FSTAB
108
109PMODES="\
110\"Auto (UFS)\" \"Guided Disk Setup\" \
111Manual \"Manual Disk Setup (experts)\" \
112Shell \"Open a shell and partition by hand\""
113
114CURARCH=$( uname -m )
115case $CURARCH in
116	amd64|i386)	# Booting ZFS Supported
117		PMODES="$PMODES \"Auto (ZFS)\" \"Guided Root-on-ZFS\""
118		;;
119	*)		# Booting ZFS Unspported
120		;;
121esac
122
123exec 3>&1
124PARTMODE=`echo $PMODES | xargs dialog --backtitle "FreeBSD Installer" \
125	--title "Partitioning" \
126	--menu "How would you like to partition your disk?" \
127	0 0 0 2>&1 1>&3` || exit 1
128exec 3>&-
129
130case "$PARTMODE" in
131"Auto (UFS)")	# Guided
132	bsdinstall autopart || error "Partitioning error"
133	bsdinstall mount || error "Failed to mount filesystem"
134	;;
135"Shell")	# Shell
136	clear
137	echo "Use this shell to set up partitions for the new system. When finished, mount the system at $BSDINSTALL_CHROOT and place an fstab file for the new system at $PATH_FSTAB. Then type 'exit'. You can also enter the partition editor at any time by entering 'bsdinstall partedit'."
138	sh 2>&1
139	;;
140"Manual")	# Manual
141	if f_isset debugFile; then
142		# Give partedit the path to our logfile so it can append
143		BSDINSTALL_LOG="${debugFile#+}" bsdinstall partedit || error "Partitioning error"
144	else
145		bsdinstall partedit || error "Partitioning error"
146	fi
147	bsdinstall mount || error "Failed to mount filesystem"
148	;;
149"Auto (ZFS)")	# ZFS
150	bsdinstall zfsboot || error "ZFS setup failed"
151	bsdinstall mount || error "Failed to mount filesystem"
152	;;
153*)
154	error "Unknown partitioning mode"
155	;;
156esac
157
158if [ ! -z "$FETCH_DISTRIBUTIONS" ]; then
159	ALL_DISTRIBUTIONS="$DISTRIBUTIONS"
160
161	# Download to a directory in the new system as scratch space
162	BSDINSTALL_FETCHDEST="$BSDINSTALL_CHROOT/usr/freebsd-dist"
163	mkdir -p "$BSDINSTALL_FETCHDEST" || error "Could not create directory $BSDINSTALL_FETCHDEST"
164
165	export DISTRIBUTIONS="$FETCH_DISTRIBUTIONS"
166	# Try to use any existing distfiles
167	if [ -d $BSDINSTALL_DISTDIR ]; then
168		DISTDIR_IS_UNIONFS=1
169		mount_nullfs -o union "$BSDINSTALL_FETCHDEST" "$BSDINSTALL_DISTDIR"
170	else
171		export DISTRIBUTIONS="MANIFEST $ALL_DISTRIBUTIONS"
172		export BSDINSTALL_DISTDIR="$BSDINSTALL_FETCHDEST"
173	fi
174
175	export FTP_PASSIVE_MODE=YES
176	bsdinstall distfetch || error "Failed to fetch distribution"
177	export DISTRIBUTIONS="$ALL_DISTRIBUTIONS"
178fi
179
180bsdinstall checksum || error "Distribution checksum failed"
181bsdinstall distextract || error "Distribution extract failed"
182bsdinstall rootpass || error "Could not set root password"
183
184trap true SIGINT	# This section is optional
185if [ "$NETCONFIG_DONE" != yes ]; then
186	bsdinstall netconfig	# Don't check for errors -- the user may cancel
187fi
188bsdinstall time
189bsdinstall services
190
191dialog --backtitle "FreeBSD Installer" --title "Add User Accounts" --yesno \
192    "Would you like to add users to the installed system now?" 0 0 && \
193    bsdinstall adduser
194
195finalconfig() {
196	exec 3>&1
197	REVISIT=$(dialog --backtitle "FreeBSD Installer" \
198	    --title "Final Configuration" --no-cancel --menu \
199	    "Setup of your FreeBSD system is nearly complete. You can now modify your configuration choices. After this screen, you will have an opportunity to make more complex changes using a shell." 0 0 0 \
200		"Exit" "Apply configuration and exit installer" \
201		"Add User" "Add a user to the system" \
202		"Root Password" "Change root password" \
203		"Hostname" "Set system hostname" \
204		"Network" "Networking configuration" \
205		"Services" "Set daemons to run on startup" \
206		"Time Zone" "Set system timezone" \
207		"Handbook" "Install FreeBSD Handbook (requires network)" 2>&1 1>&3)
208	exec 3>&-
209
210	case "$REVISIT" in
211	"Add User")
212		bsdinstall adduser
213		finalconfig
214		;;
215	"Root Password")
216		bsdinstall rootpass
217		finalconfig
218		;;
219	"Hostname")
220		bsdinstall hostname
221		finalconfig
222		;;
223	"Network")
224		bsdinstall netconfig
225		finalconfig
226		;;
227	"Services")
228		bsdinstall services
229		finalconfig
230		;;
231	"Time Zone")
232		bsdinstall time
233		finalconfig
234		;;
235	"Handbook")
236		bsdinstall docsinstall
237		finalconfig
238		;;
239	esac
240}
241
242# Allow user to change his mind
243finalconfig
244
245trap error SIGINT	# SIGINT is bad again
246bsdinstall config  || error "Failed to save config"
247
248if [ ! -z "$BSDINSTALL_FETCHDEST" ]; then
249	[ "$BSDINSTALL_FETCHDEST" != "$BSDINSTALL_DISTDIR" ] && \
250	    umount "$BSDINSTALL_DISTDIR"
251	rm -rf "$BSDINSTALL_FETCHDEST"
252fi
253
254dialog --backtitle "FreeBSD Installer" --title "Manual Configuration" \
255    --default-button no --yesno \
256   "The installation is now finished. Before exiting the installer, would you like to open a shell in the new system to make any final manual modifications?" 0 0
257if [ $? -eq 0 ]; then
258	clear
259	mount -t devfs devfs "$BSDINSTALL_CHROOT/dev"
260	echo This shell is operating in a chroot in the new system. \
261	    When finished making configuration changes, type \"exit\".
262	chroot "$BSDINSTALL_CHROOT" /bin/sh 2>&1
263fi
264
265bsdinstall entropy
266bsdinstall umount
267
268f_dprintf "Installation Completed at %s" "$( date )"
269
270################################################################################
271# END
272################################################################################
273