2017-12-20 14:39:09 +01:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
2014-11-28 15:50:27 +01:00
|
|
|
*
|
2015-02-13 14:02:42 +01:00
|
|
|
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
|
2018-06-15 11:59:11 +02:00
|
|
|
* Copyright 2018, Adriaan de Groot <groot@kde.org>
|
2019-05-06 15:08:10 +02:00
|
|
|
* Copyright 2019, Kevin Kofler <kevin.kofler@chello.at>
|
2014-11-28 15:50:27 +01:00
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ClearMountsJob.h"
|
|
|
|
|
2015-07-02 13:49:21 +02:00
|
|
|
#include "core/PartitionInfo.h"
|
2019-06-13 12:27:39 +02:00
|
|
|
|
|
|
|
#include "partition/PartitionIterator.h"
|
2020-04-07 11:36:40 +02:00
|
|
|
#include "partition/Sync.h"
|
2015-07-02 13:49:21 +02:00
|
|
|
#include "utils/Logger.h"
|
|
|
|
|
|
|
|
// KPMcore
|
|
|
|
#include <kpmcore/core/device.h>
|
|
|
|
#include <kpmcore/core/partition.h>
|
|
|
|
#include <kpmcore/util/report.h>
|
2014-11-28 15:50:27 +01:00
|
|
|
|
2016-05-13 17:11:13 +02:00
|
|
|
#include <QDir>
|
2015-02-13 14:02:42 +01:00
|
|
|
#include <QProcess>
|
2014-11-28 15:50:27 +01:00
|
|
|
#include <QStringList>
|
|
|
|
|
2019-06-13 12:27:39 +02:00
|
|
|
using CalamaresUtils::Partition::PartitionIterator;
|
2014-11-28 15:50:27 +01:00
|
|
|
|
|
|
|
ClearMountsJob::ClearMountsJob( Device* device )
|
|
|
|
: Calamares::Job()
|
|
|
|
, m_device( device )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString
|
|
|
|
ClearMountsJob::prettyName() const
|
|
|
|
{
|
2020-04-07 11:36:40 +02:00
|
|
|
return tr( "Clear mounts for partitioning operations on %1" ).arg( m_device->deviceNode() );
|
2014-11-28 15:50:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-13 02:24:58 +02:00
|
|
|
QString
|
|
|
|
ClearMountsJob::prettyStatusMessage() const
|
|
|
|
{
|
2020-04-07 11:36:40 +02:00
|
|
|
return tr( "Clearing mounts for partitioning operations on %1." ).arg( m_device->deviceNode() );
|
2015-06-13 02:24:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-21 21:17:43 +02:00
|
|
|
QStringList
|
|
|
|
getPartitionsForDevice( const QString& deviceName )
|
2014-11-28 15:50:27 +01:00
|
|
|
{
|
2019-06-24 13:10:45 +02:00
|
|
|
QStringList partitions;
|
|
|
|
|
|
|
|
QFile dev_partitions( "/proc/partitions" );
|
|
|
|
if ( dev_partitions.open( QFile::ReadOnly ) )
|
|
|
|
{
|
|
|
|
cDebug() << "Reading from" << dev_partitions.fileName();
|
|
|
|
QTextStream in( &dev_partitions );
|
2020-04-07 11:36:40 +02:00
|
|
|
(void)in.readLine(); // That's the header line, skip it
|
2019-06-24 13:10:45 +02:00
|
|
|
while ( !in.atEnd() )
|
|
|
|
{
|
|
|
|
// The fourth column (index from 0, so index 3) is the name of the device;
|
|
|
|
// keep it if it is followed by something.
|
2020-06-23 11:13:55 +02:00
|
|
|
QStringList columns = in.readLine().split( ' ', SplitSkipEmptyParts );
|
2020-04-07 11:36:40 +02:00
|
|
|
if ( ( columns.count() >= 4 ) && ( columns[ 3 ].startsWith( deviceName ) )
|
|
|
|
&& ( columns[ 3 ] != deviceName ) )
|
2019-06-24 13:10:45 +02:00
|
|
|
{
|
2020-04-07 11:36:40 +02:00
|
|
|
partitions.append( columns[ 3 ] );
|
2019-06-24 13:10:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cDebug() << "Could not open" << dev_partitions.fileName();
|
|
|
|
}
|
|
|
|
|
|
|
|
return partitions;
|
2019-06-21 21:17:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Calamares::JobResult
|
|
|
|
ClearMountsJob::exec()
|
|
|
|
{
|
|
|
|
CalamaresUtils::Partition::Syncer s;
|
|
|
|
|
|
|
|
QString deviceName = m_device->deviceNode().split( '/' ).last();
|
|
|
|
|
|
|
|
QStringList goodNews;
|
|
|
|
QProcess process;
|
|
|
|
|
|
|
|
QStringList partitionsList = getPartitionsForDevice( deviceName );
|
|
|
|
|
2015-06-20 20:38:40 +02:00
|
|
|
// Build a list of partitions of type 82 (Linux swap / Solaris).
|
|
|
|
// We then need to clear them just in case they contain something resumable from a
|
|
|
|
// previous suspend-to-disk.
|
|
|
|
QStringList swapPartitions;
|
|
|
|
process.start( "sfdisk", { "-d", m_device->deviceNode() } );
|
|
|
|
process.waitForFinished();
|
|
|
|
// Sample output:
|
|
|
|
// % sudo sfdisk -d /dev/sda
|
|
|
|
// label: dos
|
|
|
|
// label-id: 0x000ced89
|
|
|
|
// device: /dev/sda
|
|
|
|
// unit: sectors
|
|
|
|
|
|
|
|
// /dev/sda1 : start= 63, size= 29329345, type=83, bootable
|
|
|
|
// /dev/sda2 : start= 29331456, size= 2125824, type=82
|
|
|
|
|
2020-04-07 11:36:40 +02:00
|
|
|
swapPartitions = QString::fromLocal8Bit( process.readAllStandardOutput() ).split( '\n' );
|
2015-06-20 20:38:40 +02:00
|
|
|
swapPartitions = swapPartitions.filter( "type=82" );
|
2020-04-07 11:36:40 +02:00
|
|
|
for ( QStringList::iterator it = swapPartitions.begin(); it != swapPartitions.end(); ++it )
|
2015-06-20 20:38:40 +02:00
|
|
|
{
|
2020-04-07 11:36:40 +02:00
|
|
|
*it = ( *it ).simplified().split( ' ' ).first();
|
2015-06-20 20:38:40 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 14:21:05 +02:00
|
|
|
const QStringList cryptoDevices = getCryptoDevices();
|
2020-04-07 11:36:40 +02:00
|
|
|
for ( const QString& mapperPath : cryptoDevices )
|
2016-05-13 17:11:13 +02:00
|
|
|
{
|
|
|
|
tryUmount( mapperPath );
|
|
|
|
QString news = tryCryptoClose( mapperPath );
|
|
|
|
if ( !news.isEmpty() )
|
2020-04-07 11:36:40 +02:00
|
|
|
{
|
2016-05-13 17:11:13 +02:00
|
|
|
goodNews.append( news );
|
2020-04-07 11:36:40 +02:00
|
|
|
}
|
2016-05-13 17:11:13 +02:00
|
|
|
}
|
|
|
|
|
2015-06-11 03:17:52 +02:00
|
|
|
// First we umount all LVM logical volumes we can find
|
|
|
|
process.start( "lvscan", { "-a" } );
|
|
|
|
process.waitForFinished();
|
2020-04-07 11:36:40 +02:00
|
|
|
if ( process.exitCode() == 0 ) //means LVM2 tools are installed
|
2015-06-11 03:17:52 +02:00
|
|
|
{
|
2016-09-01 14:21:05 +02:00
|
|
|
const QStringList lvscanLines = QString::fromLocal8Bit( process.readAllStandardOutput() ).split( '\n' );
|
|
|
|
for ( const QString& lvscanLine : lvscanLines )
|
2015-06-11 03:17:52 +02:00
|
|
|
{
|
2020-04-07 11:36:40 +02:00
|
|
|
QString lvPath = lvscanLine.simplified().split( ' ' ).value( 1 ); //second column
|
2015-06-11 03:17:52 +02:00
|
|
|
lvPath = lvPath.replace( '\'', "" );
|
|
|
|
|
|
|
|
QString news = tryUmount( lvPath );
|
|
|
|
if ( !news.isEmpty() )
|
2020-04-07 11:36:40 +02:00
|
|
|
{
|
2015-06-11 03:17:52 +02:00
|
|
|
goodNews.append( news );
|
2020-04-07 11:36:40 +02:00
|
|
|
}
|
2015-06-11 03:17:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2020-04-07 11:36:40 +02:00
|
|
|
{
|
2018-02-13 11:07:12 +01:00
|
|
|
cWarning() << "this system does not seem to have LVM2 tools.";
|
2020-04-07 11:36:40 +02:00
|
|
|
}
|
2015-06-11 03:17:52 +02:00
|
|
|
|
|
|
|
// Then we go looking for volume groups that use this device for physical volumes
|
|
|
|
process.start( "pvdisplay", { "-C", "--noheadings" } );
|
|
|
|
process.waitForFinished();
|
2020-04-07 11:36:40 +02:00
|
|
|
if ( process.exitCode() == 0 ) //means LVM2 tools are installed
|
2015-06-11 03:17:52 +02:00
|
|
|
{
|
|
|
|
QString pvdisplayOutput = process.readAllStandardOutput();
|
2020-04-07 11:36:40 +02:00
|
|
|
if ( !pvdisplayOutput.simplified().isEmpty() ) //means there is at least one LVM PV
|
2015-06-11 03:17:52 +02:00
|
|
|
{
|
|
|
|
QSet< QString > vgSet;
|
|
|
|
|
2016-09-01 14:21:05 +02:00
|
|
|
const QStringList pvdisplayLines = pvdisplayOutput.split( '\n' );
|
|
|
|
for ( const QString& pvdisplayLine : pvdisplayLines )
|
2015-06-11 03:17:52 +02:00
|
|
|
{
|
|
|
|
QString pvPath = pvdisplayLine.simplified().split( ' ' ).value( 0 );
|
|
|
|
QString vgName = pvdisplayLine.simplified().split( ' ' ).value( 1 );
|
|
|
|
if ( !pvPath.contains( deviceName ) )
|
2020-04-07 11:36:40 +02:00
|
|
|
{
|
2015-06-11 03:17:52 +02:00
|
|
|
continue;
|
2020-04-07 11:36:40 +02:00
|
|
|
}
|
2015-06-11 03:17:52 +02:00
|
|
|
|
|
|
|
vgSet.insert( vgName );
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( const QString& vgName, vgSet )
|
|
|
|
{
|
|
|
|
process.start( "vgchange", { "-an", vgName } );
|
|
|
|
process.waitForFinished();
|
|
|
|
if ( process.exitCode() == 0 )
|
2020-04-07 11:36:40 +02:00
|
|
|
{
|
2015-06-11 03:17:52 +02:00
|
|
|
goodNews.append( QString( "Successfully disabled volume group %1." ).arg( vgName ) );
|
2020-04-07 11:36:40 +02:00
|
|
|
}
|
2015-06-11 03:17:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2020-04-07 11:36:40 +02:00
|
|
|
{
|
2018-02-13 11:07:12 +01:00
|
|
|
cWarning() << "this system does not seem to have LVM2 tools.";
|
2020-04-07 11:36:40 +02:00
|
|
|
}
|
2015-06-11 03:17:52 +02:00
|
|
|
|
2016-09-01 14:21:05 +02:00
|
|
|
const QStringList cryptoDevices2 = getCryptoDevices();
|
2020-04-07 11:36:40 +02:00
|
|
|
for ( const QString& mapperPath : cryptoDevices2 )
|
2016-05-13 17:11:13 +02:00
|
|
|
{
|
|
|
|
tryUmount( mapperPath );
|
|
|
|
QString news = tryCryptoClose( mapperPath );
|
|
|
|
if ( !news.isEmpty() )
|
2020-04-07 11:36:40 +02:00
|
|
|
{
|
2016-05-13 17:11:13 +02:00
|
|
|
goodNews.append( news );
|
2020-04-07 11:36:40 +02:00
|
|
|
}
|
2016-05-13 17:11:13 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 11:36:40 +02:00
|
|
|
for ( const QString& p : partitionsList )
|
2014-11-28 15:50:27 +01:00
|
|
|
{
|
2015-02-13 14:02:42 +01:00
|
|
|
QString partPath = QString( "/dev/%1" ).arg( p );
|
|
|
|
|
2015-06-11 03:17:52 +02:00
|
|
|
QString news = tryUmount( partPath );
|
|
|
|
if ( !news.isEmpty() )
|
2020-04-07 11:36:40 +02:00
|
|
|
{
|
2015-06-11 03:17:52 +02:00
|
|
|
goodNews.append( news );
|
2020-04-07 11:36:40 +02:00
|
|
|
}
|
2014-11-28 15:50:27 +01:00
|
|
|
}
|
2015-02-13 14:02:42 +01:00
|
|
|
|
2015-06-20 20:38:40 +02:00
|
|
|
foreach ( QString p, swapPartitions )
|
|
|
|
{
|
|
|
|
QString news = tryClearSwap( p );
|
|
|
|
if ( !news.isEmpty() )
|
2020-04-07 11:36:40 +02:00
|
|
|
{
|
2015-06-20 20:38:40 +02:00
|
|
|
goodNews.append( news );
|
2020-04-07 11:36:40 +02:00
|
|
|
}
|
2015-06-20 20:38:40 +02:00
|
|
|
}
|
|
|
|
|
2014-11-28 15:50:27 +01:00
|
|
|
Calamares::JobResult ok = Calamares::JobResult::ok();
|
2020-04-07 11:36:40 +02:00
|
|
|
ok.setMessage( tr( "Cleared all mounts for %1" ).arg( m_device->deviceNode() ) );
|
2014-11-28 15:50:27 +01:00
|
|
|
ok.setDetails( goodNews.join( "\n" ) );
|
2015-02-13 11:56:10 +01:00
|
|
|
|
2015-02-19 20:24:14 +01:00
|
|
|
cDebug() << "ClearMountsJob finished. Here's what was done:\n" << goodNews.join( "\n" );
|
|
|
|
|
2014-11-28 15:50:27 +01:00
|
|
|
return ok;
|
|
|
|
}
|
2015-06-11 03:17:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
QString
|
|
|
|
ClearMountsJob::tryUmount( const QString& partPath )
|
|
|
|
{
|
|
|
|
QProcess process;
|
|
|
|
process.start( "umount", { partPath } );
|
|
|
|
process.waitForFinished();
|
|
|
|
if ( process.exitCode() == 0 )
|
2020-04-07 11:36:40 +02:00
|
|
|
{
|
2015-06-11 03:17:52 +02:00
|
|
|
return QString( "Successfully unmounted %1." ).arg( partPath );
|
2020-04-07 11:36:40 +02:00
|
|
|
}
|
2015-06-11 03:17:52 +02:00
|
|
|
|
|
|
|
process.start( "swapoff", { partPath } );
|
|
|
|
process.waitForFinished();
|
|
|
|
if ( process.exitCode() == 0 )
|
2020-04-07 11:36:40 +02:00
|
|
|
{
|
2015-06-20 20:38:40 +02:00
|
|
|
return QString( "Successfully disabled swap %1." ).arg( partPath );
|
2020-04-07 11:36:40 +02:00
|
|
|
}
|
2015-06-20 20:38:40 +02:00
|
|
|
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString
|
|
|
|
ClearMountsJob::tryClearSwap( const QString& partPath )
|
|
|
|
{
|
|
|
|
QProcess process;
|
2015-09-17 15:14:18 +02:00
|
|
|
process.start( "blkid", { "-s", "UUID", "-o", "value", partPath } );
|
2015-06-20 20:38:40 +02:00
|
|
|
process.waitForFinished();
|
2015-09-17 15:14:18 +02:00
|
|
|
QString swapPartUuid = QString::fromLocal8Bit( process.readAllStandardOutput() ).simplified();
|
2020-04-07 11:36:40 +02:00
|
|
|
if ( process.exitCode() != 0 || swapPartUuid.isEmpty() )
|
|
|
|
{
|
2015-09-17 15:14:18 +02:00
|
|
|
return QString();
|
2020-04-07 11:36:40 +02:00
|
|
|
}
|
2015-06-11 03:17:52 +02:00
|
|
|
|
2015-09-17 15:14:18 +02:00
|
|
|
process.start( "mkswap", { "-U", swapPartUuid, partPath } );
|
|
|
|
process.waitForFinished();
|
|
|
|
if ( process.exitCode() != 0 )
|
2020-04-07 11:36:40 +02:00
|
|
|
{
|
2015-09-17 15:14:18 +02:00
|
|
|
return QString();
|
2020-04-07 11:36:40 +02:00
|
|
|
}
|
2015-09-17 15:14:18 +02:00
|
|
|
|
|
|
|
return QString( "Successfully cleared swap %1." ).arg( partPath );
|
2015-06-11 03:17:52 +02:00
|
|
|
}
|
2016-05-13 17:11:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
QString
|
|
|
|
ClearMountsJob::tryCryptoClose( const QString& mapperPath )
|
|
|
|
{
|
|
|
|
QProcess process;
|
|
|
|
process.start( "cryptsetup", { "close", mapperPath } );
|
|
|
|
process.waitForFinished();
|
|
|
|
if ( process.exitCode() == 0 )
|
2020-04-07 11:36:40 +02:00
|
|
|
{
|
2016-05-13 17:11:13 +02:00
|
|
|
return QString( "Successfully closed mapper device %1." ).arg( mapperPath );
|
2020-04-07 11:36:40 +02:00
|
|
|
}
|
2016-05-13 17:11:13 +02:00
|
|
|
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QStringList
|
2016-09-01 14:21:05 +02:00
|
|
|
ClearMountsJob::getCryptoDevices() const
|
2016-05-13 17:11:13 +02:00
|
|
|
{
|
|
|
|
QDir mapperDir( "/dev/mapper" );
|
2016-09-01 14:21:05 +02:00
|
|
|
const QFileInfoList fiList = mapperDir.entryInfoList( QDir::Files );
|
2016-05-13 17:11:13 +02:00
|
|
|
QStringList list;
|
|
|
|
QProcess process;
|
2020-04-07 11:36:40 +02:00
|
|
|
for ( const QFileInfo& fi : fiList )
|
2016-05-13 17:11:13 +02:00
|
|
|
{
|
2019-05-06 15:08:10 +02:00
|
|
|
QString baseName = fi.baseName();
|
|
|
|
// Fedora live images use /dev/mapper/live-* internally. We must not
|
|
|
|
// unmount those devices, because they are used by the live image and
|
|
|
|
// because we need /dev/mapper/live-base in the unpackfs module.
|
|
|
|
if ( baseName == "control" || baseName.startsWith( "live-" ) )
|
2020-04-07 11:36:40 +02:00
|
|
|
{
|
2016-05-13 17:11:13 +02:00
|
|
|
continue;
|
2020-04-07 11:36:40 +02:00
|
|
|
}
|
2016-05-13 17:11:13 +02:00
|
|
|
list.append( fi.absoluteFilePath() );
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|