From fcc8a7c992a54ea22a624a664e7b51b0f269d8e8 Mon Sep 17 00:00:00 2001 From: Chantara Tith Date: Wed, 1 Jun 2016 01:28:31 +0700 Subject: [PATCH] Initial Device separation --- src/core/CMakeLists.txt | 2 + src/core/copysourcedevice.cpp | 2 +- src/core/copytargetdevice.cpp | 2 +- src/core/device.cpp | 79 ++++-------------- src/core/device.h | 92 ++++++++++----------- src/core/devicescanner.cpp | 1 + src/core/diskdevice.cpp | 95 ++++++++++++++++++++++ src/core/diskdevice.h | 84 +++++++++++++++++++ src/core/partition.cpp | 2 +- src/core/partitionalignment.cpp | 21 +++-- src/core/partitionrole.h | 1 + src/core/partitiontable.cpp | 81 ++++++++++-------- src/core/partitiontable.h | 3 +- src/jobs/backupfilesystemjob.cpp | 2 +- src/jobs/createpartitionjob.cpp | 36 ++++---- src/jobs/resizefilesystemjob.cpp | 2 +- src/ops/copyoperation.cpp | 4 +- src/ops/resizeoperation.cpp | 6 +- src/ops/restoreoperation.cpp | 4 +- src/plugins/dummy/dummybackend.cpp | 4 +- src/plugins/libparted/libpartedbackend.cpp | 8 +- src/plugins/libparted/libpartedbackend.h | 1 - 22 files changed, 345 insertions(+), 187 deletions(-) create mode 100644 src/core/diskdevice.cpp create mode 100644 src/core/diskdevice.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 73e742f..60b420b 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -15,6 +15,7 @@ set(CORE_SRC core/devicescanner.cpp core/partitionnode.cpp core/partitionalignment.cpp + core/diskdevice.cpp core/device.cpp core/operationstack.cpp core/partitionrole.cpp @@ -25,6 +26,7 @@ set(CORE_LIB_HDRS core/copysourcedevice.h core/copytarget.h core/copytargetdevice.h + core/diskdevice.h core/device.h core/devicescanner.h core/mountentry.h diff --git a/src/core/copysourcedevice.cpp b/src/core/copysourcedevice.cpp index 34a5e69..d147429 100644 --- a/src/core/copysourcedevice.cpp +++ b/src/core/copysourcedevice.cpp @@ -60,7 +60,7 @@ bool CopySourceDevice::open() */ qint32 CopySourceDevice::sectorSize() const { - return device().logicalSectorSize(); + return device().logicalSize(); } /** Returns the length of this CopySource diff --git a/src/core/copytargetdevice.cpp b/src/core/copytargetdevice.cpp index ec6b0f2..b53004a 100644 --- a/src/core/copytargetdevice.cpp +++ b/src/core/copytargetdevice.cpp @@ -56,7 +56,7 @@ bool CopyTargetDevice::open() /** @return the Device's sector size */ qint32 CopyTargetDevice::sectorSize() const { - return device().logicalSectorSize(); + return device().logicalSize(); } /** Writes the given number of sectors to the Device. diff --git a/src/core/device.cpp b/src/core/device.cpp index 46bdc86..8f6e377 100644 --- a/src/core/device.cpp +++ b/src/core/device.cpp @@ -17,7 +17,6 @@ *************************************************************************/ #include "core/device.h" - #include "core/partitiontable.h" #include "core/smartstatus.h" @@ -25,73 +24,25 @@ #include -#include -#include - -#include -#include -#include -#include -#include -#include - -#if !defined(BLKPBSZGET) -#define BLKPBSZGET _IO(0x12,123)/* get block physical sector size */ -#endif - -static qint32 getPhysicalSectorSize(const QString& device_node) -{ - /* - * possible ways of getting the physical sector size for a drive: - * - ioctl(BLKPBSZGET) -- supported with Linux 2.6.32 and later - * - /sys/block/sda/queue/physical_block_size - * - libblkid from util-linux-ng 2.17 or later - * TODO: implement the blkid method - */ - -#if defined(BLKPBSZGET) - int phSectorSize = -1; - int fd = open(device_node.toLocal8Bit().constData(), O_RDONLY); - if (fd != -1) { - if (ioctl(fd, BLKPBSZGET, &phSectorSize) >= 0) { - close(fd); - return phSectorSize; - } - - close(fd); - } -#endif - - QFile f(QStringLiteral("/sys/block/%1/queue/physical_block_size").arg(QString(device_node).remove(QStringLiteral("/dev/")))); - - if (f.open(QIODevice::ReadOnly)) { - QByteArray a = f.readLine(); - return a.trimmed().toInt(); - } - - return -1; -} - /** Constructs a Device with an empty PartitionTable. @param name the Device's name, usually some string defined by the manufacturer @param devicenode the Device's node, for example "/dev/sda" - @param heads the number of heads in CHS notation - @param numSectors the number of sectors in CHS notation - @param cylinders the number of cylinders in CHS notation - @param sectorSize the size of a sector in bytes */ -Device::Device(const QString& name, const QString& devicenode, qint32 heads, qint32 numSectors, qint32 cylinders, qint64 sectorSize, const QString& iconname) : - QObject(), - m_Name(name.length() > 0 ? name : i18n("Unknown Device")), - m_DeviceNode(devicenode), - m_PartitionTable(nullptr), - m_Heads(heads), - m_SectorsPerTrack(numSectors), - m_Cylinders(cylinders), - m_LogicalSectorSize(sectorSize), - m_PhysicalSectorSize(getPhysicalSectorSize(devicenode)), - m_IconName(iconname.isEmpty() ? QStringLiteral("drive-harddisk") : iconname), - m_SmartStatus(new SmartStatus(devicenode)) +Device::Device(const QString& name, + const QString& devicenode, + const qint32 logicalSize, + const qint64 totalLogical, + const QString& iconname, + Device::Type type) + : QObject() + , m_Name(name.length() > 0 ? name : i18n("Unknown Device")) + , m_DeviceNode(devicenode) + , m_LogicalSize(logicalSize) + , m_TotalLogical(totalLogical) + , m_PartitionTable(nullptr) + , m_IconName(iconname.isEmpty() ? QStringLiteral("drive-harddisk") : iconname) + , m_SmartStatus(new SmartStatus(devicenode)) + , m_Type(type) { } diff --git a/src/core/device.h b/src/core/device.h index 9b440e3..1190fec 100644 --- a/src/core/device.h +++ b/src/core/device.h @@ -30,7 +30,7 @@ class CreatePartitionTableOperation; class CoreBackend; class SmartStatus; -/** A device. +/** A abstract device interface. Represents a device like /dev/sda. @@ -47,81 +47,81 @@ class LIBKPMCORE_EXPORT Device : public QObject friend class CoreBackend; public: - Device(const QString& name, const QString& devicenode, qint32 heads, qint32 numSectors, qint32 cylinders, qint64 sectorSize, const QString& iconname = QString()); - ~Device(); + enum Type { + Disk_Device = 0, + LVM_Device = 1, /* VG */ + RAID_Device = 2, /* software RAID device */ + Unknown_Device = 4 + }; + +protected: + Device(const QString& name, const QString& devicenode, const qint32 logicalSize, const qint64 totalLogical, const QString& iconname = QString(), Device::Type type = Device::Disk_Device); public: - bool operator==(const Device& other) const; - bool operator!=(const Device& other) const; + virtual ~Device(); - const QString& name() const { +public: + virtual bool operator==(const Device& other) const; + virtual bool operator!=(const Device& other) const; + + virtual const QString& name() const { return m_Name; /**< @return the Device's name, usually some manufacturer string */ } - const QString& deviceNode() const { + virtual const QString& deviceNode() const { return m_DeviceNode; /**< @return the Device's node, for example "/dev/sda" */ } - PartitionTable* partitionTable() { + + virtual PartitionTable* partitionTable() { return m_PartitionTable; /**< @return the Device's PartitionTable */ } - const PartitionTable* partitionTable() const { + virtual const PartitionTable* partitionTable() const { return m_PartitionTable; /**< @return the Device's PartitionTable */ } - qint32 heads() const { - return m_Heads; /**< @return the number of heads on the Device in CHS notation */ - } - qint32 cylinders() const { - return m_Cylinders; /**< @return the number of cylinders on the Device in CHS notation */ - } - qint32 sectorsPerTrack() const { - return m_SectorsPerTrack; /**< @return the number of sectors on the Device in CHS notation */ - } - qint32 physicalSectorSize() const { - return m_PhysicalSectorSize; /**< @return the physical sector size the Device uses or -1 if unknown */ - } - qint32 logicalSectorSize() const { - return m_LogicalSectorSize; /**< @return the logical sector size the Device uses */ - } - qint64 totalSectors() const { - return static_cast(heads()) * cylinders() * sectorsPerTrack(); /**< @return the total number of sectors on the device */ - } - qint64 capacity() const { - return totalSectors() * logicalSectorSize(); /**< @return the Device's capacity in bytes */ - } - qint64 cylinderSize() const { - return static_cast(heads()) * sectorsPerTrack(); /**< @return the size of a cylinder on this Device in sectors */ - } - void setIconName(const QString& name) { + virtual qint64 capacity() const { /**< @return the Device's capacity in bytes */ + return logicalSize() * totalLogical(); + } + + virtual void setIconName(const QString& name) { m_IconName = name; } - const QString& iconName() const { + virtual const QString& iconName() const { return m_IconName; /**< @return suggested icon name for this Device */ } - SmartStatus& smartStatus() { + virtual SmartStatus& smartStatus() { return *m_SmartStatus; } - const SmartStatus& smartStatus() const { + virtual const SmartStatus& smartStatus() const { return *m_SmartStatus; } - QString prettyName() const; - - void setPartitionTable(PartitionTable* ptable) { + virtual void setPartitionTable(PartitionTable* ptable) { m_PartitionTable = ptable; } -private: + virtual qint32 logicalSize() const { + return m_LogicalSize; + } + virtual qint64 totalLogical() const { + return m_TotalLogical; + } + + virtual Device::Type type() const { + return m_Type; + } + + virtual QString prettyName() const; + +protected: QString m_Name; QString m_DeviceNode; + qint32 m_LogicalSize; + qint32 m_TotalLogical; PartitionTable* m_PartitionTable; - qint32 m_Heads; - qint32 m_SectorsPerTrack; - qint32 m_Cylinders; - qint32 m_LogicalSectorSize; - qint32 m_PhysicalSectorSize; QString m_IconName; SmartStatus* m_SmartStatus; + Device::Type m_Type; }; #endif diff --git a/src/core/devicescanner.cpp b/src/core/devicescanner.cpp index e835db5..9e67a3b 100644 --- a/src/core/devicescanner.cpp +++ b/src/core/devicescanner.cpp @@ -57,6 +57,7 @@ void DeviceScanner::scan() clear(); QList deviceList = CoreBackendManager::self()->backend()->scanDevices(); + //TODO: Scan for LVM here and add to the list. foreach(Device * d, deviceList) operationStack().addDevice(d); diff --git a/src/core/diskdevice.cpp b/src/core/diskdevice.cpp new file mode 100644 index 0000000..e1026d4 --- /dev/null +++ b/src/core/diskdevice.cpp @@ -0,0 +1,95 @@ +/************************************************************************* + * Copyright (C) 2008 by Volker Lanz * + * Copyright (C) 2016 by Andrius Štikonas * + * * + * 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 3 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, see .* + *************************************************************************/ + +#include "core/diskdevice.h" + +#include "core/partitiontable.h" +#include "core/smartstatus.h" + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +#if !defined(BLKPBSZGET) +#define BLKPBSZGET _IO(0x12,123)/* get block physical sector size */ +#endif + +static qint32 getPhysicalSectorSize(const QString& device_node) +{ + /* + * possible ways of getting the physical sector size for a drive: + * - ioctl(BLKPBSZGET) -- supported with Linux 2.6.32 and later + * - /sys/block/sda/queue/physical_block_size + * - libblkid from util-linux-ng 2.17 or later + * TODO: implement the blkid method + */ + +#if defined(BLKPBSZGET) + int phSectorSize = -1; + int fd = open(device_node.toLocal8Bit().constData(), O_RDONLY); + if (fd != -1) { + if (ioctl(fd, BLKPBSZGET, &phSectorSize) >= 0) { + close(fd); + return phSectorSize; + } + + close(fd); + } +#endif + + QFile f(QStringLiteral("/sys/block/%1/queue/physical_block_size").arg(QString(device_node).remove(QStringLiteral("/dev/")))); + + if (f.open(QIODevice::ReadOnly)) { + QByteArray a = f.readLine(); + return a.trimmed().toInt(); + } + + return -1; +} + +/** Constructs a Disk Device with an empty PartitionTable. + @param name the Device's name, usually some string defined by the manufacturer + @param devicenode the Device's node, for example "/dev/sda" + @param heads the number of heads in CHS notation + @param numSectors the number of sectors in CHS notation + @param cylinders the number of cylinders in CHS notation + @param sectorSize the size of a sector in bytes +*/ +DiskDevice::DiskDevice(const QString& name, + const QString& devicenode, + qint32 heads, + qint32 numSectors, + qint32 cylinders, + qint32 sectorSize, + const QString& iconname) + : Device(name, devicenode, sectorSize, (static_cast(heads) * cylinders * numSectors), iconname, Device::Disk_Device) + , m_Heads(heads) + , m_SectorsPerTrack(numSectors) + , m_Cylinders(cylinders) + , m_LogicalSectorSize(sectorSize) + , m_PhysicalSectorSize(getPhysicalSectorSize(devicenode)) +{ +} diff --git a/src/core/diskdevice.h b/src/core/diskdevice.h new file mode 100644 index 0000000..0ecb28e --- /dev/null +++ b/src/core/diskdevice.h @@ -0,0 +1,84 @@ +/************************************************************************* + * Copyright (C) 2008 by Volker Lanz * + * * + * 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 3 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, see .* + *************************************************************************/ + +#if !defined(DISKDEVICE__H) + +#define DISKDEVICE__H + +#include "util/libpartitionmanagerexport.h" +#include "core/device.h" + +#include +#include +#include + +class PartitionTable; +class CreatePartitionTableOperation; +class CoreBackend; +class SmartStatus; + +/** A disk device. + + Represents a device like /dev/sda. + + Devices are the outermost entity; they contain a PartitionTable that itself contains Partitions. + + @see PartitionTable, Partition + @author Volker Lanz +*/ +class LIBKPMCORE_EXPORT DiskDevice : public Device +{ + Q_DISABLE_COPY(DiskDevice) + + friend class CreatePartitionTableOperation; + friend class CoreBackend; + +public: + DiskDevice(const QString& name, const QString& devicenode, qint32 heads, qint32 numSectors, qint32 cylinders, qint32 sectorSize, const QString& iconname = QString()); + +public: + qint32 heads() const { + return m_Heads; /**< @return the number of heads on the Device in CHS notation */ + } + qint32 cylinders() const { + return m_Cylinders; /**< @return the number of cylinders on the Device in CHS notation */ + } + qint32 sectorsPerTrack() const { + return m_SectorsPerTrack; /**< @return the number of sectors on the Device in CHS notation */ + } + qint32 physicalSectorSize() const { + return m_PhysicalSectorSize; /**< @return the physical sector size the Device uses or -1 if unknown */ + } + qint32 logicalSectorSize() const { + return m_LogicalSectorSize; /**< @return the logical sector size the Device uses */ + } + qint64 totalSectors() const { + return static_cast(heads()) * cylinders() * sectorsPerTrack(); /**< @return the total number of sectors on the device */ + } + qint64 cylinderSize() const { + return static_cast(heads()) * sectorsPerTrack(); /**< @return the size of a cylinder on this Device in sectors */ + } + +private: + qint32 m_Heads; + qint32 m_SectorsPerTrack; + qint32 m_Cylinders; + qint32 m_LogicalSectorSize; + qint32 m_PhysicalSectorSize; +}; + +#endif diff --git a/src/core/partition.cpp b/src/core/partition.cpp index b1095b9..50eaf37 100644 --- a/src/core/partition.cpp +++ b/src/core/partition.cpp @@ -62,11 +62,11 @@ Partition::Partition(PartitionNode* parent, const Device& device, const Partitio m_AvailableFlags(availableFlags), m_ActiveFlags(activeFlags), m_IsMounted(mounted), - m_SectorSize(device.logicalSectorSize()), m_State(state) { setPartitionPath(partitionPath); Q_ASSERT(m_Parent); + m_SectorSize = device.logicalSize(); } /** Destroys a Partition, destroying its children and its FileSystem */ diff --git a/src/core/partitionalignment.cpp b/src/core/partitionalignment.cpp index 97fa827..9762a25 100644 --- a/src/core/partitionalignment.cpp +++ b/src/core/partitionalignment.cpp @@ -21,6 +21,7 @@ #include "core/partition.h" #include "core/partitiontable.h" #include "core/device.h" +#include "core/diskdevice.h" #include "fs/filesystem.h" @@ -32,12 +33,14 @@ int PartitionAlignment::s_sectorAlignment = 2048; qint64 PartitionAlignment::firstDelta(const Device& d, const Partition& p, qint64 s) { + //TODO: make sure things work for LVM if (d.partitionTable()->type() == PartitionTable::msdos) { - if (p.roles().has(PartitionRole::Logical) && s == 2 * d.sectorsPerTrack()) - return (s - (2 * d.sectorsPerTrack())) % sectorAlignment(d); + const DiskDevice& diskDevice = dynamic_cast(d); + if (p.roles().has(PartitionRole::Logical) && s == 2 * diskDevice.sectorsPerTrack()) + return (s - (2 * diskDevice.sectorsPerTrack())) % sectorAlignment(d); - if (p.roles().has(PartitionRole::Logical) || s == d.sectorsPerTrack()) - return (s - d.sectorsPerTrack()) % sectorAlignment(d); + if (p.roles().has(PartitionRole::Logical) || s == diskDevice.sectorsPerTrack()) + return (s - diskDevice.sectorsPerTrack()) % sectorAlignment(d); } return s % sectorAlignment(d); @@ -50,12 +53,14 @@ qint64 PartitionAlignment::lastDelta(const Device& d, const Partition&, qint64 s bool PartitionAlignment::isLengthAligned(const Device& d, const Partition& p) { + //TODO: make sure things work for LVM if (d.partitionTable()->type() == PartitionTable::msdos) { - if (p.roles().has(PartitionRole::Logical) && p.firstSector() == 2 * d.sectorsPerTrack()) - return (p.length() + (2 * d.sectorsPerTrack())) % sectorAlignment(d) == 0; + const DiskDevice& diskDevice = dynamic_cast(d); + if (p.roles().has(PartitionRole::Logical) && p.firstSector() == 2 * diskDevice.sectorsPerTrack()) + return (p.length() + (2 * diskDevice.sectorsPerTrack())) % sectorAlignment(d) == 0; - if (p.roles().has(PartitionRole::Logical) || p.firstSector() == d.sectorsPerTrack()) - return (p.length() + d.sectorsPerTrack()) % sectorAlignment(d) == 0; + if (p.roles().has(PartitionRole::Logical) || p.firstSector() == diskDevice.sectorsPerTrack()) + return (p.length() + diskDevice.sectorsPerTrack()) % sectorAlignment(d) == 0; } return p.length() % sectorAlignment(d) == 0; diff --git a/src/core/partitionrole.h b/src/core/partitionrole.h index 39c2cb2..26df5f1 100644 --- a/src/core/partitionrole.h +++ b/src/core/partitionrole.h @@ -43,6 +43,7 @@ public: Logical = 4, /**< Logical inside an extended */ Unallocated = 8, /**< No real Partition, just unallocated space */ Luks = 16, /**< Encrypted partition with LUKS key management */ + Lvm_Lv = 32, /**< Logical Volume of LVM */ Any = 255 /**< In case we're looking for a Partition with a PartitionRole, any will do */ }; diff --git a/src/core/partitiontable.cpp b/src/core/partitiontable.cpp index 3f88628..e19554b 100644 --- a/src/core/partitiontable.cpp +++ b/src/core/partitiontable.cpp @@ -23,8 +23,8 @@ #include "core/partitiontable.h" #include "core/partition.h" #include "core/device.h" +#include "core/diskdevice.h" #include "core/partitionalignment.h" - #include "fs/filesystem.h" #include "fs/filesystemfactory.h" @@ -240,29 +240,38 @@ QStringList PartitionTable::flagNames(Flags flags) return rval; } -bool PartitionTable::getUnallocatedRange(const Device& device, PartitionNode& parent, qint64& start, qint64& end) +bool PartitionTable::getUnallocatedRange(const Device& d, PartitionNode& parent, qint64& start, qint64& end) { - if (!parent.isRoot()) { - Partition* extended = dynamic_cast(&parent); + //TODO: alignment for LVM device + if (d.type() == Device::Disk_Device) { + const DiskDevice& device = dynamic_cast(d); + if (!parent.isRoot()) { + Partition* extended = dynamic_cast(&parent); - if (extended == nullptr) { - qWarning() << "extended is null. start: " << start << ", end: " << end << ", device: " << device.deviceNode(); - return false; + if (extended == nullptr) { + qWarning() << "extended is null. start: " << start << ", end: " << end << ", device: " << device.deviceNode(); + return false; + } + + // Leave a track (cylinder aligned) or sector alignment sectors (sector based) free at the + // start for a new partition's metadata + start += device.partitionTable()->type() == PartitionTable::msdos ? device.sectorsPerTrack() : PartitionAlignment::sectorAlignment(device); + + // .. and also at the end for the metadata for a partition to follow us, if we're not + // at the end of the extended partition + if (end < extended->lastSector()) + end -= device.partitionTable()->type() == PartitionTable::msdos ? device.sectorsPerTrack() : PartitionAlignment::sectorAlignment(device); } - // Leave a track (cylinder aligned) or sector alignment sectors (sector based) free at the - // start for a new partition's metadata - start += device.partitionTable()->type() == PartitionTable::msdos ? device.sectorsPerTrack() : PartitionAlignment::sectorAlignment(device); - - // .. and also at the end for the metadata for a partition to follow us, if we're not - // at the end of the extended partition - if (end < extended->lastSector()) - end -= device.partitionTable()->type() == PartitionTable::msdos ? device.sectorsPerTrack() : PartitionAlignment::sectorAlignment(device); + return end - start + 1 >= PartitionAlignment::sectorAlignment(device); + } else if (d.type() == Device::LVM_Device) { + return false; } - - return end - start + 1 >= PartitionAlignment::sectorAlignment(device); + return false; } + + /** Creates a new unallocated Partition on the given Device. @param device the Device to create the new Partition on @param parent the parent PartitionNode for the new Partition @@ -369,15 +378,16 @@ void PartitionTable::updateUnallocated(const Device& d) qint64 PartitionTable::defaultFirstUsable(const Device& d, TableType t) { Q_UNUSED(t) - return PartitionAlignment::sectorAlignment(d); + const DiskDevice& diskDevice = dynamic_cast(d); + return PartitionAlignment::sectorAlignment(diskDevice); } qint64 PartitionTable::defaultLastUsable(const Device& d, TableType t) { if (t == gpt) - return d.totalSectors() - 1 - 32 - 1; + return d.totalLogical() - 1 - 32 - 1; - return d.totalSectors() - 1; + return d.totalLogical() - 1; } static struct { @@ -452,26 +462,31 @@ bool PartitionTable::tableTypeIsReadOnly(TableType l) */ bool PartitionTable::isSectorBased(const Device& d) const { - if (type() == PartitionTable::msdos) { - // the default for empty partition tables is sector based - if (numPrimaries() == 0) - return true; + if (d.type() == Device::Disk_Device) { + const DiskDevice& diskDevice = dynamic_cast(d); - quint32 numCylinderAligned = 0; - quint32 numSectorAligned = 0; + if (type() == PartitionTable::msdos) { + // the default for empty partition tables is sector based + if (numPrimaries() == 0) + return true; - // see if we have more cylinder aligned partitions than sector - // aligned ones. - foreach(const Partition * p, children()) - if (p->firstSector() % PartitionAlignment::sectorAlignment(d) == 0) + quint32 numCylinderAligned = 0; + quint32 numSectorAligned = 0; + + // see if we have more cylinder aligned partitions than sector + // aligned ones. + foreach(const Partition * p, children()) + if (p->firstSector() % PartitionAlignment::sectorAlignment(diskDevice) == 0) numSectorAligned++; - else if (p->firstSector() % d.cylinderSize() == 0) + else if (p->firstSector() % diskDevice.cylinderSize() == 0) numCylinderAligned++; - return numSectorAligned >= numCylinderAligned; + return numSectorAligned >= numCylinderAligned; + } + return type() == PartitionTable::msdos_sectorbased; } - return type() == PartitionTable::msdos_sectorbased; + return false; } void PartitionTable::setType(const Device& d, TableType t) diff --git a/src/core/partitiontable.h b/src/core/partitiontable.h index 0881c08..aebc15b 100644 --- a/src/core/partitiontable.h +++ b/src/core/partitiontable.h @@ -64,7 +64,8 @@ public: mac, pc98, amiga, - sun + sun, + vmd /* Volume Manager Device */ }; /** Partition flags */ diff --git a/src/jobs/backupfilesystemjob.cpp b/src/jobs/backupfilesystemjob.cpp index 4fc2183..9d84f84 100644 --- a/src/jobs/backupfilesystemjob.cpp +++ b/src/jobs/backupfilesystemjob.cpp @@ -57,7 +57,7 @@ bool BackupFileSystemJob::run(Report& parent) rval = sourcePartition().fileSystem().backup(*report, sourceDevice(), sourcePartition().deviceNode(), fileName()); else if (sourcePartition().fileSystem().supportBackup() == FileSystem::cmdSupportCore) { CopySourceDevice copySource(sourceDevice(), sourcePartition().fileSystem().firstSector(), sourcePartition().fileSystem().lastSector()); - CopyTargetFile copyTarget(fileName(), sourceDevice().logicalSectorSize()); + CopyTargetFile copyTarget(fileName(), sourceDevice().logicalSize()); if (!copySource.open()) report->line() << xi18nc("@info:progress", "Could not open file system on source partition %1 for backup.", sourcePartition().deviceNode()); diff --git a/src/jobs/createpartitionjob.cpp b/src/jobs/createpartitionjob.cpp index 87ad824..5c02b05 100644 --- a/src/jobs/createpartitionjob.cpp +++ b/src/jobs/createpartitionjob.cpp @@ -49,29 +49,33 @@ bool CreatePartitionJob::run(Report& parent) Report* report = jobStarted(parent); - CoreBackendDevice* backendDevice = CoreBackendManager::self()->backend()->openDevice(device().deviceNode()); + if (device().type() == Device::Disk_Device) { + CoreBackendDevice* backendDevice = CoreBackendManager::self()->backend()->openDevice(device().deviceNode()); - if (backendDevice) { - CoreBackendPartitionTable* backendPartitionTable = backendDevice->openPartitionTable(); + if (backendDevice) { + CoreBackendPartitionTable* backendPartitionTable = backendDevice->openPartitionTable(); - if (backendPartitionTable) { - QString partitionPath = backendPartitionTable->createPartition(*report, partition()); + if (backendPartitionTable) { + QString partitionPath = backendPartitionTable->createPartition(*report, partition()); - if (partitionPath != QString()) { - rval = true; - partition().setPartitionPath(partitionPath); - partition().setState(Partition::StateNone); - backendPartitionTable->commit(); + if (partitionPath != QString()) { + rval = true; + partition().setPartitionPath(partitionPath); + partition().setState(Partition::StateNone); + backendPartitionTable->commit(); + } else + report->line() << xi18nc("@info/plain", "Failed to add partition %1 to device %2.", partition().deviceNode(), device().deviceNode()); + + delete backendPartitionTable; } else - report->line() << xi18nc("@info:progress", "Failed to add partition %1 to device %2.", partition().deviceNode(), device().deviceNode()); + report->line() << xi18nc("@info:progress", "Could not open partition table on device %1 to create new partition %2.", device().deviceNode(), partition().deviceNode()); - delete backendPartitionTable; + delete backendDevice; } else - report->line() << xi18nc("@info:progress", "Could not open partition table on device %1 to create new partition %2.", device().deviceNode(), partition().deviceNode()); + report->line() << xi18nc("@info:progress", "Could not open device %1 to create new partition %2.", device().deviceNode(), partition().deviceNode()); + } else if (device().type() == Device::LVM_Device) { - delete backendDevice; - } else - report->line() << xi18nc("@info:progress", "Could not open device %1 to create new partition %2.", device().deviceNode(), partition().deviceNode()); + } jobFinished(*report, rval); diff --git a/src/jobs/resizefilesystemjob.cpp b/src/jobs/resizefilesystemjob.cpp index 8bad2d2..60a81ba 100644 --- a/src/jobs/resizefilesystemjob.cpp +++ b/src/jobs/resizefilesystemjob.cpp @@ -86,7 +86,7 @@ bool ResizeFileSystemJob::run(Report& parent) } case FileSystem::cmdSupportFileSystem: { - const qint64 newLengthInByte = Capacity(newLength() * device().logicalSectorSize()).toInt(Capacity::Byte); + const qint64 newLengthInByte = Capacity(newLength() * device().logicalSize()).toInt(Capacity::Byte); if (partition().isMounted()) rval = partition().fileSystem().resizeOnline(*report, partition().deviceNode(), partition().mountPoint(), newLengthInByte); else diff --git a/src/ops/copyoperation.cpp b/src/ops/copyoperation.cpp index 878e3a2..b21a7ae 100644 --- a/src/ops/copyoperation.cpp +++ b/src/ops/copyoperation.cpp @@ -211,7 +211,7 @@ QString CopyOperation::updateDescription() const sourcePartition().deviceNode(), Capacity::formatByteSize(sourcePartition().capacity()), sourcePartition().fileSystem().name(), - Capacity::formatByteSize(copiedPartition().firstSector() * targetDevice().logicalSectorSize()), + Capacity::formatByteSize(copiedPartition().firstSector() * targetDevice().logicalSize()), targetDevice().deviceNode() ); @@ -219,7 +219,7 @@ QString CopyOperation::updateDescription() const sourcePartition().deviceNode(), Capacity::formatByteSize(sourcePartition().capacity()), sourcePartition().fileSystem().name(), - Capacity::formatByteSize(copiedPartition().firstSector() * targetDevice().logicalSectorSize()), + Capacity::formatByteSize(copiedPartition().firstSector() * targetDevice().logicalSize()), targetDevice().deviceNode(), Capacity::formatByteSize(copiedPartition().capacity()) ); diff --git a/src/ops/resizeoperation.cpp b/src/ops/resizeoperation.cpp index 95ccb7a..13a48d5 100644 --- a/src/ops/resizeoperation.cpp +++ b/src/ops/resizeoperation.cpp @@ -199,10 +199,10 @@ QString ResizeOperation::description() const // Each of these needs a different description. And for reasons of i18n, we cannot // just concatenate strings together... - const QString moveDelta = Capacity::formatByteSize(qAbs(newFirstSector() - origFirstSector()) * targetDevice().logicalSectorSize()); + const QString moveDelta = Capacity::formatByteSize(qAbs(newFirstSector() - origFirstSector()) * targetDevice().logicalSize()); - const QString origCapacity = Capacity::formatByteSize(origLength() * targetDevice().logicalSectorSize()); - const QString newCapacity = Capacity::formatByteSize(newLength() * targetDevice().logicalSectorSize()); + const QString origCapacity = Capacity::formatByteSize(origLength() * targetDevice().logicalSize()); + const QString newCapacity = Capacity::formatByteSize(newLength() * targetDevice().logicalSize()); switch (resizeAction()) { case MoveLeft: diff --git a/src/ops/restoreoperation.cpp b/src/ops/restoreoperation.cpp index de7459f..670d7b1 100644 --- a/src/ops/restoreoperation.cpp +++ b/src/ops/restoreoperation.cpp @@ -164,7 +164,7 @@ QString RestoreOperation::description() const if (overwrittenPartition()) return xi18nc("@info:status", "Restore partition from %1 to %2", fileName(), overwrittenPartition()->deviceNode()); - return xi18nc("@info:status", "Restore partition on %1 at %2 from %3", targetDevice().deviceNode(), Capacity::formatByteSize(restorePartition().firstSector() * targetDevice().logicalSectorSize()), fileName()); + return xi18nc("@info:status", "Restore partition on %1 at %2 from %3", targetDevice().deviceNode(), Capacity::formatByteSize(restorePartition().firstSector() * targetDevice().logicalSize()), fileName()); } void RestoreOperation::setOverwrittenPartition(Partition* p) @@ -222,7 +222,7 @@ Partition* RestoreOperation::createRestorePartition(const Device& device, Partit if (!fileInfo.exists()) return nullptr; - const qint64 end = start + fileInfo.size() / device.logicalSectorSize() - 1; + const qint64 end = start + fileInfo.size() / device.logicalSize() - 1; Partition* p = new Partition(&parent, device, PartitionRole(r), FileSystemFactory::create(FileSystem::Unknown, start, end), start, end, QString()); p->setState(Partition::StateRestore); diff --git a/src/plugins/dummy/dummybackend.cpp b/src/plugins/dummy/dummybackend.cpp index 6e16222..e62e6e5 100644 --- a/src/plugins/dummy/dummybackend.cpp +++ b/src/plugins/dummy/dummybackend.cpp @@ -22,7 +22,7 @@ #include "plugins/dummy/dummybackend.h" #include "plugins/dummy/dummydevice.h" -#include "core/device.h" +#include "core/diskdevice.h" #include "core/partition.h" #include "core/partitiontable.h" @@ -59,7 +59,7 @@ QList DummyBackend::scanDevices(bool excludeLoop) Device* DummyBackend::scanDevice(const QString& device_node) { - Device* d = new Device(QStringLiteral("Dummy Device"), QStringLiteral("/tmp") + device_node, 255, 30, 63, 512); + DiskDevice* d = new DiskDevice(QStringLiteral("Dummy Device"), QStringLiteral("/tmp") + device_node, 255, 30, 63, 512); CoreBackend::setPartitionTableForDevice(*d, new PartitionTable(PartitionTable::msdos_sectorbased, 2048, d->totalSectors() - 2048)); CoreBackend::setPartitionTableMaxPrimaries(*d->partitionTable(), 128); d->partitionTable()->updateUnallocated(*d); diff --git a/src/plugins/libparted/libpartedbackend.cpp b/src/plugins/libparted/libpartedbackend.cpp index 5e117bc..248ccfe 100644 --- a/src/plugins/libparted/libpartedbackend.cpp +++ b/src/plugins/libparted/libpartedbackend.cpp @@ -24,7 +24,7 @@ #include "plugins/libparted/libparteddevice.h" #include "plugins/libparted/pedflags.h" -#include "core/device.h" +#include "core/diskdevice.h" #include "core/partition.h" #include "core/partitiontable.h" #include "core/partitionalignment.h" @@ -99,7 +99,7 @@ static qint64 readSectorsUsedLibParted(const Partition& p) @param p the Partition the FileSystem is on @param mountPoint mount point of the partition in question */ -static void readSectorsUsed(const Device& d, Partition& p, const QString& mountPoint) +static void readSectorsUsed(const DiskDevice& d, Partition& p, const QString& mountPoint) { const KDiskFreeSpaceInfo freeSpaceInfo = KDiskFreeSpaceInfo::freeSpaceInfo(mountPoint); @@ -176,7 +176,7 @@ Device* LibPartedBackend::scanDevice(const QString& deviceNode) Log(Log::information) << xi18nc("@info:status", "Device found: %1", model); - Device* d = new Device(model, path, heads, sectors, cylinders, sectorSize); + DiskDevice* d = new DiskDevice(model, path, heads, sectors, cylinders, sectorSize); if (pedDiskError) return d; @@ -277,7 +277,7 @@ Device* LibPartedBackend::scanDevice(const QString& deviceNode) luksFs->setMounted(mounted); } else if (type == FileSystem::Lvm2_PV) { //TODO: adding PartitionRole - mountPoint = FS::lvm2_pv::getVGName(node); + mountPoint = FS::lvm2_pv::getVGName(partitionNode); mounted = false; } else { mountPoint = mountPoints.findByDevice(partitionNode) ? diff --git a/src/plugins/libparted/libpartedbackend.h b/src/plugins/libparted/libpartedbackend.h index 041ab0f..45d0b54 100644 --- a/src/plugins/libparted/libpartedbackend.h +++ b/src/plugins/libparted/libpartedbackend.h @@ -38,7 +38,6 @@ class LibPartedPartitionTable; class LibPartedPartition; class OperationStack; -class Device; class KPluginFactory; class QString;