When systemd is started, it sets the system clock to epoch to ensure the
system clock is reasonably initialized if no working RTC.

As init process, systemd sets epoch very early to the more recent
timestamp of[1]:
 - the build time of systemd (-Dtime-epoch)
 - the modification time ("mtime") of /var/lib/systemd/timesync/clock
   (systemd-timesyncd)
 - the modification time ("mtime") of /usr/lib/clock-epoch (systemd)

The first epoch timestamp is is hard-coded at build-time by the systemd
recipe (using either SOURCE_DATE_EPOCH, git-tag, or NEWS modification
time[2]).

The second epoch timestamp is maintained at run-time if the system runs
systemd-timesyncd.

This implements the third epoch timestamp at image build-time, from a
newly introduced rootfs post-process command that is very similar to
update_timestamp.

It touches the file /usr/lib/clock-epoch with the modification time set
to either the current timestamp (now) or to a reproducible timestamp if
REPRODUCIBLE_TIMESTAMP_ROOTFS is set.

Note: That newly introduced rootfs post-process command is automatically
set if systemd is set in the distro features.

[1]: 
https://github.com/systemd/systemd/commit/863098fdc9cd91e4f760085356ac02c4b7ba6df1
[2]: https://github.com/systemd/systemd/blob/v256/meson.build#L804-L825

Signed-off-by: Gaël PORTAY <gael.portay+rt...@gmail.com>
---
Hello,

With this patch, one can have a better sane default time set by systemd
very very very early in the boot (i.e. even before systemd outputs its
version!).

        # dmesg
        (...)
        [    3.347641] Run /sbin/init as init process
        [    3.350435]   with arguments:
        [    3.350440]     /sbin/init
        [    3.350443]   with environment:
        [    3.350447]     HOME=/
        [    3.350451]     TERM=linux
        [    3.431275] EXT4-fs (mmcblk2p2): re-mounted. Opts: (null)
        [    3.523997] EXT4-fs (mmcblk2p4): recovery complete
        [    3.527828] EXT4-fs (mmcblk2p4): mounted filesystem with ordered 
data mode. Opts: (null)
        [    3.694440] systemd[1]: System time before build time, advancing 
clock.
        [    3.714755] systemd[1]: systemd 250.5+ running in system mode (+PAM 
-AUDIT -SELINUX -APPARMOR +IMA -SMACK -SECCOMP -GCRYPT -GNUTLS +OPENSSL +ACL 
+BLKID -CURL -ELFUTILS -FIDO2 -IDN2 -IDN -IPTC +KMOD -LIBCRYPTSETUP +LIBFDISK 
-PCRE2 -PWQUALITY -P11KIT -QRENCODE -BZIP2 -LZ4 -XZ -ZLIB +ZSTD -BPF_FRAMEWORK 
-XKBCOMMON +UTMP +SYSVINIT default-hierarchy=hybrid)

Thus, one can do time-based related things offline or before the system
clock is set by the network (if trusting the time does not matter that
much for sure).

This happens if the system has no hardware clock or if it is way too far
in the past and if using an old versions of systemd; systemd v250.5 sets
the system time to April 28, 2022 5:53:22 PM (1651168402).

Also, please set REPRODUCIBLE_TIMESTAMP_ROOTFS to any appropriate value
if enabling reproducible-builds (the default). Otherwise, bitbake sets
the timestamp to Friday, March 9, 2018 12:34:56 PM (1520598896).

If you have a better idea on how to implement it, please tell; in the
meanwhile...

Kind Regards,
Gaël
 meta/classes-recipe/rootfs-postcommands.bbclass | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/meta/classes-recipe/rootfs-postcommands.bbclass 
b/meta/classes-recipe/rootfs-postcommands.bbclass
index 5f4d67f93c..cc74c75cfa 100644
--- a/meta/classes-recipe/rootfs-postcommands.bbclass
+++ b/meta/classes-recipe/rootfs-postcommands.bbclass
@@ -22,6 +22,9 @@ ROOTFS_POSTPROCESS_COMMAND += 
'${@bb.utils.contains_any("IMAGE_FEATURES", [ 'deb
 # Create /etc/timestamp during image construction to give a reasonably sane 
default time setting
 ROOTFS_POSTPROCESS_COMMAND += "rootfs_update_timestamp "
 
+# Create /usr/lib/clock-epoch during image construction to give a reasonably 
sane default time setting to systemd
+ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("DISTRO_FEATURES", 
"systemd", "rootfs_systemd_timestamp ", "", d)}'
+
 # Tweak files in /etc if read-only-rootfs is enabled
 ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("IMAGE_FEATURES", 
"read-only-rootfs", "read_only_rootfs_hook ", "",d)}'
 
@@ -402,6 +405,19 @@ rootfs_update_timestamp () {
        bbnote "rootfs_update_timestamp: set /etc/timestamp to $sformatted"
 }
 
+# Can be used to create /usr/lib/clock-epoch during image construction to give 
a reasonably
+# sane default time setting
+rootfs_systemd_timestamp () {
+       if [ "${REPRODUCIBLE_TIMESTAMP_ROOTFS}" != "" ]; then
+               # Convert UTC into %4Y%2m%2d%2H%2M.%2S
+               sformatted=`date -u -d @${REPRODUCIBLE_TIMESTAMP_ROOTFS} 
+%4Y%2m%2d%2H%2M.%2S`
+       else
+               sformatted=`date -u +%4Y%2m%2d%2H%2M.%2S`
+       fi
+       touch -m -t "$sformatted" ${IMAGE_ROOTFS}/usr/lib/clock-epoch
+       bbnote "rootfs_systemd_timestamp: set /usr/lib/clock-epoch mtime to 
$sformatted"
+}
+
 # Prevent X from being started
 rootfs_no_x_startup () {
        if [ -f ${IMAGE_ROOTFS}/etc/init.d/xserver-nodm ]; then
-- 
2.47.0

-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group.
View/Reply Online (#206078): 
https://lists.openembedded.org/g/openembedded-core/message/206078
Mute This Topic: https://lists.openembedded.org/mt/109095282/21656
Group Owner: openembedded-core+ow...@lists.openembedded.org
Unsubscribe: https://lists.openembedded.org/g/openembedded-core/unsub 
[arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-

Reply via email to