-
Notifications
You must be signed in to change notification settings - Fork 6
/
create-jail.sh
executable file
·149 lines (127 loc) · 3.95 KB
/
create-jail.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/sh
# ----------------------------------------------------------------------------
# create jail
# ----------------------------------------------------------------------------
set_defaults() {
ZPOOL="tank"
JAILNAME="base"
FREEBSD_VERSION=12
NUMBER_OF_CORES=`sysctl -n hw.ncpu`
PASSWORD=fabrik
USER=devops
}
write() {
echo -e '\e[0;32m'
cat <<-EOF
#----------------------------------------------------------------------------
# $1
#----------------------------------------------------------------------------
EOF
echo -e '\e[0m'
}
usage() {
set_defaults
cat <<-EOF
Example: $(basename "$0") -p=/jails/test <-- zfs create tank/jails/test/tmp (in advance)
$(basename "$0") -n=xxx (jail path will be /fabrik/jail/xxx)
$(basename "$0") -z=tank -n=test (jail path will be <tank>/fabrik/jail/test)
Available pools:
$(zpool list)
Parameters:
-h | --help)
Show this help.
-p | --path)
Jail path
Default: ${JAILPATH}
-n | --name)
jail name
Default: ${JAILNAME}
-z | --zpool)
ZFS pool to use
Default: ${ZPOOL}
EOF
}
parse_args() {
set_defaults
SAFE_DELIMITER="$(printf "\a")"
while [ "$1" != "" ]
do
PARAM=$(echo $1 | cut -f1 -d=)
VALUE=$(echo $1 | sed "s/=/${SAFE_DELIMITER}/" | cut -f2 "-d${SAFE_DELIMITER}")
case $PARAM in
-h | --help)
usage
exit
;;
-p | --path)
JAILPATH="${VALUE}"
;;
-n | --name)
JAILNAME="${VALUE}"
;;
-z | --zpool)
ZPOOL="${VALUE}"
;;
*)
echo "ERROR: Unknown parameter ${PARAM}"
usage
exit 1
esac
shift
done
}
main() {
if [ $# -eq 0 ]
then
usage
exit
fi
parse_args $@
}
main $@
[ -z "${JAILPATH}" ] && JAILPATH="/fabrik/jail/${JAILNAME}"
# ----------------------------------------------------------------------------
# no need to edit below this
# ----------------------------------------------------------------------------
START=$(date +%s)
set -e
# [ ! -d "${JAILPATH}" ] && write "Creating ${JAILPATH#*/}" && zfs create -p ${ZPOOL}${JAILPATH#*/}
[ ! -d "${JAILPATH}" ] && write "Creating ${JAILPATH#*/}" && zfs create -p ${ZPOOL}${JAILPATH}
set +e
write "Checking out and updating sources FreeBSD: ${FREEBSD_VERSION}"
svnlite co svn://svn.freebsd.org/base/stable/${FREEBSD_VERSION} /usr/src
write "Fetching src-jail.conf"
fetch --no-verify-peer -a https://raw.githubusercontent.com/fabrik-red/images/master/src-jail.conf -o /etc/src-jail.conf
write "Creating /fabrik dir"
set +e
zfs create -o mountpoint=/fabrik ${ZPOOL}/fabrik
zfs create ${ZPOOL}/fabrik/jail
zfs create ${ZPOOL}/fabrik/jail/obj
zfs set exec=on ${ZPOOL}/tmp
set -e
write "building jail"
cd /usr/src
env MAKEOBJDIRPREFIX=/fabrik/jail/obj SRCCONF=/etc/src-jail.conf __MAKE_CONF=/etc/make.conf make -DNO_CLEAN -j${NUMBER_OF_CORES} buildworld
write "Installing world, kernel and jail world"
env MAKEOBJDIRPREFIX=/fabrik/jail/obj SRCCONF=/etc/src-jail.conf __MAKE_CONF=/etc/make.conf make DESTDIR=${JAILPATH} installworld 2>&1 | tee /tmp/jail-installworld.log && \
env MAKEOBJDIRPREFIX=/fabrik/jail/obj SRCCONF=/etc/src-jail.conf __MAKE_CONF=/etc/make.conf make DESTDIR=${JAILPATH} distribution 2>&1 | tee /tmp/jail-distribution.log
write "Creating user ${USER} with password ${PASSWORD}"
chroot ${JAILPATH} pw useradd ${USER} -m -G wheel -s /bin/csh -h 0 <<EOP
${PASSWORD}
EOP
# jail rc.conf
cat << EOF > ${JAILPATH}/etc/rc.conf
clear_tmp_enable="YES"
cron_flags="\$cron_flags -J 60"
sendmail_enable="NONE"
sshd_enable="YES"
syslogd_flags="-ssC8"
EOF
# jail /etc/resolv.conf
cat << EOF > ${JAILPATH}/etc/resolv.conf
nameserver 172.16.8.1
EOF
zfs set exec=off ${ZPOOL}/tmp
END=$(date +%s)
DIFF=$(echo "$END - $START" | bc)
write "Done! build in $DIFF seconds."