Hey, @oliverde8 I am messing with your revision of @Squid 's cool hot swap script. I am trying to hot swap a keyboard and mouse through a hub (similar to your KVM situation). For reference the script is named "KM Script." I am running into an issue where it works for a few seconds and then everything automatically disconnects. I have tried both via the bus option and with the hubname option with both the same result. Additionally, I keep getting the following error every few seconds in the logs. Attached is my version of the script and a snip from my logs. Any insight would be appreciated.
V/R
Revrto
LSUSB:
Bus 006 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 005 Device 002: ID 0781:5571 SanDisk Corp. Cruzer Fit
Bus 005 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 002: ID 05e3:0612 Genesys Logic, Inc. Hub
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 003: ID 051d:0003 American Power Conversion UPS
Bus 003 Device 009: ID 05e3:0610 Genesys Logic, Inc. 4-port hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 8087:0aa7 Intel Corp.
Bus 001 Device 003: ID 1e71:170e NZXT NZXT USB Device
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
MY VERSION OF SCRIPT:
#!/usr/bin/php
<?PHP
#backgroundOnly=true
#description=A script designed to monitor a USB hub or a particular USB bus for changes and then attach or detach devices to an applicable VM
/*
* Configuration's
*/
$vmName = "Truff-Metal"; // must be the exact name
$pollingTime = 10; // the interval between checks in seconds
$startupDelay = 10; // startup delay before monitoring for changes in seconds (set to enough time for the VM to get up and running)
$waitBetweenPlugs = 10; // If using a device such a KVM mounting multiple usb drives at once can cause some issues with the host. Wait in between.
$ignoredDevices = [ // List of device names that should be ignored.
'American Power Conversion UPS / Linux Foundation 2.0 root hub' // must be exact as listed via lsusb
];
// only use one or the other of the following lines not both See thread for details
$hubName = "Genesys Logic, Inc. 4-port hub"; // must be exact as listed via lsusb
#$bus = "003"; // see thread for details
/**
* Code don't change from here on.
*/
function getDeviceList($bus, $ignoredDevices) {
exec("lsusb | grep 'Bus $bus'",$allDevicesOnBus);
foreach ($allDevicesOnBus as $Devices) {
$deviceLine = explode(" ",$Devices);
if (!in_array($deviceLine[5], $ignoredDevices)) {
$initialState[$deviceLine[5]] = $deviceLine[5];
} else {
logger("Ignoring device $Devices");
}
var_dump();
}
return $initialState;
}
function createXML($deviceID, $waitBetweenPlugs) {
sleep($waitBetweenPlugs);
$usb = explode(":",$deviceID);
$usbstr .= "<hostdev mode='subsystem' type='usb'>
<source>
<vendor id='0x".$usb[0]."'/>
<product id='0x".$usb[1]."'/>
</source>
</hostdev>";
file_put_contents("/tmp/USBTempXML.xml",$usbstr);
}
function logger($string) {
echo "$string\n";
shell_exec("logger ".escapeshellarg($string));
}
# Begin Main
logger("Sleeping for $startupDelay before monitoring $bus for changes to passthrough to $vmName");
sleep($startupDelay);
$hubBus = $bus;
if ( ! $hubBus ) {
$hub = explode(" ",exec("lsusb | grep '$hubName'"));
$hubBus = $hub[1];
}
logger("Monitoring $hubBus for changes");
$initialState = getDeviceList($hubBus, $ignoredDevices);
while (true) {
$unRaidVars = parse_ini_file("/var/local/emhttp/var.ini");
if ($unRaidVars['mdState'] != "STARTED") {
break;
}
$currentDevices = getDeviceList($hubBus, $ignoredDevices);
foreach ($currentDevices as $Device) {
if ( ! $initialState[$Device] ) {
logger("$Device Added to bus $hubBus Attaching to $vmName");
createXML($Device, $waitBetweenPlugs);
exec("/usr/sbin/virsh attach-device '$vmName' /tmp/USBTempXML.xml 2>&1");
$initialState[$Device] = $Device;
}
}
foreach ($initialState as $Device) {
if ( ! $currentDevices[$Device] ) {
logger("$Device Removed from bus $hubBus Detaching from $vmName");
createXML($Device, $waitBetweenPlugs);
exec("/usr/sbin/virsh detach-device '$vmName' /tmp/USBTempXML.xml 2>&1");
unset ($initialState[$Device]);
}
}
sleep($pollingTime);
}
?>
LOG:
Warning: var_dump() expects at least 1 parameter, 0 given in /tmp/user.scripts/tmpScripts/KM Switch/script on line 35
046d:c07e Added to bus Attaching to Truff-Metal
04d9:a0d1 Added to bus Attaching to Truff-Metal
05e3:0610 Added to bus Attaching to Truff-Metal
Warning: var_dump() expects at least 1 parameter, 0 given in /tmp/user.scripts/tmpScripts/KM Switch/script on line 35
Warning: var_dump() expects at least 1 parameter, 0 given in /tmp/user.scripts/tmpScripts/KM Switch/script on line 35
Warning: var_dump() expects at least 1 parameter, 0 given in /tmp/user.scripts/tmpScripts/KM Switch/script on line 35
Warning: var_dump() expects at least 1 parameter, 0 given in /tmp/user.scripts/tmpScripts/KM Switch/script on line 35
Warning: var_dump() expects at least 1 parameter, 0 given in /tmp/user.scripts/tmpScripts/KM Switch/script on line 35
Warning: var_dump() expects at least 1 parameter, 0 given in /tmp/user.scripts/tmpScripts/KM Switch/script on line 35
Warning: var_dump() expects at least 1 parameter, 0 given in /tmp/user.scripts/tmpScripts/KM Switch/script on line 35
Warning: var_dump() expects at least 1 parameter, 0 given in /tmp/user.scripts/tmpScripts/KM Switch/script on line 35
Warning: var_dump() expects at least 1 parameter, 0 given in /tmp/user.scripts/tmpScripts/KM Switch/script on line 35
Warning: var_dump() expects at least 1 parameter, 0 given in /tmp/user.scripts/tmpScripts/KM Switch/script on line 35
Warning: var_dump() expects at least 1 parameter, 0 given in /tmp/user.scripts/tmpScripts/KM Switch/script on line 35
Warning: var_dump() expects at least 1 parameter, 0 given in /tmp/user.scripts/tmpScripts/KM Switch/script on line 35
046d:c07e Removed from bus Detaching from Truff-Metal
04d9:a0d1 Removed from bus Detaching from Truff-Metal
Warning: var_dump() expects at least 1 parameter, 0 given in /tmp/user.scripts/tmpScripts/KM Switch/script on line 35