blob: 57a1c26fbcdb05941fa4cc01c0c7ec84da294fff [file] [log] [blame]
John Wedig2098dab2021-09-14 13:56:28 -07001#pragma once
2
John Wedigb810c922021-11-17 16:38:03 -08003#include "cryptsetupInterface.hpp"
4#include "filesystemInterface.hpp"
John Edward Broadbent5d799bb2022-03-22 16:14:24 -07005#include "util.hpp"
John Wedigb810c922021-11-17 16:38:03 -08006
7#include <libcryptsetup.h>
8
John Wedig67a47442022-04-05 17:21:29 -07009#include <sdbusplus/asio/object_server.hpp>
John Wedig2098dab2021-09-14 13:56:28 -070010#include <sdbusplus/bus.hpp>
11#include <sdbusplus/exception.hpp>
12#include <sdbusplus/server/object.hpp>
John Edward Broadbente35e7362022-03-22 16:14:24 -070013#include <util.hpp>
John Edward Broadbent86dfb242022-03-14 11:04:36 -070014#include <xyz/openbmc_project/Inventory/Item/Drive/server.hpp>
John Wedig972c3fa2021-12-29 17:30:41 -080015#include <xyz/openbmc_project/Inventory/Item/Volume/server.hpp>
John Wedig2098dab2021-09-14 13:56:28 -070016
John Wedigb810c922021-11-17 16:38:03 -080017#include <filesystem>
18#include <memory>
John Wedig2098dab2021-09-14 13:56:28 -070019#include <string>
John Wedigb810c922021-11-17 16:38:03 -080020#include <string_view>
John Wedig2098dab2021-09-14 13:56:28 -070021#include <vector>
22
23namespace estoraged
24{
John Wedigb810c922021-11-17 16:38:03 -080025using estoraged::Cryptsetup;
26using estoraged::Filesystem;
John Edward Broadbent91c1ec12022-05-20 16:51:43 -070027using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Drive;
John Wedig67a47442022-04-05 17:21:29 -070028using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Volume;
John Wedig2098dab2021-09-14 13:56:28 -070029
30/** @class eStoraged
31 * @brief eStoraged object to manage a LUKS encrypted storage device.
32 */
John Wedig67a47442022-04-05 17:21:29 -070033class EStoraged
John Wedig2098dab2021-09-14 13:56:28 -070034{
35 public:
John Wedigb810c922021-11-17 16:38:03 -080036 /** @brief Constructor for eStoraged
37 *
John Wedig67a47442022-04-05 17:21:29 -070038 * @param[in] server - sdbusplus asio object server
John Wedig6c0d8ce2022-04-22 14:00:43 -070039 * @param[in] configPath - path of the config object from Entity Manager
John Wedigb810c922021-11-17 16:38:03 -080040 * @param[in] devPath - path to device file, e.g. /dev/mmcblk0
41 * @param[in] luksName - name for the LUKS container
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070042 * @param[in] size - size of the drive in bytes
43 * @param[in] lifeTime - percent of lifetime remaining for a drive
John Wedigb4838302022-07-22 13:51:16 -070044 * @param[in] partNumber - part number for the storage device
45 * @param[in] serialNumber - serial number for the storage device
Rahul Kapoor19825052023-05-27 01:52:23 +000046 * @param[in] locationCode - location code for the storage device
Tom Tung043af592023-11-24 13:37:05 +080047 * @param[in] eraseMaxGeometry - max geometry to erase if it's specified
48 * @param[in] eraseMinGeometry - min geometry to erase if it's specified
John Wedigd7be42b2024-01-19 16:07:19 -080049 * @param[in] driveType - type of drive, e.g. HDD vs SSD
John Wedigc0d66eb2024-02-26 15:54:47 -080050 * @param[in] driveProtocol - protocol used to communicate with drive
John Wedigb810c922021-11-17 16:38:03 -080051 * @param[in] cryptInterface - (optional) pointer to CryptsetupInterface
52 * object
53 * @param[in] fsInterface - (optional) pointer to FilesystemInterface
54 * object
55 */
John Wedig67a47442022-04-05 17:21:29 -070056 EStoraged(sdbusplus::asio::object_server& server,
John Wedig6c0d8ce2022-04-22 14:00:43 -070057 const std::string& configPath, const std::string& devPath,
58 const std::string& luksName, uint64_t size, uint8_t lifeTime,
John Wedigb4838302022-07-22 13:51:16 -070059 const std::string& partNumber, const std::string& serialNumber,
Tom Tung043af592023-11-24 13:37:05 +080060 const std::string& locationCode, uint64_t eraseMaxGeometry,
John Wedigd7be42b2024-01-19 16:07:19 -080061 uint64_t eraseMinGeometry, const std::string& driveType,
John Wedigc0d66eb2024-02-26 15:54:47 -080062 const std::string& driveProtocol,
John Wedigb810c922021-11-17 16:38:03 -080063 std::unique_ptr<CryptsetupInterface> cryptInterface =
64 std::make_unique<Cryptsetup>(),
65 std::unique_ptr<FilesystemInterface> fsInterface =
John Wedig67a47442022-04-05 17:21:29 -070066 std::make_unique<Filesystem>());
67
68 /** @brief Destructor for eStoraged. */
69 ~EStoraged();
70
71 EStoraged& operator=(const EStoraged&) = delete;
72 EStoraged(const EStoraged&) = delete;
73 EStoraged(EStoraged&&) = default;
John Wedig61cf4262023-03-17 14:32:04 -070074 EStoraged& operator=(EStoraged&&) = delete;
John Wedig2098dab2021-09-14 13:56:28 -070075
76 /** @brief Format the LUKS encrypted device and create empty filesystem.
77 *
78 * @param[in] password - password to set for the LUKS device.
John Wedig972c3fa2021-12-29 17:30:41 -080079 * @param[in] type - filesystem type, e.g. ext4
John Wedig2098dab2021-09-14 13:56:28 -070080 */
John Wedig67a47442022-04-05 17:21:29 -070081 void formatLuks(const std::vector<uint8_t>& password,
82 Volume::FilesystemType type);
John Wedig2098dab2021-09-14 13:56:28 -070083
84 /** @brief Erase the contents of the storage device.
85 *
John Wedig2098dab2021-09-14 13:56:28 -070086 * @param[in] eraseType - type of erase operation.
87 */
John Wedig67a47442022-04-05 17:21:29 -070088 void erase(Volume::EraseMethod eraseType);
John Wedig2098dab2021-09-14 13:56:28 -070089
90 /** @brief Unmount filesystem and lock the LUKS device.
John Wedig2098dab2021-09-14 13:56:28 -070091 */
John Wedig67a47442022-04-05 17:21:29 -070092 void lock();
John Wedig2098dab2021-09-14 13:56:28 -070093
94 /** @brief Unlock device and mount the filesystem.
95 *
96 * @param[in] password - password for the LUKS device.
97 */
John Wedig67a47442022-04-05 17:21:29 -070098 void unlock(std::vector<uint8_t> password);
John Wedig2098dab2021-09-14 13:56:28 -070099
100 /** @brief Change the password for the LUKS device.
101 *
102 * @param[in] oldPassword - old password for the LUKS device.
103 * @param[in] newPassword - new password for the LUKS device.
104 */
John Wedig67a47442022-04-05 17:21:29 -0700105 void changePassword(const std::vector<uint8_t>& oldPassword,
106 const std::vector<uint8_t>& newPassword);
John Wedig2098dab2021-09-14 13:56:28 -0700107
John Wedigb810c922021-11-17 16:38:03 -0800108 /** @brief Check if the LUKS device is currently locked. */
109 bool isLocked() const;
110
111 /** @brief Get the mount point for the filesystem on the LUKS device. */
112 std::string_view getMountPoint() const;
113
John Wedig2443a022023-03-17 13:42:32 -0700114 /** @brief Get the path to the mapped crypt device. */
115 std::string_view getCryptDevicePath() const;
116
John Wedig2098dab2021-09-14 13:56:28 -0700117 private:
John Wedigb810c922021-11-17 16:38:03 -0800118 /** @brief Full path of the device file, e.g. /dev/mmcblk0. */
John Wedig2098dab2021-09-14 13:56:28 -0700119 std::string devPath;
120
John Wedigb810c922021-11-17 16:38:03 -0800121 /** @brief Name of the LUKS container. */
John Wedig2098dab2021-09-14 13:56:28 -0700122 std::string containerName;
John Wedigb810c922021-11-17 16:38:03 -0800123
124 /** @brief Mount point for the filesystem. */
125 std::string mountPoint;
126
Tom Tung043af592023-11-24 13:37:05 +0800127 /** @brief Max geometry to erase. */
128 uint64_t eraseMaxGeometry;
129
130 /** @brief Min geometry to erase. */
131 uint64_t eraseMinGeometry;
132
John Wedig67a47442022-04-05 17:21:29 -0700133 /** @brief Indicates whether the LUKS device is currently locked. */
John Edward Broadbent6771c692022-06-22 19:49:27 -0700134 bool lockedProperty{false};
John Wedig67a47442022-04-05 17:21:29 -0700135
John Wedigb810c922021-11-17 16:38:03 -0800136 /** @brief Pointer to cryptsetup interface object.
137 * @details This is used to mock out the cryptsetup functions.
138 */
139 std::unique_ptr<CryptsetupInterface> cryptIface;
140
141 /** @brief Pointer to filesystem interface object.
142 * @details This is used to mock out filesystem operations.
143 */
144 std::unique_ptr<FilesystemInterface> fsIface;
145
John Wedig2443a022023-03-17 13:42:32 -0700146 /** @brief Path where the mapped crypt device gets created. */
147 const std::string cryptDevicePath;
148
John Wedig67a47442022-04-05 17:21:29 -0700149 /** @brief D-Bus object server. */
150 sdbusplus::asio::object_server& objectServer;
151
152 /** @brief D-Bus interface for the logical volume. */
153 std::shared_ptr<sdbusplus::asio::dbus_interface> volumeInterface;
154
155 /** @brief D-Bus interface for the physical drive. */
156 std::shared_ptr<sdbusplus::asio::dbus_interface> driveInterface;
157
Rahul Kapoor19825052023-05-27 01:52:23 +0000158 /** @brief D-Bus interface for the location type of the drive. */
John Edward Broadbent49796412022-06-22 18:31:52 -0700159 std::shared_ptr<sdbusplus::asio::dbus_interface> embeddedLocationInterface;
John Edward Broadbent740e94b2022-06-10 19:42:30 -0700160
Rahul Kapoor19825052023-05-27 01:52:23 +0000161 /** @brief D-Bus interface for the location code of the drive. */
162 std::shared_ptr<sdbusplus::asio::dbus_interface> locationCodeInterface;
163
John Wedigb4838302022-07-22 13:51:16 -0700164 /** @brief D-Bus interface for the asset information. */
165 std::shared_ptr<sdbusplus::asio::dbus_interface> assetInterface;
166
John Wedig6c0d8ce2022-04-22 14:00:43 -0700167 /** @brief Association between chassis and drive. */
168 std::shared_ptr<sdbusplus::asio::dbus_interface> association;
169
John Edward Broadbent91c1ec12022-05-20 16:51:43 -0700170 /** @brief Indicates whether the LUKS header is on the disk. */
John Edward Broadbent6771c692022-06-22 19:49:27 -0700171 Drive::DriveEncryptionState encryptionStatus{
172 Drive::DriveEncryptionState::Unknown};
John Edward Broadbent91c1ec12022-05-20 16:51:43 -0700173
John Wedigb810c922021-11-17 16:38:03 -0800174 /** @brief Format LUKS encrypted device.
175 *
John Wedigb810c922021-11-17 16:38:03 -0800176 * @param[in] password - password to set for the LUKS device.
177 */
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700178 void formatLuksDev(std::vector<uint8_t> password);
John Wedigb810c922021-11-17 16:38:03 -0800179
John Edward Broadbent91c1ec12022-05-20 16:51:43 -0700180 /** @brief check the LUKS header, for devPath
181 *
182 * @returns a CryptHandle to the LUKS drive
183 */
184 CryptHandle loadLuksHeader();
185
John Wedigb810c922021-11-17 16:38:03 -0800186 /** @brief Unlock the device.
187 *
John Wedigb810c922021-11-17 16:38:03 -0800188 * @param[in] password - password to activate the LUKS device.
189 */
John Edward Broadbent91c1ec12022-05-20 16:51:43 -0700190
191 Drive::DriveEncryptionState findEncryptionStatus();
192
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700193 void activateLuksDev(std::vector<uint8_t> password);
John Wedigb810c922021-11-17 16:38:03 -0800194
195 /** @brief Create the filesystem on the LUKS device.
196 * @details The LUKS device should already be activated, i.e. unlocked.
197 */
198 void createFilesystem();
199
200 /** @brief Deactivate the LUKS device.
201 * @details The filesystem is assumed to be unmounted already.
202 */
203 void deactivateLuksDev();
204
205 /** @brief Mount the filesystem.
206 * @details The filesystem should already exist and the LUKS device should
207 * be unlocked already.
208 */
209 void mountFilesystem();
210
211 /** @brief Unmount the filesystem. */
212 void unmountFilesystem();
John Wedig2098dab2021-09-14 13:56:28 -0700213};
214
215} // namespace estoraged