Advertisement
Guest User

Gparted LUKS v2

a guest
Aug 31st, 2012
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 13.16 KB | None | 0 0
  1. From 4cd726ba6add936d1f3de57d0ab1747717cba615 Mon Sep 17 00:00:00 2001
  2. From: Matthias Gehre <M.Gehre@gmx.de>
  3. Date: Fri, 31 Aug 2012 10:35:34 +0200
  4. Subject: [PATCH] Add initial luks support
  5.  
  6. Supported:
  7. 1. Obtain size of active luks mapping
  8. 2. Show name of mapping for device
  9. ---
  10. configure.in          |    2 +-
  11.  include/luks.h        |   57 +++++++++++++++
  12.  src/DialogFeatures.cc |    1 -
  13.  src/GParted_Core.cc   |   20 ++++--
  14.  src/Makefile.am       |    3 +
  15.  src/luks.cc           |  191 +++++++++++++++++++++++++++++++++++++++++++++++++
  16.  6 files changed, 265 insertions(+), 9 deletions(-)
  17.  create mode 100644 include/luks.h
  18.  create mode 100644 src/luks.cc
  19.  
  20. diff --git a/configure.in b/configure.in
  21. index 2bf4224..3702aec 100644
  22. --- a/configure.in
  23. +++ b/configure.in
  24. @@ -38,7 +38,7 @@ dnl checks for libs
  25.  dnl======================
  26.  AC_CHECK_LIB([uuid], [uuid_generate], [], AC_MSG_ERROR([*** uuid library (libuuid) not found]))
  27.  AC_CHECK_LIB([dl], [dlopen], [], AC_MSG_ERROR([*** dl library (libdl) not found]))
  28. -
  29. +AC_CHECK_LIB([cryptsetup],  [crypt_init], [], AC_MSG_ERROR([*** cryptsetup library (libcryptsetup) not found]))
  30.  
  31.  dnl libparted
  32.  LIBPARTED_VERSION=1.7.1
  33. diff --git a/include/luks.h b/include/luks.h
  34. new file mode 100644
  35. index 0000000..e3bc111
  36. --- /dev/null
  37. +++ b/include/luks.h
  38. @@ -0,0 +1,57 @@
  39. +/* Copyright (C) 2012 Matthias Gehre
  40. + *
  41. + *  This program is free software; you can redistribute it and/or modify
  42. + *  it under the terms of the GNU General Public License as published by
  43. + *  the Free Software Foundation; either version 2 of the License, or
  44. + *  (at your option) any later version.
  45. + *
  46. + *  This program is distributed in the hope that it will be useful,
  47. + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  48. + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  49. + *  GNU Library General Public License for more details.
  50. + *
  51. + *  You should have received a copy of the GNU General Public License
  52. + *  along with this program; if not, write to the Free Software
  53. + *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  54. + */
  55. +
  56. +
  57. +#ifndef LUKS_H_
  58. +#define LUKS_H_
  59. +
  60. +#include "../include/FileSystem.h"
  61. +
  62. +namespace GParted
  63. +{
  64. +
  65. +class luks : public FileSystem
  66. +{
  67. +public:
  68. +   /*
  69. +    * Tries to find the map for device created
  70. +    * by "cryptsetup luksOpen device map".
  71. +    * Append any errors to messages
  72. +    */
  73. +   static Glib::ustring find_map_by_device(Glib::ustring device, std::vector<Glib::ustring>& messages);
  74. +
  75. +   FS get_filesystem_support() ;
  76. +   void set_used_sectors( Partition & partition ) ;
  77. +   void read_label( Partition & partition ) ;
  78. +   bool write_label( const Partition & partition, OperationDetail & operationdetail ) ;
  79. +   void read_uuid( Partition & partition ) ;
  80. +   bool write_uuid( const Partition & partition, OperationDetail & operationdetail ) ;
  81. +   bool create( const Partition & new_partition, OperationDetail & operationdetail ) ;
  82. +   bool resize( const Partition & partition_new, OperationDetail & operationdetail, bool fill_partition = false ) ;
  83. +   bool move( const Partition & partition_new
  84. +            , const Partition & partition_old
  85. +            , OperationDetail & operationdetail
  86. +            ) ;
  87. +   bool copy( const Glib::ustring & src_part_path
  88. +            , const Glib::ustring & dest_part_path
  89. +            , OperationDetail & operationdetail ) ;
  90. +   bool check_repair( const Partition & partition, OperationDetail & operationdetail ) ;
  91. +};
  92. +
  93. +} //GParted
  94. +
  95. +#endif /*LUKS_H_*/
  96. diff --git a/src/DialogFeatures.cc b/src/DialogFeatures.cc
  97. index cc51f44..3424af3 100644
  98. --- a/src/DialogFeatures.cc
  99. +++ b/src/DialogFeatures.cc
  100. @@ -125,7 +125,6 @@ void DialogFeatures::load_filesystems( const std::vector<FS> & FILESYSTEMS )
  101.     {
  102.         //Skip luks and unknown because these are not file systems
  103.         if (
  104. -            FILESYSTEMS[ t ] .filesystem == GParted::FS_LUKS    ||
  105.              FILESYSTEMS[ t ] .filesystem == GParted::FS_UNKNOWN
  106.            )
  107.             continue ;
  108. diff --git a/src/GParted_Core.cc b/src/GParted_Core.cc
  109. index a664d2b..38053af 100644
  110. --- a/src/GParted_Core.cc
  111. +++ b/src/GParted_Core.cc
  112. @@ -39,6 +39,7 @@
  113.  #include "../include/fat16.h"
  114.  #include "../include/fat32.h"
  115.  #include "../include/linux_swap.h"
  116. +#include "../include/luks.h"
  117.  #include "../include/lvm2_pv.h"
  118.  #include "../include/reiserfs.h"
  119.  #include "../include/nilfs2.h"
  120. @@ -120,7 +121,7 @@ void GParted_Core::find_supported_filesystems()
  121.     FILESYSTEM_MAP[ FS_REISERFS ]   = new reiserfs() ;
  122.     FILESYSTEM_MAP[ FS_UFS ]    = new ufs() ;
  123.     FILESYSTEM_MAP[ FS_XFS ]    = new xfs() ;
  124. -   FILESYSTEM_MAP[ FS_LUKS ]   = NULL ;
  125. +   FILESYSTEM_MAP[ FS_LUKS ]   = new luks() ;
  126.     FILESYSTEM_MAP[ FS_UNKNOWN ]    = NULL ;
  127.  
  128.     FILESYSTEMS .clear() ;
  129. @@ -1018,7 +1019,8 @@ void GParted_Core::set_device_partitions( Device & device )
  130.                 else
  131.  #endif
  132.                     partition_is_busy = ped_partition_is_busy( lp_partition ) ||
  133. -                                       ( filesystem == GParted::FS_LVM2_PV && lvm2_pv_info .has_active_lvs( partition_path ) ) ;
  134. +                                       ( filesystem == GParted::FS_LVM2_PV && lvm2_pv_info .has_active_lvs( partition_path ) ) ||
  135. +                                       ( filesystem == GParted::FS_LUKS && !luks::find_map_by_device( partition_path, partition_temp. messages ) .empty() );
  136.  
  137.                 partition_temp .Set( device .get_path(),
  138.                              partition_path,
  139. @@ -1154,9 +1156,6 @@ GParted::FILESYSTEM GParted_Core::get_filesystem()
  140.  
  141.         if ( 0 == memcmp( magic1 , "LUKS\xBA\xBE", 6 ) )
  142.         {
  143. -           temp = _( "Linux Unified Key Setup encryption is not yet supported." ) ;
  144. -           temp += "\n" ;
  145. -           partition_temp .messages .push_back( temp ) ;
  146.             return GParted::FS_LUKS ;
  147.         }
  148.     }
  149. @@ -1489,6 +1488,12 @@ void GParted_Core::set_mountpoints( std::vector<Partition> & partitions )
  150.             if ( ! vgname .empty() )
  151.                 partitions[ t ] .add_mountpoint( vgname ) ;
  152.         }
  153. +       else if ( partitions[ t ] .filesystem == GParted::FS_LUKS )
  154. +       {
  155. +           Glib::ustring map_name = luks::find_map_by_device( partitions[t].get_path(), partitions[ t ] .messages );
  156. +           if ( ! map_name .empty() )
  157. +               partitions[ t ] .add_mountpoint( map_name ) ;
  158. +       }
  159.     }
  160.  }
  161.    
  162. @@ -1499,14 +1504,15 @@ void GParted_Core::set_used_sectors( std::vector<Partition> & partitions )
  163.     for ( unsigned int t = 0 ; t < partitions .size() ; t++ )
  164.     {
  165.         if ( partitions[ t ] .filesystem != GParted::FS_LINUX_SWAP &&
  166. -            partitions[ t ] .filesystem != GParted::FS_LUKS       &&
  167.              partitions[ t ] .filesystem != GParted::FS_UNKNOWN
  168.            )
  169.         {
  170.             if ( partitions[ t ] .type == GParted::TYPE_PRIMARY ||
  171.                  partitions[ t ] .type == GParted::TYPE_LOGICAL )
  172.             {
  173. -               if ( partitions[ t ] .busy && partitions[t] .filesystem != GParted::FS_LVM2_PV )
  174. +               if ( partitions[ t ] .busy &&
  175. +                    partitions[t] .filesystem != GParted::FS_LVM2_PV &&
  176. +                    partitions[t] .filesystem != GParted::FS_LUKS )
  177.                 {
  178.                     if ( partitions[ t ] .get_mountpoints() .size() > 0  )
  179.                     {
  180. diff --git a/src/Makefile.am b/src/Makefile.am
  181. index 7c65fb2..44c67fd 100644
  182. --- a/src/Makefile.am
  183. +++ b/src/Makefile.am
  184. @@ -59,6 +59,7 @@ gpartedbin_SOURCES = \
  185.     hfsplus.cc          \
  186.     jfs.cc              \
  187.     linux_swap.cc           \
  188. +   luks.cc             \
  189.     lvm2_pv.cc          \
  190.     main.cc             \
  191.     nilfs2.cc           \
  192. @@ -74,5 +75,7 @@ if USE_LIBPARTED_FS_RESIZE_LIBRARY
  193.  gpartedbin_LDFLAGS += -lparted-fs-resize
  194.  endif
  195.  
  196. +gpartedbin_LDFLAGS += -lcryptsetup
  197. +
  198.  gpartedbin_LDADD = $(GTHREAD_LIBS) $(GTKMM_LIBS)
  199.  
  200. diff --git a/src/luks.cc b/src/luks.cc
  201. new file mode 100644
  202. index 0000000..fecc456
  203. --- /dev/null
  204. +++ b/src/luks.cc
  205. @@ -0,0 +1,191 @@
  206. +/* Copyright (C) 2012 Matthias Gehre
  207. + *
  208. + *  This program is free software; you can redistribute it and/or modify
  209. + *  it under the terms of the GNU General Public License as published by
  210. + *  the Free Software Foundation; either version 2 of the License, or
  211. + *  (at your option) any later version.
  212. + *
  213. + *  This program is distributed in the hope that it will be useful,
  214. + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  215. + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  216. + *  GNU Library General Public License for more details.
  217. + *
  218. + *  You should have received a copy of the GNU General Public License
  219. + *  along with this program; if not, write to the Free Software
  220. + *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  221. + */
  222. +
  223. +#include <stdlib.h>
  224. +#include <libcryptsetup.h>
  225. +#include "../include/luks.h"
  226. +
  227. +/*
  228. + * The luks "filesystem" does not have a 'size' written on disk. It always uses all of its underlaying block-device (e.g. partition)
  229. + * when luksOpen'ed.
  230. + * You can resize an active luks device. But that information is lost when the device is luksClose'd. luksOpen'ing it
  231. + * again will show the size of the underlaying block-device again.
  232. + * 1. Thus resize is mainly useful when the size of the underlaying device grow after luksOpen'ing and we want to
  233. + *    let luks use the additional space. (It would do so automatically when luksClose'd followed by luksOpen'ed.)
  234. + * 2. Shrinking luks to make place to shrink the underlaying device afterwards.
  235. + */
  236. +
  237. +namespace GParted
  238. +{
  239. +
  240. +FS luks::get_filesystem_support()
  241. +{
  242. +   FS fs ;
  243. +   fs .filesystem = FS_LUKS ;
  244. +
  245. +   fs .read = FS::EXTERNAL ;
  246. +
  247. +   return fs ;
  248. +}
  249. +
  250. +/*
  251. + * Find a map for the given device.
  252. + * I.e. if /dev/sda5 caries the LUKS data and is currently
  253. + * mapped to /dev/mapper/home (by cryptsetup luksOpen /dev/sda5 home)
  254. + * then find_map("/dev/sda5") will return "home".
  255. + */
  256. +Glib::ustring luks::find_map_by_device(Glib::ustring device, std::vector<Glib::ustring>& messages)
  257. +{
  258. +   int r;
  259. +   /* Find all active crypt mappings */
  260. +   Glib::ustring output, error ;
  261. +   std::vector<Glib::ustring> crypts ;
  262. +   if ( ! Utils::execute_command( "dmsetup ls --target crypt", output, error, true ) && error.empty())
  263. +   {
  264. +       Utils::tokenize( output, crypts, "\n" ) ;
  265. +       for(size_t i=0; i< crypts.size(); ++i)
  266. +       {
  267. +           std::vector<Glib::ustring> entrys;
  268. +           Utils::split( crypts[i], entrys, "\t" ) ;
  269. +           if(entrys.empty())
  270. +           {
  271. +               messages .push_back( Glib::ustring::compose("Error parsing 'dmsetup ls --target' crypt output, line: %1!", crypts[i]) ) ;
  272. +               continue;
  273. +           }
  274. +           Glib::ustring map_name = entrys[0];
  275. +
  276. +           crypt_device* cd;
  277. +           r = crypt_init_by_name(&cd, map_name.c_str());
  278. +           if( r )
  279. +           {
  280. +               messages .push_back( Glib::ustring::compose("Error: crypt_init_by_name(%1) returned %2", map_name, r) ) ;
  281. +               continue;
  282. +           }
  283. +           const char* devname = crypt_get_device_name(cd);
  284. +           if( devname == NULL )
  285. +           {
  286. +               crypt_free(cd);
  287. +               messages .push_back( Glib::ustring::compose("Error: crypt_get_device_name(%1) returned %2", map_name, r) ) ;
  288. +               continue;
  289. +           }
  290. +
  291. +           if( strcmp(devname, device.c_str()) == 0 )
  292. +               return map_name;
  293. +
  294. +           crypt_free(cd);
  295. +       }
  296. +   }
  297. +   else
  298. +   {
  299. +       messages .push_back( Glib::ustring::compose("Error: Executing 'dmsetup ls --target crypt' failed with %1", error) ) ;
  300. +   }
  301. +   return "";
  302. +}
  303. +
  304. +/*
  305. + * Returns the size of the whole LUKS partition,
  306. + * i.e. header size (cad.offset) + data size (cad.size)
  307. + */
  308. +Sector get_partition_size(Glib::ustring map_name, std::vector<Glib::ustring>& messages)
  309. +{
  310. +   crypt_active_device cad;
  311. +   int r = crypt_get_active_device(NULL, map_name.c_str(), &cad);
  312. +   if( r )
  313. +   {
  314. +       messages .push_back( Glib::ustring::compose("Error: crypt_get_active_device returned %1!", r) ) ;
  315. +       return -1;
  316. +   }
  317. +   return cad.offset + cad.size;
  318. +}
  319. +
  320. +void luks::set_used_sectors( Partition & partition )
  321. +{
  322. +    partition .set_sector_usage( -1, 0 );
  323. +   crypt_set_debug_level(-1);
  324. +
  325. +   Glib::ustring map_name = find_map_by_device( partition .get_path(). c_str(), partition .messages );
  326. +
  327. +   if( map_name.empty() )
  328. +   {
  329. +       //Currently unmapped, so it spans the whole underlaying partition
  330. +       partition .set_sector_usage( -1, 0 );
  331. +   }
  332. +   else
  333. +   {
  334. +       Sector size = get_partition_size( map_name, partition .messages );
  335. +       if( size == -1 )
  336. +       {
  337. +           partition .set_sector_usage( -1, 0 );
  338. +           partition .messages .push_back( "Error obtaining partition size" ) ;
  339. +       }
  340. +       else
  341. +       {
  342. +           partition .set_sector_usage( size, 0 );
  343. +       }
  344. +   }
  345. +}
  346. +
  347. +void luks::read_label( Partition & partition )
  348. +{
  349. +   return ;
  350. +}
  351. +
  352. +bool luks::write_label( const Partition & partition, OperationDetail & operationdetail )
  353. +{
  354. +   return true ;
  355. +}
  356. +
  357. +void luks::read_uuid( Partition & partition )
  358. +{
  359. +}
  360. +
  361. +bool luks::write_uuid( const Partition & partition, OperationDetail & operationdetail )
  362. +{
  363. +   return true ;
  364. +}
  365. +
  366. +bool luks::create( const Partition & new_partition, OperationDetail & operationdetail )
  367. +{
  368. +   return true ;
  369. +}
  370. +
  371. +bool luks::resize( const Partition & partition_new, OperationDetail & operationdetail, bool fill_partition )
  372. +{
  373. +   return true ;
  374. +}
  375. +
  376. +bool luks::move( const Partition & partition_new
  377. +                  , const Partition & partition_old
  378. +                  , OperationDetail & operationdetail
  379. +               )
  380. +{
  381. +   return true ;
  382. +}
  383. +
  384. +bool luks::copy( const Glib::ustring & src_part_path
  385. +                  , const Glib::ustring & dest_part_path
  386. +                  , OperationDetail & operationdetail )
  387. +{
  388. +   return true ;
  389. +}
  390. +
  391. +bool luks::check_repair( const Partition & partition, OperationDetail & operationdetail )
  392. +{
  393. +   return true ;
  394. +}
  395. +
  396. +} //GParted
  397. --
  398. 1.7.9.5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement