xref: /freebsd/usr.sbin/bsdinstall/scripts/script (revision 283959bbe0863917c4fc3200a92d1055a4c89bdc)
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. $TMPDIR/bsdinstall-installscript-preamble
102export BSDINSTALL_DISTDIR
103
104# Re-initialize a new log if preamble changed BSDINSTALL_LOG
105if [ "$BSDINSTALL_LOG" != "${debugFile#+}" ]; then
106	export debugFile="$BSDINSTALL_LOG"
107	f_quietly f_debug_init
108	# NB: Being scripted, let debug go to terminal for invalid debugFile
109	f_dprintf "Began Installation at %s" "$( date )"
110fi
111
112# Make partitions
113rm -f $PATH_FSTAB
114touch $PATH_FSTAB
115if [ "$ZFSBOOT_DISKS" ]; then
116	bsdinstall zfsboot
117else
118	bsdinstall scriptedpart "$PARTITIONS"
119fi
120bsdinstall mount
121
122if [ -n "$COMPONENTS" -a -n "$DISTRIBUTIONS" ]; then
123	error "Cannot set both COMPONENTS and DISTRIBUTIONS"
124elif [ -z "$DISTRIBUTIONS" ]; then
125	# If COMPONENTS is set, or neither is, install with pkgbase
126	bsdinstall pkgbase --non-interactive
127else
128	# Otherwise, unpack distsets
129
130	# Fetch missing distribution files, if any
131	exec 5>&1
132	export BSDINSTALL_DISTDIR=$(`dirname $0`/fetchmissingdists 2>&1 1>&5)
133	FETCH_RESULT=$?
134	exec 5>&-
135
136	[ $FETCH_RESULT -ne 0 ] && error "Could not fetch remote distributions"
137
138	bsdinstall checksum
139	if [ -t 0 ]; then
140		# If install is a tty, use distextract as normal
141		bsdinstall distextract
142	else
143		# Otherwise, we need to use tar (see
144		# https://reviews.freebsd.org/D10736)
145		for set in $DISTRIBUTIONS; do
146			f_dprintf "Extracting $BSDINSTALL_DISTDIR/$set"
147			# XXX: The below fails if any mountpoints are FAT, due
148			# to inability to set ctime/mtime on the root of FAT
149			# partitions, which is needed to support e.g. EFI
150			# system partitions. tar has no option to ignore this
151			# (distextract ignores them internally through a hack),
152			# and returns 1 on any warning or error, effectively
153			# turning all warnings into fatal errors.
154			#
155			# Work around this in an extremely lame way for the
156			# specific case of EFI system partitions only. This
157			# *ONLY WORKS* if /boot/efi is empty and does not
158			# handle analogous problems on other systems (ARM,
159			# PPC64).
160			tar -xf "$BSDINSTALL_DISTDIR/$set" -C $BSDINSTALL_CHROOT --exclude boot/efi
161			mkdir -p $BSDINSTALL_CHROOT/boot/efi
162		done
163	fi
164fi
165
166# Configure bootloader if needed
167bsdinstall bootconfig
168
169# Finalize install
170bsdinstall config
171
172# Make sure networking is functional, if we can arrange that
173if [ ! -f $BSDINSTALL_CHROOT/etc/resolv.conf -a -f /etc/resolv.conf ]; then
174	cp /etc/resolv.conf $BSDINSTALL_CHROOT/etc/resolv.conf
175fi
176
177# Run post-install script
178if [ -f $TMPDIR/bsdinstall-installscript-setup ]; then
179	cp $TMPDIR/bsdinstall-installscript-setup \
180	    $BSDINSTALL_CHROOT/tmp/installscript
181	chmod a+x $BSDINSTALL_CHROOT/tmp/installscript
182	chroot $BSDINSTALL_CHROOT /tmp/installscript $@ 2>&1
183	rm $BSDINSTALL_CHROOT/tmp/installscript
184fi
185
186bsdinstall entropy
187bsdinstall umount
188if [ "$ZFSBOOT_DISKS" ]; then
189	zpool export $ZFSBOOT_POOL_NAME
190fi
191
192f_dprintf "Installation Completed at %s" "$( date )"
193
194trap - EXIT
195exit $SUCCESS
196
197################################################################################
198# END
199################################################################################
200