FlatImage
A configurable Linux containerization system
Loading...
Searching...
No Matches
overlayfs.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <filesystem>
12#include <unistd.h>
13
14#include "../lib/subprocess.hpp"
15#include "../lib/env.hpp"
16#include "../lib/fuse.hpp"
17#include "../std/filesystem.hpp"
18#include "filesystem.hpp"
19
20namespace
21{
22
23namespace fs = std::filesystem;
24
25} // namespace
26
37{
38
40{
41 private:
42 fs::path m_path_dir_upper;
43 fs::path m_path_dir_work;
44 std::vector<fs::path> m_vec_path_dir_layers;
45
46 public:
47 Overlayfs(pid_t pid_to_die_for
48 , fs::path const& path_dir_mount
49 , fs::path const& path_dir_upper
50 , fs::path const& path_dir_work
51 , fs::path const& path_file_log
52 , std::vector<fs::path> const& vec_path_dir_layers
53 );
54 Value<void> mount() override;
55};
56
57
68inline Overlayfs::Overlayfs(pid_t pid_to_die_for
69 , fs::path const& path_dir_mount
70 , fs::path const& path_dir_upper
71 , fs::path const& path_dir_work
72 , fs::path const& path_file_log
73 , std::vector<fs::path> const& vec_path_dir_layers
74 )
75 : ns_filesystem::Filesystem(pid_to_die_for, path_dir_mount, path_file_log)
76 , m_path_dir_upper(path_dir_upper)
77 , m_path_dir_work(path_dir_work)
78 , m_vec_path_dir_layers(vec_path_dir_layers)
79{
80 this->mount().discard("E::Could not mount overlayfs filesystem to '{}'", path_dir_mount);
81}
82
89{
90 // Validate directories
91 Pop(ns_fs::create_directories(m_path_dir_upper), "E::Failed to create upper directory");
92 Pop(ns_fs::create_directories(m_path_dir_mount), "E::Failed to create mount directory");
93 // Find overlayfs
94 auto path_file_overlayfs = Pop(ns_env::search_path("overlayfs"), "E::Could not find overlayfs in PATH");
95 // Get user and group ids
96 uid_t user_id = getuid();
97 gid_t group_id = getgid();
98 // Create string to represent argument of lowerdirs
99 // lowerdir= option is top-down
100 std::string arg_lowerdir = m_vec_path_dir_layers
101 | std::views::reverse
102 | std::views::transform([](auto&& e){ return e.string(); })
103 | std::views::join_with(std::string{":"})
104 | std::ranges::to<std::string>();
105 arg_lowerdir = "lowerdir=" + arg_lowerdir;
106 // Include arguments and spawn process
107 using enum ns_subprocess::Stream;
108 m_child = ns_subprocess::Subprocess(path_file_overlayfs)
109 .with_args("-f")
110 .with_args("-o", std::format("squash_to_uid={}", user_id))
111 .with_args("-o", std::format("squash_to_gid={}", group_id))
112 .with_args("-o", arg_lowerdir)
113 .with_args("-o", std::format("upperdir={}", m_path_dir_upper.string()))
114 .with_args("-o", std::format("workdir={}", m_path_dir_work.string()))
115 .with_args(m_path_dir_mount)
116 .with_die_on_pid(m_pid_to_die_for)
117 .with_stdio(ns_subprocess::Stream::Pipe)
118 .with_log_file(m_path_file_log)
119 .spawn();
120 // Wait for mount
121 ns_fuse::wait_fuse(m_path_dir_mount);
122 return {};
123} // Overlayfs
124
125
126
127} // namespace ns_filesystems::ns_overlayfs
128
129/* vim: set expandtab fdm=marker ts=2 sw=2 tw=100 et :*/
Filesystem(pid_t pid_to_die_for, std::filesystem::path const &path_dir_mount, std::filesystem::path const &path_file_log)
Construct a new Filesystem object.
Value< void > mount() override
Mounts the filesystem.
Definition overlayfs.hpp:88
Overlayfs(pid_t pid_to_die_for, fs::path const &path_dir_mount, fs::path const &path_dir_upper, fs::path const &path_dir_work, fs::path const &path_file_log, std::vector< fs::path > const &vec_path_dir_layers)
Construct a new Overlayfs object.
Definition overlayfs.hpp:68
Subprocess & with_die_on_pid(pid_t pid)
Configures the child process to die when the specified PID dies.
std::unique_ptr< Child > spawn()
Spawns (forks) the child process and begins execution.
Subprocess & with_stdio(Stream mode)
Sets the stdio redirection mode for the child process.
Subprocess & with_log_file(std::filesystem::path const &path)
Configures logging output for child process stdout/stderr.
Subprocess & with_args(Args &&... args)
Arguments forwarded as the process' arguments.
The base class for filesystems.
A library for operations on fuse filesystems.
A library for manipulating environment variables.
Value< fs::path > search_path(std::string const &query)
Search the directories in the PATH variable for the given input file name.
Definition env.hpp:150
Base filesystem interface and abstraction.
FUSE-OverlayFS overlay filesystem implementation.
Value< fs::path > create_directories(fs::path const &p)
Creates directories recursively.
void wait_fuse(fs::path const &path_dir_filesystem)
Waits for the given directory to not be fuse.
Definition fuse.hpp:68
Stream
Stream redirection modes for child process stdio.
Filesystem helpers.
Enhanced expected type with integrated logging capabilities.
Definition expected.hpp:44
A library to spawn sub-processes in linux.