Merge pull request #16687 from DaanDeMeyer/bootloader-machine-id

Allow bootctl and kernel-install to be called without /etc/machine-id present
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2020-08-18 15:05:26 +02:00 committed by GitHub
commit f9536e6793
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 56 deletions

View File

@ -211,7 +211,9 @@
<filename>/etc/machine-id</filename>
</term>
<listitem>
<para>The content of the file specifies the machine identification <replaceable>MACHINE-ID</replaceable>.</para>
<para>The content of this file specifies the machine identification
<replaceable>MACHINE-ID</replaceable>. If it cannot read <filename>/etc/machine-id</filename>,
kernel-install will use "Linux" as the machine ID instead.</para>
</listitem>
</varlistentry>
<varlistentry>

View File

@ -888,14 +888,6 @@ static int remove_subdirs(const char *root, const char *const *subdirs) {
return r < 0 ? r : q;
}
static int remove_machine_id_directory(const char *root, sd_id128_t machine_id) {
char buf[SD_ID128_STRING_MAX];
assert(root);
return rmdir_one(root, sd_id128_to_string(machine_id, buf));
}
static int remove_binaries(const char *esp_path) {
const char *p;
int r, q;
@ -978,8 +970,7 @@ static int remove_loader_variables(void) {
return r;
}
static int install_loader_config(const char *esp_path, sd_id128_t machine_id) {
char machine_string[SD_ID128_STRING_MAX];
static int install_loader_config(const char *esp_path) {
_cleanup_(unlink_and_freep) char *t = NULL;
_cleanup_fclose_ FILE *f = NULL;
_cleanup_close_ int fd = -1;
@ -999,8 +990,7 @@ static int install_loader_config(const char *esp_path, sd_id128_t machine_id) {
return log_oom();
fprintf(f, "#timeout 3\n"
"#console-mode keep\n"
"default %s-*\n", sd_id128_to_string(machine_id, machine_string));
"#console-mode keep\n");
r = fflush_sync_and_check(f);
if (r < 0)
@ -1016,14 +1006,6 @@ static int install_loader_config(const char *esp_path, sd_id128_t machine_id) {
return 1;
}
static int install_machine_id_directory(const char *root, sd_id128_t machine_id) {
char buf[SD_ID128_STRING_MAX];
assert(root);
return mkdir_one(root, sd_id128_to_string(machine_id, buf));
}
static int help(int argc, char *argv[], void *userdata) {
_cleanup_free_ char *link = NULL;
int r;
@ -1531,7 +1513,6 @@ 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;
@ -1543,10 +1524,6 @@ 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) {
@ -1568,11 +1545,7 @@ static int verb_install(int argc, char *argv[], void *userdata) {
return r;
if (install) {
r = install_loader_config(arg_esp_path, machine_id);
if (r < 0)
return r;
r = install_machine_id_directory(arg_dollar_boot_path(), machine_id);
r = install_loader_config(arg_esp_path);
if (r < 0)
return r;
@ -1594,7 +1567,7 @@ static int verb_install(int argc, char *argv[], void *userdata) {
}
static int verb_remove(int argc, char *argv[], void *userdata) {
sd_id128_t uuid = SD_ID128_NULL, machine_id;
sd_id128_t uuid = SD_ID128_NULL;
int r, q;
r = acquire_esp(false, NULL, NULL, NULL, &uuid);
@ -1605,10 +1578,6 @@ static int verb_remove(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");
r = remove_binaries(arg_esp_path);
q = remove_file(arg_esp_path, "/loader/loader.conf");
@ -1627,19 +1596,11 @@ static int verb_remove(int argc, char *argv[], void *userdata) {
if (q < 0 && r >= 0)
r = q;
q = remove_machine_id_directory(arg_esp_path, machine_id);
if (q < 0 && r >= 0)
r = 1;
if (arg_xbootldr_path) {
/* Remove the latter two also in the XBOOTLDR partition if it exists */
q = remove_subdirs(arg_xbootldr_path, dollar_boot_subdirs);
if (q < 0 && r >= 0)
r = q;
q = remove_machine_id_directory(arg_xbootldr_path, machine_id);
if (q < 0 && r >= 0)
r = q;
}
(void) sync_everything();

View File

@ -87,6 +87,8 @@ KERNEL_IMAGE="$2"
if [[ -f /etc/machine-id ]]; then
read MACHINE_ID < /etc/machine-id
else
MACHINE_ID="Linux"
fi
if [[ ! $COMMAND ]] || [[ ! $KERNEL_VERSION ]]; then
@ -94,10 +96,7 @@ if [[ ! $COMMAND ]] || [[ ! $KERNEL_VERSION ]]; then
exit 1
fi
if ! [[ $MACHINE_ID ]]; then
ENTRY_DIR_ABS=$(mktemp -d /tmp/kernel-install.XXXXX) || exit 1
trap "rm -rf '$ENTRY_DIR_ABS'" EXIT INT QUIT PIPE
elif [[ -d /efi/loader/entries ]] || [[ -d /efi/$MACHINE_ID ]]; then
if [[ -d /efi/loader/entries ]] || [[ -d /efi/$MACHINE_ID ]]; then
ENTRY_DIR_ABS="/efi/$MACHINE_ID/$KERNEL_VERSION"
elif [[ -d /boot/loader/entries ]] || [[ -d /boot/$MACHINE_ID ]]; then
ENTRY_DIR_ABS="/boot/$MACHINE_ID/$KERNEL_VERSION"
@ -146,14 +145,6 @@ case $COMMAND in
((ret+=$x))
fi
done
if ! [[ $MACHINE_ID ]] && ! rmdir "$ENTRY_DIR_ABS"; then
echo "Warning: In kernel-install plugins, requiring ENTRY_DIR_ABS to be preset is deprecated." >&2
echo " All plugins should not put anything in ENTRY_DIR_ABS if the environment" >&2
echo " variable KERNEL_INSTALL_MACHINE_ID is empty." >&2
rm -rf "$ENTRY_DIR_ABS"
((ret+=$?))
fi
;;
remove)