🧹 move hwclock into gucc

This commit is contained in:
Vladislav Nepogodin 2024-07-06 02:52:46 +04:00
parent 742022ec5c
commit ec7b6f47c6
No known key found for this signature in database
GPG Key ID: B62C3D10C54D5DA9
5 changed files with 76 additions and 1 deletions

View File

@ -29,6 +29,7 @@ add_library(${PROJECT_NAME} SHARED
src/autologin.cpp include/gucc/autologin.hpp
src/mtab.cpp include/gucc/mtab.hpp
src/umount_partitions.cpp include/gucc/umount_partitions.hpp
src/hwclock.cpp include/gucc/hwclock.hpp
#src/chwd_profiles.cpp src/chwd_profiles.hpp
#src/disk.cpp src/disk.hpp
)

View File

@ -0,0 +1,16 @@
#ifndef HWCLOCK_HPP
#define HWCLOCK_HPP
#include <string_view> // for string_view
namespace gucc::hwclock {
// set utc hwclock
auto set_hwclock_utc(std::string_view root_mountpoint) noexcept -> bool;
// set localtime hwclock
auto set_hwclock_localtime(std::string_view root_mountpoint) noexcept -> bool;
} // namespace gucc::hwclock
#endif // HWCLOCK_HPP

View File

@ -19,6 +19,7 @@ gucc_lib = library('gucc',
'src/autologin.cpp',
'src/mtab.cpp',
'src/umount_partitions.cpp',
'src/hwclock.cpp',
],
include_directories : [include_directories('include')],
dependencies: deps

42
gucc/src/hwclock.cpp Normal file
View File

@ -0,0 +1,42 @@
#include "gucc/hwclock.hpp"
#include "gucc/io_utils.hpp"
#include <fmt/compile.h>
#include <fmt/format.h>
#include <spdlog/spdlog.h>
using namespace std::string_view_literals;
namespace gucc::hwclock {
auto set_hwclock_utc(std::string_view root_mountpoint) noexcept -> bool {
// try default
static constexpr auto hwclock_cmd = "hwclock --systohc --utc"sv;
if (utils::arch_chroot_checked(hwclock_cmd, root_mountpoint)) {
spdlog::info("hwclock UTC set on '{}'", root_mountpoint);
return true;
}
// try fallback with direct ISA
static constexpr auto hwclock_isa_cmd = "hwclock --systohc --utc --directisa"sv;
if (utils::arch_chroot_checked(hwclock_isa_cmd, root_mountpoint)) {
spdlog::info("hwclock UTC set using direct ISA on '{}'", root_mountpoint);
return true;
}
return false;
}
auto set_hwclock_localtime(std::string_view root_mountpoint) noexcept -> bool {
// try default
static constexpr auto hwclock_cmd = "hwclock --systohc --localtime"sv;
if (utils::arch_chroot_checked(hwclock_cmd, root_mountpoint)) {
spdlog::info("hwclock localtime set on '{}'", root_mountpoint);
return true;
}
return false;
}
} // namespace gucc::hwclock

View File

@ -11,6 +11,7 @@
#include "gucc/cpu.hpp"
#include "gucc/file_utils.hpp"
#include "gucc/fs_utils.hpp"
#include "gucc/hwclock.hpp"
#include "gucc/initcpio.hpp"
#include "gucc/io_utils.hpp"
#include "gucc/locale.hpp"
@ -432,7 +433,21 @@ void set_timezone(const std::string_view& timezone) noexcept {
void set_hw_clock(const std::string_view& clock_type) noexcept {
spdlog::info("Clock type is: {}", clock_type);
#ifdef NDEVENV
utils::arch_chroot(fmt::format(FMT_COMPILE("hwclock --systohc --{}"), clock_type), false);
auto* config_instance = Config::instance();
auto& config_data = config_instance->data();
const auto& mountpoint = std::get<std::string>(config_data["MOUNTPOINT"]);
if (clock_type == "utc"sv) {
if (!gucc::hwclock::set_hwclock_utc(mountpoint)) {
spdlog::error("Failed to set UTC hwclock");
}
} else if (clock_type == "localtime"sv) {
if (!gucc::hwclock::set_hwclock_localtime(mountpoint)) {
spdlog::error("Failed to set localtime hwclock");
}
} else {
spdlog::error("Unknown clock type {}", clock_type);
}
#endif
}