Manage old users that no longer uses any space

This commit is contained in:
Jeremy Gardais 2022-08-18 15:20:40 +02:00
parent bc190d399c
commit 5719ee5466
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
1 changed files with 32 additions and 17 deletions

View File

@ -286,7 +286,7 @@ main() { # {{{
## If ZFS pool/dataset is not available {{{ ## If ZFS pool/dataset is not available {{{
### Exit ### Exit
is_zfs_dataset_exists "${zfs_dataset}" \ is_zfs_dataset_exists "${zfs_dataset}" \
|| error_message "Please verify your ZFS pool (${zfs_dataset} doesn't seems available." 3 || error_message "Please verify your ZFS pool (${zfs_dataset}) doesn't seems available." 3
## }}} ## }}}
## Try to get the user list of ZFS pool/dataset {{{ ## Try to get the user list of ZFS pool/dataset {{{
@ -295,13 +295,19 @@ main() { # {{{
|| error_message "Can't get the user list of ${zfs_dataset} ZFS pool/dataset. Please use --debug option." 4 || error_message "Can't get the user list of ${zfs_dataset} ZFS pool/dataset. Please use --debug option." 4
## }}} ## }}}
## If a previous list of users exists {{{ ## Don't compare current user list with any previous list {{{
## 1. Cause the list of users might be the same ✅
## 2. But new quota might have been set for specific user ❎
## 3. And if a user no longer exists on the system, it will remains on ZFS quota/userspace ❎
## Disabled
## If a previous list of users exists
### If the two list are the same ### If the two list are the same
### Exit ### Exit
is_file_present "${zfs_previous_user_list_path}" \ #is_file_present "${zfs_previous_user_list_path}" \
&& is_file_similar "${zfs_user_list_path}" "${zfs_previous_user_list_path}" \ #&& is_file_similar "${zfs_user_list_path}" "${zfs_previous_user_list_path}" \
&& debug_message "main No new user from previous run, no more actions required." \ #&& debug_message "main No new user from previous run, no more actions required." \
&& exit 0 #&& exit 0
## }}} ## }}}
## Information message ## Information message
@ -309,6 +315,8 @@ main() { # {{{
## Read user one by one ## Read user one by one
while IFS= read -r zfs_username; do while IFS= read -r zfs_username; do
### If user uses more than 0B {{{
if ! zfs get -H -o value userused@"${zfs_username:-0}" "${zfs_dataset:-/dev/null}" | grep --only-matching --quiet -- "0B" ; then
### Define quota to use {{{ ### Define quota to use {{{
if [ "${QUOTA_FILE_MODE}" -eq "0" ] \ if [ "${QUOTA_FILE_MODE}" -eq "0" ] \
&& grep --word-regexp -- "^${zfs_username}" "${quota_file}" | grep --only-matching --perl-regexp --quiet -- '[[:digit:].]*.$'; then && grep --word-regexp -- "^${zfs_username}" "${quota_file}" | grep --only-matching --perl-regexp --quiet -- '[[:digit:].]*.$'; then
@ -322,6 +330,13 @@ main() { # {{{
user_zfs_quota="${zfs_quota}" user_zfs_quota="${zfs_quota}"
fi fi
### }}} ### }}}
### }}}
### If user don't uses any space, it has to be deleted from zfs userspace list {{{
else
### Set quota to "none" to remove this user from the list
user_zfs_quota="none"
fi
### }}}
### Try to apply the quota to the user {{{ ### Try to apply the quota to the user {{{
### OR Exit with error ### OR Exit with error
apply_zfs_userquota "${zfs_username}" "${user_zfs_quota}" \ apply_zfs_userquota "${zfs_username}" "${user_zfs_quota}" \
@ -329,7 +344,7 @@ main() { # {{{
### }}} ### }}}
done < "${zfs_user_list_path}" done < "${zfs_user_list_path}"
## Rename user list for next run of the script ## Rename user list for log
## AND exit ## AND exit
mv --force -- "${zfs_user_list_path}" "${zfs_previous_user_list_path}" \ mv --force -- "${zfs_user_list_path}" "${zfs_previous_user_list_path}" \
&& exit 0 && exit 0