source: calamares/trunk/fuentes/src/modules/users/SetHostNameJob.cpp @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 13 months ago

sync with github

File size: 3.1 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2014, Rohan Garg <rohan@kde.org>
4 *   Copyright 2015, Teo Mrnjavac <teo@kde.org>
5 *
6 *   Calamares is free software: you can redistribute it and/or modify
7 *   it under the terms of the GNU General Public License as published by
8 *   the Free Software Foundation, either version 3 of the License, or
9 *   (at your option) any later version.
10 *
11 *   Calamares is distributed in the hope that it will be useful,
12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *   GNU General Public License for more details.
15 *
16 *   You should have received a copy of the GNU General Public License
17 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "SetHostNameJob.h"
21
22#include "GlobalStorage.h"
23#include "utils/Logger.h"
24#include "JobQueue.h"
25
26#include <QFile>
27#include <QDir>
28
29SetHostNameJob::SetHostNameJob( const QString& hostname )
30    : Calamares::Job()
31    , m_hostname( hostname )
32{
33}
34
35QString SetHostNameJob::prettyName() const
36{
37    return tr( "Set hostname %1" ).arg( m_hostname );
38}
39
40
41QString
42SetHostNameJob::prettyDescription() const
43{
44    return tr( "Set hostname <strong>%1</strong>." ).arg( m_hostname );
45}
46
47
48QString
49SetHostNameJob::prettyStatusMessage() const
50{
51    return tr( "Setting hostname %1." ).arg( m_hostname );
52}
53
54Calamares::JobResult SetHostNameJob::exec()
55{
56    Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
57
58    if ( !gs || !gs->contains( "rootMountPoint" ) )
59    {
60        cError() << "No rootMountPoint in global storage";
61        return Calamares::JobResult::error( tr( "Internal Error" ) );
62    }
63
64    QString destDir = gs->value( "rootMountPoint" ).toString();
65    if ( !QDir( destDir ).exists() )
66    {
67        cError() << "rootMountPoint points to a dir which does not exist";
68        return Calamares::JobResult::error( tr( "Internal Error" ) );
69    }
70
71    QFile hostfile( destDir + "/etc/hostname" );
72    if ( !hostfile.open( QFile::WriteOnly ) )
73    {
74        cError() << "Can't write to hostname file";
75        return Calamares::JobResult::error( tr( "Cannot write hostname to target system" ) );
76    }
77
78    QTextStream hostfileout( &hostfile );
79    hostfileout << m_hostname << "\n";
80    hostfile.close();
81
82    QFile hostsfile( destDir + "/etc/hosts" );
83    if ( !hostsfile.open( QFile::WriteOnly ) )
84    {
85        cError() << "Can't write to hosts file";
86        return Calamares::JobResult::error( tr( "Cannot write hostname to target system" ) );
87    }
88
89    // We also need to write the appropriate entries for /etc/hosts
90    QTextStream hostsfileout( &hostsfile );
91    // ipv4 support
92    hostsfileout << "127.0.0.1" << "\t" << "localhost" << "\n";
93    hostsfileout << "127.0.1.1" << "\t" << m_hostname << "\n";
94    // ipv6 support
95    hostsfileout << "::1" << "\t" << "localhost ip6-localhost ip6-loopback" << "\n";
96    hostsfileout << "ff02::1 ip6-allnodes" << "\n" << "ff02::2 ip6-allrouters" << "\n";
97    hostsfile.close();
98
99    return Calamares::JobResult::ok();
100}
Note: See TracBrowser for help on using the repository browser.