USB Attach based on ID: What's wrong with this script?

I’ve hashed at this every which way. I just can’t get it to work. No errors are thrown. But the USB just doesn’t attach. What am I doing wrong.

The problem code:

# Attach USB
echo "qubes.USB $id $qube_name sys-usb all user=root" | sudo tee -a /etc/qubes/policy.d/30-user.policy
echo sys-usb $id | qvm-run -p -u root --service $qube_name qubes.USBAttach

Script in full context:

#!/bin/sh
##########################################
# Attaches USB to qube based on USB ID and then mounts it.
# Params to pass when calling:
# $1 = qube to attach USB to.
# $2 = USB ID.  Format #X#X:#X#X
# $3 = Mount name
# $4 = Mapper path
##########################################

qube_name=$1
usb_id=$2

# guard clause
if [ -z $qube_name ]; then
	msg='Enter a qube name to attach USB: '
	notify-send "$msg"; echo "$msg"
	read qube_name 
fi

qubes=$("qvm-ls") # get full list of qubes

# check to see that qube is in list.
while [[ "$qubes" != *"$qube_name"* ]]; do
	msg="$qube_name not found.  Try again: "
	notify-send "$msg"; echo "$msg"
	read qube_name 
done

if [ -z $usb_id ]; then
	msg='Enter a USB ID: '
	notify-send "$msg"; echo "$msg"
	read usb_id
fi

# check USB ID format.
l=${#usb_id}
m=${usb_id:4:1} # Get the middle character
while [ $l != 9 ] & [ $m != ':' ]; do
	msg='Check USB ID. Format should match #X#X:#X#X. Try again:'
	notify-send "$msg"; echo "$msg"
	read usb_id
	l=${#usb_id}
	m=${usb_id:4:1} # Get the middle character
done


# Format USB ID
first=${usb_id:0:4} 
last=${usb_id:6:4}
id="+0x$first.0x$last"

# Start qube if not started.

qvm-start $qube_name --skip-if-running && msg='' || msg="No qube matching $qube_name found"
notify-send "$msg"; echo "$msg"

# Attach USB
echo "qubes.USB $id $qube_name sys-usb all user=root" | sudo tee -a /etc/qubes/policy.d/30-user.policy
echo sys-usb $id | qvm-run -p -u root --service $qube_name qubes.USBAttach

# echo "qubes.USB $id $qube_name sys-usb all user=root" && msg="USB $usb_id is now attached to $qube_name." || msg="Something went wrong attaching USB $usb_id to $qube_name"
# notify-send "$msg"; echo "$msg"

# Mount USB in qube
### To be continued.



1 Like