#!/bin/sh
#-
# Copyright (c) 2011 Nathan Whitehorn
# Copyright (c) 2013-2018 Devin Teske
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#
############################################################ INCLUDES
BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
f_include $BSDCFG_SHARE/dialog.subr
############################################################ GLOBALS
#
# List of environment variables that may be defined by the user, but modified
# during the installation process. They are then restored when restarting this
# script.
#
user_env_vars="BSDINSTALL_DISTSITE DISTRIBUTIONS WORKAROUND_GPTACTIVE WORKAROUND_LENOVO ZFSBOOT_PARTITION_SCHEME"
#
# Strings that should be moved to an i18n file and loaded with f_include_lang()
#
hline_arrows_tab_enter="Press arrows, TAB or ENTER"
hline_arrows_tab_space_enter="Press arrows, TAB, SPACE or ENTER"
msg_abort="Abort"
msg_an_installation_step_has_been_aborted="An installation step has been aborted. Would you like\nto restart the installation or exit the installer?"
msg_auto_ufs="Auto (UFS)"
msg_auto_ufs_desc="Guided UFS Disk Setup"
msg_auto_ufs_help="Menu options help choose which disk to setup using UFS and standard partitions"
msg_auto_zfs="Auto (ZFS)"
msg_auto_zfs_desc="Guided Root-on-ZFS"
msg_auto_zfs_help="To use ZFS with less than 8GB RAM, see https://wiki.freebsd.org/ZFSTuningGuide"
msg_exit="Exit"
msg_freebsd_installer="$OSNAME Installer"
msg_gpt_active_fix="Your hardware is known to have issues booting in CSM/Legacy/BIOS mode from GPT partitions that are not set active. Would you like the installer to apply this workaround for you?"
msg_lenovo_fix="Your model of Lenovo is known to have a BIOS bug that prevents it booting from GPT partitions without UEFI. Would you like the installer to apply a workaround for you?"
msg_manual="Manual"
msg_manual_desc="Manual Disk Setup (experts)"
msg_manual_help="Create customized partitions from menu options"
msg_no="NO"
msg_restart="Restart"
msg_shell="Shell"
msg_shell_desc="Open a shell and partition by hand"
msg_shell_help="Create customized partitions using command-line utilities"
msg_yes="YES"
############################################################ FUNCTIONS
# error [$msg]
#
# Display generic error message when a script fails. An optional message
# argument can precede the generic message. User is given the choice of
# restarting the installer or exiting.
#
error()
{
local title="$msg_abort"
local btitle="$msg_freebsd_installer"
local prompt="${1:+$1\n\n}$msg_an_installation_step_has_been_aborted"
local hline="$hline_arrows_tab_space_enter"
[ -f "$PATH_FSTAB" ] && bsdinstall umount
local height width
f_dialog_buttonbox_size height width \
"$title" "$btitle" "$prompt" "$hline"
if $DIALOG \
--title "$title" \
--backtitle "$btitle" \
--hline "$hline" \
--no-label "$msg_exit" \
--yes-label "$msg_restart" \
--yesno "$prompt" $height $width
then
environment_restore
exec $0
# NOTREACHED
fi
exit 1
}
# dialog_workaround
#
# Ask the user if they wish to apply a workaround
#
dialog_workaround()
{
local passed_msg="$1"
local title="$DIALOG_TITLE"
local btitle="$DIALOG_BACKTITLE"
local prompt # Calculated below
local hline="$hline_arrows_tab_enter"
local height=8 width=50 prefix=" "
local plen=${#prefix} list= line=
local max_width=$(( $width - 3 - $plen ))
local yes no defaultno extra_args format
if [ "$USE_XDIALOG" ]; then
yes=ok no=cancel defaultno=default-no
extra_args="--wrap --left"
format="$passed_msg"
else
yes=yes no=no defaultno=defaultno
extra_args="--cr-wrap"
format="$passed_msg"
fi
# Add height for Xdialog(1)
[ "$USE_XDIALOG" ] && height=$(( $height + $height / 5 + 3 ))
prompt=$( printf "$format" )
f_dprintf "%s: Workaround prompt" "$0"
$DIALOG \
--title "$title" \
--backtitle "$btitle" \
--hline "$hline" \
--$yes-label "$msg_yes" \
--$no-label "$msg_no" \
$extra_args \
--yesno "$prompt" $height $width
}
# environment_restore
#
# Restore a list of environment variables when this script is restarted.
#
environment_restore()
{
for var in $user_env_vars; do
eval "if [ -n \"\${ORIG_$var}\" -o -z \"\${ORIG_$var-z}\" ]; then $var=\${ORIG_$var}; else unset $var; fi"
done
}
# environment_save
#
# Save any user-defined environment variable that may be modified during the
# installation process. They are then restored when restarting this script.
#
environment_save()
{
for var in $user_env_vars; do
eval "if [ -n \"\${$var}\" -o -z \"\${$var-z}\" ]; then ORIG_$var=\${$var}; else unset ORIG_$var; fi"
done
}
############################################################ MAIN
f_dprintf "Began Installation at %s" "$( date )"
environment_save
rm -rf $BSDINSTALL_TMPETC
mkdir $BSDINSTALL_TMPETC
# Reset the ESP list
: > ${TMPDIR:-"/tmp"}/bsdinstall-esps
# With pkgbase, pkg OOM has been observed with QEMU-default 128 MiB memory size.
# Ensure we have at least about 256 MiB (with an allowance for rounding etc.).
physmem=$(($(sysctl -n hw.physmem) / 1048576))
if [ $physmem -lt 200 ]; then
bsddialog --backtitle "$OSNAME Installer" --title "Warning" \
--msgbox "Insufficient physical memory (${physmem} MiB) detected. At least 256 MiB is recommended. The installer or installed system may not function correctly." 0 0
fi
[ -f /usr/libexec/bsdinstall/local.pre-everything ] && f_dprintf "Running local.pre-everything" && sh /usr/libexec/bsdinstall/local.pre-everything "$BSDINSTALL_CHROOT"
trap true SIGINT # This section is optional
[ -z "$BSDINSTALL_SKIP_KEYMAP" ] && bsdinstall keymap
trap error SIGINT # Catch cntrl-C here
if [ -z "$BSDINSTALL_SKIP_HOSTNAME" ]; then bsdinstall hostname || error "Set hostname failed"; fi
if [ -f /usr/freebsd-packages/repos/FreeBSD-base-offline.conf ]; then
HAVE_BASE_PACKAGES=yes
PKGBASE_DEFAULT_BUTTON=--default-no
else
unset HAVE_BASE_PACKAGES
unset PKGBASE_DEFAULT_BUTTON
fi
if [ ! -f $BSDINSTALL_DISTDIR/MANIFEST ]; then
PKGBASE=yes
else
bsddialog --backtitle "$OSNAME Installer" --title "Select Installation Type" \
--yes-label "Distribution Sets" --no-label "Packages" --yesno \
$PKGBASE_DEFAULT_BUTTON \
"Would you like to install the base system using legacy distribution sets or packages?" 0 0
if [ $? -eq 1 ]; then
PKGBASE=yes
fi
fi
if [ "$PKGBASE" == yes ]; then
if [ "$HAVE_BASE_PACKAGES" == yes ]; then
bsddialog --backtitle "$OSNAME Installer" --title "Network or Offline Installation" \
--yes-label "Network" --no-label "Offline (Limited Packages)" --yesno \
"Would you like to fetch packages from the internet or use the limited set of packages included in this installation media?" 0 0
if [ $? -eq 1 ]; then
export BSDINSTALL_PKG_REPOS_DIR=/usr/freebsd-packages/repos/
else
bsdinstall netconfig || error
NETCONFIG_DONE=yes
fi
else
bsddialog --backtitle "$OSNAME Installer" --title "Network Installation" \
--msgbox "No base system packages are included in this installation media. The next few screens will allow you to configure networking." 0 0
bsdinstall netconfig || error
NETCONFIG_DONE=yes
fi
else
export DISTRIBUTIONS="${DISTRIBUTIONS:-base.txz kernel.txz}"
if [ -f $BSDINSTALL_DISTDIR/MANIFEST ]; then
DISTMENU=`awk -F'\t' '!/^(kernel\.txz|base\.txz)/{print $1,$5,$6}' $BSDINSTALL_DISTDIR/MANIFEST`
DISTMENU="$(echo ${DISTMENU} | sed -E 's/\.txz//g')"
if [ -n "$DISTMENU" ]; then
exec 5>&1
EXTRA_DISTS=$( eval bsddialog \
--backtitle \"$OSNAME Installer\" \
--title \"Distribution Select\" --nocancel --separate-output \
--checklist \"Choose optional system components to install:\" \
0 0 0 $DISTMENU \
2>&1 1>&5 )
for dist in $EXTRA_DISTS; do
export DISTRIBUTIONS="$DISTRIBUTIONS $dist.txz"
done
fi
fi
FETCH_DISTRIBUTIONS=""
for dist in $DISTRIBUTIONS; do
if [ ! -f $BSDINSTALL_DISTDIR/$dist ]; then
FETCH_DISTRIBUTIONS="$FETCH_DISTRIBUTIONS $dist"
fi
done
if [ -n "$FETCH_DISTRIBUTIONS" -a -n "$BSDINSTALL_CONFIGCURRENT" ]; then
bsddialog --backtitle "$OSNAME Installer" --title "Network Installation" --msgbox "Some installation files were not found on the boot volume. The next few screens will allow you to configure networking so that they can be downloaded from the Internet." 0 0
bsdinstall netconfig || error
NETCONFIG_DONE=yes
fi
fi
rm -f $PATH_FSTAB
touch $PATH_FSTAB
[ -f /usr/libexec/bsdinstall/local.pre-partition ] && f_dprintf "Running local.pre-partition" && sh /usr/libexec/bsdinstall/local.pre-partition "$BSDINSTALL_CHROOT"
#
# Try to detect known broken platforms and apply their workarounds
#
if f_interactive; then
sys_maker=$( kenv -q smbios.system.maker )
f_dprintf "smbios.system.maker=[%s]" "$sys_maker"
sys_model=$( kenv -q smbios.system.product )
f_dprintf "smbios.system.product=[%s]" "$sys_model"
sys_version=$( kenv -q smbios.system.version )
f_dprintf "smbios.system.version=[%s]" "$sys_version"
sys_mb_maker=$( kenv -q smbios.planar.maker )
f_dprintf "smbios.planar.maker=[%s]" "$sys_mb_maker"
sys_mb_product=$( kenv -q smbios.planar.product )
f_dprintf "smbios.planar.product=[%s]" "$sys_mb_product"
#
# Laptop Models
#
case "$sys_maker" in
"LENOVO")
case "$sys_version" in
"ThinkPad X220"|"ThinkPad T420"|"ThinkPad T520"|"ThinkPad W520"|"ThinkPad X1")
dialog_workaround "$msg_lenovo_fix"
retval=$?
f_dprintf "lenovofix_prompt=[%s]" "$retval"
if [ $retval -eq $DIALOG_OK ]; then
export ZFSBOOT_PARTITION_SCHEME="GPT + Lenovo Fix"
export WORKAROUND_LENOVO=1
fi
;;
esac
;;
"Dell Inc.")
case "$sys_model" in
"Latitude E6330"|"Latitude E7440"|"Latitude E7240"|"Precision Tower 5810")
dialog_workaround "$msg_gpt_active_fix"
retval=$?
f_dprintf "gpt_active_fix_prompt=[%s]" "$retval"
if [ $retval -eq $DIALOG_OK ]; then
export ZFSBOOT_PARTITION_SCHEME="GPT + Active"
export WORKAROUND_GPTACTIVE=1
fi
;;
esac
;;
"Hewlett-Packard")
case "$sys_model" in
"HP ProBook 4330s")
dialog_workaround "$msg_gpt_active_fix"
retval=$?
f_dprintf "gpt_active_fix_prompt=[%s]" "$retval"
if [ $retval -eq $DIALOG_OK ]; then
export ZFSBOOT_PARTITION_SCHEME="GPT + Active"
export WORKAROUND_GPTACTIVE=1
fi
;;
esac
;;
esac
#
# Motherboard Models
#
case "$sys_mb_maker" in
"Intel Corporation")
case "$sys_mb_product" in
"DP965LT"|"D510MO")
dialog_workaround "$msg_gpt_active_fix"
retval=$?
f_dprintf "gpt_active_fix_prompt=[%s]" "$retval"
if [ $retval -eq $DIALOG_OK ]; then
export ZFSBOOT_PARTITION_SCHEME="GPT + Active"
export WORKAROUND_GPTACTIVE=1
fi
;;
esac
;;
"Acer")
case "$sys_mb_product" in
"Veriton M6630G")
dialog_workaround "$msg_gpt_active_fix"
retval=$?
f_dprintf "gpt_active_fix_prompt=[%s]" "$retval"
if [ $retval -eq $DIALOG_OK ]; then
export ZFSBOOT_PARTITION_SCHEME="GPT + Active"
export WORKAROUND_GPTACTIVE=1
fi
;;
esac
;;
esac
fi
PMODES="
'$msg_auto_ufs' '$msg_auto_ufs_desc' '$msg_auto_ufs_help'
'$msg_manual' '$msg_manual_desc' '$msg_manual_help'
'$msg_shell' '$msg_shell_desc' '$msg_shell_help'
" # END-QUOTE
CURARCH=$( uname -m )
case $CURARCH in
amd64|arm64|i386|riscv) # Booting ZFS Supported
PMODES="
'$msg_auto_zfs' '$msg_auto_zfs_desc' '$msg_auto_zfs_help'
$PMODES
" # END-QUOTE
;;
*) # Booting ZFS Unsupported
;;
esac
exec 5>&1
PARTMODE=`echo $PMODES | xargs -o bsddialog --backtitle "$OSNAME Installer" \
--title "Partitioning" \
--item-help \
--menu "How would you like to partition your disk?" \
0 0 0 2>&1 1>&5` || exit 1
exec 5>&-
case "$PARTMODE" in
"$msg_auto_zfs") # ZFS
bsdinstall zfsboot || error "ZFS setup failed"
bsdinstall mount || error "Failed to mount filesystem"
;;
"$msg_auto_ufs") # Guided UFS
bsdinstall autopart || error "Partitioning error"
bsdinstall mount || error "Failed to mount filesystem"
;;
"$msg_shell") # Shell
clear
echo "Use this shell to set up partitions for the new system. When finished, mount the system at $BSDINSTALL_CHROOT and place an fstab file for the new system at $PATH_FSTAB. Then type 'exit'. You can also enter the partition editor at any time by entering 'bsdinstall partedit'."
sh 2>&1
bsdinstall mount_aux || error "Failed to mount auxiliary filesystems"
;;
"$msg_manual") # Manual
if f_isset debugFile; then
# Give partedit the path to our logfile so it can append
BSDINSTALL_LOG="${debugFile#+}" bsdinstall partedit || error "Partitioning error"
else
bsdinstall partedit || error "Partitioning error"
fi
bsdinstall mount || error "Failed to mount filesystem"
;;
*)
error "Unknown partitioning mode"
;;
esac
[ -f /usr/libexec/bsdinstall/local.pre-fetch ] && f_dprintf "Running local.pre-fetch" && sh /usr/libexec/bsdinstall/local.pre-fetch "$BSDINSTALL_CHROOT"
if [ "$PKGBASE" == yes ]; then
bsdinstall pkgbase || error "Installation of base system packages failed"
else
if [ -n "$FETCH_DISTRIBUTIONS" ]; then
exec 5>&1
export BSDINSTALL_DISTDIR=$(`dirname $0`/fetchmissingdists 2>&1 1>&5)
FETCH_RESULT=$?
exec 5>&-
[ $FETCH_RESULT -ne 0 ] && error "Could not fetch remote distributions"
fi
bsdinstall checksum || error "Distribution checksum failed"
bsdinstall distextract || error "Distribution extract failed"
fi
# Set up boot loader
bsdinstall bootconfig || error "Failed to configure bootloader"
[ -f /usr/libexec/bsdinstall/local.pre-configure ] && f_dprintf "Running local.pre-configure" && sh /usr/libexec/bsdinstall/local.pre-configure "$BSDINSTALL_CHROOT"
bsdinstall rootpass || error "Could not set root password"
trap true SIGINT # This section is optional
if [ "$NETCONFIG_DONE" != yes ]; then
bsdinstall netconfig # Don't check for errors -- the user may cancel
fi
[ -z "$BSDINSTALL_SKIP_TIME" ] && bsdinstall time
[ -z "$BSDINSTALL_SKIP_SERVICES" ] && bsdinstall services
[ -z "$BSDINSTALL_SKIP_HARDENING" ] && bsdinstall hardening
[ -z "$BSDINSTALL_SKIP_FIRMWARE" ] && bsdinstall firmware
[ -z "$BSDINSTALL_SKIP_USERS" ] && bsddialog --backtitle "$OSNAME Installer" \
--title "Add User Accounts" --yesno \
"Would you like to add users to the installed system now?" 0 0 && \
bsdinstall adduser
# Allow user to change his mind
[ -z "$BSDINSTALL_SKIP_FINALCONFIG" ] && bsdinstall finalconfig
trap error SIGINT # SIGINT is bad again
bsdinstall config || error "Failed to save config"
if [ ! -z "$BSDINSTALL_FETCHDEST" ]; then
rm -rf "$BSDINSTALL_FETCHDEST"
fi
[ -f /usr/libexec/bsdinstall/local.post-configure ] && f_dprintf "Running local.post-configure" && sh /usr/libexec/bsdinstall/local.post-configure "$BSDINSTALL_CHROOT"
if [ -z "$BSDINSTALL_SKIP_MANUAL" ]; then
bsddialog --backtitle "$OSNAME Installer" --title "Manual Configuration" \
--default-no --yesno \
"The installation is now finished. Before exiting the installer, would you like to open a shell in the new system to make any final manual modifications?" 0 0
if [ $? -eq 0 ]; then
clear
echo This shell is operating in a chroot in the new system. \
When finished making configuration changes, type \"exit\".
chroot "$BSDINSTALL_CHROOT" /bin/sh -l 2>&1
fi
fi
bsdinstall entropy
bsdinstall umount
f_dprintf "Installation Completed at %s" "$( date )"
################################################################################
# END
################################################################################