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>
|
2017-06-12 15:59:04 +02:00
|
|
|
* Copyright 2017, Adriaan de Groot <groot@kde.org>
|
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"
|
2015-02-14 22:53:00 +01:00
|
|
|
#include "JobQueue.h"
|
2015-10-29 17:33:15 +01:00
|
|
|
#include "utils/Logger.h"
|
|
|
|
#include "GlobalStorage.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::GiBtoBytes;
|
|
|
|
using CalamaresUtils::MiBtoBytes;
|
|
|
|
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
|
|
|
|
swapSuggestion( const qint64 availableSpaceB )
|
|
|
|
{
|
2017-09-20 14:22:39 +02:00
|
|
|
/* 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).
|
|
|
|
*/
|
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
|
|
|
|
2015-10-29 17:33:15 +01:00
|
|
|
bool ensureSuspendToDisk =
|
|
|
|
Calamares::JobQueue::instance()->globalStorage()->
|
|
|
|
value( "ensureSuspendToDisk" ).toBool();
|
2014-09-04 19:37:30 +02:00
|
|
|
|
2015-10-29 17:33:15 +01:00
|
|
|
if ( ensureSuspendToDisk )
|
2014-09-04 19:37:30 +02:00
|
|
|
{
|
2017-06-12 10:51:55 +02:00
|
|
|
if ( availableRamB < 4_GiB )
|
|
|
|
suggestedSwapSizeB = qMax( 2_GiB, availableRamB * 2 );
|
|
|
|
else if ( availableRamB >= 4_GiB && availableRamB < 8_GiB )
|
|
|
|
suggestedSwapSizeB = 8_GiB;
|
2015-10-29 17:33:15 +01:00
|
|
|
else
|
|
|
|
suggestedSwapSizeB = availableRamB;
|
2014-09-04 19:37:30 +02:00
|
|
|
|
2015-10-29 17:33:15 +01:00
|
|
|
suggestedSwapSizeB *= overestimationFactor;
|
|
|
|
}
|
|
|
|
else //if we don't care about suspend to disk
|
|
|
|
{
|
2017-06-12 10:51:55 +02:00
|
|
|
if ( availableRamB < 2_GiB )
|
|
|
|
suggestedSwapSizeB = qMax( 2_GiB, availableRamB * 2 );
|
|
|
|
else if ( availableRamB >= 2_GiB && availableRamB < 8_GiB )
|
2015-10-29 17:33:15 +01:00
|
|
|
suggestedSwapSizeB = availableRamB;
|
2017-09-20 14:22:39 +02:00
|
|
|
else if ( availableRamB >= 8_GiB && availableRamB < 16_GiB )
|
|
|
|
suggestedSwapSizeB = 8_GiB;
|
2015-10-29 17:33:15 +01:00
|
|
|
else
|
2017-06-12 10:51:55 +02:00
|
|
|
suggestedSwapSizeB = 4_GiB;
|
2014-09-04 19:37:30 +02:00
|
|
|
|
2015-10-29 17:33:15 +01:00
|
|
|
suggestedSwapSizeB *= overestimationFactor;
|
2014-09-04 19:37:30 +02:00
|
|
|
|
2015-10-29 17:33:15 +01:00
|
|
|
// don't use more than 10% of available space
|
|
|
|
qreal maxSwapDiskRatio = 1.10;
|
|
|
|
qint64 maxSwapSizeB = availableSpaceB * maxSwapDiskRatio;
|
|
|
|
if ( suggestedSwapSizeB > maxSwapSizeB )
|
|
|
|
suggestedSwapSizeB = maxSwapSizeB;
|
|
|
|
}
|
2014-09-04 19:37:30 +02:00
|
|
|
|
2015-10-29 17:33:15 +01: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
|
2016-04-22 16:49:58 +02:00
|
|
|
doAutopartition( PartitionCoreModule* core, Device* dev, const QString& luksPassphrase )
|
2014-09-16 18:12:30 +02:00
|
|
|
{
|
2017-02-09 18:08:47 +01:00
|
|
|
Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
|
|
|
|
|
2017-08-28 11:36:21 +02:00
|
|
|
bool isEfi = PartUtils::isEfiSystem();
|
2014-09-16 18:12:30 +02:00
|
|
|
|
2017-02-09 18:08:47 +01:00
|
|
|
QString defaultFsType = gs->value( "defaultFileSystemType" ).toString();
|
2016-06-10 15:22:21 +02:00
|
|
|
if ( FileSystem::typeForName( defaultFsType ) == FileSystem::Unknown )
|
|
|
|
defaultFsType = "ext4";
|
|
|
|
|
2014-09-16 18:12:30 +02:00
|
|
|
// 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 )
|
|
|
|
{
|
2015-05-29 18:31:52 +02:00
|
|
|
uefisys_part_size = 300;
|
2014-09-16 18:12:30 +02:00
|
|
|
empty_space_size = 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// we start with a 1MiB offset before the first partition
|
|
|
|
empty_space_size = 1;
|
|
|
|
}
|
|
|
|
|
2017-09-08 14:52:26 +02:00
|
|
|
qint64 firstFreeSector = MiBtoBytes(empty_space_size) / dev->logicalSize() + 1;
|
2014-09-16 18:12:30 +02:00
|
|
|
|
|
|
|
if ( isEfi )
|
|
|
|
{
|
2017-09-08 14:52:26 +02:00
|
|
|
qint64 lastSector = firstFreeSector + ( MiBtoBytes(uefisys_part_size) / dev->logicalSize() );
|
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,
|
|
|
|
PartitionTable::FlagEsp
|
2014-09-16 18:12:30 +02:00
|
|
|
);
|
2015-06-02 18:55:51 +02:00
|
|
|
PartitionInfo::setFormat( efiPartition, true );
|
2017-02-09 18:08:47 +01:00
|
|
|
PartitionInfo::setMountPoint( efiPartition, gs->value( "efiSystemPartition" )
|
2015-02-14 22:53:00 +01:00
|
|
|
.toString() );
|
2017-01-12 16:52:22 +01:00
|
|
|
core->createPartition( dev, efiPartition, PartitionTable::FlagEsp | PartitionTable::FlagBoot );
|
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
|
|
|
}
|
|
|
|
|
2017-02-09 18:08:47 +01:00
|
|
|
const bool mayCreateSwap = !gs->value( "neverCreateSwap" ).toBool();
|
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();
|
|
|
|
suggestedSwapSizeB = swapSuggestion( availableSpaceB );
|
|
|
|
qint64 requiredSpaceB =
|
2017-09-08 14:52:26 +02:00
|
|
|
GiBtoBytes( gs->value( "requiredStorageGB" ).toDouble() + 0.1 + 2.0 ) +
|
2017-02-09 18:08:47 +01:00
|
|
|
suggestedSwapSizeB;
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2016-04-22 16:49:58 +02:00
|
|
|
Partition* rootPartition = nullptr;
|
|
|
|
if ( luksPassphrase.isEmpty() )
|
|
|
|
{
|
|
|
|
rootPartition = KPMHelpers::createNewPartition(
|
|
|
|
dev->partitionTable(),
|
|
|
|
*dev,
|
|
|
|
PartitionRole( PartitionRole::Primary ),
|
2016-06-10 15:22:21 +02:00
|
|
|
FileSystem::typeForName( defaultFsType ),
|
2016-04-22 16:49:58 +02:00
|
|
|
firstFreeSector,
|
|
|
|
lastSectorForRoot
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rootPartition = KPMHelpers::createNewEncryptedPartition(
|
|
|
|
dev->partitionTable(),
|
|
|
|
*dev,
|
|
|
|
PartitionRole( PartitionRole::Primary ),
|
2016-06-10 15:22:21 +02:00
|
|
|
FileSystem::typeForName( defaultFsType ),
|
2016-04-22 16:49:58 +02:00
|
|
|
firstFreeSector,
|
|
|
|
lastSectorForRoot,
|
|
|
|
luksPassphrase
|
|
|
|
);
|
|
|
|
}
|
2015-06-02 18:55:51 +02:00
|
|
|
PartitionInfo::setFormat( rootPartition, true );
|
|
|
|
PartitionInfo::setMountPoint( rootPartition, "/" );
|
2015-10-29 17:33:15 +01:00
|
|
|
core->createPartition( dev, rootPartition );
|
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;
|
|
|
|
if ( luksPassphrase.isEmpty() )
|
|
|
|
{
|
|
|
|
swapPartition = KPMHelpers::createNewPartition(
|
|
|
|
dev->partitionTable(),
|
|
|
|
*dev,
|
|
|
|
PartitionRole( PartitionRole::Primary ),
|
|
|
|
FileSystem::LinuxSwap,
|
|
|
|
lastSectorForRoot + 1,
|
2016-08-31 05:30:45 +02:00
|
|
|
dev->totalLogical() - 1
|
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,
|
2016-04-22 16:49:58 +02:00
|
|
|
luksPassphrase
|
|
|
|
);
|
|
|
|
}
|
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,
|
|
|
|
const QString& luksPassphrase )
|
2015-11-18 16:02:51 +01:00
|
|
|
{
|
2015-12-18 15:07:22 +01:00
|
|
|
cDebug() << "doReplacePartition for device" << partition->partitionPath();
|
2016-03-10 12:33:19 +01:00
|
|
|
|
2016-06-10 15:22:21 +02:00
|
|
|
QString defaultFsType = Calamares::JobQueue::instance()->
|
|
|
|
globalStorage()->
|
|
|
|
value( "defaultFileSystemType" ).toString();
|
|
|
|
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 );
|
|
|
|
cDebug() << "WARNING: selected partition is free space";
|
|
|
|
if ( partition->parent() )
|
|
|
|
{
|
|
|
|
Partition* parent = dynamic_cast< Partition* >( partition->parent() );
|
|
|
|
if ( parent && parent->roles().has( PartitionRole::Extended ) )
|
|
|
|
newRoles = PartitionRole( PartitionRole::Logical );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 16:39:28 +02:00
|
|
|
Partition* newPartition = nullptr;
|
|
|
|
if ( luksPassphrase.isEmpty() )
|
|
|
|
{
|
|
|
|
newPartition = KPMHelpers::createNewPartition(
|
|
|
|
partition->parent(),
|
|
|
|
*dev,
|
|
|
|
newRoles,
|
2016-06-10 15:22:21 +02:00
|
|
|
FileSystem::typeForName( defaultFsType ),
|
2016-06-03 16:39:28 +02:00
|
|
|
partition->firstSector(),
|
|
|
|
partition->lastSector()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newPartition = KPMHelpers::createNewEncryptedPartition(
|
|
|
|
partition->parent(),
|
|
|
|
*dev,
|
|
|
|
newRoles,
|
2016-06-10 15:22:21 +02:00
|
|
|
FileSystem::typeForName( defaultFsType ),
|
2016-06-03 16:39:28 +02:00
|
|
|
partition->firstSector(),
|
|
|
|
partition->lastSector(),
|
|
|
|
luksPassphrase
|
|
|
|
);
|
|
|
|
}
|
2015-12-23 16:00:20 +01:00
|
|
|
PartitionInfo::setMountPoint( newPartition, "/" );
|
|
|
|
PartitionInfo::setFormat( newPartition, true );
|
|
|
|
|
2016-03-10 12:33:19 +01:00
|
|
|
if ( !partition->roles().has( PartitionRole::Unallocated ) )
|
|
|
|
core->deletePartition( dev, partition );
|
2015-12-23 16:00:20 +01:00
|
|
|
core->createPartition( dev, newPartition );
|
2015-11-18 16:02:51 +01:00
|
|
|
|
|
|
|
core->dumpQueue();
|
|
|
|
}
|
|
|
|
|
2015-06-25 14:10:56 +02:00
|
|
|
}
|