xref: /freebsd/usr.sbin/bsdinstall/scripts/jail (revision 9de72af2cceb6fc4aead0990cccdf565531bc248)
1#!/bin/sh
2#-
3# Copyright (c) 2011 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
33
34############################################################ GLOBALS
35
36#
37# List of environment variables that may be defined by the user, but modified
38# during the installation process. They are then restored when restarting this
39# script.
40#
41user_env_vars="BSDINSTALL_DISTSITE DISTRIBUTIONS"
42
43############################################################ FUNCTIONS
44
45# error [$msg]
46#
47# Display generic error message when a script fails. An optional message
48# argument can preceed the generic message. User is given the choice of
49# restarting the installer or exiting.
50#
51error() {
52	local msg
53	if [ -n "$1" ]; then
54		msg="$1\n\n"
55	fi
56	bsddialog --backtitle "$OSNAME Installer" --title "Abort" \
57	    --no-label "Exit" --yes-label "Restart" --yesno \
58	    "${msg}An installation step has been aborted. Would you like to restart the installation or exit the installer?" 0 0
59	if [ $? -ne $BSDDIALOG_OK ]; then
60		exit
61	else
62		environment_restore
63		exec $0 $BSDINSTALL_CHROOT
64	fi
65}
66
67distbase() {
68	test ! -d $BSDINSTALL_DISTDIR && mkdir -p $BSDINSTALL_DISTDIR
69
70	if [ ! -f $BSDINSTALL_DISTDIR/MANIFEST -a -z "$BSDINSTALL_DISTSITE" ]; then
71		exec 5>&1
72		BSDINSTALL_DISTSITE=$(`dirname $0`/mirrorselect 2>&1 1>&5)
73		MIRROR_BUTTON=$?
74		exec 5>&-
75		test $MIRROR_BUTTON -eq 0 || error "No mirror selected"
76		export BSDINSTALL_DISTSITE
77		fetch -o $BSDINSTALL_DISTDIR/MANIFEST $BSDINSTALL_DISTSITE/MANIFEST || error "Could not download $BSDINSTALL_DISTSITE/MANIFEST"
78	fi
79
80	: ${DISTRIBUTIONS="base.txz"}; export DISTRIBUTIONS
81	if [ -f $BSDINSTALL_DISTDIR/MANIFEST ]; then
82		DISTMENU=`cut -f 4,5,6 $BSDINSTALL_DISTDIR/MANIFEST | grep -v -e ^kernel -e ^base`
83
84		if [ ! "$nonInteractive" == "YES" ]
85		then
86			exec 5>&1
87			EXTRA_DISTS=$(echo $DISTMENU | xargs -o bsddialog \
88			    --backtitle "$OSNAME Installer" \
89			    --title "Distribution Select" --no-cancel \
90			    --separate-output \
91			    --checklist "Choose optional system components to install:" \
92			    0 0 0 \
93			    2>&1 1>&5)
94			for dist in $EXTRA_DISTS; do
95				export DISTRIBUTIONS="$DISTRIBUTIONS $dist.txz"
96			done
97		fi
98	fi
99
100	FETCH_DISTRIBUTIONS=""
101	for dist in $DISTRIBUTIONS; do
102		if [ ! -f $BSDINSTALL_DISTDIR/$dist ]; then
103			FETCH_DISTRIBUTIONS="$FETCH_DISTRIBUTIONS $dist"
104		fi
105	done
106	FETCH_DISTRIBUTIONS=`echo $FETCH_DISTRIBUTIONS`	# Trim white space
107
108	if [ -n "$FETCH_DISTRIBUTIONS" -a -z "$BSDINSTALL_DISTSITE" ]; then
109		exec 5>&1
110		BSDINSTALL_DISTSITE=$(`dirname $0`/mirrorselect 2>&1 1>&5)
111		MIRROR_BUTTON=$?
112		exec 5>&-
113		test $MIRROR_BUTTON -eq 0 || error "No mirror selected"
114		export BSDINSTALL_DISTSITE
115	fi
116
117	if [ ! -z "$FETCH_DISTRIBUTIONS" ]; then
118		bsdinstall distfetch || error "Failed to fetch distribution"
119	fi
120
121	bsdinstall checksum || error "Distribution checksum failed"
122	bsdinstall distextract || error "Distribution extract failed"
123}
124
125# environment_restore
126#
127# Restore a list of environment variables when this script is restarted.
128#
129environment_restore()
130{
131	for var in $user_env_vars; do
132		eval "if [ -n \"\${ORIG_$var}\" -o -z \"\${ORIG_$var-z}\" ]; then $var=\${ORIG_$var}; else unset $var; fi"
133	done
134}
135
136# environment_save
137#
138# Save any user-defined environment variable that may be modified during the
139# installation process. They are then restored when restarting this script.
140#
141environment_save()
142{
143	for var in $user_env_vars; do
144		eval "if [ -n \"\${$var}\" -o -z \"\${$var-z}\" ]; then ORIG_$var=\${$var}; else unset ORIG_$var; fi"
145	done
146}
147
148############################################################ MAIN
149
150: ${BSDDIALOG_OK=0}
151
152f_dprintf "Began Installation at %s" "$( date )"
153
154if [ -z "$1" ]; then
155	error "Directory can not be empty\n\nUsage:\nbsdinstall jail directory"
156fi
157export BSDINSTALL_CHROOT=$1
158
159environment_save
160
161rm -rf $BSDINSTALL_TMPETC
162mkdir $BSDINSTALL_TMPETC
163mkdir -p $1 || error "mkdir failed for $1"
164rm -f $TMPDIR/bsdinstall-installscript-setup
165
166if [ -n "$SCRIPT" ]; then
167	# split script into preamble and setup script at first shebang
168	awk 'BEGIN {pathb=ARGV[2]; ARGV[2]=""} /^#!/{b=1} {
169	    if (b) print >pathb; else print}' \
170	    "$SCRIPT" $TMPDIR/bsdinstall-installscript-setup \
171	    >$TMPDIR/bsdinstall-installscript-preamble
172
173	. $TMPDIR/bsdinstall-installscript-preamble
174fi
175
176if [ ! "$nonInteractive" == "YES" ]; then
177	bsddialog --backtitle "$OSNAME Installer" --title "Select Installation Type" \
178	    --yes-label "Traditional" --no-label "Packages (Experimental)" --yesno \
179	    "Would you like to install the base system using traditional distribution sets or packages (experimental)?" 0 0
180	if [ $? -eq 1 ]; then
181		PKGBASE=yes
182	fi
183fi
184
185if [ "$PKGBASE" == yes ]; then
186	bsdinstall pkgbase --no-kernel || error "Installation of base system packages failed"
187else
188	distbase
189fi
190
191if [ ! "$nonInteractive" == "YES" ]
192then
193    bsdinstall rootpass || error "Could not set root password"
194fi
195
196trap true SIGINT	# This section is optional
197
198if [ ! "$nonInteractive" == "YES" ]
199then
200bsdinstall services
201
202    bsddialog --backtitle "$OSNAME Installer" --title "Add User Accounts" --yesno \
203        "Would you like to add users to the installed system now?" 0 0 && \
204        bsdinstall adduser
205fi
206
207trap error SIGINT	# SIGINT is bad again
208bsdinstall config  || error "Failed to save config"
209cp /etc/resolv.conf $1/etc
210cp /etc/localtime $1/etc
211cp /var/db/zoneinfo $1/var/db
212
213# Run post-install script
214if [ -f $TMPDIR/bsdinstall-installscript-setup ]; then
215	cp $TMPDIR/bsdinstall-installscript-setup \
216	    $BSDINSTALL_CHROOT/tmp/installscript
217	chmod a+x $BSDINSTALL_CHROOT/tmp/installscript
218	mount -t devfs devfs "$BSDINSTALL_CHROOT/dev"
219	chroot $BSDINSTALL_CHROOT /tmp/installscript $@ 2>&1
220	umount "$BSDINSTALL_CHROOT/dev"
221	rm $BSDINSTALL_CHROOT/tmp/installscript
222fi
223
224bsdinstall entropy
225
226f_dprintf "Installation Completed at %s" "$(date)"
227exit $SUCCESS
228
229################################################################################
230# END
231################################################################################
232