Skip to content

Commit 9c2b2e7

Browse files
committed
libvirt: speed up kcli list vm
1 parent a8eacc4 commit 9c2b2e7

3 files changed

Lines changed: 35 additions & 6 deletions

File tree

kvirt/providers/kvm/__init__.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3+
from concurrent.futures import ThreadPoolExecutor
34
from getpass import getuser
45
from kvirt.defaults import IMAGES, METADATA_FIELDS, UBUNTUS
56
from kvirt import common
@@ -1727,13 +1728,11 @@ def status(self, name):
17271728
return status[vm.isActive()]
17281729

17291730
def list(self):
1730-
vms = []
17311731
conn = self.conn
1732-
for vm in conn.listAllDomains(0):
1733-
try:
1734-
vms.append(self.info(vm.name(), vm=vm))
1735-
except:
1736-
continue
1732+
domains = conn.listAllDomains(0)
1733+
with ThreadPoolExecutor(max_workers=len(domains) or 1) as executor:
1734+
futures = {executor.submit(self.info, vm.name(), vm=vm): vm for vm in domains}
1735+
vms = [f.result() for f in futures if f.exception() is None]
17371736
return sorted(vms, key=lambda x: x['name'])
17381737

17391738
def console(self, name, tunnel=False, tunnelhost=None, tunnelport=22, tunneluser='root', web=False):

skills/kcli-vm-operations/SKILL.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ name: kcli-vm-operations
33
description: Guides VM lifecycle operations with kcli. Use when creating, managing, or troubleshooting virtual machines across providers.
44
---
55

6+
> **Important**: Before creating a VM, always run `kcli create vm -h` to check the latest available flags and examples.
7+
68
# kcli VM Operations
79

810
## VM Lifecycle Commands
@@ -17,6 +19,21 @@ kcli create vm -i centos9stream -P memory=4096 -P numcpus=4 -P disks=[20,50] myv
1719

1820
# From profile
1921
kcli create vm -p myprofile myvm
22+
23+
# Create multiple VMs at once with -c (count)
24+
kcli create vm -c 3 myvm
25+
26+
# Create 3 VMs to emulate baremetal
27+
kcli create vm -P start=false -P memory=20480 -P numcpus=16 -P disks=[200] -P uefi=true -P nets=[default] -c 3 myvm
28+
29+
# Create a VM with a static IP
30+
kcli create vm -i centos9stream -P nets=['{"name":"default","ip":"192.168.122.250","netmask":"24","gateway":"192.168.122.1"}'] myvm
31+
32+
# Create a VM with a DNS reservation
33+
kcli create vm -i centos9stream -P nets=['{"name":"default","reservedns":"true"}'] myvm
34+
35+
# Create a VM with a DHCP reservation
36+
kcli create vm -i centos9stream -P nets=['{"name":"default","ip":"192.168.122.250","reserveip":"true"}'] myvm
2037
```
2138

2239
### List VMs

skills/kcli/SKILL.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ description: Comprehensive guide for kcli usage. Use when creating VMs, deployin
77

88
kcli is a unified CLI for managing virtual infrastructure across multiple providers (Libvirt/KVM, AWS, GCP, Azure, vSphere, KubeVirt, OpenStack, oVirt, Proxmox, Hetzner, IBM Cloud).
99

10+
## Remote Host Optimization
11+
12+
When the user asks to run a kcli command against a specific host that is **not** defined as a client in `~/.kcli/config.yml`, run the command directly on that host via SSH instead of trying to use `-C`:
13+
14+
```bash
15+
# Host not in config — run via SSH
16+
ssh root@myhost "kcli create vm -i centos9stream myvm"
17+
ssh root@myhost "kcli list vm"
18+
ssh root@myhost "kcli create plan -f plan.yml myplan"
19+
```
20+
21+
This is both faster (libvirt calls stay local on the host) and avoids needing to add a client entry.
22+
1023
## Quick Reference
1124

1225
```bash

0 commit comments

Comments
 (0)