service: Ensure fsck is called for templated user add (image) 90/324990/3
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Thu, 29 May 2025 23:13:40 +0000 (01:13 +0200)
committerMichal Bloch <m.bloch@samsung.com>
Fri, 30 May 2025 19:16:19 +0000 (21:16 +0200)
Change-Id: I238fd4925f842519e29b5c49304f885df6ff0abe

packaging/sessiond.spec
src/service/src/dir_backend_fixed_size.cpp
src/service/src/os_ops.cpp
src/service/src/os_ops.hpp

index 6bceb371557a907ab2e764a61095eeb1296b18ab..efd367f5478b3840c752d7c045716565f5b65264 100644 (file)
@@ -21,6 +21,7 @@ BuildRequires: pkgconfig(dlog)
 BuildRequires: pkgconfig(libsyscommon-plugin-api-sessiond)
 
 Requires:   /usr/sbin/mkfs.ext4
+Requires:   /usr/sbin/fsck.ext4
 %if %{with resize2fs}
 Requires:   /usr/sbin/resize2fs
 %endif
index 7399a3323dcdcdd674b0e708682e68396cd07e3c..5d64e2cc9754dd2ba07cf2615695fd456039231f 100644 (file)
@@ -130,6 +130,7 @@ bool DirBackendAddFixedSize::AddSubsessionPrepareFromTemplate (const std::string
        OS::do_mount(template_img, template_dir);
 
        try {
+               OS::do_fsck(subsession_img);
                OS::do_resize2fs(subsession_img, size_kB);
                OS::change_owner_and_group(subsession_img, uid, gid);
                fs::create_directory(subsession_dir);
index f6a0b6535ea21267367914a30f40a8eb1cd85294..5f0f6aee9c722036deba9c1382032f513f4a9761 100644 (file)
@@ -192,6 +192,31 @@ void OS::do_resize2fs(const fs::path& fs_path, uint64_t size_kB)
        }
 }
 
+void OS::do_fsck(const fs::path& fs_path)
+{
+       const auto child_pid = OS::throwing_fork();
+       if (child_pid == 0) {
+               const auto cmd = "/usr/sbin/fsck.ext4"sv;
+
+               const auto r = execl
+                       ( cmd.data(), cmd.data() /* argv[0] convention */
+                       , fs_path.c_str()
+                       , "-fp"
+                       , (char *) NULL
+               );
+
+               LOGE("Failed to execute `%s -fp %s`: %m"
+                       , cmd.data()
+                       , fs_path.c_str()
+               );
+               assert(r == -1);
+
+               _exit(1);
+       } else {
+               OS::throw_if_child_failed(child_pid, "fsck.ext4 failed!");
+       }
+}
+
 void OS::do_umount(const fs::path& path)
 {
        const auto child_pid = OS::throwing_fork();
index 30a1e834b47ba3a987108f1d349dc81a56b623a4..e40f547a427f71afc3cea3ddbe5ca764f3b0134a 100644 (file)
@@ -22,6 +22,8 @@ namespace OS {
 
        void do_resize2fs(const fs::path& fs_path, uint64_t size_kB);
 
+       void do_fsck(const fs::path& fs_path);
+
        void do_mount(const fs::path& image_path, const fs::path& mount_path);
 
        void do_umount(const fs::path& path);