Skip to content

Commit ac9b387

Browse files
ghaf-vms: similar to microvm cmdline tool
Adds functionality to host to query the status of the vms that are on the system. When using the storeOnDisk option, there is no link created to the current system so the `microvm -l` will not work and just stops. Ensure that it is possible to see at a glance which vm is actually running/stopped. Signed-off-by: Brian McGillion <[email protected]>
1 parent ff38cbc commit ac9b387

File tree

5 files changed

+203
-0
lines changed

5 files changed

+203
-0
lines changed

docs/astro.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export default defineConfig({
9797
"ghaf/dev/ref/kill_switch",
9898
"ghaf/dev/ref/wireguard-gui",
9999
"ghaf/dev/ref/yubikey",
100+
"ghaf/dev/ref/ghaf-vms",
100101
],
101102
},
102103
{
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
title: Ghaf VMs Status Tool
3+
description: CLI tool to monitor and manage MicroVM status in Ghaf.
4+
---
5+
6+
## Overview
7+
8+
`ghaf-vms` is a command-line utility in Ghaf that provides real-time status information about running MicroVMs on the host system. It queries systemd to determine the state of each configured MicroVM and displays the information in a color-coded, easy-to-read format.
9+
10+
This tool is particularly useful for:
11+
- Checking which VMs are running or stopped
12+
- Debugging VM startup issues
13+
- Monitoring the overall health of the MicroVM architecture
14+
- Quick system status overview without navigating through systemd commands
15+
16+
---
17+
18+
## Usage & Commands
19+
20+
Run `ghaf-vms` on the Ghaf host to view VM status. General usage:
21+
22+
```sh
23+
[ghaf@ghaf-host:~]$ ghaf-vms [OPTIONS]
24+
```
25+
26+
### Options
27+
28+
* `-l` — List all configured MicroVMs and their current status (default behavior)
29+
* `-h` — Show usage instructions
30+
31+
### Examples
32+
33+
#### List all VMs and their status
34+
35+
```bash
36+
[ghaf@ghaf-host:~]$ ghaf-vms -l
37+
```
38+
39+
Or simply:
40+
41+
```bash
42+
[ghaf@ghaf-host:~]$ ghaf-vms
43+
```
44+
45+
Example output:
46+
47+
```
48+
VM Status:
49+
=========
50+
audio-vm: running
51+
business-vm: running
52+
chrome-vm: running
53+
gui-vm: running
54+
ids-vm: running
55+
net-vm: running
56+
zathura-vm: stopped
57+
```
58+
59+
### Status Indicators
60+
61+
The tool displays VMs with color-coded status indicators:
62+
63+
| Status | Color | Description |
64+
|--------|-------|-------------|
65+
| `RUNNING` | Green | VM is currently active and running |
66+
| `STOPPED` | Red | VM is configured but not running |
67+
| `FAILED` | Bold Red | VM service failed to start |
68+
| `ACTIVATING` | Yellow | VM is in the process of starting |
69+
| Other states | Cyan | VM is in another systemd state (e.g., deactivating, reloading) |
70+
71+
72+
---
73+
74+
## See Also
75+
76+
- [Creating Application VM](/ghaf/dev/ref/creating_appvm/) — How to create new MicroVMs
77+
- [MicroVM Architecture](/ghaf/overview/arch/system-architecture/) — Understanding Ghaf's VM architecture
78+
- [Troubleshooting](/ghaf/dev/troubleshooting/) — General troubleshooting guidance

modules/development/dt-host.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ in
2727
pkgs.cryptsetup
2828
# check hardware info
2929
pkgs.lshw
30+
# List microvm status
31+
pkgs.ghaf-vms
3032
]);
3133
};
3234
}

packages/own-pkgs-overlay.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
ghaf-installer = final.callPackage ./pkgs-by-name/ghaf-installer/package.nix { };
1515
ghaf-open = final.callPackage ./pkgs-by-name/ghaf-open/package.nix { };
1616
ghaf-powercontrol = final.callPackage ./ghaf-powercontrol/package.nix { };
17+
ghaf-vms = final.callPackage ./pkgs-by-name/ghaf-vms/package.nix { };
1718
hardware-scan = final.callPackage ./pkgs-by-name/hardware-scan/package.nix { };
1819
make-checks = final.callPackage ./pkgs-by-name/make-checks/package.nix { };
1920
memsocket = final.callPackage ./pkgs-by-name/memsocket/package.nix { };
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# SPDX-FileCopyrightText: 2022-2026 TII (SSRC) and the Ghaf contributors
2+
# SPDX-License-Identifier: Apache-2.0
3+
{
4+
writeShellApplication,
5+
lib,
6+
busybox,
7+
systemd,
8+
}:
9+
let
10+
colors = {
11+
normal = "\\033[0m";
12+
red = "\\033[0;31m";
13+
green = "\\033[0;32m";
14+
yellow = "\\033[0;33m";
15+
boldRed = "\\033[1;31m";
16+
};
17+
colored = color: text: "${colors.${color}}${text}${colors.normal}";
18+
in
19+
writeShellApplication {
20+
name = "ghaf-vms";
21+
22+
runtimeInputs = [
23+
busybox
24+
systemd
25+
];
26+
27+
text = ''
28+
MICROVM_DIR="/var/lib/microvms"
29+
30+
# Function to display usage
31+
show_usage() {
32+
echo "Usage: ghaf-vms [OPTIONS]"
33+
echo ""
34+
echo "Options:"
35+
echo " -l, --list List all VMs and their status"
36+
echo " -h, --help Show this help message"
37+
echo ""
38+
echo "If no option is specified, -l/--list is assumed."
39+
}
40+
41+
# Function to list VMs
42+
list_vms() {
43+
if [ ! -d "$MICROVM_DIR" ]; then
44+
echo -e "${colored "red" "Error: MicroVM directory $MICROVM_DIR does not exist"}"
45+
exit 1
46+
fi
47+
48+
# Check if there are any VMs
49+
if [ -z "$(ls -A "$MICROVM_DIR" 2>/dev/null)" ]; then
50+
echo "No VMs found in $MICROVM_DIR"
51+
exit 0
52+
fi
53+
54+
echo "VM Status:"
55+
echo "=========="
56+
57+
# Iterate through each VM directory
58+
for vm_dir in "$MICROVM_DIR"/*; do
59+
if [ -d "$vm_dir" ]; then
60+
vm_name=$(basename "$vm_dir")
61+
62+
# Only allow alphanumeric, hyphens, and underscores
63+
if [[ ! "$vm_name" =~ ^[a-zA-Z0-9_-]+$ ]]; then
64+
echo -e "${colored "yellow" "Warning: Skipping invalid VM name: $vm_name"}" >&2
65+
continue
66+
fi
67+
68+
service_name="microvm@$vm_name.service"
69+
70+
# Get service status
71+
if systemctl is-active --quiet "$service_name"; then
72+
status="${colored "green" "running"}"
73+
elif systemctl is-enabled --quiet "$service_name" 2>/dev/null; then
74+
status="${colored "red" "stopped"}"
75+
else
76+
# Check if service exists but is failed or in other state
77+
if systemctl status "$service_name" &>/dev/null; then
78+
service_state=$(systemctl show -p ActiveState --value "$service_name")
79+
case "$service_state" in
80+
failed)
81+
status="${colored "boldRed" "failed"}"
82+
;;
83+
*)
84+
status="${colored "yellow" "$service_state"}"
85+
;;
86+
esac
87+
else
88+
status="${colored "yellow" "unknown"}"
89+
fi
90+
fi
91+
92+
# Print VM status
93+
echo -e "$vm_name: $status"
94+
fi
95+
done
96+
}
97+
98+
while getopts ":lh" opt; do
99+
case $opt in
100+
l) list_vms ;;
101+
h)
102+
show_usage
103+
exit 0
104+
;;
105+
*)
106+
show_usage
107+
exit 1
108+
;;
109+
esac
110+
done
111+
112+
# If no arguments are provided, list VMs
113+
list_vms
114+
'';
115+
116+
meta = {
117+
description = "List status of Ghaf MicroVMs";
118+
license = lib.licenses.asl20;
119+
platforms = lib.platforms.linux;
120+
};
121+
}

0 commit comments

Comments
 (0)