Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- From 4cd726ba6add936d1f3de57d0ab1747717cba615 Mon Sep 17 00:00:00 2001
- From: Matthias Gehre <M.Gehre@gmx.de>
- Date: Fri, 31 Aug 2012 10:35:34 +0200
- Subject: [PATCH] Add initial luks support
- Supported:
- 1. Obtain size of active luks mapping
- 2. Show name of mapping for device
- ---
- configure.in | 2 +-
- include/luks.h | 57 +++++++++++++++
- src/DialogFeatures.cc | 1 -
- src/GParted_Core.cc | 20 ++++--
- src/Makefile.am | 3 +
- src/luks.cc | 191 +++++++++++++++++++++++++++++++++++++++++++++++++
- 6 files changed, 265 insertions(+), 9 deletions(-)
- create mode 100644 include/luks.h
- create mode 100644 src/luks.cc
- diff --git a/configure.in b/configure.in
- index 2bf4224..3702aec 100644
- --- a/configure.in
- +++ b/configure.in
- @@ -38,7 +38,7 @@ dnl checks for libs
- dnl======================
- AC_CHECK_LIB([uuid], [uuid_generate], [], AC_MSG_ERROR([*** uuid library (libuuid) not found]))
- AC_CHECK_LIB([dl], [dlopen], [], AC_MSG_ERROR([*** dl library (libdl) not found]))
- -
- +AC_CHECK_LIB([cryptsetup], [crypt_init], [], AC_MSG_ERROR([*** cryptsetup library (libcryptsetup) not found]))
- dnl libparted
- LIBPARTED_VERSION=1.7.1
- diff --git a/include/luks.h b/include/luks.h
- new file mode 100644
- index 0000000..e3bc111
- --- /dev/null
- +++ b/include/luks.h
- @@ -0,0 +1,57 @@
- +/* Copyright (C) 2012 Matthias Gehre
- + *
- + * 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
- + * GNU Library General Public License for more details.
- + *
- + * You should have received a copy of the GNU General Public License
- + * along with this program; if not, write to the Free Software
- + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- + */
- +
- +
- +#ifndef LUKS_H_
- +#define LUKS_H_
- +
- +#include "../include/FileSystem.h"
- +
- +namespace GParted
- +{
- +
- +class luks : public FileSystem
- +{
- +public:
- + /*
- + * Tries to find the map for device created
- + * by "cryptsetup luksOpen device map".
- + * Append any errors to messages
- + */
- + static Glib::ustring find_map_by_device(Glib::ustring device, std::vector<Glib::ustring>& messages);
- +
- + FS get_filesystem_support() ;
- + void set_used_sectors( Partition & partition ) ;
- + void read_label( Partition & partition ) ;
- + bool write_label( const Partition & partition, OperationDetail & operationdetail ) ;
- + void read_uuid( Partition & partition ) ;
- + bool write_uuid( const Partition & partition, OperationDetail & operationdetail ) ;
- + bool create( const Partition & new_partition, OperationDetail & operationdetail ) ;
- + bool resize( const Partition & partition_new, OperationDetail & operationdetail, bool fill_partition = false ) ;
- + bool move( const Partition & partition_new
- + , const Partition & partition_old
- + , OperationDetail & operationdetail
- + ) ;
- + bool copy( const Glib::ustring & src_part_path
- + , const Glib::ustring & dest_part_path
- + , OperationDetail & operationdetail ) ;
- + bool check_repair( const Partition & partition, OperationDetail & operationdetail ) ;
- +};
- +
- +} //GParted
- +
- +#endif /*LUKS_H_*/
- diff --git a/src/DialogFeatures.cc b/src/DialogFeatures.cc
- index cc51f44..3424af3 100644
- --- a/src/DialogFeatures.cc
- +++ b/src/DialogFeatures.cc
- @@ -125,7 +125,6 @@ void DialogFeatures::load_filesystems( const std::vector<FS> & FILESYSTEMS )
- {
- //Skip luks and unknown because these are not file systems
- if (
- - FILESYSTEMS[ t ] .filesystem == GParted::FS_LUKS ||
- FILESYSTEMS[ t ] .filesystem == GParted::FS_UNKNOWN
- )
- continue ;
- diff --git a/src/GParted_Core.cc b/src/GParted_Core.cc
- index a664d2b..38053af 100644
- --- a/src/GParted_Core.cc
- +++ b/src/GParted_Core.cc
- @@ -39,6 +39,7 @@
- #include "../include/fat16.h"
- #include "../include/fat32.h"
- #include "../include/linux_swap.h"
- +#include "../include/luks.h"
- #include "../include/lvm2_pv.h"
- #include "../include/reiserfs.h"
- #include "../include/nilfs2.h"
- @@ -120,7 +121,7 @@ void GParted_Core::find_supported_filesystems()
- FILESYSTEM_MAP[ FS_REISERFS ] = new reiserfs() ;
- FILESYSTEM_MAP[ FS_UFS ] = new ufs() ;
- FILESYSTEM_MAP[ FS_XFS ] = new xfs() ;
- - FILESYSTEM_MAP[ FS_LUKS ] = NULL ;
- + FILESYSTEM_MAP[ FS_LUKS ] = new luks() ;
- FILESYSTEM_MAP[ FS_UNKNOWN ] = NULL ;
- FILESYSTEMS .clear() ;
- @@ -1018,7 +1019,8 @@ void GParted_Core::set_device_partitions( Device & device )
- else
- #endif
- partition_is_busy = ped_partition_is_busy( lp_partition ) ||
- - ( filesystem == GParted::FS_LVM2_PV && lvm2_pv_info .has_active_lvs( partition_path ) ) ;
- + ( filesystem == GParted::FS_LVM2_PV && lvm2_pv_info .has_active_lvs( partition_path ) ) ||
- + ( filesystem == GParted::FS_LUKS && !luks::find_map_by_device( partition_path, partition_temp. messages ) .empty() );
- partition_temp .Set( device .get_path(),
- partition_path,
- @@ -1154,9 +1156,6 @@ GParted::FILESYSTEM GParted_Core::get_filesystem()
- if ( 0 == memcmp( magic1 , "LUKS\xBA\xBE", 6 ) )
- {
- - temp = _( "Linux Unified Key Setup encryption is not yet supported." ) ;
- - temp += "\n" ;
- - partition_temp .messages .push_back( temp ) ;
- return GParted::FS_LUKS ;
- }
- }
- @@ -1489,6 +1488,12 @@ void GParted_Core::set_mountpoints( std::vector<Partition> & partitions )
- if ( ! vgname .empty() )
- partitions[ t ] .add_mountpoint( vgname ) ;
- }
- + else if ( partitions[ t ] .filesystem == GParted::FS_LUKS )
- + {
- + Glib::ustring map_name = luks::find_map_by_device( partitions[t].get_path(), partitions[ t ] .messages );
- + if ( ! map_name .empty() )
- + partitions[ t ] .add_mountpoint( map_name ) ;
- + }
- }
- }
- @@ -1499,14 +1504,15 @@ void GParted_Core::set_used_sectors( std::vector<Partition> & partitions )
- for ( unsigned int t = 0 ; t < partitions .size() ; t++ )
- {
- if ( partitions[ t ] .filesystem != GParted::FS_LINUX_SWAP &&
- - partitions[ t ] .filesystem != GParted::FS_LUKS &&
- partitions[ t ] .filesystem != GParted::FS_UNKNOWN
- )
- {
- if ( partitions[ t ] .type == GParted::TYPE_PRIMARY ||
- partitions[ t ] .type == GParted::TYPE_LOGICAL )
- {
- - if ( partitions[ t ] .busy && partitions[t] .filesystem != GParted::FS_LVM2_PV )
- + if ( partitions[ t ] .busy &&
- + partitions[t] .filesystem != GParted::FS_LVM2_PV &&
- + partitions[t] .filesystem != GParted::FS_LUKS )
- {
- if ( partitions[ t ] .get_mountpoints() .size() > 0 )
- {
- diff --git a/src/Makefile.am b/src/Makefile.am
- index 7c65fb2..44c67fd 100644
- --- a/src/Makefile.am
- +++ b/src/Makefile.am
- @@ -59,6 +59,7 @@ gpartedbin_SOURCES = \
- hfsplus.cc \
- jfs.cc \
- linux_swap.cc \
- + luks.cc \
- lvm2_pv.cc \
- main.cc \
- nilfs2.cc \
- @@ -74,5 +75,7 @@ if USE_LIBPARTED_FS_RESIZE_LIBRARY
- gpartedbin_LDFLAGS += -lparted-fs-resize
- endif
- +gpartedbin_LDFLAGS += -lcryptsetup
- +
- gpartedbin_LDADD = $(GTHREAD_LIBS) $(GTKMM_LIBS)
- diff --git a/src/luks.cc b/src/luks.cc
- new file mode 100644
- index 0000000..fecc456
- --- /dev/null
- +++ b/src/luks.cc
- @@ -0,0 +1,191 @@
- +/* Copyright (C) 2012 Matthias Gehre
- + *
- + * 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
- + * GNU Library General Public License for more details.
- + *
- + * You should have received a copy of the GNU General Public License
- + * along with this program; if not, write to the Free Software
- + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- + */
- +
- +#include <stdlib.h>
- +#include <libcryptsetup.h>
- +#include "../include/luks.h"
- +
- +/*
- + * The luks "filesystem" does not have a 'size' written on disk. It always uses all of its underlaying block-device (e.g. partition)
- + * when luksOpen'ed.
- + * You can resize an active luks device. But that information is lost when the device is luksClose'd. luksOpen'ing it
- + * again will show the size of the underlaying block-device again.
- + * 1. Thus resize is mainly useful when the size of the underlaying device grow after luksOpen'ing and we want to
- + * let luks use the additional space. (It would do so automatically when luksClose'd followed by luksOpen'ed.)
- + * 2. Shrinking luks to make place to shrink the underlaying device afterwards.
- + */
- +
- +namespace GParted
- +{
- +
- +FS luks::get_filesystem_support()
- +{
- + FS fs ;
- + fs .filesystem = FS_LUKS ;
- +
- + fs .read = FS::EXTERNAL ;
- +
- + return fs ;
- +}
- +
- +/*
- + * Find a map for the given device.
- + * I.e. if /dev/sda5 caries the LUKS data and is currently
- + * mapped to /dev/mapper/home (by cryptsetup luksOpen /dev/sda5 home)
- + * then find_map("/dev/sda5") will return "home".
- + */
- +Glib::ustring luks::find_map_by_device(Glib::ustring device, std::vector<Glib::ustring>& messages)
- +{
- + int r;
- + /* Find all active crypt mappings */
- + Glib::ustring output, error ;
- + std::vector<Glib::ustring> crypts ;
- + if ( ! Utils::execute_command( "dmsetup ls --target crypt", output, error, true ) && error.empty())
- + {
- + Utils::tokenize( output, crypts, "\n" ) ;
- + for(size_t i=0; i< crypts.size(); ++i)
- + {
- + std::vector<Glib::ustring> entrys;
- + Utils::split( crypts[i], entrys, "\t" ) ;
- + if(entrys.empty())
- + {
- + messages .push_back( Glib::ustring::compose("Error parsing 'dmsetup ls --target' crypt output, line: %1!", crypts[i]) ) ;
- + continue;
- + }
- + Glib::ustring map_name = entrys[0];
- +
- + crypt_device* cd;
- + r = crypt_init_by_name(&cd, map_name.c_str());
- + if( r )
- + {
- + messages .push_back( Glib::ustring::compose("Error: crypt_init_by_name(%1) returned %2", map_name, r) ) ;
- + continue;
- + }
- + const char* devname = crypt_get_device_name(cd);
- + if( devname == NULL )
- + {
- + crypt_free(cd);
- + messages .push_back( Glib::ustring::compose("Error: crypt_get_device_name(%1) returned %2", map_name, r) ) ;
- + continue;
- + }
- +
- + if( strcmp(devname, device.c_str()) == 0 )
- + return map_name;
- +
- + crypt_free(cd);
- + }
- + }
- + else
- + {
- + messages .push_back( Glib::ustring::compose("Error: Executing 'dmsetup ls --target crypt' failed with %1", error) ) ;
- + }
- + return "";
- +}
- +
- +/*
- + * Returns the size of the whole LUKS partition,
- + * i.e. header size (cad.offset) + data size (cad.size)
- + */
- +Sector get_partition_size(Glib::ustring map_name, std::vector<Glib::ustring>& messages)
- +{
- + crypt_active_device cad;
- + int r = crypt_get_active_device(NULL, map_name.c_str(), &cad);
- + if( r )
- + {
- + messages .push_back( Glib::ustring::compose("Error: crypt_get_active_device returned %1!", r) ) ;
- + return -1;
- + }
- + return cad.offset + cad.size;
- +}
- +
- +void luks::set_used_sectors( Partition & partition )
- +{
- + partition .set_sector_usage( -1, 0 );
- + crypt_set_debug_level(-1);
- +
- + Glib::ustring map_name = find_map_by_device( partition .get_path(). c_str(), partition .messages );
- +
- + if( map_name.empty() )
- + {
- + //Currently unmapped, so it spans the whole underlaying partition
- + partition .set_sector_usage( -1, 0 );
- + }
- + else
- + {
- + Sector size = get_partition_size( map_name, partition .messages );
- + if( size == -1 )
- + {
- + partition .set_sector_usage( -1, 0 );
- + partition .messages .push_back( "Error obtaining partition size" ) ;
- + }
- + else
- + {
- + partition .set_sector_usage( size, 0 );
- + }
- + }
- +}
- +
- +void luks::read_label( Partition & partition )
- +{
- + return ;
- +}
- +
- +bool luks::write_label( const Partition & partition, OperationDetail & operationdetail )
- +{
- + return true ;
- +}
- +
- +void luks::read_uuid( Partition & partition )
- +{
- +}
- +
- +bool luks::write_uuid( const Partition & partition, OperationDetail & operationdetail )
- +{
- + return true ;
- +}
- +
- +bool luks::create( const Partition & new_partition, OperationDetail & operationdetail )
- +{
- + return true ;
- +}
- +
- +bool luks::resize( const Partition & partition_new, OperationDetail & operationdetail, bool fill_partition )
- +{
- + return true ;
- +}
- +
- +bool luks::move( const Partition & partition_new
- + , const Partition & partition_old
- + , OperationDetail & operationdetail
- + )
- +{
- + return true ;
- +}
- +
- +bool luks::copy( const Glib::ustring & src_part_path
- + , const Glib::ustring & dest_part_path
- + , OperationDetail & operationdetail )
- +{
- + return true ;
- +}
- +
- +bool luks::check_repair( const Partition & partition, OperationDetail & operationdetail )
- +{
- + return true ;
- +}
- +
- +} //GParted
- --
- 1.7.9.5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement