Skip to content

Commit 90d21ec

Browse files
committed
Use timedatectl to enable/disable ntp
Command timedatectl is used to set server time. However, if disable chronyd before set_servertime by systemctl, timedatectl may not sync the status in time, then the set-time will fail as `Failed to set time: Automatic time synchronization is enabled`. This is because systemd checks synchronization status periodically and update its own NTP flag (which is timedatectl replied on) To let timedatectl update NTP flag instantly, use timedatectl set-ntp true/false to enable/disable the NTP. Signed-off-by: Changlei Li <changlei.li@cloud.com>
1 parent 1200a96 commit 90d21ec

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

ocaml/xapi/xapi_host_ntp.ml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,17 @@ let remove_dhcp_ntp_servers () =
7272
Sys.remove (!Xapi_globs.ntp_dhcp_dir // fname)
7373
)
7474

75-
let restart_ntp_service () =
76-
Xapi_systemctl.restart ~wait_until_success:false !Xapi_globs.ntp_service
77-
7875
let enable_ntp_service () =
79-
Xapi_systemctl.enable ~wait_until_success:false !Xapi_globs.ntp_service ;
80-
Xapi_systemctl.start ~wait_until_success:false !Xapi_globs.ntp_service
76+
Helpers.call_script !Xapi_globs.timedatectl ["set-ntp"; "true"] |> ignore
8177

8278
let disable_ntp_service () =
83-
Xapi_systemctl.stop ~wait_until_success:false !Xapi_globs.ntp_service ;
84-
Xapi_systemctl.disable ~wait_until_success:false !Xapi_globs.ntp_service
79+
Helpers.call_script !Xapi_globs.timedatectl ["set-ntp"; "false"] |> ignore
80+
81+
let restart_ntp_service () =
82+
(* Make sure the NTP service is enabled before restarting, and the timedatectl
83+
ntp enabled status is consistent with the service state *)
84+
enable_ntp_service () ;
85+
Xapi_systemctl.restart ~wait_until_success:false !Xapi_globs.ntp_service
8586

8687
let is_ntp_service_active () =
8788
Fe_systemctl.is_active ~service:!Xapi_globs.ntp_service

ocaml/xapi/xapi_host_ntp.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ val clear_servers_in_conf : unit -> unit
2222

2323
val promote_legacy_default_servers : unit -> unit
2424

25-
val restart_ntp_service : unit -> unit
26-
2725
val enable_ntp_service : unit -> unit
2826

2927
val disable_ntp_service : unit -> unit
3028

29+
val restart_ntp_service : unit -> unit
30+
3131
val is_ntp_service_active : unit -> bool
3232

3333
val get_servers_from_conf : unit -> string list

0 commit comments

Comments
 (0)