From 7b8b3fab6dce5357c14fc435b436514fc985f0f7 Mon Sep 17 00:00:00 2001 From: Samuel Dionne-Riel Date: Sun, 25 Apr 2021 15:24:45 -0400 Subject: [PATCH] make-disk-image: Round image size to the next mebibyte This ensures the following gptfdisk warning won't happen: ``` Warning: File size is not a multiple of 512 bytes! Misbehavior is likely! ``` Additionally, helps towards aligning the partition to be more optimal for the underlying storage. It is actually impossible to align for the actual underlying storage optimally because we don't know what the block device will be! But aligning on 1MiB should help. --- nixos/lib/make-disk-image.nix | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/nixos/lib/make-disk-image.nix b/nixos/lib/make-disk-image.nix index 26a591646e2..d87b3ef8da0 100644 --- a/nixos/lib/make-disk-image.nix +++ b/nixos/lib/make-disk-image.nix @@ -188,6 +188,8 @@ let format' = format; in let echo "$acc" } + mebibyte=$(( 1024 * 1024 )) + # Approximative percentage of reserved space in an ext4 fs over 512MiB. # 0.05208587646484375 # × 1000, integer part: 52 @@ -266,10 +268,10 @@ let format' = format; in let # Add the GPT at the end gptSpace=$(( 512 * 34 * 1 )) # And include the bios_grub partition; the ext4 partition starts at 2MB exactly. - reservedSpace=$(( gptSpace + 2 * 1024*1024 )) + reservedSpace=$(( gptSpace + 2 * mebibyte )) '' else if partitionTableType == "legacy" then '' # Add the 1MiB aligned reserved space (includes MBR) - reservedSpace=$(( 1024*1024 )) + reservedSpace=$(( mebibyte )) '' else '' reservedSpace=0 ''} @@ -286,6 +288,14 @@ let format' = format; in let requiredFilesystemSpace=$(( diskUsage + fudge )) diskSize=$(( requiredFilesystemSpace + additionalSpace )) + + # Round up to the nearest mebibyte. + # This ensures whole 512 bytes sector sizes in the disk image + # and helps towards aligning partitions optimally. + if (( diskSize % mebibyte )); then + diskSize=$(( ( diskSize / mebibyte + 1) * mebibyte )) + fi + truncate -s "$diskSize" $diskImage printf "Automatic disk size...\n"