Apply syslinux changes to all menus except safe mode

This commit is contained in:
bergware
2018-11-24 11:34:52 +01:00
parent 0cded7602b
commit 6918a2b4cf
2 changed files with 53 additions and 57 deletions

View File

@@ -25,37 +25,35 @@ function requireLibvirt() {
}
}
function scan($area, $text) {
return strpos($area,$text)!==false;
function scan($line, $text) {
return stripos($line,$text)!==false;
}
function embed(&$syslinux, $key, $value) {
$size = count($syslinux);
$menu = $i = 0;
$cmd = [];
$make = false;
// find the default section
$new = strlen($value) ? "$key=$value" : false;
$i = 0;
while ($i < $size) {
if (scan($syslinux[$i],'label ')) {
// 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],'menu default')) $menu = 1;
if (scan($syslinux[$n],'append')) {$cmd = preg_split('/\s+/',trim($syslinux[$n])); break;}
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));
}
$n++;
}
if ($menu) break; else $i = $n - 1;
$i = $n - 1;
}
$i++;
}
if ($cmd) {
$new = strlen($value) ? "$key=$value" : "";
// replace the existing setting
for ($c = 0; $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));
}
return $make;
}

View File

@@ -13,8 +13,8 @@
<?
$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
function scan($area, $text) {
return strpos($area,$text)!==false;
function scan($line, $text) {
return stripos($line,$text)!==false;
}
$name = urldecode($_POST['name']);
@@ -127,52 +127,50 @@ case 'is':
$cfg = '/boot/syslinux/syslinux.cfg';
$syslinux = file($cfg, FILE_IGNORE_NEW_LINES+FILE_SKIP_EMPTY_LINES);
$size = count($syslinux);
$menu = $i = 0;
$cmd = [];
// find the default section
$make = false;
$file = "/var/tmp/$name.tmp";
$isolcpus = file_get_contents($file);
if ($isolcpus != '') {
$numbers = explode(',',$isolcpus);
sort($numbers,SORT_NUMERIC);
$isolcpus = $previous = array_shift($numbers);
$range = false;
// convert sequential numbers to a range
foreach ($numbers as $number) {
if ($number == $previous+1) {
$range = true;
} else {
if ($range) {$isolcpus .= '-'.$previous; $range = false;}
$isolcpus .= ','.$number;
}
$previous = $number;
}
if ($range) $isolcpus .= '-'.$previous;
$isolcpus = "isolcpus=$isolcpus";
}
unlink($file);
$i = 0;
while ($i < $size) {
if (scan($syslinux[$i],'label ')) {
// 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],'menu default')) $menu = 1;
// find the current command
if (scan($syslinux[$n],'append')) {$cmd = preg_split('/\s+/',trim($syslinux[$n])); break;}
if (scan($syslinux[$n],'append ')) {
$cmd = preg_split('/\s+/',trim($syslinux[$n]));
// replace an existing 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 setting
if ($c==count($cmd) && $isolcpus) {array_splice($cmd,-1,0,$isolcpus); $make = true;}
$syslinux[$n] = ' '.str_replace(' ',' ',implode(' ',$cmd));
}
$n++;
}
if ($menu) break; else $i = $n - 1;
$i = $n - 1;
}
$i++;
}
if ($cmd) {
// modify the current command
$file = "/var/tmp/$name.tmp";
// read new isolcpus assignments
$isolcpus = file_get_contents($file); unlink($file);
if ($isolcpus != '') {
$numbers = explode(',',$isolcpus);
sort($numbers,SORT_NUMERIC);
$isolcpus = $previous = array_shift($numbers);
$range = false;
// convert sequential numbers to a range
foreach ($numbers as $number) {
if ($number == $previous+1) {
$range = true;
} else {
if ($range) {$isolcpus .= '-'.$previous; $range = false;}
$isolcpus .= ','.$number;
}
$previous = $number;
}
if ($range) $isolcpus .= '-'.$previous;
$isolcpus = "isolcpus=$isolcpus";
}
// replace an existing setting
for ($c = 0; $c < count($cmd); $c++) if (scan($cmd[$c],'isolcpus')) {$cmd[$c] = $isolcpus; break;}
// or insert a new setting
if ($c == count($cmd) && $isolcpus) array_splice($cmd,-1,0,$isolcpus);
$syslinux[$n] = ' '.str_replace(' ',' ',implode(' ',$cmd));
file_put_contents($cfg, implode("\n",$syslinux)."\n");
}
if ($make) file_put_contents($cfg, implode("\n",$syslinux)."\n");
$reply = ['success' => $name];
break;
}