From 2c3ce3eb31b2bb22aac829a5d74cc2c15a92ee29 Mon Sep 17 00:00:00 2001 From: Hongbo Date: Mon, 4 Aug 2025 22:56:16 +0800 Subject: [PATCH 1/2] grub: build grub2_xen_pvh This enables the GRUB package to be built for the `xen_pvh` platform, which is needed to generate a corresponding GRUB image and boot PVH DomUs. Signed-off-by: Hongbo Co-authored-by: William Johansson --- pkgs/tools/misc/grub/default.nix | 15 ++++++++++++++- pkgs/top-level/all-packages.nix | 4 ++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/pkgs/tools/misc/grub/default.nix b/pkgs/tools/misc/grub/default.nix index 8b56473f87ab..ab5a6a94964a 100644 --- a/pkgs/tools/misc/grub/default.nix +++ b/pkgs/tools/misc/grub/default.nix @@ -28,6 +28,7 @@ efiSupport ? false, zfsSupport ? false, xenSupport ? false, + xenPvhSupport ? false, kbdcompSupport ? false, ckbcomp, }: @@ -65,6 +66,11 @@ let x86_64-linux.target = "x86_64"; }; + xenPvhSystemsBuild = { + i686-linux.target = "i386"; + x86_64-linux.target = "i386"; # Xen PVH is only i386 on x86. + }; + inPCSystems = lib.any (system: stdenv.hostPlatform.system == system) (lib.attrNames pcSystems); gnulib = fetchFromSavannah { @@ -90,7 +96,8 @@ let in assert zfsSupport -> zfs != null; -assert !(efiSupport && xenSupport); +assert !(efiSupport && (xenSupport || xenPvhSupport)); +assert !(xenSupport && xenPvhSupport); stdenv.mkDerivation rec { pname = "grub"; @@ -610,6 +617,10 @@ stdenv.mkDerivation rec { ++ lib.optionals xenSupport [ "--with-platform=xen" "--target=${xenSystemsBuild.${stdenv.hostPlatform.system}.target}" + ] + ++ lib.optionals xenPvhSupport [ + "--with-platform=xen_pvh" + "--target=${xenPvhSystemsBuild.${stdenv.hostPlatform.system}.target}" ]; # save target that grub is compiled for @@ -661,6 +672,8 @@ stdenv.mkDerivation rec { lib.attrNames efiSystemsBuild else if xenSupport then lib.attrNames xenSystemsBuild + else if xenPvhSupport then + lib.attrNames xenPvhSystemsBuild else platforms.gnu ++ platforms.linux; diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 7841c216b74f..6c4728ed19a3 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -3138,6 +3138,10 @@ with pkgs; xenSupport = true; }; + grub2_xen_pvh = grub2.override { + xenPvhSupport = true; + }; + grub4dos = callPackage ../tools/misc/grub4dos { stdenv = stdenv_32bit; }; From 47881f96c7eff2641cc9386ed28667fc44fa78c1 Mon Sep 17 00:00:00 2001 From: Hongbo Date: Thu, 3 Apr 2025 02:44:34 +0800 Subject: [PATCH 2/2] grub: build grub2_pvhgrub_image together with grub2_pvgrub_image This enables the GRUB package to be built for the `xen_pvh` platform, which is needed to generate a corresponding GRUB image and boot PVH DomUs. Also, modifications made to the original `grub2_pvgrub_image` package: - Use split phases and avoid overriding `buildCommand` during the build. - Remove logic that excludes `all_video.mod` (already ironed out by upstream). - Fix `lib.platforms` and remove platforms that cannot build. - Update the description. - Add maintainer information of the Xen Project team. Signed-off-by: Hongbo Reviewed-by: Fernando Rodrigues Co-authored-by: William Johansson --- .../by-name/gr/grub2_pvgrub_image/package.nix | 54 ------------ .../configs/grub-bootstrap.cfg | 0 .../configs/grub.cfg | 0 .../gr/grub2_pvhgrub_image/package.nix | 83 +++++++++++++++++++ pkgs/top-level/all-packages.nix | 4 + 5 files changed, 87 insertions(+), 54 deletions(-) delete mode 100644 pkgs/by-name/gr/grub2_pvgrub_image/package.nix rename pkgs/by-name/gr/{grub2_pvgrub_image => grub2_pvhgrub_image}/configs/grub-bootstrap.cfg (100%) rename pkgs/by-name/gr/{grub2_pvgrub_image => grub2_pvhgrub_image}/configs/grub.cfg (100%) create mode 100644 pkgs/by-name/gr/grub2_pvhgrub_image/package.nix diff --git a/pkgs/by-name/gr/grub2_pvgrub_image/package.nix b/pkgs/by-name/gr/grub2_pvgrub_image/package.nix deleted file mode 100644 index 3aa240ed9fee..000000000000 --- a/pkgs/by-name/gr/grub2_pvgrub_image/package.nix +++ /dev/null @@ -1,54 +0,0 @@ -{ - lib, - stdenv, - grub2_xen, -}: - -let - efiSystemsBuild = { - i686-linux.target = "i386"; - x86_64-linux.target = "x86_64"; - armv7l-linux.target = "arm"; - aarch64-linux.target = "aarch64"; - riscv32-linux.target = "riscv32"; - riscv64-linux.target = "riscv64"; - }; - -in -( - - stdenv.mkDerivation rec { - name = "pvgrub-image"; - - configs = ./configs; - - buildInputs = [ grub2_xen ]; - - buildCommand = '' - cp "${configs}"/* . - tar -cf memdisk.tar grub.cfg - # We include all modules except all_video.mod as otherwise grub will fail printing "no symbol table" - # if we include it. - grub-mkimage -O "${ - efiSystemsBuild.${stdenv.hostPlatform.system}.target - }-xen" -c grub-bootstrap.cfg \ - -m memdisk.tar -o "grub-${efiSystemsBuild.${stdenv.hostPlatform.system}.target}-xen.bin" \ - $(ls "${grub2_xen}/lib/grub/${ - efiSystemsBuild.${stdenv.hostPlatform.system}.target - }-xen/" |grep 'mod''$'|grep -v '^all_video\.mod''$') - mkdir -p "$out/lib/grub-xen" - cp "grub-${efiSystemsBuild.${stdenv.hostPlatform.system}.target}-xen.bin" $out/lib/grub-xen/ - ''; - - meta = with lib; { - description = "PvGrub image for use for booting PV Xen guests"; - - longDescription = '' - This package provides a PvGrub image for booting Para-Virtualized (PV) - Xen guests - ''; - - platforms = platforms.gnu ++ platforms.linux; - }; - } -) diff --git a/pkgs/by-name/gr/grub2_pvgrub_image/configs/grub-bootstrap.cfg b/pkgs/by-name/gr/grub2_pvhgrub_image/configs/grub-bootstrap.cfg similarity index 100% rename from pkgs/by-name/gr/grub2_pvgrub_image/configs/grub-bootstrap.cfg rename to pkgs/by-name/gr/grub2_pvhgrub_image/configs/grub-bootstrap.cfg diff --git a/pkgs/by-name/gr/grub2_pvgrub_image/configs/grub.cfg b/pkgs/by-name/gr/grub2_pvhgrub_image/configs/grub.cfg similarity index 100% rename from pkgs/by-name/gr/grub2_pvgrub_image/configs/grub.cfg rename to pkgs/by-name/gr/grub2_pvhgrub_image/configs/grub.cfg diff --git a/pkgs/by-name/gr/grub2_pvhgrub_image/package.nix b/pkgs/by-name/gr/grub2_pvhgrub_image/package.nix new file mode 100644 index 000000000000..f65c0dffdc32 --- /dev/null +++ b/pkgs/by-name/gr/grub2_pvhgrub_image/package.nix @@ -0,0 +1,83 @@ +{ + lib, + stdenv, + grub2_xen, + grub2_xen_pvh, + grubPlatform ? "xen_pvh", +}: + +assert lib.assertOneOf "grubPlatform" grubPlatform [ + "xen" + "xen_pvh" +]; +let + targets = { + i686-linux.target = "i386"; + x86_64-linux.target = if grubPlatform == "xen_pvh" then "i386" else "x86_64"; + }; +in +stdenv.mkDerivation { + name = "grub2_${grubPlatform}_image"; + + env = { + GRUB_CONFIGS = "${./configs}"; + GRUB_FORMAT = "${targets.${stdenv.buildPlatform.system}.target}-${grubPlatform}"; + }; + + buildInputs = + lib.optional (grubPlatform == "xen") grub2_xen + ++ lib.optional (grubPlatform == "xen_pvh") grub2_xen_pvh; + + dontUnpack = true; + + buildPhase = '' + cp "$GRUB_CONFIGS"/* . + tar -cf memdisk.tar grub.cfg + grub-mkimage \ + -O "$GRUB_FORMAT" \ + -c grub-bootstrap.cfg \ + -m memdisk.tar \ + -o grub-"$GRUB_FORMAT".bin \ + ${if grubPlatform == "xen_pvh" then grub2_xen_pvh else grub2_xen}/lib/grub/"$GRUB_FORMAT"/*.mod + ''; + + installPhase = '' + mkdir -p "$out/lib/grub-${grubPlatform}" + cp grub-"$GRUB_FORMAT".bin $out/lib/grub-${grubPlatform}/ + ''; + + dontFixup = true; + + meta = { + description = "PvGrub2 image for booting ${ + if grubPlatform == "xen_pvh" then "PVH" else "PV" + } Xen guests"; + + longDescription = + if (grubPlatform == "xen_pvh") then + '' + This package provides a prebuilt PvGrub2 image for booting PVH Xen + guests (also commonly referred to as PvhGrub2), which searches for + `grub.cfg` on the guest disk and interprets it to load the kernel, + eliminating the need to copy the guest kernel to the Dom0. + + You will need `pkgs.grub2_xen_pvh` to build a customized PvhGrub2 + image. See [PvGrub2](https://wiki.xenproject.org/wiki/PvGrub2) for + more explanations. + '' + else + '' + This package provides a prebuilt PvGrub2 image for booting PV Xen + guests, which searches for `grub.cfg` on the guest disk and interprets + it to load the kernel, eliminating the need to copy the guest kernel + to the Dom0. + + You will need `pkgs.grub2_xen` to build a customized PvGrub2 image. + See [PvGrub2](https://wiki.xenproject.org/wiki/PvGrub2) for more + explanations. + ''; + + teams = [ lib.teams.xen ]; + platforms = lib.attrNames targets; + }; +} diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 6c4728ed19a3..484aaa20dcfd 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -3142,6 +3142,10 @@ with pkgs; xenPvhSupport = true; }; + grub2_pvgrub_image = grub2_pvhgrub_image.override { + grubPlatform = "xen"; + }; + grub4dos = callPackage ../tools/misc/grub4dos { stdenv = stdenv_32bit; };