2013-03-20 05:27:32 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
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
|
|
|
|
XS_KEY="qubes-block-devices/$NAME"
|
|
|
|
|
|
|
|
xs_remove() {
|
|
|
|
if [ "$QUBES_EXPOSED" == "1" ]; then
|
|
|
|
xenstore-rm "$XS_KEY"
|
|
|
|
fi
|
|
|
|
echo QUBES_EXPOSED=0
|
|
|
|
}
|
|
|
|
|
|
|
|
# Ignore mounted...
|
|
|
|
if fgrep -q $DEVNAME /proc/mounts; then
|
|
|
|
xs_remove
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
# ... and used by device-mapper
|
|
|
|
if [ -n "`ls -A /sys/$DEVPATH/holders 2> /dev/null`" ]; then
|
|
|
|
xs_remove
|
|
|
|
exit 0
|
|
|
|
fi
|
2014-06-07 02:53:32 +00:00
|
|
|
# ... and used device-mapper devices
|
2014-07-26 13:47:15 +00:00
|
|
|
if [ -n "$DM_NAME" ] && /sbin/dmsetup info "$DM_NAME" | grep -q "^Open count:.*[1-9]"; then
|
2014-06-07 02:53:32 +00:00
|
|
|
xs_remove
|
|
|
|
exit 0
|
|
|
|
fi
|
2013-03-20 05:27:32 +00:00
|
|
|
# ... and "empty" loop devices
|
|
|
|
if [ "$MAJOR" -eq 7 -a ! -d /sys/$DEVPATH/loop ]; then
|
|
|
|
xs_remove
|
|
|
|
exit 0
|
|
|
|
fi
|
2015-03-30 02:54:37 +00:00
|
|
|
# ... and temporary device-mapper devices used during VM startup
|
|
|
|
if [[ "$DM_NAME" = 'loop'* ] && \
|
|
|
|
[[ "`cat /sys/block/${DM_NAME%p*}/loop/backing_file`" = \
|
|
|
|
'/var/lib/qubes/'*'/volatile.img' ]]; then
|
|
|
|
xs_remove
|
|
|
|
exit 0
|
|
|
|
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
|
|
|
|
|
|
|
# Get lock only in dom0 - there are so many block devices so it causes xenstore
|
|
|
|
# deadlocks sometimes.
|
|
|
|
if [ -f /etc/qubes-release ]; then
|
|
|
|
# Skip xenstore-write if cannot obtain lock. This can mean very early system startup
|
|
|
|
# stage without /run mounted (or populated). Devices will be rediscovered later
|
|
|
|
# by qubes-core startup script.
|
|
|
|
exec 9>>/var/run/qubes/block-xenstore.lock || exit 0
|
|
|
|
flock 9
|
|
|
|
fi
|
|
|
|
|
|
|
|
xenstore-write "$XS_KEY/desc" "$DESC" "$XS_KEY/size" "$SIZE" "$XS_KEY/mode" "$MODE"
|
|
|
|
echo QUBES_EXPOSED=1
|
|
|
|
|
|
|
|
# Make sure that block backend is loaded
|
|
|
|
/sbin/modprobe xen-blkback 2> /dev/null || /sbin/modprobe blkbk
|