Create the default network file during startup of network manager
if network file is not on the system then create the default network
file with dhcp enabled.
Resolves openbmc/openbmc#2332
Change-Id: I9e523c76f684dcb2951a0a730f94b11f326b5489
Signed-off-by: Ratan Gupta <ratagupt@in.ibm.com>
diff --git a/network_manager.cpp b/network_manager.cpp
index 4525e34..bb6d204 100644
--- a/network_manager.cpp
+++ b/network_manager.cpp
@@ -39,6 +39,56 @@
setConfDir(confDir);
}
+bool Manager::createDefaultNetworkFiles(bool force)
+{
+ auto isCreated = false;
+ try
+ {
+ // Directory would have created before with
+ // setConfDir function.
+ if (force)
+ {
+ // Factory Reset case
+ // we need to forcefully write the files
+ // so delete the existing ones.
+ if (fs::is_directory(confDir))
+ {
+ for (const auto& file : fs::directory_iterator(confDir))
+ {
+ fs::remove(file.path());
+ }
+ }
+ }
+
+ auto interfaceStrList = getInterfaces();
+ for (const auto& interface : interfaceStrList)
+ {
+ auto fileName = systemd::config::networkFilePrefix + interface +
+ systemd::config::networkFileSuffix;
+
+ fs::path filePath = confDir;
+ filePath /= fileName;
+
+ // create the interface specific network file
+ // if not exist or we forcefully wants to write
+ // the network file.
+
+ if (force || !fs::is_regular_file(filePath.string()))
+ {
+ bmc::writeDHCPDefault(filePath.string(), interface);
+ log<level::INFO>("Created the default network file.",
+ entry("INTERFACE=%s", interface.c_str()));
+ isCreated = true;
+ }
+ }
+ }
+ catch (std::exception& e)
+ {
+ log<level::ERR>("Unable to create the default network file");
+ }
+ return isCreated;
+}
+
void Manager::setConfDir(const fs::path& dir)
{
confDir = dir;
@@ -130,45 +180,14 @@
void Manager::reset()
{
- const std::string networkConfig = confDir.string();
- bool interfacesMapped = false;
-
- if(fs::is_directory(networkConfig))
+ if(!createDefaultNetworkFiles(true))
{
- for(auto& file : fs::directory_iterator(networkConfig))
- {
- fs::remove(file.path());
- }
-
- for (auto& intf : interfaces)
- {
- auto fileName = systemd::config::networkFilePrefix + intf.first +
- systemd::config::networkFileSuffix;
-
- fs::path filePath = networkConfig;
- filePath /= fileName;
-
- bmc::writeDHCPDefault(filePath.string(), intf.first);
- interfacesMapped = true;
- }
-
- if(interfacesMapped)
- {
- log<level::INFO>("Network configuration reset to DHCP.");
- }
- else
- {
- log<level::ERR>("No network interfaces are mapped.");
- elog<InternalFailure>();
- }
- }
- else
- {
- log<level::ERR>("Network configuration directory not found!");
- elog<InternalFailure>();
+ log<level::ERR>("Network Factory Reset failed.");
+ return;
+ // TODO: openbmc/openbmc#1721 - Log ResetFailed error here.
}
- return;
+ log<level::INFO>("Network Factory Reset done.");
}
// Need to merge the below function with the code which writes the
diff --git a/network_manager.hpp b/network_manager.hpp
index 0d15fc6..70ed8fc 100644
--- a/network_manager.hpp
+++ b/network_manager.hpp
@@ -94,11 +94,19 @@
*/
const DHCPConfPtr& getDHCPConf() { return dhcpConf; }
- private:
+ /** @brief create the default network files for each interface
+ * @detail if force param is true then forcefully create the network
+ * files otherwise if network file doesn't exist then
+ * create it.
+ * @param[in] force - forcefully create the file
+ * @return true if network file created else false
+ */
+ bool createDefaultNetworkFiles(bool force);
/** @brief restart the systemd networkd. */
void restartNetwork();
+ private:
/** @brief Persistent sdbusplus DBus bus connection. */
sdbusplus::bus::bus& bus;
diff --git a/network_manager_main.cpp b/network_manager_main.cpp
index 7fa3b05..969b705 100644
--- a/network_manager_main.cpp
+++ b/network_manager_main.cpp
@@ -63,8 +63,20 @@
phosphor::network::rtnetlink::Server svr(eventPtr);
+ // create the network interface dbus objects and system config
phosphor::network::manager->createChildObjects();
+ // create the default network files if the network file
+ // is not there for any interface.
+ // Parameter false means don't create the network
+ // files forcefully.
+ if (phosphor::network::manager->createDefaultNetworkFiles(false))
+ {
+ // if files created restart the network.
+ // don't need to call the create child objects as eventhandler
+ // will create it.
+ phosphor::network::manager->restartNetwork();
+ }
return svr.run();
}