🚧 properly handle errors on zfs operations

This commit is contained in:
Vladislav Nepogodin 2024-09-25 02:59:36 +04:00
parent c76930d0d9
commit ff53cd84a3
No known key found for this signature in database
GPG Key ID: B62C3D10C54D5DA9
3 changed files with 22 additions and 12 deletions

View File

@ -13,7 +13,7 @@ struct ZfsDataset final {
}; };
// Creates a zfs volume // Creates a zfs volume
void zfs_create_zvol(std::string_view zsize, std::string_view zpath) noexcept; auto zfs_create_zvol(std::string_view zsize, std::string_view zpath) noexcept -> bool;
// Creates a zfs filesystem, the first parameter is the ZFS path and the second is the mount path // Creates a zfs filesystem, the first parameter is the ZFS path and the second is the mount path
auto zfs_create_dataset(std::string_view zpath, std::string_view zmount) noexcept -> bool; auto zfs_create_dataset(std::string_view zpath, std::string_view zmount) noexcept -> bool;
@ -21,7 +21,7 @@ auto zfs_create_dataset(std::string_view zpath, std::string_view zmount) noexcep
// Creates a zfs datasets from predefined scheme // Creates a zfs datasets from predefined scheme
auto zfs_create_datasets(const std::vector<ZfsDataset>& zdatasets) noexcept -> bool; auto zfs_create_datasets(const std::vector<ZfsDataset>& zdatasets) noexcept -> bool;
void zfs_destroy_dataset(std::string_view zdataset) noexcept; auto zfs_destroy_dataset(std::string_view zdataset) noexcept -> bool;
// returns a list of imported zpools // returns a list of imported zpools
auto zfs_list_pools() noexcept -> std::string; auto zfs_list_pools() noexcept -> std::string;
@ -31,7 +31,8 @@ auto zfs_list_devs() noexcept -> std::string;
auto zfs_list_datasets(std::string_view type = "none") 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; // Sets zfs property
auto zfs_set_property(std::string_view property, std::string_view dataset) noexcept -> bool;
} // namespace gucc::fs } // namespace gucc::fs

View File

@ -12,11 +12,12 @@ using namespace std::string_view_literals;
namespace gucc::fs { namespace gucc::fs {
// Creates a zfs volume // Creates a zfs volume
void zfs_create_zvol(std::string_view zsize, std::string_view zpath) noexcept { auto zfs_create_zvol(std::string_view zsize, std::string_view zpath) noexcept -> bool {
#ifdef NDEVENV #ifdef NDEVENV
utils::exec(fmt::format(FMT_COMPILE("zfs create -V {}M {} 2>>/tmp/cachyos-install.log"), zsize, zpath), true); return utils::exec_checked(fmt::format(FMT_COMPILE("zfs create -V {}M {} 2>>/tmp/cachyos-install.log"), zsize, zpath));
#else #else
spdlog::debug("zfs create -V {}M {}", zsize, zpath); spdlog::debug("zfs create -V {}M {}", zsize, zpath);
return true;
#endif #endif
} }
@ -41,11 +42,12 @@ auto zfs_create_datasets(const std::vector<ZfsDataset>& zdatasets) noexcept -> b
return true; return true;
} }
void zfs_destroy_dataset(std::string_view zdataset) noexcept { auto zfs_destroy_dataset(std::string_view zdataset) noexcept -> bool {
#ifdef NDEVENV #ifdef NDEVENV
utils::exec(fmt::format(FMT_COMPILE("zfs destroy -r {} 2>>/tmp/cachyos-install.log"), zdataset), true); return utils::exec_checked(fmt::format(FMT_COMPILE("zfs destroy -r {} 2>>/tmp/cachyos-install.log"), zdataset));
#else #else
spdlog::debug("zfs destroy -r {}", zdataset); spdlog::debug("zfs destroy -r {}", zdataset);
return true;
#endif #endif
} }
@ -87,11 +89,12 @@ auto zfs_list_datasets(std::string_view type) noexcept -> std::string {
#endif #endif
} }
void zfs_set_property(std::string_view property, std::string_view dataset) noexcept { auto zfs_set_property(std::string_view property, std::string_view dataset) noexcept -> bool {
#ifdef NDEVENV #ifdef NDEVENV
utils::exec(fmt::format(FMT_COMPILE("zfs set {} {} 2>>/tmp/cachyos-install.log"), property, dataset), true); return utils::exec_checked(fmt::format(FMT_COMPILE("zfs set {} {} 2>>/tmp/cachyos-install.log"), property, dataset));
#else #else
spdlog::debug("zfs set {} {}", property, dataset); spdlog::debug("zfs set {} {}", property, dataset);
return true;
#endif #endif
} }

View File

@ -1346,7 +1346,9 @@ bool zfs_new_ds(const std::string_view& zmount = "") noexcept {
const auto& zfs_zpath = fmt::format(FMT_COMPILE("{}/{}"), zfs_zpool_name, zfs_dataset_name); const auto& zfs_zpath = fmt::format(FMT_COMPILE("{}/{}"), zfs_zpool_name, zfs_dataset_name);
if (zmount == "legacy"sv) { if (zmount == "legacy"sv) {
gucc::fs::zfs_create_dataset(zfs_zpath, zmount); if (!gucc::fs::zfs_create_dataset(zfs_zpath, zmount)) {
spdlog::error("Failed to create zfs dataset {} at mountpoint {}", zfs_zpath, zmount);
}
} else if (zmount == "zvol"sv) { } else if (zmount == "zvol"sv) {
static constexpr auto zvol_size_menu_body = "\nEnter the size of the zvol in megabytes(MB)\n"sv; 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; static constexpr auto zvol_size_menu_validation = "\nYou must enter a number greater than 0\n"sv;
@ -1372,7 +1374,9 @@ bool zfs_new_ds(const std::string_view& zmount = "") noexcept {
if (zfs_menu_text == zvol_size_menu_body) { break; } if (zfs_menu_text == zvol_size_menu_body) { break; }
/* clang-format on */ /* clang-format on */
} }
gucc::fs::zfs_create_zvol(zvol_size, zfs_zpath); if (!gucc::fs::zfs_create_zvol(zvol_size, zfs_zpath)) {
spdlog::error("Failed to create zfs zvol {} with size {}", zfs_zpath, zvol_size);
}
} else { } else {
spdlog::error("HELLO! IMPLEMENT ME!"); spdlog::error("HELLO! IMPLEMENT ME!");
return false; return false;
@ -1433,7 +1437,9 @@ void zfs_set_property() noexcept {
} }
// Set the property // Set the property
gucc::fs::zfs_set_property(zfs_property_ent, zdataset); if (!gucc::fs::zfs_set_property(zfs_property_ent, zdataset)) {
spdlog::error("Failed to set zfs property '{}' on dataset '{}'", zfs_property_ent, zdataset);
}
} }
void zfs_destroy_dataset() noexcept { void zfs_destroy_dataset() noexcept {