Skip to content

Commit 4365082

Browse files
authored
Merge pull request #778 from tschettervictor/mount-fixes
Mount/Umount fixes and improvements
2 parents 2c4ff17 + 9d7b727 commit 4365082

File tree

6 files changed

+186
-71
lines changed

6 files changed

+186
-71
lines changed

docs/chapters/subcommands/mount.rst

+31-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,40 @@ To mount storage within the container use `bastille mount`.
66

77
.. code-block:: shell
88
9-
ishmael ~ # bastille mount azkaban /storage/foo /media/foo nullfs ro 0 0
9+
ishmael ~ # bastille mount azkaban /storage/foo media/foo nullfs ro 0 0
1010
[azkaban]:
11+
Added: /media/foo /usr/local/bastille/jails/azkaban/root/media/foo nullfs ro 0 0
12+
ishmael ~ # bastille mount azkaban /storage/bar /media/bar nullfs ro 0 0
13+
[azkaban]:
14+
Added: /media/bar /usr/local/bastille/jails/azkaban/root/media/bar nullfs ro 0 0
15+
16+
Notice the JAIL_PATH format can be /media/foo or simply media/bar. The leading slash / is optional. The HOST_PATH howerver, must be the full path including the leading slash /.
17+
18+
It is also possible to mount individual files into a jail as seen below.
19+
Bastille will not mount if a file is already present at the specified mount point.
20+
If you do not specify a file name, bastille will mount the file underneath the specified directory as seen in the second example below.
21+
22+
.. code-block:: shell
23+
24+
ishmael ~ # bastille mount azkaban /etc/rc.conf /mnt/etc/rc.conf nullfs ro 0 0
25+
[azkaban]:
26+
Added: /etc/rc.conf /usr/local/bastille/jails/azkaban/root/mnt/etc/rc.conf nullfs ro 0 0
27+
ishmael ~ # bastille mount azkaban /etc/rc.conf /media/bar nullfs ro 0 0
28+
[azkaban]:
29+
Added: /etc/rc.conf usr/local/bastille/jails/azkaban/root/media/bar/rc.conf nullfs ro 0 0
30+
31+
It is also possible (but not recommended) to have spaces in the directories that are mounted.
32+
It is necessary to escape each space with a backslash \ and enclose the mount point in quotes "" as seen below.
33+
It is possible to do the same for the jail path, but again, not recommemded.
34+
35+
.. code-block:: shell
36+
37+
ishmael ~ # bastille mount azkaban "/storage/my\ directory\ with\ spaces" /media/foo nullfs ro 0 0
38+
[azkaban]:
39+
Added: /storage/my\040directory\040with\040spaces /usr/local/bastille/jails/azkaban/root/media/foo nullfs ro 0 0
1140
1241
Syntax follows standard `/etc/fstab` format:
1342

1443
.. code-block:: shell
1544
16-
Usage: bastille mount TARGET host_path container_path [filesystem_type options dump pass_number]
45+
Usage: bastille mount TARGET HOST_PATH JAIL_PATH [filesystem_type options dump pass_number]

docs/chapters/subcommands/umount.rst

+14-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,21 @@ To unmount storage from a container use `bastille umount`.
88
99
ishmael ~ # bastille umount azkaban /media/foo
1010
[azkaban]:
11+
Unmounted: /usr/local/bastille/jails/jail4/root/media/foo
12+
ishmael ~ # bastille umount azkaban /mnt/etc/rc.conf
13+
[azkaban]:
14+
Unmounted: /usr/local/bastille/jails/jail4/root/mnt/etc/rc.conf
15+
16+
Syntax requires only the jail path to unmount.
17+
18+
.. code-block:: shell
19+
20+
Usage: bastille umount TARGET JAIL_PATH
1121
12-
Syntax requires only the container path to unmount:
22+
If the directory you are unmounting has spaces, make sure to escape them with a backslash \, and enclode the mount point in quotes "".
1323

1424
.. code-block:: shell
1525
16-
Usage: bastille umount TARGET container_path
26+
ishmael ~ # bastille umount azkaban "/media/foo\ with\ spaces"
27+
[azkaban]:
28+
Unmounted: /usr/local/bastille/jails/jail4/root/media/foo with spaces

