Restore BMC version on systems that doesn't have new BMC ubi layout.

- Create a dir under /media called rofs-<versionId> and create a
  symlink for /etc/os-release. This would allow us to recreate the
  Software Object corresponding to the current BMC version.

Resolves openbmc/openbmc#2277

Change-Id: Icaed9eaaffd479243e115dffe1dea6de40b9f795
Signed-off-by: Saqib Khan <khansa@gfwa124.aus.stglabs.ibm.com>
diff --git a/item_updater.cpp b/item_updater.cpp
index 3dfa2bc..bb361f7 100644
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -1,6 +1,9 @@
 #include <fstream>
 #include <string>
 #include <phosphor-logging/log.hpp>
+#include <phosphor-logging/elog.hpp>
+#include <elog-errors.hpp>
+#include <xyz/openbmc_project/Software/Version/error.hpp>
 #include "config.h"
 #include "item_updater.hpp"
 #include "xyz/openbmc_project/Software/Version/server.hpp"
@@ -20,6 +23,7 @@
 namespace control = sdbusplus::xyz::openbmc_project::Control::server;
 
 using namespace phosphor::logging;
+using namespace sdbusplus::xyz::openbmc_project::Software::Version::Error;
 namespace fs = std::experimental::filesystem;
 
 const std::vector<std::string> bmcImages = { "image-kernel",
@@ -248,6 +252,31 @@
             }
         }
     }
+
+    // If there is no ubi volume for bmc version then read the /etc/os-release
+    // and create rofs-<versionId> under /media
+    if (activations.size() == 0)
+    {
+        auto version =
+                phosphor::software::manager::Version::
+                        getBMCVersion(OS_RELEASE_FILE);
+        auto id = phosphor::software::manager::Version::getId(version);
+        auto versionFileDir = BMC_ROFS_PREFIX + id + "/etc/";
+        try
+        {
+            if(!fs::is_directory(versionFileDir))
+            {
+                fs::create_directories(versionFileDir);
+            }
+            auto versionFilePath = BMC_ROFS_PREFIX + id + OS_RELEASE_FILE;
+            fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath);
+            ItemUpdater::processBMCImage();
+        }
+        catch (const std::exception& e)
+        {
+            log<level::ERR>(e.what());
+        }
+    }
     return;
 }