|
| 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 add/remove custom NTP servers via XenAPI |
| 26 | +- User can set custom NTP servers via XenAPI |
| 27 | +- User can get the current custom NTP servers via XenAPI |
| 28 | +- User can enable/disable NTP service via XenAPI |
| 29 | +- User can get the current NTP service enabled or disabled via XenAPI |
| 30 | +- User can set NTP to use DHCP assigned servers via XenAPI |
| 31 | +- User can set NTP to use custom servers via XenAPI |
| 32 | +- User can set NTP to use default NTP servers via XenAPI |
| 33 | +- User can get the DHCP assigned NTP servers via XenAPI |
| 34 | +- User can get status of current NTP servers via XenAPI |
| 35 | +- User can get NTP sync status via XenAPI |
| 36 | +- User can set the items above from pool level API |
| 37 | +- User can set host’s local time when NTP is disabled via XenAPI |
| 38 | + |
| 39 | +## NTP configuration on the host |
| 40 | + |
| 41 | +New fields: |
| 42 | +`host.ntp_mode`, enum host_ntp_mode(ntp_mode_dhcp, ntp_mode_custom, ntp_mode_default) |
| 43 | +`host.ntp_custom_servers`, string set |
| 44 | +`host.ntp_enabled`, bool |
| 45 | + |
| 46 | +New APIs: `host.set_ntp_mode`, `host.set_ntp_custom_servers`, `host.get_ntp_mode`, |
| 47 | +`host.get_ntp_custom_servers`, `host.get_ntp_servers_status`, `host.enable_ntp`, |
| 48 | +`host.disable_ntp` |
| 49 | + |
| 50 | +Abstract the NTP configuration to 3 modes. |
| 51 | +- ntp_mode_dhcp: set NTP to use DHCP assigned servers |
| 52 | +- ntp_mode_custom: set NTP to use custom servers |
| 53 | +- ntp_mode_default: set NTP to use default NTP servers |
| 54 | + |
| 55 | +### DHCP mode |
| 56 | + |
| 57 | +In this mode, ntp uses the dhcp assigned ntp servers as sources. |
| 58 | + |
| 59 | +How the NTP and DHCP interaction? |
| 60 | + |
| 61 | +On the host, dhclient executes `/etc/dhcp/dhclient.d/chrony.sh` to update the |
| 62 | +ntp servers when network event happens. |
| 63 | +- `chrony.sh` writes ntp servers to `/run/chrony-dhcp/$interface.sources` |
| 64 | +- Chonryd include the dir `/run/chrony-dhcp` by `sourcedir /run/chrony-dhcp` in |
| 65 | +the conf file |
| 66 | +- `chrony.sh` runs `chronyc reload sources` to reload NTP sources |
| 67 | + |
| 68 | +Then NTP sources can be updated automatically in the DHCP mode |
| 69 | + |
| 70 | +How to switch DHCP mode? |
| 71 | + |
| 72 | +Dhclient stores dhcp lease in `/var/lib/xcp/dhclient-$interface.leases`, see |
| 73 | +module Dhclient in `/ocaml/networkd/lib/network_utils.ml`. |
| 74 | + |
| 75 | +When switch the NTP mode to DHCP, XAPI |
| 76 | +- check ntp server item in the lease and fills it in chrony-dhcp files |
| 77 | +- Add the exec permission of `chrony.sh` |
| 78 | +- Remove all the NTP source items(custom or default) in chronyd conf |
| 79 | +- Restart chronyd |
| 80 | + |
| 81 | +When swith ntp mode from dhcp to others, XAPI |
| 82 | +- Remove the chrony-dhcp files |
| 83 | +- Remove the exec permission of chrony.sh |
| 84 | +- Add NTP source items(custom or default) in chronyd conf |
| 85 | +- Restart chronyd |
| 86 | + |
| 87 | +### Custom mode |
| 88 | + |
| 89 | +In this mode, NTP uses `host.ntp_custom_servers` as sources. |
| 90 | + |
| 91 | +When switch the NTP mode to custom, XAPI |
| 92 | +- Remove NTP source items in chronyd conf |
| 93 | +- Add `host.ntp_custom_servers` as NTP source items in chronyd conf |
| 94 | +- Resetart chronyd |
| 95 | + |
| 96 | +When `host.ntp_custom_servers` changes and `host.mode` is custom, set chronyd |
| 97 | +conf with new custom servers and restart chronyd. |
| 98 | + |
| 99 | +### Default mode |
| 100 | + |
| 101 | +In this mode, ntp uses `default-ntp-servers` in XAPI config file. Generally the |
| 102 | +default-ntp-servers will be defined by the product. |
| 103 | + |
| 104 | +### Others |
| 105 | + |
| 106 | +`host.enable/disable_ntp` starts or stops chronyd service. |
| 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_server_localtime` |
| 142 | + |
| 143 | +`host.get_ntp_synchronized` shows if the system time is synchronized with NTP |
| 144 | +source. |
| 145 | + |
| 146 | +`host.set_server_localtime` offers an API to set the local time when NTP disabled. |
| 147 | +User will use the set of APIs about NTP, timezone to manage the host. So, it |
| 148 | +accepts a localtime without timezone as there is already `host.timezone` in the |
| 149 | +XAPI DB. |
| 150 | + |
| 151 | +## Pool level set APIs |
| 152 | + |
| 153 | +For a pool with large amount of hosts, provides a pool level set APIs which call |
| 154 | +its host level API iteratively is a convenience for user to manage the pool. |
| 155 | + |
| 156 | +## Dbsync and restriction |
| 157 | + |
| 158 | +For the new fields in this doc, on XAPI start, dbsync will get the real host |
| 159 | +status and sync to XAPI DB to make the real host status and XAPI DB consistent. |
| 160 | +Upgrade case can also be benefited from the dbsync. |
| 161 | + |
| 162 | +If the user change the config behind XAPI, like modify the chronyd conf directly |
| 163 | +via ssh, the real status on the host and XAPI DB come to inconsistent and may |
| 164 | +lead to unpredicted result, unless restart XAPI. |
| 165 | + |
| 166 | +## Usage examples |
| 167 | + |
| 168 | +Set NTP DHCP mode and get the status |
| 169 | +```python |
| 170 | +session.xenapi.host.set_timezone(host_ref, 'UTC') |
| 171 | +session.xenapi.host.set_ntp_mode(host_ref, 'ntp_mode_dhcp') |
| 172 | +session.xenapi.host.enable_ntp(host_ref) |
| 173 | +session.xenapi.host.get_ntp_synchronized(host_ref) |
| 174 | +session.xenapi.host.get_ntp_servers_status(host_ref) |
| 175 | +``` |
| 176 | + |
| 177 | +Set NTP custom mode and get the status |
| 178 | +```python |
| 179 | +session.xenapi.host.set_timezone(host_ref, 'Europe/London') |
| 180 | +servers = ['time.custom1.com', 'time.custom2.com', 'time.custom3.com'] |
| 181 | +session.xenapi.host.set_ntp_custom_servers(host_ref, servers) |
| 182 | +session.xenapi.host.set_ntp_mode(host_ref, 'ntp_mode_custom') |
| 183 | +session.xenapi.host.enable_ntp(host_ref) |
| 184 | +session.xenapi.host.get_ntp_synchronized(host_ref) |
| 185 | +session.xenapi.host.get_ntp_servers_status(host_ref) |
| 186 | +``` |
| 187 | + |
| 188 | +Set NTP default mode and get the status |
| 189 | +```python |
| 190 | +session.xenapi.host.set_ntp_mode(host_ref, 'ntp_mode_default') |
| 191 | +session.xenapi.host.enable_ntp(host_ref) |
| 192 | +session.xenapi.host.get_ntp_synchronized(host_ref) |
| 193 | +session.xenapi.host.get_ntp_servers_status(host_ref) |
| 194 | +``` |
| 195 | + |
| 196 | +Disable NTP and set local time |
| 197 | +```python |
| 198 | +session.xenapi.host.set_timezone(host_ref, 'Europe/London') |
| 199 | +session.xenapi.host.disable_ntp(host_ref) |
| 200 | +session.xenapi.host.set_server_localtime(host_ref, "20251105T16:11:55") |
| 201 | +``` |
| 202 | + |
| 203 | +## APIs summary |
| 204 | + |
| 205 | +``` |
| 206 | +host.ntp_mode |
| 207 | +host.ntp_custom_servers |
| 208 | +host.ntp_enabled |
| 209 | +host.timezone |
| 210 | +
|
| 211 | +host.set_timezone / pool.set_timezone |
| 212 | +host.list_timezones |
| 213 | +host.get_timezone |
| 214 | +host.enable_ntp / pool.enable_ntp |
| 215 | +host.disable_ntp / pool.disable_ntp |
| 216 | +host.get_ntp_enabled |
| 217 | +host.get_ntp_synchronized |
| 218 | +host.set_ntp_mode / pool.set_ntp_mode |
| 219 | +host.get_ntp_mode |
| 220 | +host.set_ntp_custom_servers / pool.set_ntp_custom_servers |
| 221 | +host.get_ntp_custom_servers |
| 222 | +host.get_ntp_servers_status |
| 223 | +host.set_server_localtime |
| 224 | +``` |
0 commit comments