1 // Copyright 2018 The Chromium OS Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef LIBBRILLO_BRILLO_BLKDEV_UTILS_DEVICE_MAPPER_H_ 6 #define LIBBRILLO_BRILLO_BLKDEV_UTILS_DEVICE_MAPPER_H_ 7 8 #include <functional> 9 #include <memory> 10 #include <string> 11 12 #include <base/bind.h> 13 #include <base/callback.h> 14 #include <base/files/file_path.h> 15 #include <brillo/blkdev_utils/device_mapper_task.h> 16 17 namespace brillo { 18 19 // DevmapperTable manages device parameters. Contains helper 20 // functions to parse results from dmsetup. Since the table parameters 21 // may contain sensitive data eg. dm-crypt keys, we use SecureBlobs for 22 // the table parameters and as the table output format. 23 24 class BRILLO_EXPORT DevmapperTable { 25 public: 26 // Create table from table parameters. 27 // Useful for setting up devices. 28 DevmapperTable(uint64_t start, 29 uint64_t size, 30 const std::string& type, 31 const SecureBlob& parameters); 32 33 ~DevmapperTable() = default; 34 35 // Returns the table as a SecureBlob. 36 SecureBlob ToSecureBlob(); 37 38 // Getters for table components. GetStart()39 uint64_t GetStart() const { return start_; } GetSize()40 uint64_t GetSize() const { return size_; } GetType()41 std::string GetType() const { return type_; } GetParameters()42 SecureBlob GetParameters() const { return parameters_; } 43 44 // Create table from table blob. 45 // Useful for parsing output from dmsetup. 46 // Using a static function to surface errors in parsing the blob. 47 static DevmapperTable CreateTableFromSecureBlob(const SecureBlob& table); 48 49 // dm-crypt specific functions: 50 // ---------------------------- 51 // Extract key from (crypt) table. 52 SecureBlob CryptGetKey(); 53 54 // Create crypt parameters . 55 // Useful for parsing output from dmsetup. 56 // Using a static function to surface errors in parsing the blob. 57 static SecureBlob CryptCreateParameters(const std::string& cipher, 58 const SecureBlob& encryption_key, 59 const int iv_offset, 60 const base::FilePath& device, 61 int device_offset, 62 bool allow_discard); 63 64 private: 65 const uint64_t start_; 66 const uint64_t size_; 67 const std::string type_; 68 const SecureBlob parameters_; 69 }; 70 71 // DevmapperTask is an abstract class so we wrap it in a unique_ptr. 72 using DevmapperTaskFactory = 73 base::Callback<std::unique_ptr<DevmapperTask>(int)>; 74 75 // DeviceMapper handles the creation and removal of dm devices. 76 class BRILLO_EXPORT DeviceMapper { 77 public: 78 // Default constructor: sets up real devmapper devices. 79 DeviceMapper(); 80 81 // Set a non-default dm task factory. 82 explicit DeviceMapper(const DevmapperTaskFactory& factory); 83 84 // Default destructor. 85 ~DeviceMapper() = default; 86 87 // Sets up device with table on /dev/mapper/<name>. 88 // Parameters 89 // name - Name of the devmapper device. 90 // table - Table for the devmapper device. 91 bool Setup(const std::string& name, const DevmapperTable& table); 92 93 // Removes device. 94 // Parameters 95 // name - Name of the devmapper device. 96 bool Remove(const std::string& device); 97 98 // Returns table for device. 99 // Parameters 100 // name - Name of the devmapper device. 101 DevmapperTable GetTable(const std::string& name); 102 103 // Clears table for device. 104 // Parameters 105 // name - Name of the devmapper device. 106 bool WipeTable(const std::string& name); 107 108 private: 109 // Devmapper task factory. 110 DevmapperTaskFactory dm_task_factory_; 111 DISALLOW_COPY_AND_ASSIGN(DeviceMapper); 112 }; 113 114 } // namespace brillo 115 116 #endif // LIBBRILLO_BRILLO_BLKDEV_UTILS_DEVICE_MAPPER_H_ 117