2017-12-20 14:39:09 +01:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
2014-08-01 16:28:29 +02:00
|
|
|
*
|
|
|
|
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
|
2015-08-07 15:38:31 +02:00
|
|
|
* Copyright 2015, Rohan Garg <rohan@garg.io>
|
2014-08-01 16:28:29 +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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <SetTimezoneJob.h>
|
|
|
|
|
|
|
|
#include "GlobalStorage.h"
|
2019-09-07 16:58:37 +02:00
|
|
|
#include "JobQueue.h"
|
2016-01-15 02:44:27 +01:00
|
|
|
#include "Settings.h"
|
2014-08-01 16:28:29 +02:00
|
|
|
#include "utils/CalamaresUtilsSystem.h"
|
2019-09-07 16:58:37 +02:00
|
|
|
#include "utils/Logger.h"
|
2014-08-01 16:28:29 +02:00
|
|
|
|
|
|
|
#include <QDir>
|
2014-08-04 21:47:44 +02:00
|
|
|
#include <QFileInfo>
|
2014-08-01 16:28:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
SetTimezoneJob::SetTimezoneJob( const QString& region, const QString& zone )
|
|
|
|
: Calamares::Job()
|
|
|
|
, m_region( region )
|
|
|
|
, m_zone( zone )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString
|
|
|
|
SetTimezoneJob::prettyName() const
|
|
|
|
{
|
|
|
|
return tr( "Set timezone to %1/%2" ).arg( m_region ).arg( m_zone );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Calamares::JobResult
|
|
|
|
SetTimezoneJob::exec()
|
|
|
|
{
|
2016-01-15 02:44:27 +01:00
|
|
|
// do not call timedatectl in a chroot, it is not safe (timedatectl talks
|
|
|
|
// to a running timedated over D-Bus), and we have code that works
|
|
|
|
if ( !Calamares::Settings::instance()->doChroot() )
|
|
|
|
{
|
2019-09-07 16:58:37 +02:00
|
|
|
int ec = CalamaresUtils::System::instance()->targetEnvCall(
|
|
|
|
{ "timedatectl", "set-timezone", m_region + '/' + m_zone } );
|
2015-08-07 15:38:31 +02:00
|
|
|
|
2016-01-15 02:44:27 +01:00
|
|
|
if ( !ec )
|
2019-09-07 16:58:37 +02:00
|
|
|
{
|
2016-01-15 02:44:27 +01:00
|
|
|
return Calamares::JobResult::ok();
|
2019-09-07 16:58:37 +02:00
|
|
|
}
|
2016-01-15 02:44:27 +01:00
|
|
|
}
|
2015-08-07 15:38:31 +02:00
|
|
|
|
2014-08-19 20:39:26 +02:00
|
|
|
QString localtimeSlink( "/etc/localtime" );
|
2014-08-01 16:28:29 +02:00
|
|
|
QString zoneinfoPath( "/usr/share/zoneinfo" );
|
|
|
|
zoneinfoPath.append( QDir::separator() + m_region );
|
|
|
|
zoneinfoPath.append( QDir::separator() + m_zone );
|
|
|
|
|
|
|
|
Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
|
2014-08-04 21:47:44 +02:00
|
|
|
QFileInfo zoneFile( gs->value( "rootMountPoint" ).toString() + zoneinfoPath );
|
|
|
|
if ( !zoneFile.exists() || !zoneFile.isReadable() )
|
|
|
|
return Calamares::JobResult::error( tr( "Cannot access selected timezone path." ),
|
|
|
|
tr( "Bad path: %1" ).arg( zoneFile.absolutePath() ) );
|
2014-08-01 16:28:29 +02:00
|
|
|
|
2014-08-19 20:39:26 +02:00
|
|
|
// Make sure /etc/localtime doesn't exist, otherwise symlinking will fail
|
2019-09-07 16:58:37 +02:00
|
|
|
CalamaresUtils::System::instance()->targetEnvCall( { "rm", "-f", localtimeSlink } );
|
|
|
|
|
|
|
|
int ec = CalamaresUtils::System::instance()->targetEnvCall( { "ln", "-s", zoneinfoPath, localtimeSlink } );
|
2014-08-01 16:28:29 +02:00
|
|
|
if ( ec )
|
2019-09-07 16:58:37 +02:00
|
|
|
return Calamares::JobResult::error(
|
|
|
|
tr( "Cannot set timezone." ),
|
|
|
|
tr( "Link creation failed, target: %1; link name: %2" ).arg( zoneinfoPath ).arg( "/etc/localtime" ) );
|
2014-08-01 16:28:29 +02:00
|
|
|
|
2016-08-26 15:05:26 +02:00
|
|
|
QFile timezoneFile( gs->value( "rootMountPoint" ).toString() + "/etc/timezone" );
|
|
|
|
|
2019-09-07 16:58:37 +02:00
|
|
|
if ( !timezoneFile.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
|
|
|
|
return Calamares::JobResult::error( tr( "Cannot set timezone," ),
|
|
|
|
tr( "Cannot open /etc/timezone for writing" ) );
|
2016-08-26 15:05:26 +02:00
|
|
|
|
2019-09-07 16:58:37 +02:00
|
|
|
QTextStream out( &timezoneFile );
|
2016-08-26 15:05:26 +02:00
|
|
|
out << m_region << '/' << m_zone << "\n";
|
|
|
|
timezoneFile.close();
|
2015-08-07 17:32:02 +02:00
|
|
|
|
2014-08-01 16:28:29 +02:00
|
|
|
return Calamares::JobResult::ok();
|
|
|
|
}
|