Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
bootloader: gracefully reboot after update completes
Browse files Browse the repository at this point in the history
This patch mainly aims to add a graceful way to reboot the system by
executing a user preferred reboot command rather than calling reboot
system call. The reboot command could be set in 'bootloader' section of
a config file, it defaults to be '/sbin/reboot'.

Signed-off-by: Ming Liu <liu.ming50@gmail.com>
Signed-off-by: Mykhaylo Sul <myk.sul@gmail.com>
  • Loading branch information
liuming50 authored and mike-sul committed Jul 31, 2019
1 parent 3d3b131 commit 1f6171d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
1 change: 1 addition & 0 deletions docs/configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,5 @@ Options for configuring boot-specific behavior
| `rollback_mode` | `"none"` | Controls rollback on supported platforms, see link:{aktualizr-github-url}/docs/rollback.adoc[]. Options: `"none"`, `"uboot_generic"`, `"uboot_masked"`
| `reboot_sentinel_dir` | `"/var/run/aktualizr-session"` | Base directory for reboot detection sentinel. Must reside in a temporary file system.
| `reboot_sentinel_name` | `"need_reboot"` | Name of the reboot detection sentinel.
| `reboot_command` | `"/sbin/reboot"` | Command to reboot the system after update completes. Applicable only if `uptane::force_install_completion` is set to `true`.
|==========================================================================================
23 changes: 14 additions & 9 deletions src/libaktualizr/bootloader/bootloader.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <fcntl.h>
#include <sys/stat.h>

#include <stdlib.h>
#include <string.h>
#include <sys/reboot.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <boost/filesystem.hpp>
Expand All @@ -12,6 +14,7 @@

Bootloader::Bootloader(const BootloaderConfig& config, INvStorage& storage) : config_(config), storage_(storage) {
reboot_sentinel_ = config_.reboot_sentinel_dir / config_.reboot_sentinel_name;
reboot_command_ = config_.reboot_command;

if (!Utils::createSecureDirectory(config_.reboot_sentinel_dir)) {
LOG_WARNING << "Could not create " << config_.reboot_sentinel_dir << " securely, reboot detection support disabled";
Expand Down Expand Up @@ -116,12 +119,14 @@ void Bootloader::rebootFlagClear() {
void Bootloader::reboot(bool fake_reboot) {
if (fake_reboot) {
boost::filesystem::remove(reboot_sentinel_);
} else {
sync();
if (setuid(0) != 0) {
LOG_ERROR << "Failed to set/verify a root user so cannot reboot system programmatically";
} else {
::reboot(RB_AUTOBOOT);
}
return;
}
if (setuid(0) != 0) {
LOG_ERROR << "Failed to set/verify a root user so cannot reboot system programmatically";
return;
}
sync();
if (system(reboot_command_.c_str()) != 0) {
LOG_ERROR << "Failed to execute the reboot command: " << reboot_command_;
}
}
1 change: 1 addition & 0 deletions src/libaktualizr/bootloader/bootloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Bootloader {

INvStorage& storage_;
boost::filesystem::path reboot_sentinel_;
std::string reboot_command_;
bool reboot_detect_supported_{false};
};

Expand Down
2 changes: 2 additions & 0 deletions src/libaktualizr/bootloader/bootloader_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ void BootloaderConfig::updateFromPropertyTree(const boost::property_tree::ptree&
CopyFromConfig(rollback_mode, "rollback_mode", pt);
CopyFromConfig(reboot_sentinel_dir, "reboot_sentinel_dir", pt);
CopyFromConfig(reboot_sentinel_name, "reboot_sentinel_name", pt);
CopyFromConfig(reboot_command, "reboot_command", pt);
}

void BootloaderConfig::writeToStream(std::ostream& out_stream) const {
writeOption(out_stream, rollback_mode, "rollback_mode");
writeOption(out_stream, reboot_sentinel_dir, "reboot_sentinel_dir");
writeOption(out_stream, reboot_sentinel_name, "reboot_sentinel_name");
writeOption(out_stream, reboot_command, "reboot_command");
}
1 change: 1 addition & 0 deletions src/libaktualizr/bootloader/bootloader_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct BootloaderConfig {
RollbackMode rollback_mode{RollbackMode::kBootloaderNone};
boost::filesystem::path reboot_sentinel_dir{"/var/run/aktualizr-session"};
boost::filesystem::path reboot_sentinel_name{"need_reboot"};
std::string reboot_command{"/sbin/reboot"};

void updateFromPropertyTree(const boost::property_tree::ptree& pt);
void writeToStream(std::ostream& out_stream) const;
Expand Down

0 comments on commit 1f6171d

Please sign in to comment.