auto revision 286286
1122405Sharti#!/bin/sh
2122405Sharti#-
3122405Sharti# Copyright (c) 2011 Nathan Whitehorn
4122405Sharti# Copyright (c) 2013 Devin Teske
5122405Sharti# All rights reserved.
6122405Sharti#
7122405Sharti# Redistribution and use in source and binary forms, with or without
8122405Sharti# modification, are permitted provided that the following conditions
9122405Sharti# are met:
10122405Sharti# 1. Redistributions of source code must retain the above copyright
11122405Sharti#    notice, this list of conditions and the following disclaimer.
12122405Sharti# 2. Redistributions in binary form must reproduce the above copyright
13122405Sharti#    notice, this list of conditions and the following disclaimer in the
14122405Sharti#    documentation and/or other materials provided with the distribution.
15122405Sharti#
16122405Sharti# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17122405Sharti# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18122405Sharti# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19122405Sharti# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20122405Sharti# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21122405Sharti# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22122405Sharti# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23122405Sharti# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24122405Sharti# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25122405Sharti# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26122405Sharti# SUCH DAMAGE.
27122405Sharti#
28122405Sharti# $FreeBSD: stable/10/usr.sbin/bsdinstall/scripts/auto 286286 2015-08-04 13:41:37Z gjb $
29122405Sharti#
30122405Sharti############################################################ INCLUDES
31122405Sharti
32122405ShartiBSDCFG_SHARE="/usr/share/bsdconfig"
33122405Sharti. $BSDCFG_SHARE/common.subr || exit 1
34122405Shartif_include $BSDCFG_SHARE/dialog.subr
35122405Sharti
36122405Sharti############################################################ FUNCTIONS
37122405Sharti
38122405Shartierror() {
39122405Sharti	local msg
40122405Sharti	if [ -n "$1" ]; then
41122405Sharti		msg="$1\n\n"
42122405Sharti	fi
43122405Sharti	test -n "$DISTDIR_IS_UNIONFS" && umount -f $BSDINSTALL_DISTDIR
44122405Sharti	test -f $PATH_FSTAB && bsdinstall umount
45122405Sharti	dialog --backtitle "FreeBSD Installer" --title "Abort" \
46122405Sharti	    --no-label "Exit" --yes-label "Restart" --yesno \
47152268Sharti	    "${msg}An installation step has been aborted. Would you like to restart the installation or exit the installer?" 0 0
48122405Sharti	if [ $? -ne 0 ]; then
49122405Sharti		exit 1
50122405Sharti	else
51122405Sharti		exec $0
52122405Sharti	fi
53122405Sharti}
54122405Sharti
55122405Shartihline_arrows_tab_enter="Press arrows, TAB or ENTER"
56122405Shartimsg_gpt_active_fix="Your hardware is known to have issues booting in BIOS mode from GPT partitions that are not set active. Would you like the installer to apply this workaround for you?"
57122405Shartimsg_lenovo_fix="Your model of Lenovo is known to have a BIOS bug that prevents it booting from GPT partitions without UEFI. Would you like the installer to apply a workaround for you?"
58122405Shartimsg_no="NO"
59122405Shartimsg_yes="YES"
60122405Sharti
61122405Sharti# dialog_workaround
62122405Sharti#
63122405Sharti# Ask the user if they wish to apply a workaround
64122405Sharti#
65122405Shartidialog_workaround()
66122405Sharti{
67122405Sharti	local passed_msg="$1"
68122405Sharti	local title="$DIALOG_TITLE"
69122405Sharti	local btitle="$DIALOG_BACKTITLE"
70122405Sharti	local prompt # Calculated below
71122405Sharti	local hline="$hline_arrows_tab_enter"
72122405Sharti
73122405Sharti	local height=8 width=50 prefix="   "
74122405Sharti	local plen=${#prefix} list= line=
75122405Sharti	local max_width=$(( $width - 3 - $plen ))
76122405Sharti
77122405Sharti	local yes no defaultno extra_args format
78122405Sharti	if [ "$USE_XDIALOG" ]; then
79122405Sharti		yes=ok no=cancel defaultno=default-no
80122405Sharti		extra_args="--wrap --left"
81122405Sharti		format="$passed_msg"
82122758Sharti	else
83122405Sharti		yes=yes no=no defaultno=defaultno
84122405Sharti		extra_args="--cr-wrap"
85122405Sharti		format="$passed_msg"
86122405Sharti	fi
87122405Sharti
88122405Sharti	# Add height for Xdialog(1)
89122405Sharti	[ "$USE_XDIALOG" ] && height=$(( $height + $height / 5 + 3 ))
90122405Sharti
91122405Sharti	prompt=$( printf "$format" )
92122405Sharti	f_dprintf "%s: Workaround prompt" "$0"
93122405Sharti	$DIALOG \
94122405Sharti		--title "$title"        \
95122405Sharti		--backtitle "$btitle"   \
96122405Sharti		--hline "$hline"        \
97122405Sharti		--$yes-label "$msg_yes" \
98122405Sharti		--$no-label "$msg_no"   \
99122405Sharti		$extra_args             \
100122405Sharti		--yesno "$prompt" $height $width
101122405Sharti}
102122405Sharti
103122405Sharti############################################################ MAIN
104122405Sharti
105122405Shartif_dprintf "Began Installation at %s" "$( date )"
106122758Sharti
107122405Shartirm -rf $BSDINSTALL_TMPETC
108122405Shartimkdir $BSDINSTALL_TMPETC
109122405Sharti
110122405Shartitrap true SIGINT	# This section is optional
111122405Shartibsdinstall keymap
112122405Sharti
113122405Shartitrap error SIGINT	# Catch cntrl-C here
114122405Shartibsdinstall hostname || error "Set hostname failed"
115122405Sharti
116122405Shartiexport DISTRIBUTIONS="base.txz kernel.txz"
117122405Shartiif [ -f $BSDINSTALL_DISTDIR/MANIFEST ]; then
118122405Sharti	DISTMENU=`awk -F'\t' '!/^(kernel|base)/{print $4,$5,$6}' $BSDINSTALL_DISTDIR/MANIFEST`
119122405Sharti
120122405Sharti	exec 3>&1
121122405Sharti	EXTRA_DISTS=$( eval dialog \
122122405Sharti	    --backtitle \"FreeBSD Installer\" \
123122405Sharti	    --title \"Distribution Select\" --nocancel --separate-output \
124122405Sharti	    --checklist \"Choose optional system components to install:\" \
125122405Sharti	    0 0 0 $DISTMENU \
126122405Sharti	2>&1 1>&3 )
127122758Sharti	for dist in $EXTRA_DISTS; do
128122405Sharti		export DISTRIBUTIONS="$DISTRIBUTIONS $dist.txz"
129122405Sharti	done
130122405Shartifi
131122405Sharti
132122405ShartiFETCH_DISTRIBUTIONS=""
133122405Shartifor dist in $DISTRIBUTIONS; do
134146529Sharti	if [ ! -f $BSDINSTALL_DISTDIR/$dist ]; then
135122405Sharti		FETCH_DISTRIBUTIONS="$FETCH_DISTRIBUTIONS $dist"
136122405Sharti	fi
137122405Shartidone
138122405ShartiFETCH_DISTRIBUTIONS=`echo $FETCH_DISTRIBUTIONS`	# Trim white space
139122405Sharti
140122405Shartiif [ -n "$FETCH_DISTRIBUTIONS" -a -n "$BSDINSTALL_CONFIGCURRENT" ]; then
141122405Sharti	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
142122405Sharti	bsdinstall netconfig || error
143122405Sharti	NETCONFIG_DONE=yes
144122405Shartifi
145122405Sharti
146122405Shartiif [ -n "$FETCH_DISTRIBUTIONS" ]; then
147122405Sharti	exec 3>&1
148122405Sharti	BSDINSTALL_DISTSITE=$(`dirname $0`/mirrorselect 2>&1 1>&3)
149122405Sharti	MIRROR_BUTTON=$?
150122405Sharti	exec 3>&-
151122405Sharti	test $MIRROR_BUTTON -eq 0 || error "No mirror selected"
152122405Sharti	export BSDINSTALL_DISTSITE
153122405Shartifi
154122405Sharti
155122405Shartirm -f $PATH_FSTAB
156122405Shartitouch $PATH_FSTAB
157122405Sharti
158122405Sharti#
159122405Sharti# Try to detect known broken platforms and apply their workarounds
160122405Sharti#
161122405Sharti
162122405Shartiif f_interactive; then
163122405Sharti	sys_maker=$( kenv -q smbios.system.maker )
164122405Sharti	f_dprintf "smbios.system.maker=[%s]" "$sys_maker"
165122405Sharti	sys_model=$( kenv -q smbios.system.product )
166122405Sharti	f_dprintf "smbios.system.product=[%s]" "$sys_model"
167122405Sharti	sys_version=$( kenv -q smbios.system.version )
168122405Sharti	f_dprintf "smbios.system.version=[%s]" "$sys_version"
169122405Sharti	case "$sys_maker" in
170122405Sharti	"LENOVO")
171122405Sharti		case "$sys_version" in
172122405Sharti		"ThinkPad X220"|"ThinkPad T420"|"ThinkPad T520")
173122405Sharti			dialog_workaround "$msg_lenovo_fix"
174122405Sharti			retval=$?
175122405Sharti			f_dprintf "lenovofix_prompt=[%s]" "$retval"
176310903Sngie			if [ $retval -eq $DIALOG_OK ]; then
177122405Sharti				export ZFSBOOT_PARTITION_SCHEME="GPT + Lenovo Fix"
178122405Sharti				export WORKAROUND_LENOVO=1
179122405Sharti			fi
180122405Sharti			;;
181122405Sharti		esac
182122405Sharti		;;
183122405Sharti	"Dell Inc.")
184122405Sharti		case "$sys_model" in
185122405Sharti		"Latitude E7440")
186122405Sharti			dialog_workaround "$msg_gpt_active_fix"
187122405Sharti			retval=$?
188122405Sharti			f_dprintf "gpt_active_fix_prompt=[%s]" "$retval"
189122405Sharti			if [ $retval -eq $DIALOG_OK ]; then
190122405Sharti				export ZFSBOOT_PARTITION_SCHEME="GPT + Active"
191122405Sharti				export WORKAROUND_GPTACTIVE=1
192122405Sharti			fi
193122405Sharti			;;
194122405Sharti		esac
195122405Sharti		;;
196122405Sharti	esac
197122405Shartifi
198122405Sharti
199122405ShartiPMODES="\
200122405Sharti\"Auto (UFS)\" \"Guided Disk Setup\" \
201122405ShartiManual \"Manual Disk Setup (experts)\" \
202122405ShartiShell \"Open a shell and partition by hand\""
203122405Sharti
204122405ShartiCURARCH=$( uname -m )
205122405Sharticase $CURARCH in
206122405Sharti	amd64|i386)	# Booting ZFS Supported
207122405Sharti		PMODES="$PMODES \"Auto (ZFS)\" \"Guided Root-on-ZFS\""
208122405Sharti		;;
209122405Sharti	*)		# Booting ZFS Unspported
210122405Sharti		;;
211122405Shartiesac
212122405Sharti
213122405Shartiexec 3>&1
214122405ShartiPARTMODE=`echo $PMODES | xargs dialog --backtitle "FreeBSD Installer" \
215122405Sharti	--title "Partitioning" \
216122405Sharti	--menu "How would you like to partition your disk?" \
217122405Sharti	0 0 0 2>&1 1>&3` || exit 1
218122405Shartiexec 3>&-
219122405Sharti
220122405Sharticase "$PARTMODE" in
221122405Sharti"Auto (UFS)")	# Guided
222122405Sharti	bsdinstall autopart || error "Partitioning error"
223122405Sharti	bsdinstall mount || error "Failed to mount filesystem"
224122405Sharti	;;
225122405Sharti"Shell")	# Shell
226122405Sharti	clear
227122405Sharti	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'."
228122405Sharti	sh 2>&1
229122405Sharti	;;
230122405Sharti"Manual")	# Manual
231122405Sharti	if f_isset debugFile; then
232122405Sharti		# Give partedit the path to our logfile so it can append
233122405Sharti		BSDINSTALL_LOG="${debugFile#+}" bsdinstall partedit || error "Partitioning error"
234122405Sharti	else
235122405Sharti		bsdinstall partedit || error "Partitioning error"
236122405Sharti	fi
237122405Sharti	bsdinstall mount || error "Failed to mount filesystem"
238122405Sharti	;;
239122405Sharti"Auto (ZFS)")	# ZFS
240122405Sharti	bsdinstall zfsboot || error "ZFS setup failed"
241122405Sharti	bsdinstall mount || error "Failed to mount filesystem"
242122405Sharti	;;
243122405Sharti*)
244122405Sharti	error "Unknown partitioning mode"
245122405Sharti	;;
246122405Shartiesac
247122405Sharti
248122405Shartiif [ ! -z "$FETCH_DISTRIBUTIONS" ]; then
249122405Sharti	ALL_DISTRIBUTIONS="$DISTRIBUTIONS"
250122405Sharti
251122405Sharti	# Download to a directory in the new system as scratch space
252122405Sharti	BSDINSTALL_FETCHDEST="$BSDINSTALL_CHROOT/usr/freebsd-dist"
253122405Sharti	mkdir -p "$BSDINSTALL_FETCHDEST" || error "Could not create directory $BSDINSTALL_FETCHDEST"
254122405Sharti
255122405Sharti	export DISTRIBUTIONS="$FETCH_DISTRIBUTIONS"
256122405Sharti	# Try to use any existing distfiles
257122405Sharti	if [ -d $BSDINSTALL_DISTDIR ]; then
258122405Sharti		DISTDIR_IS_UNIONFS=1
259122405Sharti		mount_nullfs -o union "$BSDINSTALL_FETCHDEST" "$BSDINSTALL_DISTDIR"
260122405Sharti	else
261122405Sharti		export DISTRIBUTIONS="$ALL_DISTRIBUTIONS"
262122405Sharti		export BSDINSTALL_DISTDIR="$BSDINSTALL_FETCHDEST"
263122405Sharti	fi
264122405Sharti		
265122405Sharti	export FTP_PASSIVE_MODE=YES
266122405Sharti	bsdinstall distfetch || error "Failed to fetch distribution"
267122405Sharti	export DISTRIBUTIONS="$ALL_DISTRIBUTIONS"
268122405Shartifi
269122405Sharti
270122405Shartibsdinstall checksum || error "Distribution checksum failed"
271122405Shartibsdinstall distextract || error "Distribution extract failed"
272122405Shartibsdinstall rootpass || error "Could not set root password"
273122405Sharti
274122405Shartitrap true SIGINT	# This section is optional
275122405Shartiif [ "$NETCONFIG_DONE" != yes ]; then
276122405Sharti	bsdinstall netconfig	# Don't check for errors -- the user may cancel
277122405Shartifi
278122405Shartibsdinstall time
279122405Shartibsdinstall services
280122405Sharti
281122405Shartidialog --backtitle "FreeBSD Installer" --title "Add User Accounts" --yesno \
282122405Sharti    "Would you like to add users to the installed system now?" 0 0 && \
283122405Sharti    bsdinstall adduser
284122405Sharti
285122405Shartifinalconfig() {
286122405Sharti	exec 3>&1
287122405Sharti	REVISIT=$(dialog --backtitle "FreeBSD Installer" \
288122405Sharti	    --title "Final Configuration" --no-cancel --menu \
289122405Sharti	    "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 \
290122405Sharti		"Exit" "Apply configuration and exit installer" \
291122405Sharti		"Add User" "Add a user to the system" \
292122405Sharti		"Root Password" "Change root password" \
293122405Sharti		"Hostname" "Set system hostname" \
294122405Sharti		"Network" "Networking configuration" \
295122405Sharti		"Services" "Set daemons to run on startup" \
296122405Sharti		"Time Zone" "Set system timezone" \
297122405Sharti		"Handbook" "Install FreeBSD Handbook (requires network)" 2>&1 1>&3)
298122405Sharti	exec 3>&-
299122405Sharti
300122405Sharti	case "$REVISIT" in
301122405Sharti	"Add User")
302122405Sharti		bsdinstall adduser
303122405Sharti		finalconfig
304122405Sharti		;;
305122405Sharti	"Root Password")
306122405Sharti		bsdinstall rootpass 
307122405Sharti		finalconfig
308122405Sharti		;;
309122405Sharti	"Hostname")
310122758Sharti		bsdinstall hostname
311122405Sharti		finalconfig
312122405Sharti		;;
313122405Sharti	"Network")
314122405Sharti		bsdinstall netconfig
315122405Sharti		finalconfig
316122405Sharti		;;
317122405Sharti	"Services")
318122405Sharti		bsdinstall services
319122405Sharti		finalconfig
320122405Sharti		;;
321122405Sharti	"Time Zone")
322122405Sharti		bsdinstall time
323122405Sharti		finalconfig
324122405Sharti		;;
325122405Sharti	"Handbook")
326122405Sharti		bsdinstall docsinstall
327122405Sharti		finalconfig
328122405Sharti		;;
329122405Sharti	esac
330122405Sharti}
331122758Sharti
332122405Sharti# Allow user to change his mind
333122405Shartifinalconfig
334122405Sharti
335122405Shartitrap error SIGINT	# SIGINT is bad again
336122405Shartibsdinstall config  || error "Failed to save config"
337122405Sharti
338122405Shartiif [ ! -z "$BSDINSTALL_FETCHDEST" ]; then
339122405Sharti	[ "$BSDINSTALL_FETCHDEST" != "$BSDINSTALL_DISTDIR" ] && \
340122758Sharti	    umount "$BSDINSTALL_DISTDIR"
341122405Sharti	rm -rf "$BSDINSTALL_FETCHDEST"
342122405Shartifi
343122405Sharti
344122405Shartidialog --backtitle "FreeBSD Installer" --title "Manual Configuration" \
345122405Sharti    --default-button no --yesno \
346122405Sharti   "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
347122405Shartiif [ $? -eq 0 ]; then
348122405Sharti	clear
349122405Sharti	mount -t devfs devfs "$BSDINSTALL_CHROOT/dev"
350122405Sharti	echo This shell is operating in a chroot in the new system. \
351122405Sharti	    When finished making configuration changes, type \"exit\".
352122405Sharti	chroot "$BSDINSTALL_CHROOT" /bin/sh 2>&1
353122405Shartifi
354122405Sharti
355122405Shartibsdinstall entropy
356122405Shartibsdinstall umount
357122405Sharti
358122758Shartif_dprintf "Installation Completed at %s" "$( date )"
359122405Sharti
360122405Sharti################################################################################
361122405Sharti# END
362122405Sharti################################################################################
363122405Sharti