2015-06-04 01:29:22 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* Copyright (C) 2008-2012 by Volker Lanz <vl@fidra.de> *
|
2016-03-31 17:43:38 +01:00
|
|
|
* Copyright (C) 2015-2016 by Teo Mrnjavac <teo@kde.org> *
|
2017-01-05 17:27:17 +00:00
|
|
|
* Copyright (C) 2016-2017 by Andrius Štikonas <andrius@stikonas.eu> *
|
2015-06-04 01:29:22 +01:00
|
|
|
* *
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.*
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
/** @file
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "plugins/libparted/libpartedbackend.h"
|
|
|
|
#include "plugins/libparted/libparteddevice.h"
|
2016-07-21 13:16:29 +01:00
|
|
|
#include "plugins/libparted/pedflags.h"
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2016-05-31 19:28:31 +01:00
|
|
|
#include "core/diskdevice.h"
|
2015-06-04 01:29:22 +01:00
|
|
|
#include "core/partition.h"
|
|
|
|
#include "core/partitiontable.h"
|
|
|
|
#include "core/partitionalignment.h"
|
|
|
|
|
|
|
|
#include "fs/filesystem.h"
|
|
|
|
#include "fs/filesystemfactory.h"
|
|
|
|
|
|
|
|
#include "fs/fat16.h"
|
|
|
|
#include "fs/hfs.h"
|
|
|
|
#include "fs/hfsplus.h"
|
|
|
|
#include "fs/luks.h"
|
2016-05-23 11:29:33 +01:00
|
|
|
#include "fs/lvm2_pv.h"
|
2015-06-04 01:29:22 +01:00
|
|
|
|
|
|
|
#include "util/globallog.h"
|
2016-07-22 12:56:59 +01:00
|
|
|
#include "util/externalcommand.h"
|
2015-06-04 01:29:22 +01:00
|
|
|
#include "util/helpers.h"
|
|
|
|
|
2016-07-29 18:15:28 +01:00
|
|
|
#include <blkid/blkid.h>
|
|
|
|
|
2016-05-09 11:07:31 +01:00
|
|
|
#include <QDebug>
|
2015-06-04 01:29:22 +01:00
|
|
|
#include <QString>
|
|
|
|
#include <QStringList>
|
|
|
|
|
|
|
|
#include <KLocalizedString>
|
2016-05-22 00:48:55 +01:00
|
|
|
#include <KDiskFreeSpaceInfo>
|
2015-06-04 01:29:22 +01:00
|
|
|
#include <KPluginFactory>
|
|
|
|
|
2016-10-29 19:45:33 +01:00
|
|
|
#include <parted/parted.h>
|
2015-06-04 01:29:22 +01:00
|
|
|
|
|
|
|
K_PLUGIN_FACTORY_WITH_JSON(LibPartedBackendFactory, "pmlibpartedbackendplugin.json", registerPlugin<LibPartedBackend>();)
|
|
|
|
|
|
|
|
static QString s_lastPartedExceptionMessage;
|
|
|
|
|
|
|
|
/** Callback to handle exceptions from libparted
|
2015-07-13 15:16:36 +01:00
|
|
|
@param e the libparted exception to handle
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
static PedExceptionOption pedExceptionHandler(PedException* e)
|
|
|
|
{
|
2016-07-17 23:41:00 +01:00
|
|
|
Log(Log::error) << xi18nc("@info:status", "LibParted Exception: %1", QString::fromLocal8Bit(e->message));
|
2015-07-13 15:16:36 +01:00
|
|
|
s_lastPartedExceptionMessage = QString::fromLocal8Bit(e->message);
|
|
|
|
return PED_EXCEPTION_UNHANDLED;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
2016-10-29 19:45:33 +01:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// The following structs and the typedef come from libparted's internal gpt sources.
|
|
|
|
// It's very unfortunate there is no public API to get at the first and last usable
|
|
|
|
// sector for GPT a partition table, so this is the only (libparted) way to get that
|
|
|
|
// information (another way would be to read the GPT header and parse the
|
|
|
|
// information ourselves; if the libparted devs begin changing these internal
|
|
|
|
// structs for each point release and break our code, we'll have to do just that).
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint32_t time_low;
|
|
|
|
uint16_t time_mid;
|
|
|
|
uint16_t time_hi_and_version;
|
|
|
|
uint8_t clock_seq_hi_and_reserved;
|
|
|
|
uint8_t clock_seq_low;
|
|
|
|
uint8_t node[6];
|
|
|
|
} /* __attribute__ ((packed)) */ efi_guid_t;
|
|
|
|
|
|
|
|
|
|
|
|
struct __attribute__((packed)) _GPTDiskData {
|
|
|
|
PedGeometry data_area;
|
|
|
|
int entry_count;
|
|
|
|
efi_guid_t uuid;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct _GPTDiskData GPTDiskData;
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/** Get the first sector a Partition may cover on a given Device
|
|
|
|
@param d the Device in question
|
|
|
|
@return the first sector usable by a Partition
|
|
|
|
*/
|
|
|
|
static quint64 firstUsableSector(const Device& d)
|
|
|
|
{
|
|
|
|
PedDevice* pedDevice = ped_device_get(d.deviceNode().toLatin1().constData());
|
|
|
|
PedDisk* pedDisk = pedDevice ? ped_disk_new(pedDevice) : nullptr;
|
|
|
|
|
|
|
|
quint64 rval = 0;
|
|
|
|
if (pedDisk)
|
|
|
|
rval = pedDisk->dev->bios_geom.sectors;
|
|
|
|
|
|
|
|
if (pedDisk && strcmp(pedDisk->type->name, "gpt") == 0) {
|
|
|
|
GPTDiskData* gpt_disk_data = reinterpret_cast<GPTDiskData*>(pedDisk->disk_specific);
|
|
|
|
PedGeometry* geom = reinterpret_cast<PedGeometry*>(&gpt_disk_data->data_area);
|
|
|
|
|
|
|
|
if (geom)
|
|
|
|
rval = geom->start;
|
|
|
|
else
|
|
|
|
rval += 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
ped_disk_destroy(pedDisk);
|
|
|
|
|
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get the last sector a Partition may cover on a given Device
|
|
|
|
@param d the Device in question
|
|
|
|
@return the last sector usable by a Partition
|
|
|
|
*/
|
|
|
|
static quint64 lastUsableSector(const Device& d)
|
|
|
|
{
|
|
|
|
PedDevice* pedDevice = ped_device_get(d.deviceNode().toLatin1().constData());
|
|
|
|
PedDisk* pedDisk = pedDevice ? ped_disk_new(pedDevice) : nullptr;
|
|
|
|
|
|
|
|
quint64 rval = 0;
|
|
|
|
if (pedDisk)
|
|
|
|
rval = static_cast< quint64 >( pedDisk->dev->bios_geom.sectors ) *
|
|
|
|
pedDisk->dev->bios_geom.heads *
|
|
|
|
pedDisk->dev->bios_geom.cylinders - 1;
|
|
|
|
|
|
|
|
if (pedDisk && strcmp(pedDisk->type->name, "gpt") == 0) {
|
|
|
|
GPTDiskData* gpt_disk_data = reinterpret_cast<GPTDiskData*>(pedDisk->disk_specific);
|
|
|
|
PedGeometry* geom = reinterpret_cast<PedGeometry*>(&gpt_disk_data->data_area);
|
|
|
|
|
|
|
|
if (geom)
|
|
|
|
rval = geom->end;
|
|
|
|
else
|
|
|
|
rval -= 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
ped_disk_destroy(pedDisk);
|
|
|
|
|
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
2015-06-04 01:29:22 +01:00
|
|
|
/** Reads sectors used on a FileSystem using libparted functions.
|
2015-07-13 15:16:36 +01:00
|
|
|
@param pedDisk pointer to pedDisk where the Partition and its FileSystem are
|
|
|
|
@param p the Partition the FileSystem is on
|
|
|
|
@return the number of sectors used
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
#if defined LIBPARTED_FS_RESIZE_LIBRARY_SUPPORT
|
2016-10-29 19:45:33 +01:00
|
|
|
static qint64 readSectorsUsedLibParted(PedDisk* pedDisk, const Partition& p)
|
2015-06-04 01:29:22 +01:00
|
|
|
{
|
2016-10-29 19:45:33 +01:00
|
|
|
Q_ASSERT(pedDisk);
|
|
|
|
|
|
|
|
qint64 rval = -1;
|
|
|
|
|
|
|
|
PedPartition* pedPartition = ped_disk_get_partition_by_sector(pedDisk, p.firstSector());
|
|
|
|
|
|
|
|
if (pedPartition) {
|
|
|
|
PedFileSystem* pedFileSystem = ped_file_system_open(&pedPartition->geom);
|
|
|
|
|
|
|
|
if (pedFileSystem) {
|
|
|
|
if (PedConstraint* pedConstraint = ped_file_system_get_resize_constraint(pedFileSystem)) {
|
|
|
|
rval = pedConstraint->min_size;
|
|
|
|
ped_constraint_destroy(pedConstraint);
|
|
|
|
}
|
|
|
|
|
|
|
|
ped_file_system_close(pedFileSystem);
|
|
|
|
}
|
2015-07-13 15:16:36 +01:00
|
|
|
}
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2016-10-29 19:45:33 +01:00
|
|
|
return rval;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** Reads the sectors used in a FileSystem and stores the result in the Partition's FileSystem object.
|
2016-10-29 19:45:33 +01:00
|
|
|
@param pedDisk pointer to pedDisk where the Partition and its FileSystem are
|
2015-07-13 15:16:36 +01:00
|
|
|
@param p the Partition the FileSystem is on
|
|
|
|
@param mountPoint mount point of the partition in question
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
2016-10-29 19:45:33 +01:00
|
|
|
static void readSectorsUsed(PedDisk* pedDisk, const Device& d, Partition& p, const QString& mountPoint)
|
2015-06-04 01:29:22 +01:00
|
|
|
{
|
2017-01-05 17:27:17 +00:00
|
|
|
if (!mountPoint.isEmpty()) {
|
2016-11-05 20:29:31 +00:00
|
|
|
const KDiskFreeSpaceInfo freeSpaceInfo = KDiskFreeSpaceInfo::freeSpaceInfo(mountPoint);
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2016-11-15 17:00:59 +00:00
|
|
|
// KDiskFreeSpaceInfo does not work with swap
|
2017-01-05 17:27:17 +00:00
|
|
|
if (p.isMounted() && freeSpaceInfo.isValid() && p.fileSystem().type() != FileSystem::LinuxSwap)
|
2016-11-05 20:29:31 +00:00
|
|
|
p.fileSystem().setSectorsUsed(freeSpaceInfo.used() / d.logicalSize());
|
|
|
|
}
|
2017-01-05 17:27:17 +00:00
|
|
|
else if (p.fileSystem().supportGetUsed() == FileSystem::cmdSupportFileSystem)
|
|
|
|
p.fileSystem().setSectorsUsed(p.fileSystem().readUsedCapacity(p.deviceNode()) / d.logicalSize());
|
2015-06-04 01:29:22 +01:00
|
|
|
#if defined LIBPARTED_FS_RESIZE_LIBRARY_SUPPORT
|
2016-10-29 19:45:33 +01:00
|
|
|
else if (p.fileSystem().supportGetUsed() == FileSystem::cmdSupportCore)
|
|
|
|
p.fileSystem().setSectorsUsed(readSectorsUsedLibParted(pedDisk, p));
|
|
|
|
#else
|
|
|
|
Q_UNUSED(pedDisk);
|
2015-06-04 01:29:22 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-10-29 19:45:33 +01:00
|
|
|
static PartitionTable::Flags activeFlags(PedPartition* p)
|
|
|
|
{
|
|
|
|
PartitionTable::Flags flags = PartitionTable::FlagNone;
|
|
|
|
|
|
|
|
// We might get here with a pedPartition just picked up from libparted that is
|
|
|
|
// unallocated. Libparted doesn't like it if we ask for flags for unallocated
|
|
|
|
// space.
|
|
|
|
if (p->num <= 0)
|
|
|
|
return flags;
|
|
|
|
|
|
|
|
for (const auto &flag : flagmap)
|
|
|
|
if (ped_partition_is_flag_available(p, flag.pedFlag) && ped_partition_get_flag(p, flag.pedFlag))
|
|
|
|
flags |= flag.flag;
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PartitionTable::Flags availableFlags(PedPartition* p)
|
|
|
|
{
|
|
|
|
PartitionTable::Flags flags;
|
|
|
|
|
|
|
|
// see above.
|
|
|
|
if (p->num <= 0)
|
|
|
|
return flags;
|
|
|
|
|
|
|
|
for (const auto &flag : flagmap) {
|
|
|
|
if (ped_partition_is_flag_available(p, flag.pedFlag)) {
|
|
|
|
// Workaround: libparted claims the hidden flag is available for extended partitions, but
|
|
|
|
// throws an error when we try to set or clear it. So skip this combination. Also see setFlag.
|
|
|
|
if (p->type != PED_PARTITION_EXTENDED || flag.flag != PartitionTable::FlagHidden)
|
|
|
|
flags |= flag.flag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2015-06-04 01:29:22 +01:00
|
|
|
/** Constructs a LibParted object. */
|
|
|
|
LibPartedBackend::LibPartedBackend(QObject*, const QList<QVariant>&) :
|
2015-07-13 15:16:36 +01:00
|
|
|
CoreBackend()
|
2015-06-04 01:29:22 +01:00
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
ped_exception_set_handler(pedExceptionHandler);
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LibPartedBackend::initFSSupport()
|
|
|
|
{
|
|
|
|
#if defined LIBPARTED_FS_RESIZE_LIBRARY_SUPPORT
|
2015-07-13 15:16:36 +01:00
|
|
|
if (FS::fat16::m_Shrink == FileSystem::cmdSupportNone)
|
|
|
|
FS::fat16::m_Shrink = FileSystem::cmdSupportBackend;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
if (FS::fat16::m_Grow == FileSystem::cmdSupportNone)
|
|
|
|
FS::fat16::m_Grow = FileSystem::cmdSupportBackend;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
if (FS::hfs::m_Shrink == FileSystem::cmdSupportNone)
|
|
|
|
FS::hfs::m_Shrink = FileSystem::cmdSupportBackend;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
if (FS::hfsplus::m_Shrink == FileSystem::cmdSupportNone)
|
|
|
|
FS::hfsplus::m_Shrink = FileSystem::cmdSupportBackend;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
if (FS::hfs::m_GetUsed == FileSystem::cmdSupportNone)
|
|
|
|
FS::hfs::m_GetUsed = FileSystem::cmdSupportBackend;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
if (FS::hfsplus::m_GetUsed == FileSystem::cmdSupportNone)
|
|
|
|
FS::hfsplus::m_GetUsed = FileSystem::cmdSupportBackend;
|
2015-06-04 01:29:22 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-10-29 19:45:33 +01:00
|
|
|
/** Scans a Device for Partitions.
|
2016-07-21 13:16:29 +01:00
|
|
|
|
2016-10-29 19:45:33 +01:00
|
|
|
This method will scan a Device for all Partitions on it, detect the FileSystem for each Partition,
|
|
|
|
try to determine the FileSystem usage, read the FileSystem label and store it all in newly created
|
|
|
|
objects that are in the end added to the Device's PartitionTable.
|
2016-07-21 13:16:29 +01:00
|
|
|
|
2016-10-29 19:45:33 +01:00
|
|
|
@param d Device
|
|
|
|
@param pedDisk libparted pointer to the partition table
|
|
|
|
*/
|
|
|
|
void LibPartedBackend::scanDevicePartitions(Device& d, PedDisk* pedDisk)
|
|
|
|
{
|
|
|
|
Q_ASSERT(pedDisk);
|
|
|
|
Q_ASSERT(d.partitionTable());
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2016-10-29 19:45:33 +01:00
|
|
|
PedPartition* pedPartition = nullptr;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2016-07-21 13:16:29 +01:00
|
|
|
QList<Partition*> partitions;
|
2016-10-29 19:45:33 +01:00
|
|
|
|
|
|
|
while ((pedPartition = ped_disk_next_partition(pedDisk, pedPartition))) {
|
|
|
|
if (pedPartition->num < 1)
|
|
|
|
continue;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
PartitionRole::Roles r = PartitionRole::None;
|
2016-05-06 19:14:01 +01:00
|
|
|
|
2016-10-29 19:45:33 +01:00
|
|
|
FileSystem::Type type = FileSystem::Unknown;
|
|
|
|
char* pedPath = ped_partition_get_path(pedPartition);
|
|
|
|
const QString partitionNode = pedPath ? QString::fromUtf8(pedPath) : QString();
|
|
|
|
free(pedPath);
|
|
|
|
type = detectFileSystem(partitionNode);
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2016-10-29 19:45:33 +01:00
|
|
|
switch (pedPartition->type) {
|
2015-07-13 15:16:36 +01:00
|
|
|
case PED_PARTITION_NORMAL:
|
|
|
|
r = PartitionRole::Primary;
|
|
|
|
break;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
case PED_PARTITION_EXTENDED:
|
|
|
|
r = PartitionRole::Extended;
|
|
|
|
type = FileSystem::Extended;
|
|
|
|
break;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
case PED_PARTITION_LOGICAL:
|
|
|
|
r = PartitionRole::Logical;
|
|
|
|
break;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
// Find an extended partition this partition is in.
|
2016-10-29 19:45:33 +01:00
|
|
|
PartitionNode* parent = d.partitionTable()->findPartitionBySector(pedPartition->geom.start, PartitionRole(PartitionRole::Extended));
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
// None found, so it's a primary in the device's partition table.
|
2015-07-22 14:48:03 +01:00
|
|
|
if (parent == nullptr)
|
2016-10-29 19:45:33 +01:00
|
|
|
parent = d.partitionTable();
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2016-10-29 19:45:33 +01:00
|
|
|
FileSystem* fs = FileSystemFactory::create(type, pedPartition->geom.start, pedPartition->geom.end);
|
2016-09-04 23:32:00 +01:00
|
|
|
fs->scan(partitionNode);
|
2016-09-12 11:23:19 +01:00
|
|
|
QString mountPoint;
|
|
|
|
bool mounted;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
// libparted does not handle LUKS partitions
|
2016-09-12 11:23:19 +01:00
|
|
|
if (fs->type() == FileSystem::Luks) {
|
2016-04-19 15:46:18 +01:00
|
|
|
r |= PartitionRole::Luks;
|
2016-11-30 17:18:51 +00:00
|
|
|
FS::luks* luksFs = static_cast<FS::luks*>(fs);
|
|
|
|
luksFs->initLUKS();
|
|
|
|
QString mapperNode = luksFs->mapperName();
|
2016-09-12 11:23:19 +01:00
|
|
|
mountPoint = FileSystem::detectMountPoint(fs, mapperNode);
|
|
|
|
mounted = FileSystem::detectMountStatus(fs, mapperNode);
|
2015-07-13 15:16:36 +01:00
|
|
|
} else {
|
2016-09-12 11:23:19 +01:00
|
|
|
mountPoint = FileSystem::detectMountPoint(fs, partitionNode);
|
|
|
|
mounted = FileSystem::detectMountStatus(fs, partitionNode);
|
2015-07-13 15:16:36 +01:00
|
|
|
}
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2016-10-29 19:45:33 +01:00
|
|
|
Partition* part = new Partition(parent, d, PartitionRole(r), fs, pedPartition->geom.start, pedPartition->geom.end, partitionNode, availableFlags(pedPartition), mountPoint, mounted, activeFlags(pedPartition));
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2016-05-22 00:48:55 +01:00
|
|
|
if (!part->roles().has(PartitionRole::Luks))
|
2016-10-29 19:45:33 +01:00
|
|
|
readSectorsUsed(pedDisk, d, *part, mountPoint);
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
if (fs->supportGetLabel() != FileSystem::cmdSupportNone)
|
|
|
|
fs->setLabel(fs->readLabel(part->deviceNode()));
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2016-09-24 22:15:15 +01:00
|
|
|
// GPT partitions support partition labels and partition UUIDs
|
2016-10-29 19:45:33 +01:00
|
|
|
if(d.partitionTable()->type() == PartitionTable::TableType::gpt)
|
|
|
|
fs->setLabel(fs->readLabel(part->deviceNode()));
|
2016-09-24 22:15:15 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
if (fs->supportGetUUID() != FileSystem::cmdSupportNone)
|
|
|
|
fs->setUUID(fs->readUUID(part->deviceNode()));
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
parent->append(part);
|
|
|
|
partitions.append(part);
|
|
|
|
}
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2016-10-29 19:45:33 +01:00
|
|
|
d.partitionTable()->updateUnallocated(d);
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2016-10-29 19:45:33 +01:00
|
|
|
if (d.partitionTable()->isSectorBased(d))
|
|
|
|
d.partitionTable()->setType(d, PartitionTable::msdos_sectorbased);
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
foreach(const Partition * part, partitions)
|
2016-10-29 19:45:33 +01:00
|
|
|
PartitionAlignment::isAligned(d, *part);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Create a Device for the given device_node and scan it for partitions.
|
|
|
|
@param deviceNode the device node (e.g. "/dev/sda")
|
|
|
|
@return the created Device object. callers need to free this.
|
|
|
|
*/
|
|
|
|
Device* LibPartedBackend::scanDevice(const QString& deviceNode)
|
|
|
|
{
|
|
|
|
PedDevice* pedDevice = ped_device_get(deviceNode.toLocal8Bit().constData());
|
|
|
|
|
|
|
|
if (pedDevice == nullptr) {
|
|
|
|
Log(Log::warning) << xi18nc("@info:status", "Could not access device <filename>%1</filename>", deviceNode);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log(Log::information) << xi18nc("@info:status", "Device found: %1", QString::fromUtf8(pedDevice->model));
|
|
|
|
|
|
|
|
DiskDevice* d = new DiskDevice(QString::fromUtf8(pedDevice->model), QString::fromUtf8(pedDevice->path), pedDevice->bios_geom.heads, pedDevice->bios_geom.sectors, pedDevice->bios_geom.cylinders, pedDevice->sector_size);
|
|
|
|
|
|
|
|
PedDisk* pedDisk = ped_disk_new(pedDevice);
|
|
|
|
|
|
|
|
if (pedDisk) {
|
|
|
|
const PartitionTable::TableType type = PartitionTable::nameToTableType(QString::fromUtf8(pedDisk->type->name));
|
|
|
|
CoreBackend::setPartitionTableForDevice(*d, new PartitionTable(type, firstUsableSector(*d), lastUsableSector(*d)));
|
|
|
|
CoreBackend::setPartitionTableMaxPrimaries(*d->partitionTable(), ped_disk_get_max_primary_partition_count(pedDisk));
|
|
|
|
|
|
|
|
scanDevicePartitions(*d, pedDisk);
|
|
|
|
}
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2016-10-29 19:45:33 +01:00
|
|
|
ped_device_destroy(pedDevice);
|
2015-07-13 15:16:36 +01:00
|
|
|
return d;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
2016-07-23 22:01:38 +01:00
|
|
|
QList<Device*> LibPartedBackend::scanDevices(bool excludeReadOnly)
|
2015-06-04 01:29:22 +01:00
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
QList<Device*> result;
|
2016-07-23 22:01:38 +01:00
|
|
|
// linux.git/tree/Documentation/devices.txt
|
|
|
|
QString blockDeviceMajorNumbers = QStringLiteral(
|
|
|
|
"3,22,33,34,56,57,88,89,90,91,128,129,130,131,132,133,134,135," // MFM, RLL and IDE hard disk/CD-ROM interface
|
2016-11-25 15:31:09 +00:00
|
|
|
// "7," // loop devices TODO: add another bool option for loopDevices
|
2016-07-23 22:01:38 +01:00
|
|
|
"8,65,66,67,68,69,70,71," // SCSI disk devices
|
|
|
|
"80,81,82,83,84,85,86,87," // I2O hard disk
|
|
|
|
"179," // MMC block devices
|
|
|
|
"259" // Block Extended Major (include NVMe)
|
|
|
|
);
|
2016-07-22 12:56:59 +01:00
|
|
|
ExternalCommand cmd(QStringLiteral("lsblk"), {
|
|
|
|
QStringLiteral("--nodeps"),
|
|
|
|
QStringLiteral("--noheadings"),
|
|
|
|
QStringLiteral("--output"), QString::fromLatin1("name"),
|
|
|
|
QStringLiteral("--paths"),
|
2016-07-23 22:01:38 +01:00
|
|
|
QStringLiteral("--include"), blockDeviceMajorNumbers});
|
2016-07-22 12:56:59 +01:00
|
|
|
if (cmd.run(-1) && cmd.exitCode() == 0) {
|
|
|
|
QStringList devices = cmd.output().split(QString::fromLatin1("\n"));
|
|
|
|
devices.removeLast();
|
|
|
|
quint32 totalDevices = devices.length();
|
|
|
|
for (quint32 i = 0; i < totalDevices; ++i) {
|
2016-07-23 22:01:38 +01:00
|
|
|
if (excludeReadOnly) {
|
|
|
|
QFile f(QStringLiteral("/sys/block/%1/ro").arg(QString(devices[i]).remove(QStringLiteral("/dev/"))));
|
|
|
|
if (f.open(QIODevice::ReadOnly))
|
|
|
|
if (f.readLine().trimmed().toInt() == 1)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-07-22 12:56:59 +01:00
|
|
|
emitScanProgress(devices[i], i * 100 / totalDevices);
|
|
|
|
result.append(scanDevice(devices[i]));
|
|
|
|
}
|
2015-07-13 15:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
2016-05-06 19:14:01 +01:00
|
|
|
/** Detects the type of a FileSystem given a PedDevice and a PedPartition
|
2016-07-29 18:15:28 +01:00
|
|
|
@param partitionPath path to the partition
|
2016-05-06 19:14:01 +01:00
|
|
|
@return the detected FileSystem type (FileSystem::Unknown if not detected)
|
|
|
|
*/
|
2016-07-29 18:15:28 +01:00
|
|
|
FileSystem::Type LibPartedBackend::detectFileSystem(const QString& partitionPath)
|
2016-05-06 19:14:01 +01:00
|
|
|
{
|
|
|
|
FileSystem::Type rval = FileSystem::Unknown;
|
|
|
|
|
2016-07-29 18:15:28 +01:00
|
|
|
blkid_cache cache;
|
|
|
|
if (blkid_get_cache(&cache, nullptr) == 0) {
|
|
|
|
blkid_dev dev;
|
|
|
|
|
|
|
|
if ((dev = blkid_get_dev(cache,
|
|
|
|
partitionPath.toLocal8Bit().constData(),
|
|
|
|
BLKID_DEV_NORMAL)) != nullptr) {
|
|
|
|
char *string = blkid_get_tag_value(cache, "TYPE", partitionPath.toLocal8Bit().constData());
|
|
|
|
QString s = QString::fromUtf8(string);
|
|
|
|
free(string);
|
|
|
|
|
|
|
|
if (s == QStringLiteral("ext2")) rval = FileSystem::Ext2;
|
|
|
|
else if (s == QStringLiteral("ext3")) rval = FileSystem::Ext3;
|
|
|
|
else if (s.startsWith(QStringLiteral("ext4"))) rval = FileSystem::Ext4;
|
|
|
|
else if (s == QStringLiteral("swap")) rval = FileSystem::LinuxSwap;
|
|
|
|
else if (s == QStringLiteral("ntfs")) rval = FileSystem::Ntfs;
|
|
|
|
else if (s == QStringLiteral("reiserfs")) rval = FileSystem::ReiserFS;
|
|
|
|
else if (s == QStringLiteral("reiser4")) rval = FileSystem::Reiser4;
|
|
|
|
else if (s == QStringLiteral("xfs")) rval = FileSystem::Xfs;
|
|
|
|
else if (s == QStringLiteral("jfs")) rval = FileSystem::Jfs;
|
|
|
|
else if (s == QStringLiteral("hfs")) rval = FileSystem::Hfs;
|
|
|
|
else if (s == QStringLiteral("hfsplus")) rval = FileSystem::HfsPlus;
|
|
|
|
else if (s == QStringLiteral("ufs")) rval = FileSystem::Ufs;
|
|
|
|
else if (s == QStringLiteral("vfat")) {
|
|
|
|
// libblkid uses SEC_TYPE to distinguish between FAT16 and FAT32
|
|
|
|
string = blkid_get_tag_value(cache, "SEC_TYPE", partitionPath.toLocal8Bit().constData());
|
|
|
|
QString st = QString::fromUtf8(string);
|
|
|
|
free(string);
|
|
|
|
if (st == QStringLiteral("msdos"))
|
|
|
|
rval = FileSystem::Fat16;
|
|
|
|
else
|
|
|
|
rval = FileSystem::Fat32;
|
|
|
|
} else if (s == QStringLiteral("btrfs")) rval = FileSystem::Btrfs;
|
|
|
|
else if (s == QStringLiteral("ocfs2")) rval = FileSystem::Ocfs2;
|
|
|
|
else if (s == QStringLiteral("zfs_member")) rval = FileSystem::Zfs;
|
|
|
|
else if (s == QStringLiteral("hpfs")) rval = FileSystem::Hpfs;
|
|
|
|
else if (s == QStringLiteral("crypto_LUKS")) rval = FileSystem::Luks;
|
|
|
|
else if (s == QStringLiteral("exfat")) rval = FileSystem::Exfat;
|
|
|
|
else if (s == QStringLiteral("nilfs2")) rval = FileSystem::Nilfs2;
|
|
|
|
else if (s == QStringLiteral("LVM2_member")) rval = FileSystem::Lvm2_PV;
|
|
|
|
else if (s == QStringLiteral("f2fs")) rval = FileSystem::F2fs;
|
|
|
|
else
|
|
|
|
qWarning() << "blkid: unknown file system type " << s << " on " << partitionPath;
|
|
|
|
}
|
2016-07-29 14:17:36 +01:00
|
|
|
|
2016-07-29 18:15:28 +01:00
|
|
|
blkid_put_cache(cache);
|
2016-07-24 21:36:56 +01:00
|
|
|
}
|
2016-05-06 19:14:01 +01:00
|
|
|
|
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
2016-07-21 13:16:29 +01:00
|
|
|
CoreBackendDevice* LibPartedBackend::openDevice(const QString& deviceNode)
|
2015-06-04 01:29:22 +01:00
|
|
|
{
|
2016-07-21 13:16:29 +01:00
|
|
|
LibPartedDevice* device = new LibPartedDevice(deviceNode);
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-22 14:48:03 +01:00
|
|
|
if (device == nullptr || !device->open()) {
|
2015-07-13 15:16:36 +01:00
|
|
|
delete device;
|
2015-07-22 14:48:03 +01:00
|
|
|
device = nullptr;
|
2015-07-13 15:16:36 +01:00
|
|
|
}
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
return device;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 13:16:29 +01:00
|
|
|
CoreBackendDevice* LibPartedBackend::openDeviceExclusive(const QString& deviceNode)
|
2015-06-04 01:29:22 +01:00
|
|
|
{
|
2016-07-21 13:16:29 +01:00
|
|
|
LibPartedDevice* device = new LibPartedDevice(deviceNode);
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-22 14:48:03 +01:00
|
|
|
if (device == nullptr || !device->openExclusive()) {
|
2015-07-13 15:16:36 +01:00
|
|
|
delete device;
|
2015-07-22 14:48:03 +01:00
|
|
|
device = nullptr;
|
2015-07-13 15:16:36 +01:00
|
|
|
}
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
return device;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LibPartedBackend::closeDevice(CoreBackendDevice* core_device)
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
return core_device->close();
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PedPartitionFlag LibPartedBackend::getPedFlag(PartitionTable::Flag flag)
|
|
|
|
{
|
2016-08-08 22:18:03 +01:00
|
|
|
for (const auto &f : flagmap)
|
|
|
|
if (f.flag == flag)
|
|
|
|
return f.pedFlag;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
return static_cast<PedPartitionFlag>(-1);
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString LibPartedBackend::lastPartedExceptionMessage()
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
return s_lastPartedExceptionMessage;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "libpartedbackend.moc"
|