Skip to content

Commit 5393e27

Browse files
Merge branch 'BastilleBSD:master' into fix-cmd-template
2 parents 7169931 + 7d3ca7b commit 5393e27

File tree

8 files changed

+79
-37
lines changed

8 files changed

+79
-37
lines changed

docs/chapters/subcommands/destroy.rst

+8
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,11 @@ created. Note: containers must be stopped before destroyed.
1717
Deleting Container: folsom.
1818
Note: containers console logs not destroyed.
1919
/usr/local/bastille/logs/folsom_console.log
20+
21+
Release can be destroyed provided there are no child jails. The `force` option
22+
deletes the release cache directory as well:
23+
24+
.. code-block:: shell
25+
26+
ishmael ~ # bastille destroy force 14.0-RELEASE
27+
Deleting base: 14.0-RELEASE

docs/chapters/upgrading.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ After upgrading all jails from one release to the next you may find that you now
3838

3939
`bastille list releases` to list all bootstrapped releases.
4040

41-
`bastille destroy X.Y-RELEASE` to fully delete the release.
41+
`bastille destroy X.Y-RELEASE` to fully delete the release.
42+
43+
`bastille destroy force X.Y-RELEASE` to delete the cache directory as well.

usr/local/share/bastille/clone.sh

+23-19
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
. /usr/local/etc/bastille/bastille.conf
3333

3434
usage() {
35-
error_exit "Usage: bastille clone [TARGET] [NEW_NAME] [IPADRESS]"
35+
error_exit "Usage: bastille clone TARGET NEW_NAME IPADDRESS"
3636
}
3737

