Merge pull request #2252 from calamares/issue-2225

Add support for systemd-machine-id variations
This commit is contained in:
Adriaan de Groot 2023-12-11 22:18:03 +01:00 committed by GitHub
commit 558f045b65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 78 additions and 17 deletions

View File

@ -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 );

View File

@ -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" ) )
{

View File

@ -10,16 +10,16 @@
#ifndef MACHINEIDJOB_H
#define MACHINEIDJOB_H
#include "Workers.h"
#include "CppJob.h"
#include "DllMacro.h"
#include "utils/PluginFactory.h"
#include <QObject>
#include <QStringList>
#include <QVariantMap>
#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::Uuid;
bool m_dbus = false; ///< write dbus files
bool m_dbus_symlink = false; ///< .. or just symlink to systemd

View File

@ -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

View File

@ -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

View File

@ -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*.

View File

@ -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 }