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

systemd / systemd / 13210122091

07 Feb 2025 10:56PM UTC coverage: 71.748% (-0.07%) from 71.813%
13210122091

push

github

web-flow
network: allow to configure routing policy rule even if requesting interface is not activated yet (#36257)

Fixes a regression caused by 4f8b153d9
(v257).
Fixes #36244.

16 of 16 new or added lines in 2 files covered. (100.0%)

3079 existing lines in 54 files now uncovered.

292917 of 408258 relevant lines covered (71.75%)

714241.77 hits per line

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

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

3
#include <fcntl.h>
4
#include <getopt.h>
5
#include <linux/loop.h>
6
#include <unistd.h>
7

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 "kbd-util.h"
30
#include "libcrypt-util.h"
31
#include "locale-util.h"
32
#include "lock-util.h"
33
#include "main-func.h"
34
#include "memory-util.h"
35
#include "mkdir.h"
36
#include "mount-util.h"
37
#include "os-util.h"
38
#include "parse-argument.h"
39
#include "parse-util.h"
40
#include "password-quality-util.h"
41
#include "path-util.h"
42
#include "pretty-print.h"
43
#include "proc-cmdline.h"
44
#include "random-util.h"
45
#include "smack-util.h"
46
#include "string-util.h"
47
#include "strv.h"
48
#include "terminal-util.h"
49
#include "time-util.h"
50
#include "tmpfile-util-label.h"
51
#include "tmpfile-util.h"
52
#include "umask-util.h"
53
#include "user-util.h"
54
#include "vconsole-util.h"
55

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

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

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

103
        assert(rfd >= 0);
7✔
104

105
        if (!arg_welcome)
7✔
106
                return;
107

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

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

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

124
        (void) terminal_reset_defensive_locked(STDOUT_FILENO, /* switch_to_text= */ false);
6✔
125

126
        if (colors_enabled())
6✔
UNCOV
127
                printf("\nWelcome to your new installation of \x1B[%sm%s\x1B[0m!\n", ac, pn);
×
128
        else
129
                printf("\nWelcome to your new installation of %s!\n", pn);
6✔
130

131
        printf("\nPlease configure your system!\n\n");
6✔
132

133
        any_key_to_proceed();
6✔
134

135
        done = true;
6✔
136
}
137

138
static int prompt_loop(int rfd, const char *text, char **l, unsigned percentage, bool (*is_valid)(int rfd, const char *name), char **ret) {
7✔
139
        int r;
7✔
140

141
        assert(text);
7✔
142
        assert(is_valid);
7✔
143
        assert(ret);
7✔
144

145
        for (;;) {
7✔
146
                _cleanup_free_ char *p = NULL;
7✔
147
                unsigned u;
7✔
148

149
                r = ask_string(&p, strv_isempty(l) ? "%s %s (empty to skip): "
11✔
150
                                                   : "%s %s (empty to skip, \"list\" to list options): ",
151
                               special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), text);
152
                if (r < 0)
7✔
UNCOV
153
                        return log_error_errno(r, "Failed to query user: %m");
×
154

155
                if (isempty(p)) {
7✔
156
                        log_info("No data entered, skipping.");
×
UNCOV
157
                        return 0;
×
158
                }
159

160
                if (!strv_isempty(l)) {
7✔
161
                        if (streq(p, "list")) {
4✔
162
                                r = show_menu(l, 3, 20, percentage);
×
UNCOV
163
                                if (r < 0)
×
164
                                        return r;
165

166
                                putchar('\n');
×
UNCOV
167
                                continue;
×
168
                        }
169

170
                        r = safe_atou(p, &u);
4✔
171
                        if (r >= 0) {
4✔
172
                                if (u <= 0 || u > strv_length(l)) {
×
173
                                        log_error("Specified entry number out of range.");
×
UNCOV
174
                                        continue;
×
175
                                }
176

177
                                log_info("Selected '%s'.", l[u-1]);
×
UNCOV
178
                                return free_and_strdup_warn(ret, l[u-1]);
×
179
                        }
180
                }
181

182
                if (is_valid(rfd, p))
7✔
183
                        return free_and_replace(*ret, p);
7✔
184

185
                /* Be more helpful to the user, and give a hint what the user might have wanted to type. */
186
                const char *best_match = strv_find_closest(l, p);
×
187
                if (best_match)
×
UNCOV
188
                        log_error("Invalid data '%s', did you mean '%s'?", p, best_match);
×
189
                else
UNCOV
190
                        log_error("Invalid data '%s'.", p);
×
191
        }
192
}
193

194
static int should_configure(int dir_fd, const char *filename) {
1,064✔
195
        _cleanup_fclose_ FILE *passwd = NULL, *shadow = NULL;
1,064✔
196
        int r;
1,064✔
197

198
        assert(dir_fd >= 0);
1,064✔
199
        assert(filename);
1,064✔
200

201
        if (streq(filename, "passwd") && !arg_force)
1,064✔
202
                /* We may need to do additional checks, so open the file. */
203
                r = xfopenat(dir_fd, filename, "re", O_NOFOLLOW, &passwd);
131✔
204
        else
205
                r = RET_NERRNO(faccessat(dir_fd, filename, F_OK, AT_SYMLINK_NOFOLLOW));
933✔
206

207
        if (r == -ENOENT)
517✔
208
                return true; /* missing */
209
        if (r < 0)
655✔
UNCOV
210
                return log_error_errno(r, "Failed to access %s: %m", filename);
×
211
        if (arg_force)
655✔
212
                return true; /* exists, but if --force was given we should still configure the file. */
213

214
        if (!passwd)
641✔
215
                return false;
216

217
        /* In case of /etc/passwd, do an additional check for the root password field.
218
         * We first check that passwd redirects to shadow, and then we check shadow.
219
         */
220
        struct passwd *i;
221
        while ((r = fgetpwent_sane(passwd, &i)) > 0) {
108✔
222
                if (!streq(i->pw_name, "root"))
107✔
UNCOV
223
                        continue;
×
224

225
                if (streq_ptr(i->pw_passwd, PASSWORD_SEE_SHADOW))
107✔
226
                        break;
UNCOV
227
                log_debug("passwd: root account with non-shadow password found, treating root as configured");
×
228
                return false;
229
        }
230
        if (r < 0)
108✔
UNCOV
231
                return log_error_errno(r, "Failed to read %s: %m", filename);
×
232
        if (r == 0) {
108✔
233
                log_debug("No root account found in %s, assuming root is not configured.", filename);
1✔
234
                return true;
1✔
235
        }
236

237
        r = xfopenat(dir_fd, "shadow", "re", O_NOFOLLOW, &shadow);
107✔
238
        if (r == -ENOENT) {
107✔
239
                log_debug("No shadow file found, assuming root is not configured.");
×
UNCOV
240
                return true; /* missing */
×
241
        }
242
        if (r < 0)
107✔
UNCOV
243
                return log_error_errno(r, "Failed to access shadow: %m");
×
244

245
        struct spwd *j;
246
        while ((r = fgetspent_sane(shadow, &j)) > 0) {
107✔
247
                if (!streq(j->sp_namp, "root"))
107✔
UNCOV
248
                        continue;
×
249

250
                bool unprovisioned = streq_ptr(j->sp_pwdp, PASSWORD_UNPROVISIONED);
107✔
251
                log_debug("Root account found, %s.",
208✔
252
                          unprovisioned ? "with unprovisioned password, treating root as not configured" :
253
                                          "treating root as configured");
254
                return unprovisioned;
107✔
255
        }
256
        if (r < 0)
×
257
                return log_error_errno(r, "Failed to read shadow: %m");
×
UNCOV
258
        assert(r == 0);
×
259
        log_debug("No root account found in shadow, assuming root is not configured.");
1,064✔
260
        return true;
261
}
262

263
static bool locale_is_installed_bool(const char *name) {
×
UNCOV
264
        return locale_is_installed(name) > 0;
×
265
}
266

267
static bool locale_is_ok(int rfd, const char *name) {
10✔
268
        int r;
10✔
269

270
        assert(rfd >= 0);
10✔
271

272
        r = dir_fd_is_root(rfd);
10✔
273
        if (r < 0)
10✔
UNCOV
274
                log_debug_errno(r, "Unable to determine if operating on host root directory, assuming we are: %m");
×
275

276
        return r != 0 ? locale_is_installed_bool(name) : locale_is_valid(name);
10✔
277
}
278

