Skip to content

Commit b4060fe

Browse files
more fixes and improvements
1 parent d8b0a8d commit b4060fe

28 files changed

+223
-149
lines changed

usr/local/share/bastille/clone.sh

+44-17
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,48 @@
3232
. /usr/local/etc/bastille/bastille.conf
3333

3434
usage() {
35-
error_exit "Usage: bastille clone TARGET NEW_NAME IP_ADDRESS"
36-
35+
error_notify "Usage: bastille clone [option(s)] TARGET NEW_NAME IP_ADDRESS"
3736
cat << EOF
3837
Options:
39-
40-
-r | --restart -- Start/Restart jail(s) on completion.
41-
-f | --force -- Stop the jail if it is running.
42-
Mandatory for UFS, optional for ZFS.
38+
39+
-l | --live -- Clone a running jail. ZFS only.
40+
Jail must be running.
41+
Cannot be used with [-f|--force].
42+
-f | --force -- Stop the jail if it is running.
43+
Cannot be used with [-l|--live].
44+
-s | --start -- Start jail(s) when complete.
4345
4446
EOF
4547
exit 1
4648
}
4749

4850
# Handle options.
51+
LIVE=0
4952
FORCE=0
50-
RESTART=0
53+
START=0
5154
while [ "$#" -gt 0 ]; do
5255
case "${1}" in
5356
-h|--help|help)
5457
usage
5558
;;
56-
-r|--restart)
57-
RESTART=1
58-
shift
59+
-l|--live)
60+
if ! checkyesno bastille_zfs_enable; then
61+
error_exit "[-l|--live] can only be used with ZFS."
62+
else
63+
LIVE=1
64+
shift
65+
fi
5966
;;
6067
-f|--force)
61-
FORCE=1
68+
if [ "${LIVE}" -eq 1 ]; then
69+
error_exit "[-f|--force] cannot be used with [-l|--live]."
70+
else
71+
FORCE=1
72+
shift
73+
fi
74+
;;
75+
-s|--start)
76+
START=1
6277
shift
6378
;;
6479
-*)
@@ -161,8 +176,8 @@ update_jailconf_vnet() {
161176
sed -i '' "s|e\([0-9]\{1,\}\)b_${NEWNAME}|e${uniq_epair_bridge}b_${NEWNAME}|g" "${JAIL_CONFIG}"
162177
sed -i '' "s|epair\([0-9]\{1,\}\)|epair${uniq_epair_bridge}|g" "${JAIL_CONFIG}"
163178
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}"
164-
sed -i '' "s|ether.*:.*:.*:.*:.*:.*a|ether ${macaddr}a|" "${JAIL_CONFIG}"
165-
sed -i '' "s|ether.*:.*:.*:.*:.*:.*b|ether ${macaddr}b|" "${JAIL_CONFIG}"
179+
sed -i '' "s|ether.*:.*:.*:.*:.*:.*a |ether ${macaddr}a |" "${JAIL_CONFIG}"
180+
sed -i '' "s|ether.*:.*:.*:.*:.*:.*b |ether ${macaddr}b |" "${JAIL_CONFIG}"
166181
break
167182
fi
168183
fi
@@ -205,8 +220,13 @@ clone_jail() {
205220

206221
if ! [ -d "${bastille_jailsdir}/${NEWNAME}" ]; then
207222
if checkyesno bastille_zfs_enable; then
208-
check_target_is_stopped "${TARGET}" || if [ "${FORCE}" -eq 1 ]; then
209-
bastille stop "${TARGET}"
223+
if [ "${LIVE}" -eq 1 ]; then
224+
check_target_is_running "${TARGET}" || error_exit "[-l|--live] can only be used with a running jail."
225+
elif [ "${FORCE}" -eq 1 ]; then
226+
check_target_is_stopped "${TARGET}" || bastille stop "${TARGET}"
227+
else
228+
error_notify "Jail is running."
229+
error_exit "Use [-f|--force] to force stop the jail, or [-l|--live] (ZFS only) to clone a running jail."
210230
fi
211231
if [ -n "${bastille_zfs_zpool}" ]; then
212232
# Replicate the existing container
@@ -226,10 +246,11 @@ clone_jail() {
226246
# Perform container file copy (archive mode)
227247
check_target_is_stopped "${TARGET}" || if [ "${FORCE}" -eq 1 ]; then
228248
bastille stop "${TARGET}"
229-
cp -a "${bastille_jailsdir}/${TARGET}" "${bastille_jailsdir}/${NEWNAME}"
230249
else
231-
exit
250+
error_notify "Jail is running."
251+
error_exit "Use [-f|--force] to force stop the jail."
232252
fi
253+
cp -a "${bastille_jailsdir}/${TARGET}" "${bastille_jailsdir}/${NEWNAME}"
233254
fi
234255
else
235256
error_exit "${NEWNAME} already exists."
@@ -245,6 +266,12 @@ clone_jail() {
245266
else
246267
info "Cloned '${TARGET}' to '${NEWNAME}' successfully."
247268
fi
269+
if [ "${START}" -eq 1 ]; then
270+
if [ "${LIVE}" -eq 0 ]; then
271+
bastille start "${TARGET}"
272+
fi
273+
bastille start "${NEWNAME}"
274+
fi
248275
}
249276

250277
# Check if IP address is valid.

usr/local/share/bastille/cmd.sh

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

3434
usage() {
35-
error_exit "Usage: bastille cmd TARGET command"
36-
35+
error_notify "Usage: bastille cmd TARGET command"
3736
cat << EOF
3837
Options:
3938
@@ -77,15 +76,17 @@ RETURN=0
7776
set_target "${TARGET}"
7877

7978
for _jail in ${JAILS}; do
80-
# If target is stopped or not found, continue...
79+
80+
info "[${_jail}]:"
81+
8182
check_target_is_running "${_jail}" || if [ "${FORCE}" -eq 1 ]; then
8283
bastille start "${_jail}"
83-
else
84-
continue
84+
else
85+
error_notify "Jail is not running."
86+
error_continue "Use [-f|--force] to force start the jail."
8587
fi
8688

8789
COUNT=$(($COUNT+1))
88-
info "[${_jail}]:"
8990
if grep -qw "linsysfs" "${bastille_jailsdir}/${_jail}/fstab"; then
9091
# Allow executing commands on Linux jails.
9192
echo "$@"
@@ -94,7 +95,7 @@ for _jail in ${JAILS}; do
9495
echo "$@"
9596
jexec -l -U root "${_jail}" "$@"
9697
fi
97-
ERROR_CODE="$?"
98+
ERROR_CODE=$?
9899
if [ "${ERROR_CODE}" -ne 0 ]; then
99100
warn "[${_jail}]: ${ERROR_CODE}"
100101
fi

usr/local/share/bastille/common.sh

+2-5
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ warn() {
8282
check_target_exists() {
8383
local _TARGET="${1}"
8484
if [ ! -d "${bastille_jailsdir}"/"${_TARGET}" ]; then
85-
error_notify "Jail not found \"${_TARGET}\""
8685
return 1
8786
else
8887
return 0
@@ -92,7 +91,6 @@ check_target_exists() {
9291
check_target_is_running() {
9392
local _TARGET="${1}"
9493
if [ ! "$(/usr/sbin/jls name | awk "/^${_TARGET}$/")" ]; then
95-
error_notify "[${_TARGET}]: Not started. See 'bastille start ${_TARGET}'."
9694
return 1
9795
else
9896
return 0
@@ -102,7 +100,6 @@ check_target_is_running() {
102100
check_target_is_stopped() {
103101
local _TARGET="${1}"
104102
if [ "$(/usr/sbin/jls name | awk "/^${_TARGET}$/")" ]; then
105-
error_notify "${_TARGET} is running. See 'bastille stop ${_TARGET}'."
106103
return 1
107104
else
108105
return 0
@@ -114,7 +111,7 @@ set_target() {
114111
if [ "${_TARGET}" = ALL ] || [ "${_TARGET}" = all ]; then
115112
target_all_jails
116113
else
117-
check_target_exists "${_TARGET}" || exit
114+
check_target_exists "${_TARGET}" || error_exit "Jail not found \"${_TARGET}\""
118115
JAILS="${_TARGET}"
119116
TARGET="${_TARGET}"
120117
export JAILS
@@ -127,7 +124,7 @@ set_target_single() {
127124
if [ "${_TARGET}" = ALL ] || [ "${_TARGET}" = all ]; then
128125
error_exit "[all|ALL] not supported with this command."
129126
else
130-
check_target_exists "${_TARGET}" || exit
127+
check_target_exists "${_TARGET}" || error_exit "Jail not found \"${_TARGET}\""
131128
JAILS="${_TARGET}"
132129
TARGET="${_TARGET}"
133130
export JAILS

usr/local/share/bastille/console.sh

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

3434
usage() {
35-
error_exit "Usage: bastille console TARGET [user]"
36-
35+
error_notify "Usage: bastille console [option(s)] TARGET [user]"
3736
cat << EOF
3837
Options:
3938
@@ -75,8 +74,9 @@ bastille_root_check
7574
set_target_single "${TARGET}"
7675
check_target_is_running "${TARGET}" || if [ "${FORCE}" -eq 1 ]; then
7776
bastille start "${TARGET}"
78-
else
79-
exit
77+
else
78+
error_notify "Jail is not running."
79+
error_continue "Use [-f|--force] to force start the jail."
8080
fi
8181

8282
validate_user() {

usr/local/share/bastille/convert.sh

+4-3
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 convert [option(s)] TARGET"
35+
error_notify "Usage: bastille convert [option(s)] TARGET"
3636

3737
cat << EOF
3838
Options:
@@ -73,8 +73,9 @@ bastille_root_check
7373
set_target_single "${TARGET}"
7474
check_target_is_stopped "${TARGET}" || if [ "${FORCE}" -eq 1 ]; then
7575
bastille stop "${TARGET}"
76-
else
77-
exit
76+
else
77+
error_notify "Jail is running."
78+
error_exit "Use [-f|--force] to force stop the jail."
7879
fi
7980

8081
convert_symlinks() {

usr/local/share/bastille/cp.sh

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

3434
usage() {
35-
error_exit "Usage: bastille cp [option(s)] TARGET HOST_PATH JAIL_PATH"
36-
35+
error_notify "Usage: bastille cp [option(s)] TARGET HOST_PATH JAIL_PATH"
3736
cat << EOF
3837
Options:
3938
@@ -77,7 +76,7 @@ set_target "${TARGET}"
7776
for _jail in ${JAILS}; do
7877
info "[${_jail}]:"
7978
bastille_jail_path="${bastille_jailsdir}/${_jail}/root"
80-
cp "${OPTION}" "${CPSOURCE}" "${bastille_jail_path}${CPDEST}"
81-
RETURN="$?"
82-
return "${RETURN}"
79+
if ! cp "${OPTION}" "${CPSOURCE}" "${bastille_jail_path}${CPDEST}"; then
80+
error_continue "CP failed: ${CPSOURCE} -> ${bastille_jail_path}${CPDEST}"
81+
fi
8382
done

usr/local/share/bastille/create.sh

+6
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,12 @@ create_jail() {
506506
fi
507507
fi
508508

509+
# Exit if jail was not started, which means something is wrong.
510+
if ! check_target_is_running "${NAME}"; then
511+
bastille destroy "${NAME}"
512+
error_exit "[${NAME}]: Failed to create jail..."
513+
fi
514+
509515
if [ -n "${VNET_JAIL}" ]; then
510516
if [ -n "${bastille_template_vnet}" ]; then
511517
## rename interface to generic vnet0

usr/local/share/bastille/destroy.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434
usage() {
3535
error_notify "Usage: bastille destroy [option(s)] [JAIL|RELEASE]"
36-
3736
cat << EOF
3837
Options:
3938
@@ -50,10 +49,11 @@ destroy_jail() {
5049
bastille_jail_base="${bastille_jailsdir}/${TARGET}" ## dir
5150
bastille_jail_log="${bastille_logsdir}/${TARGET}_console.log" ## file
5251

53-
check_target_is_stopped "${TARGET}" || if [ "${FORCE}" -eq "1" ]; then
52+
check_target_is_stopped "${TARGET}" || if [ "${FORCE}" -eq 1 ]; then
5453
bastille stop "${TARGET}"
55-
else
56-
exit
54+
else
55+
error_notify "Jail is running."
56+
error_exit "Use [-f|--force] to force stop the jail."
5757
fi
5858

5959
if [ -d "${bastille_jail_base}" ]; then

usr/local/share/bastille/etcupdate.sh

+3-4
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@
3131
. /usr/local/etc/bastille/bastille.conf
3232

3333
usage() {
34-
error_exit "Usage: bastille etcupdate [option(s)] [TARGET|bootstrap RELEASE]"
35-
34+
error_notify "Usage: bastille etcupdate [option(s)] [TARGET|bootstrap RELEASE]"
3635
cat << EOF
3736
Options:
3837
39-
-d | --dry-run | -- Only show output of what etcupdate will do.
38+
-d | --dry-run -- Only show output of what etcupdate will do.
4039
4140
EOF
4241
exit 1
@@ -75,7 +74,7 @@ update_jail_etc() {
7574
fi
7675
}
7776

78-
if [ "$#" -lt 2 ] || [ "$#" -gt 3 ]; then
77+
if [ $# -lt 2 ] || [ $# -gt 3 ]; then
7978
usage
8079
fi
8180

usr/local/share/bastille/export.sh

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ usage() {
3737
# Valid compress/options for non ZFS configured systems are .tgz and .txz
3838
# If no compression option specified, user must redirect standard output
3939
error_notify "Usage: bastille export | option(s) | TARGET | PATH"
40-
4140
cat << EOF
4241
Options:
4342

usr/local/share/bastille/htop.sh

+5-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434
usage() {
3535
error_exit "Usage: bastille htop [option(s)] TARGET"
36-
3736
cat << EOF
3837
Options:
3938
@@ -71,16 +70,18 @@ TARGET="${1}"
7170

7271
bastille_root_check
7372
set_target_single "${TARGET}"
73+
74+
info "[${TARGET}]:"
7475
check_target_is_running "${TARGET}" || if [ "${FORCE}" -eq 1 ]; then
7576
bastille start "${TARGET}"
76-
else
77-
exit
77+
else
78+
error_notify "Jail is not running."
79+
error_continue "Use [-f|--force] to force start the jail."
7880
fi
7981

8082
bastille_jail_path="${bastille_jailsdir}/${TARGET}/root"
8183
if [ ! -x "${bastille_jail_path}/usr/local/bin/htop" ]; then
8284
error_notify "htop not found on ${TARGET}."
8385
elif [ -x "${bastille_jail_path}/usr/local/bin/htop" ]; then
84-
info "[${TARGET}]:"
8586
jexec -l ${TARGET} /usr/local/bin/htop
8687
fi

usr/local/share/bastille/import.sh

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ usage() {
3535
# Build an independent usage for the import command
3636
# If no file/extension specified, will import from standard input
3737
error_notify "Usage: bastille import [option(s)] FILE"
38-
3938
cat << EOF
4039
Options:
4140

usr/local/share/bastille/limits.sh

+12-4
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@
3333
. /usr/local/etc/bastille/bastille.conf
3434

3535
usage() {
36-
error_notify "Usage: bastille limits TARGET OPTION VALUE"
36+
error_notify "Usage: bastille limits [option(s)] TARGET OPTION VALUE"
3737
echo -e "Example: bastille limits JAILNAME memoryuse 1G"
38+
cat << EOF
39+
Options:
40+
41+
-f | --force -- Start the jail if it is stopped.
42+
43+
EOF
3844
exit 1
3945
}
4046

@@ -63,10 +69,12 @@ set_target "${TARGET}"
6369
for _jail in ${JAILS}; do
6470

6571
info "[${_jail}]:"
66-
check_target_is_running "${TARGET}" || if [ "${FORCE}" -eq 1 ]; then
72+
73+
check_target_is_running "${_jail}" || if [ "${FORCE}" -eq 1 ]; then
6774
bastille start "${_jail}"
68-
else
69-
exit
75+
else
76+
error_notify "Jail is not running."
77+
error_continue "Use [-f|--force] to force start the jail."
7078
fi
7179

7280
_rctl_rule="jail:${_jail}:${OPTION}:deny=${VALUE}/jail"

0 commit comments

Comments
 (0)