calamares/src/modules/partition/jobs/FillGlobalStorageJob.cpp

271 lines
8.7 KiB
C++
Raw Normal View History

/* === This file is part of Calamares - <https://github.com/calamares> ===
2014-07-22 17:32:26 +02:00
*
* Copyright 2014, Aurélien Gâteau <agateau@kde.org>
* Copyright 2015-2016, Teo Mrnjavac <teo@kde.org>
* Copyright 2017, 2019-2020, Adriaan de Groot <groot@kde.org>
2014-07-22 17:32:26 +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/>.
*/
#include "jobs/FillGlobalStorageJob.h"
2014-07-22 17:32:26 +02:00
#include "core/KPMHelpers.h"
#include "core/PartitionInfo.h"
#include "core/PartitionIterator.h"
#include "Branding.h"
#include "GlobalStorage.h"
#include "JobQueue.h"
2015-06-04 18:57:50 +02:00
#include "utils/Logger.h"
2014-07-22 17:32:26 +02:00
// KPMcore
#include <core/device.h>
#include <core/partition.h>
#include <fs/filesystem.h>
#include <fs/luks.h>
2014-07-22 17:32:26 +02:00
// Qt
#include <QDebug>
#include <QDir>
#include <QFileInfo>
2016-05-05 13:29:08 +02:00
#include <QProcess>
using KPMHelpers::untranslatedFS;
using KPMHelpers::userVisibleFS;
typedef QHash< QString, QString > UuidForPartitionHash;
static UuidForPartitionHash
findPartitionUuids( QList< Device* > devices )
{
UuidForPartitionHash hash;
foreach ( Device* device, devices )
{
for ( auto it = PartitionIterator::begin( device ); it != PartitionIterator::end( device ); ++it )
{
Partition* p = *it;
QString path = p->partitionPath();
QString uuid = p->fileSystem().readUUID( p->partitionPath() );
hash.insert( path, uuid );
}
}
if ( hash.isEmpty() )
{
2019-03-17 21:08:31 +01:00
cDebug() << "No UUIDs found for existing partitions.";
}
return hash;
}
2016-05-05 13:29:08 +02:00
static QString
getLuksUuid( const QString& path )
{
QProcess process;
process.setProgram( "cryptsetup" );
process.setArguments( { "luksUUID", path } );
process.start();
process.waitForFinished();
if ( process.exitStatus() != QProcess::NormalExit || process.exitCode() )
{
2016-05-05 13:29:08 +02:00
return QString();
}
2016-05-05 13:29:08 +02:00
QString uuid = QString::fromLocal8Bit( process.readAllStandardOutput() ).trimmed();
return uuid;
}
static QVariant
mapForPartition( Partition* partition, const QString& uuid )
{
QVariantMap map;
map[ "device" ] = partition->partitionPath();
map[ "mountPoint" ] = PartitionInfo::mountPoint( partition );
map[ "fsName" ] = userVisibleFS( partition->fileSystem() );
map[ "fs" ] = untranslatedFS( partition->fileSystem() );
if ( partition->fileSystem().type() == FileSystem::Luks
&& dynamic_cast< FS::luks& >( partition->fileSystem() ).innerFS() )
{
map[ "fs" ] = untranslatedFS( dynamic_cast< FS::luks& >( partition->fileSystem() ).innerFS() );
}
map[ "uuid" ] = uuid;
map[ "new" ] = partition->state() == Partition::StateNew;
// Debugging for inside the loop in createPartitionList(),
// so indent a bit
Logger::CDebug deb;
using TR = Logger::DebugRow< const char* const, const QString& >;
deb << Logger::SubEntry << "mapping for" << partition->partitionPath() << partition->deviceNode()
<< TR( "mtpoint:", PartitionInfo::mountPoint( partition ) ) << TR( "fs:", map[ "fs" ].toString() )
<< TR( "fsName", map[ "fsName" ].toString() ) << TR( "uuid", uuid );
if ( partition->roles().has( PartitionRole::Luks ) )
{
const FileSystem& fsRef = partition->fileSystem();
const FS::luks* luksFs = dynamic_cast< const FS::luks* >( &fsRef );
if ( luksFs )
{
2016-09-05 08:20:02 +02:00
map[ "luksMapperName" ] = luksFs->mapperName().split( "/" ).last();
2016-05-05 13:29:08 +02:00
map[ "luksUuid" ] = getLuksUuid( partition->partitionPath() );
2016-05-06 16:59:11 +02:00
map[ "luksPassphrase" ] = luksFs->passphrase();
deb << TR( "luksMapperName:", map[ "luksMapperName" ].toString() );
}
}
return map;
}
FillGlobalStorageJob::FillGlobalStorageJob( QList< Device* > devices, const QString& bootLoaderPath )
2014-07-22 17:32:26 +02:00
: m_devices( devices )
, m_bootLoaderPath( bootLoaderPath )
2014-07-22 17:32:26 +02:00
{
}
QString
FillGlobalStorageJob::prettyName() const
{
return tr( "Set partition information" );
}
QString
FillGlobalStorageJob::prettyDescription() const
{
QStringList lines;
const auto partitionList = createPartitionList().toList();
for ( const QVariant& partitionItem : partitionList )
{
if ( partitionItem.type() == QVariant::Map )
{
QVariantMap partitionMap = partitionItem.toMap();
QString path = partitionMap.value( "device" ).toString();
QString mountPoint = partitionMap.value( "mountPoint" ).toString();
QString fsType = partitionMap.value( "fs" ).toString();
if ( mountPoint.isEmpty() || fsType.isEmpty() )
{
continue;
}
if ( path.isEmpty() )
{
if ( mountPoint == "/" )
{
lines.append( tr( "Install %1 on <strong>new</strong> %2 system partition." )
.arg( *Calamares::Branding::ShortProductName )
.arg( fsType ) );
}
else
{
lines.append( tr( "Set up <strong>new</strong> %2 partition with mount point "
"<strong>%1</strong>." )
.arg( mountPoint )
.arg( fsType ) );
}
}
else
{
if ( mountPoint == "/" )
{
lines.append( tr( "Install %2 on %3 system partition <strong>%1</strong>." )
.arg( path )
.arg( *Calamares::Branding::ShortProductName )
.arg( fsType ) );
}
else
{
lines.append( tr( "Set up %3 partition <strong>%1</strong> with mount point "
"<strong>%2</strong>." )
.arg( path )
.arg( mountPoint )
.arg( fsType ) );
}
}
}
}
QVariant bootloaderMap = createBootLoaderMap();
if ( !m_bootLoaderPath.isEmpty() )
{
lines.append( tr( "Install boot loader on <strong>%1</strong>." ).arg( m_bootLoaderPath ) );
}
return lines.join( "<br/>" );
}
QString
FillGlobalStorageJob::prettyStatusMessage() const
{
return tr( "Setting up mount points." );
}
2014-07-22 17:32:26 +02:00
Calamares::JobResult
FillGlobalStorageJob::exec()
{
Calamares::GlobalStorage* storage = Calamares::JobQueue::instance()->globalStorage();
storage->insert( "partitions", createPartitionList() );
cDebug() << "Saving partition information map to GlobalStorage[\"partitions\"]";
if ( !m_bootLoaderPath.isEmpty() )
{
QVariant var = createBootLoaderMap();
if ( !var.isValid() )
{
cDebug() << "Failed to find path for boot loader";
}
cDebug() << "FillGlobalStorageJob writing bootLoader path:" << var;
storage->insert( "bootLoader", var );
}
else
{
cDebug() << "FillGlobalStorageJob writing empty bootLoader value";
storage->insert( "bootLoader", QVariant() );
}
return Calamares::JobResult::ok();
}
QVariant
FillGlobalStorageJob::createPartitionList() const
2014-07-22 17:32:26 +02:00
{
UuidForPartitionHash hash = findPartitionUuids( m_devices );
2014-07-22 17:32:26 +02:00
QVariantList lst;
cDebug() << "Building partition information map";
for ( auto device : m_devices )
2015-06-04 18:57:50 +02:00
{
cDebug() << Logger::SubEntry << "partitions on" << device->deviceNode();
for ( auto it = PartitionIterator::begin( device ); it != PartitionIterator::end( device ); ++it )
2015-06-04 18:57:50 +02:00
{
// Debug-logging is done when creating the map
lst << mapForPartition( *it, hash.value( ( *it )->partitionPath() ) );
2015-06-04 18:57:50 +02:00
}
}
return lst;
2014-07-22 17:32:26 +02:00
}
QVariant
FillGlobalStorageJob::createBootLoaderMap() const
2014-07-22 17:32:26 +02:00
{
QVariantMap map;
QString path = m_bootLoaderPath;
if ( !path.startsWith( "/dev/" ) )
{
Partition* partition = KPMHelpers::findPartitionByMountPoint( m_devices, path );
if ( !partition )
{
return QVariant();
}
path = partition->partitionPath();
}
map[ "installPath" ] = path;
2014-07-22 17:32:26 +02:00
return map;
}