From 8cdccaeb71a167c6a16b715e65967da3be8a040a Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 10 Jul 2020 16:40:06 +0200 Subject: [serversetup-bwlp-ipxe] Progress on new structure (+bash) --- .../inc/scriptbuilderbash.inc.php | 97 ++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 modules-available/serversetup-bwlp-ipxe/inc/scriptbuilderbash.inc.php (limited to 'modules-available/serversetup-bwlp-ipxe/inc/scriptbuilderbash.inc.php') diff --git a/modules-available/serversetup-bwlp-ipxe/inc/scriptbuilderbash.inc.php b/modules-available/serversetup-bwlp-ipxe/inc/scriptbuilderbash.inc.php new file mode 100644 index 00000000..86b2931f --- /dev/null +++ b/modules-available/serversetup-bwlp-ipxe/inc/scriptbuilderbash.inc.php @@ -0,0 +1,97 @@ +menuToScript($menu); + } + + public function getBootEntry($entry) + { + if (!$entry) { + return "echo 'Invalid boot entry id'\nread -n1 -r _\n"; + } + return $entry->toScript($this); + } + + public function getMenuEntry($entry, $honorPassword = true) + { + if ($entry === null) + return "echo 'Invalid menu entry id - press any key to continue'\nread -n1 -r _\n"; + return $entry->getBootEntryScript($this); + } + + public function getSpecial($special) + { + return ''; // We can't really do localboot here I guess + } + + public function menuToScript($menu) + { + $output = "declare -A items_name items_gap hotkey_item\ndeclare menu_default menu_timeout menu_title\n"; + foreach ($menu->items as $entry) { + $id = $entry->menuentryid; + if ($entry->bootEntry === null || (!empty($this->platform) && !$entry->bootEntry->supportsMode($this->platform))) + continue; + if (!$entry->hidden) { + $output .= 'items_name[' . $id . ']=' . $this->bashString($entry->title) . "\n"; + if ($entry->gap) { + $output .= 'items_gap[' . $id . "]=1\n"; + } + } + if ($entry->hotkey !== false) { + $output .= 'hotkey_item[' . $entry->hotkey . ']=' . $id . "\n"; + } + if ($id == $menu->defaultEntryId) { + $output .= "menu_default={$id}\n"; + } + } + return $output . "menu_timeout=" . $menu->timeoutMs + . "\nmenu_title=" . $this->bashString($menu->title) . "\n"; + } + + public function execDataToScript($agnostic, $bios, $efi) : string + { + if ($agnostic !== null) + return $this->execDataToScriptInternal($agnostic); + if ($bios !== null && $this->platform === BootEntry::BIOS) + return $this->execDataToScriptInternal($bios); + if ($efi !== null && $this->platform === BootEntry::EFI) + return $this->execDataToScriptInternal($efi); + return $this->execDataToScriptInternal($bios ?? $efi ?? new ExecData()); + } + + private function execDataToScriptInternal(ExecData $entry) : string + { + $entry->sanitize(); + $script = "declare -a initrd\ndeclare kernel kcl\n"; + if (!empty($entry->initRd)) { + foreach ($entry->initRd as $initrd) { + if (empty($initrd)) + continue; + $script .= 'initrd+=( ' . $this->bashString($initrd) . " )\n"; + } + } + $script .= 'kernel=' . $this->bashString($entry->executable) . "\n"; + $script .= 'kcl="' . str_replace('"', '"\\""', $entry->commandLine) . "\"\n"; // Allow expansion + return $script; + } + + private function bashString($string) + { + if (strpos($string, "'") === false) { + return "'$string'"; + } + return "'" . str_replace("'", "'\\''", $string) . "'"; + } + +} -- cgit v1.2.3-55-g7522