From b557961a3276b3b232e981f7254a32897e19077d Mon Sep 17 00:00:00 2001 From: Teo Mrnjavac Date: Fri, 27 Feb 2015 19:07:17 +0100 Subject: [PATCH 1/4] New ClearTempMountsJob, to be executed before partitioning jobs. --- src/modules/partition/CMakeLists.txt | 1 + .../partition/core/PartitionCoreModule.cpp | 5 +- .../partition/jobs/ClearTempMountsJob.cpp | 84 +++++++++++++++++++ .../partition/jobs/ClearTempMountsJob.h | 39 +++++++++ 4 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 src/modules/partition/jobs/ClearTempMountsJob.cpp create mode 100644 src/modules/partition/jobs/ClearTempMountsJob.h diff --git a/src/modules/partition/CMakeLists.txt b/src/modules/partition/CMakeLists.txt index 4aecc045d..6113ab685 100644 --- a/src/modules/partition/CMakeLists.txt +++ b/src/modules/partition/CMakeLists.txt @@ -44,6 +44,7 @@ calamares_add_plugin( partition gui/ReplacePage.cpp jobs/CheckFileSystemJob.cpp jobs/ClearMountsJob.cpp + jobs/ClearTempMountsJob.cpp jobs/CreatePartitionJob.cpp jobs/CreatePartitionTableJob.cpp jobs/DeletePartitionJob.cpp diff --git a/src/modules/partition/core/PartitionCoreModule.cpp b/src/modules/partition/core/PartitionCoreModule.cpp index 524018687..9b7e21b00 100644 --- a/src/modules/partition/core/PartitionCoreModule.cpp +++ b/src/modules/partition/core/PartitionCoreModule.cpp @@ -1,7 +1,7 @@ /* === This file is part of Calamares - === * * Copyright 2014, Aurélien Gâteau - * Copyright 2014, Teo Mrnjavac + * Copyright 2014-2015, 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 @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -281,6 +282,8 @@ PartitionCoreModule::jobs() const QList< Calamares::job_ptr > lst; QList< Device* > devices; + lst << Calamares::job_ptr( new ClearTempMountsJob() ); + for ( auto info : m_deviceInfos ) { if ( info->isDirty() ) diff --git a/src/modules/partition/jobs/ClearTempMountsJob.cpp b/src/modules/partition/jobs/ClearTempMountsJob.cpp new file mode 100644 index 000000000..2fef32d16 --- /dev/null +++ b/src/modules/partition/jobs/ClearTempMountsJob.cpp @@ -0,0 +1,84 @@ +/* === This file is part of Calamares - === + * + * Copyright 2014-2015, 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 "ClearTempMountsJob.h" + +#include +#include + +#include +#include +#include + + +ClearTempMountsJob::ClearTempMountsJob() + : Calamares::Job() +{ +} + + +QString +ClearTempMountsJob::prettyName() const +{ + return tr( "Clear all temporary mounts." ); +} + + +Calamares::JobResult +ClearTempMountsJob::exec() +{ + // Fetch a list of current mounts to Calamares temporary directories. + QMap< QString, QString > lst; + QFile mtab( "/etc/mtab" ); + if ( !mtab.open( QFile::ReadOnly | QFile::Text ) ) + return Calamares::JobResult::error( tr( "Cannot get list of temporary mounts." ) ); + + while ( !mtab.atEnd() ) + { + QStringList line = QString::fromLocal8Bit( mtab.readLine() ) + .split( ' ', QString::SkipEmptyParts ); + QString device = line.at( 0 ); + QString mountPoint = line.at( 1 ); + if ( mountPoint.startsWith( "/tmp/calamares-" ) ) + lst.insert( device, mountPoint ); + } + + QStringList keys = lst.keys(); + keys.sort(); + + QStringList goodNews; + QProcess process; + + for ( int i = keys.length() - 1; i >= 0; --i ) + { + QString partPath = lst.value( keys[ i ] ); + process.start( "umount", { "-lv", partPath } ); + process.waitForFinished(); + if ( process.exitCode() == 0 ) + goodNews.append( QString( "Successfully unmounted %1." ).arg( partPath ) ); + + } + + Calamares::JobResult ok = Calamares::JobResult::ok(); + ok.setMessage( tr( "Cleared all temporary mounts." ) ); + ok.setDetails( goodNews.join( "\n" ) ); + + cDebug() << "ClearTempMountsJob finished. Here's what was done:\n" << goodNews.join( "\n" ); + + return ok; +} diff --git a/src/modules/partition/jobs/ClearTempMountsJob.h b/src/modules/partition/jobs/ClearTempMountsJob.h new file mode 100644 index 000000000..f83145c43 --- /dev/null +++ b/src/modules/partition/jobs/ClearTempMountsJob.h @@ -0,0 +1,39 @@ +/* === This file is part of Calamares - === + * + * Copyright 2014-2015, 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 . + */ + +#ifndef CLEARTEMPMOUNTSJOB_H +#define CLEARTEMPMOUNTSJOB_H + +#include + +class Device; + +/** + * This job tries to free all temporary mounts used by Calamares, so partitioning + * operations can proceed. + */ +class ClearTempMountsJob : public Calamares::Job +{ + Q_OBJECT +public: + explicit ClearTempMountsJob(); + QString prettyName() const override; + Calamares::JobResult exec() override; +}; + +#endif // CLEARTEMPMOUNTSJOB_H From 20a8b222c60375100444b4da51f025a21149d221 Mon Sep 17 00:00:00 2001 From: Teo Mrnjavac Date: Fri, 27 Feb 2015 20:31:17 +0100 Subject: [PATCH 2/4] Verbose debug output. --- src/modules/partition/jobs/ClearTempMountsJob.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/modules/partition/jobs/ClearTempMountsJob.cpp b/src/modules/partition/jobs/ClearTempMountsJob.cpp index 2fef32d16..0000f7720 100644 --- a/src/modules/partition/jobs/ClearTempMountsJob.cpp +++ b/src/modules/partition/jobs/ClearTempMountsJob.cpp @@ -48,30 +48,37 @@ ClearTempMountsJob::exec() if ( !mtab.open( QFile::ReadOnly | QFile::Text ) ) return Calamares::JobResult::error( tr( "Cannot get list of temporary mounts." ) ); + cDebug() << "Opened mtab. Lines:"; while ( !mtab.atEnd() ) { QStringList line = QString::fromLocal8Bit( mtab.readLine() ) .split( ' ', QString::SkipEmptyParts ); + cDebug() << line.join( ' ' ); QString device = line.at( 0 ); QString mountPoint = line.at( 1 ); if ( mountPoint.startsWith( "/tmp/calamares-" ) ) + { + cDebug() << "INSERTING pair (device, mountPoint)" << device << mountPoint; lst.insert( device, mountPoint ); + } } QStringList keys = lst.keys(); keys.sort(); + cDebug() << "Sorted keys:\n" << keys; + QStringList goodNews; QProcess process; for ( int i = keys.length() - 1; i >= 0; --i ) { QString partPath = lst.value( keys[ i ] ); + cDebug() << "Will try to umount path" << partPath; process.start( "umount", { "-lv", partPath } ); process.waitForFinished(); if ( process.exitCode() == 0 ) goodNews.append( QString( "Successfully unmounted %1." ).arg( partPath ) ); - } Calamares::JobResult ok = Calamares::JobResult::ok(); From 642b4be1f2f87e76c48b9c1da9db0cc99cf38abb Mon Sep 17 00:00:00 2001 From: Teo Mrnjavac Date: Fri, 27 Feb 2015 20:43:21 +0100 Subject: [PATCH 3/4] Keys are not necessarily unique, so use a QList< QPair > instead. --- .../partition/jobs/ClearTempMountsJob.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/modules/partition/jobs/ClearTempMountsJob.cpp b/src/modules/partition/jobs/ClearTempMountsJob.cpp index 0000f7720..23d484f0c 100644 --- a/src/modules/partition/jobs/ClearTempMountsJob.cpp +++ b/src/modules/partition/jobs/ClearTempMountsJob.cpp @@ -43,7 +43,7 @@ Calamares::JobResult ClearTempMountsJob::exec() { // Fetch a list of current mounts to Calamares temporary directories. - QMap< QString, QString > lst; + QList< QPair < QString, QString > > lst; QFile mtab( "/etc/mtab" ); if ( !mtab.open( QFile::ReadOnly | QFile::Text ) ) return Calamares::JobResult::error( tr( "Cannot get list of temporary mounts." ) ); @@ -59,21 +59,22 @@ ClearTempMountsJob::exec() if ( mountPoint.startsWith( "/tmp/calamares-" ) ) { cDebug() << "INSERTING pair (device, mountPoint)" << device << mountPoint; - lst.insert( device, mountPoint ); + lst.append( qMakePair( device, mountPoint ) ); } } - QStringList keys = lst.keys(); - keys.sort(); - - cDebug() << "Sorted keys:\n" << keys; + qSort( lst.begin(), lst.end(), []( const QPair< QString, QString >& a, + const QPair< QString, QString >& b ) -> bool + { + return a.first > b.first; + } ); QStringList goodNews; QProcess process; - for ( int i = keys.length() - 1; i >= 0; --i ) + foreach ( auto line, lst ) { - QString partPath = lst.value( keys[ i ] ); + QString partPath = line.second; cDebug() << "Will try to umount path" << partPath; process.start( "umount", { "-lv", partPath } ); process.waitForFinished(); From fb3072bbbb64e51bebdecbd6ab44fe0b1694ba9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ramon=20Buld=C3=B3?= Date: Sat, 28 Feb 2015 16:44:09 +0100 Subject: [PATCH 4/4] /etc/mtab reports a file size of 0, so using .atEnd() may not work. Read the file until it doesn't return more data. Use QTextStream because it takes care of the conversion between 8-bit and 16-bit. http://doc.qt.io/qt-5/qfile.html#using-streams-to-read-files --- src/modules/partition/jobs/ClearTempMountsJob.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/modules/partition/jobs/ClearTempMountsJob.cpp b/src/modules/partition/jobs/ClearTempMountsJob.cpp index 23d484f0c..043dddb84 100644 --- a/src/modules/partition/jobs/ClearTempMountsJob.cpp +++ b/src/modules/partition/jobs/ClearTempMountsJob.cpp @@ -49,10 +49,11 @@ ClearTempMountsJob::exec() return Calamares::JobResult::error( tr( "Cannot get list of temporary mounts." ) ); cDebug() << "Opened mtab. Lines:"; - while ( !mtab.atEnd() ) + QTextStream in(&mtab); + QString lineIn = in.readLine(); + while ( !lineIn.isNull() ) { - QStringList line = QString::fromLocal8Bit( mtab.readLine() ) - .split( ' ', QString::SkipEmptyParts ); + QStringList line = lineIn.split( ' ', QString::SkipEmptyParts ); cDebug() << line.join( ' ' ); QString device = line.at( 0 ); QString mountPoint = line.at( 1 ); @@ -61,6 +62,7 @@ ClearTempMountsJob::exec() cDebug() << "INSERTING pair (device, mountPoint)" << device << mountPoint; lst.append( qMakePair( device, mountPoint ) ); } + lineIn = in.readLine(); } qSort( lst.begin(), lst.end(), []( const QPair< QString, QString >& a,