usr/local/bin/bastille

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,10 @@ version|-v|--version)
157157
help|-h|--help)
158158
usage
159159
;;
160-
bootstrap|create|destroy|export|import|list|rdr|restart|setup|start|update|upgrade|verify)
160+
bootstrap|create|destroy|export|import|list|mount|rdr|restart|setup|start|umount|update|upgrade|verify)
161161
# Nothing "extra" to do for these commands. -- cwells
162162
;;
163-
clone|config|cmd|console|convert|cp|edit|htop|limits|mount|pkg|rcp|rename|service|stop|sysrc|tags|template|top|umount|zfs)
163+
clone|config|cmd|console|convert|cp|edit|htop|limits|pkg|rcp|rename|service|stop|sysrc|tags|template|top|zfs)
164164
# Parse the target and ensure it exists. -- cwells
165165
if [ $# -eq 0 ]; then # No target was given, so show the command's help. -- cwells
166166
PARAMS='help'

usr/local/share/bastille/common.sh

+29
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ error_notify() {
5656
echo -e "${COLOR_RED}$*${COLOR_RESET}" 1>&2
5757
}
5858

59+
error_continue() {
60+
error_notify "$@"
61+
# Disabling this shellcheck as we only ever call it inside of a loop
62+
# shellcheck disable=SC2104
63+
continue
64+
}
65+
5966
# Notify message on error and exit
6067
error_exit() {
6168
error_notify "$@"
@@ -70,6 +77,15 @@ warn() {
7077
echo -e "${COLOR_YELLOW}$*${COLOR_RESET}"
7178
}
7279

80+
check_target_exists() {
81+
local _TARGET="${1}"
82+
if [ ! -d "${bastille_jailsdir}"/"${_TARGET}" ]; then
83+
return 1
84+
else
85+
return 0
86+
fi
87+
}
88+
7389
generate_static_mac() {
7490
local jail_name="${1}"
7591
local external_interface="${2}"
@@ -131,6 +147,19 @@ EOF
131147
fi
132148
}
133149

150+
set_target() {
151+
local _TARGET="${1}"
152+
if [ "${_TARGET}" = ALL ] || [ "${_TARGET}" = all ]; then
153+
target_all_jails
154+
else
155+
check_target_exists "${_TARGET}" || error_exit "Jail not found \"${_TARGET}\""
156+
JAILS="${_TARGET}"
157+
TARGET="${_TARGET}"
158+
export JAILS
159+
export TARGET
160+
fi
161+
}
162+
134163
checkyesno() {
135164
## copied from /etc/rc.subr -- cedwards (20231125)
136165
## issue #368 (lowercase values should be parsed)

usr/local/share/bastille/mount.sh

+77-46
Original file line numberDiff line numberDiff line change
@@ -32,96 +32,127 @@
3232
. /usr/local/etc/bastille/bastille.conf
3333

3434
usage() {
35-
error_exit "Usage: bastille mount TARGET host_path container_path [filesystem_type options dump pass_number]"
35+
error_exit "Usage: bastille mount TARGET HOST_PATH JAIL_PATH [filesystem_type options dump pass_number]"
3636
}
3737

3838
# Handle special-case commands first.
39-
case "$1" in
40-
help|-h|--help)
41-
usage
42-
;;
39+
case "${1}" in
40+
help|-h|--help)
41+
usage
42+
;;
4343
esac
4444

45-
if [ $# -lt 2 ]; then
45+
if [ "$#" -lt 3 ] || [ "$#" -gt 6 ]; then
4646
usage
47-
elif [ $# -eq 2 ]; then
48-
_fstab="$@ nullfs ro 0 0"
47+
fi
48+
49+
TARGET="${1}"
50+
shift
51+
52+
if [ "$#" -eq 2 ]; then
53+
_fstab="$(echo "$* nullfs ro 0 0" | sed 's#\\ #\\040#g')"
4954
else
50-
_fstab="$@"
55+
_fstab="$(echo "$*" | sed 's#\\ #\\040#g')"
5156
fi
5257

5358
bastille_root_check
59+
set_target "${TARGET}"
5460

55-
## assign needed variables
56-
_hostpath=$(echo "${_fstab}" | awk '{print $1}')
57-
_jailpath=$(echo "${_fstab}" | awk '{print $2}')
61+
# Assign variables
62+
_hostpath_fstab=$(echo "${_fstab}" | awk '{print $1}')
63+
_hostpath="$(echo "${_hostpath_fstab}" 2>/dev/null | sed 's#\\040# #g')"
64+
_jailpath_fstab=$(echo "${_fstab}" | awk '{print $2}')
65+
_jailpath="$(echo "${_jailpath_fstab}" 2>/dev/null | sed 's#\\040# #g')"
5866
_type=$(echo "${_fstab}" | awk '{print $3}')
5967
_perms=$(echo "${_fstab}" | awk '{print $4}')
6068
_checks=$(echo "${_fstab}" | awk '{print $5" "$6}')
6169

62-
## if any variables are empty, bail out
70+
# Exit if any variables are empty
6371
if [ -z "${_hostpath}" ] || [ -z "${_jailpath}" ] || [ -z "${_type}" ] || [ -z "${_perms}" ] || [ -z "${_checks}" ]; then
6472
error_notify "FSTAB format not recognized."
65-
warn "Format: /host/path jail/path nullfs ro 0 0"
73+
warn "Format: /host/path /jail/path nullfs ro 0 0"
6674
warn "Read: ${_fstab}"
67-
exit 1
75+
usage
6876
fi
6977

70-
# if host path doesn't exist, type is not "nullfs" or are using advanced mount type "tmpfs,linprocfs,linsysfs, fdescfs,
71-
# procfs"
78+
# Exit if host path doesn't exist, type is not "nullfs", or mount is an advanced mount type "tmpfs,linprocfs,linsysfs,fdescfs,procfs"
7279
if { [ "${_hostpath}" = "tmpfs" ] && [ "$_type" = "tmpfs" ]; } || \
7380
{ [ "${_hostpath}" = "linprocfs" ] && [ "${_type}" = "linprocfs" ]; } || \
7481
{ [ "${_hostpath}" = "linsysfs" ] && [ "${_type}" = "linsysfs" ]; } || \
7582
{ [ "${_hostpath}" = "proc" ] && [ "${_type}" = "procfs" ]; } || \
7683
{ [ "${_hostpath}" = "fdesc" ] && [ "${_type}" = "fdescfs" ]; } then
7784
warn "Detected advanced mount type ${_hostpath}"
78-
elif [ ! -d "${_hostpath}" ] || [ "${_type}" != "nullfs" ]; then
79-
error_notify "Detected invalid host path or incorrect mount type in FSTAB."
80-
warn "Format: /host/path jail/path nullfs ro 0 0"
85+
elif [ ! -e "${_hostpath}" ] || [ "${_type}" != "nullfs" ]; then
86+
error_notify "Invalid host path or incorrect mount type in FSTAB."
87+
warn "Format: /host/path /jail/path nullfs ro 0 0"
8188
warn "Read: ${_fstab}"
82-
exit 1
89+
usage
8390
fi
8491

85-
## if mount permissions are not "ro" or "rw"
92+
# Mount permissions need to be "ro" or "rw"
8693
if [ "${_perms}" != "ro" ] && [ "${_perms}" != "rw" ]; then
8794
error_notify "Detected invalid mount permissions in FSTAB."
88-
warn "Format: /host/path jail/path nullfs ro 0 0"
95+
warn "Format: /host/path /jail/path nullfs ro 0 0"
8996
warn "Read: ${_fstab}"
90-
exit 1
97+
usage
9198
fi
9299

93-
## if check & pass are not "0 0 - 1 1"; bail out
100+
# Dump and pass need to be "0 0 - 1 1"
94101
if [ "${_checks}" != "0 0" ] && [ "${_checks}" != "1 0" ] && [ "${_checks}" != "0 1" ] && [ "${_checks}" != "1 1" ]; then
95102
error_notify "Detected invalid fstab options in FSTAB."
96-
warn "Format: /host/path jail/path nullfs ro 0 0"
103+
warn "Format: /host/path /jail/path nullfs ro 0 0"
97104
warn "Read: ${_fstab}"
98-
exit 1
105+
usage
99106
fi
100107

101108
for _jail in ${JAILS}; do
109+
102110
info "[${_jail}]:"
103111

104-
## aggregate variables into FSTAB entry
105-
_fullpath="${bastille_jailsdir}/${_jail}/root/${_jailpath}"
106-
_fstab_entry="${_hostpath} ${_fullpath} ${_type} ${_perms} ${_checks}"
112+
_fullpath_fstab="$( echo "${bastille_jailsdir}/${_jail}/root/${_jailpath_fstab}" 2>/dev/null | sed 's#//#/#' )"
113+
_fullpath="$( echo "${bastille_jailsdir}/${_jail}/root/${_jailpath}" 2>/dev/null | sed 's#//#/#' )"
114+
_fstab_entry="${_hostpath_fstab} ${_fullpath_fstab} ${_type} ${_perms} ${_checks}"
107115

108-
## Create mount point if it does not exist. -- cwells
109-
if [ ! -d "${_fullpath}" ]; then
110-
if ! mkdir -p "${_fullpath}"; then
111-
error_exit "Failed to create mount point inside jail."
112-
fi
116+
# Check if mount point has already been added
117+
_existing_mount="$(echo ${_fullpath_fstab} 2>/dev/null | sed 's#\\#\\\\#g')"
118+
if grep -Eq "[[:blank:]]${_existing_mount}.*[[:blank:]]" "${bastille_jailsdir}/${_jail}/fstab"; then
119+
warn "Mountpoint already present in ${bastille_jailsdir}/${_jail}/fstab"
120+
grep -E "[[:blank:]]${_existing_mount}" "${bastille_jailsdir}/${_jail}/fstab"
121+
continue
113122
fi
114123

115-
## if entry doesn't exist, add; else show existing entry
116-
if ! egrep -q "[[:blank:]]${_fullpath}[[:blank:]]" "${bastille_jailsdir}/${_jail}/fstab" 2> /dev/null; then
117-
if ! echo "${_fstab_entry}" >> "${bastille_jailsdir}/${_jail}/fstab"; then
118-
error_exit "Failed to create fstab entry: ${_fstab_entry}"
124+
125+
# Create mount point if it does not exist
126+
if [ -d "${_hostpath}" ] && [ ! -d "${_fullpath}" ]; then
127+
mkdir -p "${_fullpath}" || error_continue "Failed to create mount point."
128+
elif [ -f "${_hostpath}" ] ; then
129+
_filename="$( basename ${_hostpath} )"
130+
if echo "${_fullpath}" 2>/dev/null | grep -qow "${_filename}"; then
131+
mkdir -p "$( dirname "${_fullpath}" )" || error_continue "Failed to create mount point."
132+
if [ ! -f "${_fullpath}" ]; then
133+
touch "${_fullpath}" || error_continue "Failed to create mount point."
134+
else
135+
error_notify "Failed. File exists at mount point."
136+
warn "${_fullpath}"
137+
continue
138+
fi
139+
else
140+
_fullpath_fstab="$( echo "${bastille_jailsdir}/${_jail}/root/${_jailpath_fstab}/${_filename}" 2>/dev/null | sed 's#//#/#' )"
141+
_fullpath="$( echo "${bastille_jailsdir}/${_jail}/root/${_jailpath}/${_filename}" 2>/dev/null | sed 's#//#/#' )"
142+
_fstab_entry="${_hostpath_fstab} ${_fullpath_fstab} ${_type} ${_perms} ${_checks}"
143+
mkdir -p "$( dirname "${_fullpath}" )" || error_continue "Failed to create mount point."
144+
if [ ! -f "${_fullpath}" ]; then
145+
touch "${_fullpath}" || error_continue "Failed to create mount point."
146+
else
147+
error_notify "Failed. File exists at mount point."
148+
warn "${_fullpath}"
149+
continue
150+
fi
119151
fi
120-
echo "Added: ${_fstab_entry}"
121-
else
122-
warn "Mountpoint already present in ${bastille_jailsdir}/${_jail}/fstab"
123-
egrep "[[:blank:]]${_fullpath}[[:blank:]]" "${bastille_jailsdir}/${_jail}/fstab"
124-
fi
125-
mount -F "${bastille_jailsdir}/${_jail}/fstab" -a
126-
echo
152+
fi
153+
154+
# Add entry to fstab and mount
155+
echo "${_fstab_entry}" >> "${bastille_jailsdir}/${_jail}/fstab" || error_continue "Failed to create fstab entry: ${_fstab_entry}"
156+
mount -F "${bastille_jailsdir}/${_jail}/fstab" -a || error_continue "Failed to mount volume: ${_fullpath}"
157+
echo "Added: ${_fstab_entry}"
127158
done

usr/local/share/bastille/umount.sh

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

3434
usage() {
35-
error_exit "Usage: bastille umount TARGET container_path"
35+
error_exit "Usage: bastille umount TARGET JAIL_PATH"
3636
}
3737

3838
# Handle special-case commands first.
39-
case "$1" in
40-
help|-h|--help)
41-
usage
42-
;;
39+
case "${1}" in
40+
help|-h|--help)
41+
usage
42+
;;
4343
esac
4444

45-
if [ $# -ne 1 ]; then
45+
if [ "$#" -ne 2 ]; then
4646
usage
4747
fi
4848

49-
bastille_root_check
49+
TARGET="${1}"
50+
MOUNT_PATH="${2}"
5051

51-
MOUNT_PATH=$1
52+
bastille_root_check
53+
set_target "${TARGET}"
5254

5355
for _jail in ${JAILS}; do
56+
5457
info "[${_jail}]:"
5558

56-
_jailpath="${bastille_jailsdir}/${_jail}/root/${MOUNT_PATH}"
59+
_jailpath="$( echo "${bastille_jailsdir}/${_jail}/root/${MOUNT_PATH}" 2>/dev/null | sed 's#//#/#' | sed 's#\\##g')"
60+
_mount="$( mount | grep -Eo "[[:blank:]]${_jailpath}[[:blank:]]" )"
61+
_jailpath_fstab="$(echo "${bastille_jailsdir}/${_jail}/root/${MOUNT_PATH}" | sed 's#//#/#g' | sed 's# #\\#g' | sed 's#\\#\\\\040#g')"
62+
_fstab_entry="$(grep -Eo "[[:blank:]]${_jailpath_fstab}[[:blank:]]" ${bastille_jailsdir}/${_jail}/fstab)"
5763

58-
if [ ! -d "${_jailpath}" ]; then
59-
error_exit "The specified mount point does not exist inside the jail."
64+
# Exit if mount point non-existent
65+
if [ -z "${_mount}" ] && [ -z "${_fstab_entry}" ]; then
66+
error_continue "The specified mount point does not exist."
6067
fi
6168

62-
# Unmount the volume. -- cwells
63-
if ! umount "${_jailpath}"; then
64-
error_exit "Failed to unmount volume: ${MOUNT_PATH}"
69+
# Unmount
70+
if [ -n "${_mount}" ]; then
71+
umount "${_jailpath}" || error_continue "Failed to unmount volume: ${MOUNT_PATH}"
6572
fi
6673

67-
# Remove the entry from fstab so it is not automounted in the future. -- cwells
68-
if ! sed -E -i '' "\, +${_jailpath} +,d" "${bastille_jailsdir}/${_jail}/fstab"; then
69-
error_exit "Failed to delete fstab entry: ${_fstab_entry}"
74+
# Remove entry from fstab
75+
if [ -n "${_fstab_entry}" ]; then
76+
if ! sed -E -i '' "\, +${_jailpath_fstab} +,d" "${bastille_jailsdir}/${_jail}/fstab"; then
77+
error_continue "Failed to delete fstab entry: ${MOUNT_PATH}"
78+
fi
7079
fi
7180

72-
echo "Unmounted: ${MOUNT_PATH}"
73-
echo
81+
# Delete if mount point was a file
82+
if [ -f "${_jailpath}" ]; then
83+
rm -f "${_jailpath}" || error_continue "Failed to unmount volume: ${MOUNT_PATH}"
84+
fi
85+
86+
echo "Unmounted: ${_jailpath}"
87+
7488
done

0 commit comments

Comments
 (0)