Skip to content

It's now possible to pass zfs_dataset=.... to cbsd jcreate. #786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions etc/defaults/jail-freebsd-default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ exec_master_prestart="0"
exec_master_prestop="0"

# dynamic variable
zfs_dataset=""
host_hostname="${jname}.${default_domain}"
path="${jaildir}/${jname}"
data="${jaildatadir}/${jname}-${jaildatapref}"
Expand Down
64 changes: 41 additions & 23 deletions subr/jfs.subr
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,14 @@ remove_data_dir()
*)
;;
esac
unmountdata ${_data}
${ZFS_CMD} list ${ZPOOL}/${jname} >/dev/null 2>/dev/null

DATA_ZPOOL=""
unmountdata ${_data} --keep-DATA_ZPOOL
if [ -z "${DATA_ZPOOL}" ] ; then
DATA_ZPOOL=${ZPOOL}/${jname}
fi

${ZFS_CMD} list ${DATA_ZPOOL} >/dev/null 2>/dev/null
if [ $? -eq 0 ]; then
# sometimes these errors happen:
# cannot unmount 'XXX': pool or dataset is busy
Expand All @@ -348,14 +354,16 @@ remove_data_dir()
# fstat/processes?
${UMOUNT_CMD} -f ${_data} 2>/dev/null
fi
${ZFS_CMD} destroy ${ZPOOL}/${jname}
${ZFS_CMD} list ${ZPOOL}/${jname} >/dev/null 2>/dev/null
${ZFS_CMD} destroy ${DATA_ZPOOL}
${ZFS_CMD} list ${DATA_ZPOOL} >/dev/null 2>/dev/null
[ $? -ne 0 ] && break
${ECHO} "${N1_COLOR}${CBSD_APP}: cannot destroy ${ZPOOL}/${jname} (${_data}), retry: ${N2_COLOR}${_retry}/${_max_retry}${N0_COLOR}" 1>&2
${ECHO} "${N1_COLOR}${CBSD_APP}: cannot destroy ${DATA_ZPOOL} (${_data}), retry: ${N2_COLOR}${_retry}/${_max_retry}${N0_COLOR}" 1>&2
sleep 1
_retry=$(( _retry + 1 ))
done
fi

unset DATA_ZPOOL
fi
elif [ "${hammerfeat}" = "1" ]; then
destroy_hammer_fs ${jname}
Expand Down Expand Up @@ -386,6 +394,9 @@ unmountdata()
# store and restore old ZPOOL value after zfsmnt
OZPOOL="${ZPOOL}"
zfsmnt ${_data}
if [ -n "${2}" ] ; then
DATA_ZPOOL=${ZPOOL}
fi
ZPOOL="${OZPOOL}"

if [ $? -eq 1 ]; then
Expand All @@ -401,7 +412,7 @@ create_fs()
[ -z "${data}" ] && return 1

if [ "${zfsfeat}" = "1" ]; then
create_zfs_fs -d ${data}
create_zfs_fs -d ${data} -Z "${zfs_dataset}"
elif [ "${hammerfeat}" = "1" ]; then
create_hammer_fs ${jname}
else
Expand All @@ -416,18 +427,20 @@ create_fs()
# -n - jail_name
# -p promote fs ( 0 or 1 )
# -z zfs_snapsrc
# -Z ZFS dataset location
create_zfs_fs()
{
local _data _mount_point _name _promote _zfs_snapsrc _ret
local _data _mount_point _name _promote _zfs_snapsrc _zfs_dataset _ret
local _cmd

while getopts "d:m:n:p:z:" opt; do
while getopts "d:m:n:p:z:Z:" opt; do
case "${opt}" in
d) _data="${OPTARG}" ;;
m) _mount_point="${OPTARG}" ;;
n) _name="${OPTARG}" ;;
p) _promote="${OPTARG}" ;;
z) _zfs_snapsrc="${OPTARG}" ;;
Z) _zfs_dataset="${OPTARG}" ;;
esac
shift $(($OPTIND - 1))
done
Expand All @@ -439,12 +452,17 @@ create_zfs_fs()
[ -n "${zfs_snapsrc}" ] && _zfs_snapsrc="${zfs_snapsrc}"
[ -z "${_promote}" ] && _promote=0

local ZPOOL

ZPOOL=$( ${ZFS_CMD} get -Ho value name ${jaildatadir} )
local ZPOOL ZDATASET
if [ -n "$_zfs_dataset" ] ; then
ZDATASET="${_zfs_dataset}"
ZPOOL=`echo ${_zfs_dataset} | ${SED_CMD} -e 's#/[^/]$##'`
else
ZPOOL=$( ${ZFS_CMD} get -Ho value name ${jaildatadir} )
ZDATASET=${ZPOOL}/${_name}
fi