279
static int prompt_locale(int rfd) {
10✔
UNCOV
280
        _cleanup_strv_free_ char **locales = NULL;
×
281
        bool acquired_from_creds = false;
10✔
282
        int r;
10✔
283

284
        assert(rfd >= 0);
10✔
285

286
        if (arg_locale || arg_locale_messages)
10✔
287
                return 0;
288

289
        r = read_credential("firstboot.locale", (void**) &arg_locale, NULL);
6✔
290
        if (r < 0)
6✔
291
                log_debug_errno(r, "Failed to read credential firstboot.locale, ignoring: %m");
5✔
292
        else
293
                acquired_from_creds = true;
294

295
        r = read_credential("firstboot.locale-messages", (void**) &arg_locale_messages, NULL);
6✔
296
        if (r < 0)
6✔
297
                log_debug_errno(r, "Failed to read credential firstboot.locale-messages, ignoring: %m");
5✔
298
        else
299
                acquired_from_creds = true;
300

301
        if (acquired_from_creds) {
5✔
302
                log_debug("Acquired locale from credentials.");
1✔
303
                return 0;
1✔
304
        }
305

306
        if (!arg_prompt_locale) {
5✔
307
                log_debug("Prompting for locale was not requested.");
4✔
308
                return 0;
4✔
309
        }
310

311
        r = get_locales(&locales);
1✔
312
        if (r < 0)
1✔
UNCOV
313
                return log_error_errno(r, "Cannot query locales list: %m");
×
314

315
        if (strv_isempty(locales))
1✔
UNCOV
316
                log_debug("No locales found, skipping locale selection.");
×
317
        else if (strv_length(locales) == 1) {
1✔
318

319
                if (streq(locales[0], SYSTEMD_DEFAULT_LOCALE))
×
UNCOV
320
                        log_debug("Only installed locale is default locale anyway, not setting locale explicitly.");
×
321
                else {
UNCOV
322
                        log_debug("Only a single locale available (%s), selecting it as default.", locales[0]);
×
323

324
                        arg_locale = strdup(locales[0]);
×
325
                        if (!arg_locale)
×
UNCOV
326
                                return log_oom();
×
327

328
                        /* Not setting arg_locale_message here, since it defaults to LANG anyway */
329
                }
330
        } else {
331
                print_welcome(rfd);
1✔
332

333
                r = prompt_loop(rfd, "Please enter the new system locale name or number",
1✔
334
                                locales, 60, locale_is_ok, &arg_locale);
335
                if (r < 0)
1✔
336
                        return r;
337

338
                if (isempty(arg_locale))
11✔
339
                        return 0;
340

341
                r = prompt_loop(rfd, "Please enter the new system message locale name or number",
1✔
342
                                locales, 60, locale_is_ok, &arg_locale_messages);
343
                if (r < 0)
1✔
344
                        return r;
345

346
                /* Suppress the messages setting if it's the same as the main locale anyway */
347
                if (streq_ptr(arg_locale, arg_locale_messages))
1✔
UNCOV
348
                        arg_locale_messages = mfree(arg_locale_messages);
×
349
        }
350

351
        return 0;
352
}
353

354
static int process_locale(int rfd) {
133✔
355
        _cleanup_close_ int pfd = -EBADF;
133✔
356
        _cleanup_free_ char *f = NULL;
133✔
357
        char* locales[3];
133✔
358
        unsigned i = 0;
133✔
359
        int r;
133✔
360

361
        assert(rfd >= 0);
133✔
362

363
        pfd = chase_and_open_parent_at(rfd, "/etc/locale.conf",
133✔
364
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
365
                                       &f);
366
        if (pfd < 0)
133✔
UNCOV
367
                return log_error_errno(pfd, "Failed to chase /etc/locale.conf: %m");
×
368

369
        r = should_configure(pfd, f);
133✔
370
        if (r == 0)
133✔
371
                log_debug("Found /etc/locale.conf, assuming locale information has been configured.");
121✔
372
        if (r <= 0)
133✔
373
                return r;
374

375
        r = dir_fd_is_root(rfd);
12✔
376
        if (r < 0)
12✔
UNCOV
377
                return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
×
378

379
        if (arg_copy_locale && r == 0) {
12✔
380
                r = copy_file_atomic_at(AT_FDCWD, "/etc/locale.conf", pfd, f, 0644, COPY_REFLINK);
2✔
381
                if (r != -ENOENT) {
2✔
382
                        if (r < 0)
2✔
UNCOV
383
                                return log_error_errno(r, "Failed to copy host's /etc/locale.conf: %m");
×
384

385
                        log_info("Copied host's /etc/locale.conf.");
2✔
386
                        return 0;
2✔
387
                }
388
        }
389

390
        r = prompt_locale(rfd);
10✔
391
        if (r < 0)
10✔
392
                return r;
393

394
        if (!isempty(arg_locale))
10✔
395
                locales[i++] = strjoina("LANG=", arg_locale);
25✔
396
        if (!isempty(arg_locale_messages) && !streq_ptr(arg_locale_messages, arg_locale))
15✔
397
                locales[i++] = strjoina("LC_MESSAGES=", arg_locale_messages);
25✔
398

399
        if (i == 0)
10✔
400
                return 0;
401

402
        locales[i] = NULL;
6✔
403

404
        r = write_env_file(pfd, f, NULL, locales);
6✔
405
        if (r < 0)
6✔
UNCOV
406
                return log_error_errno(r, "Failed to write /etc/locale.conf: %m");
×
407

408
        log_info("/etc/locale.conf written.");
133✔
409
        return 1;
410
}
411

412
static bool keymap_exists_bool(const char *name) {
×
UNCOV
413
        return keymap_exists(name) > 0;
×
414
}
415

416
static bool keymap_is_ok(int rfd, const char* name) {
4✔
417
        int r;
4✔
418

419
        assert(rfd >= 0);
4✔
420

421
        r = dir_fd_is_root(rfd);
4✔
422
        if (r < 0)
4✔
UNCOV
423
                log_debug_errno(r, "Unable to determine if operating on host root directory, assuming we are: %m");
×
424

425
        return r != 0 ? keymap_exists_bool(name) : keymap_is_valid(name);
4✔
426
}
427

428
static int prompt_keymap(int rfd) {
12✔
429
        _cleanup_strv_free_ char **kmaps = NULL;
12✔
430
        int r;
12✔
431

432
        assert(rfd >= 0);
12✔
433

434
        if (arg_keymap)
12✔
435
                return 0;
436

437
        r = read_credential("firstboot.keymap", (void**) &arg_keymap, NULL);
10✔
438
        if (r < 0)
10✔
439
                log_debug_errno(r, "Failed to read credential firstboot.keymap, ignoring: %m");
9✔
440
        else {
441
                log_debug("Acquired keymap from credential.");
1✔
442
                return 0;
1✔
443
        }
444

445
        if (!arg_prompt_keymap) {
9✔
446
                log_debug("Prompting for keymap was not requested.");
8✔
447
                return 0;
8✔
448
        }
449

450
        r = get_keymaps(&kmaps);
1✔
451
        if (r == -ENOENT) /* no keymaps installed */
1✔
UNCOV
452
                return log_debug_errno(r, "No keymaps are installed.");
×
453
        if (r < 0)
1✔
UNCOV
454
                return log_error_errno(r, "Failed to read keymaps: %m");
×
455

456
        print_welcome(rfd);
1✔
457

458
        return prompt_loop(rfd, "Please enter the new keymap name or number",
1✔
459
                           kmaps, 60, keymap_is_ok, &arg_keymap);
460
}
461

462
static int process_keymap(int rfd) {
133✔
463
        _cleanup_close_ int pfd = -EBADF;
133✔
464
        _cleanup_free_ char *f = NULL;
133✔
UNCOV
465
        _cleanup_strv_free_ char **keymap = NULL;
×
466
        int r;
133✔
467

468
        assert(rfd >= 0);
133✔
469

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

476
        r = should_configure(pfd, f);
133✔
477
        if (r == 0)
133✔
478
                log_debug("Found /etc/vconsole.conf, assuming console has been configured.");
119✔
479
        if (r <= 0)
133✔
480
                return r;
481

482
        r = dir_fd_is_root(rfd);
14✔
483
        if (r < 0)
14✔
UNCOV
484
                return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
×
485

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

492
                        log_info("Copied host's /etc/vconsole.conf.");
2✔
493
                        return 0;
2✔
494
                }
495
        }
496

497
        r = prompt_keymap(rfd);
