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