1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include <linux/loop.h>
10 #include "alloc-util.h"
11 #include "ask-password-api.h"
13 #include "dissect-image.h"
18 #include "hostname-util.h"
20 #include "libcrypt-util.h"
21 #include "locale-util.h"
22 #include "main-func.h"
23 #include "memory-util.h"
25 #include "mount-util.h"
27 #include "parse-argument.h"
28 #include "parse-util.h"
29 #include "path-util.h"
30 #include "pretty-print.h"
31 #include "proc-cmdline.h"
32 #include "pwquality-util.h"
33 #include "random-util.h"
34 #include "string-util.h"
36 #include "terminal-util.h"
37 #include "time-util.h"
38 #include "tmpfile-util-label.h"
39 #include "tmpfile-util.h"
40 #include "umask-util.h"
41 #include "user-util.h"
43 static char *arg_root
= NULL
;
44 static char *arg_image
= NULL
;
45 static char *arg_locale
= NULL
; /* $LANG */
46 static char *arg_keymap
= NULL
;
47 static char *arg_locale_messages
= NULL
; /* $LC_MESSAGES */
48 static char *arg_timezone
= NULL
;
49 static char *arg_hostname
= NULL
;
50 static sd_id128_t arg_machine_id
= {};
51 static char *arg_root_password
= NULL
;
52 static char *arg_root_shell
= NULL
;
53 static char *arg_kernel_cmdline
= NULL
;
54 static bool arg_prompt_locale
= false;
55 static bool arg_prompt_keymap
= false;
56 static bool arg_prompt_timezone
= false;
57 static bool arg_prompt_hostname
= false;
58 static bool arg_prompt_root_password
= false;
59 static bool arg_prompt_root_shell
= false;
60 static bool arg_copy_locale
= false;
61 static bool arg_copy_keymap
= false;
62 static bool arg_copy_timezone
= false;
63 static bool arg_copy_root_password
= false;
64 static bool arg_copy_root_shell
= false;
65 static bool arg_force
= false;
66 static bool arg_delete_root_password
= false;
67 static bool arg_root_password_is_hashed
= false;
68 static bool arg_welcome
= true;
70 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
71 STATIC_DESTRUCTOR_REGISTER(arg_image
, freep
);
72 STATIC_DESTRUCTOR_REGISTER(arg_locale
, freep
);
73 STATIC_DESTRUCTOR_REGISTER(arg_locale_messages
, freep
);
74 STATIC_DESTRUCTOR_REGISTER(arg_keymap
, freep
);
75 STATIC_DESTRUCTOR_REGISTER(arg_timezone
, freep
);
76 STATIC_DESTRUCTOR_REGISTER(arg_hostname
, freep
);
77 STATIC_DESTRUCTOR_REGISTER(arg_root_password
, erase_and_freep
);
79 static bool press_any_key(void) {
83 printf("-- Press any key to proceed --");
86 (void) read_one_char(stdin
, &k
, USEC_INFINITY
, &need_nl
);
94 static void print_welcome(void) {
95 _cleanup_free_
char *pretty_name
= NULL
, *ansi_color
= NULL
;
96 static bool done
= false;
106 r
= parse_os_release(
108 "PRETTY_NAME", &pretty_name
,
109 "ANSI_COLOR", &ansi_color
,
112 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
,
113 "Failed to read os-release file, ignoring: %m");
115 pn
= isempty(pretty_name
) ? "Linux" : pretty_name
;
116 ac
= isempty(ansi_color
) ? "0" : ansi_color
;
118 if (colors_enabled())
119 printf("\nWelcome to your new installation of \x1B[%sm%s\x1B[0m!\n", ac
, pn
);
121 printf("\nWelcome to your new installation of %s!\n", pn
);
123 printf("\nPlease configure your system!\n\n");
130 static int show_menu(char **x
, unsigned n_columns
, unsigned width
, unsigned percentage
) {
131 unsigned break_lines
, break_modulo
;
132 size_t n
, per_column
, i
, j
;
134 assert(n_columns
> 0);
137 per_column
= DIV_ROUND_UP(n
, n_columns
);
139 break_lines
= lines();
143 /* The first page gets two extra lines, since we want to show
145 break_modulo
= break_lines
;
146 if (break_modulo
> 3)
149 for (i
= 0; i
< per_column
; i
++) {
151 for (j
= 0; j
< n_columns
; j
++) {
152 _cleanup_free_
char *e
= NULL
;
154 if (j
* per_column
+ i
>= n
)
157 e
= ellipsize(x
[j
* per_column
+ i
], width
, percentage
);
161 printf("%4zu) %-*s", j
* per_column
+ i
+ 1, width
, e
);
166 /* on the first screen we reserve 2 extra lines for the title */
167 if (i
% break_lines
== break_modulo
) {
168 if (!press_any_key())
176 static int prompt_loop(const char *text
, char **l
, unsigned percentage
, bool (*is_valid
)(const char *name
), char **ret
) {
184 _cleanup_free_
char *p
= NULL
;
187 r
= ask_string(&p
, "%s %s (empty to skip, \"list\" to list options): ",
188 special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), text
);
190 return log_error_errno(r
, "Failed to query user: %m");
193 log_warning("No data entered, skipping.");
197 if (streq(p
, "list")) {
198 r
= show_menu(l
, 3, 22, percentage
);
206 r
= safe_atou(p
, &u
);
208 if (u
<= 0 || u
> strv_length(l
)) {
209 log_error("Specified entry number out of range.");
213 log_info("Selected '%s'.", l
[u
-1]);
214 return free_and_strdup_warn(ret
, l
[u
-1]);
218 log_error("Entered data invalid.");
222 return free_and_replace(*ret
, p
);
226 static bool locale_is_ok(const char *name
) {
229 return locale_is_valid(name
);
231 return locale_is_installed(name
) > 0;
234 static int prompt_locale(void) {
235 _cleanup_strv_free_
char **locales
= NULL
;
238 if (arg_locale
|| arg_locale_messages
)
241 if (!arg_prompt_locale
)
244 r
= get_locales(&locales
);
246 return log_error_errno(r
, "Cannot query locales list: %m");
248 if (strv_isempty(locales
))
249 log_debug("No locales found, skipping locale selection.");
250 else if (strv_length(locales
) == 1) {
252 if (streq(locales
[0], SYSTEMD_DEFAULT_LOCALE
))
253 log_debug("Only installed locale is default locale anyway, not setting locale explicitly.");
255 log_debug("Only a single locale available (%s), selecting it as default.", locales
[0]);
257 arg_locale
= strdup(locales
[0]);
261 /* Not setting arg_locale_message here, since it defaults to LANG anyway */
266 r
= prompt_loop("Please enter system locale name or number",
267 locales
, 60, locale_is_ok
, &arg_locale
);
271 if (isempty(arg_locale
))
274 r
= prompt_loop("Please enter system message locale name or number",
275 locales
, 60, locale_is_ok
, &arg_locale_messages
);
279 /* Suppress the messages setting if it's the same as the main locale anyway */
280 if (streq_ptr(arg_locale
, arg_locale_messages
))
281 arg_locale_messages
= mfree(arg_locale_messages
);
287 static int process_locale(void) {
288 const char *etc_localeconf
;
293 etc_localeconf
= prefix_roota(arg_root
, "/etc/locale.conf");
294 if (laccess(etc_localeconf
, F_OK
) >= 0 && !arg_force
)
297 if (arg_copy_locale
&& arg_root
) {
299 (void) mkdir_parents(etc_localeconf
, 0755);
300 r
= copy_file("/etc/locale.conf", etc_localeconf
, 0, 0644, 0, 0, COPY_REFLINK
);
303 return log_error_errno(r
, "Failed to copy %s: %m", etc_localeconf
);
305 log_info("%s copied.", etc_localeconf
);
314 if (!isempty(arg_locale
))
315 locales
[i
++] = strjoina("LANG=", arg_locale
);
316 if (!isempty(arg_locale_messages
) && !streq(arg_locale_messages
, arg_locale
))
317 locales
[i
++] = strjoina("LC_MESSAGES=", arg_locale_messages
);
324 (void) mkdir_parents(etc_localeconf
, 0755);
325 r
= write_env_file(etc_localeconf
, locales
);
327 return log_error_errno(r
, "Failed to write %s: %m", etc_localeconf
);
329 log_info("%s written.", etc_localeconf
);
333 static int prompt_keymap(void) {
334 _cleanup_strv_free_
char **kmaps
= NULL
;
340 if (!arg_prompt_keymap
)
343 r
= get_keymaps(&kmaps
);
344 if (r
== -ENOENT
) /* no keymaps installed */
347 return log_error_errno(r
, "Failed to read keymaps: %m");
351 return prompt_loop("Please enter system keymap name or number",
352 kmaps
, 60, keymap_is_valid
, &arg_keymap
);
355 static int process_keymap(void) {
356 const char *etc_vconsoleconf
;
360 etc_vconsoleconf
= prefix_roota(arg_root
, "/etc/vconsole.conf");
361 if (laccess(etc_vconsoleconf
, F_OK
) >= 0 && !arg_force
)
364 if (arg_copy_keymap
&& arg_root
) {
366 (void) mkdir_parents(etc_vconsoleconf
, 0755);
367 r
= copy_file("/etc/vconsole.conf", etc_vconsoleconf
, 0, 0644, 0, 0, COPY_REFLINK
);
370 return log_error_errno(r
, "Failed to copy %s: %m", etc_vconsoleconf
);
372 log_info("%s copied.", etc_vconsoleconf
);
379 return 0; /* don't fail if no keymaps are installed */
383 if (isempty(arg_keymap
))
386 keymap
= STRV_MAKE(strjoina("KEYMAP=", arg_keymap
));
388 r
= mkdir_parents(etc_vconsoleconf
, 0755);
390 return log_error_errno(r
, "Failed to create the parent directory of %s: %m", etc_vconsoleconf
);
392 r
= write_env_file(etc_vconsoleconf
, keymap
);
394 return log_error_errno(r
, "Failed to write %s: %m", etc_vconsoleconf
);
396 log_info("%s written.", etc_vconsoleconf
);
400 static bool timezone_is_valid_log_error(const char *name
) {
401 return timezone_is_valid(name
, LOG_ERR
);
404 static int prompt_timezone(void) {
405 _cleanup_strv_free_
char **zones
= NULL
;
411 if (!arg_prompt_timezone
)
414 r
= get_timezones(&zones
);
416 return log_error_errno(r
, "Cannot query timezone list: %m");
420 r
= prompt_loop("Please enter timezone name or number",
421 zones
, 30, timezone_is_valid_log_error
, &arg_timezone
);
428 static int process_timezone(void) {
429 const char *etc_localtime
, *e
;
432 etc_localtime
= prefix_roota(arg_root
, "/etc/localtime");
433 if (laccess(etc_localtime
, F_OK
) >= 0 && !arg_force
)
436 if (arg_copy_timezone
&& arg_root
) {
437 _cleanup_free_
char *p
= NULL
;
439 r
= readlink_malloc("/etc/localtime", &p
);
442 return log_error_errno(r
, "Failed to read host timezone: %m");
444 (void) mkdir_parents(etc_localtime
, 0755);
445 if (symlink(p
, etc_localtime
) < 0)
446 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
448 log_info("%s copied.", etc_localtime
);
453 r
= prompt_timezone();
457 if (isempty(arg_timezone
))
460 e
= strjoina("../usr/share/zoneinfo/", arg_timezone
);
462 (void) mkdir_parents(etc_localtime
, 0755);
463 if (symlink(e
, etc_localtime
) < 0)
464 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
466 log_info("%s written", etc_localtime
);
470 static int prompt_hostname(void) {
476 if (!arg_prompt_hostname
)
483 _cleanup_free_
char *h
= NULL
;
485 r
= ask_string(&h
, "%s Please enter hostname for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
));
487 return log_error_errno(r
, "Failed to query hostname: %m");
490 log_warning("No hostname entered, skipping.");
494 if (!hostname_is_valid(h
, VALID_HOSTNAME_TRAILING_DOT
)) {
495 log_error("Specified hostname invalid.");
499 /* Get rid of the trailing dot that we allow, but don't want to see */
500 arg_hostname
= hostname_cleanup(h
);
508 static int process_hostname(void) {
509 const char *etc_hostname
;
512 etc_hostname
= prefix_roota(arg_root
, "/etc/hostname");
513 if (laccess(etc_hostname
, F_OK
) >= 0 && !arg_force
)
516 r
= prompt_hostname();
520 if (isempty(arg_hostname
))
523 r
= write_string_file(etc_hostname
, arg_hostname
,
524 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
|
525 (arg_force
? WRITE_STRING_FILE_ATOMIC
: 0));
527 return log_error_errno(r
, "Failed to write %s: %m", etc_hostname
);
529 log_info("%s written.", etc_hostname
);
533 static int process_machine_id(void) {
534 const char *etc_machine_id
;
535 char id
[SD_ID128_STRING_MAX
];
538 etc_machine_id
= prefix_roota(arg_root
, "/etc/machine-id");
539 if (laccess(etc_machine_id
, F_OK
) >= 0 && !arg_force
)
542 if (sd_id128_is_null(arg_machine_id
))
545 r
= write_string_file(etc_machine_id
, sd_id128_to_string(arg_machine_id
, id
),
546 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
|
547 (arg_force
? WRITE_STRING_FILE_ATOMIC
: 0));
549 return log_error_errno(r
, "Failed to write machine id: %m");
551 log_info("%s written.", etc_machine_id
);
555 static int prompt_root_password(void) {
556 const char *msg1
, *msg2
;
559 if (arg_root_password
)
562 if (!arg_prompt_root_password
)
568 msg1
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter a new root password (empty to skip):");
569 msg2
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter new root password again:");
574 _cleanup_strv_free_erase_
char **a
= NULL
, **b
= NULL
;
575 _cleanup_free_
char *error
= NULL
;
577 r
= ask_password_tty(-1, msg1
, NULL
, 0, 0, NULL
, &a
);
579 return log_error_errno(r
, "Failed to query root password: %m");
580 if (strv_length(a
) != 1)
581 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
582 "Received multiple passwords, where we expected one.");
585 log_warning("No password entered, skipping.");
589 r
= quality_check_password(*a
, "root", &error
);
591 return log_error_errno(r
, "Failed to check quality of password: %m");
593 log_warning("Password is weak, accepting anyway: %s", error
);
595 r
= ask_password_tty(-1, msg2
, NULL
, 0, 0, NULL
, &b
);
597 return log_error_errno(r
, "Failed to query root password: %m");
598 if (strv_length(b
) != 1)
599 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
600 "Received multiple passwords, where we expected one.");
602 if (!streq(*a
, *b
)) {
603 log_error("Entered passwords did not match, please try again.");
607 arg_root_password
= TAKE_PTR(*a
);
614 static int find_shell(const char *path
, const char *root
) {
619 if (!valid_shell(path
))
620 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "%s is not a valid shell", path
);
622 r
= chase_symlinks(path
, root
, CHASE_PREFIX_ROOT
, NULL
, NULL
);
625 p
= prefix_roota(root
, path
);
626 return log_error_errno(r
, "Failed to resolve shell %s: %m", p
);
632 static int prompt_root_shell(void) {
635 if (arg_root_shell
|| !arg_prompt_root_shell
)
642 _cleanup_free_
char *s
= NULL
;
644 r
= ask_string(&s
, "%s Please enter root shell for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
));
646 return log_error_errno(r
, "Failed to query root shell: %m");
649 log_warning("No shell entered, skipping.");
653 r
= find_shell(s
, arg_root
);
657 arg_root_shell
= TAKE_PTR(s
);
664 static int write_root_passwd(const char *passwd_path
, const char *password
, const char *shell
) {
665 _cleanup_fclose_
FILE *original
= NULL
, *passwd
= NULL
;
666 _cleanup_(unlink_and_freep
) char *passwd_tmp
= NULL
;
671 r
= fopen_temporary_label("/etc/passwd", passwd_path
, &passwd
, &passwd_tmp
);
675 original
= fopen(passwd_path
, "re");
679 r
= sync_rights(fileno(original
), fileno(passwd
));
683 while ((r
= fgetpwent_sane(original
, &i
)) > 0) {
685 if (streq(i
->pw_name
, "root")) {
686 i
->pw_passwd
= (char *) password
;
688 i
->pw_shell
= (char *) shell
;
691 r
= putpwent_sane(i
, passwd
);
699 struct passwd root
= {
700 .pw_name
= (char *) "root",
701 .pw_passwd
= (char *) password
,
704 .pw_gecos
= (char *) "Super User",
705 .pw_dir
= (char *) "/root",
706 .pw_shell
= (char *) (shell
?: "/bin/sh"),
712 r
= fchmod(fileno(passwd
), 0644);
716 r
= putpwent_sane(&root
, passwd
);
721 r
= fflush_sync_and_check(passwd
);
725 r
= rename_and_apply_smack_floor_label(passwd_tmp
, passwd_path
);
732 static int write_root_shadow(const char *shadow_path
, const char *hashed_password
) {
733 _cleanup_fclose_
FILE *original
= NULL
, *shadow
= NULL
;
734 _cleanup_(unlink_and_freep
) char *shadow_tmp
= NULL
;
737 assert(hashed_password
);
739 r
= fopen_temporary_label("/etc/shadow", shadow_path
, &shadow
, &shadow_tmp
);
743 original
= fopen(shadow_path
, "re");
747 r
= sync_rights(fileno(original
), fileno(shadow
));
751 while ((r
= fgetspent_sane(original
, &i
)) > 0) {
753 if (streq(i
->sp_namp
, "root")) {
754 i
->sp_pwdp
= (char *) hashed_password
;
755 i
->sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
);
758 r
= putspent_sane(i
, shadow
);
767 .sp_namp
= (char*) "root",
768 .sp_pwdp
= (char *) hashed_password
,
769 .sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
),
775 .sp_flag
= (unsigned long) -1, /* this appears to be what everybody does ... */
781 r
= fchmod(fileno(shadow
), 0000);
785 r
= putspent_sane(&root
, shadow
);
790 r
= fflush_sync_and_check(shadow
);
794 r
= rename_and_apply_smack_floor_label(shadow_tmp
, shadow_path
);
801 static int process_root_args(void) {
802 _cleanup_close_
int lock
= -1;
803 _cleanup_(erase_and_freep
) char *_hashed_password
= NULL
;
804 const char *password
, *hashed_password
;
805 const char *etc_passwd
, *etc_shadow
;
808 etc_passwd
= prefix_roota(arg_root
, "/etc/passwd");
809 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
811 /* We only mess with passwd and shadow if both do not exist or --force is specified. These files are
812 * tightly coupled and hence we make sure we have permission from the user to create/modify both
814 if ((laccess(etc_passwd
, F_OK
) >= 0 || laccess(etc_shadow
, F_OK
) >= 0) && !arg_force
)
816 /* Don't create/modify passwd and shadow if not asked */
817 if (!(arg_root_password
|| arg_prompt_root_password
|| arg_copy_root_password
|| arg_delete_root_password
||
818 arg_root_shell
|| arg_prompt_root_shell
|| arg_copy_root_shell
))
821 (void) mkdir_parents(etc_passwd
, 0755);
823 lock
= take_etc_passwd_lock(arg_root
);
825 return log_error_errno(lock
, "Failed to take a lock on %s: %m", etc_passwd
);
827 if (arg_copy_root_shell
&& arg_root
) {
831 p
= getpwnam("root");
833 return log_error_errno(errno_or_else(EIO
), "Failed to find passwd entry for root: %m");
835 r
= free_and_strdup(&arg_root_shell
, p
->pw_shell
);
840 r
= prompt_root_shell();
844 if (arg_copy_root_password
&& arg_root
) {
848 p
= getspnam("root");
850 return log_error_errno(errno_or_else(EIO
), "Failed to find shadow entry for root: %m");
852 r
= free_and_strdup(&arg_root_password
, p
->sp_pwdp
);
856 arg_root_password_is_hashed
= true;
859 r
= prompt_root_password();
863 if (arg_root_password
&& arg_root_password_is_hashed
) {
865 hashed_password
= arg_root_password
;
866 } else if (arg_root_password
) {
867 r
= hash_password(arg_root_password
, &_hashed_password
);
869 return log_error_errno(r
, "Failed to hash password: %m");
872 hashed_password
= _hashed_password
;
874 } else if (arg_delete_root_password
)
875 password
= hashed_password
= "";
877 password
= hashed_password
= "!";
879 r
= write_root_passwd(etc_passwd
, password
, arg_root_shell
);
881 return log_error_errno(r
, "Failed to write %s: %m", etc_passwd
);
883 log_info("%s written", etc_passwd
);
885 r
= write_root_shadow(etc_shadow
, hashed_password
);
887 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
889 log_info("%s written.", etc_shadow
);
893 static int process_kernel_cmdline(void) {
894 const char *etc_kernel_cmdline
;
897 etc_kernel_cmdline
= prefix_roota(arg_root
, "/etc/kernel/cmdline");
898 if (laccess(etc_kernel_cmdline
, F_OK
) >= 0 && !arg_force
)
901 if (!arg_kernel_cmdline
)
904 r
= write_string_file(etc_kernel_cmdline
, arg_kernel_cmdline
,
905 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
|
906 (arg_force
? WRITE_STRING_FILE_ATOMIC
: 0));
908 return log_error_errno(r
, "Failed to write %s: %m", etc_kernel_cmdline
);
910 log_info("%s written.", etc_kernel_cmdline
);
914 static int help(void) {
915 _cleanup_free_
char *link
= NULL
;
918 r
= terminal_urlify_man("systemd-firstboot", "1", &link
);
922 printf("%s [OPTIONS...]\n\n"
923 "Configures basic settings of the system.\n\n"
924 " -h --help Show this help\n"
925 " --version Show package version\n"
926 " --root=PATH Operate on an alternate filesystem root\n"
927 " --image=PATH Operate on an alternate filesystem image\n"
928 " --locale=LOCALE Set primary locale (LANG=)\n"
929 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
930 " --keymap=KEYMAP Set keymap\n"
931 " --timezone=TIMEZONE Set timezone\n"
932 " --hostname=NAME Set hostname\n"
933 " --machine-ID=ID Set machine ID\n"
934 " --root-password=PASSWORD Set root password from plaintext password\n"
935 " --root-password-file=FILE Set root password from file\n"
936 " --root-password-hashed=HASHED_PASSWORD Set root password from hashed password\n"
937 " --root-shell=SHELL Set root shell\n"
938 " --prompt-locale Prompt the user for locale settings\n"
939 " --prompt-keymap Prompt the user for keymap settings\n"
940 " --prompt-timezone Prompt the user for timezone\n"
941 " --prompt-hostname Prompt the user for hostname\n"
942 " --prompt-root-password Prompt the user for root password\n"
943 " --prompt-root-shell Prompt the user for root shell\n"
944 " --prompt Prompt for all of the above\n"
945 " --copy-locale Copy locale from host\n"
946 " --copy-keymap Copy keymap from host\n"
947 " --copy-timezone Copy timezone from host\n"
948 " --copy-root-password Copy root password from host\n"
949 " --copy-root-shell Copy root shell from host\n"
950 " --copy Copy locale, keymap, timezone, root password\n"
951 " --setup-machine-id Generate a new random machine ID\n"
952 " --force Overwrite existing files\n"
953 " --delete-root-password Delete root password\n"
954 " --welcome=no Disable the welcome text\n"
955 "\nSee the %s for details.\n",
956 program_invocation_short_name
,
962 static int parse_argv(int argc
, char *argv
[]) {
975 ARG_ROOT_PASSWORD_FILE
,
976 ARG_ROOT_PASSWORD_HASHED
,
978 ARG_KERNEL_COMMAND_LINE
,
984 ARG_PROMPT_ROOT_PASSWORD
,
985 ARG_PROMPT_ROOT_SHELL
,
990 ARG_COPY_ROOT_PASSWORD
,
992 ARG_SETUP_MACHINE_ID
,
994 ARG_DELETE_ROOT_PASSWORD
,
998 static const struct option options
[] = {
999 { "help", no_argument
, NULL
, 'h' },
1000 { "version", no_argument
, NULL
, ARG_VERSION
},
1001 { "root", required_argument
, NULL
, ARG_ROOT
},
1002 { "image", required_argument
, NULL
, ARG_IMAGE
},
1003 { "locale", required_argument
, NULL
, ARG_LOCALE
},
1004 { "locale-messages", required_argument
, NULL
, ARG_LOCALE_MESSAGES
},
1005 { "keymap", required_argument
, NULL
, ARG_KEYMAP
},
1006 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
1007 { "hostname", required_argument
, NULL
, ARG_HOSTNAME
},
1008 { "machine-id", required_argument
, NULL
, ARG_MACHINE_ID
},
1009 { "root-password", required_argument
, NULL
, ARG_ROOT_PASSWORD
},
1010 { "root-password-file", required_argument
, NULL
, ARG_ROOT_PASSWORD_FILE
},
1011 { "root-password-hashed", required_argument
, NULL
, ARG_ROOT_PASSWORD_HASHED
},
1012 { "root-shell", required_argument
, NULL
, ARG_ROOT_SHELL
},
1013 { "kernel-command-line", required_argument
, NULL
, ARG_KERNEL_COMMAND_LINE
},
1014 { "prompt", no_argument
, NULL
, ARG_PROMPT
},
1015 { "prompt-locale", no_argument
, NULL
, ARG_PROMPT_LOCALE
},
1016 { "prompt-keymap", no_argument
, NULL
, ARG_PROMPT_KEYMAP
},
1017 { "prompt-timezone", no_argument
, NULL
, ARG_PROMPT_TIMEZONE
},
1018 { "prompt-hostname", no_argument
, NULL
, ARG_PROMPT_HOSTNAME
},
1019 { "prompt-root-password", no_argument
, NULL
, ARG_PROMPT_ROOT_PASSWORD
},
1020 { "prompt-root-shell", no_argument
, NULL
, ARG_PROMPT_ROOT_SHELL
},
1021 { "copy", no_argument
, NULL
, ARG_COPY
},
1022 { "copy-locale", no_argument
, NULL
, ARG_COPY_LOCALE
},
1023 { "copy-keymap", no_argument
, NULL
, ARG_COPY_KEYMAP
},
1024 { "copy-timezone", no_argument
, NULL
, ARG_COPY_TIMEZONE
},
1025 { "copy-root-password", no_argument
, NULL
, ARG_COPY_ROOT_PASSWORD
},
1026 { "copy-root-shell", no_argument
, NULL
, ARG_COPY_ROOT_SHELL
},
1027 { "setup-machine-id", no_argument
, NULL
, ARG_SETUP_MACHINE_ID
},
1028 { "force", no_argument
, NULL
, ARG_FORCE
},
1029 { "delete-root-password", no_argument
, NULL
, ARG_DELETE_ROOT_PASSWORD
},
1030 { "welcome", required_argument
, NULL
, ARG_WELCOME
},
1039 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
1050 r
= parse_path_argument(optarg
, true, &arg_root
);
1056 r
= parse_path_argument(optarg
, false, &arg_image
);
1062 r
= free_and_strdup(&arg_locale
, optarg
);
1068 case ARG_LOCALE_MESSAGES
:
1069 r
= free_and_strdup(&arg_locale_messages
, optarg
);
1076 if (!keymap_is_valid(optarg
))
1077 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1078 "Keymap %s is not valid.", optarg
);
1080 r
= free_and_strdup(&arg_keymap
, optarg
);
1087 if (!timezone_is_valid(optarg
, LOG_ERR
))
1088 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1089 "Timezone %s is not valid.", optarg
);
1091 r
= free_and_strdup(&arg_timezone
, optarg
);
1097 case ARG_ROOT_PASSWORD
:
1098 r
= free_and_strdup(&arg_root_password
, optarg
);
1102 arg_root_password_is_hashed
= false;
1105 case ARG_ROOT_PASSWORD_FILE
:
1106 arg_root_password
= mfree(arg_root_password
);
1108 r
= read_one_line_file(optarg
, &arg_root_password
);
1110 return log_error_errno(r
, "Failed to read %s: %m", optarg
);
1112 arg_root_password_is_hashed
= false;
1115 case ARG_ROOT_PASSWORD_HASHED
:
1116 r
= free_and_strdup(&arg_root_password
, optarg
);
1120 arg_root_password_is_hashed
= true;
1123 case ARG_ROOT_SHELL
:
1124 r
= find_shell(optarg
, arg_root
);
1128 r
= free_and_strdup(&arg_root_shell
, optarg
);
1135 if (!hostname_is_valid(optarg
, VALID_HOSTNAME_TRAILING_DOT
))
1136 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1137 "Host name %s is not valid.", optarg
);
1139 r
= free_and_strdup(&arg_hostname
, optarg
);
1143 hostname_cleanup(arg_hostname
);
1146 case ARG_MACHINE_ID
:
1147 r
= sd_id128_from_string(optarg
, &arg_machine_id
);
1149 return log_error_errno(r
, "Failed to parse machine id %s.", optarg
);
1153 case ARG_KERNEL_COMMAND_LINE
:
1154 r
= free_and_strdup(&arg_kernel_cmdline
, optarg
);
1161 arg_prompt_locale
= arg_prompt_keymap
= arg_prompt_timezone
= arg_prompt_hostname
=
1162 arg_prompt_root_password
= arg_prompt_root_shell
= true;
1165 case ARG_PROMPT_LOCALE
:
1166 arg_prompt_locale
= true;
1169 case ARG_PROMPT_KEYMAP
:
1170 arg_prompt_keymap
= true;
1173 case ARG_PROMPT_TIMEZONE
:
1174 arg_prompt_timezone
= true;
1177 case ARG_PROMPT_HOSTNAME
:
1178 arg_prompt_hostname
= true;
1181 case ARG_PROMPT_ROOT_PASSWORD
:
1182 arg_prompt_root_password
= true;
1185 case ARG_PROMPT_ROOT_SHELL
:
1186 arg_prompt_root_shell
= true;
1190 arg_copy_locale
= arg_copy_keymap
= arg_copy_timezone
= arg_copy_root_password
=
1191 arg_copy_root_shell
= true;
1194 case ARG_COPY_LOCALE
:
1195 arg_copy_locale
= true;
1198 case ARG_COPY_KEYMAP
:
1199 arg_copy_keymap
= true;
1202 case ARG_COPY_TIMEZONE
:
1203 arg_copy_timezone
= true;
1206 case ARG_COPY_ROOT_PASSWORD
:
1207 arg_copy_root_password
= true;
1210 case ARG_COPY_ROOT_SHELL
:
1211 arg_copy_root_shell
= true;
1214 case ARG_SETUP_MACHINE_ID
:
1215 r
= sd_id128_randomize(&arg_machine_id
);
1217 return log_error_errno(r
, "Failed to generate randomized machine ID: %m");
1225 case ARG_DELETE_ROOT_PASSWORD
:
1226 arg_delete_root_password
= true;
1230 r
= parse_boolean(optarg
);
1232 return log_error_errno(r
, "Failed to parse --welcome= argument: %s", optarg
);
1241 assert_not_reached("Unhandled option");
1244 /* We check if the specified locale strings are valid down here, so that we can take --root= into
1245 * account when looking for the locale files. */
1247 if (arg_locale
&& !locale_is_ok(arg_locale
))
1248 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale %s is not installed.", arg_locale
);
1249 if (arg_locale_messages
&& !locale_is_ok(arg_locale_messages
))
1250 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale %s is not installed.", arg_locale_messages
);
1252 if (arg_delete_root_password
&& (arg_copy_root_password
|| arg_root_password
|| arg_prompt_root_password
))
1253 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1254 "--delete-root-password cannot be combined with other root password options");
1256 if (arg_image
&& arg_root
)
1257 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Please specify either --root= or --image=, the combination of both is not supported.");
1262 static int run(int argc
, char *argv
[]) {
1263 _cleanup_(loop_device_unrefp
) LoopDevice
*loop_device
= NULL
;
1264 _cleanup_(decrypted_image_unrefp
) DecryptedImage
*decrypted_image
= NULL
;
1265 _cleanup_(umount_and_rmdir_and_freep
) char *unlink_dir
= NULL
;
1268 r
= parse_argv(argc
, argv
);
1276 if (!arg_root
&& !arg_image
) {
1279 /* If we are called without --root=/--image= let's honour the systemd.firstboot kernel
1280 * command line option, because we are called to provision the host with basic settings (as
1281 * opposed to some other file system tree/image) */
1283 r
= proc_cmdline_get_bool("systemd.firstboot", &enabled
);
1285 return log_error_errno(r
, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
1286 if (r
> 0 && !enabled
)
1287 return 0; /* disabled */
1293 r
= mount_image_privately_interactively(
1295 DISSECT_IMAGE_REQUIRE_ROOT
|DISSECT_IMAGE_VALIDATE_OS
|DISSECT_IMAGE_RELAX_VAR_CHECK
|DISSECT_IMAGE_FSCK
,
1302 arg_root
= strdup(unlink_dir
);
1307 r
= process_locale();
1311 r
= process_keymap();
1315 r
= process_timezone();
1319 r
= process_hostname();
1323 r
= process_machine_id();
1327 r
= process_root_args();
1331 r
= process_kernel_cmdline();
1338 DEFINE_MAIN_FUNCTION(run
);