Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE3
/
gpuvirt
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
6eeb5839
authored
Aug 16, 2023
by
Zoltan Karsa
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
arguments parser
parent
49a28fd7
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
60 additions
and
28 deletions
+60
-28
vm.py
+60
-28
No files found.
vm.py
View file @
6eeb5839
import
libvirt
import
sys
import
sys
,
getopt
,
os
import
uuid
from
jinja2
import
Environment
,
select_autoescape
,
FileSystemLoader
import
subprocess
from
utils
import
fwd_port
,
show_ip
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
(
...
...
@@ -33,44 +27,52 @@ params = {
"boot_menu"
:
"yes"
,
}
template
=
env
.
get_template
(
"demovm.xml"
)
def
start_vm
(
params
):
vmxml
=
template
.
render
(
template
=
env
.
get_template
(
"demovm.xml"
)
vmxml
=
template
.
render
(
**
params
)
)
print
(
vmxml
)
print
(
vmxml
)
input
(
"Correct? "
)
input
(
"Correct? "
)
try
:
conn
=
libvirt
.
open
(
"qemu:///system"
)
except
libvirt
.
libvirtError
:
print
(
'Failed to open connection to the hypervisor'
)
sys
.
exit
(
1
)
id
=
params
[
"uuid"
]
bus
=
params
[
"mdev_bus"
]
vgpu
=
params
[
"vgpu_type"
]
id
=
params
[
"uuid"
]
if
params
[
"vgpu"
]:
if
params
[
"vgpu"
]:
bus
=
params
[
"mdev_bus"
]
vgpu
=
params
[
"vgpu_type"
]
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
:
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
:
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
(
"Domain 0: id
%
d running
%
s"
%
(
dom
.
ID
(),
dom
.
OSType
()))
print
(
dom
.
info
())
print
(
"Type stop, to shutdown vm!"
)
cmd
=
input
()
while
cmd
!=
"stop"
:
print
(
"Type stop, to shutdown vm!"
)
cmd
=
input
()
while
cmd
!=
"stop"
:
cmdarr
=
cmd
.
split
()
if
cmdarr
[
0
]
==
"fwd"
:
fwd_port
(
dom
,
int
(
cmdarr
[
1
]),
int
(
cmdarr
[
2
]))
...
...
@@ -78,15 +80,15 @@ while cmd != "stop":
show_ip
(
dom
)
cmd
=
input
()
try
:
try
:
print
(
"Stop VM"
)
if
dom
.
destroy
()
<
0
:
raise
SystemExit
(
"Can not stop guest domain"
)
except
libvirt
.
libvirtError
:
except
libvirt
.
libvirtError
:
print
(
'Failed to find the main domain'
)
sys
.
exit
(
1
)
if
params
[
"vgpu"
]:
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
)
...
...
@@ -94,4 +96,34 @@ if params["vgpu"]:
raise
RuntimeError
(
"command '{}' return with error (code {}): {}"
.
format
(
e
.
cmd
,
e
.
returncode
,
e
.
output
))
conn
.
close
()
\ No newline at end of file
conn
.
close
()
def
main
():
try
:
opts
,
args
=
getopt
.
getopt
(
sys
.
argv
[
1
:],
"gbmcd:v"
,
[
"help"
,
"output="
])
except
getopt
.
GetoptError
as
err
:
# print help information and exit:
print
(
err
)
# will print something like "option -a not recognized"
sys
.
exit
(
2
)
for
o
,
a
in
opts
:
if
o
in
(
"-m"
,
"--mem"
):
params
[
"ram"
]
=
int
(
a
)
elif
o
in
(
"-g"
,
"--gpu"
):
params
[
"vgpu"
]
=
True
params
[
"vgpu_type"
]
=
a
elif
o
in
(
"-b"
,
"--boot"
):
params
[
"boot_iso"
]
=
True
params
[
"iso"
]
=
a
if
not
os
.
path
.
isfile
(
a
):
raise
RuntimeError
(
"The attached boot iso is not a file or not exists"
)
elif
o
in
(
"-c"
,
"--cpu"
):
params
[
"vcpu"
]
=
int
(
a
)
elif
o
in
(
"-d"
,
"--disk"
):
params
[
"disk"
]
=
a
if
not
os
.
path
.
isfile
(
a
):
raise
RuntimeError
(
"The attached disk is not a file or not exists"
)
else
:
assert
False
,
"unhandled option"
start_vm
(
params
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment