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 {{{
### Exit
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 {{{
@ -295,13 +295,19 @@ main() { # {{{
|| 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
### Exit
is_file_present "${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." \
&& exit 0
#is_file_present "${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." \
#&& exit 0
## }}}
## Information message
@ -309,17 +315,26 @@ main() { # {{{
## Read user one by one
while IFS= read -r zfs_username; do
### Define quota to use {{{
if [ "${QUOTA_FILE_MODE}" -eq "0" ] \
&& grep --word-regexp -- "^${zfs_username}" "${quota_file}" | grep --only-matching --perl-regexp --quiet -- '[[:digit:].]*.$'; then
### From quota file if any information for this user
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." 5)"
debug_message "Get specific quota (${RED}${user_zfs_quota}${COLOR_DEBUG}) for ${RED}${zfs_username}${COLOR_DEBUG} user from quota file (${RED}${quota_file}${COLOR_DEBUG})."
### 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 {{{
if [ "${QUOTA_FILE_MODE}" -eq "0" ] \
&& grep --word-regexp -- "^${zfs_username}" "${quota_file}" | grep --only-matching --perl-regexp --quiet -- '[[:digit:].]*.$'; then
### From quota file if any information for this user
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." 5)"
debug_message "Get specific quota (${RED}${user_zfs_quota}${COLOR_DEBUG}) for ${RED}${zfs_username}${COLOR_DEBUG} user from quota file (${RED}${quota_file}${COLOR_DEBUG})."
else
### Default quota by default
user_zfs_quota="${zfs_quota}"
fi
### }}}
### }}}
### If user don't uses any space, it has to be deleted from zfs userspace list {{{
else
### Default quota by default
user_zfs_quota="${zfs_quota}"
### Set quota to "none" to remove this user from the list
user_zfs_quota="none"
fi
### }}}
### Try to apply the quota to the user {{{
@ -329,7 +344,7 @@ main() { # {{{
### }}}
done < "${zfs_user_list_path}"
## Rename user list for next run of the script
## Rename user list for log
## AND exit
mv --force -- "${zfs_user_list_path}" "${zfs_previous_user_list_path}" \
&& exit 0