Make MEDIA_DIR configurable

MEDIA_DIR was configured as "/media", it works on ubi-fs systems.

On static layout systems, "/media" is in rwfs, and thus is persistent
after BMC reboot or code update (if rwfs is not updated).
This makes the dirs/files in MEDIA_DIR persistent, and it causes an
issue that BMC uses the incorrect versionId of the image that is the
persistent one in MEDIA_DIR, while it should generate the versionID from
the functional BMC version.

Making MEDIA_DIR configurable, so static layout system could configure
it to a tmpfs, and the above issue is fixed.

Partly resolves openbmc/phosphor-bmc-code-mgmt#3

Tested: Verify that MEDIA_DIR could be configured via configure
        argument.

Change-Id: I10d46ea00b79677bec16ac69d2a3375597665f0c
Signed-off-by: Lei YU <mine260309@gmail.com>
diff --git a/configure.ac b/configure.ac
index deaf110..f768d45 100755
--- a/configure.ac
+++ b/configure.ac
@@ -87,10 +87,6 @@
     [The common file path interface])
 AC_DEFINE(OS_RELEASE_FILE, "/etc/os-release",
     [The name of the BMC table of contents file])
-AC_DEFINE(MEDIA_DIR, "/media/",
-    [The base dir where all RO partitions are mounted])
-AC_DEFINE(BMC_ROFS_PREFIX, "/media/rofs-",
-    [The prefix path for the versioned read-only bmc partitions])
 AC_DEFINE(ALT_RWFS, "/media/alt/var/persist",
     [The path of the alt rwfs overlay])
 AC_DEFINE(PERSIST_DIR, "/var/lib/obmc/phosphor-bmc-code-mgmt/",
@@ -102,6 +98,13 @@
 AC_DEFINE(SYSTEMD_INTERFACE, "org.freedesktop.systemd1.Manager",
     [The systemd interface])
 
+AC_ARG_VAR(MEDIA_DIR, [The base dir where all RO partitions are mounted])
+AS_IF([test "x$MEDIA_DIR" == "x"], [MEDIA_DIR="/media"])
+AC_DEFINE_UNQUOTED([MEDIA_DIR], ["$MEDIA_DIR"], [The base dir where all RO partitions are mounted])
+
+AC_DEFINE_UNQUOTED(BMC_ROFS_PREFIX, "$MEDIA_DIR/rofs-",
+    [The prefix path for the versioned read-only bmc partitions])
+
 AC_ARG_VAR(IMG_UPLOAD_DIR, [Directory where downloaded software images are placed])
 AS_IF([test "x$IMG_UPLOAD_DIR" == "x"], [IMG_UPLOAD_DIR="/tmp/images"])
 AC_DEFINE_UNQUOTED([IMG_UPLOAD_DIR], ["$IMG_UPLOAD_DIR"], [Directory where downloaded software images are placed])
diff --git a/item_updater.cpp b/item_updater.cpp
index e2d0f94..014757d 100644
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -134,6 +134,21 @@
 void ItemUpdater::processBMCImage()
 {
     using VersionClass = phosphor::software::manager::Version;
+
+    // Check MEDIA_DIR and create if it does not exist
+    try
+    {
+        if (!fs::is_directory(MEDIA_DIR))
+        {
+            fs::create_directory(MEDIA_DIR);
+        }
+    }
+    catch (const fs::filesystem_error& e)
+    {
+        log<level::ERR>("Failed to prepare dir", entry("ERR=%s", e.what()));
+        return;
+    }
+
     // Read os-release from /etc/ to get the functional BMC version
     auto functionalVersion = VersionClass::getBMCVersion(OS_RELEASE_FILE);
 
@@ -172,7 +187,8 @@
             auto purpose = server::Version::VersionPurpose::BMC;
             auto path = fs::path(SOFTWARE_OBJPATH) / id;
 
-            // Create functional association if this is the functional version
+            // Create functional association if this is the functional
+            // version
             if (version.compare(functionalVersion) == 0)
             {
                 createFunctionalAssociation(path);
@@ -209,7 +225,8 @@
                 id, std::make_unique<Activation>(
                         bus, path, *this, id, activationState, associations)));
 
-            // If Active, create RedundancyPriority instance for this version.
+            // If Active, create RedundancyPriority instance for this
+            // version.
             if (activationState == server::Activation::Activations::Active)
             {
                 uint8_t priority = std::numeric_limits<uint8_t>::max();