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