EHfive / flakes

NixOS configurations & Nix packages for @EHfive's mail server & NanoPi R2S router
MIT License
11 stars 1 forks source link

How do you install the nixos on r2s? #1

Closed jerrita closed 5 months ago

jerrita commented 5 months ago

Hi, I've watched your excellent repo for a time. Seems like currently you are using a r2s for router, which is a similar circumstance to me.

However, when I refer some of your codes and try using ${nixpkgs}/nixos/modules/installer/sd-card/sd-image.nix to create an image, it seems failed to boot. It is hard to me to debug the reason, could you pls post some detailed blog for booting a nixos on r2s? Appreciate it a lot!!

EHfive commented 5 months ago

Firstly, Use ${nixpkgs}/nixos/modules/installer/sd-card/sd-image-aarch64.nix instead to have extlinux booting files populated.

And SD image built with sd-image-aarch64.nix does not came with u-boot image flashed by default, you would also need to configure sdImage.postBuildCommands to flash the u-boot image for NanoPi R2S.

{ ... }: {
  sdImage.postBuildCommands = ''
    dd if=${ubootNanopiR2s}/idbloader.img of=$img conv=notrunc seek=64
    dd if=${ubootNanopiR2s}/u-boot.itb of=$img conv=notrunc seek=16384
    # or just flash the combined bootloader image
    dd if=${ubootNanopiR2s}/u-boot-rockchip.bin of=$img conv=notrunc seek=64
  '';
}

Alternative, you can just manually flash the u-boot images after flashing SD image.

dd if=sd-image.img of=/dev/your_sd_card conv=notrunc
dd if=u-boot-rockchip.bin of=/dev/your_sd_card conv=notrunc seek=64

Note the ubootNanopiR2s mentioned here is github:EHfive/flakes#packages.aarch64-linux.ubootNanopiR2s, which is patched for booting kernel image larger than ~64MB as recent NixOS kernels has reached this size.

The R2S should boot just fine with that.

Also it's recommend to have TTL connector monitoring your R2S for debugging boot process.


I personally prefer building system tarball instead of SD image so I can use filesystems other than default ext4, e.g. F2FS. Though I would need to partition the SD card and dispatch tarball contents manually.

Note the boot partition(or a single rootfs) still needs to be ext4 or other filesystems that u-boot can recognize. And the partition should starts from disk block 32768, blocks 0-63 are for partition table, blocks 64-32767 are for u-boot images.

jerrita commented 5 months ago

Thanks, it helps a lot!!

I successfully booted into the system, and it works basically. However, the LEDs of the r8152 (closest to the power) seems not run well, do you have the same issue? Or it seems can be fixed by the this. Any ideals?

jerrita commented 5 months ago

BTW, here are some my tuning methods, wish a help.

# network irq balance
echo 8 > /proc/irq/24/smp_affinity
echo 2 > /proc/irq/47/smp_affinity
echo 7 > /sys/class/net/wan/queues/rx-0/rps_cpus
echo d > /sys/class/net/lan/queues/rx-0/rps_cpus
echo 2048 > /sys/class/net/wan/queues/rx-0/rps_flow_cnt
echo 2048 > /sys/class/net/lan/queues/rx-0/rps_flow_cnt

ethtool -G lan tx 1024
ethtool -G lan rx 1024
ethtool -G wan rx 4096
  zramSwap = {
    enable = true;
    memoryPercent = 150;  # default algo is zstd, excepted ratio is 3
  };

  boot.kernel.sysctl = {
    "vm.swapiness" = 180;
    "vm.watermark_boost_factor" = 0;
    "vm.watermark_scale_factor" = 125;
    "vm.page-cluster" = 0;

    "net.core.rps_sock_flow_entries" = 32768;
    "net.core.netdev_max_backlog" = 3000;
    "net.core.dev_weight" = 600;
  };

And I added following to rk3328.dtsi to overclock the CPU.

  opp-1512000000 {
      opp-hz = /bits/ 64 <1512000000>;
      opp-microvolt = <1450000>;
      clock-latency-ns = <40000>;
  };
EHfive commented 5 months ago

However, the LEDs of the r8152 (closest to the power) seems not run well, do you have the same issue?

Same here, I have no idea why it does not work though. Maybe it's because the DTS for LAN led was misconfigured.

Or it seems can be fixed by the this. Any ideals?

This patch is to allow you configure LEDs around the network port from DTS, it has nothing to do with GPIO connected LEDs in front.

jerrita commented 5 months ago

Just find this: openwrt-patch

Maybe needs to come along with: this patch

Also they used this for system LEDs' settings.

EHfive commented 5 months ago

Just find this: openwrt-patch

That is exactly what I said "allow you configure LEDs around the network port from DTS" in previous comment, note these LEDs are part of network card and they are not exposed in system interface.

In contrast, the LEDs in the front are for general purpose(thus exposed as /sys/class/leds/*) and have no hardware inter connection with network card, that's why you need to configure the LED trigger manually to link network activity in software way.

Also they used this for system LEDs' settings.

These are just re-labeling and aliasing stuffs.

Closing as the original question has been answered.

EHfive commented 1 month ago

@jerrita Hi, I have managed to fix the LED issue. It turns out the IO MUX bits for GPIO2-15 was wrongly overridden to non-GPIO mode causing no output to LAN LED connecting the GPIO2-15.

I have submitted a patch addressing this issue, see https://patchwork.kernel.org/project/linux-rockchip/patch/20240606125755.53778-2-i@eh5.me/ and series.

As a temporary measure before that patch got into kernel running on NixOS, you can set the IO MUX to correct value manually, see https://github.com/EHfive/flakes/commit/abdf698bc4eaa7c70eac709c9c66b76cf4a5923b.

$ nix run nixpkgs#busybox devmem 0xff100028 32 0x00070000

Alternatively, use existing kernel pinctrl debug infrastructure, this is ultimately the same as above.

$ echo "lan-led-pin leds" > /sys/kernel/debug/pinctrl/pinctrl-rockchip-pinctrl/pinmux-select

update: There is now https://github.com/EHfive/rtl8152-led-ctrl to set LED configuration from user space, so you don't have to take enormous time to compile the kernel with patches.

For LAN LED on network interface(USB RTL8153) to work, you would need the out-of-tree driver patch and corresponding DTS property on device DTS node. This is a different and unrelated issue to LEDs in the front panel.

I did't include these patches in my flakes for compilation time sake. However you are free to include them in your NixOS config.

EHfive commented 1 month ago

In case your are interested, I have made an user space tool to set LED configuration for USB connected RTL8152/8153 series NICs on the fly.

https://github.com/EHfive/rtl8152-led-ctrl