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