blob: aa89bee8b5fa0ff9486d25e989be5bb013a905d6 [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"
5
6#include <libcryptsetup.h>
7
John Wedig67a47442022-04-05 17:21:29 -07008#include <sdbusplus/asio/object_server.hpp>
John Wedig2098dab2021-09-14 13:56:28 -07009#include <sdbusplus/bus.hpp>
10#include <sdbusplus/exception.hpp>
11#include <sdbusplus/server/object.hpp>
John Edward Broadbente35e7362022-03-22 16:14:24 -070012#include <util.hpp>
John Edward Broadbent86dfb242022-03-14 11:04:36 -070013#include <xyz/openbmc_project/Inventory/Item/Drive/server.hpp>
John Wedig972c3fa2021-12-29 17:30:41 -080014#include <xyz/openbmc_project/Inventory/Item/Volume/server.hpp>
John Wedig2098dab2021-09-14 13:56:28 -070015
John Wedigb810c922021-11-17 16:38:03 -080016#include <filesystem>
17#include <memory>
John Wedig2098dab2021-09-14 13:56:28 -070018#include <string>
John Wedigb810c922021-11-17 16:38:03 -080019#include <string_view>
John Wedig2098dab2021-09-14 13:56:28 -070020#include <vector>
21
22namespace estoraged
23{
John Wedigb810c922021-11-17 16:38:03 -080024using estoraged::Cryptsetup;
25using estoraged::Filesystem;
John Wedig67a47442022-04-05 17:21:29 -070026using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Volume;
John Wedig2098dab2021-09-14 13:56:28 -070027
28/** @class eStoraged
29 * @brief eStoraged object to manage a LUKS encrypted storage device.
30 */
John Wedig67a47442022-04-05 17:21:29 -070031class EStoraged
John Wedig2098dab2021-09-14 13:56:28 -070032{
33 public:
John Wedigb810c922021-11-17 16:38:03 -080034 /** @brief Constructor for eStoraged
35 *
John Wedig67a47442022-04-05 17:21:29 -070036 * @param[in] server - sdbusplus asio object server
John Wedigb810c922021-11-17 16:38:03 -080037 * @param[in] devPath - path to device file, e.g. /dev/mmcblk0
38 * @param[in] luksName - name for the LUKS container
39 * @param[in] cryptInterface - (optional) pointer to CryptsetupInterface
40 * object
41 * @param[in] fsInterface - (optional) pointer to FilesystemInterface
42 * object
43 */
John Wedig67a47442022-04-05 17:21:29 -070044 EStoraged(sdbusplus::asio::object_server& server,
John Wedigb810c922021-11-17 16:38:03 -080045 const std::string& devPath, const std::string& luksName,
John Edward Broadbente35e7362022-03-22 16:14:24 -070046 uint64_t size,
John Wedigb810c922021-11-17 16:38:03 -080047 std::unique_ptr<CryptsetupInterface> cryptInterface =
48 std::make_unique<Cryptsetup>(),
49 std::unique_ptr<FilesystemInterface> fsInterface =
John Wedig67a47442022-04-05 17:21:29 -070050 std::make_unique<Filesystem>());
51
52 /** @brief Destructor for eStoraged. */
53 ~EStoraged();
54
55 EStoraged& operator=(const EStoraged&) = delete;
56 EStoraged(const EStoraged&) = delete;
57 EStoraged(EStoraged&&) = default;
58 EStoraged& operator=(EStoraged&&) = default;
John Wedig2098dab2021-09-14 13:56:28 -070059
60 /** @brief Format the LUKS encrypted device and create empty filesystem.
61 *
62 * @param[in] password - password to set for the LUKS device.
John Wedig972c3fa2021-12-29 17:30:41 -080063 * @param[in] type - filesystem type, e.g. ext4
John Wedig2098dab2021-09-14 13:56:28 -070064 */
John Wedig67a47442022-04-05 17:21:29 -070065 void formatLuks(const std::vector<uint8_t>& password,
66 Volume::FilesystemType type);
John Wedig2098dab2021-09-14 13:56:28 -070067
68 /** @brief Erase the contents of the storage device.
69 *
John Wedig2098dab2021-09-14 13:56:28 -070070 * @param[in] eraseType - type of erase operation.
71 */
John Wedig67a47442022-04-05 17:21:29 -070072 void erase(Volume::EraseMethod eraseType);
John Wedig2098dab2021-09-14 13:56:28 -070073
74 /** @brief Unmount filesystem and lock the LUKS device.
John Wedig2098dab2021-09-14 13:56:28 -070075 */
John Wedig67a47442022-04-05 17:21:29 -070076 void lock();
John Wedig2098dab2021-09-14 13:56:28 -070077
78 /** @brief Unlock device and mount the filesystem.
79 *
80 * @param[in] password - password for the LUKS device.
81 */
John Wedig67a47442022-04-05 17:21:29 -070082 void unlock(std::vector<uint8_t> password);
John Wedig2098dab2021-09-14 13:56:28 -070083
84 /** @brief Change the password for the LUKS device.
85 *
86 * @param[in] oldPassword - old password for the LUKS device.
87 * @param[in] newPassword - new password for the LUKS device.
88 */
John Wedig67a47442022-04-05 17:21:29 -070089 void changePassword(const std::vector<uint8_t>& oldPassword,
90 const std::vector<uint8_t>& newPassword);
John Wedig2098dab2021-09-14 13:56:28 -070091
John Wedigb810c922021-11-17 16:38:03 -080092 /** @brief Check if the LUKS device is currently locked. */
93 bool isLocked() const;
94
95 /** @brief Get the mount point for the filesystem on the LUKS device. */
96 std::string_view getMountPoint() const;
97
John Wedig2098dab2021-09-14 13:56:28 -070098 private:
John Wedigb810c922021-11-17 16:38:03 -080099 /** @brief Full path of the device file, e.g. /dev/mmcblk0. */
John Wedig2098dab2021-09-14 13:56:28 -0700100 std::string devPath;
101
John Wedigb810c922021-11-17 16:38:03 -0800102 /** @brief Name of the LUKS container. */
John Wedig2098dab2021-09-14 13:56:28 -0700103 std::string containerName;
John Wedigb810c922021-11-17 16:38:03 -0800104
105 /** @brief Mount point for the filesystem. */
106 std::string mountPoint;
107
John Wedig67a47442022-04-05 17:21:29 -0700108 /** @brief Indicates whether the LUKS device is currently locked. */
109 bool lockedProperty;
110
John Wedigb810c922021-11-17 16:38:03 -0800111 /** @brief Pointer to cryptsetup interface object.
112 * @details This is used to mock out the cryptsetup functions.
113 */
114 std::unique_ptr<CryptsetupInterface> cryptIface;
115
116 /** @brief Pointer to filesystem interface object.
117 * @details This is used to mock out filesystem operations.
118 */
119 std::unique_ptr<FilesystemInterface> fsIface;
120
John Wedig67a47442022-04-05 17:21:29 -0700121 /** @brief D-Bus object server. */
122 sdbusplus::asio::object_server& objectServer;
123
124 /** @brief D-Bus interface for the logical volume. */
125 std::shared_ptr<sdbusplus::asio::dbus_interface> volumeInterface;
126
127 /** @brief D-Bus interface for the physical drive. */
128 std::shared_ptr<sdbusplus::asio::dbus_interface> driveInterface;
129
John Wedigb810c922021-11-17 16:38:03 -0800130 /** @brief Format LUKS encrypted device.
131 *
132 * @param[in] cd - initialized crypt_device struct for the device.
133 * @param[in] password - password to set for the LUKS device.
134 */
135 void formatLuksDev(struct crypt_device* cd, std::vector<uint8_t> password);
136
137 /** @brief Unlock the device.
138 *
139 * @param[in] cd - initialized crypt_device struct for the device.
140 * @param[in] password - password to activate the LUKS device.
141 */
142 void activateLuksDev(struct crypt_device* cd,
143 std::vector<uint8_t> password);
144
145 /** @brief Create the filesystem on the LUKS device.
146 * @details The LUKS device should already be activated, i.e. unlocked.
147 */
148 void createFilesystem();
149
150 /** @brief Deactivate the LUKS device.
151 * @details The filesystem is assumed to be unmounted already.
152 */
153 void deactivateLuksDev();
154
155 /** @brief Mount the filesystem.
156 * @details The filesystem should already exist and the LUKS device should
157 * be unlocked already.
158 */
159 void mountFilesystem();
160
161 /** @brief Unmount the filesystem. */
162 void unmountFilesystem();
John Wedig67a47442022-04-05 17:21:29 -0700163
164 /** @brief Set the locked property.
165 *
166 * @param[in] isLocked - indicates whether the LUKS device is locked.
167 */
168 void locked(bool isLocked);
John Wedig2098dab2021-09-14 13:56:28 -0700169};
170
171} // namespace estoraged