git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #62462 from samueldr/sd-image/fat-free

sd-image: FAT free /boot

commit6bc2d30ee3276fcfdc59f9ab028b4f4f046411c3 authorSamuel Dionne-Riel <samuel@dionne-riel.com> dateMon 24 Jun 2019, 14:53 (7 years ago) parent7c81998288118c
5 files changed+107−53
nixos/lib/make-ext4-fs.nix+20−5 nixos/modules/installer/cd-dvd/sd-image-aarch64.nix+8−5 nixos/modules/installer/cd-dvd/sd-image-armv7l-multiplatform.nix+9−6 nixos/modules/installer/cd-dvd/sd-image-raspberrypi.nix+8−5 nixos/modules/installer/cd-dvd/sd-image.nix+62−32
nixos/lib/make-ext4-fs.nix+20−5
@@ -1,9 +1,14 @@
11# Builds an ext4 image containing a populated /nix/store with the closure
2−# of store paths passed in the storePaths parameter. The generated image
3−# is sized to only fit its contents, with the expectation that a script
4−# resizes the filesystem at boot time.
2+# of store paths passed in the storePaths parameter, in addition to the
3+# contents of a directory that can be populated with commands. The
4+# generated image is sized to only fit its contents, with the expectation
5+# that a script resizes the filesystem at boot time.
56{ pkgs
7+# List of derivations to be included
68, storePaths
9+# Shell commands to populate the ./files directory.
10+# All files in that directory are copied to the root of the FS.
11+, populateImageCommands ? ""
712, volumeLabel
813, uuid ? "44444444-4444-4444-8888-888888888888"
914, e2fsprogs
@@ -23,13 +28,17 @@ pkgs.stdenv.mkDerivation {
2328
2429 buildCommand =
2530 ''
31+ (
32+ mkdir -p ./files
33+ ${populateImageCommands}
34+ )
2635 # Add the closures of the top-level store objects.
2736 storePaths=$(cat ${sdClosureInfo}/store-paths)
2837
2938 # Make a crude approximation of the size of the target image.
3039 # If the script starts failing, increase the fudge factors here.
31− numInodes=$(find $storePaths | wc -l)
32− numDataBlocks=$(du -c -B 4096 --apparent-size $storePaths | awk '$2 == "total" { print int($1 * 1.03) }')
40+ numInodes=$(find $storePaths ./files | wc -l)
41+ numDataBlocks=$(du -s -c -B 4096 --apparent-size $storePaths ./files | tail -1 | awk '{ print int($1 * 1.03) }')
3342 bytes=$((2 * 4096 * $numInodes + 4096 * $numDataBlocks))
3443 echo "Creating an EXT4 image of $bytes bytes (numInodes=$numInodes, numDataBlocks=$numDataBlocks)"
3544
@@ -47,6 +56,12 @@ pkgs.stdenv.mkDerivation {
4756 echo "copying store paths to image..."
4857 cptofs -t ext4 -i $out $storePaths /nix/store/
4958
59+ (
60+ echo "copying files to image..."
61+ cd ./files
62+ cptofs -t ext4 -i $out ./* /
63+ )
64+
5065 # I have ended up with corrupted images sometimes, I suspect that happens when the build machine's disk gets full during the build.
5166 if ! fsck.ext4 -n -f $out; then
5267 echo "--- Fsck failed for EXT4 image of $bytes bytes (numInodes=$numInodes, numDataBlocks=$numDataBlocks) ---"
nixos/modules/installer/cd-dvd/sd-image-aarch64.nix+8−5
@@ -34,7 +34,7 @@ in
3434 ];
3535
3636 sdImage = {
37− populateBootCommands = let
37+ populateFirmwareCommands = let
3838 configTxt = pkgs.writeText "config.txt" ''
3939 kernel=u-boot-rpi3.bin
4040
@@ -50,10 +50,13 @@ in
5050 avoid_warnings=1
5151 '';
5252 in ''
53− (cd ${pkgs.raspberrypifw}/share/raspberrypi/boot && cp bootcode.bin fixup*.dat start*.elf $NIX_BUILD_TOP/boot/)
54− cp ${pkgs.ubootRaspberryPi3_64bit}/u-boot.bin boot/u-boot-rpi3.bin
55− cp ${configTxt} boot/config.txt
56− ${extlinux-conf-builder} -t 3 -c ${config.system.build.toplevel} -d ./boot
53+ (cd ${pkgs.raspberrypifw}/share/raspberrypi/boot && cp bootcode.bin fixup*.dat start*.elf $NIX_BUILD_TOP/firmware/)
54+ cp ${pkgs.ubootRaspberryPi3_64bit}/u-boot.bin firmware/u-boot-rpi3.bin
55+ cp ${configTxt} firmware/config.txt
5756 '';
57+ populateRootCommands = ''
58+ mkdir -p ./files/boot
59+ ${extlinux-conf-builder} -t 3 -c ${config.system.build.toplevel} -d ./files/boot
60+ '';
5861 };
5962}
nixos/modules/installer/cd-dvd/sd-image-armv7l-multiplatform.nix+9−6
@@ -29,7 +29,7 @@ in
2929 boot.kernelParams = ["console=ttyS0,115200n8" "console=ttymxc0,115200n8" "console=ttyAMA0,115200n8" "console=ttyO0,115200n8" "console=ttySAC2,115200n8" "console=tty0"];
3030
3131 sdImage = {
32− populateBootCommands = let
32+ populateFirmwareCommands = let
3333 configTxt = pkgs.writeText "config.txt" ''
3434 # Prevent the firmware from smashing the framebuffer setup done by the mainline kernel
3535 # when attempting to show low-voltage or overtemperature warnings.
@@ -46,11 +46,14 @@ in
4646 enable_uart=1
4747 '';
4848 in ''
49− (cd ${pkgs.raspberrypifw}/share/raspberrypi/boot && cp bootcode.bin fixup*.dat start*.elf $NIX_BUILD_TOP/boot/)
50− cp ${pkgs.ubootRaspberryPi2}/u-boot.bin boot/u-boot-rpi2.bin
51− cp ${pkgs.ubootRaspberryPi3_32bit}/u-boot.bin boot/u-boot-rpi3.bin
52− cp ${configTxt} boot/config.txt
53− ${extlinux-conf-builder} -t 3 -c ${config.system.build.toplevel} -d ./boot
49+ (cd ${pkgs.raspberrypifw}/share/raspberrypi/boot && cp bootcode.bin fixup*.dat start*.elf $NIX_BUILD_TOP/firmware/)
50+ cp ${pkgs.ubootRaspberryPi2}/u-boot.bin firmware/u-boot-rpi2.bin
51+ cp ${pkgs.ubootRaspberryPi3_32bit}/u-boot.bin firmware/u-boot-rpi3.bin
52+ cp ${configTxt} firmware/config.txt
5453 '';
54+ populateRootCommands = ''
55+ mkdir -p ./files/boot
56+ ${extlinux-conf-builder} -t 3 -c ${config.system.build.toplevel} -d ./files/boot
57+ '';
5558 };
5659}
nixos/modules/installer/cd-dvd/sd-image-raspberrypi.nix+8−5
@@ -35,11 +35,14 @@ in
3535 kernel=u-boot-rpi1.bin
3636 '';
3737 in ''
38− (cd ${pkgs.raspberrypifw}/share/raspberrypi/boot && cp bootcode.bin fixup*.dat start*.elf $NIX_BUILD_TOP/boot/)
39− cp ${pkgs.ubootRaspberryPiZero}/u-boot.bin boot/u-boot-rpi0.bin
40− cp ${pkgs.ubootRaspberryPi}/u-boot.bin boot/u-boot-rpi1.bin
41− cp ${configTxt} boot/config.txt
42− ${extlinux-conf-builder} -t 3 -c ${config.system.build.toplevel} -d ./boot
38+ (cd ${pkgs.raspberrypifw}/share/raspberrypi/boot && cp bootcode.bin fixup*.dat start*.elf $NIX_BUILD_TOP/firmware/)
39+ cp ${pkgs.ubootRaspberryPiZero}/u-boot.bin firmware/u-boot-rpi0.bin
40+ cp ${pkgs.ubootRaspberryPi}/u-boot.bin firmware/u-boot-rpi1.bin
41+ cp ${configTxt} firmware/config.txt
4342 '';
43+ populateRootCommands = ''
44+ mkdir -p ./files/boot
45+ ${extlinux-conf-builder} -t 3 -c ${config.system.build.toplevel} -d ./files/boot
46+ '';
4447 };
4548}
nixos/modules/installer/cd-dvd/sd-image.nix+62−32
@@ -1,8 +1,12 @@
11# This module creates a bootable SD card image containing the given NixOS
2−# configuration. The generated image is MBR partitioned, with a FAT /boot
3−# partition, and ext4 root partition. The generated image is sized to fit
4−# its contents, and a boot script automatically resizes the root partition
5−# to fit the device on the first boot.
2+# configuration. The generated image is MBR partitioned, with a FAT
3+# /boot/firmware partition, and ext4 root partition. The generated image
4+# is sized to fit its contents, and a boot script automatically resizes
5+# the root partition to fit the device on the first boot.
6+#
7+# The firmware partition is built with expectation to hold the Raspberry
8+# Pi firmware and bootloader, and be removed and replaced with a firmware
9+# build for the target SoC for other board families.
610#
711# The derivation for the SD image will be placed in
812# config.system.build.sdImage
@@ -14,12 +18,18 @@ with lib;
1418let
1519 rootfsImage = pkgs.callPackage ../../../lib/make-ext4-fs.nix ({
1620 inherit (config.sdImage) storePaths;
21+ populateImageCommands = config.sdImage.populateRootCommands;
1722 volumeLabel = "NIXOS_SD";
1823 } // optionalAttrs (config.sdImage.rootPartitionUUID != null) {
1924 uuid = config.sdImage.rootPartitionUUID;
2025 });
2126in
2227{
28+ imports = [
29+ (mkRemovedOptionModule [ "sdImage" "bootPartitionID" ] "The FAT partition for SD image now only holds the Raspberry Pi firmware files. Use firmwarePartitionID to configure that partition's ID.")
30+ (mkRemovedOptionModule [ "sdImage" "bootSize" ] "The boot files for SD image have been moved to the main ext4 partition. The FAT partition now only holds the Raspberry Pi firmware files. Changing its size may not be required.")
31+ ];
32+
2333 options.sdImage = {
2434 imageName = mkOption {
2535 default = "${config.sdImage.imageBaseName}-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}.img";
@@ -43,12 +53,12 @@ in
4353 '';
4454 };
4555
46− bootPartitionID = mkOption {
56+ firmwarePartitionID = mkOption {
4757 type = types.string;
4858 default = "0x2178694e";
4959 description = ''
50− Volume ID for the /boot partition on the SD card. This value must be a
51− 32-bit hexadecimal number.
60+ Volume ID for the /boot/firmware partition on the SD card. This value
61+ must be a 32-bit hexadecimal number.
5262 '';
5363 };
5464
@@ -61,29 +71,44 @@ in
6171 '';
6272 };
6373
64− bootSize = mkOption {
74+ firmwareSize = mkOption {
6575 type = types.int;
66− default = 120;
76+ # As of 2019-05-31 the Raspberry pi firmware + u-bot takes ~13MiB
77+ default = 20;
78+ description = ''
79+ Size of the /boot/firmware partition, in megabytes.
80+ '';
81+ };
82+
83+ populateFirmwareCommands = mkOption {
84+ example = literalExample "'' cp \${pkgs.myBootLoader}/u-boot.bin firmware/ ''";
6785 description = ''
68− Size of the /boot partition, in megabytes.
86+ Shell commands to populate the ./firmware directory.
87+ All files in that directory are copied to the
88+ /boot/firmware partition on the SD image.
6989 '';
7090 };
7191
72− populateBootCommands = mkOption {
73− example = literalExample "'' cp \${pkgs.myBootLoader}/u-boot.bin boot/ ''";
92+ populateRootCommands = mkOption {
93+ example = literalExample "''\${extlinux-conf-builder} -t 3 -c \${config.system.build.toplevel} -d ./files/boot''";
7494 description = ''
75− Shell commands to populate the ./boot directory.
95+ Shell commands to populate the ./files directory.
7696 All files in that directory are copied to the
77− /boot partition on the SD image.
97+ root (/) partition on the SD image. Use this to
98+ populate the ./files/boot (/boot) directory.
7899 '';
79100 };
80101 };
81102
82103 config = {
83104 fileSystems = {
84− "/boot" = {
85− device = "/dev/disk/by-label/NIXOS_BOOT";
105+ "/boot/firmware" = {
106+ device = "/dev/disk/by-label/FIRMWARE";
86107 fsType = "vfat";
108+ # Alternatively, this could be removed from the configuration.
109+ # The filesystem is not needed at runtime, it could be treated
110+ # as an opaque blob instead of a discrete FAT32 filesystem.
111+ options = [ "nofail" "noauto" ];
87112 };
88113 "/" = {
89114 device = "/dev/disk/by-label/NIXOS_SD";
@@ -105,39 +130,44 @@ in
105130 echo "${pkgs.stdenv.buildPlatform.system}" > $out/nix-support/system
106131 echo "file sd-image $img" >> $out/nix-support/hydra-build-products
107132
108− # Create the image file sized to fit /boot and /, plus 20M of slack
133+ # Gap in front of the first partition, in MiB
134+ gap=8
135+
136+ # Create the image file sized to fit /boot/firmware and /, plus slack for the gap.
109137 rootSizeBlocks=$(du -B 512 --apparent-size ${rootfsImage} | awk '{ print $1 }')
110− bootSizeBlocks=$((${toString config.sdImage.bootSize} * 1024 * 1024 / 512))
111− imageSize=$((rootSizeBlocks * 512 + bootSizeBlocks * 512 + 20 * 1024 * 1024))
138+ firmwareSizeBlocks=$((${toString config.sdImage.firmwareSize} * 1024 * 1024 / 512))
139+ imageSize=$((rootSizeBlocks * 512 + firmwareSizeBlocks * 512 + gap * 1024 * 1024))
112140 truncate -s $imageSize $img
113141
114142 # type=b is 'W95 FAT32', type=83 is 'Linux'.
143+ # The "bootable" partition is where u-boot will look file for the bootloader
144+ # information (dtbs, extlinux.conf file).
115145 sfdisk $img <<EOF
116146 label: dos
117− label-id: ${config.sdImage.bootPartitionID}
147+ label-id: ${config.sdImage.firmwarePartitionID}
118148
119− start=8M, size=$bootSizeBlocks, type=b, bootable
120− start=${toString (8 + config.sdImage.bootSize)}M, type=83
149+ start=''${gap}M, size=$firmwareSizeBlocks, type=b
150+ start=$((gap + ${toString config.sdImage.firmwareSize}))M, type=83, bootable
121151 EOF
122152
123153 # Copy the rootfs into the SD image
124154 eval $(partx $img -o START,SECTORS --nr 2 --pairs)
125155 dd conv=notrunc if=${rootfsImage} of=$img seek=$START count=$SECTORS
126156
127− # Create a FAT32 /boot partition of suitable size into bootpart.img
157+ # Create a FAT32 /boot/firmware partition of suitable size into firmware_part.img
128158 eval $(partx $img -o START,SECTORS --nr 1 --pairs)
129− truncate -s $((SECTORS * 512)) bootpart.img
130− faketime "1970-01-01 00:00:00" mkfs.vfat -i ${config.sdImage.bootPartitionID} -n NIXOS_BOOT bootpart.img
159+ truncate -s $((SECTORS * 512)) firmware_part.img
160+ faketime "1970-01-01 00:00:00" mkfs.vfat -i ${config.sdImage.firmwarePartitionID} -n FIRMWARE firmware_part.img
131161
132− # Populate the files intended for /boot
133− mkdir boot
134− ${config.sdImage.populateBootCommands}
162+ # Populate the files intended for /boot/firmware
163+ mkdir firmware
164+ ${config.sdImage.populateFirmwareCommands}
135165
136− # Copy the populated /boot into the SD image
137− (cd boot; mcopy -psvm -i ../bootpart.img ./* ::)
166+ # Copy the populated /boot/firmware into the SD image
167+ (cd firmware; mcopy -psvm -i ../firmware_part.img ./* ::)
138168 # Verify the FAT partition before copying it.
139− fsck.vfat -vn bootpart.img
140− dd conv=notrunc if=bootpart.img of=$img seek=$START count=$SECTORS
169+ fsck.vfat -vn firmware_part.img
170+ dd conv=notrunc if=firmware_part.img of=$img seek=$START count=$SECTORS
141171 '';
142172 }) {};
143173