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

systemd / systemd / 13610116813

01 Mar 2025 03:22PM UTC coverage: 71.857% (+0.04%) from 71.822%
13610116813

push

github

DaanDeMeyer
Add a few more bypass environment variables

When we're building ParticleOS images, we don't want the package
manager (or mkosi) to run systemd-sysusers, systemd-tmpfiles or
systemctl preset so let's add a few more bypass environment
variables that we can set to have execution of these skipped like
we already have $SYSTEMD_HWDB_UPDATE_BYPASS and $KERNEL_INSTALL_BYPASS.

12 of 16 new or added lines in 7 files covered. (75.0%)

4203 existing lines in 46 files now uncovered.

294791 of 410246 relevant lines covered (71.86%)

717494.72 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 <stdbool.h>
5
#include <sys/utsname.h>
6

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

39
static bool arg_verbose = false;
40
static char *arg_esp_path = NULL;
41
static char *arg_xbootldr_path = NULL;
42
static int arg_make_entry_directory = -1; /* tristate */
43
static PagerFlags arg_pager_flags = 0;
44
static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
45
static char *arg_root = NULL;
46
static char *arg_image = NULL;
47
static ImagePolicy *arg_image_policy = NULL;
48
static bool arg_legend = true;
49

50
STATIC_DESTRUCTOR_REGISTER(arg_esp_path, freep);
×
51
STATIC_DESTRUCTOR_REGISTER(arg_xbootldr_path, freep);
×
52
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
×
53
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
×
54
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
×
55

56
typedef enum Action {
57
        ACTION_ADD,
58
        ACTION_REMOVE,
59
        ACTION_INSPECT,
60
        _ACTION_MAX,
61
        _ACTION_INVALID = -EINVAL,
62
} Action;
63

64
typedef enum Layout {
65
        LAYOUT_AUTO,
66
        LAYOUT_UKI,
67
        LAYOUT_BLS,
68
        LAYOUT_OTHER,
69
        _LAYOUT_MAX,
70
        _LAYOUT_INVALID = -EINVAL,
71
} Layout;
72

73
static const char * const layout_table[_LAYOUT_MAX] = {
74
        [LAYOUT_AUTO]  = "auto",
75
        [LAYOUT_UKI]   = "uki",
76
        [LAYOUT_BLS]   = "bls",
77
        [LAYOUT_OTHER] = "other",
78
};
79

80
DEFINE_PRIVATE_STRING_TABLE_LOOKUP(layout, Layout);
×
81

82
typedef struct Context {
83
        int rfd;
84
        Action action;
85
        sd_id128_t machine_id;
86
        bool machine_id_is_random;
87
        KernelImageType kernel_image_type;
88
        Layout layout;
89
        char *layout_other;
90
        char *conf_root;
91
        char *boot_root;
92
        BootEntryTokenType entry_token_type;
93
        char *entry_token;
94
        char *entry_dir;
95
        char *version;
96
        char *kernel;
97
        char **initrds;
98
        char *initrd_generator;
99
        char *uki_generator;
100
        char *staging_area;
101
        char **plugins;
102
        char **argv;
103
        char **envp;
104
} Context;
105

106
#define CONTEXT_NULL (Context) { .rfd = -EBADF }
107

108
static void context_done(Context *c) {
×
109
        assert(c);
×
110

111
        free(c->layout_other);
×
112
        free(c->conf_root);
×
113
        free(c->boot_root);
×
114
        free(c->entry_token);
×
115
        free(c->entry_dir);
×
116
        free(c->version);
×
117
        free(c->kernel);
×
118
        strv_free(c->initrds);
×
119
        free(c->initrd_generator);
×
120
        free(c->uki_generator);
×
121
        if (c->action == ACTION_INSPECT)
×
122
                free(c->staging_area);
×
123
        else
124
                rm_rf_physical_and_free(c->staging_area);
×
125
        strv_free(c->plugins);
×
126
        strv_free(c->argv);
×
127
        strv_free(c->envp);
×
128

129
        safe_close(c->rfd);
×
130
}
×
131

132
static int context_copy(const Context *source, Context *ret) {
×
133
        int r;
×
134

135
        assert(source);
×
136
        assert(ret);
×
137
        assert(source->rfd >= 0 || source->rfd == AT_FDCWD);
×
138

139
        _cleanup_(context_done) Context copy = (Context) {
×
140
                .rfd = AT_FDCWD,
141
                .action = source->action,
×
142
                .machine_id = source->machine_id,
143
                .machine_id_is_random = source->machine_id_is_random,
×
144
                .kernel_image_type = source->kernel_image_type,
×
145
                .layout = source->layout,
×
146
                .entry_token_type = source->entry_token_type,
×
147
        };
148

149
        if (source->rfd >= 0) {
×
150
                copy.rfd = fd_reopen(source->rfd, O_CLOEXEC|O_DIRECTORY|O_PATH);
×
151
                if (copy.rfd < 0)
×
152
                        return copy.rfd;
153
        }
154

155
        r = strdup_to(&copy.layout_other, source->layout_other);
×
156
        if (r < 0)
×
157
                return r;
158
        r = strdup_to(&copy.conf_root, source->conf_root);
×
159
        if (r < 0)
×
160
                return r;
161
        r = strdup_to(&copy.boot_root, source->boot_root);
×
162
        if (r < 0)
×
163
                return r;
164
        r = strdup_to(&copy.entry_token, source->entry_token);
×
165
        if (r < 0)
×
166
                return r;
167
        r = strdup_to(&copy.entry_dir, source->entry_dir);
×
168
        if (r < 0)
×
169
                return r;
170
        r = strdup_to(&copy.version, source->version);
×
171
        if (r < 0)
×
172
                return r;
173
        r = strdup_to(&copy.kernel, source->kernel);
×
174
        if (r < 0)
×
175
                return r;
176
        r = strv_copy_unless_empty(source->initrds, &copy.initrds);
×
177
        if (r < 0)
×
178
                return r;
179
        r = strdup_to(&copy.initrd_generator, source->initrd_generator);
×
180
        if (r < 0)
×
181
                return r;
182
        r = strdup_to(&copy.uki_generator, source->uki_generator);
×
183
        if (r < 0)
×
184
                return r;
185
        r = strdup_to(&copy.staging_area, source->staging_area);
×
186
        if (r < 0)
×
187
                return r;
188
        r = strv_copy_unless_empty(source->plugins, &copy.plugins);
×
189
        if (r < 0)
×
190
                return r;
191
        r = strv_copy_unless_empty(source->argv, &copy.argv);
×
192
        if (r < 0)
×
193
                return r;
194
        r = strv_copy_unless_empty(source->envp, &copy.envp);
×
195
        if (r < 0)
×
196
                return r;
197

198
        *ret = copy;
×
199
        copy = CONTEXT_NULL;
×
200

201
        return 0;
×
202
}
203

204
static int context_open_root(Context *c) {
×
205
        int r;
×
206

207
        assert(c);
×
208
        assert(c->rfd < 0);
×
209

210
        if (isempty(arg_root))
×
211
                return 0;
212

213
        r = path_is_root(arg_root);
×
214
        if (r < 0)
×
215
                return log_error_errno(r, "Failed to determine if '%s' is the root directory: %m", arg_root);
×
216
        if (r > 0)
×
217
                return 0;
218

219
        c->rfd = open(empty_to_root(arg_root), O_CLOEXEC | O_DIRECTORY | O_PATH);
×
220
        if (c->rfd < 0)
×
221
                return log_error_errno(errno, "Failed to open root directory '%s': %m", empty_to_root(arg_root));
×
222

223
        return 0;
224
}
225

226
static const char* context_get_layout(const Context *c) {
×
227
        assert(c);
×
228
        assert(c->layout >= 0);
×
229

230
        return c->layout_other ?: layout_to_string(c->layout);
×
231
}
232

