xref: /freebsd/usr.sbin/bsdinstall/scripts/auto (revision 25ecdc7d52770caf1c9b44b5ec11f468f6b636f3)
1#!/bin/sh
2#-
3# Copyright (c) 2011 Nathan Whitehorn
4# Copyright (c) 2013-2018 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
34f_include $BSDCFG_SHARE/dialog.subr
35
36############################################################ GLOBALS
37
38#
39# Strings that should be moved to an i18n file and loaded with f_include_lang()
40#
41hline_arrows_tab_enter="Press arrows, TAB or ENTER"
42hline_arrows_tab_space_enter="Press arrows, TAB, SPACE or ENTER"
43msg_abort="Abort"
44msg_an_installation_step_has_been_aborted="An installation step has been aborted. Would you like\nto restart the installation or exit the installer?"
45msg_auto_ufs="Auto (UFS)"
46msg_auto_ufs_desc="Guided UFS Disk Setup"
47msg_auto_ufs_help="Menu options help choose which disk to setup using UFS and standard partitions"
48msg_auto_zfs="Auto (ZFS)"
49msg_auto_zfs_desc="Guided Root-on-ZFS"
50msg_auto_zfs_help="To use ZFS with less than 8GB RAM, see https://wiki.freebsd.org/ZFSTuningGuide"
51msg_exit="Exit"
52msg_freebsd_installer="FreeBSD Installer"
53msg_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?"
54msg_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?"
55msg_manual="Manual"
56msg_manual_desc="Manual Disk Setup (experts)"
57msg_manual_help="Create customized partitions from menu options"
58msg_no="NO"
59msg_restart="Restart"
60msg_shell="Shell"
61msg_shell_desc="Open a shell and partition by hand"
62msg_shell_help="Create customized partitions using command-line utilities"
63msg_yes="YES"
64
65############################################################ FUNCTIONS
66
67# error [$msg]
68#
69# Display generic error message when a script fails. An optional message
70# argument can preceed the generic message. User is given the choice of
71# restarting the installer or exiting.
72#
73error()
74{
75	local title="$msg_abort"
76	local btitle="$msg_freebsd_installer"
77	local prompt="${1:+$1\n\n}$msg_an_installation_step_has_been_aborted"
78	local hline="$hline_arrows_tab_space_enter"
79
80	[ -f "$PATH_FSTAB" ] && bsdinstall umount
81
82	local height width
83	f_dialog_buttonbox_size height width \
84		"$title" "$btitle" "$prompt" "$hline"
85
86	if $DIALOG \
87		--title "$title"           \
88		--backtitle "$btitle"      \
89		--hline "$hline"           \
90		--no-label "$msg_exit"     \
91		--yes-label "$msg_restart" \
92		--yesno "$prompt" $height $width
93	then
94		exec $0
95		# NOTREACHED
96	fi
97	exit 1
98}
99
100# dialog_workaround
101#
102# Ask the user if they wish to apply a workaround
103#
104dialog_workaround()
105{
106	local passed_msg="$1"
107	local title="$DIALOG_TITLE"
108	local btitle="$DIALOG_BACKTITLE"
109	local prompt # Calculated below
110	local hline="$hline_arrows_tab_enter"
111
112	local height=8 width=50 prefix="   "
113	local plen=${#prefix} list= line=
114	local max_width=$(( $width - 3 - $plen ))
115
116	local yes no defaultno extra_args format
117	if [ "$USE_XDIALOG" ]; then
118		yes=ok no=cancel defaultno=default-no
119		extra_args="--wrap --left"
120		format="$passed_msg"
121	else
122		yes=yes no=no defaultno=defaultno
123		extra_args="--cr-wrap"
124		format="$passed_msg"
125	fi
126
127	# Add height for Xdialog(1)
128	[ "$USE_XDIALOG" ] && height=$(( $height + $height / 5 + 3 ))
129
130	prompt=$( printf "$format" )
131	f_dprintf "%s: Workaround prompt" "$0"
132	$DIALOG \
133		--title "$title"        \
134		--backtitle "$btitle"   \
135		--hline "$hline"        \
136		--$yes-label "$msg_yes" \
137		--$no-label "$msg_no"   \
138		$extra_args             \
139		--yesno "$prompt" $height $width
140}
141
142############################################################ MAIN
143
144f_dprintf "Began Installation at %s" "$( date )"
145
146rm -rf $BSDINSTALL_TMPETC
147mkdir $BSDINSTALL_TMPETC
148
149trap true SIGINT	# This section is optional
150bsdinstall keymap
151
152trap error SIGINT	# Catch cntrl-C here
153bsdinstall hostname || error "Set hostname failed"
154
155export DISTRIBUTIONS="base.txz kernel.txz"
156if [ -f $BSDINSTALL_DISTDIR/MANIFEST ]; then
157	DISTMENU=`awk -F'\t' '!/^(kernel\.txz|base\.txz)/{print $1,$5,$6}' $BSDINSTALL_DISTDIR/MANIFEST`
158	DISTMENU="$(echo ${DISTMENU} | sed -E 's/\.txz//g')"
159
160	exec 3>&1
161	EXTRA_DISTS=$( eval dialog \
162	    --backtitle \"FreeBSD Installer\" \
163	    --title \"Distribution Select\" --nocancel --separate-output \
164	    --checklist \"Choose optional system components to install:\" \
165	    0 0 0 $DISTMENU \
166	2>&1 1>&3 )
167	for dist in $EXTRA_DISTS; do
168		export DISTRIBUTIONS="$DISTRIBUTIONS $dist.txz"
169	done
170fi
171
172FETCH_DISTRIBUTIONS=""
173for dist in $DISTRIBUTIONS; do
174	if [ ! -f $BSDINSTALL_DISTDIR/$dist ]; then
175		FETCH_DISTRIBUTIONS="$FETCH_DISTRIBUTIONS $dist"
176	fi
177done
178
179if [ -n "$FETCH_DISTRIBUTIONS" -a -n "$BSDINSTALL_CONFIGCURRENT" ]; then
180	dialog --backtitle "FreeBSD Installer" --title "Network Installation" --msgbox "Some installation files were not 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
181	bsdinstall netconfig || error
182	NETCONFIG_DONE=yes
183fi
184
185rm -f $PATH_FSTAB
186touch $PATH_FSTAB
187
188#
189# Try to detect known broken platforms and apply their workarounds
190#
191
192if f_interactive; then
193	sys_maker=$( kenv -q smbios.system.maker )
194	f_dprintf "smbios.system.maker=[%s]" "$sys_maker"
195	sys_model=$( kenv -q smbios.system.product )
196	f_dprintf "smbios.system.product=[%s]" "$sys_model"
197	sys_version=$( kenv -q smbios.system.version )
198	f_dprintf "smbios.system.version=[%s]" "$sys_version"
199	sys_mb_maker=$( kenv -q smbios.planar.maker )
200	f_dprintf "smbios.planar.maker=[%s]" "$sys_mb_maker"
201	sys_mb_product=$( kenv -q smbios.planar.product )
202	f_dprintf "smbios.planar.product=[%s]" "$sys_mb_product"
203
204	#
205	# Laptop Models
206	#
207	case "$sys_maker" in
208	"LENOVO")
209		case "$sys_version" in
210		"ThinkPad X220"|"ThinkPad T420"|"ThinkPad T520"|"ThinkPad W520"|"ThinkPad X1")
211			dialog_workaround "$msg_lenovo_fix"
212			retval=$?
213			f_dprintf "lenovofix_prompt=[%s]" "$retval"
214			if [ $retval -eq $DIALOG_OK ]; then
215				export ZFSBOOT_PARTITION_SCHEME="GPT + Lenovo Fix"
216				export WORKAROUND_LENOVO=1
217			fi
218			;;
219		esac
220		;;
221	"Dell Inc.")
222		case "$sys_model" in
223		"Latitude E6330"|"Latitude E7440"|"Latitude E7240"|"Precision Tower 5810")
224			dialog_workaround "$msg_gpt_active_fix"
225			retval=$?
226			f_dprintf "gpt_active_fix_prompt=[%s]" "$retval"
227			if [ $retval -eq $DIALOG_OK ]; then
228				export ZFSBOOT_PARTITION_SCHEME="GPT + Active"
229				export WORKAROUND_GPTACTIVE=1
230			fi
231			;;
232		esac
233		;;
234	"Hewlett-Packard")
235		case "$sys_model" in
236		"HP ProBook 4330s")
237			dialog_workaround "$msg_gpt_active_fix"
238			retval=$?
239			f_dprintf "gpt_active_fix_prompt=[%s]" "$retval"
240			if [ $retval -eq $DIALOG_OK ]; then
241				export ZFSBOOT_PARTITION_SCHEME="GPT + Active"
242				export WORKAROUND_GPTACTIVE=1
243			fi
244			;;
245		esac
246		;;
247	esac
248	#
249	# Motherboard Models
250	#
251	case "$sys_mb_maker" in
252	"Intel Corporation")
253		case "$sys_mb_product" in
254		"DP965LT"|"D510MO")
255			dialog_workaround "$msg_gpt_active_fix"
256			retval=$?
257			f_dprintf "gpt_active_fix_prompt=[%s]" "$retval"
258			if [ $retval -eq $DIALOG_OK ]; then
259				export ZFSBOOT_PARTITION_SCHEME="GPT + Active"
260				export WORKAROUND_GPTACTIVE=1
261			fi
262			;;
263		esac
264		;;
265	"Acer")
266		case "$sys_mb_product" in
267		"Veriton M6630G")
268			dialog_workaround "$msg_gpt_active_fix"
269			retval=$?
270			f_dprintf "gpt_active_fix_prompt=[%s]" "$retval"
271			if [ $retval -eq $DIALOG_OK ]; then
272				export ZFSBOOT_PARTITION_SCHEME="GPT + Active"
273				export WORKAROUND_GPTACTIVE=1
274			fi
275			;;
276		esac
277		;;
278	esac
279fi
280
281PMODES="
282	'$msg_auto_ufs' '$msg_auto_ufs_desc' '$msg_auto_ufs_help'
283	'$msg_manual' '$msg_manual_desc' '$msg_manual_help'
284	'$msg_shell' '$msg_shell_desc' '$msg_shell_help'
285" # END-QUOTE
286
287CURARCH=$( uname -m )
288case $CURARCH in
289	amd64|arm64|i386|riscv)	# Booting ZFS Supported
290		PMODES="
291			'$msg_auto_zfs' '$msg_auto_zfs_desc' '$msg_auto_zfs_help'
292			$PMODES
293		" # END-QUOTE
294		;;
295	*)			# Booting ZFS Unsupported
296		;;
297esac
298
299exec 3>&1
300PARTMODE=`echo $PMODES | xargs dialog --backtitle "FreeBSD Installer" \
301	--title "Partitioning" \
302	--item-help \
303	--menu "How would you like to partition your disk?" \
304	0 0 0 2>&1 1>&3` || exit 1
305exec 3>&-
306
307case "$PARTMODE" in
308"$msg_auto_zfs")	# ZFS
309	bsdinstall zfsboot || error "ZFS setup failed"
310	bsdinstall mount || error "Failed to mount filesystem"
311	;;
312"$msg_auto_ufs")	# Guided UFS
313	bsdinstall autopart || error "Partitioning error"
314	bsdinstall mount || error "Failed to mount filesystem"
315	;;
316"$msg_shell")		# Shell
317	clear
318	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'."
319	sh 2>&1
320	;;
321"$msg_manual")		# Manual
322	if f_isset debugFile; then
323		# Give partedit the path to our logfile so it can append
324		BSDINSTALL_LOG="${debugFile#+}" bsdinstall partedit || error "Partitioning error"
325	else
326		bsdinstall partedit || error "Partitioning error"
327	fi
328	bsdinstall mount || error "Failed to mount filesystem"
329	;;
330*)
331	error "Unknown partitioning mode"
332	;;
333esac
334
335if [ -n "$FETCH_DISTRIBUTIONS" ]; then
336	exec 3>&1
337	export BSDINSTALL_DISTDIR=$(`dirname $0`/fetchmissingdists 2>&1 1>&3)
338	FETCH_RESULT=$?
339	exec 3>&-
340
341	[ $FETCH_RESULT -ne 0 ] && error "Could not fetch remote distributions"
342fi
343bsdinstall checksum || error "Distribution checksum failed"
344bsdinstall distextract || error "Distribution extract failed"
345
346# Set up boot loader
347bsdinstall bootconfig || error "Failed to configure bootloader"
348
349bsdinstall rootpass || error "Could not set root password"
350
351trap true SIGINT	# This section is optional
352if [ "$NETCONFIG_DONE" != yes ]; then
353	bsdinstall netconfig	# Don't check for errors -- the user may cancel
354fi
355bsdinstall time
356bsdinstall services
357bsdinstall hardening
358
359dialog --backtitle "FreeBSD Installer" --title "Add User Accounts" --yesno \
360    "Would you like to add users to the installed system now?" 0 0 && \
361    bsdinstall adduser
362
363finalconfig() {
364	exec 3>&1
365	REVISIT=$(dialog --backtitle "FreeBSD Installer" \
366	    --title "Final Configuration" --no-cancel --menu \
367	    "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 \
368		"Exit" "Apply configuration and exit installer" \
369		"Add User" "Add a user to the system" \
370		"Root Password" "Change root password" \
371		"Hostname" "Set system hostname" \
372		"Network" "Networking configuration" \
373		"Services" "Set daemons to run on startup" \
374		"System Hardening" "Set security options" \
375		"Time Zone" "Set system timezone" \
376		"Handbook" "Install FreeBSD Handbook (requires network)" 2>&1 1>&3)
377	exec 3>&-
378
379	case "$REVISIT" in
380	"Add User")
381		bsdinstall adduser
382		finalconfig
383		;;
384	"Root Password")
385		bsdinstall rootpass
386		finalconfig
387		;;
388	"Hostname")
389		bsdinstall hostname
390		finalconfig
391		;;
392	"Network")
393		bsdinstall netconfig
394		finalconfig
395		;;
396	"Services")
397		bsdinstall services
398		finalconfig
399		;;
400	"System Hardening")
401		bsdinstall hardening
402		finalconfig
403		;;
404	"Time Zone")
405		bsdinstall time
406		finalconfig
407		;;
408	"Handbook")
409		bsdinstall docsinstall
410		finalconfig
411		;;
412	esac
413}
414
415# Allow user to change his mind
416finalconfig
417
418trap error SIGINT	# SIGINT is bad again
419bsdinstall config  || error "Failed to save config"
420
421if [ ! -z "$BSDINSTALL_FETCHDEST" ]; then
422	rm -rf "$BSDINSTALL_FETCHDEST"
423fi
424
425dialog --backtitle "FreeBSD Installer" --title "Manual Configuration" \
426    --default-button no --yesno \
427   "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
428if [ $? -eq 0 ]; then
429	clear
430	echo This shell is operating in a chroot in the new system. \
431	    When finished making configuration changes, type \"exit\".
432	chroot "$BSDINSTALL_CHROOT" /bin/sh 2>&1
433fi
434
435bsdinstall entropy
436bsdinstall umount
437
438f_dprintf "Installation Completed at %s" "$( date )"
439
440################################################################################
441# END
442################################################################################
443