John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 1 | |
| 2 | #include "estoraged.hpp" |
| 3 | |
John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 4 | #include "cryptErase.hpp" |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 5 | #include "cryptsetupInterface.hpp" |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 6 | #include "pattern.hpp" |
John Edward Broadbent | 605085a | 2021-11-05 13:45:45 -0700 | [diff] [blame] | 7 | #include "sanitize.hpp" |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 8 | #include "verifyDriveGeometry.hpp" |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 9 | #include "zero.hpp" |
John Edward Broadbent | 4e13b0a | 2021-11-15 15:21:59 -0800 | [diff] [blame] | 10 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 11 | #include <libcryptsetup.h> |
| 12 | #include <openssl/rand.h> |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 13 | |
| 14 | #include <phosphor-logging/lg2.hpp> |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 15 | #include <sdbusplus/asio/object_server.hpp> |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 16 | #include <xyz/openbmc_project/Common/error.hpp> |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 17 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 18 | #include <cstdlib> |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 19 | #include <filesystem> |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 20 | #include <iostream> |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 21 | #include <string> |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 22 | #include <string_view> |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 23 | #include <utility> |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
| 26 | namespace estoraged |
| 27 | { |
| 28 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 29 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 30 | using sdbusplus::xyz::openbmc_project::Common::Error::UnsupportedRequest; |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 31 | using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Volume; |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 32 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 33 | EStoraged::EStoraged(sdbusplus::asio::object_server& server, |
| 34 | const std::string& devPath, const std::string& luksName, |
John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 35 | uint64_t size, uint8_t lifeTime, |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 36 | std::unique_ptr<CryptsetupInterface> cryptInterface, |
| 37 | std::unique_ptr<FilesystemInterface> fsInterface) : |
| 38 | devPath(devPath), |
| 39 | containerName(luksName), mountPoint("/mnt/" + luksName + "_fs"), |
| 40 | lockedProperty(false), cryptIface(std::move(cryptInterface)), |
| 41 | fsIface(std::move(fsInterface)), objectServer(server) |
| 42 | { |
| 43 | /* Get the filename of the device (without "/dev/"). */ |
| 44 | std::string deviceName = std::filesystem::path(devPath).filename().string(); |
| 45 | /* DBus object path */ |
| 46 | std::string path = "/xyz/openbmc_project/inventory/storage/" + deviceName; |
| 47 | |
| 48 | /* Add Volume interface. */ |
| 49 | volumeInterface = objectServer.add_interface( |
| 50 | path, "xyz.openbmc_project.Inventory.Item.Volume"); |
| 51 | volumeInterface->register_method( |
| 52 | "FormatLuks", [this](const std::vector<uint8_t>& password, |
| 53 | Volume::FilesystemType type) { |
| 54 | this->formatLuks(password, type); |
| 55 | }); |
| 56 | volumeInterface->register_method( |
| 57 | "Erase", |
| 58 | [this](Volume::EraseMethod eraseType) { this->erase(eraseType); }); |
| 59 | volumeInterface->register_method("Lock", [this]() { this->lock(); }); |
| 60 | volumeInterface->register_method( |
| 61 | "Unlock", |
| 62 | [this](std::vector<uint8_t>& password) { this->unlock(password); }); |
| 63 | volumeInterface->register_method( |
| 64 | "ChangePassword", [this](const std::vector<uint8_t>& oldPassword, |
| 65 | const std::vector<uint8_t>& newPassword) { |
| 66 | this->changePassword(oldPassword, newPassword); |
| 67 | }); |
| 68 | volumeInterface->register_property_r( |
| 69 | "Locked", lockedProperty, sdbusplus::vtable::property_::emits_change, |
| 70 | [this](bool& value) { |
| 71 | value = this->isLocked(); |
| 72 | return value; |
| 73 | }); |
| 74 | |
| 75 | /* Add Drive interface. */ |
| 76 | driveInterface = objectServer.add_interface( |
| 77 | path, "xyz.openbmc_project.Inventory.Item.Drive"); |
| 78 | driveInterface->register_property("Capacity", size); |
John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 79 | driveInterface->register_property("PredictedMediaLifeLeftPercent", |
| 80 | lifeTime); |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 81 | |
| 82 | volumeInterface->initialize(); |
| 83 | driveInterface->initialize(); |
| 84 | } |
| 85 | |
| 86 | EStoraged::~EStoraged() |
| 87 | { |
| 88 | objectServer.remove_interface(volumeInterface); |
| 89 | objectServer.remove_interface(driveInterface); |
| 90 | } |
| 91 | |
| 92 | void EStoraged::formatLuks(const std::vector<uint8_t>& password, |
| 93 | Volume::FilesystemType type) |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 94 | { |
John Edward Broadbent | 4e13b0a | 2021-11-15 15:21:59 -0800 | [diff] [blame] | 95 | std::string msg = "OpenBMC.0.1.DriveFormat"; |
| 96 | lg2::info("Starting format", "REDFISH_MESSAGE_ID", msg); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 97 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 98 | if (type != Volume::FilesystemType::ext4) |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 99 | { |
| 100 | lg2::error("Only ext4 filesystems are supported currently", |
| 101 | "REDFISH_MESSAGE_ID", std::string("OpenBMC.0.1.FormatFail")); |
| 102 | throw UnsupportedRequest(); |
| 103 | } |
| 104 | |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame^] | 105 | formatLuksDev(password); |
| 106 | activateLuksDev(password); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 107 | |
| 108 | createFilesystem(); |
| 109 | mountFilesystem(); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 110 | } |
| 111 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 112 | void EStoraged::erase(Volume::EraseMethod inEraseMethod) |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 113 | { |
| 114 | std::cerr << "Erasing encrypted eMMC" << std::endl; |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 115 | lg2::info("Starting erase", "REDFISH_MESSAGE_ID", |
| 116 | std::string("OpenBMC.0.1.DriveErase")); |
| 117 | switch (inEraseMethod) |
| 118 | { |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 119 | case Volume::EraseMethod::CryptoErase: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 120 | { |
John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 121 | CryptErase myCryptErase(devPath); |
| 122 | myCryptErase.doErase(); |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 123 | break; |
| 124 | } |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 125 | case Volume::EraseMethod::VerifyGeometry: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 126 | { |
| 127 | VerifyDriveGeometry myVerifyGeometry(devPath); |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 128 | myVerifyGeometry.geometryOkay(); |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 129 | break; |
| 130 | } |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 131 | case Volume::EraseMethod::LogicalOverWrite: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 132 | { |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 133 | Pattern myErasePattern(devPath); |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 134 | myErasePattern.writePattern(); |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 135 | break; |
| 136 | } |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 137 | case Volume::EraseMethod::LogicalVerify: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 138 | { |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 139 | Pattern myErasePattern(devPath); |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 140 | myErasePattern.verifyPattern(); |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 141 | break; |
| 142 | } |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 143 | case Volume::EraseMethod::VendorSanitize: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 144 | { |
John Edward Broadbent | 605085a | 2021-11-05 13:45:45 -0700 | [diff] [blame] | 145 | Sanitize mySanitize(devPath); |
| 146 | mySanitize.doSanitize(); |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 147 | break; |
| 148 | } |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 149 | case Volume::EraseMethod::ZeroOverWrite: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 150 | { |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 151 | Zero myZero(devPath); |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 152 | myZero.writeZero(); |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 153 | break; |
| 154 | } |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 155 | case Volume::EraseMethod::ZeroVerify: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 156 | { |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 157 | Zero myZero(devPath); |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 158 | myZero.verifyZero(); |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 159 | break; |
| 160 | } |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 161 | case Volume::EraseMethod::SecuredLocked: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 162 | { |
John Edward Broadbent | f59b729 | 2022-02-15 15:07:15 -0800 | [diff] [blame] | 163 | if (isLocked()) |
| 164 | { |
| 165 | lock(); |
| 166 | } |
| 167 | // TODO: implement hardware locking |
| 168 | // Until that is done, we can lock using eStoraged::lock() |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 169 | break; |
| 170 | } |
| 171 | } |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 174 | void EStoraged::lock() |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 175 | { |
John Edward Broadbent | 4e13b0a | 2021-11-15 15:21:59 -0800 | [diff] [blame] | 176 | std::string msg = "OpenBMC.0.1.DriveLock"; |
| 177 | lg2::info("Starting lock", "REDFISH_MESSAGE_ID", msg); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 178 | |
| 179 | unmountFilesystem(); |
| 180 | deactivateLuksDev(); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 183 | void EStoraged::unlock(std::vector<uint8_t> password) |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 184 | { |
John Edward Broadbent | 4e13b0a | 2021-11-15 15:21:59 -0800 | [diff] [blame] | 185 | std::string msg = "OpenBMC.0.1.DriveUnlock"; |
| 186 | lg2::info("Starting unlock", "REDFISH_MESSAGE_ID", msg); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 187 | |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame^] | 188 | activateLuksDev(std::move(password)); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 189 | mountFilesystem(); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 190 | } |
| 191 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 192 | void EStoraged::changePassword(const std::vector<uint8_t>& /*oldPassword*/, |
| 193 | const std::vector<uint8_t>& /*newPassword*/) |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 194 | { |
| 195 | std::cerr << "Changing password for encrypted eMMC" << std::endl; |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 196 | lg2::info("Starting change password", "REDFISH_MESSAGE_ID", |
| 197 | std::string("OpenBMC.0.1.DrivePasswordChanged")); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 200 | bool EStoraged::isLocked() const |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 201 | { |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 202 | return lockedProperty; |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 205 | std::string_view EStoraged::getMountPoint() const |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 206 | { |
| 207 | return mountPoint; |
| 208 | } |
| 209 | |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame^] | 210 | void EStoraged::formatLuksDev(std::vector<uint8_t> password) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 211 | { |
| 212 | lg2::info("Formatting device {DEV}", "DEV", devPath, "REDFISH_MESSAGE_ID", |
| 213 | std::string("OpenBMC.0.1.FormatLuksDev")); |
| 214 | |
| 215 | /* Generate the volume key. */ |
| 216 | const std::size_t keySize = 64; |
| 217 | std::vector<uint8_t> volumeKey(keySize); |
| 218 | if (RAND_bytes(volumeKey.data(), keySize) != 1) |
| 219 | { |
| 220 | lg2::error("Failed to create volume key", "REDFISH_MESSAGE_ID", |
| 221 | std::string("OpenBMC.0.1.FormatLuksDevFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 222 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 223 | } |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame^] | 224 | |
| 225 | /* Create the handle. */ |
| 226 | CryptHandle cryptHandle(devPath); |
| 227 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 228 | /* Format the LUKS encrypted device. */ |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame^] | 229 | int retval = cryptIface->cryptFormat( |
| 230 | cryptHandle.get(), CRYPT_LUKS2, "aes", "xts-plain64", nullptr, |
| 231 | reinterpret_cast<const char*>(volumeKey.data()), volumeKey.size(), |
| 232 | nullptr); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 233 | if (retval < 0) |
| 234 | { |
| 235 | lg2::error("Failed to format encrypted device: {RETVAL}", "RETVAL", |
| 236 | retval, "REDFISH_MESSAGE_ID", |
| 237 | std::string("OpenBMC.0.1.FormatLuksDevFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 238 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /* Device is now encrypted. */ |
| 242 | locked(true); |
| 243 | |
| 244 | /* Set the password. */ |
| 245 | retval = cryptIface->cryptKeyslotAddByVolumeKey( |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame^] | 246 | cryptHandle.get(), CRYPT_ANY_SLOT, nullptr, 0, |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 247 | reinterpret_cast<const char*>(password.data()), password.size()); |
| 248 | |
| 249 | if (retval < 0) |
| 250 | { |
| 251 | lg2::error("Failed to set encryption password", "REDFISH_MESSAGE_ID", |
| 252 | std::string("OpenBMC.0.1.FormatLuksDevFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 253 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | lg2::info("Encrypted device {DEV} successfully formatted", "DEV", devPath, |
| 257 | "REDFISH_MESSAGE_ID", |
| 258 | std::string("OpenBMC.0.1.FormatLuksDevSuccess")); |
| 259 | } |
| 260 | |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame^] | 261 | void EStoraged::activateLuksDev(std::vector<uint8_t> password) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 262 | { |
| 263 | lg2::info("Activating LUKS dev {DEV}", "DEV", devPath, "REDFISH_MESSAGE_ID", |
| 264 | std::string("OpenBMC.0.1.ActivateLuksDev")); |
| 265 | |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame^] | 266 | /* Create the handle. */ |
| 267 | CryptHandle cryptHandle(devPath); |
| 268 | |
| 269 | int retval = cryptIface->cryptLoad(cryptHandle.get(), CRYPT_LUKS2, nullptr); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 270 | if (retval < 0) |
| 271 | { |
| 272 | lg2::error("Failed to load LUKS header: {RETVAL}", "RETVAL", retval, |
| 273 | "REDFISH_MESSAGE_ID", |
| 274 | std::string("OpenBMC.0.1.ActivateLuksDevFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 275 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | retval = cryptIface->cryptActivateByPassphrase( |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame^] | 279 | cryptHandle.get(), containerName.c_str(), CRYPT_ANY_SLOT, |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 280 | reinterpret_cast<const char*>(password.data()), password.size(), 0); |
| 281 | |
| 282 | if (retval < 0) |
| 283 | { |
| 284 | lg2::error("Failed to activate LUKS dev: {RETVAL}", "RETVAL", retval, |
| 285 | "REDFISH_MESSAGE_ID", |
| 286 | std::string("OpenBMC.0.1.ActivateLuksDevFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 287 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /* Device is now unlocked. */ |
| 291 | locked(false); |
| 292 | |
| 293 | lg2::info("Successfully activated LUKS dev {DEV}", "DEV", devPath, |
| 294 | "REDFISH_MESSAGE_ID", |
| 295 | std::string("OpenBMC.0.1.ActivateLuksDevSuccess")); |
| 296 | } |
| 297 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 298 | void EStoraged::createFilesystem() |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 299 | { |
| 300 | /* Run the command to create the filesystem. */ |
| 301 | int retval = fsIface->runMkfs(containerName); |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 302 | if (retval != 0) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 303 | { |
| 304 | lg2::error("Failed to create filesystem: {RETVAL}", "RETVAL", retval, |
| 305 | "REDFISH_MESSAGE_ID", |
| 306 | std::string("OpenBMC.0.1.CreateFilesystemFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 307 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 308 | } |
| 309 | lg2::info("Successfully created filesystem for /dev/mapper/{CONTAINER}", |
| 310 | "CONTAINER", containerName, "REDFISH_MESSAGE_ID", |
| 311 | std::string("OpenBMC.0.1.CreateFilesystemSuccess")); |
| 312 | } |
| 313 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 314 | void EStoraged::mountFilesystem() |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 315 | { |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 316 | /* |
| 317 | * Create directory for the filesystem, if it's not already present. It |
| 318 | * might already exist if, for example, the BMC reboots after creating the |
| 319 | * directory. |
| 320 | */ |
| 321 | if (!fsIface->directoryExists(std::filesystem::path(mountPoint))) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 322 | { |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 323 | bool success = |
| 324 | fsIface->createDirectory(std::filesystem::path(mountPoint)); |
| 325 | if (!success) |
| 326 | { |
| 327 | lg2::error("Failed to create mount point: {DIR}", "DIR", mountPoint, |
| 328 | "REDFISH_MESSAGE_ID", |
| 329 | std::string("OpenBMC.0.1.MountFilesystemFail")); |
| 330 | throw InternalFailure(); |
| 331 | } |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /* Run the command to mount the filesystem. */ |
| 335 | std::string luksContainer("/dev/mapper/" + containerName); |
| 336 | int retval = fsIface->doMount(luksContainer.c_str(), mountPoint.c_str(), |
| 337 | "ext4", 0, nullptr); |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 338 | if (retval != 0) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 339 | { |
| 340 | lg2::error("Failed to mount filesystem: {RETVAL}", "RETVAL", retval, |
| 341 | "REDFISH_MESSAGE_ID", |
| 342 | std::string("OpenBMC.0.1.MountFilesystemFail")); |
| 343 | bool removeSuccess = |
| 344 | fsIface->removeDirectory(std::filesystem::path(mountPoint)); |
| 345 | if (!removeSuccess) |
| 346 | { |
| 347 | lg2::error("Failed to remove mount point: {DIR}", "DIR", mountPoint, |
| 348 | "REDFISH_MESSAGE_ID", |
| 349 | std::string("OpenBMC.0.1.MountFilesystemFail")); |
| 350 | } |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 351 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | lg2::info("Successfully mounted filesystem at {DIR}", "DIR", mountPoint, |
| 355 | "REDFISH_MESSAGE_ID", |
| 356 | std::string("OpenBMC.0.1.MountFilesystemSuccess")); |
| 357 | } |
| 358 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 359 | void EStoraged::unmountFilesystem() |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 360 | { |
| 361 | int retval = fsIface->doUnmount(mountPoint.c_str()); |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 362 | if (retval != 0) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 363 | { |
| 364 | lg2::error("Failed to unmount filesystem: {RETVAL}", "RETVAL", retval, |
| 365 | "REDFISH_MESSAGE_ID", |
| 366 | std::string("OpenBMC.0.1.UnmountFilesystemFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 367 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /* Remove the mount point. */ |
| 371 | bool success = fsIface->removeDirectory(std::filesystem::path(mountPoint)); |
| 372 | if (!success) |
| 373 | { |
| 374 | lg2::error("Failed to remove mount point {DIR}", "DIR", mountPoint, |
| 375 | "REDFISH_MESSAGE_ID", |
| 376 | std::string("OpenBMC.0.1.UnmountFilesystemFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 377 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | lg2::info("Successfully unmounted filesystem at {DIR}", "DIR", mountPoint, |
| 381 | "REDFISH_MESSAGE_ID", |
| 382 | std::string("OpenBMC.0.1.MountFilesystemSuccess")); |
| 383 | } |
| 384 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 385 | void EStoraged::deactivateLuksDev() |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 386 | { |
| 387 | lg2::info("Deactivating LUKS device {DEV}", "DEV", devPath, |
| 388 | "REDFISH_MESSAGE_ID", |
| 389 | std::string("OpenBMC.0.1.DeactivateLuksDev")); |
| 390 | |
| 391 | int retval = cryptIface->cryptDeactivate(nullptr, containerName.c_str()); |
| 392 | if (retval < 0) |
| 393 | { |
| 394 | lg2::error("Failed to deactivate crypt device: {RETVAL}", "RETVAL", |
| 395 | retval, "REDFISH_MESSAGE_ID", |
| 396 | std::string("OpenBMC.0.1.DeactivateLuksDevFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 397 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /* Device is now locked. */ |
| 401 | locked(true); |
| 402 | |
| 403 | lg2::info("Successfully deactivated LUKS device {DEV}", "DEV", devPath, |
| 404 | "REDFISH_MESSAGE_ID", |
| 405 | std::string("OpenBMC.0.1.DeactivateLuksDevSuccess")); |
| 406 | } |
| 407 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 408 | void EStoraged::locked(bool isLocked) |
| 409 | { |
| 410 | lockedProperty = isLocked; |
| 411 | } |
| 412 | |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 413 | } // namespace estoraged |