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

systemd / systemd / 21652912420

03 Feb 2026 07:37PM UTC coverage: 72.767% (-0.04%) from 72.804%
21652912420

push

github

web-flow
Introduce name_to_handle_at_u64() helper function and use it for getting pidfd ID and cgroup ID (#40500)

26 of 28 new or added lines in 8 files covered. (92.86%)

2713 existing lines in 47 files now uncovered.

311340 of 427861 relevant lines covered (72.77%)

1144580.1 hits per line

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

77.69
/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
#include "sd-varlink.h"
10

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

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

93
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
142✔
94
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
142✔
95
STATIC_DESTRUCTOR_REGISTER(arg_locale, freep);
142✔
96
STATIC_DESTRUCTOR_REGISTER(arg_locale_messages, freep);
142✔
97
STATIC_DESTRUCTOR_REGISTER(arg_keymap, freep);
142✔
98
STATIC_DESTRUCTOR_REGISTER(arg_timezone, freep);
142✔
99
STATIC_DESTRUCTOR_REGISTER(arg_hostname, freep);
142✔
100
STATIC_DESTRUCTOR_REGISTER(arg_root_password, erase_and_freep);
142✔
101
STATIC_DESTRUCTOR_REGISTER(arg_root_shell, freep);
142✔
102
STATIC_DESTRUCTOR_REGISTER(arg_kernel_cmdline, freep);
142✔
103
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
142✔
104

105
static void print_welcome(int rfd, sd_varlink **mute_console_link) {
7✔
106
        _cleanup_free_ char *pretty_name = NULL, *os_name = NULL, *ansi_color = NULL;
7✔
107
        static bool done = false;
7✔
108
        const char *pn, *ac;
7✔
109
        int r;
7✔
110

111
        assert(rfd >= 0);
7✔
112
        assert(mute_console_link);
7✔
113

114
        /* Needs to be called before mute_console or it will garble the screen */
115
        if (arg_welcome)
7✔
116
                (void) plymouth_hide_splash();
6✔
117

118
        if (!*mute_console_link && arg_mute_console)
7✔
UNCOV
119
                (void) mute_console(mute_console_link);
×
120

121
        if (!arg_welcome)
7✔
122
                return;
123

124
        if (done) {
6✔
125
                putchar('\n'); /* Add some breathing room between multiple prompts */
1✔
126
                return;
127
        }
128

129
        (void) terminal_reset_defensive_locked(STDOUT_FILENO, /* flags= */ 0);
6✔
130

131
        if (arg_chrome)
6✔
132
                chrome_show("Initial Setup", /* bottom= */ NULL);
6✔
133

134
        r = parse_os_release_at(rfd,
6✔
135
                                "PRETTY_NAME", &pretty_name,
136
                                "NAME", &os_name,
137
                                "ANSI_COLOR", &ansi_color);
138
        if (r < 0)
6✔
139
                log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
6✔
140
                               "Failed to read os-release file, ignoring: %m");
141

142
        pn = os_release_pretty_name(pretty_name, os_name);
6✔
143
        ac = isempty(ansi_color) ? "0" : ansi_color;
6✔
144

145
        if (colors_enabled())
6✔
UNCOV
146
                printf(ANSI_HIGHLIGHT "Welcome to " ANSI_NORMAL "\x1B[%sm%s" ANSI_HIGHLIGHT "!" ANSI_NORMAL "\n", ac, pn);
×
147
        else
148
                printf("Welcome to %s!\n", pn);
6✔
149

150
        putchar('\n');
6✔
151
        if (emoji_enabled()) {
6✔
UNCOV
152
                fputs(glyph(GLYPH_SPARKLES), stdout);
×
UNCOV
153
                putchar(' ');
×
154
        }
155
        printf("Please configure the system!\n\n");
6✔
156

157
        done = true;
6✔
158
}
159

160
static int should_configure(int dir_fd, const char *filename) {
1,112✔
161
        _cleanup_fclose_ FILE *passwd = NULL, *shadow = NULL;
1,112✔
162
        int r;
1,112✔
163

164
        assert(dir_fd >= 0);
1,112✔
165
        assert(filename);
1,112✔
166

167
        if (streq(filename, "passwd") && !arg_force)
1,112✔
168
                /* We may need to do additional checks, so open the file. */
169
                r = xfopenat(dir_fd, filename, "re", O_NOFOLLOW, &passwd);
136✔
170
        else
171
                r = RET_NERRNO(faccessat(dir_fd, filename, F_OK, AT_SYMLINK_NOFOLLOW));
976✔
172

173
        if (r == -ENOENT)
597✔
174
                return true; /* missing */
175
        if (r < 0)
626✔
UNCOV
176
                return log_error_errno(r, "Failed to access %s: %m", filename);
×
177
        if (arg_force)
626✔
178
                return true; /* exists, but if --force was given we should still configure the file. */
179

180
        if (!passwd)
609✔
181
                return false;
182

183
        /* In case of /etc/passwd, do an additional check for the root password field.
184
         * We first check that passwd redirects to shadow, and then we check shadow.
185
         */
186
        struct passwd *i;
187
        while ((r = fgetpwent_sane(passwd, &i)) > 0) {
111✔
188
                if (!streq(i->pw_name, "root"))
110✔
UNCOV
189
                        continue;
×
190

191
                if (streq_ptr(i->pw_passwd, PASSWORD_SEE_SHADOW))
110✔
192
                        break;
UNCOV
193
                log_debug("passwd: root account with non-shadow password found, treating root as configured");
×
194
                return false;
195
        }
196
        if (r < 0)
111✔
UNCOV
197
                return log_error_errno(r, "Failed to read %s: %m", filename);
×
198
        if (r == 0) {
111✔
199
                log_debug("No root account found in %s, assuming root is not configured.", filename);
1✔
200
                return true;
1✔
201
        }
202

203
        r = xfopenat(dir_fd, "shadow", "re", O_NOFOLLOW, &shadow);
110✔
204
        if (r == -ENOENT) {
110✔
UNCOV
205
                log_debug("No shadow file found, assuming root is not configured.");
×
UNCOV
206
                return true; /* missing */
×
207
        }
208
        if (r < 0)
110✔
209
                return log_error_errno(r, "Failed to access shadow: %m");
×
210

211
        struct spwd *j;
212
        while ((r = fgetspent_sane(shadow, &j)) > 0) {
110✔
213
                if (!streq(j->sp_namp, "root"))
110✔
UNCOV
214
                        continue;
×
215

216
                bool unprovisioned = streq_ptr(j->sp_pwdp, PASSWORD_UNPROVISIONED);
110✔
217
                log_debug("Root account found, %s.",
214✔
218
                          unprovisioned ? "with unprovisioned password, treating root as not configured" :
219
                                          "treating root as configured");
220
                return unprovisioned;
110✔
221
        }
UNCOV
222
        if (r < 0)
×
UNCOV
223
                return log_error_errno(r, "Failed to read shadow: %m");
×
UNCOV
224
        assert(r == 0);
×
UNCOV
225
        log_debug("No root account found in shadow, assuming root is not configured.");
×
226
        return true;
227
}
228

229
static int locale_is_ok(const char *name, void *userdata) {
11✔
230
        int rfd = ASSERT_FD(PTR_TO_FD(userdata)), r;
11✔
231

232
        r = dir_fd_is_root(rfd);
11✔
233
        if (r < 0)
11✔
UNCOV
234
                log_debug_errno(r, "Unable to determine if operating on host root directory, assuming we are: %m");
×
235

236
        return r != 0 ? locale_is_installed(name) > 0 : locale_is_valid(name);
11✔
237
}
238

239
static int prompt_locale(int rfd, sd_varlink **mute_console_link) {
11✔
UNCOV
240
        _cleanup_strv_free_ char **locales = NULL;
×
241
        bool acquired_from_creds = false;
11✔
242
        int r;
11✔
243

244
        assert(rfd >= 0);
11✔
245

246
        if (arg_locale || arg_locale_messages)
11✔
247
                return 0;
248

249
        r = read_credential("firstboot.locale", (void**) &arg_locale, NULL);
6✔
250
        if (r < 0)
6✔
251
                log_debug_errno(r, "Failed to read credential firstboot.locale, ignoring: %m");
5✔
252
        else
253
                acquired_from_creds = true;
254

255
        r = read_credential("firstboot.locale-messages", (void**) &arg_locale_messages, NULL);
6✔
256
        if (r < 0)
6✔
257
                log_debug_errno(r, "Failed to read credential firstboot.locale-messages, ignoring: %m");
5✔
258
        else
259
                acquired_from_creds = true;
260

261
        if (acquired_from_creds) {
5✔
262
                log_debug("Acquired locale from credentials.");
1✔
263
                return 0;
1✔
264
        }
265

266
        if (!arg_prompt_locale) {
5✔
267
                log_debug("Prompting for locale was not requested.");
4✔
268
                return 0;
4✔
269
        }
270

271
        r = get_locales(&locales);
1✔
272
        if (r < 0)
1✔
UNCOV
273
                return log_error_errno(r, "Cannot query locales list: %m");
×
274

275
        if (strv_isempty(locales))
1✔
UNCOV
276
                log_debug("No locales found, skipping locale selection.");
×
277
        else if (strv_length(locales) == 1) {
1✔
278

279
                if (streq(locales[0], SYSTEMD_DEFAULT_LOCALE))
×
280
                        log_debug("Only installed locale is default locale anyway, not setting locale explicitly.");
×
281
                else {
UNCOV
282
                        log_debug("Only a single locale available (%s), selecting it as default.", locales[0]);
×
283

UNCOV
284
                        arg_locale = strdup(locales[0]);
×
UNCOV
285
                        if (!arg_locale)
×
UNCOV
286
                                return log_oom();
×
287

288
                        /* Not setting arg_locale_message here, since it defaults to LANG anyway */
289
                }
290
        } else {
291
                print_welcome(rfd, mute_console_link);
1✔
292

293
                r = prompt_loop("Please enter the new system locale name or number",
2✔
294
                                GLYPH_WORLD,
295
                                locales,
296
                                /* accepted= */ NULL,
297
                                /* ellipsize_percentage= */ 60,
298
                                /* n_columns= */ 3,
299
                                /* column_width= */ 20,
300
                                locale_is_ok,
301
                                /* refresh= */ NULL,
302
                                FD_TO_PTR(rfd),
1✔
303
                                PROMPT_MAY_SKIP|PROMPT_SHOW_MENU,
304
                                &arg_locale);
305
                if (r < 0)
1✔
306
                        return r;
307
                if (isempty(arg_locale))
12✔
308
                        return 0;
309

310
                r = prompt_loop("Please enter the new system message locale name or number",
1✔
311
                                GLYPH_WORLD,
312
                                locales,
313
                                /* accepted= */ NULL,
314
                                /* ellipsize_percentage= */ 60,
315
                                /* n_columns= */ 3,
316
                                /* column_width= */ 20,
317
                                locale_is_ok,
318
                                /* refresh= */ NULL,
319
                                FD_TO_PTR(rfd),
320
                                PROMPT_MAY_SKIP|PROMPT_SHOW_MENU,
321
                                &arg_locale_messages);
322
                if (r < 0)
1✔
323
                        return r;
324

325
                /* Suppress the messages setting if it's the same as the main locale anyway */
326
                if (streq_ptr(arg_locale, arg_locale_messages))
1✔
UNCOV
327
                        arg_locale_messages = mfree(arg_locale_messages);
×
328
        }
329

330
        return 0;
331
}
332

333
static int process_locale(int rfd, sd_varlink **mute_console_link) {
139✔
334
        _cleanup_close_ int pfd = -EBADF;
139✔
335
        _cleanup_free_ char *f = NULL;
139✔
336
        char* locales[3];
139✔
337
        unsigned i = 0;
139✔
338
        int r;
139✔
339

340
        assert(rfd >= 0);
139✔
341

342
        pfd = chase_and_open_parent_at(rfd, etc_locale_conf(),
139✔
343
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
344
                                       &f);
345
        if (pfd < 0)
139✔
UNCOV
346
                return log_error_errno(pfd, "Failed to chase /etc/locale.conf: %m");
×
347

348
        r = should_configure(pfd, f);
139✔
349
        if (r == 0)
139✔
350
                log_debug("Found /etc/locale.conf, assuming locale information has been configured.");
125✔
351
        if (r <= 0)
139✔
352
                return r;
353

354
        r = dir_fd_is_root(rfd);
14✔
355
        if (r < 0)
14✔
UNCOV
356
                return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
×
357

358
        if (arg_copy_locale && r == 0) {
14✔
359
                r = copy_file_atomic_at(AT_FDCWD, etc_locale_conf(), pfd, f, 0644, COPY_REFLINK);
3✔
360
                if (r != -ENOENT) {
3✔
361
                        if (r < 0)
3✔
UNCOV
362
                                return log_error_errno(r, "Failed to copy host's /etc/locale.conf: %m");
×
363

364
                        log_info("Copied host's /etc/locale.conf.");
3✔
365
                        return 0;
3✔
366
                }
367
        }
368

369
        r = prompt_locale(rfd, mute_console_link);
11✔
370
        if (r < 0)
11✔
371
                return r;
372

373
        if (!isempty(arg_locale))
11✔
374
                locales[i++] = strjoina("LANG=", arg_locale);
30✔
375
        if (!isempty(arg_locale_messages) && !streq_ptr(arg_locale_messages, arg_locale))
16✔
376
                locales[i++] = strjoina("LC_MESSAGES=", arg_locale_messages);
25✔
377

378
        if (i == 0)
11✔
379
                return 0;
380

381
        locales[i] = NULL;
7✔
382

383
        r = write_env_file(
7✔
384
                        pfd,
385
                        f,
386
                        /* headers= */ NULL,
387
                        locales,
388
                        WRITE_ENV_FILE_LABEL);
389
        if (r < 0)
7✔
UNCOV
390
                return log_error_errno(r, "Failed to write /etc/locale.conf: %m");
×
391

392
        log_info("/etc/locale.conf written.");
7✔
393
        return 1;
394
}
395

396
static int keymap_is_ok(const char* name, void *userdata) {
5✔
397
        int rfd = ASSERT_FD(PTR_TO_FD(userdata)), r;
5✔
398

399
        r = dir_fd_is_root(rfd);
5✔
400
        if (r < 0)
5✔
UNCOV
401
                log_debug_errno(r, "Unable to determine if operating on host root directory, assuming we are: %m");
×
402

403
        return r != 0 ? keymap_exists(name) > 0 : keymap_is_valid(name);
5✔
404
}
405

406
static int prompt_keymap(int rfd, sd_varlink **mute_console_link) {
19✔
407
        _cleanup_strv_free_ char **kmaps = NULL;
19✔
408
        int r;
19✔
409

410
        assert(rfd >= 0);
19✔
411

412
        if (arg_keymap)
19✔
413
                return 0;
414

415
        r = read_credential("firstboot.keymap", (void**) &arg_keymap, NULL);
16✔
416
        if (r < 0)
16✔
417
                log_debug_errno(r, "Failed to read credential firstboot.keymap, ignoring: %m");
15✔
418
        else {
419
                log_debug("Acquired keymap from credential.");
1✔
420
                return 0;
1✔
421
        }
422

423
        bool b;
15✔
424
        if (arg_prompt_keymap_auto) {
15✔
425
                _cleanup_free_ char *ttyname = NULL;
1✔
426

427
                r = getttyname_harder(STDOUT_FILENO, &ttyname);
1✔
428
                if (r < 0) {
1✔
429
                        log_debug_errno(r, "Cannot determine TTY we are connected, ignoring: %m");
1✔
430
                        b = false; /* if we can't resolve this, it's probably not a VT */
431
                } else {
UNCOV
432
                        b = tty_is_vc_resolve(ttyname);
×
UNCOV
433
                        log_debug("Detected connection to local console: %s", yes_no(b));
×
434
                }
435
        } else
436
                b = arg_prompt_keymap;
14✔
437
        if (!b) {
15✔
438
                log_debug("Prompting for keymap was not requested.");
14✔
439
                return 0;
14✔
440
        }
441

442
        r = get_keymaps(&kmaps);
1✔
443
        if (r == -ENOENT) /* no keymaps installed */
1✔
UNCOV
444
                return log_debug_errno(r, "No keymaps are installed.");
×
445
        if (r < 0)
1✔
UNCOV
446
                return log_error_errno(r, "Failed to read keymaps: %m");
×
447

448
        print_welcome(rfd, mute_console_link);
1✔
449

450
        return prompt_loop(
1✔
451
                        "Please enter the new keymap name or number",
452
                        GLYPH_KEYBOARD,
453
                        kmaps,
454
                        /* accepted= */ NULL,
455
                        /* ellipsize_percentage= */ 60,
456
                        /* n_columns= */ 3,
457
                        /* column_width= */ 20,
458
                        keymap_is_ok,
459
                        /* refresh= */ NULL,
460
                        FD_TO_PTR(rfd),
1✔
461
                        PROMPT_MAY_SKIP|PROMPT_SHOW_MENU,
462
                        &arg_keymap);
463
}
464

465
static int process_keymap(int rfd, sd_varlink **mute_console_link) {
139✔
466
        _cleanup_close_ int pfd = -EBADF;
139✔
467
        _cleanup_free_ char *f = NULL;
139✔
UNCOV
468
        _cleanup_strv_free_ char **keymap = NULL;
×
469
        int r;
139✔
470

471
        assert(rfd >= 0);
139✔
472

473
        pfd = chase_and_open_parent_at(rfd, etc_vconsole_conf(),
139✔
474
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
475
                                       &f);
476
        if (pfd < 0)
139✔
UNCOV
477
                return log_error_errno(pfd, "Failed to chase /etc/vconsole.conf: %m");
×
478

479
        r = should_configure(pfd, f);
139✔
480
        if (r == 0)
139✔
481
                log_debug("Found /etc/vconsole.conf, assuming console has been configured.");
117✔
482
        if (r <= 0)
139✔
483
                return r;
484

485
        r = dir_fd_is_root(rfd);
22✔
486
        if (r < 0)
22✔
UNCOV
487
                return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
×
488

489
        if (arg_copy_keymap && r == 0) {
22✔
490
                r = copy_file_atomic_at(AT_FDCWD, etc_vconsole_conf(), pfd, f, 0644, COPY_REFLINK);
3✔
491
                if (r != -ENOENT) {
3✔
492
                        if (r < 0)
3✔
UNCOV
493
                                return log_error_errno(r, "Failed to copy host's /etc/vconsole.conf: %m");
×
494

495
                        log_info("Copied host's /etc/vconsole.conf.");
3✔
496
                        return 0;
3✔
497
                }
498
        }
499

500
        r = prompt_keymap(rfd, mute_console_link);
19✔
501
        if (r == -ENOENT)
19✔
502
                return 0; /* don't fail if no keymaps are installed */
503
        if (r < 0)
19✔
504
                return r;
505

506
        if (isempty(arg_keymap))
144✔
507
                return 0;
508

509
        VCContext vc = {
5✔
510
                .keymap = arg_keymap,
511
        };
512
        _cleanup_(x11_context_clear) X11Context xc = {};
5✔
513

514
        r = vconsole_convert_to_x11(&vc, /* verify= */ NULL, &xc);
5✔
515
        if (r < 0)
5✔
UNCOV
516
                return log_error_errno(r, "Failed to convert keymap data: %m");
×
517

518
        r = vconsole_serialize(&vc, &xc, &keymap);
5✔
519
        if (r < 0)
5✔
UNCOV
520
                return log_error_errno(r, "Failed to serialize keymap data: %m");
×
521

522
        r = write_vconsole_conf(pfd, f, keymap);
5✔
523
        if (r < 0)
5✔
UNCOV
524
                return log_error_errno(r, "Failed to write /etc/vconsole.conf: %m");
×
525

526
        log_info("/etc/vconsole.conf written.");
5✔
527
        return 1;
528
}
529

530
static int timezone_is_ok(const char *name, void *userdata) {
1✔
531
        return timezone_is_valid(name, LOG_DEBUG);
1✔
532
}
533

534
static int prompt_timezone(int rfd, sd_varlink **mute_console_link) {
120✔
535
        _cleanup_strv_free_ char **zones = NULL;
120✔
536
        int r;
120✔
537

538
        assert(rfd >= 0);
120✔
539

540
        if (arg_timezone)
120✔
541
                return 0;
542

543
        r = read_credential("firstboot.timezone", (void**) &arg_timezone, NULL);
117✔
544
        if (r < 0)
117✔
545
                log_debug_errno(r, "Failed to read credential firstboot.timezone, ignoring: %m");
12✔
546
        else {
547
                log_debug("Acquired timezone from credential.");
105✔
548
                return 0;
105✔
549
        }
550

551
        if (!arg_prompt_timezone) {
12✔
552
                log_debug("Prompting for timezone was not requested.");
11✔
553
                return 0;
11✔
554
        }
555

556
        r = get_timezones(&zones);
1✔
557
        if (r < 0)
1✔
UNCOV
558
                return log_error_errno(r, "Cannot query timezone list: %m");
×
559

560
        print_welcome(rfd, mute_console_link);
1✔
561

562
        return prompt_loop(
1✔
563
                        "Please enter the new timezone name or number",
564
                        GLYPH_CLOCK,
565
                        zones,
566
                        /* accepted= */ NULL,
567
                        /* ellipsize_percentage= */ 30,
568
                        /* n_columns= */ 3,
569
                        /* column_width= */ 20,
570
                        timezone_is_ok,
571
                        /* refresh= */ NULL,
572
                        FD_TO_PTR(rfd),
1✔
573
                        PROMPT_MAY_SKIP|PROMPT_SHOW_MENU,
574
                        &arg_timezone);
575
}
576

577
static int process_timezone(int rfd, sd_varlink **mute_console_link) {
139✔
578
        _cleanup_close_ int pfd = -EBADF;
139✔
579
        _cleanup_free_ char *f = NULL, *relpath = NULL;
139✔
580
        const char *e;
139✔
581
        int r;
139✔
582

583
        assert(rfd >= 0);
139✔
584

585
        pfd = chase_and_open_parent_at(rfd, etc_localtime(),
139✔
586
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
587
                                       &f);
588
        if (pfd < 0)
139✔
UNCOV
589
                return log_error_errno(pfd, "Failed to chase /etc/localtime: %m");
×
590

591
        r = should_configure(pfd, f);
139✔
592
        if (r == 0)
139✔
593
                log_debug("Found /etc/localtime, assuming timezone has been configured.");
16✔
594
        if (r <= 0)
139✔
595
                return r;
596

597
        r = dir_fd_is_root(rfd);
123✔
598
        if (r < 0)
123✔
UNCOV
599
                return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
×
600

601
        if (arg_copy_timezone && r == 0) {
123✔
602
                _cleanup_free_ char *s = NULL;
3✔
603

604
                r = readlink_malloc(etc_localtime(), &s);
3✔
605
                if (r != -ENOENT) {
3✔
606
                        if (r < 0)
3✔
UNCOV
607
                                return log_error_errno(r, "Failed to read host's /etc/localtime: %m");
×
608

609
                        r = symlinkat_atomic_full(s, pfd, f, SYMLINK_LABEL);
3✔
610
                        if (r < 0)
3✔
UNCOV
611
                                return log_error_errno(r, "Failed to create /etc/localtime symlink: %m");
×
612

613
                        log_info("Copied host's /etc/localtime.");
3✔
614
                        return 0;
3✔
615
                }
616
        }
617

618
        r = prompt_timezone(rfd, mute_console_link);
120✔
619
        if (r < 0)
120✔
620
                return r;
621

622
        if (isempty(arg_timezone))
248✔
623
                return 0;
624

625
        e = strjoina("/usr/share/zoneinfo/", arg_timezone);
545✔
626
        r = path_make_relative_parent(etc_localtime(), e, &relpath);
109✔
627
        if (r < 0)
109✔
628
                return r;
629

630
        r = symlinkat_atomic_full(relpath, pfd, f, SYMLINK_LABEL);
109✔
631
        if (r < 0)
109✔
UNCOV
632
                return log_error_errno(r, "Failed to create /etc/localtime symlink: %m");
×
633

634
        log_info("/etc/localtime written");
109✔
635
        return 0;
636
}
637

638
static int hostname_is_ok(const char *name, void *userdata) {
1✔
639
        return hostname_is_valid(name, VALID_HOSTNAME_TRAILING_DOT);
1✔
640
}
641

642
static int prompt_hostname(int rfd, sd_varlink **mute_console_link) {
125✔
643
        int r;
125✔
644

645
        assert(rfd >= 0);
125✔
646

647
        if (arg_hostname)
125✔
648
                return 0;
649

650
        if (!arg_prompt_hostname) {
121✔
651
                log_debug("Prompting for hostname was not requested.");
120✔
652
                return 0;
120✔
653
        }
654

655
        print_welcome(rfd, mute_console_link);
1✔
656

657
        r = prompt_loop("Please enter the new hostname",
2✔
658
                        GLYPH_LABEL,
659
                        /* menu= */ NULL,
660
                        /* accepted= */ NULL,
661
                        /* ellipsize_percentage= */ 100,
662
                        /* n_columns= */ 3,
663
                        /* column_width= */ 20,
664
                        hostname_is_ok,
665
                        /* refresh= */ NULL,
666
                        FD_TO_PTR(rfd),
1✔
667
                        PROMPT_MAY_SKIP,
668
                        &arg_hostname);
669
        if (r < 0)
1✔
670
                return r;
671

672
        if (arg_hostname)
1✔
673
                hostname_cleanup(arg_hostname);
1✔
674

675
        return 0;
676
}
677

678
static int process_hostname(int rfd, sd_varlink **mute_console_link) {
139✔
679
        _cleanup_close_ int pfd = -EBADF;
139✔
680
        _cleanup_free_ char *f = NULL;
139✔
681
        int r;
139✔
682

683
        assert(rfd >= 0);
139✔
684

685
        pfd = chase_and_open_parent_at(rfd, etc_hostname(),
139✔
686
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN,
687
                                       &f);
688
        if (pfd < 0)
139✔
UNCOV
689
                return log_error_errno(pfd, "Failed to chase /etc/hostname: %m");
×
690

691
        r = should_configure(pfd, f);
139✔
692
        if (r == 0)
139✔
693
                log_debug("Found /etc/hostname, assuming hostname has been configured.");
14✔
694
        if (r <= 0)
139✔
695
                return r;
696

697
        r = prompt_hostname(rfd, mute_console_link);
125✔
698
        if (r < 0)
125✔
699
                return r;
700

701
        if (isempty(arg_hostname))
144✔
702
                return 0;
703

704
        r = write_string_file_at(pfd, f, arg_hostname,
5✔
705
                                 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL);
706
        if (r < 0)
5✔
UNCOV
707
                return log_error_errno(r, "Failed to write /etc/hostname: %m");
×
708

709
        log_info("/etc/hostname written.");
5✔
710
        return 0;
711
}
712

713
static int process_machine_id(int rfd) {
139✔
714
        _cleanup_close_ int pfd = -EBADF;
139✔
715
        _cleanup_free_ char *f = NULL;
139✔
716
        int r;
139✔
717

718
        assert(rfd >= 0);
139✔
719

720
        pfd = chase_and_open_parent_at(rfd, "/etc/machine-id",
139✔
721
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
722
                                       &f);
723
        if (pfd < 0)
139✔
UNCOV
724
                return log_error_errno(pfd, "Failed to chase /etc/machine-id: %m");
×
725

726
        r = should_configure(pfd, f);
139✔
727
        if (r == 0)
139✔
728
                log_debug("Found /etc/machine-id, assuming machine-id has been configured.");
114✔
729
        if (r <= 0)
139✔
730
                return r;
731

732
        if (sd_id128_is_null(arg_machine_id)) {
25✔
733
                log_debug("Initialization of machine-id was not requested, skipping.");
22✔
734
                return 0;
22✔
735
        }
736

737
        r = write_string_file_at(pfd, "machine-id", SD_ID128_TO_STRING(arg_machine_id),
3✔
738
                                 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL);
739
        if (r < 0)
3✔
UNCOV
740
                return log_error_errno(r, "Failed to write /etc/machine-id: %m");
×
741

742
        log_info("/etc/machine-id written.");
3✔
743
        return 0;
744
}
745

