the `shell`, `boot`, `qemu` verbs are not available when this option
is used. Implied for `tar` and `cpio`.
-`Hostname=`, `--hostname=`
-
-: Set the image's hostname to the specified name.
-
`ImageVersion=`, `--image-version=`
: Configure the image version. This accepts any string, but it is
# systemd-nspawn -bi image.raw
```
-To create a *Fedora Linux* image with hostname:
-```bash
-# mkosi --distribution fedora --hostname image
-```
-
-Also you could set hostname in configuration file:
-```bash
-# cat mkosi.conf
-...
-[Output]
-Hostname=image
-...
-```
-
# REQUIREMENTS
mkosi is packaged for various distributions: Debian, Ubuntu, Arch
yield
-def configure_locale(state: MkosiState) -> None:
- if state.for_cache:
- return
-
- etc_locale = state.root / "etc/locale.conf"
- etc_locale.unlink(missing_ok=True)
- # Let's ensure we use a UTF-8 locale everywhere.
- etc_locale.write_text("LANG=C.UTF-8\n")
-
-
-def configure_hostname(state: MkosiState) -> None:
- if state.for_cache:
- return
-
- etc_hostname = state.root / "etc/hostname"
-
- # Always unlink first, so that we don't get in trouble due to a
- # symlink or suchlike. Also if no hostname is configured we really
- # don't want the file to exist, so that systemd's implicit
- # hostname logic can take effect.
- etc_hostname.unlink(missing_ok=True)
-
- if state.config.hostname:
- with complete_step("Assigning hostname"):
- etc_hostname.write_text(state.config.hostname + "\n")
-
-
def prepare_tree_root(state: MkosiState) -> None:
if state.config.output_format == OutputFormat.subvolume:
with complete_step("Setting up OS tree root…"):
tz = run(["timedatectl", "show", "-p", "Timezone", "--value"], text=True, stdout=subprocess.PIPE).stdout.strip()
creds["firstboot.timezone"] = tz
+ if "firstboot.locale" not in creds:
+ creds["firstboot.locale"] = "C.UTF-8"
+
+ if "firstboot.hostname" not in creds:
+ creds["firstboot.hostname"] = machine_name(args)
+
if args.ssh and "ssh.authorized_keys.root" not in creds and "SSH_AUTH_SOCK" in os.environ:
key = run(["ssh-add", "-L"], text=True, stdout=subprocess.PIPE, env=os.environ).stdout.strip()
creds["ssh.authorized_keys.root"] = key
print("\nOUTPUT:")
- if config.hostname:
- print(" Hostname:", config.hostname)
-
if config.image_id is not None:
print(" Image ID:", config.image_id)
run_prepare_script(state, cached, build=False)
install_build_packages(state, cached)
run_prepare_script(state, cached, build=True)
- configure_locale(state)
- configure_hostname(state)
configure_root_password(state)
configure_autologin(state)
configure_initrd(state)
raise MkosiException() from e
-def machine_name(config: MkosiConfig) -> str:
- return config.hostname or config.image_id or config.output.with_suffix("").name.partition("_")[0]
+def machine_name(config: Union[MkosiConfig, argparse.Namespace]) -> str:
+ return config.image_id or config.output.with_suffix("").name.partition("_")[0]
def machine_cid(config: MkosiConfig) -> int:
assert parse([]).packages == ["yes"]
-def test_hostname() -> None:
- with cd_temp_dir():
- assert parse(["--hostname", "name"]).hostname == "name"
- with pytest.raises(SystemExit):
- parse(["--hostname", "name", "additional_name"])
- with pytest.raises(SystemExit):
- parse(["--hostname"])
-
- config = Path("mkosi.conf")
- config.write_text("[Output]\nHostname=name")
- assert parse([]).hostname == "name"
-
-
def test_shell_boot() -> None:
with cd_temp_dir():
with pytest.raises(MkosiException, match=".boot.*tar"):