forked from kimchi-project/kimchi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageinfo.py
73 lines (59 loc) · 2.24 KB
/
imageinfo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#
# Kimchi
#
# Copyright IBM Corp, 2015-2016
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import guestfs
import json
import os
import sys
from wok.exception import ImageFormatError, InvalidParameter, TimeoutExpired
from wok.utils import run_command, wok_log
def probe_img_info(path):
cmd = ["qemu-img", "info", "--output=json", path]
info = dict()
try:
out = run_command(cmd, 10)[0]
except TimeoutExpired:
wok_log.warning("Cannot decide format of base img %s", path)
return None
info = json.loads(out)
info['virtual-size'] = info['virtual-size'] >> 30
info['actual-size'] = info['actual-size'] >> 30
return info
def probe_image(image_path):
if not os.path.isfile(image_path):
raise InvalidParameter("KCHIMG0004E", {'filename': image_path})
if not os.access(image_path, os.R_OK):
raise ImageFormatError("KCHIMG0003E", {'filename': image_path})
g = guestfs.GuestFS(python_return_dict=True)
g.add_drive_opts(image_path, readonly=1)
g.launch()
try:
roots = g.inspect_os()
except:
raise ImageFormatError("KCHIMG0001E")
if len(roots) == 0:
# If we are unable to detect the OS, still add the image
# but make distro and vendor 'unknown'
return ("unknown", "unknown")
for root in roots:
version = "%d.%d" % (g.inspect_get_major_version(root),
g.inspect_get_minor_version(root))
distro = "%s" % (g.inspect_get_distro(root))
return (distro, version)
if __name__ == '__main__':
print probe_image(sys.argv[1])