!/bin/sh

plug - wrapper for udisksctl for LUKS-encrypted devices

SPDX-FileCopyrightText: 2023 Daniel Kalak

SPDX-License-Identifier: GPL-3.0-or-later

This program depends on udisks2, grep, and the GNU coreutils.

The program assumes that there is a file $DISKSTXT. Empty lines and

lines starting with "#" are ignored. All other lines represent one

LUKS-encrypted device each. The lines are assumed to have 3

whitespace-separated fields: the mount directory (usually

/run/media/USER/FSLABEL; do not choose it on your own, but state the

one udisksctl tells you during your first manual mount), the device

file name of the LUKS-encrypted partition ("locked"), and the device

file name of the decrypted/mapped partition containing the file system

("unlocked"). It is recommended to use device file names of the form

/dev/disk/by-uuid/UUID; you can look up a UUID with lsblk -f.

With the command "in", the program unlocks and mounts all available

disks in $DISKSTXT using udisksctl. With "out", the program unmounts,

locks, and powers off all available disks in $DISKSTXT using

udisksctl.

The program returns 0 on successful, 1 on bad, and 2 on idle usage.

usage_quit() {

printf 'Usage: %s in|out\n'           "$(basename "$0")" >&2

printf 'Environment variable DISKSTXT needs to be set\n' >&2

exit 1

}

[ "$DISKSTXT" ] || usage_quit

[ "$#" = 1 ] || usage_quit

case "$1" in

'in'|'out') direction="$1" ;;

     *) usage_quit     ;;

esac

grep -v -e '^#' -e '^$' "$DISKSTXT" |

{

while read -r mountdir locked unlocked

do

	# Skip if the device isn't even plugged in.

	[ -b "$locked" ] || continue

	case "$direction" in

	'in')

		# Skip if it's already mounted.

		[ -d "$mountdir" ] && continue

		printf 'Plugging in %s...\n' "$mountdir"

		[ -b "$unlocked" ] || udisksctl unlock -b "$locked"

		udisksctl mount -b "$unlocked"

		did_something=yes

		;;

	'out')

		printf 'Unplugging %s...\n' "$mountdir"

		[ -d "$mountdir" ] && udisksctl unmount -b "$unlocked"

		[ -b "$unlocked" ] && udisksctl lock -b "$locked"

		udisksctl power-off -b "$locked"

		did_something=yes

		;;

	esac

done

if ! [ "$did_something" ]

then

	printf 'Nothing to do\n' >&2

	exit 2

fi

}

The return value of the block above is passed on by default. We can

make this explicit with "exit $?".

Proxy Information
Original URL
gemini://dkalak.de/software/plug.sh
Status Code
Success (20)
Meta
text/plain
Capsule Response Time
106.487264 milliseconds
Gemini-to-HTML Time
0.944907 milliseconds

This content has been proxied by September (3851b).