diff --git a/api/src/OgBootBundle/Controller/OgBootController.php b/api/src/OgBootBundle/Controller/OgBootController.php index f60ffbe..455f9e8 100644 --- a/api/src/OgBootBundle/Controller/OgBootController.php +++ b/api/src/OgBootBundle/Controller/OgBootController.php @@ -1642,45 +1642,57 @@ public function createBootFile(Request $request): JsonResponse 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 - ); + $pxeFileName = '01-' . $mac; + $pxeFilePath = $templateCacheDir . '/' . $pxeFileName; - // 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); + // Generar archivo si la plantilla es oglives cache + if ($templateName === 'ogliveCache') { + $templateCachePath = $templateCacheDir . '/templateCache'; + $templateCacheContent = file_get_contents($templateCachePath); + $grubContent = str_replace( + ['__INFOHOST__', '__OGLIVE__'], + [$kernelArgs, basename($ogLiveDir)], + $templateCacheContent + ); + } + // Generar archivo si la plantilla es de tipo disco/partición + elseif (in_array($templateName, ['firstDisk_firstPartition', 'firstDisk_secondPartition', 'firstDisk_thirdPartition'])) { + $templateDiskPath = $templateCacheDir . '/templateDisk'; + $templateDiskContent = file_get_contents($templateDiskPath); + + // Determinar DISK, PART y DISKPART en función del templateName + $map = [ + 'firstDisk_firstPartition' => ['disk' => '01', 'part' => '01', 'diskpart' => '(hd0,gpt1)'], + 'firstDisk_secondPartition' => ['disk' => '01', 'part' => '02', 'diskpart' => '(hd0,gpt1)'], + 'firstDisk_thirdPartition' => ['disk' => '01', 'part' => '03', 'diskpart' => '(hd0,gpt1)'], + ]; + + $diskInfo = $map[$templateName]; + + // Sustituir variables en plantilla + $grubContent = str_replace( + ['__DISK__', '__PART__', '__DISKPART__'], + [$diskInfo['disk'], $diskInfo['part'], $diskInfo['diskpart']], + $templateDiskContent + ); + } + // En cualquier otro caso, no se hace nada + else { + return new JsonResponse(['success' => 'NO_TEMPLATE_ACTION', 'message' => 'No se requiere generar archivo PXE'], Response::HTTP_OK); } - - // Creación de fichero de arranque por cache - $cacheFilePath = $this->tftpbootDir . '/menu.lst/' . $pxeFileName; - if (file_put_contents($cacheFilePath, $grubContent) === false) { + // Escribir el archivo PXE final (común a ambos casos) + if (file_put_contents($pxeFilePath, $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.' + 'desc' => 'Failed to create PXE boot 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); + return new JsonResponse(['error' => 'FAILED_TO_CREATE_PXE_FILE', 'message' => 'Error al crear el archivo de arranque PXE'], Response::HTTP_INTERNAL_SERVER_ERROR); } diff --git a/tftpboot/ipxe_scripts/templates/firstDisk_firstPartition b/tftpboot/ipxe_scripts/templates/firstDisk_firstPartition index e4bb9d4..625bc95 100644 --- a/tftpboot/ipxe_scripts/templates/firstDisk_firstPartition +++ b/tftpboot/ipxe_scripts/templates/firstDisk_firstPartition @@ -10,6 +10,6 @@ exit :uefi_boot echo "Running in UEFI mode - Booting first disk, first partition" -sanboot --no-describe --drive 0 --filename \EFI\Part-01-01\Boot\ogloader.efi || echo "Failed to boot in UEFI mode" +chain http://__SERVERIP__/tftpboot/grubx64.efi exit diff --git a/tftpboot/ipxe_scripts/templates/firstDisk_secondPartition b/tftpboot/ipxe_scripts/templates/firstDisk_secondPartition index a24f5ba..a1442f2 100644 --- a/tftpboot/ipxe_scripts/templates/firstDisk_secondPartition +++ b/tftpboot/ipxe_scripts/templates/firstDisk_secondPartition @@ -10,6 +10,7 @@ exit :uefi_boot echo "Running in UEFI mode - Booting first disk, second partition" -sanboot --no-describe --drive 0 --filename \EFI\Part-01-02\Boot\ogloader.efi || echo "Failed to boot in UEFI mode" +chain http://__SERVERIP__/tftpboot/grubx64.efi exit + diff --git a/tftpboot/ipxe_scripts/templates/firstDisk_thirdPartition b/tftpboot/ipxe_scripts/templates/firstDisk_thirdPartition index a1677d6..261c0ae 100644 --- a/tftpboot/ipxe_scripts/templates/firstDisk_thirdPartition +++ b/tftpboot/ipxe_scripts/templates/firstDisk_thirdPartition @@ -10,6 +10,7 @@ exit :uefi_boot echo "Running in UEFI mode - Booting first disk, third partition" -sanboot --no-describe --drive 0 --filename \EFI\Part-01-03\Boot\ogloader.efi || echo "Failed to boot in UEFI mode" +chain http://__SERVERIP__/tftpboot/grubx64.efi exit +