source: calamares/trunk/fuentes/src/modules/welcome/checker/RequirementsChecker.cpp @ 7538

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

sync with github

File size: 14.5 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2014-2017, Teo Mrnjavac <teo@kde.org>
4 *   Copyright 2017-2018, Adriaan de Groot <groot@kde.org>
5 *   Copyright 2017, Gabriel Craciunescu <crazy@frugalware.org>
6 *
7 *   Calamares is free software: you can redistribute it and/or modify
8 *   it under the terms of the GNU General Public License as published by
9 *   the Free Software Foundation, either version 3 of the License, or
10 *   (at your option) any later version.
11 *
12 *   Calamares is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "RequirementsChecker.h"
22
23#include "CheckerWidget.h"
24#include "partman_devices.h"
25
26#include "widgets/WaitingWidget.h"
27#include "utils/CalamaresUtilsGui.h"
28#include "utils/Logger.h"
29#include "utils/Retranslator.h"
30#include "utils/CalamaresUtilsSystem.h"
31#include "utils/Units.h"
32
33#include "JobQueue.h"
34#include "GlobalStorage.h"
35
36#include <QApplication>
37#include <QBoxLayout>
38#include <QDBusConnection>
39#include <QDBusInterface>
40#include <QDesktopWidget>
41#include <QDir>
42#include <QEventLoop>
43#include <QFile>
44#include <QFileInfo>
45#include <QLabel>
46#include <QNetworkAccessManager>
47#include <QNetworkRequest>
48#include <QNetworkReply>
49#include <QProcess>
50#include <QTimer>
51
52#include <unistd.h> //geteuid
53
54RequirementsChecker::RequirementsChecker( QObject* parent )
55    : QObject( parent )
56    , m_widget( new QWidget() )
57    , m_requiredStorageGB( -1 )
58    , m_requiredRamGB( -1 )
59    , m_actualWidget( new CheckerWidget() )
60    , m_verdict( false )
61{
62    QBoxLayout* mainLayout = new QHBoxLayout;
63    m_widget->setLayout( mainLayout );
64    CalamaresUtils::unmarginLayout( mainLayout );
65
66    WaitingWidget* waitingWidget = new WaitingWidget( QString() );
67    mainLayout->addWidget( waitingWidget );
68    CALAMARES_RETRANSLATE( waitingWidget->setText( tr( "Gathering system information..." ) ); )
69
70    QSize availableSize = qApp->desktop()->availableGeometry( m_widget ).size();
71
72    QTimer* timer = new QTimer;
73    timer->setSingleShot( true );
74    connect( timer, &QTimer::timeout,
75             [=]()
76    {
77        bool enoughStorage = false;
78        bool enoughRam = false;
79        bool hasPower = false;
80        bool hasInternet = false;
81        bool isRoot = false;
82        bool enoughScreen = (availableSize.width() >= CalamaresUtils::windowMinimumWidth) && (availableSize.height() >= CalamaresUtils::windowMinimumHeight);
83
84        qint64 requiredStorageB = CalamaresUtils::GiBtoBytes(m_requiredStorageGB);
85        cDebug() << "Need at least storage bytes:" << requiredStorageB;
86        if ( m_entriesToCheck.contains( "storage" ) )
87            enoughStorage = checkEnoughStorage( requiredStorageB );
88
89        qint64 requiredRamB = CalamaresUtils::GiBtoBytes(m_requiredRamGB);
90        cDebug() << "Need at least ram bytes:" << requiredRamB;
91        if ( m_entriesToCheck.contains( "ram" ) )
92            enoughRam = checkEnoughRam( requiredRamB );
93
94        if ( m_entriesToCheck.contains( "power" ) )
95            hasPower = checkHasPower();
96
97        if ( m_entriesToCheck.contains( "internet" ) )
98            hasInternet = checkHasInternet();
99
100        if ( m_entriesToCheck.contains( "root" ) )
101            isRoot = checkIsRoot();
102
103        using TR = Logger::DebugRow<const char *, bool>;
104
105        cDebug() << "RequirementsChecker output:"
106                 << TR("enoughStorage", enoughStorage)
107                 << TR("enoughRam", enoughRam)
108                 << TR("hasPower", hasPower)
109                 << TR("hasInternet", hasInternet)
110                 << TR("isRoot", isRoot);
111
112        QList< PrepareEntry > checkEntries;
113        foreach ( const QString& entry, m_entriesToCheck )
114        {
115            if ( entry == "storage" )
116                checkEntries.append( {
117                    entry,
118                    [this]{ return tr( "has at least %1 GB available drive space" )
119                        .arg( m_requiredStorageGB ); },
120                    [this]{ return tr( "There is not enough drive space. At least %1 GB is required." )
121                        .arg( m_requiredStorageGB ); },
122                    enoughStorage,
123                    m_entriesToRequire.contains( entry )
124                } );
125            else if ( entry == "ram" )
126                checkEntries.append( {
127                    entry,
128                    [this]{ return tr( "has at least %1 GB working memory" )
129                        .arg( m_requiredRamGB ); },
130                    [this]{ return tr( "The system does not have enough working memory. At least %1 GB is required." )
131                        .arg( m_requiredRamGB ); },
132                    enoughRam,
133                    m_entriesToRequire.contains( entry )
134                } );
135            else if ( entry == "power" )
136                checkEntries.append( {
137                    entry,
138                    [this]{ return tr( "is plugged in to a power source" ); },
139                    [this]{ return tr( "The system is not plugged in to a power source." ); },
140                    hasPower,
141                    m_entriesToRequire.contains( entry )
142                } );
143            else if ( entry == "internet" )
144                checkEntries.append( {
145                    entry,
146                    [this]{ return tr( "is connected to the Internet" ); },
147                    [this]{ return tr( "The system is not connected to the Internet." ); },
148                    hasInternet,
149                    m_entriesToRequire.contains( entry )
150                } );
151            else if ( entry == "root" )
152                checkEntries.append( {
153                    entry,
154                    [this]{ return QString(); }, //we hide it
155                    [this]{ return tr( "The installer is not running with administrator rights." ); },
156                    isRoot,
157                    m_entriesToRequire.contains( entry )
158                } );
159            else if ( entry == "screen" )
160                checkEntries.append( {
161                    entry,
162                    [this]{ return QString(); }, // we hide it
163                    [this]{ return tr( "The screen is too small to display the installer." ); },
164                    enoughScreen,
165                    false
166                } );
167        }
168
169        m_actualWidget->init( checkEntries );
170        m_widget->layout()->removeWidget( waitingWidget );
171        waitingWidget->deleteLater();
172        m_actualWidget->setParent( m_widget );
173        m_widget->layout()->addWidget( m_actualWidget );
174
175        bool canGoNext = true;
176        foreach ( const PrepareEntry& entry, checkEntries )
177        {
178            if ( !entry.checked && entry.required )
179            {
180                canGoNext = false;
181                break;
182            }
183        }
184        m_verdict = canGoNext;
185        emit verdictChanged( m_verdict );
186
187        if ( canGoNext )
188            detectFirmwareType();
189
190        timer->deleteLater();
191    } );
192    timer->start( 0 );
193
194    emit verdictChanged( true );
195}
196
197
198RequirementsChecker::~RequirementsChecker()
199{
200    if ( m_widget && m_widget->parent() == nullptr )
201        m_widget->deleteLater();
202}
203
204
205QWidget*
206RequirementsChecker::widget() const
207{
208    return m_widget;
209}
210
211
212void
213RequirementsChecker::setConfigurationMap( const QVariantMap& configurationMap )
214{
215    bool incompleteConfiguration = false;
216
217    if ( configurationMap.contains( "check" ) &&
218         configurationMap.value( "check" ).type() == QVariant::List )
219    {
220        m_entriesToCheck.clear();
221        m_entriesToCheck.append( configurationMap.value( "check" ).toStringList() );
222    }
223    else
224    {
225        cWarning() << "RequirementsChecker entry 'check' is incomplete.";
226        incompleteConfiguration = true;
227    }
228
229    if ( configurationMap.contains( "required" ) &&
230         configurationMap.value( "required" ).type() == QVariant::List )
231    {
232        m_entriesToRequire.clear();
233        m_entriesToRequire.append( configurationMap.value( "required" ).toStringList() );
234    }
235    else
236    {
237        cWarning() << "RequirementsChecker entry 'required' is incomplete.";
238        incompleteConfiguration = true;
239    }
240
241    // Help out with consistency, but don't fix
242    for ( const auto& r : m_entriesToRequire )
243        if ( !m_entriesToCheck.contains( r ) )
244            cWarning() << "RequirementsChecker requires" << r << "but does not check it.";
245
246    if ( configurationMap.contains( "requiredStorage" ) &&
247         ( configurationMap.value( "requiredStorage" ).type() == QVariant::Double ||
248           configurationMap.value( "requiredStorage" ).type() == QVariant::Int ) )
249    {
250        bool ok = false;
251        m_requiredStorageGB = configurationMap.value( "requiredStorage" ).toDouble( &ok );
252        if ( !ok )
253        {
254            cWarning() << "RequirementsChecker entry 'requiredStorage' is invalid.";
255            m_requiredStorageGB = 3.;
256        }
257
258        Calamares::JobQueue::instance()->globalStorage()->insert( "requiredStorageGB", m_requiredStorageGB );
259    }
260    else
261    {
262        cWarning() << "RequirementsChecker entry 'requiredStorage' is missing.";
263        m_requiredStorageGB = 3.;
264        incompleteConfiguration = true;
265    }
266
267    if ( configurationMap.contains( "requiredRam" ) &&
268         ( configurationMap.value( "requiredRam" ).type() == QVariant::Double ||
269           configurationMap.value( "requiredRam" ).type() == QVariant::Int ) )
270    {
271        bool ok = false;
272        m_requiredRamGB = configurationMap.value( "requiredRam" ).toDouble( &ok );
273        if ( !ok )
274        {
275            cWarning() << "RequirementsChecker entry 'requiredRam' is invalid.";
276            m_requiredRamGB = 1.;
277            incompleteConfiguration = true;
278        }
279    }
280    else
281    {
282        cWarning() << "RequirementsChecker entry 'requiredRam' is missing.";
283        m_requiredRamGB = 1.;
284        incompleteConfiguration = true;
285    }
286
287    if ( configurationMap.contains( "internetCheckUrl" ) &&
288         configurationMap.value( "internetCheckUrl" ).type() == QVariant::String )
289    {
290        m_checkHasInternetUrl = configurationMap.value( "internetCheckUrl" ).toString().trimmed();
291        if ( m_checkHasInternetUrl.isEmpty() ||
292             !QUrl( m_checkHasInternetUrl ).isValid() )
293        {
294            cWarning() << "RequirementsChecker entry 'internetCheckUrl' is invalid in welcome.conf" << m_checkHasInternetUrl
295                     << "reverting to default (http://example.com).";
296            m_checkHasInternetUrl = "http://example.com";
297            incompleteConfiguration = true;
298        }
299    }
300    else
301    {
302        cWarning() << "RequirementsChecker entry 'internetCheckUrl' is undefined in welcome.conf,"
303                    "reverting to default (http://example.com).";
304
305        m_checkHasInternetUrl = "http://example.com";
306        incompleteConfiguration = true;
307    }
308
309    if ( incompleteConfiguration )
310    {
311        cWarning() << "RequirementsChecker configuration map:" << Logger::DebugMap( configurationMap );
312    }
313}
314
315
316bool
317RequirementsChecker::verdict() const
318{
319    return m_verdict;
320}
321
322
323bool
324RequirementsChecker::checkEnoughStorage( qint64 requiredSpace )
325{
326#ifdef WITHOUT_LIBPARTED
327    Q_UNUSED( requiredSpace );
328    cWarning() << "RequirementsChecker is configured without libparted.";
329    return false;
330#else
331    return check_big_enough( requiredSpace );
332#endif
333}
334
335
336bool
337RequirementsChecker::checkEnoughRam( qint64 requiredRam )
338{
339    // Ignore the guesstimate-factor; we get an under-estimate
340    // which is probably the usable RAM for programs.
341    quint64 availableRam = CalamaresUtils::System::instance()->getTotalMemoryB().first;
342    return availableRam >= requiredRam * 0.95; // because MemTotal is variable
343}
344
345
346bool
347RequirementsChecker::checkBatteryExists()
348{
349    const QFileInfo basePath( "/sys/class/power_supply" );
350
351    if ( !( basePath.exists() && basePath.isDir() ) )
352        return false;
353
354    QDir baseDir( basePath.absoluteFilePath() );
355    const auto entries = baseDir.entryList( QDir::AllDirs | QDir::Readable | QDir::NoDotAndDotDot );
356    for ( const auto &item : entries )
357    {
358        QFileInfo typePath( baseDir.absoluteFilePath( QString( "%1/type" )
359                                                      .arg( item ) ) );
360        QFile typeFile( typePath.absoluteFilePath() );
361        if ( typeFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
362        {
363            if ( typeFile.readAll().startsWith( "Battery" ) )
364                return true;
365        }
366    }
367
368    return false;
369}
370
371
372bool
373RequirementsChecker::checkHasPower()
374{
375    const QString UPOWER_SVC_NAME( "org.freedesktop.UPower" );
376    const QString UPOWER_INTF_NAME( "org.freedesktop.UPower" );
377    const QString UPOWER_PATH( "/org/freedesktop/UPower" );
378
379    if ( !checkBatteryExists() )
380        return true;
381
382    cDebug() << "A battery exists, checking for mains power.";
383    QDBusInterface upowerIntf( UPOWER_SVC_NAME,
384                               UPOWER_PATH,
385                               UPOWER_INTF_NAME,
386                               QDBusConnection::systemBus() );
387
388    bool onBattery = upowerIntf.property( "OnBattery" ).toBool();
389
390    if ( !upowerIntf.isValid() )
391    {
392        // We can't talk to upower but we're obviously up and running
393        // so I guess we got that going for us, which is nice...
394        return true;
395    }
396
397    // If a battery exists but we're not using it, means we got mains
398    // power.
399    return !onBattery;
400}
401
402
403bool
404RequirementsChecker::checkHasInternet()
405{
406    // default to true in the QNetworkAccessManager::UnknownAccessibility case
407    QNetworkAccessManager qnam( this );
408    bool hasInternet = qnam.networkAccessible() == QNetworkAccessManager::Accessible;
409
410    if ( !hasInternet && qnam.networkAccessible() == QNetworkAccessManager::UnknownAccessibility )
411    {
412        QNetworkRequest req = QNetworkRequest( QUrl( m_checkHasInternetUrl ) );
413        QNetworkReply* reply = qnam.get( req );
414        QEventLoop loop;
415        connect( reply, &QNetworkReply::finished,
416                 &loop, &QEventLoop::quit );
417        loop.exec();
418        if( reply->bytesAvailable() )
419            hasInternet = true;
420    }
421
422    Calamares::JobQueue::instance()->globalStorage()->insert( "hasInternet", hasInternet );
423    return hasInternet;
424}
425
426
427bool
428RequirementsChecker::checkIsRoot()
429{
430    return !geteuid();
431}
432
433
434void
435RequirementsChecker::detectFirmwareType()
436{
437    QString fwType = QFile::exists( "/sys/firmware/efi/efivars" ) ? "efi" : "bios";
438    Calamares::JobQueue::instance()->globalStorage()->insert( "firmwareType", fwType );
439}
Note: See TracBrowser for help on using the repository browser.