From 1cbfa9d693a0f00fd3e007604cc65e9ad69abe54 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 8 Aug 2018 04:49:13 -0400 Subject: [PATCH 1/5] [partition] Simplify code for sizing --- .../partition/core/PartitionActions.cpp | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/modules/partition/core/PartitionActions.cpp b/src/modules/partition/core/PartitionActions.cpp index d654edf12..999847e37 100644 --- a/src/modules/partition/core/PartitionActions.cpp +++ b/src/modules/partition/core/PartitionActions.cpp @@ -114,19 +114,11 @@ doAutopartition( PartitionCoreModule* core, Device* dev, const QString& luksPass defaultFsType = "ext4"; // Partition sizes are expressed in MiB, should be multiples of - // the logical sector size (usually 512B). - int uefisys_part_size = 0; - int empty_space_size = 0; - if ( isEfi ) - { - uefisys_part_size = 300; - empty_space_size = 2; - } - else - { - // we start with a 1MiB offset before the first partition - empty_space_size = 1; - } + // the logical sector size (usually 512B). EFI starts with 2MiB + // empty and a 300MiB EFI boot partition, while BIOS starts at + // the 1MiB boundary (usually sector 2048). + int uefisys_part_size = isEfi ? 300 : 0; + int empty_space_size = isEfi ? 2 : 1; qint64 firstFreeSector = MiBtoBytes(empty_space_size) / dev->logicalSize() + 1; From 59fea041b601af942833880506822a50ec755407 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 8 Aug 2018 05:16:46 -0400 Subject: [PATCH 2/5] [partition] Fix up calculations of sectors in auto-partition - Calculating first free sector had an off-by-one so that partitioning would start at 2049. - EFI boot partition grew 1 sector larger than desired. - While here, align everything to 1MiB boundaries as well. FIXES #1008 --- .../partition/core/PartitionActions.cpp | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/modules/partition/core/PartitionActions.cpp b/src/modules/partition/core/PartitionActions.cpp index 999847e37..87c9d6e72 100644 --- a/src/modules/partition/core/PartitionActions.cpp +++ b/src/modules/partition/core/PartitionActions.cpp @@ -101,6 +101,24 @@ swapSuggestion( const qint64 availableSpaceB ) return suggestedSwapSizeB; } +constexpr qint64 +alignBytesToBlockSize( qint64 bytes, qint64 blocksize ) +{ + Q_ASSERT( bytes >= 0 ); + Q_ASSERT( blocksize > 0 ); + qint64 blocks = bytes / blocksize; + Q_ASSERT( blocks >= 0 ); + + if ( blocks * blocksize != bytes ) + ++blocks; + return blocks * blocksize; +} + +constexpr qint64 +bytesToSectors( qint64 bytes, qint64 blocksize ) +{ + return alignBytesToBlockSize( alignBytesToBlockSize( bytes, blocksize), MiBtoBytes(1) ) / blocksize; +} void doAutopartition( PartitionCoreModule* core, Device* dev, const QString& luksPassphrase ) @@ -120,11 +138,20 @@ doAutopartition( PartitionCoreModule* core, Device* dev, const QString& luksPass int uefisys_part_size = isEfi ? 300 : 0; int empty_space_size = isEfi ? 2 : 1; - qint64 firstFreeSector = MiBtoBytes(empty_space_size) / dev->logicalSize() + 1; + // Since sectors count from 0, if the space is 2048 sectors in size, + // the first free sector has number 2048 (and there are 2048 sectors + // before that one, numbered 0..2047). + qint64 firstFreeSector = bytesToSectors( MiBtoBytes(empty_space_size), dev->logicalSize() ); if ( isEfi ) { - qint64 lastSector = firstFreeSector + ( MiBtoBytes(uefisys_part_size) / dev->logicalSize() ); + qint64 efiSectorCount = bytesToSectors( MiBtoBytes(uefisys_part_size), dev->logicalSize() ); + Q_ASSERT( efiSectorCount > 0 ); + + // Since sectors count from 0, and this partition is created starting + // at firstFreeSector, we need efiSectorCount sectors, numbered + // firstFreeSector..firstFreeSector+efiSectorCount-1. + qint64 lastSector = firstFreeSector + efiSectorCount - 1; core->createPartitionTable( dev, PartitionTable::gpt ); Partition* efiPartition = KPMHelpers::createNewPartition( dev->partitionTable(), From 8ed26e537f73959e295e82b4cc51540478393204 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 8 Aug 2018 05:26:55 -0400 Subject: [PATCH 3/5] [partition] Fix swap maximum size - The existing calculation comment says "maximum 10% of disk" but the calculation itself uses 110%. --- src/modules/partition/core/PartitionActions.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/partition/core/PartitionActions.cpp b/src/modules/partition/core/PartitionActions.cpp index 87c9d6e72..130f5ab01 100644 --- a/src/modules/partition/core/PartitionActions.cpp +++ b/src/modules/partition/core/PartitionActions.cpp @@ -90,7 +90,7 @@ swapSuggestion( const qint64 availableSpaceB ) suggestedSwapSizeB *= overestimationFactor; // don't use more than 10% of available space - qreal maxSwapDiskRatio = 1.10; + qreal maxSwapDiskRatio = 0.10; qint64 maxSwapSizeB = availableSpaceB * maxSwapDiskRatio; if ( suggestedSwapSizeB > maxSwapSizeB ) suggestedSwapSizeB = maxSwapSizeB; From f10bab8a3af9c3504cf566468e5e4f9ec219a0fe Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 8 Aug 2018 05:29:19 -0400 Subject: [PATCH 4/5] [partition] Minor code-styling --- src/modules/partition/core/PartitionActions.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/partition/core/PartitionActions.cpp b/src/modules/partition/core/PartitionActions.cpp index 130f5ab01..6db23c00a 100644 --- a/src/modules/partition/core/PartitionActions.cpp +++ b/src/modules/partition/core/PartitionActions.cpp @@ -96,7 +96,7 @@ swapSuggestion( const qint64 availableSpaceB ) suggestedSwapSizeB = maxSwapSizeB; } - cDebug() << "Suggested swap size:" << suggestedSwapSizeB / 1024. / 1024. /1024. << "GiB"; + cDebug() << "Suggested swap size:" << suggestedSwapSizeB / 1024. / 1024. / 1024. << "GiB"; return suggestedSwapSizeB; } From 238a1e812f999fad6f98fff750cf4a086cec8495 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 8 Aug 2018 05:58:41 -0400 Subject: [PATCH 5/5] [partition] Reduce the fudge-factor for swap size - Reported by Bill Auger (I think), a 15GiB disk wouldn't hold a 8.9GiB root plus 4GiB swap -- due to 10% overprovisioning of swap, plus the 2.1GiB fudge factor. --- src/modules/partition/core/PartitionActions.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/modules/partition/core/PartitionActions.cpp b/src/modules/partition/core/PartitionActions.cpp index 6db23c00a..68ad85cf2 100644 --- a/src/modules/partition/core/PartitionActions.cpp +++ b/src/modules/partition/core/PartitionActions.cpp @@ -181,8 +181,11 @@ doAutopartition( PartitionCoreModule* core, Device* dev, const QString& luksPass { qint64 availableSpaceB = ( dev->totalLogical() - firstFreeSector ) * dev->logicalSize(); suggestedSwapSizeB = swapSuggestion( availableSpaceB ); + // Space required by this installation is what the distro claims is needed + // (via global configuration) plus the swap size plus a fudge factor of + // 0.6GiB (this was 2.1GiB up to Calamares 3.2.2). qint64 requiredSpaceB = - GiBtoBytes( gs->value( "requiredStorageGB" ).toDouble() + 0.1 + 2.0 ) + + GiBtoBytes( gs->value( "requiredStorageGB" ).toDouble() + 0.6 ) + suggestedSwapSizeB; // If there is enough room for ESP + root + swap, create swap, otherwise don't.