233
static int context_set_layout(Context *c, const char *s, const char *source) {
×
234
        Layout t;
×
235

236
        assert(c);
×
237
        assert(source);
×
238

239
        if (c->layout >= 0 || !s)
×
240
                return 0;
241

242
        assert(!c->layout_other);
×
243

244
        t = layout_from_string(s);
×
245
        if (t >= 0)
×
246
                c->layout = t;
×
247
        else if (isempty(s))
×
248
                c->layout = LAYOUT_AUTO;
×
249
        else {
250
                c->layout_other = strdup(s);
×
251
                if (!c->layout_other)
×
252
                        return log_oom();
×
253

254
                c->layout = LAYOUT_OTHER;
×
255
        }
256

257
        log_debug("layout=%s set via %s", context_get_layout(c), source);
×
258
        return 1;
259
}
260

261
static int context_set_machine_id(Context *c, const char *s, const char *source) {
×
262
        int r;
×
263

264
        assert(c);
×
265
        assert(source);
×
266

267
        if (!sd_id128_is_null(c->machine_id) || !s)
×
268
                return 0;
×
269

270
        r = sd_id128_from_string(s, &c->machine_id);
×
271
        if (r < 0)
×
272
                return log_warning_errno(r, "Failed to parse machine ID specified via %s, ignoring.", source);
×
273

274
        if (sd_id128_is_null(c->machine_id))
×
275
                return 0;
×
276

277
        log_debug("MACHINE_ID=%s set via %s.", SD_ID128_TO_STRING(c->machine_id), source);
×
278
        return 1;
×
279
}
280

281
static int context_set_string(const char *s, const char *source, const char *name, char **dest) {
×
282
        char *p;
×
283

284
        assert(source);
×
285
        assert(name);
×
286
        assert(dest);
×
287

288
        if (*dest || !s)
×
289
                return 0;
290

291
        p = strdup(s);
×
292
        if (!p)
×
293
                return log_oom();
×
294

295
        log_debug("%s (%s) set via %s.", name, p, source);
×
296

297
        *dest = p;
×
298
        return 1;
×
299
}
300

301
static int context_set_initrd_generator(Context *c, const char *s, const char *source) {
×
302
        assert(c);
×
303
        return context_set_string(s, source, "INITRD_GENERATOR", &c->initrd_generator);
×
304
}
305

306
static int context_set_uki_generator(Context *c, const char *s, const char *source) {
×
307
        assert(c);
×
308
        return context_set_string(s, source, "UKI_GENERATOR", &c->uki_generator);
×
309
}
310

311
static int context_set_version(Context *c, const char *s) {
×
312
        assert(c);
×
313

314
        if (s && !filename_is_valid(s))
×
315
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid version specified: %s", s);
×
316

317
        return context_set_string(s, "command line", "kernel version", &c->version);
×
318
}
319

320
static int context_set_path(Context *c, const char *s, const char *source, const char *name, char **dest) {
×
321
        char *p;
×
322
        int r;
×
323

324
        assert(c);
×
325
        assert(source);
×
326
        assert(name);
×
327
        assert(dest);
×
328

329
        if (*dest || !s)
×
330
                return 0;
×
331

332
        if (c->rfd >= 0) {
×
333
                r = chaseat(c->rfd, s, CHASE_AT_RESOLVE_IN_ROOT, &p, /* ret_fd = */ NULL);
×
334
                if (r < 0)
×
335
                        return log_warning_errno(r, "Failed to chase path %s for %s specified via %s, ignoring: %m",
×
336
                                                 s, name, source);
337
        } else {
338
                r = path_make_absolute_cwd(s, &p);
×
339
                if (r < 0)
×
340
                        return log_warning_errno(r, "Failed to make path '%s' for %s specified via %s absolute, ignoring: %m",
×
341
                                                 s, name, source);
342
        }
343

344
        log_debug("%s (%s) set via %s.", name, p, source);
×
345

346
        *dest = p;
×
347
        return 1;
×
348
}
349

350
static int context_set_boot_root(Context *c, const char *s, const char *source) {
×
351
        assert(c);
×
352
        return context_set_path(c, s, source, "BOOT_ROOT", &c->boot_root);
×
353
}
354

355
static int context_set_conf_root(Context *c, const char *s, const char *source) {
×
356
        assert(c);
×
357
        return context_set_path(c, s, source, "CONF_ROOT", &c->conf_root);
×
358
}
359

360
static int context_set_kernel(Context *c, const char *s) {
×
361
        assert(c);
×
362
        return context_set_path(c, s, "command line", "kernel image file", &c->kernel);
×
363
}
364

365
static int context_set_path_strv(Context *c, char* const* strv, const char *source, const char *name, char ***dest) {
×
366
        _cleanup_strv_free_ char **w = NULL;
×
367
        int r;
×
368

369
        assert(c);
×
370
        assert(source);
×
371
        assert(name);
×
372
        assert(dest);
×
373

374
        if (*dest)
×
375
                return 0;
376

377
        STRV_FOREACH(s, strv) {
×
378
                char *p;
×
379

380
                if (c->rfd >= 0) {
×
381
                        r = chaseat(c->rfd, *s, CHASE_AT_RESOLVE_IN_ROOT, &p, /* ret_fd = */ NULL);
×
382
                        if (r < 0)
×
383
                                return log_warning_errno(r, "Failed to chase path %s for %s specified via %s: %m",
×
384
                                                         *s, name, source);
385
                } else {
386
                        r = path_make_absolute_cwd(*s, &p);
×
387
                        if (r < 0)
×
388
                                return log_warning_errno(r, "Failed to make path '%s' for %s specified via %s absolute, ignoring: %m",
×
389
                                                         *s, name, source);
390
                }
391
                r = strv_consume(&w, p);
×
392
                if (r < 0)
×
393
                        return log_oom();
×
394
        }
395

396
        if (strv_isempty(w))
×
397
                return 0;
398

399
        log_debug("%s set via %s", name, source);
×
400

401
        *dest = TAKE_PTR(w);
×
402
        return 1;
×
403
}
404

405
static int context_set_plugins(Context *c, const char *s, const char *source) {
×
406
        _cleanup_strv_free_ char **v = NULL;
×
407
        int r;
×
408

409
        assert(c);
×
410

411
        if (c->plugins || !s)
×
412
                return 0;
413

414
        r = strv_split_full(&v, s, NULL, EXTRACT_UNQUOTE);
×
415
        if (r < 0)
×
416
                return log_error_errno(r, "Failed to parse plugin paths from %s: %m", source);
×
417

418
        return context_set_path_strv(c, v, source, "plugins", &c->plugins);
×
419
}
420

421
static int context_set_initrds(Context *c, char* const* strv) {
×
422
        assert(c);
×
423
        return context_set_path_strv(c, strv, "command line", "initrds", &c->initrds);
×
424
}
425

426
static int context_load_environment(Context *c) {
×
427
        assert(c);
×
428

429
        (void) context_set_machine_id(c, getenv("MACHINE_ID"), "environment");
×
430
        (void) context_set_boot_root(c, getenv("BOOT_ROOT"), "environment");
×
431
        (void) context_set_conf_root(c, getenv("KERNEL_INSTALL_CONF_ROOT"), "environment");
×
432
        (void) context_set_plugins(c, getenv("KERNEL_INSTALL_PLUGINS"), "environment");
×
433
        return 0;
×
434
}
435

