2020-08-25 16:05:56 +02:00
|
|
|
/* === This file is part of Calamares - <https://calamares.io> ===
|
2019-01-07 17:25:22 +01:00
|
|
|
*
|
2020-08-22 01:19:58 +02:00
|
|
|
* SPDX-FileCopyrightText: 2014-2017 Teo Mrnjavac <teo@kde.org>
|
|
|
|
* SPDX-FileCopyrightText: 2017-2018 Adriaan de Groot <groot@kde.org>
|
|
|
|
* SPDX-FileCopyrightText: 2018-2019 Collabora Ltd <arnaud.ferraris@collabora.com>
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2019-01-07 17:25:22 +01:00
|
|
|
*
|
2020-08-25 16:05:56 +02:00
|
|
|
* Calamares is Free Software: see the License-Identifier above.
|
2019-01-07 17:25:22 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-02-22 12:58:55 +01:00
|
|
|
#include "GlobalStorage.h"
|
|
|
|
#include "JobQueue.h"
|
|
|
|
|
2019-04-17 19:16:48 +02:00
|
|
|
#include "utils/Logger.h"
|
|
|
|
|
2019-01-07 17:25:22 +01:00
|
|
|
#include "core/PartitionLayout.h"
|
|
|
|
|
2019-01-07 17:26:37 +01:00
|
|
|
#include "core/KPMHelpers.h"
|
2020-02-14 11:15:57 +01:00
|
|
|
#include "core/PartUtils.h"
|
2019-01-07 17:26:37 +01:00
|
|
|
#include "core/PartitionActions.h"
|
|
|
|
#include "core/PartitionInfo.h"
|
|
|
|
|
2020-06-20 05:06:38 +02:00
|
|
|
#include "utils/Variant.h"
|
|
|
|
|
2019-01-07 17:26:37 +01:00
|
|
|
#include <kpmcore/core/device.h>
|
|
|
|
#include <kpmcore/core/partition.h>
|
2019-01-07 17:25:22 +01:00
|
|
|
#include <kpmcore/fs/filesystem.h>
|
|
|
|
|
2019-02-22 18:42:16 +01:00
|
|
|
static FileSystem::Type
|
2019-02-22 12:58:55 +01:00
|
|
|
getDefaultFileSystemType()
|
|
|
|
{
|
|
|
|
Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
|
2019-02-22 18:42:16 +01:00
|
|
|
FileSystem::Type defaultFS = FileSystem::Ext4;
|
2019-02-22 12:58:55 +01:00
|
|
|
|
2019-02-22 18:42:16 +01:00
|
|
|
if ( gs->contains( "defaultFileSystemType" ) )
|
|
|
|
{
|
2020-02-14 11:15:57 +01:00
|
|
|
PartUtils::findFS( gs->value( "defaultFileSystemType" ).toString(), &defaultFS );
|
2019-02-22 18:42:16 +01:00
|
|
|
if ( defaultFS == FileSystem::Unknown )
|
2020-02-14 11:15:57 +01:00
|
|
|
{
|
2019-02-22 18:42:16 +01:00
|
|
|
defaultFS = FileSystem::Ext4;
|
2020-02-14 11:15:57 +01:00
|
|
|
}
|
2019-02-22 18:42:16 +01:00
|
|
|
}
|
2019-02-22 12:58:55 +01:00
|
|
|
|
2019-02-22 18:42:16 +01:00
|
|
|
return defaultFS;
|
2019-02-22 12:58:55 +01:00
|
|
|
}
|
|
|
|
|
2019-01-07 17:25:22 +01:00
|
|
|
PartitionLayout::PartitionLayout()
|
2020-10-23 22:27:30 +02:00
|
|
|
: m_defaultFsType( getDefaultFileSystemType() )
|
2019-01-07 17:25:22 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PartitionLayout::PartitionLayout( const PartitionLayout& layout )
|
2019-04-15 16:06:21 +02:00
|
|
|
: m_defaultFsType( layout.m_defaultFsType )
|
|
|
|
, m_partLayout( layout.m_partLayout )
|
2019-01-07 17:25:22 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-14 11:15:57 +01:00
|
|
|
PartitionLayout::~PartitionLayout() {}
|
2019-01-07 17:25:22 +01:00
|
|
|
|
2020-06-21 02:30:22 +02:00
|
|
|
PartitionLayout::PartitionEntry::PartitionEntry()
|
|
|
|
: partAttributes( 0 )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-22 00:02:04 +02:00
|
|
|
PartitionLayout::PartitionEntry::PartitionEntry( const QString& mountPoint, const QString& size, const QString& minSize, const QString& maxSize )
|
2020-10-23 22:31:22 +02:00
|
|
|
: partAttributes( 0 )
|
2020-06-22 00:02:04 +02:00
|
|
|
, partMountPoint( mountPoint )
|
2020-10-23 22:31:22 +02:00
|
|
|
, partSize( size )
|
2020-06-22 00:02:04 +02:00
|
|
|
, partMinSize( minSize )
|
|
|
|
, partMaxSize( maxSize )
|
2019-02-11 23:37:14 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-22 00:02:04 +02:00
|
|
|
PartitionLayout::PartitionEntry::PartitionEntry( const QString& label,
|
|
|
|
const QString& uuid,
|
|
|
|
const QString& type,
|
|
|
|
quint64 attributes,
|
|
|
|
const QString& mountPoint,
|
|
|
|
const QString& fs,
|
|
|
|
const QVariantMap& features,
|
|
|
|
const QString& size,
|
|
|
|
const QString& minSize,
|
|
|
|
const QString& maxSize )
|
|
|
|
: partLabel( label )
|
|
|
|
, partUUID( uuid )
|
|
|
|
, partType( type )
|
|
|
|
, partAttributes( attributes )
|
|
|
|
, partMountPoint( mountPoint )
|
|
|
|
, partFeatures( features )
|
|
|
|
, partSize( size )
|
|
|
|
, partMinSize( minSize )
|
|
|
|
, partMaxSize( maxSize )
|
2019-01-07 17:25:22 +01:00
|
|
|
{
|
2020-06-22 00:02:04 +02:00
|
|
|
PartUtils::findFS( fs, &partFileSystem );
|
|
|
|
}
|
2019-04-17 19:16:48 +02:00
|
|
|
|
2020-06-22 00:02:04 +02:00
|
|
|
PartitionLayout::PartitionEntry::PartitionEntry( const PartitionEntry& e )
|
|
|
|
: partLabel( e.partLabel )
|
|
|
|
, partUUID( e.partUUID )
|
|
|
|
, partType( e.partType )
|
|
|
|
, partAttributes( e.partAttributes )
|
|
|
|
, partMountPoint( e.partMountPoint )
|
|
|
|
, partFileSystem( e.partFileSystem )
|
|
|
|
, partFeatures( e.partFeatures )
|
|
|
|
, partSize( e.partSize )
|
|
|
|
, partMinSize( e.partMinSize )
|
|
|
|
, partMaxSize( e.partMaxSize )
|
|
|
|
{
|
2019-01-07 17:25:22 +01:00
|
|
|
}
|
|
|
|
|
2020-06-22 00:02:04 +02:00
|
|
|
|
2019-04-17 19:16:48 +02:00
|
|
|
bool
|
2020-06-22 00:02:04 +02:00
|
|
|
PartitionLayout::addEntry( const PartitionEntry& entry )
|
2019-01-07 17:25:22 +01:00
|
|
|
{
|
2019-04-17 19:16:48 +02:00
|
|
|
if ( !entry.isValid() )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-07 17:25:22 +01:00
|
|
|
|
2019-02-22 13:08:59 +01:00
|
|
|
m_partLayout.append( entry );
|
2019-04-17 19:16:48 +02:00
|
|
|
|
|
|
|
return true;
|
2019-01-07 17:25:22 +01:00
|
|
|
}
|
2019-01-07 17:26:37 +01:00
|
|
|
|
2020-06-20 05:06:38 +02:00
|
|
|
void
|
|
|
|
PartitionLayout::init( const QVariantList& config )
|
|
|
|
{
|
|
|
|
bool ok;
|
|
|
|
|
|
|
|
m_partLayout.clear();
|
|
|
|
|
|
|
|
for ( const auto& r : config )
|
|
|
|
{
|
|
|
|
QVariantMap pentry = r.toMap();
|
|
|
|
|
|
|
|
if ( !pentry.contains( "name" ) || !pentry.contains( "mountPoint" ) || !pentry.contains( "filesystem" )
|
|
|
|
|| !pentry.contains( "size" ) )
|
|
|
|
{
|
|
|
|
cError() << "Partition layout entry #" << config.indexOf( r )
|
|
|
|
<< "lacks mandatory attributes, switching to default layout.";
|
|
|
|
m_partLayout.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-06-22 00:02:04 +02:00
|
|
|
if ( !addEntry( { CalamaresUtils::getString( pentry, "name" ),
|
|
|
|
CalamaresUtils::getString( pentry, "uuid" ),
|
|
|
|
CalamaresUtils::getString( pentry, "type" ),
|
|
|
|
CalamaresUtils::getUnsignedInteger( pentry, "attributes", 0 ),
|
|
|
|
CalamaresUtils::getString( pentry, "mountPoint" ),
|
|
|
|
CalamaresUtils::getString( pentry, "filesystem" ),
|
|
|
|
CalamaresUtils::getSubMap( pentry, "features", ok ),
|
|
|
|
CalamaresUtils::getString( pentry, "size", QStringLiteral( "0" ) ),
|
|
|
|
CalamaresUtils::getString( pentry, "minSize", QStringLiteral( "0" ) ),
|
|
|
|
CalamaresUtils::getString( pentry, "maxSize", QStringLiteral( "0" ) ) } ) )
|
2020-06-20 05:06:38 +02:00
|
|
|
{
|
|
|
|
cError() << "Partition layout entry #" << config.indexOf( r ) << "is invalid, switching to default layout.";
|
|
|
|
m_partLayout.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !m_partLayout.count() )
|
|
|
|
{
|
2020-06-22 00:02:04 +02:00
|
|
|
addEntry( { QString( "/" ), QString( "100%" ) } );
|
2020-06-20 05:06:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-07 17:26:37 +01:00
|
|
|
QList< Partition* >
|
2020-10-30 15:15:30 +01:00
|
|
|
PartitionLayout::createPartitions( Device* dev,
|
|
|
|
qint64 firstSector,
|
|
|
|
qint64 lastSector,
|
|
|
|
QString luksPassphrase,
|
|
|
|
PartitionNode* parent,
|
|
|
|
const PartitionRole& role )
|
2019-01-07 17:26:37 +01:00
|
|
|
{
|
|
|
|
QList< Partition* > partList;
|
2020-09-16 12:46:28 +02:00
|
|
|
// Map each partition entry to its requested size (0 when calculated later)
|
2020-06-22 16:36:20 +02:00
|
|
|
QMap< const PartitionLayout::PartitionEntry*, qint64 > partSectorsMap;
|
|
|
|
const qint64 totalSectors = lastSector - firstSector + 1;
|
|
|
|
qint64 currentSector, availableSectors = totalSectors;
|
2019-01-07 17:26:37 +01:00
|
|
|
|
2020-06-22 16:36:20 +02:00
|
|
|
// Let's check if we have enough space for each partitions, using the size
|
|
|
|
// propery or the min-size property if unit is in percentage.
|
2020-06-22 19:56:57 +02:00
|
|
|
for ( const auto& entry : qAsConst( m_partLayout ) )
|
2019-01-07 17:26:37 +01:00
|
|
|
{
|
2020-06-22 19:56:57 +02:00
|
|
|
if ( !entry.partSize.isValid() )
|
2020-10-23 22:34:53 +02:00
|
|
|
{
|
2020-06-22 19:56:57 +02:00
|
|
|
cWarning() << "Partition" << entry.partMountPoint << "size is invalid, skipping...";
|
2020-10-23 22:34:53 +02:00
|
|
|
continue;
|
|
|
|
}
|
2020-09-16 12:46:28 +02:00
|
|
|
|
2020-10-23 22:34:53 +02:00
|
|
|
// Calculate partition size: Rely on "possibly uninitialized use"
|
|
|
|
// warnings to ensure that all the cases are covered below.
|
|
|
|
// We need to ignore the percent-defined until later
|
2020-06-22 16:36:20 +02:00
|
|
|
qint64 sectors = 0;
|
2020-06-22 19:56:57 +02:00
|
|
|
if ( entry.partSize.unit() != CalamaresUtils::Partition::SizeUnit::Percent )
|
2020-10-23 22:34:53 +02:00
|
|
|
{
|
2020-06-22 19:56:57 +02:00
|
|
|
sectors = entry.partSize.toSectors( totalSectors, dev->logicalSize() );
|
2020-10-23 22:34:53 +02:00
|
|
|
}
|
2020-06-22 19:56:57 +02:00
|
|
|
else if ( entry.partMinSize.isValid() )
|
2019-04-17 19:16:48 +02:00
|
|
|
{
|
2020-06-22 19:56:57 +02:00
|
|
|
sectors = entry.partMinSize.toSectors( totalSectors, dev->logicalSize() );
|
2019-04-17 19:16:48 +02:00
|
|
|
}
|
2020-06-22 19:56:57 +02:00
|
|
|
partSectorsMap.insert( &entry, sectors );
|
2020-06-22 16:36:20 +02:00
|
|
|
availableSectors -= sectors;
|
2020-09-16 12:46:28 +02:00
|
|
|
}
|
2019-04-17 19:16:48 +02:00
|
|
|
|
2020-06-22 16:36:20 +02:00
|
|
|
// There is not enough space for all partitions, use the min-size property
|
|
|
|
// and see if we can do better afterward.
|
|
|
|
if ( availableSectors < 0 )
|
2020-09-16 12:46:28 +02:00
|
|
|
{
|
2020-06-22 16:36:20 +02:00
|
|
|
availableSectors = totalSectors;
|
2020-06-22 19:56:57 +02:00
|
|
|
for ( const auto& entry : qAsConst( m_partLayout ) )
|
2020-02-14 11:15:57 +01:00
|
|
|
{
|
2020-06-22 19:56:57 +02:00
|
|
|
qint64 sectors = partSectorsMap.value( &entry );
|
|
|
|
if ( entry.partMinSize.isValid() )
|
2020-09-16 12:46:28 +02:00
|
|
|
{
|
2020-06-22 19:56:57 +02:00
|
|
|
sectors = entry.partMinSize.toSectors( totalSectors, dev->logicalSize() );
|
|
|
|
partSectorsMap.insert( &entry, sectors );
|
2020-09-16 12:46:28 +02:00
|
|
|
}
|
2020-06-22 16:36:20 +02:00
|
|
|
availableSectors -= sectors;
|
2020-02-14 11:15:57 +01:00
|
|
|
}
|
2020-09-16 12:46:28 +02:00
|
|
|
}
|
2019-04-17 19:16:48 +02:00
|
|
|
|
2020-06-22 16:36:20 +02:00
|
|
|
// Assign sectors for percentage-defined partitions.
|
2020-06-22 19:56:57 +02:00
|
|
|
for ( const auto& entry : qAsConst( m_partLayout ) )
|
2020-09-16 12:46:28 +02:00
|
|
|
{
|
2020-06-22 19:56:57 +02:00
|
|
|
if ( entry.partSize.unit() == CalamaresUtils::Partition::SizeUnit::Percent )
|
2019-04-17 19:16:48 +02:00
|
|
|
{
|
2020-06-22 19:56:57 +02:00
|
|
|
qint64 sectors = entry.partSize.toSectors( availableSectors + partSectorsMap.value( &entry ),
|
2020-06-22 16:36:20 +02:00
|
|
|
dev->logicalSize() );
|
2020-06-22 19:56:57 +02:00
|
|
|
if ( entry.partMinSize.isValid() )
|
2020-09-16 12:46:28 +02:00
|
|
|
{
|
2020-06-22 19:56:57 +02:00
|
|
|
sectors = std::max( sectors, entry.partMinSize.toSectors( totalSectors, dev->logicalSize() ) );
|
2020-09-16 12:46:28 +02:00
|
|
|
}
|
2020-06-22 19:56:57 +02:00
|
|
|
if ( entry.partMaxSize.isValid() )
|
2020-09-16 12:46:28 +02:00
|
|
|
{
|
2020-06-22 19:56:57 +02:00
|
|
|
sectors = std::min( sectors, entry.partMaxSize.toSectors( totalSectors, dev->logicalSize() ) );
|
2020-09-16 12:46:28 +02:00
|
|
|
}
|
2020-06-22 19:56:57 +02:00
|
|
|
partSectorsMap.insert( &entry, sectors );
|
2019-04-17 19:16:48 +02:00
|
|
|
}
|
2020-09-16 12:46:28 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 16:36:20 +02:00
|
|
|
// Create the partitions.
|
|
|
|
currentSector = firstSector;
|
|
|
|
availableSectors = totalSectors;
|
2020-06-22 19:56:57 +02:00
|
|
|
for ( const auto& entry : qAsConst( m_partLayout ) )
|
2020-09-16 12:46:28 +02:00
|
|
|
{
|
2020-06-22 16:36:20 +02:00
|
|
|
// Adjust partition size based on available space.
|
2020-06-22 19:56:57 +02:00
|
|
|
qint64 sectors = partSectorsMap.value( &entry );
|
2020-06-22 16:36:20 +02:00
|
|
|
sectors = std::min( sectors, availableSectors );
|
|
|
|
if ( sectors == 0 )
|
2020-02-14 11:15:57 +01:00
|
|
|
{
|
2020-06-22 16:36:20 +02:00
|
|
|
continue;
|
2020-02-14 11:15:57 +01:00
|
|
|
}
|
2020-10-01 15:38:18 +02:00
|
|
|
|
2020-06-22 22:34:06 +02:00
|
|
|
Partition* part = nullptr;
|
2019-01-07 17:26:37 +01:00
|
|
|
if ( luksPassphrase.isEmpty() )
|
|
|
|
{
|
2020-06-22 22:34:06 +02:00
|
|
|
part = KPMHelpers::createNewPartition(
|
2020-06-22 19:56:57 +02:00
|
|
|
parent, *dev, role, entry.partFileSystem, currentSector, currentSector + sectors - 1, KPM_PARTITION_FLAG( None ) );
|
2019-01-07 17:26:37 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-22 22:34:06 +02:00
|
|
|
part = KPMHelpers::createNewEncryptedPartition(
|
2020-06-22 19:56:57 +02:00
|
|
|
parent, *dev, role, entry.partFileSystem, currentSector, currentSector + sectors - 1, luksPassphrase, KPM_PARTITION_FLAG( None ) );
|
2019-01-07 17:26:37 +01:00
|
|
|
}
|
2020-06-22 22:34:06 +02:00
|
|
|
PartitionInfo::setFormat( part, true );
|
|
|
|
PartitionInfo::setMountPoint( part, entry.partMountPoint );
|
2020-06-22 19:56:57 +02:00
|
|
|
if ( !entry.partLabel.isEmpty() )
|
2020-02-14 11:15:57 +01:00
|
|
|
{
|
2020-06-22 22:34:06 +02:00
|
|
|
part->setLabel( entry.partLabel );
|
|
|
|
part->fileSystem().setLabel( entry.partLabel );
|
2020-02-14 11:15:57 +01:00
|
|
|
}
|
2020-06-22 19:56:57 +02:00
|
|
|
if ( !entry.partUUID.isEmpty() )
|
2020-05-14 18:30:58 +02:00
|
|
|
{
|
2020-06-22 22:34:06 +02:00
|
|
|
part->setUUID( entry.partUUID );
|
2020-05-14 18:30:58 +02:00
|
|
|
}
|
2020-06-22 19:56:57 +02:00
|
|
|
if ( !entry.partType.isEmpty() )
|
2020-03-19 13:37:21 +01:00
|
|
|
{
|
|
|
|
#if defined( WITH_KPMCORE42API )
|
2020-06-22 22:34:06 +02:00
|
|
|
part->setType( entry.partType );
|
2020-03-19 13:37:21 +01:00
|
|
|
#else
|
|
|
|
cWarning() << "Ignoring type; requires KPMcore >= 4.2.0.";
|
2020-03-21 19:21:16 +01:00
|
|
|
#endif
|
|
|
|
}
|
2020-06-22 19:56:57 +02:00
|
|
|
if ( entry.partAttributes )
|
2020-03-21 19:21:16 +01:00
|
|
|
{
|
|
|
|
#if defined( WITH_KPMCORE42API )
|
2020-06-22 22:34:06 +02:00
|
|
|
part->setAttributes( entry.partAttributes );
|
2020-03-21 19:21:16 +01:00
|
|
|
#else
|
|
|
|
cWarning() << "Ignoring attributes; requires KPMcore >= 4.2.0.";
|
2020-03-23 17:09:44 +01:00
|
|
|
#endif
|
|
|
|
}
|
2020-06-22 19:56:57 +02:00
|
|
|
if ( !entry.partFeatures.isEmpty() )
|
2020-03-16 22:25:55 +01:00
|
|
|
{
|
|
|
|
#if defined( WITH_KPMCORE42API )
|
2020-06-22 19:56:57 +02:00
|
|
|
for ( const auto& k : entry.partFeatures.keys() )
|
2020-03-23 17:09:44 +01:00
|
|
|
{
|
2020-06-22 22:34:06 +02:00
|
|
|
part->fileSystem().addFeature( k, entry.partFeatures.value( k ) );
|
2020-03-23 17:09:44 +01:00
|
|
|
}
|
2020-03-16 22:25:55 +01:00
|
|
|
#else
|
|
|
|
cWarning() << "Ignoring features; requires KPMcore >= 4.2.0.";
|
2020-03-19 13:37:21 +01:00
|
|
|
#endif
|
|
|
|
}
|
2019-01-07 17:26:37 +01:00
|
|
|
// Some buggy (legacy) BIOSes test if the bootflag of at least one partition is set.
|
|
|
|
// Otherwise they ignore the device in boot-order, so add it here.
|
2020-06-22 22:34:06 +02:00
|
|
|
partList.append( part );
|
2020-06-22 16:36:20 +02:00
|
|
|
currentSector += sectors;
|
|
|
|
availableSectors -= sectors;
|
2019-01-07 17:26:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return partList;
|
|
|
|
}
|