From 5358e2314dac247f1552de802650d48d085a64e8 Mon Sep 17 00:00:00 2001 From: Arnaud Ferraris Date: Mon, 8 Apr 2019 12:19:35 +0200 Subject: [PATCH] [partition] Add max size parameter When using a custom partition layout with partition sizes in %, it can be useful to set an upper limit to the partition size. For instance, using a 20% size for the `/` partition will create a 24G partition on a 120GB drive, but a 200GB partition on a 1TB drive, which is not useful, and could be avoided by setting a maximum partition size. This commit adds the `maxSize` parameter (with a default value of 100%). Signed-off-by: Arnaud Ferraris --- .../partition/core/PartitionCoreModule.cpp | 9 ++++++++- src/modules/partition/core/PartitionLayout.cpp | 17 +++++++++++------ src/modules/partition/core/PartitionLayout.h | 10 ++++++---- src/modules/partition/partition.conf | 2 ++ 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/modules/partition/core/PartitionCoreModule.cpp b/src/modules/partition/core/PartitionCoreModule.cpp index 07cb0fcfd..6b43756f2 100644 --- a/src/modules/partition/core/PartitionCoreModule.cpp +++ b/src/modules/partition/core/PartitionCoreModule.cpp @@ -774,6 +774,7 @@ PartitionCoreModule::initLayout( const QVariantList& config ) { QString sizeString; QString minSizeString; + QString maxSizeString; m_partLayout = new PartitionLayout(); @@ -791,11 +792,17 @@ PartitionCoreModule::initLayout( const QVariantList& config ) else minSizeString = CalamaresUtils::getString( pentry, "minSize" ); + if ( pentry.contains("maxSize") && CalamaresUtils::getString( pentry, "maxSize" ).isEmpty() ) + maxSizeString.setNum( CalamaresUtils::getInteger( pentry, "maxSize", 100 ) ); + else + maxSizeString = CalamaresUtils::getString( pentry, "maxSize" ); + m_partLayout->addEntry( CalamaresUtils::getString( pentry, "name" ), CalamaresUtils::getString( pentry, "mountPoint" ), CalamaresUtils::getString( pentry, "filesystem" ), sizeString, - minSizeString + minSizeString, + maxSizeString ); } } diff --git a/src/modules/partition/core/PartitionLayout.cpp b/src/modules/partition/core/PartitionLayout.cpp index 4f62db7d6..c88b8df2a 100644 --- a/src/modules/partition/core/PartitionLayout.cpp +++ b/src/modules/partition/core/PartitionLayout.cpp @@ -75,17 +75,19 @@ PartitionLayout::addEntry( PartitionLayout::PartitionEntry entry ) m_partLayout.append( entry ); } -PartitionLayout::PartitionEntry::PartitionEntry(const QString& size, const QString& min) +PartitionLayout::PartitionEntry::PartitionEntry( const QString& size, const QString& min, const QString& max ) { partSize = PartUtils::parseSizeString( size , &partSizeUnit ); if ( !min.isEmpty() ) partMinSize = PartUtils::parseSizeString( min , &partMinSizeUnit ); + if ( !max.isEmpty() ) + partMaxSize = PartUtils::parseSizeString( max , &partMaxSizeUnit ); } void -PartitionLayout::addEntry( const QString& mountPoint, const QString& size, const QString& min ) +PartitionLayout::addEntry( const QString& mountPoint, const QString& size, const QString& min, const QString& max ) { - PartitionLayout::PartitionEntry entry( size, min ); + PartitionLayout::PartitionEntry entry( size, min, max ); entry.partMountPoint = mountPoint; entry.partFileSystem = m_defaultFsType; @@ -94,9 +96,9 @@ PartitionLayout::addEntry( const QString& mountPoint, const QString& size, const } void -PartitionLayout::addEntry( const QString& label, const QString& mountPoint, const QString& fs, const QString& size, const QString& min ) +PartitionLayout::addEntry( const QString& label, const QString& mountPoint, const QString& fs, const QString& size, const QString& min, const QString& max ) { - PartitionLayout::PartitionEntry entry( size, min ); + PartitionLayout::PartitionEntry entry( size, min, max ); entry.partLabel = label; entry.partMountPoint = mountPoint; @@ -114,7 +116,7 @@ PartitionLayout::execute( Device *dev, qint64 firstSector, const PartitionRole& role ) { QList< Partition* > partList; - qint64 size, minSize, end; + qint64 size, minSize, maxSize, end; qint64 totalSize = lastSector - firstSector + 1; qint64 availableSize = totalSize; @@ -128,8 +130,11 @@ PartitionLayout::execute( Device *dev, qint64 firstSector, // Calculate partition size size = PartUtils::sizeToSectors( part.partSize, part.partSizeUnit, totalSize, dev->logicalSize() ); minSize = PartUtils::sizeToSectors( part.partMinSize, part.partMinSizeUnit, totalSize, dev->logicalSize() ); + maxSize = PartUtils::sizeToSectors( part.partMaxSize, part.partMaxSizeUnit, totalSize, dev->logicalSize() ); if ( size < minSize ) size = minSize; + if ( size > maxSize ) + size = maxSize; if ( size > availableSize ) size = availableSize; end = firstSector + size - 1; diff --git a/src/modules/partition/core/PartitionLayout.h b/src/modules/partition/core/PartitionLayout.h index ab597734c..fd6075ba0 100644 --- a/src/modules/partition/core/PartitionLayout.h +++ b/src/modules/partition/core/PartitionLayout.h @@ -47,11 +47,13 @@ public: PartUtils::SizeUnit partSizeUnit = PartUtils::SizeUnit::Percent; double partMinSize = 0.0L; PartUtils::SizeUnit partMinSizeUnit = PartUtils::SizeUnit::Percent; + double partMaxSize = 100.0L; + PartUtils::SizeUnit partMaxSizeUnit = PartUtils::SizeUnit::Percent; /// @brief All-zeroes PartitionEntry PartitionEntry() {}; - /// @brief Parse @p size and @p min to their respective member variables - PartitionEntry( const QString& size, const QString& min ); + /// @brief Parse @p size, @p min and @p max to their respective member variables + PartitionEntry( const QString& size, const QString& min, const QString& max ); }; PartitionLayout(); @@ -60,8 +62,8 @@ public: ~PartitionLayout(); void addEntry( PartitionEntry entry ); - void addEntry( const QString& mountPoint, const QString& size, const QString& min = QString() ); - void addEntry( const QString& label, const QString& mountPoint, const QString& fs, const QString& size, const QString& min = QString() ); + void addEntry( const QString& mountPoint, const QString& size, const QString& min = QString(), const QString& max = QString() ); + void addEntry( const QString& label, const QString& mountPoint, const QString& fs, const QString& size, const QString& min = QString(), const QString& max = QString() ); /** * @brief Apply the current partition layout to the selected drive space. diff --git a/src/modules/partition/partition.conf b/src/modules/partition/partition.conf index 66f40020c..b9262e86f 100644 --- a/src/modules/partition/partition.conf +++ b/src/modules/partition/partition.conf @@ -100,6 +100,7 @@ defaultFileSystemType: "ext4" # mountPoint: "/" # size: 20% # minSize: 500M +# maxSize: 10G # - name: "home" # filesystem: "ext4" # mountPoint: "/home" @@ -118,3 +119,4 @@ defaultFileSystemType: "ext4" # or # % of the available drive space if a '%' is appended to the value # - minSize: minimum partition size (optional parameter) +# - maxSize: maximum partition size (optional parameter)