436
static int context_load_install_conf(Context *c) {
×
437
        _cleanup_free_ char *machine_id = NULL, *boot_root = NULL, *layout = NULL,
×
438
                            *initrd_generator = NULL, *uki_generator = NULL;
×
439
        int r;
×
440

441
        assert(c);
×
442

443
        r = load_kernel_install_conf(arg_root,
×
444
                                     c->conf_root,
×
445
                                     &machine_id,
446
                                     &boot_root,
447
                                     &layout,
448
                                     &initrd_generator,
449
                                     &uki_generator);
450
        if (r <= 0)
×
451
                return r;
452

453
        (void) context_set_machine_id(c, machine_id, "config");
×
454
        (void) context_set_boot_root(c, boot_root, "config");
×
455
        (void) context_set_layout(c, layout, "config");
×
456
        (void) context_set_initrd_generator(c, initrd_generator, "config");
×
457
        (void) context_set_uki_generator(c, uki_generator, "config");
×
458

459
        log_debug("Loaded config.");
×
460
        return 0;
461
}
462

463
static int context_load_machine_info(Context *c) {
×
464
        _cleanup_fclose_ FILE *f = NULL;
×
465
        _cleanup_free_ char *machine_id = NULL, *layout = NULL;
×
466
        static const char *path = "/etc/machine-info";
×
467
        int r;
×
468

469
        assert(c);
×
470

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

474
        if (!sd_id128_is_null(c->machine_id) && c->layout >= 0)
×
475
                return 0;
476

477
        /* For testing. To make not read host's /etc/machine-info. */
478
        r = getenv_bool("KERNEL_INSTALL_READ_MACHINE_INFO");
×
479
        if (r < 0 && r != -ENXIO)
×
480
                log_warning_errno(r, "Failed to read $KERNEL_INSTALL_READ_MACHINE_INFO, assuming yes: %m");
×
481
        if (r == 0) {
×
482
                log_debug("Skipping reading of /etc/machine-info.");
×
483
                return 0;
×
484
        }
485

486
        r = chase_and_fopenat_unlocked(c->rfd, path, CHASE_AT_RESOLVE_IN_ROOT, "re", NULL, &f);
×
487
        if (r == -ENOENT)
×
488
                return 0;
489
        if (r < 0)
×
490
                return log_error_errno(r, "Failed to chase %s: %m", path);
×
491

492
        log_debug("Loading %s…", path);
×
493

494
        r = parse_env_file(f, path,
×
495
                           "KERNEL_INSTALL_MACHINE_ID", &machine_id,
496
                           "KERNEL_INSTALL_LAYOUT", &layout);
497
        if (r < 0)
×
498
                return log_error_errno(r, "Failed to parse '%s': %m", path);
×
499

500
        (void) context_set_machine_id(c, machine_id, path);
×
501
        (void) context_set_layout(c, layout, path);
×
502
        return 0;
503
}
504

505
static int context_load_machine_id(Context *c) {
×
506
        int r;
×
507

508
        assert(c);
×
509

510
        r = id128_get_machine_at(c->rfd, &c->machine_id);
×
511
        if (ERRNO_IS_NEG_MACHINE_ID_UNSET(r))
×
512
                return 0;
513
        if (r < 0)
×
514
                return log_error_errno(r, "Failed to load machine ID from /etc/machine-id: %m");
×
515

516
        log_debug("MACHINE_ID=%s set via /etc/machine-id.", SD_ID128_TO_STRING(c->machine_id));
×
517
        return 1; /* loaded */
×
518
}
519

520
static int context_ensure_machine_id(Context *c) {
×
521
        int r;
×
522

523
        assert(c);
×
524

525
        if (!sd_id128_is_null(c->machine_id))
×
526
                return 0;
×
527

528
        /* If /etc/machine-id is initialized we'll use it. */
529
        r = context_load_machine_id(c);
×
530
        if (r != 0)
×
531
                return r;
532

533
        /* Otherwise we'll use a freshly generated one. */
534
        r = sd_id128_randomize(&c->machine_id);
×
535
        if (r < 0)
×
536
                return log_error_errno(r, "Failed to generate random ID: %m");
×
537

538
        c->machine_id_is_random = true;
×
539
        log_debug("New machine ID '%s' generated.", SD_ID128_TO_STRING(c->machine_id));
×
540
        return 0;
×
541
}
542

543
static int context_acquire_xbootldr(Context *c) {
×
544
        int r;
×
545

546
        assert(c);
×
547
        assert(!c->boot_root);
×
548

549
        r = find_xbootldr_and_warn_at(
×
550
                        /* rfd = */ c->rfd,
551
                        /* path = */ arg_xbootldr_path,
552
                        /* unprivileged_mode= */ -1,
553
                        /* ret_path = */ &c->boot_root,
554
                        /* ret_uuid = */ NULL,
555
                        /* ret_devid = */ NULL);
556
        if (r == -ENOKEY) {
×
557
                log_debug_errno(r, "Couldn't find an XBOOTLDR partition.");
×
558
                return 0;
×
559
        }
560
        if (r == -EACCES && geteuid() != 0)
×
561
                return log_error_errno(r, "Failed to determine XBOOTLDR partition: %m");
×
562
        if (r < 0)
×
563
                return r;
564

565
        log_debug("Using XBOOTLDR partition at %s as $BOOT_ROOT.", c->boot_root);
×
566
        return 1; /* found */
567
}
568

569
static int context_acquire_esp(Context *c) {
×
570
        int r;
×
571

572
        assert(c);
×
573
        assert(!c->boot_root);
×
574

575
        r = find_esp_and_warn_at(
×
576
                        /* rfd = */ c->rfd,
577
                        /* path = */ arg_esp_path,
578
                        /* unprivileged_mode= */ -1,
579
                        /* ret_path = */ &c->boot_root,
580
                        /* ret_part = */ NULL,
581
                        /* ret_pstart = */ NULL,
582
                        /* ret_psize = */ NULL,
583
                        /* ret_uuid = */ NULL,
584
                        /* ret_devid = */ NULL);
585
        if (r == -ENOKEY) {
×
586
                log_debug_errno(r, "Couldn't find EFI system partition, ignoring.");
×
587
                return 0;
×
588
        }
589
        if (r == -EACCES && geteuid() != 0)
×
590
                return log_error_errno(r, "Failed to determine EFI system partition: %m");
×
591
        if (r < 0)
×
592
                return r;
593

594
        log_debug("Using EFI System Partition at %s as $BOOT_ROOT.", c->boot_root);
×
595
        return 1; /* found */
596
}
597

598
static int context_ensure_boot_root(Context *c) {
×
599
        int r;
×
600

601
        assert(c);
×
602

603
        /* If BOOT_ROOT is specified via environment or install.conf, then use it. */
604
        if (c->boot_root)
×
605
                return 0;
606

607
        /* Otherwise, use XBOOTLDR partition, if mounted. */
608
        r = context_acquire_xbootldr(c);
×
609
        if (r != 0)
×
610
                return r;
611

612
        /* Otherwise, use EFI system partition, if mounted. */
613
        r = context_acquire_esp(c);
×
614
        if (r != 0)
×
615
                return r;
616

617
        /* If all else fails, use /boot. */
618
        if (c->rfd >= 0) {
×
619
                r = chaseat(c->rfd, "/boot", CHASE_AT_RESOLVE_IN_ROOT, &c->boot_root, /* ret_fd = */ NULL);
×
620
                if (r < 0)
×
621
                        return log_error_errno(r, "Failed to chase '/boot/': %m");
×
622
        } else {
623
                c->boot_root = strdup("/boot");
×
624
                if (!c->boot_root)
×
625
                        return log_oom();
×
626
        }
627

628
        log_debug("KERNEL_INSTALL_BOOT_ROOT autodetection yielded no candidates, using \"%s\".", c->boot_root);
×
629
        return 0;
630
}
631

632
static int context_ensure_entry_token(Context *c) {
×
633
        int r;
×
634

635
        assert(c);
×
636

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

642
        r = boot_entry_token_ensure_at(
×
643
                        c->rfd,
644
                        c->conf_root,
×
645
                        c->machine_id,
646
                        c->machine_id_is_random,
×
647
                        &c->entry_token_type,
648
                        &c->entry_token);
649
        if (r < 0)
×
650
                return r;
651

652
        log_debug("Using entry token: %s", c->entry_token);
×
653
        return 0;
654
}
655

656
static int context_load_plugins(Context *c) {
×
657
        int r;
×
658

659
        assert(c);
×
660

661
        if (c->plugins)
×
662
                return 0;
×
663

664
        r = conf_files_list_strv_at(
×
665
                        &c->plugins,
666
                        ".install",
667
                        c->rfd,
668
                        CONF_FILES_EXECUTABLE | CONF_FILES_REGULAR | CONF_FILES_FILTER_MASKED,
669
                        STRV_MAKE_CONST("/etc/kernel/install.d", "/usr/lib/kernel/install.d"));
×
670
        if (r < 0)
×
671
                return log_error_errno(r, "Failed to find plugins: %m");
×
672

673
        return 0;
674
}
675

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

679
        assert(c);
×
680

681
        r = context_open_root(c);
×
682
        if (r < 0)
×
683
                return r;
684

685
        r = context_load_environment(c);
×
686
        if (r < 0)
×
687
                return r;
688

689
        r = context_load_install_conf(c);
×
690
        if (r < 0)
×
691
                return r;
692

693
        r = context_load_machine_info(c);
×
694
        if (r < 0)
×
695
                return r;
696

697
        r = context_ensure_machine_id(c);
×
698
        if (r < 0)
×
699
                return r;
700

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

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

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

713
        return 0;
714
}
715

716
static int context_inspect_kernel(Context *c) {
×
717
        assert(c);
×
718

719
        if (!c->kernel)
×
720
                return 0;
721

722
        return inspect_kernel(c->rfd, c->kernel, &c->kernel_image_type, NULL, NULL, NULL);
×
723
}
724

725
static int context_ensure_layout(Context *c) {
×
726
        int r;
×
727

728
        assert(c);
×
729
        assert(c->boot_root);
×
730
        assert(c->entry_token);
×
731

732
        if (c->layout >= 0 && c->layout != LAYOUT_AUTO)
×
733
                return 0;
×
734

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

738
        if (c->kernel_image_type == KERNEL_IMAGE_TYPE_UKI) {
×
739
                c->layout = LAYOUT_UKI;
×
740
                log_debug("Kernel image type is %s, using layout=%s.",
×
741
                          kernel_image_type_to_string(c->kernel_image_type), layout_to_string(c->layout));
742
                return 0;
×
743
        }
744

745
        _cleanup_free_ char *srel_path = path_join(c->boot_root, "loader/entries.srel");
×
746
        if (!srel_path)
×
747
                return log_oom();
×
748

749
        _cleanup_free_ char *srel = NULL;
×
750
        r = read_one_line_file_at(c->rfd, srel_path, &srel);
×
751
        if (r >= 0) {
×
752
                if (streq(srel, "type1"))
×
753
                        /* The loader/entries.srel file clearly indicates that the installed boot loader
754
                         * implements the proper standard upstream boot loader spec for Type #1 entries.
755
                         * Let's default to that, then. */
756
                        c->layout = LAYOUT_BLS;
×
757
                else
758
                        /* The loader/entries.srel file indicates some other spec is implemented and owns the
759
                         * /loader/entries/ directory. Since we have no idea what that means, let's stay away
760
                         * from it by default. */
761
                        c->layout = LAYOUT_OTHER;
×
762

763
                log_debug("%s with '%s' found, using layout=%s.", srel_path, srel, layout_to_string(c->layout));
×
764
                return 0;
×
765
        } else if (r != -ENOENT)
×
766
                return log_error_errno(r, "Failed to read %s: %m", srel_path);
×
767

768
        _cleanup_free_ char *entry_token_path = path_join(c->boot_root, c->entry_token);
×
769
        if (!entry_token_path)
×
770
                return log_oom();
×
771

772
        r = is_dir_at(c->rfd, entry_token_path, /* follow = */ false);
×
773
        if (r < 0 && r != -ENOENT)
×
774
                return log_error_errno(r, "Failed to check if '%s' is a directory: %m", entry_token_path);
×
775
        if (r > 0) {
×
776
                /* If the metadata in $BOOT_ROOT doesn't tell us anything, then check if the entry token
777
                 * directory already exists. If so, let's assume it's the standard boot loader spec, too. */
778
                c->layout = LAYOUT_BLS;
×
779
                log_debug("%s exists, using layout=%s.", entry_token_path, layout_to_string(c->layout));
×
780
                return 0;
×
781
        }
782

783
        /* There's no metadata in $BOOT_ROOT, and apparently no entry token directory installed? Then we
784
         * really don't know anything. */
785
        c->layout = LAYOUT_OTHER;
×
786
        log_debug("Entry-token directory not found, using layout=%s.", layout_to_string(c->layout));
×
787
        return 0;
788
}
789

790
static int context_set_up_staging_area(Context *c) {
×
791
        static const char *template = "/tmp/kernel-install.staging.XXXXXX";
×
792
        int r;
×
793

794
        assert(c);
×
795

796
        if (c->staging_area)
×
797
                return 0;
798

799
        if (c->action == ACTION_INSPECT) {
×
800
                /* This is only used for display. The directory will not be created. */
801
                c->staging_area = strdup(template);
×
802
                if (!c->staging_area)
×
803
                        return log_oom();
×
804
        } else {
805
                r = mkdtemp_malloc(template, &c->staging_area);
×
806
                if (r < 0)
×
807
                        return log_error_errno(r, "Failed to create staging area: %m");
×
808
        }
809

810
        return 0;
811
}
812

813
static int context_build_entry_dir(Context *c) {
×
814
        assert(c);
×
815
        assert(c->boot_root);
×
816
        assert(c->entry_token);
×
817
        assert(c->version || c->action == ACTION_INSPECT);
×
818

819
        if (c->entry_dir)
×
820
                return 0;
821

822
        c->entry_dir = path_join(c->boot_root, c->entry_token, c->version ?: "KERNEL_VERSION");
×
823
        if (!c->entry_dir)
×
824
                return log_oom();
×
825

826
        log_debug("Using ENTRY_DIR=%s", c->entry_dir);
×
827
        return 0;
828
}
829

830
static bool context_should_make_entry_dir(Context *c) {
×
831
        assert(c);
×
832

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

836
        if (arg_make_entry_directory < 0)
×
837
                return c->layout == LAYOUT_BLS;
×
838

839
        return arg_make_entry_directory;
×
840
}
841

842
static int context_make_entry_dir(Context *c) {
×
843
        _cleanup_close_ int fd = -EBADF;
×
844

845
        assert(c);
×
846
        assert(c->entry_dir);
×
847

848
        if (c->action != ACTION_ADD)
×
849
                return 0;
850

851
        if (!context_should_make_entry_dir(c))
×
852
                return 0;
853

854
        log_debug("mkdir -p %s", c->entry_dir);
×
855
        fd = chase_and_openat(c->rfd, c->entry_dir, CHASE_AT_RESOLVE_IN_ROOT | CHASE_MKDIR_0755,
×
856
                              O_CLOEXEC | O_CREAT | O_DIRECTORY | O_PATH, NULL);
857
        if (fd < 0)
×
858
                return log_error_errno(fd, "Failed to make directory '%s': %m", c->entry_dir);
×
859

860
        return 0;
861
}
862

863
static int context_remove_entry_dir(Context *c) {
×
864
        _cleanup_free_ char *p = NULL;
×
865
        _cleanup_close_ int fd = -EBADF;
×
866
        struct stat st;
×
867
        int r;
×
868

869
        assert(c);
×
870
        assert(c->entry_dir);
×
871

872
        if (c->action != ACTION_REMOVE)
×
873
                return 0;
874

875
        if (!context_should_make_entry_dir(c))
×
876
                return 0;
877

878
        log_debug("rm -rf %s", c->entry_dir);
×
879
        fd = chase_and_openat(c->rfd, c->entry_dir, CHASE_AT_RESOLVE_IN_ROOT, O_CLOEXEC | O_DIRECTORY, &p);
×
880
        if (fd < 0) {
×
881
                if (IN_SET(fd, -ENOTDIR, -ENOENT))
×
882
                        return 0;
883
                return log_debug_errno(fd, "Failed to chase and open %s, ignoring: %m", c->entry_dir);
×
884
        }
885

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

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

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

896
        return 0;
897
}
898

