xref: /openbmc/estoraged/src/main.cpp (revision 59dffa63)
1 
2 #include "estoraged.hpp"
3 
4 #include <unistd.h>
5 
6 #include <phosphor-logging/lg2.hpp>
7 #include <sdbusplus/bus.hpp>
8 
9 #include <filesystem>
10 #include <iostream>
11 #include <string>
12 
13 static void usage(std::string_view name)
14 {
15     std::cerr
16         << "Usage: " << name
17         << "eStorageD service on the BMC\n\n"
18            "  -b <blockDevice>          The phyical encrypted device\n"
19            "                            If omitted, default is /dev/mmcblk0.\n"
20            "  -c <containerName>        The LUKS container name to be created\n"
21            "                            If omitted, default is luks-<devName>";
22 }
23 
24 int main(int argc, char** argv)
25 {
26 
27     std::string physicalBlockDev = "/dev/mmcblk0";
28     std::string containerBlockDev;
29     int opt;
30     while ((opt = getopt(argc, argv, "b:c:")) != -1)
31     {
32         switch (opt)
33         {
34             case 'b':
35                 physicalBlockDev = optarg;
36                 break;
37             case 'c':
38                 containerBlockDev = optarg;
39                 break;
40             default:
41                 usage(argv[0]);
42                 exit(EXIT_FAILURE);
43         }
44     }
45 
46     /* Get the filename of the device (without "/dev/"). */
47     auto deviceName =
48         std::filesystem::path(physicalBlockDev).filename().string();
49 
50     /* If containerName arg wasn't provided, create one based on deviceName. */
51     if (containerBlockDev.empty())
52     {
53         containerBlockDev = "luks-" + deviceName;
54     }
55 
56     /* DBus path location to place the object. */
57     std::string path = "/xyz/openbmc_project/storage/" + deviceName;
58 
59     /*
60      * Create a new bus and affix an object manager for the subtree path we
61      * intend to place objects at.
62      */
63     auto b = sdbusplus::bus::new_default();
64     sdbusplus::server::manager_t m{b, path.c_str()};
65 
66     /* Reserve the dbus service name. */
67     std::string busName = "xyz.openbmc_project.eStoraged." + deviceName;
68     b.request_name(busName.c_str());
69 
70     /* Create an eStoraged object. */
71     estoraged::eStoraged esObject{b, path.c_str(), physicalBlockDev,
72                                   containerBlockDev};
73     lg2::info("Storage management service is running", "REDFISH_MESSAGE_ID",
74               std::string("OpenBMC.1.0.ServiceStarted"));
75 
76     while (true)
77     {
78         b.wait();
79         b.process_discard();
80     }
81 
82     return 1;
83 }
84