From 1eb2a9ff79c0fcbabf84a07d52dbdaa6e46304f3 Mon Sep 17 00:00:00 2001 From: Dmitry Voronin Date: Fri, 11 Oct 2024 21:47:47 +0300 Subject: [PATCH] Disk : Add funlock, fmount and funmount. --- home/program/bash/module/Disk.nix | 66 ++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 24 deletions(-) diff --git a/home/program/bash/module/Disk.nix b/home/program/bash/module/Disk.nix index 4e6a088..82cd01e 100644 --- a/home/program/bash/module/Disk.nix +++ b/home/program/bash/module/Disk.nix @@ -13,40 +13,58 @@ } # Unlock encrypted disk file. - # Usage: funlock + # Usage: funlock function funlock() { - if [[ "''${UID}" != 0 ]]; then - _error "Must be root." - return 2 - fi - local file="''${1}" - local dir="''${2}" - if [[ "''${dir}" = "" ]]; then + if [[ "''${file}" = "" ]]; then help funlock return 2 fi local name=$(parse_alnum "''${file##*/}") - cryptsetup open "''${file}" "''${name}" - mount "/dev/mapper/''${name}" "''${dir}" + + local loop=$(udisksctl loop-setup --no-user-interaction --file "''${file}") + loop="''${loop##* }"; loop="''${loop%.}" + + local decrypted=$(udisksctl unlock --block-device "''${loop}") + decrypted="''${decrypted##* }"; decrypted="''${decrypted%.}" + + local mount=$(udisksctl mount --no-user-interaction --block-device "''${decrypted}") + mount="''${mount#* at }" + + cd "''${mount}" } - # Unlock encrypted disk file. - # Usage: unlock - # function unlock() { - # _filter() { - # sed -e "s/.*\ a[st]\ //" -e "s/\.$//" - # } - # local file="''${1}" - # local name=$(parse_alnum ''${file} | _filter) - # local loop=$(udisksctl loop-setup -f "''${file}" | _filter) - # local unlock=$(udisksctl unlock -b "''${loop}" | _filter) - # local mount=$(udisksctl mount -b "''${unlock}" | _filter) + # Mount file. + # Usage: fmount + function fmount() { + local file="''${1}" + if [[ "''${file}" = "" ]]; then + help fmount + return 2 + fi - # [ -L "./''${name}" ] || ln -s "''${mount}" "./''${name}" - # cd "''${mount}" - # } + local loop=$(udisksctl loop-setup --no-user-interaction --file "''${file}") + loop="''${loop##* }"; loop="''${loop%.}" + + local mount=$(udisksctl mount --no-user-interaction --block-device "''${loop}") + mount="''${mount#* at }" + + cd "''${mount}" + } + + # Unmount file. + # Usage: fumount + function fumount() { + local loop="''${1}" + if [[ "''${loop}" = "" ]]; then + help fumount + return 2 + fi + + udisksctl unmount --no-user-interaction --block-device "''${loop}" + udisksctl loop-delete --no-user-interaction --block-device "''${loop}" + } ''; }