auto revision 285732
1218799Snwhitehorn#!/bin/sh
2218799Snwhitehorn#-
3218799Snwhitehorn# Copyright (c) 2011 Nathan Whitehorn
4257842Sdteske# Copyright (c) 2013 Devin Teske
5218799Snwhitehorn# All rights reserved.
6218799Snwhitehorn#
7218799Snwhitehorn# Redistribution and use in source and binary forms, with or without
8218799Snwhitehorn# modification, are permitted provided that the following conditions
9218799Snwhitehorn# are met:
10218799Snwhitehorn# 1. Redistributions of source code must retain the above copyright
11218799Snwhitehorn#    notice, this list of conditions and the following disclaimer.
12218799Snwhitehorn# 2. Redistributions in binary form must reproduce the above copyright
13218799Snwhitehorn#    notice, this list of conditions and the following disclaimer in the
14218799Snwhitehorn#    documentation and/or other materials provided with the distribution.
15218799Snwhitehorn#
16218799Snwhitehorn# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17218799Snwhitehorn# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18218799Snwhitehorn# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19218799Snwhitehorn# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20218799Snwhitehorn# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21218799Snwhitehorn# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22218799Snwhitehorn# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23218799Snwhitehorn# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24218799Snwhitehorn# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25218799Snwhitehorn# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26218799Snwhitehorn# SUCH DAMAGE.
27218799Snwhitehorn#
28218799Snwhitehorn# $FreeBSD: head/usr.sbin/bsdinstall/scripts/auto 285732 2015-07-21 00:33:15Z allanjude $
29257842Sdteske#
30257842Sdteske############################################################ INCLUDES
31218799Snwhitehorn
32257842SdteskeBSDCFG_SHARE="/usr/share/bsdconfig"
33257842Sdteske. $BSDCFG_SHARE/common.subr || exit 1
34285679Sallanjudef_include $BSDCFG_SHARE/dialog.subr
35218799Snwhitehorn
36257842Sdteske############################################################ FUNCTIONS
37257842Sdteske
38218799Snwhitehornerror() {
39269653Sthompsa	local msg
40269653Sthompsa	if [ -n "$1" ]; then
41269653Sthompsa		msg="$1\n\n"
42269653Sthompsa	fi
43220088Snwhitehorn	test -n "$DISTDIR_IS_UNIONFS" && umount -f $BSDINSTALL_DISTDIR
44220088Snwhitehorn	test -f $PATH_FSTAB && bsdinstall umount
45218799Snwhitehorn	dialog --backtitle "FreeBSD Installer" --title "Abort" \
46218799Snwhitehorn	    --no-label "Exit" --yes-label "Restart" --yesno \
47269653Sthompsa	    "${msg}An installation step has been aborted. Would you like to restart the installation or exit the installer?" 0 0
48218799Snwhitehorn	if [ $? -ne 0 ]; then
49225637Snwhitehorn		exit 1
50218799Snwhitehorn	else
51218799Snwhitehorn		exec $0
52218799Snwhitehorn	fi
53218799Snwhitehorn}
54218799Snwhitehorn
55285679Sallanjudehline_arrows_tab_enter="Press arrows, TAB or ENTER"
56285732Sallanjudemsg_gpt_active_fix="Your hardware is known to have issues booting in CSM/Legacy/BIOS mode from GPT partitions that are not set active. Would you like the installer to apply this workaround for you?"
57285679Sallanjudemsg_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?"
58285679Sallanjudemsg_no="NO"
59285679Sallanjudemsg_yes="YES"
60285679Sallanjude
61285679Sallanjude# dialog_workaround
62285679Sallanjude#
63285679Sallanjude# Ask the user if they wish to apply a workaround
64285679Sallanjude#
65285679Sallanjudedialog_workaround()
66285679Sallanjude{
67285679Sallanjude	local passed_msg="$1"
68285679Sallanjude	local title="$DIALOG_TITLE"
69285679Sallanjude	local btitle="$DIALOG_BACKTITLE"
70285679Sallanjude	local prompt # Calculated below
71285679Sallanjude	local hline="$hline_arrows_tab_enter"
72285679Sallanjude
73285679Sallanjude	local height=8 width=50 prefix="   "
74285679Sallanjude	local plen=${#prefix} list= line=
75285679Sallanjude	local max_width=$(( $width - 3 - $plen ))
76285679Sallanjude
77285679Sallanjude	local yes no defaultno extra_args format
78285679Sallanjude	if [ "$USE_XDIALOG" ]; then
79285679Sallanjude		yes=ok no=cancel defaultno=default-no
80285679Sallanjude		extra_args="--wrap --left"
81285679Sallanjude		format="$passed_msg"
82285679Sallanjude	else
83285679Sallanjude		yes=yes no=no defaultno=defaultno
84285679Sallanjude		extra_args="--cr-wrap"
85285679Sallanjude		format="$passed_msg"
86285679Sallanjude	fi
87285679Sallanjude
88285679Sallanjude	# Add height for Xdialog(1)
89285679Sallanjude	[ "$USE_XDIALOG" ] && height=$(( $height + $height / 5 + 3 ))
90285679Sallanjude
91285679Sallanjude	prompt=$( printf "$format" )
92285679Sallanjude	f_dprintf "%s: Workaround prompt" "$0"
93285679Sallanjude	$DIALOG \
94285679Sallanjude		--title "$title"        \
95285679Sallanjude		--backtitle "$btitle"   \
96285679Sallanjude		--hline "$hline"        \
97285679Sallanjude		--$yes-label "$msg_yes" \
98285679Sallanjude		--$no-label "$msg_no"   \
99285679Sallanjude		$extra_args             \
100285679Sallanjude		--yesno "$prompt" $height $width
101285679Sallanjude}
102285679Sallanjude
103257842Sdteske############################################################ MAIN
104218799Snwhitehorn
105257842Sdteskef_dprintf "Began Installation at %s" "$( date )"
106257842Sdteske
107218799Snwhitehornrm -rf $BSDINSTALL_TMPETC
108218799Snwhitehornmkdir $BSDINSTALL_TMPETC
109218799Snwhitehorn
110218799Snwhitehorntrap true SIGINT	# This section is optional
111218799Snwhitehornbsdinstall keymap
112218799Snwhitehorn
113218799Snwhitehorntrap error SIGINT	# Catch cntrl-C here
114269653Sthompsabsdinstall hostname || error "Set hostname failed"
115218799Snwhitehorn
116219615Snwhitehornexport DISTRIBUTIONS="base.txz kernel.txz"
117219615Snwhitehornif [ -f $BSDINSTALL_DISTDIR/MANIFEST ]; then
118242188Sdteske	DISTMENU=`awk -F'\t' '!/^(kernel|base)/{print $4,$5,$6}' $BSDINSTALL_DISTDIR/MANIFEST`
119218947Snwhitehorn
120219615Snwhitehorn	exec 3>&1
121241902Sdteske	EXTRA_DISTS=$( eval dialog \
122241902Sdteske	    --backtitle \"FreeBSD Installer\" \
123241902Sdteske	    --title \"Distribution Select\" --nocancel --separate-output \
124241902Sdteske	    --checklist \"Choose optional system components to install:\" \
125241902Sdteske	    0 0 0 $DISTMENU \
126241902Sdteske	2>&1 1>&3 )
127219615Snwhitehorn	for dist in $EXTRA_DISTS; do
128219615Snwhitehorn		export DISTRIBUTIONS="$DISTRIBUTIONS $dist.txz"
129219615Snwhitehorn	done
130219615Snwhitehornfi
131218947Snwhitehorn
132218799SnwhitehornFETCH_DISTRIBUTIONS=""
133218799Snwhitehornfor dist in $DISTRIBUTIONS; do
134218799Snwhitehorn	if [ ! -f $BSDINSTALL_DISTDIR/$dist ]; then
135218799Snwhitehorn		FETCH_DISTRIBUTIONS="$FETCH_DISTRIBUTIONS $dist"
136218799Snwhitehorn	fi
137218799Snwhitehorndone
138220080SnwhitehornFETCH_DISTRIBUTIONS=`echo $FETCH_DISTRIBUTIONS`	# Trim white space
139218799Snwhitehorn
140220080Snwhitehornif [ -n "$FETCH_DISTRIBUTIONS" -a -n "$BSDINSTALL_CONFIGCURRENT" ]; then
141218799Snwhitehorn	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
142218799Snwhitehorn	bsdinstall netconfig || error
143218799Snwhitehorn	NETCONFIG_DONE=yes
144218799Snwhitehornfi
145218799Snwhitehorn
146220088Snwhitehornif [ -n "$FETCH_DISTRIBUTIONS" ]; then
147220080Snwhitehorn	exec 3>&1
148220834Snwhitehorn	BSDINSTALL_DISTSITE=$(`dirname $0`/mirrorselect 2>&1 1>&3)
149220080Snwhitehorn	MIRROR_BUTTON=$?
150220080Snwhitehorn	exec 3>&-
151269653Sthompsa	test $MIRROR_BUTTON -eq 0 || error "No mirror selected"
152220080Snwhitehorn	export BSDINSTALL_DISTSITE
153220080Snwhitehornfi
154220080Snwhitehorn
155259572Sdteskerm -f $PATH_FSTAB
156218799Snwhitehorntouch $PATH_FSTAB
157218799Snwhitehorn
158285679Sallanjude#
159285679Sallanjude# Try to detect known broken platforms and apply their workarounds
160285679Sallanjude#
161285679Sallanjude
162285679Sallanjudeif f_interactive; then
163285679Sallanjude	sys_maker=$( kenv -q smbios.system.maker )
164285679Sallanjude	f_dprintf "smbios.system.maker=[%s]" "$sys_maker"
165285679Sallanjude	sys_model=$( kenv -q smbios.system.product )
166285679Sallanjude	f_dprintf "smbios.system.product=[%s]" "$sys_model"
167285679Sallanjude	sys_version=$( kenv -q smbios.system.version )
168285679Sallanjude	f_dprintf "smbios.system.version=[%s]" "$sys_version"
169285732Sallanjude	sys_mb_maker=$( kenv -q smbios.planar.maker )
170285732Sallanjude	f_dprintf "smbios.planar.maker=[%s]" "$sys_mb_maker"
171285732Sallanjude	sys_mb_product=$( kenv -q smbios.planar.product )
172285732Sallanjude	f_dprintf "smbios.planar.product=[%s]" "$sys_mb_product"
173285732Sallanjude
174285732Sallanjude	#
175285732Sallanjude	# Laptop Models
176285732Sallanjude	#
177285679Sallanjude	case "$sys_maker" in
178285679Sallanjude	"LENOVO")
179285679Sallanjude		case "$sys_version" in
180285679Sallanjude		"ThinkPad X220"|"ThinkPad T420"|"ThinkPad T520")
181285679Sallanjude			dialog_workaround "$msg_lenovo_fix"
182285679Sallanjude			retval=$?
183285679Sallanjude			f_dprintf "lenovofix_prompt=[%s]" "$retval"
184285679Sallanjude			if [ $retval -eq $DIALOG_OK ]; then
185285679Sallanjude				export ZFSBOOT_PARTITION_SCHEME="GPT + Lenovo Fix"
186285679Sallanjude				export WORKAROUND_LENOVO=1
187285679Sallanjude			fi
188285679Sallanjude			;;
189285679Sallanjude		esac
190285679Sallanjude		;;
191285679Sallanjude	"Dell Inc.")
192285679Sallanjude		case "$sys_model" in
193285732Sallanjude		"Latitude E7440"|"Latitude E7240")
194285679Sallanjude			dialog_workaround "$msg_gpt_active_fix"
195285679Sallanjude			retval=$?
196285679Sallanjude			f_dprintf "gpt_active_fix_prompt=[%s]" "$retval"
197285679Sallanjude			if [ $retval -eq $DIALOG_OK ]; then
198285679Sallanjude				export ZFSBOOT_PARTITION_SCHEME="GPT + Active"
199285679Sallanjude				export WORKAROUND_GPTACTIVE=1
200285679Sallanjude			fi
201285679Sallanjude			;;
202285679Sallanjude		esac
203285679Sallanjude		;;
204285679Sallanjude	esac
205285732Sallanjude	#
206285732Sallanjude	# Motherboard Models
207285732Sallanjude	#
208285732Sallanjude	case "$sys_mb_maker" in
209285732Sallanjude	"Intel Corporation")
210285732Sallanjude		case "$sys_mb_product" in
211285732Sallanjude		"DP965LT")
212285732Sallanjude			dialog_workaround "$msg_gpt_active_fix"
213285732Sallanjude			retval=$?
214285732Sallanjude			f_dprintf "gpt_active_fix_prompt=[%s]" "$retval"
215285732Sallanjude			if [ $retval -eq $DIALOG_OK ]; then
216285732Sallanjude				export ZFSBOOT_PARTITION_SCHEME="GPT + Active"
217285732Sallanjude				export WORKAROUND_GPTACTIVE=1
218285732Sallanjude			fi
219285732Sallanjude			;;
220285732Sallanjude		esac
221285732Sallanjude		;;
222285732Sallanjude	esac
223285679Sallanjudefi
224285679Sallanjude
225256343SdteskePMODES="\
226271553Snwhitehorn\"Auto (UFS)\" \"Guided Disk Setup\" \
227271553SnwhitehornManual \"Manual Disk Setup (experts)\" \
228256343SdteskeShell \"Open a shell and partition by hand\""
229218799Snwhitehorn
230256343SdteskeCURARCH=$( uname -m )
231256343Sdteskecase $CURARCH in
232256343Sdteske	amd64|i386)	# Booting ZFS Supported
233271567Snwhitehorn		PMODES="$PMODES \"Auto (ZFS)\" \"Guided Root-on-ZFS\""
234256343Sdteske		;;
235256343Sdteske	*)		# Booting ZFS Unspported
236256343Sdteske		;;
237256343Sdteskeesac
238256343Sdteske
239256343Sdteskeexec 3>&1
240256343SdteskePARTMODE=`echo $PMODES | xargs dialog --backtitle "FreeBSD Installer" \
241256343Sdteske	--title "Partitioning" \
242256343Sdteske	--menu "How would you like to partition your disk?" \
243259570Sdteske	0 0 0 2>&1 1>&3` || exit 1
244256343Sdteskeexec 3>&-
245256343Sdteske
246256343Sdteskecase "$PARTMODE" in
247271553Snwhitehorn"Auto (UFS)")	# Guided
248269653Sthompsa	bsdinstall autopart || error "Partitioning error"
249269653Sthompsa	bsdinstall mount || error "Failed to mount filesystem"
250218799Snwhitehorn	;;
251256343Sdteske"Shell")	# Shell
252218799Snwhitehorn	clear
253218799Snwhitehorn	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'."
254222425Snwhitehorn	sh 2>&1
255218799Snwhitehorn	;;
256256343Sdteske"Manual")	# Manual
257257842Sdteske	if f_isset debugFile; then
258257842Sdteske		# Give partedit the path to our logfile so it can append
259269653Sthompsa		BSDINSTALL_LOG="${debugFile#+}" bsdinstall partedit || error "Partitioning error"
260257842Sdteske	else
261269653Sthompsa		bsdinstall partedit || error "Partitioning error"
262257842Sdteske	fi
263269653Sthompsa	bsdinstall mount || error "Failed to mount filesystem"
264218799Snwhitehorn	;;
265271553Snwhitehorn"Auto (ZFS)")	# ZFS
266269653Sthompsa	bsdinstall zfsboot || error "ZFS setup failed"
267269653Sthompsa	bsdinstall mount || error "Failed to mount filesystem"
268256343Sdteske	;;
269218799Snwhitehorn*)
270269653Sthompsa	error "Unknown partitioning mode"
271218799Snwhitehorn	;;
272218799Snwhitehornesac
273218799Snwhitehorn
274218799Snwhitehornif [ ! -z "$FETCH_DISTRIBUTIONS" ]; then
275218799Snwhitehorn	ALL_DISTRIBUTIONS="$DISTRIBUTIONS"
276218799Snwhitehorn
277218799Snwhitehorn	# Download to a directory in the new system as scratch space
278219528Snwhitehorn	BSDINSTALL_FETCHDEST="$BSDINSTALL_CHROOT/usr/freebsd-dist"
279269653Sthompsa	mkdir -p "$BSDINSTALL_FETCHDEST" || error "Could not create directory $BSDINSTALL_FETCHDEST"
280218799Snwhitehorn
281218799Snwhitehorn	export DISTRIBUTIONS="$FETCH_DISTRIBUTIONS"
282218799Snwhitehorn	# Try to use any existing distfiles
283220088Snwhitehorn	if [ -d $BSDINSTALL_DISTDIR ]; then
284220080Snwhitehorn		DISTDIR_IS_UNIONFS=1
285223897Snwhitehorn		mount_nullfs -o union "$BSDINSTALL_FETCHDEST" "$BSDINSTALL_DISTDIR"
286225637Snwhitehorn	else
287225637Snwhitehorn		export DISTRIBUTIONS="MANIFEST $ALL_DISTRIBUTIONS"
288218799Snwhitehorn		export BSDINSTALL_DISTDIR="$BSDINSTALL_FETCHDEST"
289218799Snwhitehorn	fi
290218799Snwhitehorn		
291225637Snwhitehorn	export FTP_PASSIVE_MODE=YES
292269653Sthompsa	bsdinstall distfetch || error "Failed to fetch distribution"
293218799Snwhitehorn	export DISTRIBUTIONS="$ALL_DISTRIBUTIONS"
294218799Snwhitehornfi
295218799Snwhitehorn
296269653Sthompsabsdinstall checksum || error "Distribution checksum failed"
297269653Sthompsabsdinstall distextract || error "Distribution extract failed"
298269653Sthompsabsdinstall rootpass || error "Could not set root password"
299218799Snwhitehorn
300218799Snwhitehorntrap true SIGINT	# This section is optional
301218799Snwhitehornif [ "$NETCONFIG_DONE" != yes ]; then
302218799Snwhitehorn	bsdinstall netconfig	# Don't check for errors -- the user may cancel
303218799Snwhitehornfi
304218799Snwhitehornbsdinstall time
305218799Snwhitehornbsdinstall services
306218799Snwhitehorn
307218799Snwhitehorndialog --backtitle "FreeBSD Installer" --title "Add User Accounts" --yesno \
308218799Snwhitehorn    "Would you like to add users to the installed system now?" 0 0 && \
309218799Snwhitehorn    bsdinstall adduser
310218799Snwhitehorn
311218799Snwhitehornfinalconfig() {
312218799Snwhitehorn	exec 3>&1
313218799Snwhitehorn	REVISIT=$(dialog --backtitle "FreeBSD Installer" \
314218799Snwhitehorn	    --title "Final Configuration" --no-cancel --menu \
315228194Snwhitehorn	    "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 \
316226507Skensmith		"Exit" "Apply configuration and exit installer" \
317218799Snwhitehorn		"Add User" "Add a user to the system" \
318218799Snwhitehorn		"Root Password" "Change root password" \
319218799Snwhitehorn		"Hostname" "Set system hostname" \
320218799Snwhitehorn		"Network" "Networking configuration" \
321218799Snwhitehorn		"Services" "Set daemons to run on startup" \
322218799Snwhitehorn		"Time Zone" "Set system timezone" \
323228194Snwhitehorn		"Handbook" "Install FreeBSD Handbook (requires network)" 2>&1 1>&3)
324218799Snwhitehorn	exec 3>&-
325218799Snwhitehorn
326218799Snwhitehorn	case "$REVISIT" in
327218799Snwhitehorn	"Add User")
328218799Snwhitehorn		bsdinstall adduser
329218799Snwhitehorn		finalconfig
330218799Snwhitehorn		;;
331218799Snwhitehorn	"Root Password")
332218799Snwhitehorn		bsdinstall rootpass 
333218799Snwhitehorn		finalconfig
334218799Snwhitehorn		;;
335218799Snwhitehorn	"Hostname")
336218799Snwhitehorn		bsdinstall hostname
337218799Snwhitehorn		finalconfig
338218799Snwhitehorn		;;
339218799Snwhitehorn	"Network")
340218799Snwhitehorn		bsdinstall netconfig
341218799Snwhitehorn		finalconfig
342218799Snwhitehorn		;;
343218799Snwhitehorn	"Services")
344218799Snwhitehorn		bsdinstall services
345218799Snwhitehorn		finalconfig
346218799Snwhitehorn		;;
347218799Snwhitehorn	"Time Zone")
348218799Snwhitehorn		bsdinstall time
349218799Snwhitehorn		finalconfig
350218799Snwhitehorn		;;
351223897Snwhitehorn	"Handbook")
352223897Snwhitehorn		bsdinstall docsinstall
353223897Snwhitehorn		finalconfig
354223897Snwhitehorn		;;
355218799Snwhitehorn	esac
356218799Snwhitehorn}
357218799Snwhitehorn
358218799Snwhitehorn# Allow user to change his mind
359218799Snwhitehornfinalconfig
360218799Snwhitehorn
361218799Snwhitehorntrap error SIGINT	# SIGINT is bad again
362269653Sthompsabsdinstall config  || error "Failed to save config"
363218799Snwhitehorn
364218799Snwhitehornif [ ! -z "$BSDINSTALL_FETCHDEST" ]; then
365218799Snwhitehorn	[ "$BSDINSTALL_FETCHDEST" != "$BSDINSTALL_DISTDIR" ] && \
366218799Snwhitehorn	    umount "$BSDINSTALL_DISTDIR"
367218799Snwhitehorn	rm -rf "$BSDINSTALL_FETCHDEST"
368218799Snwhitehornfi
369218799Snwhitehorn
370228194Snwhitehorndialog --backtitle "FreeBSD Installer" --title "Manual Configuration" \
371271552Snwhitehorn    --default-button no --yesno \
372271552Snwhitehorn   "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
373228194Snwhitehornif [ $? -eq 0 ]; then
374228194Snwhitehorn	clear
375232531Snwhitehorn	mount -t devfs devfs "$BSDINSTALL_CHROOT/dev"
376228194Snwhitehorn	echo This shell is operating in a chroot in the new system. \
377228194Snwhitehorn	    When finished making configuration changes, type \"exit\".
378228194Snwhitehorn	chroot "$BSDINSTALL_CHROOT" /bin/sh 2>&1
379228194Snwhitehornfi
380228194Snwhitehorn
381256338Sdesbsdinstall entropy
382256338Sdesbsdinstall umount
383256338Sdes
384257842Sdteskef_dprintf "Installation Completed at %s" "$( date )"
385218799Snwhitehorn
386257842Sdteske################################################################################
387257842Sdteske# END
388257842Sdteske################################################################################
389