2017-12-20 14:39:09 +01:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
2014-07-29 20:18:02 +02:00
|
|
|
*
|
|
|
|
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
|
2018-01-12 12:30:52 +01:00
|
|
|
* Copyright 2017-2018, Adriaan de Groot <groot@kde.org>
|
2014-07-29 20:18:02 +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 "CalamaresUtilsSystem.h"
|
|
|
|
|
2014-08-01 17:10:25 +02:00
|
|
|
#include "utils/Logger.h"
|
2014-07-29 20:18:02 +02:00
|
|
|
#include "GlobalStorage.h"
|
2018-04-05 10:51:51 +02:00
|
|
|
#include "JobQueue.h"
|
|
|
|
#include "Settings.h"
|
2014-07-29 20:18:02 +02:00
|
|
|
|
2018-02-07 16:29:36 +01:00
|
|
|
#include <QCoreApplication>
|
2014-07-29 20:18:02 +02:00
|
|
|
#include <QDir>
|
|
|
|
#include <QProcess>
|
2015-06-10 00:03:58 +02:00
|
|
|
#include <QRegularExpression>
|
2014-07-29 20:18:02 +02:00
|
|
|
|
2017-09-19 10:52:37 +02:00
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
#include <sys/sysinfo.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef Q_OS_FREEBSD
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
|
|
|
|
2019-04-15 16:44:13 +02:00
|
|
|
/** @brief When logging commands, don't log everything.
|
|
|
|
*
|
|
|
|
* The command-line arguments to some commands may contain the
|
|
|
|
* encrypted password set by the user. Don't log that password,
|
|
|
|
* since the log may get posted to bug reports, or stored in
|
|
|
|
* the target system.
|
|
|
|
*/
|
|
|
|
struct RedactedList
|
|
|
|
{
|
|
|
|
RedactedList( const QStringList& l )
|
|
|
|
: list(l)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const QStringList& list;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
QDebug&
|
|
|
|
operator<<( QDebug& s, const RedactedList& l )
|
|
|
|
{
|
|
|
|
// Special case logging: don't log the (encrypted) password.
|
|
|
|
if ( l.list.contains( "usermod" ) )
|
|
|
|
{
|
|
|
|
for ( const auto& item : l.list )
|
|
|
|
if ( item.startsWith( "$6$" ) )
|
|
|
|
s << "<password>";
|
|
|
|
else
|
|
|
|
s << item;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
s << l.list;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-07-29 20:18:02 +02:00
|
|
|
namespace CalamaresUtils
|
|
|
|
{
|
|
|
|
|
2015-08-06 19:10:04 +02:00
|
|
|
System* System::s_instance = nullptr;
|
|
|
|
|
|
|
|
|
|
|
|
System::System( bool doChroot, QObject* parent )
|
|
|
|
: QObject( parent )
|
|
|
|
, m_doChroot( doChroot )
|
|
|
|
{
|
|
|
|
Q_ASSERT( !s_instance );
|
|
|
|
s_instance = this;
|
2015-08-10 13:48:47 +02:00
|
|
|
if ( !doChroot )
|
|
|
|
Calamares::JobQueue::instance()->globalStorage()->insert( "rootMountPoint", "/" );
|
2015-08-06 19:10:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
System::~System()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2018-02-19 15:47:15 +01:00
|
|
|
System*
|
|
|
|
System::instance()
|
2015-08-06 19:10:04 +02:00
|
|
|
{
|
2018-02-19 15:47:15 +01:00
|
|
|
if ( !s_instance )
|
|
|
|
{
|
|
|
|
cError() << "No Calamares system-object has been created.";
|
2019-04-15 14:58:21 +02:00
|
|
|
cError() << Logger::SubEntry << "using a bogus instance instead.";
|
2018-02-19 15:47:15 +01:00
|
|
|
return new System( true, nullptr );
|
|
|
|
}
|
2015-08-06 19:10:04 +02:00
|
|
|
return s_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-08 14:12:53 +02:00
|
|
|
int
|
2015-08-06 19:10:04 +02:00
|
|
|
System::mount( const QString& devicePath,
|
2014-08-08 14:12:53 +02:00
|
|
|
const QString& mountPoint,
|
|
|
|
const QString& filesystemName,
|
|
|
|
const QString& options )
|
2014-07-29 20:18:02 +02:00
|
|
|
{
|
|
|
|
if ( devicePath.isEmpty() || mountPoint.isEmpty() )
|
|
|
|
return -3;
|
|
|
|
|
|
|
|
QDir mountPointDir( mountPoint );
|
|
|
|
if ( !mountPointDir.exists() )
|
|
|
|
{
|
|
|
|
bool ok = mountPointDir.mkpath( mountPoint );
|
|
|
|
if ( !ok )
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString program( "mount" );
|
|
|
|
QStringList args = { devicePath, mountPoint };
|
|
|
|
|
|
|
|
if ( !filesystemName.isEmpty() )
|
|
|
|
args << "-t" << filesystemName;
|
|
|
|
|
|
|
|
if ( !options.isEmpty() )
|
|
|
|
args << "-o" << options;
|
|
|
|
|
|
|
|
return QProcess::execute( program, args );
|
|
|
|
}
|
|
|
|
|
2017-11-28 16:27:24 +01:00
|
|
|
ProcessResult
|
2018-01-12 12:30:52 +01:00
|
|
|
System::runCommand(
|
|
|
|
System::RunLocation location,
|
2017-11-28 16:27:24 +01:00
|
|
|
const QStringList& args,
|
|
|
|
const QString& workingPath,
|
|
|
|
const QString& stdInput,
|
|
|
|
int timeoutSec )
|
2014-08-08 14:12:53 +02:00
|
|
|
{
|
2017-11-28 16:27:24 +01:00
|
|
|
QString output;
|
2014-08-08 14:12:53 +02:00
|
|
|
|
2014-08-04 16:03:33 +02:00
|
|
|
if ( !Calamares::JobQueue::instance() )
|
|
|
|
return -3;
|
|
|
|
|
2014-07-29 20:18:02 +02:00
|
|
|
Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
|
2018-01-12 12:30:52 +01:00
|
|
|
if ( ( location == System::RunLocation::RunInTarget ) &&
|
|
|
|
( !gs || !gs->contains( "rootMountPoint" ) ) )
|
2014-08-01 17:10:25 +02:00
|
|
|
{
|
2018-02-27 01:09:43 +01:00
|
|
|
cWarning() << "No rootMountPoint in global storage";
|
2014-07-29 20:18:02 +02:00
|
|
|
return -3;
|
2014-08-01 17:10:25 +02:00
|
|
|
}
|
2014-07-29 20:18:02 +02:00
|
|
|
|
2015-08-06 12:45:03 +02:00
|
|
|
QProcess process;
|
|
|
|
QString program;
|
|
|
|
QStringList arguments;
|
|
|
|
|
2018-01-12 12:30:52 +01:00
|
|
|
if ( location == System::RunLocation::RunInTarget )
|
2014-08-01 17:10:25 +02:00
|
|
|
{
|
2015-08-06 12:45:03 +02:00
|
|
|
QString destDir = gs->value( "rootMountPoint" ).toString();
|
|
|
|
if ( !QDir( destDir ).exists() )
|
|
|
|
{
|
2018-02-27 01:09:43 +01:00
|
|
|
cWarning() << "rootMountPoint points to a dir which does not exist";
|
2015-08-06 12:45:03 +02:00
|
|
|
return -3;
|
|
|
|
}
|
2014-07-29 20:18:02 +02:00
|
|
|
|
2015-08-06 12:45:03 +02:00
|
|
|
program = "chroot";
|
|
|
|
arguments = QStringList( { destDir } );
|
2015-08-07 13:23:59 +02:00
|
|
|
arguments << args;
|
2015-08-06 12:45:03 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-19 13:37:53 +02:00
|
|
|
program = "env";
|
2015-08-19 16:35:23 +02:00
|
|
|
arguments << args;
|
2015-08-06 12:45:03 +02:00
|
|
|
}
|
2014-07-29 20:18:02 +02:00
|
|
|
|
|
|
|
process.setProgram( program );
|
2014-07-30 12:43:15 +02:00
|
|
|
process.setArguments( arguments );
|
2014-08-01 17:10:25 +02:00
|
|
|
process.setProcessChannelMode( QProcess::MergedChannels );
|
2014-07-29 20:18:02 +02:00
|
|
|
|
2014-08-12 14:26:10 +02:00
|
|
|
if ( !workingPath.isEmpty() )
|
|
|
|
{
|
|
|
|
if ( QDir( workingPath ).exists() )
|
|
|
|
process.setWorkingDirectory( QDir( workingPath ).absolutePath() );
|
|
|
|
else
|
2018-02-27 01:09:43 +01:00
|
|
|
cWarning() << "Invalid working directory:" << workingPath;
|
2014-08-12 14:26:10 +02:00
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
2019-04-15 16:44:13 +02:00
|
|
|
cDebug() << "Running" << program << RedactedList( arguments );
|
2014-08-01 17:01:00 +02:00
|
|
|
process.start();
|
2014-07-29 20:18:02 +02:00
|
|
|
if ( !process.waitForStarted() )
|
2014-08-01 17:10:25 +02:00
|
|
|
{
|
2018-02-27 01:09:43 +01:00
|
|
|
cWarning() << "Process failed to start" << process.error();
|
2014-07-29 20:18:02 +02:00
|
|
|
return -2;
|
2014-08-01 17:10:25 +02:00
|
|
|
}
|
2014-07-29 20:18:02 +02:00
|
|
|
|
|
|
|
if ( !stdInput.isEmpty() )
|
|
|
|
{
|
|
|
|
process.write( stdInput.toLocal8Bit() );
|
|
|
|
process.closeWriteChannel();
|
|
|
|
}
|
|
|
|
|
2014-08-04 16:23:12 +02:00
|
|
|
if ( !process.waitForFinished( timeoutSec ? ( timeoutSec * 1000 ) : -1 ) )
|
2014-08-01 17:10:25 +02:00
|
|
|
{
|
2018-04-05 11:17:21 +02:00
|
|
|
cWarning().noquote().nospace() << "Timed out. Output so far:\n" <<
|
2018-02-27 01:09:43 +01:00
|
|
|
process.readAllStandardOutput();
|
2014-07-29 20:18:02 +02:00
|
|
|
return -4;
|
2014-08-01 17:10:25 +02:00
|
|
|
}
|
|
|
|
|
2014-08-08 14:12:53 +02:00
|
|
|
output.append( QString::fromLocal8Bit( process.readAllStandardOutput() ).trimmed() );
|
2014-07-29 20:18:02 +02:00
|
|
|
|
|
|
|
if ( process.exitStatus() == QProcess::CrashExit )
|
2014-08-01 17:10:25 +02:00
|
|
|
{
|
2018-04-05 11:17:21 +02:00
|
|
|
cWarning().noquote().nospace() << "Process crashed. Output so far:\n" << output;
|
2014-07-29 20:18:02 +02:00
|
|
|
return -1;
|
2014-08-01 17:10:25 +02:00
|
|
|
}
|
2014-07-29 20:18:02 +02:00
|
|
|
|
2017-08-23 22:44:09 +02:00
|
|
|
auto r = process.exitCode();
|
2018-02-27 01:09:43 +01:00
|
|
|
cDebug() << "Finished. Exit code:" << r;
|
2018-04-05 10:51:51 +02:00
|
|
|
if ( ( r != 0 ) || Calamares::Settings::instance()->debugMode() )
|
2017-08-23 22:44:09 +02:00
|
|
|
{
|
2019-04-15 16:44:13 +02:00
|
|
|
cDebug() << "Target cmd:" << RedactedList( args );
|
2018-04-05 11:17:21 +02:00
|
|
|
cDebug().noquote().nospace() << "Target output:\n" << output;
|
2017-08-23 22:44:09 +02:00
|
|
|
}
|
2017-11-28 16:27:24 +01:00
|
|
|
return ProcessResult(r, output);
|
2014-07-30 12:43:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-20 14:22:39 +02:00
|
|
|
QPair<quint64, float>
|
2017-11-08 15:17:18 +01:00
|
|
|
System::getTotalMemoryB() const
|
2015-06-10 00:03:58 +02:00
|
|
|
{
|
2017-09-19 10:52:37 +02:00
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
struct sysinfo i;
|
|
|
|
int r = sysinfo( &i );
|
|
|
|
|
|
|
|
if (r)
|
2017-09-20 14:22:39 +02:00
|
|
|
return qMakePair(0, 0.0);
|
2015-06-10 00:03:58 +02:00
|
|
|
|
2017-09-20 14:22:39 +02:00
|
|
|
return qMakePair(quint64( i.mem_unit ) * quint64( i.totalram ), 1.1);
|
2017-09-19 10:52:37 +02:00
|
|
|
#elif defined( Q_OS_FREEBSD )
|
|
|
|
unsigned long memsize;
|
|
|
|
size_t s = sizeof(memsize);
|
2015-06-10 00:03:58 +02:00
|
|
|
|
2017-09-19 10:52:37 +02:00
|
|
|
int r = sysctlbyname("vm.kmem_size", &memsize, &s, NULL, 0);
|
|
|
|
if (r)
|
2017-09-20 14:22:39 +02:00
|
|
|
return qMakePair(0, 0.0);
|
2015-06-10 00:03:58 +02:00
|
|
|
|
2017-09-20 14:22:39 +02:00
|
|
|
return qMakePair(memsize, 1.01);
|
|
|
|
#else
|
|
|
|
return qMakePair(0, 0.0); // Unsupported
|
2017-09-19 10:52:37 +02:00
|
|
|
#endif
|
2015-06-10 00:03:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-08 15:17:18 +01:00
|
|
|
QString
|
|
|
|
System::getCpuDescription() const
|
|
|
|
{
|
|
|
|
QString model;
|
|
|
|
|
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
QFile file("/proc/cpuinfo");
|
|
|
|
if ( file.open(QIODevice::ReadOnly | QIODevice::Text) )
|
|
|
|
while ( !file.atEnd() )
|
|
|
|
{
|
|
|
|
QByteArray line = file.readLine();
|
|
|
|
if ( line.startsWith( "model name" ) && (line.indexOf( ':' ) > 0) )
|
|
|
|
{
|
|
|
|
model = QString::fromLatin1( line.right(line.length() - line.indexOf( ':' ) ) );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#elif defined( Q_OS_FREEBSD )
|
|
|
|
// This would use sysctl "hw.model", which has a string value
|
|
|
|
#endif
|
|
|
|
return model.simplified();
|
2014-07-29 20:18:02 +02:00
|
|
|
}
|
2017-11-08 15:17:18 +01:00
|
|
|
|
|
|
|
quint64
|
|
|
|
System::getTotalDiskB() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-03 16:09:00 +01:00
|
|
|
bool
|
|
|
|
System::doChroot() const
|
|
|
|
{
|
|
|
|
return m_doChroot;
|
|
|
|
}
|
|
|
|
|
2018-01-12 12:30:52 +01:00
|
|
|
Calamares::JobResult
|
2018-02-07 17:12:49 +01:00
|
|
|
ProcessResult::explainProcess( int ec, const QString& command, const QString& output, int timeout )
|
2018-01-12 12:30:52 +01:00
|
|
|
{
|
|
|
|
using Calamares::JobResult;
|
|
|
|
|
|
|
|
if ( ec == 0 )
|
|
|
|
return JobResult::ok();
|
|
|
|
|
2018-02-07 16:29:36 +01:00
|
|
|
QString outputMessage = output.isEmpty()
|
|
|
|
? QCoreApplication::translate( "ProcessResult", "\nThere was no output from the command.")
|
|
|
|
: (QCoreApplication::translate( "ProcessResult", "\nOutput:\n") + output);
|
2018-01-15 11:51:58 +01:00
|
|
|
|
2018-01-12 12:30:52 +01:00
|
|
|
if ( ec == -1 ) //Crash!
|
2018-02-07 16:29:36 +01:00
|
|
|
return JobResult::error( QCoreApplication::translate( "ProcessResult", "External command crashed." ),
|
|
|
|
QCoreApplication::translate( "ProcessResult", "Command <i>%1</i> crashed." )
|
2018-01-12 12:30:52 +01:00
|
|
|
.arg( command )
|
2018-01-15 11:51:58 +01:00
|
|
|
+ outputMessage );
|
2018-01-12 12:30:52 +01:00
|
|
|
|
|
|
|
if ( ec == -2 )
|
2018-02-07 16:29:36 +01:00
|
|
|
return JobResult::error( QCoreApplication::translate( "ProcessResult", "External command failed to start." ),
|
|
|
|
QCoreApplication::translate( "ProcessResult", "Command <i>%1</i> failed to start." )
|
2018-01-12 12:30:52 +01:00
|
|
|
.arg( command ) );
|
|
|
|
|
|
|
|
if ( ec == -3 )
|
2018-02-07 16:29:36 +01:00
|
|
|
return JobResult::error( QCoreApplication::translate( "ProcessResult", "Internal error when starting command." ),
|
|
|
|
QCoreApplication::translate( "ProcessResult", "Bad parameters for process job call." ) );
|
2018-01-12 12:30:52 +01:00
|
|
|
|
|
|
|
if ( ec == -4 )
|
2018-02-07 16:29:36 +01:00
|
|
|
return JobResult::error( QCoreApplication::translate( "ProcessResult", "External command failed to finish." ),
|
|
|
|
QCoreApplication::translate( "ProcessResult", "Command <i>%1</i> failed to finish in %2 seconds." )
|
2018-01-12 12:30:52 +01:00
|
|
|
.arg( command )
|
|
|
|
.arg( timeout )
|
2018-01-15 11:51:58 +01:00
|
|
|
+ outputMessage );
|
2018-01-12 12:30:52 +01:00
|
|
|
|
|
|
|
//Any other exit code
|
2018-02-07 16:29:36 +01:00
|
|
|
return JobResult::error( QCoreApplication::translate( "ProcessResult", "External command finished with errors." ),
|
|
|
|
QCoreApplication::translate( "ProcessResult", "Command <i>%1</i> finished with exit code %2." )
|
2018-01-12 12:30:52 +01:00
|
|
|
.arg( command )
|
|
|
|
.arg( ec )
|
2018-01-15 11:51:58 +01:00
|
|
|
+ outputMessage );
|
2018-01-12 12:30:52 +01:00
|
|
|
}
|
|
|
|
|
2017-11-08 15:17:18 +01:00
|
|
|
} // namespace
|