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

systemd / systemd / 23367620641

20 Mar 2026 07:25PM UTC coverage: 72.606% (+0.3%) from 72.351%
23367620641

push

github

bluca
kmod-setup: load vsock_loopback alongside vsock

Loading vmw_vsock_virtio_transport early at boot causes vsock to be
resident before any application opens an AF_VSOCK socket. Because the
kernel skips autoloading when the vsock module is already present,
vsock_loopback never gets loaded automatically, and any subsequent
bind() to VMADDR_CID_LOCAL fails with EADDRNOTAVAIL.

Fix this by explicitly loading vsock_loopback on virtio or VMWare
machines via the new may_have_vsock_looopback() helper, wich covers both
vmw_vsock_virtio_transport and vmware_vsock_vmci_transport case.
vsock_loopback is the only module that registers a transport for
VMADDR_CID_LOCAL (CID 1) and has no hard dependency from any of the
vsock transport modules.

Fixes: #41100
Follow-up for 381c78db4

2 of 2 new or added lines in 1 file covered. (100.0%)

2634 existing lines in 55 files now uncovered.

316477 of 435881 relevant lines covered (72.61%)

1159615.85 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) {
×
UNCOV
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
        if (r == -ENOKEY) {
×
575
                log_debug_errno(r, "Couldn't find an XBOOTLDR partition.");
×
576
                return 0;
×
577
        }
578
        if (r == -EACCES && geteuid() != 0)
×
579
                return log_error_errno(r, "Failed to determine XBOOTLDR partition: %m");
×
580
        if (r < 0)
×
581
                return r;
582

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

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

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

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

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

611
static int context_ensure_boot_root(Context *c) {
×
612
        int r;
×
613

614
        assert(c);
×
615

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

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

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

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

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

645
static int context_ensure_entry_token(Context *c) {
×
646
        int r;
×
647

648
        assert(c);
×
649

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

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

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

669
static int context_load_plugins(Context *c) {
×
670
        int r;
×
671

672
        assert(c);
×
673

674
        if (c->plugins)
×
675
                return 0;
×
676

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

686
        return 0;
687
}
688

689
static int context_setup(Context *c) {
×
690
        int r;
×
691

692
        assert(c);
×
693

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

699
        r = context_open_root(c);
×
700
        if (r < 0)
×
701
                return r;
702

703
        r = context_load_environment(c);
×
704
        if (r < 0)
×
705
                return r;
706

707
        r = context_load_install_conf(c);
×
708
        if (r < 0)
×
709
                return r;
710

711
        r = context_load_machine_info(c);
×
712
        if (r < 0)
×
713
                return r;
714

715
        r = context_ensure_machine_id(c);
×
716
        if (r < 0)
×
717
                return r;
718

719
        r = context_ensure_boot_root(c);
×
720
        if (r < 0)
×
721
                return r;
722

723
        r = context_ensure_entry_token(c);
×
724
        if (r < 0)
×
725
                return r;
726

727
        r = context_load_plugins(c);
×
728
        if (r < 0)
×
729
                return r;
×
730

731
        return 0;
732
}
733

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

737
        assert(c);
×
738

739
        c->action = action;
×
740

741
        c->entry_type = arg_boot_entry_type;
×
742

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

747
        c->entry_token_type = arg_entry_token_type;
×
748

749
        return context_setup(c);
×
750
}
751

752
static int context_inspect_kernel(Context *c) {
×
753
        assert(c);
×
754

755
        if (!c->kernel)
×
756
                return 0;
757

758
        return inspect_kernel(c->rfd, c->kernel, &c->kernel_image_type, NULL, NULL, NULL);
×
759
}
760

761
static int context_ensure_layout(Context *c) {
×
762
        int r;
×
763

764
        assert(c);
×
765
        assert(c->boot_root);
×
766
        assert(c->entry_token);
×
767

768
        if (c->layout >= 0 && c->layout != LAYOUT_AUTO)
×
769
                return 0;
×
770

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

774
        if (c->kernel_image_type == KERNEL_IMAGE_TYPE_UKI) {
×
775
                c->layout = LAYOUT_UKI;
×
776
                log_debug("Kernel image type is %s, using layout=%s.",
×
777
                          kernel_image_type_to_string(c->kernel_image_type), layout_to_string(c->layout));
778
                return 0;
×
779
        }
780

781
        _cleanup_free_ char *srel_path = path_join(c->boot_root, "loader/entries.srel");
×
782
        if (!srel_path)
×
783
                return log_oom();
×
784

785
        _cleanup_fclose_ FILE *f = NULL;
×
786
        r = chase_and_fopenat_unlocked(c->rfd, srel_path, CHASE_AT_RESOLVE_IN_ROOT|CHASE_MUST_BE_REGULAR, "re", /* ret_path= */ NULL, &f);
×
787
        if (r < 0) {
×
788
                if (r != -ENOENT)
×
789
                        return log_error_errno(r, "Failed to open '%s': %m", srel_path);
×
790
        } else {
791
                _cleanup_free_ char *srel = NULL;
×
792

793
                r = read_line(f, LONG_LINE_MAX, &srel);
×
794
                if (r < 0)
×
795
                        return log_error_errno(r, "Failed to read %s: %m", srel_path);
×
796

797
                if (streq(srel, "type1"))
×
798
                        /* The loader/entries.srel file clearly indicates that the installed boot loader
799
                         * implements the proper standard upstream boot loader spec for Type #1 entries.
800
                         * Let's default to that, then. */
801
                        c->layout = LAYOUT_BLS;
×
802
                else
803
                        /* The loader/entries.srel file indicates some other spec is implemented and owns the
804
                         * /loader/entries/ directory. Since we have no idea what that means, let's stay away
805
                         * from it by default. */
806
                        c->layout = LAYOUT_OTHER;
×
807

808
                log_debug("%s with '%s' found, using layout=%s.", srel_path, srel, layout_to_string(c->layout));
×
809
                return 0;
×
810
        }
811

812
        _cleanup_free_ char *entry_token_path = path_join(c->boot_root, c->entry_token);
×
813
        if (!entry_token_path)
×
814
                return log_oom();
×
815

816
        r = chaseat(c->rfd, entry_token_path, CHASE_AT_RESOLVE_IN_ROOT|CHASE_MUST_BE_DIRECTORY, /* ret_path= */ NULL, /* ret_fd= */ NULL);
×
817
        if (r < 0) {
×
818
                if (!IN_SET(r, -ENOENT, -ENOTDIR))
×
819
                        return log_error_errno(r, "Failed to check if '%s' exists and is a directory: %m", entry_token_path);
×
820
        } else {
821
                /* If the metadata in $BOOT_ROOT doesn't tell us anything, then check if the entry token
822
                 * directory already exists. If so, let's assume it's the standard boot loader spec, too. */
823
                c->layout = LAYOUT_BLS;
×
824
                log_debug("%s exists, using layout=%s.", entry_token_path, layout_to_string(c->layout));
×
825
                return 0;
×
826
        }
827

828
        /* There's no metadata in $BOOT_ROOT, and apparently no entry token directory installed? Then we
829
         * really don't know anything. */
830
        c->layout = LAYOUT_OTHER;
×
831
        log_debug("Entry-token directory %s not found, using layout=%s.",
×
832
                  entry_token_path,
833
                  layout_to_string(c->layout));
834
        return 0;
835
}
836

837
static int context_set_up_staging_area(Context *c) {
×
838
        int r;
×
839

840
        assert(c);
×
841

842
        if (c->staging_area)
×
843
                return 0;
×
844

845
        const char *d;
×
846
        r = var_tmp_dir(&d);
×
847
        if (r < 0)
×
848
                return log_error_errno(r, "Failed to determine temporary directory location: %m");
×
849

850
        _cleanup_free_ char *template = path_join(d, "kernel-install.staging.XXXXXX");
×
851
        if (!template)
×
852
                return log_oom();
×
853

854
        if (c->action == ACTION_INSPECT)
×
855
                /* This is only used for display. The directory will not be created. */
856
                c->staging_area = TAKE_PTR(template);
×
857
        else {
858
                r = mkdtemp_malloc(template, &c->staging_area);
×
859
                if (r < 0)
×
860
                        return log_error_errno(r, "Failed to create staging area: %m");
×
861
        }
862

863
        return 0;
864
}
865

866
static int context_build_entry_dir(Context *c) {
×
867
        assert(c);
×
868
        assert(c->boot_root);
×
869
        assert(c->entry_token);
×
870
        assert(c->version || c->action == ACTION_INSPECT);
×
871

872
        if (c->entry_dir)
×
873
                return 0;
874

875
        c->entry_dir = path_join(c->boot_root, c->entry_token, c->version ?: "KERNEL_VERSION");
×
876
        if (!c->entry_dir)
×
877
                return log_oom();
×
878

879
        log_debug("Using ENTRY_DIR=%s", c->entry_dir);
×
880
        return 0;
881
}
882

883
static bool context_should_make_entry_dir(Context *c) {
×
884
        assert(c);
×
885

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

889
        if (arg_make_entry_directory < 0)
×
890
                return c->layout == LAYOUT_BLS;
×
891

892
        return arg_make_entry_directory;
×
893
}
894

895
static int context_make_entry_dir(Context *c) {
×
896
        _cleanup_close_ int fd = -EBADF;
×
897

898
        assert(c);
×
899
        assert(c->entry_dir);
×
900

901
        if (c->action != ACTION_ADD)
×
902
                return 0;
903

904
        if (!context_should_make_entry_dir(c))
×
905
                return 0;
906

907
        log_debug("mkdir -p %s", c->entry_dir);
×
908
        fd = chase_and_openat(c->rfd, c->entry_dir, CHASE_AT_RESOLVE_IN_ROOT | CHASE_MKDIR_0755,
×
909
                              O_CLOEXEC | O_CREAT | O_DIRECTORY | O_PATH, NULL);
910
        if (fd < 0)
×
911
                return log_error_errno(fd, "Failed to make directory '%s': %m", c->entry_dir);
×
912

913
        return 0;
914
}
915

916
static int context_remove_entry_dir(Context *c) {
×
917
        _cleanup_free_ char *p = NULL;
×
918
        _cleanup_close_ int fd = -EBADF;
×
919
        struct stat st;
×
920
        int r;
×
921

922
        assert(c);
×
923
        assert(c->entry_dir);
×
924

925
        if (c->action != ACTION_REMOVE)
×
926
                return 0;
927

928
        if (!context_should_make_entry_dir(c))
×
929
                return 0;
930

931
        log_debug("rm -rf %s", c->entry_dir);
×
932
        fd = chase_and_openat(c->rfd, c->entry_dir, CHASE_AT_RESOLVE_IN_ROOT, O_CLOEXEC | O_DIRECTORY, &p);
×
933
        if (fd < 0) {
×
934
                if (IN_SET(fd, -ENOTDIR, -ENOENT))
×
935
                        return 0;
936
                return log_debug_errno(fd, "Failed to chase and open %s, ignoring: %m", c->entry_dir);
×
937
        }
938

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

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

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

949
        return 0;
950
}
951

952
static int context_build_arguments(Context *c) {
×
953
        _cleanup_strv_free_ char **a = NULL;
×
954
        const char *verb;
×
955
        int r;
×
956

957
        assert(c);
×
958
        assert(c->entry_dir);
×
959

960
        if (c->argv)
×
961
                return 0;
962

963
        switch (c->action) {
×
964
        case ACTION_ADD:
×
965
                assert(c->version);
×
966
                assert(c->kernel);
×
967
                verb = "add";
968
                break;
969

970
        case ACTION_REMOVE:
×
971
                assert(c->version);
×
972
                assert(!c->kernel);
×
973
                assert(!c->initrds);
×
974
                verb = "remove";
975
                break;
976

977
        case ACTION_INSPECT:
978
                verb = "add|remove";
979
                break;
980

981
        default:
×
982
                assert_not_reached();
×
983
        }
984

985
        a = strv_new("dummy-arg", /* to make strv_free() works for this variable. */
×
986
                     verb,
987
                     c->version ?: "KERNEL_VERSION",
988
                     c->entry_dir);
989
        if (!a)
×
990
                return log_oom();
×
991

992
        if (c->action == ACTION_ADD) {
×
993
                r = strv_extend(&a, c->kernel);
×
994
                if (r < 0)
×
995
                        return log_oom();
×
996

997
                r = strv_extend_strv(&a, c->initrds, /* filter_duplicates= */ false);
×
998
                if (r < 0)
×
999
                        return log_oom();
×
1000

1001
        } else if (c->action == ACTION_INSPECT) {
×
1002
                r = strv_extend_many(
×
1003
                                &a,
1004
                                c->kernel ?: "[KERNEL_IMAGE]",
1005
                                "[INITRD...]");
1006
                if (r < 0)
×
1007
                        return log_oom();
×
1008
        }
1009

1010
        c->argv = TAKE_PTR(a);
×
1011
        return 0;
×
1012
}
1013

1014
static int context_build_environment(Context *c) {
×
1015
        _cleanup_strv_free_ char **e = NULL;
×
1016
        int r;
×
1017

1018
        assert(c);
×
1019

1020
        if (c->envp)
×
1021
                return 0;
1022

1023
        r = strv_env_assign_many(&e,
×
1024
                                 "LC_COLLATE",                      SYSTEMD_DEFAULT_LOCALE,
1025
                                 "KERNEL_INSTALL_VERBOSE",          one_zero(arg_verbose),
1026
                                 "KERNEL_INSTALL_IMAGE_TYPE",       kernel_image_type_to_string(c->kernel_image_type),
1027
                                 "KERNEL_INSTALL_MACHINE_ID",       SD_ID128_TO_STRING(c->machine_id),
1028
                                 "KERNEL_INSTALL_ENTRY_TOKEN",      c->entry_token,
1029
                                 "KERNEL_INSTALL_BOOT_ROOT",        c->boot_root,
1030
                                 "KERNEL_INSTALL_LAYOUT",           context_get_layout(c),
1031
                                 "KERNEL_INSTALL_INITRD_GENERATOR", strempty(c->initrd_generator),
1032
                                 "KERNEL_INSTALL_UKI_GENERATOR",    strempty(c->uki_generator),
1033
                                 "KERNEL_INSTALL_BOOT_ENTRY_TYPE",  boot_entry_type_to_string(c->entry_type),
1034
                                 "KERNEL_INSTALL_STAGING_AREA",     c->staging_area);
1035
        if (r < 0)
×
1036
                return log_error_errno(r, "Failed to build environment variables for plugins: %m");
×
1037

1038
        c->envp = TAKE_PTR(e);
×
1039
        return 0;
×
1040
}
1041

1042
static int context_prepare_execution(Context *c) {
×
1043
        int r;
×
1044

1045
        assert(c);
×
1046

1047
        r = context_inspect_kernel(c);
×
1048
        if (r < 0)
×
1049
                return r;
1050

1051
        r = context_ensure_layout(c);
×
1052
        if (r < 0)
×
1053
                return r;
1054

1055
        r = context_set_up_staging_area(c);
×
1056
        if (r < 0)
×
1057
                return r;
1058

1059
        r = context_build_entry_dir(c);
×
1060
        if (r < 0)
×
1061
                return r;
1062

1063
        r = context_build_arguments(c);
×
1064
        if (r < 0)
×
1065
                return r;
1066

1067
        r = context_build_environment(c);
×
1068
        if (r < 0)
×
1069
                return r;
×
1070

1071
        return 0;
1072
}
1073

1074
static int context_execute(Context *c) {
×
1075
        int r, ret;
×
1076

1077
        assert(c);
×
1078

1079
        r = context_make_entry_dir(c);
×
1080
        if (r < 0)
×
1081
                return r;
1082

1083
        if (DEBUG_LOGGING) {
×
1084
                _cleanup_free_ char *x = strv_join_full(c->plugins, "", "\n  ", /* escape_separator= */ false);
×
1085
                log_debug("Using plugins: %s", strna(x));
×
1086

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

1090
                _cleanup_free_ char *z = strv_join(strv_skip(c->argv, 1), " ");
×
1091
                log_debug("Plugin arguments: %s", strna(z));
×
1092
        }
1093

1094
        ret = execute_strv(
×
1095
                        "plugins",
1096
                        c->plugins,
×
1097
                        /* root= */ NULL,
1098
                        USEC_INFINITY,
1099
                        /* callbacks= */ NULL,
1100
                        /* callback_args= */ NULL,
1101
                        c->argv,
1102
                        c->envp,
1103
                        EXEC_DIR_SKIP_REMAINING);
1104

1105
        r = context_remove_entry_dir(c);
×
1106
        if (r < 0)
×
1107
                return r;
×
1108

1109
        /* This returns 0 on success, positive exit code on plugin failure, negative errno on other failures. */
1110
        return ret;
1111
}
1112

1113
static bool bypass(void) {
×
1114
        return should_bypass("KERNEL_INSTALL");
×
1115
}
1116

1117
static int kernel_from_version(const char *version, char **ret_kernel) {
×
1118
        _cleanup_free_ char *vmlinuz = NULL;
×
1119
        int r;
×
1120

1121
        assert(version);
×
1122

1123
        vmlinuz = path_join("/usr/lib/modules/", version, "/vmlinuz");
×
1124
        if (!vmlinuz)
×
1125
                return log_oom();
×
1126

1127
        r = access_nofollow(vmlinuz, F_OK);
×
1128
        if (r == -ENOENT)
×
1129
                return log_error_errno(r, "Kernel image not installed to '%s', specify kernel kernel image path explicitly.", vmlinuz);
×
1130
        if (r < 0)
×
1131
                return log_error_errno(r, "Failed to determine if kernel image is installed to '%s': %m", vmlinuz);
×
1132

1133
        *ret_kernel = TAKE_PTR(vmlinuz);
×
1134
        return 0;
×
1135
}
1136

1137
static int do_add(
×
1138
                Context *c,
1139
                const char *version,
1140
                const char *kernel,
1141
                char **initrds) {
1142

1143
        int r;
×
1144

1145
        assert(c);
×
1146

1147
        struct utsname un;
×
1148
        if (!version) {
×
1149
                assert_se(uname(&un) >= 0);
×
1150
                version = un.release;
1151
        }
1152

1153
        _cleanup_free_ char *vmlinuz = NULL;
×
1154
        if (!kernel) {
×
1155
                r = kernel_from_version(version, &vmlinuz);
×
1156
                if (r < 0)
×
1157
                        return r;
1158

1159
                kernel = vmlinuz;
×
1160
        }
1161

1162
        r = context_set_version(c, version);
×
1163
        if (r < 0)
×
1164
                return r;
1165

1166
        r = context_set_kernel(c, kernel);
×
1167
        if (r < 0)
×
1168
                return r;
1169

1170
        r = context_set_initrds(c, initrds);
×
1171
        if (r < 0)
×
1172
                return r;
1173

1174
        r = context_prepare_execution(c);
×
1175
        if (r < 0)
×
1176
                return r;
1177

1178
        return context_execute(c);
×
1179
}
1180

1181
static int verb_add(int argc, char *argv[], uintptr_t _data, void *userdata) {
×
1182
        const char *version, *kernel;
×
1183
        char **initrds;
×
1184
        int r;
×
1185

1186
        assert(argv);
×
1187

1188
        if (arg_root)
×
1189
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "'add' does not support --root= or --image=.");
×
1190

1191
        if (bypass())
×
1192
                return 0;
1193

1194
        _cleanup_(context_done) Context c = CONTEXT_NULL;
×
1195
        r = context_from_cmdline(&c, ACTION_ADD);
×
1196
        if (r < 0)
×
1197
                return r;
1198

1199
        /* We use the same order of arguments that "inspect" introduced, i.e. if only on argument is
1200
         * specified we take it as the kernel path, not the version, i.e. it's the first argument that is
1201
         * optional, not the 2nd. */
1202
        version = argc > 2 ? empty_or_dash_to_null(argv[1]) : NULL;
×
1203
        kernel = argc > 2 ? empty_or_dash_to_null(argv[2]) :
×
1204
                (argc > 1 ? empty_or_dash_to_null(argv[1]) : NULL);
×
1205
        initrds = strv_skip(argv, 3);
×
1206

1207
        return do_add(&c, version, kernel, initrds);
×
1208
}
1209

1210
static int verb_add_all(int argc, char *argv[], uintptr_t _data, void *userdata) {
×
1211
        _cleanup_close_ int fd = -EBADF;
×
1212
        size_t n = 0;
×
1213
        int ret = 0, r;
×
1214

1215
        assert(argv);
×
1216

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

1220
        if (bypass())
×
1221
                return 0;
1222

1223
        _cleanup_(context_done) Context c = CONTEXT_NULL;
×
1224
        r = context_from_cmdline(&c, ACTION_ADD);
×
1225
        if (r < 0)
×
1226
                return r;
1227

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

1232
        _cleanup_free_ DirectoryEntries *de = NULL;
×
1233
        r = readdir_all(fd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT, &de);
×
1234
        if (r < 0)
×
1235
                return log_error_errno(r, "Failed to numerate /usr/lib/modules/ contents: %m");
×
1236

1237
        FOREACH_ARRAY(d, de->entries, de->n_entries) {
×
1238
                r = dirent_ensure_type(fd, *d);
×
1239
                if (r < 0) {
×
1240
                        if (r != -ENOENT) /* don't log if just gone by now */
×
1241
                                log_debug_errno(r, "Failed to check if '%s/usr/lib/modules/%s' is a directory, ignoring: %m", strempty(arg_root), (*d)->d_name);
×
1242
                        continue;
×
1243
                }
1244

1245
                if ((*d)->d_type != DT_DIR)
×
1246
                        continue;
×
1247

1248
                _cleanup_free_ char *fn = path_join((*d)->d_name, "vmlinuz");
×
1249
                if (!fn)
×
1250
                        return log_oom();
×
1251

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

1256
                        log_notice("Not adding version '%s', because kernel image not found.", (*d)->d_name);
×
1257
                        continue;
×
1258
                }
1259

1260
                _cleanup_(context_done) Context copy = CONTEXT_NULL;
×
1261

1262
                r = context_copy(&c, &copy);
×
1263
                if (r < 0)
×
1264
                        return log_error_errno(r, "Failed to copy execution context: %m");
×
1265

1266
                /* do_add() will look up the path in the correct root directory so we don't need to prefix it
1267
                 * with arg_root here. */
1268
                _cleanup_free_ char *full = path_join("/usr/lib/modules/", fn);
×
1269
                if (!full)
×
1270
                        return log_oom();
×
1271

1272
                r = do_add(&copy,
×
1273
                           /* version= */ (*d)->d_name,
×
1274
                           /* kernel= */ full,
1275
                           /* initrds= */ NULL);
1276
                if (r == 0)
×
1277
                        n++;
×
1278
                else if (ret == 0)
×
1279
                        ret = r;
×
1280
        }
1281

1282
        if (n > 0)
×
1283
                log_debug("Installed %zu kernel(s).", n);
×
1284
        else if (ret == 0)
×
1285
                ret = log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No kernels to install found.");
×
1286

1287
        return ret;
1288
}
1289

1290
static int run_as_installkernel(int argc, char *argv[]) {
×
1291
        /* kernel's install.sh invokes us as
1292
         *   /sbin/installkernel <version> <vmlinuz> <map> <installation-dir>
1293
         * We ignore the last two arguments. */
1294
        if (optind + 2 > argc)
×
1295
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "'installkernel' command requires at least two arguments.");
×
1296

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

1300
static int verb_remove(int argc, char *argv[], uintptr_t _data, void *userdata) {
×
1301
        int r;
×
1302

1303
        assert(argc >= 2);
×
1304
        assert(argv);
×
1305

1306
        if (arg_root)
×
1307
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "'remove' does not support --root= or --image=.");
×
1308

1309
        if (argc > 2)
×
1310
                log_debug("Too many arguments specified. 'kernel-install remove' takes only kernel version. "
×
1311
                          "Ignoring residual arguments.");
1312

1313
        if (bypass())
×
1314
                return 0;
1315

1316
        _cleanup_(context_done) Context c = CONTEXT_NULL;
×
1317
        r = context_from_cmdline(&c, ACTION_REMOVE);
×
1318
        if (r < 0)
×
1319
                return r;
1320

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

1325
        r = context_set_version(&c, argv[1]);
×
1326
        if (r < 0)
×
1327
                return r;
1328

1329
        r = context_prepare_execution(&c);
×
1330
        if (r < 0)
×
1331
                return r;
1332

1333
        return context_execute(&c);
×
1334
}
1335

1336
static int verb_inspect(int argc, char *argv[], uintptr_t _data, void *userdata) {
×
1337
        _cleanup_(table_unrefp) Table *t = NULL;
×
1338
        _cleanup_free_ char *vmlinuz = NULL;
×
1339
        const char *version, *kernel;
×
1340
        char **initrds;
×
1341
        struct utsname un;
×
1342
        int r;
×
1343

1344
        _cleanup_(context_done) Context c = CONTEXT_NULL;
×
1345
        r = context_from_cmdline(&c, ACTION_INSPECT);
×
1346
        if (r < 0)
×
1347
                return r;
1348

1349
        /* When only a single parameter is specified 'inspect' it's the kernel image path, and not the kernel
1350
         * version. i.e. it's the first argument that is optional, not the 2nd. That's a bit unfortunate, but
1351
         * we keep the behaviour for compatibility. If users want to specify only the version (and have the
1352
         * kernel image path derived automatically), then they may specify an empty string or "dash" as
1353
         * kernel image path. */
1354
        version = argc > 2 ? empty_or_dash_to_null(argv[1]) : NULL;
×
1355
        kernel = argc > 2 ? empty_or_dash_to_null(argv[2]) :
×
1356
                (argc > 1 ? empty_or_dash_to_null(argv[1]) : NULL);
×
1357
        initrds = strv_skip(argv, 3);
×
1358

1359
        if (!version && !arg_root) {
×
1360
                assert_se(uname(&un) >= 0);
×
1361
                version = un.release;
1362
        }
1363

1364
        if (!kernel && version) {
×
1365
                r = kernel_from_version(version, &vmlinuz);
×
1366
                if (r < 0)
×
1367
                        return r;
1368

1369
                kernel = vmlinuz;
×
1370
        }
1371

1372
        r = context_set_version(&c, version);
×
1373
        if (r < 0)
×
1374
                return r;
1375

1376
        r = context_set_kernel(&c, kernel);
×
1377
        if (r < 0)
×
1378
                return r;
1379

1380
        r = context_set_initrds(&c, initrds);
×
1381
        if (r < 0)
×
1382
                return r;
1383

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

1388
        t = table_new_vertical();
×
1389
        if (!t)
×
1390
                return log_oom();
×
1391

1392
        r = table_add_many(t,
×
1393
                           TABLE_FIELD, "Machine ID",
1394
                           TABLE_ID128, c.machine_id,
1395
                           TABLE_FIELD, "Kernel Image Type",
1396
                           TABLE_STRING, kernel_image_type_to_string(c.kernel_image_type),
1397
                           TABLE_FIELD, "Layout",
1398
                           TABLE_STRING, context_get_layout(&c),
1399
                           TABLE_FIELD, "Boot Root",
1400
                           TABLE_STRING, c.boot_root,
1401
                           TABLE_FIELD, "Entry Token Type",
1402
                           TABLE_STRING, boot_entry_token_type_to_string(c.entry_token_type),
1403
                           TABLE_FIELD, "Entry Token",
1404
                           TABLE_STRING, c.entry_token,
1405
                           TABLE_FIELD, "Entry Directory",
1406
                           TABLE_STRING, c.entry_dir,
1407
                           TABLE_FIELD, "Kernel Version",
1408
                           TABLE_VERSION, c.version,
1409
                           TABLE_FIELD, "Kernel",
1410
                           TABLE_STRING, c.kernel,
1411
                           TABLE_FIELD, "Initrds",
1412
                           TABLE_STRV, c.initrds,
1413
                           TABLE_FIELD, "Initrd Generator",
1414
                           TABLE_STRING, c.initrd_generator,
1415
                           TABLE_FIELD, "UKI Generator",
1416
                           TABLE_STRING, c.uki_generator,
1417
                           TABLE_FIELD, "Plugins",
1418
                           TABLE_STRV, c.plugins,
1419
                           TABLE_FIELD, "Plugin Environment",
1420
                           TABLE_STRV, c.envp);
1421
        if (r < 0)
×
1422
                return table_log_add_error(r);
×
1423

1424
        if (!sd_json_format_enabled(arg_json_format_flags)) {
×
1425
                r = table_add_many(t,
×
1426
                                   TABLE_FIELD, "Plugin Arguments",
1427
                                   TABLE_STRV, strv_skip(c.argv, 1));
1428
                if (r < 0)
×
1429
                        return table_log_add_error(r);
×
1430
        }
1431

1432
        table_set_ersatz_string(t, TABLE_ERSATZ_UNSET);
×
1433

1434
        for (size_t row = 1; row < table_get_rows(t); row++) {
×
1435
                _cleanup_free_ char *name = NULL;
×
1436

1437
                name = strdup(table_get_at(t, row, 0));
×
1438
                if (!name)
×
1439
                        return log_oom();
×
1440

1441
                r = table_set_json_field_name(t, row - 1, delete_chars(name, " "));
×
1442
                if (r < 0)
×
1443
                        return log_error_errno(r, "Failed to set JSON field name: %m");
×
1444
        }
1445

1446
        return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, /* show_header= */ false);
×
1447
}
1448

1449
static int verb_list(int argc, char *argv[], uintptr_t _data, void *userdata) {
×
1450
        _cleanup_close_ int fd = -EBADF;
×
1451
        int r;
×
1452

1453
        _cleanup_(context_done) Context c = CONTEXT_NULL;
×
1454
        r = context_from_cmdline(&c, ACTION_INSPECT);
×
1455
        if (r < 0)
×
1456
                return r;
1457

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

1462
        _cleanup_free_ DirectoryEntries *de = NULL;
×
1463
        r = readdir_all(fd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT, &de);
×
1464
        if (r < 0)
×
1465
                return log_error_errno(r, "Failed to numerate /usr/lib/modules/ contents: %m");
×
1466

1467
        _cleanup_(table_unrefp) Table *table = NULL;
×
1468
        table = table_new("version", "has kernel", "path");
×
1469
        if (!table)
×
1470
                return log_oom();
×
1471

1472
        table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
×
1473
        table_set_align_percent(table, table_get_cell(table, 0, 1), 100);
×
1474
        (void) table_set_sort(table, (size_t) 0);
×
1475

1476
        FOREACH_ARRAY(d, de->entries, de->n_entries) {
×
1477
                _cleanup_free_ char *j = path_join("/usr/lib/modules/", (*d)->d_name);
×
1478
                if (!j)
×
1479
                        return log_oom();
×
1480

1481
                r = dirent_ensure_type(fd, *d);
×
1482
                if (r < 0) {
×
1483
                        if (r != -ENOENT) /* don't log if just gone by now */
×
1484
                                log_debug_errno(r, "Failed to check if '%s/%s' is a directory, ignoring: %m", strempty(arg_root), j);
×
1485
                        continue;
×
1486
                }
1487

1488
                if ((*d)->d_type != DT_DIR)
×
1489
                        continue;
×
1490

1491
                _cleanup_free_ char *fn = path_join((*d)->d_name, "vmlinuz");
×
1492
                if (!fn)
×
1493
                        return log_oom();
×
1494

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

1500
                        exists = false;
1501
                } else
1502
                        exists = true;
1503

1504
                r = table_add_many(table,
×
1505
                                   TABLE_VERSION, (*d)->d_name,
1506
                                   TABLE_BOOLEAN_CHECKMARK, exists,
1507
                                   TABLE_SET_COLOR, ansi_highlight_green_red(exists),
1508
                                   TABLE_PATH, j);
1509
                if (r < 0)
×
1510
                        return table_log_add_error(r);
×
1511
        }
1512

1513
        return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
×
1514
}
1515

1516
static int help(void) {
×
1517
        _cleanup_free_ char *link = NULL;
×
1518
        int r;
×
1519

1520
        r = terminal_urlify_man("kernel-install", "8", &link);
×
1521
        if (r < 0)
×
1522
                return log_oom();
×
1523

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

1565
        return 0;
1566
}
1567

1568
static int parse_argv(int argc, char *argv[]) {
×
1569
        enum {
×
1570
                ARG_VERSION = 0x100,
1571
                ARG_NO_LEGEND,
1572
                ARG_ESP_PATH,
1573
                ARG_BOOT_PATH,
1574
                ARG_MAKE_ENTRY_DIRECTORY,
1575
                ARG_ENTRY_TOKEN,
1576
                ARG_NO_PAGER,
1577
                ARG_JSON,
1578
                ARG_ROOT,
1579
                ARG_IMAGE,
1580
                ARG_IMAGE_POLICY,
1581
                ARG_BOOT_ENTRY_TYPE,
1582
        };
1583
        static const struct option options[] = {
×
1584
                { "help",                 no_argument,       NULL, 'h'                      },
1585
                { "version",              no_argument,       NULL, ARG_VERSION              },
1586
                { "verbose",              no_argument,       NULL, 'v'                      },
1587
                { "esp-path",             required_argument, NULL, ARG_ESP_PATH             },
1588
                { "boot-path",            required_argument, NULL, ARG_BOOT_PATH            },
1589
                { "make-entry-directory", required_argument, NULL, ARG_MAKE_ENTRY_DIRECTORY },
1590
                { "entry-token",          required_argument, NULL, ARG_ENTRY_TOKEN          },
1591
                { "no-pager",             no_argument,       NULL, ARG_NO_PAGER             },
1592
                { "json",                 required_argument, NULL, ARG_JSON                 },
1593
                { "root",                 required_argument, NULL, ARG_ROOT                 },
1594
                { "image",                required_argument, NULL, ARG_IMAGE                },
1595
                { "image-policy",         required_argument, NULL, ARG_IMAGE_POLICY         },
1596
                { "no-legend",            no_argument,       NULL, ARG_NO_LEGEND            },
1597
                { "entry-type",           required_argument, NULL, ARG_BOOT_ENTRY_TYPE      },
1598
                {}
1599
        };
1600
        int t, r;
×
1601

1602
        assert(argc >= 0);
×
1603
        assert(argv);
×
1604

1605
        while ((t = getopt_long(argc, argv, "hv", options, NULL)) >= 0)
×
1606
                switch (t) {
×
1607
                case 'h':
×
1608
                        return help();
×
1609

1610
                case ARG_VERSION:
×
1611
                        return version();
×
1612

1613
                case ARG_NO_LEGEND:
×
1614
                        arg_legend = false;
×
1615
                        break;
×
1616

1617
                case 'v':
×
1618
                        log_set_max_level(LOG_DEBUG);
×
1619
                        arg_verbose = true;
×
1620
                        break;
×
1621

1622
                case ARG_ESP_PATH:
×
1623
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_esp_path);
×
1624
                        if (r < 0)
×
1625
                                return log_oom();
×
1626
                        break;
1627

1628
                case ARG_BOOT_PATH:
×
1629
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_xbootldr_path);
×
1630
                        if (r < 0)
×
1631
                                return log_oom();
×
1632
                        break;
1633

1634
                case ARG_MAKE_ENTRY_DIRECTORY:
×
1635
                        if (streq(optarg, "auto"))
×
1636
                                arg_make_entry_directory = -1;
×
1637
                        else {
1638
                                r = parse_boolean_argument("--make-entry-directory=", optarg, NULL);
×
1639
                                if (r < 0)
×
1640
                                        return r;
1641

1642
                                arg_make_entry_directory = r;
×
1643
                        }
1644
                        break;
1645

1646
                case ARG_ENTRY_TOKEN:
×
1647
                        r = parse_boot_entry_token_type(optarg, &arg_entry_token_type, &arg_entry_token);
×
1648
                        if (r < 0)
×
1649
                                return r;
1650
                        break;
1651

1652
                case ARG_NO_PAGER:
×
1653
                        arg_pager_flags |= PAGER_DISABLE;
×
1654
                        break;
×
1655

1656
                case ARG_JSON:
×
1657
                        r = parse_json_argument(optarg, &arg_json_format_flags);
×
1658
                        if (r <= 0)
×
1659
                                return r;
1660
                        break;
1661

1662
                case ARG_ROOT:
×
1663
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_root);
×
1664
                        if (r < 0)
×
1665
                                return r;
1666
                        break;
1667

1668
                case ARG_IMAGE:
×
1669
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
×
1670
                        if (r < 0)
×
1671
                                return r;
1672
                        break;
1673

1674
                case ARG_IMAGE_POLICY:
×
1675
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
1676
                        if (r < 0)
×
1677
                                return r;
1678
                        break;
1679

1680
                case ARG_BOOT_ENTRY_TYPE: {
×
1681
                        if (isempty(optarg) || streq(optarg, "all")) {
×
1682
                                arg_boot_entry_type = _BOOT_ENTRY_TYPE_INVALID;
×
1683
                                break;
×
1684
                        }
1685

1686
                        BootEntryType e = boot_entry_type_from_string(optarg);
×
1687
                        if (e < 0)
×
1688
                                return log_error_errno(e, "Invalid entry type: %s", optarg);
×
1689
                        arg_boot_entry_type = e;
×
1690
                        break;
×
1691
                }
1692

1693
                case '?':
1694
                        return -EINVAL;
1695

1696
                default:
×
1697
                        assert_not_reached();
×
1698
                }
1699

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

1703
        return 1;
1704
}
1705

1706
static int kernel_install_main(int argc, char *argv[]) {
×
1707
        static const Verb verbs[] = {
×
1708
                { "add",     1, VERB_ANY, 0,            verb_add     },
1709
                { "add-all", 1, 1,        0,            verb_add_all },
1710
                { "remove",  2, VERB_ANY, 0,            verb_remove  },
1711
                { "inspect", 1, VERB_ANY, VERB_DEFAULT, verb_inspect },
1712
                { "list",    1, 1,        0,            verb_list    },
1713
                {}
1714
        };
1715

1716
        return dispatch_verb(argc, argv, verbs, /* userdata= */ NULL);
×
1717
}
1718

1719
static int run(int argc, char* argv[]) {
×
1720
        int r;
×
1721

1722
        log_setup();
×
1723

1724
        r = parse_argv(argc, argv);
×
1725
        if (r <= 0)
×
1726
                return r;
×
1727

1728
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
×
1729
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
×
1730
        if (arg_image) {
×
1731
                assert(!arg_root);
×
1732

1733
                r = mount_image_privately_interactively(
×
1734
                                arg_image,
1735
                                arg_image_policy,
1736
                                DISSECT_IMAGE_GENERIC_ROOT |
1737
                                DISSECT_IMAGE_REQUIRE_ROOT |
1738
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
1739
                                DISSECT_IMAGE_VALIDATE_OS |
1740
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
1741
                                &mounted_dir,
1742
                                /* ret_dir_fd= */ NULL,
1743
                                &loop_device);
1744
                if (r < 0)
×
1745
                        return r;
1746

1747
                arg_root = strdup(mounted_dir);
×
1748
                if (!arg_root)
×
1749
                        return log_oom();
×
1750
        }
1751

1752
        if (invoked_as(argv, "installkernel"))
×
1753
                return run_as_installkernel(argc, argv);
×
1754

1755
        return kernel_install_main(argc, argv);
×
1756
}
1757

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

© 2026 Coveralls, Inc