diff --git a/src/modules/partition/CMakeLists.txt b/src/modules/partition/CMakeLists.txt index 2dd0ef101..5f64b6530 100644 --- a/src/modules/partition/CMakeLists.txt +++ b/src/modules/partition/CMakeLists.txt @@ -30,6 +30,7 @@ calamares_add_plugin( partition SOURCES core/BootLoaderModel.cpp core/ColorUtils.cpp + core/DeviceList.cpp core/DeviceModel.cpp core/KPMHelpers.cpp core/PartitionActions.cpp diff --git a/src/modules/partition/core/DeviceList.cpp b/src/modules/partition/core/DeviceList.cpp new file mode 100644 index 000000000..307627002 --- /dev/null +++ b/src/modules/partition/core/DeviceList.cpp @@ -0,0 +1,133 @@ +/* === This file is part of Calamares - === + * + * Copyright 2015-2016, Teo Mrnjavac + * + * 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 . + */ + +#include "PartUtils.h" + +#include "PartitionCoreModule.h" + +#include "core/DeviceModel.h" +#include "core/KPMHelpers.h" +#include "core/PartitionIterator.h" + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +namespace PartUtils +{ + +/** + * Does the given @p device contain the root filesystem? This is true if + * the device contains a partition which is currently mounted at / . + */ +static bool +hasRootPartition( Device* device ) +{ + for ( auto it = PartitionIterator::begin( device ); it != PartitionIterator::end( device ); ++it ) + if ( ( *it )->mountPoint() == "/" ) + return true; + return false; +} + +static bool +isMounted( Device* device ) +{ + cDebug() << "Checking for mounted partitions in" << device->deviceNode(); + for ( auto it = PartitionIterator::begin( device ); it != PartitionIterator::end( device ); ++it ) + { + cDebug() << " .." << ( *it )->partitionPath() << "mount" << ( *it )->mountPoint(); + if ( ! ( *it )->mountPoint().isEmpty() ) + return true; + } + return false; +} + +static bool +isIso9660( const Device* device ) +{ + QString path = device->deviceNode(); + if ( path.isEmpty() ) + return false; + + QProcess blkid; + blkid.start( "blkid", { path } ); + blkid.waitForFinished(); + QString output = QString::fromLocal8Bit( blkid.readAllStandardOutput() ); + if ( output.contains( "iso9660" ) ) + return true; + + if ( device->partitionTable() && + !device->partitionTable()->children().isEmpty() ) + { + for ( const Partition* partition : device->partitionTable()->children() ) + { + path = partition->partitionPath(); + blkid.start( "blkid", { path } ); + blkid.waitForFinished(); + QString output = QString::fromLocal8Bit( blkid.readAllStandardOutput() ); + if ( output.contains( "iso9660" ) ) + return true; + } + } + return false; +} + + +QList< Device* > getDevices( bool writableOnly ) +{ + using DeviceList = QList< Device* >; + + CoreBackend* backend = CoreBackendManager::self()->backend(); + DeviceList devices = backend->scanDevices( true ); + + cDebug() << "Winnowing" << devices.count() << "devices."; + + // Remove the device which contains / from the list + for ( DeviceList::iterator it = devices.begin(); it != devices.end(); ) + if ( ! ( *it ) || + ( *it )->deviceNode().startsWith( "/dev/zram" ) + ) + { + cDebug() << " .. Winnowing" << ( ( *it ) ? ( *it )->deviceNode() : QString( "" ) ); + it = devices.erase( it ); + + } + else if ( writableOnly && ( + hasRootPartition( *it ) || + isIso9660( *it ) || + isMounted( *it ) ) + ) + { + cDebug() << " .. Winnowing" << ( ( *it ) ? ( *it )->deviceNode() : QString( "" ) ); + it = devices.erase( it ); + } + else + ++it; + + return devices; +} + +} // namespace PartUtils diff --git a/src/modules/partition/core/DeviceList.h b/src/modules/partition/core/DeviceList.h new file mode 100644 index 000000000..e13895b62 --- /dev/null +++ b/src/modules/partition/core/DeviceList.h @@ -0,0 +1,41 @@ +/* === This file is part of Calamares - === + * + * Copyright 2014-2017, Teo Mrnjavac + * Copyright 2017, Adriaan de Groot + * + * 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 . + */ + +#ifndef DEVICELIST_H +#define DEVICELIST_H + +#include +#include + +class Device; + +namespace PartUtils +{ + +/** + * @brief Gets a list of storage devices. + * @param writableOnly if set to true, only devices which can be overwritten + * safely are returned (e.g. RO-media are ignored, as are mounted partitions). + * @return a list of Devices meeting this criterium. + */ +QList< Device* > getDevices( bool writableOnly = false ); + +} + +#endif // DEVICELIST_H diff --git a/src/modules/partition/core/PartUtils.cpp b/src/modules/partition/core/PartUtils.cpp index dac76ca17..6aeffa7fb 100644 --- a/src/modules/partition/core/PartUtils.cpp +++ b/src/modules/partition/core/PartUtils.cpp @@ -328,96 +328,4 @@ runOsprober( PartitionCoreModule* core ) return osproberEntries; } - -/** - * Does the given @p device contain the root filesystem? This is true if - * the device contains a partition which is currently mounted at / . - */ -static bool -hasRootPartition( Device* device ) -{ - for ( auto it = PartitionIterator::begin( device ); it != PartitionIterator::end( device ); ++it ) - if ( ( *it )->mountPoint() == "/" ) - return true; - return false; -} - -static bool -isMounted( Device* device ) -{ - cDebug() << "Checking for mounted partitions in" << device->deviceNode(); - for ( auto it = PartitionIterator::begin( device ); it != PartitionIterator::end( device ); ++it ) - { - cDebug() << " .." << ( *it )->partitionPath() << "mount" << ( *it )->mountPoint(); - if ( ! ( *it )->mountPoint().isEmpty() ) - return true; - } - return false; -} - -static bool -isIso9660( const Device* device ) -{ - QString path = device->deviceNode(); - if ( path.isEmpty() ) - return false; - - QProcess blkid; - blkid.start( "blkid", { path } ); - blkid.waitForFinished(); - QString output = QString::fromLocal8Bit( blkid.readAllStandardOutput() ); - if ( output.contains( "iso9660" ) ) - return true; - - if ( device->partitionTable() && - !device->partitionTable()->children().isEmpty() ) - { - for ( const Partition* partition : device->partitionTable()->children() ) - { - path = partition->partitionPath(); - blkid.start( "blkid", { path } ); - blkid.waitForFinished(); - QString output = QString::fromLocal8Bit( blkid.readAllStandardOutput() ); - if ( output.contains( "iso9660" ) ) - return true; - } - } - return false; -} - - -QList< Device* > getDevices( bool writableOnly ) -{ - using DeviceList = QList< Device* >; - - CoreBackend* backend = CoreBackendManager::self()->backend(); - DeviceList devices = backend->scanDevices( true ); - - cDebug() << "Winnowing" << devices.count() << "devices."; - - // Remove the device which contains / from the list - for ( DeviceList::iterator it = devices.begin(); it != devices.end(); ) - if ( ! ( *it ) || - ( *it )->deviceNode().startsWith( "/dev/zram" ) - ) - { - cDebug() << " .. Winnowing" << ( ( *it ) ? ( *it )->deviceNode() : QString( "" ) ); - it = devices.erase( it ); - - } - else if ( writableOnly && ( - hasRootPartition( *it ) || - isIso9660( *it ) || - isMounted( *it ) ) - ) - { - cDebug() << " .. Winnowing" << ( ( *it ) ? ( *it )->deviceNode() : QString( "" ) ); - it = devices.erase( it ); - } - else - ++it; - - return devices; -} - } // nmamespace PartUtils diff --git a/src/modules/partition/core/PartUtils.h b/src/modules/partition/core/PartUtils.h index cd289e046..21d995965 100644 --- a/src/modules/partition/core/PartUtils.h +++ b/src/modules/partition/core/PartUtils.h @@ -23,7 +23,6 @@ #include -class Device; class PartitionCoreModule; class Partition; @@ -63,14 +62,6 @@ bool canBeResized( PartitionCoreModule* core, const QString& partitionPath ); */ OsproberEntryList runOsprober( PartitionCoreModule* core ); -/** - * @brief Gets a list of storage devices. - * @param writableOnly if set to true, only devices which can be overwritten - * safely are returned (e.g. RO-media are ignored, as are mounted partitions). - * @return a list of Devices meeting this criterium. - */ -QList< Device* > getDevices( bool writableOnly = false ); - } #endif // PARTUTILS_H diff --git a/src/modules/partition/core/PartitionCoreModule.cpp b/src/modules/partition/core/PartitionCoreModule.cpp index 752b7753c..e4d01fb8d 100644 --- a/src/modules/partition/core/PartitionCoreModule.cpp +++ b/src/modules/partition/core/PartitionCoreModule.cpp @@ -22,6 +22,7 @@ #include "core/BootLoaderModel.h" #include "core/ColorUtils.h" +#include "core/DeviceList.h" #include "core/DeviceModel.h" #include "core/PartitionInfo.h" #include "core/PartitionIterator.h"