Skip to content

Commit

Permalink
normalize mtime
Browse files Browse the repository at this point in the history
Co-authored-by: Malte Poll <mp@edgeless.systems>
Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com>
  • Loading branch information
katexochen and malt3 committed Aug 26, 2023
1 parent e0c151f commit f716ab3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
12 changes: 9 additions & 3 deletions mkosi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from pathlib import Path
from typing import Any, ContextManager, Mapping, Optional, TextIO, Union

from mkosi.archive import extract_tar, make_cpio, make_tar
from mkosi.archive import extract_tar, make_cpio, make_tar, normalize_mtime
from mkosi.config import (
BiosBootloader,
Bootloader,
Expand Down Expand Up @@ -934,6 +934,7 @@ def build_initrd(state: MkosiState) -> Path:
"--make-initrd", "yes",
"--bootable", "no",
"--manifest-format", "",
*([f"--environment=SOURCE_DATE_EPOCH={state.config.environment['SOURCE_DATE_EPOCH']}"] if "SOURCE_DATE_EPOCH" in state.config.environment else []),
*(["--locale", state.config.locale] if state.config.locale else []),
*(["--locale-messages", state.config.locale_messages] if state.config.locale_messages else []),
*(["--keymap", state.config.keymap] if state.config.keymap else []),
Expand Down Expand Up @@ -962,6 +963,7 @@ def build_kernel_modules_initrd(state: MkosiState, kver: str) -> Path:

make_cpio(
state.root, kmods,
state.config.environment.get("SOURCE_DATE_EPOCH"),
gen_required_kernel_modules(
state.root, kver,
state.config.kernel_modules_initrd_include,
Expand Down Expand Up @@ -1624,6 +1626,8 @@ def make_image(state: MkosiState, skip: Sequence[str] = [], split: bool = False)
if not state.config.output_format == OutputFormat.disk:
return []

normalize_mtime(state.root, state.config.environment.get("SOURCE_DATE_EPOCH"))

cmdline: list[PathString] = [
"systemd-repart",
"--empty=allow",
Expand Down Expand Up @@ -1821,11 +1825,13 @@ def build_image(args: MkosiArgs, config: MkosiConfig) -> None:
install_grub_bios(state, partitions)
make_image(state, split=True)

source_date_epoch = state.config.environment.get("SOURCE_DATE_EPOCH")
if state.config.output_format == OutputFormat.tar:
make_tar(state.root, state.staging / state.config.output_with_format)
make_tar(state.root, state.staging / state.config.output_with_format, source_date_epoch)
elif state.config.output_format == OutputFormat.cpio:
make_cpio(state.root, state.staging / state.config.output_with_format)
make_cpio(state.root, state.staging / state.config.output_with_format, source_date_epoch)
elif state.config.output_format == OutputFormat.directory:
normalize_mtime(state.root, source_date_epoch)
state.root.rename(state.staging / state.config.output_with_format)

maybe_compress(state.config, state.config.compress_output,
Expand Down
24 changes: 21 additions & 3 deletions mkosi/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pathlib import Path
from typing import Optional

from mkosi.log import log_step
from mkosi.log import complete_step, die, log_step
from mkosi.run import bwrap, finalize_passwd_mounts


Expand Down Expand Up @@ -36,8 +36,9 @@ def tar_exclude_apivfs_tmp() -> list[str]:
]


def make_tar(src: Path, dst: Path) -> None:
def make_tar(src: Path, dst: Path, source_date_epoch: Optional[str]) -> None:
log_step(f"Creating tar archive {dst}…")
normalize_mtime(src, source_date_epoch)
bwrap(
[
tar_binary(),
Expand Down Expand Up @@ -82,11 +83,12 @@ def extract_tar(src: Path, dst: Path, log: bool = True) -> None:
)


def make_cpio(src: Path, dst: Path, files: Optional[Iterable[Path]] = None) -> None:
def make_cpio(src: Path, dst: Path, source_date_epoch: Optional[str], files: Optional[Iterable[Path]] = None) -> None:
if not files:
files = src.rglob("*")

log_step(f"Creating cpio archive {dst}…")
normalize_mtime(src, source_date_epoch)
bwrap(
[
cpio_binary(),
Expand All @@ -102,3 +104,19 @@ def make_cpio(src: Path, dst: Path, files: Optional[Iterable[Path]] = None) -> N
# Make sure tar uses user/group information from the root directory instead of the host.
options=finalize_passwd_mounts(dst),
)


def normalize_mtime(root: Path, source_date_epoch: Optional[str]) -> None:
if source_date_epoch is None:
return

try:
mtime_epoch = int(source_date_epoch)
except ValueError:
die(f"SOURCE_DATE_EPOCH={source_date_epoch} is not a valid integer")
if mtime_epoch < 0:
die(f"SOURCE_DATE_EPOCH={source_date_epoch} is negative")

with complete_step("Normalizing mtime"):
for p in root.rglob("*"):
os.utime(p, (mtime_epoch, mtime_epoch), follow_symlinks=False)

0 comments on commit f716ab3

Please sign in to comment.