12✔
498
        if (r == -ENOENT)
12✔
499
                return 0; /* don't fail if no keymaps are installed */
500
        if (r < 0)
12✔
501
                return r;
502

503
        if (isempty(arg_keymap))
137✔
504
                return 0;
505

506
        VCContext vc = {
4✔
507
                .keymap = arg_keymap,
508
        };
509
        _cleanup_(x11_context_clear) X11Context xc = {};
×
510

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

515
        r = vconsole_serialize(&vc, &xc, &keymap);
4✔
516
        if (r < 0)
4✔
UNCOV
517
                return log_error_errno(r, "Failed to serialize keymap data: %m");
×
518

519
        r = write_vconsole_conf(pfd, f, keymap);
4✔
520
        if (r < 0)
4✔
UNCOV
521
                return log_error_errno(r, "Failed to write /etc/vconsole.conf: %m");
×
522

523
        log_info("/etc/vconsole.conf written.");
4✔
524
        return 1;
525
}
526

527
static bool timezone_is_ok(int rfd, const char *name) {
1✔
528
        assert(rfd >= 0);
1✔
529

530
        return timezone_is_valid(name, LOG_DEBUG);
1✔
531
}
532

533
static int prompt_timezone(int rfd) {
72✔
534
        _cleanup_strv_free_ char **zones = NULL;
72✔
535
        int r;
72✔
536

537
        assert(rfd >= 0);
72✔
538

539
        if (arg_timezone)
72✔
540
                return 0;
541

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

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

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

559
        print_welcome(rfd);
1✔
560

561
        return prompt_loop(rfd, "Please enter the new timezone name or number",
1✔
562
                           zones, 30, timezone_is_ok, &arg_timezone);
563
}
564

565
static int process_timezone(int rfd) {
133✔
566
        _cleanup_close_ int pfd = -EBADF;
133✔
567
        _cleanup_free_ char *f = NULL;
133✔
568
        const char *e;
133✔
569
        int r;
133✔
570

571
        assert(rfd >= 0);
133✔
572

573
        pfd = chase_and_open_parent_at(rfd, "/etc/localtime",
133✔
574
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
575
                                       &f);
576
        if (pfd < 0)
133✔
UNCOV
577
                return log_error_errno(pfd, "Failed to chase /etc/localtime: %m");
×
578

579
        r = should_configure(pfd, f);
133✔
580
        if (r == 0)
133✔
581
                log_debug("Found /etc/localtime, assuming timezone has been configured.");
59✔
582
        if (r <= 0)
133✔
583
                return r;
584

585
        r = dir_fd_is_root(rfd);
74✔
586
        if (r < 0)
74✔
587
                return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
×
588

589
        if (arg_copy_timezone && r == 0) {
74✔
590
                _cleanup_free_ char *s = NULL;
2✔
591

592
                r = readlink_malloc("/etc/localtime", &s);
2✔
593
                if (r != -ENOENT) {
2✔
594
                        if (r < 0)
2✔
UNCOV
595
                                return log_error_errno(r, "Failed to read host's /etc/localtime: %m");
×
596

597
                        r = symlinkat_atomic_full(s, pfd, f, /* make_relative= */ false);
2✔
598
                        if (r < 0)
2✔
UNCOV
599
                                return log_error_errno(r, "Failed to create /etc/localtime symlink: %m");
×
600

601
                        log_info("Copied host's /etc/localtime.");
2✔
602
                        return 0;
2✔
603
                }
604
        }
605

606
        r = prompt_timezone(rfd);
72✔
607
        if (r < 0)
72✔
608
                return r;
609

610
        if (isempty(arg_timezone))
72✔
611
                return 0;
612

613
        e = strjoina("../usr/share/zoneinfo/", arg_timezone);
310✔
614

615
        r = symlinkat_atomic_full(e, pfd, f, /* make_relative= */ false);
62✔
616
        if (r < 0)
62✔
UNCOV
617
                return log_error_errno(r, "Failed to create /etc/localtime symlink: %m");
×
618

619
        log_info("/etc/localtime written");
133✔
620
        return 0;
621
}
622

623
static bool hostname_is_ok(int rfd, const char *name) {
1✔
624
        assert(rfd >= 0);
1✔
625

626
        return hostname_is_valid(name, VALID_HOSTNAME_TRAILING_DOT);
1✔
627
}
628

629
static int prompt_hostname(int rfd) {
119✔
630
        int r;
119✔
631

632
        assert(rfd >= 0);
119✔
633

634
        if (arg_hostname)
119✔
635
                return 0;
636

637
        if (!arg_prompt_hostname) {
117✔
638
                log_debug("Prompting for hostname was not requested.");
116✔
639
                return 0;
116✔
640
        }
641

642
        print_welcome(rfd);
1✔
643

644
        r = prompt_loop(rfd, "Please enter the new hostname",
1✔
645
                        NULL, 0, hostname_is_ok, &arg_hostname);
646
        if (r < 0)
1✔
647
                return r;
648

649
        if (arg_hostname)
1✔
650
                hostname_cleanup(arg_hostname);
1✔
651

652
        return 0;
653
}
654

655
static int process_hostname(int rfd) {
133✔
656
        _cleanup_close_ int pfd = -EBADF;
133✔
657
        _cleanup_free_ char *f = NULL;
133✔
658
        int r;
133✔
659

660
        assert(rfd >= 0);
133✔
661

662
        pfd = chase_and_open_parent_at(rfd, "/etc/hostname",
133✔
663
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN,
664
                                       &f);
665
        if (pfd < 0)
133✔
UNCOV
666
                return log_error_errno(pfd, "Failed to chase /etc/hostname: %m");
×
667

668
        r = should_configure(pfd, f);
133✔
669
        if (r == 0)
133✔
670
                log_debug("Found /etc/hostname, assuming hostname has been configured.");
14✔
671
        if (r <= 0)
133✔
672
                return r;
673

674
        r = prompt_hostname(rfd);
119✔
675
        if (r < 0)
119✔
676
                return r;
677

678
        if (isempty(arg_hostname))
119✔
679
                return 0;
680

681
        r = write_string_file_at(pfd, f, arg_hostname,
3✔
682
                                 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC);
683
        if (r < 0)
3✔
UNCOV
684
                return log_error_errno(r, "Failed to write /etc/hostname: %m");
×
685

686
        log_info("/etc/hostname written.");
133✔
687
        return 0;
688
}
689

690
static int process_machine_id(int rfd) {
133✔
691
        _cleanup_close_ int pfd = -EBADF;
133✔
692
        _cleanup_free_ char *f = NULL;
133✔
693
        int r;
133✔
694

695
        assert(rfd >= 0);
133✔
696

697
        pfd = chase_and_open_parent_at(rfd, "/etc/machine-id",
133✔
698
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
699
                                       &f);
700
        if (pfd < 0)
133✔
UNCOV
701
                return log_error_errno(pfd, "Failed to chase /etc/machine-id: %m");
×
702

703
        r = should_configure(pfd, f);
133✔
704
        if (r == 0)
133✔
705
                log_debug("Found /etc/machine-id, assuming machine-id has been configured.");
111✔
706
        if (r <= 0)
133✔
707
                return r;
708

709
        if (sd_id128_is_null(arg_machine_id)) {
22✔
710
                log_debug("Initialization of machine-id was not requested, skipping.");
19✔
711
                return 0;
19✔
712
        }
713

714
        r = write_string_file_at(pfd, "machine-id", SD_ID128_TO_STRING(arg_machine_id),
3✔
715
                                 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC);
716
        if (r < 0)
3✔
UNCOV
717
                return log_error_errno(r, "Failed to write /etc/machine-id: %m");
×
718

719
        log_info("/etc/machine-id written.");
133✔
720
        return 0;
721
}
722

723
static int prompt_root_password(int rfd) {
27✔
724
        const char *msg1, *msg2;
27✔
725
        int r;
27✔
726

727
        assert(rfd >= 0);
27✔
728

729
        if (arg_root_password)
27✔
730
                return 0;
731

732
        if (get_credential_user_password("root", &arg_root_password, &arg_root_password_is_hashed) >= 0)
18✔
733
                return 0;
734

735
        if (!arg_prompt_root_password) {
17✔
736
                log_debug("Prompting for root password was not requested.");
16✔
737
                return 0;
16✔
738
        }
739

740
        print_welcome(rfd);
1✔
741

742
        msg1 = strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), " Please enter the new root password (empty to skip):");
