From 29376ab54639693700ab23dfba89d96b439eeaca Mon Sep 17 00:00:00 2001 From: lgromero Date: Mon, 14 Apr 2025 13:21:12 +0200 Subject: [PATCH] refs #1880 adds new bootfile to boot by cache --- .../Controller/OgBootController.php | 44 ++++++++++++++++++- tftpboot/ipxe_scripts/templates/ogliveCache | 27 ++++++------ 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/api/src/OgBootBundle/Controller/OgBootController.php b/api/src/OgBootBundle/Controller/OgBootController.php index c330b3a..f60ffbe 100644 --- a/api/src/OgBootBundle/Controller/OgBootController.php +++ b/api/src/OgBootBundle/Controller/OgBootController.php @@ -1484,7 +1484,6 @@ public function createBootFile(Request $request): JsonResponse ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); $data = json_decode($request->getContent(), true); - $templateName = $data['template_name'] ?? null; $mac = $this->validateAndFormatMac($data['mac'] ?? null); //Si nos pasan el puerto se lo quitamos ya que server_ip siempre tirara por Samba @@ -1642,6 +1641,49 @@ public function createBootFile(Request $request): JsonResponse ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); return new JsonResponse(['error' => 'FAILED_TO_CREATE_PXE_FILE', 'message' => 'Error al crear el archivo PXE'], Response::HTTP_INTERNAL_SERVER_ERROR); } + + // Sustituir en la plantilla los marcadores + $templateCacheDir = $this->tftpbootDir . '/menu.lst'; + $templateCachePath = $templateCacheDir . '/templateCache'; + $templateCacheContent = file_get_contents($templateCachePath); + $grubContent = str_replace( + ['__INFOHOST__', '__OGLIVE__'], + [$kernelArgs, basename($ogLiveDir)], + $templateCacheContent + ); + + // Nombre del archivo de arranque por cache + $pxeFileNameCache = '01-' . $mac; + $pxeFilePathCache = $templateCacheDir . '/' . $pxeFileNameCache; + // Crear el archivo de arranque por cache + if (file_put_contents($pxeFilePathCache, $grubContent) === false) { + $httpCode = '500'; + $this->logger->error(json_encode([ + 'severity' => 'ERROR', + 'operation' => $operation, + 'component' => $component, + 'http_code' => $httpCode, + 'desc' => 'Failed to create cache PXE file.' + ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); + return new JsonResponse(['error' => 'FAILED_TO_CREATE_CACHE_PXE_FILE', 'message' => 'Error al crear el archivo de caché PXE'], Response::HTTP_INTERNAL_SERVER_ERROR); + } + + + // Creación de fichero de arranque por cache + $cacheFilePath = $this->tftpbootDir . '/menu.lst/' . $pxeFileName; + if (file_put_contents($cacheFilePath, $grubContent) === false) { + $httpCode = '500'; + $this->logger->error(json_encode([ + 'severity' => 'ERROR', + 'operation' => $operation, + 'component' => $component, + 'http_code' => $httpCode, + 'desc' => 'Failed to create cache PXE file.' + ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); + return new JsonResponse(['error' => 'FAILED_TO_CREATE_CACHE_PXE_FILE', 'message' => 'Error al crear el archivo de caché PXE'], Response::HTTP_INTERNAL_SERVER_ERROR); + } + + $httpCode = '200'; $this->logger->info(json_encode([ 'severity' => 'INFO', diff --git a/tftpboot/ipxe_scripts/templates/ogliveCache b/tftpboot/ipxe_scripts/templates/ogliveCache index 035e69c..214a411 100644 --- a/tftpboot/ipxe_scripts/templates/ogliveCache +++ b/tftpboot/ipxe_scripts/templates/ogliveCache @@ -1,17 +1,16 @@ #!ipxe -# Configuración inicial -set ISODIR __OGLIVE__ -set default 0 -set kernelargs __INFOHOST__ -:bios_boot_cache -echo "Booting ogLive from cache..." +# Detectar si se está ejecutando en modo UEFI o BIOS +iseq ${platform} efi && goto uefi_boot || goto bios_boot -chain tftp://__SERVERIP__/tftpboot/grub.exe --config-file=" - timeout 0 - set ISODIR=${ISODIR}; - find --set-root --ignore-floppies --ignore-cd /boot/${ISODIR}/ogvmlinuz; - kernel /boot/${ISODIR}/ogvmlinuz ${kernelargs}; - initrd /boot/${ISODIR}/oginitrd.img; - boot; -" || echo "Failed to boot ogLive from cache." +:bios_boot +echo "Running in BIOS mode" +# Si el cliente es BIOS, arranca el grub.pxe +chain http://__SERVERIP__/tftpboot/grub.pxe +exit + +:uefi_boot +echo "Running in UEFI mode" +# Si el cliente es UEFI, arranca el grubx64.efi +chain http://__SERVERIP__/tftpboot/grubx64.efi +exit