2009-11-05 11:08:32 -07:00
|
|
|
/* Copyright (C) 2004 Bart
|
2010-10-19 10:02:18 -06:00
|
|
|
* Copyright (C) 2008, 2009, 2010 Curtis Gedak
|
2004-11-17 06:00:25 -07: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 2 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
|
2014-01-23 03:59:48 -07:00
|
|
|
* GNU General Public License for more details.
|
2004-11-17 06:00:25 -07:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2014-01-23 03:59:48 -07:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2004-11-17 06:00:25 -07:00
|
|
|
*/
|
2016-10-18 16:45:28 -06:00
|
|
|
|
|
|
|
#include "linux_swap.h"
|
|
|
|
#include "BlockSpecial.h"
|
2018-01-18 09:26:20 -07:00
|
|
|
#include "FileSystem.h"
|
2016-10-18 16:45:28 -06:00
|
|
|
#include "Partition.h"
|
2004-11-17 06:00:25 -07:00
|
|
|
|
2013-09-14 07:51:16 -06:00
|
|
|
#include <cerrno>
|
2018-12-04 09:46:27 -07:00
|
|
|
#include <glibmm/miscutils.h>
|
|
|
|
#include <glibmm/stringutils.h>
|
|
|
|
#include <glibmm/shell.h>
|
|
|
|
|
2013-09-14 07:51:16 -06:00
|
|
|
|
2004-11-17 06:00:25 -07:00
|
|
|
namespace GParted
|
|
|
|
{
|
|
|
|
|
2018-01-30 09:31:31 -07:00
|
|
|
const Glib::ustring & linux_swap::get_custom_text( CUSTOM_TEXT ttype, int index ) const
|
2012-01-27 12:41:31 -07:00
|
|
|
{
|
|
|
|
/*TO TRANSLATORS: these labels will be used in the partition menu */
|
|
|
|
static const Glib::ustring activate_text = _( "_Swapon" ) ;
|
|
|
|
static const Glib::ustring deactivate_text = _( "_Swapoff" ) ;
|
2018-01-30 09:31:31 -07:00
|
|
|
static const Glib::ustring empty_text;
|
2012-01-27 12:41:31 -07:00
|
|
|
|
|
|
|
switch ( ttype ) {
|
|
|
|
case CTEXT_ACTIVATE_FILESYSTEM :
|
2018-01-30 09:31:31 -07:00
|
|
|
return index == 0 ? activate_text : empty_text;
|
2012-01-27 12:41:31 -07:00
|
|
|
case CTEXT_DEACTIVATE_FILESYSTEM :
|
2018-01-30 09:31:31 -07:00
|
|
|
return index == 0 ? deactivate_text : empty_text;
|
2012-01-27 12:41:31 -07:00
|
|
|
default :
|
2018-01-30 09:31:31 -07:00
|
|
|
return empty_text;
|
2012-01-27 12:41:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-24 05:31:58 -07:00
|
|
|
FS linux_swap::get_filesystem_support()
|
2004-11-17 06:00:25 -07:00
|
|
|
{
|
2015-11-13 14:46:57 -07:00
|
|
|
FS fs( FS_LINUX_SWAP );
|
2014-02-14 18:26:32 -07:00
|
|
|
|
|
|
|
fs .busy = FS::GPARTED ;
|
2013-09-14 07:51:16 -06:00
|
|
|
fs .read = FS::EXTERNAL ;
|
|
|
|
fs .online_read = FS::EXTERNAL ;
|
|
|
|
|
2006-01-24 05:31:58 -07:00
|
|
|
if ( ! Glib::find_program_in_path( "mkswap" ) .empty() )
|
2004-12-28 05:59:46 -07:00
|
|
|
{
|
2018-10-13 05:59:31 -06:00
|
|
|
fs.create = FS::EXTERNAL;
|
|
|
|
fs.create_with_label = FS::EXTERNAL;
|
|
|
|
fs.grow = FS::EXTERNAL;
|
|
|
|
fs.shrink = FS::EXTERNAL;
|
2016-11-19 15:46:11 -07:00
|
|
|
fs.copy = FS::EXTERNAL;
|
|
|
|
fs.move = FS::EXTERNAL;
|
2004-12-28 05:59:46 -07:00
|
|
|
}
|
2008-04-07 13:41:18 -06:00
|
|
|
|
2010-08-04 15:57:29 -06:00
|
|
|
if ( ! Glib::find_program_in_path( "swaplabel" ) .empty() )
|
2012-01-22 13:49:52 -07:00
|
|
|
{
|
2012-10-10 17:07:08 -06:00
|
|
|
fs .read_label = FS::EXTERNAL ;
|
2012-02-18 13:54:58 -07:00
|
|
|
fs .write_label = FS::EXTERNAL ;
|
2012-01-22 13:49:52 -07:00
|
|
|
fs .read_uuid = FS::EXTERNAL ;
|
|
|
|
fs .write_uuid = FS::EXTERNAL ;
|
|
|
|
}
|
2010-08-04 15:57:29 -06:00
|
|
|
|
2004-11-17 06:00:25 -07:00
|
|
|
return fs ;
|
|
|
|
}
|
|
|
|
|
2013-09-14 07:51:16 -06:00
|
|
|
void linux_swap::set_used_sectors( Partition & partition )
|
|
|
|
{
|
|
|
|
if ( partition .busy )
|
|
|
|
{
|
2015-01-09 07:07:25 -07:00
|
|
|
N = -1;
|
2013-09-14 07:51:16 -06:00
|
|
|
std::string line ;
|
|
|
|
std::ifstream input( "/proc/swaps" ) ;
|
|
|
|
if ( input )
|
|
|
|
{
|
2016-09-14 07:18:17 -06:00
|
|
|
BlockSpecial bs_path = BlockSpecial( partition.get_path() );
|
2013-09-14 07:51:16 -06:00
|
|
|
while ( getline( input, line ) )
|
|
|
|
{
|
2016-09-14 07:18:17 -06:00
|
|
|
Glib::ustring filename = Utils::regexp_label( line, "^([[:graph:]]+)" );
|
|
|
|
if ( bs_path == BlockSpecial( filename ) )
|
2013-09-14 07:51:16 -06:00
|
|
|
{
|
2016-09-14 07:18:17 -06:00
|
|
|
sscanf( line.c_str(), "%*s %*s %*d %lld", &N );
|
2013-09-14 07:51:16 -06:00
|
|
|
break ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
input .close() ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-31 09:32:08 -07:00
|
|
|
partition.push_back_message( "open(\"/proc/swaps\", O_RDONLY): " + Glib::strerror( errno ) );
|
2013-09-14 07:51:16 -06:00
|
|
|
}
|
2015-01-09 07:07:25 -07:00
|
|
|
if ( N > -1 )
|
2013-09-14 07:51:16 -06:00
|
|
|
{
|
2015-01-09 07:07:25 -07:00
|
|
|
// Ignore swap space reported size to ignore 1 page format
|
|
|
|
// overhead. Instead use partition size as sectors_fs_size so
|
|
|
|
// reported used figure for active swap space starts from 0
|
|
|
|
// upwards, matching what 'swapon -s' reports.
|
|
|
|
T = partition.get_sector_length();
|
2013-09-14 07:51:16 -06:00
|
|
|
N = Utils::round( N * ( KIBIBYTE / double(partition .sector_size) ) ) ;
|
|
|
|
partition .set_sector_usage( T, T - N ) ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//By definition inactive swap space is 100% free
|
|
|
|
Sector size = partition .get_sector_length() ;
|
|
|
|
partition .set_sector_usage( size, size ) ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-08 16:55:17 -07:00
|
|
|
void linux_swap::read_label( Partition & partition )
|
2006-09-12 14:34:33 -06:00
|
|
|
{
|
2017-09-03 01:52:54 -06:00
|
|
|
if ( ! Utils::execute_command( "swaplabel " + Glib::shell_quote( partition.get_path() ), output, error, true ) )
|
2008-04-07 13:41:18 -06:00
|
|
|
{
|
2014-12-16 15:04:34 -07:00
|
|
|
partition.set_filesystem_label( Utils::regexp_label( output, "^LABEL:[[:blank:]]*(.*)$" ) );
|
2008-04-07 13:41:18 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( ! output .empty() )
|
2015-12-31 09:32:08 -07:00
|
|
|
partition.push_back_message( output );
|
2008-04-07 13:41:18 -06:00
|
|
|
|
|
|
|
if ( ! error .empty() )
|
2015-12-31 09:32:08 -07:00
|
|
|
partition.push_back_message( error );
|
2008-04-07 13:41:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-08 16:55:17 -07:00
|
|
|
bool linux_swap::write_label( const Partition & partition, OperationDetail & operationdetail )
|
2008-04-07 13:41:18 -06:00
|
|
|
{
|
2017-09-03 01:52:54 -06:00
|
|
|
return ! execute_command( "swaplabel -L " + Glib::shell_quote( partition.get_filesystem_label() ) +
|
|
|
|
" " + Glib::shell_quote( partition.get_path() ),
|
Time and check nearly all file system action commands (#754684)
There has been an undocumented rule that external commands displayed in
the operation details, as part of file system manipulations, only get a
time and check mark displayed when multiple commands are needed, and not
otherwise. (GParted checks whether all commands are successful or not
regardless of whether a check mark is displayed in the operation details
or not).
EXCEPTION 1: btrfs resize
Since the following commit [1] from 2013-02-22, GParted stopped
displaying the timing for the btrfs resize command in the operation
details. It being part of a multi-command sequence to perform the step.
This is because FileSystem::execute_command() since the commit can only
check the exit status for zero / non-zero while timing and checking the
command status but btrfs resize needs to consider some non-zero statuses
as successful.
[1] 52a2a9b00a32996921ace055e71d0e09fb33c5fe
Reduce threading (#685740)
EXCEPTION 2: ext2/3/4 move and copy using e2image
When use of e2image was added [2] the single command steps were timed
and check.
[2] 86111fe12a26d23d9fc2a9e2d19281290ecaf985
Use e2image to move/copy ext[234] file systems (#721516)
EXCEPTION 3: fat16/32 write label and UUID
Uses Utils::execute_command() rather than FileSystem::execute_command()
so can be separately changed. See the following commit for resolution
of the final commands not yet timed and check mark displayed.
CHANGE:
Lets make a simpler rule of always displaying the time and a check mark
for all external commands displayed in the operation details. However
this makes several of the other single command actions need special exit
status handling because zero success, non-zero failure is not correct
for every case. Specifically affects resizing of reiserfs and check
repair of ext2/3/4, fat16/32, jfs and reiserfs.
After this change all external commands run as file system actions must
follow one of these two patterns of using the EXEC_CHECK_STATUS flag or
separately calling FileSystem::set_status() to register success or
failure of the command:
exit_status = execute_command(cmd, od, EXEC_CHECK_STATUS...);
or:
exit_status = execute_command(cmd, od, ...);
bool success = (exit_status == 0 || exit_status == OTHER_SUCCESS_VALUE...);
set_status(od, success );
Bug 754684 - Updates to FileSystem:: and Utils::execute_command()
functions
2015-09-05 02:31:16 -06:00
|
|
|
operationdetail, EXEC_CHECK_STATUS );
|
2006-09-12 14:34:33 -06:00
|
|
|
}
|
|
|
|
|
2012-01-22 13:49:52 -07:00
|
|
|
void linux_swap::read_uuid( Partition & partition )
|
|
|
|
{
|
2017-09-03 01:52:54 -06:00
|
|
|
if ( ! Utils::execute_command( "swaplabel " + Glib::shell_quote( partition.get_path() ), output, error, true ) )
|
2012-01-22 13:49:52 -07:00
|
|
|
{
|
2012-09-23 08:44:40 -06:00
|
|
|
partition .uuid = Utils::regexp_label( output, "^UUID:[[:blank:]]*(" RFC4122_NONE_NIL_UUID_REGEXP ")" ) ;
|
2012-01-22 13:49:52 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( ! output .empty() )
|
2015-12-31 09:32:08 -07:00
|
|
|
partition.push_back_message( output );
|
2012-01-22 13:49:52 -07:00
|
|
|
|
|
|
|
if ( ! error .empty() )
|
2015-12-31 09:32:08 -07:00
|
|
|
partition.push_back_message( error );
|
2012-01-22 13:49:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool linux_swap::write_uuid( const Partition & partition, OperationDetail & operationdetail )
|
|
|
|
{
|
2017-09-03 01:52:54 -06:00
|
|
|
return ! execute_command( "swaplabel -U " + Glib::shell_quote( Utils::generate_uuid() ) +
|
|
|
|
" " + Glib::shell_quote( partition.get_path() ),
|
Time and check nearly all file system action commands (#754684)
There has been an undocumented rule that external commands displayed in
the operation details, as part of file system manipulations, only get a
time and check mark displayed when multiple commands are needed, and not
otherwise. (GParted checks whether all commands are successful or not
regardless of whether a check mark is displayed in the operation details
or not).
EXCEPTION 1: btrfs resize
Since the following commit [1] from 2013-02-22, GParted stopped
displaying the timing for the btrfs resize command in the operation
details. It being part of a multi-command sequence to perform the step.
This is because FileSystem::execute_command() since the commit can only
check the exit status for zero / non-zero while timing and checking the
command status but btrfs resize needs to consider some non-zero statuses
as successful.
[1] 52a2a9b00a32996921ace055e71d0e09fb33c5fe
Reduce threading (#685740)
EXCEPTION 2: ext2/3/4 move and copy using e2image
When use of e2image was added [2] the single command steps were timed
and check.
[2] 86111fe12a26d23d9fc2a9e2d19281290ecaf985
Use e2image to move/copy ext[234] file systems (#721516)
EXCEPTION 3: fat16/32 write label and UUID
Uses Utils::execute_command() rather than FileSystem::execute_command()
so can be separately changed. See the following commit for resolution
of the final commands not yet timed and check mark displayed.
CHANGE:
Lets make a simpler rule of always displaying the time and a check mark
for all external commands displayed in the operation details. However
this makes several of the other single command actions need special exit
status handling because zero success, non-zero failure is not correct
for every case. Specifically affects resizing of reiserfs and check
repair of ext2/3/4, fat16/32, jfs and reiserfs.
After this change all external commands run as file system actions must
follow one of these two patterns of using the EXEC_CHECK_STATUS flag or
separately calling FileSystem::set_status() to register success or
failure of the command:
exit_status = execute_command(cmd, od, EXEC_CHECK_STATUS...);
or:
exit_status = execute_command(cmd, od, ...);
bool success = (exit_status == 0 || exit_status == OTHER_SUCCESS_VALUE...);
set_status(od, success );
Bug 754684 - Updates to FileSystem:: and Utils::execute_command()
functions
2015-09-05 02:31:16 -06:00
|
|
|
operationdetail, EXEC_CHECK_STATUS );
|
2012-01-22 13:49:52 -07:00
|
|
|
}
|
|
|
|
|
2006-08-20 03:33:54 -06:00
|
|
|
bool linux_swap::create( const Partition & new_partition, OperationDetail & operationdetail )
|
2004-11-17 06:00:25 -07:00
|
|
|
{
|
2017-09-03 01:52:54 -06:00
|
|
|
return ! execute_command( "mkswap -L " + Glib::shell_quote( new_partition.get_filesystem_label() ) +
|
|
|
|
" " + Glib::shell_quote( new_partition.get_path() ),
|
Time and check nearly all file system action commands (#754684)
There has been an undocumented rule that external commands displayed in
the operation details, as part of file system manipulations, only get a
time and check mark displayed when multiple commands are needed, and not
otherwise. (GParted checks whether all commands are successful or not
regardless of whether a check mark is displayed in the operation details
or not).
EXCEPTION 1: btrfs resize
Since the following commit [1] from 2013-02-22, GParted stopped
displaying the timing for the btrfs resize command in the operation
details. It being part of a multi-command sequence to perform the step.
This is because FileSystem::execute_command() since the commit can only
check the exit status for zero / non-zero while timing and checking the
command status but btrfs resize needs to consider some non-zero statuses
as successful.
[1] 52a2a9b00a32996921ace055e71d0e09fb33c5fe
Reduce threading (#685740)
EXCEPTION 2: ext2/3/4 move and copy using e2image
When use of e2image was added [2] the single command steps were timed
and check.
[2] 86111fe12a26d23d9fc2a9e2d19281290ecaf985
Use e2image to move/copy ext[234] file systems (#721516)
EXCEPTION 3: fat16/32 write label and UUID
Uses Utils::execute_command() rather than FileSystem::execute_command()
so can be separately changed. See the following commit for resolution
of the final commands not yet timed and check mark displayed.
CHANGE:
Lets make a simpler rule of always displaying the time and a check mark
for all external commands displayed in the operation details. However
this makes several of the other single command actions need special exit
status handling because zero success, non-zero failure is not correct
for every case. Specifically affects resizing of reiserfs and check
repair of ext2/3/4, fat16/32, jfs and reiserfs.
After this change all external commands run as file system actions must
follow one of these two patterns of using the EXEC_CHECK_STATUS flag or
separately calling FileSystem::set_status() to register success or
failure of the command:
exit_status = execute_command(cmd, od, EXEC_CHECK_STATUS...);
or:
exit_status = execute_command(cmd, od, ...);
bool success = (exit_status == 0 || exit_status == OTHER_SUCCESS_VALUE...);
set_status(od, success );
Bug 754684 - Updates to FileSystem:: and Utils::execute_command()
functions
2015-09-05 02:31:16 -06:00
|
|
|
operationdetail, EXEC_CHECK_STATUS );
|
2004-11-17 06:00:25 -07:00
|
|
|
}
|
|
|
|
|
2006-08-20 03:33:54 -06:00
|
|
|
bool linux_swap::resize( const Partition & partition_new, OperationDetail & operationdetail, bool fill_partition )
|
2004-11-17 06:00:25 -07:00
|
|
|
{
|
2009-03-23 14:01:03 -06:00
|
|
|
//Maintain label and uuid when recreating swap
|
2017-09-03 01:52:54 -06:00
|
|
|
Glib::ustring command = "mkswap -L " + Glib::shell_quote( partition_new.get_filesystem_label() ) + " ";
|
2010-03-14 13:45:05 -06:00
|
|
|
if ( ! partition_new .uuid .empty() )
|
2017-09-03 01:52:54 -06:00
|
|
|
command += " -U " + Glib::shell_quote( partition_new.uuid ) + " ";
|
|
|
|
command += Glib::shell_quote( partition_new.get_path() );
|
2016-11-19 16:48:22 -07:00
|
|
|
return ! execute_command( command, operationdetail, EXEC_CHECK_STATUS );
|
2004-11-17 06:00:25 -07:00
|
|
|
}
|
|
|
|
|
2010-10-19 13:35:53 -06:00
|
|
|
bool linux_swap::move( const Partition & partition_new
|
|
|
|
, const Partition & partition_old
|
|
|
|
, OperationDetail & operationdetail
|
|
|
|
)
|
|
|
|
{
|
2010-10-19 13:43:42 -06:00
|
|
|
//Since linux-swap does not contain data, do not actually move the partition
|
|
|
|
operationdetail .add_child(
|
|
|
|
OperationDetail(
|
|
|
|
/* TO TRANSLATORS: looks like Partition move action skipped because linux-swap file system does not contain data */
|
|
|
|
String::ucompose( _("Partition move action skipped because %1 file system does not contain data")
|
|
|
|
, Utils::get_filesystem_string( FS_LINUX_SWAP )
|
|
|
|
)
|
|
|
|
, STATUS_NONE
|
|
|
|
, FONT_ITALIC
|
|
|
|
)
|
|
|
|
) ;
|
|
|
|
|
2010-10-19 13:35:53 -06:00
|
|
|
return true ;
|
|
|
|
}
|
|
|
|
|
2013-01-30 19:35:52 -07:00
|
|
|
bool linux_swap::copy( const Partition & src_part,
|
|
|
|
Partition & dest_part,
|
2006-08-20 03:33:54 -06:00
|
|
|
OperationDetail & operationdetail )
|
2004-11-17 06:00:25 -07:00
|
|
|
{
|
2010-10-19 10:02:18 -06:00
|
|
|
//Since linux-swap does not contain data, do not actually copy the partition
|
|
|
|
operationdetail .add_child(
|
|
|
|
OperationDetail(
|
|
|
|
/* TO TRANSLATORS: looks like Partition copy action skipped because linux-swap file system does not contain data */
|
|
|
|
String::ucompose( _("Partition copy action skipped because %1 file system does not contain data")
|
|
|
|
, Utils::get_filesystem_string( FS_LINUX_SWAP )
|
|
|
|
)
|
|
|
|
, STATUS_NONE
|
|
|
|
, FONT_ITALIC
|
|
|
|
)
|
|
|
|
) ;
|
|
|
|
|
2006-05-23 15:17:34 -06:00
|
|
|
return true ;
|
2004-11-21 14:49:38 -07:00
|
|
|
}
|
|
|
|
|
2004-11-17 06:00:25 -07:00
|
|
|
} //GParted
|