neodotfiles/new-config/.config/rofi/scripts/rofi_mount

38 lines
1.6 KiB
Text
Raw Normal View History

2023-04-04 07:00:35 +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 "]"}}')
# Rofi menu
selected_device=$(echo -e "${devices}" | rofi -dmenu -i -p "Drive manager" | awk '{print $1}')
# Verify if the drive is mounted
if [ "$(lsblk -lp | grep "${selected_device}" | awk '{print $7}')" ]; then
# Check if the drive is encrypted
if [ "$(lsblk -n -o TYPE "${selected_device}")" == "crypt" ]; then
mmg_device=$(echo "${selected_device}" | sed -s 's/\/dev\/mapper\///')
udisksctl unmount -b "${selected_device}"
pkexec cryptsetup close "${mmg_device}"
exit 1
else
# If it's mounted, unmount it
udisksctl unmount -b "$selected_device"
notify-send "The drive was unmounted successfully"
exit 1
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=$(rofi -dmenu -p "Enter passphrase for ${selected_device}" -password)
if [ -n "$passphrase" ]; then
echo "$passphrase" | pkexec cryptsetup open "${selected_device}" encrypted_"${selected_device##*/}"
udisksctl mount -b /dev/mapper/encrypted_"${selected_device##*/}"
notify-send "The encrypted drive was mounted successfully"
exit 1
fi
fi
udisksctl mount -b "$selected_device"
notify-send "The drive was mounted successfully"
exit 1
fi