Skip to content

Commit

Permalink
Add mount-boot command.
Browse files Browse the repository at this point in the history
The `mount-boot` command mounts the boot partitions.
This is useful for when the user, any system component,
requires the boot directory to be mounted.

As unmounting afterwards is a simple `umount`,
no `unmount-boot` command is added.
  • Loading branch information
silkeh committed Aug 21, 2022
1 parent 1cc4150 commit 3d18c97
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
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 @@ -31,6 +32,7 @@ static SubCommand cmd_get_timeout;
static SubCommand cmd_report_booted;
static SubCommand cmd_list_kernels;
static SubCommand cmd_set_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 @@ -209,6 +211,21 @@ kernel for the next time the system boots.",
return EXIT_FAILURE;
}

/* Set kernel to be booted */
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
*
* 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

0 comments on commit 3d18c97

Please sign in to comment.