2014-07-29 20:18:02 +02:00
|
|
|
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
|
|
|
*
|
|
|
|
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
|
|
|
|
*
|
|
|
|
* 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 "JobQueue.h"
|
|
|
|
#include "GlobalStorage.h"
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QProcess>
|
2015-06-10 00:03:58 +02:00
|
|
|
#include <QRegularExpression>
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
System::~System()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
System*System::instance()
|
|
|
|
{
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2014-08-08 14:12:53 +02:00
|
|
|
int
|
2015-08-06 19:10:04 +02:00
|
|
|
System::targetEnvCall( const QStringList& args,
|
2014-08-12 14:26:10 +02:00
|
|
|
const QString& workingPath,
|
2014-08-08 14:12:53 +02:00
|
|
|
const QString& stdInput,
|
|
|
|
int timeoutSec )
|
2014-07-29 20:18:02 +02:00
|
|
|
{
|
2014-08-08 14:12:53 +02:00
|
|
|
QString discard;
|
2015-08-06 11:57:01 +02:00
|
|
|
return targetEnvOutput( args,
|
2014-08-08 14:12:53 +02:00
|
|
|
discard,
|
2014-08-12 14:26:10 +02:00
|
|
|
workingPath,
|
2014-08-08 14:12:53 +02:00
|
|
|
stdInput,
|
|
|
|
timeoutSec );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2015-08-06 19:10:04 +02:00
|
|
|
System::targetEnvCall( const QString& command,
|
2014-08-12 14:26:10 +02:00
|
|
|
const QString& workingPath,
|
2014-08-08 14:12:53 +02:00
|
|
|
const QString& stdInput,
|
|
|
|
int timeoutSec )
|
|
|
|
{
|
2015-08-06 11:57:01 +02:00
|
|
|
return targetEnvCall( QStringList{ command },
|
2014-08-12 14:26:10 +02:00
|
|
|
workingPath,
|
2014-08-08 14:12:53 +02:00
|
|
|
stdInput,
|
|
|
|
timeoutSec );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2015-08-06 19:10:04 +02:00
|
|
|
System::targetEnvOutput( const QStringList& args,
|
2014-08-08 14:12:53 +02:00
|
|
|
QString& output,
|
2014-08-12 14:26:10 +02:00
|
|
|
const QString& workingPath,
|
2014-08-08 14:12:53 +02:00
|
|
|
const QString& stdInput,
|
|
|
|
int timeoutSec )
|
|
|
|
{
|
|
|
|
output.clear();
|
|
|
|
|
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();
|
2015-08-06 12:45:03 +02:00
|
|
|
if ( !gs ||
|
2015-08-06 19:10:04 +02:00
|
|
|
( m_doChroot && !gs->contains( "rootMountPoint" ) ) )
|
2014-08-01 17:10:25 +02:00
|
|
|
{
|
|
|
|
cLog() << "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;
|
|
|
|
|
2015-08-06 19:10:04 +02:00
|
|
|
if ( m_doChroot )
|
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() )
|
|
|
|
{
|
|
|
|
cLog() << "rootMountPoint points to a dir which does not exist";
|
|
|
|
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
|
|
|
|
{
|
|
|
|
program = "sh";
|
|
|
|
arguments = QStringList( { "-c" } );
|
2015-08-07 13:30:37 +02:00
|
|
|
arguments << args.join( ' ' );
|
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
|
|
|
|
cLog() << "Invalid working directory:" << workingPath;
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
2014-08-01 17:10:25 +02:00
|
|
|
cLog() << "Running" << program << 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
|
|
|
{
|
|
|
|
cLog() << "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
|
|
|
{
|
|
|
|
cLog() << "Timed out. output so far:";
|
|
|
|
cLog() << 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
|
|
|
{
|
|
|
|
cLog() << "Process crashed";
|
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
|
|
|
|
2014-08-01 17:10:25 +02:00
|
|
|
cLog() << "Finished. Exit code:" << process.exitCode();
|
2014-07-29 20:18:02 +02:00
|
|
|
return process.exitCode();
|
|
|
|
}
|
|
|
|
|
2014-07-30 12:43:15 +02:00
|
|
|
|
2014-08-08 14:12:53 +02:00
|
|
|
int
|
2015-08-06 19:10:04 +02:00
|
|
|
System::targetEnvOutput( const QString& command,
|
2014-08-08 14:12:53 +02:00
|
|
|
QString& output,
|
2014-08-12 14:26:10 +02:00
|
|
|
const QString& workingPath,
|
2014-08-08 14:12:53 +02:00
|
|
|
const QString& stdInput,
|
|
|
|
int timeoutSec )
|
2014-07-30 12:43:15 +02:00
|
|
|
{
|
2015-08-06 11:57:01 +02:00
|
|
|
return targetEnvOutput( QStringList{ command },
|
2014-08-08 14:12:53 +02:00
|
|
|
output,
|
2014-08-12 14:26:10 +02:00
|
|
|
workingPath,
|
2014-08-08 14:12:53 +02:00
|
|
|
stdInput,
|
|
|
|
timeoutSec );
|
2014-07-30 12:43:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-10 00:03:58 +02:00
|
|
|
qint64
|
2015-08-06 19:10:04 +02:00
|
|
|
System::getPhysicalMemoryB()
|
2015-06-10 00:03:58 +02:00
|
|
|
{
|
|
|
|
QProcess p;
|
|
|
|
p.start( "dmidecode", { "-t", "17" } );
|
|
|
|
p.waitForFinished();
|
|
|
|
QStringList lines = QString::fromLocal8Bit( p.readAllStandardOutput() ).split( '\n' );
|
|
|
|
lines = lines.filter( QRegularExpression( "^\\W*Size:\\W\\d*\\WMB" ) );
|
|
|
|
if ( !lines.isEmpty() )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
qint64 availableRamMb = 0;
|
|
|
|
foreach( const QString& line, lines )
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
availableRamMb += line.simplified()
|
|
|
|
.split( ' ' )
|
|
|
|
.value( 1 )
|
|
|
|
.toInt( &ok );
|
|
|
|
if ( !ok )
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
qint64 availableRam = availableRamMb * 1024 * 1024;
|
|
|
|
return availableRam;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
qint64
|
2015-08-06 19:10:04 +02:00
|
|
|
System::getTotalMemoryB()
|
2015-06-10 00:03:58 +02:00
|
|
|
{
|
|
|
|
// A line in meminfo looks like this, with {print $2} we grab the second column.
|
|
|
|
// MemTotal: 8133432 kB
|
|
|
|
|
|
|
|
QProcess p;
|
|
|
|
p.start( "awk", { "/MemTotal/ {print $2}", "/proc/meminfo" } );
|
|
|
|
p.waitForFinished();
|
|
|
|
QString memoryLine = p.readAllStandardOutput().simplified();
|
|
|
|
|
|
|
|
bool ok = false;
|
|
|
|
qint64 availableRam = memoryLine.toLongLong( &ok ) * 1024;
|
|
|
|
if ( !ok )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return availableRam;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-29 20:18:02 +02:00
|
|
|
}
|