🧹 gucc: refactor to use utils::exec_checked

This commit is contained in:
Vladislav Nepogodin 2024-07-14 02:54:41 +04:00
parent c275c0f0bd
commit 5670da5c8a
No known key found for this signature in database
GPG Key ID: B62C3D10C54D5DA9
2 changed files with 7 additions and 10 deletions

View File

@ -11,12 +11,12 @@
namespace gucc::crypto {
auto luks1_open(std::string_view luks_pass, std::string_view partition, std::string_view luks_name) noexcept -> bool {
auto cmd = fmt::format(FMT_COMPILE("echo \"{}\" | cryptsetup open --type luks1 {} {} &>/dev/null"), luks_pass, partition, luks_name);
auto cmd = fmt::format(FMT_COMPILE("echo '{}' | cryptsetup open --type luks1 {} {} &>/dev/null"), luks_pass, partition, luks_name);
return utils::exec_checked(cmd);
}
auto luks1_format(std::string_view luks_pass, std::string_view partition, std::string_view additional_flags) noexcept -> bool {
auto cmd = fmt::format(FMT_COMPILE("echo \"{}\" | cryptsetup -q {} --type luks1 luksFormat {} &>/dev/null"), luks_pass, additional_flags, partition);
auto cmd = fmt::format(FMT_COMPILE("echo '{}' | cryptsetup -q {} --type luks1 luksFormat {} &>/dev/null"), luks_pass, additional_flags, partition);
return utils::exec_checked(cmd);
}
@ -33,9 +33,7 @@ auto luks1_setup_keyfile(std::string_view dest_file, std::string_view mountpoint
if (!fs::exists(dest_file)) {
// TODO(vnepogodin): refactor subprocess exit code and print stderr captured output on failure
auto cmd = fmt::format(FMT_COMPILE("dd bs=512 count=4 if=/dev/urandom of={} iflag=fullblock &>/dev/null"), additional_flags, partition, dest_file);
const auto& ret_status = utils::exec(cmd, true);
if (ret_status != "0") {
if (!utils::exec_checked(cmd)) {
spdlog::error("Failed to generate(dd) luks keyfile: {}!");
return false;
}
@ -56,9 +54,8 @@ auto luks1_setup_keyfile(std::string_view dest_file, std::string_view mountpoint
const auto& mkinitcpio_conf = fmt::format(FMT_COMPILE("{}/etc/mkinitcpio.conf"), mountpoint);
// TODO(vnepogodin): that should gathered from dest_file and be relative to mountpoint
const auto& cmd = fmt::format(FMT_COMPILE("grep -q '/crypto_keyfile.bin' {0} || sed -i '/FILES/ s~)~/crypto_keyfile.bin)~' {0}"), mkinitcpio_conf);
const auto& ret_status = utils::exec(cmd, true);
if (ret_status != "0") {
const auto& cmd = fmt::format(FMT_COMPILE("grep -q '/crypto_keyfile.bin' {0} || sed -i '/FILES/ s~)~/crypto_keyfile.bin)~' {0}"), mkinitcpio_conf);
if (!utils::exec_checked(cmd)) {
spdlog::error("Failed to add keyfile to {}", mkinitcpio_conf);
return false;
}

View File

@ -26,7 +26,7 @@ auto umount_partitions(std::string_view root_mountpoint, const std::vector<std::
spdlog::debug("Got {} entries from mountpoint {}", mtab_entries->size(), root_mountpoint);
for (auto&& mtab_entry : std::move(*mtab_entries)) {
const auto& umount_cmd = fmt::format(FMT_COMPILE("umount -v {} &>>/tmp/cachyos-install.log"), mtab_entry.mountpoint);
if (utils::exec(umount_cmd, true) != "0") {
if (!utils::exec_checked(umount_cmd)) {
spdlog::error("Failed to umount partition: {} {}", mtab_entry.device, mtab_entry.mountpoint);
return false;
}
@ -34,7 +34,7 @@ auto umount_partitions(std::string_view root_mountpoint, const std::vector<std::
for (auto&& zfs_poolname : zfs_poolnames) {
const auto& zpool_export_cmd = fmt::format(FMT_COMPILE("zpool export {} &>>/tmp/cachyos-install.log"), zfs_poolname);
if (utils::exec(zpool_export_cmd, true) != "0") {
if (!utils::exec_checked(zpool_export_cmd)) {
spdlog::error("Failed to export zpool: {}", zfs_poolname);
return false;
}