From 083b0fb1e5112a3b94eef91beb5ae1879b0014e8 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 30 Oct 2023 23:25:21 +0100 Subject: [PATCH 1/9] machineid: add configuration option for machine-id SEE #2225 --- src/modules/machineid/machineid.conf | 5 +++++ src/modules/machineid/machineid.schema.yaml | 1 + 2 files changed, 6 insertions(+) diff --git a/src/modules/machineid/machineid.conf b/src/modules/machineid/machineid.conf index 15e190299..15461e3e4 100644 --- a/src/modules/machineid/machineid.conf +++ b/src/modules/machineid/machineid.conf @@ -13,6 +13,11 @@ # Whether to create /etc/machine-id for systemd. # The default is *false*. systemd: true +# If systemd is true, the kind of /etc/machine-id to create in the target +# - uuid (default) generates a UUID +# - blank creates the file but leaves it empty at 0 bytes +# - literal-uninitialized creates the file and writes the string "uninitialized\n" +systemd-style: uuid # Whether to create /var/lib/dbus/machine-id for D-Bus. # The default is *false*. diff --git a/src/modules/machineid/machineid.schema.yaml b/src/modules/machineid/machineid.schema.yaml index 59bb5f81b..f56b39018 100644 --- a/src/modules/machineid/machineid.schema.yaml +++ b/src/modules/machineid/machineid.schema.yaml @@ -7,6 +7,7 @@ additionalProperties: false type: object properties: systemd: { type: boolean, default: false } + "systemd-style": { type: string, enum: [ uuid, blank, literal-uninitialized ] } dbus: { type: boolean, default: false } "dbus-symlink": { type: boolean, default: false } "entropy-copy": { type: boolean, default: false } From 44d12379bd21049276237cc79ec37180d8a38cef Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Thu, 2 Nov 2023 21:22:35 +0100 Subject: [PATCH 2/9] machineid: pass around enum for style --- src/modules/machineid/MachineIdJob.cpp | 2 +- src/modules/machineid/MachineIdJob.h | 14 ++++++++------ src/modules/machineid/Workers.cpp | 3 ++- src/modules/machineid/Workers.h | 11 ++++++++++- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/modules/machineid/MachineIdJob.cpp b/src/modules/machineid/MachineIdJob.cpp index 41a216dd6..da0afca93 100644 --- a/src/modules/machineid/MachineIdJob.cpp +++ b/src/modules/machineid/MachineIdJob.cpp @@ -96,7 +96,7 @@ MachineIdJob::exec() { cWarning() << "Could not create systemd data-directory."; } - auto r = MachineId::createSystemdMachineId( root, target_systemd_machineid_file ); + auto r = MachineId::createSystemdMachineId( m_systemd_style, root, target_systemd_machineid_file ); if ( !r ) { return r; diff --git a/src/modules/machineid/MachineIdJob.h b/src/modules/machineid/MachineIdJob.h index 7f406fc55..52983f235 100644 --- a/src/modules/machineid/MachineIdJob.h +++ b/src/modules/machineid/MachineIdJob.h @@ -10,16 +10,16 @@ #ifndef MACHINEIDJOB_H #define MACHINEIDJOB_H +#include "Workers.h" + +#include "CppJob.h" +#include "DllMacro.h" +#include "utils/PluginFactory.h" + #include #include #include -#include "CppJob.h" - -#include "utils/PluginFactory.h" - -#include "DllMacro.h" - /** @brief Write 'random' data: machine id, entropy, UUIDs * */ @@ -48,6 +48,8 @@ public: private: bool m_systemd = false; ///< write systemd's files + MachineId::SystemdMachineIdStyle m_systemd_style = MachineId::SystemdMachineIdStyle::Blank; + bool m_dbus = false; ///< write dbus files bool m_dbus_symlink = false; ///< .. or just symlink to systemd diff --git a/src/modules/machineid/Workers.cpp b/src/modules/machineid/Workers.cpp index 6fe631f2e..497ff943a 100644 --- a/src/modules/machineid/Workers.cpp +++ b/src/modules/machineid/Workers.cpp @@ -153,8 +153,9 @@ runCmd( const QStringList& cmd ) } Calamares::JobResult -createSystemdMachineId( const QString& rootMountPoint, const QString& fileName ) +createSystemdMachineId( SystemdMachineIdStyle style, const QString& rootMountPoint, const QString& fileName ) { + Q_UNUSED( style ) Q_UNUSED( rootMountPoint ) Q_UNUSED( fileName ) return runCmd( QStringList { QStringLiteral( "systemd-machine-id-setup" ) } ); diff --git a/src/modules/machineid/Workers.h b/src/modules/machineid/Workers.h index 51c9d526d..577090a46 100644 --- a/src/modules/machineid/Workers.h +++ b/src/modules/machineid/Workers.h @@ -52,6 +52,7 @@ createEntropy( const EntropyGeneration kind, const QString& rootMountPoint, cons * Creating UUIDs for DBUS and SystemD. */ + /// @brief Create a new DBus UUID file Calamares::JobResult createDBusMachineId( const QString& rootMountPoint, const QString& fileName ); @@ -59,7 +60,15 @@ Calamares::JobResult createDBusMachineId( const QString& rootMountPoint, const Q Calamares::JobResult createDBusLink( const QString& rootMountPoint, const QString& fileName, const QString& systemdFileName ); -Calamares::JobResult createSystemdMachineId( const QString& rootMountPoint, const QString& fileName ); +enum class SystemdMachineIdStyle +{ + Uuid, + Blank, + Uninitialized +}; + +Calamares::JobResult +createSystemdMachineId( SystemdMachineIdStyle style, const QString& rootMountPoint, const QString& fileName ); } // namespace MachineId From 0d11de45259aebb6292e4e87215fb123c3dbac6a Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 8 Dec 2023 22:04:46 +0100 Subject: [PATCH 3/9] [libcalamares] Add missing parameter name --- src/libcalamares/Job.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcalamares/Job.h b/src/libcalamares/Job.h index dc89f1c49..241b2883c 100644 --- a/src/libcalamares/Job.h +++ b/src/libcalamares/Job.h @@ -76,7 +76,7 @@ public: * Pass in a suitable error code; using 0 (which would normally mean "ok") instead * gives you a GenericError code. */ - static JobResult internalError( const QString&, const QString& details, int errorCode ); + static JobResult internalError( const QString& message, const QString& details, int errorCode ); protected: explicit JobResult( const QString& message, const QString& details, int errorCode ); From 0eb387d6de429848b2be8cbda1a75add09f7ccef Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 8 Dec 2023 22:26:32 +0100 Subject: [PATCH 4/9] [machineid] Default to running systemd-machine-id --- src/modules/machineid/MachineIdJob.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/machineid/MachineIdJob.h b/src/modules/machineid/MachineIdJob.h index 52983f235..b564b3708 100644 --- a/src/modules/machineid/MachineIdJob.h +++ b/src/modules/machineid/MachineIdJob.h @@ -48,7 +48,7 @@ public: private: bool m_systemd = false; ///< write systemd's files - MachineId::SystemdMachineIdStyle m_systemd_style = MachineId::SystemdMachineIdStyle::Blank; + MachineId::SystemdMachineIdStyle m_systemd_style = MachineId::SystemdMachineIdStyle::Uuid; bool m_dbus = false; ///< write dbus files bool m_dbus_symlink = false; ///< .. or just symlink to systemd From e5ee28329d705fcfb43369d09c3ac40c8d9a21b1 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 8 Dec 2023 22:43:00 +0100 Subject: [PATCH 5/9] [machineid] Handle different settings of systemd-style --- src/modules/machineid/Workers.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/modules/machineid/Workers.cpp b/src/modules/machineid/Workers.cpp index 497ff943a..31e0e8030 100644 --- a/src/modules/machineid/Workers.cpp +++ b/src/modules/machineid/Workers.cpp @@ -155,10 +155,23 @@ runCmd( const QStringList& cmd ) Calamares::JobResult createSystemdMachineId( SystemdMachineIdStyle style, const QString& rootMountPoint, const QString& fileName ) { - Q_UNUSED( style ) Q_UNUSED( rootMountPoint ) Q_UNUSED( fileName ) - return runCmd( QStringList { QStringLiteral( "systemd-machine-id-setup" ) } ); + const QString machineIdFile = QStringLiteral("/etc/machine-id"); + + switch(style) + { + case SystemdMachineIdStyle::Uuid: + return runCmd( QStringList { QStringLiteral( "systemd-machine-id-setup" ) } ); + case SystemdMachineIdStyle::Blank: + Calamares::System::instance()->createTargetFile(machineIdFile, QByteArray(), Calamares::System::WriteMode::Overwrite); + return Calamares::JobResult::ok(); + case SystemdMachineIdStyle::Uninitialized: + Calamares::System::instance()->createTargetFile(machineIdFile, "uninitialized\n", Calamares::System::WriteMode::Overwrite); + return Calamares::JobResult::ok(); + + } + return Calamares::JobResult::internalError(QStringLiteral("Invalid systemd-style"), QStringLiteral("Invalid value %1").arg(int(style)), Calamares::JobResult::InvalidConfiguration); } Calamares::JobResult From e04e0260c914fe58ffa8576caece805c9b3def37 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 8 Dec 2023 23:15:32 +0100 Subject: [PATCH 6/9] [machineid] Apply coding style --- src/modules/machineid/Workers.cpp | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/modules/machineid/Workers.cpp b/src/modules/machineid/Workers.cpp index 31e0e8030..f55c19e0f 100644 --- a/src/modules/machineid/Workers.cpp +++ b/src/modules/machineid/Workers.cpp @@ -157,21 +157,24 @@ createSystemdMachineId( SystemdMachineIdStyle style, const QString& rootMountPoi { Q_UNUSED( rootMountPoint ) Q_UNUSED( fileName ) - const QString machineIdFile = QStringLiteral("/etc/machine-id"); + const QString machineIdFile = QStringLiteral( "/etc/machine-id" ); - switch(style) + switch ( style ) { - case SystemdMachineIdStyle::Uuid: - return runCmd( QStringList { QStringLiteral( "systemd-machine-id-setup" ) } ); - case SystemdMachineIdStyle::Blank: - Calamares::System::instance()->createTargetFile(machineIdFile, QByteArray(), Calamares::System::WriteMode::Overwrite); - return Calamares::JobResult::ok(); - case SystemdMachineIdStyle::Uninitialized: - Calamares::System::instance()->createTargetFile(machineIdFile, "uninitialized\n", Calamares::System::WriteMode::Overwrite); - return Calamares::JobResult::ok(); - + case SystemdMachineIdStyle::Uuid: + return runCmd( QStringList { QStringLiteral( "systemd-machine-id-setup" ) } ); + case SystemdMachineIdStyle::Blank: + Calamares::System::instance()->createTargetFile( + machineIdFile, QByteArray(), Calamares::System::WriteMode::Overwrite ); + return Calamares::JobResult::ok(); + case SystemdMachineIdStyle::Uninitialized: + Calamares::System::instance()->createTargetFile( + machineIdFile, "uninitialized\n", Calamares::System::WriteMode::Overwrite ); + return Calamares::JobResult::ok(); } - return Calamares::JobResult::internalError(QStringLiteral("Invalid systemd-style"), QStringLiteral("Invalid value %1").arg(int(style)), Calamares::JobResult::InvalidConfiguration); + return Calamares::JobResult::internalError( QStringLiteral( "Invalid systemd-style" ), + QStringLiteral( "Invalid value %1" ).arg( int( style ) ), + Calamares::JobResult::InvalidConfiguration ); } Calamares::JobResult From 2f740564c6001cb5ecb30a09ee497eccb1db5679 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 8 Dec 2023 23:37:48 +0100 Subject: [PATCH 7/9] [machineid] Run systemd-machine-id in host, telling it to modify target --- src/modules/machineid/Workers.cpp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/modules/machineid/Workers.cpp b/src/modules/machineid/Workers.cpp index f55c19e0f..1dd2321cf 100644 --- a/src/modules/machineid/Workers.cpp +++ b/src/modules/machineid/Workers.cpp @@ -141,9 +141,10 @@ createEntropy( const EntropyGeneration kind, const QString& rootMountPoint, cons } static Calamares::JobResult -runCmd( const QStringList& cmd ) +runCmd( const QStringList& cmd, bool inTarget ) { - auto r = Calamares::System::instance()->targetEnvCommand( cmd ); + auto r = inTarget ? Calamares::System::instance()->targetEnvCommand( cmd ) + : Calamares::System::instance()->runCommand( cmd, std::chrono::seconds( 0 ) ); if ( r.getExitCode() ) { return r.explainProcess( cmd, std::chrono::seconds( 0 ) ); @@ -153,16 +154,14 @@ runCmd( const QStringList& cmd ) } Calamares::JobResult -createSystemdMachineId( SystemdMachineIdStyle style, const QString& rootMountPoint, const QString& fileName ) +createSystemdMachineId( SystemdMachineIdStyle style, const QString& rootMountPoint, const QString& machineIdFile ) { - Q_UNUSED( rootMountPoint ) - Q_UNUSED( fileName ) - const QString machineIdFile = QStringLiteral( "/etc/machine-id" ); - switch ( style ) { case SystemdMachineIdStyle::Uuid: - return runCmd( QStringList { QStringLiteral( "systemd-machine-id-setup" ) } ); + return runCmd( + QStringList { QStringLiteral( "systemd-machine-id-setup" ), QStringLiteral( "--root=" ) + rootMountPoint }, + false ); case SystemdMachineIdStyle::Blank: Calamares::System::instance()->createTargetFile( machineIdFile, QByteArray(), Calamares::System::WriteMode::Overwrite ); @@ -182,14 +181,14 @@ createDBusMachineId( const QString& rootMountPoint, const QString& fileName ) { Q_UNUSED( rootMountPoint ) Q_UNUSED( fileName ) - return runCmd( QStringList { QStringLiteral( "dbus-uuidgen" ), QStringLiteral( "--ensure" ) } ); + return runCmd( QStringList { QStringLiteral( "dbus-uuidgen" ), QStringLiteral( "--ensure" ) }, true ); } Calamares::JobResult createDBusLink( const QString& rootMountPoint, const QString& fileName, const QString& systemdFileName ) { Q_UNUSED( rootMountPoint ) - return runCmd( QStringList { QStringLiteral( "ln" ), QStringLiteral( "-sf" ), systemdFileName, fileName } ); + return runCmd( QStringList { QStringLiteral( "ln" ), QStringLiteral( "-sf" ), systemdFileName, fileName }, true ); } } // namespace MachineId From 89348910c362d03cffbf37343062155989e70956 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Sat, 9 Dec 2023 00:08:19 +0100 Subject: [PATCH 8/9] [machineid] Document aliases (not visible in schema) --- src/modules/machineid/machineid.conf | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/modules/machineid/machineid.conf b/src/modules/machineid/machineid.conf index 15461e3e4..6a45234cf 100644 --- a/src/modules/machineid/machineid.conf +++ b/src/modules/machineid/machineid.conf @@ -15,7 +15,9 @@ systemd: true # If systemd is true, the kind of /etc/machine-id to create in the target # - uuid (default) generates a UUID +# - systemd alias of uuid # - blank creates the file but leaves it empty at 0 bytes +# - none alias of blank (use `systemd: false` if you don't want one at all) # - literal-uninitialized creates the file and writes the string "uninitialized\n" systemd-style: uuid From 1b37eb1262c5d0d6604be6039e492e78aac01450 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Sat, 9 Dec 2023 00:08:38 +0100 Subject: [PATCH 9/9] [machineid] Read systemd-style from config --- src/modules/machineid/MachineIdJob.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/modules/machineid/MachineIdJob.cpp b/src/modules/machineid/MachineIdJob.cpp index da0afca93..9bc1b3f6f 100644 --- a/src/modules/machineid/MachineIdJob.cpp +++ b/src/modules/machineid/MachineIdJob.cpp @@ -14,6 +14,7 @@ #include "Workers.h" #include "utils/Logger.h" +#include "utils/NamedEnum.h" #include "utils/System.h" #include "utils/Variant.h" @@ -22,6 +23,25 @@ #include +const NamedEnumTable< MachineId::SystemdMachineIdStyle >& +styleNames() +{ + using T = MachineId::SystemdMachineIdStyle; + // *INDENT-OFF* + // clang-format off + static const NamedEnumTable< MachineId::SystemdMachineIdStyle > names { + { QStringLiteral( "none" ), T::Blank }, + { QStringLiteral( "blank" ), T::Blank }, + { QStringLiteral( "uuid" ), T::Uuid }, + { QStringLiteral( "systemd" ), T::Uuid }, + { QStringLiteral( "literal-uninitialized" ), T::Uninitialized }, + }; + // clang-format on + // *INDENT-ON* + + return names; +} + MachineIdJob::MachineIdJob( QObject* parent ) : Calamares::CppJob( parent ) { @@ -134,6 +154,12 @@ MachineIdJob::setConfigurationMap( const QVariantMap& map ) { m_systemd = Calamares::getBool( map, "systemd", false ); + const auto style = Calamares::getString( map, "systemd-style", QString() ); + if ( !style.isEmpty() ) + { + m_systemd_style = styleNames().find( style, MachineId::SystemdMachineIdStyle::Uuid ); + } + m_dbus = Calamares::getBool( map, "dbus", false ); if ( map.contains( "dbus-symlink" ) ) {