Skip to content

Commit a15b50e

Browse files
committed
[doc] Add host-ntp-time feature doc
Signed-off-by: Changlei Li <changlei.li@cloud.com>
1 parent c711a42 commit a15b50e

File tree

1 file changed

+212
-0
lines changed

1 file changed

+212
-0
lines changed
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
---
2+
title: Host NTP and time config API
3+
layout: default
4+
design_doc: true
5+
revision: 1
6+
status: proposed
7+
---
8+
9+
# Host NTP and time
10+
11+
## Background
12+
13+
There are no APIs to config the NTP and timezone on the host. Previously, users
14+
had to login on the host via ssh and configure NTP and time manually. The goal
15+
of this feature is to introduce new APIs in XAPI to support NTP and time
16+
configuration, especially in the scenario that users disable ssh service on the
17+
host. XAPI can also store the NTP and timezone configuration in XAPI DB to provide
18+
cache for the getter APIs.
19+
20+
## Use cases
21+
22+
- User can set time zone of the host via XenAPI
23+
- User can get valid time zones list to set via XenAPI
24+
- User can get the current time zone of the host via XenAPI
25+
- User can set custom NTP servers via XenAPI
26+
- User can get the current custom NTP servers via XenAPI
27+
- User can enable/disable NTP service via XenAPI
28+
- User can get the current NTP service enabled or disabled via XenAPI
29+
- User can set NTP to use DHCP assigned servers via XenAPI
30+
- User can set NTP to use custom servers via XenAPI
31+
- User can set NTP to use default NTP servers via XenAPI
32+
- User can get the DHCP assigned NTP servers via XenAPI
33+
- User can get status of current NTP servers via XenAPI
34+
- User can get NTP sync status via XenAPI
35+
- User can set host’s time when NTP is disabled via XenAPI
36+
37+
## NTP configuration on the host
38+
39+
New fields:
40+
`host.ntp_mode`, enum host_ntp_mode(DHCP, Manual, Factory, Disabled)
41+
`host.ntp_manual_servers`, string set
42+
`host.ntp_enabled`, bool
43+
44+
New APIs: `host.set_ntp_mode`, `host.set_ntp_manual_servers`, `host.get_ntp_mode`,
45+
`host.get_ntp_manual_servers`, `host.get_ntp_servers_status`
46+
47+
Abstract the NTP configuration to 4 modes.
48+
- DHCP: set NTP to use DHCP assigned NTP servers
49+
- Manual: set NTP to use manual NTP servers
50+
- Factory: set NTP to use factory NTP servers
51+
- Disabled: disable NTP service
52+
53+
### DHCP mode
54+
55+
In this mode, NTP uses the DHCP assigned NTP servers as sources.
56+
57+
How the NTP and DHCP interaction?
58+
59+
On the host, dhclient executes `/etc/dhcp/dhclient.d/chrony.sh` to update the
60+
ntp servers when network event happens.
61+
- `chrony.sh` writes ntp servers to `/run/chrony-dhcp/$interface.sources`
62+
- Chonryd include the dir `/run/chrony-dhcp` by `sourcedir /run/chrony-dhcp` in
63+
the conf file
64+
- `chrony.sh` runs `chronyc reload sources` to reload NTP sources
65+
66+
Then NTP sources can be updated automatically in the DHCP mode
67+
68+
How to switch DHCP mode?
69+
70+
Dhclient stores dhcp lease in `/var/lib/xcp/dhclient-$interface.leases`, see
71+
module Dhclient in `/ocaml/networkd/lib/network_utils.ml`.
72+
73+
When switch the NTP mode to DHCP, XAPI
74+
- check ntp server item in the lease and fills it in chrony-dhcp files
75+
- Add the exec permission of `chrony.sh`
76+
- Remove all the NTP source items(Manual or Factory) in chronyd conf
77+
- Restart chronyd
78+
79+
When switch ntp mode from dhcp to others, XAPI
80+
- Remove the chrony-dhcp files
81+
- Remove the exec permission of chrony.sh
82+
- Add NTP source items(Manual or Factory) in chronyd conf
83+
- Restart chronyd
84+
85+
### Manual mode
86+
87+
In this mode, NTP uses `host.ntp_manual_servers` as sources.
88+
89+
When switch the NTP mode to Manual, XAPI
90+
- Remove NTP source items in chronyd conf
91+
- Add `host.ntp_manual_servers` as NTP source items in chronyd conf
92+
- Restart chronyd
93+
94+
When `host.ntp_manual_servers` changes and `host.ntp_mode` is Manual, set chronyd
95+
conf with new manual servers and restart chronyd.
96+
97+
### Factory mode
98+
99+
In this mode, ntp uses `factory-ntp-servers` in XAPI config file. Generally the
100+
factory-ntp-servers will be defined by the product.
101+
102+
### Disabled mode
103+
104+
This mode disables NTP service on the host.
105+
106+
### Others
107+
108+
`host.get_ntp_servers_status` calls `chronyc -c sources` to get ntp servers status.
109+
Output parse:
110+
```
111+
Source mode: '^' = server, '=' = peer, '#' = local clock.
112+
Source state: '*' = current synced, '+' = combined, '-' = not combined,
113+
'?' = unreachable, 'x' = time may be in error, '~' = time too variable
114+
```
115+
116+
## Timezone configuration on the host
117+
118+
New field: `host.timezone`, string
119+
120+
New APIs: `host.set_timezone`, `host.get_timezone`, `host.list_timezones`
121+
122+
The timezone is in IANA timezone database format. Timezone on the host can be
123+
get by `realpath /etc/localtime` which is linked to timezone file under
124+
`/usr/share/zoneinfo/`. It can set by link`/etc/localtime` to
125+
`/usr/share/zoneinfo/<timezone>`. All the valid timezones are actually the files
126+
under `/usr/share/zoneinfo/`.
127+
128+
Comparing to using a fixed UTC offset, the benefit is:
129+
- User-friendly: familiar region names and same with most system facilities.
130+
- Handles daylight saving time (DST) automatically
131+
132+
They are equivalent to the `timedatectl` commands
133+
```
134+
timedatectl set-timezone
135+
timedatectl status | grep "Time zone"
136+
timedatectl list-timezones
137+
```
138+
139+
## Time on the host
140+
141+
New API: `host.get_ntp_synchronized`, `host.set_servertime`
142+
143+
`host.get_ntp_synchronized` shows if the system time is synchronized with NTP
144+
source.
145+
146+
`host.set_servertime` offers an API to set the server time when NTP disabled.
147+
it accepts a ISO8601 datetime format timestamp with timezone, i.e. ends with 'Z'
148+
to represent the UTC or explicit UTC offset like '+05:00'.
149+
150+
## Dbsync and restriction
151+
152+
For the new fields in this doc, on XAPI start, dbsync will get the real host
153+
status and sync to XAPI DB to make the real host status and XAPI DB consistent.
154+
Upgrade case can also be benefited from the dbsync.
155+
156+
If the user changes the config behind XAPI, like modify the chronyd conf directly
157+
via ssh, the real status on the host and XAPI DB come to inconsistent and may
158+
lead to unpredicted result, unless restart XAPI.
159+
160+
## Usage examples
161+
162+
Set NTP DHCP mode and get the status
163+
```python
164+
session.xenapi.host.set_timezone(host_ref, 'UTC')
165+
session.xenapi.host.set_ntp_mode(host_ref, 'DHCP')
166+
session.xenapi.host.get_ntp_synchronized(host_ref)
167+
session.xenapi.host.get_ntp_servers_status(host_ref)
168+
```
169+
170+
Set NTP Manual mode and get the status
171+
```python
172+
session.xenapi.host.set_timezone(host_ref, 'Europe/London')
173+
servers = ['time.server1.com', 'time.server2.com', 'time.server3.com']
174+
session.xenapi.host.set_ntp_manual_servers(host_ref, servers)
175+
session.xenapi.host.set_ntp_mode(host_ref, 'Manual')
176+
session.xenapi.host.get_ntp_synchronized(host_ref)
177+
session.xenapi.host.get_ntp_servers_status(host_ref)
178+
```
179+
180+
Set NTP default mode and get the status
181+
```python
182+
session.xenapi.host.set_ntp_mode(host_ref, 'Factory')
183+
session.xenapi.host.get_ntp_synchronized(host_ref)
184+
session.xenapi.host.get_ntp_servers_status(host_ref)
185+
```
186+
187+
Disable NTP and set server time
188+
```python
189+
session.xenapi.host.set_timezone(host_ref, 'Europe/London')
190+
session.xenapi.host.set_ntp_mode(host_ref, 'Disabled')
191+
session.xenapi.host.set_servertime(host_ref, "20251105T16:11:55Z")
192+
```
193+
194+
## APIs summary
195+
196+
```
197+
host.ntp_mode
198+
host.ntp_manual_servers
199+
host.timezone
200+
201+
host.set_timezone
202+
host.list_timezones
203+
host.get_timezone
204+
host.get_ntp_enabled
205+
host.get_ntp_synchronized
206+
host.set_ntp_mode
207+
host.get_ntp_mode
208+
host.set_ntp_manual_servers
209+
host.get_ntp_manual_servers
210+
host.get_ntp_servers_status
211+
host.set_servertime
212+
```

0 commit comments

Comments
 (0)