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
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
91 additions
and
59 deletions
+91
-59
vm.py
+91
-59
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,65 +27,103 @@ params = {
"boot_menu"
:
"yes"
,
}
template
=
env
.
get_template
(
"demovm.xml"
)
def
start_vm
(
params
):
vmxml
=
template
.
render
(
**
params
)
template
=
env
.
get_template
(
"demovm.xml"
)
print
(
vmxml
)
vmxml
=
template
.
render
(
**
params
)
input
(
"Correct? "
)
print
(
vmxml
)
id
=
params
[
"uuid"
]
bus
=
params
[
"mdev_bus"
]
vgpu
=
params
[
"vgpu_type"
]
input
(
"Correct? "
)
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"
:
cmdarr
=
cmd
.
split
()
if
cmdarr
[
0
]
==
"fwd"
:
fwd_port
(
dom
,
int
(
cmdarr
[
1
]),
int
(
cmdarr
[
2
]))
elif
cmdarr
[
0
]
==
"showip"
:
show_ip
(
dom
)
cmd
=
input
()
conn
=
libvirt
.
open
(
"qemu:///system"
)
except
libvirt
.
libvirtError
:
print
(
'Failed to open connection to the hypervisor'
)
sys
.
exit
(
1
)
id
=
params
[
"uuid"
]
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
:
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
)
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"
:
cmdarr
=
cmd
.
split
()
if
cmdarr
[
0
]
==
"fwd"
:
fwd_port
(
dom
,
int
(
cmdarr
[
1
]),
int
(
cmdarr
[
2
]))
elif
cmdarr
[
0
]
==
"showip"
:
show_ip
(
dom
)
cmd
=
input
()
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
))
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
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