diff --git a/src/libcalamares/utils/CalamaresUtilsSystem.cpp b/src/libcalamares/utils/CalamaresUtilsSystem.cpp index 745472203..f358e46a5 100644 --- a/src/libcalamares/utils/CalamaresUtilsSystem.cpp +++ b/src/libcalamares/utils/CalamaresUtilsSystem.cpp @@ -24,6 +24,7 @@ #include #include +#include namespace CalamaresUtils { @@ -178,4 +179,51 @@ chrootOutput( const QString& command, } +qint64 +getPhysicalMemoryB() +{ + 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 +getTotalMemoryB() +{ + // 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; +} + + } diff --git a/src/libcalamares/utils/CalamaresUtilsSystem.h b/src/libcalamares/utils/CalamaresUtilsSystem.h index 1d3884541..b6ea452d7 100644 --- a/src/libcalamares/utils/CalamaresUtilsSystem.h +++ b/src/libcalamares/utils/CalamaresUtilsSystem.h @@ -65,6 +65,10 @@ DLLEXPORT int chrootOutput( const QString& command, const QString& workingPath = QString(), const QString& stdInput = QString(), int timeoutSec = 0 ); + +DLLEXPORT qint64 getPhysicalMemoryB(); //Better guess, doesn't work in VirualBox + +DLLEXPORT qint64 getTotalMemoryB(); //Always underguessed, but always works on Linux } #endif // CALAMARESUTILSSYSTEM_H