From c10c3a2c21563b0f5e719e7e2cb4fa0d37deb196 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Wed, 10 Jan 2024 21:10:16 +0100 Subject: [PATCH] Rewrite format-sdcard.py to use f2fs --- modules/hardware/raspi4/raspi-base.nix | 23 ++++++++++------- scripts/format-sdcard.py | 34 ++++++++++++++------------ 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/modules/hardware/raspi4/raspi-base.nix b/modules/hardware/raspi4/raspi-base.nix index 01b8635..af5e8ad 100644 --- a/modules/hardware/raspi4/raspi-base.nix +++ b/modules/hardware/raspi4/raspi-base.nix @@ -60,14 +60,21 @@ in { }; config = lib.mkIf cfg.enable { - boot = { - supportedFilesystems = - lib.mkForce [ "f2fs" "ntfs" "cifs" "ext4" "vfat" "nfs" "nfs4" ]; - }; + boot.supportedFilesystems = + lib.mkForce [ "f2fs" "ntfs" "cifs" "ext4" "vfat" "nfs" "nfs4" ]; + boot.kernelParams = [ "rootflags=atgc" "rw" ]; + fileSystems."/" = { device = "/dev/disk/by-label/NixosSd"; - fsType = "ext4"; - options = [ "noatime" ]; + fsType = "f2fs"; + options = [ + "atgc,gc_merge" + "compress_algorithm=lz4" + "compress_extension=*" + "compress_chksum" + "discard" + "lazytime" + ]; }; fileSystems."/boot" = { device = "/dev/disk/by-label/SdBoot"; @@ -85,7 +92,7 @@ in { "reset-raspberrypi" # required for vl805 firmware to load ]; - initrd.luks.devices."cryptlvmsd" = { + initrd.luks.devices."cryptsd" = { device = "/dev/mmcblk1p2"; allowDiscards = true; # required for TRIM }; @@ -109,7 +116,5 @@ in { sudo umount /mnt/firmware ''; }; - # Enable TRIM for SD cards - services.fstrim.enable = true; }; } diff --git a/scripts/format-sdcard.py b/scripts/format-sdcard.py index 79fefdc..49630b3 100755 --- a/scripts/format-sdcard.py +++ b/scripts/format-sdcard.py @@ -16,6 +16,12 @@ def _run_command(command, user_input=""): return result +def _partition_suffix(disk): + if ("nvmne" or "mmc") in disk: + return "p" + return "" + + def read_disks(): output = _run_command(["lsblk", "-dpno", "name"]) disks = [] @@ -39,12 +45,20 @@ def get_disk_to_format(): def create_main_partition(disk): print("Create main partition.") _run_command(["parted", "--script", disk, "mkpart", "primary", "1GiB", "100%"]) - return f"{disk}p2" + return f"{disk}{_partition_suffix(disk)}2" def _create_main_filesystem(): - _run_command(["lvcreate", "-l", "100%FREE", "MainGroupSd", "-n", "sdroot"]) - _run_command(["mkfs.ext4", "-L", "NixosSd", "/dev/MainGroupSd/sdroot"]) + _run_command( + [ + "mkfs.f2fs", + "-l", + "NixosSd", + "-O", + "extra_attr,inode_checksum,sb_checksum,compression", + "/dev/mapper/cryptsd", + ] + ) def _encrypt_disk(partition_path): @@ -54,28 +68,18 @@ def _encrypt_disk(partition_path): ["cryptsetup", "luksFormat", "-q", partition_path], user_input=password, ) - _run_command( - ["cryptsetup", "open", partition_path, "cryptlvmsd"], user_input=password - ) - - -def _setup_lvm(lvm_target): - print("Set up LVM on {}.".format(lvm_target)) - _run_command(["pvcreate", lvm_target]) - _run_command(["vgcreate", "MainGroupSd", lvm_target]) + _run_command(["cryptsetup", "open", partition_path, "cryptsd"], user_input=password) def create_file_systems(partition): print("Creating filesystems.") - lvm_target = "/dev/mapper/cryptlvmsd" _encrypt_disk(partition) - _setup_lvm(lvm_target) _create_main_filesystem() def mount_partitions(): print("Mounting partitions.") - _run_command(["mount", "/dev/MainGroupSd/sdroot", "/mnt"]) + _run_command(["mount", "/dev/disk/by-label/NixosSd", "/mnt"]) os.mkdir("/mnt/boot") _run_command(["mount", "/dev/disk/by-label/SdBoot", "/mnt/boot"])