🧹 zfs: move some functions into gucc

This commit is contained in:
Vladislav Nepogodin 2024-06-27 01:10:32 +04:00
parent 61b8c526d6
commit c5d7edb870
No known key found for this signature in database
GPG Key ID: B62C3D10C54D5DA9
8 changed files with 140 additions and 99 deletions

View File

@ -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
)

29
gucc/include/gucc/zfs.hpp Normal file
View File

@ -0,0 +1,29 @@
#ifndef ZFS_HPP
#define ZFS_HPP
#include <string> // for string
#include <string_view> // 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

View File

@ -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

86
gucc/src/zfs.cpp Normal file
View File

@ -0,0 +1,86 @@
#include "gucc/zfs.hpp"
#include "gucc/io_utils.hpp"
#include "gucc/string_utils.hpp"
#include <fmt/compile.h>
#include <fmt/format.h>
#include <spdlog/spdlog.h>
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

View File

@ -6,6 +6,7 @@
// import gucc
#include "gucc/io_utils.hpp"
#include "gucc/string_utils.hpp"
#include "gucc/zfs.hpp"
#include <filesystem> // for exists, is_directory
#include <ftxui/component/component.hpp> // 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();

View File

@ -1,9 +1,9 @@
#ifndef DISK_HPP
#define DISK_HPP
#include <string>
#include <string_view>
#include <vector>
#include <string> // for string
#include <string_view> // for string_view
#include <vector> // 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<std::string> lvm_show_vg() noexcept;
auto lvm_show_vg() noexcept -> std::vector<std::string>;
// 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;

View File

@ -8,6 +8,7 @@
// import gucc
#include "gucc/io_utils.hpp"
#include "gucc/string_utils.hpp"
#include "gucc/zfs.hpp"
/* clang-format off */
#include <cstdlib> // 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

View File

@ -12,6 +12,7 @@
// import gucc
#include "gucc/io_utils.hpp"
#include "gucc/string_utils.hpp"
#include "gucc/zfs.hpp"
#include <fmt/compile.h>
#include <fmt/format.h>
@ -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);