blob: c938c334182530731b3afb6103d29652e9f2096e [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
John Wedigb810c922021-11-17 16:38:03 -080046 * @param[in] cryptInterface - (optional) pointer to CryptsetupInterface
47 * object
48 * @param[in] fsInterface - (optional) pointer to FilesystemInterface
49 * object
50 */
John Wedig67a47442022-04-05 17:21:29 -070051 EStoraged(sdbusplus::asio::object_server& server,
John Wedig6c0d8ce2022-04-22 14:00:43 -070052 const std::string& configPath, const std::string& devPath,
53 const std::string& luksName, uint64_t size, uint8_t lifeTime,
John Wedigb4838302022-07-22 13:51:16 -070054 const std::string& partNumber, const std::string& serialNumber,
John Wedigb810c922021-11-17 16:38:03 -080055 std::unique_ptr<CryptsetupInterface> cryptInterface =
56 std::make_unique<Cryptsetup>(),
57 std::unique_ptr<FilesystemInterface> fsInterface =
John Wedig67a47442022-04-05 17:21:29 -070058 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&&) = default;
John Wedig2098dab2021-09-14 13:56:28 -070067
68 /** @brief Format the LUKS encrypted device and create empty filesystem.
69 *
70 * @param[in] password - password to set for the LUKS device.
John Wedig972c3fa2021-12-29 17:30:41 -080071 * @param[in] type - filesystem type, e.g. ext4
John Wedig2098dab2021-09-14 13:56:28 -070072 */
John Wedig67a47442022-04-05 17:21:29 -070073 void formatLuks(const std::vector<uint8_t>& password,
74 Volume::FilesystemType type);
John Wedig2098dab2021-09-14 13:56:28 -070075
76 /** @brief Erase the contents of the storage device.
77 *
John Wedig2098dab2021-09-14 13:56:28 -070078 * @param[in] eraseType - type of erase operation.
79 */
John Wedig67a47442022-04-05 17:21:29 -070080 void erase(Volume::EraseMethod eraseType);
John Wedig2098dab2021-09-14 13:56:28 -070081
82 /** @brief Unmount filesystem and lock the LUKS device.
John Wedig2098dab2021-09-14 13:56:28 -070083 */
John Wedig67a47442022-04-05 17:21:29 -070084 void lock();
John Wedig2098dab2021-09-14 13:56:28 -070085
86 /** @brief Unlock device and mount the filesystem.
87 *
88 * @param[in] password - password for the LUKS device.
89 */
John Wedig67a47442022-04-05 17:21:29 -070090 void unlock(std::vector<uint8_t> password);
John Wedig2098dab2021-09-14 13:56:28 -070091
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 */
John Wedig67a47442022-04-05 17:21:29 -070097 void changePassword(const std::vector<uint8_t>& oldPassword,
98 const std::vector<uint8_t>& newPassword);
John Wedig2098dab2021-09-14 13:56:28 -070099
John Wedigb810c922021-11-17 16:38:03 -0800100 /** @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
John Wedig2098dab2021-09-14 13:56:28 -0700106 private:
John Wedigb810c922021-11-17 16:38:03 -0800107 /** @brief Full path of the device file, e.g. /dev/mmcblk0. */
John Wedig2098dab2021-09-14 13:56:28 -0700108 std::string devPath;
109
John Wedigb810c922021-11-17 16:38:03 -0800110 /** @brief Name of the LUKS container. */
John Wedig2098dab2021-09-14 13:56:28 -0700111 std::string containerName;
John Wedigb810c922021-11-17 16:38:03 -0800112
113 /** @brief Mount point for the filesystem. */
114 std::string mountPoint;
115
John Wedig67a47442022-04-05 17:21:29 -0700116 /** @brief Indicates whether the LUKS device is currently locked. */
John Edward Broadbent6771c692022-06-22 19:49:27 -0700117 bool lockedProperty{false};
John Wedig67a47442022-04-05 17:21:29 -0700118
John Wedigb810c922021-11-17 16:38:03 -0800119 /** @brief Pointer to cryptsetup interface object.
120 * @details This is used to mock out the cryptsetup functions.
121 */
122 std::unique_ptr<CryptsetupInterface> cryptIface;
123
124 /** @brief Pointer to filesystem interface object.
125 * @details This is used to mock out filesystem operations.
126 */
127 std::unique_ptr<FilesystemInterface> fsIface;
128
John Wedig67a47442022-04-05 17:21:29 -0700129 /** @brief D-Bus object server. */
130 sdbusplus::asio::object_server& objectServer;
131
132 /** @brief D-Bus interface for the logical volume. */
133 std::shared_ptr<sdbusplus::asio::dbus_interface> volumeInterface;
134
135 /** @brief D-Bus interface for the physical drive. */
136 std::shared_ptr<sdbusplus::asio::dbus_interface> driveInterface;
137
John Edward Broadbent740e94b2022-06-10 19:42:30 -0700138 /** @brief D-Bus interface for the location of the drive. */
John Edward Broadbent49796412022-06-22 18:31:52 -0700139 std::shared_ptr<sdbusplus::asio::dbus_interface> embeddedLocationInterface;
John Edward Broadbent740e94b2022-06-10 19:42:30 -0700140
John Wedigb4838302022-07-22 13:51:16 -0700141 /** @brief D-Bus interface for the asset information. */
142 std::shared_ptr<sdbusplus::asio::dbus_interface> assetInterface;
143
John Wedig6c0d8ce2022-04-22 14:00:43 -0700144 /** @brief Association between chassis and drive. */
145 std::shared_ptr<sdbusplus::asio::dbus_interface> association;
146
John Edward Broadbent91c1ec12022-05-20 16:51:43 -0700147 /** @brief Indicates whether the LUKS header is on the disk. */
John Edward Broadbent6771c692022-06-22 19:49:27 -0700148 Drive::DriveEncryptionState encryptionStatus{
149 Drive::DriveEncryptionState::Unknown};
John Edward Broadbent91c1ec12022-05-20 16:51:43 -0700150
John Wedigb810c922021-11-17 16:38:03 -0800151 /** @brief Format LUKS encrypted device.
152 *
John Wedigb810c922021-11-17 16:38:03 -0800153 * @param[in] password - password to set for the LUKS device.
154 */
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700155 void formatLuksDev(std::vector<uint8_t> password);
John Wedigb810c922021-11-17 16:38:03 -0800156
John Edward Broadbent91c1ec12022-05-20 16:51:43 -0700157 /** @brief check the LUKS header, for devPath
158 *
159 * @returns a CryptHandle to the LUKS drive
160 */
161 CryptHandle loadLuksHeader();
162
John Wedigb810c922021-11-17 16:38:03 -0800163 /** @brief Unlock the device.
164 *
John Wedigb810c922021-11-17 16:38:03 -0800165 * @param[in] password - password to activate the LUKS device.
166 */
John Edward Broadbent91c1ec12022-05-20 16:51:43 -0700167
168 Drive::DriveEncryptionState findEncryptionStatus();
169
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700170 void activateLuksDev(std::vector<uint8_t> password);
John Wedigb810c922021-11-17 16:38:03 -0800171
172 /** @brief Create the filesystem on the LUKS device.
173 * @details The LUKS device should already be activated, i.e. unlocked.
174 */
175 void createFilesystem();
176
177 /** @brief Deactivate the LUKS device.
178 * @details The filesystem is assumed to be unmounted already.
179 */
180 void deactivateLuksDev();
181
182 /** @brief Mount the filesystem.
183 * @details The filesystem should already exist and the LUKS device should
184 * be unlocked already.
185 */
186 void mountFilesystem();
187
188 /** @brief Unmount the filesystem. */
189 void unmountFilesystem();
John Wedig67a47442022-04-05 17:21:29 -0700190
191 /** @brief Set the locked property.
192 *
193 * @param[in] isLocked - indicates whether the LUKS device is locked.
194 */
195 void locked(bool isLocked);
John Wedig2098dab2021-09-14 13:56:28 -0700196};
197
198} // namespace estoraged