• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 24697298032

20 Apr 2026 09:22PM UTC coverage: 72.23% (+1.6%) from 70.661%
24697298032

push

github

bluca
sysupdate: Emit READY=1 status when installing

`READY=1` is already correctly emitted when acquiring an update, but was
forgotten to be emitted when subsequently installing that update.

That meant that the state tracking code in `sysupdated` and hence
`updatectl` could not properly report the progress of the install
operation, and hence printed “Already up-to-date” after a successful
update installation, rather than “Done”.

Add a test to catch this in future.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Fixes: https://github.com/systemd/systemd/issues/41502

322667 of 446720 relevant lines covered (72.23%)

1191594.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/kernel-install/kernel-install.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <getopt.h>
4
#include <stdlib.h>
5
#include <sys/utsname.h>
6
#include <unistd.h>
7

8
#include "argv-util.h"
9
#include "boot-entry.h"
10
#include "bootspec.h"
11
#include "build.h"
12
#include "chase.h"
13
#include "conf-files.h"
14
#include "dirent-util.h"
15
#include "dissect-image.h"
16
#include "env-file.h"
17
#include "env-util.h"
18
#include "exec-util.h"
19
#include "extract-word.h"
20
#include "fd-util.h"
21
#include "fileio.h"
22
#include "find-esp.h"
23
#include "format-table.h"
24
#include "fs-util.h"
25
#include "id128-util.h"
26
#include "image-policy.h"
27
#include "kernel-config.h"
28
#include "kernel-image.h"
29
#include "loop-util.h"
30
#include "main-func.h"
31
#include "mount-util.h"
32
#include "parse-argument.h"
33
#include "path-util.h"
34
#include "pretty-print.h"
35
#include "recurse-dir.h"
36
#include "rm-rf.h"
37
#include "stat-util.h"
38
#include "string-table.h"
39
#include "string-util.h"
40
#include "strv.h"
41
#include "tmpfile-util.h"
42
#include "verbs.h"
43

44
static bool arg_verbose = false;
45
static char *arg_esp_path = NULL;
46
static char *arg_xbootldr_path = NULL;
47
static int arg_make_entry_directory = -1; /* tristate */
48
static PagerFlags arg_pager_flags = 0;
49
static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
50
static char *arg_root = NULL;
51
static char *arg_image = NULL;
52
static ImagePolicy *arg_image_policy = NULL;
53
static bool arg_legend = true;
54
static BootEntryTokenType arg_entry_token_type = BOOT_ENTRY_TOKEN_AUTO;
55
static char *arg_entry_token = NULL;
56
static BootEntryType arg_boot_entry_type = _BOOT_ENTRY_TYPE_INVALID;
57

58
STATIC_DESTRUCTOR_REGISTER(arg_esp_path, freep);
×
59
STATIC_DESTRUCTOR_REGISTER(arg_xbootldr_path, freep);
×
60
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
×
61
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
×
62
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
×
63
STATIC_DESTRUCTOR_REGISTER(arg_entry_token, freep);
×
64

65
typedef enum Action {
66
        ACTION_ADD,
67
        ACTION_REMOVE,
68
        ACTION_INSPECT,
69
        _ACTION_MAX,
70
        _ACTION_INVALID = -EINVAL,
71
} Action;
72

73
typedef enum Layout {
74
        LAYOUT_AUTO,
75
        LAYOUT_UKI,
76
        LAYOUT_BLS,
77
        LAYOUT_OTHER,
78
        _LAYOUT_MAX,
79
        _LAYOUT_INVALID = -EINVAL,
80
} Layout;
81

82
static const char * const layout_table[_LAYOUT_MAX] = {
83
        [LAYOUT_AUTO]  = "auto",
84
        [LAYOUT_UKI]   = "uki",
85
        [LAYOUT_BLS]   = "bls",
86
        [LAYOUT_OTHER] = "other",
87
};
88

89
DEFINE_PRIVATE_STRING_TABLE_LOOKUP(layout, Layout);
×
90

91
typedef struct Context {
92
        int rfd;
93
        Action action;
94
        sd_id128_t machine_id;
95
        bool machine_id_is_random;
96
        BootEntryType entry_type;
97
        KernelImageType kernel_image_type;
98
        Layout layout;
99
        char *layout_other;
100
        char *conf_root;
101
        char *boot_root;
102
        BootEntryTokenType entry_token_type;
103
        char *entry_token;
104
        char *entry_dir;
105
        char *version;
106
        char *kernel;
107
        char **initrds;
108
        char *initrd_generator;
109
        char *uki_generator;
110
        char *staging_area;
111
        char **plugins;
112
        char **argv;
113
        char **envp;
114
} Context;
115

116
#define CONTEXT_NULL                                                    \
117
        (Context) {                                                     \
118
                .rfd = XAT_FDROOT,                                      \
119
                .action = _ACTION_INVALID,                              \
120
                .kernel_image_type = _KERNEL_IMAGE_TYPE_INVALID,        \
121
                .layout = _LAYOUT_INVALID,                              \
122
                .entry_type = _BOOT_ENTRY_TYPE_INVALID,                 \
123
                .entry_token_type = _BOOT_ENTRY_TOKEN_TYPE_INVALID,     \
124
        }
125

126
static void context_done(Context *c) {
×
127
        assert(c);
×
128

129
        free(c->layout_other);
×
130
        free(c->conf_root);
×
131
        free(c->boot_root);
×
132
        free(c->entry_token);
×
133
        free(c->entry_dir);
×
134
        free(c->version);
×
135
        free(c->kernel);
×
136
        strv_free(c->initrds);
×
137
        free(c->initrd_generator);
×
138
        free(c->uki_generator);
×
139
        if (c->action == ACTION_INSPECT)
×
140
                free(c->staging_area);
×
141
        else
142
                rm_rf_physical_and_free(c->staging_area);
×
143
        strv_free(c->plugins);
×
144
        strv_free(c->argv);
×
145
        strv_free(c->envp);
×
146

147
        safe_close(c->rfd);
×
148
}
×
149

150
static int context_copy(const Context *source, Context *ret) {
×
151
        int r;
×
152

153
        assert(source);
×
154
        assert(ret);
×
155
        assert(source->rfd >= 0 || source->rfd == AT_FDCWD || source->rfd == XAT_FDROOT);
×
156

157
        _cleanup_(context_done) Context copy = (Context) {
×
158
                .rfd = source->rfd,
×
159
                .action = source->action,
×
160
                .machine_id = source->machine_id,
161
                .machine_id_is_random = source->machine_id_is_random,
×
162
                .kernel_image_type = source->kernel_image_type,
×
163
                .layout = source->layout,
×
164
                .entry_token_type = source->entry_token_type,
×
165
        };
166

167
        if (source->rfd >= 0) {
×
168
                copy.rfd = fd_reopen(source->rfd, O_CLOEXEC|O_DIRECTORY|O_PATH);
×
169
                if (copy.rfd < 0)
×
170
                        return copy.rfd;
171
        }
172

173
        r = strdup_to(&copy.layout_other, source->layout_other);
×
174
        if (r < 0)
×
175
                return r;
176
        r = strdup_to(&copy.conf_root, source->conf_root);
×
177
        if (r < 0)
×
178
                return r;
179
        r = strdup_to(&copy.boot_root, source->boot_root);
×
180
        if (r < 0)
×
181
                return r;
182
        r = strdup_to(&copy.entry_token, source->entry_token);
×
183
        if (r < 0)
×
184
                return r;
185
        r = strdup_to(&copy.entry_dir, source->entry_dir);
×
186
        if (r < 0)
×
187
                return r;
188
        r = strdup_to(&copy.version, source->version);
×
189
        if (r < 0)
×
190
                return r;
191
        r = strdup_to(&copy.kernel, source->kernel);
×
192
        if (r < 0)
×
193
                return r;
194
        r = strv_copy_unless_empty(source->initrds, &copy.initrds);
×
195
        if (r < 0)
×
196
                return r;
197
        r = strdup_to(&copy.initrd_generator, source->initrd_generator);
×
198
        if (r < 0)
×
199
                return r;
200
        r = strdup_to(&copy.uki_generator, source->uki_generator);
×
201
        if (r < 0)
×
202
                return r;
203
        r = strdup_to(&copy.staging_area, source->staging_area);
×
204
        if (r < 0)
×
205
                return r;
206
        r = strv_copy_unless_empty(source->plugins, &copy.plugins);
×
207
        if (r < 0)
×
208
                return r;
209
        r = strv_copy_unless_empty(source->argv, &copy.argv);
×
210
        if (r < 0)
×
211
                return r;
212
        r = strv_copy_unless_empty(source->envp, &copy.envp);
×
213
        if (r < 0)
×
214
                return r;
215

216
        *ret = copy;
×
217
        copy = CONTEXT_NULL;
×
218

219
        return 0;
×
220
}
221

222
static int context_open_root(Context *c) {
×
223
        int r;
×
224

225
        assert(c);
×
226
        assert(c->rfd < 0);
×
227

228
        if (isempty(arg_root))
×
229
                return 0;
230

231
        r = path_is_root(arg_root);
×
232
        if (r < 0)
×
233
                return log_error_errno(r, "Failed to determine if '%s' is the root directory: %m", arg_root);
×
234
        if (r > 0)
×
235
                return 0;
236

237
        c->rfd = open(arg_root, O_CLOEXEC | O_DIRECTORY | O_PATH);
×
238
        if (c->rfd < 0)
×
239
                return log_error_errno(errno, "Failed to open root directory '%s': %m", arg_root);
×
240

241
        return 0;
242
}
243

244
static const char* context_get_layout(const Context *c) {
×
245
        assert(c);
×
246
        assert(c->layout >= 0);
×
247

248
        return c->layout_other ?: layout_to_string(c->layout);
×
249
}
250

251
static int context_set_layout(Context *c, const char *s, const char *source) {
×
252
        Layout t;
×
253

254
        assert(c);
×
255
        assert(source);
×
256

257
        if (c->layout >= 0 || !s)
×
258
                return 0;
259

260
        assert(!c->layout_other);
×
261

262
        t = layout_from_string(s);
×
263
        if (t >= 0)
×
264
                c->layout = t;
×
265
        else if (isempty(s))
×
266
                c->layout = LAYOUT_AUTO;
×
267
        else {
268
                c->layout_other = strdup(s);
×
269
                if (!c->layout_other)
×
270
                        return log_oom();
×
271

272
                c->layout = LAYOUT_OTHER;
×
273
        }
274

275
        log_debug("layout=%s set via %s", context_get_layout(c), source);
×
276
        return 1;
277
}
278

279
static int context_set_machine_id(Context *c, const char *s, const char *source) {
×
280
        int r;
×
281

282
        assert(c);
×
283
        assert(source);
×
284

285
        if (!sd_id128_is_null(c->machine_id) || !s)
×
286
                return 0;
×
287

288
        r = sd_id128_from_string(s, &c->machine_id);
×
289
        if (r < 0)
×
290
                return log_warning_errno(r, "Failed to parse machine ID specified via %s, ignoring.", source);
×
291

292
        if (sd_id128_is_null(c->machine_id))
×
293
                return 0;
×
294

295
        log_debug("MACHINE_ID=%s set via %s.", SD_ID128_TO_STRING(c->machine_id), source);
×
296
        return 1;
×
297
}
298

299
static int context_set_string(const char *s, const char *source, const char *name, char **dest) {
×
300
        char *p;
×
301

302
        assert(source);
×
303
        assert(name);
×
304
        assert(dest);
×
305

306
        if (*dest || !s)
×
307
                return 0;
308

309
        p = strdup(s);
×
310
        if (!p)
×
311
                return log_oom();
×
312

313
        log_debug("%s (%s) set via %s.", name, p, source);
×
314

315
        *dest = p;
×
316
        return 1;
×
317
}
318

319
static int context_set_initrd_generator(Context *c, const char *s, const char *source) {
×
320
        assert(c);
×
321
        return context_set_string(s, source, "INITRD_GENERATOR", &c->initrd_generator);
×
322
}
323

324
static int context_set_uki_generator(Context *c, const char *s, const char *source) {
×
325
        assert(c);
×
326
        return context_set_string(s, source, "UKI_GENERATOR", &c->uki_generator);
×
327
}
328

329
static int context_set_version(Context *c, const char *s) {
×
330
        assert(c);
×
331

332
        if (s && !filename_is_valid(s))
×
333
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid version specified: %s", s);
×
334

335
        return context_set_string(s, "command line", "kernel version", &c->version);
×
336
}
337

338
static int context_set_path(Context *c, const char *s, const char *source, const char *name, char **dest) {
×
339
        char *p;
×
340
        int r;
×
341

342
        assert(c);
×
343
        assert(source);
×
344
        assert(name);
×
345
        assert(dest);
×
346

347
        if (*dest || !s)
×
348
                return 0;
×
349

350
        if (c->rfd >= 0) {
×
351
                r = chaseat(c->rfd, s, CHASE_AT_RESOLVE_IN_ROOT, &p, /* ret_fd= */ NULL);
×
352
                if (r < 0)
×
353
                        return log_warning_errno(r, "Failed to chase path %s for %s specified via %s, ignoring: %m",
×
354
                                                 s, name, source);
355
        } else {
356
                r = path_make_absolute_cwd(s, &p);
×
357
                if (r < 0)
×
358
                        return log_warning_errno(r, "Failed to make path '%s' for %s specified via %s absolute, ignoring: %m",
×
359
                                                 s, name, source);
360
        }
361

362
        log_debug("%s (%s) set via %s.", name, p, source);
×
363

364
        *dest = p;
×
365
        return 1;
×
366
}
367

368
static int context_set_boot_root(Context *c, const char *s, const char *source) {
×
369
        assert(c);
×
370
        return context_set_path(c, s, source, "BOOT_ROOT", &c->boot_root);
×
371
}
372

373
static int context_set_conf_root(Context *c, const char *s, const char *source) {
×
374
        assert(c);
×
375
        return context_set_path(c, s, source, "CONF_ROOT", &c->conf_root);
×
376
}
377

378
static int context_set_kernel(Context *c, const char *s) {
×
379
        assert(c);
×
380
        return context_set_path(c, s, "command line", "kernel image file", &c->kernel);
×
381
}
382

383
static int context_set_path_strv(Context *c, char* const* strv, const char *source, const char *name, char ***dest) {
×
384
        _cleanup_strv_free_ char **w = NULL;
×
385
        int r;
×
386

387
        assert(c);
×
388
        assert(source);
×
389
        assert(name);
×
390
        assert(dest);
×
391

392
        if (*dest)
×
393
                return 0;
394

395
        STRV_FOREACH(s, strv) {
×
396
                char *p;
×
397

398
                if (c->rfd >= 0) {
×
399
                        r = chaseat(c->rfd, *s, CHASE_AT_RESOLVE_IN_ROOT, &p, /* ret_fd= */ NULL);
×
400
                        if (r < 0)
×
401
                                return log_warning_errno(r, "Failed to chase path %s for %s specified via %s: %m",
×
402
                                                         *s, name, source);
403
                } else {
404
                        r = path_make_absolute_cwd(*s, &p);
×
405
                        if (r < 0)
×
406
                                return log_warning_errno(r, "Failed to make path '%s' for %s specified via %s absolute, ignoring: %m",
×
407
                                                         *s, name, source);
408
                }
409
                r = strv_consume(&w, p);
×
410
                if (r < 0)
×
411
                        return log_oom();
×
412
        }
413

414
        if (strv_isempty(w))
×
415
                return 0;
416

417
        log_debug("%s set via %s", name, source);
×
418

419
        *dest = TAKE_PTR(w);
×
420
        return 1;
×
421
}
422

423
static int context_set_plugins(Context *c, const char *s, const char *source) {
×
424
        _cleanup_strv_free_ char **v = NULL;
×
425
        int r;
×
426

427
        assert(c);
×
428

429
        if (c->plugins || !s)
×
430
                return 0;
431

432
        r = strv_split_full(&v, s, NULL, EXTRACT_UNQUOTE);
×
433
        if (r < 0)
×
434
                return log_error_errno(r, "Failed to parse plugin paths from %s: %m", source);
×
435

436
        return context_set_path_strv(c, v, source, "plugins", &c->plugins);
×
437
}
438

439
static int context_set_initrds(Context *c, char* const* strv) {
×
440
        assert(c);
×
441
        return context_set_path_strv(c, strv, "command line", "initrds", &c->initrds);
×
442
}
443

444
static int context_load_environment(Context *c) {
×
445
        assert(c);
×
446

447
        (void) context_set_machine_id(c, getenv("MACHINE_ID"), "environment");
×
448
        (void) context_set_boot_root(c, getenv("BOOT_ROOT"), "environment");
×
449
        (void) context_set_conf_root(c, getenv("KERNEL_INSTALL_CONF_ROOT"), "environment");
×
450
        (void) context_set_plugins(c, getenv("KERNEL_INSTALL_PLUGINS"), "environment");
×
451
        return 0;
×
452
}
453

454
static int context_load_install_conf(Context *c) {
×
455
        _cleanup_free_ char *machine_id = NULL, *boot_root = NULL, *layout = NULL,
×
456
                            *initrd_generator = NULL, *uki_generator = NULL;
×
457
        int r;
×
458

459
        assert(c);
×
460

461
        r = load_kernel_install_conf_at(
×
462
                        c->conf_root ? NULL : arg_root,
463
                        c->conf_root ? XAT_FDROOT : c->rfd,
464
                        c->conf_root,
×
465
                        &machine_id,
466
                        &boot_root,
467
                        &layout,
468
                        &initrd_generator,
469
                        &uki_generator);
470
        if (r <= 0)
×
471
                return r;
472

473
        (void) context_set_machine_id(c, machine_id, "config");
×
474
        (void) context_set_boot_root(c, boot_root, "config");
×
475
        (void) context_set_layout(c, layout, "config");
×
476
        (void) context_set_initrd_generator(c, initrd_generator, "config");
×
477
        (void) context_set_uki_generator(c, uki_generator, "config");
×
478

479
        log_debug("Loaded config.");
×
480
        return 0;
481
}
482

483
static int context_load_machine_info(Context *c) {
×
484
        _cleanup_fclose_ FILE *f = NULL;
×
485
        _cleanup_free_ char *machine_id = NULL, *layout = NULL;
×
486
        static const char *path = "/etc/machine-info";
×
487
        int r;
×
488

489
        assert(c);
×
490

491
        /* If the user configured an explicit machine ID in /etc/machine-info to use for our purpose, we'll
492
         * use that instead (for compatibility). */
493

494
        if (!sd_id128_is_null(c->machine_id) && c->layout >= 0)
×
495
                return 0;
496

497
        /* For testing. To make not read host's /etc/machine-info. */
498
        r = getenv_bool("KERNEL_INSTALL_READ_MACHINE_INFO");
×
499
        if (r < 0 && r != -ENXIO)
×
500
                log_warning_errno(r, "Failed to read $KERNEL_INSTALL_READ_MACHINE_INFO, assuming yes: %m");
×
501
        if (r == 0) {
×
502
                log_debug("Skipping reading of /etc/machine-info.");
×
503
                return 0;
×
504
        }
505

506
        r = chase_and_fopenat_unlocked(c->rfd, path, CHASE_AT_RESOLVE_IN_ROOT, "re", NULL, &f);
×
507
        if (r == -ENOENT)
×
508
                return 0;
509
        if (r < 0)
×
510
                return log_error_errno(r, "Failed to chase %s: %m", path);
×
511

512
        log_debug("Loading %s…", path);
×
513

514
        r = parse_env_file(f, path,
×
515
                           "KERNEL_INSTALL_MACHINE_ID", &machine_id,
516
                           "KERNEL_INSTALL_LAYOUT", &layout);
517
        if (r < 0)
×
518
                return log_error_errno(r, "Failed to parse '%s': %m", path);
×
519

520
        (void) context_set_machine_id(c, machine_id, path);
×
521
        (void) context_set_layout(c, layout, path);
×
522
        return 0;
523
}
524

525
static int context_load_machine_id(Context *c) {
×
526
        int r;
×
527

528
        assert(c);
×
529

530
        r = id128_get_machine_at(c->rfd, &c->machine_id);
×
531
        if (ERRNO_IS_NEG_MACHINE_ID_UNSET(r))
×
532
                return 0;
533
        if (r < 0)
×
534
                return log_error_errno(r, "Failed to load machine ID from /etc/machine-id: %m");
×
535

536
        log_debug("MACHINE_ID=%s set via /etc/machine-id.", SD_ID128_TO_STRING(c->machine_id));
×
537
        return 1; /* loaded */
×
538
}
539

540
static int context_ensure_machine_id(Context *c) {
×
541
        int r;
×
542

543
        assert(c);
×
544

545
        if (!sd_id128_is_null(c->machine_id))
×
546
                return 0;
×
547

548
        /* If /etc/machine-id is initialized we'll use it. */
549
        r = context_load_machine_id(c);
×
550
        if (r != 0)
×
551
                return r;
552

553
        /* Otherwise we'll use a freshly generated one. */
554
        r = sd_id128_randomize(&c->machine_id);
×
555
        if (r < 0)
×
556
                return log_error_errno(r, "Failed to generate random ID: %m");
×
557

558
        c->machine_id_is_random = true;
×
559
        log_debug("New machine ID '%s' generated.", SD_ID128_TO_STRING(c->machine_id));
×
560
        return 0;
×
561
}
562

563
static int context_acquire_xbootldr(Context *c) {
×
564
        int r;
×
565

566
        assert(c);
×
567
        assert(!c->boot_root);
×
568

569
        r = find_xbootldr_and_warn_at(
×
570
                        /* rfd= */ c->rfd,
571
                        /* path= */ arg_xbootldr_path,
572
                        /* unprivileged_mode= */ -1,
573
                        /* ret_path= */ &c->boot_root,
574
                        /* ret_fd= */ NULL);
575
        if (r == -ENOKEY) {
×
576
                log_debug_errno(r, "Couldn't find an XBOOTLDR partition.");
×
577
                return 0;
×
578
        }
579
        if (r == -EACCES && geteuid() != 0)
×
580
                return log_error_errno(r, "Failed to determine XBOOTLDR partition: %m");
×
581
        if (r < 0)
×
582
                return r;
583

584
        log_debug("Using XBOOTLDR partition at %s as $BOOT_ROOT.", c->boot_root);
×
585
        return 1; /* found */
586
}
587

588
static int context_acquire_esp(Context *c) {
×
589
        int r;
×
590

591
        assert(c);
×
592
        assert(!c->boot_root);
×
593

594
        r = find_esp_and_warn_at(
×
595
                        /* rfd= */ c->rfd,
596
                        /* path= */ arg_esp_path,
597
                        /* unprivileged_mode= */ -1,
598
                        /* ret_path= */ &c->boot_root,
599
                        /* ret_fd= */ NULL);
600
        if (r == -ENOKEY) {
×
601
                log_debug_errno(r, "Couldn't find EFI system partition, ignoring.");
×
602
                return 0;
×
603
        }
604
        if (r == -EACCES && geteuid() != 0)
×
605
                return log_error_errno(r, "Failed to determine EFI system partition: %m");
×
606
        if (r < 0)
×
607
                return r;
608

609
        log_debug("Using EFI System Partition at %s as $BOOT_ROOT.", c->boot_root);
×
610
        return 1; /* found */
611
}
612

613
static int context_ensure_boot_root(Context *c) {
×
614
        int r;
×
615

616
        assert(c);
×
617

618
        /* If BOOT_ROOT is specified via environment or install.conf, then use it. */
619
        if (c->boot_root)
×
620
                return 0;
621

622
        /* Otherwise, use XBOOTLDR partition, if mounted. */
623
        r = context_acquire_xbootldr(c);
×
624
        if (r != 0)
×
625
                return r;
626

627
        /* Otherwise, use EFI system partition, if mounted. */
628
        r = context_acquire_esp(c);
×
629
        if (r != 0)
×
630
                return r;
631

632
        /* If all else fails, use /boot. */
633
        if (c->rfd >= 0) {
×
634
                r = chaseat(c->rfd, "/boot", CHASE_AT_RESOLVE_IN_ROOT, &c->boot_root, /* ret_fd= */ NULL);
×
635
                if (r < 0)
×
636
                        return log_error_errno(r, "Failed to chase '/boot/': %m");
×
637
        } else {
638
                c->boot_root = strdup("/boot");
×
639
                if (!c->boot_root)
×
640
                        return log_oom();
×
641
        }
642

643
        log_debug("KERNEL_INSTALL_BOOT_ROOT autodetection yielded no candidates, using \"%s\".", c->boot_root);
×
644
        return 0;
645
}
646

647
static int context_ensure_entry_token(Context *c) {
×
648
        int r;
×
649

650
        assert(c);
×
651

652
        /* Now that we determined the machine ID to use, let's determine the "token" for the boot loader
653
         * entry to generate. We use that for naming the directory below $BOOT where we want to place the
654
         * kernel/initrd and related resources, as well for naming the .conf boot loader spec entry.
655
         * Typically this is just the machine ID, but it can be anything else, too, if we are told so. */
656

657
        r = boot_entry_token_ensure_at(
×
658
                        c->rfd,
659
                        c->conf_root,
×
660
                        c->machine_id,
661
                        c->machine_id_is_random,
×
662
                        &c->entry_token_type,
663
                        &c->entry_token);
664
        if (r < 0)
×
665
                return r;
666

667
        log_debug("Using entry token: %s", c->entry_token);
×
668
        return 0;
669
}
670

671
static int context_load_plugins(Context *c) {
×
672
        int r;
×
673

674
        assert(c);
×
675

676
        if (c->plugins)
×
677
                return 0;
×
678

679
        r = conf_files_list_strv_at(
×
680
                        &c->plugins,
681
                        ".install",
682
                        c->rfd,
683
                        CONF_FILES_EXECUTABLE | CONF_FILES_REGULAR | CONF_FILES_FILTER_MASKED | CONF_FILES_WARN,
684
                        STRV_MAKE_CONST("/etc/kernel/install.d", "/usr/lib/kernel/install.d"));
×
685
        if (r < 0)
×
686
                return log_error_errno(r, "Failed to find plugins: %m");
×
687

688
        return 0;
689
}
690

691
static int context_setup(Context *c) {
×
692
        int r;
×
693

694
        assert(c);
×
695

696
        if (c->kernel_image_type < 0)
×
697
                c->kernel_image_type = KERNEL_IMAGE_TYPE_UNKNOWN;
×
698
        if (c->entry_token_type < 0)
×
699
                c->entry_token_type = BOOT_ENTRY_TOKEN_AUTO;
×
700

701
        r = context_open_root(c);
×
702
        if (r < 0)
×
703
                return r;
704

705
        r = context_load_environment(c);
×
706
        if (r < 0)
×
707
                return r;
708

709
        r = context_load_install_conf(c);
×
710
        if (r < 0)
×
711
                return r;
712

713
        r = context_load_machine_info(c);
×
714
        if (r < 0)
×
715
                return r;
716

717
        r = context_ensure_machine_id(c);
×
718
        if (r < 0)
×
719
                return r;
720

721
        r = context_ensure_boot_root(c);
×
722
        if (r < 0)
×
723
                return r;
724

725
        r = context_ensure_entry_token(c);
×
726
        if (r < 0)
×
727
                return r;
728

729
        r = context_load_plugins(c);
×
730
        if (r < 0)
×
731
                return r;
×
732

733
        return 0;
734
}
735

736
static int context_from_cmdline(Context *c, Action action) {
×
737
        int r;
×
738

739
        assert(c);
×
740

741
        c->action = action;
×
742

743
        c->entry_type = arg_boot_entry_type;
×
744

745
        r = free_and_strdup_warn(&c->entry_token, arg_entry_token);
×
746
        if (r < 0)
×
747
                return r;
748

749
        c->entry_token_type = arg_entry_token_type;
×
750

751
        return context_setup(c);
×
752
}
753

754
static int context_inspect_kernel(Context *c) {
×
755
        int r;
×
756

757
        assert(c);
×
758

759
        if (!c->kernel)
×
760
                return 0;
761

762
        r = inspect_kernel(
×
763
                        c->rfd,
764
                        c->kernel,
765
                        &c->kernel_image_type);
766
        if (r < 0)
×
767
                return log_error_errno(r, "Failed to inspect kernel image '%s': %m", c->kernel);
×
768

769
        return 0;
770
}
771

772
static int context_ensure_layout(Context *c) {
×
773
        int r;
×
774

775
        assert(c);
×
776
        assert(c->boot_root);
×
777
        assert(c->entry_token);
×
778

779
        if (c->layout >= 0 && c->layout != LAYOUT_AUTO)
×
780
                return 0;
×
781

782
        /* No layout configured by the administrator. Let's try to figure it out automatically from metadata
783
         * already contained in $BOOT_ROOT. */
784

785
        if (c->kernel_image_type == KERNEL_IMAGE_TYPE_UKI) {
×
786
                c->layout = LAYOUT_UKI;
×
787
                log_debug("Kernel image type is %s, using layout=%s.",
×
788
                          kernel_image_type_to_string(c->kernel_image_type), layout_to_string(c->layout));
789
                return 0;
×
790
        }
791

792
        _cleanup_free_ char *srel_path = path_join(c->boot_root, "loader/entries.srel");
×
793
        if (!srel_path)
×
794
                return log_oom();
×
795

796
        _cleanup_fclose_ FILE *f = NULL;
×
797
        r = chase_and_fopenat_unlocked(c->rfd, srel_path, CHASE_AT_RESOLVE_IN_ROOT|CHASE_MUST_BE_REGULAR, "re", /* ret_path= */ NULL, &f);
×
798
        if (r < 0) {
×
799
                if (r != -ENOENT)
×
800
                        return log_error_errno(r, "Failed to open '%s': %m", srel_path);
×
801
        } else {
802
                _cleanup_free_ char *srel = NULL;
×
803

804
                r = read_line(f, LONG_LINE_MAX, &srel);
×
805
                if (r < 0)
×
806
                        return log_error_errno(r, "Failed to read %s: %m", srel_path);
×
807

808
                if (streq(srel, "type1"))
×
809
                        /* The loader/entries.srel file clearly indicates that the installed boot loader
810
                         * implements the proper standard upstream boot loader spec for Type #1 entries.
811
                         * Let's default to that, then. */
812
                        c->layout = LAYOUT_BLS;
×
813
                else
814
                        /* The loader/entries.srel file indicates some other spec is implemented and owns the
815
                         * /loader/entries/ directory. Since we have no idea what that means, let's stay away
816
                         * from it by default. */
817
                        c->layout = LAYOUT_OTHER;
×
818

819
                log_debug("%s with '%s' found, using layout=%s.", srel_path, srel, layout_to_string(c->layout));
×
820
                return 0;
×
821
        }
822

823
        _cleanup_free_ char *entry_token_path = path_join(c->boot_root, c->entry_token);
×
824
        if (!entry_token_path)
×
825
                return log_oom();
×
826

827
        r = chaseat(c->rfd, entry_token_path, CHASE_AT_RESOLVE_IN_ROOT|CHASE_MUST_BE_DIRECTORY, /* ret_path= */ NULL, /* ret_fd= */ NULL);
×
828
        if (r < 0) {
×
829
                if (!IN_SET(r, -ENOENT, -ENOTDIR))
×
830
                        return log_error_errno(r, "Failed to check if '%s' exists and is a directory: %m", entry_token_path);
×
831
        } else {
832
                /* If the metadata in $BOOT_ROOT doesn't tell us anything, then check if the entry token
833
                 * directory already exists. If so, let's assume it's the standard boot loader spec, too. */
834
                c->layout = LAYOUT_BLS;
×
835
                log_debug("%s exists, using layout=%s.", entry_token_path, layout_to_string(c->layout));
×
836
                return 0;
×
837
        }
838

839
        /* There's no metadata in $BOOT_ROOT, and apparently no entry token directory installed? Then we
840
         * really don't know anything. */
841
        c->layout = LAYOUT_OTHER;
×
842
        log_debug("Entry-token directory %s not found, using layout=%s.",
×
843
                  entry_token_path,
844
                  layout_to_string(c->layout));
845
        return 0;
846
}
847

848
static int context_set_up_staging_area(Context *c) {
×
849
        int r;
×
850

851
        assert(c);
×
852

853
        if (c->staging_area)
×
854
                return 0;
×
855

856
        const char *d;
×
857
        r = var_tmp_dir(&d);
×
858
        if (r < 0)
×
859
                return log_error_errno(r, "Failed to determine temporary directory location: %m");
×
860

861
        _cleanup_free_ char *template = path_join(d, "kernel-install.staging.XXXXXX");
×
862
        if (!template)
×
863
                return log_oom();
×
864

865
        if (c->action == ACTION_INSPECT)
×
866
                /* This is only used for display. The directory will not be created. */
867
                c->staging_area = TAKE_PTR(template);
×
868
        else {
869
                r = mkdtemp_malloc(template, &c->staging_area);
×
870
                if (r < 0)
×
871
                        return log_error_errno(r, "Failed to create staging area: %m");
×
872
        }
873

874
        return 0;
875
}
876

877
static int context_build_entry_dir(Context *c) {
×
878
        assert(c);
×
879
        assert(c->boot_root);
×
880
        assert(c->entry_token);
×
881
        assert(c->version || c->action == ACTION_INSPECT);
×
882

883
        if (c->entry_dir)
×
884
                return 0;
885

886
        c->entry_dir = path_join(c->boot_root, c->entry_token, c->version ?: "KERNEL_VERSION");
×
887
        if (!c->entry_dir)
×
888
                return log_oom();
×
889

890
        log_debug("Using ENTRY_DIR=%s", c->entry_dir);
×
891
        return 0;
892
}
893

894
static bool context_should_make_entry_dir(Context *c) {
×
895
        assert(c);
×
896

897
        /* Compatibility with earlier versions that used the presence of $BOOT_ROOT/$ENTRY_TOKEN to signal to
898
         * 00-entry-directory to create $ENTRY_DIR to serve as the indication to use or to not use the BLS */
899

900
        if (arg_make_entry_directory < 0)
×
901
                return c->layout == LAYOUT_BLS;
×
902

903
        return arg_make_entry_directory;
×
904
}
905

906
static int context_make_entry_dir(Context *c) {
×
907
        _cleanup_close_ int fd = -EBADF;
×
908

909
        assert(c);
×
910
        assert(c->entry_dir);
×
911

912
        if (c->action != ACTION_ADD)
×
913
                return 0;
914

915
        if (!context_should_make_entry_dir(c))
×
916
                return 0;
917

918
        log_debug("mkdir -p %s", c->entry_dir);
×
919
        fd = chase_and_openat(c->rfd, c->entry_dir, CHASE_AT_RESOLVE_IN_ROOT | CHASE_MKDIR_0755,
×
920
                              O_CLOEXEC | O_CREAT | O_DIRECTORY | O_PATH, NULL);
921
        if (fd < 0)
×
922
                return log_error_errno(fd, "Failed to make directory '%s': %m", c->entry_dir);
×
923

924
        return 0;
925
}
926

927
static int context_remove_entry_dir(Context *c) {
×
928
        _cleanup_free_ char *p = NULL;
×
929
        _cleanup_close_ int fd = -EBADF;
×
930
        struct stat st;
×
931
        int r;
×
932

933
        assert(c);
×
934
        assert(c->entry_dir);
×
935

936
        if (c->action != ACTION_REMOVE)
×
937
                return 0;
938

939
        if (!context_should_make_entry_dir(c))
×
940
                return 0;
941

942
        log_debug("rm -rf %s", c->entry_dir);
×
943
        fd = chase_and_openat(c->rfd, c->entry_dir, CHASE_AT_RESOLVE_IN_ROOT, O_CLOEXEC | O_DIRECTORY, &p);
×
944
        if (fd < 0) {
×
945
                if (IN_SET(fd, -ENOTDIR, -ENOENT))
×
946
                        return 0;
947
                return log_debug_errno(fd, "Failed to chase and open %s, ignoring: %m", c->entry_dir);
×
948
        }
949

950
        if (fstat(fd, &st) < 0)
×
951
                return log_debug_errno(errno, "Failed to stat %s: %m", p);
×
952

953
        r = rm_rf_children(TAKE_FD(fd), REMOVE_PHYSICAL|REMOVE_MISSING_OK|REMOVE_CHMOD, &st);
×
954
        if (r < 0)
×
955
                log_debug_errno(r, "Failed to remove children of %s, ignoring: %m", p);
×
956

957
        if (unlinkat(c->rfd, p, AT_REMOVEDIR) < 0)
×
958
                log_debug_errno(errno, "Failed to remove %s, ignoring: %m", p);
×
959

960
        return 0;
961
}
962

963
static int context_build_arguments(Context *c) {
×
964
        _cleanup_strv_free_ char **a = NULL;
×
965
        const char *verb;
×
966
        int r;
×
967

968
        assert(c);
×
969
        assert(c->entry_dir);
×
970

971
        if (c->argv)
×
972
                return 0;
973

974
        switch (c->action) {
×
975
        case ACTION_ADD:
×
976
                assert(c->version);
×
977
                assert(c->kernel);
×
978
                verb = "add";
979
                break;
980

981
        case ACTION_REMOVE:
×
982
                assert(c->version);
×
983
                assert(!c->kernel);
×
984
                assert(!c->initrds);
×
985
                verb = "remove";
986
                break;
987

988
        case ACTION_INSPECT:
989
                verb = "add|remove";
990
                break;
991

992
        default:
×
993
                assert_not_reached();
×
994
        }
995

996
        a = strv_new("dummy-arg", /* to make strv_free() works for this variable. */
×
997
                     verb,
998
                     c->version ?: "KERNEL_VERSION",
999
                     c->entry_dir);
1000
        if (!a)
×
1001
                return log_oom();
×
1002

1003
        if (c->action == ACTION_ADD) {
×
1004
                r = strv_extend(&a, c->kernel);
×
1005
                if (r < 0)
×
1006
                        return log_oom();
×
1007

1008
                r = strv_extend_strv(&a, c->initrds, /* filter_duplicates= */ false);
×
1009
                if (r < 0)
×
1010
                        return log_oom();
×
1011

1012
        } else if (c->action == ACTION_INSPECT) {
×
1013
                r = strv_extend_many(
×
1014
                                &a,
1015
                                c->kernel ?: "[KERNEL_IMAGE]",
1016
                                "[INITRD...]");
1017
                if (r < 0)
×
1018
                        return log_oom();
×
1019
        }
1020

1021
        c->argv = TAKE_PTR(a);
×
1022
        return 0;
×
1023
}
1024

1025
static int context_build_environment(Context *c) {
×
1026
        _cleanup_strv_free_ char **e = NULL;
×
1027
        int r;
×
1028

1029
        assert(c);
×
1030

1031
        if (c->envp)
×
1032
                return 0;
1033

1034
        r = strv_env_assign_many(&e,
×
1035
                                 "LC_COLLATE",                      SYSTEMD_DEFAULT_LOCALE,
1036
                                 "KERNEL_INSTALL_VERBOSE",          one_zero(arg_verbose),
1037
                                 "KERNEL_INSTALL_IMAGE_TYPE",       kernel_image_type_to_string(c->kernel_image_type),
1038
                                 "KERNEL_INSTALL_MACHINE_ID",       SD_ID128_TO_STRING(c->machine_id),
1039
                                 "KERNEL_INSTALL_ENTRY_TOKEN",      c->entry_token,
1040
                                 "KERNEL_INSTALL_BOOT_ROOT",        c->boot_root,
1041
                                 "KERNEL_INSTALL_LAYOUT",           context_get_layout(c),
1042
                                 "KERNEL_INSTALL_INITRD_GENERATOR", strempty(c->initrd_generator),
1043
                                 "KERNEL_INSTALL_UKI_GENERATOR",    strempty(c->uki_generator),
1044
                                 "KERNEL_INSTALL_BOOT_ENTRY_TYPE",  boot_entry_type_to_string(c->entry_type),
1045
                                 "KERNEL_INSTALL_STAGING_AREA",     c->staging_area);
1046
        if (r < 0)
×
1047
                return log_error_errno(r, "Failed to build environment variables for plugins: %m");
×
1048

1049
        c->envp = TAKE_PTR(e);
×
1050
        return 0;
×
1051
}
1052

1053
static int context_prepare_execution(Context *c) {
×
1054
        int r;
×
1055

1056
        assert(c);
×
1057

1058
        r = context_inspect_kernel(c);
×
1059
        if (r < 0)
×
1060
                return r;
1061

1062
        r = context_ensure_layout(c);
×
1063
        if (r < 0)
×
1064
                return r;
1065

1066
        r = context_set_up_staging_area(c);
×
1067
        if (r < 0)
×
1068
                return r;
1069

1070
        r = context_build_entry_dir(c);
×
1071
        if (r < 0)
×
1072
                return r;
1073

1074
        r = context_build_arguments(c);
×
1075
        if (r < 0)
×
1076
                return r;
1077

1078
        r = context_build_environment(c);
×
1079
        if (r < 0)
×
1080
                return r;
×
1081

1082
        return 0;
1083
}
1084

1085
static int context_execute(Context *c) {
×
1086
        int r, ret;
×
1087

1088
        assert(c);
×
1089

1090
        r = context_make_entry_dir(c);
×
1091
        if (r < 0)
×
1092
                return r;
1093

1094
        if (DEBUG_LOGGING) {
×
1095
                _cleanup_free_ char *x = strv_join_full(c->plugins, "", "\n  ", /* escape_separator= */ false);
×
1096
                log_debug("Using plugins: %s", strna(x));
×
1097

1098
                _cleanup_free_ char *y = strv_join_full(c->envp, "", "\n  ", /* escape_separator= */ false);
×
1099
                log_debug("Plugin environment: %s", strna(y));
×
1100

1101
                _cleanup_free_ char *z = strv_join(strv_skip(c->argv, 1), " ");
×
1102
                log_debug("Plugin arguments: %s", strna(z));
×
1103
        }
1104

1105
        ret = execute_strv(
×
1106
                        "plugins",
1107
                        c->plugins,
×
1108
                        /* root= */ NULL,
1109
                        USEC_INFINITY,
1110
                        /* callbacks= */ NULL,
1111
                        /* callback_args= */ NULL,
1112
                        c->argv,
1113
                        c->envp,
1114
                        EXEC_DIR_SKIP_REMAINING);
1115

1116
        r = context_remove_entry_dir(c);
×
1117
        if (r < 0)
×
1118
                return r;
×
1119

1120
        /* This returns 0 on success, positive exit code on plugin failure, negative errno on other failures. */
1121
        return ret;
1122
}
1123

1124
static bool bypass(void) {
×
1125
        return should_bypass("KERNEL_INSTALL");
×
1126
}
1127

1128
static int kernel_from_version(const char *version, char **ret_kernel) {
×
1129
        _cleanup_free_ char *vmlinuz = NULL;
×
1130
        int r;
×
1131

1132
        assert(version);
×
1133
        assert(ret_kernel);
×
1134

1135
        vmlinuz = path_join("/usr/lib/modules/", version, "/vmlinuz");
×
1136
        if (!vmlinuz)
×
1137
                return log_oom();
×
1138

1139
        r = access_nofollow(vmlinuz, F_OK);
×
1140
        if (r == -ENOENT)
×
1141
                return log_error_errno(r, "Kernel image not installed to '%s', specify kernel kernel image path explicitly.", vmlinuz);
×
1142
        if (r < 0)
×
1143
                return log_error_errno(r, "Failed to determine if kernel image is installed to '%s': %m", vmlinuz);
×
1144

1145
        *ret_kernel = TAKE_PTR(vmlinuz);
×
1146
        return 0;
×
1147
}
1148

1149
static int do_add(
×
1150
                Context *c,
1151
                const char *version,
1152
                const char *kernel,
1153
                char **initrds) {
1154

1155
        int r;
×
1156

1157
        assert(c);
×
1158

1159
        struct utsname un;
×
1160
        if (!version) {
×
1161
                assert_se(uname(&un) >= 0);
×
1162
                version = un.release;
1163
        }
1164

1165
        _cleanup_free_ char *vmlinuz = NULL;
×
1166
        if (!kernel) {
×
1167
                r = kernel_from_version(version, &vmlinuz);
×
1168
                if (r < 0)
×
1169
                        return r;
1170

1171
                kernel = vmlinuz;
×
1172
        }
1173

1174
        r = context_set_version(c, version);
×
1175
        if (r < 0)
×
1176
                return r;
1177

1178
        r = context_set_kernel(c, kernel);
×
1179
        if (r < 0)
×
1180
                return r;
1181

1182
        r = context_set_initrds(c, initrds);
×
1183
        if (r < 0)
×
1184
                return r;
1185

1186
        r = context_prepare_execution(c);
×
1187
        if (r < 0)
×
1188
                return r;
1189

1190
        return context_execute(c);
×
1191
}
1192

1193
static int verb_add(int argc, char *argv[], uintptr_t _data, void *userdata) {
×
1194
        const char *version, *kernel;
×
1195
        char **initrds;
×
1196
        int r;
×
1197

1198
        assert(argv);
×
1199

1200
        if (arg_root)
×
1201
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "'add' does not support --root= or --image=.");
×
1202

1203
        if (bypass())
×
1204
                return 0;
1205

1206
        _cleanup_(context_done) Context c = CONTEXT_NULL;
×
1207
        r = context_from_cmdline(&c, ACTION_ADD);
×
1208
        if (r < 0)
×
1209
                return r;
1210

1211
        /* We use the same order of arguments that "inspect" introduced, i.e. if only on argument is
1212
         * specified we take it as the kernel path, not the version, i.e. it's the first argument that is
1213
         * optional, not the 2nd. */
1214
        version = argc > 2 ? empty_or_dash_to_null(argv[1]) : NULL;
×
1215
        kernel = argc > 2 ? empty_or_dash_to_null(argv[2]) :
×
1216
                (argc > 1 ? empty_or_dash_to_null(argv[1]) : NULL);
×
1217
        initrds = strv_skip(argv, 3);
×
1218

1219
        return do_add(&c, version, kernel, initrds);
×
1220
}
1221

1222
static int verb_add_all(int argc, char *argv[], uintptr_t _data, void *userdata) {
×
1223
        _cleanup_close_ int fd = -EBADF;
×
1224
        size_t n = 0;
×
1225
        int ret = 0, r;
×
1226

1227
        assert(argv);
×
1228

1229
        if (arg_root)
×
1230
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "'add-all' does not support --root= or --image=.");
×
1231

1232
        if (bypass())
×
1233
                return 0;
1234

1235
        _cleanup_(context_done) Context c = CONTEXT_NULL;
×
1236
        r = context_from_cmdline(&c, ACTION_ADD);
×
1237
        if (r < 0)
×
1238
                return r;
1239

1240
        fd = chase_and_openat(c.rfd, "/usr/lib/modules", CHASE_AT_RESOLVE_IN_ROOT, O_DIRECTORY|O_RDONLY|O_CLOEXEC, NULL);
×
1241
        if (fd < 0)
×
1242
                return log_error_errno(fd, "Failed to open %s/usr/lib/modules/: %m", strempty(arg_root));
×
1243

1244
        _cleanup_free_ DirectoryEntries *de = NULL;
×
1245
        r = readdir_all(fd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT, &de);
×
1246
        if (r < 0)
×
1247
                return log_error_errno(r, "Failed to numerate /usr/lib/modules/ contents: %m");
×
1248

1249
        FOREACH_ARRAY(d, de->entries, de->n_entries) {
×
1250
                r = dirent_ensure_type(fd, *d);
×
1251
                if (r < 0) {
×
1252
                        if (r != -ENOENT) /* don't log if just gone by now */
×
1253
                                log_debug_errno(r, "Failed to check if '%s/usr/lib/modules/%s' is a directory, ignoring: %m", strempty(arg_root), (*d)->d_name);
×
1254
                        continue;
×
1255
                }
1256

1257
                if ((*d)->d_type != DT_DIR)
×
1258
                        continue;
×
1259

1260
                _cleanup_free_ char *fn = path_join((*d)->d_name, "vmlinuz");
×
1261
                if (!fn)
×
1262
                        return log_oom();
×
1263

1264
                if (faccessat(fd, fn, F_OK, AT_SYMLINK_NOFOLLOW) < 0) {
×
1265
                        if (errno != ENOENT)
×
1266
                                log_debug_errno(errno, "Failed to check if '%s/usr/lib/modules/%s/vmlinuz' exists, ignoring: %m", strempty(arg_root), (*d)->d_name);
×
1267

1268
                        log_notice("Not adding version '%s', because kernel image not found.", (*d)->d_name);
×
1269
                        continue;
×
1270
                }
1271

1272
                _cleanup_(context_done) Context copy = CONTEXT_NULL;
×
1273

1274
                r = context_copy(&c, &copy);
×
1275
                if (r < 0)
×
1276
                        return log_error_errno(r, "Failed to copy execution context: %m");
×
1277

1278
                /* do_add() will look up the path in the correct root directory so we don't need to prefix it
1279
                 * with arg_root here. */
1280
                _cleanup_free_ char *full = path_join("/usr/lib/modules/", fn);
×
1281
                if (!full)
×
1282
                        return log_oom();
×
1283

1284
                r = do_add(&copy,
×
1285
                           /* version= */ (*d)->d_name,
×
1286
                           /* kernel= */ full,
1287
                           /* initrds= */ NULL);
1288
                if (r == 0)
×
1289
                        n++;
×
1290
                else if (ret == 0)
×
1291
                        ret = r;
×
1292
        }
1293

1294
        if (n > 0)
×
1295
                log_debug("Installed %zu kernel(s).", n);
×
1296
        else if (ret == 0)
×
1297
                ret = log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No kernels to install found.");
×
1298

1299
        return ret;
1300
}
1301

1302
static int run_as_installkernel(int argc, char *argv[]) {
×
1303
        /* kernel's install.sh invokes us as
1304
         *   /sbin/installkernel <version> <vmlinuz> <map> <installation-dir>
1305
         * We ignore the last two arguments. */
1306
        if (optind + 2 > argc)
×
1307
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "'installkernel' command requires at least two arguments.");
×
1308

1309
        return verb_add(3, STRV_MAKE("add", argv[optind], argv[optind+1]), /* data= */ 0, /* userdata= */ NULL);
×
1310
}
1311

1312
static int verb_remove(int argc, char *argv[], uintptr_t _data, void *userdata) {
×
1313
        int r;
×
1314

1315
        assert(argc >= 2);
×
1316
        assert(argv);
×
1317

1318
        if (arg_root)
×
1319
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "'remove' does not support --root= or --image=.");
×
1320

1321
        if (argc > 2)
×
1322
                log_debug("Too many arguments specified. 'kernel-install remove' takes only kernel version. "
×
1323
                          "Ignoring residual arguments.");
1324

1325
        if (bypass())
×
1326
                return 0;
1327

1328
        _cleanup_(context_done) Context c = CONTEXT_NULL;
×
1329
        r = context_from_cmdline(&c, ACTION_REMOVE);
×
1330
        if (r < 0)
×
1331
                return r;
1332

1333
        /* Note, we do not automatically derive the kernel version to remove from uname() here (unlike we do
1334
         * it for the "add" verb), since we don't want to make it too easy to uninstall your running
1335
         * kernel, as a safety precaution */
1336

1337
        r = context_set_version(&c, argv[1]);
×
1338
        if (r < 0)
×
1339
                return r;
1340

1341
        r = context_prepare_execution(&c);
×
1342
        if (r < 0)
×
1343
                return r;
1344

1345
        return context_execute(&c);
×
1346
}
1347

1348
static int verb_inspect(int argc, char *argv[], uintptr_t _data, void *userdata) {
×
1349
        _cleanup_(table_unrefp) Table *t = NULL;
×
1350
        _cleanup_free_ char *vmlinuz = NULL;
×
1351
        const char *version, *kernel;
×
1352
        char **initrds;
×
1353
        struct utsname un;
×
1354
        int r;
×
1355

1356
        _cleanup_(context_done) Context c = CONTEXT_NULL;
×
1357
        r = context_from_cmdline(&c, ACTION_INSPECT);
×
1358
        if (r < 0)
×
1359
                return r;
1360

1361
        /* When only a single parameter is specified 'inspect' it's the kernel image path, and not the kernel
1362
         * version. i.e. it's the first argument that is optional, not the 2nd. That's a bit unfortunate, but
1363
         * we keep the behaviour for compatibility. If users want to specify only the version (and have the
1364
         * kernel image path derived automatically), then they may specify an empty string or "dash" as
1365
         * kernel image path. */
1366
        version = argc > 2 ? empty_or_dash_to_null(argv[1]) : NULL;
×
1367
        kernel = argc > 2 ? empty_or_dash_to_null(argv[2]) :
×
1368
                (argc > 1 ? empty_or_dash_to_null(argv[1]) : NULL);
×
1369
        initrds = strv_skip(argv, 3);
×
1370

1371
        if (!version && !arg_root) {
×
1372
                assert_se(uname(&un) >= 0);
×
1373
                version = un.release;
1374
        }
1375

1376
        if (!kernel && version) {
×
1377
                r = kernel_from_version(version, &vmlinuz);
×
1378
                if (r < 0)
×
1379
                        return r;
1380

1381
                kernel = vmlinuz;
×
1382
        }
1383

1384
        r = context_set_version(&c, version);
×
1385
        if (r < 0)
×
1386
                return r;
1387

1388
        r = context_set_kernel(&c, kernel);
×
1389
        if (r < 0)
×
1390
                return r;
1391

1392
        r = context_set_initrds(&c, initrds);
×
1393
        if (r < 0)
×
1394
                return r;
1395

1396
        r = context_prepare_execution(&c);
×
1397
        if (r < 0)
×
1398
                return r;
1399

1400
        t = table_new_vertical();
×
1401
        if (!t)
×
1402
                return log_oom();
×
1403

1404
        r = table_add_many(t,
×
1405
                           TABLE_FIELD, "Machine ID",
1406
                           TABLE_ID128, c.machine_id,
1407
                           TABLE_FIELD, "Kernel Image Type",
1408
                           TABLE_STRING, kernel_image_type_to_string(c.kernel_image_type),
1409
                           TABLE_FIELD, "Layout",
1410
                           TABLE_STRING, context_get_layout(&c),
1411
                           TABLE_FIELD, "Boot Root",
1412
                           TABLE_STRING, c.boot_root,
1413
                           TABLE_FIELD, "Entry Token Type",
1414
                           TABLE_STRING, boot_entry_token_type_to_string(c.entry_token_type),
1415
                           TABLE_FIELD, "Entry Token",
1416
                           TABLE_STRING, c.entry_token,
1417
                           TABLE_FIELD, "Entry Directory",
1418
                           TABLE_STRING, c.entry_dir,
1419
                           TABLE_FIELD, "Kernel Version",
1420
                           TABLE_VERSION, c.version,
1421
                           TABLE_FIELD, "Kernel",
1422
                           TABLE_STRING, c.kernel,
1423
                           TABLE_FIELD, "Initrds",
1424
                           TABLE_STRV, c.initrds,
1425
                           TABLE_FIELD, "Initrd Generator",
1426
                           TABLE_STRING, c.initrd_generator,
1427
                           TABLE_FIELD, "UKI Generator",
1428
                           TABLE_STRING, c.uki_generator,
1429
                           TABLE_FIELD, "Plugins",
1430
                           TABLE_STRV, c.plugins,
1431
                           TABLE_FIELD, "Plugin Environment",
1432
                           TABLE_STRV, c.envp);
1433
        if (r < 0)
×
1434
                return table_log_add_error(r);
×
1435

1436
        if (!sd_json_format_enabled(arg_json_format_flags)) {
×
1437
                r = table_add_many(t,
×
1438
                                   TABLE_FIELD, "Plugin Arguments",
1439
                                   TABLE_STRV, strv_skip(c.argv, 1));
1440
                if (r < 0)
×
1441
                        return table_log_add_error(r);
×
1442
        }
1443

1444
        table_set_ersatz_string(t, TABLE_ERSATZ_UNSET);
×
1445

1446
        for (size_t row = 1; row < table_get_rows(t); row++) {
×
1447
                _cleanup_free_ char *name = NULL;
×
1448

1449
                name = strdup(table_get_at(t, row, 0));
×
1450
                if (!name)
×
1451
                        return log_oom();
×
1452

1453
                r = table_set_json_field_name(t, row - 1, delete_chars(name, " "));
×
1454
                if (r < 0)
×
1455
                        return log_error_errno(r, "Failed to set JSON field name: %m");
×
1456
        }
1457

1458
        return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, /* show_header= */ false);
×
1459
}
1460

1461
static int verb_list(int argc, char *argv[], uintptr_t _data, void *userdata) {
×
1462
        _cleanup_close_ int fd = -EBADF;
×
1463
        int r;
×
1464

1465
        _cleanup_(context_done) Context c = CONTEXT_NULL;
×
1466
        r = context_from_cmdline(&c, ACTION_INSPECT);
×
1467
        if (r < 0)
×
1468
                return r;
1469

1470
        fd = chase_and_openat(c.rfd, "/usr/lib/modules", CHASE_AT_RESOLVE_IN_ROOT, O_DIRECTORY|O_RDONLY|O_CLOEXEC, NULL);
×
1471
        if (fd < 0)
×
1472
                return log_error_errno(fd, "Failed to open %s/usr/lib/modules/: %m", strempty(arg_root));
×
1473

1474
        _cleanup_free_ DirectoryEntries *de = NULL;
×
1475
        r = readdir_all(fd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT, &de);
×
1476
        if (r < 0)
×
1477
                return log_error_errno(r, "Failed to numerate /usr/lib/modules/ contents: %m");
×
1478

1479
        _cleanup_(table_unrefp) Table *table = NULL;
×
1480
        table = table_new("version", "has kernel", "path");
×
1481
        if (!table)
×
1482
                return log_oom();
×
1483

1484
        table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
×
1485
        table_set_align_percent(table, table_get_cell(table, 0, 1), 100);
×
1486
        (void) table_set_sort(table, (size_t) 0);
×
1487

1488
        FOREACH_ARRAY(d, de->entries, de->n_entries) {
×
1489
                _cleanup_free_ char *j = path_join("/usr/lib/modules/", (*d)->d_name);
×
1490
                if (!j)
×
1491
                        return log_oom();
×
1492

1493
                r = dirent_ensure_type(fd, *d);
×
1494
                if (r < 0) {
×
1495
                        if (r != -ENOENT) /* don't log if just gone by now */
×
1496
                                log_debug_errno(r, "Failed to check if '%s/%s' is a directory, ignoring: %m", strempty(arg_root), j);
×
1497
                        continue;
×
1498
                }
1499

1500
                if ((*d)->d_type != DT_DIR)
×
1501
                        continue;
×
1502

1503
                _cleanup_free_ char *fn = path_join((*d)->d_name, "vmlinuz");
×
1504
                if (!fn)
×
1505
                        return log_oom();
×
1506

1507
                bool exists;
×
1508
                if (faccessat(fd, fn, F_OK, AT_SYMLINK_NOFOLLOW) < 0) {
×
1509
                        if (errno != ENOENT)
×
1510
                                log_debug_errno(errno, "Failed to check if '%s/usr/lib/modules/%s/vmlinuz' exists, ignoring: %m", strempty(arg_root), (*d)->d_name);
×
1511

1512
                        exists = false;
1513
                } else
1514
                        exists = true;
1515

1516
                r = table_add_many(table,
×
1517
                                   TABLE_VERSION, (*d)->d_name,
1518
                                   TABLE_BOOLEAN_CHECKMARK, exists,
1519
                                   TABLE_SET_COLOR, ansi_highlight_green_red(exists),
1520
                                   TABLE_PATH, j);
1521
                if (r < 0)
×
1522
                        return table_log_add_error(r);
×
1523
        }
1524

1525
        return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
×
1526
}
1527

1528
static int help(void) {
×
1529
        _cleanup_free_ char *link = NULL;
×
1530
        int r;
×
1531

1532
        r = terminal_urlify_man("kernel-install", "8", &link);
×
1533
        if (r < 0)
×
1534
                return log_oom();
×
1535

1536
        printf("%1$s [OPTIONS...] COMMAND ...\n\n"
×
1537
               "%5$sAdd and remove kernel and initrd images to and from the boot partition.%6$s\n"
1538
               "\n%3$sUsage:%4$s\n"
1539
               "  kernel-install [OPTIONS...] add [[[KERNEL-VERSION] KERNEL-IMAGE] [INITRD ...]]\n"
1540
               "  kernel-install [OPTIONS...] add-all\n"
1541
               "  kernel-install [OPTIONS...] remove KERNEL-VERSION\n"
1542
               "  kernel-install [OPTIONS...] inspect [[[KERNEL-VERSION] KERNEL-IMAGE]\n"
1543
               "                                      [INITRD ...]]\n"
1544
               "  kernel-install [OPTIONS...] list\n"
1545
               "\n%3$sOptions:%4$s\n"
1546
               "  -h --help                    Show this help\n"
1547
               "     --version                 Show package version\n"
1548
               "  -v --verbose                 Increase verbosity\n"
1549
               "     --esp-path=PATH           Path to the EFI System Partition (ESP)\n"
1550
               "     --boot-path=PATH          Path to the $BOOT partition\n"
1551
               "     --make-entry-directory=yes|no|auto\n"
1552
               "                               Create $BOOT/ENTRY-TOKEN/ directory\n"
1553
               "     --entry-type=type1|type2|all\n"
1554
               "                               Operate only on the specified bootloader\n"
1555
               "                               entry type\n"
1556
               "     --entry-token=machine-id|os-id|os-image-id|auto|literal:…\n"
1557
               "                               Entry token to be used for this installation\n"
1558
               "     --no-pager                Do not pipe inspect output into a pager\n"
1559
               "     --json=pretty|short|off   Generate JSON output\n"
1560
               "     --no-legend               Do not show the headers and footers\n"
1561
               "     --root=PATH               Operate on an alternate filesystem root\n"
1562
               "     --image=PATH              Operate on disk image as filesystem root\n"
1563
               "     --image-policy=POLICY     Specify disk image dissection policy\n"
1564
               "\n"
1565
               "This program may also be invoked as 'installkernel':\n"
1566
               "  installkernel  [OPTIONS...] VERSION VMLINUZ [MAP] [INSTALLATION-DIR]\n"
1567
               "(The optional arguments are passed by kernel build system, but ignored.)\n"
1568
               "\n"
1569
               "See the %2$s for details.\n",
1570
               program_invocation_short_name,
1571
               link,
1572
               ansi_underline(),
1573
               ansi_normal(),
1574
               ansi_highlight(),
1575
               ansi_normal());
1576

1577
        return 0;
1578
}
1579

1580
static int parse_argv(int argc, char *argv[]) {
×
1581
        enum {
×
1582
                ARG_VERSION = 0x100,
1583
                ARG_NO_LEGEND,
1584
                ARG_ESP_PATH,
1585
                ARG_BOOT_PATH,
1586
                ARG_MAKE_ENTRY_DIRECTORY,
1587
                ARG_ENTRY_TOKEN,
1588
                ARG_NO_PAGER,
1589
                ARG_JSON,
1590
                ARG_ROOT,
1591
                ARG_IMAGE,
1592
                ARG_IMAGE_POLICY,
1593
                ARG_BOOT_ENTRY_TYPE,
1594
        };
1595
        static const struct option options[] = {
×
1596
                { "help",                 no_argument,       NULL, 'h'                      },
1597
                { "version",              no_argument,       NULL, ARG_VERSION              },
1598
                { "verbose",              no_argument,       NULL, 'v'                      },
1599
                { "esp-path",             required_argument, NULL, ARG_ESP_PATH             },
1600
                { "boot-path",            required_argument, NULL, ARG_BOOT_PATH            },
1601
                { "make-entry-directory", required_argument, NULL, ARG_MAKE_ENTRY_DIRECTORY },
1602
                { "entry-token",          required_argument, NULL, ARG_ENTRY_TOKEN          },
1603
                { "no-pager",             no_argument,       NULL, ARG_NO_PAGER             },
1604
                { "json",                 required_argument, NULL, ARG_JSON                 },
1605
                { "root",                 required_argument, NULL, ARG_ROOT                 },
1606
                { "image",                required_argument, NULL, ARG_IMAGE                },
1607
                { "image-policy",         required_argument, NULL, ARG_IMAGE_POLICY         },
1608
                { "no-legend",            no_argument,       NULL, ARG_NO_LEGEND            },
1609
                { "entry-type",           required_argument, NULL, ARG_BOOT_ENTRY_TYPE      },
1610
                {}
1611
        };
1612
        int t, r;
×
1613

1614
        assert(argc >= 0);
×
1615
        assert(argv);
×
1616

1617
        while ((t = getopt_long(argc, argv, "hv", options, NULL)) >= 0)
×
1618
                switch (t) {
×
1619
                case 'h':
×
1620
                        return help();
×
1621

1622
                case ARG_VERSION:
×
1623
                        return version();
×
1624

1625
                case ARG_NO_LEGEND:
×
1626
                        arg_legend = false;
×
1627
                        break;
×
1628

1629
                case 'v':
×
1630
                        log_set_max_level(LOG_DEBUG);
×
1631
                        arg_verbose = true;
×
1632
                        break;
×
1633

1634
                case ARG_ESP_PATH:
×
1635
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_esp_path);
×
1636
                        if (r < 0)
×
1637
                                return log_oom();
×
1638
                        break;
1639

1640
                case ARG_BOOT_PATH:
×
1641
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_xbootldr_path);
×
1642
                        if (r < 0)
×
1643
                                return log_oom();
×
1644
                        break;
1645

1646
                case ARG_MAKE_ENTRY_DIRECTORY:
×
1647
                        if (streq(optarg, "auto"))
×
1648
                                arg_make_entry_directory = -1;
×
1649
                        else {
1650
                                r = parse_boolean_argument("--make-entry-directory=", optarg, NULL);
×
1651
                                if (r < 0)
×
1652
                                        return r;
1653

1654
                                arg_make_entry_directory = r;
×
1655
                        }
1656
                        break;
1657

1658
                case ARG_ENTRY_TOKEN:
×
1659
                        r = parse_boot_entry_token_type(optarg, &arg_entry_token_type, &arg_entry_token);
×
1660
                        if (r < 0)
×
1661
                                return r;
1662
                        break;
1663

1664
                case ARG_NO_PAGER:
×
1665
                        arg_pager_flags |= PAGER_DISABLE;
×
1666
                        break;
×
1667

1668
                case ARG_JSON:
×
1669
                        r = parse_json_argument(optarg, &arg_json_format_flags);
×
1670
                        if (r <= 0)
×
1671
                                return r;
1672
                        break;
1673

1674
                case ARG_ROOT:
×
1675
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_root);
×
1676
                        if (r < 0)
×
1677
                                return r;
1678
                        break;
1679

1680
                case ARG_IMAGE:
×
1681
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
×
1682
                        if (r < 0)
×
1683
                                return r;
1684
                        break;
1685

1686
                case ARG_IMAGE_POLICY:
×
1687
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
1688
                        if (r < 0)
×
1689
                                return r;
1690
                        break;
1691

1692
                case ARG_BOOT_ENTRY_TYPE: {
×
1693
                        if (isempty(optarg) || streq(optarg, "all")) {
×
1694
                                arg_boot_entry_type = _BOOT_ENTRY_TYPE_INVALID;
×
1695
                                break;
×
1696
                        }
1697

1698
                        BootEntryType e = boot_entry_type_from_string(optarg);
×
1699
                        if (e < 0)
×
1700
                                return log_error_errno(e, "Invalid entry type: %s", optarg);
×
1701
                        arg_boot_entry_type = e;
×
1702
                        break;
×
1703
                }
1704

1705
                case '?':
1706
                        return -EINVAL;
1707

1708
                default:
×
1709
                        assert_not_reached();
×
1710
                }
1711

1712
        if (arg_image && arg_root)
×
1713
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
×
1714

1715
        return 1;
1716
}
1717

1718
static int kernel_install_main(int argc, char *argv[]) {
×
1719
        static const Verb verbs[] = {
×
1720
                { "add",     1, VERB_ANY, 0,            verb_add     },
1721
                { "add-all", 1, 1,        0,            verb_add_all },
1722
                { "remove",  2, VERB_ANY, 0,            verb_remove  },
1723
                { "inspect", 1, VERB_ANY, VERB_DEFAULT, verb_inspect },
1724
                { "list",    1, 1,        0,            verb_list    },
1725
                {}
1726
        };
1727

1728
        return dispatch_verb(argc, argv, verbs, /* userdata= */ NULL);
×
1729
}
1730

1731
static int run(int argc, char* argv[]) {
×
1732
        int r;
×
1733

1734
        log_setup();
×
1735

1736
        r = parse_argv(argc, argv);
×
1737
        if (r <= 0)
×
1738
                return r;
×
1739

1740
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
×
1741
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
×
1742
        if (arg_image) {
×
1743
                assert(!arg_root);
×
1744

1745
                r = mount_image_privately_interactively(
×
1746
                                arg_image,
1747
                                arg_image_policy,
1748
                                DISSECT_IMAGE_GENERIC_ROOT |
1749
                                DISSECT_IMAGE_REQUIRE_ROOT |
1750
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
1751
                                DISSECT_IMAGE_VALIDATE_OS |
1752
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
1753
                                &mounted_dir,
1754
                                /* ret_dir_fd= */ NULL,
1755
                                &loop_device);
1756
                if (r < 0)
×
1757
                        return r;
1758

1759
                arg_root = strdup(mounted_dir);
×
1760
                if (!arg_root)
×
1761
                        return log_oom();
×
1762
        }
1763

1764
        if (invoked_as(argv, "installkernel"))
×
1765
                return run_as_installkernel(argc, argv);
×
1766

1767
        return kernel_install_main(argc, argv);
×
1768
}
1769

1770
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc