[luksopenswaphookcfg] Read GS for finding LUKS config

This commit is contained in:
Adriaan de Groot 2021-12-06 19:49:48 +01:00
parent 45d6eb36fb
commit 2c20a00cc3
3 changed files with 101 additions and 12 deletions

View File

@ -59,9 +59,8 @@ struct LOSHInfo
/** @brief Creates a struct from information already set in GS
*
* TODO: implement this in LOSHJob.cpp
*/
static LOSHInfo fromGlobalStorage() { return LOSHInfo {}; }
static LOSHInfo fromGlobalStorage();
};
#endif

View File

@ -14,6 +14,7 @@
#include "utils/Logger.h"
#include "utils/Permissions.h"
#include "utils/PluginFactory.h"
#include "utils/String.h"
#include "utils/Variant.h"
#include <QList>
@ -118,4 +119,62 @@ LOSHJob::setConfigurationMap( const QVariantMap& configurationMap )
configurationMap, QStringLiteral( "configFilePath" ), QStringLiteral( "/etc/openswap.conf" ) );
}
STATICTEST void
globalStoragePartitionInfo( Calamares::GlobalStorage* gs, LOSHInfo& info )
{
if ( !gs )
{
return;
}
QVariantList l = gs->value( "partitions" ).toList();
if ( l.isEmpty() )
{
return;
}
for ( const auto& pv : l )
{
const QVariantMap partition = pv.toMap();
if ( !partition.isEmpty() )
{
QString mountPoint = partition.value( "mountPoint" ).toString();
QString fileSystem = partition.value( "fs" ).toString();
QString luksMapperName = partition.value( "luksMapperName" ).toString();
// if partition["fs"] == "linuxswap" and "luksMapperName" in partition:
if ( fileSystem == QStringLiteral( "linuxswap" ) && !luksMapperName.isEmpty() )
{
info.swap_outer_uuid = partition.value( "luksUuid" ).toString();
info.swap_mapper_name = luksMapperName;
}
else if ( mountPoint == QStringLiteral( "/" ) && !luksMapperName.isEmpty() )
{
info.mountable_keyfile_device = QStringLiteral( "/dev/mapper/" ) + luksMapperName;
}
}
}
if ( !info.mountable_keyfile_device.isEmpty() && !info.swap_outer_uuid.isEmpty() )
{
info.swap_device_path = QStringLiteral( "/dev/disk/by-uuid/" ) + info.swap_outer_uuid;
}
QString btrfsRootSubvolume = gs->value( "btrfsRootSubvolume" ).toString();
if ( !btrfsRootSubvolume.isEmpty() )
{
CalamaresUtils::removeLeading( btrfsRootSubvolume, '/' );
info.keyfile_device_mount_options
= QStringLiteral( "keyfile_device_mount_options=--options=subvol=" ) + btrfsRootSubvolume;
}
}
LOSHInfo
LOSHInfo::fromGlobalStorage()
{
LOSHInfo i {};
globalStoragePartitionInfo(
Calamares::JobQueue::instance() ? Calamares::JobQueue::instance()->globalStorage() : nullptr, i );
return i;
}
CALAMARES_PLUGIN_FACTORY_DEFINITION( LOSHJobFactory, registerPlugin< LOSHJob >(); )

View File

@ -140,7 +140,7 @@ LOSHTests::testConfigWriting()
QVERIFY( ss );
QVERIFY( Calamares::JobQueue::instance()->globalStorage() );
QVERIFY( QFile::exists( tempRoot.path() ) );
QVERIFY( QFileInfo(tempRoot.path()).isDir() );
QVERIFY( QFileInfo( tempRoot.path() ).isDir() );
const QString targetFilePath = QStringLiteral( "losh.conf" );
const QString filePath = tempRoot.filePath( targetFilePath );
@ -159,12 +159,12 @@ LOSHTests::testConfigWriting()
QCOMPARE( contents.at( 1 ).left( 4 ), QStringLiteral( "# s" ) );
// Can we write there at all?
QFile derp(filePath);
QVERIFY(derp.open(QIODevice::WriteOnly));
QVERIFY(derp.write("xx", 2));
QFile derp( filePath );
QVERIFY( derp.open( QIODevice::WriteOnly ) );
QVERIFY( derp.write( "xx", 2 ) );
derp.close();
QVERIFY(QFile::exists(filePath));
QVERIFY(QFile::remove(filePath));
QVERIFY( QFile::exists( filePath ) );
QVERIFY( QFile::remove( filePath ) );
// Once the information is valid, though, the file is written
make_valid_loshinfo( i );
@ -194,18 +194,49 @@ LOSHTests::testConfigWriting()
}
void LOSHTests::testJob()
void
LOSHTests::testJob()
{
QTemporaryDir tempRoot( QDir::tempPath() + QStringLiteral( "/test-job-XXXXXX" ) );
QVERIFY( tempRoot.isValid() );
auto* ss = file_setup( tempRoot );
QVERIFY( ss );
Calamares::GlobalStorage* gs
= Calamares::JobQueue::instance() ? Calamares::JobQueue::instance()->globalStorage() : nullptr;
QVERIFY( gs );
{
QDir d( tempRoot.path() );
d.mkdir( "etc" );
}
QVERIFY( !LOSHInfo::fromGlobalStorage().isValid() );
QVariantList outerPartition;
QVariantMap innerPartition;
innerPartition.insert( "mountPoint", "/" );
innerPartition.insert( "fs", "ext4" );
innerPartition.insert( "luksMapperName", "root" );
innerPartition.insert( "luksUUID", "0000" );
outerPartition.append( innerPartition );
innerPartition.remove( "mountPoint" );
innerPartition.insert( "fs", "linuxswap" );
innerPartition.insert( "luksMapperName", "swap" );
innerPartition.insert( "luksUuid", "0001" );
outerPartition.append( innerPartition );
gs->insert( "partitions", outerPartition );
QVERIFY( LOSHInfo::fromGlobalStorage().isValid() );
LOSHJob j;
j.setConfigurationMap(QVariantMap());
j.setConfigurationMap( QVariantMap() );
auto jobresult = j.exec();
QVERIFY(jobresult);
QVERIFY( QFile::exists( tempRoot.filePath( "etc/openswap.conf" ) ) );
QVERIFY( jobresult );
{
QFile f( tempRoot.filePath( "etc/openswap.conf" ) );
QVERIFY( f.exists() );
QVERIFY( f.open( QIODevice::ReadOnly ) );
cDebug() << f.readAll();
}
}