John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 1 | |
| 2 | #include "estoraged.hpp" |
| 3 | |
| 4 | #include <unistd.h> |
| 5 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 6 | #include <boost/asio/io_context.hpp> |
John Edward Broadbent | 4e13b0a | 2021-11-15 15:21:59 -0800 | [diff] [blame] | 7 | #include <phosphor-logging/lg2.hpp> |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 8 | #include <sdbusplus/asio/connection.hpp> |
| 9 | #include <sdbusplus/asio/object_server.hpp> |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 10 | #include <sdbusplus/bus.hpp> |
John Edward Broadbent | e35e736 | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 11 | #include <util.hpp> |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 12 | |
| 13 | #include <filesystem> |
| 14 | #include <iostream> |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 15 | #include <memory> |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 16 | #include <string> |
| 17 | |
| 18 | static void usage(std::string_view name) |
| 19 | { |
| 20 | std::cerr |
| 21 | << "Usage: " << name |
| 22 | << "eStorageD service on the BMC\n\n" |
| 23 | " -b <blockDevice> The phyical encrypted device\n" |
| 24 | " If omitted, default is /dev/mmcblk0.\n" |
| 25 | " -c <containerName> The LUKS container name to be created\n" |
John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 26 | " If omitted, default is luks-<devName>" |
| 27 | " -s <sysfsDevice> The interface to kernel data\n" |
| 28 | " structures dealing with this drive.\n" |
| 29 | " If omitted, default is\n" |
| 30 | " /sys/block/mmcblk0/device/\n"; |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | int main(int argc, char** argv) |
| 34 | { |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 35 | std::string physicalBlockDev = "/dev/mmcblk0"; |
John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 36 | std::string sysfsDev = "/sys/block/mmcblk0/device"; |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 37 | std::string containerBlockDev; |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 38 | int opt = 0; |
John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 39 | while ((opt = getopt(argc, argv, "b:c:s:")) != -1) |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 40 | { |
| 41 | switch (opt) |
| 42 | { |
| 43 | case 'b': |
| 44 | physicalBlockDev = optarg; |
| 45 | break; |
| 46 | case 'c': |
| 47 | containerBlockDev = optarg; |
| 48 | break; |
John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 49 | case 's': |
| 50 | sysfsDev = optarg; |
| 51 | break; |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 52 | default: |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 53 | usage(*argv); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 54 | exit(EXIT_FAILURE); |
| 55 | } |
| 56 | } |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 57 | try |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 58 | { |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 59 | /* Get the filename of the device (without "/dev/"). */ |
| 60 | std::string deviceName = |
| 61 | std::filesystem::path(physicalBlockDev).filename().string(); |
| 62 | /* If containerName arg wasn't provided, create one based on deviceName. |
| 63 | */ |
| 64 | if (containerBlockDev.empty()) |
| 65 | { |
| 66 | containerBlockDev = "luks-" + deviceName; |
| 67 | } |
| 68 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 69 | // setup connection to dbus |
| 70 | boost::asio::io_context io; |
| 71 | auto conn = std::make_shared<sdbusplus::asio::connection>(io); |
| 72 | // request D-Bus server name. |
John Wedig | fa5cb6f | 2022-04-12 15:07:34 -0700 | [diff] [blame^] | 73 | std::string busName = "xyz.openbmc_project.eStoraged"; |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 74 | conn->request_name(busName.c_str()); |
| 75 | auto server = sdbusplus::asio::object_server(conn); |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 76 | |
John Edward Broadbent | e35e736 | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 77 | estoraged::EStoraged esObject{ |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 78 | server, physicalBlockDev, containerBlockDev, |
John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 79 | estoraged::util::findSizeOfBlockDevice(physicalBlockDev), |
| 80 | estoraged::util::findPredictedMediaLifeLeftPercent(sysfsDev)}; |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 81 | lg2::info("Storage management service is running", "REDFISH_MESSAGE_ID", |
| 82 | std::string("OpenBMC.1.0.ServiceStarted")); |
| 83 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 84 | io.run(); |
| 85 | return 0; |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 86 | } |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 87 | catch (const std::exception& e) |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 88 | { |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 89 | lg2::error(e.what(), "REDFISH_MESSAGE_ID", |
| 90 | std::string("OpenBMC.1.0.ServiceException")); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 91 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 92 | return 2; |
| 93 | } |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 94 | return 1; |
| 95 | } |