File indexing completed on 2024-05-05 05:48:48
0001 /* 0002 SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de> 0003 SPDX-FileCopyrightText: 2012-2018 Andrius Štikonas <andrius@stikonas.eu> 0004 SPDX-FileCopyrightText: 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/xfs.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 <QStringList> 0020 #include <QTemporaryDir> 0021 0022 #include <KLocalizedString> 0023 0024 namespace FS 0025 { 0026 FileSystem::CommandSupportType xfs::m_GetUsed = FileSystem::cmdSupportNone; 0027 FileSystem::CommandSupportType xfs::m_GetLabel = FileSystem::cmdSupportNone; 0028 FileSystem::CommandSupportType xfs::m_Create = FileSystem::cmdSupportNone; 0029 FileSystem::CommandSupportType xfs::m_Grow = FileSystem::cmdSupportNone; 0030 FileSystem::CommandSupportType xfs::m_Move = FileSystem::cmdSupportNone; 0031 FileSystem::CommandSupportType xfs::m_Check = FileSystem::cmdSupportNone; 0032 FileSystem::CommandSupportType xfs::m_Copy = FileSystem::cmdSupportNone; 0033 FileSystem::CommandSupportType xfs::m_Backup = FileSystem::cmdSupportNone; 0034 FileSystem::CommandSupportType xfs::m_SetLabel = FileSystem::cmdSupportNone; 0035 0036 xfs::xfs(qint64 firstsector, qint64 lastsector, qint64 sectorsused, const QString& label, const QVariantMap& features) : 0037 FileSystem(firstsector, lastsector, sectorsused, label, features, FileSystem::Type::Xfs) 0038 { 0039 } 0040 0041 void xfs::init() 0042 { 0043 m_GetLabel = cmdSupportCore; 0044 m_SetLabel = m_GetUsed = findExternal(QStringLiteral("xfs_db")) ? cmdSupportFileSystem : cmdSupportNone; 0045 m_Create = findExternal(QStringLiteral("mkfs.xfs")) ? cmdSupportFileSystem : cmdSupportNone; 0046 0047 m_Check = findExternal(QStringLiteral("xfs_repair")) ? cmdSupportFileSystem : cmdSupportNone; 0048 m_Grow = (findExternal(QStringLiteral("xfs_growfs"), { QStringLiteral("-V") }) && m_Check != cmdSupportNone) ? cmdSupportFileSystem : cmdSupportNone; 0049 m_Copy = findExternal(QStringLiteral("xfs_copy")) ? cmdSupportFileSystem : cmdSupportNone; 0050 m_Move = (m_Check != cmdSupportNone) ? cmdSupportCore : cmdSupportNone; 0051 m_Backup = cmdSupportCore; 0052 } 0053 0054 bool xfs::supportToolFound() const 0055 { 0056 return 0057 m_GetUsed != cmdSupportNone && 0058 m_GetLabel != cmdSupportNone && 0059 m_SetLabel != cmdSupportNone && 0060 m_Create != cmdSupportNone && 0061 m_Check != cmdSupportNone && 0062 // m_UpdateUUID != cmdSupportNone && 0063 m_Grow != cmdSupportNone && 0064 // m_Shrink != cmdSupportNone && 0065 m_Copy != cmdSupportNone && 0066 m_Move != cmdSupportNone && 0067 m_Backup != cmdSupportNone; 0068 // m_GetUUID != cmdSupportNone; 0069 } 0070 0071 FileSystem::SupportTool xfs::supportToolName() const 0072 { 0073 return SupportTool(QStringLiteral("xfsprogs"), QUrl(QStringLiteral("https://xfs.org/index.php/Getting_the_latest_source_code"))); 0074 } 0075 0076 qint64 xfs::minCapacity() const 0077 { 0078 return 32 * Capacity::unitFactor(Capacity::Unit::Byte, Capacity::Unit::MiB); 0079 } 0080 0081 qint64 xfs::maxCapacity() const 0082 { 0083 return Capacity::unitFactor(Capacity::Unit::Byte, Capacity::Unit::EiB); 0084 } 0085 0086 int xfs::maxLabelLength() const 0087 { 0088 return 12; 0089 } 0090 0091 qint64 xfs::readUsedCapacity(const QString& deviceNode) const 0092 { 0093 ExternalCommand cmd(QStringLiteral("xfs_db"), { QStringLiteral("-c"), QStringLiteral("sb 0"), QStringLiteral("-c"), QStringLiteral("print"), deviceNode }); 0094 0095 if (cmd.run(-1) && cmd.exitCode() == 0) { 0096 qint64 dBlocks = -1; 0097 QRegularExpression re(QStringLiteral("dblocks = (\\d+)")); 0098 QRegularExpressionMatch reDBlocks = re.match(cmd.output()); 0099 0100 if (reDBlocks.hasMatch()) 0101 dBlocks = reDBlocks.captured(1).toLongLong(); 0102 0103 qint64 blockSize = -1; 0104 re.setPattern(QStringLiteral("blocksize = (\\d+)")); 0105 QRegularExpressionMatch reBlockSize = re.match(cmd.output()); 0106 0107 if (reBlockSize.hasMatch()) 0108 blockSize = reBlockSize.captured(1).toLongLong(); 0109 0110 qint64 fdBlocks = -1; 0111 re.setPattern(QStringLiteral("fdblocks = (\\d+)")); 0112 QRegularExpressionMatch reFdBlocks = re.match(cmd.output()); 0113 0114 if (reFdBlocks.hasMatch()) 0115 fdBlocks = reFdBlocks.captured(1).toLongLong(); 0116 0117 if (dBlocks > -1 && blockSize > -1 && fdBlocks > -1) 0118 return (dBlocks - fdBlocks) * blockSize; 0119 } 0120 0121 return -1; 0122 } 0123 0124 bool xfs::writeLabel(Report& report, const QString& deviceNode, const QString& newLabel) 0125 { 0126 ExternalCommand cmd(report, QStringLiteral("xfs_db"), { QStringLiteral("-x"), QStringLiteral("-c"), QStringLiteral("sb 0"), QStringLiteral("-c"), QStringLiteral("label ") + newLabel, deviceNode }); 0127 return cmd.run(-1) && cmd.exitCode() == 0; 0128 } 0129 0130 bool xfs::check(Report& report, const QString& deviceNode) const 0131 { 0132 ExternalCommand cmd(report, QStringLiteral("xfs_repair"), { QStringLiteral("-v"), deviceNode }); 0133 return cmd.run(-1) && cmd.exitCode() == 0; 0134 } 0135 0136 bool xfs::create(Report& report, const QString& deviceNode) 0137 { 0138 ExternalCommand cmd(report, QStringLiteral("mkfs.xfs"), { QStringLiteral("-f"), deviceNode }); 0139 return cmd.run(-1) && cmd.exitCode() == 0; 0140 } 0141 0142 bool xfs::copy(Report& report, const QString& targetDeviceNode, const QString& sourceDeviceNode) const 0143 { 0144 ExternalCommand cmd(report, QStringLiteral("xfs_copy"), { sourceDeviceNode, targetDeviceNode }); 0145 0146 // xfs_copy behaves a little strangely. It apparently kills itself at the end of main, causing QProcess 0147 // to report that it crashed. 0148 // See https://marc.info/?l=linux-xfs&m=110178337825926&w=2 0149 // So we cannot rely on QProcess::exitStatus() and thus not on ExternalCommand::run() returning true. 0150 0151 cmd.run(-1); 0152 return cmd.exitCode() == 0; 0153 } 0154 0155 bool xfs::resize(Report& report, const QString& deviceNode, qint64) const 0156 { 0157 QTemporaryDir tempDir; 0158 if (!tempDir.isValid()) { 0159 report.line() << xi18nc("@info:progress", "Resizing XFS file system on partition <filename>%1</filename> failed: Could not create temp dir.", deviceNode); 0160 return false; 0161 } 0162 0163 bool rval = false; 0164 0165 ExternalCommand mountCmd(report, QStringLiteral("mount"), { QStringLiteral("--verbose"), QStringLiteral("--types"), QStringLiteral("xfs"), deviceNode, tempDir.path() }); 0166 0167 if (mountCmd.run(-1)) { 0168 ExternalCommand resizeCmd(report, QStringLiteral("xfs_growfs"), { tempDir.path() }); 0169 0170 if (resizeCmd.run(-1) && resizeCmd.exitCode() == 0) 0171 rval = true; 0172 else 0173 report.line() << xi18nc("@info:progress", "Resizing XFS file system on partition <filename>%1</filename> failed: xfs_growfs failed.", deviceNode); 0174 0175 ExternalCommand unmountCmd(report, QStringLiteral("umount"), { tempDir.path() }); 0176 0177 if (!unmountCmd.run(-1)) 0178 report.line() << xi18nc("@info:progress", "<warning>Resizing XFS file system on partition <filename>%1</filename> failed: Unmount failed.</warning>", deviceNode); 0179 } else 0180 report.line() << xi18nc("@info:progress", "Resizing XFS file system on partition <filename>%1</filename> failed: Initial mount failed.", deviceNode); 0181 0182 return rval; 0183 } 0184 0185 bool xfs::resizeOnline(Report& report, const QString& deviceNode, const QString& mountPoint, qint64) const 0186 { 0187 ExternalCommand resizeCmd(report, QStringLiteral("xfs_growfs"), { mountPoint }); 0188 0189 if (resizeCmd.run(-1) && resizeCmd.exitCode() == 0) 0190 return true; 0191 0192 report.line() << xi18nc("@info:progress", "Resizing XFS file system on partition <filename>%1</filename> failed: xfs_growfs failed.", deviceNode); 0193 return false; 0194 } 0195 }