Skip to content

Commit 09953c3

Browse files
authored
CP-309064 Add SSH Management feature design (#6608)
Add SSH Management feature design documentation for open-source. This document provides detailed explanations of a design that has already been internally discussed and agreed, serving as clarification for the open-source community.
2 parents ad38e87 + 8d94d02 commit 09953c3

File tree

2 files changed

+249
-0
lines changed

2 files changed

+249
-0
lines changed
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
# SSH Management
2+
3+
SSH Management enables programmatic control of SSH access to XenServer hosts. This feature
4+
allows administrators to enable/disable SSH services, configure timeout settings, and implement
5+
automatic SSH management based on XAPI health status.
6+
7+
## Architecture Overview
8+
9+
The SSH Management feature is built around three core components:
10+
11+
1. **SSH Service Control**: Direct enable/disable operations for SSH on individual hosts or entire pools
12+
2. **Timeout Management**: Configurable timeouts for both SSH sessions and service duration limits
13+
3. **Auto Mode**: Intelligent SSH management that automatically adjusts based on XAPI health status
14+
15+
![SSH Status Transition](ssh-status-trans.png)
16+
17+
## SSH Service Control
18+
19+
### API Design
20+
21+
#### Host APIs
22+
23+
- `host.enable_ssh`: Enables SSH access on the specified host
24+
- `host.disable_ssh`: Disables SSH access on the specified host
25+
- `host.set_ssh_enabled_timeout`: Configures SSH service timeout duration (0-172800 seconds, maximum 2 days)
26+
- `host.set_console_idle_timeout`: Sets idle timeout for SSH/VNC console sessions
27+
- `host.set_ssh_auto_mode`: Controls SSH auto mode behavior (when true, SSH is normally disabled but enabled during XAPI downtime)
28+
29+
#### Pool APIs
30+
31+
- `pool.enable_ssh`: Enables SSH access across all hosts in the pool
32+
- `pool.disable_ssh`: Disables SSH access across all hosts in the pool
33+
- `pool.set_ssh_enabled_timeout`: Sets SSH service timeout for all pool hosts
34+
- `pool.set_console_idle_timeout`: Configures console idle timeout for all pool hosts
35+
- `pool.set_ssh_auto_mode`: Applies SSH auto mode configuration to all pool hosts
36+
37+
### Implementation Details
38+
39+
The enable/disable operations work by directly managing systemd services. The code starts and enables the sshd systemd service to enable SSH access, or stops and disables it to disable SSH access:
40+
41+
```ocaml
42+
Xapi_systemctl.start "sshd"
43+
Xapi_systemctl.enable "sshd"
44+
45+
Xapi_systemctl.stop "sshd"
46+
Xapi_systemctl.disable "sshd"
47+
```
48+
49+
#### SSH Timeout Management
50+
51+
The timeout management uses the scheduler system to automatically disable SSH after a specified period. The function removes any existing disable job from the queue and creates a new one-shot job that will execute the SSH disable operation when the timeout expires. if the XAPI restart during this period, xapi will schedule a new job to disable SSH with remaining time:
52+
53+
```ocaml
54+
let schedule_disable_ssh_job ~__context ~self ~timeout ~auto_mode =
55+
Xapi_stdext_threads_scheduler.Scheduler.remove_from_queue
56+
!Xapi_globs.job_for_disable_ssh ;
57+
Xapi_stdext_threads_scheduler.Scheduler.add_to_queue
58+
!Xapi_globs.job_for_disable_ssh
59+
Xapi_stdext_threads_scheduler.Scheduler.OneShot (Int64.to_float timeout)
60+
(fun () ->
61+
disable_ssh_internal ~__context ~self
62+
)
63+
```
64+
65+
#### Console Idle Timeout
66+
67+
The console idle timeout is configured by writing to a profile script that sets the TMOUT environment variable. The function generates appropriate content based on the timeout value and atomically writes it to the profile script file:
68+
69+
```ocaml
70+
let set_console_idle_timeout ~__context ~self ~value =
71+
let content = match value with
72+
| 0L -> "# Console timeout is disabled\n"
73+
| timeout -> Printf.sprintf "# Console timeout configuration\nexport TMOUT=%Ld\n" timeout
74+
in
75+
Unixext.atomic_write_to_file !Xapi_globs.console_timeout_profile_path 0o0644
76+
(fun fd -> Unix.write fd (Bytes.of_string content) 0 (String.length content))
77+
```
78+
79+
#### SSH Auto Mode
80+
81+
The SSH auto mode is configured by managing the monitoring service. The function updates the database with the auto mode setting and then enables or disables the SSH monitoring daemon accordingly. When auto mode is enabled, it starts the monitoring service and enable SSH service (Always enable SSH service for avoid both XAPI and Monitor service are down, user is still able to start SSH service by reboot host); when disabled, it stops and disables the monitoring service:
82+
83+
```ocaml
84+
let set_ssh_auto_mode ~__context ~self ~value =
85+
Db.Host.set_ssh_auto_mode ~__context ~self ~value ;
86+
if value then (
87+
Xapi_systemctl.enable ~wait_until_success:false !Xapi_globs.ssh_service ;
88+
Xapi_systemctl.enable ~wait_until_success:false !Xapi_globs.ssh_monitor_service ;
89+
Xapi_systemctl.start ~wait_until_success:false !Xapi_globs.ssh_monitor_service
90+
) else (
91+
Xapi_systemctl.stop ~wait_until_success:false !Xapi_globs.ssh_monitor_service ;
92+
Xapi_systemctl.disable ~wait_until_success:false !Xapi_globs.ssh_monitor_service
93+
)
94+
```
95+
96+
### CLI Commands
97+
98+
```bash
99+
# Enable/disable SSH on hosts
100+
xe host-enable-ssh host=<UUID or name>
101+
xe host-disable-ssh host-uuid=<UUID or name>
102+
103+
# Configure timeouts on individual hosts
104+
xe host-param-set uuid=<host-uuid> ssh-enabled-timeout=3600
105+
xe host-param-set uuid=<host-uuid> console-idle-timeout=300
106+
xe host-param-set uuid=<host-uuid> ssh-auto-mode=true
107+
108+
# Query host SSH parameters
109+
xe host-param-get uuid=<host-uuid> param-name=ssh-enabled
110+
xe host-param-get uuid=<host-uuid> param-name=ssh-expiry
111+
xe host-param-get uuid=<host-uuid> param-name=ssh-enabled-timeout
112+
xe host-param-get uuid=<host-uuid> param-name=console-idle-timeout
113+
xe host-param-get uuid=<host-uuid> param-name=ssh-auto-mode
114+
115+
# Enable/disable SSH across pool
116+
xe pool-enable-ssh
117+
xe pool-disable-ssh
118+
119+
# Configure timeouts across pool
120+
xe pool-param-set uuid=<pool-uuid> ssh-enabled-timeout=3600
121+
xe pool-param-set uuid=<pool-uuid> console-idle-timeout=300
122+
xe pool-param-set uuid=<pool-uuid> ssh-auto-mode=true
123+
124+
# Query pool SSH parameters
125+
xe pool-param-get uuid=<pool-uuid> param-name=ssh-enabled
126+
xe pool-param-get uuid=<pool-uuid> param-name=ssh-expiry
127+
xe pool-param-get uuid=<pool-uuid> param-name=ssh-enabled-timeout
128+
xe pool-param-get uuid=<pool-uuid> param-name=console-idle-timeout
129+
xe pool-param-get uuid=<pool-uuid> param-name=ssh-auto-mode
130+
```
131+
132+
## Auto Mode
133+
134+
### Overview
135+
136+
The auto mode feature intelligently manages SSH access based on XAPI health status:
137+
- SSH is automatically enabled when XAPI becomes unhealthy
138+
- SSH is automatically disabled when XAPI is healthy and running normally
139+
140+
When the user enables the SSH service with `enable_ssh` API, SSH auto mode will be turned off.
141+
| SSH service | auto mode |
142+
|-------------|-----------|
143+
| enabled | off |
144+
145+
If SSH auto mode is enabled and XAPI becomes unresponsive, the system will automatically enable the SSH service to allow access.
146+
| auto mode | xapi healthy | SSH service |
147+
|-----------|--------------|-------------|
148+
| on | yes | disable |
149+
| on | no | enable |
150+
| off | NA | NA |
151+
152+
When SSH is temporarily enabled using the ssh-enabled-timeout setting and enable-ssh command, the system preserves the original SSH auto-mode state in cache. During the timeout period, SSH auto-mode is suspended (set to off) to allow SSH access. Once the timeout expires, the system restores the cached auto-mode state - if auto-mode was originally enabled, it will be reactivated and automatically stop the SSH service again
153+
| auto mode before set enable timeout | SSH service before set enable timeout | auto mode during the limited time period | auto mode after enable timeout |
154+
|-----------------------------------|--------------------------------------|----------------------------------------|-------------------------------|
155+
| on | off | off | on |
156+
157+
### Service Architecture
158+
159+
#### Monitoring Daemon
160+
161+
The monitoring daemon (`/opt/xensource/libexec/xapi-state-monitor`) operates continuously:
162+
163+
1. Monitors current SSH service status
164+
2. When auto mode is enabled:
165+
- If XAPI is healthy and SSH is active → Stop SSH
166+
- If XAPI is unhealthy and SSH is inactive → Start SSH
167+
3. Implements retry logic with up to 3 attempts for failed operations
168+
4. Pauses for 60 seconds between health check cycles
169+
170+
### Health Check Integration
171+
172+
The system leverages the existing `xapi-health-check` script for health monitoring:
173+
- Returns 0 when XAPI is healthy
174+
- Returns 1 when XAPI is unhealthy
175+
- Triggers unhealthy status after 20 consecutive failures
176+
177+
### Configuration
178+
179+
#### Default Behavior
180+
181+
- **XenServer 8**: `ssh_auto_mode=false` (SSH is enabled by default)
182+
- **XenServer 9**: `ssh_auto_mode=true` (SSH is disabled by default)
183+
184+
#### Configuration Files
185+
186+
In XS8, the ssh_auto_mode default value will be overridden by the configuration file as below, while in XS9, there is no configuration file, so auto-mode will remain enabled by default.
187+
188+
```bash
189+
# XS8: /etc/xapi.conf.d/ssh-auto-mode.conf
190+
ssh_auto_mode=false
191+
```
192+
193+
## Pool Operations
194+
195+
### Pool Join
196+
197+
When a host joins a pool, the following sequence occurs:
198+
1. The host inherits SSH configuration from the pool coordinator
199+
2. SSH settings are applied before metadata updates
200+
3. The xapi-ssh-monitor service is started if auto mode is enabled
201+
202+
### Pool Eject
203+
204+
When a host is ejected from a pool:
205+
1. The host resets to its default configuration (e.g., in XS8 SSH enabled, no timeout)
206+
2. Default SSH configuration is applied before the host becomes a coordinator
207+
208+
## XAPI Restart Handling
209+
210+
During XAPI startup, the system performs several key operations to handle different restart scenarios:
211+
212+
#### SSH Status Synchronization
213+
The database is updated to reflect the actual SSH service state, ensuring consistency between the database and the running system.
214+
215+
#### Short XAPI Downtime Recovery
216+
When `ssh_enabled_timeout > 0` and `ssh_expiry > current_time`, indicating that XAPI restarted during a temporary SSH disable period:
217+
- The system reschedules the disable SSH job with the remaining time
218+
- This ensures that the original timeout period is maintained even after XAPI restart
219+
220+
#### Extended XAPI Downtime Handling
221+
When a ssh_enabled_timeout is configured, `ssh_expiry < current_time` and the SSH service is currently active, indicating that XAPI was down for an extended period that exceeded the timeout duration:
222+
- SSH is automatically disabled
223+
- SSH auto mode is enabled to ensure continuous SSH availability
224+
225+
This scenario typically occurs when XAPI is not active when the SSH timeout expires, requiring the system to disable SSH and enable auto mode for remains continuously available.
226+
227+
## Error Handling
228+
229+
### Retry Logic
230+
231+
The system implements robust retry mechanisms:
232+
- SSH disable operations are retried up to 3 times
233+
- 5-second intervals are maintained between retry attempts
234+
235+
## Integration Points
236+
237+
### xsconsole Integration
238+
239+
The xsconsole interface has been updated to use XAPI APIs rather than direct systemd commands for consistent with XAPI db status:
240+
- Enable/Disable operations: Calls `host.enable_ssh`/`host.disable_ssh`
241+
- Auto mode configuration: Calls `host.set_ssh_auto_mode`
242+
243+
### Answerfile Support
244+
245+
The following configuration in answerfile can be used, when configure ssh-mode to on, auto-mode will be disabled and SSH will be enabled, when configure ssh-mode to off, auto-mode will be disabled and SSH will be disabled as well, when configure to auto, the auto-mode will be enabled and SSH will be disabled by auto-mode once the XAPI is on:
246+
247+
```xml
248+
<ssh-mode>on|off|auto</ssh-mode>
249+
```
144 KB
Loading

0 commit comments

Comments
 (0)