899
static int context_build_arguments(Context *c) {
×
900
        _cleanup_strv_free_ char **a = NULL;
×
901
        const char *verb;
×
902
        int r;
×
903

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

907
        if (c->argv)
×
908
                return 0;
909

910
        switch (c->action) {
×
911
        case ACTION_ADD:
×
912
                assert(c->version);
×
913
                assert(c->kernel);
×
914
                verb = "add";
915
                break;
916

917
        case ACTION_REMOVE:
×
918
                assert(c->version);
×
919
                assert(!c->kernel);
×
920
                assert(!c->initrds);
×
921
                verb = "remove";
922
                break;
923

924
        case ACTION_INSPECT:
925
                verb = "add|remove";
926
                break;
927

928
        default:
×
929
                assert_not_reached();
×
930
        }
931

932
        a = strv_new("dummy-arg", /* to make strv_free() works for this variable. */
×
933
                     verb,
934
                     c->version ?: "KERNEL_VERSION",
935
                     c->entry_dir);
936
        if (!a)
×
937
                return log_oom();
×
938

939
        if (c->action == ACTION_ADD) {
×
940
                r = strv_extend(&a, c->kernel);
×
941
                if (r < 0)
×
942
                        return log_oom();
×
943

944
                r = strv_extend_strv(&a, c->initrds, /* filter_duplicates = */ false);
×
945
                if (r < 0)
×
946
                        return log_oom();
×
947

948
        } else if (c->action == ACTION_INSPECT) {
×
949
                r = strv_extend_many(
×
950
                                &a,
951
                                c->kernel ?: "[KERNEL_IMAGE]",
952
                                "[INITRD...]");
953
                if (r < 0)
×
954
                        return log_oom();
×
955
        }
956

957
        c->argv = TAKE_PTR(a);
×
958
        return 0;
×
959
}
960

961
static int context_build_environment(Context *c) {
×
962
        _cleanup_strv_free_ char **e = NULL;
×
963
        int r;
×
964

965
        assert(c);
×
966

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

970
        r = strv_env_assign_many(&e,
×
971
                                 "LC_COLLATE",                      SYSTEMD_DEFAULT_LOCALE,
972
                                 "KERNEL_INSTALL_VERBOSE",          one_zero(arg_verbose),
973
                                 "KERNEL_INSTALL_IMAGE_TYPE",       kernel_image_type_to_string(c->kernel_image_type),
974
                                 "KERNEL_INSTALL_MACHINE_ID",       SD_ID128_TO_STRING(c->machine_id),
975
                                 "KERNEL_INSTALL_ENTRY_TOKEN",      c->entry_token,
976
                                 "KERNEL_INSTALL_BOOT_ROOT",        c->boot_root,
977
                                 "KERNEL_INSTALL_LAYOUT",           context_get_layout(c),
978
                                 "KERNEL_INSTALL_INITRD_GENERATOR", strempty(c->initrd_generator),
979
                                 "KERNEL_INSTALL_UKI_GENERATOR",    strempty(c->uki_generator),
980
                                 "KERNEL_INSTALL_STAGING_AREA",     c->staging_area);
981
        if (r < 0)
×
982
                return log_error_errno(r, "Failed to build environment variables for plugins: %m");
×
983

984
        c->envp = TAKE_PTR(e);
×
985
        return 0;
×
986
}
987

988
static int context_prepare_execution(Context *c) {
×
989
        int r;
×
990

991
        assert(c);
×
992

993
        r = context_inspect_kernel(c);
×
994
        if (r < 0)
×
995
                return r;
996

997
        r = context_ensure_layout(c);
×
998
        if (r < 0)
×
999
                return r;
1000

1001
        r = context_set_up_staging_area(c);
×
1002
        if (r < 0)
×
1003
                return r;
1004

1005
        r = context_build_entry_dir(c);
×
1006
        if (r < 0)
×
1007
                return r;
1008

1009
        r = context_build_arguments(c);
×
1010
        if (r < 0)
×
1011
                return r;
1012

1013
        r = context_build_environment(c);
×
1014
        if (r < 0)
×
1015
                return r;
×
1016

1017
        return 0;
1018
}
1019

1020
static int context_execute(Context *c) {
×
1021
        int r, ret;
×
1022

1023
        assert(c);
×
1024

1025
        r = context_make_entry_dir(c);
×
1026
        if (r < 0)
×
1027
                return r;
1028

1029
        if (DEBUG_LOGGING) {
×
1030
                _cleanup_free_ char *x = strv_join_full(c->plugins, "", "\n  ", /* escape_separator = */ false);
×
1031
                log_debug("Using plugins: %s", strna(x));
×
1032

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

1036
                _cleanup_free_ char *z = strv_join(strv_skip(c->argv, 1), " ");
×
1037
                log_debug("Plugin arguments: %s", strna(z));
×
1038
        }
1039

1040
        ret = execute_strv(
×
1041
                        /* name = */ NULL,
1042
                        c->plugins,
×
1043
                        /* root = */ NULL,
1044
                        USEC_INFINITY,
1045
                        /* callbacks = */ NULL,
1046
                        /* callback_args = */ NULL,
1047
                        c->argv,
1048
                        c->envp,
1049
                        EXEC_DIR_SKIP_REMAINING);
1050

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

1055
        /* This returns 0 on success, positive exit code on plugin failure, negative errno on other failures. */
1056
        return ret;
1057
}
1058

1059
static bool bypass(void) {
×
NEW
1060
        return should_bypass("KERNEL_INSTALL");
×
1061
}
1062

1063
static int do_add(
×
1064
                Context *c,
1065
                const char *version,
1066
                const char *kernel,
1067
                char **initrds) {
1068

1069
        int r;
×
1070

1071
        assert(c);
×
1072
        assert(version);
×
1073
        assert(kernel);
×
1074

1075
        r = context_set_version(c, version);
×
1076
        if (r < 0)
×
1077
                return r;
1078

1079
        r = context_set_kernel(c, kernel);
×
1080
        if (r < 0)
×
1081
                return r;
1082

1083
        r = context_set_initrds(c, initrds);
×
1084
        if (r < 0)
×
1085
                return r;
1086

1087
        r = context_prepare_execution(c);
×
1088
        if (r < 0)
×
1089
                return r;
1090

1091
        return context_execute(c);
×
1092
}
1093

1094
static int kernel_from_version(const char *version, char **ret_kernel) {
×
1095
        _cleanup_free_ char *vmlinuz = NULL;
×
1096
        int r;
×
1097

1098
        assert(version);
×
1099

1100
        vmlinuz = path_join("/usr/lib/modules/", version, "/vmlinuz");
×
1101
        if (!vmlinuz)
×
1102
                return log_oom();
×
1103

1104
        r = access_nofollow(vmlinuz, F_OK);
×
1105
        if (r == -ENOENT)
×
1106
                return log_error_errno(r, "Kernel image not installed to '%s', requiring manual kernel image path specification.", vmlinuz);
×
1107
        if (r < 0)
×
1108
                return log_error_errno(r, "Failed to determine if kernel image is installed to '%s': %m", vmlinuz);
×
1109

1110
        *ret_kernel = TAKE_PTR(vmlinuz);
×
1111
        return 0;
×
1112
}
1113

1114
static int verb_add(int argc, char *argv[], void *userdata) {
×
1115
        Context *c = ASSERT_PTR(userdata);
×
1116
        _cleanup_free_ char *vmlinuz = NULL;
×
1117
        const char *version, *kernel;
×
1118
        char **initrds;
×
1119
        struct utsname un;
×
1120
        int r;
×
1121

1122
        assert(argv);
×
1123

1124
        if (arg_root)
×
1125
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "'add' does not support --root= or --image=.");
×
1126

