xref: /freebsd/usr.sbin/bsdinstall/scripts/script (revision 3b16e96b005c723717aa6a4ebc48000354e64fa1)
1#!/bin/sh
2#-
3# Copyright (c) 2013 Nathan Whitehorn
4# Copyright (c) 2013-2015 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#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." "$0"
34f_include $BSDCFG_SHARE/dialog.subr
35f_include $BSDCFG_SHARE/variable.subr
36
37############################################################ CONFIGURATION
38
39# VARIABLES:
40# PARTITIONS
41# DISTRIBUTIONS
42# BSDINSTALL_DISTDIR
43# COMPONENTS
44
45#
46# Default name of the ZFS boot-pool
47#
48: ${ZFSBOOT_POOL_NAME:=zroot}
49
50############################################################ GLOBALS
51
52: ${TMPDIR:="/tmp"}
53
54#
55# Strings that should be moved to an i18n file and loaded with f_include_lang()
56#
57msg_installation_error="Installation Error!"
58
59############################################################ FUNCTIONS
60
61error()
62{
63	local file
64	f_getvar "$VAR_DEBUG_FILE#+" file
65	if [ "$file" ]; then
66		f_dialog_title "$msg_installation_error"
67		f_dialog_textbox "$file"
68		# No need to restore title, pining for the fjords
69	fi
70
71	[ -f "$PATH_FSTAB" ] || exit
72	if [ "$ZFSBOOT_DISKS" ]; then
73		zpool export $ZFSBOOT_POOL_NAME
74	else
75		bsdinstall umount
76	fi
77
78	exit 1
79}
80
81############################################################ MAIN
82
83set -e
84trap error EXIT
85
86SCRIPT="$1"
87shift
88
89f_dprintf "Began Installation at %s" "$( date )"
90rm -rf $BSDINSTALL_TMPETC
91mkdir $BSDINSTALL_TMPETC
92rm -f $TMPDIR/bsdinstall-installscript-setup
93
94# split script into preamble and setup script at first shebang
95awk 'BEGIN {pathb=ARGV[2]; ARGV[2]=""} /^#!/{b=1} {
96    if (b) print >pathb; else print}' \
97    "$SCRIPT" $TMPDIR/bsdinstall-installscript-setup \
98    >$TMPDIR/bsdinstall-installscript-preamble
99
100. $TMPDIR/bsdinstall-installscript-preamble
101export BSDINSTALL_DISTDIR
102
103# Re-initialize a new log if preamble changed BSDINSTALL_LOG
104if [ "$BSDINSTALL_LOG" != "${debugFile#+}" ]; then
105	export debugFile="$BSDINSTALL_LOG"
106	f_quietly f_debug_init
107	# NB: Being scripted, let debug go to terminal for invalid debugFile
108	f_dprintf "Began Installation at %s" "$( date )"
109fi
110
111# Make partitions
112rm -f $PATH_FSTAB
113touch $PATH_FSTAB
114if [ "$ZFSBOOT_DISKS" ]; then
115	bsdinstall zfsboot
116else
117	bsdinstall scriptedpart "$PARTITIONS"
118fi
119bsdinstall mount
120
121if [ -n "$COMPONENTS" -a -n "$DISTRIBUTIONS" ]; then
122	error "Cannot set both COMPONENTS and DISTRIBUTIONS"
123elif [ -z "$DISTRIBUTIONS" ]; then
124	# If COMPONENTS is set, or neither is, install with pkgbase
125	bsdinstall pkgbase --non-interactive
126else
127	# Otherwise, unpack distsets
128
129	# Fetch missing distribution files, if any
130	exec 5>&1
131	export BSDINSTALL_DISTDIR=$(`dirname $0`/fetchmissingdists 2>&1 1>&5)
132	FETCH_RESULT=$?
133	exec 5>&-
134
135	[ $FETCH_RESULT -ne 0 ] && error "Could not fetch remote distributions"
136
137	bsdinstall checksum
138	if [ -t 0 ]; then
139		# If install is a tty, use distextract as normal
140		bsdinstall distextract
141	else
142		# Otherwise, we need to use tar (see
143		# https://reviews.freebsd.org/D10736)
144		for set in $DISTRIBUTIONS; do
145			f_dprintf "Extracting $BSDINSTALL_DISTDIR/$set"
146			# XXX: The below fails if any mountpoints are FAT, due
147			# to inability to set ctime/mtime on the root of FAT
148			# partitions, which is needed to support e.g. EFI
149			# system partitions. tar has no option to ignore this
150			# (distextract ignores them internally through a hack),
151			# and returns 1 on any warning or error, effectively
152			# turning all warnings into fatal errors.
153			#
154			# Work around this in an extremely lame way for the
155			# specific case of EFI system partitions only. This
156			# *ONLY WORKS* if /boot/efi is empty and does not
157			# handle analogous problems on other systems (ARM,
158			# PPC64).
159			tar -xf "$BSDINSTALL_DISTDIR/$set" -C $BSDINSTALL_CHROOT --exclude boot/efi
160			mkdir -p $BSDINSTALL_CHROOT/boot/efi
161		done
162	fi
163fi
164
165# Configure bootloader if needed
166bsdinstall bootconfig
167
168# Finalize install
169bsdinstall config
170
171# Make sure networking is functional, if we can arrange that
172if [ ! -f $BSDINSTALL_CHROOT/etc/resolv.conf -a -f /etc/resolv.conf ]; then
173	cp /etc/resolv.conf $BSDINSTALL_CHROOT/etc/resolv.conf
174fi
175
176# Run post-install script
177if [ -f $TMPDIR/bsdinstall-installscript-setup ]; then
178	cp $TMPDIR/bsdinstall-installscript-setup \
179	    $BSDINSTALL_CHROOT/tmp/installscript
180	chmod a+x $BSDINSTALL_CHROOT/tmp/installscript
181	chroot $BSDINSTALL_CHROOT /tmp/installscript $@ 2>&1
182	rm $BSDINSTALL_CHROOT/tmp/installscript
183fi
184
185bsdinstall entropy
186bsdinstall umount
187if [ "$ZFSBOOT_DISKS" ]; then
188	zpool export $ZFSBOOT_POOL_NAME
189fi
190
191f_dprintf "Installation Completed at %s" "$( date )"
192
193trap - EXIT
194exit $SUCCESS
195
196################################################################################
197# END
198################################################################################
199