xref: /openbmc/estoraged/include/estoraged.hpp (revision c0d66eb78bdeaf8e9689b1ed07f819a5984aecf0)
12098dabeSJohn Wedig #pragma once
22098dabeSJohn Wedig 
3b810c926SJohn Wedig #include "cryptsetupInterface.hpp"
4b810c926SJohn Wedig #include "filesystemInterface.hpp"
55d799bb9SJohn Edward Broadbent #include "util.hpp"
6b810c926SJohn Wedig 
7b810c926SJohn Wedig #include <libcryptsetup.h>
8b810c926SJohn Wedig 
967a47446SJohn Wedig #include <sdbusplus/asio/object_server.hpp>
102098dabeSJohn Wedig #include <sdbusplus/bus.hpp>
112098dabeSJohn Wedig #include <sdbusplus/exception.hpp>
122098dabeSJohn Wedig #include <sdbusplus/server/object.hpp>
13e35e7361SJohn Edward Broadbent #include <util.hpp>
1486dfb244SJohn Edward Broadbent #include <xyz/openbmc_project/Inventory/Item/Drive/server.hpp>
15972c3faaSJohn Wedig #include <xyz/openbmc_project/Inventory/Item/Volume/server.hpp>
162098dabeSJohn Wedig 
17b810c926SJohn Wedig #include <filesystem>
18b810c926SJohn Wedig #include <memory>
192098dabeSJohn Wedig #include <string>
20b810c926SJohn Wedig #include <string_view>
212098dabeSJohn Wedig #include <vector>
222098dabeSJohn Wedig 
232098dabeSJohn Wedig namespace estoraged
242098dabeSJohn Wedig {
25b810c926SJohn Wedig using estoraged::Cryptsetup;
26b810c926SJohn Wedig using estoraged::Filesystem;
2791c1ec1bSJohn Edward Broadbent using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Drive;
2867a47446SJohn Wedig using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Volume;
292098dabeSJohn Wedig 
302098dabeSJohn Wedig /** @class eStoraged
312098dabeSJohn Wedig  *  @brief eStoraged object to manage a LUKS encrypted storage device.
322098dabeSJohn Wedig  */
3367a47446SJohn Wedig class EStoraged
342098dabeSJohn Wedig {
352098dabeSJohn Wedig   public:
36b810c926SJohn Wedig     /** @brief Constructor for eStoraged
37b810c926SJohn Wedig      *
3867a47446SJohn Wedig      *  @param[in] server - sdbusplus asio object server
396c0d8ce1SJohn Wedig      *  @param[in] configPath - path of the config object from Entity Manager
40b810c926SJohn Wedig      *  @param[in] devPath - path to device file, e.g. /dev/mmcblk0
41b810c926SJohn Wedig      *  @param[in] luksName - name for the LUKS container
425d799bb9SJohn Edward Broadbent      *  @param[in] size - size of the drive in bytes
435d799bb9SJohn Edward Broadbent      *  @param[in] lifeTime - percent of lifetime remaining for a drive
44b4838308SJohn Wedig      *  @param[in] partNumber - part number for the storage device
45b4838308SJohn Wedig      *  @param[in] serialNumber - serial number for the storage device
4619825057SRahul Kapoor      *  @param[in] locationCode - location code for the storage device
47043af59fSTom Tung      *  @param[in] eraseMaxGeometry - max geometry to erase if it's specified
48043af59fSTom Tung      *  @param[in] eraseMinGeometry - min geometry to erase if it's specified
49d7be42bdSJohn Wedig      *  @param[in] driveType - type of drive, e.g. HDD vs SSD
50*c0d66eb7SJohn Wedig      *  @param[in] driveProtocol - protocol used to communicate with drive
51b810c926SJohn Wedig      *  @param[in] cryptInterface - (optional) pointer to CryptsetupInterface
52b810c926SJohn Wedig      *    object
53b810c926SJohn Wedig      *  @param[in] fsInterface - (optional) pointer to FilesystemInterface
54b810c926SJohn Wedig      *    object
55b810c926SJohn Wedig      */
5667a47446SJohn Wedig     EStoraged(sdbusplus::asio::object_server& server,
576c0d8ce1SJohn Wedig               const std::string& configPath, const std::string& devPath,
586c0d8ce1SJohn Wedig               const std::string& luksName, uint64_t size, uint8_t lifeTime,
59b4838308SJohn Wedig               const std::string& partNumber, const std::string& serialNumber,
60043af59fSTom Tung               const std::string& locationCode, uint64_t eraseMaxGeometry,
61d7be42bdSJohn Wedig               uint64_t eraseMinGeometry, const std::string& driveType,
62*c0d66eb7SJohn Wedig               const std::string& driveProtocol,
63b810c926SJohn Wedig               std::unique_ptr<CryptsetupInterface> cryptInterface =
64b810c926SJohn Wedig                   std::make_unique<Cryptsetup>(),
65b810c926SJohn Wedig               std::unique_ptr<FilesystemInterface> fsInterface =
6667a47446SJohn Wedig                   std::make_unique<Filesystem>());
6767a47446SJohn Wedig 
6867a47446SJohn Wedig     /** @brief Destructor for eStoraged. */
6967a47446SJohn Wedig     ~EStoraged();
7067a47446SJohn Wedig 
7167a47446SJohn Wedig     EStoraged& operator=(const EStoraged&) = delete;
7267a47446SJohn Wedig     EStoraged(const EStoraged&) = delete;
7367a47446SJohn Wedig     EStoraged(EStoraged&&) = default;
7461cf4260SJohn Wedig     EStoraged& operator=(EStoraged&&) = delete;
752098dabeSJohn Wedig 
762098dabeSJohn Wedig     /** @brief Format the LUKS encrypted device and create empty filesystem.
772098dabeSJohn Wedig      *
782098dabeSJohn Wedig      *  @param[in] password - password to set for the LUKS device.
79972c3faaSJohn Wedig      *  @param[in] type - filesystem type, e.g. ext4
802098dabeSJohn Wedig      */
8167a47446SJohn Wedig     void formatLuks(const std::vector<uint8_t>& password,
8267a47446SJohn Wedig                     Volume::FilesystemType type);
832098dabeSJohn Wedig 
842098dabeSJohn Wedig     /** @brief Erase the contents of the storage device.
852098dabeSJohn Wedig      *
862098dabeSJohn Wedig      *  @param[in] eraseType - type of erase operation.
872098dabeSJohn Wedig      */
8867a47446SJohn Wedig     void erase(Volume::EraseMethod eraseType);
892098dabeSJohn Wedig 
902098dabeSJohn Wedig     /** @brief Unmount filesystem and lock the LUKS device.
912098dabeSJohn Wedig      */
9267a47446SJohn Wedig     void lock();
932098dabeSJohn Wedig 
942098dabeSJohn Wedig     /** @brief Unlock device and mount the filesystem.
952098dabeSJohn Wedig      *
962098dabeSJohn Wedig      *  @param[in] password - password for the LUKS device.
972098dabeSJohn Wedig      */
9867a47446SJohn Wedig     void unlock(std::vector<uint8_t> password);
992098dabeSJohn Wedig 
1002098dabeSJohn Wedig     /** @brief Change the password for the LUKS device.
1012098dabeSJohn Wedig      *
1022098dabeSJohn Wedig      *  @param[in] oldPassword - old password for the LUKS device.
1032098dabeSJohn Wedig      *  @param[in] newPassword - new password for the LUKS device.
1042098dabeSJohn Wedig      */
10567a47446SJohn Wedig     void changePassword(const std::vector<uint8_t>& oldPassword,
10667a47446SJohn Wedig                         const std::vector<uint8_t>& newPassword);
1072098dabeSJohn Wedig 
108b810c926SJohn Wedig     /** @brief Check if the LUKS device is currently locked. */
109b810c926SJohn Wedig     bool isLocked() const;
110b810c926SJohn Wedig 
111b810c926SJohn Wedig     /** @brief Get the mount point for the filesystem on the LUKS device. */
112b810c926SJohn Wedig     std::string_view getMountPoint() const;
113b810c926SJohn Wedig 
1142443a021SJohn Wedig     /** @brief Get the path to the mapped crypt device. */
1152443a021SJohn Wedig     std::string_view getCryptDevicePath() const;
1162443a021SJohn Wedig 
1172098dabeSJohn Wedig   private:
118b810c926SJohn Wedig     /** @brief Full path of the device file, e.g. /dev/mmcblk0. */
1192098dabeSJohn Wedig     std::string devPath;
1202098dabeSJohn Wedig 
121b810c926SJohn Wedig     /** @brief Name of the LUKS container. */
1222098dabeSJohn Wedig     std::string containerName;
123b810c926SJohn Wedig 
124b810c926SJohn Wedig     /** @brief Mount point for the filesystem. */
125b810c926SJohn Wedig     std::string mountPoint;
126b810c926SJohn Wedig 
127043af59fSTom Tung     /** @brief Max geometry to erase. */
128043af59fSTom Tung     uint64_t eraseMaxGeometry;
129043af59fSTom Tung 
130043af59fSTom Tung     /** @brief Min geometry to erase. */
131043af59fSTom Tung     uint64_t eraseMinGeometry;
132043af59fSTom Tung 
13367a47446SJohn Wedig     /** @brief Indicates whether the LUKS device is currently locked. */
1346771c691SJohn Edward Broadbent     bool lockedProperty{false};
13567a47446SJohn Wedig 
136b810c926SJohn Wedig     /** @brief Pointer to cryptsetup interface object.
137b810c926SJohn Wedig      *  @details This is used to mock out the cryptsetup functions.
138b810c926SJohn Wedig      */
139b810c926SJohn Wedig     std::unique_ptr<CryptsetupInterface> cryptIface;
140b810c926SJohn Wedig 
141b810c926SJohn Wedig     /** @brief Pointer to filesystem interface object.
142b810c926SJohn Wedig      *  @details This is used to mock out filesystem operations.
143b810c926SJohn Wedig      */
144b810c926SJohn Wedig     std::unique_ptr<FilesystemInterface> fsIface;
145b810c926SJohn Wedig 
1462443a021SJohn Wedig     /** @brief Path where the mapped crypt device gets created. */
1472443a021SJohn Wedig     const std::string cryptDevicePath;
1482443a021SJohn Wedig 
14967a47446SJohn Wedig     /** @brief D-Bus object server. */
15067a47446SJohn Wedig     sdbusplus::asio::object_server& objectServer;
15167a47446SJohn Wedig 
15267a47446SJohn Wedig     /** @brief D-Bus interface for the logical volume. */
15367a47446SJohn Wedig     std::shared_ptr<sdbusplus::asio::dbus_interface> volumeInterface;
15467a47446SJohn Wedig 
15567a47446SJohn Wedig     /** @brief D-Bus interface for the physical drive. */
15667a47446SJohn Wedig     std::shared_ptr<sdbusplus::asio::dbus_interface> driveInterface;
15767a47446SJohn Wedig 
15819825057SRahul Kapoor     /** @brief D-Bus interface for the location type of the drive. */
15949796415SJohn Edward Broadbent     std::shared_ptr<sdbusplus::asio::dbus_interface> embeddedLocationInterface;
160740e94bdSJohn Edward Broadbent 
16119825057SRahul Kapoor     /** @brief D-Bus interface for the location code of the drive. */
16219825057SRahul Kapoor     std::shared_ptr<sdbusplus::asio::dbus_interface> locationCodeInterface;
16319825057SRahul Kapoor 
164b4838308SJohn Wedig     /** @brief D-Bus interface for the asset information. */
165b4838308SJohn Wedig     std::shared_ptr<sdbusplus::asio::dbus_interface> assetInterface;
166b4838308SJohn Wedig 
1676c0d8ce1SJohn Wedig     /** @brief Association between chassis and drive. */
1686c0d8ce1SJohn Wedig     std::shared_ptr<sdbusplus::asio::dbus_interface> association;
1696c0d8ce1SJohn Wedig 
17091c1ec1bSJohn Edward Broadbent     /** @brief Indicates whether the LUKS header is on the disk. */
1716771c691SJohn Edward Broadbent     Drive::DriveEncryptionState encryptionStatus{
1726771c691SJohn Edward Broadbent         Drive::DriveEncryptionState::Unknown};
17391c1ec1bSJohn Edward Broadbent 
174b810c926SJohn Wedig     /** @brief Format LUKS encrypted device.
175b810c926SJohn Wedig      *
176b810c926SJohn Wedig      *  @param[in] password - password to set for the LUKS device.
177b810c926SJohn Wedig      */
178b2c86be3SJohn Edward Broadbent     void formatLuksDev(std::vector<uint8_t> password);
179b810c926SJohn Wedig 
18091c1ec1bSJohn Edward Broadbent     /** @brief check the LUKS header, for devPath
18191c1ec1bSJohn Edward Broadbent      *
18291c1ec1bSJohn Edward Broadbent      *  @returns a CryptHandle to the LUKS drive
18391c1ec1bSJohn Edward Broadbent      */
18491c1ec1bSJohn Edward Broadbent     CryptHandle loadLuksHeader();
18591c1ec1bSJohn Edward Broadbent 
186b810c926SJohn Wedig     /** @brief Unlock the device.
187b810c926SJohn Wedig      *
188b810c926SJohn Wedig      *  @param[in] password - password to activate the LUKS device.
189b810c926SJohn Wedig      */
19091c1ec1bSJohn Edward Broadbent 
19191c1ec1bSJohn Edward Broadbent     Drive::DriveEncryptionState findEncryptionStatus();
19291c1ec1bSJohn Edward Broadbent 
193b2c86be3SJohn Edward Broadbent     void activateLuksDev(std::vector<uint8_t> password);
194b810c926SJohn Wedig 
195b810c926SJohn Wedig     /** @brief Create the filesystem on the LUKS device.
196b810c926SJohn Wedig      *  @details The LUKS device should already be activated, i.e. unlocked.
197b810c926SJohn Wedig      */
198b810c926SJohn Wedig     void createFilesystem();
199b810c926SJohn Wedig 
200b810c926SJohn Wedig     /** @brief Deactivate the LUKS device.
201b810c926SJohn Wedig      *  @details The filesystem is assumed to be unmounted already.
202b810c926SJohn Wedig      */
203b810c926SJohn Wedig     void deactivateLuksDev();
204b810c926SJohn Wedig 
205b810c926SJohn Wedig     /** @brief Mount the filesystem.
206b810c926SJohn Wedig      *  @details The filesystem should already exist and the LUKS device should
207b810c926SJohn Wedig      *  be unlocked already.
208b810c926SJohn Wedig      */
209b810c926SJohn Wedig     void mountFilesystem();
210b810c926SJohn Wedig 
211b810c926SJohn Wedig     /** @brief Unmount the filesystem. */
212b810c926SJohn Wedig     void unmountFilesystem();
2132098dabeSJohn Wedig };
2142098dabeSJohn Wedig 
2152098dabeSJohn Wedig } // namespace estoraged
216