kpmcore/src/jobs/createpartitionjob.cpp

91 lines
3.9 KiB
C++
Raw Normal View History

/*
SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
SPDX-FileCopyrightText: 2013-2018 Andrius Štikonas <andrius@stikonas.eu>
SPDX-FileCopyrightText: 2016 Chantara Tith <tith.chantara@gmail.com>
SPDX-FileCopyrightText: 2018 Caio Jordão Carvalho <caiojcarvalho@gmail.com>
SPDX-FileCopyrightText: 2019 Yuri Chornoivan <yurchor@ukr.net>
SPDX-FileCopyrightText: 2020 Gaël PORTAY <gael.portay@collabora.com>
SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "jobs/createpartitionjob.h"
#include "backend/corebackend.h"
#include "backend/corebackendmanager.h"
#include "backend/corebackenddevice.h"
#include "backend/corebackendpartitiontable.h"
#include "core/partition.h"
#include "core/device.h"
2016-06-23 19:41:55 +01:00
#include "core/lvmdevice.h"
#include "util/report.h"
#include <KLocalizedString>
/** Creates a new CreatePartitionJob
2015-07-13 15:16:36 +01:00
@param d the Device the Partition to be created will be on
@param p the Partition to create
*/
CreatePartitionJob::CreatePartitionJob(Device& d, Partition& p) :
2015-07-13 15:16:36 +01:00
Job(),
m_Device(d),
m_Partition(p)
{
}
bool CreatePartitionJob::run(Report& parent)
{
2015-07-13 15:16:36 +01:00
Q_ASSERT(partition().devicePath() == device().deviceNode());
2015-07-13 15:16:36 +01:00
bool rval = false;
2015-07-13 15:16:36 +01:00
Report* report = jobStarted(parent);
if (device().type() == Device::Type::Disk_Device || device().type() == Device::Type::SoftwareRAID_Device) {
std::unique_ptr<CoreBackendDevice> backendDevice = CoreBackendManager::self()->backend()->openDevice(device());
2016-05-31 19:28:31 +01:00
if (backendDevice) {
std::unique_ptr<CoreBackendPartitionTable> backendPartitionTable = backendDevice->openPartitionTable();
2016-05-31 19:28:31 +01:00
if (backendPartitionTable) {
QString partitionPath = backendPartitionTable->createPartition(*report, partition());
2019-12-09 16:09:53 +00:00
if (!partitionPath.isEmpty()) {
2016-05-31 19:28:31 +01:00
rval = true;
partition().setPartitionPath(partitionPath);
partition().setState(Partition::State::None);
2016-05-31 19:28:31 +01:00
backendPartitionTable->commit();
// The UUID is supported by GPT only; it is generated automatically once the creation of a partition.
// Store the generated UUID to the partition object if no UUID is set.
if (m_Device.partitionTable()->type() == PartitionTable::gpt && partition().uuid().isEmpty())
partition().setUUID(backendPartitionTable->getPartitionUUID(*report, partition()));
2016-05-31 19:28:31 +01:00
} else
report->line() << xi18nc("@info/plain", "Failed to add partition <filename>%1</filename> to device <filename>%2</filename>.", partition().deviceNode(), device().deviceNode());
2015-07-13 15:16:36 +01:00
} else
2016-05-31 19:28:31 +01:00
report->line() << xi18nc("@info:progress", "Could not open partition table on device <filename>%1</filename> to create new partition <filename>%2</filename>.", device().deviceNode(), partition().deviceNode());
2015-07-13 15:16:36 +01:00
} else
2016-05-31 19:28:31 +01:00
report->line() << xi18nc("@info:progress", "Could not open device <filename>%1</filename> to create new partition <filename>%2</filename>.", device().deviceNode(), partition().deviceNode());
} else if (device().type() == Device::Type::LVM_Device) {
2016-06-23 19:41:55 +01:00
LvmDevice& dev = dynamic_cast<LvmDevice&>(device());
partition().setState(Partition::State::None);
2016-06-25 12:01:32 +01:00
QString partPath = partition().partitionPath();
QString lvname = partPath.right(partPath.length() - partPath.lastIndexOf(QStringLiteral("/")) - 1);
rval = LvmDevice::createLV(*report, dev, partition(), lvname);
2016-05-31 19:28:31 +01:00
}
2015-07-13 15:16:36 +01:00
jobFinished(*report, rval);
2015-07-13 15:16:36 +01:00
return rval;
}
QString CreatePartitionJob::description() const
{
2015-07-13 15:16:36 +01:00
if (partition().number() > 0)
return xi18nc("@info:progress", "Create new partition <filename>%1</filename>", partition().deviceNode());
return xi18nc("@info:progress", "Create new partition on device <filename>%1</filename>", device().deviceNode());
}