2020-08-25 16:05:56 +02:00
|
|
|
/* === This file is part of Calamares - <https://calamares.io> ===
|
2014-10-27 16:53:57 +01:00
|
|
|
*
|
2020-08-22 01:19:58 +02:00
|
|
|
* SPDX-FileCopyrightText: 2014 Rohan Garg <rohan@kde.org>
|
|
|
|
* SPDX-FileCopyrightText: 2015 Teo Mrnjavac <teo@kde.org>
|
|
|
|
* SPDX-FileCopyrightText: 2018 Adriaan de Groot <groot@kde.org>
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2014-10-27 16:53:57 +01:00
|
|
|
*
|
2020-08-25 16:05:56 +02:00
|
|
|
* Calamares is Free Software: see the License-Identifier above.
|
2014-10-27 16:53:57 +01:00
|
|
|
*
|
2014-10-21 18:59:02 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SetHostNameJob.h"
|
|
|
|
|
|
|
|
#include "GlobalStorage.h"
|
|
|
|
#include "JobQueue.h"
|
2020-02-14 12:31:53 +01:00
|
|
|
#include "utils/CalamaresUtilsSystem.h"
|
2019-10-21 17:21:33 +02:00
|
|
|
#include "utils/Logger.h"
|
2014-10-21 18:59:02 +02:00
|
|
|
|
2020-08-05 10:29:13 +02:00
|
|
|
#include <QDBusConnection>
|
|
|
|
#include <QDBusInterface>
|
|
|
|
#include <QDBusReply>
|
2020-08-25 23:44:08 +02:00
|
|
|
#include <QDir>
|
|
|
|
#include <QFile>
|
2014-10-21 18:59:02 +02:00
|
|
|
|
2020-02-24 12:48:01 +01:00
|
|
|
using WriteMode = CalamaresUtils::System::WriteMode;
|
|
|
|
|
2020-08-05 10:29:13 +02:00
|
|
|
SetHostNameJob::SetHostNameJob( const QString& hostname, HostNameActions a )
|
2014-10-21 18:59:02 +02:00
|
|
|
: Calamares::Job()
|
2014-10-27 16:53:57 +01:00
|
|
|
, m_hostname( hostname )
|
2020-02-17 15:24:44 +01:00
|
|
|
, m_actions( a )
|
2014-10-21 18:59:02 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-10-21 17:21:33 +02:00
|
|
|
QString
|
|
|
|
SetHostNameJob::prettyName() const
|
2014-10-21 18:59:02 +02:00
|
|
|
{
|
2014-10-27 16:53:57 +01:00
|
|
|
return tr( "Set hostname %1" ).arg( m_hostname );
|
2014-10-21 18:59:02 +02:00
|
|
|
}
|
|
|
|
|
2015-06-13 02:22:03 +02:00
|
|
|
|
|
|
|
QString
|
|
|
|
SetHostNameJob::prettyDescription() const
|
|
|
|
{
|
|
|
|
return tr( "Set hostname <strong>%1</strong>." ).arg( m_hostname );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString
|
|
|
|
SetHostNameJob::prettyStatusMessage() const
|
|
|
|
{
|
|
|
|
return tr( "Setting hostname %1." ).arg( m_hostname );
|
|
|
|
}
|
|
|
|
|
2020-02-17 18:10:46 +01:00
|
|
|
STATICTEST bool
|
2020-02-17 10:42:54 +01:00
|
|
|
setFileHostname( const QString& hostname )
|
|
|
|
{
|
2020-02-24 12:48:01 +01:00
|
|
|
return CalamaresUtils::System::instance()->createTargetFile(
|
|
|
|
QStringLiteral( "/etc/hostname" ), ( hostname + '\n' ).toUtf8(), WriteMode::Overwrite );
|
2020-02-17 10:42:54 +01:00
|
|
|
}
|
|
|
|
|
2020-02-17 18:10:46 +01:00
|
|
|
STATICTEST bool
|
2020-02-17 10:42:54 +01:00
|
|
|
writeFileEtcHosts( const QString& hostname )
|
|
|
|
{
|
|
|
|
// The actual hostname gets substituted in at %1
|
|
|
|
static const char etc_hosts[] = R"(# Host addresses
|
|
|
|
127.0.0.1 localhost
|
|
|
|
127.0.1.1 %1
|
|
|
|
::1 localhost ip6-localhost ip6-loopback
|
|
|
|
ff02::1 ip6-allnodes
|
|
|
|
ff02::2 ip6-allrouters
|
|
|
|
)";
|
|
|
|
|
2020-02-24 12:48:01 +01:00
|
|
|
return CalamaresUtils::System::instance()->createTargetFile(
|
|
|
|
QStringLiteral( "/etc/hosts" ), QString( etc_hosts ).arg( hostname ).toUtf8(), WriteMode::Overwrite );
|
2020-02-17 10:42:54 +01:00
|
|
|
}
|
|
|
|
|
2020-02-18 10:16:19 +01:00
|
|
|
STATICTEST bool
|
2020-02-17 10:57:41 +01:00
|
|
|
setSystemdHostname( const QString& hostname )
|
|
|
|
{
|
|
|
|
QDBusInterface hostnamed( "org.freedesktop.hostname1",
|
|
|
|
"/org/freedesktop/hostname1",
|
|
|
|
"org.freedesktop.hostname1",
|
|
|
|
QDBusConnection::systemBus() );
|
2020-02-18 10:16:19 +01:00
|
|
|
if ( !hostnamed.isValid() )
|
|
|
|
{
|
|
|
|
cWarning() << "Interface" << hostnamed.interface() << "is not valid.";
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-17 10:57:41 +01:00
|
|
|
|
2020-02-18 10:16:19 +01:00
|
|
|
bool success = true;
|
2020-02-17 10:57:41 +01:00
|
|
|
// Static, writes /etc/hostname
|
|
|
|
{
|
2020-02-18 10:35:47 +01:00
|
|
|
QDBusReply< void > r = hostnamed.call( "SetStaticHostname", hostname, false );
|
2020-02-17 10:57:41 +01:00
|
|
|
if ( !r.isValid() )
|
|
|
|
{
|
|
|
|
cWarning() << "Could not set hostname through org.freedesktop.hostname1.SetStaticHostname." << r.error();
|
2020-02-18 10:16:19 +01:00
|
|
|
success = false;
|
2020-02-17 10:57:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Dynamic, updates kernel
|
|
|
|
{
|
2020-02-18 10:35:47 +01:00
|
|
|
QDBusReply< void > r = hostnamed.call( "SetHostname", hostname, false );
|
2020-02-17 10:57:41 +01:00
|
|
|
if ( !r.isValid() )
|
|
|
|
{
|
|
|
|
cWarning() << "Could not set hostname through org.freedesktop.hostname1.SetHostname." << r.error();
|
2020-02-18 10:16:19 +01:00
|
|
|
success = false;
|
2020-02-17 10:57:41 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-18 10:16:19 +01:00
|
|
|
|
|
|
|
return success;
|
2020-02-17 10:57:41 +01:00
|
|
|
}
|
|
|
|
|
2020-02-17 10:42:54 +01:00
|
|
|
|
2019-10-21 17:21:33 +02:00
|
|
|
Calamares::JobResult
|
|
|
|
SetHostNameJob::exec()
|
2014-10-21 18:59:02 +02:00
|
|
|
{
|
|
|
|
Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
|
2014-10-27 16:53:57 +01:00
|
|
|
|
2014-10-21 18:59:02 +02:00
|
|
|
if ( !gs || !gs->contains( "rootMountPoint" ) )
|
|
|
|
{
|
2018-03-28 15:31:45 +02:00
|
|
|
cError() << "No rootMountPoint in global storage";
|
2014-10-21 18:59:02 +02:00
|
|
|
return Calamares::JobResult::error( tr( "Internal Error" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
QString destDir = gs->value( "rootMountPoint" ).toString();
|
|
|
|
if ( !QDir( destDir ).exists() )
|
|
|
|
{
|
2018-03-28 15:31:45 +02:00
|
|
|
cError() << "rootMountPoint points to a dir which does not exist";
|
2014-10-21 18:59:02 +02:00
|
|
|
return Calamares::JobResult::error( tr( "Internal Error" ) );
|
|
|
|
}
|
|
|
|
|
2020-08-05 10:29:13 +02:00
|
|
|
if ( m_actions & HostNameAction::EtcHostname )
|
2014-10-21 18:59:02 +02:00
|
|
|
{
|
2020-02-17 15:24:44 +01:00
|
|
|
if ( !setFileHostname( m_hostname ) )
|
|
|
|
{
|
|
|
|
cError() << "Can't write to hostname file";
|
|
|
|
return Calamares::JobResult::error( tr( "Cannot write hostname to target system" ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-05 10:29:13 +02:00
|
|
|
if ( m_actions & HostNameAction::WriteEtcHosts )
|
2020-02-17 15:24:44 +01:00
|
|
|
{
|
|
|
|
if ( !writeFileEtcHosts( m_hostname ) )
|
|
|
|
{
|
|
|
|
cError() << "Can't write to hosts file";
|
|
|
|
return Calamares::JobResult::error( tr( "Cannot write hostname to target system" ) );
|
|
|
|
}
|
2014-10-21 18:59:02 +02:00
|
|
|
}
|
2020-02-14 12:31:53 +01:00
|
|
|
|
2020-08-05 10:29:13 +02:00
|
|
|
if ( m_actions & HostNameAction::SystemdHostname )
|
2014-11-07 15:18:45 +01:00
|
|
|
{
|
2020-02-17 15:24:44 +01:00
|
|
|
// Does its own logging
|
|
|
|
setSystemdHostname( m_hostname );
|
2014-11-07 15:18:45 +01:00
|
|
|
}
|
|
|
|
|
2014-10-21 18:59:02 +02:00
|
|
|
return Calamares::JobResult::ok();
|
|
|
|
}
|