File indexing completed on 2024-05-05 05:48:43

0001 /*
0002     SPDX-FileCopyrightText: 2010 Volker Lanz <vl@fidra.de>
0003     SPDX-FileCopyrightText: 2012-2018 Andrius Štikonas <andrius@stikonas.eu>
0004     SPDX-FileCopyrightText: 2012,2019 Yuri Chornoivan <yurchor@ukr.net>
0005     SPDX-FileCopyrightText: 2020 Arnaud Ferraris <arnaud.ferraris@collabora.com>
0006     SPDX-FileCopyrightText: 2020 Gaël PORTAY <gael.portay@collabora.com>
0007 
0008     SPDX-License-Identifier: GPL-3.0-or-later
0009 */
0010 
0011 #include "fs/btrfs.h"
0012 
0013 #include "util/externalcommand.h"
0014 #include "util/capacity.h"
0015 #include "util/report.h"
0016 
0017 #include <QRegularExpression>
0018 #include <QString>
0019 #include <QTemporaryDir>
0020 
0021 #include <KLocalizedString>
0022 
0023 namespace FS
0024 {
0025 FileSystem::CommandSupportType btrfs::m_GetUsed = FileSystem::cmdSupportNone;
0026 FileSystem::CommandSupportType btrfs::m_GetLabel = FileSystem::cmdSupportNone;
0027 FileSystem::CommandSupportType btrfs::m_Create = FileSystem::cmdSupportNone;
0028 FileSystem::CommandSupportType btrfs::m_Grow = FileSystem::cmdSupportNone;
0029 FileSystem::CommandSupportType btrfs::m_Shrink = FileSystem::cmdSupportNone;
0030 FileSystem::CommandSupportType btrfs::m_Move = FileSystem::cmdSupportNone;
0031 FileSystem::CommandSupportType btrfs::m_Check = FileSystem::cmdSupportNone;
0032 FileSystem::CommandSupportType btrfs::m_Copy = FileSystem::cmdSupportNone;
0033 FileSystem::CommandSupportType btrfs::m_Backup = FileSystem::cmdSupportNone;
0034 FileSystem::CommandSupportType btrfs::m_SetLabel = FileSystem::cmdSupportNone;
0035 FileSystem::CommandSupportType btrfs::m_UpdateUUID = FileSystem::cmdSupportNone;
0036 FileSystem::CommandSupportType btrfs::m_GetUUID = FileSystem::cmdSupportNone;
0037 
0038 btrfs::btrfs(qint64 firstsector, qint64 lastsector, qint64 sectorsused, const QString& label, const QVariantMap& features) :
0039     FileSystem(firstsector, lastsector, sectorsused, label, features, FileSystem::Type::Btrfs)
0040 {
0041 }
0042 
0043 void btrfs::init()
0044 {
0045     m_Create = findExternal(QStringLiteral("mkfs.btrfs")) ? cmdSupportFileSystem : cmdSupportNone;
0046     m_Check = findExternal(QStringLiteral("btrfs")) ? cmdSupportFileSystem : cmdSupportNone;
0047     m_Grow = m_Check;
0048     m_GetUsed = m_Check;
0049     m_Shrink = (m_Grow != cmdSupportNone && m_GetUsed != cmdSupportNone) ? cmdSupportFileSystem : cmdSupportNone;
0050 
0051     m_SetLabel = m_Check;
0052     m_UpdateUUID = findExternal(QStringLiteral("btrfstune")) ? cmdSupportFileSystem : cmdSupportNone;
0053 
0054     m_Copy = (m_Check != cmdSupportNone) ? cmdSupportCore : cmdSupportNone;
0055     m_Move = (m_Check != cmdSupportNone) ? cmdSupportCore : cmdSupportNone;
0056 
0057     m_GetLabel = cmdSupportCore;
0058     m_Backup = cmdSupportCore;
0059     m_GetUUID = cmdSupportCore;
0060 
0061     if (m_Create == cmdSupportFileSystem) {
0062         ExternalCommand cmd(QStringLiteral("mkfs.btrfs"), QStringList() << QStringLiteral("-O") << QStringLiteral("list-all"));
0063         if (cmd.run(-1) && cmd.exitCode() == 0) {
0064             QStringList lines = cmd.output().split(QStringLiteral("\n"));
0065 
0066             // First line is introductory text, we don't need it
0067             lines.removeFirst();
0068 
0069             for (const auto& l: lines) {
0070                 if (!l.isEmpty())
0071                     addAvailableFeature(l.split(QStringLiteral(" ")).first());
0072             }
0073         }
0074     }
0075 
0076 }
0077 
0078 bool btrfs::supportToolFound() const
0079 {
0080     return
0081         m_GetUsed != cmdSupportNone &&
0082         m_GetLabel != cmdSupportNone &&
0083         m_SetLabel != cmdSupportNone &&
0084         m_Create != cmdSupportNone &&
0085         m_Check != cmdSupportNone &&
0086 //          m_UpdateUUID != cmdSupportNone &&
0087         m_Grow != cmdSupportNone &&
0088         m_Shrink != cmdSupportNone &&
0089         m_Copy != cmdSupportNone &&
0090         m_Move != cmdSupportNone &&
0091         m_Backup != cmdSupportNone &&
0092         m_GetUUID != cmdSupportNone;
0093 }
0094 
0095 FileSystem::SupportTool btrfs::supportToolName() const
0096 {
0097     return SupportTool(QStringLiteral("btrfs-tools"), QUrl(QStringLiteral("https://btrfs.wiki.kernel.org/")));
0098 }
0099 
0100 qint64 btrfs::minCapacity() const
0101 {
0102     return 256 * Capacity::unitFactor(Capacity::Unit::Byte, Capacity::Unit::MiB);
0103 }
0104 
0105 qint64 btrfs::maxCapacity() const
0106 {
0107     return Capacity::unitFactor(Capacity::Unit::Byte, Capacity::Unit::EiB);
0108 }
0109 
0110 int btrfs::maxLabelLength() const
0111 {
0112     return 255;
0113 }
0114 
0115 qint64 btrfs::readUsedCapacity(const QString& deviceNode) const
0116 {
0117     ExternalCommand cmd(QStringLiteral("btrfs"),
0118                         { QStringLiteral("filesystem"), QStringLiteral("show"), QStringLiteral("--raw"), deviceNode });
0119 
0120     if (cmd.run(-1) && cmd.exitCode() == 0) {
0121         QRegularExpression re(QStringLiteral(" used (\\d+) path ") + deviceNode);
0122         QRegularExpressionMatch reBytesUsed = re.match(cmd.output());
0123 
0124         if (reBytesUsed.hasMatch())
0125             return reBytesUsed.captured(1).toLongLong();
0126     }
0127 
0128     return -1;
0129 }
0130 
0131 bool btrfs::check(Report& report, const QString& deviceNode) const
0132 {
0133     ExternalCommand cmd(report, QStringLiteral("btrfs"), { QStringLiteral("check"), QStringLiteral("--repair"), deviceNode });
0134     return cmd.run(-1) && cmd.exitCode() == 0;
0135 }
0136 
0137 bool btrfs::create(Report& report, const QString& deviceNode)
0138 {
0139     QStringList args = QStringList();
0140 
0141     if (!this->features().isEmpty()) {
0142         QStringList feature_list = QStringList();
0143         for (const auto& k : this->features().keys()) {
0144         const auto& v = this->features().value(k);
0145             if (v.type() == QVariant::Type::Bool) {
0146                 if (v.toBool())
0147                     feature_list << k;
0148         else
0149                     feature_list << (QStringLiteral("^") +  k);
0150             } else {
0151                 qWarning() << "Ignoring feature" << k << "of type" << v.type() << "; requires type QVariant::bool.";
0152             }
0153         }
0154         args << QStringLiteral("--features") << feature_list.join(QStringLiteral(","));
0155     }
0156     args << QStringLiteral("--force") << deviceNode;
0157 
0158     ExternalCommand cmd(report, QStringLiteral("mkfs.btrfs"), args);
0159     return cmd.run(-1) && cmd.exitCode() == 0;
0160 }
0161 
0162 bool btrfs::resize(Report& report, const QString& deviceNode, qint64 length) const
0163 {
0164     QTemporaryDir tempDir;
0165     if (!tempDir.isValid()) {
0166         report.line() << xi18nc("@info:progress", "Resizing Btrfs file system on partition <filename>%1</filename> failed: Could not create temp dir.", deviceNode);
0167         return false;
0168     }
0169 
0170     bool rval = false;
0171 
0172     ExternalCommand mountCmd(report, QStringLiteral("mount"),
0173                              { QStringLiteral("--verbose"),  QStringLiteral("--types"), QStringLiteral("btrfs"), deviceNode, tempDir.path() });
0174 
0175     if (mountCmd.run(-1) && mountCmd.exitCode() == 0) {
0176         ExternalCommand resizeCmd(report, QStringLiteral("btrfs"),
0177                                   { QStringLiteral("filesystem"), QStringLiteral("resize"), QString::number(length), tempDir.path() });
0178 
0179         if (resizeCmd.run(-1) && resizeCmd.exitCode() == 0)
0180             rval = true;
0181         else
0182             report.line() << xi18nc("@info:progress", "Resizing Btrfs file system on partition <filename>%1</filename> failed: btrfs file system resize failed.", deviceNode);
0183 
0184         ExternalCommand unmountCmd(report, QStringLiteral("umount"), { tempDir.path() });
0185 
0186         if (!unmountCmd.run(-1) && unmountCmd.exitCode() == 0)
0187             report.line() << xi18nc("@info:progress", "<warning>Resizing Btrfs file system on partition <filename>%1</filename>: Unmount failed.</warning>", deviceNode);
0188     } else
0189         report.line() << xi18nc("@info:progress", "Resizing Btrfs file system on partition <filename>%1</filename> failed: Initial mount failed.", deviceNode);
0190 
0191     return rval;
0192 }
0193 
0194 bool btrfs::resizeOnline(Report& report, const QString& deviceNode, const QString& mountPoint, qint64 length) const
0195 {
0196     ExternalCommand resizeCmd(report, QStringLiteral("btrfs"),
0197                               { QStringLiteral("filesystem"), QStringLiteral("resize"), QString::number(length), mountPoint });
0198 
0199     if (resizeCmd.run(-1) && resizeCmd.exitCode() == 0)
0200         return true;
0201 
0202     report.line() << xi18nc("@info:progress", "Resizing Btrfs file system on partition <filename>%1</filename> failed: btrfs file system resize failed.", deviceNode);
0203     return false;
0204 }
0205 
0206 bool btrfs::writeLabel(Report& report, const QString& deviceNode, const QString& newLabel)
0207 {
0208     ExternalCommand cmd(report, QStringLiteral("btrfs"), { QStringLiteral("filesystem"), QStringLiteral("label"), deviceNode, newLabel });
0209     return cmd.run(-1) && cmd.exitCode() == 0;
0210 }
0211 
0212 bool btrfs::writeLabelOnline(Report& report, const QString& deviceNode, const QString& mountPoint, const QString& newLabel)
0213 {
0214     Q_UNUSED(deviceNode)
0215     ExternalCommand cmd(report, QStringLiteral("btrfs"), { QStringLiteral("filesystem"), QStringLiteral("label"), mountPoint, newLabel });
0216     return cmd.run(-1) && cmd.exitCode() == 0;
0217 }
0218 
0219 bool btrfs::updateUUID(Report& report, const QString& deviceNode) const
0220 {
0221     ExternalCommand cmd(report, QStringLiteral("btrfstune"), { QStringLiteral("-f"), QStringLiteral("-u"), deviceNode });
0222     return cmd.run(-1) && cmd.exitCode() == 0;
0223 }
0224 }