From 0b4c0f9c3896930544f89fe7931094042eb75615 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 1 Oct 2018 04:06:01 -0400 Subject: [PATCH 1/4] [fsresizer] Add setting required - If resize is required, fail if it doesn't happen. --- src/modules/fsresizer/ResizeFSJob.cpp | 14 +++++++++----- src/modules/fsresizer/ResizeFSJob.h | 9 +++++---- src/modules/fsresizer/fsresizer.conf | 16 ++++++++++++++-- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/modules/fsresizer/ResizeFSJob.cpp b/src/modules/fsresizer/ResizeFSJob.cpp index 0589e1866..254e80adb 100644 --- a/src/modules/fsresizer/ResizeFSJob.cpp +++ b/src/modules/fsresizer/ResizeFSJob.cpp @@ -33,6 +33,7 @@ #include "JobQueue.h" #include "GlobalStorage.h" +#include "utils/CalamaresUtils.h" #include "utils/Logger.h" #include "utils/Units.h" @@ -68,7 +69,7 @@ ResizeFSJob::RelativeSize::RelativeSize( const QString& s) { matchUnitSuffix( s, "%", Percent, m_value, m_unit ); matchUnitSuffix( s, "MiB", Absolute, m_value, m_unit ); - + if ( ( unit() == Percent ) && ( value() > 100 ) ) { cDebug() << "Percent value" << value() << "is not valid."; @@ -87,7 +88,7 @@ ResizeFSJob::RelativeSize::apply( qint64 totalSectors , qint64 sectorSize ) return -1; if ( sectorSize < 1 ) return -1; - + switch( m_unit ) { case None: @@ -113,6 +114,7 @@ ResizeFSJob::RelativeSize::apply( Device* d ) ResizeFSJob::ResizeFSJob( QObject* parent ) : Calamares::CppJob( parent ) + , m_required( false ) { } @@ -156,7 +158,7 @@ ResizeFSJob::findPartition( CoreBackend* backend ) } /** @brief Returns the last sector the matched partition should occupy. - * + * * Returns a sector number. Returns -1 if something is wrong (e.g. * can't resize at all, or missing data). Returns 0 if the resize * won't fit because it doesn't satisfy the settings for atleast @@ -223,7 +225,7 @@ ResizeFSJob::findGrownEnd(ResizeFSJob::PartitionMatch m) cDebug() << ".. only growing by" << wanted << "instead of full" << expand; last_available -= ( expand - wanted ); } - + return last_available; } @@ -300,7 +302,7 @@ ResizeFSJob::exec() << "skipped as not-useful."; return Calamares::JobResult::ok(); } - + if ( ( new_end > 0 ) && ( new_end > m.second->lastSector() ) ) { ResizeOperation op( *m.first, *m.second, m.second->firstSector(), new_end ); @@ -334,6 +336,8 @@ ResizeFSJob::setConfigurationMap( const QVariantMap& configurationMap ) m_size = RelativeSize( configurationMap["size"].toString() ); m_atleast = RelativeSize( configurationMap["atleast"].toString() ); + + m_required = CalamaresUtils::getBool( configurationMap, "required", false ); } CALAMARES_PLUGIN_FACTORY_DEFINITION( ResizeFSJobFactory, registerPlugin(); ) diff --git a/src/modules/fsresizer/ResizeFSJob.h b/src/modules/fsresizer/ResizeFSJob.h index 063495e7d..c34ccb865 100644 --- a/src/modules/fsresizer/ResizeFSJob.h +++ b/src/modules/fsresizer/ResizeFSJob.h @@ -68,19 +68,19 @@ public: * * Each sector has size @p sectorSize , for converting absolute * sizes in MiB to sector counts. - * + * * For invalid sizes, returns -1. * For absolute sizes, returns the number of sectors needed. * For percent sizes, returns that percent of the number of sectors. */ qint64 apply( qint64 totalSectors, qint64 sectorSize ); - + /** @brief Apply this size to the given device. - * + * * Equivalent to apply( d->totalLogical(), d->logicalSize() ) */ qint64 apply( Device* d ); - + private: int m_value; Unit m_unit; @@ -107,6 +107,7 @@ private: RelativeSize m_atleast; QString m_fsname; // Either this, or devicename, is set, not both QString m_devicename; + bool m_required; using PartitionMatch = QPair; /** @brief Find the configured FS using KPMCore @p backend */ diff --git a/src/modules/fsresizer/fsresizer.conf b/src/modules/fsresizer/fsresizer.conf index 06141fc7d..33329248d 100644 --- a/src/modules/fsresizer/fsresizer.conf +++ b/src/modules/fsresizer/fsresizer.conf @@ -12,7 +12,7 @@ # Which FS needs to be grown? Choose one way to identify it: # - *fs* names a mount point which should already be mounted # in the system. -# - *dev* names a device +# - *dev* names a device fs: / # dev: /dev/mmcblk0p1 @@ -33,5 +33,17 @@ size: 100% # size, as above. If missing, then it's assumed to be 0, # which means resizing is always worthwhile. # +# If *atleast* is not zero, then the setting *required*, +# below, becomes relevant. +# # Percentages apply to **total device size**. -atleast: 1000MiB +#atleast: 1000MiB + +# When *atleast* is not zero, then the resize may be +# recommended (the default) or **required**. If the +# resize is required and cannot be carried out (because +# there's not enough space), then that is a fatal +# error for the installer. By default, resize is only +# recommended and it is not an error for no resize to be +# carried out. +required: false From eb7c6385cdce9c76c27bdb6c6518a4e407e2e357 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 1 Oct 2018 04:14:15 -0400 Subject: [PATCH 2/4] [fsresizer] Bail out if resize required but not feasible --- src/modules/fsresizer/ResizeFSJob.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/modules/fsresizer/ResizeFSJob.cpp b/src/modules/fsresizer/ResizeFSJob.cpp index 254e80adb..886ce803a 100644 --- a/src/modules/fsresizer/ResizeFSJob.cpp +++ b/src/modules/fsresizer/ResizeFSJob.cpp @@ -297,9 +297,14 @@ ResizeFSJob::exec() : tr( "The device %1 can not be resized." ).arg(m_devicename) ); if ( new_end == 0 ) { - // TODO: is that a bad thing? is the resize required? cWarning() << "Resize operation on" << m_fsname << m_devicename << "skipped as not-useful."; + if ( m_required ) + return Calamares::JobResult::error( + tr( "Resize Failed" ), + !m_fsname.isEmpty() ? tr( "The filesystem %1 must be resized, but cannot." ).arg(m_fsname) + : tr( "The device %11 must be resized, but cannot" ).arg(m_fsname) ); + return Calamares::JobResult::ok(); } From 39902d1c3f4dcdea42fa7748723d2d508e45cb70 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 1 Oct 2018 04:15:09 -0400 Subject: [PATCH 3/4] [fsresizer] "can not" -> "cannot" --- src/modules/fsresizer/ResizeFSJob.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/modules/fsresizer/ResizeFSJob.cpp b/src/modules/fsresizer/ResizeFSJob.cpp index 886ce803a..af0cb7bd0 100644 --- a/src/modules/fsresizer/ResizeFSJob.cpp +++ b/src/modules/fsresizer/ResizeFSJob.cpp @@ -204,7 +204,7 @@ ResizeFSJob::findGrownEnd(ResizeFSJob::PartitionMatch m) if ( !( last_available > last_currently ) ) { - cDebug() << "Partition can not grow larger."; + cDebug() << "Partition cannot grow larger."; return 0; } @@ -271,8 +271,8 @@ ResizeFSJob::exec() if ( !m.first || !m.second ) return Calamares::JobResult::error( tr( "Resize Failed" ), - !m_fsname.isEmpty() ? tr( "The filesystem %1 could not be found in this system, and can not be resized." ).arg(m_fsname) - : tr( "The device %1 could not be found in this system, and can not be resized." ).arg(m_devicename) ); + !m_fsname.isEmpty() ? tr( "The filesystem %1 could not be found in this system, and cannot be resized." ).arg(m_fsname) + : tr( "The device %1 could not be found in this system, and cannot be resized." ).arg(m_devicename) ); m.second->fileSystem().init(); // Initialize support for specific FS if ( !ResizeOperation::canGrow( m.second ) ) @@ -280,8 +280,8 @@ ResizeFSJob::exec() cDebug() << "canGrow() returned false."; return Calamares::JobResult::error( tr( "Resize Failed" ), - !m_fsname.isEmpty() ? tr( "The filesystem %1 can not be resized." ).arg(m_fsname) - : tr( "The device %1 can not be resized." ).arg(m_devicename) ); + !m_fsname.isEmpty() ? tr( "The filesystem %1 cannot be resized." ).arg(m_fsname) + : tr( "The device %1 cannot be resized." ).arg(m_devicename) ); } qint64 new_end = findGrownEnd( m ); @@ -293,8 +293,8 @@ ResizeFSJob::exec() if ( new_end < 0 ) return Calamares::JobResult::error( tr( "Resize Failed" ), - !m_fsname.isEmpty() ? tr( "The filesystem %1 can not be resized." ).arg(m_fsname) - : tr( "The device %1 can not be resized." ).arg(m_devicename) ); + !m_fsname.isEmpty() ? tr( "The filesystem %1 cannot be resized." ).arg(m_fsname) + : tr( "The device %1 cannot be resized." ).arg(m_devicename) ); if ( new_end == 0 ) { cWarning() << "Resize operation on" << m_fsname << m_devicename From 21fedfce65a5ef7f1f4c41914389e4b4dd91366b Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 1 Oct 2018 04:22:25 -0400 Subject: [PATCH 4/4] [fsresizer] Calamares oding style --- src/modules/fsresizer/ResizeFSJob.cpp | 51 +++++++++++++-------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/src/modules/fsresizer/ResizeFSJob.cpp b/src/modules/fsresizer/ResizeFSJob.cpp index af0cb7bd0..4320baba1 100644 --- a/src/modules/fsresizer/ResizeFSJob.cpp +++ b/src/modules/fsresizer/ResizeFSJob.cpp @@ -49,11 +49,11 @@ ResizeFSJob::RelativeSize::RelativeSize() template void matchUnitSuffix( const QString& s, - const char (&suffix)[N], + const char ( &suffix )[N], ResizeFSJob::RelativeSize::Unit matchedUnit, int& value, ResizeFSJob::RelativeSize::Unit& unit - ) +) { if ( s.endsWith( suffix ) ) { @@ -63,7 +63,7 @@ void matchUnitSuffix( } -ResizeFSJob::RelativeSize::RelativeSize( const QString& s) +ResizeFSJob::RelativeSize::RelativeSize( const QString& s ) : m_value( 0 ) , m_unit( None ) { @@ -82,14 +82,14 @@ ResizeFSJob::RelativeSize::RelativeSize( const QString& s) } qint64 -ResizeFSJob::RelativeSize::apply( qint64 totalSectors , qint64 sectorSize ) +ResizeFSJob::RelativeSize::apply( qint64 totalSectors, qint64 sectorSize ) { if ( !isValid() ) return -1; if ( sectorSize < 1 ) return -1; - switch( m_unit ) + switch ( m_unit ) { case None: return -1; @@ -138,14 +138,14 @@ ResizeFSJob::findPartition( CoreBackend* backend ) cDebug() << "ResizeFSJob found" << devices.count() << "devices."; for ( DeviceList::iterator dev_it = devices.begin(); dev_it != devices.end(); ++dev_it ) { - if ( ! (*dev_it) ) + if ( ! ( *dev_it ) ) continue; cDebug() << "ResizeFSJob found" << ( *dev_it )->deviceNode(); for ( auto part_it = PartitionIterator::begin( *dev_it ); part_it != PartitionIterator::end( *dev_it ); ++part_it ) { cDebug() << ".." << ( *part_it )->mountPoint() << "on" << ( *part_it )->deviceNode(); if ( ( !m_fsname.isEmpty() && ( *part_it )->mountPoint() == m_fsname ) || - ( !m_devicename.isEmpty() && ( *part_it )->deviceNode() == m_devicename ) ) + ( !m_devicename.isEmpty() && ( *part_it )->deviceNode() == m_devicename ) ) { cDebug() << ".. matched configuration dev=" << m_devicename << "fs=" << m_fsname; return PartitionMatch( *dev_it, *part_it ); @@ -166,7 +166,7 @@ ResizeFSJob::findPartition( CoreBackend* backend ) * by occupied space after it). */ qint64 -ResizeFSJob::findGrownEnd(ResizeFSJob::PartitionMatch m) +ResizeFSJob::findGrownEnd( ResizeFSJob::PartitionMatch m ) { if ( !m.first || !m.second ) return -1; // Missing device data @@ -195,7 +195,7 @@ ResizeFSJob::findGrownEnd(ResizeFSJob::PartitionMatch m) continue; } cDebug() << ".. comparing" << next_start << '-' << next_end; - if ( (next_start > last_currently) && (next_start < last_available) ) + if ( ( next_start > last_currently ) && ( next_start < last_available ) ) { cDebug() << " .. shrunk last available to" << next_start; last_available = next_start - 1; // Before that one starts @@ -236,13 +236,12 @@ ResizeFSJob::exec() if ( !isValid() ) return Calamares::JobResult::error( tr( "Invalid configuration" ), - tr( "The file-system resize job has an invalid configuration " - "and will not run." ) ); + tr( "The file-system resize job has an invalid configuration and will not run." ) ); // Get KPMCore auto backend_p = CoreBackendManager::self()->backend(); if ( backend_p ) - cDebug() << "KPMCore backend @" << (void *)backend_p << backend_p->id() << backend_p->version(); + cDebug() << "KPMCore backend @" << ( void* )backend_p << backend_p->id() << backend_p->version(); else { cDebug() << "No KPMCore backend loaded yet"; @@ -271,8 +270,8 @@ ResizeFSJob::exec() if ( !m.first || !m.second ) return Calamares::JobResult::error( tr( "Resize Failed" ), - !m_fsname.isEmpty() ? tr( "The filesystem %1 could not be found in this system, and cannot be resized." ).arg(m_fsname) - : tr( "The device %1 could not be found in this system, and cannot be resized." ).arg(m_devicename) ); + !m_fsname.isEmpty() ? tr( "The filesystem %1 could not be found in this system, and cannot be resized." ).arg( m_fsname ) + : tr( "The device %1 could not be found in this system, and cannot be resized." ).arg( m_devicename ) ); m.second->fileSystem().init(); // Initialize support for specific FS if ( !ResizeOperation::canGrow( m.second ) ) @@ -280,30 +279,30 @@ ResizeFSJob::exec() cDebug() << "canGrow() returned false."; return Calamares::JobResult::error( tr( "Resize Failed" ), - !m_fsname.isEmpty() ? tr( "The filesystem %1 cannot be resized." ).arg(m_fsname) - : tr( "The device %1 cannot be resized." ).arg(m_devicename) ); + !m_fsname.isEmpty() ? tr( "The filesystem %1 cannot be resized." ).arg( m_fsname ) + : tr( "The device %1 cannot be resized." ).arg( m_devicename ) ); } qint64 new_end = findGrownEnd( m ); cDebug() << "Resize from" - << m.second->firstSector() << '-' << m.second->lastSector() - << '(' << m.second->length() << ')' - << "to -" << new_end; + << m.second->firstSector() << '-' << m.second->lastSector() + << '(' << m.second->length() << ')' + << "to -" << new_end; if ( new_end < 0 ) return Calamares::JobResult::error( tr( "Resize Failed" ), - !m_fsname.isEmpty() ? tr( "The filesystem %1 cannot be resized." ).arg(m_fsname) - : tr( "The device %1 cannot be resized." ).arg(m_devicename) ); + !m_fsname.isEmpty() ? tr( "The filesystem %1 cannot be resized." ).arg( m_fsname ) + : tr( "The device %1 cannot be resized." ).arg( m_devicename ) ); if ( new_end == 0 ) { cWarning() << "Resize operation on" << m_fsname << m_devicename - << "skipped as not-useful."; + << "skipped as not-useful."; if ( m_required ) return Calamares::JobResult::error( tr( "Resize Failed" ), - !m_fsname.isEmpty() ? tr( "The filesystem %1 must be resized, but cannot." ).arg(m_fsname) - : tr( "The device %11 must be resized, but cannot" ).arg(m_fsname) ); + !m_fsname.isEmpty() ? tr( "The filesystem %1 must be resized, but cannot." ).arg( m_fsname ) + : tr( "The device %11 must be resized, but cannot" ).arg( m_fsname ) ); return Calamares::JobResult::ok(); } @@ -318,8 +317,8 @@ ResizeFSJob::exec() { cDebug() << "Resize failed." << op_report.output(); return Calamares::JobResult::error( - tr( "Resize Failed" ), - op_report.toText() ); + tr( "Resize Failed" ), + op_report.toText() ); } }