1127
        if (bypass())
×
1128
                return 0;
1129

1130
        c->action = ACTION_ADD;
×
1131

1132
        /* We use the same order of arguments that "inspect" introduced, i.e. if only on argument is
1133
         * specified we take it as the kernel path, not the version, i.e. it's the first argument that is
1134
         * optional, not the 2nd. */
1135
        version = argc > 2 ? empty_or_dash_to_null(argv[1]) : NULL;
×
1136
        kernel = argc > 2 ? empty_or_dash_to_null(argv[2]) :
×
1137
                (argc > 1 ? empty_or_dash_to_null(argv[1]) : NULL);
×
1138
        initrds = strv_skip(argv, 3);
×
1139

1140
        if (!version) {
×
1141
                assert_se(uname(&un) >= 0);
×
1142
                version = un.release;
1143
        }
1144

1145
        if (!kernel) {
×
1146
                r = kernel_from_version(version, &vmlinuz);
×
1147
                if (r < 0)
×
1148
                        return r;
1149

1150
                kernel = vmlinuz;
×
1151
        }
1152

1153
        return do_add(c, version, kernel, initrds);
×
1154
}
1155

1156
static int verb_add_all(int argc, char *argv[], void *userdata) {
×
1157
        Context *c = ASSERT_PTR(userdata);
×
1158
        _cleanup_close_ int fd = -EBADF;
×
1159
        size_t n = 0;
×
1160
        int ret = 0, r;
×
1161

1162
        assert(argv);
×
1163

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

1167
        if (bypass())
×
1168
                return 0;
1169

1170
        c->action = ACTION_ADD;
×
1171

1172
        fd = chase_and_openat(c->rfd, "/usr/lib/modules", CHASE_AT_RESOLVE_IN_ROOT, O_DIRECTORY|O_RDONLY|O_CLOEXEC, NULL);
×
1173
        if (fd < 0)
×
1174
                return log_error_errno(fd, "Failed to open %s/usr/lib/modules/: %m", strempty(arg_root));
×
1175

1176
        _cleanup_free_ DirectoryEntries *de = NULL;
×
1177
        r = readdir_all(fd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT, &de);
×
1178
        if (r < 0)
×
1179
                return log_error_errno(r, "Failed to numerate /usr/lib/modules/ contents: %m");
×
1180

1181
        FOREACH_ARRAY(d, de->entries, de->n_entries) {
×
1182
                r = dirent_ensure_type(fd, *d);
×
1183
                if (r < 0) {
×
1184
                        if (r != -ENOENT) /* don't log if just gone by now */
×
1185
                                log_debug_errno(r, "Failed to check if '%s/usr/lib/modules/%s' is a directory, ignoring: %m", strempty(arg_root), (*d)->d_name);
×
1186
                        continue;
×
1187
                }
1188

1189
                if ((*d)->d_type != DT_DIR)
×
1190
                        continue;
×
1191

1192
                _cleanup_free_ char *fn = path_join((*d)->d_name, "vmlinuz");
×
1193
                if (!fn)
×
1194
                        return log_oom();
×
1195

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

1200
                        log_notice("Not adding version '%s', because kernel image not found.", (*d)->d_name);
×
1201
                        continue;
×
1202
                }
1203

1204
                _cleanup_(context_done) Context copy = CONTEXT_NULL;
×
1205

1206
                r = context_copy(c, &copy);
×
1207
                if (r < 0)
×
1208
                        return log_error_errno(r, "Failed to copy execution context: %m");
×
1209

1210
                /* do_add() will look up the path in the correct root directory so we don't need to prefix it
1211
                 * with arg_root here. */
1212
                _cleanup_free_ char *full = path_join("/usr/lib/modules/", fn);
×
1213
                if (!full)
×
1214
                        return log_oom();
×
1215

1216
                r = do_add(&copy,
×
1217
                           /* version= */ (*d)->d_name,
×
1218
                           /* kernel= */ full,
1219
                           /* initrds= */ NULL);
1220
                if (r == 0)
×
1221
                        n++;
×
1222
                else if (ret == 0)
×
1223
                        ret = r;
×
1224
        }
1225

1226
        if (n > 0)
×
1227
                log_debug("Installed %zu kernel(s).", n);
×
1228
        else if (ret == 0)
×
1229
                ret = log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No kernels to install found.");
×
1230

1231
        return ret;
1232
}
1233

1234
static int run_as_installkernel(int argc, char *argv[], Context *c) {
×
1235
        /* kernel's install.sh invokes us as
1236
         *   /sbin/installkernel <version> <vmlinuz> <map> <installation-dir>
1237
         * We ignore the last two arguments. */
1238
        if (optind + 2 > argc)
×
1239
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "'installkernel' command requires at least two arguments.");
×
1240

1241
        return verb_add(3, STRV_MAKE("add", argv[optind], argv[optind+1]), c);
×
1242
}
1243

1244
static int verb_remove(int argc, char *argv[], void *userdata) {
×
1245
        Context *c = ASSERT_PTR(userdata);
×
1246
        int r;
×
1247

1248
        assert(argc >= 2);
×
1249
        assert(argv);
×
1250

1251
        if (arg_root)
×
1252
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "'remove' does not support --root= or --image=.");
×
1253

1254
        if (argc > 2)
×
1255
                log_debug("Too many arguments specified. 'kernel-install remove' takes only kernel version. "
×
1256
                          "Ignoring residual arguments.");
1257

1258
        if (bypass())
×
1259
                return 0;
1260

1261
        c->action = ACTION_REMOVE;
×
1262

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

1267
        r = context_set_version(c, argv[1]);
×
1268
        if (r < 0)
×
1269
                return r;
1270

1271
        r = context_prepare_execution(c);
×
1272
        if (r < 0)
×
1273
                return r;
1274

1275
        return context_execute(c);
×
1276
}
1277

1278
static int verb_inspect(int argc, char *argv[], void *userdata) {
×
1279
        Context *c = ASSERT_PTR(userdata);
×
1280
        _cleanup_(table_unrefp) Table *t = NULL;
×
1281
        _cleanup_free_ char *vmlinuz = NULL;
×
1282
        const char *version, *kernel;
×
1283
        char **initrds;
×
1284
        struct utsname un;
×
1285
        int r;
×
1286

1287
        c->action = ACTION_INSPECT;
×
1288

1289
        /* When only a single parameter is specified 'inspect' it's the kernel image path, and not the kernel
1290
         * version. i.e. it's the first argument that is optional, not the 2nd. That's a bit unfortunate, but
1291
         * we keep the behaviour for compatibility. If users want to specify only the version (and have the
1292
         * kernel image path derived automatically), then they may specify an empty string or "dash" as
1293
         * kernel image path. */
1294
        version = argc > 2 ? empty_or_dash_to_null(argv[1]) : NULL;
×
1295
        kernel = argc > 2 ? empty_or_dash_to_null(argv[2]) :
×
1296
                (argc > 1 ? empty_or_dash_to_null(argv[1]) : NULL);
×
1297
        initrds = strv_skip(argv, 3);
×
1298

1299
        if (!version && !arg_root) {
×
1300
                assert_se(uname(&un) >= 0);
×
1301
                version = un.release;
1302
        }
1303

1304
        if (!kernel && version) {
×
1305
                r = kernel_from_version(version, &vmlinuz);
×
1306
                if (r < 0)
×
1307
                        return r;
1308

1309
                kernel = vmlinuz;
×
1310
        }
1311

1312
        r = context_set_version(c, version);
×
1313
        if (r < 0)
×
1314
                return r;
1315

1316
        r = context_set_kernel(c, kernel);
×
1317
        if (r < 0)
×
1318
                return r;
1319

1320
        r = context_set_initrds(c, initrds);
×
1321
        if (r < 0)
×
1322
                return r;
1323

1324
        r = context_prepare_execution(c);
×
1325
        if (r < 0)
×
1326
                return r;
1327

1328
        t = table_new_vertical();
×
1329
        if (!t)
×
1330
                return log_oom();
×
1331

1332
        r = table_add_many(t,
×
1333
                           TABLE_FIELD, "Machine ID",
1334
                           TABLE_ID128, c->machine_id,
1335
                           TABLE_FIELD, "Kernel Image Type",
1336
                           TABLE_STRING, kernel_image_type_to_string(c->kernel_image_type),
1337
                           TABLE_FIELD, "Layout",
1338
                           TABLE_STRING, context_get_layout(c),
1339
                           TABLE_FIELD, "Boot Root",
1340
                           TABLE_STRING, c->boot_root,
1341
                           TABLE_FIELD, "Entry Token Type",
1342
                           TABLE_STRING, boot_entry_token_type_to_string(c->entry_token_type),
1343
                           TABLE_FIELD, "Entry Token",
1344
                           TABLE_STRING, c->entry_token,
1345
                           TABLE_FIELD, "Entry Directory",
1346
                           TABLE_STRING, c->entry_dir,
1347
                           TABLE_FIELD, "Kernel Version",
1348
                           TABLE_STRING, c->version,
1349
                           TABLE_FIELD, "Kernel",
1350
                           TABLE_STRING, c->kernel,
1351
                           TABLE_FIELD, "Initrds",
1352
                           TABLE_STRV, c->initrds,
1353
                           TABLE_FIELD, "Initrd Generator",
1354
                           TABLE_STRING, c->initrd_generator,
1355
                           TABLE_FIELD, "UKI Generator",
1356
                           TABLE_STRING, c->uki_generator,
1357
                           TABLE_FIELD, "Plugins",
1358
                           TABLE_STRV, c->plugins,
1359
                           TABLE_FIELD, "Plugin Environment",
1360
                           TABLE_STRV, c->envp);
1361
        if (r < 0)
×
1362
                return table_log_add_error(r);
×
1363

1364
        if (!sd_json_format_enabled(arg_json_format_flags)) {
×
1365
                r = table_add_many(t,
×
1366
                                   TABLE_FIELD, "Plugin Arguments",
1367
                                   TABLE_STRV, strv_skip(c->argv, 1));
1368
                if (r < 0)
×
1369
                        return table_log_add_error(r);
×
1370
        }
1371

1372
        table_set_ersatz_string(t, TABLE_ERSATZ_UNSET);
×
1373

1374
        for (size_t row = 1; row < table_get_rows(t); row++) {
×
1375
                _cleanup_free_ char *name = NULL;
×
1376

1377
                name = strdup(table_get_at(t, row, 0));
×
1378
                if (!name)
×
1379
                        return log_oom();
×
1380

1381
                r = table_set_json_field_name(t, row - 1, delete_chars(name, " "));
×
1382
                if (r < 0)
×
1383
                        return log_error_errno(r, "Failed to set JSON field name: %m");
×
1384
        }
1385

1386
        return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, /* show_header= */ false);
×
1387
}
1388

1389
static int verb_list(int argc, char *argv[], void *userdata) {
×
1390
        Context *c = ASSERT_PTR(userdata);
×
1391
        _cleanup_close_ int fd = -EBADF;
×
1392
        int r;
×
1393

1394
        fd = chase_and_openat(c->rfd, "/usr/lib/modules", CHASE_AT_RESOLVE_IN_ROOT, O_DIRECTORY|O_RDONLY|O_CLOEXEC, NULL);
×
1395
        if (fd < 0)
×
1396
                return log_error_errno(fd, "Failed to open %s/usr/lib/modules/: %m", strempty(arg_root));
×
1397

1398
        _cleanup_free_ DirectoryEntries *de = NULL;
×
1399
        r = readdir_all(fd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT, &de);
×
1400
        if (r < 0)
×
1401
                return log_error_errno(r, "Failed to numerate /usr/lib/modules/ contents: %m");
×
1402

1403
        _cleanup_(table_unrefp) Table *table = NULL;
×
1404
        table = table_new("version", "has kernel", "path");
×
1405
        if (!table)
×
1406
                return log_oom();
×
1407

1408
        table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
×
1409
        table_set_align_percent(table, table_get_cell(table, 0, 1), 100);
×
1410

1411
        FOREACH_ARRAY(d, de->entries, de->n_entries) {
×
1412
                _cleanup_free_ char *j = path_join("/usr/lib/modules/", (*d)->d_name);
×
1413
                if (!j)
×
1414
                        return log_oom();
×
1415

1416
                r = dirent_ensure_type(fd, *d);
×
1417
                if (r < 0) {
×
1418
                        if (r != -ENOENT) /* don't log if just gone by now */
×
1419
                                log_debug_errno(r, "Failed to check if '%s/%s' is a directory, ignoring: %m", strempty(arg_root), j);
×
1420
                        continue;
×
1421
                }
1422

1423
                if ((*d)->d_type != DT_DIR)
×
1424
                        continue;
×
1425

1426
                _cleanup_free_ char *fn = path_join((*d)->d_name, "vmlinuz");
×
1427
                if (!fn)
×
1428
                        return log_oom();
×
1429

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

1435
                        exists = false;
1436
                } else
1437
                        exists = true;
1438

1439
                r = table_add_many(table,
×
1440
                                   TABLE_STRING, (*d)->d_name,
1441
                                   TABLE_BOOLEAN_CHECKMARK, exists,
1442
                                   TABLE_SET_COLOR, ansi_highlight_green_red(exists),
1443
                                   TABLE_PATH, j);
1444
                if (r < 0)
×
1445
                        return table_log_add_error(r);
×
1446
        }
1447

1448
        return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
×
1449
}
1450

1451
static int help(void) {
×
1452
        _cleanup_free_ char *link = NULL;
×
1453
        int r;
×
1454

1455
        r = terminal_urlify_man("kernel-install", "8", &link);
×
1456
        if (r < 0)
×
1457
                return log_oom();
×
1458

1459
        printf("%1$s [OPTIONS...] COMMAND ...\n\n"
×
1460
               "%5$sAdd and remove kernel and initrd images to and from the boot partition.%6$s\n"
1461
               "\n%3$sUsage:%4$s\n"
1462
               "  kernel-install [OPTIONS...] add [[[KERNEL-VERSION] KERNEL-IMAGE] [INITRD ...]]\n"
1463
               "  kernel-install [OPTIONS...] add-all\n"
1464
               "  kernel-install [OPTIONS...] remove KERNEL-VERSION\n"
1465
               "  kernel-install [OPTIONS...] inspect [[[KERNEL-VERSION] KERNEL-IMAGE]\n"
1466
               "                                      [INITRD ...]]\n"
1467
               "  kernel-install [OPTIONS...] list\n"
1468
               "\n%3$sOptions:%4$s\n"
1469
               "  -h --help                    Show this help\n"
1470
               "     --version                 Show package version\n"
1471
               "  -v --verbose                 Increase verbosity\n"
1472
               "     --esp-path=PATH           Path to the EFI System Partition (ESP)\n"
1473
               "     --boot-path=PATH          Path to the $BOOT partition\n"
1474
               "     --make-entry-directory=yes|no|auto\n"
1475
               "                               Create $BOOT/ENTRY-TOKEN/ directory\n"
1476
               "     --entry-token=machine-id|os-id|os-image-id|auto|literal:…\n"
1477
               "                               Entry token to use for this installation\n"
1478
               "     --no-pager                Do not pipe inspect output into a pager\n"
1479
               "     --json=pretty|short|off   Generate JSON output\n"
1480
               "     --no-legend               Do not show the headers and footers\n"
1481
               "     --root=PATH               Operate on an alternate filesystem root\n"
1482
               "     --image=PATH              Operate on disk image as filesystem root\n"
1483
               "     --image-policy=POLICY     Specify disk image dissection policy\n"
1484
               "\n"
1485
               "This program may also be invoked as 'installkernel':\n"
1486
               "  installkernel  [OPTIONS...] VERSION VMLINUZ [MAP] [INSTALLATION-DIR]\n"
1487
               "(The optional arguments are passed by kernel build system, but ignored.)\n"
1488
               "\n"
1489
               "See the %2$s for details.\n",
1490
               program_invocation_short_name,
1491
               link,
1492
               ansi_underline(),
1493
               ansi_normal(),
1494
               ansi_highlight(),
1495
               ansi_normal());
1496

1497
        return 0;
1498
}
1499

