From c5d7edb870a01d036b35d571a485bbb5ab42f038 Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Thu, 27 Jun 2024 01:10:32 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20zfs:=20move=20some=20functions?= =?UTF-8?q?=20into=20gucc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gucc/CMakeLists.txt | 1 + gucc/include/gucc/zfs.hpp | 29 +++++++++++++ gucc/meson.build | 1 + gucc/src/zfs.cpp | 86 +++++++++++++++++++++++++++++++++++++++ src/disk.cpp | 85 ++++---------------------------------- src/disk.hpp | 15 ++----- src/simple_tui.cpp | 3 +- src/tui.cpp | 19 +++++---- 8 files changed, 140 insertions(+), 99 deletions(-) create mode 100644 gucc/include/gucc/zfs.hpp create mode 100644 gucc/src/zfs.cpp diff --git a/gucc/CMakeLists.txt b/gucc/CMakeLists.txt index 8a581a0..fd43494 100644 --- a/gucc/CMakeLists.txt +++ b/gucc/CMakeLists.txt @@ -17,6 +17,7 @@ add_library(${PROJECT_NAME} SHARED src/pacmanconf_repo.cpp include/gucc/pacmanconf_repo.hpp src/initcpio.cpp include/gucc/initcpio.hpp src/luks.cpp include/gucc/luks.hpp + src/zfs.cpp include/gucc/zfs.hpp #src/chwd_profiles.cpp src/chwd_profiles.hpp #src/disk.cpp src/disk.hpp ) diff --git a/gucc/include/gucc/zfs.hpp b/gucc/include/gucc/zfs.hpp new file mode 100644 index 0000000..3dfb6de --- /dev/null +++ b/gucc/include/gucc/zfs.hpp @@ -0,0 +1,29 @@ +#ifndef ZFS_HPP +#define ZFS_HPP + +#include // for string +#include // for string_view + +namespace gucc::fs { + +// Creates a zfs volume +void zfs_create_zvol(std::string_view zsize, std::string_view zpath) noexcept; + +// Creates a zfs filesystem, the first parameter is the ZFS path and the second is the mount path +void zfs_create_dataset(std::string_view zpath, std::string_view zmount) noexcept; + +void zfs_destroy_dataset(std::string_view zdataset) noexcept; + +// returns a list of imported zpools +auto zfs_list_pools() noexcept -> std::string; + +// returns a list of devices containing zfs members +auto zfs_list_devs() noexcept -> std::string; + +auto zfs_list_datasets(std::string_view type = "none") noexcept -> std::string; + +void zfs_set_property(std::string_view property, std::string_view dataset) noexcept; + +} // namespace gucc::fs + +#endif // ZFS_HPP diff --git a/gucc/meson.build b/gucc/meson.build index e227bfa..1c8adcd 100644 --- a/gucc/meson.build +++ b/gucc/meson.build @@ -7,6 +7,7 @@ gucc_lib = library('gucc', 'src/pacmanconf_repo.cpp', 'src/initcpio.cpp', 'src/luks.cpp', + 'src/zfs.cpp', ], include_directories : [include_directories('include')], dependencies: deps diff --git a/gucc/src/zfs.cpp b/gucc/src/zfs.cpp new file mode 100644 index 0000000..5b61f16 --- /dev/null +++ b/gucc/src/zfs.cpp @@ -0,0 +1,86 @@ +#include "gucc/zfs.hpp" +#include "gucc/io_utils.hpp" +#include "gucc/string_utils.hpp" + +#include +#include + +#include + +using namespace std::string_view_literals; + +namespace gucc::fs { + +// Creates a zfs volume +void zfs_create_zvol(std::string_view zsize, std::string_view zpath) noexcept { +#ifdef NDEVENV + utils::exec(fmt::format(FMT_COMPILE("zfs create -V {}M {} 2>>/tmp/cachyos-install.log"), zsize, zpath), true); +#else + spdlog::debug("zfs create -V {}M {}", zsize, zpath); +#endif +} + +// Creates a zfs filesystem, the first parameter is the ZFS path and the second is the mount path +void zfs_create_dataset(std::string_view zpath, std::string_view zmount) noexcept { +#ifdef NDEVENV + utils::exec(fmt::format(FMT_COMPILE("zfs create -o mountpoint={} {} 2>>/tmp/cachyos-install.log"), zmount, zpath), true); +#else + spdlog::debug("zfs create -o mountpoint={} {}", zmount, zpath); +#endif +} + +void zfs_destroy_dataset(std::string_view zdataset) noexcept { +#ifdef NDEVENV + utils::exec(fmt::format(FMT_COMPILE("zfs destroy -r {} 2>>/tmp/cachyos-install.log"), zdataset), true); +#else + spdlog::debug("zfs destroy -r {}", zdataset); +#endif +} + +// returns a list of imported zpools +auto zfs_list_pools() noexcept -> std::string { +#ifdef NDEVENV + return utils::exec("zfs list -H -o name 2>/dev/null | grep \"/\""sv); +#else + return {"vol0\nvol1\n"}; +#endif +} + +// returns a list of devices containing zfs members +auto zfs_list_devs() noexcept -> std::string { + std::string list_of_devices{}; + // get a list of devices with zpools on them + const auto& devices = utils::make_multiline(utils::exec("zpool status -PL 2>/dev/null | awk '{print $1}' | grep \"^/\""sv)); + for (const auto& device : devices) { + // add the device + list_of_devices += fmt::format(FMT_COMPILE("{}\n"), device); + // now let's add any other forms of those devices + list_of_devices += utils::exec(fmt::format(FMT_COMPILE("find -L /dev/ -xtype l -samefile {} 2>/dev/null"), device)); + } + return list_of_devices; +} + +auto zfs_list_datasets(std::string_view type) noexcept -> std::string { +#ifdef NDEVENV + if (type == "zvol"sv) { + return utils::exec("zfs list -Ht volume -o name,volsize 2>/dev/null"sv); + } else if (type == "legacy"sv) { + return utils::exec("zfs list -Ht filesystem -o name,mountpoint 2>/dev/null | grep \"^.*/.*legacy$\" | awk '{print $1}'"sv); + } + + return utils::exec("zfs list -H -o name 2>/dev/null | grep \"/\""sv); +#else + spdlog::debug("type := {}", type); + return {"zpcachyos"}; +#endif +} + +void zfs_set_property(std::string_view property, std::string_view dataset) noexcept { +#ifdef NDEVENV + utils::exec(fmt::format(FMT_COMPILE("zfs set {} {} 2>>/tmp/cachyos-install.log"), property, dataset), true); +#else + spdlog::debug("zfs set {} {}", property, dataset); +#endif +} + +} // namespace gucc::fs diff --git a/src/disk.cpp b/src/disk.cpp index 0058b99..4479bd1 100644 --- a/src/disk.cpp +++ b/src/disk.cpp @@ -6,6 +6,7 @@ // import gucc #include "gucc/io_utils.hpp" #include "gucc/string_utils.hpp" +#include "gucc/zfs.hpp" #include // for exists, is_directory #include // for Renderer, Button @@ -149,12 +150,12 @@ bool zfs_auto_pres(const std::string_view& partition, const std::string_view& zf } // next create the datasets including their parents - utils::zfs_create_dataset(fmt::format(FMT_COMPILE("{}/ROOT"), zfs_zpool_name), "none"sv); - utils::zfs_create_dataset(fmt::format(FMT_COMPILE("{}/ROOT/cos"), zfs_zpool_name), "none"sv); - utils::zfs_create_dataset(fmt::format(FMT_COMPILE("{}/ROOT/cos/root"), zfs_zpool_name), "/"sv); - utils::zfs_create_dataset(fmt::format(FMT_COMPILE("{}/ROOT/cos/home"), zfs_zpool_name), "/home"sv); - utils::zfs_create_dataset(fmt::format(FMT_COMPILE("{}/ROOT/cos/varcache"), zfs_zpool_name), "/var/cache"sv); - utils::zfs_create_dataset(fmt::format(FMT_COMPILE("{}/ROOT/cos/varlog"), zfs_zpool_name), "/var/log"sv); + gucc::fs::zfs_create_dataset(fmt::format(FMT_COMPILE("{}/ROOT"), zfs_zpool_name), "none"sv); + gucc::fs::zfs_create_dataset(fmt::format(FMT_COMPILE("{}/ROOT/cos"), zfs_zpool_name), "none"sv); + gucc::fs::zfs_create_dataset(fmt::format(FMT_COMPILE("{}/ROOT/cos/root"), zfs_zpool_name), "/"sv); + gucc::fs::zfs_create_dataset(fmt::format(FMT_COMPILE("{}/ROOT/cos/home"), zfs_zpool_name), "/home"sv); + gucc::fs::zfs_create_dataset(fmt::format(FMT_COMPILE("{}/ROOT/cos/varcache"), zfs_zpool_name), "/var/cache"sv); + gucc::fs::zfs_create_dataset(fmt::format(FMT_COMPILE("{}/ROOT/cos/varlog"), zfs_zpool_name), "/var/log"sv); #ifdef NDEVENV // set the rootfs @@ -208,78 +209,6 @@ bool zfs_create_zpool(const std::string_view& partition, const std::string_view& return true; } -// Creates a zfs volume -void zfs_create_zvol(const std::string_view& zsize, const std::string_view& zpath) noexcept { -#ifdef NDEVENV - gucc::utils::exec(fmt::format(FMT_COMPILE("zfs create -V {}M {} 2>>/tmp/cachyos-install.log"), zsize, zpath), true); -#else - spdlog::debug("zfs create -V {}M {}", zsize, zpath); -#endif -} - -// Creates a zfs filesystem, the first parameter is the ZFS path and the second is the mount path -void zfs_create_dataset(const std::string_view& zpath, const std::string_view& zmount) noexcept { -#ifdef NDEVENV - gucc::utils::exec(fmt::format(FMT_COMPILE("zfs create -o mountpoint={} {} 2>>/tmp/cachyos-install.log"), zmount, zpath), true); -#else - spdlog::debug("zfs create -o mountpoint={} {}", zmount, zpath); -#endif -} - -void zfs_destroy_dataset(const std::string_view& zdataset) noexcept { -#ifdef NDEVENV - gucc::utils::exec(fmt::format(FMT_COMPILE("zfs destroy -r {} 2>>/tmp/cachyos-install.log"), zdataset), true); -#else - spdlog::debug("zfs destroy -r {}", zdataset); -#endif -} - -// returns a list of imported zpools -std::string zfs_list_pools() noexcept { -#ifdef NDEVENV - return gucc::utils::exec("zfs list -H -o name 2>/dev/null | grep \"/\""sv); -#else - return "vol0\nvol1\n"; -#endif -} - -// returns a list of devices containing zfs members -std::string zfs_list_devs() noexcept { - std::string list_of_devices{}; - // get a list of devices with zpools on them - const auto& devices = gucc::utils::make_multiline(gucc::utils::exec("zpool status -PL 2>/dev/null | awk '{print $1}' | grep \"^/\""sv)); - for (const auto& device : devices) { - // add the device - list_of_devices += fmt::format(FMT_COMPILE("{}\n"), device); - // now let's add any other forms of those devices - list_of_devices += gucc::utils::exec(fmt::format(FMT_COMPILE("find -L /dev/ -xtype l -samefile {} 2>/dev/null"), device)); - } - return list_of_devices; -} - -std::string zfs_list_datasets(const std::string_view& type) noexcept { -#ifdef NDEVENV - if (type == "zvol"sv) { - return gucc::utils::exec("zfs list -Ht volume -o name,volsize 2>/dev/null"sv); - } else if (type == "legacy"sv) { - return gucc::utils::exec("zfs list -Ht filesystem -o name,mountpoint 2>/dev/null | grep \"^.*/.*legacy$\" | awk '{print $1}'"sv); - } - - return gucc::utils::exec("zfs list -H -o name 2>/dev/null | grep \"/\""sv); -#else - spdlog::debug("type := {}", type); - return "zpcachyos"; -#endif -} - -void zfs_set_property(const std::string_view& property, const std::string_view& dataset) noexcept { -#ifdef NDEVENV - gucc::utils::exec(fmt::format(FMT_COMPILE("zfs set {} {} 2>>/tmp/cachyos-install.log"), property, dataset), true); -#else - spdlog::debug("zfs set {} {}", property, dataset); -#endif -} - // Other filesystems void select_filesystem(const std::string_view& file_sys) noexcept { auto* config_instance = Config::instance(); diff --git a/src/disk.hpp b/src/disk.hpp index 6967f58..649ed55 100644 --- a/src/disk.hpp +++ b/src/disk.hpp @@ -1,9 +1,9 @@ #ifndef DISK_HPP #define DISK_HPP -#include -#include -#include +#include // for string +#include // for string_view +#include // for vector namespace utils { @@ -15,18 +15,11 @@ struct disk_part { void btrfs_create_subvols(const disk_part& disk, const std::string_view& mode, bool ignore_note = false) noexcept; void mount_existing_subvols(const disk_part& disk) noexcept; -std::vector lvm_show_vg() noexcept; +auto lvm_show_vg() noexcept -> std::vector; // ZFS filesystem [[nodiscard]] bool zfs_auto_pres(const std::string_view& partition, const std::string_view& zfs_zpool_name) noexcept; [[nodiscard]] bool zfs_create_zpool(const std::string_view& partition, const std::string_view& pool_name) noexcept; -void zfs_create_zvol(const std::string_view& zsize, const std::string_view& zpath) noexcept; -void zfs_create_dataset(const std::string_view& zpath, const std::string_view& zmount) noexcept; -void zfs_destroy_dataset(const std::string_view& zdataset) noexcept; -[[nodiscard]] std::string zfs_list_pools() noexcept; -[[nodiscard]] std::string zfs_list_devs() noexcept; -[[nodiscard]] std::string zfs_list_datasets(const std::string_view& type = "none") noexcept; -void zfs_set_property(const std::string_view& property, const std::string_view& dataset) noexcept; // Other filesystems void select_filesystem(const std::string_view& fs) noexcept; diff --git a/src/simple_tui.cpp b/src/simple_tui.cpp index 9fb240e..9834bec 100644 --- a/src/simple_tui.cpp +++ b/src/simple_tui.cpp @@ -8,6 +8,7 @@ // import gucc #include "gucc/io_utils.hpp" #include "gucc/string_utils.hpp" +#include "gucc/zfs.hpp" /* clang-format off */ #include // for setenv @@ -350,7 +351,7 @@ void menu_simple() noexcept { // Filter out partitions that have already been mounted and partitions that just contain crypt or zfs devices auto ignore_part = utils::list_mounted(); - ignore_part += utils::zfs_list_devs(); + ignore_part += gucc::fs::zfs_list_devs(); ignore_part += utils::list_containing_crypt(); // We must have bootloader before running partitioning step diff --git a/src/tui.cpp b/src/tui.cpp index c784e78..bc5dd6e 100644 --- a/src/tui.cpp +++ b/src/tui.cpp @@ -12,6 +12,7 @@ // import gucc #include "gucc/io_utils.hpp" #include "gucc/string_utils.hpp" +#include "gucc/zfs.hpp" #include #include @@ -1326,7 +1327,7 @@ bool zfs_create_zpool(bool do_create_zpool = true) noexcept { // Filter out partitions that have already been mounted and partitions that just contain crypt or zfs devices auto ignore_part = utils::list_mounted(); - ignore_part += utils::zfs_list_devs(); + ignore_part += gucc::fs::zfs_list_devs(); ignore_part += utils::list_containing_crypt(); /* const auto& parts = gucc::utils::make_multiline(ignore_part); @@ -1438,7 +1439,7 @@ bool zfs_import_pool() noexcept { } bool zfs_new_ds(const std::string_view& zmount = "") noexcept { - const auto& zlist = gucc::utils::make_multiline(utils::zfs_list_pools()); + const auto& zlist = gucc::utils::make_multiline(gucc::fs::zfs_list_pools()); if (zlist.empty()) { // no available datasets detail::infobox_widget("\nNo pools available\"\n"sv); @@ -1489,7 +1490,7 @@ bool zfs_new_ds(const std::string_view& zmount = "") noexcept { } if (zmount == "legacy") { - utils::zfs_create_dataset(fmt::format(FMT_COMPILE("{}/{}"), zfs_zpool_name, zfs_dataset_name), zmount); + gucc::fs::zfs_create_dataset(fmt::format(FMT_COMPILE("{}/{}"), zfs_zpool_name, zfs_dataset_name), zmount); } else if (zmount == "zvol") { static constexpr auto zvol_size_menu_body = "\nEnter the size of the zvol in megabytes(MB)\n"sv; static constexpr auto zvol_size_menu_validation = "\nYou must enter a number greater than 0\n"sv; @@ -1515,7 +1516,7 @@ bool zfs_new_ds(const std::string_view& zmount = "") noexcept { if (zfs_menu_text == zvol_size_menu_body) { break; } /* clang-format on */ } - utils::zfs_create_zvol(zvol_size, fmt::format(FMT_COMPILE("{}/{}"), zfs_zpool_name, zfs_dataset_name)); + gucc::fs::zfs_create_zvol(zvol_size, fmt::format(FMT_COMPILE("{}/{}"), zfs_zpool_name, zfs_dataset_name)); } else { spdlog::error("HELLO! IMPLEMENT ME!"); return false; @@ -1525,7 +1526,7 @@ bool zfs_new_ds(const std::string_view& zmount = "") noexcept { } void zfs_set_property() noexcept { - const auto& zlist = gucc::utils::make_multiline(utils::zfs_list_datasets()); + const auto& zlist = gucc::utils::make_multiline(gucc::fs::zfs_list_datasets()); if (zlist.empty()) { // no available datasets detail::infobox_widget("\nNo datasets available\"\n"sv); @@ -1576,11 +1577,11 @@ void zfs_set_property() noexcept { } // Set the property - utils::zfs_set_property(zfs_property_ent, zdataset); + gucc::fs::zfs_set_property(zfs_property_ent, zdataset); } void zfs_destroy_dataset() noexcept { - const auto& zlist = gucc::utils::make_multiline(utils::zfs_list_datasets()); + const auto& zlist = gucc::utils::make_multiline(gucc::fs::zfs_list_datasets()); if (zlist.empty()) { // no available datasets detail::infobox_widget("\nNo datasets available\"\n"sv); @@ -1612,7 +1613,7 @@ void zfs_destroy_dataset() noexcept { if (!do_destroy) { return; } /* clang-format on */ - utils::zfs_destroy_dataset(zdataset); + gucc::fs::zfs_destroy_dataset(zdataset); } // Automated configuration of zfs. Creates a new zpool and a default set of filesystems @@ -1840,7 +1841,7 @@ void mount_partitions() noexcept { // Filter out partitions that have already been mounted and partitions that just contain crypt or zfs devices auto ignore_part = utils::list_mounted(); - ignore_part += utils::zfs_list_devs(); + ignore_part += gucc::fs::zfs_list_devs(); ignore_part += utils::list_containing_crypt(); /* const auto& parts = gucc::utils::make_multiline(ignore_part);