/path/to/directory/
is a path that points within an encrypted volume, to an arbitrary depth. In a bash script I need to determine if the block device related to this path is a removable device. I'm using Arch Linux.
I have looked at a lot of similar questions (such as these listed below and others) but did not find a suitable answer:
- linux - How to determine which sd* is usb? - Unix & Linux Stack Exchange
- bash - How to know if /dev/sdX is a connected USB or HDD? - Unix & Linux Stack Exchange
- mount - How do I know the device path to an USB-stick? - Ask Ubuntu
This is an example of what I'm working with:
findmnt -n -o SOURCE --target /path/to/directory/
/dev/mapper/luksdev[/@subvolume]
findmnt -D --target /path/to/directory
SOURCE FSTYPE SIZE USED AVAIL USE% TARGET
/dev/mapper/luksdev[/@subvolume] btrfs 4.5T 203.5G 4.3T 4% /path/to/directory
df -P /path/to/directory/ | awk 'END{print $1}'
/dev/mapper/luksdev
(The findmnt parameter --target
seems to be required if the path is not the exact mountpoint.)
If the script can determine the block device (e.g., /dev/sda1
) associated with /dev/mapper/luksdev
, I get a step closer:
udevadm info --query=all --name=/dev/sda1 | grep ID_BUS | grep "=usb"
E: ID_BUS=usb
But I assume not all removable devices are usb, right?
By the way, I am OK with methods specific to BTRFS, if that makes this any easier. I did check:
EDIT: based on answer by Vojtech Trefny, here is what I have:
mapper_path=$(findmnt -n -o SOURCE --target /path/to/directory/ | cut -d [ -f 1)
mydev=$(lsblk -sl -o NAME /${mapper_path} | tail -n 1)
drive_name=$(udisksctl info -b /dev/${mydev} | grep "Drive:" | cut -d"'" -f2)
drive_name=$(echo $drive_name | sed -e 's|/org/freedesktop/UDisks2/drives/||')
udisksctl info -d ${drive_name} | grep "\sRemovable:" | cut -d":" -f2 | tr -d "[:blank:]"
busctl
orudisksctl
I get the errorError looking up object for drive /org/freedesktop/UDisks2/drives/SanDisk_Cruzer_Blade_XYZ123456789
. If I use/sys/block/sdX/removable
it does give the expected value of1
for USB sticks, but for a removable USB HDD (WD Elements) it reports 0. – MountainX Oct 10 '20 at 07:20realpath /sys/block/sda
will tell you the full path to the device. Starting from there, you can make some guesses. – Oct 10 '20 at 07:41udisksctl dump
? If thesda
object says the drive is/org/freedesktop/UDisks2/drives/SanDisk_Cruzer_Blade_XYZ123456789
and then the drive object is not there, that's a bug in UDisks, unfortunately. – Vojtech Trefny Oct 10 '20 at 07:47