chore: added article about mounting and using an encrypted device

This commit is contained in:
Michele Cereda
2023-03-22 20:35:13 +01:00
parent a48fea3e77
commit 91ae9d6146
3 changed files with 54 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
#!/usr/bin/env sh
[[ -v DEBUG ]] && set -x
: ${DEVICE:?not set}
: ${LABEL:?not set}
: "${MAPPER_NAME:=${LABEL// /}}"
: "${MOUNT_OPTIONS:=compress-force=zstd}"
: "${MOUNT_POINT:=/mnt/$LABEL}"
: "${USERNAME:=root}"
: "${GROUPNAME:=root}"
: "${CLOSE_WHEN_DONE:=true}"
[[ $EUID -eq 0 ]] || (echo "Re-run this script with root privileges" >&2 && exit 1)
[[ -b "$DEVICE" ]] || (echo "${DEVICE} not found" >&2 && exit 1)
cryptsetup luksFormat "$DEVICE"
cryptsetup open "$DEVICE" "$MAPPER_NAME"
mkfs.btrfs -f --label "$LABEL" "/dev/mapper/${MAPPER_NAME}"
mkdir -p "$MOUNT_POINT"
mount -t btrfs -o "$MOUNT_OPTIONS" "/dev/mapper/${MAPPER_NAME}" "$MOUNT_POINT"
btrfs subvolume create "$MOUNT_POINT/.snapshots"
btrfs subvolume create "$MOUNT_POINT/data"
chown "$USERNAME":"$GROUPNAME" "$MOUNT_POINT/data"
if [[ "$CLOSE_WHEN_DONE" == false ]]
then
umount "/mnt/${LABEL}"
cryptsetup close "/dev/mapper/${MAPPER_NAME}"
fi
[[ -v "$DEBUG" ]] && set +x