Merge pull request #1818 from ich777/grub_vm_settings_fix

Grub VM settings fix
This commit is contained in:
tom mortensen
2024-08-13 10:27:49 -07:00
committed by GitHub
2 changed files with 132 additions and 51 deletions

View File

@@ -30,30 +30,68 @@ if (!$hardware) {
function scan($area, $text) {
return strpos($area,$text)!==false;
}
function detect(&$syslinux, $key) {
$size = count($syslinux);
$menu = $i = 0;
$value = '';
// find the default section
while ($i < $size) {
if (scan($syslinux[$i],'label ')) {
$n = $i + 1;
// find the current requested setting
while (!scan($syslinux[$n],'label ') && $n < $size) {
if (scan($syslinux[$n],'menu default')) $menu = 1;
if (scan($syslinux[$n],'append')) foreach (explode(' ',$syslinux[$n]) as $cmd) if (scan($cmd,$key)) {$value = explode('=',$cmd)[1]; break;}
$n++;
function detect(&$bootcfg, $bootenv, $key) {
if ($bootenv === 'syslinux') {
$size = count($bootcfg);
$menu = $i = 0;
$value = '';
// find the default section
while ($i < $size) {
if (scan($bootcfg[$i],'label ')) {
$n = $i + 1;
// find the current requested setting
while (!scan($bootcfg[$n],'label ') && $n < $size) {
if (scan($bootcfg[$n],'menu default')) $menu = 1;
if (scan($bootcfg[$n],'append')) foreach (explode(' ',$bootcfg[$n]) as $cmd) if (scan($cmd,$key)) {$value = explode('=',$cmd)[1]; break;}
$n++;
}
if ($menu) break; else $i = $n - 1;
}
if ($menu) break; else $i = $n - 1;
$i++;
}
$i++;
} elseif ($bootenv === 'grub') {
$menu_entries = [];
// find the current boot entry
foreach ($bootcfg as $line) {
if (preg_match('/set default=(\d+)/', $line, $match)) {
$bootentry = (int)$match[1];
break;
}
}
// split boot entries
foreach ($bootcfg as $line) {
if (strpos($line, 'menuentry ') === 0) {
$in_menuentry = true;
$current_entry = $line . "\n";
} elseif ($in_menuentry) {
$current_entry .= $line . "\n";
if (trim($line) === "}") {
$menu_entries[] = $current_entry;
$in_menuentry = false;
}
}
}
// search in selected menuentry
$menuentry = explode("\n", $menu_entries[$bootentry]);
foreach (explode(' ', $menu_entries[$bootentry]) as $cmd) {
if (scan($cmd,$key)) {
$value = explode('=',$cmd)[1];
break;
}
}
}
return $value;
return trim($value);
}
if (is_file('/boot/syslinux/syslinux.cfg')) {
$bootcfg = file('/boot/syslinux/syslinux.cfg',FILE_IGNORE_NEW_LINES+FILE_SKIP_EMPTY_LINES);
$bootenv = 'syslinux';
} elseif (is_file('/boot/grub/grub.cfg')) {
$bootcfg = file('/boot/grub/grub.cfg',FILE_IGNORE_NEW_LINES+FILE_SKIP_EMPTY_LINES);
$bootenv = 'grub';
}
$syslinux = file('/boot/syslinux/syslinux.cfg',FILE_IGNORE_NEW_LINES+FILE_SKIP_EMPTY_LINES);
$arrValidNetworks = getValidNetworks();
$pcie_acs_override = detect($syslinux, 'pcie_acs_override');
$vfio_allow_unsafe = detect($syslinux, 'allow_unsafe_interrupts');
$pcie_acs_override = detect($bootcfg, $bootenv, 'pcie_acs_override');
$vfio_allow_unsafe = detect($bootcfg, $bootenv, 'allow_unsafe_interrupts');
$bgcolor = strstr('white,azure',$display['theme']) ? '#f2f2f2' : '#1c1c1c';
$started = $var['fsState']=='Started';
$libvirt_up = $libvirt_running=='yes';
@@ -321,7 +359,7 @@ $(function(){
<?if ($safemode):?>
if (run) $("#settingsForm").submit();
<?else:?>
if (run) $.post("/plugins/dynamix.vm.manager/include/VMajax.php", {action:'syslinux',pcie:$('#pcie_acs_override').val(),vfio:$('#vfio_allow_unsafe').val()}, function(data){
if (run) $.post("/plugins/dynamix.vm.manager/include/VMajax.php", {action:'cmdlineoverride',pcie:$('#pcie_acs_override').val(),vfio:$('#vfio_allow_unsafe').val()}, function(data){
$("#settingsForm").submit();
});
<?endif;?>

View File

@@ -33,30 +33,60 @@ function scan($line, $text) {
return stripos($line,$text)!==false;
}
function embed(&$syslinux, $key, $value) {
$size = count($syslinux);
$make = false;
$new = strlen($value) ? "$key=$value" : false;
$i = 0;
while ($i < $size) {
// find sections and exclude safemode
if (scan($syslinux[$i],'label ') && !scan($syslinux[$i],'safe mode') && !scan($syslinux[$i],'safemode')) {
$n = $i + 1;
// find the current requested setting
while (!scan($syslinux[$n],'label ') && $n < $size) {
if (scan($syslinux[$n],'append ')) {
$cmd = preg_split('/\s+/',trim($syslinux[$n]));
// replace the existing setting
for ($c = 1; $c < count($cmd); $c++) if (scan($cmd[$c],$key)) {$make |= ($cmd[$c]!=$new); $cmd[$c] = $new; break;}
// or insert the new setting
if ($c==count($cmd) && $new) {array_splice($cmd,-1,0,$new); $make = true;}
$syslinux[$n] = ' '.str_replace(' ',' ',implode(' ',$cmd));
function embed(&$bootcfg, $env, $key, $value) {
if ($env === 'syslinux') {
$size = count($bootcfg);
$make = false;
$new = strlen($value) ? "$key=$value" : false;
$i = 0;
while ($i < $size) {
// find sections and exclude safemode
if (scan($bootcfg[$i],'label ') && !scan($bootcfg[$i],'safe mode') && !scan($bootcfg[$i],'safemode')) {
$n = $i + 1;
// find the current requested setting
while (!scan($bootcfg[$n],'label ') && $n < $size) {
if (scan($bootcfg[$n],'append ')) {
$cmd = preg_split('/\s+/',trim($bootcfg[$n]));
// replace the existing setting
for ($c = 1; $c < count($cmd); $c++) if (scan($cmd[$c],$key)) {$make |= ($cmd[$c]!=$new); $cmd[$c] = $new; break;}
// or insert the new setting
if ($c==count($cmd) && $new) {array_splice($cmd,-1,0,$new); $make = true;}
$bootcfg[$n] = ' '.str_replace(' ',' ',implode(' ',$cmd));
}
$n++;
}
$n++;
$i = $n - 1;
}
$i = $n - 1;
$i++;
}
} elseif ($env === 'grub') {
$size = count($bootcfg);
$make = false;
$new = strlen($value) ? "$key=$value" : false;
$i = 0;
while ($i < $size) {
// find sections and exclude safemode/memtest
if (scan($bootcfg[$i],'menuentry ') && !scan($bootcfg[$i],'safe mode') && !scan($bootcfg[$i],'safemode') && !scan($bootcfg[$i],'memtest')) {
$n = $i + 1;
// find the current requested setting
while (!scan($bootcfg[$n],'menuentry ') && $n < $size) {
if (scan($bootcfg[$n],'linux ')) {
$cmd = preg_split('/\s+/',trim($bootcfg[$n]));
// replace the existing setting
for ($c = 1; $c < count($cmd); $c++) if (scan($cmd[$c],$key)) {$make |= ($cmd[$c]!=$new); $cmd[$c] = $new; break;}
// or insert the new setting
if ($c == count($cmd) && $new) {
$cmd[] = $new;
$make = true;
}
$bootcfg[$n] = ' ' . str_replace(' ', ' ', implode(' ', $cmd));
}
$n++;
}
$i = $n - 1;
}
$i++;
}
$i++;
}
return $make;
}
@@ -536,26 +566,39 @@ case 'hot-detach-usb':
//TODO
break;
case 'syslinux':
$cfg = '/boot/syslinux/syslinux.cfg';
$syslinux = file($cfg, FILE_IGNORE_NEW_LINES+FILE_SKIP_EMPTY_LINES);
$m1 = embed($syslinux, 'pcie_acs_override', $_REQUEST['pcie']);
$m2 = embed($syslinux, 'vfio_iommu_type1.allow_unsafe_interrupts', $_REQUEST['vfio']);
if ($m1||$m2) file_put_contents($cfg, implode("\n",$syslinux)."\n");
case 'cmdlineoverride':
if (is_file('/boot/syslinux/syslinux.cfg')) {
$cfg = '/boot/syslinux/syslinux.cfg';
$env = 'syslinux';
$bootcfg = file($cfg, FILE_IGNORE_NEW_LINES+FILE_SKIP_EMPTY_LINES);
} elseif (is_file('/boot/grub/grub.cfg')) {
$cfg = '/boot/grub/grub.cfg';
$env = 'grub';
$bootcfg = file($cfg, FILE_IGNORE_NEW_LINES);
}
$m1 = embed($bootcfg, $env, 'pcie_acs_override', $_REQUEST['pcie']);
$m2 = embed($bootcfg, $env, 'vfio_iommu_type1.allow_unsafe_interrupts', $_REQUEST['vfio']);
if ($m1||$m2) file_put_contents($cfg, implode("\n",$bootcfg)."\n");
$arrResponse = ['success' => true, 'modified' => $m1|$m2];
break;
case 'reboot':
$cfg = '/boot/syslinux/syslinux.cfg';
$syslinux = file($cfg, FILE_IGNORE_NEW_LINES+FILE_SKIP_EMPTY_LINES);
if (is_file('/boot/syslinux/syslinux.cfg')) {
$cfg = '/boot/syslinux/syslinux.cfg';
$env = 'syslinux';
} elseif (is_file('/boot/grub/grub.cfg')) {
$cfg = '/boot/grub/grub.cfg';
$env = 'grub';
}
$bootcfg = file($cfg, FILE_IGNORE_NEW_LINES+FILE_SKIP_EMPTY_LINES);
$cmdline = explode(' ',file_get_contents('/proc/cmdline'));
$pcie = $vfio = '';
foreach ($cmdline as $cmd) {
if (scan($cmd,'pcie_acs_override')) $pcie = explode('=',$cmd)[1];
if (scan($cmd,'allow_unsafe_interrupts')) $vfio = explode('=',$cmd)[1];
}
$m1 = embed($syslinux, 'pcie_acs_override', $pcie);
$m2 = embed($syslinux, 'vfio_iommu_type1.allow_unsafe_interrupts', $vfio);
$m1 = embed($bootcfg, $env, 'pcie_acs_override', $pcie);
$m2 = embed($bootcfg, $env, 'vfio_iommu_type1.allow_unsafe_interrupts', $vfio);
$arrResponse = ['success' => true, 'modified' => $m1|$m2];
break;