rwx/sh/cryptsetup.sh

123 lines
3.2 KiB
Bash
Raw Normal View History

2025-03-30 12:18:16 +02:00
_rwx_cmd_cs() { rwx_crypt "${@}"; }
2025-03-30 21:03:06 +02:00
RWX_CRYPT_ROOT="/data/home/user/crypt"
2025-03-30 22:17:01 +02:00
RWX_CRYPT_VAR="/var/lib/crypt"
2025-03-30 12:18:16 +02:00
2025-03-30 18:50:51 +02:00
rwx_crypt_device() {
local device size
local index=0
while [ -z "${device}" ]; do
device="/dev/nbd${index}"
if [ -b "${device}" ]; then
size="$(cat /sys/block/nbd"${index}/size")"
[ "${size}" -eq 0 ] ||
device=""
else
device=""
break
fi
index=$((index + 1))
done
if [ -n "${device}" ]; then
echo "${device}"
else
rwx_log_error 1 "No device available"
fi
}
2025-03-30 12:18:16 +02:00
rwx_crypt() {
local action="${1}"
2025-03-30 12:18:16 +02:00
local action_close="close"
local action_open="open"
local mapper="/dev/mapper"
local mount_root="/media"
local crypt_arg crypt_file crypt_map crypt_mount pass_phrase
case "${action}" in
"${action_close}" | "${action_open}")
2025-03-30 18:50:51 +02:00
shift
2025-03-30 12:53:39 +02:00
local user_id
user_id="$(id --user)"
[ "${user_id}" -eq 0 ] ||
rwx_log_error 1 "Not root"
[ -n "${1}" ] ||
rwx_log_error 2 "No files"
2025-03-30 12:18:16 +02:00
[ "${action}" = "${action_open}" ] &&
pass_phrase="$(rwx_read_passphrase)"
for crypt_arg in "${@}"; do
rwx_log_info
2025-03-30 18:50:51 +02:00
crypt_file="${RWX_CRYPT_ROOT}/${crypt_arg}.qcow2"
2025-03-30 12:18:16 +02:00
if [ -f "${crypt_file}" ]; then
crypt_map="${mapper}/${crypt_arg}"
crypt_mount="${mount_root}/${crypt_arg}"
2025-03-30 22:17:01 +02:00
local device
2025-03-30 12:18:16 +02:00
case "${action}" in
"${action_open}")
2025-03-30 20:18:20 +02:00
if ! device="$(rwx_crypt_device)"; then
rwx_log_error 4 "No device available"
fi
2025-03-30 22:17:01 +02:00
# record device
if ! rwx_file_write \
"${RWX_CRYPT_VAR}/${crypt_arg}" "${device}"; then
rwx_log_error 5 "Writing failure: ${device}"
fi
2025-03-30 20:18:20 +02:00
# connect device
if ! qemu-nbd --connect "${device}" "${crypt_file}"; then
2025-03-30 22:17:01 +02:00
rwx_log_error 6 "Connection failure: ${device}"
2025-03-30 20:18:20 +02:00
fi
# open device
2025-03-30 22:17:01 +02:00
if ! echo "${pass_phrase}" |
cryptsetup luksOpen "${device}" "${crypt_arg}"; then
rwx_log_error 7 "Opening failure: ${device}"
fi
2025-03-30 20:18:20 +02:00
# make mount directory
if ! mkdir --parents "${crypt_mount}"; then
2025-03-30 22:17:01 +02:00
rwx_log_error 8 "Making failure: ${crypt_mount}"
2025-03-30 20:18:20 +02:00
fi
# mount file system
if ! mount \
--options "autodefrag,compress-force=zstd" \
"${crypt_map}" "${crypt_mount}"; then
2025-03-30 22:17:01 +02:00
rwx_log_error 9 "Mounting failure: ${crypt_map}"
2025-03-30 20:18:20 +02:00
fi
2025-03-30 12:18:16 +02:00
;;
2025-03-30 12:41:48 +02:00
"${action_close}")
2025-03-30 20:18:20 +02:00
# unmount file system
if ! umount "${crypt_mount}"; then
rwx_log_error 4 "Unmounting failure: ${crypt_mount}"
fi
# remove mount directory
if ! rmdir "${crypt_mount}"; then
rwx_log_error 5 "Removal failure: ${crypt_mount}"
fi
# close device
if ! cryptsetup luksClose "${crypt_arg}"; then
rwx_log_error 6 "Closing failure: ${crypt_arg}"
fi
2025-03-30 22:17:01 +02:00
# load device
if ! device="$(cat "${RWX_CRYPT_ROOT}/${crypt_arg}")"; then
rwx_log_error 7 "Loading failure: ${crypt_arg}"
fi
# disconnect device
if ! qemu-nbd --disconnect "${device}"; then
rwx_log_error 8 "Disconnection failure: ${device}"
fi
2025-03-30 22:18:56 +02:00
# remove record
if ! rm "${RWX_CRYPT_ROOT}/${crypt_arg}"; then
rwx_log_error 9 "Removal failure: ${crypt_arg}"
fi
2025-03-30 12:41:48 +02:00
;;
2025-03-30 12:18:16 +02:00
*) ;;
esac
else
2025-03-30 20:18:20 +02:00
rwx_log_error 3 "Not a file: ${crypt_file}"
2025-03-30 12:18:16 +02:00
fi
done
;;
*)
rwx_log_info "Usage:"
rwx_log_info "${action_close}|${action_open}"
2025-03-30 12:41:48 +02:00
# TODO list
2025-03-30 12:18:16 +02:00
;;
esac
}