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 bool arg_prompt_locale
= false;
46 static bool arg_prompt_keymap
= false;
47 static bool arg_prompt_timezone
= false;
48 static bool arg_prompt_hostname
= false;
49 static bool arg_prompt_root_password
= false;
50 static bool arg_copy_locale
= false;
51 static bool arg_copy_keymap
= false;
52 static bool arg_copy_timezone
= false;
53 static bool arg_copy_root_password
= false;
54 static bool arg_force
= false;
55 static bool arg_delete_root_password
= false;
57 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
58 STATIC_DESTRUCTOR_REGISTER(arg_locale
, freep
);
59 STATIC_DESTRUCTOR_REGISTER(arg_locale_messages
, freep
);
60 STATIC_DESTRUCTOR_REGISTER(arg_keymap
, freep
);
61 STATIC_DESTRUCTOR_REGISTER(arg_timezone
, freep
);
62 STATIC_DESTRUCTOR_REGISTER(arg_hostname
, freep
);
63 STATIC_DESTRUCTOR_REGISTER(arg_root_password
, erase_and_freep
);
65 static bool press_any_key(void) {
69 printf("-- Press any key to proceed --");
72 (void) read_one_char(stdin
, &k
, USEC_INFINITY
, &need_nl
);
80 static void print_welcome(void) {
81 _cleanup_free_
char *pretty_name
= NULL
, *ansi_color
= NULL
;
82 static bool done
= false;
91 "PRETTY_NAME", &pretty_name
,
92 "ANSI_COLOR", &ansi_color
,
95 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
,
96 "Failed to read os-release file, ignoring: %m");
98 pn
= isempty(pretty_name
) ? "Linux" : pretty_name
;
100 if (colors_enabled())
101 printf("\nWelcome to your new installation of \x1B[%sm%s\x1B[0m!\n", ansi_color
, pn
);
103 printf("\nWelcome to your new installation of %s!\n", pn
);
105 printf("\nPlease configure your system!\n\n");
112 static int show_menu(char **x
, unsigned n_columns
, unsigned width
, unsigned percentage
) {
113 unsigned break_lines
, break_modulo
;
114 size_t n
, per_column
, i
, j
;
116 assert(n_columns
> 0);
119 per_column
= DIV_ROUND_UP(n
, n_columns
);
121 break_lines
= lines();
125 /* The first page gets two extra lines, since we want to show
127 break_modulo
= break_lines
;
128 if (break_modulo
> 3)
131 for (i
= 0; i
< per_column
; i
++) {
133 for (j
= 0; j
< n_columns
; j
++) {
134 _cleanup_free_
char *e
= NULL
;
136 if (j
* per_column
+ i
>= n
)
139 e
= ellipsize(x
[j
* per_column
+ i
], width
, percentage
);
143 printf("%4zu) %-*s", j
* per_column
+ i
+ 1, width
, e
);
148 /* on the first screen we reserve 2 extra lines for the title */
149 if (i
% break_lines
== break_modulo
) {
150 if (!press_any_key())
158 static int prompt_loop(const char *text
, char **l
, unsigned percentage
, bool (*is_valid
)(const char *name
), char **ret
) {
166 _cleanup_free_
char *p
= NULL
;
169 r
= ask_string(&p
, "%s %s (empty to skip, \"list\" to list options): ",
170 special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), text
);
172 return log_error_errno(r
, "Failed to query user: %m");
175 log_warning("No data entered, skipping.");
179 if (streq(p
, "list")) {
180 r
= show_menu(l
, 3, 22, percentage
);
188 r
= safe_atou(p
, &u
);
190 if (u
<= 0 || u
> strv_length(l
)) {
191 log_error("Specified entry number out of range.");
195 log_info("Selected '%s'.", l
[u
-1]);
196 if (free_and_strdup(ret
, l
[u
-1]) < 0)
203 log_error("Entered data invalid.");
207 return free_and_replace(*ret
, p
);
211 static bool locale_is_ok(const char *name
) {
214 return locale_is_valid(name
);
216 return locale_is_installed(name
) > 0;
219 static int prompt_locale(void) {
220 _cleanup_strv_free_
char **locales
= NULL
;
223 if (arg_locale
|| arg_locale_messages
)
226 if (!arg_prompt_locale
)
229 r
= get_locales(&locales
);
231 return log_error_errno(r
, "Cannot query locales list: %m");
233 if (strv_isempty(locales
))
234 log_debug("No locales found, skipping locale selection.");
235 else if (strv_length(locales
) == 1) {
237 if (streq(locales
[0], SYSTEMD_DEFAULT_LOCALE
))
238 log_debug("Only installed locale is default locale anyway, not setting locale explicitly.");
240 log_debug("Only a single locale available (%s), selecting it as default.", locales
[0]);
242 arg_locale
= strdup(locales
[0]);
246 /* Not setting arg_locale_message here, since it defaults to LANG anyway */
251 r
= prompt_loop("Please enter system locale name or number",
252 locales
, 60, locale_is_ok
, &arg_locale
);
256 if (isempty(arg_locale
))
259 r
= prompt_loop("Please enter system message locale name or number",
260 locales
, 60, locale_is_ok
, &arg_locale_messages
);
264 /* Suppress the messages setting if it's the same as the main locale anyway */
265 if (streq_ptr(arg_locale
, arg_locale_messages
))
266 arg_locale_messages
= mfree(arg_locale_messages
);
272 static int process_locale(void) {
273 const char *etc_localeconf
;
278 etc_localeconf
= prefix_roota(arg_root
, "/etc/locale.conf");
279 if (laccess(etc_localeconf
, F_OK
) >= 0 && !arg_force
)
282 if (arg_copy_locale
&& arg_root
) {
284 (void) mkdir_parents(etc_localeconf
, 0755);
285 r
= copy_file("/etc/locale.conf", etc_localeconf
, 0, 0644, 0, 0, COPY_REFLINK
);
288 return log_error_errno(r
, "Failed to copy %s: %m", etc_localeconf
);
290 log_info("%s copied.", etc_localeconf
);
299 if (!isempty(arg_locale
))
300 locales
[i
++] = strjoina("LANG=", arg_locale
);
301 if (!isempty(arg_locale_messages
) && !streq(arg_locale_messages
, arg_locale
))
302 locales
[i
++] = strjoina("LC_MESSAGES=", arg_locale_messages
);
309 (void) mkdir_parents(etc_localeconf
, 0755);
310 r
= write_env_file(etc_localeconf
, locales
);
312 return log_error_errno(r
, "Failed to write %s: %m", etc_localeconf
);
314 log_info("%s written.", etc_localeconf
);
318 static int prompt_keymap(void) {
319 _cleanup_strv_free_
char **kmaps
= NULL
;
325 if (!arg_prompt_keymap
)
328 r
= get_keymaps(&kmaps
);
329 if (r
== -ENOENT
) /* no keymaps installed */
332 return log_error_errno(r
, "Failed to read keymaps: %m");
336 return prompt_loop("Please enter system keymap name or number",
337 kmaps
, 60, keymap_is_valid
, &arg_keymap
);
340 static int process_keymap(void) {
341 const char *etc_vconsoleconf
;
345 etc_vconsoleconf
= prefix_roota(arg_root
, "/etc/vconsole.conf");
346 if (laccess(etc_vconsoleconf
, F_OK
) >= 0 && !arg_force
)
349 if (arg_copy_keymap
&& arg_root
) {
351 (void) mkdir_parents(etc_vconsoleconf
, 0755);
352 r
= copy_file("/etc/vconsole.conf", etc_vconsoleconf
, 0, 0644, 0, 0, COPY_REFLINK
);
355 return log_error_errno(r
, "Failed to copy %s: %m", etc_vconsoleconf
);
357 log_info("%s copied.", etc_vconsoleconf
);
364 return 0; /* don't fail if no keymaps are installed */
368 if (isempty(arg_keymap
))
371 keymap
= STRV_MAKE(strjoina("KEYMAP=", arg_keymap
));
373 r
= mkdir_parents(etc_vconsoleconf
, 0755);
375 return log_error_errno(r
, "Failed to create the parent directory of %s: %m", etc_vconsoleconf
);
377 r
= write_env_file(etc_vconsoleconf
, keymap
);
379 return log_error_errno(r
, "Failed to write %s: %m", etc_vconsoleconf
);
381 log_info("%s written.", etc_vconsoleconf
);
385 static bool timezone_is_valid_log_error(const char *name
) {
386 return timezone_is_valid(name
, LOG_ERR
);
389 static int prompt_timezone(void) {
390 _cleanup_strv_free_
char **zones
= NULL
;
396 if (!arg_prompt_timezone
)
399 r
= get_timezones(&zones
);
401 return log_error_errno(r
, "Cannot query timezone list: %m");
405 r
= prompt_loop("Please enter timezone name or number",
406 zones
, 30, timezone_is_valid_log_error
, &arg_timezone
);
413 static int process_timezone(void) {
414 const char *etc_localtime
, *e
;
417 etc_localtime
= prefix_roota(arg_root
, "/etc/localtime");
418 if (laccess(etc_localtime
, F_OK
) >= 0 && !arg_force
)
421 if (arg_copy_timezone
&& arg_root
) {
422 _cleanup_free_
char *p
= NULL
;
424 r
= readlink_malloc("/etc/localtime", &p
);
427 return log_error_errno(r
, "Failed to read host timezone: %m");
429 (void) mkdir_parents(etc_localtime
, 0755);
430 if (symlink(p
, etc_localtime
) < 0)
431 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
433 log_info("%s copied.", etc_localtime
);
438 r
= prompt_timezone();
442 if (isempty(arg_timezone
))
445 e
= strjoina("../usr/share/zoneinfo/", arg_timezone
);
447 (void) mkdir_parents(etc_localtime
, 0755);
448 if (symlink(e
, etc_localtime
) < 0)
449 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
451 log_info("%s written", etc_localtime
);
455 static int prompt_hostname(void) {
461 if (!arg_prompt_hostname
)
468 _cleanup_free_
char *h
= NULL
;
470 r
= ask_string(&h
, "%s Please enter hostname for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
));
472 return log_error_errno(r
, "Failed to query hostname: %m");
475 log_warning("No hostname entered, skipping.");
479 if (!hostname_is_valid(h
, true)) {
480 log_error("Specified hostname invalid.");
484 /* Get rid of the trailing dot that we allow, but don't want to see */
485 arg_hostname
= hostname_cleanup(h
);
493 static int process_hostname(void) {
494 const char *etc_hostname
;
497 etc_hostname
= prefix_roota(arg_root
, "/etc/hostname");
498 if (laccess(etc_hostname
, F_OK
) >= 0 && !arg_force
)
501 r
= prompt_hostname();
505 if (isempty(arg_hostname
))
508 r
= write_string_file(etc_hostname
, arg_hostname
,
509 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
|
510 (arg_force
? WRITE_STRING_FILE_ATOMIC
: 0));
512 return log_error_errno(r
, "Failed to write %s: %m", etc_hostname
);
514 log_info("%s written.", etc_hostname
);
518 static int process_machine_id(void) {
519 const char *etc_machine_id
;
520 char id
[SD_ID128_STRING_MAX
];
523 etc_machine_id
= prefix_roota(arg_root
, "/etc/machine-id");
524 if (laccess(etc_machine_id
, F_OK
) >= 0 && !arg_force
)
527 if (sd_id128_is_null(arg_machine_id
))
530 r
= write_string_file(etc_machine_id
, sd_id128_to_string(arg_machine_id
, id
),
531 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
|
532 (arg_force
? WRITE_STRING_FILE_ATOMIC
: 0));
534 return log_error_errno(r
, "Failed to write machine id: %m");
536 log_info("%s written.", etc_machine_id
);
540 static int prompt_root_password(void) {
541 const char *msg1
, *msg2
;
544 if (arg_root_password
)
547 if (!arg_prompt_root_password
)
553 msg1
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter a new root password (empty to skip):");
554 msg2
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter new root password again:");
557 _cleanup_strv_free_erase_
char **a
= NULL
, **b
= NULL
;
559 r
= ask_password_tty(-1, msg1
, NULL
, 0, 0, NULL
, &a
);
561 return log_error_errno(r
, "Failed to query root password: %m");
562 if (strv_length(a
) != 1)
563 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
564 "Received multiple passwords, where we expected one.");
567 log_warning("No password entered, skipping.");
571 r
= ask_password_tty(-1, msg2
, NULL
, 0, 0, NULL
, &b
);
573 return log_error_errno(r
, "Failed to query root password: %m");
574 if (strv_length(b
) != 1)
575 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
576 "Received multiple passwords, where we expected one.");
578 if (!streq(*a
, *b
)) {
579 log_error("Entered passwords did not match, please try again.");
583 arg_root_password
= TAKE_PTR(*a
);
590 static int write_root_passwd(const char *passwd_path
, const char *password
) {
591 _cleanup_fclose_
FILE *original
= NULL
, *passwd
= NULL
;
592 _cleanup_(unlink_and_freep
) char *passwd_tmp
= NULL
;
595 r
= fopen_temporary_label("/etc/passwd", passwd_path
, &passwd
, &passwd_tmp
);
599 original
= fopen(passwd_path
, "re");
603 r
= sync_rights(fileno(original
), fileno(passwd
));
607 while ((r
= fgetpwent_sane(original
, &i
)) > 0) {
609 if (streq(i
->pw_name
, "root"))
610 i
->pw_passwd
= (char *) password
;
612 r
= putpwent_sane(i
, passwd
);
620 struct passwd root
= {
621 .pw_name
= (char *) "root",
622 .pw_passwd
= (char *) password
,
625 .pw_gecos
= (char *) "Super User",
626 .pw_dir
= (char *) "/root",
627 .pw_shell
= (char *) "/bin/sh",
633 r
= fchmod(fileno(passwd
), 0000);
637 r
= putpwent_sane(&root
, passwd
);
642 r
= fflush_sync_and_check(passwd
);
646 r
= rename_and_apply_smack_floor_label(passwd_tmp
, passwd_path
);
653 static int write_root_shadow(const char *shadow_path
, const char *hashed_password
) {
654 _cleanup_fclose_
FILE *original
= NULL
, *shadow
= NULL
;
655 _cleanup_(unlink_and_freep
) char *shadow_tmp
= NULL
;
658 r
= fopen_temporary_label("/etc/shadow", shadow_path
, &shadow
, &shadow_tmp
);
662 original
= fopen(shadow_path
, "re");
666 r
= sync_rights(fileno(original
), fileno(shadow
));
670 while ((r
= fgetspent_sane(original
, &i
)) > 0) {
672 if (streq(i
->sp_namp
, "root")) {
673 i
->sp_pwdp
= (char *) hashed_password
;
674 i
->sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
);
677 r
= putspent_sane(i
, shadow
);
686 .sp_namp
= (char*) "root",
687 .sp_pwdp
= (char *) hashed_password
,
688 .sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
),
694 .sp_flag
= (unsigned long) -1, /* this appears to be what everybody does ... */
700 r
= fchmod(fileno(shadow
), 0000);
704 r
= putspent_sane(&root
, shadow
);
709 r
= fflush_sync_and_check(shadow
);
713 r
= rename_and_apply_smack_floor_label(shadow_tmp
, shadow_path
);
720 static int process_root_password(void) {
721 _cleanup_free_
char *salt
= NULL
;
722 _cleanup_close_
int lock
= -1;
723 struct crypt_data cd
= {};
724 const char *hashed_password
;
725 const char *etc_shadow
;
728 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
729 if (laccess(etc_shadow
, F_OK
) >= 0 && !arg_force
)
732 (void) mkdir_parents(etc_shadow
, 0755);
734 lock
= take_etc_passwd_lock(arg_root
);
736 return log_error_errno(lock
, "Failed to take a lock: %m");
738 if (arg_delete_root_password
) {
739 const char *etc_passwd
;
741 /* Mixing alloca() and other stuff that touches the stack in one expression is not portable. */
742 etc_passwd
= prefix_roota(arg_root
, "/etc/passwd");
744 r
= write_root_passwd(etc_passwd
, "");
746 return log_error_errno(r
, "Failed to write %s: %m", etc_passwd
);
748 log_info("%s written", etc_passwd
);
753 if (arg_copy_root_password
&& arg_root
) {
757 p
= getspnam("root");
758 if (p
|| errno
!= ENOENT
) {
763 return log_error_errno(errno
, "Failed to find shadow entry for root: %m");
766 r
= write_root_shadow(etc_shadow
, p
->sp_pwdp
);
768 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
770 log_info("%s copied.", etc_shadow
);
775 r
= prompt_root_password();
779 if (!arg_root_password
)
782 r
= make_salt(&salt
);
784 return log_error_errno(r
, "Failed to get salt: %m");
787 hashed_password
= crypt_r(arg_root_password
, salt
, &cd
);
788 if (!hashed_password
)
789 return log_error_errno(errno
== 0 ? SYNTHETIC_ERRNO(EINVAL
) : errno
,
790 "Failed to encrypt password: %m");
792 r
= write_root_shadow(etc_shadow
, hashed_password
);
794 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
796 log_info("%s written.", etc_shadow
);
800 static int help(void) {
801 _cleanup_free_
char *link
= NULL
;
804 r
= terminal_urlify_man("systemd-firstboot", "1", &link
);
808 printf("%s [OPTIONS...]\n\n"
809 "Configures basic settings of the system.\n\n"
810 " -h --help Show this help\n"
811 " --version Show package version\n"
812 " --root=PATH Operate on an alternate filesystem root\n"
813 " --locale=LOCALE Set primary locale (LANG=)\n"
814 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
815 " --keymap=KEYMAP Set keymap\n"
816 " --timezone=TIMEZONE Set timezone\n"
817 " --hostname=NAME Set hostname\n"
818 " --machine-ID=ID Set machine ID\n"
819 " --root-password=PASSWORD Set root password\n"
820 " --root-password-file=FILE Set root password from file\n"
821 " --prompt-locale Prompt the user for locale settings\n"
822 " --prompt-keymap Prompt the user for keymap settings\n"
823 " --prompt-timezone Prompt the user for timezone\n"
824 " --prompt-hostname Prompt the user for hostname\n"
825 " --prompt-root-password Prompt the user for root password\n"
826 " --prompt Prompt for all of the above\n"
827 " --copy-locale Copy locale from host\n"
828 " --copy-keymap Copy keymap from host\n"
829 " --copy-timezone Copy timezone from host\n"
830 " --copy-root-password Copy root password from host\n"
831 " --copy Copy locale, keymap, timezone, root password\n"
832 " --setup-machine-id Generate a new random machine ID\n"
833 " --force Overwrite existing files\n"
834 " --delete-root-password Delete root password\n"
835 "\nSee the %s for details.\n"
836 , program_invocation_short_name
843 static int parse_argv(int argc
, char *argv
[]) {
855 ARG_ROOT_PASSWORD_FILE
,
861 ARG_PROMPT_ROOT_PASSWORD
,
866 ARG_COPY_ROOT_PASSWORD
,
867 ARG_SETUP_MACHINE_ID
,
869 ARG_DELETE_ROOT_PASSWORD
,
872 static const struct option options
[] = {
873 { "help", no_argument
, NULL
, 'h' },
874 { "version", no_argument
, NULL
, ARG_VERSION
},
875 { "root", required_argument
, NULL
, ARG_ROOT
},
876 { "locale", required_argument
, NULL
, ARG_LOCALE
},
877 { "locale-messages", required_argument
, NULL
, ARG_LOCALE_MESSAGES
},
878 { "keymap", required_argument
, NULL
, ARG_KEYMAP
},
879 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
880 { "hostname", required_argument
, NULL
, ARG_HOSTNAME
},
881 { "machine-id", required_argument
, NULL
, ARG_MACHINE_ID
},
882 { "root-password", required_argument
, NULL
, ARG_ROOT_PASSWORD
},
883 { "root-password-file", required_argument
, NULL
, ARG_ROOT_PASSWORD_FILE
},
884 { "prompt", no_argument
, NULL
, ARG_PROMPT
},
885 { "prompt-locale", no_argument
, NULL
, ARG_PROMPT_LOCALE
},
886 { "prompt-keymap", no_argument
, NULL
, ARG_PROMPT_KEYMAP
},
887 { "prompt-timezone", no_argument
, NULL
, ARG_PROMPT_TIMEZONE
},
888 { "prompt-hostname", no_argument
, NULL
, ARG_PROMPT_HOSTNAME
},
889 { "prompt-root-password", no_argument
, NULL
, ARG_PROMPT_ROOT_PASSWORD
},
890 { "copy", no_argument
, NULL
, ARG_COPY
},
891 { "copy-locale", no_argument
, NULL
, ARG_COPY_LOCALE
},
892 { "copy-keymap", no_argument
, NULL
, ARG_COPY_KEYMAP
},
893 { "copy-timezone", no_argument
, NULL
, ARG_COPY_TIMEZONE
},
894 { "copy-root-password", no_argument
, NULL
, ARG_COPY_ROOT_PASSWORD
},
895 { "setup-machine-id", no_argument
, NULL
, ARG_SETUP_MACHINE_ID
},
896 { "force", no_argument
, NULL
, ARG_FORCE
},
897 { "delete-root-password", no_argument
, NULL
, ARG_DELETE_ROOT_PASSWORD
},
906 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
917 r
= parse_path_argument_and_warn(optarg
, true, &arg_root
);
923 r
= free_and_strdup(&arg_locale
, optarg
);
929 case ARG_LOCALE_MESSAGES
:
930 r
= free_and_strdup(&arg_locale_messages
, optarg
);
937 if (!keymap_is_valid(optarg
))
938 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
939 "Keymap %s is not valid.", optarg
);
941 r
= free_and_strdup(&arg_keymap
, optarg
);
948 if (!timezone_is_valid(optarg
, LOG_ERR
))
949 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
950 "Timezone %s is not valid.", optarg
);
952 r
= free_and_strdup(&arg_timezone
, optarg
);
958 case ARG_ROOT_PASSWORD
:
959 r
= free_and_strdup(&arg_root_password
, optarg
);
964 case ARG_ROOT_PASSWORD_FILE
:
965 arg_root_password
= mfree(arg_root_password
);
967 r
= read_one_line_file(optarg
, &arg_root_password
);
969 return log_error_errno(r
, "Failed to read %s: %m", optarg
);
974 if (!hostname_is_valid(optarg
, true))
975 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
976 "Host name %s is not valid.", optarg
);
978 hostname_cleanup(optarg
);
979 r
= free_and_strdup(&arg_hostname
, optarg
);
986 if (sd_id128_from_string(optarg
, &arg_machine_id
) < 0)
987 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
988 "Failed to parse machine id %s.", optarg
);
993 arg_prompt_locale
= arg_prompt_keymap
= arg_prompt_timezone
= arg_prompt_hostname
= arg_prompt_root_password
= true;
996 case ARG_PROMPT_LOCALE
:
997 arg_prompt_locale
= true;
1000 case ARG_PROMPT_KEYMAP
:
1001 arg_prompt_keymap
= true;
1004 case ARG_PROMPT_TIMEZONE
:
1005 arg_prompt_timezone
= true;
1008 case ARG_PROMPT_HOSTNAME
:
1009 arg_prompt_hostname
= true;
1012 case ARG_PROMPT_ROOT_PASSWORD
:
1013 arg_prompt_root_password
= true;
1017 arg_copy_locale
= arg_copy_keymap
= arg_copy_timezone
= arg_copy_root_password
= true;
1020 case ARG_COPY_LOCALE
:
1021 arg_copy_locale
= true;
1024 case ARG_COPY_KEYMAP
:
1025 arg_copy_keymap
= true;
1028 case ARG_COPY_TIMEZONE
:
1029 arg_copy_timezone
= true;
1032 case ARG_COPY_ROOT_PASSWORD
:
1033 arg_copy_root_password
= true;
1036 case ARG_SETUP_MACHINE_ID
:
1038 r
= sd_id128_randomize(&arg_machine_id
);
1040 return log_error_errno(r
, "Failed to generate randomized machine ID: %m");
1048 case ARG_DELETE_ROOT_PASSWORD
:
1049 arg_delete_root_password
= true;
1056 assert_not_reached("Unhandled option");
1059 /* We check if the specified locale strings are valid down here, so that we can take --root= into
1060 * account when looking for the locale files. */
1062 if (arg_locale
&& !locale_is_ok(arg_locale
))
1063 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale %s is not installed.", arg_locale
);
1064 if (arg_locale_messages
&& !locale_is_ok(arg_locale_messages
))
1065 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale %s is not installed.", arg_locale_messages
);
1067 if (arg_delete_root_password
&& (arg_copy_root_password
|| arg_root_password
|| arg_prompt_root_password
))
1068 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1069 "--delete-root-password cannot be combined with other root password options");
1074 static int run(int argc
, char *argv
[]) {
1078 r
= parse_argv(argc
, argv
);
1082 log_setup_service();
1086 r
= proc_cmdline_get_bool("systemd.firstboot", &enabled
);
1088 return log_error_errno(r
, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
1089 if (r
> 0 && !enabled
)
1090 return 0; /* disabled */
1092 r
= process_locale();
1096 r
= process_keymap();
1100 r
= process_timezone();
1104 r
= process_hostname();
1108 r
= process_machine_id();
1112 r
= process_root_password();
1119 DEFINE_MAIN_FUNCTION(run
);