Skip to content

Conversation

benle7
Copy link

@benle7 benle7 commented Sep 21, 2025

Why I did it
To fix an issue where newly installed SONiC images default /etc/localtime to UTC even when the previous image
was configured with and used a different timezone, and config save was executed before the image update.
This causes a mismatch between the timezone defined in CONFIG_DB and the one defined in the Linux file /etc/localtime, because when the new image comes up with a new filesystem, the Linux file /etc/localtime is created with the default UTC, while the timezone value in CONFIG_DB (DEVICE_METADATA|localhost|timezone) is preserved from the previous image.

How I did it
In src/sonic-host-services/scripts/hostcfgd::DeviceMetaCfg:load, we already read the timezone from CONFIG_DB to save it for future events (DeviceMetaCfg:timezone_update observes changes in DEVICE_METADATA).
So at this point, when hostcfgd is up, we can simply update the Linux file /etc/localtime with the initial correct timezone value from CONFIG_DB by executing (only if it differs from the current /etc/localtime):
timedatectl set-timezone
(as is already done in timezone_update).
The same pattern exists in hostcfgd::DnsCfg:load.

How to verify it

  1. Set a non-UTC timezone on the current image: config clock timezone Asia/Jerusalem
  2. Run config save
  3. Install a new image
  4. Reboot and check that hostcfgd is up: systemctl status hostcfgd (~1m)
  5. Verify that the timezone is identical in CONFIG_DB and /etc/localtime:
    timedatectl
    date
    cat /etc/localtime
    cat /etc/sonic/config_db.json | grep timezone
    hget "DEVICE_METADATA|localhost" "timezone"

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@benle7 benle7 force-pushed the apply_correct_timezone branch from 105b1eb to 8893f51 Compare September 28, 2025 07:49
@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@dprital dprital requested a review from qiluo-msft September 30, 2025 09:11
@dprital
Copy link

dprital commented Sep 30, 2025

@qiluo-msft , Can you please review ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants