blob: aea7904a78229762dc9f7e150258cebf6b491b05 [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"
26 " If omitted, default is luks-<devName>";
27}
28
29int main(int argc, char** argv)
30{
John Wedig2098dab2021-09-14 13:56:28 -070031 std::string physicalBlockDev = "/dev/mmcblk0";
32 std::string containerBlockDev;
Ed Tanous82897c32022-02-21 14:11:59 -080033 int opt = 0;
John Wedig2098dab2021-09-14 13:56:28 -070034 while ((opt = getopt(argc, argv, "b:c:")) != -1)
35 {
36 switch (opt)
37 {
38 case 'b':
39 physicalBlockDev = optarg;
40 break;
41 case 'c':
42 containerBlockDev = optarg;
43 break;
44 default:
Ed Tanous82897c32022-02-21 14:11:59 -080045 usage(*argv);
John Wedig2098dab2021-09-14 13:56:28 -070046 exit(EXIT_FAILURE);
47 }
48 }
Ed Tanous82897c32022-02-21 14:11:59 -080049 try
John Wedig2098dab2021-09-14 13:56:28 -070050 {
Ed Tanous82897c32022-02-21 14:11:59 -080051 /* Get the filename of the device (without "/dev/"). */
52 std::string deviceName =
53 std::filesystem::path(physicalBlockDev).filename().string();
54 /* If containerName arg wasn't provided, create one based on deviceName.
55 */
56 if (containerBlockDev.empty())
57 {
58 containerBlockDev = "luks-" + deviceName;
59 }
60
John Wedig67a47442022-04-05 17:21:29 -070061 // setup connection to dbus
62 boost::asio::io_context io;
63 auto conn = std::make_shared<sdbusplus::asio::connection>(io);
64 // request D-Bus server name.
Ed Tanous82897c32022-02-21 14:11:59 -080065 std::string busName = "xyz.openbmc_project.eStoraged." + deviceName;
John Wedig67a47442022-04-05 17:21:29 -070066 conn->request_name(busName.c_str());
67 auto server = sdbusplus::asio::object_server(conn);
Ed Tanous82897c32022-02-21 14:11:59 -080068
John Edward Broadbente35e7362022-03-22 16:14:24 -070069 estoraged::EStoraged esObject{
John Wedig67a47442022-04-05 17:21:29 -070070 server, physicalBlockDev, containerBlockDev,
John Edward Broadbente35e7362022-03-22 16:14:24 -070071 estoraged::util::Util::findSizeOfBlockDevice(physicalBlockDev)};
Ed Tanous82897c32022-02-21 14:11:59 -080072 lg2::info("Storage management service is running", "REDFISH_MESSAGE_ID",
73 std::string("OpenBMC.1.0.ServiceStarted"));
74
John Wedig67a47442022-04-05 17:21:29 -070075 io.run();
76 return 0;
John Wedig2098dab2021-09-14 13:56:28 -070077 }
Ed Tanous82897c32022-02-21 14:11:59 -080078 catch (const std::exception& e)
John Wedig2098dab2021-09-14 13:56:28 -070079 {
Ed Tanous82897c32022-02-21 14:11:59 -080080 lg2::error(e.what(), "REDFISH_MESSAGE_ID",
81 std::string("OpenBMC.1.0.ServiceException"));
John Wedig2098dab2021-09-14 13:56:28 -070082
Ed Tanous82897c32022-02-21 14:11:59 -080083 return 2;
84 }
John Wedig2098dab2021-09-14 13:56:28 -070085 return 1;
86}