Skip to content

Commit 42718af

Browse files
josecastillolemakarmab
authored andcommitted
Add option to use slirp for usermode (#827)
Signed-off-by: Jose Castillo Lema <[email protected]>
1 parent e45fcaf commit 42718af

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

docs/index.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,14 @@ kcli create vm -i centos9stream -P nets=['{"name": "default", "type": "e1000"}']
836836

837837
Again, both syntaxes can be combined
838838

839+
It is also possible to leverage user mode networking with a couple of plugins, `slirp` and `passt`, and you can ssh into them
840+
841+
```Shell
842+
kcli create vm -i centos9stream -P usermode=true vm-passt
843+
```
844+
845+
By default, passt is used as backend unless not available, in which case we fail back to slirp
846+
839847
### Injecting files
840848

841849
You can inject a list of `files` in your vms. For instance, to inject a file named myfile.txt, use

docs/index.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,14 @@ Or change the nic driver
862862
863863
Again, both syntaxes can be combined
864864

865+
It is also possible to leverage user mode networking with a couple of plugins, ``slirp`` and ``passt``, and you can ssh into them
866+
867+
.. code:: shell
868+
869+
kcli create vm -i centos9stream -P usermode=true vm-passt
870+
871+
By default, passt is used as backend unless not available, in which case we fail back to slirp
872+
865873
Injecting files
866874
~~~~~~~~~~~~~~~
867875

kvirt/keywords.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ tunneldir: Specific directory on the tunnel host where to store ignition files
139139
tunnelhost: Specific host to use for tunneling console, ssh and scp commands
140140
tunnelport: Specific port to use for tunneling console, ssh and scp commands
141141
tunneluser: Specific user to use for tunneling console, ssh and scp commands
142+
usermode: |
143+
Enable user mode networking for the VM. When enabled, the VM will use user mode networking instead of bridged or NAT networking.
144+
Automatically sets up port forwarding for SSH (port 22) to a random host port.
142145
virttype: |
143146
Only used for libvirt where it evaluates to kvm if acceleration shows in capabilities, or qemu emulation otherwise.
144147
If a value is provided, it must be either kvm, qemu, xen or lxc

kvirt/providers/kvm/__init__.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ def create(self, name, virttype=None, profile='kvirt', flavor=None, plan='kvirt'
233233
self.url = 'qemu:///session'
234234
userport = common.get_free_port()
235235
metadata['userport'] = userport
236+
metadata['usermode_backend'] = 'passt' if which('passt') is not None else 'slirp'
236237
usermode = True
237238
if self.exists(name):
238239
return {'result': 'failure', 'reason': f"VM {name} already exists"}
@@ -736,9 +737,15 @@ def create(self, name, virttype=None, profile='kvirt', flavor=None, plan='kvirt'
736737
if netname in ovsnetworks:
737738
ovs = True
738739
if usermode:
739-
iftype = 'user'
740-
sourcexml = "<backend type='passt'/>"
741-
sourcexml += f"<portForward proto='tcp'><range start='{userport}' to='22'/></portForward>"
740+
usermode_backend = 'passt' if which('passt') is not None else 'slirp'
741+
if usermode_backend == 'slirp':
742+
# Skip libvirt interface for slirp - handled via QEMU command line
743+
continue
744+
else:
745+
# Default to passt
746+
iftype = 'user'
747+
sourcexml = "<backend type='passt'/>"
748+
sourcexml += f"<portForward proto='tcp'><range start='{userport}' to='22'/></portForward>"
742749
elif netname in networks:
743750
iftype = 'network'
744751
sourcexml = f"<source network='{netname}'/>"
@@ -1097,7 +1104,8 @@ def create(self, name, virttype=None, profile='kvirt', flavor=None, plan='kvirt'
10971104
vcpuxml = f"<vcpu>{numcpus}</vcpu>"
10981105
clockxml = "<clock offset='utc'/>"
10991106
qemuextraxml = ''
1100-
if ignition or macosx or tpm or qemuextra is not None or nvmedisks:
1107+
slirp = usermode and which('passt') is None
1108+
if ignition or macosx or tpm or qemuextra is not None or nvmedisks or slirp:
11011109
namespace = "xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'"
11021110
ignitionxml = ""
11031111
if ignition:
@@ -1139,12 +1147,19 @@ def create(self, name, virttype=None, profile='kvirt', flavor=None, plan='kvirt'
11391147
<qemu:arg value='file={diskpath},format=qcow2,if=none,id=NVME{index}'/>
11401148
<qemu:arg value='-device'/>
11411149
<qemu:arg value='nvme,drive=NVME{index},serial=nvme-{index}'/>""".format(index=index, diskpath=diskpath)
1150+
slirpxml = ""
1151+
if slirp:
1152+
slirpxml = f"""<qemu:arg value='-netdev'/>
1153+
<qemu:arg value='user,id=net0,hostfwd=tcp::{userport}-:22'/>
1154+
<qemu:arg value='-device'/>
1155+
<qemu:arg value='virtio-net-pci,netdev=net0,addr=0x10'/>"""
11421156
qemuextraxml = """<qemu:commandline>
11431157
{ignitionxml}
11441158
{macosxml}
11451159
{freeformxml}
11461160
{nvmexml}
1147-
</qemu:commandline>""".format(ignitionxml=ignitionxml, macosxml=macosxml, freeformxml=freeformxml, nvmexml=nvmexml)
1161+
{slirpxml}
1162+
</qemu:commandline>""".format(ignitionxml=ignitionxml, macosxml=macosxml, freeformxml=freeformxml, nvmexml=nvmexml, slirpxml=slirpxml)
11481163
sharedxml = ""
11491164
if sharedfolders:
11501165
for folder in sharedfolders:

0 commit comments

Comments
 (0)