From c76930d0d934b55b7e7209f9a4e5406edd51b61b Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Wed, 25 Sep 2024 02:32:27 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20move=20ZFS=20datasets=20creation?= =?UTF-8?q?=20into=20gucc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit use predefined ZFS dataset scheme --- gucc/include/gucc/zfs.hpp | 11 ++++++++++- gucc/src/zfs.cpp | 16 ++++++++++++++-- src/disk.cpp | 17 +++++++++++------ 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/gucc/include/gucc/zfs.hpp b/gucc/include/gucc/zfs.hpp index 3dfb6de..f6ef94c 100644 --- a/gucc/include/gucc/zfs.hpp +++ b/gucc/include/gucc/zfs.hpp @@ -3,14 +3,23 @@ #include // for string #include // for string_view +#include // for vector namespace gucc::fs { +struct ZfsDataset final { + std::string zpath; + std::string mountpoint; +}; + // 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; +auto zfs_create_dataset(std::string_view zpath, std::string_view zmount) noexcept -> bool; + +// Creates a zfs datasets from predefined scheme +auto zfs_create_datasets(const std::vector& zdatasets) noexcept -> bool; void zfs_destroy_dataset(std::string_view zdataset) noexcept; diff --git a/gucc/src/zfs.cpp b/gucc/src/zfs.cpp index 68eeacf..37c2390 100644 --- a/gucc/src/zfs.cpp +++ b/gucc/src/zfs.cpp @@ -21,14 +21,26 @@ 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 { +auto zfs_create_dataset(std::string_view zpath, std::string_view zmount) noexcept -> bool { #ifdef NDEVENV - utils::exec(fmt::format(FMT_COMPILE("zfs create -o mountpoint={} {} 2>>/tmp/cachyos-install.log"), zmount, zpath), true); + return utils::exec_checked(fmt::format(FMT_COMPILE("zfs create -o mountpoint={} {} 2>>/tmp/cachyos-install.log"), zmount, zpath)); #else spdlog::debug("zfs create -o mountpoint={} {}", zmount, zpath); + return true; #endif } +auto zfs_create_datasets(const std::vector& zdatasets) noexcept -> bool { + // Create datasets + for (const auto& zdataset : zdatasets) { + if (!fs::zfs_create_dataset(zdataset.zpath, zdataset.mountpoint)) { + spdlog::error("Failed to create zfs dataset {} at mountpoint {}", zdataset.zpath, zdataset.mountpoint); + return false; + } + } + return true; +} + 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); diff --git a/src/disk.cpp b/src/disk.cpp index 68ba486..0b65982 100644 --- a/src/disk.cpp +++ b/src/disk.cpp @@ -212,12 +212,17 @@ bool zfs_auto_pres(const std::string_view& partition, const std::string_view& zf } // next create the datasets including their parents - 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); + const std::vector default_zfs_datasets{ + gucc::fs::ZfsDataset{.zpath = fmt::format(FMT_COMPILE("{}/ROOT"), zfs_zpool_name), .mountpoint = "none"s}, + gucc::fs::ZfsDataset{.zpath = fmt::format(FMT_COMPILE("{}/ROOT/cos"), zfs_zpool_name), .mountpoint = "none"s}, + gucc::fs::ZfsDataset{.zpath = fmt::format(FMT_COMPILE("{}/ROOT/cos/root"), zfs_zpool_name), .mountpoint = "/"s}, + gucc::fs::ZfsDataset{.zpath = fmt::format(FMT_COMPILE("{}/ROOT/cos/home"), zfs_zpool_name), .mountpoint = "/home"s}, + gucc::fs::ZfsDataset{.zpath = fmt::format(FMT_COMPILE("{}/ROOT/cos/varcache"), zfs_zpool_name), .mountpoint = "/var/cache"s}, + gucc::fs::ZfsDataset{.zpath = fmt::format(FMT_COMPILE("{}/ROOT/cos/varlog"), zfs_zpool_name), .mountpoint = "/var/log"s}, + }; + if (!gucc::fs::zfs_create_datasets(default_zfs_datasets)) { + spdlog::error("Failed to create zfs datasets automatically"); + } #ifdef NDEVENV // set the rootfs