teg
by Jitter00
README.md
# teg
teg (test env generator) builds network labs on a libvirt/QEMU/KVM host: Juniper vSRX and vMX,
FortiGate and Ubuntu VMs, linked by isolated point-to-point networks. Use it from the command
line, or let Claude Code (or any MCP client) drive it through teg's MCP server.
## Prerequisites
- libvirt with QEMU/KVM, and `genisoimage` (for cloud-init VMs)
- Your user in the `libvirt` group. teg does everything through libvirt, so it never needs sudo.
- Python 3.10+, plus `libvirt-dev` and `pkg-config` (`libvirt-python` builds from source)
- Base images in a libvirt storage pool (see [Base images](#base-images))
> **Note:** teg does not ship any images. Names such as `default_srx.qcow2` and
> `default_switch.qcow2` are not vendor files: they are ordinary vendor images (e.g.
> `junos-media-vsrx-x86-64-vmdisk-20.4R3.8.qcow2`) that you prepare once with a minimal starting
> config and save under that name. See [Base images](#base-images).
## Installation
Install a tagged release (replace `vX.Y.Z` with a tag from [Releases](https://github.com/Jitter00/teg/releases)):
```bash
uv tool install git+https://github.com/Jitter00/teg@vX.Y.Z
# or: pipx install git+https://github.com/Jitter00/teg@vX.Y.Z
```
Either command puts `teg` in `~/.local/bin`, which is on your `PATH`.
From a clone, for development:
```bash
uv tool install --editable . # `teg` on your PATH; code changes take effect immediately
uv sync # .venv with teg and the dev tools, for `uv run pytest` etc.
```
## Use with Claude Code
The repository is also a Claude Code plugin marketplace. With `teg` installed (above), in Claude Code:
```
/plugin install teg --marketplace Jitter00/teg
```
(or `/plugin marketplace add Jitter00/teg`, then `/plugin install teg@teg`). The plugin brings:
- **MCP server** (`teg mcp`): tools to list, create, start, stop and remove VMs and lab networks,
and to attach and detach links. Deliberately narrower than the CLI: no file-path parameters,
`stop_vm` needs explicit names, and `remove_vm` only removes VMs teg created.
- **Skill** `teg-lab`: how to plan a topology, interface order per platform, memory limits.
- **Hooks**: removing a VM or network always asks you first, naming what will be deleted; every
change is logged to `~/.local/state/teg/audit.log`.
Then ask, for example: *"Build two vSRX connected by one link, plus an Ubuntu host behind the first."*
Other MCP clients can run the server directly with the command `teg mcp` (stdio).
## Security
- **Access to libvirt is root-equivalent.** With `qemu:///system`, being in the `libvirt` group
lets you (and anything running as you, including `teg mcp`) define VMs that mount any host
file or device. Only add users you'd trust with root, and run teg on a lab host, not on a
machine holding data you care about.
- **The MCP server is narrower than the CLI, not a sandbox.** It has no file-path parameters,
only attaches VMs to `default` and `p2p_<n>` networks, and `remove_vm` refuses VMs without
teg's metadata. `start_vm`, `stop_vm`, `attach_network` and `detach_network` work on any VM
on the host. teg's metadata guards against mistakes, not against someone with libvirt
access, who can add it to any VM.
- **Ubuntu VMs give your user passwordless sudo.** The cloud-init template locks the password,
disables SSH password login and adds only your public key. Use `--user-data` for anything
stricter.
- **Guest output is untrusted.** Addresses and other values that come from guests are reported
as data; the MCP server tells clients not to treat them as instructions.
To report a vulnerability, see [SECURITY.md](SECURITY.md).
## Base images
VMs are thin qcow2 overlays on base images kept in a libvirt storage pool (by default the pool
`default`, usually `/var/lib/libvirt/images/`):
| Device type | Base image | Specs |
|-------------|------------|-------|
| `srx` | `default_srx.qcow2` | Juniper vSRX: 4 GB RAM, 2 vCPUs, virtio NICs |
| `switch` | `default_switch.qcow2` | Juniper vMX (EVPN/VXLAN): 16 GB RAM, 4 vCPUs, virtio NICs |
| `fortios` | `fortios.qcow2` | FortiOS KVM image: 2 GB RAM, 1 vCPU, e1000 NICs |
| `ubuntu24` | `default_ubuntu24.qcow2` | Ubuntu 24.04 cloud image: 2 GB RAM, 2 vCPUs, 20G disk, cloud-init |
Get the Juniper and FortiOS images from the vendor and the Ubuntu cloud image from
cloud-images.ubuntu.com, and copy them into the pool once (e.g. `sudo cp ... /var/lib/libvirt/images/`).
`teg list_device_types` shows which ones teg can find.
The `default_*` Juniper images are the vendor images plus a little initial config, so every VM
cloned from them is reachable straight away. To make one, boot the vendor image once (for
example `junos-media-vsrx-x86-64-vmdisk-20.4R3.8.qcow2` for `srx`), set a root password, add your
SSH public key, commit, shut it down cleanly, and save the disk in the pool as
`default_srx.qcow2` (likewise `default_switch.qcow2` from the vMX image). An untouched vendor
image works too, but you then do that first-login setup on the console of every new VM. If you
would rather keep your own file names, map them under `[images]` in the
[configuration](#configuration).
## Configuration
Optional, in `~/.config/teg/config.toml` (or the file named by `$TEG_CONFIG`):
```toml
libvirt_uri = "qemu:///system" # $TEG_LIBVIRT_URI overrides it
storage_pool = "default"
[images] # device type -> base image volume in the pool
ubuntu24 = "noble-server-cloudimg-amd64.qcow2"
```
Anything not set keeps the defaults shown above.
## Creating VMs
```bash
teg create_vm srx1 --device-type srx
teg create_vm vmx1 --device-type switch
teg create_vm fw1 --device-type fortios
teg create_vm host1 --device-type ubuntu24
```
VM names must be valid hostnames: 1-63 letters, digits and hyphens, starting and ending with a
letter or digit. The name becomes the guest's hostname and part of its disk volume's name.
`create_vm` checks the name, the networks, free memory and the base image before changing
anything, and cleans up after itself if a later step fails.
### Ubuntu 24.04 (cloud-init)
No installer runs. teg builds a cloud-init NoCloud seed (`<name>-seed.iso` in the pool) from
`teg/cloud-init/user-data`. It creates a user named after you, with passwordless sudo and your SSH
public key (`~/.ssh/id_ed25519.pub`, falling back to `~/.ssh/id_rsa.pub`). Once it has booted:
```bash
teg show_vm host1 # shows the IP address and the ssh command
```
Options: `--ssh-key PATH` picks another public key; `--user-data PATH` uses your own template, in
which `${hostname}`, `${username}` and `${ssh_key}` are replaced.
## Networks
Every VM's first interface is on libvirt's `default` network (NAT, DHCP, management). Links between
VMs are isolated point-to-point networks, `p2p_1`, `p2p_2`, ...:
```bash
teg create_p2p_network --count 2 # --reuse: hand out unused p2p networks first
teg create_vm srx1 --device-type srx --networks p2p_1 p2p_2
teg create_vm srx2 --device-type srx --networks p2p_1
teg list_lab_networks # which VMs are on which network
```
Interface order follows the network order: on vSRX, `fxp0` is `default`, then `ge-0/0/0`,
`ge-0/0/1`, ...
Changing the links of an existing VM:
```bash
teg attach_network srx2 p2p_2 # add as the next interface
teg detach_network srx2 p2p_1
teg update_networks srx1 --networks p2p_2 p2p_1 # set all links at once, in this order
teg remove_p2p_network p2p_2 # refused while a VM still uses it
```
`attach_network` and `detach_network` change the VM's definition; a running VM picks the change
up on its next start (`--restart` restarts it now). `update_networks` restarts a running VM.
## VM lifecycle
```bash
teg list_vms
teg start_vm srx1 vmx1
teg stop_vm srx1 # graceful shutdown, forced off after 60 s
teg stop_vm # all running VMs
teg start_vm 'vsrx*' # start, stop and remove accept glob patterns
teg remove_vm srx1 # stop, undefine, delete its disk and seed volumes
teg remove_vm 'vsrx*' # every VM whose name matches the glob pattern
```
`remove_vm` lists the volumes it will delete and asks first (`-y` skips the prompt). It only deletes
volumes directly in the storage pool that belong to that VM, never base images or another VM's files.
`start_vm`, `stop_vm` and `remove_vm` accept glob patterns (`*`, `?`, `[...]`) and mix them freely
with plain names. Quote patterns so the shell does not expand them against files in the current
directory. A pattern matches any VM on the libvirt host, not only those teg created; `remove_vm`
shows the VMs it expanded to in its prompt, so read the list before confirming.
## Command reference
```bash
teg --version
teg [--json] list_vms
teg [--json] show_vm <name>
teg [--json] list_device_types
teg create_vm <name> [--device-type srx|switch|fortios|ubuntu24] [--networks NET ...] [--ssh-key PATH] [--user-data PATH]
teg start_vm <name|pattern> [name|pattern ...]
teg stop_vm [name|pattern ...]
teg remove_vm <name|pattern> [name|pattern ...] [-y]
teg list_networks <name>
teg update_networks <name> [--networks NET ...]
teg attach_network <name> <network> [--restart]
teg detach_network <name> <network> [--restart]
teg create_p2p_network [--count N] [--reuse]
teg remove_p2p_network <network> [network ...]
teg [--json] list_lab_networks
teg mcp # MCP server on stdio
```
`--json` prints machine-readable output for any command.
## Development
```bash
uv run pytest # tests (use libvirt's test driver, no KVM needed)
uv run ruff check . && uv run ruff format . # lint and format
claude plugin validate ./plugin # the Claude Code plugin
```
Work happens in branches and pull requests; the PR title decides the next version.
See [CONTRIBUTING.md](CONTRIBUTING.md). Planned work: [TODO.md](TODO.md) and [PLAN.md](PLAN.md).
## License
MIT, see [LICENSE](LICENSE).
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues