blob: 341440a356af47a8cec90e42a4ba7dfde5969dab [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 */
John Wedig6c0d8ce2022-04-22 14:00:43 -070051 std::string objectPath =
52 "/xyz/openbmc_project/inventory/storage/" + 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 });
62 volumeInterface->register_method(
63 "Erase",
64 [this](Volume::EraseMethod eraseType) { this->erase(eraseType); });
65 volumeInterface->register_method("Lock", [this]() { this->lock(); });
66 volumeInterface->register_method(
67 "Unlock",
68 [this](std::vector<uint8_t>& password) { this->unlock(password); });
69 volumeInterface->register_method(
70 "ChangePassword", [this](const std::vector<uint8_t>& oldPassword,
71 const std::vector<uint8_t>& newPassword) {
72 this->changePassword(oldPassword, newPassword);
73 });
74 volumeInterface->register_property_r(
75 "Locked", lockedProperty, sdbusplus::vtable::property_::emits_change,
76 [this](bool& value) {
77 value = this->isLocked();
78 return value;
79 });
80
81 /* Add Drive interface. */
82 driveInterface = objectServer.add_interface(
John Wedig6c0d8ce2022-04-22 14:00:43 -070083 objectPath, "xyz.openbmc_project.Inventory.Item.Drive");
John Wedig67a47442022-04-05 17:21:29 -070084 driveInterface->register_property("Capacity", size);
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070085 driveInterface->register_property("PredictedMediaLifeLeftPercent",
86 lifeTime);
John Edward Broadbent14aee772022-04-20 13:46:48 -070087 /* This registers the Locked property for the Drives interface.
88 * Now it is the same as the volume Locked property */
89 driveInterface->register_property_r(
90 "Locked", lockedProperty, sdbusplus::vtable::property_::emits_change,
91 [this](bool& value) {
92 value = this->isLocked();
93 return value;
94 });
John Wedig67a47442022-04-05 17:21:29 -070095
John Edward Broadbent91c1ec12022-05-20 16:51:43 -070096 driveInterface->register_property_r(
97 "EncryptionStatus", encryptionStatus,
98 sdbusplus::vtable::property_::emits_change,
99 [this](Drive::DriveEncryptionState& value) {
100 value = this->findEncryptionStatus();
101 return value;
102 });
103
John Edward Broadbent49796412022-06-22 18:31:52 -0700104 embeddedLocationInterface = objectServer.add_interface(
105 objectPath, "xyz.openbmc_project.Inventory.Connector.Embedded");
John Edward Broadbent740e94b2022-06-10 19:42:30 -0700106
John Wedigb4838302022-07-22 13:51:16 -0700107 /* Add Asset interface. */
108 assetInterface = objectServer.add_interface(
109 objectPath, "xyz.openbmc_project.Inventory.Decorator.Asset");
110 assetInterface->register_property("PartNumber", partNumber);
111 assetInterface->register_property("SerialNumber", serialNumber);
112
John Wedig67a47442022-04-05 17:21:29 -0700113 volumeInterface->initialize();
114 driveInterface->initialize();
John Edward Broadbent49796412022-06-22 18:31:52 -0700115 embeddedLocationInterface->initialize();
John Wedigb4838302022-07-22 13:51:16 -0700116 assetInterface->initialize();
John Wedig6c0d8ce2022-04-22 14:00:43 -0700117
118 /* Set up the association between chassis and drive. */
119 association = objectServer.add_interface(
120 objectPath, "xyz.openbmc_project.Association.Definitions");
121
122 std::vector<Association> associations;
123 associations.emplace_back("chassis", "drive",
124 std::filesystem::path(configPath).parent_path());
125 association->register_property("Associations", associations);
126 association->initialize();
John Wedig67a47442022-04-05 17:21:29 -0700127}
128
129EStoraged::~EStoraged()
130{
131 objectServer.remove_interface(volumeInterface);
132 objectServer.remove_interface(driveInterface);
John Edward Broadbent49796412022-06-22 18:31:52 -0700133 objectServer.remove_interface(embeddedLocationInterface);
John Wedigb4838302022-07-22 13:51:16 -0700134 objectServer.remove_interface(assetInterface);
John Wedig6c0d8ce2022-04-22 14:00:43 -0700135 objectServer.remove_interface(association);
John Wedig67a47442022-04-05 17:21:29 -0700136}
137
138void EStoraged::formatLuks(const std::vector<uint8_t>& password,
139 Volume::FilesystemType type)
John Wedig2098dab2021-09-14 13:56:28 -0700140{
John Edward Broadbent4e13b0a2021-11-15 15:21:59 -0800141 std::string msg = "OpenBMC.0.1.DriveFormat";
142 lg2::info("Starting format", "REDFISH_MESSAGE_ID", msg);
John Wedigb810c922021-11-17 16:38:03 -0800143
John Wedig67a47442022-04-05 17:21:29 -0700144 if (type != Volume::FilesystemType::ext4)
John Wedig972c3fa2021-12-29 17:30:41 -0800145 {
146 lg2::error("Only ext4 filesystems are supported currently",
147 "REDFISH_MESSAGE_ID", std::string("OpenBMC.0.1.FormatFail"));
148 throw UnsupportedRequest();
149 }
150
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700151 formatLuksDev(password);
152 activateLuksDev(password);
John Wedigb810c922021-11-17 16:38:03 -0800153
154 createFilesystem();
155 mountFilesystem();
John Wedig2098dab2021-09-14 13:56:28 -0700156}
157
John Wedig67a47442022-04-05 17:21:29 -0700158void EStoraged::erase(Volume::EraseMethod inEraseMethod)
John Wedig2098dab2021-09-14 13:56:28 -0700159{
160 std::cerr << "Erasing encrypted eMMC" << std::endl;
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700161 lg2::info("Starting erase", "REDFISH_MESSAGE_ID",
162 std::string("OpenBMC.0.1.DriveErase"));
163 switch (inEraseMethod)
164 {
John Wedig67a47442022-04-05 17:21:29 -0700165 case Volume::EraseMethod::CryptoErase:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700166 {
John Edward Broadbent59dffa62022-01-13 17:41:32 -0800167 CryptErase myCryptErase(devPath);
168 myCryptErase.doErase();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700169 break;
170 }
John Wedig67a47442022-04-05 17:21:29 -0700171 case Volume::EraseMethod::VerifyGeometry:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700172 {
173 VerifyDriveGeometry myVerifyGeometry(devPath);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -0700174 myVerifyGeometry.geometryOkay();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700175 break;
176 }
John Wedig67a47442022-04-05 17:21:29 -0700177 case Volume::EraseMethod::LogicalOverWrite:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700178 {
John Edward Broadbent7f2ab642021-11-11 21:00:38 -0800179 Pattern myErasePattern(devPath);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -0700180 myErasePattern.writePattern();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700181 break;
182 }
John Wedig67a47442022-04-05 17:21:29 -0700183 case Volume::EraseMethod::LogicalVerify:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700184 {
John Edward Broadbent7f2ab642021-11-11 21:00:38 -0800185 Pattern myErasePattern(devPath);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -0700186 myErasePattern.verifyPattern();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700187 break;
188 }
John Wedig67a47442022-04-05 17:21:29 -0700189 case Volume::EraseMethod::VendorSanitize:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700190 {
John Edward Broadbent605085a2021-11-05 13:45:45 -0700191 Sanitize mySanitize(devPath);
192 mySanitize.doSanitize();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700193 break;
194 }
John Wedig67a47442022-04-05 17:21:29 -0700195 case Volume::EraseMethod::ZeroOverWrite:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700196 {
John Edward Broadbent4bc8a102021-12-30 16:11:49 -0800197 Zero myZero(devPath);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -0700198 myZero.writeZero();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700199 break;
200 }
John Wedig67a47442022-04-05 17:21:29 -0700201 case Volume::EraseMethod::ZeroVerify:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700202 {
John Edward Broadbent4bc8a102021-12-30 16:11:49 -0800203 Zero myZero(devPath);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -0700204 myZero.verifyZero();
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700205 break;
206 }
John Wedig67a47442022-04-05 17:21:29 -0700207 case Volume::EraseMethod::SecuredLocked:
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700208 {
John Wedig47cd7992022-10-05 15:45:11 -0700209 if (!isLocked())
John Edward Broadbentf59b7292022-02-15 15:07:15 -0800210 {
211 lock();
212 }
213 // TODO: implement hardware locking
214 // Until that is done, we can lock using eStoraged::lock()
John Edward Broadbente6ffe702021-10-14 14:03:11 -0700215 break;
216 }
217 }
John Wedig2098dab2021-09-14 13:56:28 -0700218}
219
Ed Tanous82897c32022-02-21 14:11:59 -0800220void EStoraged::lock()
John Wedig2098dab2021-09-14 13:56:28 -0700221{
John Edward Broadbent4e13b0a2021-11-15 15:21:59 -0800222 std::string msg = "OpenBMC.0.1.DriveLock";
223 lg2::info("Starting lock", "REDFISH_MESSAGE_ID", msg);
John Wedigb810c922021-11-17 16:38:03 -0800224
225 unmountFilesystem();
226 deactivateLuksDev();
John Wedig2098dab2021-09-14 13:56:28 -0700227}
228
Ed Tanous82897c32022-02-21 14:11:59 -0800229void EStoraged::unlock(std::vector<uint8_t> password)
John Wedig2098dab2021-09-14 13:56:28 -0700230{
John Edward Broadbent4e13b0a2021-11-15 15:21:59 -0800231 std::string msg = "OpenBMC.0.1.DriveUnlock";
232 lg2::info("Starting unlock", "REDFISH_MESSAGE_ID", msg);
John Wedigb810c922021-11-17 16:38:03 -0800233
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700234 activateLuksDev(std::move(password));
John Wedigb810c922021-11-17 16:38:03 -0800235 mountFilesystem();
John Wedig2098dab2021-09-14 13:56:28 -0700236}
237
John Wedig8d5a3a02022-09-29 15:25:58 -0700238void EStoraged::changePassword(const std::vector<uint8_t>& oldPassword,
239 const std::vector<uint8_t>& newPassword)
John Wedig2098dab2021-09-14 13:56:28 -0700240{
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 Wedig8d5a3a02022-09-29 15:25:58 -0700243
244 CryptHandle cryptHandle = loadLuksHeader();
245
246 int retval = cryptIface->cryptKeyslotChangeByPassphrase(
247 cryptHandle.get(), CRYPT_ANY_SLOT, CRYPT_ANY_SLOT,
248 reinterpret_cast<const char*>(oldPassword.data()), oldPassword.size(),
249 reinterpret_cast<const char*>(newPassword.data()), newPassword.size());
250 if (retval < 0)
251 {
252 lg2::error("Failed to change password", "REDFISH_MESSAGE_ID",
253 std::string("OpenBMC.0.1.DrivePasswordChangeFail"));
254 throw InternalFailure();
255 }
256
257 lg2::info("Successfully changed password for {DEV}", "DEV", devPath,
258 "REDFISH_MESSAGE_ID",
259 std::string("OpenBMC.0.1.DrivePasswordChangeSuccess"));
John Wedig2098dab2021-09-14 13:56:28 -0700260}
261
Ed Tanous82897c32022-02-21 14:11:59 -0800262bool EStoraged::isLocked() const
John Wedigb810c922021-11-17 16:38:03 -0800263{
John Wedig2443a022023-03-17 13:42:32 -0700264 /*
265 * Check if the mapped virtual device exists. If it exists, the LUKS volume
266 * is unlocked.
267 */
268 try
269 {
270 std::filesystem::path mappedDevicePath(cryptDevicePath);
271 return (std::filesystem::exists(mappedDevicePath) == false);
272 }
273 catch (const std::exception& e)
274 {
275 lg2::error("Failed to query locked status: {EXCEPT}", "EXCEPT",
276 e.what(), "REDFISH_MESSAGE_ID",
277 std::string("OpenBMC.0.1.IsLockedFail"));
278 /* If we couldn't query the filesystem path, assume unlocked. */
279 return false;
280 }
John Wedigb810c922021-11-17 16:38:03 -0800281}
282
Ed Tanous82897c32022-02-21 14:11:59 -0800283std::string_view EStoraged::getMountPoint() const
John Wedigb810c922021-11-17 16:38:03 -0800284{
285 return mountPoint;
286}
287
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700288void EStoraged::formatLuksDev(std::vector<uint8_t> password)
John Wedigb810c922021-11-17 16:38:03 -0800289{
290 lg2::info("Formatting device {DEV}", "DEV", devPath, "REDFISH_MESSAGE_ID",
291 std::string("OpenBMC.0.1.FormatLuksDev"));
292
293 /* Generate the volume key. */
294 const std::size_t keySize = 64;
295 std::vector<uint8_t> volumeKey(keySize);
296 if (RAND_bytes(volumeKey.data(), keySize) != 1)
297 {
298 lg2::error("Failed to create volume key", "REDFISH_MESSAGE_ID",
299 std::string("OpenBMC.0.1.FormatLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800300 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800301 }
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700302
303 /* Create the handle. */
304 CryptHandle cryptHandle(devPath);
305
John Wedigb810c922021-11-17 16:38:03 -0800306 /* Format the LUKS encrypted device. */
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700307 int retval = cryptIface->cryptFormat(
308 cryptHandle.get(), CRYPT_LUKS2, "aes", "xts-plain64", nullptr,
309 reinterpret_cast<const char*>(volumeKey.data()), volumeKey.size(),
310 nullptr);
John Wedigb810c922021-11-17 16:38:03 -0800311 if (retval < 0)
312 {
313 lg2::error("Failed to format encrypted device: {RETVAL}", "RETVAL",
314 retval, "REDFISH_MESSAGE_ID",
315 std::string("OpenBMC.0.1.FormatLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800316 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800317 }
318
John Wedigb810c922021-11-17 16:38:03 -0800319 /* Set the password. */
320 retval = cryptIface->cryptKeyslotAddByVolumeKey(
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700321 cryptHandle.get(), CRYPT_ANY_SLOT, nullptr, 0,
John Wedigb810c922021-11-17 16:38:03 -0800322 reinterpret_cast<const char*>(password.data()), password.size());
323
324 if (retval < 0)
325 {
326 lg2::error("Failed to set encryption password", "REDFISH_MESSAGE_ID",
327 std::string("OpenBMC.0.1.FormatLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800328 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800329 }
330
331 lg2::info("Encrypted device {DEV} successfully formatted", "DEV", devPath,
332 "REDFISH_MESSAGE_ID",
333 std::string("OpenBMC.0.1.FormatLuksDevSuccess"));
334}
335
John Edward Broadbent91c1ec12022-05-20 16:51:43 -0700336CryptHandle EStoraged::loadLuksHeader()
John Wedigb810c922021-11-17 16:38:03 -0800337{
John Wedigb810c922021-11-17 16:38:03 -0800338
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700339 CryptHandle cryptHandle(devPath);
340
341 int retval = cryptIface->cryptLoad(cryptHandle.get(), CRYPT_LUKS2, nullptr);
John Wedigb810c922021-11-17 16:38:03 -0800342 if (retval < 0)
343 {
344 lg2::error("Failed to load LUKS header: {RETVAL}", "RETVAL", retval,
345 "REDFISH_MESSAGE_ID",
346 std::string("OpenBMC.0.1.ActivateLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800347 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800348 }
John Edward Broadbent91c1ec12022-05-20 16:51:43 -0700349 return cryptHandle;
350}
John Wedigb810c922021-11-17 16:38:03 -0800351
John Edward Broadbent91c1ec12022-05-20 16:51:43 -0700352Drive::DriveEncryptionState EStoraged::findEncryptionStatus()
353{
354 try
355 {
356 loadLuksHeader();
357 return Drive::DriveEncryptionState::Encrypted;
358 }
359 catch (...)
360 {
361 return Drive::DriveEncryptionState::Unknown;
362 }
363}
364
365void EStoraged::activateLuksDev(std::vector<uint8_t> password)
366{
367 lg2::info("Activating LUKS dev {DEV}", "DEV", devPath, "REDFISH_MESSAGE_ID",
368 std::string("OpenBMC.0.1.ActivateLuksDev"));
369
370 /* Create the handle. */
371 CryptHandle cryptHandle = loadLuksHeader();
372
373 int retval = cryptIface->cryptActivateByPassphrase(
John Edward Broadbentb2c86be2022-04-15 11:45:53 -0700374 cryptHandle.get(), containerName.c_str(), CRYPT_ANY_SLOT,
John Wedigb810c922021-11-17 16:38:03 -0800375 reinterpret_cast<const char*>(password.data()), password.size(), 0);
376
377 if (retval < 0)
378 {
379 lg2::error("Failed to activate LUKS dev: {RETVAL}", "RETVAL", retval,
380 "REDFISH_MESSAGE_ID",
381 std::string("OpenBMC.0.1.ActivateLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800382 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800383 }
384
John Wedigb810c922021-11-17 16:38:03 -0800385 lg2::info("Successfully activated LUKS dev {DEV}", "DEV", devPath,
386 "REDFISH_MESSAGE_ID",
387 std::string("OpenBMC.0.1.ActivateLuksDevSuccess"));
388}
389
Ed Tanous82897c32022-02-21 14:11:59 -0800390void EStoraged::createFilesystem()
John Wedigb810c922021-11-17 16:38:03 -0800391{
392 /* Run the command to create the filesystem. */
John Wedig2443a022023-03-17 13:42:32 -0700393 int retval = fsIface->runMkfs(cryptDevicePath);
Ed Tanous82897c32022-02-21 14:11:59 -0800394 if (retval != 0)
John Wedigb810c922021-11-17 16:38:03 -0800395 {
396 lg2::error("Failed to create filesystem: {RETVAL}", "RETVAL", retval,
397 "REDFISH_MESSAGE_ID",
398 std::string("OpenBMC.0.1.CreateFilesystemFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800399 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800400 }
John Wedig2443a022023-03-17 13:42:32 -0700401 lg2::info("Successfully created filesystem for {CONTAINER}", "CONTAINER",
402 cryptDevicePath, "REDFISH_MESSAGE_ID",
John Wedigb810c922021-11-17 16:38:03 -0800403 std::string("OpenBMC.0.1.CreateFilesystemSuccess"));
404}
405
Ed Tanous82897c32022-02-21 14:11:59 -0800406void EStoraged::mountFilesystem()
John Wedigb810c922021-11-17 16:38:03 -0800407{
John Wedigb17f8252022-01-12 14:24:26 -0800408 /*
409 * Create directory for the filesystem, if it's not already present. It
410 * might already exist if, for example, the BMC reboots after creating the
411 * directory.
412 */
413 if (!fsIface->directoryExists(std::filesystem::path(mountPoint)))
John Wedigb810c922021-11-17 16:38:03 -0800414 {
John Wedigb17f8252022-01-12 14:24:26 -0800415 bool success =
416 fsIface->createDirectory(std::filesystem::path(mountPoint));
417 if (!success)
418 {
419 lg2::error("Failed to create mount point: {DIR}", "DIR", mountPoint,
420 "REDFISH_MESSAGE_ID",
421 std::string("OpenBMC.0.1.MountFilesystemFail"));
422 throw InternalFailure();
423 }
John Wedigb810c922021-11-17 16:38:03 -0800424 }
425
426 /* Run the command to mount the filesystem. */
John Wedig2443a022023-03-17 13:42:32 -0700427 int retval = fsIface->doMount(cryptDevicePath.c_str(), mountPoint.c_str(),
John Wedigb810c922021-11-17 16:38:03 -0800428 "ext4", 0, nullptr);
Ed Tanous82897c32022-02-21 14:11:59 -0800429 if (retval != 0)
John Wedigb810c922021-11-17 16:38:03 -0800430 {
431 lg2::error("Failed to mount filesystem: {RETVAL}", "RETVAL", retval,
432 "REDFISH_MESSAGE_ID",
433 std::string("OpenBMC.0.1.MountFilesystemFail"));
434 bool removeSuccess =
435 fsIface->removeDirectory(std::filesystem::path(mountPoint));
436 if (!removeSuccess)
437 {
438 lg2::error("Failed to remove mount point: {DIR}", "DIR", mountPoint,
439 "REDFISH_MESSAGE_ID",
440 std::string("OpenBMC.0.1.MountFilesystemFail"));
441 }
John Wedig972c3fa2021-12-29 17:30:41 -0800442 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800443 }
444
445 lg2::info("Successfully mounted 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::unmountFilesystem()
John Wedigb810c922021-11-17 16:38:03 -0800451{
452 int retval = fsIface->doUnmount(mountPoint.c_str());
Ed Tanous82897c32022-02-21 14:11:59 -0800453 if (retval != 0)
John Wedigb810c922021-11-17 16:38:03 -0800454 {
455 lg2::error("Failed to unmount filesystem: {RETVAL}", "RETVAL", retval,
456 "REDFISH_MESSAGE_ID",
457 std::string("OpenBMC.0.1.UnmountFilesystemFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800458 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800459 }
460
461 /* Remove the mount point. */
462 bool success = fsIface->removeDirectory(std::filesystem::path(mountPoint));
463 if (!success)
464 {
465 lg2::error("Failed to remove mount point {DIR}", "DIR", mountPoint,
466 "REDFISH_MESSAGE_ID",
467 std::string("OpenBMC.0.1.UnmountFilesystemFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800468 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800469 }
470
471 lg2::info("Successfully unmounted filesystem at {DIR}", "DIR", mountPoint,
472 "REDFISH_MESSAGE_ID",
473 std::string("OpenBMC.0.1.MountFilesystemSuccess"));
474}
475
Ed Tanous82897c32022-02-21 14:11:59 -0800476void EStoraged::deactivateLuksDev()
John Wedigb810c922021-11-17 16:38:03 -0800477{
478 lg2::info("Deactivating LUKS device {DEV}", "DEV", devPath,
479 "REDFISH_MESSAGE_ID",
480 std::string("OpenBMC.0.1.DeactivateLuksDev"));
481
482 int retval = cryptIface->cryptDeactivate(nullptr, containerName.c_str());
483 if (retval < 0)
484 {
485 lg2::error("Failed to deactivate crypt device: {RETVAL}", "RETVAL",
486 retval, "REDFISH_MESSAGE_ID",
487 std::string("OpenBMC.0.1.DeactivateLuksDevFail"));
John Wedig972c3fa2021-12-29 17:30:41 -0800488 throw InternalFailure();
John Wedigb810c922021-11-17 16:38:03 -0800489 }
490
John Wedigb810c922021-11-17 16:38:03 -0800491 lg2::info("Successfully deactivated LUKS device {DEV}", "DEV", devPath,
492 "REDFISH_MESSAGE_ID",
493 std::string("OpenBMC.0.1.DeactivateLuksDevSuccess"));
494}
495
John Wedig2443a022023-03-17 13:42:32 -0700496std::string_view EStoraged::getCryptDevicePath() const
John Wedig67a47442022-04-05 17:21:29 -0700497{
John Wedig2443a022023-03-17 13:42:32 -0700498 return cryptDevicePath;
John Wedig67a47442022-04-05 17:21:29 -0700499}
500
John Wedig2098dab2021-09-14 13:56:28 -0700501} // namespace estoraged