Skip to content

Commit

Permalink
🧹 move ZFS datasets creation into gucc
Browse files Browse the repository at this point in the history
use predefined ZFS dataset scheme
  • Loading branch information
vnepogodin committed Sep 24, 2024
1 parent e98c7fb commit c76930d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
11 changes: 10 additions & 1 deletion gucc/include/gucc/zfs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@

#include <string> // for string
#include <string_view> // for string_view
#include <vector> // 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<ZfsDataset>& zdatasets) noexcept -> bool;

void zfs_destroy_dataset(std::string_view zdataset) noexcept;

Expand Down
16 changes: 14 additions & 2 deletions gucc/src/zfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ZfsDataset>& 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);
Expand Down
17 changes: 11 additions & 6 deletions src/disk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<gucc::fs::ZfsDataset> 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
Expand Down

0 comments on commit c76930d

Please sign in to comment.