2017-12-20 14:39:09 +01:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
2014-09-04 19:37:30 +02:00
|
|
|
*
|
2017-01-12 16:52:22 +01:00
|
|
|
* Copyright 2014-2017, Teo Mrnjavac <teo@kde.org>
|
2018-06-15 11:59:11 +02:00
|
|
|
* Copyright 2017-2018, Adriaan de Groot <groot@kde.org>
|
2019-02-28 13:36:29 +01:00
|
|
|
* Copyright 2019, Collabora Ltd <arnaud.ferraris@collabora.com>
|
2014-09-04 19:37:30 +02:00
|
|
|
*
|
|
|
|
* Calamares is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Calamares is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
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"
|
2014-09-16 18:12:30 +02:00
|
|
|
#include "core/PartitionInfo.h"
|
2015-10-29 17:33:15 +01:00
|
|
|
#include "core/PartitionCoreModule.h"
|
2017-08-28 11:36:21 +02:00
|
|
|
#include "core/PartUtils.h"
|
2014-09-04 19:37:30 +02:00
|
|
|
|
2015-06-10 00:06:20 +02:00
|
|
|
#include "utils/CalamaresUtilsSystem.h"
|
2017-09-08 14:52:26 +02:00
|
|
|
#include "utils/Units.h"
|
2019-01-18 15:56:51 +01:00
|
|
|
#include "utils/NamedEnum.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
|
2018-09-17 12:42:14 +02:00
|
|
|
swapSuggestion( const qint64 availableSpaceB, Choices::SwapChoice swap )
|
2015-10-29 17:33:15 +01:00
|
|
|
{
|
2018-09-17 12:42:14 +02:00
|
|
|
if ( ( swap != Choices::SmallSwap ) && ( swap != Choices::FullSwap ) )
|
|
|
|
return 0;
|
|
|
|
|
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
|
|
|
|
2018-09-17 12:42:14 +02:00
|
|
|
bool ensureSuspendToDisk = swap == Choices::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 )
|
|
|
|
suggestedSwapSizeB = availableRamB * 2;
|
|
|
|
else if ( availableRamB <= 8_GiB )
|
|
|
|
suggestedSwapSizeB = 8_GiB;
|
|
|
|
else
|
|
|
|
suggestedSwapSizeB = availableRamB;
|
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 )
|
|
|
|
suggestedSwapSizeB = qMin( 8_GiB, suggestedSwapSizeB );
|
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 )
|
|
|
|
suggestedSwapSizeB = qMin( suggestedSwapSizeB, qint64( 0.10 * availableSpaceB ) );
|
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
|
|
|
{
|
2018-09-17 12:42:14 +02:00
|
|
|
QString defaultFsType = o.defaultFsType;
|
2016-06-10 15:22:21 +02:00
|
|
|
if ( FileSystem::typeForName( defaultFsType ) == FileSystem::Unknown )
|
|
|
|
defaultFsType = "ext4";
|
|
|
|
|
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
|
|
|
|
// empty and a 300MiB EFI boot partition, while BIOS starts at
|
|
|
|
// the 1MiB boundary (usually sector 2048).
|
2018-09-13 11:33:39 +02:00
|
|
|
int uefisys_part_sizeB = isEfi ? 300_MiB : 0_MiB;
|
|
|
|
int empty_space_sizeB = isEfi ? 2_MiB : 1_MiB;
|
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-02-28 13:36:29 +01:00
|
|
|
qint64 firstFreeSector = PartUtils::bytesToSectors( empty_space_sizeB, dev->logicalSize() );
|
2014-09-16 18:12:30 +02:00
|
|
|
|
|
|
|
if ( isEfi )
|
|
|
|
{
|
2019-02-28 13:36:29 +01:00
|
|
|
qint64 efiSectorCount = PartUtils::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 );
|
2015-09-18 15:41:07 +02:00
|
|
|
Partition* efiPartition = KPMHelpers::createNewPartition(
|
2014-09-16 18:12:30 +02:00
|
|
|
dev->partitionTable(),
|
|
|
|
*dev,
|
|
|
|
PartitionRole( PartitionRole::Primary ),
|
|
|
|
FileSystem::Fat32,
|
2015-06-10 00:06:20 +02:00
|
|
|
firstFreeSector,
|
2016-10-29 00:52:38 +02:00
|
|
|
lastSector,
|
2018-08-07 13:34:38 +02:00
|
|
|
PartitionTable::FlagNone
|
2014-09-16 18:12:30 +02:00
|
|
|
);
|
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 );
|
2018-08-22 10:14:49 +02:00
|
|
|
core->createPartition( dev, efiPartition, PartitionTable::FlagEsp );
|
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
|
|
|
}
|
|
|
|
|
2018-09-17 12:42:14 +02:00
|
|
|
const bool mayCreateSwap = ( o.swap == Choices::SmallSwap ) || ( o.swap == Choices::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
|
|
|
|
2016-08-31 05:30:45 +02: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
|
|
|
{
|
|
|
|
swapPartition = KPMHelpers::createNewPartition(
|
|
|
|
dev->partitionTable(),
|
|
|
|
*dev,
|
|
|
|
PartitionRole( PartitionRole::Primary ),
|
|
|
|
FileSystem::LinuxSwap,
|
|
|
|
lastSectorForRoot + 1,
|
2018-11-11 13:55:46 +01:00
|
|
|
dev->totalLogical() - 1,
|
|
|
|
PartitionTable::FlagNone
|
2016-04-22 16:49:58 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
swapPartition = KPMHelpers::createNewEncryptedPartition(
|
|
|
|
dev->partitionTable(),
|
|
|
|
*dev,
|
|
|
|
PartitionRole( PartitionRole::Primary ),
|
|
|
|
FileSystem::LinuxSwap,
|
|
|
|
lastSectorForRoot + 1,
|
2016-08-31 05:30:45 +02:00
|
|
|
dev->totalLogical() - 1,
|
2018-12-03 21:02:10 +01:00
|
|
|
o.luksPassphrase,
|
2018-11-11 13:55:46 +01:00
|
|
|
PartitionTable::FlagNone
|
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
|
2016-06-03 16:39:28 +02:00
|
|
|
doReplacePartition( PartitionCoreModule* core,
|
|
|
|
Device* dev,
|
|
|
|
Partition* partition,
|
2018-09-17 12:42:14 +02:00
|
|
|
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
|
|
|
|
2018-09-17 12:42:14 +02:00
|
|
|
QString defaultFsType = o.defaultFsType;
|
2016-06-10 15:22:21 +02:00
|
|
|
if ( FileSystem::typeForName( defaultFsType ) == FileSystem::Unknown )
|
|
|
|
defaultFsType = "ext4";
|
|
|
|
|
2015-12-31 17:33:13 +01:00
|
|
|
PartitionRole newRoles( partition->roles() );
|
|
|
|
if ( partition->roles().has( PartitionRole::Extended ) )
|
|
|
|
newRoles = PartitionRole( PartitionRole::Primary );
|
|
|
|
|
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 ) )
|
|
|
|
newRoles = PartitionRole( PartitionRole::Logical );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 ) )
|
|
|
|
core->deletePartition( dev, partition );
|
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 Choices
|
|
|
|
{
|
2019-01-18 15:56:51 +01:00
|
|
|
static const NamedEnumTable<SwapChoice>&
|
|
|
|
nameTable()
|
|
|
|
{
|
|
|
|
static const NamedEnumTable<SwapChoice> names{
|
|
|
|
{ QStringLiteral( "none" ), SwapChoice::NoSwap },
|
|
|
|
{ QStringLiteral( "small" ), SwapChoice::SmallSwap },
|
|
|
|
{ QStringLiteral( "suspend" ), SwapChoice::FullSwap },
|
|
|
|
{ QStringLiteral( "reuse" ), SwapChoice::ReuseSwap },
|
|
|
|
{ QStringLiteral( "file" ), SwapChoice::SwapFile }
|
|
|
|
};
|
|
|
|
|
|
|
|
return names;
|
|
|
|
}
|
2018-12-04 11:15:38 +01:00
|
|
|
|
|
|
|
SwapChoice
|
|
|
|
nameToChoice( QString name, bool& ok )
|
|
|
|
{
|
2019-01-18 15:56:51 +01:00
|
|
|
return nameTable().find( name, ok );
|
2015-06-25 14:10:56 +02:00
|
|
|
}
|
2018-12-04 11:15:38 +01:00
|
|
|
|
|
|
|
QString
|
|
|
|
choiceToName( SwapChoice c )
|
|
|
|
{
|
2019-01-18 15:56:51 +01:00
|
|
|
bool ok = false;
|
|
|
|
return nameTable().find( c, ok );
|
2018-12-04 11:15:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Choices
|
|
|
|
|
|
|
|
} // namespace PartitionActions
|