mkfs: Pre-create empty image file to workaround obsolete mkfs.ext4 18/323718/2
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Tue, 6 May 2025 16:58:48 +0000 (18:58 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Wed, 7 May 2025 19:20:52 +0000 (21:20 +0200)
We not only need to create empty file but also add -F (force) flag
for mkfs.ext4 to actually create filesystem.

Needed for mkfs.ext4 v1.43 from 2013.

Change-Id: I69fb57f2b1d6a8c9823d70a81c0f86f56bd33739

src/service/src/dir_backend_fixed_size.cpp

index fde65d96ccddda9fdc00be70405d85a8e8b2c51c..e069f7e4b7329334e69b8f0e50eec3a4f28947a6 100644 (file)
@@ -23,6 +23,7 @@
 #undef LOG_TAG
 #define LOG_TAG "SESSIOND"
 #include <dlog.h>
+#include <fcntl.h>
 
 #include "dir_backend_fixed_size.hpp"
 #include "os_ops.hpp"
@@ -45,6 +46,13 @@ fs::path DirBackendFixedSize::GetImagePathFromSubsessionPath(fs::path subsession
 
 static void do_mkfs(const fs::path& image_path, int uid, int gid, uint64_t size_kB)
 {
+       // Pre-create empty file is needed for VD only, which uses obsolete mkfs.ext4 from 2013
+       int fd = open(image_path.c_str(), O_WRONLY | O_CREAT| O_TRUNC, 0777);
+       if (fd < 0)
+               throw std::runtime_error("Unable to create empty file to contain subsession filesystem: %m");
+       close(fd);
+       fd = -1;
+
        const auto child_pid = OS::throwing_fork();
        if (child_pid == 0) {
 
@@ -64,7 +72,7 @@ static void do_mkfs(const fs::path& image_path, int uid, int gid, uint64_t size_
 
                execl
                        ( mkfs.data(), mkfs.data() /* argv[0] convention */
-                       , "-t", "ext4"
+                       , "-F" /* dangerous, but needed for old mkfs.ext4 */
                        , "-E", owner_arg.c_str()
                        , "-m", "0"
                        , image_path.c_str()
@@ -74,7 +82,12 @@ static void do_mkfs(const fs::path& image_path, int uid, int gid, uint64_t size_
 
                _exit(1);
        } else {
-               OS::throw_if_child_failed(child_pid, "mkfs.ext4 failed!");
+               try {
+                       OS::throw_if_child_failed(child_pid, "mkfs.ext4 failed!");
+               } catch (std::exception& ex) {
+                       unlink(image_path.c_str());
+                       throw;
+               }
        }
 }