From 1bfbd7c7b7f0455a052e71dd0aba0dccafb60a80 Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Fri, 12 Aug 2022 03:49:00 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7=20implement=20using=20native=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils.cpp | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/utils.cpp b/src/utils.cpp index 7b5f36f..3dd2848 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -5,6 +5,7 @@ #include "subprocess.h" #include "tui.hpp" #include "widgets.hpp" +#include "initcpio.hpp" #include // for transform #include // for array @@ -706,6 +707,9 @@ void install_base(const std::string_view& packages) noexcept { const auto& mountpoint = std::get(config_data["MOUNTPOINT"]); const auto& zfs = std::get(config_data["ZFS"]); + const auto& initcpio_filename = fmt::format(FMT_COMPILE("{}/etc/mkinitcpio.conf"), mountpoint); + auto initcpio = detail::Initcpio{initcpio_filename}; + // filter_packages utils::install_from_pkglist(base_pkgs); @@ -721,10 +725,13 @@ void install_base(const std::string_view& packages) noexcept { const auto& filesystem_type = fmt::format(FMT_COMPILE("findmnt -ln -o FSTYPE {}"), mountpoint); if (filesystem_type == "btrfs") { btrfs_root = 1; - utils::exec(fmt::format(FMT_COMPILE("sed -e '/^HOOKS=/s/\\ fsck//g' -e '/^MODULES=/s/\"$/ btrfs\"/g' -i {}/etc/mkinitcpio.conf"), mountpoint)); + initcpio.remove_hook("fsck"); + initcpio.append_module("btrfs"); } else if (filesystem_type == "zfs") { zfs_root = 1; - utils::exec(fmt::format(FMT_COMPILE("sed -e '/^HOOKS=/s/\\ filesystems//g' -e '/^HOOKS=/s/\\ keyboard/\\ keyboard\\ zfs\\ filesystems/g' -e '/^HOOKS=/s/\\ fsck//g' -e '/^FILES=/c\\FILES=(\"/usr/lib/libgcc_s.so.1\")' -i {}/etc/mkinitcpio.conf"), mountpoint)); + initcpio.remove_hook("fsck"); + initcpio.insert_hook("filesystems", "zfs"); + initcpio.append_file("\"/usr/lib/libgcc_s.so.1\""); } utils::recheck_luks(); @@ -734,16 +741,22 @@ void install_base(const std::string_view& packages) noexcept { const auto& luks = std::get(config_data["LUKS"]); if (lvm == 1 && luks == 0) { - utils::exec(fmt::format(FMT_COMPILE("sed -i 's/block filesystems/block lvm2 filesystems/g' {}/etc/mkinitcpio.conf"), mountpoint)); + initcpio.insert_hook("filesystems", "lvm2"); spdlog::info("add lvm2 hook"); } else if (lvm == 0 && luks == 1) { - utils::exec(fmt::format(FMT_COMPILE("sed -i 's/block filesystems keyboard/block consolefont keymap keyboard encrypt filesystems/g' {}/etc/mkinitcpio.conf"), mountpoint)); + initcpio.insert_hook("keyboard", {"consolefont", "keymap"}); + initcpio.insert_hook("filesystems", "encrypt"); spdlog::info("add luks hook"); } else if (lvm == 1 && luks == 1) { - utils::exec(fmt::format(FMT_COMPILE("sed -i 's/block filesystems keyboard/block consolefont keymap keyboard encrypt lvm2 filesystems/g' {}/etc/mkinitcpio.conf"), mountpoint)); + initcpio.insert_hook("keyboard", {"consolefont", "keymap"}); + initcpio.insert_hook("filesystems", {"encrypt", "lvm2"}); spdlog::info("add lvm/luks hooks"); } + // Just explicitly flush the data to file, + // if smth really happened between our calls. + initcpio.write(); + if (lvm + luks + btrfs_root + zfs_root > 0) { utils::arch_chroot("mkinitcpio -P"); spdlog::info("re-run mkinitcpio"); @@ -924,8 +937,11 @@ void install_refind() noexcept { if (utils::to_int(removable.data()) == 1) { utils::exec("refind-install --root /mnt --alldrivers --yes 2>>/tmp/cachyos-install.log &>/dev/null"); + const auto& initcpio_filename = fmt::format(FMT_COMPILE("{}/etc/mkinitcpio.conf"), mountpoint); + auto initcpio = detail::Initcpio{initcpio_filename}; + // Remove autodetect hook - utils::exec("sed -i -e '/^HOOKS=/s/\\ autodetect//g' /mnt/etc/mkinitcpio.conf"); + initcpio.remove_hook("autodetect"); spdlog::info("\"Autodetect\" hook was removed"); } else if (luks == 1) { utils::exec("refind-install --root /mnt --alldrivers --yes 2>>/tmp/cachyos-install.log &>/dev/null"); @@ -1006,8 +1022,12 @@ void install_systemd_boot() noexcept { spdlog::info("root_name: {}. root_device: {}", root_name, root_device); const auto& removable = utils::exec(fmt::format(FMT_COMPILE("cat /sys/block/{}/removable"), root_device)); if (utils::to_int(removable.data()) == 1) { + const auto& mountpoint = std::get(config_data["MOUNTPOINT"]); + const auto& initcpio_filename = fmt::format(FMT_COMPILE("{}/etc/mkinitcpio.conf"), mountpoint); + auto initcpio = detail::Initcpio{initcpio_filename}; + // Remove autodetect hook - utils::exec("sed -i -e '/^HOOKS=/s/\\ autodetect//g' /mnt/etc/mkinitcpio.conf"); + initcpio.remove_hook("autodetect"); spdlog::info("\"Autodetect\" hook was removed"); } #endif