xref: /freebsd/usr.sbin/bsdinstall/scripts/jail (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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############################################################ MAIN
35
36: ${BSDDIALOG_OK=0}
37
38f_dprintf "Began Installation at %s" "$( date )"
39
40error() {
41	local msg
42	if [ -n "$1" ]; then
43		msg="$1\n\n"
44	fi
45	bsddialog --backtitle "$OSNAME Installer" --title "Abort" \
46	    --no-label "Exit" --yes-label "Restart" --yesno \
47	    "${msg}An installation step has been aborted. Would you like to restart the installation or exit the installer?" 0 0
48	if [ $? -ne $BSDDIALOG_OK ]; then
49		exit
50	else
51		[ -z "$MIRROR_BUTTON" ] || unset BSDINSTALL_DISTSITE
52		exec $0 $BSDINSTALL_CHROOT
53	fi
54}
55
56if [ -z "$1" ]; then
57	error "Directory can not be empty\n\nUsage:\nbsdinstall jail directory"
58fi
59export BSDINSTALL_CHROOT=$1
60
61rm -rf $BSDINSTALL_TMPETC
62mkdir $BSDINSTALL_TMPETC
63mkdir -p $1 || error "mkdir failed for $1"
64rm -f $TMPDIR/bsdinstall-installscript-setup
65
66if [ -n "$SCRIPT" ]; then
67	# split script into preamble and setup script at first shebang
68	awk 'BEGIN {pathb=ARGV[2]; ARGV[2]=""} /^#!/{b=1} {
69	    if (b) print >pathb; else print}' \
70	    "$SCRIPT" $TMPDIR/bsdinstall-installscript-setup \
71	    >$TMPDIR/bsdinstall-installscript-preamble
72
73	. $TMPDIR/bsdinstall-installscript-preamble
74fi
75
76test ! -d $BSDINSTALL_DISTDIR && mkdir -p $BSDINSTALL_DISTDIR
77
78if [ ! -f $BSDINSTALL_DISTDIR/MANIFEST -a -z "$BSDINSTALL_DISTSITE" ]; then
79	exec 5>&1
80	BSDINSTALL_DISTSITE=$(`dirname $0`/mirrorselect 2>&1 1>&5)
81	MIRROR_BUTTON=$?
82	exec 5>&-
83	test $MIRROR_BUTTON -eq 0 || error "No mirror selected"
84	export BSDINSTALL_DISTSITE
85	fetch -o $BSDINSTALL_DISTDIR/MANIFEST $BSDINSTALL_DISTSITE/MANIFEST || error "Could not download $BSDINSTALL_DISTSITE/MANIFEST"
86fi
87
88: ${DISTRIBUTIONS="base.txz"}; export DISTRIBUTIONS
89if [ -f $BSDINSTALL_DISTDIR/MANIFEST ]; then
90	DISTMENU=`cut -f 4,5,6 $BSDINSTALL_DISTDIR/MANIFEST | grep -v -e ^kernel -e ^base`
91
92    if [ ! "$nonInteractive" == "YES" ]
93    then
94	    exec 5>&1
95	    EXTRA_DISTS=$(echo $DISTMENU | xargs -o bsddialog \
96	        --backtitle "$OSNAME Installer" \
97	        --title "Distribution Select" --no-cancel --separate-output \
98	        --checklist "Choose optional system components to install:" \
99	        0 0 0 \
100	    2>&1 1>&5)
101	    for dist in $EXTRA_DISTS; do
102	    	export DISTRIBUTIONS="$DISTRIBUTIONS $dist.txz"
103	    done
104    fi
105fi
106
107FETCH_DISTRIBUTIONS=""
108for dist in $DISTRIBUTIONS; do
109	if [ ! -f $BSDINSTALL_DISTDIR/$dist ]; then
110		FETCH_DISTRIBUTIONS="$FETCH_DISTRIBUTIONS $dist"
111	fi
112done
113FETCH_DISTRIBUTIONS=`echo $FETCH_DISTRIBUTIONS`	# Trim white space
114
115if [ -n "$FETCH_DISTRIBUTIONS" -a -z "$BSDINSTALL_DISTSITE" ]; then
116	exec 5>&1
117	BSDINSTALL_DISTSITE=$(`dirname $0`/mirrorselect 2>&1 1>&5)
118	MIRROR_BUTTON=$?
119	exec 5>&-
120	test $MIRROR_BUTTON -eq 0 || error "No mirror selected"
121	export BSDINSTALL_DISTSITE
122fi
123
124if [ ! -z "$FETCH_DISTRIBUTIONS" ]; then
125	bsdinstall distfetch || error "Failed to fetch distribution"
126fi
127
128bsdinstall checksum || error "Distribution checksum failed"
129bsdinstall distextract || error "Distribution extract failed"
130
131if [ ! "$nonInteractive" == "YES" ]
132then
133    bsdinstall rootpass || error "Could not set root password"
134fi
135
136trap true SIGINT	# This section is optional
137
138if [ ! "$nonInteractive" == "YES" ]
139then
140bsdinstall services
141
142    bsddialog --backtitle "$OSNAME Installer" --title "Add User Accounts" --yesno \
143        "Would you like to add users to the installed system now?" 0 0 && \
144        bsdinstall adduser
145fi
146
147trap error SIGINT	# SIGINT is bad again
148bsdinstall config  || error "Failed to save config"
149cp /etc/resolv.conf $1/etc
150cp /etc/localtime $1/etc
151cp /var/db/zoneinfo $1/var/db
152
153# Run post-install script
154if [ -f $TMPDIR/bsdinstall-installscript-setup ]; then
155	cp $TMPDIR/bsdinstall-installscript-setup \
156	    $BSDINSTALL_CHROOT/tmp/installscript
157	chmod a+x $BSDINSTALL_CHROOT/tmp/installscript
158	mount -t devfs devfs "$BSDINSTALL_CHROOT/dev"
159	chroot $BSDINSTALL_CHROOT /tmp/installscript $@ 2>&1
160	umount "$BSDINSTALL_CHROOT/dev"
161	rm $BSDINSTALL_CHROOT/tmp/installscript
162fi
163
164bsdinstall entropy
165
166f_dprintf "Installation Completed at %s" "$(date)"
167exit $SUCCESS
168
169################################################################################
170# END
171################################################################################
172