5✔
743
        msg2 = strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), " Please enter the new root password again:");
5✔
744

745
        suggest_passwords();
1✔
746

UNCOV
747
        for (;;) {
×
748
                _cleanup_strv_free_erase_ char **a = NULL, **b = NULL;
1✔
749
                _cleanup_free_ char *error = NULL;
1✔
750

751
                AskPasswordRequest req = {
1✔
752
                        .tty_fd = -EBADF,
753
                        .message = msg1,
754
                        .until = USEC_INFINITY,
755
                        .hup_fd = -EBADF,
756
                };
757

758
                r = ask_password_tty(&req, /* flags= */ 0, &a);
1✔
759
                if (r < 0)
1✔
760
                        return log_error_errno(r, "Failed to query root password: %m");
×
761
                if (strv_length(a) != 1)
1✔
762
                        return log_error_errno(SYNTHETIC_ERRNO(EIO),
×
763
                                               "Received multiple passwords, where we expected one.");
764

765
                if (isempty(*a)) {
1✔
766
                        log_info("No password entered, skipping.");
1✔
767
                        break;
768
                }
769

770
                r = check_password_quality(*a, /* old */ NULL, "root", &error);
×
771
                if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
×
772
                        log_warning("Password quality check is not supported, proceeding anyway.");
×
UNCOV
773
                else if (r < 0)
×
UNCOV
774
                        return log_error_errno(r, "Failed to check password quality: %m");
×
775
                else if (r == 0)
×
776
                        log_warning("Password is weak, accepting anyway: %s", error);
×
777

UNCOV
778
                req.message = msg2;
×
779

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

UNCOV
787
                if (!streq(*a, *b)) {
×
UNCOV
788
                        log_error("Entered passwords did not match, please try again.");
×
UNCOV
789
                        continue;
×
790
                }
791

UNCOV
792
                arg_root_password = TAKE_PTR(*a);
×
793
                break;
×
794
        }
795

796
        return 0;
1✔
797
}
798

799
static int find_shell(int rfd, const char *path) {
7✔
800
        int r;
7✔
801

802
        assert(path);
7✔
803

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

807
        r = chaseat(rfd, path, CHASE_AT_RESOLVE_IN_ROOT, NULL, NULL);
7✔
808
        if (r < 0)
7✔
809
                return log_error_errno(r, "Failed to resolve shell %s: %m", path);
1✔
810

811
        return 0;
812
}
813

814
static bool shell_is_ok(int rfd, const char *path) {
2✔
815
        assert(rfd >= 0);
2✔
816

817
        return find_shell(rfd, path) >= 0;
2✔
818
}
819

820
static int prompt_root_shell(int rfd) {
27✔
821
        int r;
27✔
822

823
        assert(rfd >= 0);
27✔
824

825
        if (arg_root_shell)
27✔
826
                return 0;
827

828
        r = read_credential("passwd.shell.root", (void**) &arg_root_shell, NULL);
22✔
829
        if (r < 0)
22✔
830
                log_debug_errno(r, "Failed to read credential passwd.shell.root, ignoring: %m");
21✔
831
        else {
832
                log_debug("Acquired root shell from credential.");
1✔
833
                return 0;
1✔
834
        }
835

836
        if (!arg_prompt_root_shell) {
21✔
837
                log_debug("Prompting for root shell was not requested.");
19✔
838
                return 0;
19✔
839
        }
840

841
        print_welcome(rfd);
2✔
842

843
        return prompt_loop(rfd, "Please enter the new root shell",
2✔
844
                           NULL, 0, shell_is_ok, &arg_root_shell);
845
}
846

847
static int write_root_passwd(int rfd, int etc_fd, const char *password, const char *shell) {
15✔
848
        _cleanup_fclose_ FILE *original = NULL, *passwd = NULL;
15✔
849
        _cleanup_(unlink_and_freep) char *passwd_tmp = NULL;
15✔
850
        int r;
15✔
851
        bool found = false;
15✔
852

853
        r = fopen_temporary_at_label(etc_fd, "passwd", "passwd", &passwd, &passwd_tmp);
15✔
854
        if (r < 0)
15✔
855
                return r;
856

857
        r = xfopenat(etc_fd, "passwd", "re", O_NOFOLLOW, &original);
15✔
858
        if (r < 0 && r != -ENOENT)
15✔
859
                return r;
860

861
        if (original) {
15✔
862
                struct passwd *i;
4✔
863

864
                r = copy_rights(fileno(original), fileno(passwd));
4✔
865
                if (r < 0)
4✔
UNCOV
866
                        return r;
×
867

868
                while ((r = fgetpwent_sane(original, &i)) > 0) {
7✔
869

870
                        if (streq(i->pw_name, "root")) {
3✔
871
                                if (password)
3✔
872
                                        i->pw_passwd = (char *) password;
2✔
873
                                if (shell)
3✔
874
                                        i->pw_shell = (char *) shell;
2✔
875
                                found = true;
876
                        }
877

878
                        r = putpwent_sane(i, passwd);
3✔
879
                        if (r < 0)
3✔
880
                                return r;
881
                }
882
                if (r < 0)
4✔
883
                        return r;
884

885
        } else {
886
                r = fchmod(fileno(passwd), 0644);
11✔
887
                if (r < 0)
11✔
UNCOV
888
                        return -errno;
×
889
        }
890

891
        if (!found) {
4✔
892
                struct passwd root = {
12✔
893
                        .pw_name = (char *) "root",
894
                        .pw_passwd = (char *) (password ?: PASSWORD_SEE_SHADOW),
12✔
895
                        .pw_uid = 0,
896
                        .pw_gid = 0,
897
                        .pw_gecos = (char *) "Super User",
898
                        .pw_dir = (char *) "/root",
899
                        .pw_shell = (char *) (shell ?: default_root_shell_at(rfd)),
6✔
900
                };
901

902
                if (errno != ENOENT)
12✔
UNCOV
903
                        return -errno;
×
904

905
                r = putpwent_sane(&root, passwd);
12✔
906
                if (r < 0)
12✔
907
                        return r;
908
        }
909

910
        r = fflush_sync_and_check(passwd);
15✔
911
        if (r < 0)
15✔
912
                return r;
913

914
        r = renameat_and_apply_smack_floor_label(etc_fd, passwd_tmp, etc_fd, "passwd");
15✔
915
        if (r < 0)
15✔
UNCOV
916
                return r;
×
917

918
        return 0;
919
}
920

921
static int write_root_shadow(int etc_fd, const char *hashed_password) {
15✔
922
        _cleanup_fclose_ FILE *original = NULL, *shadow = NULL;
15✔
923
        _cleanup_(unlink_and_freep) char *shadow_tmp = NULL;
15✔
924
        int r;
15✔
925
        bool found = false;
15✔
926

927
        r = fopen_temporary_at_label(etc_fd, "shadow", "shadow", &shadow, &shadow_tmp);
15✔
928
        if (r < 0)
15✔
929
                return r;
930

931
        r = xfopenat(etc_fd, "shadow", "re", O_NOFOLLOW, &original);
15✔
932
        if (r < 0 && r != -ENOENT)
15✔
933
                return r;
934

935
        if (original) {
15✔
936
                struct spwd *i;
4✔
937

938
                r = copy_rights(fileno(original), fileno(shadow));
4✔
939
                if (r < 0)
4✔
UNCOV
940
                        return r;
×
941

942
                while ((r = fgetspent_sane(original, &i)) > 0) {
7✔
943

944
                        if (streq(i->sp_namp, "root")) {
3✔
945
                                if (hashed_password) {
3✔
946
                                        i->sp_pwdp = (char *) hashed_password;
2✔
947
                                        i->sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY);
2✔
948
                                }
949
                                found = true;
950
                        }
951

952
                        r = putspent_sane(i, shadow);
3✔
953
                        if (r < 0)
3✔
954
                                return r;
955
                }
956
                if (r < 0)
4✔
957
                        return r;
958

959
        } else {
960
                r = fchmod(fileno(shadow), 0000);
11✔
961
                if (r < 0)
11✔
UNCOV
962
                        return -errno;
×
963
        }
964

965
        if (!found) {
4✔
966
                struct spwd root = {
36✔
967
                        .sp_namp = (char*) "root",
968
                        .sp_pwdp = (char *) (hashed_password ?: PASSWORD_LOCKED_AND_INVALID),
12✔
969
                        .sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY),
12✔
970
                        .sp_min = -1,
971
                        .sp_max = -1,
972
                        .sp_warn = -1,
973
                        .sp_inact = -1,
974
                        .sp_expire = -1,
975
                        .sp_flag = ULONG_MAX, /* this appears to be what everybody does ... */
976
                };
977

978
                if (errno != ENOENT)
12✔
UNCOV
979
                        return -errno;
×
980

981
                r = putspent_sane(&root, shadow);
12✔
982
                if (r < 0)
12✔
983
                        return r;
984
        }
985

986
        r = fflush_sync_and_check(shadow);
15✔
987
        if (r < 0)
15✔
988
                return r;
989

990
        r = renameat_and_apply_smack_floor_label(etc_fd, shadow_tmp, etc_fd, "shadow");
15✔
991
        if (r < 0)
15✔
UNCOV
992
                return r;
×
993

994
        return 0;
995
}
996

997
static int process_root_account(int rfd) {
133✔
998
        _cleanup_close_ int pfd = -EBADF;
133✔
UNCOV
999
        _cleanup_(release_lock_file) LockFile lock = LOCK_FILE_INIT;
×
1000
        _cleanup_(erase_and_freep) char *_hashed_password = NULL;
133✔
1001
        const char *password, *hashed_password;
133✔
1002
        int k = 0, r;
133✔
1003

1004
        assert(rfd >= 0);
133✔
1005

1006
        pfd = chase_and_open_parent_at(rfd, "/etc/passwd",
133✔
1007
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
1008
                                       NULL);
1009
        if (pfd < 0)
133✔
UNCOV
1010
                return log_error_errno(pfd, "Failed to chase /etc/passwd: %m");
×
1011

1012
        /* Ensure that passwd and shadow are in the same directory and are not symlinks. */
1013

1014
        FOREACH_STRING(s, "passwd", "shadow") {
399✔
1015
                r = verify_regular_at(pfd, s, /* follow = */ false);
266✔
1016
                if (r < 0 && r != -ENOENT)
266✔
UNCOV
1017
                        return log_error_errno(r, "Verification of /etc/%s being regular file failed: %m", s);
×
1018

1019
                r = should_configure(pfd, s);
266✔
1020
                if (r < 0)
266✔
1021
                        return r;
1022

1023
                k += r;
266✔
1024
        }
1025

1026
        if (k == 0) {
133✔
1027
                log_debug("Found /etc/passwd and /etc/shadow, assuming root account has been initialized.");
106✔
1028
                return 0;
106✔
1029
        }
1030

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

1035
        k = dir_fd_is_root(rfd);
27✔
1036
        if (k < 0)
27✔
UNCOV
1037
                return log_error_errno(k, "Failed to check if directory file descriptor is root: %m");
×
1038

1039
        if (arg_copy_root_shell && k == 0) {
27✔
1040
                _cleanup_free_ struct passwd *p = NULL;
2✔
1041

1042
                r = getpwnam_malloc("root", &p);
2✔
1043
                if (r < 0)
2✔
UNCOV
1044
                        return log_error_errno(r, "Failed to find passwd entry for root: %m");
×
1045

1046
                r = free_and_strdup(&arg_root_shell, p->pw_shell);
2✔
1047
                if (r < 0)
2✔
UNCOV
1048
                        return log_oom();
×
1049
        }
1050

1051
        r = prompt_root_shell(rfd);
27✔
1052
        if (r < 0)
27✔
1053
                return r;
1054

1055
        if (arg_copy_root_password && k == 0) {
27✔
1056
                struct spwd *p;
2✔
1057

1058
                errno = 0;
2✔
1059
                p = getspnam("root");
2✔
1060
                if (!p)
2✔
UNCOV
1061
                        return log_error_errno(errno_or_else(EIO), "Failed to find shadow entry for root: %m");
×
1062

1063
                r = free_and_strdup(&arg_root_password, p->sp_pwdp);
2✔
1064
                if (r < 0)
2✔
UNCOV
1065
                        return log_oom();
×
1066

1067
                arg_root_password_is_hashed = true;
2✔
1068
        }
1069

1070
        r = prompt_root_password(rfd);
27✔
1071
        if (r < 0)
27✔
1072
                return r;
1073

1074
        if (arg_root_password && arg_root_password_is_hashed) {
27✔
1075
                password = PASSWORD_SEE_SHADOW;
1076
                hashed_password = arg_root_password;
1077
        } else if (arg_root_password) {
21✔
1078
                r = hash_password(arg_root_password, &_hashed_password);
4✔
1079
                if (r < 0)
4✔
UNCOV
1080
                        return log_error_errno(r, "Failed to hash password: %m");
×
1081

1082
                password = PASSWORD_SEE_SHADOW;
4✔
1083
                hashed_password = _hashed_password;
4✔
1084

1085
        } else if (arg_delete_root_password) {
17✔
1086
                password = PASSWORD_SEE_SHADOW;
1087
                hashed_password = PASSWORD_NONE;
1088
        } else if (!arg_root_password && arg_prompt_root_password) {
16✔
1089
                /* If the user was prompted, but no password was supplied, lock the account. */
1090
                password = PASSWORD_SEE_SHADOW;
1091
                hashed_password = PASSWORD_LOCKED_AND_INVALID;
1092
        } else
1093
                /* Leave the password as is. */
1094
                password = hashed_password = NULL;
15✔
1095

1096
        /* Don't create/modify passwd and shadow if there's nothing to do. */
1097
        if (!(password || hashed_password || arg_root_shell)) {
27✔
1098
                log_debug("Initialization of root account was not requested, skipping.");
12✔
1099
                return 0;
12✔
1100
        }
1101

1102
        r = write_root_passwd(rfd, pfd, password, arg_root_shell);
15✔
1103
        if (r < 0)
15✔
UNCOV
1104
                return log_error_errno(r, "Failed to write /etc/passwd: %m");
×
1105

1106
        log_info("/etc/passwd written.");
15✔
1107

1108
        r = write_root_shadow(pfd, hashed_password);
15✔
1109
        if (r < 0)
15✔
UNCOV
1110
                return log_error_errno(r, "Failed to write /etc/shadow: %m");
×
1111

1112
        log_info("/etc/shadow written.");
133✔
1113
        return 0;
1114
}
1115

1116
static int process_kernel_cmdline(int rfd) {
133✔
1117
        _cleanup_close_ int pfd = -EBADF;
133✔
1118
        _cleanup_free_ char *f = NULL;
133✔
1119
        int r;
133✔
1120

1121
        assert(rfd >= 0);
133✔
1122

1123
        pfd = chase_and_open_parent_at(rfd, "/etc/kernel/cmdline",
133✔
1124
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
1125
                                       &f);
1126
        if (pfd < 0)
133✔
UNCOV
1127
                return log_error_errno(pfd, "Failed to chase /etc/kernel/cmdline: %m");
×
1128

1129
        r = should_configure(pfd, f);
133✔
1130
        if (r == 0)
133✔
1131
                log_debug("Found /etc/kernel/cmdline, assuming kernel command line has been configured.");
1✔
1132
        if (r <= 0)
133✔
1133
                return r;
1134

1135
        if (!arg_kernel_cmdline) {
132✔
1136
                log_debug("Creation of /etc/kernel/cmdline was not requested, skipping.");
130✔
1137
                return 0;
130✔
1138
        }
1139

1140
        r = write_string_file_at(pfd, "cmdline", arg_kernel_cmdline,
2✔
1141
                                 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC);
1142
        if (r < 0)
2✔
UNCOV
1143
                return log_error_errno(r, "Failed to write /etc/kernel/cmdline: %m");
×
1144

1145
        log_info("/etc/kernel/cmdline written.");
133✔
1146
        return 0;
1147
}
1148

1149
static int reset_one(int rfd, const char *path) {
6✔
1150
        _cleanup_close_ int pfd = -EBADF;
6✔
1151
        _cleanup_free_ char *f = NULL;
6✔
1152

1153
        assert(rfd >= 0);
6✔
1154
        assert(path);
6✔
1155

1156
        pfd = chase_and_open_parent_at(rfd, path, CHASE_AT_RESOLVE_IN_ROOT|CHASE_WARN|CHASE_NOFOLLOW, &f);
6✔
1157
        if (pfd == -ENOENT)
6✔
1158
                return 0;
1159
        if (pfd < 0)
6✔
UNCOV
1160
                return log_error_errno(pfd, "Failed to resolve %s: %m", path);
×
1161

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

1165
        log_info("Removed %s", path);
6✔
1166
        return 0;
1167
}
1168

