Skip to content

Commit affae84

Browse files
committed
Add mount-boot command.
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.
1 parent 4f427fb commit affae84

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

src/cli/main.c

+17
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "ops/timeout.h"
2323
#include "ops/update.h"
2424
#include "ops/kernels.h"
25+
#include "ops/mount.h"
2526

2627
static SubCommand cmd_update;
2728
static SubCommand cmd_help;
@@ -32,6 +33,7 @@ static SubCommand cmd_report_booted;
3233
static SubCommand cmd_list_kernels;
3334
static SubCommand cmd_set_kernel;
3435
static SubCommand cmd_remove_kernel;
36+
static SubCommand cmd_mount_boot;
3537
static char *binary_name = NULL;
3638
static NcHashmap *g_commands = NULL;
3739
static bool explicit_help = false;
@@ -225,6 +227,21 @@ kernel for the next time the system boots.",
225227
return EXIT_FAILURE;
226228
}
227229

230+
/* Mount the boot directory */
231+
cmd_mount_boot = (SubCommand){
232+
.name = "mount-boot",
233+
.blurb = "Mount the boot directory",
234+
.help = "This command ensures the boot directory is mounted.",
235+
.callback = cbm_command_mount_boot,
236+
.usage = " [--path=/path/to/filesystem/root]",
237+
.requires_root = true
238+
};
239+
240+
if (!nc_hashmap_put(commands, cmd_mount_boot.name, &cmd_mount_boot)) {
241+
DECLARE_OOM();
242+
return EXIT_FAILURE;
243+
}
244+
228245
/* Version */
229246
cmd_version = (SubCommand){
230247
.name = "version",

src/cli/ops/mount.c

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* This file is part of clr-boot-manager.
3+
*
4+
* Copyright © 2016-2018 Intel Corporation
5+
* Copyright © 2020 Silke Hofstra
6+
*
7+
* clr-boot-manager is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation; either version 2.1
10+
* of the License, or (at your option) any later version.
11+
*/
12+
13+
#define _GNU_SOURCE
14+
15+
#include <stdio.h>
16+
#include <string.h>
17+
18+
#include "bootman.h"
19+
#include "cli.h"
20+
#include "log.h"
21+
22+
bool cbm_command_mount_boot(int argc, char **argv)
23+
{
24+
autofree(char) *root = NULL;
25+
autofree(BootManager) *manager = NULL;
26+
bool forced_image = false;
27+
bool update_efi_vars = false;
28+
autofree(char) *boot_dir = NULL;
29+
int did_mount = -1;
30+
31+
if (!cli_default_args_init(&argc, &argv, &root, &forced_image, &update_efi_vars)) {
32+
return false;
33+
}
34+
35+
manager = boot_manager_new();
36+
if (!manager) {
37+
DECLARE_OOM();
38+
return false;
39+
}
40+
41+
boot_manager_set_update_efi_vars(manager, update_efi_vars);
42+
43+
if (root) {
44+
autofree(char) *realp = NULL;
45+
46+
realp = realpath(root, NULL);
47+
if (!realp) {
48+
LOG_FATAL("Path specified does not exist: %s", root);
49+
return false;
50+
}
51+
/* Anything not / is image mode */
52+
if (!streq(realp, "/")) {
53+
boot_manager_set_image_mode(manager, true);
54+
} else {
55+
boot_manager_set_image_mode(manager, forced_image);
56+
}
57+
58+
/* CBM will check this again, we just needed to check for
59+
* image mode.. */
60+
if (!boot_manager_set_prefix(manager, root)) {
61+
return false;
62+
}
63+
} else {
64+
boot_manager_set_image_mode(manager, forced_image);
65+
/* Default to "/", bail if it doesn't work. */
66+
if (!boot_manager_set_prefix(manager, "/")) {
67+
return false;
68+
}
69+
}
70+
71+
/* Let CBM detect and mount the boot directory */
72+
did_mount = boot_manager_detect_and_mount_boot(manager, &boot_dir);
73+
return did_mount >= 0;
74+
}
75+
76+
/*
77+
* Editor modelines - https://www.wireshark.org/tools/modelines.html
78+
*
79+
* Local variables:
80+
* c-basic-offset: 8
81+
* tab-width: 8
82+
* indent-tabs-mode: nil
83+
* End:
84+
*
85+
* vi: set shiftwidth=8 tabstop=8 expandtab:
86+
* :indentSize=8:tabSize=8:noTabs=true:
87+
*/

src/cli/ops/mount.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* This file is part of clr-boot-manager.
3+
*
4+
* Copyright © 2020 Silke Hofstra
5+
*
6+
* clr-boot-manager is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public License as
8+
* published by the Free Software Foundation; either version 2.1
9+
* of the License, or (at your option) any later version.
10+
*/
11+
12+
#pragma once
13+
14+
#include "cli.h"
15+
16+
bool cbm_command_mount_boot(int argc, char **argv);
17+
18+
/*
19+
* Editor modelines - https://www.wireshark.org/tools/modelines.html
20+
*
21+
* Local variables:
22+
* c-basic-offset: 8
23+
* tab-width: 8
24+
* indent-tabs-mode: nil
25+
* End:
26+
*
27+
* vi: set shiftwidth=8 tabstop=8 expandtab:
28+
* :indentSize=8:tabSize=8:noTabs=true:
29+
*/

src/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ clr_boot_manager_sources = [
9898
'cli/cli.c',
9999
'cli/main.c',
100100
'cli/ops/kernels.c',
101+
'cli/ops/mount.c',
101102
'cli/ops/report_booted.c',
102103
'cli/ops/timeout.c',
103104
'cli/ops/update.c',

0 commit comments

Comments
 (0)