746
static int prompt_root_password(int rfd, sd_varlink **mute_console_link) {
30✔
747
        const char *msg1, *msg2;
30✔
748
        int r;
30✔
749

750
        assert(rfd >= 0);
30✔
751

752
        if (arg_root_password)
30✔
753
                return 0;
754

755
        if (get_credential_user_password("root", &arg_root_password, &arg_root_password_is_hashed) >= 0)
21✔
756
                return 0;
757

758
        if (!arg_prompt_root_password) {
20✔
759
                log_debug("Prompting for root password was not requested.");
19✔
760
                return 0;
19✔
761
        }
762

763
        print_welcome(rfd, mute_console_link);
1✔
764

765
        msg1 = "Please enter the new root password (empty to skip):";
1✔
766
        msg2 = "Please enter the new root password again:";
1✔
767

768
        suggest_passwords();
1✔
769

UNCOV
770
        for (;;) {
×
771
                _cleanup_strv_free_erase_ char **a = NULL, **b = NULL;
1✔
772
                _cleanup_free_ char *error = NULL;
1✔
773

774
                AskPasswordRequest req = {
1✔
775
                        .tty_fd = -EBADF,
776
                        .message = msg1,
777
                        .until = USEC_INFINITY,
778
                        .hup_fd = -EBADF,
779
                };
780

781
                r = ask_password_tty(&req, /* flags= */ 0, &a);
1✔
782
                if (r < 0)
1✔
UNCOV
783
                        return log_error_errno(r, "Failed to query root password: %m");
×
784
                if (strv_length(a) != 1)
1✔
UNCOV
785
                        return log_error_errno(SYNTHETIC_ERRNO(EIO),
×
786
                                               "Received multiple passwords, where we expected one.");
787

788
                if (isempty(*a)) {
1✔
789
                        log_info("No password entered, skipping.");
1✔
790
                        break;
791
                }
792

793
                r = check_password_quality(*a, /* old= */ NULL, "root", &error);
×
794
                if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
×
UNCOV
795
                        log_warning("Password quality check is not supported, proceeding anyway.");
×
796
                else if (r < 0)
×
UNCOV
797
                        return log_error_errno(r, "Failed to check password quality: %m");
×
798
                else if (r == 0)
×
799
                        log_warning("Password is weak, accepting anyway: %s", error);
×
800

801
                req.message = msg2;
×
802

UNCOV
803
                r = ask_password_tty(&req, /* flags= */ 0, &b);
×
UNCOV
804
                if (r < 0)
×
805
                        return log_error_errno(r, "Failed to query root password: %m");
×
806
                if (strv_length(b) != 1)
×
807
                        return log_error_errno(SYNTHETIC_ERRNO(EIO),
×
808
                                               "Received multiple passwords, where we expected one.");
809

810
                if (!streq(*a, *b)) {
×
811
                        log_error("Entered passwords did not match, please try again.");
×
UNCOV
812
                        continue;
×
813
                }
814

UNCOV
815
                arg_root_password = TAKE_PTR(*a);
×
UNCOV
816
                break;
×
817
        }
818

819
        return 0;
1✔
820
}
821

822
static int find_shell(int rfd, const char *path) {
7✔
823
        int r;
7✔
824

825
        assert(path);
7✔
826

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

830
        r = chaseat(rfd, path, CHASE_AT_RESOLVE_IN_ROOT, NULL, NULL);
7✔
831
        if (r < 0)
7✔
832
                return log_error_errno(r, "Failed to resolve shell %s: %m", path);
1✔
833

834
        return 0;
835
}
836

837
static int shell_is_ok(const char *path, void *userdata) {
2✔
838
        int rfd = ASSERT_FD(PTR_TO_FD(userdata));
2✔
839

840
        return find_shell(rfd, path) >= 0;
2✔
841
}
842

843
static int prompt_root_shell(int rfd, sd_varlink **mute_console_link) {
30✔
844
        int r;
30✔
845

846
        assert(rfd >= 0);
30✔
847

848
        if (arg_root_shell)
30✔
849
                return 0;
850

851
        r = read_credential("passwd.shell.root", (void**) &arg_root_shell, NULL);
25✔
852
        if (r < 0)
25✔
853
                log_debug_errno(r, "Failed to read credential passwd.shell.root, ignoring: %m");
24✔
854
        else {
855
                log_debug("Acquired root shell from credential.");
1✔
856
                return 0;
1✔
857
        }
858

859
        if (!arg_prompt_root_shell) {
24✔
860
                log_debug("Prompting for root shell was not requested.");
22✔
861
                return 0;
22✔
862
        }
863

864
        print_welcome(rfd, mute_console_link);
2✔
865

866
        return prompt_loop(
2✔
867
                        "Please enter the new root shell",
868
                        GLYPH_SHELL,
869
                        /* menu= */ NULL,
870
                        /* accepted= */ NULL,
871
                        /* ellipsize_percentage= */ 0,
872
                        /* n_columns= */ 3,
873
                        /* column_width= */ 20,
874
                        shell_is_ok,
875
                        /* refresh= */ NULL,
876
                        FD_TO_PTR(rfd),
2✔
877
                        PROMPT_MAY_SKIP,
878
                        &arg_root_shell);
879
}
880

881
static int write_root_passwd(int rfd, int etc_fd, const char *password, const char *shell) {
15✔
882
        _cleanup_fclose_ FILE *original = NULL, *passwd = NULL;
15✔
883
        _cleanup_(unlink_and_freep) char *passwd_tmp = NULL;
15✔
884
        int r;
15✔
885
        bool found = false;
15✔
886

887
        r = fopen_temporary_at_label(etc_fd, "passwd", "passwd", &passwd, &passwd_tmp);
15✔
888
        if (r < 0)
15✔
889
                return r;
890

891
        r = xfopenat(etc_fd, "passwd", "re", O_NOFOLLOW, &original);
15✔
892
        if (r < 0 && r != -ENOENT)
15✔
893
                return r;
894

895
        if (original) {
15✔
896
                struct passwd *i;
4✔
897

898
                r = copy_rights(fileno(original), fileno(passwd));
4✔
899
                if (r < 0)
4✔
UNCOV
900
                        return r;
×
901

902
                while ((r = fgetpwent_sane(original, &i)) > 0) {
7✔
903

904
                        if (streq(i->pw_name, "root")) {
3✔
905
                                if (password)
3✔
906
                                        i->pw_passwd = (char *) password;
2✔
907
                                if (shell)
3✔
908
                                        i->pw_shell = (char *) shell;
2✔
909
                                found = true;
910
                        }
911

912
                        r = putpwent_sane(i, passwd);
3✔
913
                        if (r < 0)
3✔
914
                                return r;
915
                }
916
                if (r < 0)
4✔
917
                        return r;
918

919
        } else {
920
                r = fchmod(fileno(passwd), 0644);
11✔
921
                if (r < 0)
11✔
UNCOV
922
                        return -errno;
×
923
        }
924

925
        if (!found) {
4✔
926
                struct passwd root = {
12✔
927
                        .pw_name = (char *) "root",
928
                        .pw_passwd = (char *) (password ?: PASSWORD_SEE_SHADOW),
12✔
929
                        .pw_uid = 0,
930
                        .pw_gid = 0,
931
                        .pw_gecos = (char *) "Super User",
932
                        .pw_dir = (char *) "/root",
933
                        .pw_shell = (char *) (shell ?: default_root_shell_at(rfd)),
6✔
934
                };
935

936
                r = putpwent_sane(&root, passwd);
12✔
937
                if (r < 0)
12✔
UNCOV
938
                        return r;
×
939
        }
940

941
        r = fflush_sync_and_check(passwd);
15✔
942
        if (r < 0)
15✔
943
                return r;
944

945
        r = renameat_and_apply_smack_floor_label(etc_fd, passwd_tmp, etc_fd, "passwd");
15✔
946
        if (r < 0)
15✔
UNCOV
947
                return r;
×
948

949
        return 0;
950
}
951

