John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 3 | #include "cryptsetupInterface.hpp" |
| 4 | #include "filesystemInterface.hpp" |
John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 5 | #include "util.hpp" |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 6 | |
| 7 | #include <libcryptsetup.h> |
| 8 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 9 | #include <sdbusplus/asio/object_server.hpp> |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 10 | #include <sdbusplus/bus.hpp> |
| 11 | #include <sdbusplus/exception.hpp> |
| 12 | #include <sdbusplus/server/object.hpp> |
John Edward Broadbent | e35e736 | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 13 | #include <util.hpp> |
John Edward Broadbent | 86dfb24 | 2022-03-14 11:04:36 -0700 | [diff] [blame] | 14 | #include <xyz/openbmc_project/Inventory/Item/Drive/server.hpp> |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 15 | #include <xyz/openbmc_project/Inventory/Item/Volume/server.hpp> |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 16 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 17 | #include <filesystem> |
| 18 | #include <memory> |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 19 | #include <string> |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 20 | #include <string_view> |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
| 23 | namespace estoraged |
| 24 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 25 | using estoraged::Cryptsetup; |
| 26 | using estoraged::Filesystem; |
John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 27 | using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Drive; |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 28 | using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Volume; |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 29 | |
| 30 | /** @class eStoraged |
| 31 | * @brief eStoraged object to manage a LUKS encrypted storage device. |
| 32 | */ |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 33 | class EStoraged |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 34 | { |
| 35 | public: |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 36 | /** @brief Constructor for eStoraged |
| 37 | * |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 38 | * @param[in] server - sdbusplus asio object server |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 39 | * @param[in] configPath - path of the config object from Entity Manager |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 40 | * @param[in] devPath - path to device file, e.g. /dev/mmcblk0 |
| 41 | * @param[in] luksName - name for the LUKS container |
John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 42 | * @param[in] size - size of the drive in bytes |
| 43 | * @param[in] lifeTime - percent of lifetime remaining for a drive |
John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 44 | * @param[in] partNumber - part number for the storage device |
| 45 | * @param[in] serialNumber - serial number for the storage device |
Rahul Kapoor | 1982505 | 2023-05-27 01:52:23 +0000 | [diff] [blame] | 46 | * @param[in] locationCode - location code for the storage device |
Tom Tung | 043af59 | 2023-11-24 13:37:05 +0800 | [diff] [blame] | 47 | * @param[in] eraseMaxGeometry - max geometry to erase if it's specified |
| 48 | * @param[in] eraseMinGeometry - min geometry to erase if it's specified |
John Wedig | d7be42b | 2024-01-19 16:07:19 -0800 | [diff] [blame] | 49 | * @param[in] driveType - type of drive, e.g. HDD vs SSD |
John Wedig | c0d66eb | 2024-02-26 15:54:47 -0800 | [diff] [blame] | 50 | * @param[in] driveProtocol - protocol used to communicate with drive |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 51 | * @param[in] cryptInterface - (optional) pointer to CryptsetupInterface |
| 52 | * object |
| 53 | * @param[in] fsInterface - (optional) pointer to FilesystemInterface |
| 54 | * object |
| 55 | */ |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 56 | EStoraged(sdbusplus::asio::object_server& server, |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 57 | const std::string& configPath, const std::string& devPath, |
| 58 | const std::string& luksName, uint64_t size, uint8_t lifeTime, |
John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 59 | const std::string& partNumber, const std::string& serialNumber, |
Tom Tung | 043af59 | 2023-11-24 13:37:05 +0800 | [diff] [blame] | 60 | const std::string& locationCode, uint64_t eraseMaxGeometry, |
John Wedig | d7be42b | 2024-01-19 16:07:19 -0800 | [diff] [blame] | 61 | uint64_t eraseMinGeometry, const std::string& driveType, |
John Wedig | c0d66eb | 2024-02-26 15:54:47 -0800 | [diff] [blame] | 62 | const std::string& driveProtocol, |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 63 | std::unique_ptr<CryptsetupInterface> cryptInterface = |
| 64 | std::make_unique<Cryptsetup>(), |
| 65 | std::unique_ptr<FilesystemInterface> fsInterface = |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 66 | 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 Wedig | 61cf426 | 2023-03-17 14:32:04 -0700 | [diff] [blame] | 74 | EStoraged& operator=(EStoraged&&) = delete; |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 75 | |
| 76 | /** @brief Format the LUKS encrypted device and create empty filesystem. |
| 77 | * |
| 78 | * @param[in] password - password to set for the LUKS device. |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 79 | * @param[in] type - filesystem type, e.g. ext4 |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 80 | */ |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 81 | void formatLuks(const std::vector<uint8_t>& password, |
| 82 | Volume::FilesystemType type); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 83 | |
| 84 | /** @brief Erase the contents of the storage device. |
| 85 | * |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 86 | * @param[in] eraseType - type of erase operation. |
| 87 | */ |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 88 | void erase(Volume::EraseMethod eraseType); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 89 | |
| 90 | /** @brief Unmount filesystem and lock the LUKS device. |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 91 | */ |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 92 | void lock(); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 93 | |
| 94 | /** @brief Unlock device and mount the filesystem. |
| 95 | * |
| 96 | * @param[in] password - password for the LUKS device. |
| 97 | */ |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 98 | void unlock(std::vector<uint8_t> password); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 99 | |
| 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 Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 105 | void changePassword(const std::vector<uint8_t>& oldPassword, |
| 106 | const std::vector<uint8_t>& newPassword); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 107 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 108 | /** @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 Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 114 | /** @brief Get the path to the mapped crypt device. */ |
| 115 | std::string_view getCryptDevicePath() const; |
| 116 | |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 117 | private: |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 118 | /** @brief Full path of the device file, e.g. /dev/mmcblk0. */ |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 119 | std::string devPath; |
| 120 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 121 | /** @brief Name of the LUKS container. */ |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 122 | std::string containerName; |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 123 | |
| 124 | /** @brief Mount point for the filesystem. */ |
| 125 | std::string mountPoint; |
| 126 | |
Tom Tung | 043af59 | 2023-11-24 13:37:05 +0800 | [diff] [blame] | 127 | /** @brief Max geometry to erase. */ |
| 128 | uint64_t eraseMaxGeometry; |
| 129 | |
| 130 | /** @brief Min geometry to erase. */ |
| 131 | uint64_t eraseMinGeometry; |
| 132 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 133 | /** @brief Indicates whether the LUKS device is currently locked. */ |
John Edward Broadbent | 6771c69 | 2022-06-22 19:49:27 -0700 | [diff] [blame] | 134 | bool lockedProperty{false}; |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 135 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 136 | /** @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 Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 146 | /** @brief Path where the mapped crypt device gets created. */ |
| 147 | const std::string cryptDevicePath; |
| 148 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 149 | /** @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 Kapoor | 1982505 | 2023-05-27 01:52:23 +0000 | [diff] [blame] | 158 | /** @brief D-Bus interface for the location type of the drive. */ |
John Edward Broadbent | 4979641 | 2022-06-22 18:31:52 -0700 | [diff] [blame] | 159 | std::shared_ptr<sdbusplus::asio::dbus_interface> embeddedLocationInterface; |
John Edward Broadbent | 740e94b | 2022-06-10 19:42:30 -0700 | [diff] [blame] | 160 | |
Rahul Kapoor | 1982505 | 2023-05-27 01:52:23 +0000 | [diff] [blame] | 161 | /** @brief D-Bus interface for the location code of the drive. */ |
| 162 | std::shared_ptr<sdbusplus::asio::dbus_interface> locationCodeInterface; |
| 163 | |
John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 164 | /** @brief D-Bus interface for the asset information. */ |
| 165 | std::shared_ptr<sdbusplus::asio::dbus_interface> assetInterface; |
| 166 | |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 167 | /** @brief Association between chassis and drive. */ |
| 168 | std::shared_ptr<sdbusplus::asio::dbus_interface> association; |
| 169 | |
John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 170 | /** @brief Indicates whether the LUKS header is on the disk. */ |
John Edward Broadbent | 6771c69 | 2022-06-22 19:49:27 -0700 | [diff] [blame] | 171 | Drive::DriveEncryptionState encryptionStatus{ |
| 172 | Drive::DriveEncryptionState::Unknown}; |
John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 173 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 174 | /** @brief Format LUKS encrypted device. |
| 175 | * |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 176 | * @param[in] password - password to set for the LUKS device. |
| 177 | */ |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 178 | void formatLuksDev(std::vector<uint8_t> password); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 179 | |
John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 180 | /** @brief check the LUKS header, for devPath |
| 181 | * |
| 182 | * @returns a CryptHandle to the LUKS drive |
| 183 | */ |
| 184 | CryptHandle loadLuksHeader(); |
| 185 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 186 | /** @brief Unlock the device. |
| 187 | * |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 188 | * @param[in] password - password to activate the LUKS device. |
| 189 | */ |
John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 190 | |
| 191 | Drive::DriveEncryptionState findEncryptionStatus(); |
| 192 | |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 193 | void activateLuksDev(std::vector<uint8_t> password); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 194 | |
| 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 Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | } // namespace estoraged |