USB passthrough to LXC problem

The problem is that it seems that hookscripts (even pre-start stage) run when the container has started
I indicated above that you may have timing problems.

A possible workaround - try adding a post-stop piece in the hookscript to delete the USB device - this way the container will start fresh again without any device that can again be added on the container start.
 
A possible workaround - try adding a post-stop piece in the hookscript to delete the USB device - this way the container will start fresh again without any device that can again be added on the container start.
Oh of course! It was 3AM, my brain was already fried o_O

That should do the trick, I will share the whole script once finished and tested (for any future visitor) :)
 
Just to post the last version of the code.
I needed a temp file to avoid an infinite loop of rebooting to apply changes and adding/deleting device, but it works fine now:

/var/lib/vz/snippets/usb-hookscript.sh:
Bash:
#!/bin/bash

if [ $2 == "pre-start" ]
then
  # Execute in a separate script to bypass PCT lock
  echo "Preparing LxC $1 before start"
  nohup /var/lib/vz/snippets/helpers/set-usb-device.sh $1 > /var/lib/vz/snippets/helpers/latest.log 2>&1 &
elif [ $2 == "post-stop" ]
then
  if [ -f "/var/lib/vz/snippets/helpers/.reboot-$1" ]
  then
    echo "LxC $1 is rebooting to assign the correct device"
  else
    pct set $1 -del dev0
  fi
fi

/var/lib/vz/snippets/helpers/set-usb-device.sh:
Bash:
#!/bin/bash

if [ $1 == 104 ] # NUT VM
then
  echo "LxC $1 (NUT) is starting"
  idVendor="051d"
  idProduct="0002"
elif [ $1 == 107 ] # Frigate VM
then
  echo "LxC $1 (Frigate) is starting"
  idVendor="18d1"
  idProduct="9302"
fi

currentPath="$(pct config $1 | grep dev0 | cut -d' ' -f2 | cut -d'=' -f2)"
newPath="/dev/bus/usb/$(lsusb| grep $idVendor:$idProduct | cut -d ':' -f1 | cut -d ' ' -f2,4 | sed 's| |/|g')"

if [ "$currentPath" = "$newPath" ]
then
  echo "Path $currentPath is correctly set for LxC $1"
  rm "/var/lib/vz/snippets/helpers/.reboot-$1"
else
  echo "Updating LxC $1 dev0 to $newPath"
  pct set $1 --dev0 path=$newPath
  touch "/var/lib/vz/snippets/helpers/.reboot-$1"
  #Rebooting to apply changes
  pct reboot $1
fi
 
I've added a support for add USB devices via tags - it's not need to edit every time shell script
Code:
#!/bin/bash
vmid=$1
for usb in $(pct config $vmid | grep tags: | cut -d ' ' -f2,4 | tr ';' '\n' | grep 'usb-' | sed 's/usb-//g' | sed 's/-/:/g')
do
    currentPath="$(pct config $vmid | grep dev0 | cut -d' ' -f2 | cut -d'=' -f2)"
    newPath="/dev/bus/usb/$(lsusb| grep $usb | cut -d ':' -f1 | cut -d ' ' -f2,4 | sed 's| |/|g')"
    if [ "$currentPath" = "$newPath" ]
    then
        echo "Path $currentPath is correctly set for LxC $vmid"
        rm "/mnt/pve/nas/snippets/helpers/.reboot-$vmid"
    else
        echo "Updating LxC $vmid dev0 to $newPath"
        pct set $vmid --dev0 path=$newPath
        touch "/mnt/pve/nas/snippets/helpers/.reboot-$vmid"
        pct reboot $vmid
    fi
done

Then just add tag to lxc like: usb-vendor-product (ex. usb-0a5c-4500)
 
  • Like
Reactions: kRowone9