952
static int write_root_shadow(int etc_fd, const char *hashed_password) {
15✔
953
        _cleanup_fclose_ FILE *original = NULL, *shadow = NULL;
15✔
954
        _cleanup_(unlink_and_freep) char *shadow_tmp = NULL;
15✔
955
        int r;
15✔
956
        bool found = false;
15✔
957

958
        r = fopen_temporary_at_label(etc_fd, "shadow", "shadow", &shadow, &shadow_tmp);
15✔
959
        if (r < 0)
15✔
960
                return r;
961

962
        r = xfopenat(etc_fd, "shadow", "re", O_NOFOLLOW, &original);
15✔
963
        if (r < 0 && r != -ENOENT)
15✔
964
                return r;
965

966
        if (original) {
15✔
967
                struct spwd *i;
4✔
968

969
                r = copy_rights(fileno(original), fileno(shadow));
4✔
970
                if (r < 0)
4✔
UNCOV
971
                        return r;
×
972

973
                while ((r = fgetspent_sane(original, &i)) > 0) {
7✔
974

975
                        if (streq(i->sp_namp, "root")) {
3✔
976
                                if (hashed_password) {
3✔
977
                                        i->sp_pwdp = (char *) hashed_password;
2✔
978
                                        i->sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY);
2✔
979
                                }
980
                                found = true;
981
                        }
982

983
                        r = putspent_sane(i, shadow);
3✔
984
                        if (r < 0)
3✔
985
                                return r;
986
                }
987
                if (r < 0)
4✔
988
                        return r;
989

990
        } else {
991
                r = fchmod(fileno(shadow), 0000);
11✔
992
                if (r < 0)
11✔
UNCOV
993
                        return -errno;
×
994
        }
995

996
        if (!found) {
4✔
997
                struct spwd root = {
36✔
998
                        .sp_namp = (char*) "root",
999
                        .sp_pwdp = (char *) (hashed_password ?: PASSWORD_LOCKED_AND_INVALID),
12✔
1000
                        .sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY),
12✔
1001
                        .sp_min = -1,
1002
                        .sp_max = -1,
1003
                        .sp_warn = -1,
1004
                        .sp_inact = -1,
1005
                        .sp_expire = -1,
1006
                        .sp_flag = ULONG_MAX, /* this appears to be what everybody does ... */
1007
                };
1008

1009
                r = putspent_sane(&root, shadow);
12✔
1010
                if (r < 0)
12✔
UNCOV
1011
                        return r;
×
1012
        }
1013

1014
        r = fflush_sync_and_check(shadow);
15✔
1015
        if (r < 0)
15✔
1016
                return r;
1017

1018
        r = renameat_and_apply_smack_floor_label(etc_fd, shadow_tmp, etc_fd, "shadow");
15✔
1019
        if (r < 0)
15✔
UNCOV
1020
                return r;
×
1021

1022
        return 0;
1023
}
1024

1025
static int process_root_account(int rfd, sd_varlink **mute_console_link) {
139✔
1026
        _cleanup_close_ int pfd = -EBADF;
139✔
UNCOV
1027
        _cleanup_(release_lock_file) LockFile lock = LOCK_FILE_INIT;
×
1028
        _cleanup_(erase_and_freep) char *_hashed_password = NULL;
139✔
1029
        const char *password, *hashed_password;
139✔
1030
        int k = 0, r;
139✔
1031

1032
        assert(rfd >= 0);
139✔
1033

1034
        pfd = chase_and_open_parent_at(rfd, "/etc/passwd",
139✔
1035
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
1036
                                       NULL);
1037
        if (pfd < 0)
139✔
UNCOV
1038
                return log_error_errno(pfd, "Failed to chase /etc/passwd: %m");
×
1039

1040
        /* Ensure that passwd and shadow are in the same directory and are not symlinks. */
1041

1042
        FOREACH_STRING(s, "passwd", "shadow") {
417✔
1043
                r = verify_regular_at(pfd, s, /* follow= */ false);
278✔
1044
                if (r < 0 && r != -ENOENT)
278✔
UNCOV
1045
                        return log_error_errno(r, "Verification of /etc/%s being regular file failed: %m", s);
×
1046

1047
                r = should_configure(pfd, s);
278✔
1048
                if (r < 0)
278✔
1049
                        return r;
1050

1051
                k += r;
278✔
1052
        }
1053

1054
        if (k == 0) {
139✔
1055
                log_debug("Found /etc/passwd and /etc/shadow, assuming root account has been initialized.");
109✔
1056
                return 0;
109✔
1057
        }
1058

1059
        r = make_lock_file_at(pfd, ETC_PASSWD_LOCK_FILENAME, LOCK_EX, &lock);
30✔
1060
        if (r < 0)
30✔
UNCOV
1061
                return log_error_errno(r, "Failed to take a lock on /etc/passwd: %m");
×
1062

1063
        k = dir_fd_is_root(rfd);
30✔
1064
        if (k < 0)
30✔
UNCOV
1065
                return log_error_errno(k, "Failed to check if directory file descriptor is root: %m");
×
1066

1067
        if (arg_copy_root_shell && k == 0) {
30✔
1068
                _cleanup_free_ struct passwd *p = NULL;
2✔
1069

1070
                r = getpwnam_malloc("root", &p);
2✔
1071
                if (r < 0)
2✔
UNCOV
1072
                        return log_error_errno(r, "Failed to find passwd entry for root: %m");
×
1073

1074
                r = free_and_strdup(&arg_root_shell, p->pw_shell);
2✔
1075
                if (r < 0)
2✔
UNCOV
1076
                        return log_oom();
×
1077
        }
1078

1079
        r = prompt_root_shell(rfd, mute_console_link);
30✔
1080
        if (r < 0)
30✔
1081
                return r;
1082

1083
        if (arg_copy_root_password && k == 0) {
30✔
1084
                struct spwd *p;
2✔
1085

1086
                errno = 0;
2✔
1087
                p = getspnam("root");
2✔
1088
                if (!p)
2✔
UNCOV
1089
                        return log_error_errno(errno_or_else(EIO), "Failed to find shadow entry for root: %m");
×
1090

1091
                r = free_and_strdup(&arg_root_password, p->sp_pwdp);
2✔
1092
                if (r < 0)
2✔
UNCOV
1093
                        return log_oom();
×
1094

1095
                arg_root_password_is_hashed = true;
2✔
1096
        }
1097

1098
        r = prompt_root_password(rfd, mute_console_link);
30✔
1099
        if (r < 0)
30✔
1100
                return r;
1101

1102
        if (arg_root_password && arg_root_password_is_hashed) {
30✔
1103
                password = PASSWORD_SEE_SHADOW;
1104
                hashed_password = arg_root_password;
1105
        } else if (arg_root_password) {
24✔
1106
                r = hash_password(arg_root_password, &_hashed_password);
4✔
1107
                if (r < 0)
4✔
UNCOV
1108
                        return log_error_errno(r, "Failed to hash password: %m");
×
1109

1110
                password = PASSWORD_SEE_SHADOW;
4✔
1111
                hashed_password = _hashed_password;
4✔
1112

1113
        } else if (arg_delete_root_password) {
20✔
1114
                password = PASSWORD_SEE_SHADOW;
1115
                hashed_password = PASSWORD_NONE;
1116
        } else if (!arg_root_password && arg_prompt_root_password) {
19✔
1117
                /* If the user was prompted, but no password was supplied, lock the account. */
1118
                password = PASSWORD_SEE_SHADOW;
1119
                hashed_password = PASSWORD_LOCKED_AND_INVALID;
1120
        } else
1121
                /* Leave the password as is. */
1122
                password = hashed_password = NULL;
18✔
1123

1124
        /* Don't create/modify passwd and shadow if there's nothing to do. */
1125
        if (!(password || hashed_password || arg_root_shell)) {
30✔
1126
                log_debug("Initialization of root account was not requested, skipping.");
15✔
1127
                return 0;
15✔
1128
        }
1129

1130
        r = write_root_passwd(rfd, pfd, password, arg_root_shell);
15✔
1131
        if (r < 0)
15✔
UNCOV
1132
                return log_error_errno(r, "Failed to write /etc/passwd: %m");
×
1133

1134
        log_info("/etc/passwd written.");
15✔
1135

1136
        r = write_root_shadow(pfd, hashed_password);
15✔
1137
        if (r < 0)
15✔
UNCOV
1138
                return log_error_errno(r, "Failed to write /etc/shadow: %m");
×
1139

1140
        log_info("/etc/shadow written.");
15✔
1141
        return 0;
1142
}
1143

1144
static int process_kernel_cmdline(int rfd) {
139✔
1145
        _cleanup_close_ int pfd = -EBADF;
139✔
1146
        _cleanup_free_ char *f = NULL;
139✔
1147
        int r;
139✔
1148

1149
        assert(rfd >= 0);
139✔
1150

1151
        pfd = chase_and_open_parent_at(rfd, "/etc/kernel/cmdline",
139✔
1152
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
1153
                                       &f);
1154
        if (pfd < 0)
139✔
UNCOV
1155
                return log_error_errno(pfd, "Failed to chase /etc/kernel/cmdline: %m");
×
1156

1157
        r = should_configure(pfd, f);
139✔
1158
        if (r == 0)
139✔
1159
                log_debug("Found /etc/kernel/cmdline, assuming kernel command line has been configured.");
1✔
1160
        if (r <= 0)
139✔
1161
                return r;
1162

1163
        if (!arg_kernel_cmdline) {
138✔
1164
                log_debug("Creation of /etc/kernel/cmdline was not requested, skipping.");
136✔
1165
                return 0;
136✔
1166
        }
1167

1168
        r = write_string_file_at(pfd, "cmdline", arg_kernel_cmdline,
2✔
1169
                                 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL);
1170
        if (r < 0)
2✔
UNCOV
1171
                return log_error_errno(r, "Failed to write /etc/kernel/cmdline: %m");
×
1172

1173
        log_info("/etc/kernel/cmdline written.");
2✔
1174
        return 0;
1175
}
1176

1177
static int reset_one(int rfd, const char *path) {
6✔
1178
        _cleanup_close_ int pfd = -EBADF;
6✔
1179
        _cleanup_free_ char *f = NULL;
6✔
1180

1181
        assert(rfd >= 0);
6✔
1182
        assert(path);
6✔
1183

1184
        pfd = chase_and_open_parent_at(rfd, path, CHASE_AT_RESOLVE_IN_ROOT|CHASE_WARN|CHASE_NOFOLLOW, &f);
6✔
1185
        if (pfd == -ENOENT)
6✔
1186
                return 0;
1187
        if (pfd < 0)
6✔
UNCOV
1188
                return log_error_errno(pfd, "Failed to resolve %s: %m", path);
×
1189

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

1193
        log_info("Removed %s", path);
6✔
1194
        return 0;
1195
}
1196

1197
static int process_reset(int rfd) {
139✔
1198
        int r;
139✔
1199

1200
        assert(rfd >= 0);
139✔
1201

1202
        if (!arg_reset)
139✔
1203
                return 0;
1204

1205
        FOREACH_STRING(p,
7✔
1206
                       etc_locale_conf(),
1207
                       etc_vconsole_conf(),
1208
                       etc_hostname(),
1209
                       "/etc/machine-id",
1210
                       "/etc/kernel/cmdline",
1211
                       etc_localtime()) {
1212
                r = reset_one(rfd, p);
6✔
1213
                if (r < 0)
6✔
UNCOV
1214
                        return r;
×
1215
        }
1216

1217
        return 0;
1✔
1218
}
1219

1220
static int help(void) {
×
1221
        _cleanup_free_ char *link = NULL;
×
UNCOV
1222
        int r;
×
1223

UNCOV
1224
        r = terminal_urlify_man("systemd-firstboot", "1", &link);
×
UNCOV
1225
        if (r < 0)
×
UNCOV
1226
                return log_oom();
×
1227

UNCOV
1228
        printf("%1$s [OPTIONS...]\n"
×
1229
               "\n%3$sConfigures basic settings of the system.%4$s\n\n"
1230
               "  -h --help                       Show this help\n"
1231
               "     --version                    Show package version\n"
1232
               "     --root=PATH                  Operate on an alternate filesystem root\n"
1233
               "     --image=PATH                 Operate on disk image as filesystem root\n"
1234
               "     --image-policy=POLICY        Specify disk image dissection policy\n"
1235
               "     --locale=LOCALE              Set primary locale (LANG=)\n"
1236
               "     --locale-messages=LOCALE     Set message locale (LC_MESSAGES=)\n"
1237
               "     --keymap=KEYMAP              Set keymap\n"
1238
               "     --timezone=TIMEZONE          Set timezone\n"
1239
               "     --hostname=NAME              Set hostname\n"
1240
               "     --setup-machine-id           Set a random machine ID\n"
1241
               "     --machine-id=ID              Set specified machine ID\n"
1242
               "     --root-password=PASSWORD     Set root password from plaintext password\n"
1243
               "     --root-password-file=FILE    Set root password from file\n"
1244
               "     --root-password-hashed=HASH  Set root password from hashed password\n"
1245
               "     --root-shell=SHELL           Set root shell\n"
1246
               "     --kernel-command-line=CMDLINE\n"
1247
               "                                  Set kernel command line\n"
1248
               "     --prompt-locale              Prompt the user for locale settings\n"
1249
               "     --prompt-keymap              Prompt the user for keymap settings\n"
1250
               "     --prompt-keymap-auto         Prompt the user for keymap settings if invoked\n"
1251
               "                                  on local console\n"
1252
               "     --prompt-timezone            Prompt the user for timezone\n"
1253
               "     --prompt-hostname            Prompt the user for hostname\n"
1254
               "     --prompt-root-password       Prompt the user for root password\n"
1255
               "     --prompt-root-shell          Prompt the user for root shell\n"
1256
               "     --prompt                     Prompt for all of the above\n"
1257
               "     --copy-locale                Copy locale from host\n"
1258
               "     --copy-keymap                Copy keymap from host\n"
1259
               "     --copy-timezone              Copy timezone from host\n"
1260
               "     --copy-root-password         Copy root password from host\n"
1261
               "     --copy-root-shell            Copy root shell from host\n"
1262
               "     --copy                       Copy locale, keymap, timezone, root password\n"
1263
               "     --force                      Overwrite existing files\n"
1264
               "     --delete-root-password       Delete root password\n"
1265
               "     --welcome=no                 Disable the welcome text\n"
1266
               "     --chrome=no                  Don't show color bar at top and bottom of\n"
1267
               "                                  terminal\n"
1268
               "     --mute-console=yes           Tell kernel/PID 1 to not write to the console\n"
1269
               "                                  while running\n"
1270
               "     --reset                      Remove existing files\n"
1271
               "\nSee the %2$s for details.\n",
1272
               program_invocation_short_name,
1273
               link,
1274
               ansi_highlight(),
1275
               ansi_normal());
1276

1277
        return 0;
1278
}
1279

1280
static int parse_argv(int argc, char *argv[]) {
142✔
1281

1282
        enum {
142✔
1283
                ARG_VERSION = 0x100,
1284
                ARG_ROOT,
1285
                ARG_IMAGE,
1286
                ARG_IMAGE_POLICY,
1287
                ARG_LOCALE,
1288
                ARG_LOCALE_MESSAGES,
1289
                ARG_KEYMAP,
1290
                ARG_TIMEZONE,
1291
                ARG_HOSTNAME,
1292
                ARG_SETUP_MACHINE_ID,
1293
                ARG_MACHINE_ID,
1294
                ARG_ROOT_PASSWORD,
1295
                ARG_ROOT_PASSWORD_FILE,
1296
                ARG_ROOT_PASSWORD_HASHED,
1297
                ARG_ROOT_SHELL,
1298
                ARG_KERNEL_COMMAND_LINE,
1299
                ARG_PROMPT,
1300
                ARG_PROMPT_LOCALE,
1301
                ARG_PROMPT_KEYMAP,
1302
                ARG_PROMPT_KEYMAP_AUTO,
1303
                ARG_PROMPT_TIMEZONE,
1304
                ARG_PROMPT_HOSTNAME,
1305
                ARG_PROMPT_ROOT_PASSWORD,
1306
                ARG_PROMPT_ROOT_SHELL,
1307
                ARG_COPY,
1308
                ARG_COPY_LOCALE,
1309
                ARG_COPY_KEYMAP,
1310
                ARG_COPY_TIMEZONE,
1311
                ARG_COPY_ROOT_PASSWORD,
1312
                ARG_COPY_ROOT_SHELL,
1313
                ARG_FORCE,
1314
                ARG_DELETE_ROOT_PASSWORD,
1315
                ARG_WELCOME,
1316
                ARG_CHROME,
1317
                ARG_RESET,
1318
                ARG_MUTE_CONSOLE,
1319
        };
1320

1321
        static const struct option options[] = {
142✔
1322
                { "help",                    no_argument,       NULL, 'h'                         },
1323
                { "version",                 no_argument,       NULL, ARG_VERSION                 },
1324
                { "root",                    required_argument, NULL, ARG_ROOT                    },
1325
                { "image",                   required_argument, NULL, ARG_IMAGE                   },
1326
                { "image-policy",            required_argument, NULL, ARG_IMAGE_POLICY            },
1327
                { "locale",                  required_argument, NULL, ARG_LOCALE                  },
1328
                { "locale-messages",         required_argument, NULL, ARG_LOCALE_MESSAGES         },
1329
                { "keymap",                  required_argument, NULL, ARG_KEYMAP                  },
1330
                { "timezone",                required_argument, NULL, ARG_TIMEZONE                },
1331
                { "hostname",                required_argument, NULL, ARG_HOSTNAME                },
1332
                { "setup-machine-id",        no_argument,       NULL, ARG_SETUP_MACHINE_ID        },
1333
                { "machine-id",              required_argument, NULL, ARG_MACHINE_ID              },
1334
                { "root-password",           required_argument, NULL, ARG_ROOT_PASSWORD           },
1335
                { "root-password-file",      required_argument, NULL, ARG_ROOT_PASSWORD_FILE      },
1336
                { "root-password-hashed",    required_argument, NULL, ARG_ROOT_PASSWORD_HASHED    },
1337
                { "root-shell",              required_argument, NULL, ARG_ROOT_SHELL              },
1338
                { "kernel-command-line",     required_argument, NULL, ARG_KERNEL_COMMAND_LINE     },
1339
                { "prompt",                  no_argument,       NULL, ARG_PROMPT                  },
1340
                { "prompt-locale",           no_argument,       NULL, ARG_PROMPT_LOCALE           },
1341
                { "prompt-keymap",           no_argument,       NULL, ARG_PROMPT_KEYMAP           },
1342
                { "prompt-keymap-auto",      no_argument,       NULL, ARG_PROMPT_KEYMAP_AUTO      },
1343
                { "prompt-timezone",         no_argument,       NULL, ARG_PROMPT_TIMEZONE         },
1344
                { "prompt-hostname",         no_argument,       NULL, ARG_PROMPT_HOSTNAME         },
1345
                { "prompt-root-password",    no_argument,       NULL, ARG_PROMPT_ROOT_PASSWORD    },
1346
                { "prompt-root-shell",       no_argument,       NULL, ARG_PROMPT_ROOT_SHELL       },
1347
                { "copy",                    no_argument,       NULL, ARG_COPY                    },
1348
                { "copy-locale",             no_argument,       NULL, ARG_COPY_LOCALE             },
1349
                { "copy-keymap",             no_argument,       NULL, ARG_COPY_KEYMAP             },
1350
                { "copy-timezone",           no_argument,       NULL, ARG_COPY_TIMEZONE           },
1351
                { "copy-root-password",      no_argument,       NULL, ARG_COPY_ROOT_PASSWORD      },
1352
                { "copy-root-shell",         no_argument,       NULL, ARG_COPY_ROOT_SHELL         },
1353
                { "force",                   no_argument,       NULL, ARG_FORCE                   },
1354
                { "delete-root-password",    no_argument,       NULL, ARG_DELETE_ROOT_PASSWORD    },
1355
                { "welcome",                 required_argument, NULL, ARG_WELCOME                 },
1356
                { "chrome",                  required_argument, NULL, ARG_CHROME                  },
1357
                { "reset",                   no_argument,       NULL, ARG_RESET                   },
1358
                { "mute-console",            required_argument, NULL, ARG_MUTE_CONSOLE            },
1359
                {}
1360
        };
1361

1362
        int r, c;
142✔
1363

1364
        assert(argc >= 0);
142✔
1365
        assert(argv);
142✔
1366

1367
        while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
772✔
1368

1369
                switch (c) {
632✔
1370

UNCOV
1371
                case 'h':
×
UNCOV
1372
                        return help();
×
1373

UNCOV
1374
                case ARG_VERSION:
×
UNCOV
1375
                        return version();
×
1376

1377
                case ARG_ROOT:
38✔
1378
                        r = parse_path_argument(optarg, true, &arg_root);
38✔
1379
                        if (r < 0)
38✔
1380
                                return r;
1381
                        break;
1382

UNCOV
1383
                case ARG_IMAGE:
×
1384
                        r = parse_path_argument(optarg, false, &arg_image);
×
1385
                        if (r < 0)
×
1386
                                return r;
1387
                        break;
1388

UNCOV
1389
                case ARG_IMAGE_POLICY:
×
UNCOV
1390
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
UNCOV
1391
                        if (r < 0)
×
1392
                                return r;
1393
                        break;
1394

1395
                case ARG_LOCALE:
5✔
1396
                        r = free_and_strdup(&arg_locale, optarg);
5✔
1397
                        if (r < 0)
5✔
UNCOV
1398
                                return log_oom();
×
1399

1400
                        break;
1401

1402
                case ARG_LOCALE_MESSAGES:
4✔
1403
                        r = free_and_strdup(&arg_locale_messages, optarg);
4✔
1404
                        if (r < 0)
4✔
UNCOV
1405
                                return log_oom();
×
1406

1407
                        break;
1408

1409
                case ARG_KEYMAP:
4✔
1410
                        if (!keymap_is_valid(optarg))
4✔
1411
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1412
                                                       "Keymap %s is not valid.", optarg);
1413

1414
                        r = free_and_strdup(&arg_keymap, optarg);
4✔
1415
                        if (r < 0)
4✔
UNCOV
1416
                                return log_oom();
×
1417

1418
                        break;
1419

1420
                case ARG_TIMEZONE:
5✔
1421
                        if (!timezone_is_valid(optarg, LOG_ERR))
5✔
1422
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1✔
1423
                                                       "Timezone %s is not valid.", optarg);
1424

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

1429
                        break;
1430

1431
                case ARG_ROOT_PASSWORD:
4✔
1432
                        r = free_and_strdup(&arg_root_password, optarg);
4✔
1433
                        if (r < 0)
4✔
UNCOV
1434
                                return log_oom();
×
1435

1436
                        arg_root_password_is_hashed = false;
4✔
1437
                        break;
4✔
1438

1439
                case ARG_ROOT_PASSWORD_FILE:
1✔
1440
                        arg_root_password = mfree(arg_root_password);
1✔
1441

1442
                        r = read_one_line_file(optarg, &arg_root_password);
1✔
1443
                        if (r < 0)
1✔
UNCOV
1444
                                return log_error_errno(r, "Failed to read %s: %m", optarg);
×
1445

1446
                        arg_root_password_is_hashed = false;
1✔
1447
                        break;
1✔
1448

1449
                case ARG_ROOT_PASSWORD_HASHED:
4✔
1450
                        r = free_and_strdup(&arg_root_password, optarg);
4✔
1451
                        if (r < 0)
4✔
UNCOV
1452
                                return log_oom();
×
1453

1454
                        arg_root_password_is_hashed = true;
4✔
1455
                        break;
4✔
1456

1457
                case ARG_ROOT_SHELL:
5✔
1458
                        r = free_and_strdup(&arg_root_shell, optarg);
5✔
1459
                        if (r < 0)
5✔
UNCOV
1460
                                return log_oom();
×
1461

1462
                        break;
1463

1464
                case ARG_HOSTNAME:
5✔
1465
                        if (!hostname_is_valid(optarg, VALID_HOSTNAME_TRAILING_DOT|VALID_HOSTNAME_QUESTION_MARK))
5✔
1466
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1467
                                                       "Host name %s is not valid.", optarg);
1468

1469
                        r = free_and_strdup(&arg_hostname, optarg);
5✔
1470
                        if (r < 0)
5✔
UNCOV
1471
                                return log_oom();
×
1472

1473
                        hostname_cleanup(arg_hostname);
5✔
1474
                        break;
5✔
1475

1476
                case ARG_SETUP_MACHINE_ID:
1✔
1477
                        r = sd_id128_randomize(&arg_machine_id);
1✔
1478
                        if (r < 0)
1✔
UNCOV
1479
                                return log_error_errno(r, "Failed to generate randomized machine ID: %m");
×
1480

1481
                        break;
1482

1483
                case ARG_MACHINE_ID:
4✔
1484
                        r = sd_id128_from_string(optarg, &arg_machine_id);
4✔
1485
                        if (r < 0)
4✔
1486
                                return log_error_errno(r, "Failed to parse machine id %s.", optarg);
1✔
1487

1488
                        break;
1489

1490
                case ARG_KERNEL_COMMAND_LINE:
3✔
1491
                        r = free_and_strdup(&arg_kernel_cmdline, optarg);
3✔
1492
                        if (r < 0)
3✔
1493
                                return log_oom();
×
1494

1495
                        break;
1496

UNCOV
1497
                case ARG_PROMPT:
×
UNCOV
1498
                        arg_prompt_locale = arg_prompt_keymap = arg_prompt_timezone = arg_prompt_hostname =
×
UNCOV
1499
                                arg_prompt_root_password = arg_prompt_root_shell = true;
×
UNCOV
1500
                        arg_prompt_keymap_auto = false;
×
UNCOV
1501
                        break;
×
1502

1503
                case ARG_PROMPT_LOCALE:
106✔
1504
                        arg_prompt_locale = true;
106✔
1505
                        break;
106✔
1506

1507
                case ARG_PROMPT_KEYMAP:
2✔
1508
                        arg_prompt_keymap = true;
2✔
1509
                        arg_prompt_keymap_auto = false;
2✔
1510
                        break;
2✔
1511

1512
                case ARG_PROMPT_KEYMAP_AUTO:
105✔
1513
                        arg_prompt_keymap_auto = true;
105✔
1514
                        break;
105✔
1515

1516
                case ARG_PROMPT_TIMEZONE:
106✔
1517
                        arg_prompt_timezone = true;
106✔
1518
                        break;
106✔
1519

1520
                case ARG_PROMPT_HOSTNAME:
1✔
1521
                        arg_prompt_hostname = true;
1✔
1522
                        break;
1✔
1523

1524
                case ARG_PROMPT_ROOT_PASSWORD:
106✔
1525
                        arg_prompt_root_password = true;
106✔
1526
                        break;
106✔
1527

1528
                case ARG_PROMPT_ROOT_SHELL:
4✔
1529
                        arg_prompt_root_shell = true;
4✔
1530
                        break;
4✔
1531

1532
                case ARG_COPY:
1✔
1533
                        arg_copy_locale = arg_copy_keymap = arg_copy_timezone = arg_copy_root_password =
1✔
1534
                                arg_copy_root_shell = true;
1✔
1535
                        break;
1✔
1536

1537
                case ARG_COPY_LOCALE:
2✔
1538
                        arg_copy_locale = true;
2✔
1539
                        break;
2✔
1540

1541
                case ARG_COPY_KEYMAP:
2✔
1542
                        arg_copy_keymap = true;
2✔
1543
                        break;
2✔
1544

1545
                case ARG_COPY_TIMEZONE:
2✔
1546
                        arg_copy_timezone = true;
2✔
1547
                        break;
2✔
1548

1549
                case ARG_COPY_ROOT_PASSWORD:
1✔
1550
                        arg_copy_root_password = true;
1✔
1551
                        break;
1✔
1552

1553
                case ARG_COPY_ROOT_SHELL:
1✔
1554
                        arg_copy_root_shell = true;
1✔
1555
                        break;
1✔
1556

1557
                case ARG_FORCE:
3✔
1558
                        arg_force = true;
3✔
1559
                        break;
3✔
1560

1561
                case ARG_DELETE_ROOT_PASSWORD:
1✔
1562
                        arg_delete_root_password = true;
1✔
1563
                        break;
1✔
1564

1565
                case ARG_WELCOME:
1✔
1566
                        r = parse_boolean(optarg);
1✔
1567
                        if (r < 0)
1✔
1568
                                return log_error_errno(r, "Failed to parse --welcome= argument: %s", optarg);
×
1569

1570
                        arg_welcome = r;
1✔
1571
                        break;
1✔
1572

UNCOV
1573
                case ARG_CHROME:
×
UNCOV
1574
                        r = parse_boolean_argument("--chrome=", optarg, &arg_chrome);
×
UNCOV
1575
                        if (r < 0)
×
1576
                                return r;
1577

1578
                        break;
1579

1580
                case ARG_RESET:
1✔
1581
                        arg_reset = true;
1✔
1582
                        break;
1✔
1583

1584
                case ARG_MUTE_CONSOLE:
104✔
1585
                        r = parse_boolean_argument("--mute-console=", optarg, &arg_mute_console);
104✔
1586
                        if (r < 0)
104✔
1587
                                return r;
1588

1589
                        break;
1590

1591
                case '?':
1592
                        return -EINVAL;
1593

1594
                default:
×
UNCOV
1595
                        assert_not_reached();
×
1596
                }
1597

1598
        if (arg_delete_root_password && (arg_copy_root_password || arg_root_password || arg_prompt_root_password))
140✔
UNCOV
1599
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1600
                                       "--delete-root-password cannot be combined with other root password options.");
1601

1602
        if (arg_image && arg_root)
140✔
UNCOV
1603
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1604
                                       "--root= and --image= cannot be used together.");
1605

1606
        if (!sd_id128_is_null(arg_machine_id) && !(arg_image || arg_root) && !arg_force)
276✔
UNCOV
1607
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1608
                                       "--machine-id=/--setup-machine-id only works with --root= or --image=.");
1609

1610
        return 1;
1611
}
1612

1613
static int reload_system_manager(sd_bus **bus) {
×
1614
        int r;
×
1615

1616
        assert(bus);
×
1617

UNCOV
1618
        if (!*bus) {
×
1619
                r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL, RUNTIME_SCOPE_SYSTEM, bus);
×
1620
                if (r < 0)
×
UNCOV
1621
                        return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL, RUNTIME_SCOPE_SYSTEM);
×
1622
        }
1623

UNCOV
1624
        r = bus_service_manager_reload(*bus);
×
UNCOV
1625
        if (r < 0)
×
1626
                return r;
1627

1628
        log_info("Requested manager reload to apply locale configuration.");
×
1629
        return 0;
1630
}
1631

1632
static int reload_vconsole(sd_bus **bus) {
×
UNCOV
1633
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
1634
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
×
UNCOV
1635
        _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
×
1636
        const char *object;
×
1637
        int r;
×
1638

1639
        assert(bus);
×
1640

UNCOV
1641
        if (!*bus) {
×
1642
                r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL, RUNTIME_SCOPE_SYSTEM, bus);
×
1643
                if (r < 0)
×
1644
                        return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL, RUNTIME_SCOPE_SYSTEM);
×
1645
        }
1646

UNCOV
1647
        r = bus_wait_for_jobs_new(*bus, &w);
×
1648
        if (r < 0)
×
1649
                return log_error_errno(r, "Could not watch jobs: %m");
×
1650

1651
        r = bus_call_method(*bus, bus_systemd_mgr, "RestartUnit", &error, &reply,
×
1652
                            "ss", "systemd-vconsole-setup.service", "replace");
1653
        if (r < 0)
×
UNCOV
1654
                return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
×
1655

1656
        r = sd_bus_message_read(reply, "o", &object);
×
1657
        if (r < 0)
×
UNCOV
1658
                return bus_log_parse_error(r);
×
1659

UNCOV
1660
        r = bus_wait_for_jobs_one(w, object, BUS_WAIT_JOBS_LOG_ERROR, NULL);
×
UNCOV
1661
        if (r < 0)
×
UNCOV
1662
                return log_error_errno(r, "Failed to wait for systemd-vconsole-setup.service/restart: %m");
×
1663
        return 0;
1664
}
1665

1666
static int run(int argc, char *argv[]) {
142✔
1667
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
142✔
1668
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
142✔
UNCOV
1669
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
×
1670
        _cleanup_close_ int rfd = -EBADF;
142✔
1671
        int r;
142✔
1672

1673
        r = parse_argv(argc, argv);
142✔
1674
        if (r <= 0)
142✔
1675
                return r;
1676

1677
        log_setup();
140✔
1678

1679
        umask(0022);
140✔
1680

1681
        bool offline = arg_root || arg_image;
140✔
1682

1683
        if (!offline) {
104✔
1684
                /* If we are called without --root=/--image= let's honour the systemd.firstboot kernel
1685
                 * command line option, because we are called to provision the host with basic settings (as
1686
                 * opposed to some other file system tree/image) */
1687

1688
                bool enabled;
104✔
1689
                r = proc_cmdline_get_bool("systemd.firstboot", /* flags= */ 0, &enabled);
104✔
1690
                if (r < 0)
104✔
UNCOV
1691
                        return log_error_errno(r, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
×
1692
                if (r > 0 && !enabled) {
104✔
1693
                        log_debug("Found systemd.firstboot=no kernel command line argument, turning off all prompts.");
104✔
1694
                        arg_prompt_locale = arg_prompt_keymap = arg_prompt_keymap_auto = arg_prompt_timezone = arg_prompt_hostname = arg_prompt_root_password = arg_prompt_root_shell = false;
104✔
1695
                }
1696
        }
1697

1698
        r = mac_init();
140✔
1699
        if (r < 0)
140✔
1700
                return r;
1701

1702
        if (arg_image) {
140✔
UNCOV
1703
                assert(!arg_root);
×
1704

UNCOV
1705
                r = mount_image_privately_interactively(
×
1706
                                arg_image,
1707
                                arg_image_policy,
1708
                                DISSECT_IMAGE_GENERIC_ROOT |
1709
                                DISSECT_IMAGE_REQUIRE_ROOT |
1710
                                DISSECT_IMAGE_VALIDATE_OS |
1711
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
1712
                                DISSECT_IMAGE_FSCK |
1713
                                DISSECT_IMAGE_GROWFS |
1714
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
1715
                                &mounted_dir,
1716
                                &rfd,
1717
                                &loop_device);
1718
                if (r < 0)
×
1719
                        return r;
1720

UNCOV
1721
                arg_root = strdup(mounted_dir);
×
1722
                if (!arg_root)
×
UNCOV
1723
                        return log_oom();
×
1724
        } else {
1725
                rfd = open(empty_to_root(arg_root), O_DIRECTORY|O_CLOEXEC);
140✔
1726
                if (rfd < 0)
140✔
UNCOV
1727
                        return log_error_errno(errno, "Failed to open %s: %m", empty_to_root(arg_root));
×
1728
        }
1729

1730
        LOG_SET_PREFIX(arg_image ?: arg_root);
282✔
UNCOV
1731
        DEFER_VOID_CALL(chrome_hide);
×
1732

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

1736
        if (arg_keymap && !keymap_is_ok(arg_keymap, FD_TO_PTR(rfd)))
140✔
UNCOV
1737
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Keymap %s is not installed.", arg_keymap);
×
1738
        if (arg_locale && !locale_is_ok(arg_locale, FD_TO_PTR(rfd)))
140✔
UNCOV
1739
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale);
×
1740
        if (arg_locale_messages && !locale_is_ok(arg_locale_messages, FD_TO_PTR(rfd)))
140✔
UNCOV
1741
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale_messages);
×
1742

1743
        if (arg_root_shell) {
140✔
1744
                r = find_shell(rfd, arg_root_shell);
5✔
1745
                if (r < 0)
5✔
1746
                        return r;
1747
        }
1748

1749
        r = process_reset(rfd);
139✔
1750
        if (r < 0)
139✔
1751
                return r;
1752

1753
        _cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *mute_console_link = NULL;
140✔
1754
        r = process_locale(rfd, &mute_console_link);
139✔
1755
        if (r < 0)
139✔
1756
                return r;
1757
        if (r > 0 && !offline)
139✔
UNCOV
1758
                (void) reload_system_manager(&bus);
×
1759

1760
        r = process_keymap(rfd, &mute_console_link);
139✔
1761
        if (r < 0)
139✔
1762
                return r;
1763
        if (r > 0 && !offline)
139✔
UNCOV
1764
                (void) reload_vconsole(&bus);
×
1765

1766
        r = process_timezone(rfd, &mute_console_link);
139✔
1767
        if (r < 0)
139✔
1768
                return r;
1769

1770
        r = process_hostname(rfd, &mute_console_link);
139✔
1771
        if (r < 0)
139✔
1772
                return r;
1773

1774
        r = process_root_account(rfd, &mute_console_link);
139✔
1775
        if (r < 0)
139✔
1776
                return r;
1777

1778
        r = process_kernel_cmdline(rfd);
139✔
1779
        if (r < 0)
139✔
1780
                return r;
1781

1782
        r = process_machine_id(rfd);
139✔
1783
        if (r < 0)
139✔
UNCOV
1784
                return r;
×
1785

1786
        return 0;
1787
}
1788

1789
DEFINE_MAIN_FUNCTION(run);
142✔
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