1500
static int parse_argv(int argc, char *argv[], Context *c) {
×
1501
        enum {
×
1502
                ARG_VERSION = 0x100,
1503
                ARG_NO_LEGEND,
1504
                ARG_ESP_PATH,
1505
                ARG_BOOT_PATH,
1506
                ARG_MAKE_ENTRY_DIRECTORY,
1507
                ARG_ENTRY_TOKEN,
1508
                ARG_NO_PAGER,
1509
                ARG_JSON,
1510
                ARG_ROOT,
1511
                ARG_IMAGE,
1512
                ARG_IMAGE_POLICY,
1513
        };
1514
        static const struct option options[] = {
×
1515
                { "help",                 no_argument,       NULL, 'h'                      },
1516
                { "version",              no_argument,       NULL, ARG_VERSION              },
1517
                { "verbose",              no_argument,       NULL, 'v'                      },
1518
                { "esp-path",             required_argument, NULL, ARG_ESP_PATH             },
1519
                { "boot-path",            required_argument, NULL, ARG_BOOT_PATH            },
1520
                { "make-entry-directory", required_argument, NULL, ARG_MAKE_ENTRY_DIRECTORY },
1521
                { "entry-token",          required_argument, NULL, ARG_ENTRY_TOKEN          },
1522
                { "no-pager",             no_argument,       NULL, ARG_NO_PAGER             },
1523
                { "json",                 required_argument, NULL, ARG_JSON                 },
1524
                { "root",                 required_argument, NULL, ARG_ROOT                 },
1525
                { "image",                required_argument, NULL, ARG_IMAGE                },
1526
                { "image-policy",         required_argument, NULL, ARG_IMAGE_POLICY         },
1527
                { "no-legend",            no_argument,       NULL, ARG_NO_LEGEND            },
1528
                {}
1529
        };
1530
        int t, r;
×
1531

1532
        assert(argc >= 0);
×
1533
        assert(argv);
×
1534
        assert(c);
×
1535

1536
        while ((t = getopt_long(argc, argv, "hv", options, NULL)) >= 0)
×
1537
                switch (t) {
×
1538
                case 'h':
×
1539
                        return help();
×
1540

1541
                case ARG_VERSION:
×
1542
                        return version();
×
1543

1544
                case ARG_NO_LEGEND:
×
1545
                        arg_legend = false;
×
1546
                        break;
×
1547

1548
                case 'v':
×
1549
                        log_set_max_level(LOG_DEBUG);
×
1550
                        arg_verbose = true;
×
1551
                        break;
×
1552

1553
                case ARG_ESP_PATH:
×
1554
                        r = parse_path_argument(optarg, /* suppress_root = */ false, &arg_esp_path);
×
1555
                        if (r < 0)
×
1556
                                return log_oom();
×
1557
                        break;
1558

1559
                case ARG_BOOT_PATH:
×
1560
                        r = parse_path_argument(optarg, /* suppress_root = */ false, &arg_xbootldr_path);
×
1561
                        if (r < 0)
×
1562
                                return log_oom();
×
1563
                        break;
1564

1565
                case ARG_MAKE_ENTRY_DIRECTORY:
×
1566
                        if (streq(optarg, "auto"))
×
1567
                                arg_make_entry_directory = -1;
×
1568
                        else {
1569
                                r = parse_boolean_argument("--make-entry-directory=", optarg, NULL);
×
1570
                                if (r < 0)
×
1571
                                        return r;
1572

1573
                                arg_make_entry_directory = r;
×
1574
                        }
1575
                        break;
1576

1577
                case ARG_ENTRY_TOKEN:
×
1578
                        r = parse_boot_entry_token_type(optarg, &c->entry_token_type, &c->entry_token);
×
1579
                        if (r < 0)
×
1580
                                return r;
1581
                        break;
1582

1583
                case ARG_NO_PAGER:
×
1584
                        arg_pager_flags |= PAGER_DISABLE;
×
1585
                        break;
×
1586

1587
                case ARG_JSON:
×
1588
                        r = parse_json_argument(optarg, &arg_json_format_flags);
×
1589
                        if (r < 0)
×
1590
                                return r;
1591
                        break;
1592

1593
                case ARG_ROOT:
×
1594
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_root);
×
1595
                        if (r < 0)
×
1596
                                return r;
1597
                        break;
1598

1599
                case ARG_IMAGE:
×
1600
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
×
1601
                        if (r < 0)
×
1602
                                return r;
1603
                        break;
1604

1605
                case ARG_IMAGE_POLICY:
×
1606
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
1607
                        if (r < 0)
×
1608
                                return r;
1609
                        break;
1610

1611
                case '?':
1612
                        return -EINVAL;
1613

1614
                default:
×
1615
                        assert_not_reached();
×
1616
                }
1617

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

1621
        return 1;
1622
}
1623

1624
static int run(int argc, char* argv[]) {
×
1625
        static const Verb verbs[] = {
×
1626
                { "add",         1,        VERB_ANY, 0,            verb_add            },
1627
                { "add-all",     1,        1,        0,            verb_add_all        },
1628
                { "remove",      2,        VERB_ANY, 0,            verb_remove         },
1629
                { "inspect",     1,        VERB_ANY, VERB_DEFAULT, verb_inspect        },
1630
                { "list",        1,        1,        0,            verb_list           },
1631
                {}
1632
        };
1633
        _cleanup_(context_done) Context c = {
×
1634
                .rfd = AT_FDCWD,
1635
                .action = _ACTION_INVALID,
1636
                .kernel_image_type = KERNEL_IMAGE_TYPE_UNKNOWN,
1637
                .layout = _LAYOUT_INVALID,
1638
                .entry_token_type = BOOT_ENTRY_TOKEN_AUTO,
1639
        };
1640
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
×
1641
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
×
1642
        int r;
×
1643

1644
        log_setup();
×
1645

1646
        r = parse_argv(argc, argv, &c);
×
1647
        if (r <= 0)
×
1648
                return r;
1649

1650
        if (arg_image) {
×
1651
                assert(!arg_root);
×
1652

1653
                r = mount_image_privately_interactively(
×
1654
                                arg_image,
1655
                                arg_image_policy,
1656
                                DISSECT_IMAGE_GENERIC_ROOT |
1657
                                DISSECT_IMAGE_REQUIRE_ROOT |
1658
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
1659
                                DISSECT_IMAGE_VALIDATE_OS |
1660
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
1661
                                &mounted_dir,
1662
                                /* ret_dir_fd= */ NULL,
1663
                                &loop_device);
1664
                if (r < 0)
×
1665
                        return r;
1666

1667
                arg_root = strdup(mounted_dir);
×
1668
                if (!arg_root)
×
1669
                        return log_oom();
×
1670
        }
1671

1672
        r = context_init(&c);
×
1673
        if (r < 0)
×
1674
                return r;
1675

1676
        if (invoked_as(argv, "installkernel"))
×
1677
                return run_as_installkernel(argc, argv, &c);
×
1678

1679
        return dispatch_verb(argc, argv, verbs, &c);
×
1680
}
1681

1682
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