View previous topic :: View next topic |
Author |
Message |
DaggyStyle Watchman
![Watchman Watchman](/images/ranks/rank-G-2-watchman.gif)
![](images/avatars/182793287489b53393316c.gif)
Joined: 22 Mar 2006 Posts: 5941
|
Posted: Sun Jan 06, 2019 5:37 pm Post subject: usb not settled when udev rule triggered [solved] |
|
|
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 |
|
![](templates/gentoo/images/spacer.gif) |
ribx Apprentice
![Apprentice Apprentice](/images/ranks/rank_rect_2.gif)
Joined: 20 Nov 2003 Posts: 219 Location: germany
|
Posted: Mon Jan 07, 2019 12:09 am Post subject: |
|
|
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 |
|
![](templates/gentoo/images/spacer.gif) |
DaggyStyle Watchman
![Watchman Watchman](/images/ranks/rank-G-2-watchman.gif)
![](images/avatars/182793287489b53393316c.gif)
Joined: 22 Mar 2006 Posts: 5941
|
Posted: Tue Jan 08, 2019 3:30 pm Post subject: |
|
|
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 |
|
![](templates/gentoo/images/spacer.gif) |
Fitzcarraldo Advocate
![Advocate Advocate](/images/ranks/rank-G-1-advocate.gif)
![](images/avatars/32674073548b9c27b587f8.jpg)
Joined: 30 Aug 2008 Posts: 2056 Location: United Kingdom
|
Posted: Wed Jan 09, 2019 4:24 am Post subject: |
|
|
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 |
|
![](templates/gentoo/images/spacer.gif) |
DaggyStyle Watchman
![Watchman Watchman](/images/ranks/rank-G-2-watchman.gif)
![](images/avatars/182793287489b53393316c.gif)
Joined: 22 Mar 2006 Posts: 5941
|
Posted: Wed Jan 09, 2019 12:52 pm Post subject: |
|
|
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 |
|
![](templates/gentoo/images/spacer.gif) |
DaggyStyle Watchman
![Watchman Watchman](/images/ranks/rank-G-2-watchman.gif)
![](images/avatars/182793287489b53393316c.gif)
Joined: 22 Mar 2006 Posts: 5941
|
Posted: Wed Feb 06, 2019 8:31 pm Post subject: |
|
|
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 |
|
![](templates/gentoo/images/spacer.gif) |
|