neodotfiles/dmenu/scripts/dmenu_mount

44 lines
2 KiB
Text
Raw Normal View History

2024-03-28 21:40:01 +00:00
#!/usr/bin/env bash
# List of available drives
devices=$(lsblk -lpo "name,size,type,mountpoint" --noheadings | grep -v -e "disk" -e "lvm" -e "nvme" |
awk '{if ($4=="") {print $1, "(" $2 ")", "[unmounted]"} else {print $1, "(" $2 ")", "[" $4 "]"}}')
# Main menu
selected_device=$(echo -e "${devices}" | dmenu -i -p "Drive manager" | awk '{print $1}')
# Verify if a drive is selected
if [ -n "$selected_device" ]; then
mounted_path=$(lsblk -lp | grep "${selected_device}" | awk '{print $7}')
# Verify if the drive is mounted
if [ -n "$mounted_path" ]; then
# Check if the drive is encrypted
if [ "$(lsblk -n -o TYPE "${selected_device}")" == "crypt" ]; then
crypt_device=$(echo "${selected_device}" | sed -s 's/\/dev\/mapper\///')
udisksctl unmount -b "${selected_device}"
udisksctl lock -b "$crypt_device"
notify-send "The encrypted drive was unmounted successfully"
else
# If it's mounted, unmount it
udisksctl unmount -b "$selected_device"
notify-send "The drive was unmounted successfully"
fi
else
# If it's not mounted, check if it's an encrypted drive
if [ "$(lsblk -n -o FSTYPE "${selected_device}")" == "crypto_LUKS" ]; then
# If it's an encrypted drive, prompt for the passphrase and mount it
passphrase=$(echo "" | dmenu -p "Enter passphrase for ${selected_device}")
if [ -n "$passphrase" ]; then
unlocked_device=$(udisksctl unlock -b "${selected_device}" --key-file <(echo -n "$passphrase") | awk '{print $NF}' | sed 's/\.$//')
udisksctl mount -b "${unlocked_device}"
notify-send "The encrypted drive was mounted successfully"
fi
else
# If it's not an encrypted drive, mount it normally
udisksctl mount -b "$selected_device"
notify-send "The drive was mounted successfully"
fi
fi
fi