1 /* SPDX-License-Identifier: LGPL-2.1+ */
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 "loop-util.h"
23 #include "main-func.h"
24 #include "memory-util.h"
26 #include "mount-util.h"
27 #include "namespace-util.h"
29 #include "parse-util.h"
30 #include "path-util.h"
31 #include "pretty-print.h"
32 #include "proc-cmdline.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_kernel_cmdline
= NULL
;
53 static bool arg_prompt_locale
= false;
54 static bool arg_prompt_keymap
= false;
55 static bool arg_prompt_timezone
= false;
56 static bool arg_prompt_hostname
= false;
57 static bool arg_prompt_root_password
= false;
58 static bool arg_copy_locale
= false;
59 static bool arg_copy_keymap
= false;
60 static bool arg_copy_timezone
= false;
61 static bool arg_copy_root_password
= false;
62 static bool arg_force
= false;
63 static bool arg_delete_root_password
= false;
64 static bool arg_root_password_is_hashed
= false;
65 static bool arg_welcome
= true;
67 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
68 STATIC_DESTRUCTOR_REGISTER(arg_image
, freep
);
69 STATIC_DESTRUCTOR_REGISTER(arg_locale
, freep
);
70 STATIC_DESTRUCTOR_REGISTER(arg_locale_messages
, freep
);
71 STATIC_DESTRUCTOR_REGISTER(arg_keymap
, freep
);
72 STATIC_DESTRUCTOR_REGISTER(arg_timezone
, freep
);
73 STATIC_DESTRUCTOR_REGISTER(arg_hostname
, freep
);
74 STATIC_DESTRUCTOR_REGISTER(arg_root_password
, erase_and_freep
);
76 static bool press_any_key(void) {
80 printf("-- Press any key to proceed --");
83 (void) read_one_char(stdin
, &k
, USEC_INFINITY
, &need_nl
);
91 static void print_welcome(void) {
92 _cleanup_free_
char *pretty_name
= NULL
, *ansi_color
= NULL
;
93 static bool done
= false;
103 r
= parse_os_release(
105 "PRETTY_NAME", &pretty_name
,
106 "ANSI_COLOR", &ansi_color
,
109 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
,
110 "Failed to read os-release file, ignoring: %m");
112 pn
= isempty(pretty_name
) ? "Linux" : pretty_name
;
114 if (colors_enabled())
115 printf("\nWelcome to your new installation of \x1B[%sm%s\x1B[0m!\n", ansi_color
, pn
);
117 printf("\nWelcome to your new installation of %s!\n", pn
);
119 printf("\nPlease configure your system!\n\n");
126 static int show_menu(char **x
, unsigned n_columns
, unsigned width
, unsigned percentage
) {
127 unsigned break_lines
, break_modulo
;
128 size_t n
, per_column
, i
, j
;
130 assert(n_columns
> 0);
133 per_column
= DIV_ROUND_UP(n
, n_columns
);
135 break_lines
= lines();
139 /* The first page gets two extra lines, since we want to show
141 break_modulo
= break_lines
;
142 if (break_modulo
> 3)
145 for (i
= 0; i
< per_column
; i
++) {
147 for (j
= 0; j
< n_columns
; j
++) {
148 _cleanup_free_
char *e
= NULL
;
150 if (j
* per_column
+ i
>= n
)
153 e
= ellipsize(x
[j
* per_column
+ i
], width
, percentage
);
157 printf("%4zu) %-*s", j
* per_column
+ i
+ 1, width
, e
);
162 /* on the first screen we reserve 2 extra lines for the title */
163 if (i
% break_lines
== break_modulo
) {
164 if (!press_any_key())
172 static int prompt_loop(const char *text
, char **l
, unsigned percentage
, bool (*is_valid
)(const char *name
), char **ret
) {
180 _cleanup_free_
char *p
= NULL
;
183 r
= ask_string(&p
, "%s %s (empty to skip, \"list\" to list options): ",
184 special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), text
);
186 return log_error_errno(r
, "Failed to query user: %m");
189 log_warning("No data entered, skipping.");
193 if (streq(p
, "list")) {
194 r
= show_menu(l
, 3, 22, percentage
);
202 r
= safe_atou(p
, &u
);
204 if (u
<= 0 || u
> strv_length(l
)) {
205 log_error("Specified entry number out of range.");
209 log_info("Selected '%s'.", l
[u
-1]);
210 if (free_and_strdup(ret
, l
[u
-1]) < 0)
217 log_error("Entered data invalid.");
221 return free_and_replace(*ret
, p
);
225 static bool locale_is_ok(const char *name
) {
228 return locale_is_valid(name
);
230 return locale_is_installed(name
) > 0;
233 static int prompt_locale(void) {
234 _cleanup_strv_free_
char **locales
= NULL
;
237 if (arg_locale
|| arg_locale_messages
)
240 if (!arg_prompt_locale
)
243 r
= get_locales(&locales
);
245 return log_error_errno(r
, "Cannot query locales list: %m");
247 if (strv_isempty(locales
))
248 log_debug("No locales found, skipping locale selection.");
249 else if (strv_length(locales
) == 1) {
251 if (streq(locales
[0], SYSTEMD_DEFAULT_LOCALE
))
252 log_debug("Only installed locale is default locale anyway, not setting locale explicitly.");
254 log_debug("Only a single locale available (%s), selecting it as default.", locales
[0]);
256 arg_locale
= strdup(locales
[0]);
260 /* Not setting arg_locale_message here, since it defaults to LANG anyway */
265 r
= prompt_loop("Please enter system locale name or number",
266 locales
, 60, locale_is_ok
, &arg_locale
);
270 if (isempty(arg_locale
))
273 r
= prompt_loop("Please enter system message locale name or number",
274 locales
, 60, locale_is_ok
, &arg_locale_messages
);
278 /* Suppress the messages setting if it's the same as the main locale anyway */
279 if (streq_ptr(arg_locale
, arg_locale_messages
))
280 arg_locale_messages
= mfree(arg_locale_messages
);
286 static int process_locale(void) {
287 const char *etc_localeconf
;
292 etc_localeconf
= prefix_roota(arg_root
, "/etc/locale.conf");
293 if (laccess(etc_localeconf
, F_OK
) >= 0 && !arg_force
)
296 if (arg_copy_locale
&& arg_root
) {
298 (void) mkdir_parents(etc_localeconf
, 0755);
299 r
= copy_file("/etc/locale.conf", etc_localeconf
, 0, 0644, 0, 0, COPY_REFLINK
);
302 return log_error_errno(r
, "Failed to copy %s: %m", etc_localeconf
);
304 log_info("%s copied.", etc_localeconf
);
313 if (!isempty(arg_locale
))
314 locales
[i
++] = strjoina("LANG=", arg_locale
);
315 if (!isempty(arg_locale_messages
) && !streq(arg_locale_messages
, arg_locale
))
316 locales
[i
++] = strjoina("LC_MESSAGES=", arg_locale_messages
);
323 (void) mkdir_parents(etc_localeconf
, 0755);
324 r
= write_env_file(etc_localeconf
, locales
);
326 return log_error_errno(r
, "Failed to write %s: %m", etc_localeconf
);
328 log_info("%s written.", etc_localeconf
);
332 static int prompt_keymap(void) {
333 _cleanup_strv_free_
char **kmaps
= NULL
;
339 if (!arg_prompt_keymap
)
342 r
= get_keymaps(&kmaps
);
343 if (r
== -ENOENT
) /* no keymaps installed */
346 return log_error_errno(r
, "Failed to read keymaps: %m");
350 return prompt_loop("Please enter system keymap name or number",
351 kmaps
, 60, keymap_is_valid
, &arg_keymap
);
354 static int process_keymap(void) {
355 const char *etc_vconsoleconf
;
359 etc_vconsoleconf
= prefix_roota(arg_root
, "/etc/vconsole.conf");
360 if (laccess(etc_vconsoleconf
, F_OK
) >= 0 && !arg_force
)
363 if (arg_copy_keymap
&& arg_root
) {
365 (void) mkdir_parents(etc_vconsoleconf
, 0755);
366 r
= copy_file("/etc/vconsole.conf", etc_vconsoleconf
, 0, 0644, 0, 0, COPY_REFLINK
);
369 return log_error_errno(r
, "Failed to copy %s: %m", etc_vconsoleconf
);
371 log_info("%s copied.", etc_vconsoleconf
);
378 return 0; /* don't fail if no keymaps are installed */
382 if (isempty(arg_keymap
))
385 keymap
= STRV_MAKE(strjoina("KEYMAP=", arg_keymap
));
387 r
= mkdir_parents(etc_vconsoleconf
, 0755);
389 return log_error_errno(r
, "Failed to create the parent directory of %s: %m", etc_vconsoleconf
);
391 r
= write_env_file(etc_vconsoleconf
, keymap
);
393 return log_error_errno(r
, "Failed to write %s: %m", etc_vconsoleconf
);
395 log_info("%s written.", etc_vconsoleconf
);
399 static bool timezone_is_valid_log_error(const char *name
) {
400 return timezone_is_valid(name
, LOG_ERR
);
403 static int prompt_timezone(void) {
404 _cleanup_strv_free_
char **zones
= NULL
;
410 if (!arg_prompt_timezone
)
413 r
= get_timezones(&zones
);
415 return log_error_errno(r
, "Cannot query timezone list: %m");
419 r
= prompt_loop("Please enter timezone name or number",
420 zones
, 30, timezone_is_valid_log_error
, &arg_timezone
);
427 static int process_timezone(void) {
428 const char *etc_localtime
, *e
;
431 etc_localtime
= prefix_roota(arg_root
, "/etc/localtime");
432 if (laccess(etc_localtime
, F_OK
) >= 0 && !arg_force
)
435 if (arg_copy_timezone
&& arg_root
) {
436 _cleanup_free_
char *p
= NULL
;
438 r
= readlink_malloc("/etc/localtime", &p
);
441 return log_error_errno(r
, "Failed to read host timezone: %m");
443 (void) mkdir_parents(etc_localtime
, 0755);
444 if (symlink(p
, etc_localtime
) < 0)
445 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
447 log_info("%s copied.", etc_localtime
);
452 r
= prompt_timezone();
456 if (isempty(arg_timezone
))
459 e
= strjoina("../usr/share/zoneinfo/", arg_timezone
);
461 (void) mkdir_parents(etc_localtime
, 0755);
462 if (symlink(e
, etc_localtime
) < 0)
463 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
465 log_info("%s written", etc_localtime
);
469 static int prompt_hostname(void) {
475 if (!arg_prompt_hostname
)
482 _cleanup_free_
char *h
= NULL
;
484 r
= ask_string(&h
, "%s Please enter hostname for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
));
486 return log_error_errno(r
, "Failed to query hostname: %m");
489 log_warning("No hostname entered, skipping.");
493 if (!hostname_is_valid(h
, true)) {
494 log_error("Specified hostname invalid.");
498 /* Get rid of the trailing dot that we allow, but don't want to see */
499 arg_hostname
= hostname_cleanup(h
);
507 static int process_hostname(void) {
508 const char *etc_hostname
;
511 etc_hostname
= prefix_roota(arg_root
, "/etc/hostname");
512 if (laccess(etc_hostname
, F_OK
) >= 0 && !arg_force
)
515 r
= prompt_hostname();
519 if (isempty(arg_hostname
))
522 r
= write_string_file(etc_hostname
, arg_hostname
,
523 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
|
524 (arg_force
? WRITE_STRING_FILE_ATOMIC
: 0));
526 return log_error_errno(r
, "Failed to write %s: %m", etc_hostname
);
528 log_info("%s written.", etc_hostname
);
532 static int process_machine_id(void) {
533 const char *etc_machine_id
;
534 char id
[SD_ID128_STRING_MAX
];
537 etc_machine_id
= prefix_roota(arg_root
, "/etc/machine-id");
538 if (laccess(etc_machine_id
, F_OK
) >= 0 && !arg_force
)
541 if (sd_id128_is_null(arg_machine_id
))
544 r
= write_string_file(etc_machine_id
, sd_id128_to_string(arg_machine_id
, id
),
545 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
|
546 (arg_force
? WRITE_STRING_FILE_ATOMIC
: 0));
548 return log_error_errno(r
, "Failed to write machine id: %m");
550 log_info("%s written.", etc_machine_id
);
554 static int prompt_root_password(void) {
555 const char *msg1
, *msg2
;
558 if (arg_root_password
)
561 if (!arg_prompt_root_password
)
567 msg1
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter a new root password (empty to skip):");
568 msg2
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter new root password again:");
571 _cleanup_strv_free_erase_
char **a
= NULL
, **b
= NULL
;
573 r
= ask_password_tty(-1, msg1
, NULL
, 0, 0, NULL
, &a
);
575 return log_error_errno(r
, "Failed to query root password: %m");
576 if (strv_length(a
) != 1)
577 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
578 "Received multiple passwords, where we expected one.");
581 log_warning("No password entered, skipping.");
585 r
= ask_password_tty(-1, msg2
, NULL
, 0, 0, NULL
, &b
);
587 return log_error_errno(r
, "Failed to query root password: %m");
588 if (strv_length(b
) != 1)
589 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
590 "Received multiple passwords, where we expected one.");
592 if (!streq(*a
, *b
)) {
593 log_error("Entered passwords did not match, please try again.");
597 arg_root_password
= TAKE_PTR(*a
);
604 static int write_root_passwd(const char *passwd_path
, const char *password
) {
605 _cleanup_fclose_
FILE *original
= NULL
, *passwd
= NULL
;
606 _cleanup_(unlink_and_freep
) char *passwd_tmp
= NULL
;
609 r
= fopen_temporary_label("/etc/passwd", passwd_path
, &passwd
, &passwd_tmp
);
613 original
= fopen(passwd_path
, "re");
617 r
= sync_rights(fileno(original
), fileno(passwd
));
621 while ((r
= fgetpwent_sane(original
, &i
)) > 0) {
623 if (streq(i
->pw_name
, "root"))
624 i
->pw_passwd
= (char *) password
;
626 r
= putpwent_sane(i
, passwd
);
634 struct passwd root
= {
635 .pw_name
= (char *) "root",
636 .pw_passwd
= (char *) password
,
639 .pw_gecos
= (char *) "Super User",
640 .pw_dir
= (char *) "/root",
641 .pw_shell
= (char *) "/bin/sh",
647 r
= fchmod(fileno(passwd
), 0000);
651 r
= putpwent_sane(&root
, passwd
);
656 r
= fflush_sync_and_check(passwd
);
660 r
= rename_and_apply_smack_floor_label(passwd_tmp
, passwd_path
);
667 static int write_root_shadow(const char *shadow_path
, const char *hashed_password
) {
668 _cleanup_fclose_
FILE *original
= NULL
, *shadow
= NULL
;
669 _cleanup_(unlink_and_freep
) char *shadow_tmp
= NULL
;
672 r
= fopen_temporary_label("/etc/shadow", shadow_path
, &shadow
, &shadow_tmp
);
676 original
= fopen(shadow_path
, "re");
680 r
= sync_rights(fileno(original
), fileno(shadow
));
684 while ((r
= fgetspent_sane(original
, &i
)) > 0) {
686 if (streq(i
->sp_namp
, "root")) {
687 i
->sp_pwdp
= (char *) hashed_password
;
688 i
->sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
);
691 r
= putspent_sane(i
, shadow
);
700 .sp_namp
= (char*) "root",
701 .sp_pwdp
= (char *) hashed_password
,
702 .sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
),
708 .sp_flag
= (unsigned long) -1, /* this appears to be what everybody does ... */
714 r
= fchmod(fileno(shadow
), 0000);
718 r
= putspent_sane(&root
, shadow
);
723 r
= fflush_sync_and_check(shadow
);
727 r
= rename_and_apply_smack_floor_label(shadow_tmp
, shadow_path
);
734 static int process_root_password(void) {
735 _cleanup_close_
int lock
= -1;
736 struct crypt_data cd
= {};
737 const char *hashed_password
;
738 const char *etc_shadow
;
741 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
742 if (laccess(etc_shadow
, F_OK
) >= 0 && !arg_force
)
745 (void) mkdir_parents(etc_shadow
, 0755);
747 lock
= take_etc_passwd_lock(arg_root
);
749 return log_error_errno(lock
, "Failed to take a lock: %m");
751 if (arg_delete_root_password
) {
752 const char *etc_passwd
;
754 /* Mixing alloca() and other stuff that touches the stack in one expression is not portable. */
755 etc_passwd
= prefix_roota(arg_root
, "/etc/passwd");
757 r
= write_root_passwd(etc_passwd
, "");
759 return log_error_errno(r
, "Failed to write %s: %m", etc_passwd
);
761 log_info("%s written", etc_passwd
);
766 if (arg_copy_root_password
&& arg_root
) {
770 p
= getspnam("root");
771 if (p
|| errno
!= ENOENT
) {
776 return log_error_errno(errno
, "Failed to find shadow entry for root: %m");
779 r
= write_root_shadow(etc_shadow
, p
->sp_pwdp
);
781 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
783 log_info("%s copied.", etc_shadow
);
788 r
= prompt_root_password();
792 if (!arg_root_password
)
795 if (arg_root_password_is_hashed
)
796 hashed_password
= arg_root_password
;
798 _cleanup_free_
char *salt
= NULL
;
799 /* hashed_password points inside cd after crypt_r returns so cd has function scope. */
801 r
= make_salt(&salt
);
803 return log_error_errno(r
, "Failed to get salt: %m");
806 hashed_password
= crypt_r(arg_root_password
, salt
, &cd
);
807 if (!hashed_password
)
808 return log_error_errno(errno
== 0 ? SYNTHETIC_ERRNO(EINVAL
) : errno
,
809 "Failed to encrypt password: %m");
812 r
= write_root_shadow(etc_shadow
, hashed_password
);
814 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
816 log_info("%s written.", etc_shadow
);
820 static int process_kernel_cmdline(void) {
821 const char *etc_kernel_cmdline
;
824 etc_kernel_cmdline
= prefix_roota(arg_root
, "/etc/kernel/cmdline");
825 if (laccess(etc_kernel_cmdline
, F_OK
) >= 0 && !arg_force
)
828 if (!arg_kernel_cmdline
)
831 r
= write_string_file(etc_kernel_cmdline
, arg_kernel_cmdline
,
832 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
|
833 (arg_force
? WRITE_STRING_FILE_ATOMIC
: 0));
835 return log_error_errno(r
, "Failed to write %s: %m", etc_kernel_cmdline
);
837 log_info("%s written.", etc_kernel_cmdline
);
841 static int setup_image(char **ret_mount_dir
, LoopDevice
**ret_loop_device
, DecryptedImage
**ret_decrypted_image
) {
842 DissectImageFlags f
= DISSECT_IMAGE_REQUIRE_ROOT
|DISSECT_IMAGE_VALIDATE_OS
|DISSECT_IMAGE_RELAX_VAR_CHECK
|DISSECT_IMAGE_FSCK
;
843 _cleanup_(loop_device_unrefp
) LoopDevice
*d
= NULL
;
844 _cleanup_(decrypted_image_unrefp
) DecryptedImage
*decrypted_image
= NULL
;
845 _cleanup_(dissected_image_unrefp
) DissectedImage
*dissected_image
= NULL
;
846 _cleanup_(rmdir_and_freep
) char *mount_dir
= NULL
;
847 _cleanup_free_
char *temp
= NULL
;
851 *ret_mount_dir
= NULL
;
852 *ret_decrypted_image
= NULL
;
853 *ret_loop_device
= NULL
;
859 r
= tempfn_random_child(NULL
, "firstboot", &temp
);
861 return log_error_errno(r
, "Failed to generate temporary mount directory: %m");
863 r
= loop_device_make_by_path(arg_image
, O_RDWR
, LO_FLAGS_PARTSCAN
, &d
);
865 return log_error_errno(r
, "Failed to set up loopback device: %m");
867 r
= dissect_image_and_warn(d
->fd
, arg_image
, NULL
, 0, NULL
, f
, &dissected_image
);
871 r
= dissected_image_decrypt_interactively(dissected_image
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, 0, f
, &decrypted_image
);
875 r
= detach_mount_namespace();
877 return log_error_errno(r
, "Failed to detach mount namespace: %m");
879 mount_dir
= strdup(temp
);
883 r
= mkdir_p(mount_dir
, 0700);
885 mount_dir
= mfree(mount_dir
);
886 return log_error_errno(r
, "Failed to create mount point: %m");
889 r
= dissected_image_mount(dissected_image
, mount_dir
, UID_INVALID
, f
);
891 return log_error_errno(r
, "Failed to mount image: %m");
893 if (decrypted_image
) {
894 r
= decrypted_image_relinquish(decrypted_image
);
896 return log_error_errno(r
, "Failed to relinquish DM devices: %m");
899 loop_device_relinquish(d
);
901 arg_root
= TAKE_PTR(temp
);
903 *ret_mount_dir
= TAKE_PTR(mount_dir
);
904 *ret_decrypted_image
= TAKE_PTR(decrypted_image
);
905 *ret_loop_device
= TAKE_PTR(d
);
910 static int help(void) {
911 _cleanup_free_
char *link
= NULL
;
914 r
= terminal_urlify_man("systemd-firstboot", "1", &link
);
918 printf("%s [OPTIONS...]\n\n"
919 "Configures basic settings of the system.\n\n"
920 " -h --help Show this help\n"
921 " --version Show package version\n"
922 " --root=PATH Operate on an alternate filesystem root\n"
923 " --image=PATH Operate on an alternate filesystem image\n"
924 " --locale=LOCALE Set primary locale (LANG=)\n"
925 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
926 " --keymap=KEYMAP Set keymap\n"
927 " --timezone=TIMEZONE Set timezone\n"
928 " --hostname=NAME Set hostname\n"
929 " --machine-ID=ID Set machine ID\n"
930 " --root-password=PASSWORD Set root password from plaintext password\n"
931 " --root-password-file=FILE Set root password from file\n"
932 " --root-password-hashed=HASHED_PASSWORD Set root password from hashed password\n"
933 " --prompt-locale Prompt the user for locale settings\n"
934 " --prompt-keymap Prompt the user for keymap settings\n"
935 " --prompt-timezone Prompt the user for timezone\n"
936 " --prompt-hostname Prompt the user for hostname\n"
937 " --prompt-root-password Prompt the user for root password\n"
938 " --prompt Prompt for all of the above\n"
939 " --copy-locale Copy locale from host\n"
940 " --copy-keymap Copy keymap from host\n"
941 " --copy-timezone Copy timezone from host\n"
942 " --copy-root-password Copy root password from host\n"
943 " --copy Copy locale, keymap, timezone, root password\n"
944 " --setup-machine-id Generate a new random machine ID\n"
945 " --force Overwrite existing files\n"
946 " --delete-root-password Delete root password\n"
947 " --welcome=no Disable the welcome text\n"
948 "\nSee the %s for details.\n"
949 , program_invocation_short_name
956 static int parse_argv(int argc
, char *argv
[]) {
969 ARG_ROOT_PASSWORD_FILE
,
970 ARG_ROOT_PASSWORD_HASHED
,
971 ARG_KERNEL_COMMAND_LINE
,
977 ARG_PROMPT_ROOT_PASSWORD
,
982 ARG_COPY_ROOT_PASSWORD
,
983 ARG_SETUP_MACHINE_ID
,
985 ARG_DELETE_ROOT_PASSWORD
,
989 static const struct option options
[] = {
990 { "help", no_argument
, NULL
, 'h' },
991 { "version", no_argument
, NULL
, ARG_VERSION
},
992 { "root", required_argument
, NULL
, ARG_ROOT
},
993 { "image", required_argument
, NULL
, ARG_IMAGE
},
994 { "locale", required_argument
, NULL
, ARG_LOCALE
},
995 { "locale-messages", required_argument
, NULL
, ARG_LOCALE_MESSAGES
},
996 { "keymap", required_argument
, NULL
, ARG_KEYMAP
},
997 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
998 { "hostname", required_argument
, NULL
, ARG_HOSTNAME
},
999 { "machine-id", required_argument
, NULL
, ARG_MACHINE_ID
},
1000 { "root-password", required_argument
, NULL
, ARG_ROOT_PASSWORD
},
1001 { "root-password-file", required_argument
, NULL
, ARG_ROOT_PASSWORD_FILE
},
1002 { "root-password-hashed", required_argument
, NULL
, ARG_ROOT_PASSWORD_HASHED
},
1003 { "kernel-command-line", required_argument
, NULL
, ARG_KERNEL_COMMAND_LINE
},
1004 { "prompt", no_argument
, NULL
, ARG_PROMPT
},
1005 { "prompt-locale", no_argument
, NULL
, ARG_PROMPT_LOCALE
},
1006 { "prompt-keymap", no_argument
, NULL
, ARG_PROMPT_KEYMAP
},
1007 { "prompt-timezone", no_argument
, NULL
, ARG_PROMPT_TIMEZONE
},
1008 { "prompt-hostname", no_argument
, NULL
, ARG_PROMPT_HOSTNAME
},
1009 { "prompt-root-password", no_argument
, NULL
, ARG_PROMPT_ROOT_PASSWORD
},
1010 { "copy", no_argument
, NULL
, ARG_COPY
},
1011 { "copy-locale", no_argument
, NULL
, ARG_COPY_LOCALE
},
1012 { "copy-keymap", no_argument
, NULL
, ARG_COPY_KEYMAP
},
1013 { "copy-timezone", no_argument
, NULL
, ARG_COPY_TIMEZONE
},
1014 { "copy-root-password", no_argument
, NULL
, ARG_COPY_ROOT_PASSWORD
},
1015 { "setup-machine-id", no_argument
, NULL
, ARG_SETUP_MACHINE_ID
},
1016 { "force", no_argument
, NULL
, ARG_FORCE
},
1017 { "delete-root-password", no_argument
, NULL
, ARG_DELETE_ROOT_PASSWORD
},
1018 { "welcome", required_argument
, NULL
, ARG_WELCOME
},
1027 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
1038 r
= parse_path_argument_and_warn(optarg
, true, &arg_root
);
1044 r
= parse_path_argument_and_warn(optarg
, false, &arg_image
);
1050 r
= free_and_strdup(&arg_locale
, optarg
);
1056 case ARG_LOCALE_MESSAGES
:
1057 r
= free_and_strdup(&arg_locale_messages
, optarg
);
1064 if (!keymap_is_valid(optarg
))
1065 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1066 "Keymap %s is not valid.", optarg
);
1068 r
= free_and_strdup(&arg_keymap
, optarg
);
1075 if (!timezone_is_valid(optarg
, LOG_ERR
))
1076 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1077 "Timezone %s is not valid.", optarg
);
1079 r
= free_and_strdup(&arg_timezone
, optarg
);
1085 case ARG_ROOT_PASSWORD
:
1086 r
= free_and_strdup(&arg_root_password
, optarg
);
1090 arg_root_password_is_hashed
= false;
1093 case ARG_ROOT_PASSWORD_FILE
:
1094 arg_root_password
= mfree(arg_root_password
);
1096 r
= read_one_line_file(optarg
, &arg_root_password
);
1098 return log_error_errno(r
, "Failed to read %s: %m", optarg
);
1100 arg_root_password_is_hashed
= false;
1103 case ARG_ROOT_PASSWORD_HASHED
:
1104 r
= free_and_strdup(&arg_root_password
, optarg
);
1108 arg_root_password_is_hashed
= true;
1112 if (!hostname_is_valid(optarg
, true))
1113 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1114 "Host name %s is not valid.", optarg
);
1116 hostname_cleanup(optarg
);
1117 r
= free_and_strdup(&arg_hostname
, optarg
);
1123 case ARG_MACHINE_ID
:
1124 if (sd_id128_from_string(optarg
, &arg_machine_id
) < 0)
1125 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1126 "Failed to parse machine id %s.", optarg
);
1130 case ARG_KERNEL_COMMAND_LINE
:
1131 r
= free_and_strdup(&arg_kernel_cmdline
, optarg
);
1138 arg_prompt_locale
= arg_prompt_keymap
= arg_prompt_timezone
= arg_prompt_hostname
= arg_prompt_root_password
= true;
1141 case ARG_PROMPT_LOCALE
:
1142 arg_prompt_locale
= true;
1145 case ARG_PROMPT_KEYMAP
:
1146 arg_prompt_keymap
= true;
1149 case ARG_PROMPT_TIMEZONE
:
1150 arg_prompt_timezone
= true;
1153 case ARG_PROMPT_HOSTNAME
:
1154 arg_prompt_hostname
= true;
1157 case ARG_PROMPT_ROOT_PASSWORD
:
1158 arg_prompt_root_password
= true;
1162 arg_copy_locale
= arg_copy_keymap
= arg_copy_timezone
= arg_copy_root_password
= true;
1165 case ARG_COPY_LOCALE
:
1166 arg_copy_locale
= true;
1169 case ARG_COPY_KEYMAP
:
1170 arg_copy_keymap
= true;
1173 case ARG_COPY_TIMEZONE
:
1174 arg_copy_timezone
= true;
1177 case ARG_COPY_ROOT_PASSWORD
:
1178 arg_copy_root_password
= true;
1181 case ARG_SETUP_MACHINE_ID
:
1182 r
= sd_id128_randomize(&arg_machine_id
);
1184 return log_error_errno(r
, "Failed to generate randomized machine ID: %m");
1192 case ARG_DELETE_ROOT_PASSWORD
:
1193 arg_delete_root_password
= true;
1197 r
= parse_boolean(optarg
);
1199 return log_error_errno(r
, "Failed to parse --welcome= argument: %s", optarg
);
1208 assert_not_reached("Unhandled option");
1211 /* We check if the specified locale strings are valid down here, so that we can take --root= into
1212 * account when looking for the locale files. */
1214 if (arg_locale
&& !locale_is_ok(arg_locale
))
1215 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale %s is not installed.", arg_locale
);
1216 if (arg_locale_messages
&& !locale_is_ok(arg_locale_messages
))
1217 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale %s is not installed.", arg_locale_messages
);
1219 if (arg_delete_root_password
&& (arg_copy_root_password
|| arg_root_password
|| arg_prompt_root_password
))
1220 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1221 "--delete-root-password cannot be combined with other root password options");
1223 if (arg_image
&& arg_root
)
1224 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Please specify either --root= or --image=, the combination of both is not supported.");
1229 static int run(int argc
, char *argv
[]) {
1230 _cleanup_(loop_device_unrefp
) LoopDevice
*loop_device
= NULL
;
1231 _cleanup_(decrypted_image_unrefp
) DecryptedImage
*decrypted_image
= NULL
;
1232 _cleanup_(umount_and_rmdir_and_freep
) char *unlink_dir
= NULL
;
1235 r
= parse_argv(argc
, argv
);
1239 log_setup_service();
1243 if (!arg_root
&& !arg_image
) {
1246 /* If we are called without --root=/--image= let's honour the systemd.firstboot kernel
1247 * command line option, because we are called to provision the host with basic settings (as
1248 * opposed to some other file system tree/image) */
1250 r
= proc_cmdline_get_bool("systemd.firstboot", &enabled
);
1252 return log_error_errno(r
, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
1253 if (r
> 0 && !enabled
)
1254 return 0; /* disabled */
1257 r
= setup_image(&unlink_dir
, &loop_device
, &decrypted_image
);
1261 r
= process_locale();
1265 r
= process_keymap();
1269 r
= process_timezone();
1273 r
= process_hostname();
1277 r
= process_machine_id();
1281 r
= process_root_password();
1285 r
= process_kernel_cmdline();
1292 DEFINE_MAIN_FUNCTION(run
);