Add OS::do_resize2fs 54/324254/12
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Tue, 13 May 2025 14:09:19 +0000 (16:09 +0200)
committerMichal Bloch <m.bloch@samsung.com>
Fri, 30 May 2025 19:16:19 +0000 (21:16 +0200)
And the corresponding OS::have_resize2fs

Change-Id: I7558a4e2f866a3b3990124cebf803f44ee3663df

CMakeLists.txt
packaging/sessiond.spec
src/service/src/os_ops.cpp
src/service/src/os_ops.hpp

index 406ece43c17e0171386056e6282989e0f744ee68..7a1ebc3cdc2e499c058418103b5e0891b6ff1f77 100644 (file)
@@ -10,5 +10,9 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -fPIE -Wno-error=shadow -Werror=missin
 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fPIE -Wno-error=shadow -Werror=missing-field-initializers -fconcepts")
 set(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie")
 
+if("${HAVE_RESIZE2FS}" STREQUAL "ON")
+  add_definitions(-DHAVE_RESIZE2FS=1)
+endif()
+
 add_subdirectory(src)
 add_subdirectory(tests)
index 32fe4d0c80f5a5f2e9719389435586fb23e46f2b..6bceb371557a907ab2e764a61095eeb1296b18ab 100644 (file)
@@ -1,3 +1,9 @@
+%define on_off() %{expand:%%{?with_%{1}:ON}%%{!?with_%{1}:OFF}}
+
+# resize2fs is needed for fast user add in fixed size (image) backend
+%define _with_resize2fs on
+%bcond_with resize2fs
+
 Name:       sessiond
 Summary:    Service to manage subsessions
 Version:    0.10.1.1
@@ -15,6 +21,9 @@ BuildRequires: pkgconfig(dlog)
 BuildRequires: pkgconfig(libsyscommon-plugin-api-sessiond)
 
 Requires:   /usr/sbin/mkfs.ext4
+%if %{with resize2fs}
+Requires:   /usr/sbin/resize2fs
+%endif
 
 %description
 
@@ -55,7 +64,11 @@ Group:      Development/Libraries
 %build
 mkdir -p build
 pushd build
-%cmake .. -DVERSION=%{version} -DCMAKE_BUILD_TYPE=Release
+%cmake .. \
+       -DVERSION=%{version} \
+       -DCMAKE_BUILD_TYPE=Release \
+       -DHAVE_RESIZE2FS=%{on_off resize2fs}
+
 make %{?jobs:-j%jobs}
 popd
 cp %{SOURCE1} .
index 6f768a6af0a7c1839bf766ba80f174cef84ef6dc..f6a0b6535ea21267367914a30f40a8eb1cd85294 100644 (file)
@@ -152,6 +152,46 @@ void OS::do_mkfs(const fs::path& image_path, int uid, int gid, uint64_t size_kB)
        }
 }
 
+bool OS::have_resize2fs()
+{
+#ifdef HAVE_RESIZE2FS
+       return true;
+#else
+       return false;
+#endif
+}
+
+void OS::do_resize2fs(const fs::path& fs_path, uint64_t size_kB)
+{
+       // Checking this precondition is caller responsibility
+       assert(OS::have_resize2fs());
+
+       const auto child_pid = OS::throwing_fork();
+       if (child_pid == 0) {
+               const auto cmd = "/usr/sbin/resize2fs"sv;
+
+               const auto size_arg = std::to_string(size_kB);
+
+               const auto r = execl
+                       ( cmd.data(), cmd.data() /* argv[0] convention */
+                       , fs_path.c_str()
+                       , size_arg.c_str()
+                       , (char *) NULL
+               );
+
+               LOGE("Failed to execute `%s %s %s`: %m"
+                       , cmd.data()
+                       , fs_path.c_str()
+                       , size_arg.c_str()
+               );
+               assert(r == -1);
+
+               _exit(1);
+       } else {
+               OS::throw_if_child_failed(child_pid, "resize2fs failed!");
+       }
+}
+
 void OS::do_umount(const fs::path& path)
 {
        const auto child_pid = OS::throwing_fork();
index a365f497da9eef2c6bee527de3fd10ca132bd118..30a1e834b47ba3a987108f1d349dc81a56b623a4 100644 (file)
@@ -18,6 +18,10 @@ namespace OS {
 
        void do_mkfs(const fs::path& image_path, int uid, int gid, uint64_t size_kB);
 
+       bool have_resize2fs();
+
+       void do_resize2fs(const fs::path& fs_path, uint64_t size_kB);
+
        void do_mount(const fs::path& image_path, const fs::path& mount_path);
 
        void do_umount(const fs::path& path);