blob: 93efaaee1bdb7492aa6b010bcea8b7757e25471f [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,
38 uint8_t lifeTime,
John Wedig67a47442022-04-05 17:21:29 -070039 std::unique_ptr<CryptsetupInterface> cryptInterface,
40 std::unique_ptr<FilesystemInterface> fsInterface) :
41 devPath(devPath),
42 containerName(luksName), mountPoint("/mnt/" + luksName + "_fs"),
43 lockedProperty(false), cryptIface(std::move(cryptInterface)),
John Edward Broadbent91c1ec12022-05-20 16:51:43 -070044 fsIface(std::move(fsInterface)), objectServer(server),
45 encryptionStatus(Drive::DriveEncryptionState::Unknown)
John Wedig67a47442022-04-05 17:21:29 -070046{
47 /* Get the filename of the device (without "/dev/"). */
48 std::string deviceName = std::filesystem::path(devPath).filename().string();
49 /* DBus object path */
John Wedig6c0d8ce2022-04-22 14:00:43 -070050 std::string objectPath =
51 "/xyz/openbmc_project/inventory/storage/" + deviceName;
John Wedig67a47442022-04-05 17:21:29 -070052
53 /* Add Volume interface. */
54 volumeInterface = objectServer.add_interface(
John Wedig6c0d8ce2022-04-22 14:00:43 -070055 objectPath, "xyz.openbmc_project.Inventory.Item.Volume");
John Wedig67a47442022-04-05 17:21:29 -070056 volumeInterface->register_method(
57 "FormatLuks", [this](const std::vector<uint8_t>& password,
58 Volume::FilesystemType type) {
59 this->formatLuks(password, type);
60 });
61 volumeInterface->register_method(
62 "Erase",
63 [this](Volume::EraseMethod eraseType) { this->erase(eraseType); });
64 volumeInterface->register_method("Lock", [this]() { this->lock(); });
65 volumeInterface->register_method(
66 "Unlock",
67 [this](std::vector<uint8_t>& password) { this->unlock(password); });
68 volumeInterface->register_method(
69 "ChangePassword", [this](const std::vector<uint8_t>& oldPassword,
70 const std::vector<uint8_t>& newPassword) {
71 this->changePassword(oldPassword, newPassword);
72 });
73 volumeInterface->register_property_r(
74 "Locked", lockedProperty, sdbusplus::vtable::property_::emits_change,
75 [this](bool& value) {
76 value = this->isLocked();
77 return value;
78 });
79
80 /* Add Drive interface. */
81 driveInterface = objectServer.add_interface(
John Wedig6c0d8ce2022-04-22 14:00:43 -070082 objectPath, "xyz.openbmc_project.Inventory.Item.Drive");
John Wedig67a47442022-04-05 17:21:29 -070083 driveInterface->register_property("Capacity", size);
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070084 driveInterface->register_property("PredictedMediaLifeLeftPercent",
85 lifeTime);
John Edward Broadbent14aee772022-04-20 13:46:48 -070086 /* This registers the Locked property for the Drives interface.
87 * Now it is the same as the volume Locked property */
88 driveInterface->register_property_r(
89 "Locked", lockedProperty, sdbusplus::vtable::property_::emits_change,
90 [this](bool& value) {
91 value = this->isLocked();
92 return value;
93 });
John Wedig67a47442022-04-05 17:21:29 -070094
John Edward Broadbent91c1ec12022-05-20 16:51:43 -070095 driveInterface->register_property_r(
96 "EncryptionStatus", encryptionStatus,
97 sdbusplus::vtable::property_::emits_change,
98 [this](Drive::DriveEncryptionState& value) {
99 value = this->findEncryptionStatus();
100 return value;
101 });
102
John Wedig67a47442022-04-05 17:21:29 -0700103 volumeInterface->initialize();
104 driveInterface->initialize();
John Wedig6c0d8ce2022-04-22 14:00:43 -0700105
106 /* Set up the association between chassis and drive. */
107 association = objectServer.add_interface(
108 objectPath, "xyz.openbmc_project.Association.Definitions");
109
110 std::vector<Association> associations;
111 associations.emplace_back("chassis", "drive",
112 std::filesystem::path(configPath).parent_path());
113 association->register_property("Associations", associations);
114 association->initialize();
John Wedig67a47442022-04-05 17:21:29 -0700115}
116
117EStoraged::~EStoraged()
118{
119 objectServer.remove_interface(volumeInterface);
120 objectServer.remove_interface(driveInterface);
John Wedig6c0d8ce2022-04-22 14:00:43 -0700121 objectServer.remove_interface(association);
John Wedig67a47442022-04-05 17:21:29 -0700122}
123
124void EStoraged::formatLuks(const std::vector<uint8_t>& password,
125 Volume::FilesystemType type)
John Wedig2098dab2021-09-14 13:56:28 -0700126{
John Edward Broadbent4e13b0a2021-11-15 15:21:59 -0800127 std::string msg = "OpenBMC.0.1.DriveFormat";
128 lg2::info("Starting format", "REDFISH_MESSAGE_ID", msg);
John Wedigb810c922021-11-17 16:38:03 -0800129
John Wedig67a47442022-04-05 17:21:29 -0700130 if (type != Volume::FilesystemType::ext4)
John Wedig972c3fa2021-12-29 17:30:41 -0800131 {
132 lg2::error("Only ext4 filesystems are supported currently",
133 "REDFISH_MESSAGE_ID", std::string("OpenBMC.0.1.FormatFail"));
134 throw UnsupportedRequest();
135 }
136
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700137 formatLuksDev(password);
138 activateLuksDev(password);
John Wedigb810c922021-11-17 16:38:03 -0800139
140 createFilesystem();
141 mountFilesystem();
John Wedig2098dab2021-09-14 13:56:28 -0700142}
143
John Wedig67a47442022-04-05 17:21:29 -0700144void EStoraged::erase(Volume::EraseMethod inEraseMethod)
John Wedig2098dab2021-09-14 13:56:28 -0700145{
146 std::cerr << "Erasing encrypted eMMC" << std::endl;
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700147 lg2::info("Starting erase", "REDFISH_MESSAGE_ID",
148 std::string("OpenBMC.0.1.DriveErase"));
149 switch (inEraseMethod)
150 {
John Wedig67a47442022-04-05 17:21:29 -0700151 case Volume::EraseMethod::CryptoErase:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700152 {
John Edward Broadbent59dffa62022-01-13 17:41:32 -0800153 CryptErase myCryptErase(devPath);
154 myCryptErase.doErase();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700155 break;
156 }
John Wedig67a47442022-04-05 17:21:29 -0700157 case Volume::EraseMethod::VerifyGeometry:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700158 {
159 VerifyDriveGeometry myVerifyGeometry(devPath);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -0700160 myVerifyGeometry.geometryOkay();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700161 break;
162 }
John Wedig67a47442022-04-05 17:21:29 -0700163 case Volume::EraseMethod::LogicalOverWrite:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700164 {
John Edward Broadbent7f2ab642021-11-11 21:00:38 -0800165 Pattern myErasePattern(devPath);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -0700166 myErasePattern.writePattern();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700167 break;
168 }
John Wedig67a47442022-04-05 17:21:29 -0700169 case Volume::EraseMethod::LogicalVerify:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700170 {
John Edward Broadbent7f2ab642021-11-11 21:00:38 -0800171 Pattern myErasePattern(devPath);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -0700172 myErasePattern.verifyPattern();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700173 break;
174 }
John Wedig67a47442022-04-05 17:21:29 -0700175 case Volume::EraseMethod::VendorSanitize:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700176 {
John Edward Broadbent605085a2021-11-05 13:45:45 -0700177 Sanitize mySanitize(devPath);
178 mySanitize.doSanitize();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700179 break;
180 }
John Wedig67a47442022-04-05 17:21:29 -0700181 case Volume::EraseMethod::ZeroOverWrite:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700182 {
John Edward Broadbent4bc8a102021-12-30 16:11:49 -0800183 Zero myZero(devPath);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -0700184 myZero.writeZero();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700185 break;
186 }
John Wedig67a47442022-04-05 17:21:29 -0700187 case Volume::EraseMethod::ZeroVerify:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700188 {
John Edward Broadbent4bc8a102021-12-30 16:11:49 -0800189 Zero myZero(devPath);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -0700190 myZero.verifyZero();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700191 break;
192 }
John Wedig67a47442022-04-05 17:21:29 -0700193 case Volume::EraseMethod::SecuredLocked:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700194 {
John Edward Broadbentf59b7292022-02-15 15:07:15 -0800195 if (isLocked())
196 {
197 lock();
198 }
199 // TODO: implement hardware locking
200 // Until that is done, we can lock using eStoraged::lock()
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700201 break;
202 }
203 }
John Wedig2098dab2021-09-14 13:56:28 -0700204}
205
Ed Tanous82897c32022-02-21 14:11:59 -0800206void EStoraged::lock()
John Wedig2098dab2021-09-14 13:56:28 -0700207{
John Edward Broadbent4e13b0a2021-11-15 15:21:59 -0800208 std::string msg = "OpenBMC.0.1.DriveLock";
209 lg2::info("Starting lock", "REDFISH_MESSAGE_ID", msg);
John Wedigb810c922021-11-17 16:38:03 -0800210
211 unmountFilesystem();
212 deactivateLuksDev();
John Wedig2098dab2021-09-14 13:56:28 -0700213}
214
Ed Tanous82897c32022-02-21 14:11:59 -0800215void EStoraged::unlock(std::vector<uint8_t> password)
John Wedig2098dab2021-09-14 13:56:28 -0700216{
John Edward Broadbent4e13b0a2021-11-15 15:21:59 -0800217 std::string msg = "OpenBMC.0.1.DriveUnlock";
218 lg2::info("Starting unlock", "REDFISH_MESSAGE_ID", msg);
John Wedigb810c922021-11-17 16:38:03 -0800219
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700220 activateLuksDev(std::move(password));
John Wedigb810c922021-11-17 16:38:03 -0800221 mountFilesystem();
John Wedig2098dab2021-09-14 13:56:28 -0700222}
223
John Wedig67a47442022-04-05 17:21:29 -0700224void EStoraged::changePassword(const std::vector<uint8_t>& /*oldPassword*/,
225 const std::vector<uint8_t>& /*newPassword*/)
John Wedig2098dab2021-09-14 13:56:28 -0700226{
227 std::cerr << "Changing password for encrypted eMMC" << std::endl;
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700228 lg2::info("Starting change password", "REDFISH_MESSAGE_ID",
229 std::string("OpenBMC.0.1.DrivePasswordChanged"));
John Wedig2098dab2021-09-14 13:56:28 -0700230}
231
Ed Tanous82897c32022-02-21 14:11:59 -0800232bool EStoraged::isLocked() const
John Wedigb810c922021-11-17 16:38:03 -0800233{
John Wedig67a47442022-04-05 17:21:29 -0700234 return lockedProperty;
John Wedigb810c922021-11-17 16:38:03 -0800235}
236
Ed Tanous82897c32022-02-21 14:11:59 -0800237std::string_view EStoraged::getMountPoint() const
John Wedigb810c922021-11-17 16:38:03 -0800238{
239 return mountPoint;
240}
241
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700242void EStoraged::formatLuksDev(std::vector<uint8_t> password)
John Wedigb810c922021-11-17 16:38:03 -0800243{
244 lg2::info("Formatting device {DEV}", "DEV", devPath, "REDFISH_MESSAGE_ID",
245 std::string("OpenBMC.0.1.FormatLuksDev"));
246
247 /* Generate the volume key. */
248 const std::size_t keySize = 64;
249 std::vector<uint8_t> volumeKey(keySize);
250 if (RAND_bytes(volumeKey.data(), keySize) != 1)
251 {
252 lg2::error("Failed to create volume key", "REDFISH_MESSAGE_ID",
253 std::string("OpenBMC.0.1.FormatLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800254 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800255 }
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700256
257 /* Create the handle. */
258 CryptHandle cryptHandle(devPath);
259
John Wedigb810c922021-11-17 16:38:03 -0800260 /* Format the LUKS encrypted device. */
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700261 int retval = cryptIface->cryptFormat(
262 cryptHandle.get(), CRYPT_LUKS2, "aes", "xts-plain64", nullptr,
263 reinterpret_cast<const char*>(volumeKey.data()), volumeKey.size(),
264 nullptr);
John Wedigb810c922021-11-17 16:38:03 -0800265 if (retval < 0)
266 {
267 lg2::error("Failed to format encrypted device: {RETVAL}", "RETVAL",
268 retval, "REDFISH_MESSAGE_ID",
269 std::string("OpenBMC.0.1.FormatLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800270 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800271 }
272
273 /* Device is now encrypted. */
274 locked(true);
275
276 /* Set the password. */
277 retval = cryptIface->cryptKeyslotAddByVolumeKey(
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700278 cryptHandle.get(), CRYPT_ANY_SLOT, nullptr, 0,
John Wedigb810c922021-11-17 16:38:03 -0800279 reinterpret_cast<const char*>(password.data()), password.size());
280
281 if (retval < 0)
282 {
283 lg2::error("Failed to set encryption password", "REDFISH_MESSAGE_ID",
284 std::string("OpenBMC.0.1.FormatLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800285 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800286 }
287
288 lg2::info("Encrypted device {DEV} successfully formatted", "DEV", devPath,
289 "REDFISH_MESSAGE_ID",
290 std::string("OpenBMC.0.1.FormatLuksDevSuccess"));
291}
292
John Edward Broadbent91c1ec12022-05-20 16:51:43 -0700293CryptHandle EStoraged::loadLuksHeader()
John Wedigb810c922021-11-17 16:38:03 -0800294{
John Wedigb810c922021-11-17 16:38:03 -0800295
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700296 CryptHandle cryptHandle(devPath);
297
298 int retval = cryptIface->cryptLoad(cryptHandle.get(), CRYPT_LUKS2, nullptr);
John Wedigb810c922021-11-17 16:38:03 -0800299 if (retval < 0)
300 {
301 lg2::error("Failed to load LUKS header: {RETVAL}", "RETVAL", retval,
302 "REDFISH_MESSAGE_ID",
303 std::string("OpenBMC.0.1.ActivateLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800304 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800305 }
John Edward Broadbent91c1ec12022-05-20 16:51:43 -0700306 return cryptHandle;
307}
John Wedigb810c922021-11-17 16:38:03 -0800308
John Edward Broadbent91c1ec12022-05-20 16:51:43 -0700309Drive::DriveEncryptionState EStoraged::findEncryptionStatus()
310{
311 try
312 {
313 loadLuksHeader();
314 return Drive::DriveEncryptionState::Encrypted;
315 }
316 catch (...)
317 {
318 return Drive::DriveEncryptionState::Unknown;
319 }
320}
321
322void EStoraged::activateLuksDev(std::vector<uint8_t> password)
323{
324 lg2::info("Activating LUKS dev {DEV}", "DEV", devPath, "REDFISH_MESSAGE_ID",
325 std::string("OpenBMC.0.1.ActivateLuksDev"));
326
327 /* Create the handle. */
328 CryptHandle cryptHandle = loadLuksHeader();
329
330 int retval = cryptIface->cryptActivateByPassphrase(
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700331 cryptHandle.get(), containerName.c_str(), CRYPT_ANY_SLOT,
John Wedigb810c922021-11-17 16:38:03 -0800332 reinterpret_cast<const char*>(password.data()), password.size(), 0);
333
334 if (retval < 0)
335 {
336 lg2::error("Failed to activate LUKS dev: {RETVAL}", "RETVAL", retval,
337 "REDFISH_MESSAGE_ID",
338 std::string("OpenBMC.0.1.ActivateLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800339 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800340 }
341
342 /* Device is now unlocked. */
343 locked(false);
344
345 lg2::info("Successfully activated LUKS dev {DEV}", "DEV", devPath,
346 "REDFISH_MESSAGE_ID",
347 std::string("OpenBMC.0.1.ActivateLuksDevSuccess"));
348}
349
Ed Tanous82897c32022-02-21 14:11:59 -0800350void EStoraged::createFilesystem()
John Wedigb810c922021-11-17 16:38:03 -0800351{
352 /* Run the command to create the filesystem. */
353 int retval = fsIface->runMkfs(containerName);
Ed Tanous82897c32022-02-21 14:11:59 -0800354 if (retval != 0)
John Wedigb810c922021-11-17 16:38:03 -0800355 {
356 lg2::error("Failed to create filesystem: {RETVAL}", "RETVAL", retval,
357 "REDFISH_MESSAGE_ID",
358 std::string("OpenBMC.0.1.CreateFilesystemFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800359 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800360 }
361 lg2::info("Successfully created filesystem for /dev/mapper/{CONTAINER}",
362 "CONTAINER", containerName, "REDFISH_MESSAGE_ID",
363 std::string("OpenBMC.0.1.CreateFilesystemSuccess"));
364}
365
Ed Tanous82897c32022-02-21 14:11:59 -0800366void EStoraged::mountFilesystem()
John Wedigb810c922021-11-17 16:38:03 -0800367{
John Wedigb17f8252022-01-12 14:24:26 -0800368 /*
369 * Create directory for the filesystem, if it's not already present. It
370 * might already exist if, for example, the BMC reboots after creating the
371 * directory.
372 */
373 if (!fsIface->directoryExists(std::filesystem::path(mountPoint)))
John Wedigb810c922021-11-17 16:38:03 -0800374 {
John Wedigb17f8252022-01-12 14:24:26 -0800375 bool success =
376 fsIface->createDirectory(std::filesystem::path(mountPoint));
377 if (!success)
378 {
379 lg2::error("Failed to create mount point: {DIR}", "DIR", mountPoint,
380 "REDFISH_MESSAGE_ID",
381 std::string("OpenBMC.0.1.MountFilesystemFail"));
382 throw InternalFailure();
383 }
John Wedigb810c922021-11-17 16:38:03 -0800384 }
385
386 /* Run the command to mount the filesystem. */
387 std::string luksContainer("/dev/mapper/" + containerName);
388 int retval = fsIface->doMount(luksContainer.c_str(), mountPoint.c_str(),
389 "ext4", 0, nullptr);
Ed Tanous82897c32022-02-21 14:11:59 -0800390 if (retval != 0)
John Wedigb810c922021-11-17 16:38:03 -0800391 {
392 lg2::error("Failed to mount filesystem: {RETVAL}", "RETVAL", retval,
393 "REDFISH_MESSAGE_ID",
394 std::string("OpenBMC.0.1.MountFilesystemFail"));
395 bool removeSuccess =
396 fsIface->removeDirectory(std::filesystem::path(mountPoint));
397 if (!removeSuccess)
398 {
399 lg2::error("Failed to remove mount point: {DIR}", "DIR", mountPoint,
400 "REDFISH_MESSAGE_ID",
401 std::string("OpenBMC.0.1.MountFilesystemFail"));
402 }
John Wedig972c3fa2021-12-29 17:30:41 -0800403 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800404 }
405
406 lg2::info("Successfully mounted filesystem at {DIR}", "DIR", mountPoint,
407 "REDFISH_MESSAGE_ID",
408 std::string("OpenBMC.0.1.MountFilesystemSuccess"));
409}
410
Ed Tanous82897c32022-02-21 14:11:59 -0800411void EStoraged::unmountFilesystem()
John Wedigb810c922021-11-17 16:38:03 -0800412{
413 int retval = fsIface->doUnmount(mountPoint.c_str());
Ed Tanous82897c32022-02-21 14:11:59 -0800414 if (retval != 0)
John Wedigb810c922021-11-17 16:38:03 -0800415 {
416 lg2::error("Failed to unmount filesystem: {RETVAL}", "RETVAL", retval,
417 "REDFISH_MESSAGE_ID",
418 std::string("OpenBMC.0.1.UnmountFilesystemFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800419 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800420 }
421
422 /* Remove the mount point. */
423 bool success = fsIface->removeDirectory(std::filesystem::path(mountPoint));
424 if (!success)
425 {
426 lg2::error("Failed to remove mount point {DIR}", "DIR", mountPoint,
427 "REDFISH_MESSAGE_ID",
428 std::string("OpenBMC.0.1.UnmountFilesystemFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800429 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800430 }
431
432 lg2::info("Successfully unmounted filesystem at {DIR}", "DIR", mountPoint,
433 "REDFISH_MESSAGE_ID",
434 std::string("OpenBMC.0.1.MountFilesystemSuccess"));
435}
436
Ed Tanous82897c32022-02-21 14:11:59 -0800437void EStoraged::deactivateLuksDev()
John Wedigb810c922021-11-17 16:38:03 -0800438{
439 lg2::info("Deactivating LUKS device {DEV}", "DEV", devPath,
440 "REDFISH_MESSAGE_ID",
441 std::string("OpenBMC.0.1.DeactivateLuksDev"));
442
443 int retval = cryptIface->cryptDeactivate(nullptr, containerName.c_str());
444 if (retval < 0)
445 {
446 lg2::error("Failed to deactivate crypt device: {RETVAL}", "RETVAL",
447 retval, "REDFISH_MESSAGE_ID",
448 std::string("OpenBMC.0.1.DeactivateLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800449 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800450 }
451
452 /* Device is now locked. */
453 locked(true);
454
455 lg2::info("Successfully deactivated LUKS device {DEV}", "DEV", devPath,
456 "REDFISH_MESSAGE_ID",
457 std::string("OpenBMC.0.1.DeactivateLuksDevSuccess"));
458}
459
John Wedig67a47442022-04-05 17:21:29 -0700460void EStoraged::locked(bool isLocked)
461{
462 lockedProperty = isLocked;
463}
464
John Wedig2098dab2021-09-14 13:56:28 -0700465} // namespace estoraged