source: calamares/trunk/fuentes/src/modules/partition/core/PartitionActions.cpp @ 7538

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

sync with github

File size: 10.1 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, Adriaan de Groot <groot@kde.org>
5 *
6 *   Calamares is free software: you can redistribute it and/or modify
7 *   it under the terms of the GNU General Public License as published by
8 *   the Free Software Foundation, either version 3 of the License, or
9 *   (at your option) any later version.
10 *
11 *   Calamares is distributed in the hope that it will be useful,
12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *   GNU General Public License for more details.
15 *
16 *   You should have received a copy of the GNU General Public License
17 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "PartitionActions.h"
21
22#include "core/KPMHelpers.h"
23#include "core/PartitionInfo.h"
24#include "core/PartitionCoreModule.h"
25#include "core/PartUtils.h"
26
27#include "utils/CalamaresUtilsSystem.h"
28#include "utils/Units.h"
29#include "JobQueue.h"
30#include "utils/Logger.h"
31#include "GlobalStorage.h"
32
33#include <kpmcore/core/device.h>
34#include <kpmcore/core/partition.h>
35
36#include <QDir>
37
38namespace PartitionActions
39{
40using CalamaresUtils::GiBtoBytes;
41using CalamaresUtils::MiBtoBytes;
42using CalamaresUtils::operator""_GiB;
43using CalamaresUtils::operator""_MiB;
44
45qint64
46swapSuggestion( const qint64 availableSpaceB )
47{
48    /* If suspend-to-disk is demanded, then we always need enough
49     * swap to write the whole memory to disk -- between 2GB and 8GB
50     * RAM give proportionally more swap, and from 8GB RAM keep
51     * swap = RAM.
52     *
53     * If suspend-to-disk is not demanded, then ramp up more slowly,
54     * to 8GB swap at 16GB memory, and then drop to 4GB for "large
55     * memory" machines, on the assumption that those don't need swap
56     * because they have tons of memory (or whatever they are doing,
57     * had better not run into swap).
58     */
59    qint64 suggestedSwapSizeB = 0;
60    auto memory = CalamaresUtils::System::instance()->getTotalMemoryB();
61    qint64 availableRamB = memory.first;
62    qreal overestimationFactor = memory.second;
63
64    bool ensureSuspendToDisk =
65        Calamares::JobQueue::instance()->globalStorage()->
66            value( "ensureSuspendToDisk" ).toBool();
67
68    if ( ensureSuspendToDisk )
69    {
70        if ( availableRamB < 4_GiB )
71            suggestedSwapSizeB = qMax( 2_GiB, availableRamB * 2 );
72        else if ( availableRamB >= 4_GiB && availableRamB < 8_GiB )
73            suggestedSwapSizeB = 8_GiB;
74        else
75            suggestedSwapSizeB = availableRamB;
76
77        suggestedSwapSizeB *= overestimationFactor;
78    }
79    else //if we don't care about suspend to disk
80    {
81        if ( availableRamB < 2_GiB )
82            suggestedSwapSizeB = qMax( 2_GiB, availableRamB * 2 );
83        else if ( availableRamB >= 2_GiB && availableRamB < 8_GiB )
84            suggestedSwapSizeB = availableRamB;
85        else if ( availableRamB >= 8_GiB && availableRamB < 16_GiB )
86            suggestedSwapSizeB = 8_GiB;
87        else
88            suggestedSwapSizeB = 4_GiB;
89
90        suggestedSwapSizeB *= overestimationFactor;
91
92        // don't use more than 10% of available space
93        qreal maxSwapDiskRatio = 1.10;
94        qint64 maxSwapSizeB = availableSpaceB * maxSwapDiskRatio;
95        if ( suggestedSwapSizeB > maxSwapSizeB )
96            suggestedSwapSizeB = maxSwapSizeB;
97    }
98
99    cDebug() << "Suggested swap size:" << suggestedSwapSizeB / 1024. / 1024. /1024. << "GiB";
100
101    return suggestedSwapSizeB;
102}
103
104
105void
106doAutopartition( PartitionCoreModule* core, Device* dev, const QString& luksPassphrase )
107{
108    Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
109
110    bool isEfi = PartUtils::isEfiSystem();
111
112    QString defaultFsType = gs->value( "defaultFileSystemType" ).toString();
113    if ( FileSystem::typeForName( defaultFsType ) == FileSystem::Unknown )
114        defaultFsType = "ext4";
115
116    // Partition sizes are expressed in MiB, should be multiples of
117    // the logical sector size (usually 512B).
118    int uefisys_part_size = 0;
119    int empty_space_size = 0;
120    if ( isEfi )
121    {
122        uefisys_part_size = 300;
123        empty_space_size = 2;
124    }
125    else
126    {
127        // we start with a 1MiB offset before the first partition
128        empty_space_size = 1;
129    }
130
131    qint64 firstFreeSector = MiBtoBytes(empty_space_size) / dev->logicalSize() + 1;
132
133    if ( isEfi )
134    {
135        qint64 lastSector = firstFreeSector + ( MiBtoBytes(uefisys_part_size) / dev->logicalSize() );
136        core->createPartitionTable( dev, PartitionTable::gpt );
137        Partition* efiPartition = KPMHelpers::createNewPartition(
138            dev->partitionTable(),
139            *dev,
140            PartitionRole( PartitionRole::Primary ),
141            FileSystem::Fat32,
142            firstFreeSector,
143            lastSector,
144            PartitionTable::FlagEsp
145        );
146        PartitionInfo::setFormat( efiPartition, true );
147        PartitionInfo::setMountPoint( efiPartition, gs->value( "efiSystemPartition" )
148                                                        .toString() );
149        core->createPartition( dev, efiPartition, PartitionTable::FlagEsp | PartitionTable::FlagBoot );
150        firstFreeSector = lastSector + 1;
151    }
152    else
153    {
154        core->createPartitionTable( dev, PartitionTable::msdos );
155    }
156
157    const bool mayCreateSwap = !gs->value( "neverCreateSwap" ).toBool();
158    bool shouldCreateSwap = false;
159    qint64 suggestedSwapSizeB = 0;
160
161    if ( mayCreateSwap )
162    {
163        qint64 availableSpaceB = ( dev->totalLogical() - firstFreeSector ) * dev->logicalSize();
164        suggestedSwapSizeB = swapSuggestion( availableSpaceB );
165        qint64 requiredSpaceB =
166                GiBtoBytes( gs->value( "requiredStorageGB" ).toDouble() + 0.1 + 2.0 ) +
167                suggestedSwapSizeB;
168
169        // If there is enough room for ESP + root + swap, create swap, otherwise don't.
170        shouldCreateSwap = availableSpaceB > requiredSpaceB;
171    }
172
173    qint64 lastSectorForRoot = dev->totalLogical() - 1; //last sector of the device
174    if ( shouldCreateSwap )
175    {
176        lastSectorForRoot -= suggestedSwapSizeB / dev->logicalSize() + 1;
177    }
178
179    Partition* rootPartition = nullptr;
180    if ( luksPassphrase.isEmpty() )
181    {
182        rootPartition = KPMHelpers::createNewPartition(
183            dev->partitionTable(),
184            *dev,
185            PartitionRole( PartitionRole::Primary ),
186            FileSystem::typeForName( defaultFsType ),
187            firstFreeSector,
188            lastSectorForRoot
189        );
190    }
191    else
192    {
193        rootPartition = KPMHelpers::createNewEncryptedPartition(
194            dev->partitionTable(),
195            *dev,
196            PartitionRole( PartitionRole::Primary ),
197            FileSystem::typeForName( defaultFsType ),
198            firstFreeSector,
199            lastSectorForRoot,
200            luksPassphrase
201       );
202    }
203    PartitionInfo::setFormat( rootPartition, true );
204    PartitionInfo::setMountPoint( rootPartition, "/" );
205    core->createPartition( dev, rootPartition );
206
207    if ( shouldCreateSwap )
208    {
209        Partition* swapPartition = nullptr;
210        if ( luksPassphrase.isEmpty() )
211        {
212            swapPartition = KPMHelpers::createNewPartition(
213                dev->partitionTable(),
214                *dev,
215                PartitionRole( PartitionRole::Primary ),
216                FileSystem::LinuxSwap,
217                lastSectorForRoot + 1,
218                dev->totalLogical() - 1
219            );
220        }
221        else
222        {
223            swapPartition = KPMHelpers::createNewEncryptedPartition(
224                dev->partitionTable(),
225                *dev,
226                PartitionRole( PartitionRole::Primary ),
227                FileSystem::LinuxSwap,
228                lastSectorForRoot + 1,
229                dev->totalLogical() - 1,
230                luksPassphrase
231            );
232        }
233        PartitionInfo::setFormat( swapPartition, true );
234        core->createPartition( dev, swapPartition );
235    }
236
237    core->dumpQueue();
238}
239
240
241void
242doReplacePartition( PartitionCoreModule* core,
243                    Device* dev,
244                    Partition* partition,
245                    const QString& luksPassphrase )
246{
247    cDebug() << "doReplacePartition for device" << partition->partitionPath();
248
249    QString defaultFsType = Calamares::JobQueue::instance()->
250                                globalStorage()->
251                                value( "defaultFileSystemType" ).toString();
252    if ( FileSystem::typeForName( defaultFsType ) == FileSystem::Unknown )
253        defaultFsType = "ext4";
254
255    PartitionRole newRoles( partition->roles() );
256    if ( partition->roles().has( PartitionRole::Extended ) )
257        newRoles = PartitionRole( PartitionRole::Primary );
258
259    if ( partition->roles().has( PartitionRole::Unallocated ) )
260    {
261        newRoles = PartitionRole( PartitionRole::Primary );
262        cWarning() << "selected partition is free space";
263        if ( partition->parent() )
264        {
265            Partition* parent = dynamic_cast< Partition* >( partition->parent() );
266            if ( parent && parent->roles().has( PartitionRole::Extended ) )
267                newRoles = PartitionRole( PartitionRole::Logical );
268        }
269    }
270
271    Partition* newPartition = nullptr;
272    if ( luksPassphrase.isEmpty() )
273    {
274        newPartition = KPMHelpers::createNewPartition(
275            partition->parent(),
276            *dev,
277            newRoles,
278            FileSystem::typeForName( defaultFsType ),
279            partition->firstSector(),
280            partition->lastSector()
281        );
282    }
283    else
284    {
285        newPartition = KPMHelpers::createNewEncryptedPartition(
286            partition->parent(),
287            *dev,
288            newRoles,
289            FileSystem::typeForName( defaultFsType ),
290            partition->firstSector(),
291            partition->lastSector(),
292            luksPassphrase
293        );
294    }
295    PartitionInfo::setMountPoint( newPartition, "/" );
296    PartitionInfo::setFormat( newPartition, true );
297
298    if ( !partition->roles().has( PartitionRole::Unallocated ) )
299        core->deletePartition( dev, partition );
300    core->createPartition( dev, newPartition );
301
302    core->dumpQueue();
303}
304
305}
Note: See TracBrowser for help on using the repository browser.