1169
static int process_reset(int rfd) {
133✔
1170
        int r;
133✔
1171

1172
        assert(rfd >= 0);
133✔
1173

1174
        if (!arg_reset)
133✔
1175
                return 0;
1176

1177
        FOREACH_STRING(p,
7✔
1178
                       "/etc/locale.conf",
1179
                       "/etc/vconsole.conf",
1180
                       "/etc/hostname",
1181
                       "/etc/machine-id",
1182
                       "/etc/kernel/cmdline",
1183
                       "/etc/localtime") {
1184
                r = reset_one(rfd, p);
6✔
1185
                if (r < 0)
6✔
1186
                        return r;
×
1187
        }
1188

1189
        return 0;
1✔
1190
}
1191

UNCOV
1192
static int help(void) {
×
UNCOV
1193
        _cleanup_free_ char *link = NULL;
×
UNCOV
1194
        int r;
×
1195

UNCOV
1196
        r = terminal_urlify_man("systemd-firstboot", "1", &link);
×
UNCOV
1197
        if (r < 0)
×
UNCOV
1198
                return log_oom();
×
1199

UNCOV
1200
        printf("%s [OPTIONS...]\n\n"
×
1201
               "Configures basic settings of the system.\n\n"
1202
               "  -h --help                       Show this help\n"
1203
               "     --version                    Show package version\n"
1204
               "     --root=PATH                  Operate on an alternate filesystem root\n"
1205
               "     --image=PATH                 Operate on disk image as filesystem root\n"
1206
               "     --image-policy=POLICY        Specify disk image dissection policy\n"
1207
               "     --locale=LOCALE              Set primary locale (LANG=)\n"
1208
               "     --locale-messages=LOCALE     Set message locale (LC_MESSAGES=)\n"
1209
               "     --keymap=KEYMAP              Set keymap\n"
1210
               "     --timezone=TIMEZONE          Set timezone\n"
1211
               "     --hostname=NAME              Set hostname\n"
1212
               "     --setup-machine-id           Set a random machine ID\n"
1213
               "     --machine-id=ID              Set specified machine ID\n"
1214
               "     --root-password=PASSWORD     Set root password from plaintext password\n"
1215
               "     --root-password-file=FILE    Set root password from file\n"
1216
               "     --root-password-hashed=HASH  Set root password from hashed password\n"
1217
               "     --root-shell=SHELL           Set root shell\n"
1218
               "     --kernel-command-line=CMDLINE\n"
1219
               "                                  Set kernel command line\n"
1220
               "     --prompt-locale              Prompt the user for locale settings\n"
1221
               "     --prompt-keymap              Prompt the user for keymap settings\n"
1222
               "     --prompt-timezone            Prompt the user for timezone\n"
1223
               "     --prompt-hostname            Prompt the user for hostname\n"
1224
               "     --prompt-root-password       Prompt the user for root password\n"
1225
               "     --prompt-root-shell          Prompt the user for root shell\n"
1226
               "     --prompt                     Prompt for all of the above\n"
1227
               "     --copy-locale                Copy locale from host\n"
1228
               "     --copy-keymap                Copy keymap from host\n"
1229
               "     --copy-timezone              Copy timezone from host\n"
1230
               "     --copy-root-password         Copy root password from host\n"
1231
               "     --copy-root-shell            Copy root shell from host\n"
1232
               "     --copy                       Copy locale, keymap, timezone, root password\n"
1233
               "     --force                      Overwrite existing files\n"
1234
               "     --delete-root-password       Delete root password\n"
1235
               "     --welcome=no                 Disable the welcome text\n"
1236
               "     --reset                      Remove existing files\n"
1237
               "\nSee the %s for details.\n",
1238
               program_invocation_short_name,
1239
               link);
1240

1241
        return 0;
1242
}
1243

1244
static int parse_argv(int argc, char *argv[]) {
136✔
1245

1246
        enum {
136✔
1247
                ARG_VERSION = 0x100,
1248
                ARG_ROOT,
1249
                ARG_IMAGE,
1250
                ARG_IMAGE_POLICY,
1251
                ARG_LOCALE,
1252
                ARG_LOCALE_MESSAGES,
1253
                ARG_KEYMAP,
1254
                ARG_TIMEZONE,
1255
                ARG_HOSTNAME,
1256
                ARG_SETUP_MACHINE_ID,
1257
                ARG_MACHINE_ID,
1258
                ARG_ROOT_PASSWORD,
1259
                ARG_ROOT_PASSWORD_FILE,
1260
                ARG_ROOT_PASSWORD_HASHED,
1261
                ARG_ROOT_SHELL,
1262
                ARG_KERNEL_COMMAND_LINE,
1263
                ARG_PROMPT,
1264
                ARG_PROMPT_LOCALE,
1265
                ARG_PROMPT_KEYMAP,
1266
                ARG_PROMPT_TIMEZONE,
1267
                ARG_PROMPT_HOSTNAME,
1268
                ARG_PROMPT_ROOT_PASSWORD,
1269
                ARG_PROMPT_ROOT_SHELL,
1270
                ARG_COPY,
1271
                ARG_COPY_LOCALE,
1272
                ARG_COPY_KEYMAP,
1273
                ARG_COPY_TIMEZONE,
1274
                ARG_COPY_ROOT_PASSWORD,
1275
                ARG_COPY_ROOT_SHELL,
1276
                ARG_FORCE,
1277
                ARG_DELETE_ROOT_PASSWORD,
1278
                ARG_WELCOME,
1279
                ARG_RESET,
1280
        };
1281

1282
        static const struct option options[] = {
136✔
1283
                { "help",                    no_argument,       NULL, 'h'                         },
1284
                { "version",                 no_argument,       NULL, ARG_VERSION                 },
1285
                { "root",                    required_argument, NULL, ARG_ROOT                    },
1286
                { "image",                   required_argument, NULL, ARG_IMAGE                   },
1287
                { "image-policy",            required_argument, NULL, ARG_IMAGE_POLICY            },
1288
                { "locale",                  required_argument, NULL, ARG_LOCALE                  },
1289
                { "locale-messages",         required_argument, NULL, ARG_LOCALE_MESSAGES         },
1290
                { "keymap",                  required_argument, NULL, ARG_KEYMAP                  },
1291
                { "timezone",                required_argument, NULL, ARG_TIMEZONE                },
1292
                { "hostname",                required_argument, NULL, ARG_HOSTNAME                },
1293
                { "setup-machine-id",        no_argument,       NULL, ARG_SETUP_MACHINE_ID        },
1294
                { "machine-id",              required_argument, NULL, ARG_MACHINE_ID              },
1295
                { "root-password",           required_argument, NULL, ARG_ROOT_PASSWORD           },
1296
                { "root-password-file",      required_argument, NULL, ARG_ROOT_PASSWORD_FILE      },
1297
                { "root-password-hashed",    required_argument, NULL, ARG_ROOT_PASSWORD_HASHED    },
1298
                { "root-shell",              required_argument, NULL, ARG_ROOT_SHELL              },
1299
                { "kernel-command-line",     required_argument, NULL, ARG_KERNEL_COMMAND_LINE     },
1300
                { "prompt",                  no_argument,       NULL, ARG_PROMPT                  },
1301
                { "prompt-locale",           no_argument,       NULL, ARG_PROMPT_LOCALE           },
1302
                { "prompt-keymap",           no_argument,       NULL, ARG_PROMPT_KEYMAP           },
1303
                { "prompt-timezone",         no_argument,       NULL, ARG_PROMPT_TIMEZONE         },
1304
                { "prompt-hostname",         no_argument,       NULL, ARG_PROMPT_HOSTNAME         },
1305
                { "prompt-root-password",    no_argument,       NULL, ARG_PROMPT_ROOT_PASSWORD    },
1306
                { "prompt-root-shell",       no_argument,       NULL, ARG_PROMPT_ROOT_SHELL       },
1307
                { "copy",                    no_argument,       NULL, ARG_COPY                    },
1308
                { "copy-locale",             no_argument,       NULL, ARG_COPY_LOCALE             },
1309
                { "copy-keymap",             no_argument,       NULL, ARG_COPY_KEYMAP             },
1310
                { "copy-timezone",           no_argument,       NULL, ARG_COPY_TIMEZONE           },
1311
                { "copy-root-password",      no_argument,       NULL, ARG_COPY_ROOT_PASSWORD      },
1312
                { "copy-root-shell",         no_argument,       NULL, ARG_COPY_ROOT_SHELL         },
1313
                { "force",                   no_argument,       NULL, ARG_FORCE                   },
1314
                { "delete-root-password",    no_argument,       NULL, ARG_DELETE_ROOT_PASSWORD    },
1315
                { "welcome",                 required_argument, NULL, ARG_WELCOME                 },
1316
                { "reset",                   no_argument,       NULL, ARG_RESET                   },
1317
                {}
1318
        };
1319

1320
        int r, c;
136✔
1321

1322
        assert(argc >= 0);
136✔
1323
        assert(argv);
136✔
1324

1325
        while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
637✔
1326

1327
                switch (c) {
503✔
1328

1329
                case 'h':
×
1330
                        return help();
×
1331

UNCOV
1332
                case ARG_VERSION:
×
UNCOV
1333
                        return version();
×
1334

1335
                case ARG_ROOT:
35✔
1336
                        r = parse_path_argument(optarg, true, &arg_root);
35✔
1337
                        if (r < 0)
35✔
1338
                                return r;
1339
                        break;
1340

UNCOV
1341
                case ARG_IMAGE:
×
UNCOV
1342
                        r = parse_path_argument(optarg, false, &arg_image);
×
UNCOV
1343
                        if (r < 0)
×
1344
                                return r;
1345
                        break;
1346

UNCOV
1347
                case ARG_IMAGE_POLICY:
×
UNCOV
1348
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
UNCOV
1349
                        if (r < 0)
×
1350
                                return r;
1351
                        break;
1352

1353
                case ARG_LOCALE:
4✔
1354
                        r = free_and_strdup(&arg_locale, optarg);
4✔
1355
                        if (r < 0)
4✔
UNCOV
1356
                                return log_oom();
×
1357

1358
                        break;
1359

1360
                case ARG_LOCALE_MESSAGES:
4✔
1361
                        r = free_and_strdup(&arg_locale_messages, optarg);
4✔
1362
                        if (r < 0)
4✔
UNCOV
1363
                                return log_oom();
×
1364

1365
                        break;
1366

1367
                case ARG_KEYMAP:
3✔
1368
                        if (!keymap_is_valid(optarg))
3✔
UNCOV
1369
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1370
                                                       "Keymap %s is not valid.", optarg);
1371

1372
                        r = free_and_strdup(&arg_keymap, optarg);
3✔
1373
                        if (r < 0)
3✔
UNCOV
1374
                                return log_oom();
×
1375

1376
                        break;
1377

1378
                case ARG_TIMEZONE:
4✔
1379
                        if (!timezone_is_valid(optarg, LOG_ERR))
4✔
1380
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1✔
1381
                                                       "Timezone %s is not valid.", optarg);