3838
# Handle special-case commands first
@@ -87,7 +87,7 @@ update_jailconf() {
8787
JAIL_CONFIG="${bastille_jailsdir}/${NEWNAME}/jail.conf"
8888
if [ -f "${JAIL_CONFIG}" ]; then
8989
if ! grep -qw "path = ${bastille_jailsdir}/${NEWNAME}/root;" "${JAIL_CONFIG}"; then
90-
sed -i '' "s|host.hostname = ${TARGET};|host.hostname = ${NEWNAME};|" "${JAIL_CONFIG}"
90+
sed -i '' "s|host.hostname = ${TARGET};|host.hostname = ${NEWNAME};|" "${JAIL_CONFIG}"
9191
sed -i '' "s|exec.consolelog = .*;|exec.consolelog = ${bastille_logsdir}/${NEWNAME}_console.log;|" "${JAIL_CONFIG}"
9292
sed -i '' "s|path = .*;|path = ${bastille_jailsdir}/${NEWNAME}/root;|" "${JAIL_CONFIG}"
9393
sed -i '' "s|mount.fstab = .*;|mount.fstab = ${bastille_jailsdir}/${NEWNAME}/fstab;|" "${JAIL_CONFIG}"
@@ -111,20 +111,33 @@ update_jailconf_vnet() {
111111
for _num in $(seq 0 "${num_range}"); do
112112
if [ -n "${jail_list}" ]; then
113113
if ! grep -q "e0b_bastille${_num}" "${bastille_jailsdir}"/*/jail.conf; then
114-
uniq_epair="bastille${_num}"
115-
# Update the exec.* with uniq_epair when cloning jails.
116-
sed -i '' "s|vnet.interface = e0b_bastille.*;|vnet.interface = e0b_${uniq_epair};|" "${JAIL_CONFIG}"
117-
sed -i '' "s|exec.prestart += \"jib addm bastille[0-9]|exec.prestart += \"jib addm ${uniq_epair}|" "${JAIL_CONFIG}"
118-
sed -i '' "s|exec.prestart += \"ifconfig e0a_bastille[0-9].*|exec.prestart += \"ifconfig e0a_${uniq_epair} description \\\\\"vnet host interface for Bastille jail ${NEWNAME}\\\\\"\";|" "${JAIL_CONFIG}"
119-
sed -i '' "s|exec.poststop += \"jib destroy bastille[0-9]\";|exec.poststop += \"jib destroy ${uniq_epair}\";|" "${JAIL_CONFIG}"
120-
break
114+
if ! grep -q "epair${_num}" "${bastille_jailsdir}"/*/jail.conf; then
115+
local uniq_epair="bastille${_num}"
116+
local uniq_epair_bridge="${_num}"
117+
# since we don't have access to the external_interface variable, we cat the jail.conf file to retrieve the mac prefix
118+
# we also do not use the main generate_static_mac function here
119+
local macaddr_prefix="$(cat ${JAIL_CONFIG} | grep -m 1 ether | grep -oE '([0-9a-f]{2}(:[0-9a-f]{2}){5})' | awk -F: '{print $1":"$2":"$3}')"
120+
local macaddr_suffix="$(echo -n ${NEWNAME} | sha256 | cut -b -5 | sed 's/\([0-9a-fA-F][0-9a-fA-F]\)\([0-9a-fA-F][0-9a-fA-F]\)\([0-9a-fA-F]\)/\1:\2:\3/')"
121+
local macaddr="${macaddr_prefix}:${macaddr_suffix}"
122+
# Update the exec.* with uniq_epair when cloning jails.
123+
# for VNET jails
124+
sed -i '' "s|bastille\([0-9]\{1,\}\)|${uniq_epair}|g" "${JAIL_CONFIG}"
125+
sed -i '' "s|e\([0-9]\{1,\}\)a_${NEWNAME}|e${uniq_epair_bridge}a_${NEWNAME}|g" "${JAIL_CONFIG}"
126+
sed -i '' "s|e\([0-9]\{1,\}\)b_${NEWNAME}|e${uniq_epair_bridge}b_${NEWNAME}|g" "${JAIL_CONFIG}"
127+
sed -i '' "s|epair\([0-9]\{1,\}\)|epair${uniq_epair_bridge}|g" "${JAIL_CONFIG}"
128+
sed -i '' "s|exec.prestart += \"ifconfig e0a_bastille\([0-9]\{1,\}\).*description.*|exec.prestart += \"ifconfig e0a_${uniq_epair} description \\\\\"vnet host interface for Bastille jail ${NEWNAME}\\\\\"\";|" "${JAIL_CONFIG}"
129+
sed -i '' "s|ether.*:.*:.*:.*:.*:.*a |ether ${macaddr}a |" "${JAIL_CONFIG}"
130+
sed -i '' "s|ether.*:.*:.*:.*:.*:.*b |ether ${macaddr}b |" "${JAIL_CONFIG}"
131+
break
132+
fi
121133
fi
122134
fi
123135
done
124136

125137
# Rename interface to new uniq_epair
126138
sed -i '' "s|ifconfig_e0b_bastille.*_name|ifconfig_e0b_${uniq_epair}_name|" "${bastille_jail_rc_conf}"
127-
139+
sed -i '' "s|ifconfig_e.*b_${TARGET}_name|ifconfig_e${uniq_epair_bridge}b_${NEWNAME}_name|" "${bastille_jail_rc_conf}"
140+
128141
# If 0.0.0.0 set DHCP, else set static IP address
129142
if [ "${IP}" = "0.0.0.0" ]; then
130143
sysrc -f "${bastille_jail_rc_conf}" ifconfig_vnet0="SYNCDHCP"
@@ -137,15 +150,6 @@ update_fstab() {
137150
# Update fstab to use the new name
138151
FSTAB_CONFIG="${bastille_jailsdir}/${NEWNAME}/fstab"
139152
if [ -f "${FSTAB_CONFIG}" ]; then
140-
FSTAB_RELEASE=$(grep -owE '([1-9]{2,2})\.[0-9](-RELEASE|-RELEASE-i386|-RC[1-9]|-BETA[1-9]|-CURRENT)|([0-9]{1,2}(-stable-build-[0-9]{1,3}|-stable-LAST))|(current-build)-([0-9]{1,3})|(current-BUILD-LATEST)|([0-9]{1,2}-stable-BUILD-LATEST)' "${FSTAB_CONFIG}" | uniq)
141-
FSTAB_CURRENT=$(grep -w ".*/releases/.*/jails/${TARGET}/root/.bastille" "${FSTAB_CONFIG}")
142-
FSTAB_NEWCONF="${bastille_releasesdir}/${FSTAB_RELEASE} ${bastille_jailsdir}/${NEWNAME}/root/.bastille nullfs ro 0 0"
143-
if [ -n "${FSTAB_CURRENT}" ] && [ -n "${FSTAB_NEWCONF}" ]; then
144-
# If both variables are set, update as needed
145-
if ! grep -qw "${bastille_releasesdir}/${FSTAB_RELEASE}.*${bastille_jailsdir}/${NEWNAME}/root/.bastille" "${FSTAB_CONFIG}"; then
146-
sed -i '' "s|${FSTAB_CURRENT}|${FSTAB_NEWCONF}|" "${FSTAB_CONFIG}"
147-
fi
148-
fi
149153
# Update additional fstab paths with new jail path
150154
sed -i '' "s|${bastille_jailsdir}/${TARGET}/root/|${bastille_jailsdir}/${NEWNAME}/root/|" "${FSTAB_CONFIG}"
151155
fi

usr/local/share/bastille/common.sh

+14-1
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,19 @@ warn() {
7070
echo -e "${COLOR_YELLOW}$*${COLOR_RESET}"
7171
}
7272

73+
generate_static_mac() {
74+
local jail_name="${1}"
75+
local external_interface="${2}"
76+
local macaddr_prefix="$(ifconfig ${external_interface} | grep ether | awk '{print $2}' | cut -d':' -f1-3)"
77+
local macaddr_suffix="$(echo -n ${jail_name} | sha256 | cut -b -5 | sed 's/\([0-9a-fA-F][0-9a-fA-F]\)\([0-9a-fA-F][0-9a-fA-F]\)\([0-9a-fA-F]\)/\1:\2:\3/')"
78+
macaddr="${macaddr_prefix}:${macaddr_suffix}"
79+
}
80+
7381
generate_vnet_jail_netblock() {
7482
local jail_name="$1"
7583
local use_unique_bridge="$2"
7684
local external_interface="$3"
85+
generate_static_mac "${jail_name}" "${external_interface}"
7786
## determine number of containers + 1
7887
## iterate num and grep all jail configs
7988
## define uniq_epair
@@ -98,11 +107,13 @@ generate_vnet_jail_netblock() {
98107
## generate bridge config
99108
cat <<-EOF
100109
vnet;
101-
vnet.interface = "e${uniq_epair_bridge}b_${jail_name}";
110+
vnet.interface = e${uniq_epair_bridge}b_${jail_name};
102111
exec.prestart += "ifconfig epair${uniq_epair_bridge} create";
103112
exec.prestart += "ifconfig ${external_interface} addm epair${uniq_epair_bridge}a";
104113
exec.prestart += "ifconfig epair${uniq_epair_bridge}a up name e${uniq_epair_bridge}a_${jail_name}";
105114
exec.prestart += "ifconfig epair${uniq_epair_bridge}b up name e${uniq_epair_bridge}b_${jail_name}";
115+
exec.prestart += "ifconfig e${uniq_epair_bridge}a_${jail_name} ether ${macaddr}a";
116+
exec.prestart += "ifconfig e${uniq_epair_bridge}b_${jail_name} ether ${macaddr}b";
106117
exec.poststop += "ifconfig ${external_interface} deletem e${uniq_epair_bridge}a_${jail_name}";
107118
exec.poststop += "ifconfig e${uniq_epair_bridge}a_${jail_name} destroy";
108119
EOF
@@ -112,6 +123,8 @@ EOF
112123
vnet;
113124
vnet.interface = e0b_${uniq_epair};
114125
exec.prestart += "jib addm ${uniq_epair} ${external_interface}";
126+
exec.prestart += "ifconfig e0a_${uniq_epair} ether ${macaddr}a";
127+
exec.prestart += "ifconfig e0b_${uniq_epair} ether ${macaddr}b";
115128
exec.prestart += "ifconfig e0a_${uniq_epair} description \"vnet host interface for Bastille jail ${jail_name}\"";
116129
exec.poststop += "jib destroy ${uniq_epair}";
117130
EOF

usr/local/share/bastille/list.sh

+18-3
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,22 @@ list_all(){
150150
JAIL_HOSTNAME=${JAIL_HOSTNAME:-${DEFAULT_VALUE}}
151151
JAIL_RELEASE=${JAIL_RELEASE:-${DEFAULT_VALUE}}
152152
JAIL_PATH=${JAIL_PATH:-${DEFAULT_VALUE}}
153-
printf " ${JAIL_NAME}%*s${JAIL_STATE}%*s${JAIL_IP}%*s${JAIL_PORTS}%*s${JAIL_HOSTNAME}%*s${JAIL_RELEASE}%*s${JAIL_PATH}\n" "$((${MAX_LENGTH_JAIL_NAME} - ${#JAIL_NAME} + ${SPACER}))" "" "$((5 - ${#JAIL_STATE} + ${SPACER}))" "" "$((${MAX_LENGTH_JAIL_IP} - ${#JAIL_IP} + ${SPACER}))" "" "$((${MAX_LENGTH_JAIL_PORTS} - ${#JAIL_PORTS} + ${SPACER}))" "" "$((${MAX_LENGTH_JAIL_HOSTNAME} - ${#JAIL_HOSTNAME} + ${SPACER}))" "" "$((${MAX_LENGTH_JAIL_RELEASE} - ${#JAIL_RELEASE} + ${SPACER}))" ""
153+
JAIL_IP_COUNT=$(echo "${JAIL_IP}" | wc -l)
154+
if [ ${JAIL_IP_COUNT} -gt 1 ]; then
155+
# vnet0 has more than one IPs assigned.
156+
# Put each IP in its own line below the jails first address. For instance:
157+
# JID State IP Address Published Ports Hostname Release Path
158+
# foo Up 10.10.10.10 - foo 14.0-RELEASE-p5 /usr/local/bastille/jails/foo/root
159+
# 10.10.10.11
160+
# 10.10.10.12
161+
FIRST_IP="$(echo "${JAIL_IP}" | head -n 1)"
162+
printf " ${JAIL_NAME}%*s${JAIL_STATE}%*s${FIRST_IP}%*s${JAIL_PORTS}%*s${JAIL_HOSTNAME}%*s${JAIL_RELEASE}%*s${JAIL_PATH}\n" "$((${MAX_LENGTH_JAIL_NAME} - ${#JAIL_NAME} + ${SPACER}))" "" "$((5 - ${#JAIL_STATE} + ${SPACER}))" "" "$((${MAX_LENGTH_JAIL_IP} - ${#FIRST_IP} + ${SPACER}))" "" "$((${MAX_LENGTH_JAIL_PORTS} - ${#JAIL_PORTS} + ${SPACER}))" "" "$((${MAX_LENGTH_JAIL_HOSTNAME} - ${#JAIL_HOSTNAME} + ${SPACER}))" "" "$((${MAX_LENGTH_JAIL_RELEASE} - ${#JAIL_RELEASE} + ${SPACER}))" ""
163+
for IP in $(echo "${JAIL_IP}" | tail -n +2); do
164+
printf "%*s %*s${IP}\n" "$((${MAX_LENGTH_JAIL_NAME} + ${SPACER}))" "" "$((5 + ${SPACER}))" ""
165+
done
166+
else
167+
printf " ${JAIL_NAME}%*s${JAIL_STATE}%*s${JAIL_IP}%*s${JAIL_PORTS}%*s${JAIL_HOSTNAME}%*s${JAIL_RELEASE}%*s${JAIL_PATH}\n" "$((${MAX_LENGTH_JAIL_NAME} - ${#JAIL_NAME} + ${SPACER}))" "" "$((5 - ${#JAIL_STATE} + ${SPACER}))" "" "$((${MAX_LENGTH_JAIL_IP} - ${#JAIL_IP} + ${SPACER}))" "" "$((${MAX_LENGTH_JAIL_PORTS} - ${#JAIL_PORTS} + ${SPACER}))" "" "$((${MAX_LENGTH_JAIL_HOSTNAME} - ${#JAIL_HOSTNAME} + ${SPACER}))" "" "$((${MAX_LENGTH_JAIL_RELEASE} - ${#JAIL_RELEASE} + ${SPACER}))" ""
168+
fi
154169
fi
155170
done
156171
else
@@ -167,7 +182,7 @@ list_release(){
167182
REL_LIST="$(ls "${bastille_releasesdir}" | sed "s/\n//g")"
168183
for _REL in ${REL_LIST}; do
169184
if [ -f "${bastille_releasesdir}/${_REL}/root/.profile" ] || [ -d "${bastille_releasesdir}/${_REL}/debootstrap" ]; then
170-
if [ "${2}" = "-p" ] && [ -f "${bastille_releasesdir}/${_REL}/bin/freebsd-version" ]; then
185+
if [ "${1}" = "-p" ] && [ -f "${bastille_releasesdir}/${_REL}/bin/freebsd-version" ]; then
171186
REL_PATCH_LEVEL=$(sed -n "s/^USERLAND_VERSION=\"\(.*\)\"$/\1/p" "${bastille_releasesdir}/${_REL}/bin/freebsd-version" 2> /dev/null)
172187
REL_PATCH_LEVEL=${REL_PATCH_LEVEL:-${_REL}}
173188
echo "${REL_PATCH_LEVEL}"
@@ -214,7 +229,7 @@ if [ $# -gt 0 ]; then
214229
list_all
215230
;;
216231
release|releases)
217-
list_release
232+
list_release "${2}"
218233
;;
219234
template|templates)
220235
list_template

usr/local/share/bastille/rdr.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ fi
115115

116116
# function: load rdr rule via pfctl
117117
load_rdr_rule() {
118-
( pfctl -a "rdr/${JAIL_NAME}" -Psn;
118+
( pfctl -a "rdr/${JAIL_NAME}" -Psn 2>/dev/null;
119119
printf '%s\nrdr pass on $%s inet proto %s to port %s -> %s port %s\n' "$EXT_IF" "${bastille_network_pf_ext_if}" "$1" "$2" "$JAIL_IP" "$3" ) \
120120
| pfctl -a "rdr/${JAIL_NAME}" -f-
121121
if [ -n "$JAIL_IP6" ]; then
122-
( pfctl -a "rdr/${JAIL_NAME}" -Psn;
122+
( pfctl -a "rdr/${JAIL_NAME}" -Psn 2>/dev/null;
123123
printf '%s\nrdr pass on $%s inet proto %s to port %s -> %s port %s\n' "$EXT_IF" "${bastille_network_pf_ext_if}" "$1" "$2" "$JAIL_IP6" "$3" ) \
124124
| pfctl -a "rdr/${JAIL_NAME}" -f-
125125
fi
@@ -130,11 +130,11 @@ load_rdr_log_rule() {
130130
proto=$1;host_port=$2;jail_port=$3;
131131
shift 3;
132132
log=$@
133-
( pfctl -a "rdr/${JAIL_NAME}" -Psn;
133+
( pfctl -a "rdr/${JAIL_NAME}" -Psn 2>/dev/null;
134134
printf '%s\nrdr pass %s on $%s inet proto %s to port %s -> %s port %s\n' "$EXT_IF" "$log" "${bastille_network_pf_ext_if}" "$proto" "$host_port" "$JAIL_IP" "$jail_port" ) \
135135
| pfctl -a "rdr/${JAIL_NAME}" -f-
136136
if [ -n "$JAIL_IP6" ]; then
137-
( pfctl -a "rdr/${JAIL_NAME}" -Psn;
137+
( pfctl -a "rdr/${JAIL_NAME}" -Psn 2>/dev/null;
138138
printf '%s\nrdr pass %s on $%s inet proto %s to port %s -> %s port %s\n' "$EXT_IF" "$log" "${bastille_network_pf_ext_if}" "$proto" "$host_port" "$JAIL_IP6" "$jail_port" ) \
139139
| pfctl -a "rdr/${JAIL_NAME}" -f-
140140
fi

usr/local/share/bastille/start.sh

+5-5
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ for _jail in ${JAILS}; do
7979
fi
8080

8181
## warn if matching configured (but not online) ip4.addr, ignore if there's no ip4.addr entry
82-
ip=$(bastille config "${_jail}" get ip4.addr)
83-
if [ -n "${ip}" ]; then
84-
if ifconfig | grep -wF "${ip}" >/dev/null; then
85-
error_notify "Error: IP address (${ip}) already in use."
82+
_ip4=$(bastille config "${_jail}" get ip4.addr)
83+
if [ "${_ip4}" != "not set" ]; then
84+
if ifconfig | grep -wF "${_ip4}" >/dev/null; then
85+
error_notify "Error: IP address (${_ip4}) already in use."
8686
continue
8787
fi
8888
## add ip4.addr to firewall table
89-
pfctl -q -t "${bastille_network_pf_table}" -T add "${ip}"
89+
pfctl -q -t "${bastille_network_pf_table}" -T add "${_ip4}"
9090
fi
9191

9292
## start the container

usr/local/share/bastille/stop.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ for _jail in ${JAILS}; do
5252
## test if running
5353
if [ "$(/usr/sbin/jls name | awk "/^${_jail}$/")" ]; then
5454
## Capture ip4.addr address while still running
55-
_ip="$(/usr/sbin/jls -j ${_jail} ip4.addr)"
55+
_ip4="$(bastille config ${_jail} get ip4.addr)"
5656

5757
# Check if pfctl is present
58-
if which -s pfctl; then
58+
if [ "${_ip4}" != "not set" ]; then
5959
if [ "$(bastille rdr ${_jail} list)" ]; then
6060
bastille rdr ${_jail} clear
6161
fi
@@ -73,9 +73,9 @@ for _jail in ${JAILS}; do
7373
jail -f "${bastille_jailsdir}/${_jail}/jail.conf" -r "${_jail}"
7474

7575
## remove (captured above) ip4.addr from firewall table
76-
if [ -n "${bastille_network_loopback}" ] && [ ! -z "${_ip}" ]; then
76+
if [ -n "${bastille_network_loopback}" ] && [ "${_ip4}" != "not set" ]; then
7777
if grep -qw "interface.*=.*${bastille_network_loopback}" "${bastille_jailsdir}/${_jail}/jail.conf"; then
78-
pfctl -q -t "${bastille_network_pf_table}" -T delete "${_ip}"
78+
pfctl -q -t "${bastille_network_pf_table}" -T delete "${_ip4}"
7979
fi
8080
fi
8181
fi

0 commit comments

Comments
 (0)