list_tomb_mounts: simplify the function

previously it had dedicated cases for listing all tombs and a singular
one, which duplicated code.
The function got reworked, that it uses a different approach for
findmnt. Instead of filtering the general result, it now uses --source
on the tomb specific crypsetup mapper. Those are searched via general
globbing of the devices in /dev/mapper. This allows to combine the
previous separate cases.
Additionally remove the usage of _sudo for findmnt, as it is not
necessary.
This commit is contained in:
Narrat 2024-08-06 14:59:51 +02:00 committed by Jaromil
parent 0b25ba6d68
commit 6df1cdeab9

26
tomb
View File

@ -2756,34 +2756,18 @@ awk "/mapper/"' { print $2 ";" $3 ";" $4 ";" $5 }'`
# #
# 5. tomb name # 5. tomb name
list_tomb_mounts() { list_tomb_mounts() {
[[ -z "$1" ]] && { # execute in subshell to avoid ZSH non-POSIX behaviour if globbing doesn't match something
# list all open tombs ( for dev in /dev/mapper/tomb*$1.*; do
_sudo findmnt -rvo SOURCE,TARGET,FSTYPE,OPTIONS,LABEL \ findmnt --source ${dev} -rnvo SOURCE,TARGET,FSTYPE,OPTIONS,LABEL \
| awk ' | awk '
BEGIN { main="" } BEGIN { main="" }
/^\/dev\/mapper\/tomb/ { {
if(main==$1) next; if(main==$1) next;
print $1 ";" $2 ";" $3 ";(" $4 ");[" $5 "]" print $1 ";" $2 ";" $3 ";(" $4 ");[" $5 "]"
main=$1 main=$1
} }
' '
} || { done ) 2>/dev/null
# list a specific tomb
# add square parens if not present (detection)
local tname
if [[ "${1[1]}" = "[" ]]; then tname="$1"
else tname="[$1]"; fi
_sudo findmnt -rvo SOURCE,TARGET,FSTYPE,OPTIONS,LABEL \
| awk -vtomb="$tname" '
BEGIN { main="" }
/^\/dev\/mapper\/tomb/ {
if("["$5"]"!=tomb) next;
if(main==$1) next;
print $1 ";" $2 ";" $3 ";(" $4 ");[" $5 "]"
main=$1
}
'
}
} }
# list_tomb_binds # list_tomb_binds