Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE
/
storagedriver
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
2
Merge Requests
4
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
abda6f85
authored
Sep 26, 2013
by
Guba Sándor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactoring create and adding snapshot command
parent
89bbef63
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
9 deletions
+45
-9
disk.py
+45
-9
No files found.
disk.py
View file @
abda6f85
...
...
@@ -38,11 +38,16 @@ class Disk(object):
'name'
:
self
.
name
,
'dir'
:
self
.
dir
,
'format'
:
self
.
format
,
'type'
:
self
.
type
,
'size'
:
self
.
size
,
'base_name'
:
self
.
base_name
,
}
def
match_types
(
self
,
tupple_list
):
for
k
in
tupple_list
:
if
k
[
0
]
==
self
.
type
[
0
]:
return
k
[
1
]
raise
Exception
(
"Could not match qemu format
%
s"
%
self
.
format
[
0
])
def
get_path
(
self
):
return
os
.
path
.
realpath
(
self
.
dir
+
'/'
+
self
.
name
)
...
...
@@ -55,6 +60,8 @@ class Disk(object):
@classmethod
def
get
(
cls
,
dir
,
name
):
''' Create disk from path
'''
path
=
os
.
path
.
realpath
(
dir
+
'/'
+
name
)
output
=
subprocess
.
check_output
([
'qemu-img'
,
'info'
,
path
])
...
...
@@ -75,21 +82,50 @@ class Disk(object):
return
Disk
(
dir
,
name
,
format
,
size
,
base
,
type
)
def
create
(
self
):
''' Creating new image format specified at self.format.
self.format van be "qcow2-normal"
'''
# Check if type is avaliable to create
if
self
.
format
[
0
]
not
in
[
k
[
0
]
for
k
in
self
.
CREATE_TYPES
]:
raise
Exception
(
'Invalid format:
%
s'
%
self
.
format
)
# Check for file if already exist
if
os
.
path
.
isfile
(
self
.
get_path
()):
raise
Exception
(
'File already exists:
%
s'
%
self
.
get_path
())
# Match model type to qemu type
qemu_format
=
self
.
match_types
(
self
.
CREATE_TYPES
)
# Build list of Strings as command parameters
cmdline
=
[
'qemu-img'
,
'create'
,
'-f'
,
qemu_format
,
str
(
self
.
size
)]
# Call subprocess
subprocess
.
check_output
(
cmdline
)
def
snapshot
(
self
):
''' Creating qcow2 snapshot with base image.
'''
# Check if snapshot type match
if
self
.
format
[
0
]
not
in
[
k
[
0
]
for
k
in
self
.
SNAPSHOT_TYPES
]:
raise
Exception
(
'Invalid format:
%
s'
%
self
.
format
)
# Check if file already exists
if
os
.
path
.
isfile
(
self
.
get_path
()):
raise
Exception
(
'File already exists:
%
s'
%
self
.
get_path
())
# Check if base file exist
if
not
os
.
path
.
isfile
(
self
.
get_base
()):
raise
Exception
(
'Image Base does not exists:
%
s'
%
self
.
get_base
())
qemu_format
=
self
.
match_types
(
self
.
SNAPSHOT_TYPES
)
# Build list of Strings as command parameters
cmdline
=
[
'qemu-img'
,
'create'
,
'-f'
,
self
.
format
]
if
self
.
type
==
'snapshot'
:
cmdline
.
append
(
'-b'
)
cmdline
.
append
(
self
.
get_base
())
cmdline
.
append
(
self
.
get_path
())
if
self
.
type
!=
'snapshot'
:
cmdline
.
append
(
str
(
self
.
size
))
print
' '
.
join
(
cmdline
)
'-f'
,
qemu_format
,
'-b'
,
self
.
get_base
(),
str
(
self
.
size
)]
# Call subprocess
subprocess
.
check_output
(
cmdline
)
def
delete
(
self
):
''' Delete file
'''
if
os
.
path
.
isfile
(
self
.
get_path
()):
os
.
unlink
(
self
.
get_path
())
...
...
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