1382

1383
                        r = free_and_strdup(&arg_timezone, optarg);
3✔
1384
                        if (r < 0)
3✔
UNCOV
1385
                                return log_oom();
×
1386

1387
                        break;
1388

1389
                case ARG_ROOT_PASSWORD:
4✔
1390
                        r = free_and_strdup(&arg_root_password, optarg);
4✔
1391
                        if (r < 0)
4✔
UNCOV
1392
                                return log_oom();
×
1393

1394
                        arg_root_password_is_hashed = false;
4✔
1395
                        break;
4✔
1396

1397
                case ARG_ROOT_PASSWORD_FILE:
1✔
1398
                        arg_root_password = mfree(arg_root_password);
1✔
1399

1400
                        r = read_one_line_file(optarg, &arg_root_password);
1✔
1401
                        if (r < 0)
1✔
UNCOV
1402
                                return log_error_errno(r, "Failed to read %s: %m", optarg);
×
1403

1404
                        arg_root_password_is_hashed = false;
1✔
1405
                        break;
1✔
1406

1407
                case ARG_ROOT_PASSWORD_HASHED:
4✔
1408
                        r = free_and_strdup(&arg_root_password, optarg);
4✔
1409
                        if (r < 0)
4✔
UNCOV
1410
                                return log_oom();
×
1411

1412
                        arg_root_password_is_hashed = true;
4✔
1413
                        break;
4✔
1414

1415
                case ARG_ROOT_SHELL:
5✔
1416
                        r = free_and_strdup(&arg_root_shell, optarg);
5✔
1417
                        if (r < 0)
5✔
UNCOV
1418
                                return log_oom();
×
1419

1420
                        break;
1421

1422
                case ARG_HOSTNAME:
3✔
1423
                        if (!hostname_is_valid(optarg, VALID_HOSTNAME_TRAILING_DOT))
3✔
UNCOV
1424
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1425
                                                       "Host name %s is not valid.", optarg);
1426

1427
                        r = free_and_strdup(&arg_hostname, optarg);
3✔
1428
                        if (r < 0)
3✔
UNCOV
1429
                                return log_oom();
×
1430

1431
                        hostname_cleanup(arg_hostname);
3✔
1432
                        break;
3✔
1433

1434
                case ARG_SETUP_MACHINE_ID:
1✔
1435
                        r = sd_id128_randomize(&arg_machine_id);
1✔
1436
                        if (r < 0)
1✔
UNCOV
1437
                                return log_error_errno(r, "Failed to generate randomized machine ID: %m");
×
1438

1439
                        break;
1440

1441
                case ARG_MACHINE_ID:
4✔
1442
                        r = sd_id128_from_string(optarg, &arg_machine_id);
4✔
1443
                        if (r < 0)
4✔
1444
                                return log_error_errno(r, "Failed to parse machine id %s.", optarg);
1✔
1445

1446
                        break;
1447

1448
                case ARG_KERNEL_COMMAND_LINE:
3✔
1449
                        r = free_and_strdup(&arg_kernel_cmdline, optarg);
3✔
1450
                        if (r < 0)
3✔
UNCOV
1451
                                return log_oom();
×
1452

1453
                        break;
1454

UNCOV
1455
                case ARG_PROMPT:
×
UNCOV
1456
                        arg_prompt_locale = arg_prompt_keymap = arg_prompt_timezone = arg_prompt_hostname =
×
UNCOV
1457
                                arg_prompt_root_password = arg_prompt_root_shell = true;
×
UNCOV
1458
                        break;
×
1459

1460
                case ARG_PROMPT_LOCALE:
103✔
1461
                        arg_prompt_locale = true;
103✔
1462
                        break;
103✔
1463

1464
                case ARG_PROMPT_KEYMAP:
103✔
1465
                        arg_prompt_keymap = true;
103✔
1466
                        break;
103✔
1467

1468
                case ARG_PROMPT_TIMEZONE:
103✔
1469
                        arg_prompt_timezone = true;
103✔
1470
                        break;
103✔
1471

1472
                case ARG_PROMPT_HOSTNAME:
1✔
1473
                        arg_prompt_hostname = true;
1✔
1474
                        break;
1✔
1475

1476
                case ARG_PROMPT_ROOT_PASSWORD:
103✔
1477
                        arg_prompt_root_password = true;
103✔
1478
                        break;
103✔
1479

1480
                case ARG_PROMPT_ROOT_SHELL:
4✔
1481
                        arg_prompt_root_shell = true;
4✔
1482
                        break;
4✔
1483

1484
                case ARG_COPY:
1✔
1485
                        arg_copy_locale = arg_copy_keymap = arg_copy_timezone = arg_copy_root_password =
1✔
1486
                                arg_copy_root_shell = true;
1✔
1487
                        break;
1✔
1488

1489
                case ARG_COPY_LOCALE:
1✔
1490
                        arg_copy_locale = true;
1✔
1491
                        break;
1✔
1492

1493
                case ARG_COPY_KEYMAP:
1✔
1494
                        arg_copy_keymap = true;
1✔
1495
                        break;
1✔
1496

1497
                case ARG_COPY_TIMEZONE:
1✔
1498
                        arg_copy_timezone = true;
1✔
1499
                        break;
1✔
1500

1501
                case ARG_COPY_ROOT_PASSWORD:
1✔
1502
                        arg_copy_root_password = true;
1✔
1503
                        break;
1✔
1504

1505
                case ARG_COPY_ROOT_SHELL:
1✔
1506
                        arg_copy_root_shell = true;
1✔
1507
                        break;
1✔
1508

1509
                case ARG_FORCE:
2✔
1510
                        arg_force = true;
2✔
1511
                        break;
2✔
1512

1513
                case ARG_DELETE_ROOT_PASSWORD:
