2020-08-25 16:05:56 +02:00
|
|
|
/* === This file is part of Calamares - <https://calamares.io> ===
|
2014-09-04 19:37:30 +02:00
|
|
|
*
|
2020-08-22 01:19:58 +02:00
|
|
|
* SPDX-FileCopyrightText: 2014-2017 Teo Mrnjavac <teo@kde.org>
|
|
|
|
* SPDX-FileCopyrightText: 2017-2019 Adriaan de Groot <groot@kde.org>
|
|
|
|
* SPDX-FileCopyrightText: 2019 Collabora Ltd <arnaud.ferraris@collabora.com>
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2014-09-04 19:37:30 +02:00
|
|
|
*
|
2020-08-25 16:05:56 +02:00
|
|
|
* Calamares is Free Software: see the License-Identifier above.
|
2014-09-04 19:37:30 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-10-29 17:33:15 +01:00
|
|
|
#include "PartitionActions.h"
|
2014-09-04 19:37:30 +02:00
|
|
|
|
2015-09-18 15:41:07 +02:00
|
|
|
#include "core/KPMHelpers.h"
|
2017-08-28 11:36:21 +02:00
|
|
|
#include "core/PartUtils.h"
|
2020-02-14 11:15:57 +01:00
|
|
|
#include "core/PartitionCoreModule.h"
|
|
|
|
#include "core/PartitionInfo.h"
|
2014-09-04 19:37:30 +02:00
|
|
|
|
2015-06-10 00:06:20 +02:00
|
|
|
#include "utils/CalamaresUtilsSystem.h"
|
2019-01-18 15:56:51 +01:00
|
|
|
#include "utils/NamedEnum.h"
|
2020-02-14 11:15:57 +01:00
|
|
|
#include "utils/Units.h"
|
2019-01-18 15:56:51 +01:00
|
|
|
|
2019-02-28 13:46:25 +01:00
|
|
|
#include "GlobalStorage.h"
|
2015-02-14 22:53:00 +01:00
|
|
|
#include "JobQueue.h"
|
2015-10-29 17:33:15 +01:00
|
|
|
#include "utils/Logger.h"
|
2014-09-04 19:37:30 +02:00
|
|
|
|
2015-07-02 13:49:21 +02:00
|
|
|
#include <kpmcore/core/device.h>
|
2015-11-18 16:02:51 +01:00
|
|
|
#include <kpmcore/core/partition.h>
|
2015-07-02 13:49:21 +02:00
|
|
|
|
2014-09-16 18:12:30 +02:00
|
|
|
#include <QDir>
|
2014-09-04 19:37:30 +02:00
|
|
|
|
2015-10-29 17:33:15 +01:00
|
|
|
namespace PartitionActions
|
2014-09-04 19:37:30 +02:00
|
|
|
{
|
2017-09-08 14:52:26 +02:00
|
|
|
using CalamaresUtils::operator""_GiB;
|
|
|
|
using CalamaresUtils::operator""_MiB;
|
2014-09-04 19:37:30 +02:00
|
|
|
|
2015-10-29 17:33:15 +01:00
|
|
|
qint64
|
2020-10-02 12:22:53 +02:00
|
|
|
swapSuggestion( const qint64 availableSpaceB, Config::SwapChoice swap )
|
2015-10-29 17:33:15 +01:00
|
|
|
{
|
2020-10-02 12:22:53 +02:00
|
|
|
if ( ( swap != Config::SwapChoice::SmallSwap ) && ( swap != Config::SwapChoice::FullSwap ) )
|
2020-02-14 11:15:57 +01:00
|
|
|
{
|
2018-09-17 12:42:14 +02:00
|
|
|
return 0;
|
2020-02-14 11:15:57 +01:00
|
|
|
}
|
2018-09-17 12:42:14 +02:00
|
|
|
|
2018-09-11 16:05:37 +02:00
|
|
|
// See partition.conf for explanation
|
2015-10-29 17:33:15 +01:00
|
|
|
qint64 suggestedSwapSizeB = 0;
|
2017-09-20 14:22:39 +02:00
|
|
|
auto memory = CalamaresUtils::System::instance()->getTotalMemoryB();
|
|
|
|
qint64 availableRamB = memory.first;
|
|
|
|
qreal overestimationFactor = memory.second;
|
2014-09-04 19:37:30 +02:00
|
|
|
|
2020-10-02 12:22:53 +02:00
|
|
|
bool ensureSuspendToDisk = swap == Config::SwapChoice::FullSwap;
|
2014-09-04 19:37:30 +02:00
|
|
|
|
2018-09-11 16:05:37 +02:00
|
|
|
// Ramp up quickly to 8GiB, then follow memory size
|
|
|
|
if ( availableRamB <= 4_GiB )
|
2020-02-14 11:15:57 +01:00
|
|
|
{
|
2018-09-11 16:05:37 +02:00
|
|
|
suggestedSwapSizeB = availableRamB * 2;
|
2020-02-14 11:15:57 +01:00
|
|
|
}
|
2018-09-11 16:05:37 +02:00
|
|
|
else if ( availableRamB <= 8_GiB )
|
2020-02-14 11:15:57 +01:00
|
|
|
{
|
2018-09-11 16:05:37 +02:00
|
|
|
suggestedSwapSizeB = 8_GiB;
|
2020-02-14 11:15:57 +01:00
|
|
|
}
|
2018-09-11 16:05:37 +02:00
|
|
|
else
|
2020-02-14 11:15:57 +01:00
|
|
|
{
|
2018-09-11 16:05:37 +02:00
|
|
|
suggestedSwapSizeB = availableRamB;
|
2020-02-14 11:15:57 +01:00
|
|
|
}
|
2014-09-04 19:37:30 +02:00
|
|
|
|
2018-09-11 16:05:37 +02:00
|
|
|
// .. top out at 8GiB if we don't care about suspend
|
|
|
|
if ( !ensureSuspendToDisk )
|
2020-02-14 11:15:57 +01:00
|
|
|
{
|
2018-09-11 16:05:37 +02:00
|
|
|
suggestedSwapSizeB = qMin( 8_GiB, suggestedSwapSizeB );
|
2020-02-14 11:15:57 +01:00
|
|
|
}
|
2014-09-04 19:37:30 +02:00
|
|
|
|
|
|
|
|
2018-09-11 16:05:37 +02:00
|
|
|
// Allow for a fudge factor
|
|
|
|
suggestedSwapSizeB *= overestimationFactor;
|
|
|
|
|
|
|
|
// don't use more than 10% of available space
|
|
|
|
if ( !ensureSuspendToDisk )
|
2020-02-14 11:15:57 +01:00
|
|
|
{
|
2018-09-11 16:05:37 +02:00
|
|
|
suggestedSwapSizeB = qMin( suggestedSwapSizeB, qint64( 0.10 * availableSpaceB ) );
|
2020-02-14 11:15:57 +01:00
|
|
|
}
|
2014-09-04 19:37:30 +02:00
|
|
|
|
2018-08-08 11:29:19 +02:00
|
|
|
cDebug() << "Suggested swap size:" << suggestedSwapSizeB / 1024. / 1024. / 1024. << "GiB";
|
2014-09-04 19:37:30 +02:00
|
|
|
|
2015-10-29 17:33:15 +01:00
|
|
|
return suggestedSwapSizeB;
|
2014-09-04 19:37:30 +02:00
|
|
|
}
|
2014-09-16 18:12:30 +02:00
|
|
|
|
|
|
|
void
|
2018-09-17 12:42:14 +02:00
|
|
|
doAutopartition( PartitionCoreModule* core, Device* dev, Choices::AutoPartitionOptions o )
|
2014-09-16 18:12:30 +02:00
|
|
|
{
|
2019-02-28 13:46:25 +01:00
|
|
|
Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
|
2016-06-10 15:22:21 +02:00
|
|
|
|
2018-09-17 12:42:14 +02:00
|
|
|
bool isEfi = PartUtils::isEfiSystem();
|
|
|
|
|
2014-09-16 18:12:30 +02:00
|
|
|
// Partition sizes are expressed in MiB, should be multiples of
|
2018-08-08 10:49:13 +02:00
|
|
|
// the logical sector size (usually 512B). EFI starts with 2MiB
|
2019-02-28 13:46:25 +01:00
|
|
|
// empty and a EFI boot partition, while BIOS starts at
|
2018-08-08 10:49:13 +02:00
|
|
|
// the 1MiB boundary (usually sector 2048).
|
2018-09-13 11:33:39 +02:00
|
|
|
int empty_space_sizeB = isEfi ? 2_MiB : 1_MiB;
|
2019-02-28 13:46:25 +01:00
|
|
|
int uefisys_part_sizeB = 0_MiB;
|
|
|
|
|
|
|
|
if ( isEfi )
|
|
|
|
{
|
|
|
|
if ( gs->contains( "efiSystemPartitionSize" ) )
|
2019-04-17 18:40:30 +02:00
|
|
|
{
|
2020-02-14 11:15:57 +01:00
|
|
|
CalamaresUtils::Partition::PartitionSize part_size
|
|
|
|
= CalamaresUtils::Partition::PartitionSize( gs->value( "efiSystemPartitionSize" ).toString() );
|
2019-04-17 18:40:30 +02:00
|
|
|
uefisys_part_sizeB = part_size.toBytes( dev->capacity() );
|
|
|
|
}
|
2019-02-28 13:46:25 +01:00
|
|
|
else
|
2019-04-17 18:40:30 +02:00
|
|
|
{
|
2019-02-28 13:46:25 +01:00
|
|
|
uefisys_part_sizeB = 300_MiB;
|
2019-04-17 18:40:30 +02:00
|
|
|
}
|
2019-02-28 13:46:25 +01:00
|
|
|
}
|
2014-09-16 18:12:30 +02:00
|
|
|
|
2018-08-08 11:16:46 +02:00
|
|
|
// 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).
|
2019-05-08 19:20:38 +02:00
|
|
|
qint64 firstFreeSector = CalamaresUtils::bytesToSectors( empty_space_sizeB, dev->logicalSize() );
|
2014-09-16 18:12:30 +02:00
|
|
|
|
|
|
|
if ( isEfi )
|
|
|
|
{
|
2019-05-08 19:20:38 +02:00
|
|
|
qint64 efiSectorCount = CalamaresUtils::bytesToSectors( uefisys_part_sizeB, dev->logicalSize() );
|
2018-08-08 11:16:46 +02:00
|
|
|
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;
|
2015-10-29 17:33:15 +01:00
|
|
|
core->createPartitionTable( dev, PartitionTable::gpt );
|
2020-02-14 11:15:57 +01:00
|
|
|
Partition* efiPartition = KPMHelpers::createNewPartition( dev->partitionTable(),
|
|
|
|
*dev,
|
|
|
|
PartitionRole( PartitionRole::Primary ),
|
|
|
|
FileSystem::Fat32,
|
|
|
|
firstFreeSector,
|
|
|
|
lastSector,
|
|
|
|
KPM_PARTITION_FLAG( None ) );
|
2015-06-02 18:55:51 +02:00
|
|
|
PartitionInfo::setFormat( efiPartition, true );
|
2018-09-17 12:42:14 +02:00
|
|
|
PartitionInfo::setMountPoint( efiPartition, o.efiPartitionMountPoint );
|
2020-04-22 00:11:16 +02:00
|
|
|
if ( gs->contains( "efiSystemPartitionName" ) )
|
|
|
|
{
|
|
|
|
efiPartition->setLabel( gs->value( "efiSystemPartitionName" ).toString() );
|
|
|
|
}
|
2019-03-20 16:37:50 +01:00
|
|
|
core->createPartition( dev, efiPartition, KPM_PARTITION_FLAG_ESP );
|
2015-06-10 00:06:20 +02:00
|
|
|
firstFreeSector = lastSector + 1;
|
2014-09-16 18:12:30 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-10-29 17:33:15 +01:00
|
|
|
core->createPartitionTable( dev, PartitionTable::msdos );
|
2014-09-16 18:12:30 +02:00
|
|
|
}
|
|
|
|
|
2020-10-02 12:22:53 +02:00
|
|
|
const bool mayCreateSwap
|
|
|
|
= ( o.swap == Config::SwapChoice::SmallSwap ) || ( o.swap == Config::SwapChoice::FullSwap );
|
2015-06-10 00:06:20 +02:00
|
|
|
bool shouldCreateSwap = false;
|
2017-02-09 18:08:47 +01:00
|
|
|
qint64 suggestedSwapSizeB = 0;
|
|
|
|
|
|
|
|
if ( mayCreateSwap )
|
|
|
|
{
|
|
|
|
qint64 availableSpaceB = ( dev->totalLogical() - firstFreeSector ) * dev->logicalSize();
|
2018-09-17 12:42:14 +02:00
|
|
|
suggestedSwapSizeB = swapSuggestion( availableSpaceB, o.swap );
|
2018-08-08 11:58:41 +02:00
|
|
|
// 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).
|
2018-09-17 12:42:14 +02:00
|
|
|
qint64 requiredSpaceB = o.requiredSpaceB + 600_MiB + suggestedSwapSizeB;
|
2017-02-09 18:08:47 +01:00
|
|
|
|
|
|
|
// If there is enough room for ESP + root + swap, create swap, otherwise don't.
|
|
|
|
shouldCreateSwap = availableSpaceB > requiredSpaceB;
|
|
|
|
}
|
2015-06-10 00:06:20 +02:00
|
|
|
|
2020-02-14 11:15:57 +01:00
|
|
|
qint64 lastSectorForRoot = dev->totalLogical() - 1; //last sector of the device
|
2015-06-10 00:06:20 +02:00
|
|
|
if ( shouldCreateSwap )
|
|
|
|
{
|
2016-08-31 05:30:45 +02:00
|
|
|
lastSectorForRoot -= suggestedSwapSizeB / dev->logicalSize() + 1;
|
2015-06-10 00:06:20 +02:00
|
|
|
}
|
|
|
|
|
2019-01-07 17:26:37 +01:00
|
|
|
core->layoutApply( dev, firstFreeSector, lastSectorForRoot, o.luksPassphrase );
|
2014-09-16 18:12:30 +02:00
|
|
|
|
2015-06-10 00:06:20 +02:00
|
|
|
if ( shouldCreateSwap )
|
|
|
|
{
|
2016-04-22 16:49:58 +02:00
|
|
|
Partition* swapPartition = nullptr;
|
2018-09-17 12:42:14 +02:00
|
|
|
if ( o.luksPassphrase.isEmpty() )
|
2016-04-22 16:49:58 +02:00
|
|
|
{
|
2020-02-14 11:15:57 +01:00
|
|
|
swapPartition = KPMHelpers::createNewPartition( dev->partitionTable(),
|
|
|
|
*dev,
|
|
|
|
PartitionRole( PartitionRole::Primary ),
|
|
|
|
FileSystem::LinuxSwap,
|
|
|
|
lastSectorForRoot + 1,
|
|
|
|
dev->totalLogical() - 1,
|
|
|
|
KPM_PARTITION_FLAG( None ) );
|
2016-04-22 16:49:58 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-14 11:15:57 +01:00
|
|
|
swapPartition = KPMHelpers::createNewEncryptedPartition( dev->partitionTable(),
|
|
|
|
*dev,
|
|
|
|
PartitionRole( PartitionRole::Primary ),
|
|
|
|
FileSystem::LinuxSwap,
|
|
|
|
lastSectorForRoot + 1,
|
|
|
|
dev->totalLogical() - 1,
|
|
|
|
o.luksPassphrase,
|
|
|
|
KPM_PARTITION_FLAG( None ) );
|
2016-04-22 16:49:58 +02:00
|
|
|
}
|
2015-06-10 00:06:20 +02:00
|
|
|
PartitionInfo::setFormat( swapPartition, true );
|
2015-10-29 17:33:15 +01:00
|
|
|
core->createPartition( dev, swapPartition );
|
2015-06-10 00:06:20 +02:00
|
|
|
}
|
|
|
|
|
2015-10-29 17:33:15 +01:00
|
|
|
core->dumpQueue();
|
2015-03-13 15:55:06 +01:00
|
|
|
}
|
|
|
|
|
2015-11-18 16:02:51 +01:00
|
|
|
|
|
|
|
void
|
2020-02-14 11:15:57 +01:00
|
|
|
doReplacePartition( PartitionCoreModule* core, Device* dev, Partition* partition, Choices::ReplacePartitionOptions o )
|
2015-11-18 16:02:51 +01:00
|
|
|
{
|
2019-01-07 17:26:37 +01:00
|
|
|
qint64 firstSector, lastSector;
|
|
|
|
|
2015-12-18 15:07:22 +01:00
|
|
|
cDebug() << "doReplacePartition for device" << partition->partitionPath();
|
2016-03-10 12:33:19 +01:00
|
|
|
|
2015-12-31 17:33:13 +01:00
|
|
|
PartitionRole newRoles( partition->roles() );
|
|
|
|
if ( partition->roles().has( PartitionRole::Extended ) )
|
2020-02-14 11:15:57 +01:00
|
|
|
{
|
2015-12-31 17:33:13 +01:00
|
|
|
newRoles = PartitionRole( PartitionRole::Primary );
|
2020-02-14 11:15:57 +01:00
|
|
|
}
|
2015-12-31 17:33:13 +01:00
|
|
|
|
2016-03-10 12:33:19 +01:00
|
|
|
if ( partition->roles().has( PartitionRole::Unallocated ) )
|
|
|
|
{
|
|
|
|
newRoles = PartitionRole( PartitionRole::Primary );
|
2018-02-13 11:07:12 +01:00
|
|
|
cWarning() << "selected partition is free space";
|
2016-03-10 12:33:19 +01:00
|
|
|
if ( partition->parent() )
|
|
|
|
{
|
|
|
|
Partition* parent = dynamic_cast< Partition* >( partition->parent() );
|
|
|
|
if ( parent && parent->roles().has( PartitionRole::Extended ) )
|
2020-02-14 11:15:57 +01:00
|
|
|
{
|
2016-03-10 12:33:19 +01:00
|
|
|
newRoles = PartitionRole( PartitionRole::Logical );
|
2020-02-14 11:15:57 +01:00
|
|
|
}
|
2016-03-10 12:33:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-07 17:26:37 +01:00
|
|
|
// Save the first and last sector values as the partition will be deleted
|
|
|
|
firstSector = partition->firstSector();
|
|
|
|
lastSector = partition->lastSector();
|
2016-03-10 12:33:19 +01:00
|
|
|
if ( !partition->roles().has( PartitionRole::Unallocated ) )
|
2020-02-14 11:15:57 +01:00
|
|
|
{
|
2016-03-10 12:33:19 +01:00
|
|
|
core->deletePartition( dev, partition );
|
2020-02-14 11:15:57 +01:00
|
|
|
}
|
2019-01-07 17:26:37 +01:00
|
|
|
|
|
|
|
core->layoutApply( dev, firstSector, lastSector, o.luksPassphrase );
|
2015-11-18 16:02:51 +01:00
|
|
|
|
|
|
|
core->dumpQueue();
|
|
|
|
}
|
|
|
|
|
2018-12-04 11:15:38 +01:00
|
|
|
} // namespace PartitionActions
|