Skip to content

Commit

Permalink
bootctl: create $BOOT/<machine-id> when installing sd-boot
Browse files Browse the repository at this point in the history
  • Loading branch information
keszybz committed Mar 12, 2019
1 parent d271c5d commit 341890d
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/boot/bootctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "pretty-print.h"
#include "rm-rf.h"
#include "stat-util.h"
#include "stdio-util.h"
#include "string-util.h"
#include "strv.h"
#include "terminal-util.h"
Expand Down Expand Up @@ -885,22 +886,17 @@ static int remove_variables(sd_id128_t uuid, const char *path, bool in_order) {
return 0;
}

static int install_loader_config(const char *esp_path) {
static int install_loader_config(const char *esp_path, sd_id128_t machine_id) {
char machine_string[SD_ID128_STRING_MAX];
_cleanup_(unlink_and_freep) char *t = NULL;
_cleanup_fclose_ FILE *f = NULL;
sd_id128_t machine_id;
const char *p;
int r, fd;

p = strjoina(esp_path, "/loader/loader.conf");
if (access(p, F_OK) >= 0) /* Silently skip creation if the file already exists (early check) */
return 0;

r = sd_id128_get_machine(&machine_id);
if (r < 0)
return log_error_errno(r, "Failed to get machine id: %m");

fd = open_tmpfile_linkable(p, O_WRONLY|O_CLOEXEC, &t);
if (fd < 0)
return log_error_errno(fd, "Failed to open \"%s\" for writing: %m", p);
Expand Down Expand Up @@ -929,10 +925,21 @@ static int install_loader_config(const char *esp_path) {
return 1;
}

static int install_entries_directory(const char *dollar_boot_path) {
static int install_entries_directories(const char *dollar_boot_path, sd_id128_t machine_id) {
int r;
char buf[SD_ID128_STRING_MAX];

assert(dollar_boot_path);

return mkdir_one(dollar_boot_path, "/loader/entries");
/* Both /loader/entries and the entry directories themselves should be located on the same
* partition. Also create the parent directory for entry directories, so that kernel-install
* knows where to put them. */

r = mkdir_one(dollar_boot_path, "/loader/entries");
if (r < 0)
return r;

return mkdir_one(dollar_boot_path, sd_id128_to_string(machine_id, buf));
}

static int help(int argc, char *argv[], void *userdata) {
Expand Down Expand Up @@ -1243,6 +1250,7 @@ static int verb_install(int argc, char *argv[], void *userdata) {
sd_id128_t uuid = SD_ID128_NULL;
uint64_t pstart = 0, psize = 0;
uint32_t part = 0;
sd_id128_t machine_id;
bool install;
int r;

Expand All @@ -1254,6 +1262,10 @@ static int verb_install(int argc, char *argv[], void *userdata) {
if (r < 0)
return r;

r = sd_id128_get_machine(&machine_id);
if (r < 0)
return log_error_errno(r, "Failed to get machine id: %m");

install = streq(argv[0], "install");

RUN_WITH_UMASK(0002) {
Expand All @@ -1271,11 +1283,11 @@ static int verb_install(int argc, char *argv[], void *userdata) {
return r;

if (install) {
r = install_loader_config(arg_esp_path);
r = install_loader_config(arg_esp_path, machine_id);
if (r < 0)
return r;

r = install_entries_directory(arg_dollar_boot_path());
r = install_entries_directories(arg_dollar_boot_path(), machine_id);
if (r < 0)
return r;
}
Expand Down Expand Up @@ -1400,7 +1412,7 @@ static int run(int argc, char *argv[]) {
log_parse_environment();
log_open();

/* If we run in a container, automatically turn of EFI file system access */
/* If we run in a container, automatically turn off EFI file system access */
if (detect_container() > 0)
arg_touch_variables = false;

Expand Down

0 comments on commit 341890d

Please sign in to comment.