about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSamuel Dionne-Riel <samuel@dionne-riel.com>2021-04-23 23:20:49 -0400
committerSamuel Dionne-Riel <samuel@dionne-riel.com>2021-04-24 14:49:05 -0400
commitba666011a64aee55c78c9cb4b0e7c272479d9a2e (patch)
treedc3a71596ed68cf7ba25e78043a55bdeac52d5e3
parent9b18a78c739a2a43c3ed386a4a9c52e4f7360650 (diff)
downloadnixlib-ba666011a64aee55c78c9cb4b0e7c272479d9a2e.tar
nixlib-ba666011a64aee55c78c9cb4b0e7c272479d9a2e.tar.gz
nixlib-ba666011a64aee55c78c9cb4b0e7c272479d9a2e.tar.bz2
nixlib-ba666011a64aee55c78c9cb4b0e7c272479d9a2e.tar.lz
nixlib-ba666011a64aee55c78c9cb4b0e7c272479d9a2e.tar.xz
nixlib-ba666011a64aee55c78c9cb4b0e7c272479d9a2e.tar.zst
nixlib-ba666011a64aee55c78c9cb4b0e7c272479d9a2e.zip
make-disk-image: Account for reserved disk space
This is a bit of a thorny issue. See, the actual `diskSize` variable is
for the *total* disk size, not for the filesystem!

The automatic numbers are meant to compute the *filesystem* required
space. So we have to add any other reserved space!

We have different requirements for reserved space. E.g. there could be
none (when it's actually a filesystem image). There could also be 1MiB
for alignment for an MBR image, legacy+gpt needs 2MiB, then GPT with an
ESP ("bootSize") needs to take the boot partition and GPT size into
account too!

Though luckily(?) for this latter situation we can cheat! As noted in the
change, `bootSize` is NOT the boot partition size. It is actually the
offset where the target filesystem starts.
-rw-r--r--nixos/lib/make-disk-image.nix21
1 files changed, 19 insertions, 2 deletions
diff --git a/nixos/lib/make-disk-image.nix b/nixos/lib/make-disk-image.nix
index f1c942181f04..26a591646e21 100644
--- a/nixos/lib/make-disk-image.nix
+++ b/nixos/lib/make-disk-image.nix
@@ -15,6 +15,8 @@
 
 , # size of the boot partition, is only used if partitionTableType is
   # either "efi" or "hybrid"
+  # This will be undersized slightly, as this is actually the offset of
+  # the end of the partition. Generally it will be 1MiB smaller.
   bootSize ? "256M"
 
 , # The files and directories to be placed in the target file system.
@@ -253,10 +255,25 @@ let format' = format; in let
 
     ${if diskSize == "auto" then ''
       ${if partitionTableType == "efi" || partitionTableType == "hybrid" then ''
-        additionalSpace=$(( ($(numfmt --from=iec '${additionalSpace}') + $(numfmt --from=iec '${bootSize}')) ))
+        # Add the GPT at the end
+        gptSpace=$(( 512 * 34 * 1 ))
+        # Normally we'd need to account for alignment and things, if bootSize
+        # represented the actual size of the boot partition. But it instead
+        # represents the offset at which it ends.
+        # So we know bootSize is the reserved space in front of the partition.
+        reservedSpace=$(( gptSpace + $(numfmt --from=iec '${bootSize}') ))
+      '' else if partitionTableType == "legacy+gpt" then ''
+        # 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 ))
+      '' else if partitionTableType == "legacy" then ''
+        # Add the 1MiB aligned reserved space (includes MBR)
+        reservedSpace=$(( 1024*1024 ))
       '' else ''
-        additionalSpace=$(( $(numfmt --from=iec '${additionalSpace}') ))
+        reservedSpace=0
       ''}
+      additionalSpace=$(( $(numfmt --from=iec '${additionalSpace}') + reservedSpace ))
 
       # Compute required space in filesystem blocks
       diskUsage=$(find . ! -type d -exec 'du' '--apparent-size' '--block-size' "${blockSize}" '{}' ';' | cut -f1 | sum_lines)