Created
January 8, 2024 17:37
-
-
Save victortoso/a8da5e2ac967b698b85b3e8a0d33ab89 to your computer and use it in GitHub Desktop.
kubevirt: sidecar: add qemu-vdagent channel with clipboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: v1 | |
kind: ConfigMap | |
metadata: | |
name: clipboard-configmap | |
data: | |
my_script.sh: | | |
#!/usr/bin/python3 | |
import xml.etree.ElementTree as ET | |
import sys | |
def add_source(channel): | |
source = ET.fromstring( | |
""" | |
<source> | |
<mouse mode='client'/> | |
<clipboard copypaste='yes'/> | |
</source> | |
""" | |
) | |
channel.insert(len(channel), source) | |
def add_channel(devices): | |
channel = ET.fromstring( | |
""" | |
<channel type='qemu-vdagent'> | |
<target type='virtio' name='com.redhat.spice.0'/> | |
<address type='virtio-serial' controller='1' bus='0' port='3'/> | |
<source> | |
<mouse mode='client'/> | |
<clipboard copypaste='yes'/> | |
</source> | |
</channel> | |
""" | |
) | |
devices.insert(len(devices), channel) | |
def add_client_clipboard(domainxml): | |
root = ET.fromstring(domainxml) | |
devices = root.find("devices") | |
for channel in devices.findall("channel"): | |
ctype = channel.attrib.get("type") | |
if ctype is None or ctype != "qemu-vdagent": | |
continue | |
# There is already qemu-vdagent channel definition | |
source = channel.find("source") | |
if source is not None: | |
# Source already exists. Delete its content and | |
# Add content with clipboard enabled. | |
channel.remove(source) | |
add_source(channel) | |
ET.dump(root) | |
return | |
add_channel(devices) | |
ET.dump(root) | |
if __name__ == "__main__": | |
add_client_clipboard(sys.argv[4]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
apiVersion: kubevirt.io/v1 | |
kind: VirtualMachineInstance | |
metadata: | |
annotations: | |
hooks.kubevirt.io/hookSidecars: '[{"args": ["--version", "v1alpha3"], "image":"kubevirt/sidecar-shim:20240108_99b6c4bdb", | |
"configMap": {"name": "clipboard-configmap","key": "my_script.sh", "hookPath": "/usr/bin/onDefineDomain"}}]' | |
labels: | |
special: vmi-pvc | |
name: vmi-pvc | |
spec: | |
domain: | |
devices: | |
disks: | |
- disk: | |
bus: virtio | |
name: pvcdisk | |
resources: | |
requests: | |
memory: 1Gi | |
terminationGracePeriodSeconds: 0 | |
volumes: | |
- name: pvcdisk | |
persistentVolumeClaim: | |
claimName: fedora-pvc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment