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

systemd / systemd / 15789897806

20 Jun 2025 05:25PM UTC coverage: 72.105% (+0.06%) from 72.045%
15789897806

push

github

web-flow
bootctl: honour architecture when updating boot loaders (#37913)

Fixes: #33413
Follow-up for: #30418

21 of 29 new or added lines in 2 files covered. (72.41%)

1701 existing lines in 55 files now uncovered.

300497 of 416750 relevant lines covered (72.1%)

721912.12 hits per line

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

75.51
/src/firstboot/firstboot.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <fcntl.h>
4
#include <getopt.h>
5
#include <unistd.h>
6

7
#include "sd-bus.h"
8
#include "sd-id128.h"
9

10
#include "alloc-util.h"
11
#include "ask-password-api.h"
12
#include "build.h"
13
#include "bus-error.h"
14
#include "bus-locator.h"
15
#include "bus-unit-util.h"
16
#include "bus-util.h"
17
#include "bus-wait-for-jobs.h"
18
#include "chase.h"
19
#include "copy.h"
20
#include "creds-util.h"
21
#include "dissect-image.h"
22
#include "env-file.h"
23
#include "errno-util.h"
24
#include "fd-util.h"
25
#include "fileio.h"
26
#include "fs-util.h"
27
#include "glyph-util.h"
28
#include "hostname-util.h"
29
#include "image-policy.h"
30
#include "kbd-util.h"
31
#include "label.h"
32
#include "label-util.h"
33
#include "libcrypt-util.h"
34
#include "locale-util.h"
35
#include "lock-util.h"
36
#include "loop-util.h"
37
#include "main-func.h"
38
#include "memory-util.h"
39
#include "mount-util.h"
40
#include "os-util.h"
41
#include "parse-argument.h"
42
#include "parse-util.h"
43
#include "password-quality-util.h"
44
#include "path-util.h"
45
#include "pretty-print.h"
46
#include "proc-cmdline.h"
47
#include "runtime-scope.h"
48
#include "smack-util.h"
49
#include "stat-util.h"
50
#include "string-util.h"
51
#include "strv.h"
52
#include "terminal-util.h"
53
#include "time-util.h"
54
#include "tmpfile-util-label.h"
55
#include "user-util.h"
56
#include "vconsole-util.h"
57

58
static char *arg_root = NULL;
59
static char *arg_image = NULL;
60
static char *arg_locale = NULL;  /* $LANG */
61
static char *arg_locale_messages = NULL; /* $LC_MESSAGES */
62
static char *arg_keymap = NULL;
63
static char *arg_timezone = NULL;
64
static char *arg_hostname = NULL;
65
static sd_id128_t arg_machine_id = {};
66
static char *arg_root_password = NULL;
67
static char *arg_root_shell = NULL;
68
static char *arg_kernel_cmdline = NULL;
69
static bool arg_prompt_locale = false;
70
static bool arg_prompt_keymap = false;
71
static bool arg_prompt_timezone = false;
72
static bool arg_prompt_hostname = false;
73
static bool arg_prompt_root_password = false;
74
static bool arg_prompt_root_shell = false;
75
static bool arg_copy_locale = false;
76
static bool arg_copy_keymap = false;
77
static bool arg_copy_timezone = false;
78
static bool arg_copy_root_password = false;
79
static bool arg_copy_root_shell = false;
80
static bool arg_force = false;
81
static bool arg_delete_root_password = false;
82
static bool arg_root_password_is_hashed = false;
83
static bool arg_welcome = true;
84
static bool arg_reset = false;
85
static ImagePolicy *arg_image_policy = NULL;
86

87
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
138✔
88
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
138✔
89
STATIC_DESTRUCTOR_REGISTER(arg_locale, freep);
138✔
90
STATIC_DESTRUCTOR_REGISTER(arg_locale_messages, freep);
138✔
91
STATIC_DESTRUCTOR_REGISTER(arg_keymap, freep);
138✔
92
STATIC_DESTRUCTOR_REGISTER(arg_timezone, freep);
138✔
93
STATIC_DESTRUCTOR_REGISTER(arg_hostname, freep);
138✔
94
STATIC_DESTRUCTOR_REGISTER(arg_root_password, erase_and_freep);
138✔
95
STATIC_DESTRUCTOR_REGISTER(arg_root_shell, freep);
138✔
96
STATIC_DESTRUCTOR_REGISTER(arg_kernel_cmdline, freep);
138✔
97
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
138✔
98

99
static void print_welcome(int rfd) {
7✔
100
        _cleanup_free_ char *pretty_name = NULL, *os_name = NULL, *ansi_color = NULL;
7✔
101
        static bool done = false;
7✔
102
        const char *pn, *ac;
7✔
103
        int r;
7✔
104

105
        assert(rfd >= 0);
7✔
106

107
        if (!arg_welcome)
7✔
108
                return;
109

110
        if (done) {
6✔
111
                putchar('\n'); /* Add some breathing room between multiple prompts */
1✔
112
                return;
113
        }
114

115
        r = parse_os_release_at(rfd,
6✔
116
                                "PRETTY_NAME", &pretty_name,
117
                                "NAME", &os_name,
118
                                "ANSI_COLOR", &ansi_color);
119
        if (r < 0)
6✔
120
                log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
6✔
121
                               "Failed to read os-release file, ignoring: %m");
122

123
        pn = os_release_pretty_name(pretty_name, os_name);
6✔
124
        ac = isempty(ansi_color) ? "0" : ansi_color;
6✔
125

126
        (void) terminal_reset_defensive_locked(STDOUT_FILENO, /* flags= */ 0);
6✔
127

128
        if (colors_enabled())
6✔
UNCOV
129
                printf("\n"
×
130
                       ANSI_HIGHLIGHT "Welcome to your new installation of " ANSI_NORMAL "\x1B[%sm%s" ANSI_HIGHLIGHT "!" ANSI_NORMAL "\n", ac, pn);
131
        else
132
                printf("\nWelcome to your new installation of %s!\n", pn);
6✔
133

134
        putchar('\n');
6✔
135
        if (emoji_enabled()) {
6✔
UNCOV
136
                fputs(glyph(GLYPH_SPARKLES), stdout);
×
UNCOV
137
                putchar(' ');
×
138
        }
139
        printf("Please configure your new system!\n");
6✔
140

141
        any_key_to_proceed();
6✔
142

143
        done = true;
6✔
144
}
145

UNCOV
146
static int get_completions(
×
147
                const char *key,
148
                char ***ret_list,
149
                void *userdata) {
150

151
        int r;
×
152

153
        if (!userdata) {
×
UNCOV
154
                *ret_list = NULL;
×
UNCOV
155
                return 0;
×
156
        }
157

UNCOV
158
        _cleanup_strv_free_ char **copy = strv_copy(userdata);
×
UNCOV
159
        if (!copy)
×
160
                return -ENOMEM;
161

UNCOV
162
        r = strv_extend(&copy, "list");
×
UNCOV
163
        if (r < 0)
×
164
                return r;
165

UNCOV
166
        *ret_list = TAKE_PTR(copy);
×
UNCOV
167
        return 0;
×
168
}
169

170
static int prompt_loop(
7✔
171
                int rfd,
172
                const char *text,
173
                char **l,
174
                unsigned ellipsize_percentage,
175
                bool (*is_valid)(int rfd, const char *name),
176
                char **ret) {
177

178
        int r;
7✔
179

180
        assert(text);
7✔
181
        assert(is_valid);
7✔
182
        assert(ret);
7✔
183

184
        for (;;) {
7✔
185
                _cleanup_free_ char *p = NULL;
7✔
186

187
                r = ask_string_full(
7✔
188
                                &p,
189
                                get_completions,
190
                                l,
191
                                strv_isempty(l) ? "%s %s (empty to skip): "
7✔
192
                                                : "%s %s (empty to skip, \"list\" to list options): ",
193
                                glyph(GLYPH_TRIANGULAR_BULLET), text);
194
                if (r < 0)
7✔
UNCOV
195
                        return log_error_errno(r, "Failed to query user: %m");
×
196

197
                if (isempty(p)) {
7✔
UNCOV
198
                        log_info("No data entered, skipping.");
×
UNCOV
199
                        return 0;
×
200
                }
201

202
                if (!strv_isempty(l)) {
7✔
203
                        if (streq(p, "list")) {
4✔
UNCOV
204
                                r = show_menu(l,
×
205
                                              /* n_columns= */ 3,
206
                                              /* column_width= */ 20,
207
                                              ellipsize_percentage,
208
                                              /* grey_prefix= */ NULL,
209
                                              /* with_numbers= */ true);
UNCOV
210
                                if (r < 0)
×
211
                                        return log_error_errno(r, "Failed to show menu: %m");
×
212

UNCOV
213
                                putchar('\n');
×
UNCOV
214
                                continue;
×
215
                        }
216

217
                        unsigned u;
4✔
218
                        r = safe_atou(p, &u);
4✔
219
                        if (r >= 0) {
4✔
220
                                if (u <= 0 || u > strv_length(l)) {
×
UNCOV
221
                                        log_error("Specified entry number out of range.");
×
UNCOV
222
                                        continue;
×
223
                                }
224

UNCOV
225
                                log_info("Selected '%s'.", l[u-1]);
×
UNCOV
226
                                return free_and_strdup_warn(ret, l[u-1]);
×
227
                        }
228
                }
229

230
                if (is_valid(rfd, p))
7✔
231
                        return free_and_replace(*ret, p);
7✔
232

233
                /* Be more helpful to the user, and give a hint what the user might have wanted to type. */
234
                const char *best_match = strv_find_closest(l, p);
×
UNCOV
235
                if (best_match)
×
236
                        log_error("Invalid data '%s', did you mean '%s'?", p, best_match);
×
237
                else
UNCOV
238
                        log_error("Invalid data '%s'.", p);
×
239
        }
240
}
241

242
static int should_configure(int dir_fd, const char *filename) {
1,080✔
243
        _cleanup_fclose_ FILE *passwd = NULL, *shadow = NULL;
1,080✔
244
        int r;
1,080✔
245

246
        assert(dir_fd >= 0);
1,080✔
247
        assert(filename);
1,080✔
248

249
        if (streq(filename, "passwd") && !arg_force)
1,080✔
250
                /* We may need to do additional checks, so open the file. */
251
                r = xfopenat(dir_fd, filename, "re", O_NOFOLLOW, &passwd);
133✔
252
        else
253
                r = RET_NERRNO(faccessat(dir_fd, filename, F_OK, AT_SYMLINK_NOFOLLOW));
947✔
254

255
        if (r == -ENOENT)
524✔
256
                return true; /* missing */
257
        if (r < 0)
666✔
UNCOV
258
                return log_error_errno(r, "Failed to access %s: %m", filename);
×
259
        if (arg_force)
666✔
260
                return true; /* exists, but if --force was given we should still configure the file. */
261

262
        if (!passwd)
652✔
263
                return false;
264

265
        /* In case of /etc/passwd, do an additional check for the root password field.
266
         * We first check that passwd redirects to shadow, and then we check shadow.
267
         */
268
        struct passwd *i;
269
        while ((r = fgetpwent_sane(passwd, &i)) > 0) {
110✔
270
                if (!streq(i->pw_name, "root"))
109✔
UNCOV
271
                        continue;
×
272

273
                if (streq_ptr(i->pw_passwd, PASSWORD_SEE_SHADOW))
109✔
274
                        break;
UNCOV
275
                log_debug("passwd: root account with non-shadow password found, treating root as configured");
×
276
                return false;
277
        }
278
        if (r < 0)
110✔
UNCOV
279
                return log_error_errno(r, "Failed to read %s: %m", filename);
×
280
        if (r == 0) {
110✔
281
                log_debug("No root account found in %s, assuming root is not configured.", filename);
1✔
282
                return true;
1✔
283
        }
284

285
        r = xfopenat(dir_fd, "shadow", "re", O_NOFOLLOW, &shadow);
109✔
286
        if (r == -ENOENT) {
109✔
UNCOV
287
                log_debug("No shadow file found, assuming root is not configured.");
×
UNCOV
288
                return true; /* missing */
×
289
        }
290
        if (r < 0)
109✔
UNCOV
291
                return log_error_errno(r, "Failed to access shadow: %m");
×
292

293
        struct spwd *j;
294
        while ((r = fgetspent_sane(shadow, &j)) > 0) {
109✔
295
                if (!streq(j->sp_namp, "root"))
109✔
UNCOV
296
                        continue;
×
297

298
                bool unprovisioned = streq_ptr(j->sp_pwdp, PASSWORD_UNPROVISIONED);
109✔
299
                log_debug("Root account found, %s.",
212✔
300
                          unprovisioned ? "with unprovisioned password, treating root as not configured" :
301
                                          "treating root as configured");
302
                return unprovisioned;
109✔
303
        }
304
        if (r < 0)
×
305
                return log_error_errno(r, "Failed to read shadow: %m");
×
UNCOV
306
        assert(r == 0);
×
UNCOV
307
        log_debug("No root account found in shadow, assuming root is not configured.");
×
308
        return true;
309
}
310

UNCOV
311
static bool locale_is_installed_bool(const char *name) {
×
UNCOV
312
        return locale_is_installed(name) > 0;
×
313
}
314

315
static bool locale_is_ok(int rfd, const char *name) {
10✔
316
        int r;
10✔
317

318
        assert(rfd >= 0);
10✔
319

320
        r = dir_fd_is_root(rfd);
10✔
321
        if (r < 0)
10✔
UNCOV
322
                log_debug_errno(r, "Unable to determine if operating on host root directory, assuming we are: %m");
×
323

324
        return r != 0 ? locale_is_installed_bool(name) : locale_is_valid(name);
10✔
325
}
326

327
static int prompt_locale(int rfd) {
10✔
UNCOV
328
        _cleanup_strv_free_ char **locales = NULL;
×
329
        bool acquired_from_creds = false;
10✔
330
        int r;
10✔
331

332
        assert(rfd >= 0);
10✔
333

334
        if (arg_locale || arg_locale_messages)
10✔
335
                return 0;
336

337
        r = read_credential("firstboot.locale", (void**) &arg_locale, NULL);
6✔
338
        if (r < 0)
6✔
339
                log_debug_errno(r, "Failed to read credential firstboot.locale, ignoring: %m");
5✔
340
        else
341
                acquired_from_creds = true;
342

343
        r = read_credential("firstboot.locale-messages", (void**) &arg_locale_messages, NULL);
6✔
344
        if (r < 0)
6✔
345
                log_debug_errno(r, "Failed to read credential firstboot.locale-messages, ignoring: %m");
5✔
346
        else
347
                acquired_from_creds = true;
348

349
        if (acquired_from_creds) {
5✔
350
                log_debug("Acquired locale from credentials.");
1✔
351
                return 0;
1✔
352
        }
353

354
        if (!arg_prompt_locale) {
5✔
355
                log_debug("Prompting for locale was not requested.");
4✔
356
                return 0;
4✔
357
        }
358

359
        r = get_locales(&locales);
1✔
360
        if (r < 0)
1✔
UNCOV
361
                return log_error_errno(r, "Cannot query locales list: %m");
×
362

363
        if (strv_isempty(locales))
1✔
UNCOV
364
                log_debug("No locales found, skipping locale selection.");
×
365
        else if (strv_length(locales) == 1) {
1✔
366

UNCOV
367
                if (streq(locales[0], SYSTEMD_DEFAULT_LOCALE))
×
368
                        log_debug("Only installed locale is default locale anyway, not setting locale explicitly.");
×
369
                else {
370
                        log_debug("Only a single locale available (%s), selecting it as default.", locales[0]);
×
371

372
                        arg_locale = strdup(locales[0]);
×
UNCOV
373
                        if (!arg_locale)
×
UNCOV
374
                                return log_oom();
×
375

376
                        /* Not setting arg_locale_message here, since it defaults to LANG anyway */
377
                }
378
        } else {
379
                print_welcome(rfd);
1✔
380

381
                r = prompt_loop(rfd, "Please enter the new system locale name or number",
1✔
382
                                locales, 60, locale_is_ok, &arg_locale);
383
                if (r < 0)
1✔
384
                        return r;
385

386
                if (isempty(arg_locale))
11✔
387
                        return 0;
388

389
                r = prompt_loop(rfd, "Please enter the new system message locale name or number",
1✔
390
                                locales, 60, locale_is_ok, &arg_locale_messages);
391
                if (r < 0)
1✔
392
                        return r;
393

394
                /* Suppress the messages setting if it's the same as the main locale anyway */
395
                if (streq_ptr(arg_locale, arg_locale_messages))
1✔
UNCOV
396
                        arg_locale_messages = mfree(arg_locale_messages);
×
397
        }
398

399
        return 0;
400
}
401

402
static int process_locale(int rfd) {
135✔
403
        _cleanup_close_ int pfd = -EBADF;
135✔
404
        _cleanup_free_ char *f = NULL;
135✔
405
        char* locales[3];
135✔
406
        unsigned i = 0;
135✔
407
        int r;
135✔
408

409
        assert(rfd >= 0);
135✔
410

411
        pfd = chase_and_open_parent_at(rfd, "/etc/locale.conf",
135✔
412
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
413
                                       &f);
414
        if (pfd < 0)
135✔
UNCOV
415
                return log_error_errno(pfd, "Failed to chase /etc/locale.conf: %m");
×
416

417
        r = should_configure(pfd, f);
135✔
418
        if (r == 0)
135✔
419
                log_debug("Found /etc/locale.conf, assuming locale information has been configured.");
123✔
420
        if (r <= 0)
135✔
421
                return r;
422

423
        r = dir_fd_is_root(rfd);
12✔
424
        if (r < 0)
12✔
UNCOV
425
                return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
×
426

427
        if (arg_copy_locale && r == 0) {
12✔
428
                r = copy_file_atomic_at(AT_FDCWD, "/etc/locale.conf", pfd, f, 0644, COPY_REFLINK);
2✔
429
                if (r != -ENOENT) {
2✔
430
                        if (r < 0)
2✔
UNCOV
431
                                return log_error_errno(r, "Failed to copy host's /etc/locale.conf: %m");
×
432

433
                        log_info("Copied host's /etc/locale.conf.");
2✔
434
                        return 0;
2✔
435
                }
436
        }
437

438
        r = prompt_locale(rfd);
10✔
439
        if (r < 0)
10✔
440
                return r;
441

442
        if (!isempty(arg_locale))
10✔
443
                locales[i++] = strjoina("LANG=", arg_locale);
25✔
444
        if (!isempty(arg_locale_messages) && !streq_ptr(arg_locale_messages, arg_locale))
15✔
445
                locales[i++] = strjoina("LC_MESSAGES=", arg_locale_messages);
25✔
446

447
        if (i == 0)
10✔
448
                return 0;
449

450
        locales[i] = NULL;
6✔
451

452
        r = write_env_file(
6✔
453
                        pfd,
454
                        f,
455
                        /* headers= */ NULL,
456
                        locales,
457
                        WRITE_ENV_FILE_LABEL);
458
        if (r < 0)
6✔
459
                return log_error_errno(r, "Failed to write /etc/locale.conf: %m");
×
460

461
        log_info("/etc/locale.conf written.");
6✔
462
        return 1;
463
}
464

UNCOV
465
static bool keymap_exists_bool(const char *name) {
×
UNCOV
466
        return keymap_exists(name) > 0;
×
467
}
468

469
static bool keymap_is_ok(int rfd, const char* name) {
4✔
470
        int r;
4✔
471

472
        assert(rfd >= 0);
4✔
473

474
        r = dir_fd_is_root(rfd);
4✔
475
        if (r < 0)
4✔
UNCOV
476
                log_debug_errno(r, "Unable to determine if operating on host root directory, assuming we are: %m");
×
477

478
        return r != 0 ? keymap_exists_bool(name) : keymap_is_valid(name);
4✔
479
}
480

481
static int prompt_keymap(int rfd) {
12✔
482
        _cleanup_strv_free_ char **kmaps = NULL;
12✔
483
        int r;
12✔
484

485
        assert(rfd >= 0);
12✔
486

487
        if (arg_keymap)
12✔
488
                return 0;
489

490
        r = read_credential("firstboot.keymap", (void**) &arg_keymap, NULL);
10✔
491
        if (r < 0)
10✔
492
                log_debug_errno(r, "Failed to read credential firstboot.keymap, ignoring: %m");
9✔
493
        else {
494
                log_debug("Acquired keymap from credential.");
1✔
495
                return 0;
1✔
496
        }
497

498
        if (!arg_prompt_keymap) {
9✔
499
                log_debug("Prompting for keymap was not requested.");
8✔
500
                return 0;
8✔
501
        }
502

503
        r = get_keymaps(&kmaps);
1✔
504
        if (r == -ENOENT) /* no keymaps installed */
1✔
UNCOV
505
                return log_debug_errno(r, "No keymaps are installed.");
×
506
        if (r < 0)
1✔
UNCOV
507
                return log_error_errno(r, "Failed to read keymaps: %m");
×
508

509
        print_welcome(rfd);
1✔
510

511
        return prompt_loop(rfd, "Please enter the new keymap name or number",
1✔
512
                           kmaps, 60, keymap_is_ok, &arg_keymap);
513
}
514

515
static int process_keymap(int rfd) {
135✔
516
        _cleanup_close_ int pfd = -EBADF;
135✔
517
        _cleanup_free_ char *f = NULL;
135✔
UNCOV
518
        _cleanup_strv_free_ char **keymap = NULL;
×
519
        int r;
135✔
520

521
        assert(rfd >= 0);
135✔
522

523
        pfd = chase_and_open_parent_at(rfd, "/etc/vconsole.conf",
135✔
524
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
525
                                       &f);
526
        if (pfd < 0)
135✔
UNCOV
527
                return log_error_errno(pfd, "Failed to chase /etc/vconsole.conf: %m");
×
528

529
        r = should_configure(pfd, f);
135✔
530
        if (r == 0)
135✔
531
                log_debug("Found /etc/vconsole.conf, assuming console has been configured.");
121✔
532
        if (r <= 0)
135✔
533
                return r;
534

535
        r = dir_fd_is_root(rfd);
14✔
536
        if (r < 0)
14✔
UNCOV
537
                return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
×
538

539
        if (arg_copy_keymap && r == 0) {
14✔
540
                r = copy_file_atomic_at(AT_FDCWD, "/etc/vconsole.conf", pfd, f, 0644, COPY_REFLINK);
2✔
541
                if (r != -ENOENT) {
2✔
542
                        if (r < 0)
2✔
UNCOV
543
                                return log_error_errno(r, "Failed to copy host's /etc/vconsole.conf: %m");
×
544

545
                        log_info("Copied host's /etc/vconsole.conf.");
2✔
546
                        return 0;
2✔
547
                }
548
        }
549

550
        r = prompt_keymap(rfd);
12✔
551
        if (r == -ENOENT)
12✔
552
                return 0; /* don't fail if no keymaps are installed */
553
        if (r < 0)
12✔
554
                return r;
555

556
        if (isempty(arg_keymap))
139✔
557
                return 0;
558

559
        VCContext vc = {
4✔
560
                .keymap = arg_keymap,
561
        };
562
        _cleanup_(x11_context_clear) X11Context xc = {};
4✔
563

564
        r = vconsole_convert_to_x11(&vc, /* verify= */ NULL, &xc);
4✔
565
        if (r < 0)
4✔
UNCOV
566
                return log_error_errno(r, "Failed to convert keymap data: %m");
×
567

568
        r = vconsole_serialize(&vc, &xc, &keymap);
4✔
569
        if (r < 0)
4✔
UNCOV
570
                return log_error_errno(r, "Failed to serialize keymap data: %m");
×
571

572
        r = write_vconsole_conf(pfd, f, keymap);
4✔
573
        if (r < 0)
4✔
UNCOV
574
                return log_error_errno(r, "Failed to write /etc/vconsole.conf: %m");
×
575

576
        log_info("/etc/vconsole.conf written.");
4✔
577
        return 1;
578
}
579

580
static bool timezone_is_ok(int rfd, const char *name) {
1✔
581
        assert(rfd >= 0);
1✔
582

583
        return timezone_is_valid(name, LOG_DEBUG);
1✔
584
}
585

586
static int prompt_timezone(int rfd) {
73✔
587
        _cleanup_strv_free_ char **zones = NULL;
73✔
588
        int r;
73✔
589

590
        assert(rfd >= 0);
73✔
591

592
        if (arg_timezone)
73✔
593
                return 0;
594

595
        r = read_credential("firstboot.timezone", (void**) &arg_timezone, NULL);
71✔
596
        if (r < 0)
71✔
597
                log_debug_errno(r, "Failed to read credential firstboot.timezone, ignoring: %m");
11✔
598
        else {
599
                log_debug("Acquired timezone from credential.");
60✔
600
                return 0;
60✔
601
        }
602

603
        if (!arg_prompt_timezone) {
11✔
604
                log_debug("Prompting for timezone was not requested.");
10✔
605
                return 0;
10✔
606
        }
607

608
        r = get_timezones(&zones);
1✔
609
        if (r < 0)
1✔
UNCOV
610
                return log_error_errno(r, "Cannot query timezone list: %m");
×
611

612
        print_welcome(rfd);
1✔
613

614
        return prompt_loop(rfd, "Please enter the new timezone name or number",
1✔
615
                           zones, 30, timezone_is_ok, &arg_timezone);
616
}
617

618
static int process_timezone(int rfd) {
135✔
619
        _cleanup_close_ int pfd = -EBADF;
135✔
620
        _cleanup_free_ char *f = NULL;
135✔
621
        const char *e;
135✔
622
        int r;
135✔
623

624
        assert(rfd >= 0);
135✔
625

626
        pfd = chase_and_open_parent_at(rfd, "/etc/localtime",
135✔
627
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
628
                                       &f);
629
        if (pfd < 0)
135✔
UNCOV
630
                return log_error_errno(pfd, "Failed to chase /etc/localtime: %m");
×
631

632
        r = should_configure(pfd, f);
135✔
633
        if (r == 0)
135✔
634
                log_debug("Found /etc/localtime, assuming timezone has been configured.");
60✔
635
        if (r <= 0)
135✔
636
                return r;
637

638
        r = dir_fd_is_root(rfd);
75✔
639
        if (r < 0)
75✔
UNCOV
640
                return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
×
641

642
        if (arg_copy_timezone && r == 0) {
75✔
643
                _cleanup_free_ char *s = NULL;
2✔
644

645
                r = readlink_malloc("/etc/localtime", &s);
2✔
646
                if (r != -ENOENT) {
2✔
647
                        if (r < 0)
2✔
UNCOV
648
                                return log_error_errno(r, "Failed to read host's /etc/localtime: %m");
×
649

650
                        r = symlinkat_atomic_full(s, pfd, f, SYMLINK_LABEL);
2✔
651
                        if (r < 0)
2✔
UNCOV
652
                                return log_error_errno(r, "Failed to create /etc/localtime symlink: %m");
×
653

654
                        log_info("Copied host's /etc/localtime.");
2✔
655
                        return 0;
2✔
656
                }
657
        }
658

659
        r = prompt_timezone(rfd);
73✔
660
        if (r < 0)
73✔
661
                return r;
662

663
        if (isempty(arg_timezone))
198✔
664
                return 0;
665

666
        e = strjoina("../usr/share/zoneinfo/", arg_timezone);
315✔
667

668
        r = symlinkat_atomic_full(e, pfd, f, SYMLINK_LABEL);
63✔
669
        if (r < 0)
63✔
UNCOV
670
                return log_error_errno(r, "Failed to create /etc/localtime symlink: %m");
×
671

672
        log_info("/etc/localtime written");
63✔
673
        return 0;
674
}
675

676
static bool hostname_is_ok(int rfd, const char *name) {
1✔
677
        assert(rfd >= 0);
1✔
678

679
        return hostname_is_valid(name, VALID_HOSTNAME_TRAILING_DOT);
1✔
680
}
681

682
static int prompt_hostname(int rfd) {
121✔
683
        int r;
121✔
684

685
        assert(rfd >= 0);
121✔
686

687
        if (arg_hostname)
121✔
688
                return 0;
689

690
        if (!arg_prompt_hostname) {
119✔
691
                log_debug("Prompting for hostname was not requested.");
118✔
692
                return 0;
118✔
693
        }
694

695
        print_welcome(rfd);
1✔
696

697
        r = prompt_loop(rfd, "Please enter the new hostname",
1✔
698
                        NULL, 0, hostname_is_ok, &arg_hostname);
699
        if (r < 0)
1✔
700
                return r;
701

702
        if (arg_hostname)
1✔
703
                hostname_cleanup(arg_hostname);
1✔
704

705
        return 0;
706
}
707

708
static int process_hostname(int rfd) {
135✔
709
        _cleanup_close_ int pfd = -EBADF;
135✔
710
        _cleanup_free_ char *f = NULL;
135✔
711
        int r;
135✔
712

713
        assert(rfd >= 0);
135✔
714

715
        pfd = chase_and_open_parent_at(rfd, "/etc/hostname",
135✔
716
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN,
717
                                       &f);
718
        if (pfd < 0)
135✔
UNCOV
719
                return log_error_errno(pfd, "Failed to chase /etc/hostname: %m");
×
720

721
        r = should_configure(pfd, f);
135✔
722
        if (r == 0)
135✔
723
                log_debug("Found /etc/hostname, assuming hostname has been configured.");
14✔
724
        if (r <= 0)
135✔
725
                return r;
726

727
        r = prompt_hostname(rfd);
121✔
728
        if (r < 0)
121✔
729
                return r;
730

731
        if (isempty(arg_hostname))
138✔
732
                return 0;
733

734
        r = write_string_file_at(pfd, f, arg_hostname,
3✔
735
                                 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL);
736
        if (r < 0)
3✔
UNCOV
737
                return log_error_errno(r, "Failed to write /etc/hostname: %m");
×
738

739
        log_info("/etc/hostname written.");
3✔
740
        return 0;
741
}
742

743
static int process_machine_id(int rfd) {
135✔
744
        _cleanup_close_ int pfd = -EBADF;
135✔
745
        _cleanup_free_ char *f = NULL;
135✔
746
        int r;
135✔
747

748
        assert(rfd >= 0);
135✔
749

750
        pfd = chase_and_open_parent_at(rfd, "/etc/machine-id",
135✔
751
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
752
                                       &f);
753
        if (pfd < 0)
135✔
UNCOV
754
                return log_error_errno(pfd, "Failed to chase /etc/machine-id: %m");
×
755

756
        r = should_configure(pfd, f);
135✔
757
        if (r == 0)
135✔
758
                log_debug("Found /etc/machine-id, assuming machine-id has been configured.");
113✔
759
        if (r <= 0)
135✔
760
                return r;
761

762
        if (sd_id128_is_null(arg_machine_id)) {
22✔
763
                log_debug("Initialization of machine-id was not requested, skipping.");
19✔
764
                return 0;
19✔
765
        }
766

767
        r = write_string_file_at(pfd, "machine-id", SD_ID128_TO_STRING(arg_machine_id),
3✔
768
                                 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL);
769
        if (r < 0)
3✔
UNCOV
770
                return log_error_errno(r, "Failed to write /etc/machine-id: %m");
×
771

772
        log_info("/etc/machine-id written.");
3✔
773
        return 0;
774
}
775

776
static int prompt_root_password(int rfd) {
27✔
777
        const char *msg1, *msg2;
27✔
778
        int r;
27✔
779

780
        assert(rfd >= 0);
27✔
781

782
        if (arg_root_password)
27✔
783
                return 0;
784

785
        if (get_credential_user_password("root", &arg_root_password, &arg_root_password_is_hashed) >= 0)
18✔
786
                return 0;
787

788
        if (!arg_prompt_root_password) {
17✔
789
                log_debug("Prompting for root password was not requested.");
16✔
790
                return 0;
16✔
791
        }
792

793
        print_welcome(rfd);
1✔
794

795
        msg1 = strjoina(glyph(GLYPH_TRIANGULAR_BULLET), " Please enter the new root password (empty to skip):");
5✔
796
        msg2 = strjoina(glyph(GLYPH_TRIANGULAR_BULLET), " Please enter the new root password again:");
5✔
797

798
        suggest_passwords();
1✔
799

UNCOV
800
        for (;;) {
×
801
                _cleanup_strv_free_erase_ char **a = NULL, **b = NULL;
1✔
802
                _cleanup_free_ char *error = NULL;
1✔
803

804
                AskPasswordRequest req = {
1✔
805
                        .tty_fd = -EBADF,
806
                        .message = msg1,
807
                        .until = USEC_INFINITY,
808
                        .hup_fd = -EBADF,
809
                };
810

811
                r = ask_password_tty(&req, /* flags= */ 0, &a);
1✔
812
                if (r < 0)
1✔
UNCOV
813
                        return log_error_errno(r, "Failed to query root password: %m");
×
814
                if (strv_length(a) != 1)
1✔
UNCOV
815
                        return log_error_errno(SYNTHETIC_ERRNO(EIO),
×
816
                                               "Received multiple passwords, where we expected one.");
817

818
                if (isempty(*a)) {
1✔
819
                        log_info("No password entered, skipping.");
1✔
820
                        break;
821
                }
822

UNCOV
823
                r = check_password_quality(*a, /* old */ NULL, "root", &error);
×
824
                if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
×
UNCOV
825
                        log_warning("Password quality check is not supported, proceeding anyway.");
×
826
                else if (r < 0)
×
827
                        return log_error_errno(r, "Failed to check password quality: %m");
×
828
                else if (r == 0)
×
829
                        log_warning("Password is weak, accepting anyway: %s", error);
×
830

UNCOV
831
                req.message = msg2;
×
832

833
                r = ask_password_tty(&req, /* flags= */ 0, &b);
×
834
                if (r < 0)
×
835
                        return log_error_errno(r, "Failed to query root password: %m");
×
UNCOV
836
                if (strv_length(b) != 1)
×
UNCOV
837
                        return log_error_errno(SYNTHETIC_ERRNO(EIO),
×
838
                                               "Received multiple passwords, where we expected one.");
839

UNCOV
840
                if (!streq(*a, *b)) {
×
UNCOV
841
                        log_error("Entered passwords did not match, please try again.");
×
UNCOV
842
                        continue;
×
843
                }
844

UNCOV
845
                arg_root_password = TAKE_PTR(*a);
×
UNCOV
846
                break;
×
847
        }
848

849
        return 0;
1✔
850
}
851

852
static int find_shell(int rfd, const char *path) {
7✔
853
        int r;
7✔
854

855
        assert(path);
7✔
856

857
        if (!valid_shell(path))
7✔
UNCOV
858
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "%s is not a valid shell", path);
×
859

860
        r = chaseat(rfd, path, CHASE_AT_RESOLVE_IN_ROOT, NULL, NULL);
7✔
861
        if (r < 0)
7✔
862
                return log_error_errno(r, "Failed to resolve shell %s: %m", path);
1✔
863

864
        return 0;
865
}
866

867
static bool shell_is_ok(int rfd, const char *path) {
2✔
868
        assert(rfd >= 0);
2✔
869

870
        return find_shell(rfd, path) >= 0;
2✔
871
}
872

873
static int prompt_root_shell(int rfd) {
27✔
874
        int r;
27✔
875

876
        assert(rfd >= 0);
27✔
877

878
        if (arg_root_shell)
27✔
879
                return 0;
880

881
        r = read_credential("passwd.shell.root", (void**) &arg_root_shell, NULL);
22✔
882
        if (r < 0)
22✔
883
                log_debug_errno(r, "Failed to read credential passwd.shell.root, ignoring: %m");
21✔
884
        else {
885
                log_debug("Acquired root shell from credential.");
1✔
886
                return 0;
1✔
887
        }
888

889
        if (!arg_prompt_root_shell) {
21✔
890
                log_debug("Prompting for root shell was not requested.");
19✔
891
                return 0;
19✔
892
        }
893

894
        print_welcome(rfd);
2✔
895

896
        return prompt_loop(rfd, "Please enter the new root shell",
2✔
897
                           NULL, 0, shell_is_ok, &arg_root_shell);
898
}
899

900
static int write_root_passwd(int rfd, int etc_fd, const char *password, const char *shell) {
15✔
901
        _cleanup_fclose_ FILE *original = NULL, *passwd = NULL;
15✔
902
        _cleanup_(unlink_and_freep) char *passwd_tmp = NULL;
15✔
903
        int r;
15✔
904
        bool found = false;
15✔
905

906
        r = fopen_temporary_at_label(etc_fd, "passwd", "passwd", &passwd, &passwd_tmp);
15✔
907
        if (r < 0)
15✔
908
                return r;
909

910
        r = xfopenat(etc_fd, "passwd", "re", O_NOFOLLOW, &original);
15✔
911
        if (r < 0 && r != -ENOENT)
15✔
912
                return r;
913

914
        if (original) {
15✔
915
                struct passwd *i;
4✔
916

917
                r = copy_rights(fileno(original), fileno(passwd));
4✔
918
                if (r < 0)
4✔
UNCOV
919
                        return r;
×
920

921
                while ((r = fgetpwent_sane(original, &i)) > 0) {
7✔
922

923
                        if (streq(i->pw_name, "root")) {
3✔
924
                                if (password)
3✔
925
                                        i->pw_passwd = (char *) password;
2✔
926
                                if (shell)
3✔
927
                                        i->pw_shell = (char *) shell;
2✔
928
                                found = true;
929
                        }
930

931
                        r = putpwent_sane(i, passwd);
3✔
932
                        if (r < 0)
3✔
933
                                return r;
934
                }
935
                if (r < 0)
4✔
936
                        return r;
937

938
        } else {
939
                r = fchmod(fileno(passwd), 0644);
11✔
940
                if (r < 0)
11✔
UNCOV
941
                        return -errno;
×
942
        }
943

944
        if (!found) {
4✔
945
                struct passwd root = {
12✔
946
                        .pw_name = (char *) "root",
947
                        .pw_passwd = (char *) (password ?: PASSWORD_SEE_SHADOW),
12✔
948
                        .pw_uid = 0,
949
                        .pw_gid = 0,
950
                        .pw_gecos = (char *) "Super User",
951
                        .pw_dir = (char *) "/root",
952
                        .pw_shell = (char *) (shell ?: default_root_shell_at(rfd)),
6✔
953
                };
954

955
                if (errno != ENOENT)
12✔
UNCOV
956
                        return -errno;
×
957

958
                r = putpwent_sane(&root, passwd);
12✔
959
                if (r < 0)
12✔
960
                        return r;
961
        }
962

963
        r = fflush_sync_and_check(passwd);
15✔
964
        if (r < 0)
15✔
965
                return r;
966

967
        r = renameat_and_apply_smack_floor_label(etc_fd, passwd_tmp, etc_fd, "passwd");
15✔
968
        if (r < 0)
15✔
UNCOV
969
                return r;
×
970

971
        return 0;
972
}
973

974
static int write_root_shadow(int etc_fd, const char *hashed_password) {
15✔
975
        _cleanup_fclose_ FILE *original = NULL, *shadow = NULL;
15✔
976
        _cleanup_(unlink_and_freep) char *shadow_tmp = NULL;
15✔
977
        int r;
15✔
978
        bool found = false;
15✔
979

980
        r = fopen_temporary_at_label(etc_fd, "shadow", "shadow", &shadow, &shadow_tmp);
15✔
981
        if (r < 0)
15✔
982
                return r;
983

984
        r = xfopenat(etc_fd, "shadow", "re", O_NOFOLLOW, &original);
15✔
985
        if (r < 0 && r != -ENOENT)
15✔
986
                return r;
987

988
        if (original) {
15✔
989
                struct spwd *i;
4✔
990

991
                r = copy_rights(fileno(original), fileno(shadow));
4✔
992
                if (r < 0)
4✔
UNCOV
993
                        return r;
×
994

995
                while ((r = fgetspent_sane(original, &i)) > 0) {
7✔
996

997
                        if (streq(i->sp_namp, "root")) {
3✔
998
                                if (hashed_password) {
3✔
999
                                        i->sp_pwdp = (char *) hashed_password;
2✔
1000
                                        i->sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY);
2✔
1001
                                }
1002
                                found = true;
1003
                        }
1004

1005
                        r = putspent_sane(i, shadow);
3✔
1006
                        if (r < 0)
3✔
1007
                                return r;
1008
                }
1009
                if (r < 0)
4✔
1010
                        return r;
1011

1012
        } else {
1013
                r = fchmod(fileno(shadow), 0000);
11✔
1014
                if (r < 0)
11✔
UNCOV
1015
                        return -errno;
×
1016
        }
1017

1018
        if (!found) {
4✔
1019
                struct spwd root = {
36✔
1020
                        .sp_namp = (char*) "root",
1021
                        .sp_pwdp = (char *) (hashed_password ?: PASSWORD_LOCKED_AND_INVALID),
12✔
1022
                        .sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY),
12✔
1023
                        .sp_min = -1,
1024
                        .sp_max = -1,
1025
                        .sp_warn = -1,
1026
                        .sp_inact = -1,
1027
                        .sp_expire = -1,
1028
                        .sp_flag = ULONG_MAX, /* this appears to be what everybody does ... */
1029
                };
1030

1031
                if (errno != ENOENT)
12✔
UNCOV
1032
                        return -errno;
×
1033

1034
                r = putspent_sane(&root, shadow);
12✔
1035
                if (r < 0)
12✔
1036
                        return r;
1037
        }
1038

1039
        r = fflush_sync_and_check(shadow);
15✔
1040
        if (r < 0)
15✔
1041
                return r;
1042

1043
        r = renameat_and_apply_smack_floor_label(etc_fd, shadow_tmp, etc_fd, "shadow");
15✔
1044
        if (r < 0)
15✔
1045
                return r;
×
1046

1047
        return 0;
1048
}
1049

1050
static int process_root_account(int rfd) {
135✔
1051
        _cleanup_close_ int pfd = -EBADF;
135✔
UNCOV
1052
        _cleanup_(release_lock_file) LockFile lock = LOCK_FILE_INIT;
×
1053
        _cleanup_(erase_and_freep) char *_hashed_password = NULL;
135✔
1054
        const char *password, *hashed_password;
135✔
1055
        int k = 0, r;
135✔
1056

1057
        assert(rfd >= 0);
135✔
1058

1059
        pfd = chase_and_open_parent_at(rfd, "/etc/passwd",
135✔
1060
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
1061
                                       NULL);
1062
        if (pfd < 0)
135✔
1063
                return log_error_errno(pfd, "Failed to chase /etc/passwd: %m");
×
1064

1065
        /* Ensure that passwd and shadow are in the same directory and are not symlinks. */
1066

1067
        FOREACH_STRING(s, "passwd", "shadow") {
405✔
1068
                r = verify_regular_at(pfd, s, /* follow = */ false);
270✔
1069
                if (r < 0 && r != -ENOENT)
270✔
UNCOV
1070
                        return log_error_errno(r, "Verification of /etc/%s being regular file failed: %m", s);
×
1071

1072
                r = should_configure(pfd, s);
270✔
1073
                if (r < 0)
270✔
1074
                        return r;
1075

1076
                k += r;
270✔
1077
        }
1078

1079
        if (k == 0) {
135✔
1080
                log_debug("Found /etc/passwd and /etc/shadow, assuming root account has been initialized.");
108✔
1081
                return 0;
108✔
1082
        }
1083

1084
        r = make_lock_file_at(pfd, ETC_PASSWD_LOCK_FILENAME, LOCK_EX, &lock);
27✔
1085
        if (r < 0)
27✔
UNCOV
1086
                return log_error_errno(r, "Failed to take a lock on /etc/passwd: %m");
×
1087

1088
        k = dir_fd_is_root(rfd);
27✔
1089
        if (k < 0)
27✔
1090
                return log_error_errno(k, "Failed to check if directory file descriptor is root: %m");
×
1091

1092
        if (arg_copy_root_shell && k == 0) {
27✔
1093
                _cleanup_free_ struct passwd *p = NULL;
2✔
1094

1095
                r = getpwnam_malloc("root", &p);
2✔
1096
                if (r < 0)
2✔
UNCOV
1097
                        return log_error_errno(r, "Failed to find passwd entry for root: %m");
×
1098

1099
                r = free_and_strdup(&arg_root_shell, p->pw_shell);
2✔
1100
                if (r < 0)
2✔
UNCOV
1101
                        return log_oom();
×
1102
        }
1103

1104
        r = prompt_root_shell(rfd);
27✔
1105
        if (r < 0)
27✔
1106
                return r;
1107

1108
        if (arg_copy_root_password && k == 0) {
27✔
1109
                struct spwd *p;
2✔
1110

1111
                errno = 0;
2✔
1112
                p = getspnam("root");
2✔
1113
                if (!p)
2✔
UNCOV
1114
                        return log_error_errno(errno_or_else(EIO), "Failed to find shadow entry for root: %m");
×
1115

1116
                r = free_and_strdup(&arg_root_password, p->sp_pwdp);
2✔
1117
                if (r < 0)
2✔
UNCOV
1118
                        return log_oom();
×
1119

1120
                arg_root_password_is_hashed = true;
2✔
1121
        }
1122

1123
        r = prompt_root_password(rfd);
27✔
1124
        if (r < 0)
27✔
1125
                return r;
1126

1127
        if (arg_root_password && arg_root_password_is_hashed) {
27✔
1128
                password = PASSWORD_SEE_SHADOW;
1129
                hashed_password = arg_root_password;
1130
        } else if (arg_root_password) {
21✔
1131
                r = hash_password(arg_root_password, &_hashed_password);
4✔
1132
                if (r < 0)
4✔
UNCOV
1133
                        return log_error_errno(r, "Failed to hash password: %m");
×
1134

1135
                password = PASSWORD_SEE_SHADOW;
4✔
1136
                hashed_password = _hashed_password;
4✔
1137

1138
        } else if (arg_delete_root_password) {
17✔
1139
                password = PASSWORD_SEE_SHADOW;
1140
                hashed_password = PASSWORD_NONE;
1141
        } else if (!arg_root_password && arg_prompt_root_password) {
16✔
1142
                /* If the user was prompted, but no password was supplied, lock the account. */
1143
                password = PASSWORD_SEE_SHADOW;
1144
                hashed_password = PASSWORD_LOCKED_AND_INVALID;
1145
        } else
1146
                /* Leave the password as is. */
1147
                password = hashed_password = NULL;
15✔
1148

1149
        /* Don't create/modify passwd and shadow if there's nothing to do. */
1150
        if (!(password || hashed_password || arg_root_shell)) {
27✔
1151
                log_debug("Initialization of root account was not requested, skipping.");
12✔
1152
                return 0;
12✔
1153
        }
1154

1155
        r = write_root_passwd(rfd, pfd, password, arg_root_shell);
15✔
1156
        if (r < 0)
15✔
UNCOV
1157
                return log_error_errno(r, "Failed to write /etc/passwd: %m");
×
1158

1159
        log_info("/etc/passwd written.");
15✔
1160

1161
        r = write_root_shadow(pfd, hashed_password);
15✔
1162
        if (r < 0)
15✔
UNCOV
1163
                return log_error_errno(r, "Failed to write /etc/shadow: %m");
×
1164

1165
        log_info("/etc/shadow written.");
15✔
1166
        return 0;
1167
}
1168

1169
static int process_kernel_cmdline(int rfd) {
135✔
1170
        _cleanup_close_ int pfd = -EBADF;
135✔
1171
        _cleanup_free_ char *f = NULL;
135✔
1172
        int r;
135✔
1173

1174
        assert(rfd >= 0);
135✔
1175

1176
        pfd = chase_and_open_parent_at(rfd, "/etc/kernel/cmdline",
135✔
1177
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
1178
                                       &f);
1179
        if (pfd < 0)
135✔
UNCOV
1180
                return log_error_errno(pfd, "Failed to chase /etc/kernel/cmdline: %m");
×
1181

1182
        r = should_configure(pfd, f);
135✔
1183
        if (r == 0)
135✔
1184
                log_debug("Found /etc/kernel/cmdline, assuming kernel command line has been configured.");
1✔
1185
        if (r <= 0)
135✔
1186
                return r;
1187

1188
        if (!arg_kernel_cmdline) {
134✔
1189
                log_debug("Creation of /etc/kernel/cmdline was not requested, skipping.");
132✔
1190
                return 0;
132✔
1191
        }
1192

1193
        r = write_string_file_at(pfd, "cmdline", arg_kernel_cmdline,
2✔
1194
                                 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL);
1195
        if (r < 0)
2✔
UNCOV
1196
                return log_error_errno(r, "Failed to write /etc/kernel/cmdline: %m");
×
1197

1198
        log_info("/etc/kernel/cmdline written.");
2✔
1199
        return 0;
1200
}
1201

1202
static int reset_one(int rfd, const char *path) {
6✔
1203
        _cleanup_close_ int pfd = -EBADF;
6✔
1204
        _cleanup_free_ char *f = NULL;
6✔
1205

1206
        assert(rfd >= 0);
6✔
1207
        assert(path);
6✔
1208

1209
        pfd = chase_and_open_parent_at(rfd, path, CHASE_AT_RESOLVE_IN_ROOT|CHASE_WARN|CHASE_NOFOLLOW, &f);
6✔
1210
        if (pfd == -ENOENT)
6✔
1211
                return 0;
1212
        if (pfd < 0)
6✔
UNCOV
1213
                return log_error_errno(pfd, "Failed to resolve %s: %m", path);
×
1214

1215
        if (unlinkat(pfd, f, 0) < 0)
6✔
UNCOV
1216
                return errno == ENOENT ? 0 : log_error_errno(errno, "Failed to remove %s: %m", path);
×
1217

1218
        log_info("Removed %s", path);
6✔
1219
        return 0;
1220
}
1221

1222
static int process_reset(int rfd) {
135✔
1223
        int r;
135✔
1224

1225
        assert(rfd >= 0);
135✔
1226

1227
        if (!arg_reset)
135✔
1228
                return 0;
1229

1230
        FOREACH_STRING(p,
7✔
1231
                       "/etc/locale.conf",
1232
                       "/etc/vconsole.conf",
1233
                       "/etc/hostname",
1234
                       "/etc/machine-id",
1235
                       "/etc/kernel/cmdline",
1236
                       "/etc/localtime") {
1237
                r = reset_one(rfd, p);
6✔
1238
                if (r < 0)
6✔
1239
                        return r;
×
1240
        }
1241

1242
        return 0;
1✔
1243
}
1244

UNCOV
1245
static int help(void) {
×
1246
        _cleanup_free_ char *link = NULL;
×
UNCOV
1247
        int r;
×
1248

UNCOV
1249
        r = terminal_urlify_man("systemd-firstboot", "1", &link);
×
UNCOV
1250
        if (r < 0)
×
UNCOV
1251
                return log_oom();
×
1252

UNCOV
1253
        printf("%s [OPTIONS...]\n\n"
×
1254
               "Configures basic settings of the system.\n\n"
1255
               "  -h --help                       Show this help\n"
1256
               "     --version                    Show package version\n"
1257
               "     --root=PATH                  Operate on an alternate filesystem root\n"
1258
               "     --image=PATH                 Operate on disk image as filesystem root\n"
1259
               "     --image-policy=POLICY        Specify disk image dissection policy\n"
1260
               "     --locale=LOCALE              Set primary locale (LANG=)\n"
1261
               "     --locale-messages=LOCALE     Set message locale (LC_MESSAGES=)\n"
1262
               "     --keymap=KEYMAP              Set keymap\n"
1263
               "     --timezone=TIMEZONE          Set timezone\n"
1264
               "     --hostname=NAME              Set hostname\n"
1265
               "     --setup-machine-id           Set a random machine ID\n"
1266
               "     --machine-id=ID              Set specified machine ID\n"
1267
               "     --root-password=PASSWORD     Set root password from plaintext password\n"
1268
               "     --root-password-file=FILE    Set root password from file\n"
1269
               "     --root-password-hashed=HASH  Set root password from hashed password\n"
1270
               "     --root-shell=SHELL           Set root shell\n"
1271
               "     --kernel-command-line=CMDLINE\n"
1272
               "                                  Set kernel command line\n"
1273
               "     --prompt-locale              Prompt the user for locale settings\n"
1274
               "     --prompt-keymap              Prompt the user for keymap settings\n"
1275
               "     --prompt-timezone            Prompt the user for timezone\n"
1276
               "     --prompt-hostname            Prompt the user for hostname\n"
1277
               "     --prompt-root-password       Prompt the user for root password\n"
1278
               "     --prompt-root-shell          Prompt the user for root shell\n"
1279
               "     --prompt                     Prompt for all of the above\n"
1280
               "     --copy-locale                Copy locale from host\n"
1281
               "     --copy-keymap                Copy keymap from host\n"
1282
               "     --copy-timezone              Copy timezone from host\n"
1283
               "     --copy-root-password         Copy root password from host\n"
1284
               "     --copy-root-shell            Copy root shell from host\n"
1285
               "     --copy                       Copy locale, keymap, timezone, root password\n"
1286
               "     --force                      Overwrite existing files\n"
1287
               "     --delete-root-password       Delete root password\n"
1288
               "     --welcome=no                 Disable the welcome text\n"
1289
               "     --reset                      Remove existing files\n"
1290
               "\nSee the %s for details.\n",
1291
               program_invocation_short_name,
1292
               link);
1293

1294
        return 0;
1295
}
1296

1297
static int parse_argv(int argc, char *argv[]) {
138✔
1298

1299
        enum {
138✔
1300
                ARG_VERSION = 0x100,
1301
                ARG_ROOT,
1302
                ARG_IMAGE,
1303
                ARG_IMAGE_POLICY,
1304
                ARG_LOCALE,
1305
                ARG_LOCALE_MESSAGES,
1306
                ARG_KEYMAP,
1307
                ARG_TIMEZONE,
1308
                ARG_HOSTNAME,
1309
                ARG_SETUP_MACHINE_ID,
1310
                ARG_MACHINE_ID,
1311
                ARG_ROOT_PASSWORD,
1312
                ARG_ROOT_PASSWORD_FILE,
1313
                ARG_ROOT_PASSWORD_HASHED,
1314
                ARG_ROOT_SHELL,
1315
                ARG_KERNEL_COMMAND_LINE,
1316
                ARG_PROMPT,
1317
                ARG_PROMPT_LOCALE,
1318
                ARG_PROMPT_KEYMAP,
1319
                ARG_PROMPT_TIMEZONE,
1320
                ARG_PROMPT_HOSTNAME,
1321
                ARG_PROMPT_ROOT_PASSWORD,
1322
                ARG_PROMPT_ROOT_SHELL,
1323
                ARG_COPY,
1324
                ARG_COPY_LOCALE,
1325
                ARG_COPY_KEYMAP,
1326
                ARG_COPY_TIMEZONE,
1327
                ARG_COPY_ROOT_PASSWORD,
1328
                ARG_COPY_ROOT_SHELL,
1329
                ARG_FORCE,
1330
                ARG_DELETE_ROOT_PASSWORD,
1331
                ARG_WELCOME,
1332
                ARG_RESET,
1333
        };
1334

1335
        static const struct option options[] = {
138✔
1336
                { "help",                    no_argument,       NULL, 'h'                         },
1337
                { "version",                 no_argument,       NULL, ARG_VERSION                 },
1338
                { "root",                    required_argument, NULL, ARG_ROOT                    },
1339
                { "image",                   required_argument, NULL, ARG_IMAGE                   },
1340
                { "image-policy",            required_argument, NULL, ARG_IMAGE_POLICY            },
1341
                { "locale",                  required_argument, NULL, ARG_LOCALE                  },
1342
                { "locale-messages",         required_argument, NULL, ARG_LOCALE_MESSAGES         },
1343
                { "keymap",                  required_argument, NULL, ARG_KEYMAP                  },
1344
                { "timezone",                required_argument, NULL, ARG_TIMEZONE                },
1345
                { "hostname",                required_argument, NULL, ARG_HOSTNAME                },
1346
                { "setup-machine-id",        no_argument,       NULL, ARG_SETUP_MACHINE_ID        },
1347
                { "machine-id",              required_argument, NULL, ARG_MACHINE_ID              },
1348
                { "root-password",           required_argument, NULL, ARG_ROOT_PASSWORD           },
1349
                { "root-password-file",      required_argument, NULL, ARG_ROOT_PASSWORD_FILE      },
1350
                { "root-password-hashed",    required_argument, NULL, ARG_ROOT_PASSWORD_HASHED    },
1351
                { "root-shell",              required_argument, NULL, ARG_ROOT_SHELL              },
1352
                { "kernel-command-line",     required_argument, NULL, ARG_KERNEL_COMMAND_LINE     },
1353
                { "prompt",                  no_argument,       NULL, ARG_PROMPT                  },
1354
                { "prompt-locale",           no_argument,       NULL, ARG_PROMPT_LOCALE           },
1355
                { "prompt-keymap",           no_argument,       NULL, ARG_PROMPT_KEYMAP           },
1356
                { "prompt-timezone",         no_argument,       NULL, ARG_PROMPT_TIMEZONE         },
1357
                { "prompt-hostname",         no_argument,       NULL, ARG_PROMPT_HOSTNAME         },
1358
                { "prompt-root-password",    no_argument,       NULL, ARG_PROMPT_ROOT_PASSWORD    },
1359
                { "prompt-root-shell",       no_argument,       NULL, ARG_PROMPT_ROOT_SHELL       },
1360
                { "copy",                    no_argument,       NULL, ARG_COPY                    },
1361
                { "copy-locale",             no_argument,       NULL, ARG_COPY_LOCALE             },
1362
                { "copy-keymap",             no_argument,       NULL, ARG_COPY_KEYMAP             },
1363
                { "copy-timezone",           no_argument,       NULL, ARG_COPY_TIMEZONE           },
1364
                { "copy-root-password",      no_argument,       NULL, ARG_COPY_ROOT_PASSWORD      },
1365
                { "copy-root-shell",         no_argument,       NULL, ARG_COPY_ROOT_SHELL         },
1366
                { "force",                   no_argument,       NULL, ARG_FORCE                   },
1367
                { "delete-root-password",    no_argument,       NULL, ARG_DELETE_ROOT_PASSWORD    },
1368
                { "welcome",                 required_argument, NULL, ARG_WELCOME                 },
1369
                { "reset",                   no_argument,       NULL, ARG_RESET                   },
1370
                {}
1371
        };
1372

1373
        int r, c;
138✔
1374

1375
        assert(argc >= 0);
138✔
1376
        assert(argv);
138✔
1377

1378
        while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
647✔
1379

1380
                switch (c) {
511✔
1381

UNCOV
1382
                case 'h':
×
UNCOV
1383
                        return help();
×
1384

UNCOV
1385
                case ARG_VERSION:
×
UNCOV
1386
                        return version();
×
1387

1388
                case ARG_ROOT:
35✔
1389
                        r = parse_path_argument(optarg, true, &arg_root);
35✔
1390
                        if (r < 0)
35✔
1391
                                return r;
1392
                        break;
1393

1394
                case ARG_IMAGE:
×
1395
                        r = parse_path_argument(optarg, false, &arg_image);
×
UNCOV
1396
                        if (r < 0)
×
1397
                                return r;
1398
                        break;
1399

UNCOV
1400
                case ARG_IMAGE_POLICY:
×
UNCOV
1401
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
1402
                        if (r < 0)
×
1403
                                return r;
1404
                        break;
1405

1406
                case ARG_LOCALE:
4✔
1407
                        r = free_and_strdup(&arg_locale, optarg);
4✔
1408
                        if (r < 0)
4✔
1409
                                return log_oom();
×
1410

1411
                        break;
1412

1413
                case ARG_LOCALE_MESSAGES:
4✔
1414
                        r = free_and_strdup(&arg_locale_messages, optarg);
4✔
1415
                        if (r < 0)
4✔
UNCOV
1416
                                return log_oom();
×
1417

1418
                        break;
1419

1420
                case ARG_KEYMAP:
3✔
1421
                        if (!keymap_is_valid(optarg))
3✔
UNCOV
1422
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1423
                                                       "Keymap %s is not valid.", optarg);
1424

1425
                        r = free_and_strdup(&arg_keymap, optarg);
3✔
1426
                        if (r < 0)
3✔
UNCOV
1427
                                return log_oom();
×
1428

1429
                        break;
1430

1431
                case ARG_TIMEZONE:
4✔
1432
                        if (!timezone_is_valid(optarg, LOG_ERR))
4✔
1433
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1✔
1434
                                                       "Timezone %s is not valid.", optarg);
1435

1436
                        r = free_and_strdup(&arg_timezone, optarg);
3✔
1437
                        if (r < 0)
3✔
1438
                                return log_oom();
×
1439

1440
                        break;
1441

1442
                case ARG_ROOT_PASSWORD:
4✔
1443
                        r = free_and_strdup(&arg_root_password, optarg);
4✔
1444
                        if (r < 0)
4✔
UNCOV
1445
                                return log_oom();
×
1446

1447
                        arg_root_password_is_hashed = false;
4✔
1448
                        break;
4✔
1449

1450
                case ARG_ROOT_PASSWORD_FILE:
1✔
1451
                        arg_root_password = mfree(arg_root_password);
1✔
1452

1453
                        r = read_one_line_file(optarg, &arg_root_password);
1✔
1454
                        if (r < 0)
1✔
UNCOV
1455
                                return log_error_errno(r, "Failed to read %s: %m", optarg);
×
1456

1457
                        arg_root_password_is_hashed = false;
1✔
1458
                        break;
1✔
1459

1460
                case ARG_ROOT_PASSWORD_HASHED:
4✔
1461
                        r = free_and_strdup(&arg_root_password, optarg);
4✔
1462
                        if (r < 0)
4✔
UNCOV
1463
                                return log_oom();
×
1464

1465
                        arg_root_password_is_hashed = true;
4✔
1466
                        break;
4✔
1467

1468
                case ARG_ROOT_SHELL:
5✔
1469
                        r = free_and_strdup(&arg_root_shell, optarg);
5✔
1470
                        if (r < 0)
5✔
UNCOV
1471
                                return log_oom();
×
1472

1473
                        break;
1474

1475
                case ARG_HOSTNAME:
3✔
1476
                        if (!hostname_is_valid(optarg, VALID_HOSTNAME_TRAILING_DOT|VALID_HOSTNAME_QUESTION_MARK))
3✔
UNCOV
1477
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1478
                                                       "Host name %s is not valid.", optarg);
1479

1480
                        r = free_and_strdup(&arg_hostname, optarg);
3✔
1481
                        if (r < 0)
3✔
UNCOV
1482
                                return log_oom();
×
1483

1484
                        hostname_cleanup(arg_hostname);
3✔
1485
                        break;
3✔
1486

1487
                case ARG_SETUP_MACHINE_ID:
1✔
1488
                        r = sd_id128_randomize(&arg_machine_id);
1✔
1489
                        if (r < 0)
1✔
UNCOV
1490
                                return log_error_errno(r, "Failed to generate randomized machine ID: %m");
×
1491

1492
                        break;
1493

1494
                case ARG_MACHINE_ID:
4✔
1495
                        r = sd_id128_from_string(optarg, &arg_machine_id);
4✔
1496
                        if (r < 0)
4✔
1497
                                return log_error_errno(r, "Failed to parse machine id %s.", optarg);
1✔
1498

1499
                        break;
1500

1501
                case ARG_KERNEL_COMMAND_LINE:
3✔
1502
                        r = free_and_strdup(&arg_kernel_cmdline, optarg);
3✔
1503
                        if (r < 0)
3✔
1504
                                return log_oom();
×
1505

1506
                        break;
1507

UNCOV
1508
                case ARG_PROMPT:
×
UNCOV
1509
                        arg_prompt_locale = arg_prompt_keymap = arg_prompt_timezone = arg_prompt_hostname =
×
UNCOV
1510
                                arg_prompt_root_password = arg_prompt_root_shell = true;
×
UNCOV
1511
                        break;
×
1512

1513
                case ARG_PROMPT_LOCALE:
105✔
1514
                        arg_prompt_locale = true;
105✔
1515
                        break;
105✔
1516

1517
                case ARG_PROMPT_KEYMAP:
105✔
1518
                        arg_prompt_keymap = true;
105✔
1519
                        break;
105✔
1520

1521
                case ARG_PROMPT_TIMEZONE:
105✔
1522
                        arg_prompt_timezone = true;
105✔
1523
                        break;
105✔
1524

1525
                case ARG_PROMPT_HOSTNAME:
1✔
1526
                        arg_prompt_hostname = true;
1✔
1527
                        break;
1✔
1528

1529
                case ARG_PROMPT_ROOT_PASSWORD:
105✔
1530
                        arg_prompt_root_password = true;
105✔
1531
                        break;
105✔
1532

1533
                case ARG_PROMPT_ROOT_SHELL:
4✔
1534
                        arg_prompt_root_shell = true;
4✔
1535
                        break;
4✔
1536

1537
                case ARG_COPY:
1✔
1538
                        arg_copy_locale = arg_copy_keymap = arg_copy_timezone = arg_copy_root_password =
1✔
1539
                                arg_copy_root_shell = true;
1✔
1540
                        break;
1✔
1541

1542
                case ARG_COPY_LOCALE:
1✔
1543
                        arg_copy_locale = true;
1✔
1544
                        break;
1✔
1545

1546
                case ARG_COPY_KEYMAP:
1✔
1547
                        arg_copy_keymap = true;
1✔
1548
                        break;
1✔
1549

1550
                case ARG_COPY_TIMEZONE:
1✔
1551
                        arg_copy_timezone = true;
1✔
1552
                        break;
1✔
1553

1554
                case ARG_COPY_ROOT_PASSWORD:
1✔
1555
                        arg_copy_root_password = true;
1✔
1556
                        break;
1✔
1557

1558
                case ARG_COPY_ROOT_SHELL:
1✔
1559
                        arg_copy_root_shell = true;
1✔
1560
                        break;
1✔
1561

1562
                case ARG_FORCE:
2✔
1563
                        arg_force = true;
2✔
1564
                        break;
2✔
1565

1566
                case ARG_DELETE_ROOT_PASSWORD:
1✔
1567
                        arg_delete_root_password = true;
1✔
1568
                        break;
1✔
1569

1570
                case ARG_WELCOME:
1✔
1571
                        r = parse_boolean(optarg);
1✔
1572
                        if (r < 0)
1✔
UNCOV
1573
                                return log_error_errno(r, "Failed to parse --welcome= argument: %s", optarg);
×
1574

1575
                        arg_welcome = r;
1✔
1576
                        break;
1✔
1577

1578
                case ARG_RESET:
1✔
1579
                        arg_reset = true;
1✔
1580
                        break;
1✔
1581

1582
                case '?':
1583
                        return -EINVAL;
1584

UNCOV
1585
                default:
×
UNCOV
1586
                        assert_not_reached();
×
1587
                }
1588

1589
        if (arg_delete_root_password && (arg_copy_root_password || arg_root_password || arg_prompt_root_password))
136✔
UNCOV
1590
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1591
                                       "--delete-root-password cannot be combined with other root password options.");
1592

1593
        if (arg_image && arg_root)
136✔
UNCOV
1594
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1595
                                       "--root= and --image= cannot be used together.");
1596

1597
        if (!sd_id128_is_null(arg_machine_id) && !(arg_image || arg_root) && !arg_force)
268✔
1598
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1599
                                       "--machine-id=/--setup-machine-id only works with --root= or --image=.");
1600

1601
        return 1;
1602
}
1603

1604
static int reload_system_manager(sd_bus **bus) {
×
1605
        int r;
×
1606

UNCOV
1607
        assert(bus);
×
1608

1609
        if (!*bus) {
×
UNCOV
1610
                r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL, RUNTIME_SCOPE_SYSTEM, bus);
×
UNCOV
1611
                if (r < 0)
×
1612
                        return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL, RUNTIME_SCOPE_SYSTEM);
×
1613
        }
1614

UNCOV
1615
        r = bus_service_manager_reload(*bus);
×
1616
        if (r < 0)
×
1617
                return r;
1618

1619
        log_info("Requested manager reload to apply locale configuration.");
×
1620
        return 0;
1621
}
1622

1623
static int reload_vconsole(sd_bus **bus) {
×
UNCOV
1624
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
1625
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
×
1626
        _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
×
1627
        const char *object;
×
1628
        int r;
×
1629

UNCOV
1630
        assert(bus);
×
1631

1632
        if (!*bus) {
×
1633
                r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL, RUNTIME_SCOPE_SYSTEM, bus);
×
UNCOV
1634
                if (r < 0)
×
1635
                        return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL, RUNTIME_SCOPE_SYSTEM);
×
1636
        }
1637

1638
        r = bus_wait_for_jobs_new(*bus, &w);
×
UNCOV
1639
        if (r < 0)
×
1640
                return log_error_errno(r, "Could not watch jobs: %m");
×
1641

1642
        r = bus_call_method(*bus, bus_systemd_mgr, "RestartUnit", &error, &reply,
×
1643
                            "ss", "systemd-vconsole-setup.service", "replace");
1644
        if (r < 0)
×
1645
                return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
×
1646

UNCOV
1647
        r = sd_bus_message_read(reply, "o", &object);
×
UNCOV
1648
        if (r < 0)
×
UNCOV
1649
                return bus_log_parse_error(r);
×
1650

UNCOV
1651
        r = bus_wait_for_jobs_one(w, object, BUS_WAIT_JOBS_LOG_ERROR, NULL);
×
UNCOV
1652
        if (r < 0)
×
1653
                return log_error_errno(r, "Failed to wait for systemd-vconsole-setup.service/restart: %m");
×
1654
        return 0;
1655
}
1656

1657
static int run(int argc, char *argv[]) {
138✔
1658
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
138✔
1659
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
138✔
UNCOV
1660
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
×
1661
        _cleanup_close_ int rfd = -EBADF;
138✔
1662
        int r;
138✔
1663

1664
        r = parse_argv(argc, argv);
138✔
1665
        if (r <= 0)
138✔
1666
                return r;
1667

1668
        log_setup();
136✔
1669

1670
        umask(0022);
136✔
1671

1672
        bool offline = arg_root || arg_image;
136✔
1673

1674
        if (!offline) {
103✔
1675
                /* If we are called without --root=/--image= let's honour the systemd.firstboot kernel
1676
                 * command line option, because we are called to provision the host with basic settings (as
1677
                 * opposed to some other file system tree/image) */
1678

1679
                bool enabled;
103✔
1680
                r = proc_cmdline_get_bool("systemd.firstboot", /* flags = */ 0, &enabled);
103✔
1681
                if (r < 0)
103✔
UNCOV
1682
                        return log_error_errno(r, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
×
1683
                if (r > 0 && !enabled) {
103✔
1684
                        log_debug("Found systemd.firstboot=no kernel command line argument, turning off all prompts.");
103✔
1685
                        arg_prompt_locale = arg_prompt_keymap = arg_prompt_timezone = arg_prompt_hostname = arg_prompt_root_password = arg_prompt_root_shell = false;
103✔
1686
                }
1687
        }
1688

1689
        r = mac_init();
136✔
1690
        if (r < 0)
136✔
1691
                return r;
1692

1693
        if (arg_image) {
136✔
UNCOV
1694
                assert(!arg_root);
×
1695

UNCOV
1696
                r = mount_image_privately_interactively(
×
1697
                                arg_image,
1698
                                arg_image_policy,
1699
                                DISSECT_IMAGE_GENERIC_ROOT |
1700
                                DISSECT_IMAGE_REQUIRE_ROOT |
1701
                                DISSECT_IMAGE_VALIDATE_OS |
1702
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
1703
                                DISSECT_IMAGE_FSCK |
1704
                                DISSECT_IMAGE_GROWFS |
1705
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
1706
                                &mounted_dir,
1707
                                &rfd,
1708
                                &loop_device);
UNCOV
1709
                if (r < 0)
×
1710
                        return r;
1711

UNCOV
1712
                arg_root = strdup(mounted_dir);
×
UNCOV
1713
                if (!arg_root)
×
UNCOV
1714
                        return log_oom();
×
1715
        } else {
1716
                rfd = open(empty_to_root(arg_root), O_DIRECTORY|O_CLOEXEC);
136✔
1717
                if (rfd < 0)
136✔
1718
                        return log_error_errno(errno, "Failed to open %s: %m", empty_to_root(arg_root));
×
1719
        }
1720

1721
        LOG_SET_PREFIX(arg_image ?: arg_root);
274✔
1722

1723
        /* We check these conditions here instead of in parse_argv() so that we can take the root directory
1724
         * into account. */
1725

1726
        if (arg_keymap && !keymap_is_ok(rfd, arg_keymap))
136✔
UNCOV
1727
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Keymap %s is not installed.", arg_keymap);
×
1728
        if (arg_locale && !locale_is_ok(rfd, arg_locale))
136✔
UNCOV
1729
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale);
×
1730
        if (arg_locale_messages && !locale_is_ok(rfd, arg_locale_messages))
136✔
UNCOV
1731
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale_messages);
×
1732

1733
        if (arg_root_shell) {
136✔
1734
                r = find_shell(rfd, arg_root_shell);
5✔
1735
                if (r < 0)
5✔
1736
                        return r;
1737
        }
1738

1739
        r = process_reset(rfd);
135✔
1740
        if (r < 0)
135✔
1741
                return r;
1742

1743
        r = process_locale(rfd);
135✔
1744
        if (r < 0)
135✔
1745
                return r;
1746
        if (r > 0 && !offline)
135✔
UNCOV
1747
                (void) reload_system_manager(&bus);
×
1748

1749
        r = process_keymap(rfd);
135✔
1750
        if (r < 0)
135✔
1751
                return r;
1752
        if (r > 0 && !offline)
135✔
UNCOV
1753
                (void) reload_vconsole(&bus);
×
1754

1755
        r = process_timezone(rfd);
135✔
1756
        if (r < 0)
135✔
1757
                return r;
1758

1759
        r = process_hostname(rfd);
135✔
1760
        if (r < 0)
135✔
1761
                return r;
1762

1763
        r = process_root_account(rfd);
135✔
1764
        if (r < 0)
135✔
1765
                return r;
1766

1767
        r = process_kernel_cmdline(rfd);
135✔
1768
        if (r < 0)
135✔
1769
                return r;
1770

1771
        r = process_machine_id(rfd);
135✔
1772
        if (r < 0)
135✔
UNCOV
1773
                return r;
×
1774

1775
        return 0;
1776
}
1777

1778
DEFINE_MAIN_FUNCTION(run);
138✔
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