Auto create VMs on demand using script


moke

Recommended Posts

function virsh_device_by_bus($action, $vmname, $usbbus, $usbdev)
{
	$usbstr = '';
	if (!empty($usbbus)) 
	{
		$usbbus=ltrim($usbbus, "0");
		$usbdev=ltrim($usbdev, "0") ;
		$usbstr .= "<hostdev mode='subsystem' type='usb'>
	<source>
	<address bus='${usbbus}' device='${usbdev}' />
	</source>
	</hostdev>";
	}
	$filename = '/tmp/libvirthotplugusbbybus'.$vmname.'.xml';
	file_put_contents($filename,$usbstr);
	

return shell_exec("/usr/sbin/virsh $action-device '$vmname' '".$filename."' 2>&1");


#echo "Running virsh ${COMMAND} ${DOMAIN} for USB bus=${BUSNUM} device=${DEVNUM}:" >&2
#virsh "${COMMAND}" "${DOMAIN}" /dev/stdin <<END
#<hostdev mode='subsystem' type='usb'>
#  <source>
#    <address bus='${BUSNUM}' device='${DEVNUM}' />
#  </source>
#</hostdev>
#END
}

 

Here is some PHP code that I use for hotplugging USB devices into a VM.

 

  • Like 2
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.