From 2c20a00cc366a6e4a3c9ca953bbec8ec12ed988d Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 6 Dec 2021 19:49:48 +0100 Subject: [PATCH] [luksopenswaphookcfg] Read GS for finding LUKS config --- src/modules/luksopenswaphookcfg/LOSHInfo.h | 3 +- src/modules/luksopenswaphookcfg/LOSHJob.cpp | 59 +++++++++++++++++++++ src/modules/luksopenswaphookcfg/Tests.cpp | 51 ++++++++++++++---- 3 files changed, 101 insertions(+), 12 deletions(-) diff --git a/src/modules/luksopenswaphookcfg/LOSHInfo.h b/src/modules/luksopenswaphookcfg/LOSHInfo.h index 470b5a0cc..1a87f4e61 100644 --- a/src/modules/luksopenswaphookcfg/LOSHInfo.h +++ b/src/modules/luksopenswaphookcfg/LOSHInfo.h @@ -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 diff --git a/src/modules/luksopenswaphookcfg/LOSHJob.cpp b/src/modules/luksopenswaphookcfg/LOSHJob.cpp index a5e5a183a..916ffcb8b 100644 --- a/src/modules/luksopenswaphookcfg/LOSHJob.cpp +++ b/src/modules/luksopenswaphookcfg/LOSHJob.cpp @@ -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 @@ -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 >(); ) diff --git a/src/modules/luksopenswaphookcfg/Tests.cpp b/src/modules/luksopenswaphookcfg/Tests.cpp index 0cb79d7b9..7ec623eaa 100644 --- a/src/modules/luksopenswaphookcfg/Tests.cpp +++ b/src/modules/luksopenswaphookcfg/Tests.cpp @@ -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(); + } }