blob: 0b93b3bce2c263ccd8715c52e97bde88386dca26 [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)),
45 objectServer(server)
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 Edward Broadbent49796412022-06-22 18:31:52 -0700103 embeddedLocationInterface = objectServer.add_interface(
104 objectPath, "xyz.openbmc_project.Inventory.Connector.Embedded");
John Edward Broadbent740e94b2022-06-10 19:42:30 -0700105
John Wedigb4838302022-07-22 13:51:16 -0700106 /* Add Asset interface. */
107 assetInterface = objectServer.add_interface(
108 objectPath, "xyz.openbmc_project.Inventory.Decorator.Asset");
109 assetInterface->register_property("PartNumber", partNumber);
110 assetInterface->register_property("SerialNumber", serialNumber);
111
John Wedig67a47442022-04-05 17:21:29 -0700112 volumeInterface->initialize();
113 driveInterface->initialize();
John Edward Broadbent49796412022-06-22 18:31:52 -0700114 embeddedLocationInterface->initialize();
John Wedigb4838302022-07-22 13:51:16 -0700115 assetInterface->initialize();
John Wedig6c0d8ce2022-04-22 14:00:43 -0700116
117 /* Set up the association between chassis and drive. */
118 association = objectServer.add_interface(
119 objectPath, "xyz.openbmc_project.Association.Definitions");
120
121 std::vector<Association> associations;
122 associations.emplace_back("chassis", "drive",
123 std::filesystem::path(configPath).parent_path());
124 association->register_property("Associations", associations);
125 association->initialize();
John Wedig67a47442022-04-05 17:21:29 -0700126}
127
128EStoraged::~EStoraged()
129{
130 objectServer.remove_interface(volumeInterface);
131 objectServer.remove_interface(driveInterface);
John Edward Broadbent49796412022-06-22 18:31:52 -0700132 objectServer.remove_interface(embeddedLocationInterface);
John Wedigb4838302022-07-22 13:51:16 -0700133 objectServer.remove_interface(assetInterface);
John Wedig6c0d8ce2022-04-22 14:00:43 -0700134 objectServer.remove_interface(association);
John Wedig67a47442022-04-05 17:21:29 -0700135}
136
137void EStoraged::formatLuks(const std::vector<uint8_t>& password,
138 Volume::FilesystemType type)
John Wedig2098dab2021-09-14 13:56:28 -0700139{
John Edward Broadbent4e13b0a2021-11-15 15:21:59 -0800140 std::string msg = "OpenBMC.0.1.DriveFormat";
141 lg2::info("Starting format", "REDFISH_MESSAGE_ID", msg);
John Wedigb810c922021-11-17 16:38:03 -0800142
John Wedig67a47442022-04-05 17:21:29 -0700143 if (type != Volume::FilesystemType::ext4)
John Wedig972c3fa2021-12-29 17:30:41 -0800144 {
145 lg2::error("Only ext4 filesystems are supported currently",
146 "REDFISH_MESSAGE_ID", std::string("OpenBMC.0.1.FormatFail"));
147 throw UnsupportedRequest();
148 }
149
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700150 formatLuksDev(password);
151 activateLuksDev(password);
John Wedigb810c922021-11-17 16:38:03 -0800152
153 createFilesystem();
154 mountFilesystem();
John Wedig2098dab2021-09-14 13:56:28 -0700155}
156
John Wedig67a47442022-04-05 17:21:29 -0700157void EStoraged::erase(Volume::EraseMethod inEraseMethod)
John Wedig2098dab2021-09-14 13:56:28 -0700158{
159 std::cerr << "Erasing encrypted eMMC" << std::endl;
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700160 lg2::info("Starting erase", "REDFISH_MESSAGE_ID",
161 std::string("OpenBMC.0.1.DriveErase"));
162 switch (inEraseMethod)
163 {
John Wedig67a47442022-04-05 17:21:29 -0700164 case Volume::EraseMethod::CryptoErase:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700165 {
John Edward Broadbent59dffa62022-01-13 17:41:32 -0800166 CryptErase myCryptErase(devPath);
167 myCryptErase.doErase();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700168 break;
169 }
John Wedig67a47442022-04-05 17:21:29 -0700170 case Volume::EraseMethod::VerifyGeometry:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700171 {
172 VerifyDriveGeometry myVerifyGeometry(devPath);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -0700173 myVerifyGeometry.geometryOkay();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700174 break;
175 }
John Wedig67a47442022-04-05 17:21:29 -0700176 case Volume::EraseMethod::LogicalOverWrite:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700177 {
John Edward Broadbent7f2ab642021-11-11 21:00:38 -0800178 Pattern myErasePattern(devPath);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -0700179 myErasePattern.writePattern();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700180 break;
181 }
John Wedig67a47442022-04-05 17:21:29 -0700182 case Volume::EraseMethod::LogicalVerify:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700183 {
John Edward Broadbent7f2ab642021-11-11 21:00:38 -0800184 Pattern myErasePattern(devPath);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -0700185 myErasePattern.verifyPattern();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700186 break;
187 }
John Wedig67a47442022-04-05 17:21:29 -0700188 case Volume::EraseMethod::VendorSanitize:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700189 {
John Edward Broadbent605085a2021-11-05 13:45:45 -0700190 Sanitize mySanitize(devPath);
191 mySanitize.doSanitize();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700192 break;
193 }
John Wedig67a47442022-04-05 17:21:29 -0700194 case Volume::EraseMethod::ZeroOverWrite:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700195 {
John Edward Broadbent4bc8a102021-12-30 16:11:49 -0800196 Zero myZero(devPath);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -0700197 myZero.writeZero();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700198 break;
199 }
John Wedig67a47442022-04-05 17:21:29 -0700200 case Volume::EraseMethod::ZeroVerify:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700201 {
John Edward Broadbent4bc8a102021-12-30 16:11:49 -0800202 Zero myZero(devPath);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -0700203 myZero.verifyZero();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700204 break;
205 }
John Wedig67a47442022-04-05 17:21:29 -0700206 case Volume::EraseMethod::SecuredLocked:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700207 {
John Edward Broadbentf59b7292022-02-15 15:07:15 -0800208 if (isLocked())
209 {
210 lock();
211 }
212 // TODO: implement hardware locking
213 // Until that is done, we can lock using eStoraged::lock()
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700214 break;
215 }
216 }
John Wedig2098dab2021-09-14 13:56:28 -0700217}
218
Ed Tanous82897c32022-02-21 14:11:59 -0800219void EStoraged::lock()
John Wedig2098dab2021-09-14 13:56:28 -0700220{
John Edward Broadbent4e13b0a2021-11-15 15:21:59 -0800221 std::string msg = "OpenBMC.0.1.DriveLock";
222 lg2::info("Starting lock", "REDFISH_MESSAGE_ID", msg);
John Wedigb810c922021-11-17 16:38:03 -0800223
224 unmountFilesystem();
225 deactivateLuksDev();
John Wedig2098dab2021-09-14 13:56:28 -0700226}
227
Ed Tanous82897c32022-02-21 14:11:59 -0800228void EStoraged::unlock(std::vector<uint8_t> password)
John Wedig2098dab2021-09-14 13:56:28 -0700229{
John Edward Broadbent4e13b0a2021-11-15 15:21:59 -0800230 std::string msg = "OpenBMC.0.1.DriveUnlock";
231 lg2::info("Starting unlock", "REDFISH_MESSAGE_ID", msg);
John Wedigb810c922021-11-17 16:38:03 -0800232
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700233 activateLuksDev(std::move(password));
John Wedigb810c922021-11-17 16:38:03 -0800234 mountFilesystem();
John Wedig2098dab2021-09-14 13:56:28 -0700235}
236
John Wedig67a47442022-04-05 17:21:29 -0700237void EStoraged::changePassword(const std::vector<uint8_t>& /*oldPassword*/,
238 const std::vector<uint8_t>& /*newPassword*/)
John Wedig2098dab2021-09-14 13:56:28 -0700239{
240 std::cerr << "Changing password for encrypted eMMC" << std::endl;
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700241 lg2::info("Starting change password", "REDFISH_MESSAGE_ID",
242 std::string("OpenBMC.0.1.DrivePasswordChanged"));
John Wedig2098dab2021-09-14 13:56:28 -0700243}
244
Ed Tanous82897c32022-02-21 14:11:59 -0800245bool EStoraged::isLocked() const
John Wedigb810c922021-11-17 16:38:03 -0800246{
John Wedig67a47442022-04-05 17:21:29 -0700247 return lockedProperty;
John Wedigb810c922021-11-17 16:38:03 -0800248}
249
Ed Tanous82897c32022-02-21 14:11:59 -0800250std::string_view EStoraged::getMountPoint() const
John Wedigb810c922021-11-17 16:38:03 -0800251{
252 return mountPoint;
253}
254
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700255void EStoraged::formatLuksDev(std::vector<uint8_t> password)
John Wedigb810c922021-11-17 16:38:03 -0800256{
257 lg2::info("Formatting device {DEV}", "DEV", devPath, "REDFISH_MESSAGE_ID",
258 std::string("OpenBMC.0.1.FormatLuksDev"));
259
260 /* Generate the volume key. */
261 const std::size_t keySize = 64;
262 std::vector<uint8_t> volumeKey(keySize);
263 if (RAND_bytes(volumeKey.data(), keySize) != 1)
264 {
265 lg2::error("Failed to create volume key", "REDFISH_MESSAGE_ID",
266 std::string("OpenBMC.0.1.FormatLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800267 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800268 }
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700269
270 /* Create the handle. */
271 CryptHandle cryptHandle(devPath);
272
John Wedigb810c922021-11-17 16:38:03 -0800273 /* Format the LUKS encrypted device. */
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700274 int retval = cryptIface->cryptFormat(
275 cryptHandle.get(), CRYPT_LUKS2, "aes", "xts-plain64", nullptr,
276 reinterpret_cast<const char*>(volumeKey.data()), volumeKey.size(),
277 nullptr);
John Wedigb810c922021-11-17 16:38:03 -0800278 if (retval < 0)
279 {
280 lg2::error("Failed to format encrypted device: {RETVAL}", "RETVAL",
281 retval, "REDFISH_MESSAGE_ID",
282 std::string("OpenBMC.0.1.FormatLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800283 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800284 }
285
286 /* Device is now encrypted. */
287 locked(true);
288
289 /* Set the password. */
290 retval = cryptIface->cryptKeyslotAddByVolumeKey(
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700291 cryptHandle.get(), CRYPT_ANY_SLOT, nullptr, 0,
John Wedigb810c922021-11-17 16:38:03 -0800292 reinterpret_cast<const char*>(password.data()), password.size());
293
294 if (retval < 0)
295 {
296 lg2::error("Failed to set encryption password", "REDFISH_MESSAGE_ID",
297 std::string("OpenBMC.0.1.FormatLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800298 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800299 }
300
301 lg2::info("Encrypted device {DEV} successfully formatted", "DEV", devPath,
302 "REDFISH_MESSAGE_ID",
303 std::string("OpenBMC.0.1.FormatLuksDevSuccess"));
304}
305
John Edward Broadbent91c1ec12022-05-20 16:51:43 -0700306CryptHandle EStoraged::loadLuksHeader()
John Wedigb810c922021-11-17 16:38:03 -0800307{
John Wedigb810c922021-11-17 16:38:03 -0800308
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700309 CryptHandle cryptHandle(devPath);
310
311 int retval = cryptIface->cryptLoad(cryptHandle.get(), CRYPT_LUKS2, nullptr);
John Wedigb810c922021-11-17 16:38:03 -0800312 if (retval < 0)
313 {
314 lg2::error("Failed to load LUKS header: {RETVAL}", "RETVAL", retval,
315 "REDFISH_MESSAGE_ID",
316 std::string("OpenBMC.0.1.ActivateLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800317 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800318 }
John Edward Broadbent91c1ec12022-05-20 16:51:43 -0700319 return cryptHandle;
320}
John Wedigb810c922021-11-17 16:38:03 -0800321
John Edward Broadbent91c1ec12022-05-20 16:51:43 -0700322Drive::DriveEncryptionState EStoraged::findEncryptionStatus()
323{
324 try
325 {
326 loadLuksHeader();
327 return Drive::DriveEncryptionState::Encrypted;
328 }
329 catch (...)
330 {
331 return Drive::DriveEncryptionState::Unknown;
332 }
333}
334
335void EStoraged::activateLuksDev(std::vector<uint8_t> password)
336{
337 lg2::info("Activating LUKS dev {DEV}", "DEV", devPath, "REDFISH_MESSAGE_ID",
338 std::string("OpenBMC.0.1.ActivateLuksDev"));
339
340 /* Create the handle. */
341 CryptHandle cryptHandle = loadLuksHeader();
342
343 int retval = cryptIface->cryptActivateByPassphrase(
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700344 cryptHandle.get(), containerName.c_str(), CRYPT_ANY_SLOT,
John Wedigb810c922021-11-17 16:38:03 -0800345 reinterpret_cast<const char*>(password.data()), password.size(), 0);
346
347 if (retval < 0)
348 {
349 lg2::error("Failed to activate LUKS dev: {RETVAL}", "RETVAL", retval,
350 "REDFISH_MESSAGE_ID",
351 std::string("OpenBMC.0.1.ActivateLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800352 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800353 }
354
355 /* Device is now unlocked. */
356 locked(false);
357
358 lg2::info("Successfully activated LUKS dev {DEV}", "DEV", devPath,
359 "REDFISH_MESSAGE_ID",
360 std::string("OpenBMC.0.1.ActivateLuksDevSuccess"));
361}
362
Ed Tanous82897c32022-02-21 14:11:59 -0800363void EStoraged::createFilesystem()
John Wedigb810c922021-11-17 16:38:03 -0800364{
365 /* Run the command to create the filesystem. */
366 int retval = fsIface->runMkfs(containerName);
Ed Tanous82897c32022-02-21 14:11:59 -0800367 if (retval != 0)
John Wedigb810c922021-11-17 16:38:03 -0800368 {
369 lg2::error("Failed to create filesystem: {RETVAL}", "RETVAL", retval,
370 "REDFISH_MESSAGE_ID",
371 std::string("OpenBMC.0.1.CreateFilesystemFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800372 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800373 }
374 lg2::info("Successfully created filesystem for /dev/mapper/{CONTAINER}",
375 "CONTAINER", containerName, "REDFISH_MESSAGE_ID",
376 std::string("OpenBMC.0.1.CreateFilesystemSuccess"));
377}
378
Ed Tanous82897c32022-02-21 14:11:59 -0800379void EStoraged::mountFilesystem()
John Wedigb810c922021-11-17 16:38:03 -0800380{
John Wedigb17f8252022-01-12 14:24:26 -0800381 /*
382 * Create directory for the filesystem, if it's not already present. It
383 * might already exist if, for example, the BMC reboots after creating the
384 * directory.
385 */
386 if (!fsIface->directoryExists(std::filesystem::path(mountPoint)))
John Wedigb810c922021-11-17 16:38:03 -0800387 {
John Wedigb17f8252022-01-12 14:24:26 -0800388 bool success =
389 fsIface->createDirectory(std::filesystem::path(mountPoint));
390 if (!success)
391 {
392 lg2::error("Failed to create mount point: {DIR}", "DIR", mountPoint,
393 "REDFISH_MESSAGE_ID",
394 std::string("OpenBMC.0.1.MountFilesystemFail"));
395 throw InternalFailure();
396 }
John Wedigb810c922021-11-17 16:38:03 -0800397 }
398
399 /* Run the command to mount the filesystem. */
400 std::string luksContainer("/dev/mapper/" + containerName);
401 int retval = fsIface->doMount(luksContainer.c_str(), mountPoint.c_str(),
402 "ext4", 0, nullptr);
Ed Tanous82897c32022-02-21 14:11:59 -0800403 if (retval != 0)
John Wedigb810c922021-11-17 16:38:03 -0800404 {
405 lg2::error("Failed to mount filesystem: {RETVAL}", "RETVAL", retval,
406 "REDFISH_MESSAGE_ID",
407 std::string("OpenBMC.0.1.MountFilesystemFail"));
408 bool removeSuccess =
409 fsIface->removeDirectory(std::filesystem::path(mountPoint));
410 if (!removeSuccess)
411 {
412 lg2::error("Failed to remove mount point: {DIR}", "DIR", mountPoint,
413 "REDFISH_MESSAGE_ID",
414 std::string("OpenBMC.0.1.MountFilesystemFail"));
415 }
John Wedig972c3fa2021-12-29 17:30:41 -0800416 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800417 }
418
419 lg2::info("Successfully mounted filesystem at {DIR}", "DIR", mountPoint,
420 "REDFISH_MESSAGE_ID",
421 std::string("OpenBMC.0.1.MountFilesystemSuccess"));
422}
423
Ed Tanous82897c32022-02-21 14:11:59 -0800424void EStoraged::unmountFilesystem()
John Wedigb810c922021-11-17 16:38:03 -0800425{
426 int retval = fsIface->doUnmount(mountPoint.c_str());
Ed Tanous82897c32022-02-21 14:11:59 -0800427 if (retval != 0)
John Wedigb810c922021-11-17 16:38:03 -0800428 {
429 lg2::error("Failed to unmount filesystem: {RETVAL}", "RETVAL", retval,
430 "REDFISH_MESSAGE_ID",
431 std::string("OpenBMC.0.1.UnmountFilesystemFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800432 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800433 }
434
435 /* Remove the mount point. */
436 bool success = fsIface->removeDirectory(std::filesystem::path(mountPoint));
437 if (!success)
438 {
439 lg2::error("Failed to remove mount point {DIR}", "DIR", mountPoint,
440 "REDFISH_MESSAGE_ID",
441 std::string("OpenBMC.0.1.UnmountFilesystemFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800442 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800443 }
444
445 lg2::info("Successfully unmounted filesystem at {DIR}", "DIR", mountPoint,
446 "REDFISH_MESSAGE_ID",
447 std::string("OpenBMC.0.1.MountFilesystemSuccess"));
448}
449
Ed Tanous82897c32022-02-21 14:11:59 -0800450void EStoraged::deactivateLuksDev()
John Wedigb810c922021-11-17 16:38:03 -0800451{
452 lg2::info("Deactivating LUKS device {DEV}", "DEV", devPath,
453 "REDFISH_MESSAGE_ID",
454 std::string("OpenBMC.0.1.DeactivateLuksDev"));
455
456 int retval = cryptIface->cryptDeactivate(nullptr, containerName.c_str());
457 if (retval < 0)
458 {
459 lg2::error("Failed to deactivate crypt device: {RETVAL}", "RETVAL",
460 retval, "REDFISH_MESSAGE_ID",
461 std::string("OpenBMC.0.1.DeactivateLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800462 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800463 }
464
465 /* Device is now locked. */
466 locked(true);
467
468 lg2::info("Successfully deactivated LUKS device {DEV}", "DEV", devPath,
469 "REDFISH_MESSAGE_ID",
470 std::string("OpenBMC.0.1.DeactivateLuksDevSuccess"));
471}
472
John Wedig67a47442022-04-05 17:21:29 -0700473void EStoraged::locked(bool isLocked)
474{
475 lockedProperty = isLocked;
476}
477
John Wedig2098dab2021-09-14 13:56:28 -0700478} // namespace estoraged