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