1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
10 #include "ask-password-api.h"
16 #include "hostname-util.h"
18 #include "libcrypt-util.h"
19 #include "locale-util.h"
20 #include "main-func.h"
21 #include "memory-util.h"
24 #include "parse-util.h"
25 #include "path-util.h"
26 #include "pretty-print.h"
27 #include "proc-cmdline.h"
28 #include "random-util.h"
29 #include "string-util.h"
31 #include "terminal-util.h"
32 #include "time-util.h"
33 #include "tmpfile-util-label.h"
34 #include "umask-util.h"
35 #include "user-util.h"
37 static char *arg_root
= NULL
;
38 static char *arg_locale
= NULL
; /* $LANG */
39 static char *arg_keymap
= NULL
;
40 static char *arg_locale_messages
= NULL
; /* $LC_MESSAGES */
41 static char *arg_timezone
= NULL
;
42 static char *arg_hostname
= NULL
;
43 static sd_id128_t arg_machine_id
= {};
44 static char *arg_root_password
= NULL
;
45 static char *arg_kernel_cmdline
= NULL
;
46 static bool arg_prompt_locale
= false;
47 static bool arg_prompt_keymap
= false;
48 static bool arg_prompt_timezone
= false;
49 static bool arg_prompt_hostname
= false;
50 static bool arg_prompt_root_password
= false;
51 static bool arg_copy_locale
= false;
52 static bool arg_copy_keymap
= false;
53 static bool arg_copy_timezone
= false;
54 static bool arg_copy_root_password
= false;
55 static bool arg_force
= false;
56 static bool arg_delete_root_password
= false;
57 static bool arg_root_password_is_hashed
= false;
59 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
60 STATIC_DESTRUCTOR_REGISTER(arg_locale
, freep
);
61 STATIC_DESTRUCTOR_REGISTER(arg_locale_messages
, freep
);
62 STATIC_DESTRUCTOR_REGISTER(arg_keymap
, freep
);
63 STATIC_DESTRUCTOR_REGISTER(arg_timezone
, freep
);
64 STATIC_DESTRUCTOR_REGISTER(arg_hostname
, freep
);
65 STATIC_DESTRUCTOR_REGISTER(arg_root_password
, erase_and_freep
);
67 static bool press_any_key(void) {
71 printf("-- Press any key to proceed --");
74 (void) read_one_char(stdin
, &k
, USEC_INFINITY
, &need_nl
);
82 static void print_welcome(void) {
83 _cleanup_free_
char *pretty_name
= NULL
, *ansi_color
= NULL
;
84 static bool done
= false;
93 "PRETTY_NAME", &pretty_name
,
94 "ANSI_COLOR", &ansi_color
,
97 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
,
98 "Failed to read os-release file, ignoring: %m");
100 pn
= isempty(pretty_name
) ? "Linux" : pretty_name
;
102 if (colors_enabled())
103 printf("\nWelcome to your new installation of \x1B[%sm%s\x1B[0m!\n", ansi_color
, pn
);
105 printf("\nWelcome to your new installation of %s!\n", pn
);
107 printf("\nPlease configure your system!\n\n");
114 static int show_menu(char **x
, unsigned n_columns
, unsigned width
, unsigned percentage
) {
115 unsigned break_lines
, break_modulo
;
116 size_t n
, per_column
, i
, j
;
118 assert(n_columns
> 0);
121 per_column
= DIV_ROUND_UP(n
, n_columns
);
123 break_lines
= lines();
127 /* The first page gets two extra lines, since we want to show
129 break_modulo
= break_lines
;
130 if (break_modulo
> 3)
133 for (i
= 0; i
< per_column
; i
++) {
135 for (j
= 0; j
< n_columns
; j
++) {
136 _cleanup_free_
char *e
= NULL
;
138 if (j
* per_column
+ i
>= n
)
141 e
= ellipsize(x
[j
* per_column
+ i
], width
, percentage
);
145 printf("%4zu) %-*s", j
* per_column
+ i
+ 1, width
, e
);
150 /* on the first screen we reserve 2 extra lines for the title */
151 if (i
% break_lines
== break_modulo
) {
152 if (!press_any_key())
160 static int prompt_loop(const char *text
, char **l
, unsigned percentage
, bool (*is_valid
)(const char *name
), char **ret
) {
168 _cleanup_free_
char *p
= NULL
;
171 r
= ask_string(&p
, "%s %s (empty to skip, \"list\" to list options): ",
172 special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), text
);
174 return log_error_errno(r
, "Failed to query user: %m");
177 log_warning("No data entered, skipping.");
181 if (streq(p
, "list")) {
182 r
= show_menu(l
, 3, 22, percentage
);
190 r
= safe_atou(p
, &u
);
192 if (u
<= 0 || u
> strv_length(l
)) {
193 log_error("Specified entry number out of range.");
197 log_info("Selected '%s'.", l
[u
-1]);
198 if (free_and_strdup(ret
, l
[u
-1]) < 0)
205 log_error("Entered data invalid.");
209 return free_and_replace(*ret
, p
);
213 static bool locale_is_ok(const char *name
) {
216 return locale_is_valid(name
);
218 return locale_is_installed(name
) > 0;
221 static int prompt_locale(void) {
222 _cleanup_strv_free_
char **locales
= NULL
;
225 if (arg_locale
|| arg_locale_messages
)
228 if (!arg_prompt_locale
)
231 r
= get_locales(&locales
);
233 return log_error_errno(r
, "Cannot query locales list: %m");
235 if (strv_isempty(locales
))
236 log_debug("No locales found, skipping locale selection.");
237 else if (strv_length(locales
) == 1) {
239 if (streq(locales
[0], SYSTEMD_DEFAULT_LOCALE
))
240 log_debug("Only installed locale is default locale anyway, not setting locale explicitly.");
242 log_debug("Only a single locale available (%s), selecting it as default.", locales
[0]);
244 arg_locale
= strdup(locales
[0]);
248 /* Not setting arg_locale_message here, since it defaults to LANG anyway */
253 r
= prompt_loop("Please enter system locale name or number",
254 locales
, 60, locale_is_ok
, &arg_locale
);
258 if (isempty(arg_locale
))
261 r
= prompt_loop("Please enter system message locale name or number",
262 locales
, 60, locale_is_ok
, &arg_locale_messages
);
266 /* Suppress the messages setting if it's the same as the main locale anyway */
267 if (streq_ptr(arg_locale
, arg_locale_messages
))
268 arg_locale_messages
= mfree(arg_locale_messages
);
274 static int process_locale(void) {
275 const char *etc_localeconf
;
280 etc_localeconf
= prefix_roota(arg_root
, "/etc/locale.conf");
281 if (laccess(etc_localeconf
, F_OK
) >= 0 && !arg_force
)
284 if (arg_copy_locale
&& arg_root
) {
286 (void) mkdir_parents(etc_localeconf
, 0755);
287 r
= copy_file("/etc/locale.conf", etc_localeconf
, 0, 0644, 0, 0, COPY_REFLINK
);
290 return log_error_errno(r
, "Failed to copy %s: %m", etc_localeconf
);
292 log_info("%s copied.", etc_localeconf
);
301 if (!isempty(arg_locale
))
302 locales
[i
++] = strjoina("LANG=", arg_locale
);
303 if (!isempty(arg_locale_messages
) && !streq(arg_locale_messages
, arg_locale
))
304 locales
[i
++] = strjoina("LC_MESSAGES=", arg_locale_messages
);
311 (void) mkdir_parents(etc_localeconf
, 0755);
312 r
= write_env_file(etc_localeconf
, locales
);
314 return log_error_errno(r
, "Failed to write %s: %m", etc_localeconf
);
316 log_info("%s written.", etc_localeconf
);
320 static int prompt_keymap(void) {
321 _cleanup_strv_free_
char **kmaps
= NULL
;
327 if (!arg_prompt_keymap
)
330 r
= get_keymaps(&kmaps
);
331 if (r
== -ENOENT
) /* no keymaps installed */
334 return log_error_errno(r
, "Failed to read keymaps: %m");
338 return prompt_loop("Please enter system keymap name or number",
339 kmaps
, 60, keymap_is_valid
, &arg_keymap
);
342 static int process_keymap(void) {
343 const char *etc_vconsoleconf
;
347 etc_vconsoleconf
= prefix_roota(arg_root
, "/etc/vconsole.conf");
348 if (laccess(etc_vconsoleconf
, F_OK
) >= 0 && !arg_force
)
351 if (arg_copy_keymap
&& arg_root
) {
353 (void) mkdir_parents(etc_vconsoleconf
, 0755);
354 r
= copy_file("/etc/vconsole.conf", etc_vconsoleconf
, 0, 0644, 0, 0, COPY_REFLINK
);
357 return log_error_errno(r
, "Failed to copy %s: %m", etc_vconsoleconf
);
359 log_info("%s copied.", etc_vconsoleconf
);
366 return 0; /* don't fail if no keymaps are installed */
370 if (isempty(arg_keymap
))
373 keymap
= STRV_MAKE(strjoina("KEYMAP=", arg_keymap
));
375 r
= mkdir_parents(etc_vconsoleconf
, 0755);
377 return log_error_errno(r
, "Failed to create the parent directory of %s: %m", etc_vconsoleconf
);
379 r
= write_env_file(etc_vconsoleconf
, keymap
);
381 return log_error_errno(r
, "Failed to write %s: %m", etc_vconsoleconf
);
383 log_info("%s written.", etc_vconsoleconf
);
387 static bool timezone_is_valid_log_error(const char *name
) {
388 return timezone_is_valid(name
, LOG_ERR
);
391 static int prompt_timezone(void) {
392 _cleanup_strv_free_
char **zones
= NULL
;
398 if (!arg_prompt_timezone
)
401 r
= get_timezones(&zones
);
403 return log_error_errno(r
, "Cannot query timezone list: %m");
407 r
= prompt_loop("Please enter timezone name or number",
408 zones
, 30, timezone_is_valid_log_error
, &arg_timezone
);
415 static int process_timezone(void) {
416 const char *etc_localtime
, *e
;
419 etc_localtime
= prefix_roota(arg_root
, "/etc/localtime");
420 if (laccess(etc_localtime
, F_OK
) >= 0 && !arg_force
)
423 if (arg_copy_timezone
&& arg_root
) {
424 _cleanup_free_
char *p
= NULL
;
426 r
= readlink_malloc("/etc/localtime", &p
);
429 return log_error_errno(r
, "Failed to read host timezone: %m");
431 (void) mkdir_parents(etc_localtime
, 0755);
432 if (symlink(p
, etc_localtime
) < 0)
433 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
435 log_info("%s copied.", etc_localtime
);
440 r
= prompt_timezone();
444 if (isempty(arg_timezone
))
447 e
= strjoina("../usr/share/zoneinfo/", arg_timezone
);
449 (void) mkdir_parents(etc_localtime
, 0755);
450 if (symlink(e
, etc_localtime
) < 0)
451 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
453 log_info("%s written", etc_localtime
);
457 static int prompt_hostname(void) {
463 if (!arg_prompt_hostname
)
470 _cleanup_free_
char *h
= NULL
;
472 r
= ask_string(&h
, "%s Please enter hostname for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
));
474 return log_error_errno(r
, "Failed to query hostname: %m");
477 log_warning("No hostname entered, skipping.");
481 if (!hostname_is_valid(h
, true)) {
482 log_error("Specified hostname invalid.");
486 /* Get rid of the trailing dot that we allow, but don't want to see */
487 arg_hostname
= hostname_cleanup(h
);
495 static int process_hostname(void) {
496 const char *etc_hostname
;
499 etc_hostname
= prefix_roota(arg_root
, "/etc/hostname");
500 if (laccess(etc_hostname
, F_OK
) >= 0 && !arg_force
)
503 r
= prompt_hostname();
507 if (isempty(arg_hostname
))
510 r
= write_string_file(etc_hostname
, arg_hostname
,
511 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
|
512 (arg_force
? WRITE_STRING_FILE_ATOMIC
: 0));
514 return log_error_errno(r
, "Failed to write %s: %m", etc_hostname
);
516 log_info("%s written.", etc_hostname
);
520 static int process_machine_id(void) {
521 const char *etc_machine_id
;
522 char id
[SD_ID128_STRING_MAX
];
525 etc_machine_id
= prefix_roota(arg_root
, "/etc/machine-id");
526 if (laccess(etc_machine_id
, F_OK
) >= 0 && !arg_force
)
529 if (sd_id128_is_null(arg_machine_id
))
532 r
= write_string_file(etc_machine_id
, sd_id128_to_string(arg_machine_id
, id
),
533 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
|
534 (arg_force
? WRITE_STRING_FILE_ATOMIC
: 0));
536 return log_error_errno(r
, "Failed to write machine id: %m");
538 log_info("%s written.", etc_machine_id
);
542 static int prompt_root_password(void) {
543 const char *msg1
, *msg2
;
546 if (arg_root_password
)
549 if (!arg_prompt_root_password
)
555 msg1
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter a new root password (empty to skip):");
556 msg2
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter new root password again:");
559 _cleanup_strv_free_erase_
char **a
= NULL
, **b
= NULL
;
561 r
= ask_password_tty(-1, msg1
, NULL
, 0, 0, NULL
, &a
);
563 return log_error_errno(r
, "Failed to query root password: %m");
564 if (strv_length(a
) != 1)
565 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
566 "Received multiple passwords, where we expected one.");
569 log_warning("No password entered, skipping.");
573 r
= ask_password_tty(-1, msg2
, NULL
, 0, 0, NULL
, &b
);
575 return log_error_errno(r
, "Failed to query root password: %m");
576 if (strv_length(b
) != 1)
577 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
578 "Received multiple passwords, where we expected one.");
580 if (!streq(*a
, *b
)) {
581 log_error("Entered passwords did not match, please try again.");
585 arg_root_password
= TAKE_PTR(*a
);
592 static int write_root_passwd(const char *passwd_path
, const char *password
) {
593 _cleanup_fclose_
FILE *original
= NULL
, *passwd
= NULL
;
594 _cleanup_(unlink_and_freep
) char *passwd_tmp
= NULL
;
597 r
= fopen_temporary_label("/etc/passwd", passwd_path
, &passwd
, &passwd_tmp
);
601 original
= fopen(passwd_path
, "re");
605 r
= sync_rights(fileno(original
), fileno(passwd
));
609 while ((r
= fgetpwent_sane(original
, &i
)) > 0) {
611 if (streq(i
->pw_name
, "root"))
612 i
->pw_passwd
= (char *) password
;
614 r
= putpwent_sane(i
, passwd
);
622 struct passwd root
= {
623 .pw_name
= (char *) "root",
624 .pw_passwd
= (char *) password
,
627 .pw_gecos
= (char *) "Super User",
628 .pw_dir
= (char *) "/root",
629 .pw_shell
= (char *) "/bin/sh",
635 r
= fchmod(fileno(passwd
), 0000);
639 r
= putpwent_sane(&root
, passwd
);
644 r
= fflush_sync_and_check(passwd
);
648 r
= rename_and_apply_smack_floor_label(passwd_tmp
, passwd_path
);
655 static int write_root_shadow(const char *shadow_path
, const char *hashed_password
) {
656 _cleanup_fclose_
FILE *original
= NULL
, *shadow
= NULL
;
657 _cleanup_(unlink_and_freep
) char *shadow_tmp
= NULL
;
660 r
= fopen_temporary_label("/etc/shadow", shadow_path
, &shadow
, &shadow_tmp
);
664 original
= fopen(shadow_path
, "re");
668 r
= sync_rights(fileno(original
), fileno(shadow
));
672 while ((r
= fgetspent_sane(original
, &i
)) > 0) {
674 if (streq(i
->sp_namp
, "root")) {
675 i
->sp_pwdp
= (char *) hashed_password
;
676 i
->sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
);
679 r
= putspent_sane(i
, shadow
);
688 .sp_namp
= (char*) "root",
689 .sp_pwdp
= (char *) hashed_password
,
690 .sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
),
696 .sp_flag
= (unsigned long) -1, /* this appears to be what everybody does ... */
702 r
= fchmod(fileno(shadow
), 0000);
706 r
= putspent_sane(&root
, shadow
);
711 r
= fflush_sync_and_check(shadow
);
715 r
= rename_and_apply_smack_floor_label(shadow_tmp
, shadow_path
);
722 static int process_root_password(void) {
723 _cleanup_close_
int lock
= -1;
724 struct crypt_data cd
= {};
725 const char *hashed_password
;
726 const char *etc_shadow
;
729 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
730 if (laccess(etc_shadow
, F_OK
) >= 0 && !arg_force
)
733 (void) mkdir_parents(etc_shadow
, 0755);
735 lock
= take_etc_passwd_lock(arg_root
);
737 return log_error_errno(lock
, "Failed to take a lock: %m");
739 if (arg_delete_root_password
) {
740 const char *etc_passwd
;
742 /* Mixing alloca() and other stuff that touches the stack in one expression is not portable. */
743 etc_passwd
= prefix_roota(arg_root
, "/etc/passwd");
745 r
= write_root_passwd(etc_passwd
, "");
747 return log_error_errno(r
, "Failed to write %s: %m", etc_passwd
);
749 log_info("%s written", etc_passwd
);
754 if (arg_copy_root_password
&& arg_root
) {
758 p
= getspnam("root");
759 if (p
|| errno
!= ENOENT
) {
764 return log_error_errno(errno
, "Failed to find shadow entry for root: %m");
767 r
= write_root_shadow(etc_shadow
, p
->sp_pwdp
);
769 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
771 log_info("%s copied.", etc_shadow
);
776 r
= prompt_root_password();
780 if (!arg_root_password
)
783 if (arg_root_password_is_hashed
)
784 hashed_password
= arg_root_password
;
786 _cleanup_free_
char *salt
= NULL
;
787 /* hashed_password points inside cd after crypt_r returns so cd has function scope. */
789 r
= make_salt(&salt
);
791 return log_error_errno(r
, "Failed to get salt: %m");
794 hashed_password
= crypt_r(arg_root_password
, salt
, &cd
);
795 if (!hashed_password
)
796 return log_error_errno(errno
== 0 ? SYNTHETIC_ERRNO(EINVAL
) : errno
,
797 "Failed to encrypt password: %m");
800 r
= write_root_shadow(etc_shadow
, hashed_password
);
802 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
804 log_info("%s written.", etc_shadow
);
808 static int process_kernel_cmdline(void) {
809 const char *etc_kernel_cmdline
;
812 etc_kernel_cmdline
= prefix_roota(arg_root
, "/etc/kernel/cmdline");
813 if (laccess(etc_kernel_cmdline
, F_OK
) >= 0 && !arg_force
)
816 if (!arg_kernel_cmdline
)
819 r
= write_string_file(etc_kernel_cmdline
, arg_kernel_cmdline
,
820 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
|
821 (arg_force
? WRITE_STRING_FILE_ATOMIC
: 0));
823 return log_error_errno(r
, "Failed to write %s: %m", etc_kernel_cmdline
);
825 log_info("%s written.", etc_kernel_cmdline
);
829 static int help(void) {
830 _cleanup_free_
char *link
= NULL
;
833 r
= terminal_urlify_man("systemd-firstboot", "1", &link
);
837 printf("%s [OPTIONS...]\n\n"
838 "Configures basic settings of the system.\n\n"
839 " -h --help Show this help\n"
840 " --version Show package version\n"
841 " --root=PATH Operate on an alternate filesystem root\n"
842 " --locale=LOCALE Set primary locale (LANG=)\n"
843 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
844 " --keymap=KEYMAP Set keymap\n"
845 " --timezone=TIMEZONE Set timezone\n"
846 " --hostname=NAME Set hostname\n"
847 " --machine-ID=ID Set machine ID\n"
848 " --root-password=PASSWORD Set root password from plaintext password\n"
849 " --root-password-file=FILE Set root password from file\n"
850 " --root-password-hashed=HASHED_PASSWORD Set root password from hashed password\n"
851 " --prompt-locale Prompt the user for locale settings\n"
852 " --prompt-keymap Prompt the user for keymap settings\n"
853 " --prompt-timezone Prompt the user for timezone\n"
854 " --prompt-hostname Prompt the user for hostname\n"
855 " --prompt-root-password Prompt the user for root password\n"
856 " --prompt Prompt for all of the above\n"
857 " --copy-locale Copy locale from host\n"
858 " --copy-keymap Copy keymap from host\n"
859 " --copy-timezone Copy timezone from host\n"
860 " --copy-root-password Copy root password from host\n"
861 " --copy Copy locale, keymap, timezone, root password\n"
862 " --setup-machine-id Generate a new random machine ID\n"
863 " --force Overwrite existing files\n"
864 " --delete-root-password Delete root password\n"
865 "\nSee the %s for details.\n"
866 , program_invocation_short_name
873 static int parse_argv(int argc
, char *argv
[]) {
885 ARG_ROOT_PASSWORD_FILE
,
886 ARG_ROOT_PASSWORD_HASHED
,
887 ARG_KERNEL_COMMAND_LINE
,
893 ARG_PROMPT_ROOT_PASSWORD
,
898 ARG_COPY_ROOT_PASSWORD
,
899 ARG_SETUP_MACHINE_ID
,
901 ARG_DELETE_ROOT_PASSWORD
,
904 static const struct option options
[] = {
905 { "help", no_argument
, NULL
, 'h' },
906 { "version", no_argument
, NULL
, ARG_VERSION
},
907 { "root", required_argument
, NULL
, ARG_ROOT
},
908 { "locale", required_argument
, NULL
, ARG_LOCALE
},
909 { "locale-messages", required_argument
, NULL
, ARG_LOCALE_MESSAGES
},
910 { "keymap", required_argument
, NULL
, ARG_KEYMAP
},
911 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
912 { "hostname", required_argument
, NULL
, ARG_HOSTNAME
},
913 { "machine-id", required_argument
, NULL
, ARG_MACHINE_ID
},
914 { "root-password", required_argument
, NULL
, ARG_ROOT_PASSWORD
},
915 { "root-password-file", required_argument
, NULL
, ARG_ROOT_PASSWORD_FILE
},
916 { "root-password-hashed", required_argument
, NULL
, ARG_ROOT_PASSWORD_HASHED
},
917 { "kernel-command-line", required_argument
, NULL
, ARG_KERNEL_COMMAND_LINE
},
918 { "prompt", no_argument
, NULL
, ARG_PROMPT
},
919 { "prompt-locale", no_argument
, NULL
, ARG_PROMPT_LOCALE
},
920 { "prompt-keymap", no_argument
, NULL
, ARG_PROMPT_KEYMAP
},
921 { "prompt-timezone", no_argument
, NULL
, ARG_PROMPT_TIMEZONE
},
922 { "prompt-hostname", no_argument
, NULL
, ARG_PROMPT_HOSTNAME
},
923 { "prompt-root-password", no_argument
, NULL
, ARG_PROMPT_ROOT_PASSWORD
},
924 { "copy", no_argument
, NULL
, ARG_COPY
},
925 { "copy-locale", no_argument
, NULL
, ARG_COPY_LOCALE
},
926 { "copy-keymap", no_argument
, NULL
, ARG_COPY_KEYMAP
},
927 { "copy-timezone", no_argument
, NULL
, ARG_COPY_TIMEZONE
},
928 { "copy-root-password", no_argument
, NULL
, ARG_COPY_ROOT_PASSWORD
},
929 { "setup-machine-id", no_argument
, NULL
, ARG_SETUP_MACHINE_ID
},
930 { "force", no_argument
, NULL
, ARG_FORCE
},
931 { "delete-root-password", no_argument
, NULL
, ARG_DELETE_ROOT_PASSWORD
},
940 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
951 r
= parse_path_argument_and_warn(optarg
, true, &arg_root
);
957 r
= free_and_strdup(&arg_locale
, optarg
);
963 case ARG_LOCALE_MESSAGES
:
964 r
= free_and_strdup(&arg_locale_messages
, optarg
);
971 if (!keymap_is_valid(optarg
))
972 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
973 "Keymap %s is not valid.", optarg
);
975 r
= free_and_strdup(&arg_keymap
, optarg
);
982 if (!timezone_is_valid(optarg
, LOG_ERR
))
983 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
984 "Timezone %s is not valid.", optarg
);
986 r
= free_and_strdup(&arg_timezone
, optarg
);
992 case ARG_ROOT_PASSWORD
:
993 r
= free_and_strdup(&arg_root_password
, optarg
);
997 arg_root_password_is_hashed
= false;
1000 case ARG_ROOT_PASSWORD_FILE
:
1001 arg_root_password
= mfree(arg_root_password
);
1003 r
= read_one_line_file(optarg
, &arg_root_password
);
1005 return log_error_errno(r
, "Failed to read %s: %m", optarg
);
1007 arg_root_password_is_hashed
= false;
1010 case ARG_ROOT_PASSWORD_HASHED
:
1011 r
= free_and_strdup(&arg_root_password
, optarg
);
1015 arg_root_password_is_hashed
= true;
1019 if (!hostname_is_valid(optarg
, true))
1020 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1021 "Host name %s is not valid.", optarg
);
1023 hostname_cleanup(optarg
);
1024 r
= free_and_strdup(&arg_hostname
, optarg
);
1030 case ARG_MACHINE_ID
:
1031 if (sd_id128_from_string(optarg
, &arg_machine_id
) < 0)
1032 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1033 "Failed to parse machine id %s.", optarg
);
1037 case ARG_KERNEL_COMMAND_LINE
:
1038 r
= free_and_strdup(&arg_kernel_cmdline
, optarg
);
1045 arg_prompt_locale
= arg_prompt_keymap
= arg_prompt_timezone
= arg_prompt_hostname
= arg_prompt_root_password
= true;
1048 case ARG_PROMPT_LOCALE
:
1049 arg_prompt_locale
= true;
1052 case ARG_PROMPT_KEYMAP
:
1053 arg_prompt_keymap
= true;
1056 case ARG_PROMPT_TIMEZONE
:
1057 arg_prompt_timezone
= true;
1060 case ARG_PROMPT_HOSTNAME
:
1061 arg_prompt_hostname
= true;
1064 case ARG_PROMPT_ROOT_PASSWORD
:
1065 arg_prompt_root_password
= true;
1069 arg_copy_locale
= arg_copy_keymap
= arg_copy_timezone
= arg_copy_root_password
= true;
1072 case ARG_COPY_LOCALE
:
1073 arg_copy_locale
= true;
1076 case ARG_COPY_KEYMAP
:
1077 arg_copy_keymap
= true;
1080 case ARG_COPY_TIMEZONE
:
1081 arg_copy_timezone
= true;
1084 case ARG_COPY_ROOT_PASSWORD
:
1085 arg_copy_root_password
= true;
1088 case ARG_SETUP_MACHINE_ID
:
1090 r
= sd_id128_randomize(&arg_machine_id
);
1092 return log_error_errno(r
, "Failed to generate randomized machine ID: %m");
1100 case ARG_DELETE_ROOT_PASSWORD
:
1101 arg_delete_root_password
= true;
1108 assert_not_reached("Unhandled option");
1111 /* We check if the specified locale strings are valid down here, so that we can take --root= into
1112 * account when looking for the locale files. */
1114 if (arg_locale
&& !locale_is_ok(arg_locale
))
1115 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale %s is not installed.", arg_locale
);
1116 if (arg_locale_messages
&& !locale_is_ok(arg_locale_messages
))
1117 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale %s is not installed.", arg_locale_messages
);
1119 if (arg_delete_root_password
&& (arg_copy_root_password
|| arg_root_password
|| arg_prompt_root_password
))
1120 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1121 "--delete-root-password cannot be combined with other root password options");
1126 static int run(int argc
, char *argv
[]) {
1130 r
= parse_argv(argc
, argv
);
1134 log_setup_service();
1138 r
= proc_cmdline_get_bool("systemd.firstboot", &enabled
);
1140 return log_error_errno(r
, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
1141 if (r
> 0 && !enabled
)
1142 return 0; /* disabled */
1144 r
= process_locale();
1148 r
= process_keymap();
1152 r
= process_timezone();
1156 r
= process_hostname();
1160 r
= process_machine_id();
1164 r
= process_root_password();
1168 r
= process_kernel_cmdline();
1175 DEFINE_MAIN_FUNCTION(run
);