Commit c30b91b4 by Zoltan Karsa

create demo python project

parent 86069d39
# GPU Virtualizáció
A gpu kártyák virtualizációját megnehezíti, hogy a gyártók bár azonos alapokon nyugvó kártyákat fejlesztenek ki, a hozzájuk tartozó driver-ek többnyire zártak és nem nyújtanak egységes képet a kezeléshez. Továbbá a kártyák fejlődésével minden gyártó a maga által kitalált technológiai irányvonalat / újítást követi, ami nem teszi lehetővé ezen kártyák egységes kezelését.
## Osztályozás
A virtuálizációhoz hasonlóan itt is a használt technológiától függően különböző fajta GPU virtualizációkról beszélhetünk.
......@@ -7,13 +9,16 @@ A virtuálizációhoz hasonlóan itt is a használt technológiától függően
- **GPU passthrough** (kakukktojás): Nem virtualizációs technológia, de a host os lehetővé teszi egy virtuális gépnek a közvetlen PCI bus-on történő kommunikációt a kártyával. Hátránya: egyszerre csak egy VM használhatja, a használat kötött. Kevés veszteség.
- **távoli API alapú**: A virtuális gépen speciális API könyvtár segítségével kommunikálunk a host-on keresztül a kártyával. Az API könyvtár egy wrapperhez hasonlítható, a rendes GPU hívásokat a wapper továbbítja a host-ra és az ottani API (CUDA, OpenCL, OpenGL) adja meg az eredményt.
Mivel a módszer megkerüli a hypervisor réteget, ezért elenyésző többletköltséggel jár. A támogatott API-k rögzítettek, más API használata esetén a host-ot is újra kell konfigurálni. Példák: GVirtuS (CUDA), DS-CUDA (CUDA)
Mivel a módszer megkerüli a hypervisor réteget, ezért elenyésző többletköltséggel jár. A támogatott API-k rögzítettek, más API használata esetén a host-ot is újra kell konfigurálni, vagy frissíteni kell a wrapper könyvtárat, ha új funkciók kerülnek hozzáadásra a host GPU könyvtárához. Példák: GVirtuS (CUDA), DS-CUDA (CUDA), de nemcsak GPGPU wrapper-ek vannak.
![távoli API](img/remoteAPI.png)
- **Para- vagy teljes virtualizáció**:
- **hardweres virtualizáció**:
![Paravirtualizáció vagy teljes virtualizációs megoldás](img/fullvirt.png)
- **hardveres virtualizáció**:
![hardveres virtualizáció](img/hardwaresup.png)
......
echo "$1" > /sys/class/mdev_bus/$2/mdev_supported_types/$3/create
mdevctl define --auto --uuid $1
\ No newline at end of file
echo "1" > /sys/class/mdev_bus/$2/mdev_supported_types/$3/devices/$1/remove
\ No newline at end of file
<domain type='kvm'>
<name>{{name}}</name>
<uuid>{{uuid}}</uuid>
<memory unit='GB'>{{ram}}</memory>
<currentMemory unit='GB'>{{ram}}</currentMemory>
<vcpu>{{vcpu}}</vcpu>
<os>
<type arch='i686' machine='pc'>hvm</type>
</os>
<devices>
<emulator>/usr/bin/qemu-system-x86_64</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='{{ disk }}'/>
<target dev='hda'/>
<boot order='1'/>
</disk>
{% if boot_iso %}
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<source file='{{ iso }}' index='1'/>
<target dev='hdc' bus='ide'/>
<readonly/>
<boot order='2'/>
</disk>
{% endif %}
<interface type='network'>
<source network='default'/>
</interface>
<graphics type='vnc' port='-1'/>
{% if vgpu %}
<hostdev mode='subsystem' type='mdev' model='vfio-pci'>
<source>
<address uuid='{{ uuid }}'/>
</source>
</hostdev>
{% endif %}
</devices>
</domain>
\ No newline at end of file
import libvirt
import sys
import uuid
from jinja2 import Environment, select_autoescape, FileSystemLoader
import subprocess
try:
conn = libvirt.open("qemu:///system")
except libvirt.libvirtError:
print('Failed to open connection to the hypervisor')
sys.exit(1)
file_loader = FileSystemLoader('templates')
env = Environment(autoescape=select_autoescape(
enabled_extensions=('xml'),
default_for_string=True,
), loader=file_loader)
params = {
"name": "demo",
"uuid": uuid.uuid4(),
"ram": 4, # in GB
"vcpu": 2,
"disk": "/data/vms/s1/s1-ubuntu.qcow2",
"boot_iso": True,
"iso": "/data/img/ubuntu-22.04.2-live-server-amd64.iso",
"vgpu": True,
"vgpu_type": "nvidia-1",
"mdev_bus": "0000\:01\:00.0"
}
template = env.get_template("demovm.xml")
vmxml = template.render(
**params
)
print(vmxml)
input("Correct? ")
id = params["uuid"]
bus = params["mdev_bus"]
vgpu = params["vgpu_type"]
if params["vgpu"]:
print("Allocating VGPU instance for the VM")
try:
subprocess.run([f"sudo bash create_vgpu.sh {id} {bus} {vgpu}"], check=True, shell=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))
try:
dom = conn.defineXML(vmxml)
if not dom:
raise SystemExit("Failed to define a domain from an XML definition")
print("Start VM")
if dom.create() < 0:
raise SystemExit("Can not boot guest domain")
except libvirt.libvirtError:
print('Failed to find the main domain')
sys.exit(1)
print("Domain 0: id %d running %s" % (dom.ID(), dom.OSType()))
print(dom.info())
print("Type stop, to shutdown vm!")
cmd = input()
while cmd != "stop":
cmd = input()
try:
print("Stop VM")
if dom.destroy() < 0:
raise SystemExit("Can not stop guest domain")
except libvirt.libvirtError:
print('Failed to find the main domain')
sys.exit(1)
if params["vgpu"]:
print("Deallocating VGPU instance from the VM")
try:
subprocess.run([f"sudo bash remove_vgpu.sh {id} {bus} {vgpu}"], check=True, shell=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))
conn.close()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment