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>
|
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 )
|
|
|
|
{
|
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
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
|
|
|
|
// .. 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
|
|
|
|
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
|
|
|
|
2018-08-08 11:16:46 +02:00
|
|
|
constexpr qint64
|
|
|
|
alignBytesToBlockSize( qint64 bytes, qint64 blocksize )
|
|
|
|
{
|
|
|
|
qint64 blocks = bytes / blocksize;
|
|
|
|
|
|
|
|
if ( blocks * blocksize != bytes )
|
|
|
|
++blocks;
|
|
|
|
return blocks * blocksize;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr qint64
|
|
|
|
bytesToSectors( qint64 bytes, qint64 blocksize )
|
|
|
|
{
|
|
|
|
return alignBytesToBlockSize( alignBytesToBlockSize( bytes, blocksize), MiBtoBytes(1) ) / blocksize;
|
|
|
|
}
|
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
|
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).
|
2018-09-13 11:33:39 +02:00
|
|
|
qint64 firstFreeSector = bytesToSectors( empty_space_sizeB, dev->logicalSize() );
|
2014-09-16 18:12:30 +02:00
|
|
|
|
|
|
|
if ( isEfi )
|
|
|
|
{
|
2018-09-13 11:33:39 +02:00
|
|
|
qint64 efiSectorCount = 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 );
|
2017-02-09 18:08:47 +01:00
|
|
|
PartitionInfo::setMountPoint( efiPartition, gs->value( "efiSystemPartition" )
|
2015-02-14 22:53:00 +01:00
|
|
|
.toString() );
|
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
|
|
|
}
|
|
|
|
|
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 );
|
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).
|
2017-02-09 18:08:47 +01:00
|
|
|
qint64 requiredSpaceB =
|
2018-08-08 11:58:41 +02:00
|
|
|
GiBtoBytes( gs->value( "requiredStorageGB" ).toDouble() + 0.6 ) +
|
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 );
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|