[partition] fix PartitionActions

This commit is contained in:
Philip Müller 2018-12-01 09:18:04 +01:00
parent 961b603f78
commit a33cd43d09

View File

@ -28,7 +28,6 @@
#include "utils/Units.h"
#include "JobQueue.h"
#include "utils/Logger.h"
#include "GlobalStorage.h"
#include <kpmcore/core/device.h>
#include <kpmcore/core/partition.h>
@ -43,58 +42,38 @@ using CalamaresUtils::operator""_GiB;
using CalamaresUtils::operator""_MiB;
qint64
swapSuggestion( const qint64 availableSpaceB )
swapSuggestion( const qint64 availableSpaceB, Choices::SwapChoice swap )
{
/* If suspend-to-disk is demanded, then we always need enough
* swap to write the whole memory to disk -- between 2GB and 8GB
* RAM give proportionally more swap, and from 8GB RAM keep
* swap = RAM.
*
* If suspend-to-disk is not demanded, then ramp up more slowly,
* to 8GB swap at 16GB memory, and then drop to 4GB for "large
* memory" machines, on the assumption that those don't need swap
* because they have tons of memory (or whatever they are doing,
* had better not run into swap).
*/
if ( ( swap != Choices::SmallSwap ) && ( swap != Choices::FullSwap ) )
return 0;
// See partition.conf for explanation
qint64 suggestedSwapSizeB = 0;
auto memory = CalamaresUtils::System::instance()->getTotalMemoryB();
qint64 availableRamB = memory.first;
qreal overestimationFactor = memory.second;
bool ensureSuspendToDisk =
Calamares::JobQueue::instance()->globalStorage()->
value( "ensureSuspendToDisk" ).toBool();
bool ensureSuspendToDisk = swap == Choices::FullSwap;
if ( ensureSuspendToDisk )
{
if ( availableRamB < 4_GiB )
suggestedSwapSizeB = qMax( 2_GiB, availableRamB * 2 );
else if ( availableRamB >= 4_GiB && availableRamB < 8_GiB )
// Ramp up quickly to 8GiB, then follow memory size
if ( availableRamB <= 4_GiB )
suggestedSwapSizeB = availableRamB * 2;
else if ( availableRamB <= 8_GiB )
suggestedSwapSizeB = 8_GiB;
else
suggestedSwapSizeB = availableRamB;
suggestedSwapSizeB *= overestimationFactor;
}
else //if we don't care about suspend to disk
{
if ( availableRamB < 2_GiB )
suggestedSwapSizeB = qMax( 2_GiB, availableRamB * 2 );
else if ( availableRamB >= 2_GiB && availableRamB < 8_GiB )
suggestedSwapSizeB = availableRamB;
else if ( availableRamB >= 8_GiB && availableRamB < 16_GiB )
suggestedSwapSizeB = 8_GiB;
else
suggestedSwapSizeB = 4_GiB;
// .. top out at 8GiB if we don't care about suspend
if ( !ensureSuspendToDisk )
suggestedSwapSizeB = qMin( 8_GiB, suggestedSwapSizeB );
// Allow for a fudge factor
suggestedSwapSizeB *= overestimationFactor;
// don't use more than 10% of available space
qreal maxSwapDiskRatio = 0.10;
qint64 maxSwapSizeB = availableSpaceB * maxSwapDiskRatio;
if ( suggestedSwapSizeB > maxSwapSizeB )
suggestedSwapSizeB = maxSwapSizeB;
}
if ( !ensureSuspendToDisk )
suggestedSwapSizeB = qMin( suggestedSwapSizeB, qint64( 0.10 * availableSpaceB ) );
cDebug() << "Suggested swap size:" << suggestedSwapSizeB / 1024. / 1024. / 1024. << "GiB";
@ -118,16 +97,14 @@ bytesToSectors( qint64 bytes, qint64 blocksize )
}
void
doAutopartition( PartitionCoreModule* core, Device* dev, const QString& luksPassphrase )
doAutopartition( PartitionCoreModule* core, Device* dev, Choices::AutoPartitionOptions o )
{
Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
bool isEfi = PartUtils::isEfiSystem();
QString defaultFsType = gs->value( "defaultFileSystemType" ).toString();
QString defaultFsType = o.defaultFsType;
if ( FileSystem::typeForName( defaultFsType ) == FileSystem::Unknown )
defaultFsType = "ext4";
bool isEfi = PartUtils::isEfiSystem();
// Partition sizes are expressed in MiB, should be multiples of
// the logical sector size (usually 512B). EFI starts with 2MiB
// empty and a 300MiB EFI boot partition, while BIOS starts at
@ -160,8 +137,7 @@ doAutopartition( PartitionCoreModule* core, Device* dev, const QString& luksPass
PartitionTable::FlagNone
);
PartitionInfo::setFormat( efiPartition, true );
PartitionInfo::setMountPoint( efiPartition, gs->value( "efiSystemPartition" )
.toString() );
PartitionInfo::setMountPoint( efiPartition, o.efiPartitionMountPoint );
core->createPartition( dev, efiPartition, PartitionTable::FlagEsp );
firstFreeSector = lastSector + 1;
}
@ -170,20 +146,18 @@ doAutopartition( PartitionCoreModule* core, Device* dev, const QString& luksPass
core->createPartitionTable( dev, PartitionTable::msdos );
}
const bool mayCreateSwap = !gs->value( "neverCreateSwap" ).toBool();
const bool mayCreateSwap = ( o.swap == Choices::SmallSwap ) || ( o.swap == Choices::FullSwap );
bool shouldCreateSwap = false;
qint64 suggestedSwapSizeB = 0;
if ( mayCreateSwap )
{
qint64 availableSpaceB = ( dev->totalLogical() - firstFreeSector ) * dev->logicalSize();
suggestedSwapSizeB = swapSuggestion( availableSpaceB );
suggestedSwapSizeB = swapSuggestion( availableSpaceB, o.swap );
// 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.6 ) +
suggestedSwapSizeB;
qint64 requiredSpaceB = o.requiredSpaceB + 600_MiB + suggestedSwapSizeB;
// If there is enough room for ESP + root + swap, create swap, otherwise don't.
shouldCreateSwap = availableSpaceB > requiredSpaceB;
@ -196,7 +170,7 @@ doAutopartition( PartitionCoreModule* core, Device* dev, const QString& luksPass
}
Partition* rootPartition = nullptr;
if ( luksPassphrase.isEmpty() )
if ( o.luksPassphrase.isEmpty() )
{
rootPartition = KPMHelpers::createNewPartition(
dev->partitionTable(),
@ -217,7 +191,7 @@ doAutopartition( PartitionCoreModule* core, Device* dev, const QString& luksPass
FileSystem::typeForName( defaultFsType ),
firstFreeSector,
lastSectorForRoot,
luksPassphrase,
o.luksPassphrase,
PartitionTable::FlagNone
);
}
@ -232,7 +206,7 @@ doAutopartition( PartitionCoreModule* core, Device* dev, const QString& luksPass
if ( shouldCreateSwap )
{
Partition* swapPartition = nullptr;
if ( luksPassphrase.isEmpty() )
if ( o.luksPassphrase.isEmpty() )
{
swapPartition = KPMHelpers::createNewPartition(
dev->partitionTable(),
@ -253,7 +227,7 @@ doAutopartition( PartitionCoreModule* core, Device* dev, const QString& luksPass
FileSystem::LinuxSwap,
lastSectorForRoot + 1,
dev->totalLogical() - 1,
luksPassphrase,
o.luksPassphrase,
PartitionTable::FlagNone
);
}
@ -269,13 +243,11 @@ void
doReplacePartition( PartitionCoreModule* core,
Device* dev,
Partition* partition,
const QString& luksPassphrase )
Choices::ReplacePartitionOptions o )
{
cDebug() << "doReplacePartition for device" << partition->partitionPath();
QString defaultFsType = Calamares::JobQueue::instance()->
globalStorage()->
value( "defaultFileSystemType" ).toString();
QString defaultFsType = o.defaultFsType;
if ( FileSystem::typeForName( defaultFsType ) == FileSystem::Unknown )
defaultFsType = "ext4";
@ -296,7 +268,7 @@ doReplacePartition( PartitionCoreModule* core,
}
Partition* newPartition = nullptr;
if ( luksPassphrase.isEmpty() )
if ( o.luksPassphrase.isEmpty() )
{
newPartition = KPMHelpers::createNewPartition(
partition->parent(),
@ -317,7 +289,7 @@ doReplacePartition( PartitionCoreModule* core,
FileSystem::typeForName( defaultFsType ),
partition->firstSector(),
partition->lastSector(),
luksPassphrase,
o.luksPassphrase,
PartitionTable::FlagNone
);
}