Add global checks for partition layout

This commit adds several checks while reading the configuration of the
`partition` module, in case the partition layout configuration is
misformed. If an error is encountered, an message is printed to the
console and the module reverts to the default partition layout.

Checks are also added when implementing the partition layout, in case a
problem occurs that couldn't be anticipated (for example, when a
partition size is in %, checking its absolute value require knowing the
total device size, which is not the case when the configuration is
being read).

Signed-off-by: Arnaud Ferraris <arnaud.ferraris@collabora.com>
This commit is contained in:
Arnaud Ferraris 2019-04-17 19:16:48 +02:00
parent d32733bf59
commit 123222c0a8
3 changed files with 111 additions and 24 deletions

View file

@ -21,6 +21,8 @@
#include "GlobalStorage.h"
#include "JobQueue.h"
#include "utils/Logger.h"
#include "core/PartitionLayout.h"
#include "core/KPMHelpers.h"
@ -69,37 +71,67 @@ PartitionLayout::~PartitionLayout()
{
}
void
bool
PartitionLayout::addEntry( PartitionLayout::PartitionEntry entry )
{
if ( !entry.isValid() )
{
cError() << "Partition size is invalid or has min size > max size";
return false;
}
m_partLayout.append( entry );
return true;
}
PartitionLayout::PartitionEntry::PartitionEntry( const QString& size, const QString& min, const QString& max )
{
partSize = PartUtils::PartSize( size );
if ( !min.isEmpty() )
partMinSize = PartUtils::PartSize( min );
if ( !max.isEmpty() )
partMaxSize = PartUtils::PartSize( max );
partMinSize = PartUtils::PartSize( min );
partMaxSize = PartUtils::PartSize( max );
}
void
bool
PartitionLayout::addEntry( const QString& mountPoint, const QString& size, const QString& min, const QString& max )
{
PartitionLayout::PartitionEntry entry( size, min, max );
if ( !entry.isValid() )
{
cError() << "Partition size" << size << "is invalid or" << min << ">" << max;
return false;
}
if ( mountPoint.isEmpty() || !mountPoint.startsWith( QString( "/" ) ) )
{
cError() << "Partition mount point" << mountPoint << "is invalid";
return false;
}
entry.partMountPoint = mountPoint;
entry.partFileSystem = m_defaultFsType;
m_partLayout.append( entry );
return true;
}
void
bool
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, max );
if ( !entry.isValid() )
{
cError() << "Partition size" << size << "is invalid or" << min << ">" << max;
return false;
}
if ( mountPoint.isEmpty() || !mountPoint.startsWith( QString( "/" ) ) )
{
cError() << "Partition mount point" << mountPoint << "is invalid";
return false;
}
entry.partLabel = label;
entry.partMountPoint = mountPoint;
PartUtils::findFS( fs, &entry.partFileSystem );
@ -107,6 +139,8 @@ PartitionLayout::addEntry( const QString& label, const QString& mountPoint, cons
entry.partFileSystem = m_defaultFsType;
m_partLayout.append( entry );
return true;
}
QList< Partition* >
@ -128,9 +162,36 @@ PartitionLayout::execute( Device *dev, qint64 firstSector,
Partition *currentPartition = nullptr;
// Calculate partition size
size = part.partSize.toSectors( totalSize, dev->logicalSize() );
minSize = part.partMinSize.toSectors( totalSize, dev->logicalSize() );
maxSize = part.partMaxSize.toSectors( totalSize, dev->logicalSize() );
if ( part.partSize.isValid() )
{
size = part.partSize.toSectors( totalSize, dev->logicalSize() );
}
else
{
cWarning() << "Partition" << part.partMountPoint << "size ("
<< size << "sectors) is invalid, skipping...";
continue;
}
if ( part.partMinSize.isValid() )
minSize = part.partMinSize.toSectors( totalSize, dev->logicalSize() );
else
minSize = 0;
if ( part.partMaxSize.isValid() )
maxSize = part.partMaxSize.toSectors( totalSize, dev->logicalSize() );
else
maxSize = availableSize;
// Make sure we never go under minSize once converted to sectors
if ( maxSize < minSize )
{
cWarning() << "Partition" << part.partMountPoint << "max size (" << maxSize
<< "sectors) is < min size (" << minSize << "sectors), using min size";
maxSize = minSize;
}
// Adjust partition size based on user-defined boundaries and available space
if ( size < minSize )
size = minSize;
if ( size > maxSize )