New option to give quota file

This commit is contained in:
Jeremy Gardais 2022-07-19 15:09:37 +02:00
parent b29bd8c2a8
commit 8cfcb862b3
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
1 changed files with 40 additions and 6 deletions

View File

@ -9,9 +9,9 @@
# 2021-11-22
# }}}
# Vars {{{
readonly PROGNAME=$(basename "${0}")
readonly PROGDIR=$(readlink -m $(dirname "${0}"))
readonly ARGS="${*}"
PROGNAME=$(basename "${0}"); readonly PROGNAME
PROGDIR=$(readlink -m $(dirname "${0}")); readonly PROGDIR
ARGS="${*}"; readonly ARGS
readonly NBARGS="${#}"
[ -z "${DEBUG}" ] && DEBUG=1
## Export DEBUG for sub-script
@ -21,6 +21,9 @@ export DEBUG
readonly ZFS_DATASET_DEFAULT="datastore"
readonly ZFS_QUOTA_DEFAULT="50G"
# Disable QUOTA_FILE_MODE by default
QUOTA_FILE_MODE=1
## Colors
readonly PURPLE='\033[1;35m'
readonly RED='\033[0;31m'
@ -93,6 +96,19 @@ define_vars() { # {{{
readonly zfs_quota="${ZFS_QUOTA_DEFAULT}"
fi
## }}}
## If quota_file was defined (argument) {{{
if [ -n "${quota_file}" ]; then
## If the file is exists
## OR exit
is_file_present "${quota_file}" \
|| error_message "Given quota file (${quota_file}) isn't readable." 10
## Enable QUOTA_FILE_MODE
debug_message "define_vars \
${quota_file} will be used to get specific quota for defined users."
QUOTA_FILE_MODE=0
fi
## }}}
## Temp file vars {{{
readonly zfs_user_list_path="/tmp/${PROGNAME}.user.list"
@ -283,14 +299,26 @@ main() { # {{{
## }}}
## Information message
debug_message "Apply quota (${RED}${zfs_quota}${COLOR_DEBUG}) to all users of ZFS pool/dataset : ${RED}${zfs_dataset}${COLOR_DEBUG}"
debug_message "Apply quota (default : ${RED}${zfs_quota}${COLOR_DEBUG}) to all users of ZFS pool/dataset : ${RED}${zfs_dataset}${COLOR_DEBUG}"
## Read user one by one
while IFS= read -r zfs_username; do
### Get quota to use {{{
if [ "${QUOTA_FILE_MODE}" -eq "0" ] \
&& grep --word-regexp -- "^${zfs_username}" "${quota_file}" | grep --only-matching --perl-regexp --quiet -- '[[:digit:].]*.$'; then
user_zfs_quota="$(grep --word-regexp -- "^${zfs_username}" "${quota_file}" |
grep --only-matching --perl-regexp -- '[[:digit:].]*.$' \
|| error_message "Can't get quota for ${zfs_username} user from ${quota_file} quota file." 4)"
debug_message "Apply specific quota (${RED}${user_zfs_quota}${COLOR_DEBUG}) to ${zfs_username} user."
exit 0
else
user_zfs_quota="${zfs_quota}"
fi
### }}}
### Try to apply the quota to the user {{{
### OR Exit with error
apply_zfs_userquota "${zfs_username}" "${zfs_quota}" \
|| error_message "Can't define the new quota (${zfs_quota}) for ${zfs_username} user in ${zfs_dataset} ZFS pool/dataset. Please use --debug option." 4
apply_zfs_userquota "${zfs_username}" "${user_zfs_quota}" \
|| error_message "Can't define the new quota (${user_zfs_quota}) for ${zfs_username} user in ${zfs_dataset} ZFS pool/dataset. Please use --debug option." 4
### }}}
done < "${zfs_user_list_path}"
@ -327,6 +355,12 @@ if [ ! "${NBARGS}" -eq "0" ]; then
-d|--debug ) ## debug
DEBUG=0
;;
-f|--file ) ## Quota file
## Move to the next argument
shift
## Define var
readonly quota_file="${1}"
;;
-h|--help ) ## help
usage
## Exit after help informations