diff --git a/src/libcalamares/Job.h b/src/libcalamares/Job.h index dc89f1c495a607383f38c0cccc06ce027adf627e..241b2883cd17adcd13c468a0bb6c3b8ebd71b2c2 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 ); diff --git a/src/modules/machineid/MachineIdJob.cpp b/src/modules/machineid/MachineIdJob.cpp index 41a216dd6f08d4491b780a13ab7afa5313a8a6e5..9bc1b3f6f343213e98083f8a8e66cd28ba713a4d 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 <QFile> +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 ) { @@ -96,7 +116,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; @@ -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" ) ) { diff --git a/src/modules/machineid/MachineIdJob.h b/src/modules/machineid/MachineIdJob.h index 7f406fc5537ac30a1b98f254142a99fae025b437..b564b370802bcd82415dbc32ccf249a966100205 100644 --- a/src/modules/machineid/MachineIdJob.h +++ b/src/modules/machineid/MachineIdJob.h @@ -10,15 +10,15 @@ #ifndef MACHINEIDJOB_H #define MACHINEIDJOB_H -#include <QObject> -#include <QStringList> -#include <QVariantMap> +#include "Workers.h" #include "CppJob.h" - +#include "DllMacro.h" #include "utils/PluginFactory.h" -#include "DllMacro.h" +#include <QObject> +#include <QStringList> +#include <QVariantMap> /** @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::Uuid; + 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 6fe631f2ecb1658c439fcae03d85d45c172e8f32..1dd2321cf66546d4666eb19657b7976f4b023b98 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,11 +154,26 @@ runCmd( const QStringList& cmd ) } Calamares::JobResult -createSystemdMachineId( const QString& rootMountPoint, const QString& fileName ) +createSystemdMachineId( SystemdMachineIdStyle style, const QString& rootMountPoint, const QString& machineIdFile ) { - Q_UNUSED( rootMountPoint ) - Q_UNUSED( fileName ) - return runCmd( QStringList { QStringLiteral( "systemd-machine-id-setup" ) } ); + switch ( style ) + { + case SystemdMachineIdStyle::Uuid: + 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 ); + 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 @@ -165,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 diff --git a/src/modules/machineid/Workers.h b/src/modules/machineid/Workers.h index 51c9d526de95f5fffe5bd3b0b56c84c8c86ef1ba..577090a4610578dbf0511d764fe5235c8bc78901 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 diff --git a/src/modules/machineid/machineid.conf b/src/modules/machineid/machineid.conf index 15e190299c5c595ec1cafc10a21d54d309a6a91a..6a45234cfc1f421158a0f5272f257b0903d02b68 100644 --- a/src/modules/machineid/machineid.conf +++ b/src/modules/machineid/machineid.conf @@ -13,6 +13,13 @@ # 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 +# - 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 # 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 59bb5f81b7e65094a68c85f8f9e30ac5e6daaafc..f56b390182b34ca7edaefcc0a6c1ebd3f342454b 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 }