kpmcore/src/fs/linuxswap.cpp

186 lines
6.1 KiB
C++
Raw Normal View History

2020-09-28 00:45:21 +01:00
/*
SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
SPDX-FileCopyrightText: 2012-2018 Andrius Štikonas <andrius@stikonas.eu>
SPDX-FileCopyrightText: 2016 Teo Mrnjavac <teo@kde.org>
SPDX-FileCopyrightText: 2016 Chantara Tith <tith.chantara@gmail.com>
SPDX-FileCopyrightText: 2019 Yuri Chornoivan <yurchor@ukr.net>
SPDX-FileCopyrightText: 2020 Arnaud Ferraris <arnaud.ferraris@collabora.com>
SPDX-FileCopyrightText: 2020 Gaël PORTAY <gael.portay@collabora.com>
SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "fs/linuxswap.h"
#include "util/externalcommand.h"
#include <KLocalizedString>
#include <QFileInfo>
2017-09-05 12:57:45 +01:00
#include <QRegularExpression>
#include <QTextStream>
namespace FS
{
2015-07-13 15:16:36 +01:00
FileSystem::CommandSupportType linuxswap::m_Create = FileSystem::cmdSupportNone;
FileSystem::CommandSupportType linuxswap::m_Grow = FileSystem::cmdSupportNone;
FileSystem::CommandSupportType linuxswap::m_Shrink = FileSystem::cmdSupportNone;
FileSystem::CommandSupportType linuxswap::m_Move = FileSystem::cmdSupportNone;
FileSystem::CommandSupportType linuxswap::m_Copy = FileSystem::cmdSupportNone;
FileSystem::CommandSupportType linuxswap::m_GetUsed = FileSystem::cmdSupportNone;
2015-07-13 15:16:36 +01:00
FileSystem::CommandSupportType linuxswap::m_GetLabel = FileSystem::cmdSupportNone;
FileSystem::CommandSupportType linuxswap::m_SetLabel = FileSystem::cmdSupportNone;
FileSystem::CommandSupportType linuxswap::m_GetUUID = FileSystem::cmdSupportNone;
FileSystem::CommandSupportType linuxswap::m_UpdateUUID = FileSystem::cmdSupportNone;
linuxswap::linuxswap(qint64 firstsector, qint64 lastsector, qint64 sectorsused, const QString& label, const QVariantMap& features) :
FileSystem(firstsector, lastsector, sectorsused, label, features, FileSystem::Type::LinuxSwap)
2015-07-13 15:16:36 +01:00
{
}
void linuxswap::init()
{
m_SetLabel = m_Shrink = m_Grow = m_Create = m_UpdateUUID = (findExternal(QStringLiteral("mkswap"))) ? cmdSupportFileSystem : cmdSupportNone;
m_GetLabel = cmdSupportCore;
m_GetUsed = cmdSupportFileSystem;
2015-07-13 15:16:36 +01:00
m_Copy = cmdSupportFileSystem;
m_Move = cmdSupportCore;
m_GetUUID = cmdSupportCore;
}
bool linuxswap::supportToolFound() const
{
return
m_GetUsed != cmdSupportNone &&
2015-07-13 15:16:36 +01:00
m_GetLabel != cmdSupportNone &&
m_SetLabel != cmdSupportNone &&
m_Create != cmdSupportNone &&
// m_Check != cmdSupportNone &&
2015-07-13 15:16:36 +01:00
m_UpdateUUID != cmdSupportNone &&
m_Grow != cmdSupportNone &&
m_Shrink != cmdSupportNone &&
m_Copy != cmdSupportNone &&
m_Move != cmdSupportNone &&
// m_Backup != cmdSupportNone &&
2015-07-13 15:16:36 +01:00
m_GetUUID != cmdSupportNone;
}
FileSystem::SupportTool linuxswap::supportToolName() const
{
2019-11-22 13:34:10 +00:00
return SupportTool(QStringLiteral("util-linux"), QUrl(QStringLiteral("https://github.com/karelzak/util-linux")));
2015-07-13 15:16:36 +01:00
}
2017-09-11 16:52:20 +01:00
int linuxswap::maxLabelLength() const
2015-07-13 15:16:36 +01:00
{
return 15;
}
2016-09-05 12:10:56 +01:00
bool linuxswap::create(Report& report, const QString& deviceNode)
2015-07-13 15:16:36 +01:00
{
ExternalCommand cmd(report, QStringLiteral("mkswap"), { deviceNode });
2015-07-13 15:16:36 +01:00
return cmd.run(-1) && cmd.exitCode() == 0;
}
bool linuxswap::resize(Report& report, const QString& deviceNode, qint64 length) const
{
2018-07-12 00:12:43 +01:00
Q_UNUSED(length)
2015-07-13 15:16:36 +01:00
const QString label = readLabel(deviceNode);
const QString uuid = readUUID(deviceNode);
QStringList args;
if (!label.isEmpty())
args << QStringLiteral("--label") << label;
2015-07-13 15:16:36 +01:00
if (!uuid.isEmpty())
args << QStringLiteral("--uuid") << uuid;
2015-07-13 15:16:36 +01:00
args << deviceNode;
2015-07-13 15:16:36 +01:00
ExternalCommand cmd(report, QStringLiteral("mkswap"), args);
return cmd.run(-1) && cmd.exitCode() == 0;
}
bool linuxswap::copy(Report& report, const QString& targetDeviceNode, const QString& sourceDeviceNode) const
{
const QString label = readLabel(sourceDeviceNode);
const QString uuid = readUUID(sourceDeviceNode);
QStringList args;
if (!label.isEmpty())
args << QStringLiteral("--label") << label;
2015-07-13 15:16:36 +01:00
if (!uuid.isEmpty())
args << QStringLiteral("--uuid") << uuid;
2015-07-13 15:16:36 +01:00
args << targetDeviceNode;
ExternalCommand cmd(report, QStringLiteral("mkswap"), args);
return cmd.run(-1) && cmd.exitCode() == 0;
}
bool linuxswap::writeLabel(Report& report, const QString& deviceNode, const QString& newLabel)
{
2016-08-26 18:31:14 +01:00
ExternalCommand cmd(report, QStringLiteral("swaplabel"), { QStringLiteral("--label"), newLabel, deviceNode });
2015-07-13 15:16:36 +01:00
return cmd.run(-1) && cmd.exitCode() == 0;
}
QString linuxswap::mountTitle() const
{
return xi18nc("@title:menu", "Activate swap");
2015-07-13 15:16:36 +01:00
}
QString linuxswap::unmountTitle() const
{
return xi18nc("@title:menu", "Deactivate swap");
2015-07-13 15:16:36 +01:00
}
bool linuxswap::canMount(const QString& deviceNode, const QString& mountPoint) const {
2018-07-12 00:12:43 +01:00
Q_UNUSED(deviceNode)
// linux swap doesn't require mount point to activate
return mountPoint != QStringLiteral("/");
}
bool linuxswap::mount(Report& report, const QString& deviceNode, const QString& mountPoint)
2015-07-13 15:16:36 +01:00
{
2018-07-12 00:12:43 +01:00
Q_UNUSED(mountPoint)
ExternalCommand cmd(report, QStringLiteral("swapon"), { deviceNode });
2015-07-13 15:16:36 +01:00
return cmd.run(-1) && cmd.exitCode() == 0;
}
bool linuxswap::unmount(Report& report, const QString& deviceNode)
2015-07-13 15:16:36 +01:00
{
ExternalCommand cmd(report, QStringLiteral("swapoff"), { deviceNode });
2015-07-13 15:16:36 +01:00
return cmd.run(-1) && cmd.exitCode() == 0;
}
bool linuxswap::updateUUID(Report& report, const QString& deviceNode) const
{
const QString label = readLabel(deviceNode);
QStringList args;
if (!label.isEmpty())
args << QStringLiteral("--label") << label;
2015-07-13 15:16:36 +01:00
args << deviceNode;
ExternalCommand cmd(report, QStringLiteral("mkswap"), args);
return cmd.run(-1) && cmd.exitCode() == 0;
}
qint64 linuxswap::readUsedCapacity(const QString& deviceNode) const
{
QFile swapsFile(QStringLiteral("/proc/swaps"));
if (swapsFile.open(QIODevice::ReadOnly)) {
QByteArray data = swapsFile.readAll();
swapsFile.close();
QTextStream in(&data);
while (!in.atEnd()) {
2017-09-05 12:57:45 +01:00
QStringList line = in.readLine().split(QRegularExpression(QStringLiteral("\\s+")));
QFileInfo kernelPath(deviceNode);
if (line[0] == kernelPath.canonicalFilePath())
return line[3].toLongLong() * 1024;
}
}
return -1;
}
}