Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mount-boot command. #251

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions data/clr-boot-manager-mount-boot.service.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Unit]
Description=mount @BOOTDIR@ using clr-boot-manager

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=@BINDIR@/clr-boot-manager mount-boot
ExecStop=@BINDIR@/umount @BOOTDIR@

[Install]
WantedBy=multi-user.target
8 changes: 8 additions & 0 deletions data/meson.build
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
# Write systemd unit
data_conf = configuration_data()
data_conf.set('BINDIR', path_bindir)
data_conf.set('BOOTDIR', with_boot_dir)

configure_file(
input: 'clr-boot-manager-booted.service.in',
output: 'clr-boot-manager-booted.service',
configuration: data_conf,
install_dir: with_systemd_system_unit_dir,
)

configure_file(
input: 'clr-boot-manager-mount-boot.service.in',
output: 'clr-boot-manager-mount-boot.service',
configuration: data_conf,
install_dir: with_systemd_system_unit_dir,
)
10 changes: 5 additions & 5 deletions src/bootman/bootman.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ bool boot_manager_set_prefix(BootManager *self, char *prefix)
free(self->user_initrd_freestanding_dir);
}
self->user_initrd_freestanding_dir = user_initrd_dir;

if (self->bootloader) {
self->bootloader->destroy(self);
self->bootloader = NULL;
Expand Down Expand Up @@ -331,7 +331,7 @@ bool boot_manager_remove_kernel_wrapper(BootManager *self, const Kernel *kernel)
CHECK_ERR_RET_VAL(!kernels || kernels->len == 0, false,
"No kernels discovered in %s, bailing", self->kernel_dir);

did_mount = detect_and_mount_boot(self, &boot_dir);
did_mount = boot_manager_detect_and_mount_boot(self, &boot_dir);
CHECK_DBG_RET_VAL(did_mount < 0, false, "Boot was not mounted");

for (uint16_t i = 0; i < kernels->len; i++) {
Expand Down Expand Up @@ -371,7 +371,7 @@ bool boot_manager_remove_kernel(BootManager *self, const Kernel *kernel)
return self->bootloader->remove_kernel(self, kernel);
}

int detect_and_mount_boot(BootManager *self, char **boot_dir) {
int boot_manager_detect_and_mount_boot(BootManager *self, char **boot_dir) {
autofree(char) *boot_dev = NULL;
const char *prefix;
int wanted_boot_mask;
Expand Down Expand Up @@ -412,7 +412,7 @@ bool boot_manager_set_default_kernel(BootManager *self, const Kernel *kernel)
CHECK_ERR_RET_VAL(!kernels || kernels->len == 0, false,
"No kernels discovered in %s, bailing", self->kernel_dir);

did_mount = detect_and_mount_boot(self, &boot_dir);
did_mount = boot_manager_detect_and_mount_boot(self, &boot_dir);
CHECK_DBG_RET_VAL(did_mount < 0, false, "Boot was not mounted");

for (uint16_t i = 0; i < kernels->len; i++) {
Expand Down Expand Up @@ -608,7 +608,7 @@ char **boot_manager_list_kernels(BootManager *self)
/* Sort them to ensure static ordering */
nc_array_qsort(kernels, kernel_compare_reverse);

did_mount = detect_and_mount_boot(self, &boot_dir);
did_mount = boot_manager_detect_and_mount_boot(self, &boot_dir);
if (did_mount >= 0) {
default_kernel = boot_manager_get_default_kernel(self);
if (did_mount > 0) {
Expand Down
7 changes: 7 additions & 0 deletions src/bootman/bootman.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,13 @@ bool boot_manager_set_default_kernel(BootManager *manager, const Kernel *kernel)
*/
char *boot_manager_get_default_kernel(BootManager *manager);

/**
* Detect and mount the boot directory.
* @param boot_dir Path indicating the mounted boot directory.
* @return an integer value, indicating success or failure.
*/
int boot_manager_detect_and_mount_boot(BootManager *self, char **boot_dir);

/**
* Return the CbmDeviceProbe for the root partition
*
Expand Down
2 changes: 1 addition & 1 deletion src/bootman/bootman_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ int mount_boot(BootManager *self, char **boot_directory);
*
* @see mount_boot() for return and error conditions.
*/
int detect_and_mount_boot(BootManager *self, char **boot_dir);
int boot_manager_detect_and_mount_boot(BootManager *self, char **boot_dir);

/**
* Internal function to sort by Kernel structs by release number (highest first)
Expand Down
2 changes: 1 addition & 1 deletion src/bootman/update.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ bool boot_manager_update(BootManager *self)
return boot_manager_update_image(self);
}

did_mount = detect_and_mount_boot(self, &boot_dir);
did_mount = boot_manager_detect_and_mount_boot(self, &boot_dir);
if (did_mount >= 0) {
/* Do a native update */
ret = boot_manager_update_native(self);
Expand Down
17 changes: 17 additions & 0 deletions src/cli/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "ops/timeout.h"
#include "ops/update.h"
#include "ops/kernels.h"
#include "ops/mount.h"

static SubCommand cmd_update;
static SubCommand cmd_help;
Expand All @@ -32,6 +33,7 @@ static SubCommand cmd_report_booted;
static SubCommand cmd_list_kernels;
static SubCommand cmd_set_kernel;
static SubCommand cmd_remove_kernel;
static SubCommand cmd_mount_boot;
static char *binary_name = NULL;
static NcHashmap *g_commands = NULL;
static bool explicit_help = false;
Expand Down Expand Up @@ -225,6 +227,21 @@ kernel for the next time the system boots.",
return EXIT_FAILURE;
}

/* Mount the boot directory */
cmd_mount_boot = (SubCommand){
.name = "mount-boot",
.blurb = "Mount the boot directory",
.help = "This command ensures the boot directory is mounted.",
.callback = cbm_command_mount_boot,
.usage = " [--path=/path/to/filesystem/root]",
.requires_root = true
};

if (!nc_hashmap_put(commands, cmd_mount_boot.name, &cmd_mount_boot)) {
DECLARE_OOM();
return EXIT_FAILURE;
}

/* Version */
cmd_version = (SubCommand){
.name = "version",
Expand Down
87 changes: 87 additions & 0 deletions src/cli/ops/mount.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* This file is part of clr-boot-manager.
*
* Copyright © 2016-2018 Intel Corporation
* Copyright © 2020 Silke Hofstra

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you can claim copyright on this file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this is entirely reasonable.

When you add a new file, or add code that contains substantial amounts of new code authored by your, adding an author/copyright line is very much okay.

Maybe the date should be changed to 2022, though. Unless it's actually old code that's been circulated before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the date should be changed to 2022, though. Unless it's actually old code that's been circulated before.

Looking at my original commit date, this is correct. It has been ready for a while, pending a go-ahead to start integrating fwupd for solus.

*
* clr-boot-manager is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*/

#define _GNU_SOURCE

#include <stdio.h>
#include <string.h>

#include "bootman.h"
#include "cli.h"
#include "log.h"

bool cbm_command_mount_boot(int argc, char **argv)
{
autofree(char) *root = NULL;
autofree(BootManager) *manager = NULL;
bool forced_image = false;
bool update_efi_vars = false;
autofree(char) *boot_dir = NULL;
int did_mount = -1;

if (!cli_default_args_init(&argc, &argv, &root, &forced_image, &update_efi_vars)) {
return false;
}

manager = boot_manager_new();
if (!manager) {
DECLARE_OOM();
return false;
}

boot_manager_set_update_efi_vars(manager, update_efi_vars);

if (root) {
autofree(char) *realp = NULL;

realp = realpath(root, NULL);
if (!realp) {
LOG_FATAL("Path specified does not exist: %s", root);
return false;
}
/* Anything not / is image mode */
if (!streq(realp, "/")) {
boot_manager_set_image_mode(manager, true);
} else {
boot_manager_set_image_mode(manager, forced_image);
}

/* CBM will check this again, we just needed to check for
* image mode.. */
if (!boot_manager_set_prefix(manager, root)) {
return false;
}
} else {
boot_manager_set_image_mode(manager, forced_image);
/* Default to "/", bail if it doesn't work. */
if (!boot_manager_set_prefix(manager, "/")) {
return false;
}
}

/* Let CBM detect and mount the boot directory */
did_mount = boot_manager_detect_and_mount_boot(manager, &boot_dir);
return did_mount >= 0;
}

/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 8
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=8 tabstop=8 expandtab:
* :indentSize=8:tabSize=8:noTabs=true:
*/
29 changes: 29 additions & 0 deletions src/cli/ops/mount.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* This file is part of clr-boot-manager.
*
* Copyright © 2020 Silke Hofstra
*
* clr-boot-manager is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*/

#pragma once

#include "cli.h"

bool cbm_command_mount_boot(int argc, char **argv);

/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 8
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=8 tabstop=8 expandtab:
* :indentSize=8:tabSize=8:noTabs=true:
*/
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ clr_boot_manager_sources = [
'cli/cli.c',
'cli/main.c',
'cli/ops/kernels.c',
'cli/ops/mount.c',
'cli/ops/report_booted.c',
'cli/ops/timeout.c',
'cli/ops/update.c',
Expand Down