blob: 3a6a8bb0ce4a2ba202a3ee87adfa1028c59af2cd [file] [log] [blame]
John Wedig2098dab2021-09-14 13:56:28 -07001
2#include "estoraged.hpp"
3
John Edward Broadbent59dffa62022-01-13 17:41:32 -08004#include "cryptErase.hpp"
John Wedigb810c922021-11-17 16:38:03 -08005#include "cryptsetupInterface.hpp"
John Edward Broadbent7f2ab642021-11-11 21:00:38 -08006#include "pattern.hpp"
John Edward Broadbent605085a2021-11-05 13:45:45 -07007#include "sanitize.hpp"
John Edward Broadbente6ffe702021-10-14 14:03:11 -07008#include "verifyDriveGeometry.hpp"
John Edward Broadbent4bc8a102021-12-30 16:11:49 -08009#include "zero.hpp"
John Edward Broadbent4e13b0a2021-11-15 15:21:59 -080010
John Wedigb810c922021-11-17 16:38:03 -080011#include <libcryptsetup.h>
12#include <openssl/rand.h>
John Wedigb810c922021-11-17 16:38:03 -080013
14#include <phosphor-logging/lg2.hpp>
John Wedig67a47442022-04-05 17:21:29 -070015#include <sdbusplus/asio/object_server.hpp>
John Wedig972c3fa2021-12-29 17:30:41 -080016#include <xyz/openbmc_project/Common/error.hpp>
John Wedigb810c922021-11-17 16:38:03 -080017
Ed Tanous82897c32022-02-21 14:11:59 -080018#include <cstdlib>
John Wedigb810c922021-11-17 16:38:03 -080019#include <filesystem>
John Wedig2098dab2021-09-14 13:56:28 -070020#include <iostream>
John Wedig67a47442022-04-05 17:21:29 -070021#include <string>
John Wedigb810c922021-11-17 16:38:03 -080022#include <string_view>
John Wedig67a47442022-04-05 17:21:29 -070023#include <utility>
John Wedig2098dab2021-09-14 13:56:28 -070024#include <vector>
25
26namespace estoraged
27{
28
John Wedig6c0d8ce2022-04-22 14:00:43 -070029using Association = std::tuple<std::string, std::string, std::string>;
John Wedig972c3fa2021-12-29 17:30:41 -080030using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
John Wedig972c3fa2021-12-29 17:30:41 -080031using sdbusplus::xyz::openbmc_project::Common::Error::UnsupportedRequest;
John Edward Broadbent91c1ec12022-05-20 16:51:43 -070032using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Drive;
John Wedig67a47442022-04-05 17:21:29 -070033using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Volume;
John Wedigb810c922021-11-17 16:38:03 -080034
John Wedig67a47442022-04-05 17:21:29 -070035EStoraged::EStoraged(sdbusplus::asio::object_server& server,
John Wedig6c0d8ce2022-04-22 14:00:43 -070036 const std::string& configPath, const std::string& devPath,
37 const std::string& luksName, uint64_t size,
John Wedigb4838302022-07-22 13:51:16 -070038 uint8_t lifeTime, const std::string& partNumber,
39 const std::string& serialNumber,
John Wedig67a47442022-04-05 17:21:29 -070040 std::unique_ptr<CryptsetupInterface> cryptInterface,
41 std::unique_ptr<FilesystemInterface> fsInterface) :
42 devPath(devPath),
43 containerName(luksName), mountPoint("/mnt/" + luksName + "_fs"),
John Edward Broadbent6771c692022-06-22 19:49:27 -070044 cryptIface(std::move(cryptInterface)), fsIface(std::move(fsInterface)),
John Wedig2443a022023-03-17 13:42:32 -070045 cryptDevicePath(cryptIface->cryptGetDir() + "/" + luksName),
John Edward Broadbent6771c692022-06-22 19:49:27 -070046 objectServer(server)
John Wedig67a47442022-04-05 17:21:29 -070047{
48 /* Get the filename of the device (without "/dev/"). */
49 std::string deviceName = std::filesystem::path(devPath).filename().string();
50 /* DBus object path */
Patrick Williams04c28fa2023-05-10 07:51:24 -050051 std::string objectPath = "/xyz/openbmc_project/inventory/storage/" +
52 deviceName;
John Wedig67a47442022-04-05 17:21:29 -070053
54 /* Add Volume interface. */
55 volumeInterface = objectServer.add_interface(
John Wedig6c0d8ce2022-04-22 14:00:43 -070056 objectPath, "xyz.openbmc_project.Inventory.Item.Volume");
John Wedig67a47442022-04-05 17:21:29 -070057 volumeInterface->register_method(
58 "FormatLuks", [this](const std::vector<uint8_t>& password,
59 Volume::FilesystemType type) {
60 this->formatLuks(password, type);
61 });
Patrick Williams04c28fa2023-05-10 07:51:24 -050062 volumeInterface->register_method("Erase",
63 [this](Volume::EraseMethod eraseType) {
64 this->erase(eraseType);
65 });
John Wedig67a47442022-04-05 17:21:29 -070066 volumeInterface->register_method("Lock", [this]() { this->lock(); });
Patrick Williams04c28fa2023-05-10 07:51:24 -050067 volumeInterface->register_method("Unlock",
68 [this](std::vector<uint8_t>& password) {
69 this->unlock(password);
70 });
John Wedig67a47442022-04-05 17:21:29 -070071 volumeInterface->register_method(
72 "ChangePassword", [this](const std::vector<uint8_t>& oldPassword,
73 const std::vector<uint8_t>& newPassword) {
74 this->changePassword(oldPassword, newPassword);
75 });
76 volumeInterface->register_property_r(
77 "Locked", lockedProperty, sdbusplus::vtable::property_::emits_change,
78 [this](bool& value) {
Patrick Williams04c28fa2023-05-10 07:51:24 -050079 value = this->isLocked();
80 return value;
John Wedig67a47442022-04-05 17:21:29 -070081 });
82
83 /* Add Drive interface. */
84 driveInterface = objectServer.add_interface(
John Wedig6c0d8ce2022-04-22 14:00:43 -070085 objectPath, "xyz.openbmc_project.Inventory.Item.Drive");
John Wedig67a47442022-04-05 17:21:29 -070086 driveInterface->register_property("Capacity", size);
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070087 driveInterface->register_property("PredictedMediaLifeLeftPercent",
88 lifeTime);
John Edward Broadbent14aee772022-04-20 13:46:48 -070089 /* This registers the Locked property for the Drives interface.
90 * Now it is the same as the volume Locked property */
91 driveInterface->register_property_r(
92 "Locked", lockedProperty, sdbusplus::vtable::property_::emits_change,
93 [this](bool& value) {
Patrick Williams04c28fa2023-05-10 07:51:24 -050094 value = this->isLocked();
95 return value;
John Edward Broadbent14aee772022-04-20 13:46:48 -070096 });
John Wedig67a47442022-04-05 17:21:29 -070097
John Edward Broadbent91c1ec12022-05-20 16:51:43 -070098 driveInterface->register_property_r(
99 "EncryptionStatus", encryptionStatus,
100 sdbusplus::vtable::property_::emits_change,
101 [this](Drive::DriveEncryptionState& value) {
Patrick Williams04c28fa2023-05-10 07:51:24 -0500102 value = this->findEncryptionStatus();
103 return value;
John Edward Broadbent91c1ec12022-05-20 16:51:43 -0700104 });
105
John Edward Broadbent49796412022-06-22 18:31:52 -0700106 embeddedLocationInterface = objectServer.add_interface(
107 objectPath, "xyz.openbmc_project.Inventory.Connector.Embedded");
John Edward Broadbent740e94b2022-06-10 19:42:30 -0700108
John Wedigb4838302022-07-22 13:51:16 -0700109 /* Add Asset interface. */
110 assetInterface = objectServer.add_interface(
111 objectPath, "xyz.openbmc_project.Inventory.Decorator.Asset");
112 assetInterface->register_property("PartNumber", partNumber);
113 assetInterface->register_property("SerialNumber", serialNumber);
114
John Wedig67a47442022-04-05 17:21:29 -0700115 volumeInterface->initialize();
116 driveInterface->initialize();
John Edward Broadbent49796412022-06-22 18:31:52 -0700117 embeddedLocationInterface->initialize();
John Wedigb4838302022-07-22 13:51:16 -0700118 assetInterface->initialize();
John Wedig6c0d8ce2022-04-22 14:00:43 -0700119
120 /* Set up the association between chassis and drive. */
121 association = objectServer.add_interface(
122 objectPath, "xyz.openbmc_project.Association.Definitions");
123
124 std::vector<Association> associations;
125 associations.emplace_back("chassis", "drive",
126 std::filesystem::path(configPath).parent_path());
127 association->register_property("Associations", associations);
128 association->initialize();
John Wedig67a47442022-04-05 17:21:29 -0700129}
130
131EStoraged::~EStoraged()
132{
133 objectServer.remove_interface(volumeInterface);
134 objectServer.remove_interface(driveInterface);
John Edward Broadbent49796412022-06-22 18:31:52 -0700135 objectServer.remove_interface(embeddedLocationInterface);
John Wedigb4838302022-07-22 13:51:16 -0700136 objectServer.remove_interface(assetInterface);
John Wedig6c0d8ce2022-04-22 14:00:43 -0700137 objectServer.remove_interface(association);
John Wedig67a47442022-04-05 17:21:29 -0700138}
139
140void EStoraged::formatLuks(const std::vector<uint8_t>& password,
141 Volume::FilesystemType type)
John Wedig2098dab2021-09-14 13:56:28 -0700142{
John Edward Broadbent4e13b0a2021-11-15 15:21:59 -0800143 std::string msg = "OpenBMC.0.1.DriveFormat";
144 lg2::info("Starting format", "REDFISH_MESSAGE_ID", msg);
John Wedigb810c922021-11-17 16:38:03 -0800145
John Wedig67a47442022-04-05 17:21:29 -0700146 if (type != Volume::FilesystemType::ext4)
John Wedig972c3fa2021-12-29 17:30:41 -0800147 {
148 lg2::error("Only ext4 filesystems are supported currently",
149 "REDFISH_MESSAGE_ID", std::string("OpenBMC.0.1.FormatFail"));
150 throw UnsupportedRequest();
151 }
152
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700153 formatLuksDev(password);
154 activateLuksDev(password);
John Wedigb810c922021-11-17 16:38:03 -0800155
156 createFilesystem();
157 mountFilesystem();
John Wedig2098dab2021-09-14 13:56:28 -0700158}
159
John Wedig67a47442022-04-05 17:21:29 -0700160void EStoraged::erase(Volume::EraseMethod inEraseMethod)
John Wedig2098dab2021-09-14 13:56:28 -0700161{
162 std::cerr << "Erasing encrypted eMMC" << std::endl;
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700163 lg2::info("Starting erase", "REDFISH_MESSAGE_ID",
164 std::string("OpenBMC.0.1.DriveErase"));
165 switch (inEraseMethod)
166 {
John Wedig67a47442022-04-05 17:21:29 -0700167 case Volume::EraseMethod::CryptoErase:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700168 {
John Edward Broadbent59dffa62022-01-13 17:41:32 -0800169 CryptErase myCryptErase(devPath);
170 myCryptErase.doErase();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700171 break;
172 }
John Wedig67a47442022-04-05 17:21:29 -0700173 case Volume::EraseMethod::VerifyGeometry:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700174 {
175 VerifyDriveGeometry myVerifyGeometry(devPath);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -0700176 myVerifyGeometry.geometryOkay();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700177 break;
178 }
John Wedig67a47442022-04-05 17:21:29 -0700179 case Volume::EraseMethod::LogicalOverWrite:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700180 {
John Edward Broadbent7f2ab642021-11-11 21:00:38 -0800181 Pattern myErasePattern(devPath);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -0700182 myErasePattern.writePattern();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700183 break;
184 }
John Wedig67a47442022-04-05 17:21:29 -0700185 case Volume::EraseMethod::LogicalVerify:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700186 {
John Edward Broadbent7f2ab642021-11-11 21:00:38 -0800187 Pattern myErasePattern(devPath);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -0700188 myErasePattern.verifyPattern();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700189 break;
190 }
John Wedig67a47442022-04-05 17:21:29 -0700191 case Volume::EraseMethod::VendorSanitize:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700192 {
John Edward Broadbent605085a2021-11-05 13:45:45 -0700193 Sanitize mySanitize(devPath);
194 mySanitize.doSanitize();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700195 break;
196 }
John Wedig67a47442022-04-05 17:21:29 -0700197 case Volume::EraseMethod::ZeroOverWrite:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700198 {
John Edward Broadbent4bc8a102021-12-30 16:11:49 -0800199 Zero myZero(devPath);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -0700200 myZero.writeZero();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700201 break;
202 }
John Wedig67a47442022-04-05 17:21:29 -0700203 case Volume::EraseMethod::ZeroVerify:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700204 {
John Edward Broadbent4bc8a102021-12-30 16:11:49 -0800205 Zero myZero(devPath);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -0700206 myZero.verifyZero();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700207 break;
208 }
John Wedig67a47442022-04-05 17:21:29 -0700209 case Volume::EraseMethod::SecuredLocked:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700210 {
John Wedig47cd7992022-10-05 15:45:11 -0700211 if (!isLocked())
John Edward Broadbentf59b7292022-02-15 15:07:15 -0800212 {
213 lock();
214 }
215 // TODO: implement hardware locking
216 // Until that is done, we can lock using eStoraged::lock()
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700217 break;
218 }
219 }
John Wedig2098dab2021-09-14 13:56:28 -0700220}
221
Ed Tanous82897c32022-02-21 14:11:59 -0800222void EStoraged::lock()
John Wedig2098dab2021-09-14 13:56:28 -0700223{
John Edward Broadbent4e13b0a2021-11-15 15:21:59 -0800224 std::string msg = "OpenBMC.0.1.DriveLock";
225 lg2::info("Starting lock", "REDFISH_MESSAGE_ID", msg);
John Wedigb810c922021-11-17 16:38:03 -0800226
227 unmountFilesystem();
228 deactivateLuksDev();
John Wedig2098dab2021-09-14 13:56:28 -0700229}
230
Ed Tanous82897c32022-02-21 14:11:59 -0800231void EStoraged::unlock(std::vector<uint8_t> password)
John Wedig2098dab2021-09-14 13:56:28 -0700232{
John Edward Broadbent4e13b0a2021-11-15 15:21:59 -0800233 std::string msg = "OpenBMC.0.1.DriveUnlock";
234 lg2::info("Starting unlock", "REDFISH_MESSAGE_ID", msg);
John Wedigb810c922021-11-17 16:38:03 -0800235
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700236 activateLuksDev(std::move(password));
John Wedigb810c922021-11-17 16:38:03 -0800237 mountFilesystem();
John Wedig2098dab2021-09-14 13:56:28 -0700238}
239
John Wedig8d5a3a02022-09-29 15:25:58 -0700240void EStoraged::changePassword(const std::vector<uint8_t>& oldPassword,
241 const std::vector<uint8_t>& newPassword)
John Wedig2098dab2021-09-14 13:56:28 -0700242{
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700243 lg2::info("Starting change password", "REDFISH_MESSAGE_ID",
244 std::string("OpenBMC.0.1.DrivePasswordChanged"));
John Wedig8d5a3a02022-09-29 15:25:58 -0700245
246 CryptHandle cryptHandle = loadLuksHeader();
247
248 int retval = cryptIface->cryptKeyslotChangeByPassphrase(
249 cryptHandle.get(), CRYPT_ANY_SLOT, CRYPT_ANY_SLOT,
250 reinterpret_cast<const char*>(oldPassword.data()), oldPassword.size(),
251 reinterpret_cast<const char*>(newPassword.data()), newPassword.size());
252 if (retval < 0)
253 {
254 lg2::error("Failed to change password", "REDFISH_MESSAGE_ID",
255 std::string("OpenBMC.0.1.DrivePasswordChangeFail"));
256 throw InternalFailure();
257 }
258
259 lg2::info("Successfully changed password for {DEV}", "DEV", devPath,
260 "REDFISH_MESSAGE_ID",
261 std::string("OpenBMC.0.1.DrivePasswordChangeSuccess"));
John Wedig2098dab2021-09-14 13:56:28 -0700262}
263
Ed Tanous82897c32022-02-21 14:11:59 -0800264bool EStoraged::isLocked() const
John Wedigb810c922021-11-17 16:38:03 -0800265{
John Wedig2443a022023-03-17 13:42:32 -0700266 /*
267 * Check if the mapped virtual device exists. If it exists, the LUKS volume
268 * is unlocked.
269 */
270 try
271 {
272 std::filesystem::path mappedDevicePath(cryptDevicePath);
273 return (std::filesystem::exists(mappedDevicePath) == false);
274 }
275 catch (const std::exception& e)
276 {
277 lg2::error("Failed to query locked status: {EXCEPT}", "EXCEPT",
278 e.what(), "REDFISH_MESSAGE_ID",
279 std::string("OpenBMC.0.1.IsLockedFail"));
280 /* If we couldn't query the filesystem path, assume unlocked. */
281 return false;
282 }
John Wedigb810c922021-11-17 16:38:03 -0800283}
284
Ed Tanous82897c32022-02-21 14:11:59 -0800285std::string_view EStoraged::getMountPoint() const
John Wedigb810c922021-11-17 16:38:03 -0800286{
287 return mountPoint;
288}
289
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700290void EStoraged::formatLuksDev(std::vector<uint8_t> password)
John Wedigb810c922021-11-17 16:38:03 -0800291{
292 lg2::info("Formatting device {DEV}", "DEV", devPath, "REDFISH_MESSAGE_ID",
293 std::string("OpenBMC.0.1.FormatLuksDev"));
294
295 /* Generate the volume key. */
296 const std::size_t keySize = 64;
297 std::vector<uint8_t> volumeKey(keySize);
298 if (RAND_bytes(volumeKey.data(), keySize) != 1)
299 {
300 lg2::error("Failed to create volume key", "REDFISH_MESSAGE_ID",
301 std::string("OpenBMC.0.1.FormatLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800302 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800303 }
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700304
305 /* Create the handle. */
306 CryptHandle cryptHandle(devPath);
307
John Wedigb810c922021-11-17 16:38:03 -0800308 /* Format the LUKS encrypted device. */
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700309 int retval = cryptIface->cryptFormat(
310 cryptHandle.get(), CRYPT_LUKS2, "aes", "xts-plain64", nullptr,
311 reinterpret_cast<const char*>(volumeKey.data()), volumeKey.size(),
312 nullptr);
John Wedigb810c922021-11-17 16:38:03 -0800313 if (retval < 0)
314 {
315 lg2::error("Failed to format encrypted device: {RETVAL}", "RETVAL",
316 retval, "REDFISH_MESSAGE_ID",
317 std::string("OpenBMC.0.1.FormatLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800318 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800319 }
320
John Wedigb810c922021-11-17 16:38:03 -0800321 /* Set the password. */
322 retval = cryptIface->cryptKeyslotAddByVolumeKey(
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700323 cryptHandle.get(), CRYPT_ANY_SLOT, nullptr, 0,
John Wedigb810c922021-11-17 16:38:03 -0800324 reinterpret_cast<const char*>(password.data()), password.size());
325
326 if (retval < 0)
327 {
328 lg2::error("Failed to set encryption password", "REDFISH_MESSAGE_ID",
329 std::string("OpenBMC.0.1.FormatLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800330 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800331 }
332
333 lg2::info("Encrypted device {DEV} successfully formatted", "DEV", devPath,
334 "REDFISH_MESSAGE_ID",
335 std::string("OpenBMC.0.1.FormatLuksDevSuccess"));
336}
337
John Edward Broadbent91c1ec12022-05-20 16:51:43 -0700338CryptHandle EStoraged::loadLuksHeader()
John Wedigb810c922021-11-17 16:38:03 -0800339{
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700340 CryptHandle cryptHandle(devPath);
341
342 int retval = cryptIface->cryptLoad(cryptHandle.get(), CRYPT_LUKS2, nullptr);
John Wedigb810c922021-11-17 16:38:03 -0800343 if (retval < 0)
344 {
345 lg2::error("Failed to load LUKS header: {RETVAL}", "RETVAL", retval,
346 "REDFISH_MESSAGE_ID",
347 std::string("OpenBMC.0.1.ActivateLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800348 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800349 }
John Edward Broadbent91c1ec12022-05-20 16:51:43 -0700350 return cryptHandle;
351}
John Wedigb810c922021-11-17 16:38:03 -0800352
John Edward Broadbent91c1ec12022-05-20 16:51:43 -0700353Drive::DriveEncryptionState EStoraged::findEncryptionStatus()
354{
355 try
356 {
357 loadLuksHeader();
358 return Drive::DriveEncryptionState::Encrypted;
359 }
360 catch (...)
361 {
362 return Drive::DriveEncryptionState::Unknown;
363 }
364}
365
366void EStoraged::activateLuksDev(std::vector<uint8_t> password)
367{
368 lg2::info("Activating LUKS dev {DEV}", "DEV", devPath, "REDFISH_MESSAGE_ID",
369 std::string("OpenBMC.0.1.ActivateLuksDev"));
370
371 /* Create the handle. */
372 CryptHandle cryptHandle = loadLuksHeader();
373
374 int retval = cryptIface->cryptActivateByPassphrase(
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700375 cryptHandle.get(), containerName.c_str(), CRYPT_ANY_SLOT,
John Wedigb810c922021-11-17 16:38:03 -0800376 reinterpret_cast<const char*>(password.data()), password.size(), 0);
377
378 if (retval < 0)
379 {
380 lg2::error("Failed to activate LUKS dev: {RETVAL}", "RETVAL", retval,
381 "REDFISH_MESSAGE_ID",
382 std::string("OpenBMC.0.1.ActivateLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800383 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800384 }
385
John Wedigb810c922021-11-17 16:38:03 -0800386 lg2::info("Successfully activated LUKS dev {DEV}", "DEV", devPath,
387 "REDFISH_MESSAGE_ID",
388 std::string("OpenBMC.0.1.ActivateLuksDevSuccess"));
389}
390
Ed Tanous82897c32022-02-21 14:11:59 -0800391void EStoraged::createFilesystem()
John Wedigb810c922021-11-17 16:38:03 -0800392{
393 /* Run the command to create the filesystem. */
John Wedig2443a022023-03-17 13:42:32 -0700394 int retval = fsIface->runMkfs(cryptDevicePath);
Ed Tanous82897c32022-02-21 14:11:59 -0800395 if (retval != 0)
John Wedigb810c922021-11-17 16:38:03 -0800396 {
397 lg2::error("Failed to create filesystem: {RETVAL}", "RETVAL", retval,
398 "REDFISH_MESSAGE_ID",
399 std::string("OpenBMC.0.1.CreateFilesystemFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800400 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800401 }
John Wedig2443a022023-03-17 13:42:32 -0700402 lg2::info("Successfully created filesystem for {CONTAINER}", "CONTAINER",
403 cryptDevicePath, "REDFISH_MESSAGE_ID",
John Wedigb810c922021-11-17 16:38:03 -0800404 std::string("OpenBMC.0.1.CreateFilesystemSuccess"));
405}
406
Ed Tanous82897c32022-02-21 14:11:59 -0800407void EStoraged::mountFilesystem()
John Wedigb810c922021-11-17 16:38:03 -0800408{
John Wedigb17f8252022-01-12 14:24:26 -0800409 /*
410 * Create directory for the filesystem, if it's not already present. It
411 * might already exist if, for example, the BMC reboots after creating the
412 * directory.
413 */
414 if (!fsIface->directoryExists(std::filesystem::path(mountPoint)))
John Wedigb810c922021-11-17 16:38:03 -0800415 {
John Wedigb17f8252022-01-12 14:24:26 -0800416 bool success =
417 fsIface->createDirectory(std::filesystem::path(mountPoint));
418 if (!success)
419 {
420 lg2::error("Failed to create mount point: {DIR}", "DIR", mountPoint,
421 "REDFISH_MESSAGE_ID",
422 std::string("OpenBMC.0.1.MountFilesystemFail"));
423 throw InternalFailure();
424 }
John Wedigb810c922021-11-17 16:38:03 -0800425 }
426
427 /* Run the command to mount the filesystem. */
John Wedig2443a022023-03-17 13:42:32 -0700428 int retval = fsIface->doMount(cryptDevicePath.c_str(), mountPoint.c_str(),
John Wedigb810c922021-11-17 16:38:03 -0800429 "ext4", 0, nullptr);
Ed Tanous82897c32022-02-21 14:11:59 -0800430 if (retval != 0)
John Wedigb810c922021-11-17 16:38:03 -0800431 {
432 lg2::error("Failed to mount filesystem: {RETVAL}", "RETVAL", retval,
433 "REDFISH_MESSAGE_ID",
434 std::string("OpenBMC.0.1.MountFilesystemFail"));
435 bool removeSuccess =
436 fsIface->removeDirectory(std::filesystem::path(mountPoint));
437 if (!removeSuccess)
438 {
439 lg2::error("Failed to remove mount point: {DIR}", "DIR", mountPoint,
440 "REDFISH_MESSAGE_ID",
441 std::string("OpenBMC.0.1.MountFilesystemFail"));
442 }
John Wedig972c3fa2021-12-29 17:30:41 -0800443 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800444 }
445
446 lg2::info("Successfully mounted filesystem at {DIR}", "DIR", mountPoint,
447 "REDFISH_MESSAGE_ID",
448 std::string("OpenBMC.0.1.MountFilesystemSuccess"));
449}
450
Ed Tanous82897c32022-02-21 14:11:59 -0800451void EStoraged::unmountFilesystem()
John Wedigb810c922021-11-17 16:38:03 -0800452{
453 int retval = fsIface->doUnmount(mountPoint.c_str());
Ed Tanous82897c32022-02-21 14:11:59 -0800454 if (retval != 0)
John Wedigb810c922021-11-17 16:38:03 -0800455 {
456 lg2::error("Failed to unmount filesystem: {RETVAL}", "RETVAL", retval,
457 "REDFISH_MESSAGE_ID",
458 std::string("OpenBMC.0.1.UnmountFilesystemFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800459 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800460 }
461
462 /* Remove the mount point. */
463 bool success = fsIface->removeDirectory(std::filesystem::path(mountPoint));
464 if (!success)
465 {
466 lg2::error("Failed to remove mount point {DIR}", "DIR", mountPoint,
467 "REDFISH_MESSAGE_ID",
468 std::string("OpenBMC.0.1.UnmountFilesystemFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800469 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800470 }
471
472 lg2::info("Successfully unmounted filesystem at {DIR}", "DIR", mountPoint,
473 "REDFISH_MESSAGE_ID",
474 std::string("OpenBMC.0.1.MountFilesystemSuccess"));
475}
476
Ed Tanous82897c32022-02-21 14:11:59 -0800477void EStoraged::deactivateLuksDev()
John Wedigb810c922021-11-17 16:38:03 -0800478{
479 lg2::info("Deactivating LUKS device {DEV}", "DEV", devPath,
480 "REDFISH_MESSAGE_ID",
481 std::string("OpenBMC.0.1.DeactivateLuksDev"));
482
483 int retval = cryptIface->cryptDeactivate(nullptr, containerName.c_str());
484 if (retval < 0)
485 {
486 lg2::error("Failed to deactivate crypt device: {RETVAL}", "RETVAL",
487 retval, "REDFISH_MESSAGE_ID",
488 std::string("OpenBMC.0.1.DeactivateLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800489 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800490 }
491
John Wedigb810c922021-11-17 16:38:03 -0800492 lg2::info("Successfully deactivated LUKS device {DEV}", "DEV", devPath,
493 "REDFISH_MESSAGE_ID",
494 std::string("OpenBMC.0.1.DeactivateLuksDevSuccess"));
495}
496
John Wedig2443a022023-03-17 13:42:32 -0700497std::string_view EStoraged::getCryptDevicePath() const
John Wedig67a47442022-04-05 17:21:29 -0700498{
John Wedig2443a022023-03-17 13:42:32 -0700499 return cryptDevicePath;
John Wedig67a47442022-04-05 17:21:29 -0700500}
501
John Wedig2098dab2021-09-14 13:56:28 -0700502} // namespace estoraged