summaryrefslogtreecommitdiff
path: root/src/modules/partition/core/PartUtils.cpp
blob: 7b44d3d6497f8383e0cbd45c6c34579cb836c56a (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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/* === This file is part of Calamares - <https://github.com/calamares> ===
 *
 *   Copyright 2015-2016, Teo Mrnjavac <teo@kde.org>
 *   Copyright 2018-2019 Adriaan de Groot <groot@kde.org>
 *   Copyright 2019, Collabora Ltd <arnaud.ferraris@collabora.com>
 *
 *   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 "PartUtils.h"

#include "PartitionCoreModule.h"

#include "core/DeviceModel.h"
#include "core/KPMHelpers.h"
#include "core/PartitionInfo.h"
#include "core/PartitionIterator.h"

#include <kpmcore/backend/corebackend.h>
#include <kpmcore/backend/corebackendmanager.h>
#include <kpmcore/core/device.h>
#include <kpmcore/core/partition.h>

#include <utils/CalamaresUtilsSystem.h>
#include <utils/Logger.h>
#include <JobQueue.h>
#include <GlobalStorage.h>

#include <QProcess>
#include <QTemporaryDir>

namespace PartUtils
{

QString
convenienceName( const Partition* const candidate )
{
    if ( !candidate->mountPoint().isEmpty() )
        return candidate->mountPoint();
    if ( !candidate->partitionPath().isEmpty() )
        return candidate->partitionPath();
    if ( !candidate->devicePath().isEmpty() )
        return candidate->devicePath();
    if ( !candidate->deviceNode().isEmpty() )
        return candidate->devicePath();

    QString p;
    QTextStream s( &p );
    s << (void *)candidate;

    return p;
}

bool
canBeReplaced( Partition* candidate )
{
    if ( !candidate )
        return false;

    if ( candidate->isMounted() )
        return false;

    bool ok = false;
    double requiredStorageGB = Calamares::JobQueue::instance()
                                    ->globalStorage()
                                    ->value( "requiredStorageGiB" )
                                    .toDouble( &ok );

    qint64 availableStorageB = candidate->capacity();
    qint64 requiredStorageB = ( requiredStorageGB + 0.5 ) * 1024 * 1024 * 1024;
    cDebug() << "Required  storage B:" << requiredStorageB
             << QString( "(%1GB)" ).arg( requiredStorageB / 1024 / 1024 / 1024 );
    cDebug() << "Storage capacity  B:" << availableStorageB
             << QString( "(%1GB)" ).arg( availableStorageB / 1024 / 1024 / 1024 )
             << "for" << convenienceName( candidate ) << "   length:" << candidate->length();

    if ( ok &&
         availableStorageB > requiredStorageB )
    {
        cDebug() << "Partition" << convenienceName( candidate ) << "authorized for replace install.";

        return true;
    }
    return false;
}


bool
canBeResized( Partition* candidate )
{
    if ( !candidate )
    {
        cDebug() << "Partition* is NULL";
        return false;
    }

    cDebug() << "Checking if" << convenienceName( candidate ) << "can be resized.";
    if ( !candidate->fileSystem().supportGrow() ||
         !candidate->fileSystem().supportShrink() )
    {
        cDebug() << Logger::SubEntry << "NO, filesystem" << candidate->fileSystem().name()
            << "does not support resize.";
        return false;
    }

    if ( KPMHelpers::isPartitionFreeSpace( candidate ) )
    {
        cDebug() << Logger::SubEntry << "NO, partition is free space";
        return false;
    }

    if ( candidate->isMounted() )
    {
        cDebug() << Logger::SubEntry << "NO, partition is mounted";
        return false;
    }

    if ( candidate->roles().has( PartitionRole::Primary ) )
    {
        PartitionTable* table = dynamic_cast< PartitionTable* >( candidate->parent() );
        if ( !table )
        {
            cDebug() << Logger::SubEntry << "NO, no partition table found";
            return false;
        }

        if ( table->numPrimaries() >= table->maxPrimaries() )
        {
            cDebug() << Logger::SubEntry << "NO, partition table already has"
                << table->maxPrimaries() << "primary partitions.";
            return false;
        }
    }

    bool ok = false;
    double requiredStorageGB = Calamares::JobQueue::instance()
                                    ->globalStorage()
                                    ->value( "requiredStorageGiB" )
                                    .toDouble( &ok );
    // We require a little more for partitioning overhead and swap file
    double advisedStorageGB = requiredStorageGB + 0.5 + 2.0;
    qint64 availableStorageB = candidate->available();

    qint64 advisedStorageB = CalamaresUtils::GiBtoBytes( advisedStorageGB );

    if ( ok &&
         availableStorageB > advisedStorageB )
    {
        cDebug() << "Partition" << convenienceName( candidate ) << "authorized for resize + autopartition install.";

        return true;
    }
    else if ( ok )
    {
        Logger::CDebug deb;
        deb << Logger::SubEntry << "NO, insufficient storage";
        deb << Logger::Continuation << "Required  storage B:" << advisedStorageB
                << QString( "(%1GB)" ).arg( advisedStorageGB );
        deb << Logger::Continuation << "Available storage B:" << availableStorageB
                << QString( "(%1GB)" ).arg( availableStorageB / 1024 / 1024 / 1024 )
                << "for" << convenienceName( candidate ) << "length:" << candidate->length()
                << "sectorsUsed:" << candidate->sectorsUsed() << "fsType:" << candidate->fileSystem().name();
        return false;
    }
    else
    {
        cDebug() << Logger::SubEntry << "NO, requiredStorageGB is not set correctly.";
        return false;
    }
}


bool
canBeResized( PartitionCoreModule* core, const QString& partitionPath )
{
    cDebug() << "Checking if" << partitionPath << "can be resized.";
    QString partitionWithOs = partitionPath;
    if ( partitionWithOs.startsWith( "/dev/" ) )
    {
        DeviceModel* dm = core->deviceModel();
        for ( int i = 0; i < dm->rowCount(); ++i )
        {
            Device* dev = dm->deviceForIndex( dm->index( i ) );
            Partition* candidate = KPMHelpers::findPartitionByPath( { dev }, partitionWithOs );
            if ( candidate )
            {
                return canBeResized( candidate );
            }
        }
        cDebug() << Logger::SubEntry << "no Partition* found for" << partitionWithOs;
    }

    cDebug() << Logger::SubEntry << "Partition" << partitionWithOs << "CANNOT BE RESIZED FOR AUTOINSTALL.";
    return false;
}


static FstabEntryList
lookForFstabEntries( const QString& partitionPath )
{
    QStringList mountOptions{ "ro" };

    auto r = CalamaresUtils::System::runCommand(
        CalamaresUtils::System::RunLocation::RunInHost,
        { "blkid", "-s", "TYPE", "-o", "value", partitionPath }
    );
    if ( r.getExitCode() )
        cWarning() << "blkid on" << partitionPath << "failed.";
    else
    {
        QString fstype = r.getOutput().trimmed();
        if ( ( fstype == "ext3" ) || ( fstype == "ext4" ) )
            mountOptions.append( "noload" );
    }

    cDebug() << "Checking device" << partitionPath
        << "for fstab (fs=" << r.getOutput() << ')';

    FstabEntryList fstabEntries;
    QTemporaryDir mountsDir;
    mountsDir.setAutoRemove( false );

    int exit = QProcess::execute( "mount", { "-o", mountOptions.join(','), partitionPath, mountsDir.path() } );
    if ( !exit ) // if all is well
    {
        QFile fstabFile( mountsDir.path() + "/etc/fstab" );

        cDebug() << Logger::SubEntry << "reading" << fstabFile.fileName();

        if ( fstabFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
        {
            const QStringList fstabLines = QString::fromLocal8Bit( fstabFile.readAll() )
                                     .split( '\n' );

            for ( const QString& rawLine : fstabLines )
                fstabEntries.append( FstabEntry::fromEtcFstab( rawLine ) );
            fstabFile.close();
            cDebug() << Logger::SubEntry << "got" << fstabEntries.count() << "lines.";
            std::remove_if( fstabEntries.begin(), fstabEntries.end(), [](const FstabEntry& x) { return !x.isValid(); } );
            cDebug() << Logger::SubEntry << "got" << fstabEntries.count() << "fstab entries.";
        }
        else
            cWarning() << "Could not read fstab from mounted fs";

        if ( QProcess::execute( "umount", { "-R", mountsDir.path() } ) )
            cWarning() << "Could not unmount" << mountsDir.path();
    }
    else
        cWarning() << "Could not mount existing fs";

    return fstabEntries;
}


static QString
findPartitionPathForMountPoint( const FstabEntryList& fstab,
                                const QString& mountPoint )
{
    if ( fstab.isEmpty() )
        return QString();

    for ( const FstabEntry& entry : fstab )
    {
        if ( entry.mountPoint == mountPoint )
        {
            QProcess readlink;
            QString partPath;

            if ( entry.partitionNode.startsWith( "/dev" ) ) // plain dev node
            {
                partPath = entry.partitionNode;
            }
            else if ( entry.partitionNode.startsWith( "LABEL=" ) )
            {
                partPath = entry.partitionNode.mid( 6 );
                partPath.remove( "\"" );
                partPath.replace( "\\040", "\\ " );
                partPath.prepend( "/dev/disk/by-label/" );
            }
            else if ( entry.partitionNode.startsWith( "UUID=" ) )
            {
                partPath = entry.partitionNode.mid( 5 );
                partPath.remove( "\"" );
                partPath = partPath.toLower();
                partPath.prepend( "/dev/disk/by-uuid/" );
            }
            else if ( entry.partitionNode.startsWith( "PARTLABEL=" ) )
            {
                partPath = entry.partitionNode.mid( 10 );
                partPath.remove( "\"" );
                partPath.replace( "\\040", "\\ " );
                partPath.prepend( "/dev/disk/by-partlabel/" );
            }
            else if ( entry.partitionNode.startsWith( "PARTUUID=" ) )
            {
                partPath = entry.partitionNode.mid( 9 );
                partPath.remove( "\"" );
                partPath = partPath.toLower();
                partPath.prepend( "/dev/disk/by-partuuid/" );
            }

            // At this point we either have /dev/sda1, or /dev/disk/by-something/...

            if ( partPath.startsWith( "/dev/disk/by-" ) ) // we got a fancy node
            {
                readlink.start( "readlink", { "-en", partPath });
                if ( !readlink.waitForStarted( 1000 ) )
                    return QString();
                if ( !readlink.waitForFinished( 1000 ) )
                    return QString();
                if ( readlink.exitCode() != 0 || readlink.exitStatus() != QProcess::NormalExit )
                    return QString();
                partPath = QString::fromLocal8Bit(
                               readlink.readAllStandardOutput() ).trimmed();
            }

            return partPath;
        }
    }

    return QString();
}


OsproberEntryList
runOsprober( PartitionCoreModule* core )
{
    QString osproberOutput;
    QProcess osprober;
    osprober.setProgram( "os-prober" );
    osprober.setProcessChannelMode( QProcess::SeparateChannels );
    osprober.start();
    if ( !osprober.waitForStarted() )
    {
        cError() << "os-prober cannot start.";
    }
    else if ( !osprober.waitForFinished( 60000 ) )
    {
        cError() << "os-prober timed out.";
    }
    else
    {
        osproberOutput.append(
            QString::fromLocal8Bit(
                osprober.readAllStandardOutput() ).trimmed() );
    }

    QStringList osproberCleanLines;
    OsproberEntryList osproberEntries;
    const auto lines = osproberOutput.split( '\n' );
    for ( const QString& line : lines )
    {
        if ( !line.simplified().isEmpty() )
        {
            QStringList lineColumns = line.split( ':' );
            QString prettyName;
            if ( !lineColumns.value( 1 ).simplified().isEmpty() )
                prettyName = lineColumns.value( 1 ).simplified();
            else if ( !lineColumns.value( 2 ).simplified().isEmpty() )
                prettyName = lineColumns.value( 2 ).simplified();

            QString path = lineColumns.value( 0 ).simplified();
            if ( !path.startsWith( "/dev/" ) ) //basic sanity check
                continue;

            FstabEntryList fstabEntries = lookForFstabEntries( path );
            QString homePath = findPartitionPathForMountPoint( fstabEntries, "/home" );

            osproberEntries.append( { prettyName,
                                      path,
                                      QString(),
                                      canBeResized( core, path ),
                                      lineColumns,
                                      fstabEntries,
                                      homePath } );
            osproberCleanLines.append( line );
        }
    }

    if ( osproberCleanLines.count() > 0 )
        cDebug() << "os-prober lines after cleanup:" << Logger::DebugList( osproberCleanLines );
    else
        cDebug() << "os-prober gave no output.";

    Calamares::JobQueue::instance()->globalStorage()->insert( "osproberLines", osproberCleanLines );

    return osproberEntries;
}

bool
isEfiSystem()
{
    return QDir( "/sys/firmware/efi/efivars" ).exists();
}

bool
isEfiBootable( const Partition* candidate )
{
    cDebug() << "Check EFI bootable" << convenienceName( candidate ) << candidate->devicePath();
    cDebug() << Logger::SubEntry << "flags" << candidate->activeFlags();

    auto flags = PartitionInfo::flags( candidate );

    /* If bit 17 is set, old-style Esp flag, it's OK */
    if ( flags.testFlag( KPM_PARTITION_FLAG_ESP ) )
        return true;

    /* Otherwise, if it's a GPT table, Boot (bit 0) is the same as Esp */
    const PartitionNode* root = candidate;
    while ( root && !root->isRoot() )
    {
        root = root->parent();
        cDebug() << Logger::SubEntry << "moved towards root" << (void *)root;
    }

    // Strange case: no root found, no partition table node?
    if ( !root )
        return false;

    const PartitionTable* table = dynamic_cast<const PartitionTable*>( root );
    cDebug() << Logger::SubEntry << "partition table" << (void *)table << "type" << ( table ? table->type() : PartitionTable::TableType::unknownTableType );
    return table && ( table->type() == PartitionTable::TableType::gpt ) &&
        flags.testFlag( KPM_PARTITION_FLAG(Boot) );
}

