Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
usb not settled when udev rule triggered [solved]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware
View previous topic :: View next topic  
Author Message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5941

PostPosted: Sun Jan 06, 2019 5:37 pm    Post subject: usb not settled when udev rule triggered [solved] Reply with quote

Greetings,

I have a udev rule that triggers a script every add and remove, the script scans all running vms and see if they have that usb as guest passthrough and andle the attach or detach.
the detach work but the attach fails with the following error;
Code:
error: Failed to attach device from /dev/stdin
error: internal error: unable to execute QEMU command 'device_add': failed to find host usb device 1:13

some googling suggested the device didn't settled yet. how can I make sure that the device is settled before I pass it to the guest?
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein


Last edited by DaggyStyle on Wed Feb 06, 2019 8:31 pm; edited 1 time in total
Back to top
View user's profile Send private message
ribx
Apprentice
Apprentice


Joined: 20 Nov 2003
Posts: 219
Location: germany

PostPosted: Mon Jan 07, 2019 12:09 am    Post subject: Reply with quote

You could either write a wrapper script, that waits for the device or maybe your udev rule could be altered to run at a time, when the device is settle. Please provide the udev rule and the command/script that attaches the VM's usb.
_________________
The adopt an unanswered post initiative
Back to top
View user's profile Send private message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5941

PostPosted: Tue Jan 08, 2019 3:30 pm    Post subject: Reply with quote

here:
Code:

igor@utils_server:~$ cat /etc/udev/rules.d/90-usb-libvirt-hotplug.rules /usr/local/bin/libvirt_passthrough_hotplug.sh
ACTION=="add", SUBSYSTEMS=="usb", RUN+="/usr/local/bin/libvirt_passthrough_hotplug.sh"
ACTION=="remove", SUBSYSTEMS=="usb", RUN+="/usr/local/bin/libvirt_passthrough_hotplug.sh"
#!/bin/bash -x
exec 2>>/tmp/err.log

temp_xml_file="$(mktemp /tmp/vm_xml_dump.XXXXXX)"
cmd=

function error {
        echo "Error: $*"
        exit 1
}

function handle_vm_usb {
        local domain=$1
        local busnum="$(echo ${BUSNUM} | sed 's/^0\+//g')"
        local devnum="$(echo ${DEVNUM} | sed 's/^0\+//g')"

        virsh --connect qemu:///system "${cmd}" "${domain}" /dev/stdin << END
<hostdev mode='subsystem' type='usb'>
  <source>
   <address bus='${busnum}' device='${devnum}' />
  </source>
</hostdev>
END
}

function match_usb_ids {
        local domain="$1"
        local file="$2"
        local vid="$(grep vendor ${file} | sed 's/\//=/g' | cut -f 2 -d '=' | sed "s/'//g;s/^0x//g")"
        local did="$(grep product ${file} | sed 's/\//=/g' | cut -f 2 -d '=' | sed "s/'//g;s/^0x//g")"
        local ret_val=1

        if [ "${vid}" = "${ID_VENDOR_ID}" -a "${did}" = "${ID_MODEL_ID}" ]; then
                ret_val=0
                handle_vm_usb "${domain}"
        fi

        return ${ret_val}
}

function scan_vm {
        local vm_name="$1"

        virsh -c qemu:///system dumpxml ${vm_name} > ${temp_xml_file}
        for lineno in $(grep -n "<hostdev" ${temp_xml_file} | cut -f 1 -d : | sed 's/-$//g'); do
                local end_sec=$(cat -n ${temp_xml_file} | tail -n +${lineno} | grep "</hostdev>" | head -1 | awk '{print $1}')
                local type

                sed -n "${lineno},${end_sec}p;$((end_sec+1))q" ${temp_xml_file} > ${temp_xml_file}1
                type="$(head -1 ${temp_xml_file}1 | tr ' ' '\n' | grep type | cut -f 2 -d = | sed "s/'//g")"
                case ${type} in
                        usb)
                                match_usb_ids "${vm_name}" "${temp_xml_file}1"
                                if [ $? -eq 0 ]; then
                                        break
                                fi
                        ;;

                        *)
                                echo "unsupported type ${type}"
                        ;;
                esac

        done

        rm -rf ${temp_xml_file}*
}

if [ -z "${BUSNUM}" ]; then
        error BUSNUM env var not set
fi

if [ -z "${DEVNUM}" ]; then
        error DEVNUM env var not set
fi

if [ -z "${ID_VENDOR_ID}" ]; then
        error ID_VENDOR_ID env var not set
fi

if [ -z "${ID_MODEL_ID}" ]; then
        error ID_MODEL_ID env var not set
fi

case ${ACTION} in
        add)
                cmd='attach-device'
        ;;

        remove)
                cmd='detach-device'
        ;;

        *)
                error unsupported action "${ACTION}"
        ;;
esac

for vm in $(virsh -c qemu:///system list | grep running | awk '{print $2}'); do
        scan_vm "${vm}"
done

_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Back to top
View user's profile Send private message
Fitzcarraldo
Advocate
Advocate


Joined: 30 Aug 2008
Posts: 2056
Location: United Kingdom

PostPosted: Wed Jan 09, 2019 4:24 am    Post subject: Reply with quote

I'm not sure, but if the USB devices are mass storage devices you could try reducing delay_use to zero:

Code:
echo 0 > /sys/module/usb_storage/parameters/delay_use


Quote:
usb-storage.delay_use=
                        [UMS] The delay in seconds before a new device is
                        scanned for Logical Units (default 1)

Ref. https://github.com/torvalds/linux/blob/master/Documentation/admin-guide/kernel-parameters.txt
_________________
Clevo W230SS: amd64, VIDEO_CARDS="intel modesetting nvidia".
Compal NBLB2: ~amd64, xf86-video-ati. Dual boot Win 7 Pro 64-bit.
OpenRC systemd-utils[udev] elogind KDE on both.

My blog
Back to top
View user's profile Send private message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5941

PostPosted: Wed Jan 09, 2019 12:52 pm    Post subject: Reply with quote

the device in question is a usb tv tuner.

maybe the only way is to have a active thread that awaits for a signal and when the rule is triggered, it signals that thread if the input is valid.
the thread will wait for 5 secs and initiate the action.
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Back to top
View user's profile Send private message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5941

PostPosted: Wed Feb 06, 2019 8:31 pm    Post subject: Reply with quote

solved by pinging a daemon
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum