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

systemd / systemd / 21846209963

09 Feb 2026 03:52PM UTC coverage: 72.697% (-0.02%) from 72.716%
21846209963

push

github

daandemeyer
meson: guard symlinks in sysconfdir behind install_sysconfidr

Symlinks to files inside sysconfdir are now only installed if
ìnstall_sysconfdir=true (which is the default).

If sshconfdir,sshdconfdir,shellprofiledir are not inside sysconfdir and
install_sysconfidr=false, these symlinks are still installed to the
configured directory.

311951 of 429113 relevant lines covered (72.7%)

1156102.48 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);
×
156

157
        _cleanup_(context_done) Context copy = (Context) {
×
158
                .rfd = AT_FDCWD,
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_uuid= */ NULL,
575
                        /* ret_devid= */ NULL);
576
        if (r == -ENOKEY) {
×
577
                log_debug_errno(r, "Couldn't find an XBOOTLDR partition.");
×
578
                return 0;
×
579
        }
580
        if (r == -EACCES && geteuid() != 0)
×
581
                return log_error_errno(r, "Failed to determine XBOOTLDR partition: %m");
×
582
        if (r < 0)
×
583
                return r;
584

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

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

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

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

614
        log_debug("Using EFI System Partition at %s as $BOOT_ROOT.", c->boot_root);
×
615
        return 1; /* found */
616
}
617

618
static int context_ensure_boot_root(Context *c) {
×
619
        int r;
×
620

621
        assert(c);
×
622

623
        /* If BOOT_ROOT is specified via environment or install.conf, then use it. */
624
        if (c->boot_root)
×
625
                return 0;
626

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

632
        /* Otherwise, use EFI system partition, if mounted. */
633
        r = context_acquire_esp(c);
×
634
        if (r != 0)
×
635
                return r;
636

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

648
        log_debug("KERNEL_INSTALL_BOOT_ROOT autodetection yielded no candidates, using \"%s\".", c->boot_root);
×
649
        return 0;
650
}
651

652
static int context_ensure_entry_token(Context *c) {
×
653
        int r;
×
654

655
        assert(c);
×
656

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

662
        r = boot_entry_token_ensure_at(
×
663
                        c->rfd,
664
                        c->conf_root,
×
665
                        c->machine_id,
666
                        c->machine_id_is_random,
×
667
                        &c->entry_token_type,
668
                        &c->entry_token);
669
        if (r < 0)
×
670
                return r;
671

672
        log_debug("Using entry token: %s", c->entry_token);
×
673
        return 0;
674
}
675

676
static int context_load_plugins(Context *c) {
×
677
        int r;
×
678

679
        assert(c);
×
680

681
        if (c->plugins)
×
682
                return 0;
×
683

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

693
        return 0;
694
}
695

696
static int context_setup(Context *c) {
×
697
        int r;
×
698

699
        assert(c);
×
700

701
        if (c->kernel_image_type < 0)
×
702
                c->kernel_image_type = KERNEL_IMAGE_TYPE_UNKNOWN;
×
703
        if (c->entry_token_type < 0)
×
704
                c->entry_token_type = BOOT_ENTRY_TOKEN_AUTO;
×
705

706
        r = context_open_root(c);
×
707
        if (r < 0)
×
708
                return r;
709

710
        r = context_load_environment(c);
×
711
        if (r < 0)
×
712
                return r;
713

714
        r = context_load_install_conf(c);
×
715
        if (r < 0)
×
716
                return r;
717

718
        r = context_load_machine_info(c);
×
719
        if (r < 0)
×
720
                return r;
721

722
        r = context_ensure_machine_id(c);
×
723
        if (r < 0)
×
724
                return r;
725

726
        r = context_ensure_boot_root(c);
×
727
        if (r < 0)
×
728
                return r;
729

730
        r = context_ensure_entry_token(c);
×
731
        if (r < 0)
×
732
                return r;
733

734
        r = context_load_plugins(c);
×
735
        if (r < 0)
×
736
                return r;
×
737

738
        return 0;
739
}
740

741
static int context_from_cmdline(Context *c, Action action) {
×
742
        int r;
×
743

744
        assert(c);
×
745

746
        c->action = action;
×
747

748
        c->entry_type = arg_boot_entry_type;
×
749

750
        r = free_and_strdup_warn(&c->entry_token, arg_entry_token);
×
751
        if (r < 0)
×
752
                return r;
753

754
        c->entry_token_type = arg_entry_token_type;
×
755

756
        return context_setup(c);
×
757
}
758

759
static int context_inspect_kernel(Context *c) {
×
760
        assert(c);
×
761

762
        if (!c->kernel)
×
763
                return 0;
764

765
        return inspect_kernel(c->rfd, c->kernel, &c->kernel_image_type, NULL, NULL, NULL);
×
766
}
767

768
static int context_ensure_layout(Context *c) {
×
769
        int r;
×
770

771
        assert(c);
×
772
        assert(c->boot_root);
×
773
        assert(c->entry_token);
×
774

775
        if (c->layout >= 0 && c->layout != LAYOUT_AUTO)
×
776
                return 0;
×
777

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

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

788
        _cleanup_free_ char *srel_path = path_join(c->boot_root, "loader/entries.srel");
×
789
        if (!srel_path)
×
790
                return log_oom();
×
791

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

800
                r = read_line(f, LONG_LINE_MAX, &srel);
×
801
                if (r < 0)
×
802
                        return log_error_errno(r, "Failed to read %s: %m", srel_path);
×
803

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

815
                log_debug("%s with '%s' found, using layout=%s.", srel_path, srel, layout_to_string(c->layout));
×
816
                return 0;
×
817
        }
818

819
        _cleanup_free_ char *entry_token_path = path_join(c->boot_root, c->entry_token);
×
820
        if (!entry_token_path)
×
821
                return log_oom();
×
822

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

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

844
static int context_set_up_staging_area(Context *c) {
×
845
        int r;
×
846

847
        assert(c);
×
848

849
        if (c->staging_area)
×
850
                return 0;
×
851

852
        const char *d;
×
853
        r = var_tmp_dir(&d);
×
854
        if (r < 0)
×
855
                return log_error_errno(r, "Failed to determine temporary directory location: %m");
×
856

857
        _cleanup_free_ char *template = path_join(d, "kernel-install.staging.XXXXXX");
×
858
        if (!template)
×
859
                return log_oom();
×
860

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

870
        return 0;
871
}
872

873
static int context_build_entry_dir(Context *c) {
×
874
        assert(c);
×
875
        assert(c->boot_root);
×
876
        assert(c->entry_token);
×
877
        assert(c->version || c->action == ACTION_INSPECT);
×
878

879
        if (c->entry_dir)
×
880
                return 0;
881

882
        c->entry_dir = path_join(c->boot_root, c->entry_token, c->version ?: "KERNEL_VERSION");
×
883
        if (!c->entry_dir)
×
884
                return log_oom();
×
885

886
        log_debug("Using ENTRY_DIR=%s", c->entry_dir);
×
887
        return 0;
888
}
889

890
static bool context_should_make_entry_dir(Context *c) {
×
891
        assert(c);
×
892

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

896
        if (arg_make_entry_directory < 0)
×
897
                return c->layout == LAYOUT_BLS;
×
898

899
        return arg_make_entry_directory;
×
900
}
901

902
static int context_make_entry_dir(Context *c) {
×
903
        _cleanup_close_ int fd = -EBADF;
×
904

905
        assert(c);
×
906
        assert(c->entry_dir);
×
907

908
        if (c->action != ACTION_ADD)
×
909
                return 0;
910

911
        if (!context_should_make_entry_dir(c))
×
912
                return 0;
913

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

920
        return 0;
921
}
922

923
static int context_remove_entry_dir(Context *c) {
×
924
        _cleanup_free_ char *p = NULL;
×
925
        _cleanup_close_ int fd = -EBADF;
×
926
        struct stat st;
×
927
        int r;
×
928

929
        assert(c);
×
930
        assert(c->entry_dir);
×
931

932
        if (c->action != ACTION_REMOVE)
×
933
                return 0;
934

935
        if (!context_should_make_entry_dir(c))
×
936
                return 0;
937

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

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

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

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

956
        return 0;
957
}
958

959
static int context_build_arguments(Context *c) {
×
960
        _cleanup_strv_free_ char **a = NULL;
×
961
        const char *verb;
×
962
        int r;
×
963

964
        assert(c);
×
965
        assert(c->entry_dir);
×
966

967
        if (c->argv)
×
968
                return 0;
969

970
        switch (c->action) {
×
971
        case ACTION_ADD:
×
972
                assert(c->version);
×
973
                assert(c->kernel);
×
974
                verb = "add";
975
                break;
976

977
        case ACTION_REMOVE:
×
978
                assert(c->version);
×
979
                assert(!c->kernel);
×
980
                assert(!c->initrds);
×
981
                verb = "remove";
982
                break;
983

984
        case ACTION_INSPECT:
985
                verb = "add|remove";
986
                break;
987

988
        default:
×
989
                assert_not_reached();
×
990
        }
991

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

999
        if (c->action == ACTION_ADD) {
×
1000
                r = strv_extend(&a, c->kernel);
×
1001
                if (r < 0)
×
1002
                        return log_oom();
×
1003

1004
                r = strv_extend_strv(&a, c->initrds, /* filter_duplicates= */ false);
×
1005
                if (r < 0)
×
1006
                        return log_oom();
×
1007

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

1017
        c->argv = TAKE_PTR(a);
×
1018
        return 0;
×
1019
}
1020

1021
static int context_build_environment(Context *c) {
×
1022
        _cleanup_strv_free_ char **e = NULL;
×
1023
        int r;
×
1024

1025
        assert(c);
×
1026

1027
        if (c->envp)
×
1028
                return 0;
1029

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

1045
        c->envp = TAKE_PTR(e);
×
1046
        return 0;
×
1047
}
1048

1049
static int context_prepare_execution(Context *c) {
×
1050
        int r;
×
1051

1052
        assert(c);
×
1053

1054
        r = context_inspect_kernel(c);
×
1055
        if (r < 0)
×
1056
                return r;
1057

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

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

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

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

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

1078
        return 0;
1079
}
1080

1081
static int context_execute(Context *c) {
×
1082
        int r, ret;
×
1083

1084
        assert(c);
×
1085

1086
        r = context_make_entry_dir(c);
×
1087
        if (r < 0)
×
1088
                return r;
1089

1090
        if (DEBUG_LOGGING) {
×
1091
                _cleanup_free_ char *x = strv_join_full(c->plugins, "", "\n  ", /* escape_separator= */ false);
×
1092
                log_debug("Using plugins: %s", strna(x));
×
1093

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

1097
                _cleanup_free_ char *z = strv_join(strv_skip(c->argv, 1), " ");
×
1098
                log_debug("Plugin arguments: %s", strna(z));
×
1099
        }
1100

1101
        ret = execute_strv(
×
1102
                        "plugins",
1103
                        c->plugins,
×
1104
                        /* root= */ NULL,
1105
                        USEC_INFINITY,
1106
                        /* callbacks= */ NULL,
1107
                        /* callback_args= */ NULL,
1108
                        c->argv,
1109
                        c->envp,
1110
                        EXEC_DIR_SKIP_REMAINING);
1111

1112
        r = context_remove_entry_dir(c);
×
1113
        if (r < 0)
×
1114
                return r;
×
1115

1116
        /* This returns 0 on success, positive exit code on plugin failure, negative errno on other failures. */
1117
        return ret;
1118
}
1119

1120
static bool bypass(void) {
×
1121
        return should_bypass("KERNEL_INSTALL");
×
1122
}
1123

1124
static int kernel_from_version(const char *version, char **ret_kernel) {
×
1125
        _cleanup_free_ char *vmlinuz = NULL;
×
1126
        int r;
×
1127

1128
        assert(version);
×
1129

1130
        vmlinuz = path_join("/usr/lib/modules/", version, "/vmlinuz");
×
1131
        if (!vmlinuz)
×
1132
                return log_oom();
×
1133

1134
        r = access_nofollow(vmlinuz, F_OK);
×
1135
        if (r == -ENOENT)
×
1136
                return log_error_errno(r, "Kernel image not installed to '%s', specify kernel kernel image path explicitly.", vmlinuz);
×
1137
        if (r < 0)
×
1138
                return log_error_errno(r, "Failed to determine if kernel image is installed to '%s': %m", vmlinuz);
×
1139

1140
        *ret_kernel = TAKE_PTR(vmlinuz);
×
1141
        return 0;
×
1142
}
1143

1144
static int do_add(
×
1145
                Context *c,
1146
                const char *version,
1147
                const char *kernel,
1148
                char **initrds) {
1149

1150
        int r;
×
1151

1152
        assert(c);
×
1153

1154
        struct utsname un;
×
1155
        if (!version) {
×
1156
                assert_se(uname(&un) >= 0);
×
1157
                version = un.release;
1158
        }
1159

1160
        _cleanup_free_ char *vmlinuz = NULL;
×
1161
        if (!kernel) {
×
1162
                r = kernel_from_version(version, &vmlinuz);
×
1163
                if (r < 0)
×
1164
                        return r;
1165

1166
                kernel = vmlinuz;
×
1167
        }
1168

1169
        r = context_set_version(c, version);
×
1170
        if (r < 0)
×
1171
                return r;
1172

1173
        r = context_set_kernel(c, kernel);
×
1174
        if (r < 0)
×
1175
                return r;
1176

1177
        r = context_set_initrds(c, initrds);
×
1178
        if (r < 0)
×
1179
                return r;
1180

1181
        r = context_prepare_execution(c);
×
1182
        if (r < 0)
×
1183
                return r;
1184

1185
        return context_execute(c);
×
1186
}
1187

1188
static int verb_add(int argc, char *argv[], void *userdata) {
×
1189
        const char *version, *kernel;
×
1190
        char **initrds;
×
1191
        int r;
×
1192

1193
        assert(argv);
×
1194

1195
        if (arg_root)
×
1196
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "'add' does not support --root= or --image=.");
×
1197

1198
        if (bypass())
×
1199
                return 0;
1200

1201
        _cleanup_(context_done) Context c = CONTEXT_NULL;
×
1202
        r = context_from_cmdline(&c, ACTION_ADD);
×
1203
        if (r < 0)
×
1204
                return r;
1205

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

1214
        return do_add(&c, version, kernel, initrds);
×
1215
}
1216

1217
static int verb_add_all(int argc, char *argv[], void *userdata) {
×
1218
        _cleanup_close_ int fd = -EBADF;
×
1219
        size_t n = 0;
×
1220
        int ret = 0, r;
×
1221

1222
        assert(argv);
×
1223

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

1227
        if (bypass())
×
1228
                return 0;
1229

1230
        _cleanup_(context_done) Context c = CONTEXT_NULL;
×
1231
        r = context_from_cmdline(&c, ACTION_ADD);
×
1232
        if (r < 0)
×
1233
                return r;
1234

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

1239
        _cleanup_free_ DirectoryEntries *de = NULL;
×
1240
        r = readdir_all(fd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT, &de);
×
1241
        if (r < 0)
×
1242
                return log_error_errno(r, "Failed to numerate /usr/lib/modules/ contents: %m");
×
1243

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

1252
                if ((*d)->d_type != DT_DIR)
×
1253
                        continue;
×
1254

1255
                _cleanup_free_ char *fn = path_join((*d)->d_name, "vmlinuz");
×
1256
                if (!fn)
×
1257
                        return log_oom();
×
1258

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

1263
                        log_notice("Not adding version '%s', because kernel image not found.", (*d)->d_name);
×
1264
                        continue;
×
1265
                }
1266

1267
                _cleanup_(context_done) Context copy = CONTEXT_NULL;
×
1268

1269
                r = context_copy(&c, &copy);
×
1270
                if (r < 0)
×
1271
                        return log_error_errno(r, "Failed to copy execution context: %m");
×
1272

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

1279
                r = do_add(&copy,
×
1280
                           /* version= */ (*d)->d_name,
×
1281
                           /* kernel= */ full,
1282
                           /* initrds= */ NULL);
1283
                if (r == 0)
×
1284
                        n++;
×
1285
                else if (ret == 0)
×
1286
                        ret = r;
×
1287
        }
1288

1289
        if (n > 0)
×
1290
                log_debug("Installed %zu kernel(s).", n);
×
1291
        else if (ret == 0)
×
1292
                ret = log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No kernels to install found.");
×
1293

1294
        return ret;
1295
}
1296

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

1304
        return verb_add(3, STRV_MAKE("add", argv[optind], argv[optind+1]), /* userdata= */ NULL);
×
1305
}
1306

1307
static int verb_remove(int argc, char *argv[], void *userdata) {
×
1308
        int r;
×
1309

1310
        assert(argc >= 2);
×
1311
        assert(argv);
×
1312

1313
        if (arg_root)
×
1314
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "'remove' does not support --root= or --image=.");
×
1315

1316
        if (argc > 2)
×
1317
                log_debug("Too many arguments specified. 'kernel-install remove' takes only kernel version. "
×
1318
                          "Ignoring residual arguments.");
1319

1320
        if (bypass())
×
1321
                return 0;
1322

1323
        _cleanup_(context_done) Context c = CONTEXT_NULL;
×
1324
        r = context_from_cmdline(&c, ACTION_REMOVE);
×
1325
        if (r < 0)
×
1326
                return r;
1327

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

1332
        r = context_set_version(&c, argv[1]);
×
1333
        if (r < 0)
×
1334
                return r;
1335

1336
        r = context_prepare_execution(&c);
×
1337
        if (r < 0)
×
1338
                return r;
1339

1340
        return context_execute(&c);
×
1341
}
1342

1343
static int verb_inspect(int argc, char *argv[], void *userdata) {
×
1344
        _cleanup_(table_unrefp) Table *t = NULL;
×
1345
        _cleanup_free_ char *vmlinuz = NULL;
×
1346
        const char *version, *kernel;
×
1347
        char **initrds;
×
1348
        struct utsname un;
×
1349
        int r;
×
1350

1351
        _cleanup_(context_done) Context c = CONTEXT_NULL;
×
1352
        r = context_from_cmdline(&c, ACTION_INSPECT);
×
1353
        if (r < 0)
×
1354
                return r;
1355

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

1366
        if (!version && !arg_root) {
×
1367
                assert_se(uname(&un) >= 0);
×
1368
                version = un.release;
1369
        }
1370

1371
        if (!kernel && version) {
×
1372
                r = kernel_from_version(version, &vmlinuz);
×
1373
                if (r < 0)
×
1374
                        return r;
1375

1376
                kernel = vmlinuz;
×
1377
        }
1378

1379
        r = context_set_version(&c, version);
×
1380
        if (r < 0)
×
1381
                return r;
1382

1383
        r = context_set_kernel(&c, kernel);
×
1384
        if (r < 0)
×
1385
                return r;
1386

1387
        r = context_set_initrds(&c, initrds);
×
1388
        if (r < 0)
×
1389
                return r;
1390

1391
        r = context_prepare_execution(&c);
×
1392
        if (r < 0)
×
1393
                return r;
1394

1395
        t = table_new_vertical();
×
1396
        if (!t)
×
1397
                return log_oom();
×
1398

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

1431
        if (!sd_json_format_enabled(arg_json_format_flags)) {
×
1432
                r = table_add_many(t,
×
1433
                                   TABLE_FIELD, "Plugin Arguments",
1434
                                   TABLE_STRV, strv_skip(c.argv, 1));
1435
                if (r < 0)
×
1436
                        return table_log_add_error(r);
×
1437
        }
1438

1439
        table_set_ersatz_string(t, TABLE_ERSATZ_UNSET);
×
1440

1441
        for (size_t row = 1; row < table_get_rows(t); row++) {
×
1442
                _cleanup_free_ char *name = NULL;
×
1443

1444
                name = strdup(table_get_at(t, row, 0));
×
1445
                if (!name)
×
1446
                        return log_oom();
×
1447

1448
                r = table_set_json_field_name(t, row - 1, delete_chars(name, " "));
×
1449
                if (r < 0)
×
1450
                        return log_error_errno(r, "Failed to set JSON field name: %m");
×
1451
        }
1452

1453
        return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, /* show_header= */ false);
×
1454
}
1455

1456
static int verb_list(int argc, char *argv[], void *userdata) {
×
1457
        _cleanup_close_ int fd = -EBADF;
×
1458
        int r;
×
1459

1460
        _cleanup_(context_done) Context c = CONTEXT_NULL;
×
1461
        r = context_from_cmdline(&c, ACTION_INSPECT);
×
1462
        if (r < 0)
×
1463
                return r;
1464

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

1469
        _cleanup_free_ DirectoryEntries *de = NULL;
×
1470
        r = readdir_all(fd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT, &de);
×
1471
        if (r < 0)
×
1472
                return log_error_errno(r, "Failed to numerate /usr/lib/modules/ contents: %m");
×
1473

1474
        _cleanup_(table_unrefp) Table *table = NULL;
×
1475
        table = table_new("version", "has kernel", "path");
×
1476
        if (!table)
×
1477
                return log_oom();
×
1478

1479
        table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
×
1480
        table_set_align_percent(table, table_get_cell(table, 0, 1), 100);
×
1481
        (void) table_set_sort(table, (size_t) 0);
×
1482

1483
        FOREACH_ARRAY(d, de->entries, de->n_entries) {
×
1484
                _cleanup_free_ char *j = path_join("/usr/lib/modules/", (*d)->d_name);
×
1485
                if (!j)
×
1486
                        return log_oom();
×
1487

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

1495
                if ((*d)->d_type != DT_DIR)
×
1496
                        continue;
×
1497

1498
                _cleanup_free_ char *fn = path_join((*d)->d_name, "vmlinuz");
×
1499
                if (!fn)
×
1500
                        return log_oom();
×
1501

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

1507
                        exists = false;
1508
                } else
1509
                        exists = true;
1510

1511
                r = table_add_many(table,
×
1512
                                   TABLE_VERSION, (*d)->d_name,
1513
                                   TABLE_BOOLEAN_CHECKMARK, exists,
1514
                                   TABLE_SET_COLOR, ansi_highlight_green_red(exists),
1515
                                   TABLE_PATH, j);
1516
                if (r < 0)
×
1517
                        return table_log_add_error(r);
×
1518
        }
1519

1520
        return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
×
1521
}
1522

1523
static int help(void) {
×
1524
        _cleanup_free_ char *link = NULL;
×
1525
        int r;
×
1526

1527
        r = terminal_urlify_man("kernel-install", "8", &link);
×
1528
        if (r < 0)
×
1529
                return log_oom();
×
1530

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

1572
        return 0;
1573
}
1574

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

1609
        assert(argc >= 0);
×
1610
        assert(argv);
×
1611

1612
        while ((t = getopt_long(argc, argv, "hv", options, NULL)) >= 0)
×
1613
                switch (t) {
×
1614
                case 'h':
×
1615
                        return help();
×
1616

1617
                case ARG_VERSION:
×
1618
                        return version();
×
1619

1620
                case ARG_NO_LEGEND:
×
1621
                        arg_legend = false;
×
1622
                        break;
×
1623

1624
                case 'v':
×
1625
                        log_set_max_level(LOG_DEBUG);
×
1626
                        arg_verbose = true;
×
1627
                        break;
×
1628

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

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

1641
                case ARG_MAKE_ENTRY_DIRECTORY:
×
1642
                        if (streq(optarg, "auto"))
×
1643
                                arg_make_entry_directory = -1;
×
1644
                        else {
1645
                                r = parse_boolean_argument("--make-entry-directory=", optarg, NULL);
×
1646
                                if (r < 0)
×
1647
                                        return r;
1648

1649
                                arg_make_entry_directory = r;
×
1650
                        }
1651
                        break;
1652

1653
                case ARG_ENTRY_TOKEN:
×
1654
                        r = parse_boot_entry_token_type(optarg, &arg_entry_token_type, &arg_entry_token);
×
1655
                        if (r < 0)
×
1656
                                return r;
1657
                        break;
1658

1659
                case ARG_NO_PAGER:
×
1660
                        arg_pager_flags |= PAGER_DISABLE;
×
1661
                        break;
×
1662

1663
                case ARG_JSON:
×
1664
                        r = parse_json_argument(optarg, &arg_json_format_flags);
×
1665
                        if (r <= 0)
×
1666
                                return r;
1667
                        break;
1668

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

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

1681
                case ARG_IMAGE_POLICY:
×
1682
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
1683
                        if (r < 0)
×
1684
                                return r;
1685
                        break;
1686

1687
                case ARG_BOOT_ENTRY_TYPE: {
×
1688
                        if (isempty(optarg) || streq(optarg, "all")) {
×
1689
                                arg_boot_entry_type = _BOOT_ENTRY_TYPE_INVALID;
×
1690
                                break;
×
1691
                        }
1692

1693
                        BootEntryType e = boot_entry_type_from_string(optarg);
×
1694
                        if (e < 0)
×
1695
                                return log_error_errno(e, "Invalid entry type: %s", optarg);
×
1696
                        arg_boot_entry_type = e;
×
1697
                        break;
×
1698
                }
1699

1700
                case '?':
1701
                        return -EINVAL;
1702

1703
                default:
×
1704
                        assert_not_reached();
×
1705
                }
1706

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

1710
        return 1;
1711
}
1712

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

1723
        return dispatch_verb(argc, argv, verbs, /* userdata= */ NULL);
×
1724
}
1725

1726
static int run(int argc, char* argv[]) {
×
1727
        int r;
×
1728

1729
        log_setup();
×
1730

1731
        r = parse_argv(argc, argv);
×
1732
        if (r <= 0)
×
1733
                return r;
×
1734

1735
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
×
1736
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
×
1737
        if (arg_image) {
×
1738
                assert(!arg_root);
×
1739

1740
                r = mount_image_privately_interactively(
×
1741
                                arg_image,
1742
                                arg_image_policy,
1743
                                DISSECT_IMAGE_GENERIC_ROOT |
1744
                                DISSECT_IMAGE_REQUIRE_ROOT |
1745
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
1746
                                DISSECT_IMAGE_VALIDATE_OS |
1747
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
1748
                                &mounted_dir,
1749
                                /* ret_dir_fd= */ NULL,
1750
                                &loop_device);
1751
                if (r < 0)
×
1752
                        return r;
1753

1754
                arg_root = strdup(mounted_dir);
×
1755
                if (!arg_root)
×
1756
                        return log_oom();
×
1757
        }
1758

1759
        if (invoked_as(argv, "installkernel"))
×
1760
                return run_as_installkernel(argc, argv);
×
1761

1762
        return kernel_install_main(argc, argv);
×
1763
}
1764

1765
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