xref: /openbmc/estoraged/include/estoraged.hpp (revision 043af59f)
1 #pragma once
2 
3 #include "cryptsetupInterface.hpp"
4 #include "filesystemInterface.hpp"
5 #include "util.hpp"
6 
7 #include <libcryptsetup.h>
8 
9 #include <sdbusplus/asio/object_server.hpp>
10 #include <sdbusplus/bus.hpp>
11 #include <sdbusplus/exception.hpp>
12 #include <sdbusplus/server/object.hpp>
13 #include <util.hpp>
14 #include <xyz/openbmc_project/Inventory/Item/Drive/server.hpp>
15 #include <xyz/openbmc_project/Inventory/Item/Volume/server.hpp>
16 
17 #include <filesystem>
18 #include <memory>
19 #include <string>
20 #include <string_view>
21 #include <vector>
22 
23 namespace estoraged
24 {
25 using estoraged::Cryptsetup;
26 using estoraged::Filesystem;
27 using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Drive;
28 using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Volume;
29 
30 /** @class eStoraged
31  *  @brief eStoraged object to manage a LUKS encrypted storage device.
32  */
33 class EStoraged
34 {
35   public:
36     /** @brief Constructor for eStoraged
37      *
38      *  @param[in] server - sdbusplus asio object server
39      *  @param[in] configPath - path of the config object from Entity Manager
40      *  @param[in] devPath - path to device file, e.g. /dev/mmcblk0
41      *  @param[in] luksName - name for the LUKS container
42      *  @param[in] size - size of the drive in bytes
43      *  @param[in] lifeTime - percent of lifetime remaining for a drive
44      *  @param[in] partNumber - part number for the storage device
45      *  @param[in] serialNumber - serial number for the storage device
46      *  @param[in] locationCode - location code for the storage device
47      *  @param[in] eraseMaxGeometry - max geometry to erase if it's specified
48      *  @param[in] eraseMinGeometry - min geometry to erase if it's specified
49      *  @param[in] cryptInterface - (optional) pointer to CryptsetupInterface
50      *    object
51      *  @param[in] fsInterface - (optional) pointer to FilesystemInterface
52      *    object
53      */
54     EStoraged(sdbusplus::asio::object_server& server,
55               const std::string& configPath, const std::string& devPath,
56               const std::string& luksName, uint64_t size, uint8_t lifeTime,
57               const std::string& partNumber, const std::string& serialNumber,
58               const std::string& locationCode, uint64_t eraseMaxGeometry,
59               uint64_t eraseMinGeometry,
60               std::unique_ptr<CryptsetupInterface> cryptInterface =
61                   std::make_unique<Cryptsetup>(),
62               std::unique_ptr<FilesystemInterface> fsInterface =
63                   std::make_unique<Filesystem>());
64 
65     /** @brief Destructor for eStoraged. */
66     ~EStoraged();
67 
68     EStoraged& operator=(const EStoraged&) = delete;
69     EStoraged(const EStoraged&) = delete;
70     EStoraged(EStoraged&&) = default;
71     EStoraged& operator=(EStoraged&&) = delete;
72 
73     /** @brief Format the LUKS encrypted device and create empty filesystem.
74      *
75      *  @param[in] password - password to set for the LUKS device.
76      *  @param[in] type - filesystem type, e.g. ext4
77      */
78     void formatLuks(const std::vector<uint8_t>& password,
79                     Volume::FilesystemType type);
80 
81     /** @brief Erase the contents of the storage device.
82      *
83      *  @param[in] eraseType - type of erase operation.
84      */
85     void erase(Volume::EraseMethod eraseType);
86 
87     /** @brief Unmount filesystem and lock the LUKS device.
88      */
89     void lock();
90 
91     /** @brief Unlock device and mount the filesystem.
92      *
93      *  @param[in] password - password for the LUKS device.
94      */
95     void unlock(std::vector<uint8_t> password);
96 
97     /** @brief Change the password for the LUKS device.
98      *
99      *  @param[in] oldPassword - old password for the LUKS device.
100      *  @param[in] newPassword - new password for the LUKS device.
101      */
102     void changePassword(const std::vector<uint8_t>& oldPassword,
103                         const std::vector<uint8_t>& newPassword);
104 
105     /** @brief Check if the LUKS device is currently locked. */
106     bool isLocked() const;
107 
108     /** @brief Get the mount point for the filesystem on the LUKS device. */
109     std::string_view getMountPoint() const;
110 
111     /** @brief Get the path to the mapped crypt device. */
112     std::string_view getCryptDevicePath() const;
113 
114   private:
115     /** @brief Full path of the device file, e.g. /dev/mmcblk0. */
116     std::string devPath;
117 
118     /** @brief Name of the LUKS container. */
119     std::string containerName;
120 
121     /** @brief Mount point for the filesystem. */
122     std::string mountPoint;
123 
124     /** @brief Max geometry to erase. */
125     uint64_t eraseMaxGeometry;
126 
127     /** @brief Min geometry to erase. */
128     uint64_t eraseMinGeometry;
129 
130     /** @brief Indicates whether the LUKS device is currently locked. */
131     bool lockedProperty{false};
132 
133     /** @brief Pointer to cryptsetup interface object.
134      *  @details This is used to mock out the cryptsetup functions.
135      */
136     std::unique_ptr<CryptsetupInterface> cryptIface;
137 
138     /** @brief Pointer to filesystem interface object.
139      *  @details This is used to mock out filesystem operations.
140      */
141     std::unique_ptr<FilesystemInterface> fsIface;
142 
143     /** @brief Path where the mapped crypt device gets created. */
144     const std::string cryptDevicePath;
145 
146     /** @brief D-Bus object server. */
147     sdbusplus::asio::object_server& objectServer;
148 
149     /** @brief D-Bus interface for the logical volume. */
150     std::shared_ptr<sdbusplus::asio::dbus_interface> volumeInterface;
151 
152     /** @brief D-Bus interface for the physical drive. */
153     std::shared_ptr<sdbusplus::asio::dbus_interface> driveInterface;
154 
155     /** @brief D-Bus interface for the location type of the drive. */
156     std::shared_ptr<sdbusplus::asio::dbus_interface> embeddedLocationInterface;
157 
158     /** @brief D-Bus interface for the location code of the drive. */
159     std::shared_ptr<sdbusplus::asio::dbus_interface> locationCodeInterface;
160 
161     /** @brief D-Bus interface for the asset information. */
162     std::shared_ptr<sdbusplus::asio::dbus_interface> assetInterface;
163 
164     /** @brief Association between chassis and drive. */
165     std::shared_ptr<sdbusplus::asio::dbus_interface> association;
166 
167     /** @brief Indicates whether the LUKS header is on the disk. */
168     Drive::DriveEncryptionState encryptionStatus{
169         Drive::DriveEncryptionState::Unknown};
170 
171     /** @brief Format LUKS encrypted device.
172      *
173      *  @param[in] password - password to set for the LUKS device.
174      */
175     void formatLuksDev(std::vector<uint8_t> password);
176 
177     /** @brief check the LUKS header, for devPath
178      *
179      *  @returns a CryptHandle to the LUKS drive
180      */
181     CryptHandle loadLuksHeader();
182 
183     /** @brief Unlock the device.
184      *
185      *  @param[in] password - password to activate the LUKS device.
186      */
187 
188     Drive::DriveEncryptionState findEncryptionStatus();
189 
190     void activateLuksDev(std::vector<uint8_t> password);
191 
192     /** @brief Create the filesystem on the LUKS device.
193      *  @details The LUKS device should already be activated, i.e. unlocked.
194      */
195     void createFilesystem();
196 
197     /** @brief Deactivate the LUKS device.
198      *  @details The filesystem is assumed to be unmounted already.
199      */
200     void deactivateLuksDev();
201 
202     /** @brief Mount the filesystem.
203      *  @details The filesystem should already exist and the LUKS device should
204      *  be unlocked already.
205      */
206     void mountFilesystem();
207 
208     /** @brief Unmount the filesystem. */
209     void unmountFilesystem();
210 };
211 
212 } // namespace estoraged
213