xref: /openbmc/estoraged/include/estoraged.hpp (revision e7f6975e)
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] driveType - type of drive, e.g. HDD vs SSD
50      *  @param[in] cryptInterface - (optional) pointer to CryptsetupInterface
51      *    object
52      *  @param[in] fsInterface - (optional) pointer to FilesystemInterface
53      *    object
54      */
55     EStoraged(sdbusplus::asio::object_server& server,
56               const std::string& configPath, const std::string& devPath,
57               const std::string& luksName, uint64_t size, uint8_t lifeTime,
58               const std::string& partNumber, const std::string& serialNumber,
59               const std::string& locationCode, uint64_t eraseMaxGeometry,
60               uint64_t eraseMinGeometry, const std::string& driveType,
61               std::unique_ptr<CryptsetupInterface> cryptInterface =
62                   std::make_unique<Cryptsetup>(),
63               std::unique_ptr<FilesystemInterface> fsInterface =
64                   std::make_unique<Filesystem>());
65 
66     /** @brief Destructor for eStoraged. */
67     ~EStoraged();
68 
69     EStoraged& operator=(const EStoraged&) = delete;
70     EStoraged(const EStoraged&) = delete;
71     EStoraged(EStoraged&&) = default;
72     EStoraged& operator=(EStoraged&&) = delete;
73 
74     /** @brief Format the LUKS encrypted device and create empty filesystem.
75      *
76      *  @param[in] password - password to set for the LUKS device.
77      *  @param[in] type - filesystem type, e.g. ext4
78      */
79     void formatLuks(const std::vector<uint8_t>& password,
80                     Volume::FilesystemType type);
81 
82     /** @brief Erase the contents of the storage device.
83      *
84      *  @param[in] eraseType - type of erase operation.
85      */
86     void erase(Volume::EraseMethod eraseType);
87 
88     /** @brief Unmount filesystem and lock the LUKS device.
89      */
90     void lock();
91 
92     /** @brief Unlock device and mount the filesystem.
93      *
94      *  @param[in] password - password for the LUKS device.
95      */
96     void unlock(std::vector<uint8_t> password);
97 
98     /** @brief Change the password for the LUKS device.
99      *
100      *  @param[in] oldPassword - old password for the LUKS device.
101      *  @param[in] newPassword - new password for the LUKS device.
102      */
103     void changePassword(const std::vector<uint8_t>& oldPassword,
104                         const std::vector<uint8_t>& newPassword);
105 
106     /** @brief Check if the LUKS device is currently locked. */
107     bool isLocked() const;
108 
109     /** @brief Get the mount point for the filesystem on the LUKS device. */
110     std::string_view getMountPoint() const;
111 
112     /** @brief Get the path to the mapped crypt device. */
113     std::string_view getCryptDevicePath() const;
114 
115   private:
116     /** @brief Full path of the device file, e.g. /dev/mmcblk0. */
117     std::string devPath;
118 
119     /** @brief Name of the LUKS container. */
120     std::string containerName;
121 
122     /** @brief Mount point for the filesystem. */
123     std::string mountPoint;
124 
125     /** @brief Max geometry to erase. */
126     uint64_t eraseMaxGeometry;
127 
128     /** @brief Min geometry to erase. */
129     uint64_t eraseMinGeometry;
130 
131     /** @brief Indicates whether the LUKS device is currently locked. */
132     bool lockedProperty{false};
133 
134     /** @brief Pointer to cryptsetup interface object.
135      *  @details This is used to mock out the cryptsetup functions.
136      */
137     std::unique_ptr<CryptsetupInterface> cryptIface;
138 
139     /** @brief Pointer to filesystem interface object.
140      *  @details This is used to mock out filesystem operations.
141      */
142     std::unique_ptr<FilesystemInterface> fsIface;
143 
144     /** @brief Path where the mapped crypt device gets created. */
145     const std::string cryptDevicePath;
146 
147     /** @brief D-Bus object server. */
148     sdbusplus::asio::object_server& objectServer;
149 
150     /** @brief D-Bus interface for the logical volume. */
151     std::shared_ptr<sdbusplus::asio::dbus_interface> volumeInterface;
152 
153     /** @brief D-Bus interface for the physical drive. */
154     std::shared_ptr<sdbusplus::asio::dbus_interface> driveInterface;
155 
156     /** @brief D-Bus interface for the location type of the drive. */
157     std::shared_ptr<sdbusplus::asio::dbus_interface> embeddedLocationInterface;
158 
159     /** @brief D-Bus interface for the location code of the drive. */
160     std::shared_ptr<sdbusplus::asio::dbus_interface> locationCodeInterface;
161 
162     /** @brief D-Bus interface for the asset information. */
163     std::shared_ptr<sdbusplus::asio::dbus_interface> assetInterface;
164 
165     /** @brief Association between chassis and drive. */
166     std::shared_ptr<sdbusplus::asio::dbus_interface> association;
167 
168     /** @brief Indicates whether the LUKS header is on the disk. */
169     Drive::DriveEncryptionState encryptionStatus{
170         Drive::DriveEncryptionState::Unknown};
171 
172     /** @brief Format LUKS encrypted device.
173      *
174      *  @param[in] password - password to set for the LUKS device.
175      */
176     void formatLuksDev(std::vector<uint8_t> password);
177 
178     /** @brief check the LUKS header, for devPath
179      *
180      *  @returns a CryptHandle to the LUKS drive
181      */
182     CryptHandle loadLuksHeader();
183 
184     /** @brief Unlock the device.
185      *
186      *  @param[in] password - password to activate the LUKS device.
187      */
188 
189     Drive::DriveEncryptionState findEncryptionStatus();
190 
191     void activateLuksDev(std::vector<uint8_t> password);
192 
193     /** @brief Create the filesystem on the LUKS device.
194      *  @details The LUKS device should already be activated, i.e. unlocked.
195      */
196     void createFilesystem();
197 
198     /** @brief Deactivate the LUKS device.
199      *  @details The filesystem is assumed to be unmounted already.
200      */
201     void deactivateLuksDev();
202 
203     /** @brief Mount the filesystem.
204      *  @details The filesystem should already exist and the LUKS device should
205      *  be unlocked already.
206      */
207     void mountFilesystem();
208 
209     /** @brief Unmount the filesystem. */
210     void unmountFilesystem();
211 };
212 
213 } // namespace estoraged
214