FlatImage
A configurable Linux containerization system
Loading...
Searching...
No Matches
boot.cpp
Go to the documentation of this file.
1
8
9#include <elf.h>
10#include <unistd.h>
11#include <sys/stat.h>
12#include <sys/types.h>
13
14#include "../std/expected.hpp"
15#include "../lib/linux.hpp"
16#include "../lib/env.hpp"
17#include "../lib/log.hpp"
19#include "../config.hpp"
20#include "relocate.hpp"
21
22// Section for offset to filesystems
23extern "C"
24{
25 alignas(4)
26 __attribute__((section(".fim_reserved_offset"), used))
27 uint32_t FIM_RESERVED_OFFSET = 0;
28}
29
30// Unix environment variables
31extern char** environ;
32
40[[nodiscard]] Value<int> boot(int argc, char** argv)
41{
43 // Create configuration object
44 std::shared_ptr<ns_config::FlatImage> fim = Pop(ns_config::config());
45 return_if(fim == nullptr, Error("E::Failed to initialize configuration"));
46 // Set log file, permissive
47 ns_log::set_sink_file(fim->logs.path_file_boot);
48 // Execute flatimage command if exists
49 return Pop(ns_parser::execute_command(*fim, argc, argv));
50}
51
58void set_logger_level(int argc, char** argv)
59{
60 // Force debug mode
61 if(ns_env::exists("FIM_DEBUG", "1"))
62 {
63 ns_log::set_level(ns_log::Level::DEBUG);
64 return;
65 }
66 // Default critical only mode when no commands are passed
67 // Default critical only mode when commands are fim-root or fim-exec
68 if(argc < 2
69 or std::string_view{argv[1]} == "fim-exec"
70 or std::string_view{argv[1]} == "fim-root"
71 or not std::string_view{argv[1]}.starts_with("fim-"))
72 {
73 ns_log::set_level(ns_log::Level::CRITICAL);
74 return;
75 }
76 // Otherwise, info mode is the default
77 ns_log::set_level(ns_log::Level::INFO);
78}
79
87int main(int argc, char** argv)
88{
89 auto __expected_fn = [](auto&&){ return 125; };
90 // Configure logger
91 set_logger_level(argc, argv);
92 // Make variables available in environment
93 ns_env::set("FIM_VERSION", FIM_VERSION, ns_env::Replace::Y);
94 ns_env::set("FIM_COMMIT", FIM_COMMIT, ns_env::Replace::Y);
95 ns_env::set("FIM_DIST", FIM_DIST, ns_env::Replace::Y);
96 ns_env::set("FIM_TIMESTAMP", FIM_TIMESTAMP, ns_env::Replace::Y);
97 // Check if linux has the fuse module loaded
98 ns_linux::module_check("fuse").discard("W::'fuse' module might not be loaded");
99 // If it is outside /tmp, move the binary
100 Pop(ns_relocate::relocate(argv, FIM_RESERVED_OFFSET), "C::Failure to relocate binary");
101 // Launch flatimage
102 return Pop(boot(argc, argv), "C::The program exited with an error");
103}
104
105/* vim: set expandtab fdm=marker ts=2 sw=2 tw=100 et :*/
void set_logger_level(int argc, char **argv)
Set the logger level.
Definition boot.cpp:58
Value< int > boot(int argc, char **argv)
Boots the main flatimage program and the portal process.
Definition boot.cpp:40
FlatImage configuration object.
Executes parsed FlatImage commands.
Enhanced error handling framework built on std::expected.
constexpr auto __expected_fn
Lambda helper for Pop macro error returns.
Definition expected.hpp:147
A library for manipulating environment variables.
A library with helpers for linux operations.
A library for file logging.
Value< std::shared_ptr< FlatImage > > config()
Factory method that makes a FlatImage configuration object.
Definition config.hpp:589
Value< Dispatcher > deserialize(std::string_view str_raw_json) noexcept
Deserializes a json string into a Dispatcher class.
bool exists(std::string_view name, std::string_view value)
Checks if variable exists and equals value.
Definition env.hpp:82
void set(T &&name, U &&value, Replace replace)
Sets an environment variable.
Definition env.hpp:52
Value< bool > module_check(std::string_view str_name)
Checks if the linux kernel has a module loaded that matches the input name.
Definition linux.hpp:243
void set_level(Level level)
Sets the logging verbosity (CRITICAL,ERROR,INFO,DEBUG)
Definition log.hpp:339
void set_sink_file(fs::path const &path_file_sink)
Sets the sink file of the logger.
Definition log.hpp:359
Value< int > execute_command(ns_config::FlatImage &fim, int argc, char **argv)
Executes a parsed FlatImage command.
Definition executor.hpp:65
Value< void > relocate(char **argv, int32_t offset)
Calls the implementation of relocate.
Definition relocate.hpp:244
int main()
Entry point for the portal daemon.
Used to copy execve the flatimage program.
Enhanced expected type with integrated logging capabilities.
Definition expected.hpp:44