2020-09-28 00:45:21 +01:00
/*
SPDX - FileCopyrightText : 2010 Volker Lanz < vl @ fidra . de >
SPDX - FileCopyrightText : 2012 - 2018 Andrius Š tikonas < andrius @ stikonas . eu >
SPDX - FileCopyrightText : 2012 , 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
*/
2015-06-04 01:29:22 +01:00
# include "fs/btrfs.h"
# include "util/externalcommand.h"
# include "util/capacity.h"
# include "util/report.h"
2016-05-12 16:37:37 +01:00
# include <QRegularExpression>
2019-06-12 17:41:11 +01:00
# include <QString>
2015-06-04 01:29:22 +01:00
# include <QTemporaryDir>
# include <KLocalizedString>
namespace FS
{
2015-07-13 15:16:36 +01:00
FileSystem : : CommandSupportType btrfs : : m_GetUsed = FileSystem : : cmdSupportNone ;
FileSystem : : CommandSupportType btrfs : : m_GetLabel = FileSystem : : cmdSupportNone ;
FileSystem : : CommandSupportType btrfs : : m_Create = FileSystem : : cmdSupportNone ;
FileSystem : : CommandSupportType btrfs : : m_Grow = FileSystem : : cmdSupportNone ;
FileSystem : : CommandSupportType btrfs : : m_Shrink = FileSystem : : cmdSupportNone ;
FileSystem : : CommandSupportType btrfs : : m_Move = FileSystem : : cmdSupportNone ;
FileSystem : : CommandSupportType btrfs : : m_Check = FileSystem : : cmdSupportNone ;
FileSystem : : CommandSupportType btrfs : : m_Copy = FileSystem : : cmdSupportNone ;
FileSystem : : CommandSupportType btrfs : : m_Backup = FileSystem : : cmdSupportNone ;
FileSystem : : CommandSupportType btrfs : : m_SetLabel = FileSystem : : cmdSupportNone ;
FileSystem : : CommandSupportType btrfs : : m_UpdateUUID = FileSystem : : cmdSupportNone ;
FileSystem : : CommandSupportType btrfs : : m_GetUUID = FileSystem : : cmdSupportNone ;
2020-03-21 13:18:13 +00:00
btrfs : : btrfs ( qint64 firstsector , qint64 lastsector , qint64 sectorsused , const QString & label , const QVariantMap & features ) :
2020-01-12 11:54:14 +00:00
FileSystem ( firstsector , lastsector , sectorsused , label , features , FileSystem : : Type : : Btrfs )
2015-07-13 15:16:36 +01:00
{
}
void btrfs : : init ( )
{
m_Create = findExternal ( QStringLiteral ( " mkfs.btrfs " ) ) ? cmdSupportFileSystem : cmdSupportNone ;
2018-01-29 13:30:14 +00:00
m_Check = findExternal ( QStringLiteral ( " btrfs " ) ) ? cmdSupportFileSystem : cmdSupportNone ;
m_Grow = m_Check ;
m_GetUsed = m_Check ;
2015-07-13 15:16:36 +01:00
m_Shrink = ( m_Grow ! = cmdSupportNone & & m_GetUsed ! = cmdSupportNone ) ? cmdSupportFileSystem : cmdSupportNone ;
2018-01-29 13:30:14 +00:00
m_SetLabel = m_Check ;
2017-11-05 20:42:40 +00:00
m_UpdateUUID = findExternal ( QStringLiteral ( " btrfstune " ) ) ? cmdSupportFileSystem : cmdSupportNone ;
2015-07-13 15:16:36 +01:00
m_Copy = ( m_Check ! = cmdSupportNone ) ? cmdSupportCore : cmdSupportNone ;
m_Move = ( m_Check ! = cmdSupportNone ) ? cmdSupportCore : cmdSupportNone ;
m_GetLabel = cmdSupportCore ;
m_Backup = cmdSupportCore ;
m_GetUUID = cmdSupportCore ;
2020-01-12 11:54:14 +00:00
if ( m_Create = = cmdSupportFileSystem ) {
ExternalCommand cmd ( QStringLiteral ( " mkfs.btrfs " ) , QStringList ( ) < < QStringLiteral ( " -O " ) < < QStringLiteral ( " list-all " ) ) ;
if ( cmd . run ( - 1 ) & & cmd . exitCode ( ) = = 0 ) {
QStringList lines = cmd . output ( ) . split ( QStringLiteral ( " \n " ) ) ;
// First line is introductory text, we don't need it
lines . removeFirst ( ) ;
2020-03-21 13:18:13 +00:00
for ( const auto & l : lines ) {
2020-01-12 11:54:14 +00:00
if ( ! l . isEmpty ( ) )
addAvailableFeature ( l . split ( QStringLiteral ( " " ) ) . first ( ) ) ;
}
}
}
2015-07-13 15:16:36 +01:00
}
bool btrfs : : 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 btrfs : : supportToolName ( ) const
{
2019-11-22 13:34:10 +00:00
return SupportTool ( QStringLiteral ( " btrfs-tools " ) , QUrl ( QStringLiteral ( " https://btrfs.wiki.kernel.org/ " ) ) ) ;
2015-07-13 15:16:36 +01:00
}
qint64 btrfs : : minCapacity ( ) const
{
2018-04-09 15:14:34 +01:00
return 256 * Capacity : : unitFactor ( Capacity : : Unit : : Byte , Capacity : : Unit : : MiB ) ;
2015-07-13 15:16:36 +01:00
}
qint64 btrfs : : maxCapacity ( ) const
{
2018-04-09 15:14:34 +01:00
return Capacity : : unitFactor ( Capacity : : Unit : : Byte , Capacity : : Unit : : EiB ) ;
2015-07-13 15:16:36 +01:00
}
2017-09-11 16:52:20 +01:00
int btrfs : : maxLabelLength ( ) const
2015-07-13 15:16:36 +01:00
{
return 255 ;
}
qint64 btrfs : : readUsedCapacity ( const QString & deviceNode ) const
{
2016-04-21 22:48:05 +01:00
ExternalCommand cmd ( QStringLiteral ( " btrfs " ) ,
{ QStringLiteral ( " filesystem " ) , QStringLiteral ( " show " ) , QStringLiteral ( " --raw " ) , deviceNode } ) ;
2015-07-13 15:16:36 +01:00
2016-05-12 16:37:37 +01:00
if ( cmd . run ( - 1 ) & & cmd . exitCode ( ) = = 0 ) {
QRegularExpression re ( QStringLiteral ( " used ( \\ d+) path " ) + deviceNode) ;
QRegularExpressionMatch reBytesUsed = re . match ( cmd . output ( ) ) ;
2015-07-13 15:16:36 +01:00
2016-05-12 16:37:37 +01:00
if ( reBytesUsed . hasMatch ( ) )
return reBytesUsed . captured ( 1 ) . toLongLong ( ) ;
2015-07-13 15:16:36 +01:00
}
return - 1 ;
}
bool btrfs : : check ( Report & report , const QString & deviceNode ) const
{
2018-01-29 13:30:14 +00:00
ExternalCommand cmd ( report , QStringLiteral ( " btrfs " ) , { QStringLiteral ( " check " ) , QStringLiteral ( " --repair " ) , deviceNode } ) ;
2015-07-13 15:16:36 +01:00
return cmd . run ( - 1 ) & & cmd . exitCode ( ) = = 0 ;
}
2016-09-05 12:10:56 +01:00
bool btrfs : : create ( Report & report , const QString & deviceNode )
2015-07-13 15:16:36 +01:00
{
2020-01-12 11:54:14 +00:00
QStringList args = QStringList ( ) ;
if ( ! this - > features ( ) . isEmpty ( ) ) {
QStringList feature_list = QStringList ( ) ;
2020-03-21 13:18:13 +00:00
for ( const auto & k : this - > features ( ) . keys ( ) ) {
const auto & v = this - > features ( ) . value ( k ) ;
if ( v . type ( ) = = QVariant : : Type : : Bool ) {
if ( v . toBool ( ) )
feature_list < < k ;
else
feature_list < < ( QStringLiteral ( " ^ " ) + k ) ;
} else {
qWarning ( ) < < " Ignoring feature " < < k < < " of type " < < v . type ( ) < < " ; requires type QVariant::bool. " ;
2020-01-12 11:54:14 +00:00
}
}
args < < QStringLiteral ( " --features " ) < < feature_list . join ( QStringLiteral ( " , " ) ) ;
}
args < < QStringLiteral ( " --force " ) < < deviceNode ;
ExternalCommand cmd ( report , QStringLiteral ( " mkfs.btrfs " ) , args ) ;
2015-07-13 15:16:36 +01:00
return cmd . run ( - 1 ) & & cmd . exitCode ( ) = = 0 ;
}
bool btrfs : : resize ( Report & report , const QString & deviceNode , qint64 length ) const
{
QTemporaryDir tempDir ;
if ( ! tempDir . isValid ( ) ) {
2016-07-17 23:41:00 +01:00
report . line ( ) < < xi18nc ( " @info:progress " , " Resizing Btrfs file system on partition <filename>%1</filename> failed: Could not create temp dir. " , deviceNode ) ;
2015-07-13 15:16:36 +01:00
return false ;
}
bool rval = false ;
2016-04-21 22:48:05 +01:00
ExternalCommand mountCmd ( report , QStringLiteral ( " mount " ) ,
{ QStringLiteral ( " --verbose " ) , QStringLiteral ( " --types " ) , QStringLiteral ( " btrfs " ) , deviceNode , tempDir . path ( ) } ) ;
2015-07-13 15:16:36 +01:00
if ( mountCmd . run ( - 1 ) & & mountCmd . exitCode ( ) = = 0 ) {
2016-05-11 23:00:37 +01:00
ExternalCommand resizeCmd ( report , QStringLiteral ( " btrfs " ) ,
{ QStringLiteral ( " filesystem " ) , QStringLiteral ( " resize " ) , QString : : number ( length ) , tempDir . path ( ) } ) ;
2015-07-13 15:16:36 +01:00
if ( resizeCmd . run ( - 1 ) & & resizeCmd . exitCode ( ) = = 0 )
rval = true ;
else
2016-07-17 23:41:00 +01:00
report . line ( ) < < xi18nc ( " @info:progress " , " Resizing Btrfs file system on partition <filename>%1</filename> failed: btrfs file system resize failed. " , deviceNode ) ;
2015-07-13 15:16:36 +01:00
2016-04-21 22:48:05 +01:00
ExternalCommand unmountCmd ( report , QStringLiteral ( " umount " ) , { tempDir . path ( ) } ) ;
2015-07-13 15:16:36 +01:00
if ( ! unmountCmd . run ( - 1 ) & & unmountCmd . exitCode ( ) = = 0 )
2016-07-18 12:21:06 +01:00
report . line ( ) < < xi18nc ( " @info:progress " , " <warning>Resizing Btrfs file system on partition <filename>%1</filename>: Unmount failed.</warning> " , deviceNode ) ;
2015-07-13 15:16:36 +01:00
} else
2016-07-17 23:41:00 +01:00
report . line ( ) < < xi18nc ( " @info:progress " , " Resizing Btrfs file system on partition <filename>%1</filename> failed: Initial mount failed. " , deviceNode ) ;
2015-07-13 15:16:36 +01:00
return rval ;
}
2016-08-08 02:01:35 +01:00
bool btrfs : : resizeOnline ( Report & report , const QString & deviceNode , const QString & mountPoint , qint64 length ) const
{
ExternalCommand resizeCmd ( report , QStringLiteral ( " btrfs " ) ,
{ QStringLiteral ( " filesystem " ) , QStringLiteral ( " resize " ) , QString : : number ( length ) , mountPoint } ) ;
if ( resizeCmd . run ( - 1 ) & & resizeCmd . exitCode ( ) = = 0 )
return true ;
report . line ( ) < < xi18nc ( " @info:progress " , " Resizing Btrfs file system on partition <filename>%1</filename> failed: btrfs file system resize failed. " , deviceNode ) ;
return false ;
}
2015-07-13 15:16:36 +01:00
bool btrfs : : writeLabel ( Report & report , const QString & deviceNode , const QString & newLabel )
{
2016-04-21 22:48:05 +01:00
ExternalCommand cmd ( report , QStringLiteral ( " btrfs " ) , { QStringLiteral ( " filesystem " ) , QStringLiteral ( " label " ) , deviceNode , newLabel } ) ;
2015-07-13 15:16:36 +01:00
return cmd . run ( - 1 ) & & cmd . exitCode ( ) = = 0 ;
}
2017-04-09 17:49:55 +01:00
bool btrfs : : writeLabelOnline ( Report & report , const QString & deviceNode , const QString & mountPoint , const QString & newLabel )
{
Q_UNUSED ( deviceNode )
ExternalCommand cmd ( report , QStringLiteral ( " btrfs " ) , { QStringLiteral ( " filesystem " ) , QStringLiteral ( " label " ) , mountPoint , newLabel } ) ;
return cmd . run ( - 1 ) & & cmd . exitCode ( ) = = 0 ;
}
2015-07-13 15:16:36 +01:00
bool btrfs : : updateUUID ( Report & report , const QString & deviceNode ) const
{
2017-11-05 20:42:40 +00:00
ExternalCommand cmd ( report , QStringLiteral ( " btrfstune " ) , { QStringLiteral ( " -f " ) , QStringLiteral ( " -u " ) , deviceNode } ) ;
return cmd . run ( - 1 ) & & cmd . exitCode ( ) = = 0 ;
2015-07-13 15:16:36 +01:00
}
2015-06-04 01:29:22 +01:00
}