]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/firstboot/firstboot.c
network: radv: Send RA on newly-added dynamic prefix
[thirdparty/systemd.git] / src / firstboot / firstboot.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
418b9be5 2
418b9be5 3#include <fcntl.h>
418b9be5 4#include <getopt.h>
3ff9fa59 5#include <linux/loop.h>
3f6fd1ba 6#include <unistd.h>
418b9be5 7
dccca82b
LP
8#include "sd-id128.h"
9
b5efdb8a 10#include "alloc-util.h"
3f6fd1ba 11#include "ask-password-api.h"
418b9be5 12#include "copy.h"
3ff9fa59 13#include "dissect-image.h"
686d13b9 14#include "env-file.h"
6bedfcbb 15#include "fd-util.h"
3f6fd1ba 16#include "fileio.h"
f4f15635 17#include "fs-util.h"
3f6fd1ba 18#include "hostname-util.h"
f05e1d0d 19#include "kbd-util.h"
42f3b2f9 20#include "libcrypt-util.h"
3f6fd1ba 21#include "locale-util.h"
3ff9fa59 22#include "loop-util.h"
45f39418 23#include "main-func.h"
9ae4ef49 24#include "memory-util.h"
418b9be5 25#include "mkdir.h"
3ff9fa59
LP
26#include "mount-util.h"
27#include "namespace-util.h"
d58ad743 28#include "os-util.h"
6bedfcbb 29#include "parse-util.h"
418b9be5 30#include "path-util.h"
294bf0c3 31#include "pretty-print.h"
f582cbca 32#include "proc-cmdline.h"
3df3e884 33#include "random-util.h"
6bedfcbb 34#include "string-util.h"
3f6fd1ba 35#include "strv.h"
288a74cc 36#include "terminal-util.h"
3f6fd1ba 37#include "time-util.h"
b4909a3f 38#include "tmpfile-util-label.h"
3ff9fa59 39#include "tmpfile-util.h"
affb60b1 40#include "umask-util.h"
e929bee0 41#include "user-util.h"
418b9be5
LP
42
43static char *arg_root = NULL;
3ff9fa59 44static char *arg_image = NULL;
418b9be5 45static char *arg_locale = NULL; /* $LANG */
ed457f13 46static char *arg_keymap = NULL;
418b9be5
LP
47static char *arg_locale_messages = NULL; /* $LC_MESSAGES */
48static char *arg_timezone = NULL;
49static char *arg_hostname = NULL;
50static sd_id128_t arg_machine_id = {};
51static char *arg_root_password = NULL;
28900a1b 52static char *arg_root_shell = NULL;
a5925354 53static char *arg_kernel_cmdline = NULL;
418b9be5 54static bool arg_prompt_locale = false;
ed457f13 55static bool arg_prompt_keymap = false;
418b9be5
LP
56static bool arg_prompt_timezone = false;
57static bool arg_prompt_hostname = false;
58static bool arg_prompt_root_password = false;
28900a1b 59static bool arg_prompt_root_shell = false;
418b9be5 60static bool arg_copy_locale = false;
ed457f13 61static bool arg_copy_keymap = false;
418b9be5
LP
62static bool arg_copy_timezone = false;
63static bool arg_copy_root_password = false;
28900a1b 64static bool arg_copy_root_shell = false;
b4909a3f 65static bool arg_force = false;
4926ceaf 66static bool arg_delete_root_password = false;
676339a1 67static bool arg_root_password_is_hashed = false;
a1225020 68static bool arg_welcome = true;
418b9be5 69
45f39418 70STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
3ff9fa59 71STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
45f39418
YW
72STATIC_DESTRUCTOR_REGISTER(arg_locale, freep);
73STATIC_DESTRUCTOR_REGISTER(arg_locale_messages, freep);
74STATIC_DESTRUCTOR_REGISTER(arg_keymap, freep);
75STATIC_DESTRUCTOR_REGISTER(arg_timezone, freep);
76STATIC_DESTRUCTOR_REGISTER(arg_hostname, freep);
9ae4ef49 77STATIC_DESTRUCTOR_REGISTER(arg_root_password, erase_and_freep);
45f39418 78
418b9be5
LP
79static bool press_any_key(void) {
80 char k = 0;
81 bool need_nl = true;
82
83 printf("-- Press any key to proceed --");
84 fflush(stdout);
85
94956f8f 86 (void) read_one_char(stdin, &k, USEC_INFINITY, &need_nl);
418b9be5
LP
87
88 if (need_nl)
89 putchar('\n');
90
91 return k != 'q';
92}
93
94static void print_welcome(void) {
4aeadec7 95 _cleanup_free_ char *pretty_name = NULL, *ansi_color = NULL;
418b9be5 96 static bool done = false;
4aeadec7 97 const char *pn;
418b9be5
LP
98 int r;
99
a1225020
LP
100 if (!arg_welcome)
101 return;
102
418b9be5
LP
103 if (done)
104 return;
105
4aeadec7
LP
106 r = parse_os_release(
107 arg_root,
108 "PRETTY_NAME", &pretty_name,
109 "ANSI_COLOR", &ansi_color,
110 NULL);
d58ad743
LP
111 if (r < 0)
112 log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
113 "Failed to read os-release file, ignoring: %m");
418b9be5 114
4aeadec7
LP
115 pn = isempty(pretty_name) ? "Linux" : pretty_name;
116
117 if (colors_enabled())
118 printf("\nWelcome to your new installation of \x1B[%sm%s\x1B[0m!\n", ansi_color, pn);
119 else
120 printf("\nWelcome to your new installation of %s!\n", pn);
121
122 printf("\nPlease configure your system!\n\n");
418b9be5
LP
123
124 press_any_key();
125
126 done = true;
127}
128
129static int show_menu(char **x, unsigned n_columns, unsigned width, unsigned percentage) {
418b9be5 130 unsigned break_lines, break_modulo;
da6053d0 131 size_t n, per_column, i, j;
418b9be5
LP
132
133 assert(n_columns > 0);
134
135 n = strv_length(x);
be6b0c21 136 per_column = DIV_ROUND_UP(n, n_columns);
418b9be5
LP
137
138 break_lines = lines();
139 if (break_lines > 2)
140 break_lines--;
141
142 /* The first page gets two extra lines, since we want to show
143 * a title */
144 break_modulo = break_lines;
145 if (break_modulo > 3)
146 break_modulo -= 3;
147
148 for (i = 0; i < per_column; i++) {
149
150 for (j = 0; j < n_columns; j ++) {
151 _cleanup_free_ char *e = NULL;
152
153 if (j * per_column + i >= n)
154 break;
155
156 e = ellipsize(x[j * per_column + i], width, percentage);
157 if (!e)
158 return log_oom();
159
da6053d0 160 printf("%4zu) %-*s", j * per_column + i + 1, width, e);
418b9be5
LP
161 }
162
163 putchar('\n');
164
165 /* on the first screen we reserve 2 extra lines for the title */
166 if (i % break_lines == break_modulo) {
167 if (!press_any_key())
168 return 0;
169 }
170 }
171
172 return 0;
173}
174
ecada8f2 175static int prompt_loop(const char *text, char **l, unsigned percentage, bool (*is_valid)(const char *name), char **ret) {
418b9be5
LP
176 int r;
177
178 assert(text);
179 assert(is_valid);
180 assert(ret);
181
182 for (;;) {
183 _cleanup_free_ char *p = NULL;
184 unsigned u;
185
ecada8f2
ZJS
186 r = ask_string(&p, "%s %s (empty to skip, \"list\" to list options): ",
187 special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), text);
23bbb0de
MS
188 if (r < 0)
189 return log_error_errno(r, "Failed to query user: %m");
418b9be5
LP
190
191 if (isempty(p)) {
192 log_warning("No data entered, skipping.");
193 return 0;
194 }
195
ecada8f2
ZJS
196 if (streq(p, "list")) {
197 r = show_menu(l, 3, 22, percentage);
198 if (r < 0)
199 return r;
200
201 putchar('\n');
202 continue;
203 };
204
418b9be5
LP
205 r = safe_atou(p, &u);
206 if (r >= 0) {
418b9be5
LP
207 if (u <= 0 || u > strv_length(l)) {
208 log_error("Specified entry number out of range.");
209 continue;
210 }
211
212 log_info("Selected '%s'.", l[u-1]);
bfbf5f74 213 if (free_and_strdup(ret, l[u-1]) < 0)
418b9be5
LP
214 return log_oom();
215
418b9be5
LP
216 return 0;
217 }
218
219 if (!is_valid(p)) {
220 log_error("Entered data invalid.");
221 continue;
222 }
223
bfbf5f74 224 return free_and_replace(*ret, p);
418b9be5
LP
225 }
226}
227
a00a78b8
LP
228static bool locale_is_ok(const char *name) {
229
230 if (arg_root)
231 return locale_is_valid(name);
232
233 return locale_is_installed(name) > 0;
234}
235
418b9be5
LP
236static int prompt_locale(void) {
237 _cleanup_strv_free_ char **locales = NULL;
238 int r;
239
240 if (arg_locale || arg_locale_messages)
241 return 0;
242
243 if (!arg_prompt_locale)
244 return 0;
245
246 r = get_locales(&locales);
23bbb0de
MS
247 if (r < 0)
248 return log_error_errno(r, "Cannot query locales list: %m");
418b9be5 249
bdd7cd26
LP
250 if (strv_isempty(locales))
251 log_debug("No locales found, skipping locale selection.");
252 else if (strv_length(locales) == 1) {
418b9be5 253
bdd7cd26
LP
254 if (streq(locales[0], SYSTEMD_DEFAULT_LOCALE))
255 log_debug("Only installed locale is default locale anyway, not setting locale explicitly.");
256 else {
257 log_debug("Only a single locale available (%s), selecting it as default.", locales[0]);
418b9be5 258
bdd7cd26
LP
259 arg_locale = strdup(locales[0]);
260 if (!arg_locale)
261 return log_oom();
418b9be5 262
bdd7cd26
LP
263 /* Not setting arg_locale_message here, since it defaults to LANG anyway */
264 }
265 } else {
266 print_welcome();
418b9be5 267
ecada8f2 268 r = prompt_loop("Please enter system locale name or number",
a00a78b8 269 locales, 60, locale_is_ok, &arg_locale);
bdd7cd26
LP
270 if (r < 0)
271 return r;
272
273 if (isempty(arg_locale))
274 return 0;
275
ecada8f2 276 r = prompt_loop("Please enter system message locale name or number",
a00a78b8 277 locales, 60, locale_is_ok, &arg_locale_messages);
bdd7cd26
LP
278 if (r < 0)
279 return r;
280
281 /* Suppress the messages setting if it's the same as the main locale anyway */
282 if (streq_ptr(arg_locale, arg_locale_messages))
283 arg_locale_messages = mfree(arg_locale_messages);
284 }
418b9be5
LP
285
286 return 0;
287}
288
289static int process_locale(void) {
290 const char *etc_localeconf;
291 char* locales[3];
292 unsigned i = 0;
293 int r;
294
1d13f648 295 etc_localeconf = prefix_roota(arg_root, "/etc/locale.conf");
b4909a3f 296 if (laccess(etc_localeconf, F_OK) >= 0 && !arg_force)
418b9be5
LP
297 return 0;
298
299 if (arg_copy_locale && arg_root) {
300
22e596d6 301 (void) mkdir_parents(etc_localeconf, 0755);
8a016c74 302 r = copy_file("/etc/locale.conf", etc_localeconf, 0, 0644, 0, 0, COPY_REFLINK);
418b9be5 303 if (r != -ENOENT) {
23bbb0de
MS
304 if (r < 0)
305 return log_error_errno(r, "Failed to copy %s: %m", etc_localeconf);
418b9be5
LP
306
307 log_info("%s copied.", etc_localeconf);
308 return 0;
309 }
310 }
311
312 r = prompt_locale();
313 if (r < 0)
314 return r;
315
316 if (!isempty(arg_locale))
63c372cb 317 locales[i++] = strjoina("LANG=", arg_locale);
418b9be5 318 if (!isempty(arg_locale_messages) && !streq(arg_locale_messages, arg_locale))
63c372cb 319 locales[i++] = strjoina("LC_MESSAGES=", arg_locale_messages);
418b9be5
LP
320
321 if (i == 0)
322 return 0;
323
324 locales[i] = NULL;
325
22e596d6 326 (void) mkdir_parents(etc_localeconf, 0755);
418b9be5 327 r = write_env_file(etc_localeconf, locales);
23bbb0de
MS
328 if (r < 0)
329 return log_error_errno(r, "Failed to write %s: %m", etc_localeconf);
418b9be5
LP
330
331 log_info("%s written.", etc_localeconf);
332 return 0;
333}
334
ed457f13
TB
335static int prompt_keymap(void) {
336 _cleanup_strv_free_ char **kmaps = NULL;
337 int r;
338
339 if (arg_keymap)
340 return 0;
341
342 if (!arg_prompt_keymap)
343 return 0;
344
345 r = get_keymaps(&kmaps);
346 if (r == -ENOENT) /* no keymaps installed */
347 return r;
348 if (r < 0)
349 return log_error_errno(r, "Failed to read keymaps: %m");
350
351 print_welcome();
352
8700b4da 353 return prompt_loop("Please enter system keymap name or number",
ecada8f2 354 kmaps, 60, keymap_is_valid, &arg_keymap);
ed457f13
TB
355}
356
357static int process_keymap(void) {
358 const char *etc_vconsoleconf;
359 char **keymap;
360 int r;
361
362 etc_vconsoleconf = prefix_roota(arg_root, "/etc/vconsole.conf");
b4909a3f 363 if (laccess(etc_vconsoleconf, F_OK) >= 0 && !arg_force)
ed457f13
TB
364 return 0;
365
366 if (arg_copy_keymap && arg_root) {
367
22e596d6 368 (void) mkdir_parents(etc_vconsoleconf, 0755);
8a016c74 369 r = copy_file("/etc/vconsole.conf", etc_vconsoleconf, 0, 0644, 0, 0, COPY_REFLINK);
ed457f13
TB
370 if (r != -ENOENT) {
371 if (r < 0)
372 return log_error_errno(r, "Failed to copy %s: %m", etc_vconsoleconf);
373
374 log_info("%s copied.", etc_vconsoleconf);
375 return 0;
376 }
377 }
378
379 r = prompt_keymap();
380 if (r == -ENOENT)
381 return 0; /* don't fail if no keymaps are installed */
382 if (r < 0)
383 return r;
384
a7353b4d 385 if (isempty(arg_keymap))
ed457f13
TB
386 return 0;
387
a7353b4d
YW
388 keymap = STRV_MAKE(strjoina("KEYMAP=", arg_keymap));
389
390 r = mkdir_parents(etc_vconsoleconf, 0755);
391 if (r < 0)
392 return log_error_errno(r, "Failed to create the parent directory of %s: %m", etc_vconsoleconf);
393
ed457f13
TB
394 r = write_env_file(etc_vconsoleconf, keymap);
395 if (r < 0)
396 return log_error_errno(r, "Failed to write %s: %m", etc_vconsoleconf);
397
398 log_info("%s written.", etc_vconsoleconf);
399 return 0;
400}
401
089fb865
MG
402static bool timezone_is_valid_log_error(const char *name) {
403 return timezone_is_valid(name, LOG_ERR);
404}
405
418b9be5
LP
406static int prompt_timezone(void) {
407 _cleanup_strv_free_ char **zones = NULL;
408 int r;
409
410 if (arg_timezone)
411 return 0;
412
413 if (!arg_prompt_timezone)
414 return 0;
415
416 r = get_timezones(&zones);
23bbb0de
MS
417 if (r < 0)
418 return log_error_errno(r, "Cannot query timezone list: %m");
418b9be5
LP
419
420 print_welcome();
421
ecada8f2
ZJS
422 r = prompt_loop("Please enter timezone name or number",
423 zones, 30, timezone_is_valid_log_error, &arg_timezone);
418b9be5
LP
424 if (r < 0)
425 return r;
426
427 return 0;
428}
429
430static int process_timezone(void) {
431 const char *etc_localtime, *e;
432 int r;
433
1d13f648 434 etc_localtime = prefix_roota(arg_root, "/etc/localtime");
b4909a3f 435 if (laccess(etc_localtime, F_OK) >= 0 && !arg_force)
418b9be5
LP
436 return 0;
437
438 if (arg_copy_timezone && arg_root) {
439 _cleanup_free_ char *p = NULL;
440
441 r = readlink_malloc("/etc/localtime", &p);
442 if (r != -ENOENT) {
23bbb0de
MS
443 if (r < 0)
444 return log_error_errno(r, "Failed to read host timezone: %m");
418b9be5 445
22e596d6 446 (void) mkdir_parents(etc_localtime, 0755);
4a62c710
MS
447 if (symlink(p, etc_localtime) < 0)
448 return log_error_errno(errno, "Failed to create %s symlink: %m", etc_localtime);
418b9be5
LP
449
450 log_info("%s copied.", etc_localtime);
451 return 0;
452 }
453 }
454
455 r = prompt_timezone();
456 if (r < 0)
457 return r;
458
459 if (isempty(arg_timezone))
460 return 0;
461
63c372cb 462 e = strjoina("../usr/share/zoneinfo/", arg_timezone);
418b9be5 463
22e596d6 464 (void) mkdir_parents(etc_localtime, 0755);
4a62c710
MS
465 if (symlink(e, etc_localtime) < 0)
466 return log_error_errno(errno, "Failed to create %s symlink: %m", etc_localtime);
418b9be5
LP
467
468 log_info("%s written", etc_localtime);
469 return 0;
470}
471
472static int prompt_hostname(void) {
473 int r;
474
475 if (arg_hostname)
476 return 0;
477
478 if (!arg_prompt_hostname)
479 return 0;
480
481 print_welcome();
482 putchar('\n');
483
484 for (;;) {
485 _cleanup_free_ char *h = NULL;
486
9a6f746f 487 r = ask_string(&h, "%s Please enter hostname for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET));
23bbb0de
MS
488 if (r < 0)
489 return log_error_errno(r, "Failed to query hostname: %m");
418b9be5
LP
490
491 if (isempty(h)) {
492 log_warning("No hostname entered, skipping.");
493 break;
494 }
495
34ad6090 496 if (!hostname_is_valid(h, true)) {
418b9be5
LP
497 log_error("Specified hostname invalid.");
498 continue;
499 }
500
34ad6090 501 /* Get rid of the trailing dot that we allow, but don't want to see */
ae691c1d 502 arg_hostname = hostname_cleanup(h);
418b9be5
LP
503 h = NULL;
504 break;
505 }
506
507 return 0;
508}
509
510static int process_hostname(void) {
511 const char *etc_hostname;
512 int r;
513
1d13f648 514 etc_hostname = prefix_roota(arg_root, "/etc/hostname");
b4909a3f 515 if (laccess(etc_hostname, F_OK) >= 0 && !arg_force)
418b9be5
LP
516 return 0;
517
518 r = prompt_hostname();
519 if (r < 0)
520 return r;
521
522 if (isempty(arg_hostname))
523 return 0;
524
0675e94a 525 r = write_string_file(etc_hostname, arg_hostname,
b4909a3f
DDM
526 WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_SYNC | WRITE_STRING_FILE_MKDIR_0755 |
527 (arg_force ? WRITE_STRING_FILE_ATOMIC : 0));
23bbb0de
MS
528 if (r < 0)
529 return log_error_errno(r, "Failed to write %s: %m", etc_hostname);
418b9be5
LP
530
531 log_info("%s written.", etc_hostname);
532 return 0;
533}
534
535static int process_machine_id(void) {
536 const char *etc_machine_id;
537 char id[SD_ID128_STRING_MAX];
538 int r;
539
1d13f648 540 etc_machine_id = prefix_roota(arg_root, "/etc/machine-id");
b4909a3f 541 if (laccess(etc_machine_id, F_OK) >= 0 && !arg_force)
418b9be5
LP
542 return 0;
543
3bbaff3e 544 if (sd_id128_is_null(arg_machine_id))
418b9be5
LP
545 return 0;
546
0675e94a 547 r = write_string_file(etc_machine_id, sd_id128_to_string(arg_machine_id, id),
b4909a3f
DDM
548 WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_SYNC | WRITE_STRING_FILE_MKDIR_0755 |
549 (arg_force ? WRITE_STRING_FILE_ATOMIC : 0));
23bbb0de
MS
550 if (r < 0)
551 return log_error_errno(r, "Failed to write machine id: %m");
418b9be5
LP
552
553 log_info("%s written.", etc_machine_id);
554 return 0;
555}
556
557static int prompt_root_password(void) {
1fbc95d3 558 const char *msg1, *msg2;
418b9be5
LP
559 int r;
560
561 if (arg_root_password)
562 return 0;
563
564 if (!arg_prompt_root_password)
565 return 0;
566
418b9be5
LP
567 print_welcome();
568 putchar('\n');
569
3619634c
LP
570 msg1 = strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), " Please enter a new root password (empty to skip):");
571 msg2 = strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), " Please enter new root password again:");
418b9be5
LP
572
573 for (;;) {
c7b7d74e 574 _cleanup_strv_free_erase_ char **a = NULL, **b = NULL;
418b9be5 575
daa55720 576 r = ask_password_tty(-1, msg1, NULL, 0, 0, NULL, &a);
23bbb0de
MS
577 if (r < 0)
578 return log_error_errno(r, "Failed to query root password: %m");
39e96f84
ZJS
579 if (strv_length(a) != 1)
580 return log_error_errno(SYNTHETIC_ERRNO(EIO),
581 "Received multiple passwords, where we expected one.");
418b9be5 582
c7b7d74e 583 if (isempty(*a)) {
418b9be5
LP
584 log_warning("No password entered, skipping.");
585 break;
586 }
587
daa55720 588 r = ask_password_tty(-1, msg2, NULL, 0, 0, NULL, &b);
ab84f5b9 589 if (r < 0)
00843602 590 return log_error_errno(r, "Failed to query root password: %m");
39e96f84
ZJS
591 if (strv_length(b) != 1)
592 return log_error_errno(SYNTHETIC_ERRNO(EIO),
593 "Received multiple passwords, where we expected one.");
418b9be5 594
c7b7d74e 595 if (!streq(*a, *b)) {
418b9be5 596 log_error("Entered passwords did not match, please try again.");
418b9be5
LP
597 continue;
598 }
599
c7b7d74e 600 arg_root_password = TAKE_PTR(*a);
418b9be5
LP
601 break;
602 }
603
604 return 0;
605}
606
28900a1b
DDM
607static int prompt_root_shell(void) {
608 int r;
609
610 if (arg_root_shell || !arg_prompt_root_shell)
611 return 0;
612
613 print_welcome();
614 putchar('\n');
615
616 for (;;) {
617 _cleanup_free_ char *s = NULL;
618
619 r = ask_string(&s, "%s Please enter root shell for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET));
620 if (r < 0)
621 return log_error_errno(r, "Failed to query root shell: %m");
622
623 if (isempty(s)) {
624 log_warning("No shell entered, skipping.");
625 break;
626 }
627
628 if (!valid_shell(s)) {
629 log_error("Specified shell invalid.");
630 continue;
631 }
632
633 arg_root_shell = TAKE_PTR(s);
634 break;
635 }
636
637 return 0;
638}
639
640static int write_root_passwd(const char *passwd_path, const char *password, const char *shell) {
4926ceaf
DDM
641 _cleanup_fclose_ FILE *original = NULL, *passwd = NULL;
642 _cleanup_(unlink_and_freep) char *passwd_tmp = NULL;
643 int r;
644
c4a53ebf
DDM
645 assert(password);
646
4926ceaf
DDM
647 r = fopen_temporary_label("/etc/passwd", passwd_path, &passwd, &passwd_tmp);
648 if (r < 0)
649 return r;
650
651 original = fopen(passwd_path, "re");
652 if (original) {
653 struct passwd *i;
654
655 r = sync_rights(fileno(original), fileno(passwd));
656 if (r < 0)
657 return r;
658
659 while ((r = fgetpwent_sane(original, &i)) > 0) {
660
28900a1b 661 if (streq(i->pw_name, "root")) {
4926ceaf 662 i->pw_passwd = (char *) password;
28900a1b
DDM
663 if (shell)
664 i->pw_shell = (char *) shell;
665 }
4926ceaf
DDM
666
667 r = putpwent_sane(i, passwd);
668 if (r < 0)
669 return r;
670 }
671 if (r < 0)
672 return r;
673
674 } else {
675 struct passwd root = {
676 .pw_name = (char *) "root",
677 .pw_passwd = (char *) password,
678 .pw_uid = 0,
679 .pw_gid = 0,
680 .pw_gecos = (char *) "Super User",
681 .pw_dir = (char *) "/root",
28900a1b 682 .pw_shell = (char *) (shell ?: "/bin/sh"),
4926ceaf
DDM
683 };
684
685 if (errno != ENOENT)
686 return -errno;
687
b226422c 688 r = fchmod(fileno(passwd), 0644);
4926ceaf
DDM
689 if (r < 0)
690 return -errno;
691
692 r = putpwent_sane(&root, passwd);
693 if (r < 0)
694 return r;
695 }
696
697 r = fflush_sync_and_check(passwd);
698 if (r < 0)
699 return r;
700
701 r = rename_and_apply_smack_floor_label(passwd_tmp, passwd_path);
702 if (r < 0)
703 return r;
704
705 return 0;
706}
707
b4909a3f
DDM
708static int write_root_shadow(const char *shadow_path, const char *hashed_password) {
709 _cleanup_fclose_ FILE *original = NULL, *shadow = NULL;
710 _cleanup_(unlink_and_freep) char *shadow_tmp = NULL;
100d5f6e
FB
711 int r;
712
c4a53ebf
DDM
713 assert(hashed_password);
714
b4909a3f
DDM
715 r = fopen_temporary_label("/etc/shadow", shadow_path, &shadow, &shadow_tmp);
716 if (r < 0)
717 return r;
718
719 original = fopen(shadow_path, "re");
720 if (original) {
721 struct spwd *i;
722
723 r = sync_rights(fileno(original), fileno(shadow));
724 if (r < 0)
725 return r;
726
727 while ((r = fgetspent_sane(original, &i)) > 0) {
728
729 if (streq(i->sp_namp, "root")) {
730 i->sp_pwdp = (char *) hashed_password;
731 i->sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY);
732 }
733
734 r = putspent_sane(i, shadow);
735 if (r < 0)
736 return r;
737 }
738 if (r < 0)
739 return r;
740
741 } else {
742 struct spwd root = {
743 .sp_namp = (char*) "root",
744 .sp_pwdp = (char *) hashed_password,
745 .sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY),
746 .sp_min = -1,
747 .sp_max = -1,
748 .sp_warn = -1,
749 .sp_inact = -1,
750 .sp_expire = -1,
751 .sp_flag = (unsigned long) -1, /* this appears to be what everybody does ... */
752 };
753
754 if (errno != ENOENT)
755 return -errno;
418b9be5 756
b4909a3f
DDM
757 r = fchmod(fileno(shadow), 0000);
758 if (r < 0)
759 return -errno;
418b9be5 760
b4909a3f
DDM
761 r = putspent_sane(&root, shadow);
762 if (r < 0)
763 return r;
764 }
765
766 r = fflush_sync_and_check(shadow);
100d5f6e
FB
767 if (r < 0)
768 return r;
418b9be5 769
b4909a3f
DDM
770 r = rename_and_apply_smack_floor_label(shadow_tmp, shadow_path);
771 if (r < 0)
772 return r;
773
774 return 0;
418b9be5
LP
775}
776
28900a1b 777static int process_root_args(void) {
45035609 778 _cleanup_close_ int lock = -1;
38620433 779 struct crypt_data cd = {};
c4a53ebf
DDM
780 const char *password, *hashed_password;
781 const char *etc_passwd, *etc_shadow;
418b9be5
LP
782 int r;
783
c4a53ebf 784 etc_passwd = prefix_roota(arg_root, "/etc/passwd");
1d13f648 785 etc_shadow = prefix_roota(arg_root, "/etc/shadow");
c4a53ebf
DDM
786
787 /* We only mess with passwd and shadow if both do not exist or --force is specified. These files are
788 * tightly coupled and hence we make sure we have permission from the user to create/modify both
789 * files. */
790 if ((laccess(etc_passwd, F_OK) >= 0 || laccess(etc_shadow, F_OK) >= 0) && !arg_force)
418b9be5
LP
791 return 0;
792
c4a53ebf 793 (void) mkdir_parents(etc_passwd, 0755);
45035609 794
e929bee0 795 lock = take_etc_passwd_lock(arg_root);
45035609 796 if (lock < 0)
c4a53ebf 797 return log_error_errno(lock, "Failed to take a lock on %s: %m", etc_passwd);
4926ceaf 798
28900a1b
DDM
799 if (arg_copy_root_shell && arg_root) {
800 struct passwd *p;
801
802 errno = 0;
803 p = getpwnam("root");
804 if (!p)
805 return log_error_errno(errno_or_else(EIO), "Failed to find passwd entry for root: %m");
806
807 r = free_and_strdup(&arg_root_shell, p->pw_shell);
808 if (r < 0)
809 return log_oom();
810 }
811
812 r = prompt_root_shell();
813 if (r < 0)
814 return r;
815
418b9be5
LP
816 if (arg_copy_root_password && arg_root) {
817 struct spwd *p;
818
819 errno = 0;
820 p = getspnam("root");
c4a53ebf
DDM
821 if (!p)
822 return log_error_errno(errno_or_else(EIO), "Failed to find shadow entry for root: %m");
418b9be5 823
c4a53ebf
DDM
824 r = free_and_strdup(&arg_root_password, p->sp_pwdp);
825 if (r < 0)
826 return log_oom();
418b9be5 827
c4a53ebf 828 arg_root_password_is_hashed = true;
418b9be5
LP
829 }
830
831 r = prompt_root_password();
832 if (r < 0)
833 return r;
834
c4a53ebf
DDM
835 if (arg_root_password && arg_root_password_is_hashed) {
836 password = "x";
676339a1 837 hashed_password = arg_root_password;
c4a53ebf 838 } else if (arg_root_password) {
676339a1
DDM
839 _cleanup_free_ char *salt = NULL;
840 /* hashed_password points inside cd after crypt_r returns so cd has function scope. */
841
c4a53ebf
DDM
842 password = "x";
843
676339a1
DDM
844 r = make_salt(&salt);
845 if (r < 0)
846 return log_error_errno(r, "Failed to get salt: %m");
418b9be5 847
676339a1
DDM
848 errno = 0;
849 hashed_password = crypt_r(arg_root_password, salt, &cd);
850 if (!hashed_password)
851 return log_error_errno(errno == 0 ? SYNTHETIC_ERRNO(EINVAL) : errno,
852 "Failed to encrypt password: %m");
c4a53ebf
DDM
853 } else if (arg_delete_root_password)
854 password = hashed_password = "";
855 else
856 password = hashed_password = "!";
857
28900a1b 858 r = write_root_passwd(etc_passwd, password, arg_root_shell);
c4a53ebf
DDM
859 if (r < 0)
860 return log_error_errno(r, "Failed to write %s: %m", etc_passwd);
861
862 log_info("%s written", etc_passwd);
418b9be5 863
b4909a3f 864 r = write_root_shadow(etc_shadow, hashed_password);
23bbb0de
MS
865 if (r < 0)
866 return log_error_errno(r, "Failed to write %s: %m", etc_shadow);
418b9be5
LP
867
868 log_info("%s written.", etc_shadow);
869 return 0;
870}
871
a5925354
DDM
872static int process_kernel_cmdline(void) {
873 const char *etc_kernel_cmdline;
874 int r;
875
876 etc_kernel_cmdline = prefix_roota(arg_root, "/etc/kernel/cmdline");
877 if (laccess(etc_kernel_cmdline, F_OK) >= 0 && !arg_force)
878 return 0;
879
880 if (!arg_kernel_cmdline)
881 return 0;
882
883 r = write_string_file(etc_kernel_cmdline, arg_kernel_cmdline,
884 WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_SYNC | WRITE_STRING_FILE_MKDIR_0755 |
885 (arg_force ? WRITE_STRING_FILE_ATOMIC : 0));
886 if (r < 0)
887 return log_error_errno(r, "Failed to write %s: %m", etc_kernel_cmdline);
888
889 log_info("%s written.", etc_kernel_cmdline);
890 return 0;
891}
892
3ff9fa59
LP
893static int setup_image(char **ret_mount_dir, LoopDevice **ret_loop_device, DecryptedImage **ret_decrypted_image) {
894 DissectImageFlags f = DISSECT_IMAGE_REQUIRE_ROOT|DISSECT_IMAGE_VALIDATE_OS|DISSECT_IMAGE_RELAX_VAR_CHECK|DISSECT_IMAGE_FSCK;
895 _cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
896 _cleanup_(decrypted_image_unrefp) DecryptedImage *decrypted_image = NULL;
897 _cleanup_(dissected_image_unrefp) DissectedImage *dissected_image = NULL;
898 _cleanup_(rmdir_and_freep) char *mount_dir = NULL;
899 _cleanup_free_ char *temp = NULL;
900 int r;
901
902 if (!arg_image) {
903 *ret_mount_dir = NULL;
904 *ret_decrypted_image = NULL;
905 *ret_loop_device = NULL;
906 return 0;
907 }
908
909 assert(!arg_root);
910
911 r = tempfn_random_child(NULL, "firstboot", &temp);
912 if (r < 0)
913 return log_error_errno(r, "Failed to generate temporary mount directory: %m");
914
915 r = loop_device_make_by_path(arg_image, O_RDWR, LO_FLAGS_PARTSCAN, &d);
916 if (r < 0)
917 return log_error_errno(r, "Failed to set up loopback device: %m");
918
919 r = dissect_image_and_warn(d->fd, arg_image, NULL, 0, NULL, f, &dissected_image);
920 if (r < 0)
921 return r;
922
923 r = dissected_image_decrypt_interactively(dissected_image, NULL, NULL, 0, NULL, NULL, NULL, 0, f, &decrypted_image);
924 if (r < 0)
925 return r;
926
927 r = detach_mount_namespace();
928 if (r < 0)
929 return log_error_errno(r, "Failed to detach mount namespace: %m");
930
931 mount_dir = strdup(temp);
932 if (!mount_dir)
933 return log_oom();
934
935 r = mkdir_p(mount_dir, 0700);
936 if (r < 0) {
937 mount_dir = mfree(mount_dir);
938 return log_error_errno(r, "Failed to create mount point: %m");
939 }
940
941 r = dissected_image_mount(dissected_image, mount_dir, UID_INVALID, f);
942 if (r < 0)
943 return log_error_errno(r, "Failed to mount image: %m");
944
945 if (decrypted_image) {
946 r = decrypted_image_relinquish(decrypted_image);
947 if (r < 0)
948 return log_error_errno(r, "Failed to relinquish DM devices: %m");
949 }
950
951 loop_device_relinquish(d);
952
953 arg_root = TAKE_PTR(temp);
954
955 *ret_mount_dir = TAKE_PTR(mount_dir);
956 *ret_decrypted_image = TAKE_PTR(decrypted_image);
957 *ret_loop_device = TAKE_PTR(d);
958
959 return 1;
960}
961
37ec0fdd
LP
962static int help(void) {
963 _cleanup_free_ char *link = NULL;
964 int r;
965
966 r = terminal_urlify_man("systemd-firstboot", "1", &link);
967 if (r < 0)
968 return log_oom();
969
418b9be5
LP
970 printf("%s [OPTIONS...]\n\n"
971 "Configures basic settings of the system.\n\n"
676339a1
DDM
972 " -h --help Show this help\n"
973 " --version Show package version\n"
974 " --root=PATH Operate on an alternate filesystem root\n"
3ff9fa59 975 " --image=PATH Operate on an alternate filesystem image\n"
676339a1
DDM
976 " --locale=LOCALE Set primary locale (LANG=)\n"
977 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
978 " --keymap=KEYMAP Set keymap\n"
979 " --timezone=TIMEZONE Set timezone\n"
980 " --hostname=NAME Set hostname\n"
981 " --machine-ID=ID Set machine ID\n"
982 " --root-password=PASSWORD Set root password from plaintext password\n"
983 " --root-password-file=FILE Set root password from file\n"
984 " --root-password-hashed=HASHED_PASSWORD Set root password from hashed password\n"
985 " --prompt-locale Prompt the user for locale settings\n"
986 " --prompt-keymap Prompt the user for keymap settings\n"
987 " --prompt-timezone Prompt the user for timezone\n"
988 " --prompt-hostname Prompt the user for hostname\n"
989 " --prompt-root-password Prompt the user for root password\n"
990 " --prompt Prompt for all of the above\n"
991 " --copy-locale Copy locale from host\n"
992 " --copy-keymap Copy keymap from host\n"
993 " --copy-timezone Copy timezone from host\n"
994 " --copy-root-password Copy root password from host\n"
995 " --copy Copy locale, keymap, timezone, root password\n"
996 " --setup-machine-id Generate a new random machine ID\n"
997 " --force Overwrite existing files\n"
998 " --delete-root-password Delete root password\n"
a1225020 999 " --welcome=no Disable the welcome text\n"
37ec0fdd
LP
1000 "\nSee the %s for details.\n"
1001 , program_invocation_short_name
1002 , link
1003 );
1004
1005 return 0;
418b9be5
LP
1006}
1007
1008static int parse_argv(int argc, char *argv[]) {
1009
1010 enum {
1011 ARG_VERSION = 0x100,
1012 ARG_ROOT,
3ff9fa59 1013 ARG_IMAGE,
418b9be5
LP
1014 ARG_LOCALE,
1015 ARG_LOCALE_MESSAGES,
ed457f13 1016 ARG_KEYMAP,
418b9be5
LP
1017 ARG_TIMEZONE,
1018 ARG_HOSTNAME,
1019 ARG_MACHINE_ID,
1020 ARG_ROOT_PASSWORD,
1021 ARG_ROOT_PASSWORD_FILE,
676339a1 1022 ARG_ROOT_PASSWORD_HASHED,
28900a1b 1023 ARG_ROOT_SHELL,
a5925354 1024 ARG_KERNEL_COMMAND_LINE,
418b9be5
LP
1025 ARG_PROMPT,
1026 ARG_PROMPT_LOCALE,
ed457f13 1027 ARG_PROMPT_KEYMAP,
418b9be5
LP
1028 ARG_PROMPT_TIMEZONE,
1029 ARG_PROMPT_HOSTNAME,
1030 ARG_PROMPT_ROOT_PASSWORD,
28900a1b 1031 ARG_PROMPT_ROOT_SHELL,
418b9be5
LP
1032 ARG_COPY,
1033 ARG_COPY_LOCALE,
ed457f13 1034 ARG_COPY_KEYMAP,
418b9be5
LP
1035 ARG_COPY_TIMEZONE,
1036 ARG_COPY_ROOT_PASSWORD,
28900a1b 1037 ARG_COPY_ROOT_SHELL,
418b9be5 1038 ARG_SETUP_MACHINE_ID,
b4909a3f 1039 ARG_FORCE,
4926ceaf 1040 ARG_DELETE_ROOT_PASSWORD,
a1225020 1041 ARG_WELCOME,
418b9be5
LP
1042 };
1043
1044 static const struct option options[] = {
676339a1
DDM
1045 { "help", no_argument, NULL, 'h' },
1046 { "version", no_argument, NULL, ARG_VERSION },
1047 { "root", required_argument, NULL, ARG_ROOT },
3ff9fa59 1048 { "image", required_argument, NULL, ARG_IMAGE },
676339a1
DDM
1049 { "locale", required_argument, NULL, ARG_LOCALE },
1050 { "locale-messages", required_argument, NULL, ARG_LOCALE_MESSAGES },
1051 { "keymap", required_argument, NULL, ARG_KEYMAP },
1052 { "timezone", required_argument, NULL, ARG_TIMEZONE },
1053 { "hostname", required_argument, NULL, ARG_HOSTNAME },
1054 { "machine-id", required_argument, NULL, ARG_MACHINE_ID },
1055 { "root-password", required_argument, NULL, ARG_ROOT_PASSWORD },
1056 { "root-password-file", required_argument, NULL, ARG_ROOT_PASSWORD_FILE },
1057 { "root-password-hashed", required_argument, NULL, ARG_ROOT_PASSWORD_HASHED },
28900a1b 1058 { "root-shell", required_argument, NULL, ARG_ROOT_SHELL },
a5925354 1059 { "kernel-command-line", required_argument, NULL, ARG_KERNEL_COMMAND_LINE },
676339a1
DDM
1060 { "prompt", no_argument, NULL, ARG_PROMPT },
1061 { "prompt-locale", no_argument, NULL, ARG_PROMPT_LOCALE },
1062 { "prompt-keymap", no_argument, NULL, ARG_PROMPT_KEYMAP },
1063 { "prompt-timezone", no_argument, NULL, ARG_PROMPT_TIMEZONE },
1064 { "prompt-hostname", no_argument, NULL, ARG_PROMPT_HOSTNAME },
1065 { "prompt-root-password", no_argument, NULL, ARG_PROMPT_ROOT_PASSWORD },
28900a1b 1066 { "prompt-root-shell", no_argument, NULL, ARG_PROMPT_ROOT_SHELL },
676339a1
DDM
1067 { "copy", no_argument, NULL, ARG_COPY },
1068 { "copy-locale", no_argument, NULL, ARG_COPY_LOCALE },
1069 { "copy-keymap", no_argument, NULL, ARG_COPY_KEYMAP },
1070 { "copy-timezone", no_argument, NULL, ARG_COPY_TIMEZONE },
1071 { "copy-root-password", no_argument, NULL, ARG_COPY_ROOT_PASSWORD },
28900a1b 1072 { "copy-root-shell", no_argument, NULL, ARG_COPY_ROOT_SHELL },
676339a1
DDM
1073 { "setup-machine-id", no_argument, NULL, ARG_SETUP_MACHINE_ID },
1074 { "force", no_argument, NULL, ARG_FORCE },
1075 { "delete-root-password", no_argument, NULL, ARG_DELETE_ROOT_PASSWORD },
a1225020 1076 { "welcome", required_argument, NULL, ARG_WELCOME },
418b9be5
LP
1077 {}
1078 };
1079
1080 int r, c;
1081
1082 assert(argc >= 0);
1083 assert(argv);
1084
601185b4 1085 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
418b9be5
LP
1086
1087 switch (c) {
1088
1089 case 'h':
37ec0fdd 1090 return help();
418b9be5
LP
1091
1092 case ARG_VERSION:
3f6fd1ba 1093 return version();
418b9be5
LP
1094
1095 case ARG_ROOT:
0f03c2a4 1096 r = parse_path_argument_and_warn(optarg, true, &arg_root);
0f474365 1097 if (r < 0)
0f03c2a4 1098 return r;
418b9be5
LP
1099 break;
1100
3ff9fa59
LP
1101 case ARG_IMAGE:
1102 r = parse_path_argument_and_warn(optarg, false, &arg_image);
1103 if (r < 0)
1104 return r;
1105 break;
1106
418b9be5 1107 case ARG_LOCALE:
2fc09a9c
DM
1108 r = free_and_strdup(&arg_locale, optarg);
1109 if (r < 0)
418b9be5
LP
1110 return log_oom();
1111
1112 break;
1113
1114 case ARG_LOCALE_MESSAGES:
2fc09a9c
DM
1115 r = free_and_strdup(&arg_locale_messages, optarg);
1116 if (r < 0)
418b9be5
LP
1117 return log_oom();
1118
1119 break;
1120
ed457f13 1121 case ARG_KEYMAP:
baaa35ad
ZJS
1122 if (!keymap_is_valid(optarg))
1123 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1124 "Keymap %s is not valid.", optarg);
ed457f13
TB
1125
1126 r = free_and_strdup(&arg_keymap, optarg);
1127 if (r < 0)
1128 return log_oom();
1129
1130 break;
1131
418b9be5 1132 case ARG_TIMEZONE:
baaa35ad
ZJS
1133 if (!timezone_is_valid(optarg, LOG_ERR))
1134 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1135 "Timezone %s is not valid.", optarg);
418b9be5 1136
2fc09a9c
DM
1137 r = free_and_strdup(&arg_timezone, optarg);
1138 if (r < 0)
418b9be5
LP
1139 return log_oom();
1140
1141 break;
1142
1143 case ARG_ROOT_PASSWORD:
2fc09a9c
DM
1144 r = free_and_strdup(&arg_root_password, optarg);
1145 if (r < 0)
418b9be5 1146 return log_oom();
676339a1
DDM
1147
1148 arg_root_password_is_hashed = false;
418b9be5
LP
1149 break;
1150
1151 case ARG_ROOT_PASSWORD_FILE:
0da16248 1152 arg_root_password = mfree(arg_root_password);
418b9be5
LP
1153
1154 r = read_one_line_file(optarg, &arg_root_password);
23bbb0de
MS
1155 if (r < 0)
1156 return log_error_errno(r, "Failed to read %s: %m", optarg);
418b9be5 1157
676339a1
DDM
1158 arg_root_password_is_hashed = false;
1159 break;
1160
1161 case ARG_ROOT_PASSWORD_HASHED:
1162 r = free_and_strdup(&arg_root_password, optarg);
1163 if (r < 0)
1164 return log_oom();
1165
1166 arg_root_password_is_hashed = true;
418b9be5
LP
1167 break;
1168
28900a1b
DDM
1169 case ARG_ROOT_SHELL:
1170 if (!valid_shell(optarg))
1171 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1172 "%s is not a valid shell path", optarg);
1173
1174 r = free_and_strdup(&arg_root_shell, optarg);
1175 if (r < 0)
1176 return log_oom();
1177
1178 break;
1179
418b9be5 1180 case ARG_HOSTNAME:
baaa35ad
ZJS
1181 if (!hostname_is_valid(optarg, true))
1182 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1183 "Host name %s is not valid.", optarg);
418b9be5 1184
ae691c1d 1185 hostname_cleanup(optarg);
2fc09a9c
DM
1186 r = free_and_strdup(&arg_hostname, optarg);
1187 if (r < 0)
418b9be5
LP
1188 return log_oom();
1189
1190 break;
1191
1192 case ARG_MACHINE_ID:
baaa35ad
ZJS
1193 if (sd_id128_from_string(optarg, &arg_machine_id) < 0)
1194 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1195 "Failed to parse machine id %s.", optarg);
418b9be5
LP
1196
1197 break;
1198
a5925354
DDM
1199 case ARG_KERNEL_COMMAND_LINE:
1200 r = free_and_strdup(&arg_kernel_cmdline, optarg);
1201 if (r < 0)
1202 return log_oom();
1203
1204 break;
1205
418b9be5 1206 case ARG_PROMPT:
28900a1b
DDM
1207 arg_prompt_locale = arg_prompt_keymap = arg_prompt_timezone = arg_prompt_hostname =
1208 arg_prompt_root_password = arg_prompt_root_shell = true;
418b9be5
LP
1209 break;
1210
1211 case ARG_PROMPT_LOCALE:
1212 arg_prompt_locale = true;
1213 break;
1214
ed457f13
TB
1215 case ARG_PROMPT_KEYMAP:
1216 arg_prompt_keymap = true;
1217 break;
1218
418b9be5
LP
1219 case ARG_PROMPT_TIMEZONE:
1220 arg_prompt_timezone = true;
1221 break;
1222
1223 case ARG_PROMPT_HOSTNAME:
1224 arg_prompt_hostname = true;
1225 break;
1226
1227 case ARG_PROMPT_ROOT_PASSWORD:
1228 arg_prompt_root_password = true;
1229 break;
1230
28900a1b
DDM
1231 case ARG_PROMPT_ROOT_SHELL:
1232 arg_prompt_root_shell = true;
1233 break;
1234
418b9be5 1235 case ARG_COPY:
28900a1b
DDM
1236 arg_copy_locale = arg_copy_keymap = arg_copy_timezone = arg_copy_root_password =
1237 arg_copy_root_shell = true;
e926f647 1238 break;
418b9be5
LP
1239
1240 case ARG_COPY_LOCALE:
1241 arg_copy_locale = true;
1242 break;
1243
ed457f13
TB
1244 case ARG_COPY_KEYMAP:
1245 arg_copy_keymap = true;
1246 break;
1247
418b9be5
LP
1248 case ARG_COPY_TIMEZONE:
1249 arg_copy_timezone = true;
1250 break;
1251
1252 case ARG_COPY_ROOT_PASSWORD:
1253 arg_copy_root_password = true;
1254 break;
1255
28900a1b
DDM
1256 case ARG_COPY_ROOT_SHELL:
1257 arg_copy_root_shell = true;
1258 break;
1259
418b9be5 1260 case ARG_SETUP_MACHINE_ID:
418b9be5 1261 r = sd_id128_randomize(&arg_machine_id);
23bbb0de
MS
1262 if (r < 0)
1263 return log_error_errno(r, "Failed to generate randomized machine ID: %m");
418b9be5
LP
1264
1265 break;
1266
b4909a3f
DDM
1267 case ARG_FORCE:
1268 arg_force = true;
1269 break;
1270
4926ceaf
DDM
1271 case ARG_DELETE_ROOT_PASSWORD:
1272 arg_delete_root_password = true;
1273 break;
1274
a1225020
LP
1275 case ARG_WELCOME:
1276 r = parse_boolean(optarg);
1277 if (r < 0)
1278 return log_error_errno(r, "Failed to parse --welcome= argument: %s", optarg);
1279
1280 arg_welcome = r;
1281 break;
1282
418b9be5
LP
1283 case '?':
1284 return -EINVAL;
1285
1286 default:
1287 assert_not_reached("Unhandled option");
1288 }
418b9be5 1289
a00a78b8
LP
1290 /* We check if the specified locale strings are valid down here, so that we can take --root= into
1291 * account when looking for the locale files. */
1292
1293 if (arg_locale && !locale_is_ok(arg_locale))
1294 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale);
1295 if (arg_locale_messages && !locale_is_ok(arg_locale_messages))
1296 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale_messages);
1297
4926ceaf
DDM
1298 if (arg_delete_root_password && (arg_copy_root_password || arg_root_password || arg_prompt_root_password))
1299 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1300 "--delete-root-password cannot be combined with other root password options");
1301
3ff9fa59
LP
1302 if (arg_image && arg_root)
1303 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
1304
418b9be5
LP
1305 return 1;
1306}
1307
45f39418 1308static int run(int argc, char *argv[]) {
3ff9fa59
LP
1309 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
1310 _cleanup_(decrypted_image_unrefp) DecryptedImage *decrypted_image = NULL;
1311 _cleanup_(umount_and_rmdir_and_freep) char *unlink_dir = NULL;
418b9be5
LP
1312 int r;
1313
1314 r = parse_argv(argc, argv);
1315 if (r <= 0)
45f39418 1316 return r;
418b9be5 1317
6bf3c61c 1318 log_setup_service();
418b9be5
LP
1319
1320 umask(0022);
1321
3ff9fa59
LP
1322 if (!arg_root && !arg_image) {
1323 bool enabled;
1324
1325 /* If we are called without --root=/--image= let's honour the systemd.firstboot kernel
1326 * command line option, because we are called to provision the host with basic settings (as
1327 * opposed to some other file system tree/image) */
1328
1329 r = proc_cmdline_get_bool("systemd.firstboot", &enabled);
1330 if (r < 0)
1331 return log_error_errno(r, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
1332 if (r > 0 && !enabled)
1333 return 0; /* disabled */
1334 }
1335
1336 r = setup_image(&unlink_dir, &loop_device, &decrypted_image);
45f39418 1337 if (r < 0)
3ff9fa59 1338 return r;
f582cbca 1339
418b9be5
LP
1340 r = process_locale();
1341 if (r < 0)
45f39418 1342 return r;
418b9be5 1343
ed457f13
TB
1344 r = process_keymap();
1345 if (r < 0)
45f39418 1346 return r;
ed457f13 1347
418b9be5
LP
1348 r = process_timezone();
1349 if (r < 0)
45f39418 1350 return r;
418b9be5
LP
1351
1352 r = process_hostname();
1353 if (r < 0)
45f39418 1354 return r;
418b9be5
LP
1355
1356 r = process_machine_id();
1357 if (r < 0)
45f39418 1358 return r;
418b9be5 1359
28900a1b 1360 r = process_root_args();
418b9be5 1361 if (r < 0)
45f39418
YW
1362 return r;
1363
a5925354
DDM
1364 r = process_kernel_cmdline();
1365 if (r < 0)
1366 return r;
1367
45f39418 1368 return 0;
418b9be5 1369}
45f39418
YW
1370
1371DEFINE_MAIN_FUNCTION(run);