2014-08-05 14:57:00 +02:00
|
|
|
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
|
|
|
*
|
|
|
|
* Copyright 2014, Aurélien Gâteau <agateau@kde.org>
|
2015-04-09 15:16:09 +02:00
|
|
|
* Copyright 2015, Teo Mrnjavac <teo@kde.org>
|
2014-08-05 14:57:00 +02: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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// This class is heavily based on the ResizeOperation class from KDE Partition
|
|
|
|
// Manager. Original copyright follow:
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2008,2012 by Volker Lanz <vl@fidra.de> *
|
|
|
|
* *
|
|
|
|
* This program 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 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program 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 this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
|
|
|
|
***************************************************************************/
|
|
|
|
|
2015-07-02 13:49:21 +02:00
|
|
|
#include "jobs/ResizePartitionJob.h"
|
2014-08-05 14:57:00 +02:00
|
|
|
|
2015-07-02 13:49:21 +02:00
|
|
|
#include "jobs/CheckFileSystemJob.h"
|
|
|
|
#include "jobs/MoveFileSystemJob.h"
|
|
|
|
#include "utils/Logger.h"
|
2014-08-05 14:57:00 +02:00
|
|
|
|
2015-07-02 13:49:21 +02:00
|
|
|
// KPMcore
|
|
|
|
#include <kpmcore/backend/corebackend.h>
|
|
|
|
#include <kpmcore/backend/corebackendmanager.h>
|
|
|
|
#include <kpmcore/backend/corebackenddevice.h>
|
|
|
|
#include <kpmcore/backend/corebackendpartition.h>
|
|
|
|
#include <kpmcore/backend/corebackendpartitiontable.h>
|
|
|
|
#include <kpmcore/core/device.h>
|
|
|
|
#include <kpmcore/core/partition.h>
|
|
|
|
#include <kpmcore/util/report.h>
|
2014-08-05 14:57:00 +02:00
|
|
|
|
|
|
|
// Qt
|
|
|
|
#include <QScopedPointer>
|
|
|
|
|
|
|
|
//- ResizeFileSystemJob --------------------------------------------------------
|
|
|
|
class ResizeFileSystemJob : public Calamares::Job
|
|
|
|
{
|
2014-10-29 13:06:06 +01:00
|
|
|
Q_OBJECT
|
2014-08-05 14:57:00 +02:00
|
|
|
public:
|
2014-08-08 11:43:22 +02:00
|
|
|
ResizeFileSystemJob( Device* device, CoreBackendPartitionTable* backendPartitionTable, Partition* partition, qint64 length )
|
|
|
|
: m_device( device )
|
|
|
|
, m_backendPartitionTable( backendPartitionTable )
|
|
|
|
, m_partition( partition )
|
2014-08-05 16:11:34 +02:00
|
|
|
, m_length( length )
|
2014-08-05 14:57:00 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
QString prettyName() const override
|
|
|
|
{
|
2014-08-08 11:43:22 +02:00
|
|
|
QString path = m_partition->partitionPath();
|
2014-08-06 11:48:03 +02:00
|
|
|
return tr( "Resize file system on partition %1." ).arg( path );
|
2014-08-05 14:57:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Calamares::JobResult exec() override
|
|
|
|
{
|
2014-08-05 16:11:34 +02:00
|
|
|
Report report( nullptr );
|
2014-08-08 11:43:22 +02:00
|
|
|
FileSystem& fs = m_partition->fileSystem();
|
2014-08-05 16:11:34 +02:00
|
|
|
FileSystem::CommandSupportType support = m_length < fs.length() ? fs.supportShrink() : fs.supportGrow();
|
|
|
|
|
2014-08-05 17:53:10 +02:00
|
|
|
switch ( support )
|
2014-08-05 16:11:34 +02:00
|
|
|
{
|
|
|
|
case FileSystem::cmdSupportBackend:
|
|
|
|
if ( !backendResize( &report ) )
|
|
|
|
return Calamares::JobResult::error(
|
2014-08-05 17:53:10 +02:00
|
|
|
QString(),
|
|
|
|
tr( "Parted failed to resize filesystem." ) + '\n' + report.toText()
|
|
|
|
);
|
2014-08-05 16:11:34 +02:00
|
|
|
break;
|
|
|
|
case FileSystem::cmdSupportFileSystem:
|
|
|
|
{
|
2014-08-08 11:43:22 +02:00
|
|
|
qint64 byteLength = m_device->logicalSectorSize() * m_length;
|
|
|
|
bool ok = fs.resize( report, m_partition->partitionPath(), byteLength );
|
2014-08-05 16:11:34 +02:00
|
|
|
if ( !ok )
|
|
|
|
return Calamares::JobResult::error(
|
2014-08-05 17:53:10 +02:00
|
|
|
QString(),
|
|
|
|
tr( "Failed to resize filesystem." ) + '\n' + report.toText()
|
|
|
|
);
|
2014-08-05 16:11:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.setLastSector( fs.firstSector() + m_length - 1 );
|
2014-08-05 14:57:00 +02:00
|
|
|
return Calamares::JobResult::ok();
|
|
|
|
}
|
2014-08-05 16:11:34 +02:00
|
|
|
|
|
|
|
private:
|
2014-08-08 11:43:22 +02:00
|
|
|
Device* m_device;
|
|
|
|
CoreBackendPartitionTable* m_backendPartitionTable;
|
|
|
|
Partition* m_partition;
|
2014-08-05 16:11:34 +02:00
|
|
|
qint64 m_length;
|
|
|
|
|
|
|
|
bool backendResize( Report* report )
|
|
|
|
{
|
2014-08-08 11:43:22 +02:00
|
|
|
bool ok = m_backendPartitionTable->resizeFileSystem( *report, *m_partition, m_length );
|
2014-08-05 16:11:34 +02:00
|
|
|
if ( !ok )
|
|
|
|
return false;
|
2014-08-08 11:43:22 +02:00
|
|
|
m_backendPartitionTable->commit();
|
2014-08-05 16:11:34 +02:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-05 14:57:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
//- SetPartGeometryJob ---------------------------------------------------------
|
|
|
|
class SetPartGeometryJob : public Calamares::Job
|
|
|
|
{
|
2014-10-29 13:06:06 +01:00
|
|
|
Q_OBJECT
|
2014-08-05 14:57:00 +02:00
|
|
|
public:
|
2014-08-08 11:43:22 +02:00
|
|
|
SetPartGeometryJob( CoreBackendPartitionTable* backendPartitionTable, Partition* partition, qint64 firstSector, qint64 length )
|
|
|
|
: m_backendPartitionTable( backendPartitionTable )
|
|
|
|
, m_partition( partition )
|
2014-08-05 16:11:34 +02:00
|
|
|
, m_firstSector( firstSector )
|
|
|
|
, m_length( length )
|
2014-08-05 14:57:00 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
QString prettyName() const override
|
|
|
|
{
|
2014-08-08 11:43:22 +02:00
|
|
|
QString path = m_partition->partitionPath();
|
2014-08-06 11:48:03 +02:00
|
|
|
return tr( "Update geometry of partition %1." ).arg( path );
|
2014-08-05 14:57:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Calamares::JobResult exec() override
|
|
|
|
{
|
2014-08-05 16:11:34 +02:00
|
|
|
Report report( nullptr );
|
|
|
|
qint64 lastSector = m_firstSector + m_length - 1;
|
2014-08-08 11:43:22 +02:00
|
|
|
bool ok = m_backendPartitionTable->updateGeometry( report, *m_partition, m_firstSector, lastSector );
|
2014-08-05 16:11:34 +02:00
|
|
|
if ( !ok )
|
|
|
|
{
|
|
|
|
return Calamares::JobResult::error(
|
2014-08-05 17:53:10 +02:00
|
|
|
QString(),
|
|
|
|
tr( "Failed to change the geometry of the partition." ) + '\n' + report.toText() );
|
2014-08-05 16:11:34 +02:00
|
|
|
}
|
2014-08-08 11:43:22 +02:00
|
|
|
m_partition->setFirstSector( m_firstSector );
|
|
|
|
m_partition->setLastSector( lastSector );
|
|
|
|
m_backendPartitionTable->commit();
|
2014-08-05 14:57:00 +02:00
|
|
|
return Calamares::JobResult::ok();
|
|
|
|
}
|
2014-08-05 16:11:34 +02:00
|
|
|
|
|
|
|
private:
|
2014-08-08 11:43:22 +02:00
|
|
|
CoreBackendPartitionTable* m_backendPartitionTable;
|
|
|
|
Partition* m_partition;
|
2014-08-05 16:11:34 +02:00
|
|
|
qint64 m_firstSector;
|
|
|
|
qint64 m_length;
|
2014-08-05 14:57:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
//- ResizePartitionJob ---------------------------------------------------------
|
|
|
|
ResizePartitionJob::ResizePartitionJob( Device* device, Partition* partition, qint64 firstSector, qint64 lastSector )
|
|
|
|
: PartitionJob( partition )
|
|
|
|
, m_device( device )
|
2014-08-05 16:11:34 +02:00
|
|
|
, m_oldFirstSector( partition->firstSector() ) // Keep a copy of old sectors because they will be overwritten in updatePreview()
|
|
|
|
, m_oldLastSector( partition->lastSector() )
|
2014-08-05 14:57:00 +02:00
|
|
|
, m_newFirstSector( firstSector )
|
|
|
|
, m_newLastSector( lastSector )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
ResizePartitionJob::prettyName() const
|
|
|
|
{
|
2014-08-06 11:48:03 +02:00
|
|
|
// FIXME: Copy PM ResizeOperation code which generates a description of the
|
|
|
|
// operation
|
|
|
|
return tr( "Resize partition %1." ).arg( partition()->partitionPath() );
|
2014-08-05 14:57:00 +02:00
|
|
|
}
|
|
|
|
|
2015-04-09 15:16:09 +02:00
|
|
|
|
|
|
|
QString
|
|
|
|
ResizePartitionJob::prettyDescription() const
|
|
|
|
{
|
2015-04-10 13:08:26 +02:00
|
|
|
return tr( "Resize <strong>%2MB</strong> partition <strong>%1</strong> to "
|
|
|
|
"<strong>%3MB</strong>." )
|
2015-04-09 15:16:09 +02:00
|
|
|
.arg( partition()->partitionPath() )
|
|
|
|
.arg( partition()->capacity() / 1024 / 1024 )
|
|
|
|
.arg( ( m_newLastSector - m_newFirstSector + 1 ) * partition()->sectorSize() / 1024 / 1024 );
|
|
|
|
}
|
|
|
|
|
2015-06-13 02:26:38 +02:00
|
|
|
|
|
|
|
QString
|
|
|
|
ResizePartitionJob::prettyStatusMessage() const
|
|
|
|
{
|
|
|
|
return tr( "Resizing %2MB partition %1 to "
|
|
|
|
"%3MB." )
|
|
|
|
.arg( partition()->partitionPath() )
|
|
|
|
.arg( partition()->capacity() / 1024 / 1024 )
|
|
|
|
.arg( ( m_newLastSector - m_newFirstSector + 1 ) * partition()->sectorSize() / 1024 / 1024 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-05 14:57:00 +02:00
|
|
|
Calamares::JobResult
|
|
|
|
ResizePartitionJob::exec()
|
|
|
|
{
|
2014-08-05 16:11:34 +02:00
|
|
|
qint64 oldLength = m_oldLastSector - m_oldFirstSector + 1;
|
|
|
|
qint64 newLength = m_newLastSector - m_newFirstSector + 1;
|
|
|
|
|
2014-08-05 18:27:24 +02:00
|
|
|
// Assuming updatePreview() has been called, `partition` uses its new
|
|
|
|
// position and size. Reset it to the old values: part of the libparted
|
|
|
|
// backend relies on this (for example:
|
|
|
|
// LibPartedPartitionTable::updateGeometry())
|
|
|
|
// The jobs are responsible for updating the partition back when they are
|
|
|
|
// done.
|
|
|
|
m_partition->setFirstSector( m_oldFirstSector );
|
|
|
|
m_partition->setLastSector( m_oldLastSector );
|
|
|
|
|
2014-08-05 14:57:00 +02:00
|
|
|
CoreBackend* backend = CoreBackendManager::self()->backend();
|
|
|
|
QScopedPointer<CoreBackendDevice> backendDevice( backend->openDevice( m_device->deviceNode() ) );
|
|
|
|
if ( !backendDevice.data() )
|
|
|
|
{
|
2014-08-06 11:53:23 +02:00
|
|
|
QString errorMessage = tr( "The installer failed to resize partition %1 on disk '%2'." )
|
|
|
|
.arg( m_partition->partitionPath() )
|
|
|
|
.arg( m_device->name() );
|
2014-08-05 14:57:00 +02:00
|
|
|
return Calamares::JobResult::error(
|
2014-08-06 11:53:23 +02:00
|
|
|
errorMessage,
|
2014-08-05 14:57:00 +02:00
|
|
|
tr( "Could not open device '%1'." ).arg( m_device->deviceNode() )
|
|
|
|
);
|
|
|
|
}
|
2014-08-08 11:43:22 +02:00
|
|
|
QScopedPointer<CoreBackendPartitionTable> backendPartitionTable( backendDevice->openPartitionTable() );
|
2014-08-05 14:57:00 +02:00
|
|
|
|
2014-08-05 16:11:34 +02:00
|
|
|
// Create jobs
|
2014-08-05 14:57:00 +02:00
|
|
|
QList< Calamares::job_ptr > jobs;
|
2014-08-06 11:49:04 +02:00
|
|
|
jobs << Calamares::job_ptr( new CheckFileSystemJob( partition() ) );
|
2014-08-05 14:57:00 +02:00
|
|
|
if ( m_partition->roles().has( PartitionRole::Extended ) )
|
2014-08-08 11:43:22 +02:00
|
|
|
jobs << Calamares::job_ptr( new SetPartGeometryJob( backendPartitionTable.data(), m_partition, m_newFirstSector, newLength ) );
|
2014-08-05 14:57:00 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
bool shrink = newLength < oldLength;
|
|
|
|
bool grow = newLength > oldLength;
|
2014-08-05 18:27:24 +02:00
|
|
|
bool moveRight = m_newFirstSector > m_oldFirstSector;
|
|
|
|
bool moveLeft = m_newFirstSector < m_oldFirstSector;
|
2014-08-05 14:57:00 +02:00
|
|
|
if ( shrink )
|
|
|
|
{
|
2014-08-08 11:43:22 +02:00
|
|
|
jobs << Calamares::job_ptr( new ResizeFileSystemJob( m_device, backendPartitionTable.data(), m_partition, newLength ) );
|
|
|
|
jobs << Calamares::job_ptr( new SetPartGeometryJob( backendPartitionTable.data(), m_partition, m_oldFirstSector, newLength ) );
|
2014-08-05 14:57:00 +02:00
|
|
|
}
|
|
|
|
if ( moveRight || moveLeft )
|
|
|
|
{
|
|
|
|
// At this point, we need to set the partition's length to either the resized length, if it has already been
|
|
|
|
// shrunk, or to the original length (it may or may not then later be grown, we don't care here)
|
|
|
|
const qint64 length = shrink ? newLength : oldLength;
|
2014-08-08 11:43:22 +02:00
|
|
|
jobs << Calamares::job_ptr( new SetPartGeometryJob( backendPartitionTable.data(), m_partition, m_newFirstSector, length ) );
|
2014-08-05 18:27:24 +02:00
|
|
|
jobs << Calamares::job_ptr( new MoveFileSystemJob( m_device, m_partition, m_oldFirstSector, m_newFirstSector, length ) );
|
2014-08-05 14:57:00 +02:00
|
|
|
}
|
|
|
|
if ( grow )
|
|
|
|
{
|
2014-08-08 11:43:22 +02:00
|
|
|
jobs << Calamares::job_ptr( new SetPartGeometryJob( backendPartitionTable.data(), m_partition, m_newFirstSector, newLength ) );
|
|
|
|
jobs << Calamares::job_ptr( new ResizeFileSystemJob( m_device, backendPartitionTable.data(), m_partition, newLength ) );
|
2014-08-05 14:57:00 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-06 11:52:27 +02:00
|
|
|
jobs << Calamares::job_ptr( new CheckFileSystemJob( partition() ) );
|
2014-08-05 14:57:00 +02:00
|
|
|
return execJobList( jobs );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ResizePartitionJob::updatePreview()
|
|
|
|
{
|
|
|
|
m_device->partitionTable()->removeUnallocated();
|
|
|
|
m_partition->parent()->remove( m_partition );
|
|
|
|
m_partition->setFirstSector( m_newFirstSector );
|
|
|
|
m_partition->setLastSector( m_newLastSector );
|
|
|
|
m_partition->parent()->insert( m_partition );
|
|
|
|
m_device->partitionTable()->updateUnallocated( *m_device );
|
|
|
|
}
|
|
|
|
|
|
|
|
Calamares::JobResult
|
|
|
|
ResizePartitionJob::execJobList( const QList< Calamares::job_ptr >& jobs )
|
|
|
|
{
|
2014-08-05 17:38:26 +02:00
|
|
|
QString errorMessage = tr( "The installer failed to resize partition %1 on disk '%2'." )
|
2014-08-05 17:53:10 +02:00
|
|
|
.arg( m_partition->partitionPath() )
|
|
|
|
.arg( m_device->name() );
|
2014-08-05 17:38:26 +02:00
|
|
|
|
2014-08-05 14:57:00 +02:00
|
|
|
int nbJobs = jobs.size();
|
|
|
|
int count = 0;
|
|
|
|
for ( Calamares::job_ptr job : jobs )
|
|
|
|
{
|
2014-08-06 11:48:03 +02:00
|
|
|
cLog() << "- " + job->prettyName();
|
2014-08-05 14:57:00 +02:00
|
|
|
Calamares::JobResult result = job->exec();
|
|
|
|
if ( !result )
|
2014-08-05 17:38:26 +02:00
|
|
|
{
|
|
|
|
if ( result.message().isEmpty() )
|
|
|
|
result.setMessage( errorMessage );
|
2014-08-05 14:57:00 +02:00
|
|
|
return result;
|
2014-08-05 17:38:26 +02:00
|
|
|
}
|
2014-08-05 14:57:00 +02:00
|
|
|
++count;
|
|
|
|
progress( qreal( count ) / nbJobs );
|
|
|
|
}
|
|
|
|
return Calamares::JobResult::ok();
|
|
|
|
}
|
2014-10-29 13:06:06 +01:00
|
|
|
|
|
|
|
#include "ResizePartitionJob.moc"
|