1✔
1514
                        arg_delete_root_password = true;
1✔
1515
                        break;
1✔
1516

1517
                case ARG_WELCOME:
1✔
1518
                        r = parse_boolean(optarg);
1✔
1519
                        if (r < 0)
1✔
1520
                                return log_error_errno(r, "Failed to parse --welcome= argument: %s", optarg);
×
1521

1522
                        arg_welcome = r;
1✔
1523
                        break;
1✔
1524

1525
                case ARG_RESET:
1✔
1526
                        arg_reset = true;
1✔
1527
                        break;
1✔
1528

1529
                case '?':
1530
                        return -EINVAL;
1531

UNCOV
1532
                default:
×
1533
                        assert_not_reached();
×
1534
                }
1535

1536
        if (arg_delete_root_password && (arg_copy_root_password || arg_root_password || arg_prompt_root_password))
134✔
UNCOV
1537
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1538
                                       "--delete-root-password cannot be combined with other root password options.");
1539

1540
        if (arg_image && arg_root)
134✔
UNCOV
1541
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1542
                                       "--root= and --image= cannot be used together.");
1543

1544
        if (!sd_id128_is_null(arg_machine_id) && !(arg_image || arg_root) && !arg_force)
264✔
1545
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1546
                                       "--machine-id=/--setup-machine-id only works with --root= or --image=.");
1547

1548
        return 1;
1549
}
1550

1551
static int reload_system_manager(sd_bus **bus) {
×
UNCOV
1552
        int r;
×
1553

1554
        assert(bus);
×
1555

UNCOV
1556
        if (!*bus) {
×
UNCOV
1557
                r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL, RUNTIME_SCOPE_SYSTEM, bus);
×
1558
                if (r < 0)
×
1559
                        return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL, RUNTIME_SCOPE_SYSTEM);
×
1560
        }
1561

1562
        r = bus_service_manager_reload(*bus);
×
1563
        if (r < 0)
×
1564
                return r;
1565

UNCOV
1566
        log_info("Requested manager reload to apply locale configuration.");
×
1567
        return 0;
1568
}
1569

1570
static int reload_vconsole(sd_bus **bus) {
×
UNCOV
1571
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
UNCOV
1572
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
×
1573
        _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
×
1574
        const char *object;
×
1575
        int r;
×
1576

1577
        assert(bus);
×
1578

1579
        if (!*bus) {
×
1580
                r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL, RUNTIME_SCOPE_SYSTEM, bus);
×
UNCOV
1581
                if (r < 0)
×
1582
                        return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL, RUNTIME_SCOPE_SYSTEM);
×
1583
        }
1584

UNCOV
1585
        r = bus_wait_for_jobs_new(*bus, &w);
×
1586
        if (r < 0)
×
1587
                return log_error_errno(r, "Could not watch jobs: %m");
×
1588

UNCOV
1589
        r = bus_call_method(*bus, bus_systemd_mgr, "RestartUnit", &error, &reply,
×
1590
                            "ss", "systemd-vconsole-setup.service", "replace");
UNCOV
1591
        if (r < 0)
×
UNCOV
1592
                return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
×
1593

UNCOV
1594
        r = sd_bus_message_read(reply, "o", &object);
×
1595
        if (r < 0)
×
UNCOV
1596
                return bus_log_parse_error(r);
×
1597

UNCOV
1598
        r = bus_wait_for_jobs_one(w, object, BUS_WAIT_JOBS_LOG_ERROR, NULL);
×
UNCOV
1599
        if (r < 0)
×
UNCOV
1600
                return log_error_errno(r, "Failed to wait for systemd-vconsole-setup.service/restart: %m");
×
1601
        return 0;
1602
}
1603

1604
static int run(int argc, char *argv[]) {
136✔
1605
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
136✔
1606
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
136✔
UNCOV
1607
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
×
1608
        _cleanup_close_ int rfd = -EBADF;
136✔
1609
        int r;
136✔
1610

1611
        r = parse_argv(argc, argv);
136✔
1612
        if (r <= 0)
136✔
1613
                return r;
1614

1615
        log_setup();
134✔
1616

1617
        umask(0022);
134✔
1618

1619
        bool offline = arg_root || arg_image;
134✔
1620

1621
        if (!offline) {
101✔
1622
                /* If we are called without --root=/--image= let's honour the systemd.firstboot kernel
1623
                 * command line option, because we are called to provision the host with basic settings (as
1624
                 * opposed to some other file system tree/image) */
1625

1626
                bool enabled;
101✔
1627
                r = proc_cmdline_get_bool("systemd.firstboot", /* flags = */ 0, &enabled);
101✔
1628
                if (r < 0)
101✔
UNCOV
1629
                        return log_error_errno(r, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
×
1630
                if (r > 0 && !enabled) {
101✔
1631
                        log_debug("Found systemd.firstboot=no kernel command line argument, turning off all prompts.");
101✔
1632
                        arg_prompt_locale = arg_prompt_keymap = arg_prompt_timezone = arg_prompt_hostname = arg_prompt_root_password = arg_prompt_root_shell = false;
101✔
1633
                }
1634
        }
1635

1636
        if (arg_image) {
134✔
UNCOV
1637
                assert(!arg_root);
×
1638

UNCOV
1639
                r = mount_image_privately_interactively(
×
1640
                                arg_image,
1641
                                arg_image_policy,
1642
                                DISSECT_IMAGE_GENERIC_ROOT |
1643
                                DISSECT_IMAGE_REQUIRE_ROOT |
1644
                                DISSECT_IMAGE_VALIDATE_OS |
1645
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
1646
                                DISSECT_IMAGE_FSCK |
1647
                                DISSECT_IMAGE_GROWFS |
1648
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
1649
                                &mounted_dir,
1650
                                &rfd,
1651
                                &loop_device);
UNCOV
1652
                if (r < 0)
×
1653
                        return r;
1654

UNCOV
1655
                arg_root = strdup(mounted_dir);
×
UNCOV
1656
                if (!arg_root)
×
UNCOV
1657
                        return log_oom();
×
1658
        } else {
1659
                rfd = open(empty_to_root(arg_root), O_DIRECTORY|O_CLOEXEC);
167✔
1660
                if (rfd < 0)
134✔
UNCOV
1661
                        return log_error_errno(errno, "Failed to open %s: %m", empty_to_root(arg_root));
×
1662
        }
1663

1664
        LOG_SET_PREFIX(arg_image ?: arg_root);
270✔
1665

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

1669
        if (arg_keymap && !keymap_is_ok(rfd, arg_keymap))
134✔
UNCOV
1670
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Keymap %s is not installed.", arg_keymap);
×
1671
        if (arg_locale && !locale_is_ok(rfd, arg_locale))
134✔
UNCOV
1672
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale);
×
1673
        if (arg_locale_messages && !locale_is_ok(rfd, arg_locale_messages))
134✔
UNCOV
1674
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale_messages);
×
1675

1676
        if (arg_root_shell) {
134✔
1677
                r = find_shell(rfd, arg_root_shell);
5✔
1678
                if (r < 0)
5✔
1679
                        return r;
1680
        }
1681

1682
        r = process_reset(rfd);
133✔
1683
        if (r < 0)
133✔
1684
                return r;
1685

1686
        r = process_locale(rfd);
133✔
1687
        if (r < 0)
133✔
1688
                return r;
1689
        if (r > 0 && !offline)
133✔
UNCOV
1690
                (void) reload_system_manager(&bus);
×
1691

1692
        r = process_keymap(rfd);
133✔
1693
        if (r < 0)
133✔
1694
                return r;
1695
        if (r > 0 && !offline)
133✔
UNCOV
1696
                (void) reload_vconsole(&bus);
×
1697

1698
        r = process_timezone(rfd);
133✔
1699
        if (r < 0)
133✔
1700
                return r;
1701

1702
        r = process_hostname(rfd);
133✔
1703
        if (r < 0)
133✔
1704
                return r;
1705

1706
        r = process_root_account(rfd);
133✔
1707
        if (r < 0)
133✔
1708
                return r;
1709

1710
        r = process_kernel_cmdline(rfd);
133✔
1711
        if (r < 0)
133✔
1712
                return r;
1713

1714
        r = process_machine_id(rfd);
133✔
1715
        if (r < 0)
133✔
UNCOV
1716
                return r;
×
1717

1718
        return 0;
1719
}
1720

1721
DEFINE_MAIN_FUNCTION(run);
272✔
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