blob: 41085ebeffc23d520185a8fc9249c404df485a0f [file] [log] [blame]
John Wedig2098dab2021-09-14 13:56:28 -07001
2#include "estoraged.hpp"
3
4#include <unistd.h>
5
John Wedig67a47442022-04-05 17:21:29 -07006#include <boost/asio/io_context.hpp>
John Edward Broadbent4e13b0a2021-11-15 15:21:59 -08007#include <phosphor-logging/lg2.hpp>
John Wedig67a47442022-04-05 17:21:29 -07008#include <sdbusplus/asio/connection.hpp>
9#include <sdbusplus/asio/object_server.hpp>
John Wedig2098dab2021-09-14 13:56:28 -070010#include <sdbusplus/bus.hpp>
John Edward Broadbente35e7362022-03-22 16:14:24 -070011#include <util.hpp>
John Wedig2098dab2021-09-14 13:56:28 -070012
13#include <filesystem>
14#include <iostream>
John Wedig67a47442022-04-05 17:21:29 -070015#include <memory>
John Wedig2098dab2021-09-14 13:56:28 -070016#include <string>
17
18static 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 Broadbent5d799bb2022-03-22 16:14:24 -070026 " 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 Wedig2098dab2021-09-14 13:56:28 -070031}
32
33int main(int argc, char** argv)
34{
John Wedig2098dab2021-09-14 13:56:28 -070035 std::string physicalBlockDev = "/dev/mmcblk0";
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070036 std::string sysfsDev = "/sys/block/mmcblk0/device";
John Wedig2098dab2021-09-14 13:56:28 -070037 std::string containerBlockDev;
Ed Tanous82897c32022-02-21 14:11:59 -080038 int opt = 0;
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070039 while ((opt = getopt(argc, argv, "b:c:s:")) != -1)
John Wedig2098dab2021-09-14 13:56:28 -070040 {
41 switch (opt)
42 {
43 case 'b':
44 physicalBlockDev = optarg;
45 break;
46 case 'c':
47 containerBlockDev = optarg;
48 break;
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070049 case 's':
50 sysfsDev = optarg;
51 break;
John Wedig2098dab2021-09-14 13:56:28 -070052 default:
Ed Tanous82897c32022-02-21 14:11:59 -080053 usage(*argv);
John Wedig2098dab2021-09-14 13:56:28 -070054 exit(EXIT_FAILURE);
55 }
56 }
Ed Tanous82897c32022-02-21 14:11:59 -080057 try
John Wedig2098dab2021-09-14 13:56:28 -070058 {
Ed Tanous82897c32022-02-21 14:11:59 -080059 /* 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 Wedig67a47442022-04-05 17:21:29 -070069 // 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 Wedigfa5cb6f2022-04-12 15:07:34 -070073 std::string busName = "xyz.openbmc_project.eStoraged";
John Wedig67a47442022-04-05 17:21:29 -070074 conn->request_name(busName.c_str());
75 auto server = sdbusplus::asio::object_server(conn);
Ed Tanous82897c32022-02-21 14:11:59 -080076
John Edward Broadbente35e7362022-03-22 16:14:24 -070077 estoraged::EStoraged esObject{
John Wedig67a47442022-04-05 17:21:29 -070078 server, physicalBlockDev, containerBlockDev,
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070079 estoraged::util::findSizeOfBlockDevice(physicalBlockDev),
80 estoraged::util::findPredictedMediaLifeLeftPercent(sysfsDev)};
Ed Tanous82897c32022-02-21 14:11:59 -080081 lg2::info("Storage management service is running", "REDFISH_MESSAGE_ID",
82 std::string("OpenBMC.1.0.ServiceStarted"));
83
John Wedig67a47442022-04-05 17:21:29 -070084 io.run();
85 return 0;
John Wedig2098dab2021-09-14 13:56:28 -070086 }
Ed Tanous82897c32022-02-21 14:11:59 -080087 catch (const std::exception& e)
John Wedig2098dab2021-09-14 13:56:28 -070088 {
Ed Tanous82897c32022-02-21 14:11:59 -080089 lg2::error(e.what(), "REDFISH_MESSAGE_ID",
90 std::string("OpenBMC.1.0.ServiceException"));
John Wedig2098dab2021-09-14 13:56:28 -070091
Ed Tanous82897c32022-02-21 14:11:59 -080092 return 2;
93 }
John Wedig2098dab2021-09-14 13:56:28 -070094 return 1;
95}