diff --git a/gucc/CMakeLists.txt b/gucc/CMakeLists.txt index 75a4e06..8c9f097 100644 --- a/gucc/CMakeLists.txt +++ b/gucc/CMakeLists.txt @@ -13,6 +13,7 @@ add_library(${PROJECT_NAME} SHARED src/io_utils.cpp include/gucc/io_utils.hpp src/string_utils.cpp include/gucc/string_utils.hpp src/file_utils.cpp include/gucc/file_utils.hpp + src/fs_utils.cpp include/gucc/fs_utils.hpp src/cpu.cpp include/gucc/cpu.hpp src/pacmanconf_repo.cpp include/gucc/pacmanconf_repo.hpp src/initcpio.cpp include/gucc/initcpio.hpp diff --git a/gucc/include/gucc/fs_utils.hpp b/gucc/include/gucc/fs_utils.hpp new file mode 100644 index 0000000..1b1ffb7 --- /dev/null +++ b/gucc/include/gucc/fs_utils.hpp @@ -0,0 +1,17 @@ +#ifndef FS_UTILS_HPP +#define FS_UTILS_HPP + +#include // for string +#include // for string_view + +namespace gucc::fs::utils { + +// Get FSTYPE of mountpoint +auto get_mountpoint_fs(std::string_view mountpoint) noexcept -> std::string; + +// Get SOURCE of mountpoint +auto get_mountpoint_source(std::string_view mountpoint) noexcept -> std::string; + +} // namespace gucc::fs::utils + +#endif // FS_UTILS_HPP diff --git a/gucc/meson.build b/gucc/meson.build index 1b57b0b..4378c6d 100644 --- a/gucc/meson.build +++ b/gucc/meson.build @@ -3,6 +3,7 @@ gucc_lib = library('gucc', 'src/io_utils.cpp', 'src/file_utils.cpp', 'src/string_utils.cpp', + 'src/fs_utils.cpp', 'src/cpu.cpp', 'src/pacmanconf_repo.cpp', 'src/initcpio.cpp', diff --git a/gucc/src/fs_utils.cpp b/gucc/src/fs_utils.cpp new file mode 100644 index 0000000..5724b65 --- /dev/null +++ b/gucc/src/fs_utils.cpp @@ -0,0 +1,19 @@ +#include "gucc/fs_utils.hpp" +#include "gucc/io_utils.hpp" + +#include +#include + +#include + +namespace gucc::fs::utils { + +auto get_mountpoint_fs(std::string_view mountpoint) noexcept -> std::string { + return gucc::utils::exec(fmt::format(FMT_COMPILE("findmnt -ln -o FSTYPE \"{}\""), mountpoint)); +} + +auto get_mountpoint_source(std::string_view mountpoint) noexcept -> std::string { + return gucc::utils::exec(fmt::format(FMT_COMPILE("findmnt -ln -o SOURCE \"{}\""), mountpoint)); +} + +} // namespace gucc::fs::utils diff --git a/src/simple_tui.cpp b/src/simple_tui.cpp index bb52533..781400f 100644 --- a/src/simple_tui.cpp +++ b/src/simple_tui.cpp @@ -6,6 +6,7 @@ #include "widgets.hpp" // import gucc +#include "gucc/fs_utils.hpp" #include "gucc/io_utils.hpp" #include "gucc/string_utils.hpp" #include "gucc/zfs.hpp" @@ -387,7 +388,7 @@ void menu_simple() noexcept { } // If the root partition is btrfs, offer to create subvolumes - /*if (utils::get_mountpoint_fs(mountpoint) == "btrfs") { + /*if (gucc::fs::utils::get_mountpoint_fs(mountpoint) == "btrfs") { // Check if there are subvolumes already on the btrfs partition const auto& subvolumes = fmt::format(FMT_COMPILE("btrfs subvolume list \"{}\" 2>/dev/null"), mountpoint); const auto& subvolumes_count = gucc::utils::exec(fmt::format(FMT_COMPILE("{} | wc -l"), subvolumes)); diff --git a/src/tui.cpp b/src/tui.cpp index 8e8b470..b23f472 100644 --- a/src/tui.cpp +++ b/src/tui.cpp @@ -10,6 +10,7 @@ #include "widgets.hpp" // import gucc +#include "gucc/fs_utils.hpp" #include "gucc/io_utils.hpp" #include "gucc/string_utils.hpp" #include "gucc/zfs.hpp" @@ -1157,7 +1158,7 @@ void make_swap() noexcept { std::string answer{}; { std::vector temp{"None -"}; - const auto& root_filesystem = utils::get_mountpoint_fs(mountpoint_info); + const auto& root_filesystem = gucc::fs::utils::get_mountpoint_fs(mountpoint_info); if (!(root_filesystem == "zfs" || root_filesystem == "btrfs")) { temp.emplace_back("Swapfile -"); } @@ -1851,7 +1852,7 @@ void mount_partitions() noexcept { // check to see if we already have a zfs root mounted const auto& mountpoint_info = std::get(config_data["MOUNTPOINT"]); - if (utils::get_mountpoint_fs(mountpoint_info) == "zfs"sv) { + if (gucc::fs::utils::get_mountpoint_fs(mountpoint_info) == "zfs"sv) { detail::infobox_widget("\nUsing ZFS root on \'/\'\n"sv); std::this_thread::sleep_for(std::chrono::seconds(3)); } else { @@ -1895,7 +1896,7 @@ void mount_partitions() noexcept { // get_cryptroot // echo "$LUKS_DEV" > /tmp/.luks_dev // If the root partition is btrfs, offer to create subvolumes - if (utils::get_mountpoint_fs(mountpoint_info) == "btrfs") { + if (gucc::fs::utils::get_mountpoint_fs(mountpoint_info) == "btrfs") { // Check if there are subvolumes already on the btrfs partition const auto& subvolumes = fmt::format(FMT_COMPILE("btrfs subvolume list \"{}\" 2>/dev/null"), mountpoint_info); const auto& subvolumes_count = gucc::utils::exec(fmt::format(FMT_COMPILE("{} | wc -l"), subvolumes)); diff --git a/src/utils.cpp b/src/utils.cpp index b3205e0..09ca244 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -8,6 +8,7 @@ // import gucc #include "gucc/cpu.hpp" #include "gucc/file_utils.hpp" +#include "gucc/fs_utils.hpp" #include "gucc/initcpio.hpp" #include "gucc/io_utils.hpp" #include "gucc/locale.hpp" @@ -573,7 +574,7 @@ auto get_pkglist_base(const std::string_view& packages) noexcept -> std::vector< const auto& server_mode = std::get(config_data["SERVER_MODE"]); const auto& mountpoint_info = std::get(config_data["MOUNTPOINT"]); - const auto& root_filesystem = utils::get_mountpoint_fs(mountpoint_info); + const auto& root_filesystem = gucc::fs::utils::get_mountpoint_fs(mountpoint_info); const auto& is_root_on_zfs = (root_filesystem == "zfs"); const auto& is_root_on_btrfs = (root_filesystem == "btrfs"); const auto& is_root_on_bcachefs = (root_filesystem == "bcachefs"); @@ -853,7 +854,7 @@ void install_base(const std::string_view& packages) noexcept { std::int32_t btrfs_root = 0; std::int32_t zfs_root = 0; - const auto& filesystem_type = utils::get_mountpoint_fs(mountpoint); + const auto& filesystem_type = gucc::fs::utils::get_mountpoint_fs(mountpoint); spdlog::info("filesystem type on '{}' := '{}'", mountpoint, filesystem_type); if (filesystem_type == "btrfs") { btrfs_root = 1; @@ -966,7 +967,7 @@ void install_grub_uefi(const std::string_view& bootid, bool as_default) noexcept const auto& grub_installer_path = fmt::format(FMT_COMPILE("{}/usr/bin/grub_installer.sh"), mountpoint); // grub config changes for zfs root - if (utils::get_mountpoint_fs(mountpoint) == "zfs") { + if (gucc::fs::utils::get_mountpoint_fs(mountpoint) == "zfs") { // zfs needs ZPOOL_VDEV_NAME_PATH set to properly find the device gucc::utils::exec(fmt::format(FMT_COMPILE("echo ZPOOL_VDEV_NAME_PATH=YES >> {}/etc/environment"), mountpoint)); setenv("ZPOOL_VDEV_NAME_PATH", "YES", 1); @@ -979,7 +980,7 @@ pacman -S --noconfirm --needed grub efibootmgr dosfstools sed -e '/GRUB_SAVEDEFAULT/ s/^#*/#/' -i /etc/default/grub # we need to tell grub where the zfs root is)"; - const auto& mountpoint_source = utils::get_mountpoint_source(mountpoint); + const auto& mountpoint_source = gucc::fs::utils::get_mountpoint_source(mountpoint); const auto& zroot_var = fmt::format(FMT_COMPILE("zroot=\"zfs={} rw\""), mountpoint_source); constexpr auto bash_codepart2 = R"( @@ -1229,7 +1230,7 @@ void bios_bootloader(const std::string_view& bootloader) noexcept { } // If root is on btrfs volume, amend grub - if (utils::get_mountpoint_fs(mountpoint) == "btrfs") { + if (gucc::fs::utils::get_mountpoint_fs(mountpoint) == "btrfs") { gucc::utils::exec(fmt::format(FMT_COMPILE("sed -e '/GRUB_SAVEDEFAULT/ s/^#*/#/' -i {}/etc/default/grub"), mountpoint)); } @@ -1241,7 +1242,7 @@ void bios_bootloader(const std::string_view& bootloader) noexcept { const auto& grub_installer_path = fmt::format(FMT_COMPILE("{}/usr/bin/grub_installer.sh"), mountpoint); // grub config changes for zfs root - if (utils::get_mountpoint_fs(mountpoint) == "zfs") { + if (gucc::fs::utils::get_mountpoint_fs(mountpoint) == "zfs") { // zfs needs ZPOOL_VDEV_NAME_PATH set to properly find the device gucc::utils::exec(fmt::format(FMT_COMPILE("echo ZPOOL_VDEV_NAME_PATH=YES >> {}/etc/environment"), mountpoint)); setenv("ZPOOL_VDEV_NAME_PATH", "YES", 1); @@ -1254,7 +1255,7 @@ pacman -S --noconfirm --needed grub os-prober sed -e '/GRUB_SAVEDEFAULT/ s/^#*/#/' -i /etc/default/grub # we need to tell grub where the zfs root is)"; - const auto& mountpoint_source = utils::get_mountpoint_source(mountpoint); + const auto& mountpoint_source = gucc::fs::utils::get_mountpoint_source(mountpoint); const auto& zroot_var = fmt::format(FMT_COMPILE("zroot=\"zfs={} rw\""), mountpoint_source); constexpr auto bash_codepart2 = R"( @@ -1351,14 +1352,6 @@ std::string list_mounted() noexcept { return gucc::utils::exec("echo /dev/* /dev/mapper/* | xargs -n1 2>/dev/null | grep -f /tmp/.mounted"); } -std::string get_mountpoint_fs(const std::string_view& mountpoint) noexcept { - return gucc::utils::exec(fmt::format(FMT_COMPILE("findmnt -ln -o FSTYPE \"{}\""), mountpoint)); -} - -std::string get_mountpoint_source(const std::string_view& mountpoint) noexcept { - return gucc::utils::exec(fmt::format(FMT_COMPILE("findmnt -ln -o SOURCE \"{}\""), mountpoint)); -} - std::string list_containing_crypt() noexcept { return gucc::utils::exec("blkid | awk '/TYPE=\"crypto_LUKS\"/{print $1}' | sed 's/.$//'"); } diff --git a/src/utils.hpp b/src/utils.hpp index 8fad763..a5e7bae 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -31,8 +31,6 @@ void set_root_password(const std::string_view& password) noexcept; [[nodiscard]] bool check_mount() noexcept; [[nodiscard]] bool check_base() noexcept; [[nodiscard]] auto list_mounted() noexcept -> std::string; -[[nodiscard]] auto get_mountpoint_fs(const std::string_view& mountpoint) noexcept -> std::string; -[[nodiscard]] auto get_mountpoint_source(const std::string_view& mountpoint) noexcept -> std::string; [[nodiscard]] auto list_containing_crypt() noexcept -> std::string; [[nodiscard]] auto list_non_crypt() noexcept -> std::string; void lvm_detect(std::optional> func_callback = std::nullopt) noexcept;