xref: /freebsd/usr.sbin/bsdinstall/scripts/script (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
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# $FreeBSD$
29#
30############################################################ INCLUDES
31
32BSDCFG_SHARE="/usr/share/bsdconfig"
33. $BSDCFG_SHARE/common.subr || exit 1
34f_dprintf "%s: loading includes..." "$0"
35f_include $BSDCFG_SHARE/dialog.subr
36f_include $BSDCFG_SHARE/variable.subr
37
38############################################################ CONFIGURATION
39
40# VARIABLES:
41# PARTITIONS
42# DISTRIBUTIONS
43# BSDINSTALL_DISTDIR
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
92
93split -a 2 -p '^#!.*' "$SCRIPT" $TMPDIR/bsdinstall-installscript-
94
95. $TMPDIR/bsdinstall-installscript-aa
96: ${DISTRIBUTIONS="kernel.txz base.txz"}; export DISTRIBUTIONS
97export BSDINSTALL_DISTDIR
98
99# Re-initialize a new log if preamble changed BSDINSTALL_LOG
100if [ "$BSDINSTALL_LOG" != "${debugFile#+}" ]; then
101	export debugFile="$BSDINSTALL_LOG"
102	f_quietly f_debug_init
103	# NB: Being scripted, let debug go to terminal for invalid debugFile
104	f_dprintf "Began Installation at %s" "$( date )"
105fi
106
107# Make partitions
108rm -f $PATH_FSTAB
109touch $PATH_FSTAB
110if [ "$ZFSBOOT_DISKS" ]; then
111	bsdinstall zfsboot
112else
113	bsdinstall scriptedpart "$PARTITIONS"
114fi
115bsdinstall mount
116
117# Fetch missing distribution files, if any
118(
119	exec 3>&1
120	export BSDINSTALL_DISTDIR=$(`dirname $0`/fetchmissingdists 2>&1 1>&3)
121	FETCH_RESULT=$?
122	exec 3>&-
123	return $FETCH_RESULT
124) || error "Could not fetch remote distributions"
125
126# Unpack distributions
127bsdinstall checksum
128if [ -t 0 ]; then
129	# If install is a tty, use distextract as normal
130	bsdinstall distextract
131else
132	# Otherwise, we need to use tar (see https://reviews.freebsd.org/D10736)
133	for set in $DISTRIBUTIONS; do
134		f_dprintf "Extracting $BSDINSTALL_DISTDIR/$set"
135		# XXX: The below fails if any mountpoints are FAT, due to
136		# inability to set ctime/mtime on the root of FAT partitions,
137		# which is needed to support e.g. EFI system partitions. tar has
138		# no option to ignore this (distextract ignores them internally
139		# through a hack), and returns 1 on any warning or error,
140		# effectively turning all warnings into fatal errors.
141		#
142		# Work around this in an extremely lame way for the specific
143		# case of EFI system partitions only. This *ONLY WORKS* if
144		# /boot/efi is empty and does not handle analagous problems on
145		# other systems (ARM, PPC64).
146		tar -xf "$BSDINSTALL_DISTDIR/$set" -C $BSDINSTALL_CHROOT --exclude boot/efi
147		mkdir -p $BSDINSTALL_CHROOT/boot/efi
148	done
149fi
150
151# Configure bootloader if needed
152bsdinstall bootconfig
153
154# Finalize install
155bsdinstall config
156
157# Make sure networking is functional, if we can arrange that
158if [ ! -f $BSDINSTALL_CHROOT/etc/resolv.conf -a -f /etc/resolv.conf ]; then
159	cp /etc/resolv.conf $BSDINSTALL_CHROOT/etc/resolv.conf
160fi
161
162# Run post-install script
163if [ -f $TMPDIR/bsdinstall-installscript-ab ]; then
164	cp $TMPDIR/bsdinstall-installscript-ab $BSDINSTALL_CHROOT/tmp/installscript
165	chmod a+x $BSDINSTALL_CHROOT/tmp/installscript
166	chroot $BSDINSTALL_CHROOT /tmp/installscript $@ 2>&1
167	rm $BSDINSTALL_CHROOT/tmp/installscript
168fi
169
170bsdinstall entropy
171bsdinstall umount
172if [ "$ZFSBOOT_DISKS" ]; then
173	zpool export $ZFSBOOT_POOL_NAME
174fi
175
176f_dprintf "Installation Completed at %s" "$( date )"
177
178trap - EXIT
179exit $SUCCESS
180
181################################################################################
182# END
183################################################################################
184