blob: c160fb1332b8cccd38e3c546ff98128a29c058f4 [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 Wedig67a47442022-04-05 17:21:29 -070027using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Volume;
John Wedig2098dab2021-09-14 13:56:28 -070028
29/** @class eStoraged
30 * @brief eStoraged object to manage a LUKS encrypted storage device.
31 */
John Wedig67a47442022-04-05 17:21:29 -070032class EStoraged
John Wedig2098dab2021-09-14 13:56:28 -070033{
34 public:
John Wedigb810c922021-11-17 16:38:03 -080035 /** @brief Constructor for eStoraged
36 *
John Wedig67a47442022-04-05 17:21:29 -070037 * @param[in] server - sdbusplus asio object server
John Wedigb810c922021-11-17 16:38:03 -080038 * @param[in] devPath - path to device file, e.g. /dev/mmcblk0
39 * @param[in] luksName - name for the LUKS container
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070040 * @param[in] size - size of the drive in bytes
41 * @param[in] lifeTime - percent of lifetime remaining for a drive
John Wedigb810c922021-11-17 16:38:03 -080042 * @param[in] cryptInterface - (optional) pointer to CryptsetupInterface
43 * object
44 * @param[in] fsInterface - (optional) pointer to FilesystemInterface
45 * object
46 */
John Wedig67a47442022-04-05 17:21:29 -070047 EStoraged(sdbusplus::asio::object_server& server,
John Wedigb810c922021-11-17 16:38:03 -080048 const std::string& devPath, const std::string& luksName,
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070049 uint64_t size, uint8_t lifeTime,
John Wedigb810c922021-11-17 16:38:03 -080050 std::unique_ptr<CryptsetupInterface> cryptInterface =
51 std::make_unique<Cryptsetup>(),
52 std::unique_ptr<FilesystemInterface> fsInterface =
John Wedig67a47442022-04-05 17:21:29 -070053 std::make_unique<Filesystem>());
54
55 /** @brief Destructor for eStoraged. */
56 ~EStoraged();
57
58 EStoraged& operator=(const EStoraged&) = delete;
59 EStoraged(const EStoraged&) = delete;
60 EStoraged(EStoraged&&) = default;
61 EStoraged& operator=(EStoraged&&) = default;
John Wedig2098dab2021-09-14 13:56:28 -070062
63 /** @brief Format the LUKS encrypted device and create empty filesystem.
64 *
65 * @param[in] password - password to set for the LUKS device.
John Wedig972c3fa2021-12-29 17:30:41 -080066 * @param[in] type - filesystem type, e.g. ext4
John Wedig2098dab2021-09-14 13:56:28 -070067 */
John Wedig67a47442022-04-05 17:21:29 -070068 void formatLuks(const std::vector<uint8_t>& password,
69 Volume::FilesystemType type);
John Wedig2098dab2021-09-14 13:56:28 -070070
71 /** @brief Erase the contents of the storage device.
72 *
John Wedig2098dab2021-09-14 13:56:28 -070073 * @param[in] eraseType - type of erase operation.
74 */
John Wedig67a47442022-04-05 17:21:29 -070075 void erase(Volume::EraseMethod eraseType);
John Wedig2098dab2021-09-14 13:56:28 -070076
77 /** @brief Unmount filesystem and lock the LUKS device.
John Wedig2098dab2021-09-14 13:56:28 -070078 */
John Wedig67a47442022-04-05 17:21:29 -070079 void lock();
John Wedig2098dab2021-09-14 13:56:28 -070080
81 /** @brief Unlock device and mount the filesystem.
82 *
83 * @param[in] password - password for the LUKS device.
84 */
John Wedig67a47442022-04-05 17:21:29 -070085 void unlock(std::vector<uint8_t> password);
John Wedig2098dab2021-09-14 13:56:28 -070086
87 /** @brief Change the password for the LUKS device.
88 *
89 * @param[in] oldPassword - old password for the LUKS device.
90 * @param[in] newPassword - new password for the LUKS device.
91 */
John Wedig67a47442022-04-05 17:21:29 -070092 void changePassword(const std::vector<uint8_t>& oldPassword,
93 const std::vector<uint8_t>& newPassword);
John Wedig2098dab2021-09-14 13:56:28 -070094
John Wedigb810c922021-11-17 16:38:03 -080095 /** @brief Check if the LUKS device is currently locked. */
96 bool isLocked() const;
97
98 /** @brief Get the mount point for the filesystem on the LUKS device. */
99 std::string_view getMountPoint() const;
100
John Wedig2098dab2021-09-14 13:56:28 -0700101 private:
John Wedigb810c922021-11-17 16:38:03 -0800102 /** @brief Full path of the device file, e.g. /dev/mmcblk0. */
John Wedig2098dab2021-09-14 13:56:28 -0700103 std::string devPath;
104
John Wedigb810c922021-11-17 16:38:03 -0800105 /** @brief Name of the LUKS container. */
John Wedig2098dab2021-09-14 13:56:28 -0700106 std::string containerName;
John Wedigb810c922021-11-17 16:38:03 -0800107
108 /** @brief Mount point for the filesystem. */
109 std::string mountPoint;
110
John Wedig67a47442022-04-05 17:21:29 -0700111 /** @brief Indicates whether the LUKS device is currently locked. */
112 bool lockedProperty;
113
John Wedigb810c922021-11-17 16:38:03 -0800114 /** @brief Pointer to cryptsetup interface object.
115 * @details This is used to mock out the cryptsetup functions.
116 */
117 std::unique_ptr<CryptsetupInterface> cryptIface;
118
119 /** @brief Pointer to filesystem interface object.
120 * @details This is used to mock out filesystem operations.
121 */
122 std::unique_ptr<FilesystemInterface> fsIface;
123
John Wedig67a47442022-04-05 17:21:29 -0700124 /** @brief D-Bus object server. */
125 sdbusplus::asio::object_server& objectServer;
126
127 /** @brief D-Bus interface for the logical volume. */
128 std::shared_ptr<sdbusplus::asio::dbus_interface> volumeInterface;
129
130 /** @brief D-Bus interface for the physical drive. */
131 std::shared_ptr<sdbusplus::asio::dbus_interface> driveInterface;
132
John Wedigb810c922021-11-17 16:38:03 -0800133 /** @brief Format LUKS encrypted device.
134 *
135 * @param[in] cd - initialized crypt_device struct for the device.
136 * @param[in] password - password to set for the LUKS device.
137 */
138 void formatLuksDev(struct crypt_device* cd, std::vector<uint8_t> password);
139
140 /** @brief Unlock the device.
141 *
142 * @param[in] cd - initialized crypt_device struct for the device.
143 * @param[in] password - password to activate the LUKS device.
144 */
145 void activateLuksDev(struct crypt_device* cd,
146 std::vector<uint8_t> password);
147
148 /** @brief Create the filesystem on the LUKS device.
149 * @details The LUKS device should already be activated, i.e. unlocked.
150 */
151 void createFilesystem();
152
153 /** @brief Deactivate the LUKS device.
154 * @details The filesystem is assumed to be unmounted already.
155 */
156 void deactivateLuksDev();
157
158 /** @brief Mount the filesystem.
159 * @details The filesystem should already exist and the LUKS device should
160 * be unlocked already.
161 */
162 void mountFilesystem();
163
164 /** @brief Unmount the filesystem. */
165 void unmountFilesystem();
John Wedig67a47442022-04-05 17:21:29 -0700166
167 /** @brief Set the locked property.
168 *
169 * @param[in] isLocked - indicates whether the LUKS device is locked.
170 */
171 void locked(bool isLocked);
John Wedig2098dab2021-09-14 13:56:28 -0700172};
173
174} // namespace estoraged