2013-03-20 05:27:32 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2016-05-16 02:36:17 +00:00
|
|
|
shopt -s nullglob
|
|
|
|
|
2016-05-17 20:41:51 +00:00
|
|
|
|
2014-07-05 14:13:08 +00:00
|
|
|
export LC_CTYPE=en_US.UTF-8
|
2013-03-20 05:27:32 +00:00
|
|
|
NAME=${DEVNAME#/dev/}
|
2014-07-05 14:13:08 +00:00
|
|
|
DESC="`echo "${ID_MODEL} (${ID_FS_LABEL})" | iconv -f utf8 -t ascii//TRANSLIT`"
|
2013-03-20 05:27:32 +00:00
|
|
|
SIZE=$[ $(cat /sys/$DEVPATH/size) * 512 ]
|
|
|
|
MODE=w
|
2013-06-07 03:21:41 +00:00
|
|
|
QDB_KEY="/qubes-block-devices/$NAME"
|
2013-03-20 05:27:32 +00:00
|
|
|
|
|
|
|
xs_remove() {
|
2016-07-15 16:15:41 +00:00
|
|
|
if is_attached /sys$DEVPATH; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2017-09-12 02:25:34 +00:00
|
|
|
if qubesdb-read -q "$QDB_KEY/desc" >/dev/null; then
|
2016-01-14 04:01:27 +00:00
|
|
|
qubesdb-rm "$QDB_KEY/"
|
2013-06-07 03:21:41 +00:00
|
|
|
qubesdb-write /qubes-block-devices ''
|
2013-03-20 05:27:32 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-05-16 02:36:17 +00:00
|
|
|
is_used() {
|
|
|
|
local sys_devpath=$1
|
2016-07-17 03:08:25 +00:00
|
|
|
local devname=$(grep ^DEVNAME= $sys_devpath/uevent | cut -f 2 -d =)
|
2016-07-15 16:15:41 +00:00
|
|
|
# mounted; or enabled swap
|
2016-07-17 03:08:25 +00:00
|
|
|
if lsblk -dnr -o MOUNTPOINT "/dev/$devname" | grep -q .; then
|
2016-05-16 02:36:17 +00:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
# part of other device-mapper
|
|
|
|
if [ -n "`ls -A $sys_devpath/holders 2> /dev/null`" ]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
# open device-mapper device
|
|
|
|
if [ -f "$sys_devpath/dm/name" ] && \
|
|
|
|
/sbin/dmsetup info "$(cat $sys_devpath/dm/name)" |\
|
|
|
|
grep -q "^Open count:.*[1-9]"; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-09-12 02:25:34 +00:00
|
|
|
refresh_another() {
|
|
|
|
# launch this script for other device
|
|
|
|
local devpath=$1
|
|
|
|
local launch_env=$(udevadm info -q all -x -p "$devpath" \
|
2017-09-15 03:25:42 +00:00
|
|
|
| grep ^E: | cut -d ' ' -f 2- | tr ' ' ':')
|
2017-09-12 02:25:34 +00:00
|
|
|
env -i PATH=$PATH $launch_env $0
|
|
|
|
}
|
|
|
|
|
2016-05-16 02:39:19 +00:00
|
|
|
is_attached() {
|
|
|
|
dev_hex=$(stat -c %t:%T /dev/$(basename $1))
|
2017-09-12 02:25:34 +00:00
|
|
|
if [ -z "$dev_hex" -o "$dev_hex" = "0:0" ]; then
|
|
|
|
return 1
|
|
|
|
fi
|
2017-11-08 02:09:54 +00:00
|
|
|
# looking at sysfs is much faster than looking at xenstore
|
|
|
|
# this code requires no subprocesses and doesn't hit argument length limitations
|
|
|
|
for i in /sys/bus/xen-backend/drivers/vbd/vbd-*/physical_device; do
|
|
|
|
read i_dev_hex < "$i"
|
|
|
|
if test "$i_dev_hex" == "$dev_hex"; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
return 1
|
2016-05-16 02:39:19 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 02:36:17 +00:00
|
|
|
# update info about parent devices, if any:
|
|
|
|
if [ -f /sys$DEVPATH/partition ]; then
|
|
|
|
parent=$(dirname $(readlink -f /sys$DEVPATH))
|
2017-09-12 02:25:34 +00:00
|
|
|
refresh_another /$(realpath --relative-to=/sys $parent)
|
2016-05-16 02:39:19 +00:00
|
|
|
# if parent device is already attached, skip its partitions
|
|
|
|
if is_attached $parent; then
|
|
|
|
xs_remove
|
|
|
|
exit 0
|
|
|
|
fi
|
2013-03-20 05:27:32 +00:00
|
|
|
fi
|
2016-05-16 02:36:17 +00:00
|
|
|
|
|
|
|
# and underlying devices of device-mapper (if any)
|
|
|
|
for dev in /sys$DEVPATH/slaves/*; do
|
2017-09-12 02:25:34 +00:00
|
|
|
refresh_another /$(realpath --relative-to=/sys $dev)
|
2016-05-16 02:36:17 +00:00
|
|
|
done
|
|
|
|
|
2017-09-12 02:25:34 +00:00
|
|
|
# cache slave devices for remove event
|
|
|
|
if [ -n "$DM_NAME" ]; then
|
|
|
|
ls -1 /sys$DEVPATH/slaves/ > /var/run/qubes/block-slave-cache-$NAME
|
|
|
|
fi
|
|
|
|
|
2016-05-16 02:36:17 +00:00
|
|
|
# then take care of this device:
|
|
|
|
|
2017-09-12 02:25:34 +00:00
|
|
|
# udev rules already excluded this device:
|
|
|
|
|
|
|
|
if [ "$DM_UDEV_DISABLE_DISK_RULES_FLAG" = "1" ]; then
|
|
|
|
xs_remove
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2016-05-16 02:36:17 +00:00
|
|
|
# device itself is already used
|
|
|
|
if is_used /sys$DEVPATH; then
|
2014-06-07 02:53:32 +00:00
|
|
|
xs_remove
|
|
|
|
exit 0
|
|
|
|
fi
|
2016-05-16 02:36:17 +00:00
|
|
|
|
|
|
|
# or one of its partitions is used
|
2016-05-16 02:39:19 +00:00
|
|
|
# or already attached (prevent attaching both device and its partition(s) at
|
|
|
|
# the same time)
|
2016-05-16 02:36:17 +00:00
|
|
|
for part in /sys$DEVPATH/$NAME*; do
|
|
|
|
if [ -d $part ]; then
|
2016-07-15 16:15:42 +00:00
|
|
|
if is_used $part || is_attached $part; then
|
2016-05-16 02:39:19 +00:00
|
|
|
xs_remove
|
|
|
|
exit 0
|
|
|
|
fi
|
2016-05-16 02:36:17 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
# or "empty" loop device
|
2013-03-20 05:27:32 +00:00
|
|
|
if [ "$MAJOR" -eq 7 -a ! -d /sys/$DEVPATH/loop ]; then
|
|
|
|
xs_remove
|
|
|
|
exit 0
|
|
|
|
fi
|
2016-05-16 02:36:17 +00:00
|
|
|
|
2017-12-30 21:27:28 +00:00
|
|
|
# or unconnected Network Block Device
|
|
|
|
if [ "$MAJOR" -eq 43 -a ! -e /sys/$DEVPATH/pid ]; then
|
|
|
|
xs_remove
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2017-09-12 02:25:34 +00:00
|
|
|
# ... and loop devices from excluded directories
|
|
|
|
if [[ "$NAME" = 'loop'* ]]; then
|
2017-09-15 03:25:42 +00:00
|
|
|
backing_file=$(cat /sys/block/${NAME}/loop/backing_file)
|
2017-09-12 02:25:34 +00:00
|
|
|
if [ -n "$backing_file" ]; then
|
|
|
|
dir_to_check=$(dirname "$backing_file")
|
|
|
|
while [ "$dir_to_check" != "/" -a "$dir_to_check" != "." ]; do
|
|
|
|
if [ -e "$dir_to_check/.qubes-exclude-block-devices" ]; then
|
|
|
|
xs_remove
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
dir_to_check=$(dirname "$dir_to_check")
|
|
|
|
done
|
|
|
|
fi
|
2015-03-30 02:54:37 +00:00
|
|
|
fi
|
2013-03-20 05:27:32 +00:00
|
|
|
|
2013-10-01 02:19:14 +00:00
|
|
|
# Check if device is read-only
|
|
|
|
if [ "`cat /sys/$DEVPATH/ro`" -eq 1 ]; then
|
|
|
|
MODE=r
|
|
|
|
fi
|
|
|
|
|
2013-03-20 05:27:32 +00:00
|
|
|
# Special case for CD
|
|
|
|
if [ "$ID_TYPE" = "cd" ]; then
|
|
|
|
if [ "$ID_CDROM_MEDIA" != "1" ]; then
|
|
|
|
# Hide empty cdrom drive
|
|
|
|
xs_remove
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
MODE=r
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Special description for loop devices
|
|
|
|
if [ -d /sys/$DEVPATH/loop ]; then
|
|
|
|
DESC=$(cat /sys/$DEVPATH/loop/backing_file)
|
|
|
|
fi
|
2014-06-07 02:53:32 +00:00
|
|
|
# and for device-mapper
|
|
|
|
if [ -n "$DM_NAME" ]; then
|
|
|
|
DESC="$DM_NAME"
|
|
|
|
fi
|
2013-03-20 05:27:32 +00:00
|
|
|
|
2013-06-07 03:21:41 +00:00
|
|
|
# The last one is meant to trigger watches
|
|
|
|
qubesdb-write \
|
|
|
|
"$QDB_KEY/desc" "$DESC" \
|
2014-12-11 05:06:57 +00:00
|
|
|
"$QDB_KEY/size" "$SIZE" \
|
|
|
|
"$QDB_KEY/mode" "$MODE" \
|
2013-06-07 03:21:41 +00:00
|
|
|
/qubes-block-devices ''
|
2013-03-20 05:27:32 +00:00
|
|
|
|
|
|
|
# Make sure that block backend is loaded
|
|
|
|
/sbin/modprobe xen-blkback 2> /dev/null || /sbin/modprobe blkbk
|