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