QString
findFS( QString fsName, FileSystem::Type* fsType )
{
    QStringList fsLanguage { QLatin1Literal( "C" ) };  // Required language list to turn off localization
    if ( fsName.isEmpty() )
        fsName = QStringLiteral( "ext4" );

    FileSystem::Type tmpType = FileSystem::typeForName( fsName, fsLanguage );
    if ( tmpType != FileSystem::Unknown )
    {
        cDebug() << "Found filesystem" << fsName;
        if ( fsType )
            *fsType = tmpType;
        return fsName;
    }

    // Second pass: try case-insensitive
    const auto fstypes = FileSystem::types();
    for ( FileSystem::Type t : fstypes )
    {
        if ( 0 == QString::compare( fsName, FileSystem::nameForType( t, fsLanguage ), Qt::CaseInsensitive ) )
        {
            QString fsRealName = FileSystem::nameForType( t, fsLanguage );
            cDebug() << "Filesystem name" << fsName << "translated to" << fsRealName;
            if ( fsType )
                *fsType = t;
            return fsRealName;
        }
    }

    cDebug() << "Filesystem" << fsName << "not found, using ext4";
    fsName = QStringLiteral( "ext4" );
    // fsType can be used to check whether fsName was a valid filesystem.
    if (fsType)
        *fsType = FileSystem::Unknown;
#ifdef DEBUG_FILESYSTEMS
    // This bit is for distro's debugging their settings, and shows
    // all the strings that KPMCore is matching against for FS type.
    {
        Logger::CDebug d;
        using TR = Logger::DebugRow< int, QString >;
        const auto fstypes = FileSystem::types();
        d << "Available types (" << fstypes.count() << ')';
        for ( FileSystem::Type t : fstypes )
            d << TR( static_cast<int>( t ), FileSystem::nameForType( t, fsLanguage ) );
    }
#endif
    return fsName;
}

}  // nmamespace PartUtils

/* Implementation of methods for FstabEntry, from OsproberEntry.h */

bool
FstabEntry::isValid() const
{
    return !partitionNode.isEmpty() && !mountPoint.isEmpty() && !fsType.isEmpty();
}

FstabEntry
FstabEntry::fromEtcFstab( const QString& rawLine )
{
    QString line = rawLine.simplified();
    if ( line.startsWith( '#' ) )
        return FstabEntry{ QString(), QString(), QString(), QString(), 0, 0 };

    QStringList splitLine = line.split( ' ' );
    if ( splitLine.length() != 6 )
        return FstabEntry{ QString(), QString(), QString(), QString(), 0, 0 };

    return FstabEntry{ splitLine.at( 0 ), // path, or UUID, or LABEL, etc.
                       splitLine.at( 1 ), // mount point
                       splitLine.at( 2 ), // fs type
                       splitLine.at( 3 ), // options
                       splitLine.at( 4 ).toInt(), //dump
                       splitLine.at( 5 ).toInt()  //pass
                       };
 }