Skip to main content

Add a Google Coral USB Init Script to an LXC Container

Set up the DFU script

  1. Install the DFU utility:

    apt install dfu-util
  2. Create a file called /var/lib/vz/snippets/coral-init.sh with this content:

    #!/bin/bash
    grant_coral_usb() {
    # DMC 12-3-24: this does not work with less than 666
    echo "Granting access to Coral so it can be accessed inside container" | tee -a /var/log/coral-init.log
    chmod 666 $(lsusb | grep 18d1:9302 | sed -nr 's|Bus (.*) Device ([^:]*):.*|/dev/bus/usb/\1/\2|p')
    }
    if [ "$2" = "pre-start" ]; then
    echo "Running pre-start hookscript for VM $1" > /var/log/coral-init.log
    lsusb | grep 18d1:9302 > /dev/null
    if [ $? -eq 0 ]; then
    echo "Google Coral TPU already intialized!" | tee -a /var/log/coral-init.log
    grant_coral_usb
    exit 0
    fi
    lsusb | grep 1a6e:089a > /dev/null
    if [ $? -ne 0 ]; then
    echo -e "Didn't detect uninitialized Google Coral TPU:\n$(lsusb)" | tee -a /var/log/coral-init.log
    exit 1
    fi
    echo "Initializing coral..." | tee -a /var/log/vm-pre-start.log
    dfu-util -D /var/lib/vz/snippets/coral-bin/apex_latest_single_ep.bin -d "1a6e:089a" -R
    if [ $? -ne 251 ]; then
    echo "Error occured!" | tee -a /var/log/coral-init.log
    exit 2
    fi
    sleep 2 # pause to allow usb to settle
    grant_coral_usb
    echo -e "Done! USBs:\n$(lsusb)" | tee -a /var/log/coral-init.log
    fi
  3. Make it executable:

    chmod 744 coral-init.sh
  4. Create the folder /var/lib/vz/snippets/coral-bin.

  5. Download the firmware:

    wget https://github.com/google-coral/libedgetpu/raw/master/driver/usb/apex_latest_single_ep.bin
  6. Add the script to the LXC config by running:

    pct set 109 -hookscript local:snippets/coral-init.sh

Add a udev rule (probably not needed)

This was tried but may not actually be necessary — kept here for reference in case a future setup needs it.

Current version:

SUBSYSTEMS=="usb", ATTRS{idVendor}=="1a6e", ATTRS{idProduct}=="089a", MODE="0664", TAG+="uaccess", SYMLINK+="usbedgetpu"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="9302", MODE="0664", TAG+="uaccess", SYMLINK+="usbedgetpu"

Old version:

SUBSYSTEM=="usb",ATTRS{idVendor}=="1a6e",GROUP="plugdev"
SUBSYSTEM=="usb",ATTRS{idVendor}=="18d1",GROUP="plugdev"