2015-06-13 02:22:03 +02:00
|
|
|
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
2014-10-27 16:53:57 +01:00
|
|
|
*
|
2015-06-13 02:22:03 +02:00
|
|
|
* Copyright 2014, Rohan Garg <rohan@kde.org>
|
|
|
|
* Copyright 2015, Teo Mrnjavac <teo@kde.org>
|
2014-10-27 16:53:57 +01:00
|
|
|
*
|
2015-06-13 02:22:03 +02:00
|
|
|
* Calamares is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2014-10-27 16:53:57 +01:00
|
|
|
*
|
2015-06-13 02:22:03 +02:00
|
|
|
* Calamares is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2014-10-27 16:53:57 +01:00
|
|
|
*
|
2015-06-13 02:22:03 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
2014-10-21 18:59:02 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SetHostNameJob.h"
|
|
|
|
|
|
|
|
#include "GlobalStorage.h"
|
|
|
|
#include "utils/Logger.h"
|
|
|
|
#include "JobQueue.h"
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QDir>
|
|
|
|
|
2014-10-27 16:53:57 +01:00
|
|
|
SetHostNameJob::SetHostNameJob( const QString& hostname )
|
2014-10-21 18:59:02 +02:00
|
|
|
: Calamares::Job()
|
2014-10-27 16:53:57 +01:00
|
|
|
, m_hostname( hostname )
|
2014-10-21 18:59:02 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SetHostNameJob::prettyName() const
|
|
|
|
{
|
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 );
|
|
|
|
}
|
|
|
|
|
2014-10-21 18:59:02 +02:00
|
|
|
Calamares::JobResult SetHostNameJob::exec()
|
|
|
|
{
|
|
|
|
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" ) )
|
|
|
|
{
|
|
|
|
cLog() << "No rootMountPoint in global storage";
|
|
|
|
return Calamares::JobResult::error( tr( "Internal Error" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
QString destDir = gs->value( "rootMountPoint" ).toString();
|
|
|
|
if ( !QDir( destDir ).exists() )
|
|
|
|
{
|
|
|
|
cLog() << "rootMountPoint points to a dir which does not exist";
|
|
|
|
return Calamares::JobResult::error( tr( "Internal Error" ) );
|
|
|
|
}
|
|
|
|
|
2014-10-27 16:53:57 +01:00
|
|
|
QFile hostfile( destDir + "/etc/hostname" );
|
|
|
|
if ( !hostfile.open( QFile::WriteOnly ) )
|
2014-10-21 18:59:02 +02:00
|
|
|
{
|
|
|
|
cLog() << "Can't write to hostname file";
|
|
|
|
return Calamares::JobResult::error( tr( "Cannot write hostname to target system" ) );
|
|
|
|
}
|
|
|
|
|
2014-11-07 16:06:14 +01:00
|
|
|
QTextStream hostfileout( &hostfile );
|
|
|
|
hostfileout << m_hostname << "\n";
|
2014-10-21 18:59:02 +02:00
|
|
|
hostfile.close();
|
2014-10-27 16:53:57 +01:00
|
|
|
|
2014-11-07 15:18:45 +01:00
|
|
|
QFile hostsfile( destDir + "/etc/hosts" );
|
2014-11-13 17:07:48 +01:00
|
|
|
if ( !hostsfile.open( QFile::WriteOnly ) )
|
2014-11-07 15:18:45 +01:00
|
|
|
{
|
|
|
|
cLog() << "Can't write to hosts file";
|
|
|
|
return Calamares::JobResult::error( tr( "Cannot write hostname to target system" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// We also need to write the appropriate entries for /etc/hosts
|
2014-11-07 16:06:14 +01:00
|
|
|
QTextStream hostsfileout( &hostsfile );
|
2014-11-07 15:18:45 +01:00
|
|
|
// ipv4 support
|
2014-11-07 16:06:14 +01:00
|
|
|
hostsfileout << "127.0.0.1" << "\t" << "localhost" << "\n";
|
|
|
|
hostsfileout << "127.0.1.1" << "\t" << m_hostname << "\n";
|
2014-11-07 15:18:45 +01:00
|
|
|
// ipv6 support
|
2014-11-07 16:06:14 +01:00
|
|
|
hostsfileout << "::1" << "\t" << "localhost ip6-localhost ip6-loopback" << "\n";
|
|
|
|
hostsfileout << "ff02::1 ip6-allnodes" << "\n" << "ff02::2 ip6-allrouters" << "\n";
|
2014-11-07 15:18:45 +01:00
|
|
|
hostsfile.close();
|
|
|
|
|
2014-10-21 18:59:02 +02:00
|
|
|
return Calamares::JobResult::ok();
|
|
|
|
}
|