Skip to content

Commit 4df4a07

Browse files
authored
implement a detect_virt grain (#387)
This is useful for things we may not want or be able to run when operating in a docker container. Specific example is systemd-timesyncd, which refuses to start in a container: ``` [Unit] Description=Network Time Synchronization Documentation=man:systemd-timesyncd.service(8) ConditionCapability=CAP_SYS_TIME ConditionVirtualization=!container DefaultDependencies=no ... ``` Note the `ConditionVirtualization` blocking. Useful state might be something like: ``` systemd-timesyncd: pkg: - installed service: - enable: True {% if grains["detect_virt"] not in ["docker"] %} - running {% endif %} ``` Which would _enable_ the service but not fail when it fails to start.
1 parent f7803fd commit 4df4a07

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

salt/_grains/detect_virt.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python
2+
3+
import subprocess
4+
5+
6+
def main():
7+
try:
8+
result = subprocess.run(
9+
["/usr/bin/systemd-detect-virt"], stdout=subprocess.PIPE, check=True
10+
).stdout.rstrip()
11+
except FileNotFoundError:
12+
result = "unknown"
13+
return {"detect_virt": result}

0 commit comments

Comments
 (0)