-
Notifications
You must be signed in to change notification settings - Fork 364
/
osinfo.py
378 lines (323 loc) · 12.3 KB
/
osinfo.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#
# Project Kimchi
#
# Copyright IBM Corp, 2015-2017
#
# 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 copy
import glob
import os
import distro
from collections import defaultdict
from distutils.version import LooseVersion
import psutil
from configobj import ConfigObj
from wok.config import PluginPaths
from wok.exception import InvalidParameter
from wok.plugins.kimchi.config import kimchiPaths
from wok.utils import wok_log
SUPPORTED_ARCHS = {
'x86': ('i386', 'i686', 'x86_64'),
'power': ('ppc', 'ppc64'),
'ppc64le': ('ppc64le'),
's390x': ('s390x'),
}
# Memory devices slot limits by architecture
HOST_DISTRO = distro.linux_distribution(full_distribution_name=False)
MEM_DEV_SLOTS = {
'ppc64': 32 if HOST_DISTRO and HOST_DISTRO[0] == 'Ubuntu' else 256,
'ppc64le': 32 if HOST_DISTRO and HOST_DISTRO[0] == 'Ubuntu' else 256,
'x86_64': 256,
'i686': 256,
'i386': 256,
's390x': 256,
}
template_specs = {
'x86': {
'old': dict(disk_bus='ide', nic_model='e1000', sound_model='ich6'),
'modern': dict(
disk_bus='virtio', nic_model='virtio', sound_model='ich6', tablet_bus='usb'
),
},
'power': {
'old': dict(
disk_bus='scsi',
nic_model='spapr-vlan',
cdrom_bus='scsi',
kbd_type='kbd',
kbd_bus='usb',
mouse_bus='usb',
tablet_bus='usb',
),
'modern': dict(
disk_bus='virtio',
nic_model='virtio',
cdrom_bus='scsi',
kbd_bus='usb',
kbd_type='kbd',
mouse_bus='usb',
tablet_bus='usb',
),
},
'ppc64le': {
'old': dict(
disk_bus='virtio',
nic_model='virtio',
cdrom_bus='scsi',
kbd_bus='usb',
kbd_type='keyboard',
mouse_bus='usb',
tablet_bus='usb',
),
'modern': dict(
disk_bus='virtio',
nic_model='virtio',
cdrom_bus='scsi',
kbd_bus='usb',
kbd_type='keyboard',
mouse_bus='usb',
tablet_bus='usb',
),
},
's390x': {
'old': dict(disk_bus='virtio', nic_model='virtio', cdrom_bus='scsi'),
'modern': dict(disk_bus='virtio', nic_model='virtio', cdrom_bus='scsi'),
},
}
custom_specs = {
'fedora': {'22': {'x86': dict(video_model='qxl')}},
'windows': {'xp': {'x86': dict(nic_model='pcnet')}},
}
modern_version_bases = {
'x86': {
'debian': '6.0',
'ubuntu': '7.10',
'opensuse': '10.3',
'centos': '5.3',
'rhel': '6.0',
'fedora': '16',
'gentoo': '0',
'sles': '11',
'arch': '0',
},
'power': {
'rhel': '6.5',
'fedora': '19',
'ubuntu': '14.04',
'opensuse': '13.1',
'sles': '11sp3',
},
'ppc64le': {
'rhel': '6.5',
'fedora': '19',
'ubuntu': '14.04',
'opensuse': '13.1',
'sles': '11sp3',
},
}
icon_available_distros = [
icon[5:-4]
for icon in glob.glob1('%s/images/' % PluginPaths('kimchi').ui_dir, 'icon-*.png')
]
def _get_arch():
for arch, sub_archs in SUPPORTED_ARCHS.items():
if os.uname()[4] in sub_archs:
return arch
def _get_default_template_mem():
if hasattr(psutil, 'virtual_memory'):
mem = psutil.virtual_memory().total >> 10 >> 10
else:
mem = psutil.TOTAL_PHYMEM >> 10 >> 10
return 2048 if mem > 2048 else mem
def _get_tmpl_defaults():
"""
ConfigObj returns a dict like below when no changes were made in the
template configuration file (template.conf)
{'main': {}, 'memory': {}, 'storage': {'disk.0': {}}, 'processor': {},
'graphics': {}}
The default values should be like below:
{'main': {'networks': ['default']},
'memory': {'current': 1024, 'maxmemory': 1024},
'storage': { 'disk.0': {'format': 'qcow2', 'size': '10',
'pool': '/plugins/kimchi/storagepools/default'}},
'processor': {'vcpus': '1', 'maxvcpus': 1},
'graphics': {'type': 'spice', 'listen': '127.0.0.1'}}
The default values on s390x architecture:
{'memory': {'current': 1024, 'maxmemory': 1024},
'storage': { 'disk.0': {'format': 'qcow2', 'size': '10',
'pool': '/plugins/kimchi/storagepools/default'}},
'processor': {'vcpus': '1', 'maxvcpus': 1},
'graphics': {'type': 'spice', 'listen': '127.0.0.1'}}
"""
# Create dict with default values
tmpl_defaults = defaultdict(dict)
host_arch = _get_arch()
tmpl_defaults['main']['networks'] = ['default']
if host_arch in ['s390x', 's390']:
tmpl_defaults['main']['networks'] = []
tmpl_defaults['memory'] = {
'current': _get_default_template_mem(),
'maxmemory': _get_default_template_mem(),
}
tmpl_defaults['storage']['disk.0'] = {
'size': 10,
'format': 'qcow2',
'pool': 'default',
}
is_on_s390x = True if _get_arch() == 's390x' else False
if is_on_s390x:
tmpl_defaults['storage']['disk.0']['path'] = '/var/lib/libvirt/images/'
del tmpl_defaults['storage']['disk.0']['pool']
tmpl_defaults['processor']['vcpus'] = 1
tmpl_defaults['processor']['maxvcpus'] = 1
tmpl_defaults['graphics'] = {'type': 'vnc', 'listen': '127.0.0.1'}
default_config = ConfigObj(tmpl_defaults)
# Load template configuration file
config_file = os.path.join(kimchiPaths.sysconf_dir, 'template.conf')
config = ConfigObj(config_file)
# File configuration takes preference.
# In s390x, file configuration can have storage pool or path.
# Default configuration for s390x is storage path.
# In case file conf has storage pool then storage pool takes preference.
# When conf file has explicitly storage pool: "defaults" should
# have storage pool and default configured path should be removed,
# as either storage can be path or pool, cannot be both.
# When conf file does not explicity storage pool or have explicitly
# storage path: "default" should have storage path only and cannot
# have default pool.
#
# Check file conf has storage configured.
if is_on_s390x and config.get('storage').get('disk.0'):
# remove storage from default_config as file configuration takes
# preference.
default_config.pop('storage')
# Get storage configuration present in conf file
config_pool = config.get('storage').get('disk.0').get('pool')
config_path = config.get('storage').get('disk.0').get('path')
# If storage configured in conf file then it should have either
# pool or path.
if not config_pool and not config_path:
raise InvalidParameter('KCHTMPL0040E')
# On s390x if config file has both path and pool uncommented
# then path should take preference.
if config_pool and config_path:
wok_log.warning(
'Both default pool and path are specified in'
+ ' template.conf. Hence default pool is being'
+ ' ignored and only default path will be used'
)
config.get('storage').get('disk.0').pop('pool')
# Merge default configuration with file configuration
default_config.merge(config)
# Create a dict with default values according to data structure
# expected by VMTemplate
defaults = {
'domain': 'kvm',
'arch': os.uname()[4],
'cdrom_bus': 'ide',
'cdrom_index': 2,
'mouse_bus': 'ps2',
}
# Parse main section to get networks and memory values
defaults.update(default_config.pop('main'))
defaults['memory'] = default_config.pop('memory')
defaults['memory']['current'] = int(defaults['memory']['current'])
defaults['memory']['maxmemory'] = int(defaults['memory']['maxmemory'])
# for s390x architecture, set default console as virtio
if is_on_s390x:
defaults['console'] = 'virtio'
# Parse storage section to get disks values
storage_section = default_config.pop('storage')
defaults['disks'] = []
for index, disk in enumerate(storage_section.keys()):
data = storage_section[disk]
data['index'] = int(disk.split('.')[1])
# Right now 'Path' is only supported on s390x
if storage_section[disk].get('path') and is_on_s390x:
data['path'] = storage_section[disk].pop('path')
if 'size' not in storage_section[disk]:
data['size'] = tmpl_defaults['storage']['disk.0']['size']
else:
data['size'] = storage_section[disk].pop('size')
if 'format' not in storage_section[disk]:
data['format'] = tmpl_defaults['storage']['disk.0']['format']
else:
data['format'] = storage_section[disk].pop('format')
else:
data['pool'] = {
'name': '/plugins/kimchi/storagepools/'
+ storage_section[disk].pop('pool')
}
defaults['disks'].append(data)
# Parse processor section to get vcpus and cpu_topology values
processor_section = default_config.pop('processor')
defaults['cpu_info'] = {
'vcpus': processor_section.pop('vcpus'),
'maxvcpus': processor_section.pop('maxvcpus'),
}
if len(processor_section.keys()) > 0:
defaults['cpu_info']['topology'] = processor_section
# Update defaults values with graphics values
defaults['graphics'] = default_config.pop('graphics')
# Setting default memory device slots
defaults['mem_dev_slots'] = MEM_DEV_SLOTS.get(os.uname()[4], 256)
return defaults
# Set defaults values according to template.conf file
defaults = _get_tmpl_defaults()
def get_template_default(template_type, field):
host_arch = _get_arch()
# Assuming 'power' = 'ppc64le' because lookup() does the same,
# claiming libvirt compatibility.
host_arch = 'power' if host_arch == 'ppc64le' else host_arch
tmpl_defaults = copy.deepcopy(defaults)
tmpl_defaults.update(template_specs[host_arch][template_type])
return tmpl_defaults[field]
def lookup(distro, version):
"""
Lookup all parameters needed to run a VM of a known or unknown operating
system type and version. The data is constructed by starting with the
'defaults' and merging the parameters given for the identified OS. If
known, a link to a remote install CD is added.
"""
params = copy.deepcopy(defaults)
params['os_distro'] = distro
params['os_version'] = version
arch = _get_arch()
# set up arch to ppc64 instead of ppc64le due to libvirt compatibility
if params['arch'] == 'ppc64le':
params['arch'] = 'ppc64'
# On s390x, template spec does not change based on version.
if params['arch'] == 's390x' or arch == 's390x':
params.update(template_specs[arch]['old'])
if not distro:
params['os_distro'] = params['os_version'] = 'unknown'
elif distro in modern_version_bases[arch]:
if LooseVersion(version) >= LooseVersion(modern_version_bases[arch][distro]):
params.update(template_specs[arch]['modern'])
else:
params.update(template_specs[arch]['old'])
else:
params['os_distro'] = params['os_version'] = 'unknown'
params.update(template_specs[arch]['old'])
# Get custom specifications
specs = custom_specs.get(distro, {})
for v, config in specs.items():
if LooseVersion(version) >= LooseVersion(v):
params.update(config.get(arch, {}))
if distro in icon_available_distros:
params['icon'] = 'plugins/kimchi/images/icon-%s.png' % distro
else:
params['icon'] = 'plugins/kimchi/images/icon-vm.png'
return params