Merge pull request #2149 from SimonFair/Virtual-Sound-Card

7.x Initial Commit for Virtual sound cards.
This commit is contained in:
ljm42
2025-05-06 11:39:41 -07:00
committed by GitHub
3 changed files with 63 additions and 13 deletions

View File

@@ -839,6 +839,7 @@ class Libvirt {
}
}
$audiodevs_used=[];
$soundcards = "";
if (!empty($audios)) {
foreach ($audios as $i => $audio) {
$strSpecialAddressAudio = "";
@@ -847,21 +848,28 @@ class Libvirt {
continue;
}
[$audio_bus, $audio_slot, $audio_function] = my_explode(":", str_replace('.', ':', $audio['id']), 3);
if ($audio_function != 0) {
if (isset($multidevices[$audio_bus])) {
$newaudio_bus = $multidevices[$audio_bus];
if ($machine_type == "pc") $newaudio_slot = "0x01"; else $newaudio_slot = "0x00";
$strSpecialAddressAudio = "<address type='pci' domain='0x0000' bus='$newaudio_bus' slot='$newaudio_slot' function='0x".$audio_function."' />";
if ($audio_bus == "virtual")
{
$soundcards .= "<sound model='$audio_function'>
<alias name='sound0'/>
</sound>";
} else {
if ($audio_function != 0) {
if (isset($multidevices[$audio_bus])) {
$newaudio_bus = $multidevices[$audio_bus];
if ($machine_type == "pc") $newaudio_slot = "0x01"; else $newaudio_slot = "0x00";
$strSpecialAddressAudio = "<address type='pci' domain='0x0000' bus='$newaudio_bus' slot='$newaudio_slot' function='0x".$audio_function."' />";
}
}
$pcidevs .= "<hostdev mode='subsystem' type='pci' managed='yes'>
<driver name='vfio'/>
<source>
<address domain='0x0000' bus='0x".$audio_bus."' slot='0x".$audio_slot."' function='0x".$audio_function."'/>
</source>
$strSpecialAddressAudio
</hostdev>";
$audiodevs_used[] = $audio['id'];
}
$pcidevs .= "<hostdev mode='subsystem' type='pci' managed='yes'>
<driver name='vfio'/>
<source>
<address domain='0x0000' bus='0x".$audio_bus."' slot='0x".$audio_slot."' function='0x".$audio_function."'/>
</source>
$strSpecialAddressAudio
</hostdev>";
$audiodevs_used[] = $audio['id'];
}
}
$pcidevs_used=[];
@@ -951,6 +959,7 @@ class Libvirt {
$vmrc
<console type='pty'/>
$scsicontroller
$soundcards
$pcidevs
$usbstr
<channel type='unix'>
@@ -2257,6 +2266,23 @@ class Libvirt {
return ['pci' => $devs_pci, 'usb' => $devs_usb];
}
function domain_get_sound_cards($domain) {
$soundcardslist = [];
$strDOMXML = $this->domain_get_xml($domain);
$xmldoc = new DOMDocument();
$xmldoc->loadXML($strDOMXML);
$xpath = new DOMXPath($xmldoc);
$objNodes = $xpath->query('//domain/devices/sound');
if ($objNodes->length > 0) {
foreach ($objNodes as $objNode) {
$soundcardslist[] = [
'model' => $xpath->query('@model', $objNode)->Item(0)->nodeValue
];
}
}
return $soundcardslist;
}
function domain_get_vm_pciids($domain) {
$hostdevs=$this->domain_get_host_devices_pci($domain);
$vmpcidevs=[];

View File

@@ -971,6 +971,21 @@ class Array2XML {
return $arrValidAudioDevices;
}
function getValidSoundCards() {
$arrValidSoundCards = [
'ich6' => ['name' => 'ich6','id' => 'virtual::ich6'],
'ich7' => ['name' => 'ich7','id' => 'virtual::ich7'],
'ich9' => ['name' => 'ich9','id' => 'virtual::ich9'],
'ac97' => ['name' => 'ac97','id' => 'virtual::ac97'],
'es1370' => ['name' => 'es1370','id' => 'virtual::es1370'],
'pcspk' => ['name' => 'pcspk','id' => 'virtual::pcspk'],
'sb16' => ['name' => 'sb16','id' => 'virtual::sb16'],
'usb' => ['name' => 'usb','id' => 'virtual::usb'],
'virtio' => ['name' => 'virtio','id' => 'virtual::virtio'],
];
return $arrValidSoundCards;
}
function getValidOtherDevices() {
$arrValidPCIDevices = getValidPCIDevices();
@@ -1277,6 +1292,7 @@ class Array2XML {
$arrNICs = $lv->get_nic_info($res);
$arrHostDevs = $lv->domain_get_host_devices_pci($res);
$arrUSBDevs = $lv->domain_get_host_devices_usb($res);
$arrSoundCards = $lv->domain_get_sound_cards($res);
$getcopypaste=getcopypaste($res);
// Metadata Parsing
@@ -1348,6 +1364,10 @@ class Array2XML {
];
}
if (!empty($arrSoundCards)) {
foreach ($arrSoundCards as $sckey => $soundcard) $arrAudioDevices[] = ['id' => "virtual::".$soundcard['model']];
}
// Add claimed USB devices by this VM to the available USB devices
/*
foreach($arrUSBDevs as $arrUSB) {
@@ -1532,6 +1552,7 @@ class Array2XML {
unset($old['devices']['input']);
// preserve vnc/spice port settings
// unset($new['devices']['graphics']['@attributes']['port'],$new['devices']['graphics']['@attributes']['autoport']);
unset($old['devices']['sound']);
unset($old['devices']['graphics']);
if (!isset($new['devices']['graphics']['@attributes']['keymap']) && isset($old['devices']['graphics']['@attributes']['keymap'])) unset($old['devices']['graphics']['@attributes']['keymap']);
// update parent arrays

View File

@@ -27,6 +27,7 @@ $arrValidMachineTypes = getValidMachineTypes();
$arrValidPCIDevices = getValidPCIDevices();
$arrValidGPUDevices = getValidGPUDevices();
$arrValidAudioDevices = getValidAudioDevices();
$arrValidSoundCards = getValidSoundCards();
$arrValidOtherDevices = getValidOtherDevices();
$arrValidUSBDevices = getValidUSBDevices();
$arrValidDiskDrivers = getValidDiskDrivers();
@@ -1442,6 +1443,7 @@ foreach ($arrConfig['shares'] as $i => $arrShare) {
<?
echo mk_option($arrAudio['id'], '', _('None'));
foreach ($arrValidAudioDevices as $arrDev) echo mk_option($arrAudio['id'], $arrDev['id'], $arrDev['name'].' ('.$arrDev['id'].')');
foreach ($arrValidSoundCards as $arrSound) echo mk_option($arrAudio['id'], $arrSound['id'], $arrSound['name'].' ('._("Virtual").')');
?>
</select></span>
</td>
@@ -1467,6 +1469,7 @@ foreach ($arrConfig['shares'] as $i => $arrShare) {
<span class="width"><select name="audio[{{INDEX}}][id]" class="audio narrow">
<?
foreach ($arrValidAudioDevices as $arrDev) echo mk_option('', $arrDev['id'], $arrDev['name'].' ('.$arrDev['id'].')');
foreach ($arrValidSoundCards as $arrSound) echo mk_option($arrAudio['id'], $arrSound['id'], $arrSound['name'].' ('._("Virtual").')');
?>
</select></span>
</td>