Merge pull request #1834 from ich777/grub_cpuisolation_fix

GRUB compatibility changes
This commit is contained in:
tom mortensen
2024-08-24 09:18:04 -07:00
committed by GitHub
3 changed files with 160 additions and 84 deletions

View File

@@ -98,24 +98,62 @@ case 'ct':
echo "\0".implode(';',array_map('urlencode',$cts));
break;
case 'is':
$syslinux = file('/boot/syslinux/syslinux.cfg',FILE_IGNORE_NEW_LINES+FILE_SKIP_EMPTY_LINES);
$size = count($syslinux);
$menu = $i = 0;
$isol = "";
$isolcpus = [];
// find the default section
while ($i < $size) {
if (scan($syslinux[$i],'label ')) {
$n = $i + 1;
// find the current isolcpus 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,'isolcpus')) {$isol = explode('=',$cmd)[1]; break;}
$n++;
if (is_file('/boot/syslinux/syslinux.cfg')) {
$menu = $i = 0;
$isol = "";
$isolcpus = [];
$bootcfg = file('/boot/syslinux/syslinux.cfg', FILE_IGNORE_NEW_LINES+FILE_SKIP_EMPTY_LINES);
$size = count($bootcfg);
// find the default section
while ($i < $size) {
if (scan($bootcfg[$i],'label ')) {
$n = $i + 1;
// find the current isolcpus 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,'isolcpus')) {$isol = explode('=',$cmd)[1]; break;}
$n++;
}
if ($menu) break; else $i = $n - 1;
}
$i++;
}
} elseif (is_file('/boot/grub/grub.cfg')) {
$isol = "";
$isolcpus = [];
$bootcfg = file('/boot/grub/grub.cfg', FILE_IGNORE_NEW_LINES);
// find the default section
$menu_entries = [];
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]);
// find the current isolcpus setting
if (scan($menu_entries[$bootentry],'linux ')) {
foreach ($menuentry as $cmd) {
if (scan($cmd,'isolcpus')) {
$isol = explode('=',$cmd)[1];
break;
}
}
if ($menu) break; else $i = $n - 1;
}
$i++;
}
if ($isol != '') {
// convert to individual numbers
@@ -133,24 +171,28 @@ case 'is':
break;
case 'cmd':
$isolcpus_now = $isolcpus_new = '';
$syslinux = file('/boot/syslinux/syslinux.cfg',FILE_IGNORE_NEW_LINES+FILE_SKIP_EMPTY_LINES);
$cmdline = explode(' ',file_get_contents('/proc/cmdline'));
foreach ($cmdline as $cmd) if (scan($cmd,'isolcpus')) {$isolcpus_now = $cmd; break;}
$size = count($syslinux);
$menu = $i = 0;
// find the default section
while ($i < $size) {
if (scan($syslinux[$i],'label ')) {
$n = $i + 1;
// find the current isolcpus 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,'isolcpus')) {$isolcpus_new = $cmd; break;}
$n++;
if (is_file('/boot/syslinux/syslinux.cfg')) {
$bootcfg = file('/boot/syslinux/syslinux.cfg',FILE_IGNORE_NEW_LINES+FILE_SKIP_EMPTY_LINES);
foreach ($cmdline as $cmd) if (scan($cmd,'isolcpus')) {$isolcpus_now = $cmd; break;}
$size = count($bootcfg);
$menu = $i = 0;
// find the default section
while ($i < $size) {
if (scan($bootcfg[$i],'label ')) {
$n = $i + 1;
// find the current isolcpus 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,'isolcpus')) {$isolcpus_new = $cmd; break;}
$n++;
}
if ($menu) break; else $i = $n - 1;
}
if ($menu) break; else $i = $n - 1;
$i++;
}
$i++;
} elseif (is_file('/boot/grub/grub.cfg')) {
$bootcfg = file('/boot/grub/grub.cfg', FILE_IGNORE_NEW_LINES);
}
echo $isolcpus_now==$isolcpus_new ? 0 : 1;
break;

View File

@@ -169,27 +169,16 @@ case 'ct':
break;
case 'is':
/* Path to syslinux configuration file */
$cfg = '/boot/syslinux/syslinux.cfg';
/* Read the syslinux configuration file into an array, ignoring empty lines */
$syslinux = file($cfg, FILE_IGNORE_NEW_LINES + FILE_SKIP_EMPTY_LINES);
$size = count($syslinux);
$make = false;
/* Path to the temporary file containing new isolcpus settings */
/* Path to the temporary file containing new isolcpus settings */
$file = "/var/tmp/$name.tmp";
$isolcpus = file_get_contents($file);
$isolcpus = file_get_contents($file);
if ($isolcpus != '') {
/* Convert isolcpus string to an array of numbers and sort them */
$numbers = explode(',', $isolcpus);
sort($numbers, SORT_NUMERIC);
/* Initialize variables for range conversion */
$isolcpus = $previous = array_shift($numbers);
$range = false;
/* Convert sequential numbers to a range */
foreach ($numbers as $number) {
if ($number == $previous + 1) {
@@ -206,55 +195,94 @@ case 'is':
if ($range) {
$isolcpus .= '-' . $previous;
}
/* Format isolcpus string for syslinux configuration */
/* Format isolcpus string for configuration */
$isolcpus = "isolcpus=$isolcpus";
}
/* Remove the temporary file */
unlink($file);
$i = 0;
while ($i < $size) {
/* Find sections in syslinux config 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 ($n < $size && !scan($syslinux[$n], 'label ')) {
if (scan($syslinux[$n], 'append ')) {
$cmd = preg_split('/\s+/', trim($syslinux[$n]));
/* Replace an existing isolcpus setting */
for ($c = 1; $c < count($cmd); $c++) {
if (scan($cmd[$c], 'isolcpus')) {
$make |= ($cmd[$c] != $isolcpus);
$cmd[$c] = $isolcpus;
break;
if (is_file('/boot/syslinux/syslinux.cfg')) {
/* Path to syslinux configuration file */
$cfg = '/boot/syslinux/syslinux.cfg';
/* Read the syslinux configuration file into an array, ignoring empty lines */
$bootcfg = file($cfg, FILE_IGNORE_NEW_LINES + FILE_SKIP_EMPTY_LINES);
$size = count($bootcfg);
$make = false;
/* Remove the temporary file */
unlink($file);
$i = 0;
while ($i < $size) {
/* Find sections in syslinux config 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 ($n < $size && !scan($bootcfg[$n], 'label ')) {
if (scan($bootcfg[$n], 'append ')) {
$cmd = preg_split('/\s+/', trim($bootcfg[$n]));
/* Replace an existing isolcpus setting */
for ($c = 1; $c < count($cmd); $c++) {
if (scan($cmd[$c], 'isolcpus')) {
$make |= ($cmd[$c] != $isolcpus);
$cmd[$c] = $isolcpus;
break;
}
}
/* Or insert a new isolcpus setting if not found */
if ($c == count($cmd) && $isolcpus) {
array_splice($cmd, -1, 0, $isolcpus);
$make = true;
}
/* Update the syslinux configuration line */
$bootcfg[$n] = ' ' . str_replace(' ', ' ', implode(' ', $cmd));
}
/* Or insert a new isolcpus setting if not found */
if ($c == count($cmd) && $isolcpus) {
array_splice($cmd, -1, 0, $isolcpus);
$make = true;
}
/* Update the syslinux configuration line */
$syslinux[$n] = ' ' . str_replace(' ', ' ', implode(' ', $cmd));
$n++;
}
$n++;
$i = $n - 1;
}
$i = $n - 1;
$i++;
}
} elseif (is_file('/boot/grub/grub.cfg')) {
/* Path to grub configuration file */
$cfg = '/boot/grub/grub.cfg';
/* Read the grub configuration file into an array, ignoring empty lines */
$bootcfg = file($cfg, FILE_IGNORE_NEW_LINES+FILE_SKIP_EMPTY_LINES);
$size = count($bootcfg);
$make = false;
/* Remove the temporary file */
unlink($file);
$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 an existing isolcpus setting */
for ($c = 1; $c < count($cmd); $c++) {
if (scan($cmd[$c], 'isolcpus')) {
$make |= ($cmd[$c] != $isolcpus);
$cmd[$c] = $isolcpus;
break;
}
}
/* Or insert a new isolcpus setting if not found */
if ($c == count($cmd) && $isolcpus) {
$cmd[] = $isolcpus;
$make = true;
}
/* Update the grub configuration line */
$bootcfg[$n] = ' ' . str_replace(' ', ' ', implode(' ', $cmd));
}
$n++;
}
$i = $n - 1;
}
$i++;
}
$i++;
}
/* Write the updated syslinux configuration back to the file if changes were made */
/* Write the updated configuration back to the file if changes were made */
if ($make) {
file_put_contents_atomic($cfg, implode("\n", $syslinux) . "\n");
file_put_contents_atomic($cfg, implode("\n", $bootcfg) . "\n");
}
$reply = ['success' => $name];
break;
}

View File

@@ -28,7 +28,13 @@ $docroot ??= ($_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp');
require_once "$docroot/webGui/include/Helpers.php";
require_once "$docroot/webGui/include/Wrappers.php";
$folders = ['/boot','/boot/config','/boot/config/plugins','/boot/syslinux','/var/log','/var/log/plugins','/boot/extra','/var/log/packages','/var/lib/pkgtools/packages','/tmp'];
if (is_file('/boot/syslinux/syslinux.cfg')) {
$bootenv = '/boot/syslinux';
} elseif (is_file('/boot/grub/grub.cfg')) {
$bootenv = '/boot/grub';
}
$folders = ['/boot','/boot/config','/boot/config/plugins',$bootenv,'/var/log','/var/log/plugins','/boot/extra','/var/log/packages','/var/lib/pkgtools/packages','/tmp'];
// global variables
$path = "/var/local/emhttp";