2015-06-04 01:29:22 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* Copyright (C) 2008,2009 by Volker Lanz <vl@fidra.de> *
|
2016-03-02 19:00:31 +00:00
|
|
|
* Copyright (C) 2016 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/>.*
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "fs/linuxswap.h"
|
|
|
|
|
|
|
|
#include "util/externalcommand.h"
|
|
|
|
|
|
|
|
#include <KLocalizedString>
|
2016-05-24 17:26:12 +01:00
|
|
|
#include <QDebug>
|
2015-06-04 01:29:22 +01:00
|
|
|
|
|
|
|
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_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) :
|
|
|
|
FileSystem(firstsector, lastsector, sectorsused, label, FileSystem::LinuxSwap)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void linuxswap::init()
|
|
|
|
{
|
|
|
|
m_SetLabel = m_Shrink = m_Grow = m_Create = m_UpdateUUID = (findExternal(QStringLiteral("mkswap"))) ? cmdSupportFileSystem : cmdSupportNone;
|
|
|
|
m_GetLabel = cmdSupportCore;
|
|
|
|
m_Copy = cmdSupportFileSystem;
|
|
|
|
m_Move = cmdSupportCore;
|
|
|
|
m_GetUUID = cmdSupportCore;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool linuxswap::supportToolFound() const
|
|
|
|
{
|
|
|
|
return
|
|
|
|
// m_GetUsed != cmdSupportNone &&
|
|
|
|
m_GetLabel != cmdSupportNone &&
|
|
|
|
m_SetLabel != cmdSupportNone &&
|
|
|
|
m_Create != cmdSupportNone &&
|
|
|
|
// m_Check != cmdSupportNone &&
|
|
|
|
m_UpdateUUID != cmdSupportNone &&
|
|
|
|
m_Grow != cmdSupportNone &&
|
|
|
|
m_Shrink != cmdSupportNone &&
|
|
|
|
m_Copy != cmdSupportNone &&
|
|
|
|
m_Move != cmdSupportNone &&
|
|
|
|
// m_Backup != cmdSupportNone &&
|
|
|
|
m_GetUUID != cmdSupportNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileSystem::SupportTool linuxswap::supportToolName() const
|
|
|
|
{
|
|
|
|
return SupportTool(QStringLiteral("util-linux"), QUrl(QStringLiteral("http://www.kernel.org/pub/linux/utils/util-linux-ng/")));
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 linuxswap::maxLabelLength() const
|
|
|
|
{
|
|
|
|
return 15;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool linuxswap::create(Report& report, const QString& deviceNode) const
|
|
|
|
{
|
2016-04-21 22:48:05 +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
|
|
|
|
{
|
2016-04-21 17:22:48 +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())
|
2016-04-20 16:30:23 +01:00
|
|
|
args << QStringLiteral("--label") << label;
|
2015-07-13 15:16:36 +01:00
|
|
|
if (!uuid.isEmpty())
|
2016-04-20 16:30:23 +01:00
|
|
|
args << QStringLiteral("--uuid") << uuid;
|
2015-07-13 15:16:36 +01:00
|
|
|
|
2016-04-21 17:22:48 +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())
|
2016-04-20 16:30:23 +01:00
|
|
|
args << QStringLiteral("--label") << label;
|
2015-07-13 15:16:36 +01:00
|
|
|
if (!uuid.isEmpty())
|
2016-04-20 16:30:23 +01:00
|
|
|
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-04-21 22:48:05 +01:00
|
|
|
ExternalCommand cmd(report, QStringLiteral("mkswap"), { QStringLiteral("--label"), newLabel, deviceNode });
|
2015-07-13 15:16:36 +01:00
|
|
|
return cmd.run(-1) && cmd.exitCode() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString linuxswap::mountTitle() const
|
|
|
|
{
|
2016-07-17 23:41:00 +01:00
|
|
|
return xi18nc("@title:menu", "Activate swap");
|
2015-07-13 15:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString linuxswap::unmountTitle() const
|
|
|
|
{
|
2016-07-17 23:41:00 +01:00
|
|
|
return xi18nc("@title:menu", "Deactivate swap");
|
2015-07-13 15:16:36 +01:00
|
|
|
}
|
|
|
|
|
2016-05-13 17:58:05 +01:00
|
|
|
bool linuxswap::canMount(const QString& deviceNode, const QString& mountPoint) const {
|
|
|
|
Q_UNUSED(deviceNode);
|
|
|
|
// linux swap doesn't require mount point to activate
|
2016-05-24 17:26:12 +01:00
|
|
|
qDebug() << mountPoint;
|
|
|
|
return mountPoint != QStringLiteral("/");
|
2016-05-13 17:58:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool linuxswap::mount(Report& report, const QString& deviceNode, const QString& mountPoint)
|
2015-07-13 15:16:36 +01:00
|
|
|
{
|
2016-03-31 17:43:38 +01:00
|
|
|
Q_UNUSED(mountPoint);
|
2016-05-13 17:58:05 +01:00
|
|
|
ExternalCommand cmd(report, QStringLiteral("swapon"), { deviceNode });
|
2015-07-13 15:16:36 +01:00
|
|
|
return cmd.run(-1) && cmd.exitCode() == 0;
|
|
|
|
}
|
|
|
|
|
2016-05-13 17:27:23 +01:00
|
|
|
bool linuxswap::unmount(Report& report, const QString& deviceNode)
|
2015-07-13 15:16:36 +01:00
|
|
|
{
|
2016-05-13 17:27:23 +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())
|
2016-04-20 16:30:23 +01:00
|
|
|
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;
|
|
|
|
}
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|