From 61bdaad9a9221f19a7c12d8b1b654e6e54d2be32 Mon Sep 17 00:00:00 2001 From: Samuel Dionne-Riel Date: Wed, 28 Nov 2018 22:10:15 -0500 Subject: [PATCH] sd-image: Slims the ext4 filesystem even more. This is to try and squeeze more lost space from the image, so that hydra starts building it again. The fsck previous to the resize2fs is required so resize2fs works. The one afterwards is a sanity check. Using `-M` from resize2fs will not give much saved space due to a known (in the manual) issue. ``` [samueldr@aarch64:~/nixpkgs]$ ls -lh result-*/*/*.img -r--r--r-- 1 root root 2.2G Jan 1 1970 result-original/sd-image/nixos-sd-image-18.09.git.a7fd431-aarch64-linux.img -r--r--r-- 1 root root 2.1G Jan 1 1970 result-M/sd-image/nixos-sd-image-18.09.git.a7fd431-aarch64-linux.img -r--r--r-- 1 root root 1.9G Jan 1 1970 result-slimmed/sd-image/nixos-sd-image-18.09.git.a7fd431-aarch64-linux.img ``` ``` [samueldr@aarch64:~/nixpkgs]$ nix path-info -S ./result-original /nix/store/c8k9n78gylx293rjh762fr05a069kxp2-nixos-sd-image-18.09.git.a7fd431-aarch64-linux.img 3844125000 [samueldr@aarch64:~/nixpkgs]$ nix path-info -S ./result-slimmed /nix/store/962238skj5mnzhrsmjy23dyzmxk77sp4-nixos-sd-image-18.09.git.a7fd431-aarch64-linux.img 3447473208 ``` --- nixos/lib/make-ext4-fs.nix | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/nixos/lib/make-ext4-fs.nix b/nixos/lib/make-ext4-fs.nix index 88be8b73ab37..694142a5123a 100644 --- a/nixos/lib/make-ext4-fs.nix +++ b/nixos/lib/make-ext4-fs.nix @@ -94,5 +94,24 @@ pkgs.stdenv.mkDerivation { cat errorlog return 1 fi + + ( + # Resizes **snugly** to its actual limits (or closer to) + free=$(dumpe2fs $out | grep '^Free blocks:') + blocksize=$(dumpe2fs $out | grep '^Block size:') + blocks=$(dumpe2fs $out | grep '^Block count:') + blocks=$((''${blocks##*:})) # format the number. + blocksize=$((''${blocksize##*:})) # format the number. + # System can't boot with 0 blocks free. + # Add 16MiB of free space + fudge=$(( 16 * 1024 * 1024 / blocksize )) + size=$(( blocks - ''${free##*:} + fudge )) + + echo "Resizing from $blocks blocks to $size blocks. (~ $((size*blocksize/1024/1024))MiB)" + EXT2FS_NO_MTAB_OK=yes resize2fs $out -f $size + ) + + # And a final fsck, because of the previous truncating. + fsck.ext4 -n -f $out ''; }