if zfsroot ${_name}; then
log_err 1 "create_zfs_fs: ${ZPOOL}/${_name} already in use"
if zfsroot ${ZDATASET}; then
log_err 1 "create_zfs_fs: ${ZDATASET} already in use"
fi

# encrypt
Expand All @@ -470,33 +488,33 @@ create_zfs_fs()

if [ -z "${_zfs_snapsrc}" ]; then
if [ -z "${_encrypt_args}" ]; then
_cmd="${ZFS_CMD} create ${zfs_create_flags} -o mountpoint=${_data} ${ZPOOL}/${_name}"
_cmd="${ZFS_CMD} create ${zfs_create_flags} -o mountpoint=${_data} ${ZDATASET}"
res=$( ${_cmd} 2>&1 )
_ret=$?
cbsdlogger NOTICE ${CBSD_APP}: create_zfs_fs errcode: ${_ret}: ${_cmd}
else
_cmd="${ZFS_CMD} create ${zfs_create_flags} -o mountpoint=${_data} ${_encrypt_args} ${ZPOOL}/${_name}"
_cmd="${ZFS_CMD} create ${zfs_create_flags} -o mountpoint=${_data} ${_encrypt_args} ${ZDATASET}"
${_cmd}
_ret=$?
cbsdlogger NOTICE ${CBSD_APP}: create_zfs_fs errcode: ${_ret}: ${_cmd}
fi
[ ${_ret} -ne 0 ] && log_err 1 "${N1_COLOR}create_zfs_fs: zfs create failed: ${res}. No ZFS and zfsfeat=1?${N0_COLOR}"
cbsdlogger NOTICE ${CBSD_APP}: create_zfs_fs: ZFS ${ZPOOL}/${_name} has been created with mountpoint: ${_data}, opt: ${zfs_create_flags}
cbsdlogger NOTICE ${CBSD_APP}: create_zfs_fs: ZFS ${ZDATASET} has been created with mountpoint: ${_data}, opt: ${zfs_create_flags}
else
_cmd=$( ${ZFS_CMD} clone -p ${_zfs_snapsrc} ${ZPOOL}/${_name} )
_cmd=$( ${ZFS_CMD} clone -p ${_zfs_snapsrc} ${ZDATASET} )
res=$( ${_cmd} 2>&1 )
_ret=$?
[ ${_ret} -ne 0 ] && log_err 1 "${N1_COLOR}create_zfs_fs: zfs clone failed, code: ${_ret}: ${res}${N0_COLOR}"
cbsdlogger NOTICE ${CBSD_APP}: ZFS cloned: ${_zfs_snapsrc} to ${ZPOOL}/${_name}
cbsdlogger NOTICE ${CBSD_APP}: ZFS cloned: ${_zfs_snapsrc} to ${ZDATASET}

if [ "${_mount_point}" != "none" ]; then
if [ -n "${_mount_point}" ]; then
_cmd=$( ${ZFS_CMD} set mountpoint=${_mount_point} ${ZPOOL}/${_name} )
_cmd=$( ${ZFS_CMD} set mountpoint=${_mount_point} ${ZDATASET} )
${_cmd}
_ret=$?
cbsdlogger NOTICE ${CBSD_APP}: create_zfs_fs errcode: ${_ret}: ${_cmd}
else
_cmd=$( ${ZFS_CMD} set mountpoint=${_data} ${ZPOOL}/${_name} )
_cmd=$( ${ZFS_CMD} set mountpoint=${_data} ${ZDATASET} )
${_cmd}
_ret=$?
cbsdlogger NOTICE ${CBSD_APP}: create_zfs_fs errcode: ${_ret}: ${_cmd}
Expand Down Expand Up @@ -526,9 +544,9 @@ create_zfs_fs()
esac

if [ ${_promote} -eq 1 ]; then
${ECHO} "${N1_COLOR}ZFS promoting for: ${N2_COLOR}${ZPOOL}/${_name}${N0_COLOR}"
cbsdlogger NOTICE ${CBSD_APP}: zfs promoting for: ${ZPOOL}/${_name}
${ZFS_CMD} promote ${ZPOOL}/${_name}
${ECHO} "${N1_COLOR}ZFS promoting for: ${N2_COLOR}${ZDATASET}${N0_COLOR}"
cbsdlogger NOTICE ${CBSD_APP}: zfs promoting for: ${ZDATASET}
${ZFS_CMD} promote ${ZDATASET}
fi

return 0
Expand Down
11 changes: 10 additions & 1 deletion subr/zfs.subr
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,18 @@ getmnt() {
# fi
zfsroot() {
[ -z "$1" ] && return 0
local _pattern
case $1 in
*/*)
_pattern="${1}"
;;
*)
_pattern="${ZPOOL}/${1}"
;;
esac

${ZFS_CMD} list -H -o name | while read _mnt; do
[ "$_mnt" = "${ZPOOL}/${1}" ] && exit 0
[ "$_mnt" = "${_pattern}" ] && exit 0
done

return $?
Expand Down