calamares/src/modules/partition/core/PartitionCoreModule.cpp

1118 lines
31 KiB
C++
Raw Normal View History

/* === This file is part of Calamares - <https://calamares.io> ===
*
2020-08-22 01:19:58 +02:00
* SPDX-FileCopyrightText: 2014 Aurélien Gâteau <agateau@kde.org>
* SPDX-FileCopyrightText: 2014-2015 Teo Mrnjavac <teo@kde.org>
* SPDX-FileCopyrightText: 2017-2019 Adriaan de Groot <groot@kde.org>
* SPDX-FileCopyrightText: 2018 Caio Carvalho <caiojcarvalho@gmail.com>
* SPDX-FileCopyrightText: 2019 Collabora Ltd <arnaud.ferraris@collabora.com>
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Calamares is Free Software: see the License-Identifier above.
*
*/
2015-07-03 17:03:33 +02:00
#include "core/PartitionCoreModule.h"
#include "core/BootLoaderModel.h"
2016-07-27 16:49:53 +02:00
#include "core/ColorUtils.h"
#include "core/DeviceList.h"
2015-07-03 17:03:33 +02:00
#include "core/DeviceModel.h"
#include "core/KPMHelpers.h"
#include "core/PartUtils.h"
#include "core/PartitionInfo.h"
#include "core/PartitionModel.h"
#include "jobs/AutoMountManagementJob.h"
#include "jobs/ChangeFilesystemLabelJob.h"
2015-07-03 17:03:33 +02:00
#include "jobs/ClearMountsJob.h"
#include "jobs/ClearTempMountsJob.h"
#include "jobs/CreatePartitionJob.h"
#include "jobs/CreatePartitionTableJob.h"
#include "jobs/CreateVolumeGroupJob.h"
#include "jobs/DeactivateVolumeGroupJob.h"
2015-07-03 17:03:33 +02:00
#include "jobs/DeletePartitionJob.h"
#include "jobs/FillGlobalStorageJob.h"
#include "jobs/FormatPartitionJob.h"
#include "jobs/RemoveVolumeGroupJob.h"
2015-07-03 17:03:33 +02:00
#include "jobs/ResizePartitionJob.h"
#include "jobs/ResizeVolumeGroupJob.h"
#include "jobs/SetPartitionFlagsJob.h"
#ifdef DEBUG_PARTITION_LAME
#include "JobExample.h"
#endif
#include "partition/PartitionIterator.h"
#include "partition/PartitionQuery.h"
2015-07-03 17:03:33 +02:00
#include "utils/Logger.h"
#include "utils/Traits.h"
#include "utils/Variant.h"
// KPMcore
#include <kpmcore/backend/corebackend.h>
#include <kpmcore/backend/corebackendmanager.h>
#include <kpmcore/core/device.h>
#include <kpmcore/core/lvmdevice.h>
#include <kpmcore/core/partition.h>
#include <kpmcore/core/volumemanagerdevice.h>
#include <kpmcore/fs/filesystemfactory.h>
#include <kpmcore/fs/luks.h>
#include <kpmcore/fs/lvm2_pv.h>
// Qt
#include <QDir>
#include <QFutureWatcher>
#include <QProcess>
#include <QStandardItemModel>
#include <QtConcurrent/QtConcurrent>
using CalamaresUtils::Partition::isPartitionFreeSpace;
using CalamaresUtils::Partition::isPartitionNew;
using CalamaresUtils::Partition::PartitionIterator;
PartitionCoreModule::RefreshHelper::RefreshHelper( PartitionCoreModule* module )
: m_module( module )
{
}
PartitionCoreModule::RefreshHelper::~RefreshHelper()
{
m_module->refreshAfterModelChange();
}
class OperationHelper
{
public:
OperationHelper( PartitionModel* model, PartitionCoreModule* core )
: m_coreHelper( core )
, m_modelHelper( model )
{
}
OperationHelper( const OperationHelper& ) = delete;
OperationHelper& operator=( const OperationHelper& ) = delete;
private:
// Keep these in order: first the model needs to finish,
// then refresh is called. Remember that destructors are
// called in *reverse* order of declaration in this class.
PartitionCoreModule::RefreshHelper m_coreHelper;
PartitionModel::ResetHelper m_modelHelper;
};
//- DeviceInfo ---------------------------------------------
// Some jobs have an updatePreview some don't
DECLARE_HAS_METHOD( updatePreview )
template < typename Job >
void
updatePreview( Job* job, const std::true_type& )
{
job->updatePreview();
}
template < typename Job >
void
2020-10-24 02:24:37 +02:00
updatePreview( Job*, const std::false_type& )
{
}
template < typename Job >
void
updatePreview( Job* job )
{
updatePreview( job, has_updatePreview< Job > {} );
}
/**
* Owns the Device, PartitionModel and the jobs
*/
struct PartitionCoreModule::DeviceInfo
{
DeviceInfo( Device* );
~DeviceInfo();
QScopedPointer< Device > device;
QScopedPointer< PartitionModel > partitionModel;
const QScopedPointer< Device > immutableDevice;
// To check if LVM VGs are deactivated
bool isAvailable;
void forgetChanges();
bool isDirty() const;
const Calamares::JobList& jobs() const { return m_jobs; }
/** @brief Take the jobs of the given type that apply to @p partition
*
* Returns a job pointer to the job that has just been removed.
*/
template < typename Job >
Calamares::job_ptr takeJob( Partition* partition )
{
for ( auto it = m_jobs.begin(); it != m_jobs.end(); )
{
Job* job = qobject_cast< Job* >( it->data() );
if ( job && job->partition() == partition )
{
Calamares::job_ptr p = *it;
it = m_jobs.erase( it );
return p;
}
else
{
++it;
}
}
return Calamares::job_ptr( nullptr );
}
/** @brief Add a job of given type to the job list
*/
template < typename Job, typename... Args >
Calamares::Job* makeJob( Args... a )
{
auto* job = new Job( device.get(), a... );
updatePreview( job );
m_jobs << Calamares::job_ptr( job );
return job;
}
private:
Calamares::JobList m_jobs;
};
PartitionCoreModule::DeviceInfo::DeviceInfo( Device* _device )
: device( _device )
, partitionModel( new PartitionModel )
, immutableDevice( new Device( *_device ) )
, isAvailable( true )
{
}
PartitionCoreModule::DeviceInfo::~DeviceInfo() {}
void
PartitionCoreModule::DeviceInfo::forgetChanges()
{
m_jobs.clear();
2014-07-17 09:49:24 +02:00
for ( auto it = PartitionIterator::begin( device.data() ); it != PartitionIterator::end( device.data() ); ++it )
{
PartitionInfo::reset( *it );
}
2015-12-18 15:02:55 +01:00
partitionModel->revert();
}
2014-07-24 19:28:53 +02:00
bool
PartitionCoreModule::DeviceInfo::isDirty() const
{
if ( !m_jobs.isEmpty() )
{
2014-07-24 19:28:53 +02:00
return true;
}
2014-07-24 19:28:53 +02:00
for ( auto it = PartitionIterator::begin( device.data() ); it != PartitionIterator::end( device.data() ); ++it )
{
2014-07-24 19:28:53 +02:00
if ( PartitionInfo::isDirty( *it ) )
{
2014-07-24 19:28:53 +02:00
return true;
}
}
2014-07-24 19:28:53 +02:00
return false;
}
//- PartitionCoreModule ------------------------------------
PartitionCoreModule::PartitionCoreModule( QObject* parent )
: QObject( parent )
, m_deviceModel( new DeviceModel( this ) )
, m_bootLoaderModel( new BootLoaderModel( this ) )
{
if ( !m_kpmcore )
{
2015-07-03 17:03:33 +02:00
qFatal( "Failed to initialize KPMcore backend" );
}
2014-07-24 19:28:53 +02:00
}
2014-07-24 19:28:53 +02:00
void
PartitionCoreModule::init()
{
QMutexLocker locker( &m_revertMutex );
doInit();
}
void
PartitionCoreModule::doInit()
2014-07-24 19:28:53 +02:00
{
2016-07-20 13:01:33 +02:00
FileSystemFactory::init();
2017-07-04 11:32:05 +02:00
using DeviceList = QList< Device* >;
DeviceList devices = PartUtils::getDevices( PartUtils::DeviceType::WritableOnly );
2014-07-30 23:21:06 +02:00
2017-07-04 11:32:05 +02:00
cDebug() << "LIST OF DETECTED DEVICES:";
cDebug() << Logger::SubEntry << "node\tcapacity\tname\tprettyName";
for ( auto device : devices )
{
cDebug() << Logger::SubEntry << Logger::Pointer( device );
if ( device )
{
// Gives ownership of the Device* to the DeviceInfo object
auto deviceInfo = new DeviceInfo( device );
m_deviceInfos << deviceInfo;
cDebug() << Logger::SubEntry << device->deviceNode() << device->capacity() << device->name()
<< device->prettyName();
}
else
{
cDebug() << Logger::SubEntry << "(skipped null device)";
}
}
cDebug() << Logger::SubEntry << devices.count() << "devices detected.";
m_deviceModel->init( devices );
// The following PartUtils::runOsprober call in turn calls PartUtils::canBeResized,
// which relies on a working DeviceModel.
m_osproberLines = PartUtils::runOsprober( this->deviceModel() );
// We perform a best effort of filling out filesystem UUIDs in m_osproberLines
// because we will need them later on in PartitionModel if partition paths
// change.
// It is a known fact that /dev/sda1-style device paths aren't persistent
// across reboots (and this doesn't affect us), but partition numbers can also
// change at runtime against our will just for shits and giggles.
// But why would that ever happen? What system could possibly be so poorly
// designed that it requires a partition path rearrangement at runtime?
// Logical partitions on an MSDOS disklabel of course.
// See DeletePartitionJob::updatePreview.
for ( auto deviceInfo : m_deviceInfos )
{
for ( auto it = PartitionIterator::begin( deviceInfo->device.data() );
it != PartitionIterator::end( deviceInfo->device.data() );
++it )
{
Partition* partition = *it;
for ( auto jt = m_osproberLines.begin(); jt != m_osproberLines.end(); ++jt )
{
if ( jt->path == partition->partitionPath()
&& partition->fileSystem().supportGetUUID() != FileSystem::cmdSupportNone
&& !partition->fileSystem().uuid().isEmpty() )
{
jt->uuid = partition->fileSystem().uuid();
}
}
}
}
for ( auto deviceInfo : m_deviceInfos )
{
deviceInfo->partitionModel->init( deviceInfo->device.data(), m_osproberLines );
}
DeviceList bootLoaderDevices;
for ( DeviceList::Iterator it = devices.begin(); it != devices.end(); ++it )
if ( ( *it )->type() != Device::Type::Disk_Device )
{
cDebug() << "Ignoring device that is not Disk_Device to bootLoaderDevices list.";
continue;
}
else
{
bootLoaderDevices.append( *it );
}
m_bootLoaderModel->init( bootLoaderDevices );
scanForLVMPVs();
2017-07-11 11:26:10 +02:00
//FIXME: this should be removed in favor of
// proper KPM support for EFI
if ( PartUtils::isEfiSystem() )
{
2017-07-11 11:26:10 +02:00
scanForEfiSystemPartitions();
}
}
PartitionCoreModule::~PartitionCoreModule()
{
qDeleteAll( m_deviceInfos );
}
DeviceModel*
PartitionCoreModule::deviceModel() const
{
return m_deviceModel;
}
BootLoaderModel*
PartitionCoreModule::bootLoaderModel() const
{
return m_bootLoaderModel;
}
PartitionModel*
PartitionCoreModule::partitionModelForDevice( const Device* device ) const
{
DeviceInfo* info = infoForDevice( device );
Q_ASSERT( info );
return info->partitionModel.data();
}
Device*
PartitionCoreModule::immutableDeviceCopy( const Device* device )
{
Q_ASSERT( device );
DeviceInfo* info = infoForDevice( device );
if ( !info )
{
return nullptr;
}
return info->immutableDevice.data();
}
void
PartitionCoreModule::createPartitionTable( Device* device, PartitionTable::TableType type )
{
auto* deviceInfo = infoForDevice( device );
if ( deviceInfo )
2015-06-21 01:27:02 +02:00
{
// Creating a partition table wipes all the disk, so there is no need to
// keep previous changes
deviceInfo->forgetChanges();
2015-06-21 01:27:02 +02:00
OperationHelper helper( partitionModelForDevice( device ), this );
2020-08-04 16:53:29 +02:00
deviceInfo->makeJob< CreatePartitionTableJob >( type );
2015-06-21 01:27:02 +02:00
}
}
void
PartitionCoreModule::createPartition( Device* device, Partition* partition, PartitionTable::Flags flags )
{
auto* deviceInfo = infoForDevice( device );
Q_ASSERT( deviceInfo );
OperationHelper helper( partitionModelForDevice( device ), this );
2020-08-04 16:53:29 +02:00
deviceInfo->makeJob< CreatePartitionJob >( partition );
2014-07-02 15:49:35 +02:00
if ( flags != KPM_PARTITION_FLAG( None ) )
{
2020-08-04 16:53:29 +02:00
deviceInfo->makeJob< SetPartFlagsJob >( partition, flags );
PartitionInfo::setFlags( partition, flags );
}
}
void
PartitionCoreModule::createVolumeGroup( QString& vgName, QVector< const Partition* > pvList, qint32 peSize )
{
// Appending '_' character in case of repeated VG name
while ( hasVGwithThisName( vgName ) )
{
vgName.append( '_' );
}
LvmDevice* device = new LvmDevice( vgName );
for ( const Partition* p : pvList )
{
device->physicalVolumes() << p;
}
DeviceInfo* deviceInfo = new DeviceInfo( device );
deviceInfo->partitionModel->init( device, osproberEntries() );
m_deviceModel->addDevice( device );
m_deviceInfos << deviceInfo;
2020-08-04 16:53:29 +02:00
deviceInfo->makeJob< CreateVolumeGroupJob >( vgName, pvList, peSize );
refreshAfterModelChange();
}
void
PartitionCoreModule::resizeVolumeGroup( LvmDevice* device, QVector< const Partition* >& pvList )
{
auto* deviceInfo = infoForDevice( device );
Q_ASSERT( deviceInfo );
2020-08-04 16:53:29 +02:00
deviceInfo->makeJob< ResizeVolumeGroupJob >( device, pvList );
refreshAfterModelChange();
}
void
PartitionCoreModule::deactivateVolumeGroup( LvmDevice* device )
{
auto* deviceInfo = infoForDevice( device );
Q_ASSERT( deviceInfo );
deviceInfo->isAvailable = false;
2020-08-04 16:53:29 +02:00
// TODO: this leaks
DeactivateVolumeGroupJob* job = new DeactivateVolumeGroupJob( device );
// DeactivateVolumeGroupJob needs to be immediately called
job->exec();
refreshAfterModelChange();
}
void
PartitionCoreModule::removeVolumeGroup( LvmDevice* device )
{
auto* deviceInfo = infoForDevice( device );
Q_ASSERT( deviceInfo );
2020-08-04 16:53:29 +02:00
deviceInfo->makeJob< RemoveVolumeGroupJob >( device );
refreshAfterModelChange();
}
2014-07-02 15:49:35 +02:00
void
PartitionCoreModule::deletePartition( Device* device, Partition* partition )
{
auto* deviceInfo = infoForDevice( device );
Q_ASSERT( deviceInfo );
OperationHelper helper( partitionModelForDevice( device ), this );
if ( partition->roles().has( PartitionRole::Extended ) )
{
// Delete all logical partitions first
// I am not sure if we can iterate on Partition::children() while
// deleting them, so let's play it safe and keep our own list.
QList< Partition* > lst;
for ( auto childPartition : partition->children() )
if ( !isPartitionFreeSpace( childPartition ) )
{
lst << childPartition;
}
for ( auto childPartition : lst )
{
deletePartition( device, childPartition );
}
}
if ( partition->state() == KPM_PARTITION_STATE( New ) )
2014-07-02 15:49:35 +02:00
{
// Take all the SetPartFlagsJob from the list and delete them
do
{
auto job_ptr = deviceInfo->takeJob< SetPartFlagsJob >( partition );
if ( job_ptr.data() )
{
continue;
}
} while ( false );
2014-07-02 15:49:35 +02:00
// Find matching CreatePartitionJob
auto job_ptr = deviceInfo->takeJob< CreatePartitionJob >( partition );
if ( !job_ptr.data() )
2014-07-02 15:49:35 +02:00
{
cDebug() << "Failed to find a CreatePartitionJob matching the partition to remove";
return;
}
// Remove it
if ( !partition->parent()->remove( partition ) )
2014-07-02 15:49:35 +02:00
{
cDebug() << "Failed to remove partition from preview";
return;
}
2014-07-02 15:49:35 +02:00
device->partitionTable()->updateUnallocated( *device );
// The partition is no longer referenced by either a job or the device
// partition list, so we have to delete it
delete partition;
}
else
{
2014-07-18 14:29:27 +02:00
// Remove any PartitionJob on this partition
do
2014-07-18 14:29:27 +02:00
{
auto job_ptr = deviceInfo->takeJob< PartitionJob >( partition );
if ( job_ptr.data() )
{
continue;
}
} while ( false );
2020-08-04 16:53:29 +02:00
deviceInfo->makeJob< DeletePartitionJob >( partition );
2014-07-02 15:49:35 +02:00
}
}
void
PartitionCoreModule::formatPartition( Device* device, Partition* partition )
{
auto* deviceInfo = infoForDevice( device );
Q_ASSERT( deviceInfo );
OperationHelper helper( partitionModelForDevice( device ), this );
2020-08-04 16:53:29 +02:00
deviceInfo->makeJob< FormatPartitionJob >( partition );
}
void
2021-03-27 15:43:32 +01:00
PartitionCoreModule::setFilesystemLabel( Device* device, Partition* partition, const QString& newLabel )
{
auto deviceInfo = infoForDevice( device );
Q_ASSERT( deviceInfo );
OperationHelper helper( partitionModelForDevice( device ), this );
deviceInfo->makeJob< ChangeFilesystemLabelJob >( partition, newLabel );
}
void
PartitionCoreModule::resizePartition( Device* device, Partition* partition, qint64 first, qint64 last )
{
auto* deviceInfo = infoForDevice( device );
Q_ASSERT( deviceInfo );
OperationHelper helper( partitionModelForDevice( device ), this );
2020-08-04 16:53:29 +02:00
deviceInfo->makeJob< ResizePartitionJob >( partition, first, last );
}
void
PartitionCoreModule::setPartitionFlags( Device* device, Partition* partition, PartitionTable::Flags flags )
{
auto* deviceInfo = infoForDevice( device );
Q_ASSERT( deviceInfo );
OperationHelper( partitionModelForDevice( device ), this );
2020-08-04 16:53:29 +02:00
deviceInfo->makeJob< SetPartFlagsJob >( partition, flags );
PartitionInfo::setFlags( partition, flags );
}
Calamares::JobList
PartitionCoreModule::jobs( const Config* config ) const
{
Calamares::JobList lst;
2014-07-22 17:32:26 +02:00
QList< Device* > devices;
#ifdef DEBUG_PARTITION_UNSAFE
#ifdef DEBUG_PARTITION_LAME
cDebug() << "Unsafe partitioning is enabled.";
cDebug() << Logger::SubEntry << "it has been lamed, and will fail.";
lst << Calamares::job_ptr( new Calamares::FailJob( QStringLiteral( "Partition" ) ) );
#else
cWarning() << "Unsafe partitioning is enabled.";
cWarning() << Logger::SubEntry << "the unsafe actions will be executed.";
#endif
#endif
// The automountControl job goes in the list twice: the first
// time it runs, it disables automount and remembers the old setting
// for automount; the second time it restores that old setting.
Calamares::job_ptr automountControl( new AutoMountManagementJob( true /* disable automount */ ) );
lst << automountControl;
lst << Calamares::job_ptr( new ClearTempMountsJob() );
for ( auto info : m_deviceInfos )
{
if ( info->isDirty() )
{
lst << Calamares::job_ptr( new ClearMountsJob( info->device.data() ) );
}
}
for ( auto info : m_deviceInfos )
2014-07-22 17:32:26 +02:00
{
2020-08-04 16:53:29 +02:00
lst << info->jobs();
2014-07-22 17:32:26 +02:00
devices << info->device.data();
}
lst << Calamares::job_ptr( new FillGlobalStorageJob( config, devices, m_bootLoaderInstallPath ) );
lst << automountControl;
return lst;
}
2015-05-28 18:32:59 +02:00
bool
PartitionCoreModule::hasRootMountPoint() const
{
return m_hasRootMountPoint;
}
QList< Partition* >
PartitionCoreModule::efiSystemPartitions() const
{
return m_efiSystemPartitions;
}
QVector< const Partition* >
PartitionCoreModule::lvmPVs() const
{
return m_lvmPVs;
}
bool
PartitionCoreModule::hasVGwithThisName( const QString& name ) const
{
2020-10-12 14:27:01 +02:00
auto condition = [name]( DeviceInfo* d ) {
return dynamic_cast< LvmDevice* >( d->device.data() ) && d->device.data()->name() == name;
};
return std::find_if( m_deviceInfos.begin(), m_deviceInfos.end(), condition ) != m_deviceInfos.end();
}
bool
PartitionCoreModule::isInVG( const Partition* partition ) const
{
2020-10-12 14:27:01 +02:00
auto condition = [partition]( DeviceInfo* d ) {
LvmDevice* vg = dynamic_cast< LvmDevice* >( d->device.data() );
return vg && vg->physicalVolumes().contains( partition );
};
return std::find_if( m_deviceInfos.begin(), m_deviceInfos.end(), condition ) != m_deviceInfos.end();
}
2014-07-02 15:49:35 +02:00
void
PartitionCoreModule::dumpQueue() const
{
2014-07-16 11:15:22 +02:00
cDebug() << "# Queue:";
for ( auto info : m_deviceInfos )
2014-07-02 15:49:35 +02:00
{
cDebug() << Logger::SubEntry << "## Device:" << info->device->name();
2020-08-04 16:53:29 +02:00
for ( const auto& job : info->jobs() )
{
cDebug() << Logger::SubEntry << "-" << job->prettyName();
}
2014-07-02 15:49:35 +02:00
}
}
2014-07-10 18:55:19 +02:00
const OsproberEntryList
PartitionCoreModule::osproberEntries() const
{
return m_osproberLines;
}
2014-07-10 18:55:19 +02:00
void
PartitionCoreModule::refreshPartition( Device* device, Partition* )
2014-07-10 18:55:19 +02:00
{
// Keep it simple for now: reset the model. This can be improved to cause
2021-06-18 11:31:53 +02:00
// the model to Q_EMIT dataChanged() for the affected row instead, avoiding
// the loss of the current selection.
auto model = partitionModelForDevice( device );
2014-07-10 18:55:19 +02:00
Q_ASSERT( model );
OperationHelper helper( model, this );
}
void
PartitionCoreModule::refreshAfterModelChange()
{
updateHasRootMountPoint();
2014-07-24 19:28:53 +02:00
updateIsDirty();
m_bootLoaderModel->update();
2017-07-11 11:26:10 +02:00
scanForLVMPVs();
2017-07-11 11:26:10 +02:00
//FIXME: this should be removed in favor of
// proper KPM support for EFI
if ( PartUtils::isEfiSystem() )
{
2017-07-11 11:26:10 +02:00
scanForEfiSystemPartitions();
}
2014-07-10 18:55:19 +02:00
}
void
PartitionCoreModule::updateHasRootMountPoint()
{
bool oldValue = m_hasRootMountPoint;
m_hasRootMountPoint = findPartitionByMountPoint( "/" );
if ( oldValue != m_hasRootMountPoint )
{
hasRootMountPointChanged( m_hasRootMountPoint );
}
}
2014-07-24 19:28:53 +02:00
void
PartitionCoreModule::updateIsDirty()
{
bool oldValue = m_isDirty;
m_isDirty = false;
for ( auto info : m_deviceInfos )
if ( info->isDirty() )
{
m_isDirty = true;
break;
}
if ( oldValue != m_isDirty )
{
2014-07-24 19:28:53 +02:00
isDirtyChanged( m_isDirty );
}
2014-07-24 19:28:53 +02:00
}
void
PartitionCoreModule::scanForEfiSystemPartitions()
{
const bool wasEmpty = m_efiSystemPartitions.isEmpty();
m_efiSystemPartitions.clear();
QList< Device* > devices;
for ( int row = 0; row < deviceModel()->rowCount(); ++row )
{
Device* device = deviceModel()->deviceForIndex( deviceModel()->index( row ) );
devices.append( device );
}
QList< Partition* > efiSystemPartitions
= CalamaresUtils::Partition::findPartitions( devices, PartUtils::isEfiBootable );
if ( efiSystemPartitions.isEmpty() )
{
cWarning() << "system is EFI but no EFI system partitions found.";
}
else if ( wasEmpty )
{
// But it isn't empty anymore, so whatever problem has been solved
cDebug() << "system is EFI and new EFI system partition has been found.";
}
m_efiSystemPartitions = efiSystemPartitions;
}
void
PartitionCoreModule::scanForLVMPVs()
{
m_lvmPVs.clear();
QList< Device* > physicalDevices;
QList< LvmDevice* > vgDevices;
for ( DeviceInfo* deviceInfo : m_deviceInfos )
{
if ( deviceInfo->device.data()->type() == Device::Type::Disk_Device )
{
physicalDevices << deviceInfo->device.data();
}
else if ( deviceInfo->device.data()->type() == Device::Type::LVM_Device )
{
LvmDevice* device = dynamic_cast< LvmDevice* >( deviceInfo->device.data() );
// Restoring physical volume list
device->physicalVolumes().clear();
vgDevices << device;
}
}
#if defined( WITH_KPMCORE4API )
VolumeManagerDevice::scanDevices( physicalDevices );
for ( auto p : LVM::pvList::list() )
#else
LvmDevice::scanSystemLVM( physicalDevices );
for ( auto p : LVM::pvList )
#endif
{
m_lvmPVs << p.partition().data();
for ( LvmDevice* device : vgDevices )
if ( p.vgName() == device->name() )
{
// Adding scanned VG to PV list
device->physicalVolumes() << p.partition();
break;
}
}
for ( DeviceInfo* d : m_deviceInfos )
{
2020-08-04 16:53:29 +02:00
for ( const auto& job : d->jobs() )
{
// Including new LVM PVs
CreatePartitionJob* partJob = dynamic_cast< CreatePartitionJob* >( job.data() );
if ( partJob )
{
Partition* p = partJob->partition();
if ( p->fileSystem().type() == FileSystem::Type::Lvm2_PV )
{
m_lvmPVs << p;
}
else if ( p->fileSystem().type() == FileSystem::Type::Luks )
{
// Encrypted LVM PVs
FileSystem* innerFS = static_cast< const FS::luks* >( &p->fileSystem() )->innerFS();
if ( innerFS && innerFS->type() == FileSystem::Type::Lvm2_PV )
{
m_lvmPVs << p;
}
}
#if defined( WITH_KPMCORE4API )
else if ( p->fileSystem().type() == FileSystem::Type::Luks2 )
{
// Encrypted LVM PVs
FileSystem* innerFS = static_cast< const FS::luks* >( &p->fileSystem() )->innerFS();
if ( innerFS && innerFS->type() == FileSystem::Type::Lvm2_PV )
{
m_lvmPVs << p;
}
}
#endif
}
}
}
}
PartitionCoreModule::DeviceInfo*
PartitionCoreModule::infoForDevice( const Device* device ) const
{
for ( auto it = m_deviceInfos.constBegin(); it != m_deviceInfos.constEnd(); ++it )
{
if ( ( *it )->device.data() == device )
{
return *it;
}
if ( ( *it )->immutableDevice.data() == device )
{
return *it;
}
}
return nullptr;
}
Partition*
PartitionCoreModule::findPartitionByMountPoint( const QString& mountPoint ) const
{
for ( auto deviceInfo : m_deviceInfos )
{
Device* device = deviceInfo->device.data();
for ( auto it = PartitionIterator::begin( device ); it != PartitionIterator::end( device ); ++it )
if ( PartitionInfo::mountPoint( *it ) == mountPoint )
{
return *it;
}
}
return nullptr;
}
void
PartitionCoreModule::setBootLoaderInstallPath( const QString& path )
{
cDebug() << "PCM::setBootLoaderInstallPath" << path;
m_bootLoaderInstallPath = path;
}
2014-07-24 19:28:53 +02:00
void
PartitionCoreModule::initLayout( FileSystem::Type defaultFsType, const QVariantList& config )
{
m_partLayout.init( defaultFsType, config );
}
void
PartitionCoreModule::layoutApply( Device* dev,
qint64 firstSector,
qint64 lastSector,
QString luksPassphrase,
PartitionNode* parent,
const PartitionRole& role )
{
bool isEfi = PartUtils::isEfiSystem();
QList< Partition* > partList
= m_partLayout.createPartitions( dev, firstSector, lastSector, luksPassphrase, parent, role );
// Partition::mountPoint() tells us where it is mounted **now**, while
// PartitionInfo::mountPoint() says where it will be mounted in the target system.
// .. the latter is more interesting.
//
// If we have a separate /boot, mark that one as bootable, otherwise mark
// the root / as bootable.
//
// TODO: perhaps the partition that holds the bootloader?
const QString boot = QStringLiteral( "/boot" );
const QString root = QStringLiteral( "/" );
const auto is_boot
2020-10-12 14:27:01 +02:00
= [&]( Partition* p ) -> bool { return PartitionInfo::mountPoint( p ) == boot || p->mountPoint() == boot; };
const auto is_root
2020-10-12 14:27:01 +02:00
= [&]( Partition* p ) -> bool { return PartitionInfo::mountPoint( p ) == root || p->mountPoint() == root; };
const bool separate_boot_partition
= std::find_if( partList.constBegin(), partList.constEnd(), is_boot ) != partList.constEnd();
for ( Partition* part : partList )
{
if ( is_boot( part ) )
{
part->setLabel( "boot" );
}
if ( is_root( part ) )
{
part->setLabel( "root" );
}
if ( ( separate_boot_partition && is_boot( part ) ) || ( !separate_boot_partition && is_root( part ) ) )
{
createPartition(
dev, part, part->activeFlags() | ( isEfi ? KPM_PARTITION_FLAG( None ) : KPM_PARTITION_FLAG( Boot ) ) );
}
else
{
createPartition( dev, part );
}
}
}
void
PartitionCoreModule::layoutApply( Device* dev, qint64 firstSector, qint64 lastSector, QString luksPassphrase )
{
layoutApply(
dev, firstSector, lastSector, luksPassphrase, dev->partitionTable(), PartitionRole( PartitionRole::Primary ) );
}
2014-07-24 19:28:53 +02:00
void
PartitionCoreModule::revert()
{
2015-12-30 16:37:04 +01:00
QMutexLocker locker( &m_revertMutex );
2014-07-24 19:28:53 +02:00
qDeleteAll( m_deviceInfos );
m_deviceInfos.clear();
doInit();
2014-07-24 19:28:53 +02:00
updateIsDirty();
2021-06-18 11:31:53 +02:00
Q_EMIT reverted();
2014-07-24 19:28:53 +02:00
}
2015-12-30 16:37:04 +01:00
void
PartitionCoreModule::revertAllDevices()
{
for ( auto it = m_deviceInfos.begin(); it != m_deviceInfos.end(); )
{
// In new VGs device info, there will be always a CreateVolumeGroupJob as the first job in jobs list
if ( dynamic_cast< LvmDevice* >( ( *it )->device.data() ) )
{
( *it )->isAvailable = true;
2020-08-04 16:53:29 +02:00
if ( !( *it )->jobs().empty() )
{
2020-08-04 16:53:29 +02:00
CreateVolumeGroupJob* vgJob = dynamic_cast< CreateVolumeGroupJob* >( ( *it )->jobs().first().data() );
if ( vgJob )
{
vgJob->undoPreview();
( *it )->forgetChanges();
m_deviceModel->removeDevice( ( *it )->device.data() );
it = m_deviceInfos.erase( it );
continue;
}
}
}
revertDevice( ( *it )->device.data(), false );
++it;
}
refreshAfterModelChange();
2015-12-30 16:37:04 +01:00
}
2015-12-17 18:00:00 +01:00
void
PartitionCoreModule::revertDevice( Device* dev, bool individualRevert )
2015-12-17 18:00:00 +01:00
{
2015-12-23 19:14:33 +01:00
QMutexLocker locker( &m_revertMutex );
2015-12-17 18:00:00 +01:00
DeviceInfo* devInfo = infoForDevice( dev );
2015-12-17 18:00:00 +01:00
if ( !devInfo )
{
2015-12-17 18:00:00 +01:00
return;
}
2015-12-17 18:00:00 +01:00
devInfo->forgetChanges();
CoreBackend* backend = CoreBackendManager::self()->backend();
2017-07-11 11:26:10 +02:00
Device* newDev = backend->scanDevice( devInfo->device->deviceNode() );
2015-12-17 18:00:00 +01:00
devInfo->device.reset( newDev );
devInfo->partitionModel->init( newDev, m_osproberLines );
2015-12-17 18:00:00 +01:00
m_deviceModel->swapDevice( dev, newDev );
QList< Device* > devices;
for ( DeviceInfo* const info : m_deviceInfos )
{
if ( info && !info->device.isNull() && info->device->type() == Device::Type::Disk_Device )
{
devices.append( info->device.data() );
}
}
2015-12-17 18:00:00 +01:00
m_bootLoaderModel->init( devices );
if ( individualRevert )
{
refreshAfterModelChange();
}
2021-06-18 11:31:53 +02:00
Q_EMIT deviceReverted( newDev );
2015-12-17 18:00:00 +01:00
}
2015-12-23 19:14:33 +01:00
void
PartitionCoreModule::asyncRevertDevice( Device* dev, std::function< void() > callback )
{
QFutureWatcher< void >* watcher = new QFutureWatcher< void >();
2020-10-12 14:27:01 +02:00
connect( watcher, &QFutureWatcher< void >::finished, this, [watcher, callback] {
callback();
watcher->deleteLater();
2015-12-23 19:21:08 +01:00
} );
QFuture< void > future = QtConcurrent::run( this, &PartitionCoreModule::revertDevice, dev, true );
watcher->setFuture( future );
2015-12-23 19:14:33 +01:00
}
2014-09-16 18:11:19 +02:00
void
PartitionCoreModule::clearJobs()
{
foreach ( DeviceInfo* deviceInfo, m_deviceInfos )
{
2014-09-16 18:11:19 +02:00
deviceInfo->forgetChanges();
}
2014-09-16 18:11:19 +02:00
updateIsDirty();
}
bool
PartitionCoreModule::isDirty()
{
return m_isDirty;
}
bool
PartitionCoreModule::isVGdeactivated( LvmDevice* device )
{
for ( DeviceInfo* deviceInfo : m_deviceInfos )
if ( device == deviceInfo->device.data() && !deviceInfo->isAvailable )
{
return true;
}
return false;
}
QList< PartitionCoreModule::SummaryInfo >
PartitionCoreModule::createSummaryInfo() const
{
QList< SummaryInfo > lst;
for ( auto deviceInfo : m_deviceInfos )
{
if ( !deviceInfo->isDirty() )
{
continue;
}
SummaryInfo summaryInfo;
summaryInfo.deviceName = deviceInfo->device->name();
summaryInfo.deviceNode = deviceInfo->device->deviceNode();
Device* deviceBefore = deviceInfo->immutableDevice.data();
summaryInfo.partitionModelBefore = new PartitionModel;
summaryInfo.partitionModelBefore->init( deviceBefore, m_osproberLines );
// Make deviceBefore a child of partitionModelBefore so that it is not
// leaked (as long as partitionModelBefore is deleted)
deviceBefore->setParent( summaryInfo.partitionModelBefore );
summaryInfo.partitionModelAfter = new PartitionModel;
summaryInfo.partitionModelAfter->init( deviceInfo->device.data(), m_osproberLines );
lst << summaryInfo;
}
return lst;
}