Support uploading same image for multiple times

The code was unable to upload the same image for multiple times because
it has the same version ID.

Add a salt when generating the version ID so that we could get different
version IDs for the same image, and thus it is possible to upload the
images for multiple times. Use a random generator with the time as seed
to ensure uniqueness.

When the image updater starts up, use the mount directory name or
functional suffix as seeds to make the version ids unique.

The version ID is then necessary in the version object because the Delete
interface needs it when the version object is to be deleted.

With this change, it is possible to upload and update the same image for
multiple times, and systems with dual BMC chips could have the same image
as well.

Tested: Upload a same image for multiple times and verify all the
        uploads are OK and getting different DBus objects;
        Verify it is OK to delete the uploaded images by deleting the
        DBus objects.
        Verify the code update works on all layouts and that the flash
        id (Path property) was set.

- static
root@romulus:~# busctl --no-pager introspect \
xyz.openbmc_project.Software.BMC.Updater \
/xyz/openbmc_project/software/79139bc0
...
.Path   property  s "9e3b868e"

root@romulus:~# ls -l /run/media/rofs-9e3b868e-functional/etc/
lrwxrwxrwx    1 root     root            15 Jan 22 20:11 os-release ->
/etc/os-release

- ubi
root@witherspoon:~# busctl --no-pager introspect \
xyz.openbmc_project.Software.BMC.Updater \
/xyz/openbmc_project/software/151bc3d8
...
.Path   property  s "cfb85943"

root@witherspoon:~# df
/dev/ubiblock0_0         18816     18816         0 100% /media/rofs-cfb85943-functional
/dev/ubiblock4_0         18816     18816         0 100% /media/rofs-26085328

root@witherspoon:~# cat /var/lib/phosphor-bmc-code-mgmt/cfb85943/priority
{
    "priority": 0
}

- mmc
root@p10bmc:~# busctl --no-pager introspect \
xyz.openbmc_project.Software.BMC.Updater \
/xyz/openbmc_project/software/cb5f99e2
...
.Path   property  s         "b"

root@p10bmc:~# df
/dev/mmcblk0p5          202095    153782     33107  82% /media/rofs-b-functional

root@p10bmc:~# cat /var/lib/phosphor-bmc-code-mgmt/b/priority
{
    "priority": 0
}

Signed-off-by: Lei YU <yulei.sh@bytedance.com>
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
Change-Id: I76842de4d5b7c5175acc43ed7c57e0deb4057be5
diff --git a/image_manager.cpp b/image_manager.cpp
index 922575b..93fac66 100644
--- a/image_manager.cpp
+++ b/image_manager.cpp
@@ -18,7 +18,9 @@
 
 #include <algorithm>
 #include <cstring>
+#include <ctime>
 #include <filesystem>
+#include <random>
 #include <string>
 
 namespace phosphor
@@ -187,7 +189,8 @@
         Version::getValue(manifestPath.string(), "ExtendedVersion");
 
     // Compute id
-    auto id = Version::getId(version);
+    auto salt = std::to_string(randomGen());
+    auto id = Version::getId(version + salt);
 
     fs::path imageDirPath = std::string{IMG_UPLOAD_DIR};
     imageDirPath /= id;
@@ -211,7 +214,7 @@
         auto versionPtr = std::make_unique<Version>(
             bus, objPath, version, purpose, extendedVersion,
             imageDirPath.string(),
-            std::bind(&Manager::erase, this, std::placeholders::_1));
+            std::bind(&Manager::erase, this, std::placeholders::_1), id);
         versionPtr->deleteObject =
             std::make_unique<phosphor::software::manager::Delete>(bus, objPath,
                                                                   *versionPtr);