patches and low-level development discussion
 help / color / mirror / code / Atom feed
* Overriding Linux kernel in host/rootfs/default.nix
@ 2022-12-21 11:18 Ivan Nikolaenko
  2022-12-21 11:32 ` Alyssa Ross
  0 siblings, 1 reply; 2+ messages in thread
From: Ivan Nikolaenko @ 2022-12-21 11:18 UTC (permalink / raw)
  To: devel

Hello everyone.

Recently I faced an issue with overriding Linux kernel in rootfs recipe:

Rootfs has an attribute kernel in the `let ... in` section, which is 
used for
setting up correct paths to kernel modules in rootfs derivation. But this
attribute is also used by the derivation `packagesSysroot` which is used
by the derivation `packagesTar`, whiсh, in its turn, used by rootfs 
derivation.

All that derivations introduced in the scope of rootfs derivation (the same
`let ... in` section) and are anonymous.
What happens when I override kernel attribute of the rootfs derivation:
1. Nix evaluates original host/rootfs/default.nix  file and replaces all
${kernel} occurences with the actual value of kernel attribute (which is 
linux_latest  at this moment).
2. Nix sets linux_latest  as the input for rootfs and packagesSysroot 
derivations.
3. Nix evaluates overlay with kernel override and overrides (in fact, 
introduces new) kernel attribute.
4. Nothing happens after that.
Now we have:
- `kernel` attribute of rootfs derivation pointing to the imx8 kernel.
- `MODULES_ALIAS` and `MODULES_ORDER` variables of rootfs derivation are 
pointing to linux_latest modules.
The root of the issue - line `ln -s ${kernel}/lib/modules 
${firmware}/lib/firmware $out/lib`
from `packagesSysroot` derivation build command, which creates the /lib 
link in the rootfs
pointing to linux_latest modules. As I found, kernel attribure is not 
even passed as an attribute to
the rootfs derivation file. But it is possible to override MODULES_ALIAS 
and MODULES_ORDER with correct kernel modules path instead.

This also touches initramfs
The possible solution is just to override linux_latest, but in this case 
all VMs will use host kernel version, and we need to avoid this.

I would propose something like this for rootfs' default.nix, because 
this approach solves all kernel issues in an easy and clear way:

diff --git a/host/rootfs/default.nix b/host/rootfs/default.nix
index ad6ea1f..7bf16d2 100644
--- a/host/rootfs/default.nix
+++ b/host/rootfs/default.nix
@@ -7,7 +7,7 @@ pkgs.pkgsStatic.callPackage (

  { lib, stdenvNoCC, nixos, runCommand, writeReferencesToFile, s6-rc, 
tar2ext4
  , busybox, cloud-hypervisor, cryptsetup, execline, jq, kmod
-, mdevd, s6, s6-linux-init, socat, util-linuxMinimal, xorg
+, mdevd, s6, s6-linux-init, socat, util-linuxMinimal, xorg, kernel ? 
linux_latest
  }:

  let
@@ -73,8 +73,6 @@ let
      imports = [ (modulesPath + "/profiles/all-hardware.nix") ];
    });

-  kernel = pkgs.linux_latest;
-
    appvm = import ../../img/app {
      inherit config;
      inherit (foot) terminfo;

I played a bit with this, and seems that it worked.

Thanks.



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Overriding Linux kernel in host/rootfs/default.nix
  2022-12-21 11:18 Overriding Linux kernel in host/rootfs/default.nix Ivan Nikolaenko
@ 2022-12-21 11:32 ` Alyssa Ross
  0 siblings, 0 replies; 2+ messages in thread
From: Alyssa Ross @ 2022-12-21 11:32 UTC (permalink / raw)
  To: Ivan Nikolaenko; +Cc: devel

[-- Attachment #1: Type: text/plain, Size: 3350 bytes --]

On Wed, Dec 21, 2022 at 01:18:36PM +0200, Ivan Nikolaenko wrote:
> Hello everyone.
>
> Recently I faced an issue with overriding Linux kernel in rootfs recipe:
>
> Rootfs has an attribute kernel in the `let ... in` section, which is used
> for
> setting up correct paths to kernel modules in rootfs derivation. But this
> attribute is also used by the derivation `packagesSysroot` which is used
> by the derivation `packagesTar`, whiсh, in its turn, used by rootfs
> derivation.
>
> All that derivations introduced in the scope of rootfs derivation (the same
> `let ... in` section) and are anonymous.
> What happens when I override kernel attribute of the rootfs derivation:
> 1. Nix evaluates original host/rootfs/default.nix  file and replaces all
> ${kernel} occurences with the actual value of kernel attribute (which is
> linux_latest  at this moment).
> 2. Nix sets linux_latest  as the input for rootfs and packagesSysroot
> derivations.
> 3. Nix evaluates overlay with kernel override and overrides (in fact,
> introduces new) kernel attribute.
> 4. Nothing happens after that.
> Now we have:
> - `kernel` attribute of rootfs derivation pointing to the imx8 kernel.
> - `MODULES_ALIAS` and `MODULES_ORDER` variables of rootfs derivation are
> pointing to linux_latest modules.
> The root of the issue - line `ln -s ${kernel}/lib/modules
> ${firmware}/lib/firmware $out/lib`
> from `packagesSysroot` derivation build command, which creates the /lib link
> in the rootfs
> pointing to linux_latest modules. As I found, kernel attribure is not even
> passed as an attribute to
> the rootfs derivation file. But it is possible to override MODULES_ALIAS and
> MODULES_ORDER with correct kernel modules path instead.
>
> This also touches initramfs
> The possible solution is just to override linux_latest, but in this case all
> VMs will use host kernel version, and we need to avoid this.
>
> I would propose something like this for rootfs' default.nix, because this
> approach solves all kernel issues in an easy and clear way:
>
> diff --git a/host/rootfs/default.nix b/host/rootfs/default.nix
> index ad6ea1f..7bf16d2 100644
> --- a/host/rootfs/default.nix
> +++ b/host/rootfs/default.nix
> @@ -7,7 +7,7 @@ pkgs.pkgsStatic.callPackage (
>
>  { lib, stdenvNoCC, nixos, runCommand, writeReferencesToFile, s6-rc,
> tar2ext4
>  , busybox, cloud-hypervisor, cryptsetup, execline, jq, kmod
> -, mdevd, s6, s6-linux-init, socat, util-linuxMinimal, xorg
> +, mdevd, s6, s6-linux-init, socat, util-linuxMinimal, xorg, kernel ?
> linux_latest
>  }:
>
>  let
> @@ -73,8 +73,6 @@ let
>      imports = [ (modulesPath + "/profiles/all-hardware.nix") ];
>    });
>
> -  kernel = pkgs.linux_latest;
> -
>    appvm = import ../../img/app {
>      inherit config;
>      inherit (foot) terminfo;
>
> I played a bit with this, and seems that it worked.
>
> Thanks.

Seems reasonable!  Happy to apply if you resend with a Signed-off-by.

Although I think it needs to still be pkgs.linux_latest, as
linux_latest isn't defined.  (We don't want to take it as a callPackage
input because we don't need pkgsStatic.linux_latest.)  And it might be a
good idea to put the "kernel" option on a new line, so that overridable
options are different from package inputs.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-12-21 11:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-21 11:18 Overriding Linux kernel in host/rootfs/default.nix Ivan Nikolaenko
2022-12-21 11:32 ` Alyssa Ross

Code repositories for project(s) associated with this public inbox

	https://spectrum-os.org/git/crosvm
	https://spectrum-os.org/git/doc
	https://spectrum-os.org/git/mktuntap
	https://spectrum-os.org/git/nixpkgs
	https://spectrum-os.org/git/spectrum
	https://spectrum-os.org/git/ucspi-vsock
	https://spectrum-os.org/git/www

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).