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