about summary refs log tree commit diff
path: root/nixos/lib
diff options
context:
space:
mode:
authorSamuel Dionne-Riel2021-04-25 15:24:45 -0400
committerSamuel Dionne-Riel2021-04-25 15:24:45 -0400
commit7b8b3fab6dce5357c14fc435b436514fc985f0f7 (patch)
treeb2e504147bd4c13f5aa5a9058be4c78270c4f288 /nixos/lib
parent5aa4273e4f5e0ea7d0abd61d57c1c2301e284f23 (diff)
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.
Diffstat (limited to 'nixos/lib')
-rw-r--r--nixos/lib/make-disk-image.nix14
1 files changed, 12 insertions, 2 deletions
diff --git a/nixos/lib/make-disk-image.nix b/nixos/lib/make-disk-image.nix
index 26a591646e21..d87b3ef8da0c 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"