summaryrefslogtreecommitdiff
path: root/src/modules/welcome/checker/RequirementsChecker.cpp
blob: acd5887111ff22c40d649947727c0679708427ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/* === This file is part of Calamares - <http://github.com/calamares> ===
 *
 *   Copyright 2014-2017, Teo Mrnjavac <teo@kde.org>
 *   Copyright 2017, Adriaan de Groot <groot@kde.org>
 *   Copyright 2017, Gabriel Craciunescu <crazy@frugalware.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 "RequirementsChecker.h"

#include "CheckerWidget.h"
#include "partman_devices.h"

#include "widgets/WaitingWidget.h"
#include "utils/CalamaresUtilsGui.h"
#include "utils/Logger.h"
#include "utils/Retranslator.h"
#include "utils/CalamaresUtilsSystem.h"
#include "utils/Units.h"

#include "JobQueue.h"
#include "GlobalStorage.h"

#include <QApplication>
#include <QBoxLayout>
#include <QDBusConnection>
#include <QDBusInterface>
#include <QDesktopWidget>
#include <QDir>
#include <QEventLoop>
#include <QFile>
#include <QFileInfo>
#include <QLabel>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QProcess>
#include <QTimer>

#include <unistd.h> //geteuid

RequirementsChecker::RequirementsChecker( QObject* parent )
    : QObject( parent )
    , m_widget( new QWidget() )
    , m_requiredStorageGB( -1 )
    , m_requiredRamGB( -1 )
    , m_actualWidget( new CheckerWidget() )
    , m_verdict( false )
{
    QBoxLayout* mainLayout = new QHBoxLayout;
    m_widget->setLayout( mainLayout );
    CalamaresUtils::unmarginLayout( mainLayout );

    WaitingWidget* waitingWidget = new WaitingWidget( QString() );
    mainLayout->addWidget( waitingWidget );
    CALAMARES_RETRANSLATE( waitingWidget->setText( tr( "Gathering system information..." ) ); )

    QSize availableSize = qApp->desktop()->availableGeometry( m_widget ).size();

    QTimer* timer = new QTimer;
    timer->setSingleShot( true );
    connect( timer, &QTimer::timeout,
             [=]()
    {
        bool enoughStorage = false;
        bool enoughRam = false;
        bool hasPower = false;
        bool hasInternet = false;
        bool isRoot = false;
        bool enoughScreen = (availableSize.width() >= CalamaresUtils::windowMinimumWidth) && (availableSize.height() >= CalamaresUtils::windowMinimumHeight);

        qint64 requiredStorageB = CalamaresUtils::GiBtoBytes(m_requiredStorageGB);
        cDebug() << "Need at least storage bytes:" << requiredStorageB;
        if ( m_entriesToCheck.contains( "storage" ) )
            enoughStorage = checkEnoughStorage( requiredStorageB );

        qint64 requiredRamB = CalamaresUtils::GiBtoBytes(m_requiredRamGB);
        cDebug() << "Need at least ram bytes:" << requiredRamB;
        if ( m_entriesToCheck.contains( "ram" ) )
            enoughRam = checkEnoughRam( requiredRamB );

        if ( m_entriesToCheck.contains( "power" ) )
            hasPower = checkHasPower();

        if ( m_entriesToCheck.contains( "internet" ) )
            hasInternet = checkHasInternet();

        if ( m_entriesToCheck.contains( "root" ) )
            isRoot = checkIsRoot();

        cDebug() << "RequirementsChecker output:"
                 << " enoughStorage:" << enoughStorage
                 << " enoughRam:" << enoughRam
                 << " hasPower:" << hasPower
                 << " hasInternet:" << hasInternet
                 << " isRoot:" << isRoot;

        QList< PrepareEntry > checkEntries;
        foreach ( const QString& entry, m_entriesToCheck )
        {
            if ( entry == "storage" )
                checkEntries.append( {
                    entry,
                    [this]{ return tr( "has at least %1 GB available drive space" )
                        .arg( m_requiredStorageGB ); },
                    [this]{ return tr( "There is not enough drive space. At least %1 GB is required." )
                        .arg( m_requiredStorageGB ); },
                    enoughStorage,
                    m_entriesToRequire.contains( entry )
                } );
            else if ( entry == "ram" )
                checkEntries.append( {
                    entry,
                    [this]{ return tr( "has at least %1 GB working memory" )
                        .arg( m_requiredRamGB ); },
                    [this]{ return tr( "The system does not have enough working memory. At least %1 GB is required." )
                        .arg( m_requiredRamGB ); },
                    enoughRam,
                    m_entriesToRequire.contains( entry )
                } );
            else if ( entry == "power" )
                checkEntries.append( {
                    entry,
                    [this]{ return tr( "is plugged in to a power source" ); },
                    [this]{ return tr( "The system is not plugged in to a power source." ); },
                    hasPower,
                    m_entriesToRequire.contains( entry )
                } );
            else if ( entry == "internet" )
                checkEntries.append( {
                    entry,
                    [this]{ return tr( "is connected to the Internet" ); },
                    [this]{ return tr( "The system is not connected to the Internet." ); },
                    hasInternet,
                    m_entriesToRequire.contains( entry )
                } );
            else if ( entry == "root" )
                checkEntries.append( {
                    entry,
                    [this]{ return QString(); }, //we hide it
                    [this]{ return tr( "The installer is not running with administrator rights." ); },
                    isRoot,
                    m_entriesToRequire.contains( entry )
                } );
            else if ( entry == "screen" )
                checkEntries.append( {
                    entry,
                    [this]{ return QString(); }, // we hide it
                    [this]{ return tr( "The screen is too small to display the installer." ); },
                    enoughScreen,
                    false
                } );
        }

        m_actualWidget->init( checkEntries );
        m_widget->layout()->removeWidget( waitingWidget );
        waitingWidget->deleteLater();
        m_actualWidget->setParent( m_widget );
        m_widget->layout()->addWidget( m_actualWidget );

        bool canGoNext = true;
        foreach ( const PrepareEntry& entry, checkEntries )
        {
            if ( !entry.checked && entry.required )
            {
                canGoNext = false;
                break;
            }
        }
        m_verdict = canGoNext;
        emit verdictChanged( m_verdict );

        if ( canGoNext )
            detectFirmwareType();

        timer->deleteLater();
    } );
    timer->start( 0 );

    emit verdictChanged( true );
}


RequirementsChecker::~RequirementsChecker()
{
    if ( m_widget && m_widget->parent() == nullptr )
        m_widget->deleteLater();
}


QWidget*
RequirementsChecker::widget() const
{
    return m_widget;
}


void
RequirementsChecker::setConfigurationMap( const QVariantMap& configurationMap )
{
    bool incompleteConfiguration = false;
    if ( configurationMap.contains( "requiredStorage" ) &&
         ( configurationMap.value( "requiredStorage" ).type() == QVariant::Double ||
           configurationMap.value( "requiredStorage" ).type() == QVariant::Int ) )
    {
        bool ok = false;
        m_requiredStorageGB = configurationMap.value( "requiredStorage" ).toDouble( &ok );
        if ( !ok )
            m_requiredStorageGB = 3.;

        Calamares::JobQueue::instance()->globalStorage()->insert( "requiredStorageGB", m_requiredStorageGB );
    }
    else
    {
        m_requiredStorageGB = 3.;
        incompleteConfiguration = true;
    }

    if ( configurationMap.contains( "requiredRam" ) &&
         ( configurationMap.value( "requiredRam" ).type() == QVariant::Double ||
           configurationMap.value( "requiredRam" ).type() == QVariant::Int ) )
    {
        bool ok = false;
        m_requiredRamGB = configurationMap.value( "requiredRam" ).toDouble( &ok );
        if ( !ok )
        {
            m_requiredRamGB = 1.;
            incompleteConfiguration = true;
        }
    }
    else
    {
        m_requiredRamGB = 1.;
        incompleteConfiguration = true;
    }

    if ( configurationMap.contains( "internetCheckUrl" ) &&
         configurationMap.value( "internetCheckUrl" ).type() == QVariant::String )
    {
        m_checkHasInternetUrl = configurationMap.value( "internetCheckUrl" ).toString().trimmed();
        if ( m_checkHasInternetUrl.isEmpty() ||
             !QUrl( m_checkHasInternetUrl ).isValid() )
        {
            cDebug() << "Invalid internetCheckUrl in welcome.conf" << m_checkHasInternetUrl
                     << "reverting to default (http://example.com).";
            m_checkHasInternetUrl = "http://example.com";
            incompleteConfiguration = true;
        }
    }
    else
    {
        cDebug() << "internetCheckUrl is undefined in welcome.conf, "
                    "reverting to default (http://example.com).";
        m_checkHasInternetUrl = "http://example.com";
        incompleteConfiguration = true;
    }

    if ( configurationMap.contains( "check" ) &&
         configurationMap.value( "check" ).type() == QVariant::List )
    {
        m_entriesToCheck.clear();
        m_entriesToCheck.append( configurationMap.value( "check" ).toStringList() );
    }
    else
        incompleteConfiguration = true;

    if ( configurationMap.contains( "required" ) &&
         configurationMap.value( "required" ).type() == QVariant::List )
    {
        m_entriesToRequire.clear();
        m_entriesToRequire.append( configurationMap.value( "required" ).toStringList() );
    }
    else
        incompleteConfiguration = true;

    if ( incompleteConfiguration )
    {
        cDebug() << "WARNING: The RequirementsChecker configuration map provided by "
                    "the welcome module configuration file is incomplete or "
                    "incorrect.\n"
                    "Startup will continue for debugging purposes, but one or "
                    "more checks might not function correctly.\n"
                    "RequirementsChecker configuration map:\n"
                 << configurationMap;
    }

    Calamares::GlobalStorage* globalStorage = Calamares::JobQueue::instance()->globalStorage();
    bool has_isorepo = QDir("/isorepo").exists("isorepo.db.tar.gz");
    globalStorage->insert( GS::HAS_ISOREPO_KEY, has_isorepo );
    if ( has_isorepo )
    {
        m_entriesToCheck.removeAll("internet") ;
        m_entriesToRequire.removeAll("internet") ;
    }
    else
    {
        m_entriesToCheck.append("internet") ;
        m_entriesToRequire.append("internet") ;
    }
    m_entriesToCheck.removeDuplicates() ;
    m_entriesToRequire.removeDuplicates() ;
}


bool
RequirementsChecker::verdict() const
{
    return m_verdict;
}


bool
RequirementsChecker::checkEnoughStorage( qint64 requiredSpace )
{
    return check_big_enough( requiredSpace );
}


bool
RequirementsChecker::checkEnoughRam( qint64 requiredRam )
{
    // Ignore the guesstimate-factor; we get an under-estimate
    // which is probably the usable RAM for programs.
    quint64 availableRam = CalamaresUtils::System::instance()->getTotalMemoryB().first;
    return availableRam >= requiredRam * 0.95; // because MemTotal is variable
}


bool
RequirementsChecker::checkBatteryExists()
{
    const QFileInfo basePath( "/sys/class/power_supply" );

    if ( !( basePath.exists() && basePath.isDir() ) )
        return false;

    QDir baseDir( basePath.absoluteFilePath() );
    const auto entries = baseDir.entryList( QDir::AllDirs | QDir::Readable | QDir::NoDotAndDotDot );
    for ( const auto &item : entries )
    {
        QFileInfo typePath( baseDir.absoluteFilePath( QString( "%1/type" )
                                                      .arg( item ) ) );
        QFile typeFile( typePath.absoluteFilePath() );
        if ( typeFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
        {
            if ( typeFile.readAll().startsWith( "Battery" ) )
                return true;
        }
    }

    return false;
}


bool
RequirementsChecker::checkHasPower()
{
    const QString UPOWER_SVC_NAME( "org.freedesktop.UPower" );
    const QString UPOWER_INTF_NAME( "org.freedesktop.UPower" );
    const QString UPOWER_PATH( "/org/freedesktop/UPower" );

    if ( !checkBatteryExists() )
        return true;

    cDebug() << "A battery exists, checking for mains power.";
    QDBusInterface upowerIntf( UPOWER_SVC_NAME,
                               UPOWER_PATH,
                               UPOWER_INTF_NAME,
                               QDBusConnection::systemBus() );

    bool onBattery = upowerIntf.property( "OnBattery" ).toBool();

    if ( !upowerIntf.isValid() )
    {
        // We can't talk to upower but we're obviously up and running
        // so I guess we got that going for us, which is nice...
        return true;
    }

    // If a battery exists but we're not using it, means we got mains
    // power.
    return !onBattery;
}


bool
RequirementsChecker::checkHasInternet()
{
    // default to true in the QNetworkAccessManager::UnknownAccessibility case
    QNetworkAccessManager qnam( this );
    bool hasInternet = qnam.networkAccessible() == QNetworkAccessManager::Accessible;

    if ( !hasInternet && qnam.networkAccessible() == QNetworkAccessManager::UnknownAccessibility )
    {
        QNetworkRequest req = QNetworkRequest( QUrl( m_checkHasInternetUrl ) );
        QNetworkReply* reply = qnam.get( req );
        QEventLoop loop;
        connect( reply, &QNetworkReply::finished,
                 &loop, &QEventLoop::quit );
        loop.exec();
        if( reply->bytesAvailable() )
            hasInternet = true;
    }

    Calamares::JobQueue::instance()->globalStorage()->insert( "hasInternet", hasInternet );
    return hasInternet;
}


bool
RequirementsChecker::checkIsRoot()
{
    return !geteuid();
}


void
RequirementsChecker::detectFirmwareType()
{
    QString fwType = QFile::exists( "/sys/firmware/efi/efivars" ) ? "efi" : "bios";
    Calamares::JobQueue::instance()->globalStorage()->insert( "firmwareType", fwType );
}