root / lib / hypervisor / hv_kvm.py @ 43440815
History | View | Annotate | Download (28.1 kB)
1 | eb58f9b1 | Guido Trotter | #
|
---|---|---|---|
2 | eb58f9b1 | Guido Trotter | #
|
3 | eb58f9b1 | Guido Trotter | |
4 | eb58f9b1 | Guido Trotter | # Copyright (C) 2008 Google Inc.
|
5 | eb58f9b1 | Guido Trotter | #
|
6 | eb58f9b1 | Guido Trotter | # This program is free software; you can redistribute it and/or modify
|
7 | eb58f9b1 | Guido Trotter | # it under the terms of the GNU General Public License as published by
|
8 | eb58f9b1 | Guido Trotter | # the Free Software Foundation; either version 2 of the License, or
|
9 | eb58f9b1 | Guido Trotter | # (at your option) any later version.
|
10 | eb58f9b1 | Guido Trotter | #
|
11 | eb58f9b1 | Guido Trotter | # This program is distributed in the hope that it will be useful, but
|
12 | eb58f9b1 | Guido Trotter | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | eb58f9b1 | Guido Trotter | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | eb58f9b1 | Guido Trotter | # General Public License for more details.
|
15 | eb58f9b1 | Guido Trotter | #
|
16 | eb58f9b1 | Guido Trotter | # You should have received a copy of the GNU General Public License
|
17 | eb58f9b1 | Guido Trotter | # along with this program; if not, write to the Free Software
|
18 | eb58f9b1 | Guido Trotter | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | eb58f9b1 | Guido Trotter | # 02110-1301, USA.
|
20 | eb58f9b1 | Guido Trotter | |
21 | eb58f9b1 | Guido Trotter | |
22 | eb58f9b1 | Guido Trotter | """KVM hypervisor
|
23 | eb58f9b1 | Guido Trotter |
|
24 | eb58f9b1 | Guido Trotter | """
|
25 | eb58f9b1 | Guido Trotter | |
26 | eb58f9b1 | Guido Trotter | import os |
27 | eb58f9b1 | Guido Trotter | import os.path |
28 | eb58f9b1 | Guido Trotter | import re |
29 | eb58f9b1 | Guido Trotter | import tempfile |
30 | 6567aff3 | Guido Trotter | import time |
31 | 30e42c4e | Guido Trotter | import logging |
32 | eb58f9b1 | Guido Trotter | from cStringIO import StringIO |
33 | eb58f9b1 | Guido Trotter | |
34 | eb58f9b1 | Guido Trotter | from ganeti import utils |
35 | eb58f9b1 | Guido Trotter | from ganeti import constants |
36 | eb58f9b1 | Guido Trotter | from ganeti import errors |
37 | 38e250ba | Guido Trotter | from ganeti import serializer |
38 | 38e250ba | Guido Trotter | from ganeti import objects |
39 | eb58f9b1 | Guido Trotter | from ganeti.hypervisor import hv_base |
40 | eb58f9b1 | Guido Trotter | |
41 | eb58f9b1 | Guido Trotter | |
42 | eb58f9b1 | Guido Trotter | class KVMHypervisor(hv_base.BaseHypervisor): |
43 | c4469f75 | Guido Trotter | """KVM hypervisor interface"""
|
44 | eb58f9b1 | Guido Trotter | |
45 | eb58f9b1 | Guido Trotter | _ROOT_DIR = constants.RUN_GANETI_DIR + "/kvm-hypervisor"
|
46 | a1d79fc6 | Guido Trotter | _PIDS_DIR = _ROOT_DIR + "/pid" # contains live instances pids |
47 | a1d79fc6 | Guido Trotter | _CTRL_DIR = _ROOT_DIR + "/ctrl" # contains instances control sockets |
48 | a1d79fc6 | Guido Trotter | _CONF_DIR = _ROOT_DIR + "/conf" # contains instances startup data |
49 | a1d79fc6 | Guido Trotter | _DIRS = [_ROOT_DIR, _PIDS_DIR, _CTRL_DIR, _CONF_DIR] |
50 | eb58f9b1 | Guido Trotter | |
51 | 6b5605e8 | Iustin Pop | PARAMETERS = [ |
52 | 6b5605e8 | Iustin Pop | constants.HV_KERNEL_PATH, |
53 | 6b5605e8 | Iustin Pop | constants.HV_INITRD_PATH, |
54 | 074ca009 | Guido Trotter | constants.HV_ROOT_PATH, |
55 | 6b5605e8 | Iustin Pop | constants.HV_ACPI, |
56 | a2faf9ee | Guido Trotter | constants.HV_SERIAL_CONSOLE, |
57 | 56fee73b | Guido Trotter | constants.HV_VNC_BIND_ADDRESS, |
58 | 8b2d1013 | Guido Trotter | constants.HV_VNC_TLS, |
59 | 8b2d1013 | Guido Trotter | constants.HV_VNC_X509, |
60 | 8b2d1013 | Guido Trotter | constants.HV_VNC_X509_VERIFY, |
61 | 66d5dbef | Guido Trotter | constants.HV_CDROM_IMAGE_PATH, |
62 | 66d5dbef | Guido Trotter | constants.HV_BOOT_ORDER, |
63 | 43440815 | Guido Trotter | constants.HV_NIC_TYPE, |
64 | 43440815 | Guido Trotter | constants.HV_DISK_TYPE, |
65 | 6b5605e8 | Iustin Pop | ] |
66 | 6b5605e8 | Iustin Pop | |
67 | 30e42c4e | Guido Trotter | _MIGRATION_STATUS_RE = re.compile('Migration\s+status:\s+(\w+)',
|
68 | 30e42c4e | Guido Trotter | re.M | re.I) |
69 | 30e42c4e | Guido Trotter | |
70 | eb58f9b1 | Guido Trotter | def __init__(self): |
71 | eb58f9b1 | Guido Trotter | hv_base.BaseHypervisor.__init__(self)
|
72 | eb58f9b1 | Guido Trotter | # Let's make sure the directories we need exist, even if the RUN_DIR lives
|
73 | eb58f9b1 | Guido Trotter | # in a tmpfs filesystem or has been otherwise wiped out.
|
74 | 08137f9e | Iustin Pop | for mydir in self._DIRS: |
75 | 08137f9e | Iustin Pop | if not os.path.exists(mydir): |
76 | 08137f9e | Iustin Pop | os.mkdir(mydir) |
77 | eb58f9b1 | Guido Trotter | |
78 | 1f8b3a27 | Guido Trotter | def _InstancePidAlive(self, instance_name): |
79 | 1f8b3a27 | Guido Trotter | """Returns the instance pid and pidfile
|
80 | 1f8b3a27 | Guido Trotter |
|
81 | 1f8b3a27 | Guido Trotter | """
|
82 | 1f8b3a27 | Guido Trotter | pidfile = "%s/%s" % (self._PIDS_DIR, instance_name) |
83 | 1f8b3a27 | Guido Trotter | pid = utils.ReadPidFile(pidfile) |
84 | 1f8b3a27 | Guido Trotter | alive = utils.IsProcessAlive(pid) |
85 | 1f8b3a27 | Guido Trotter | |
86 | 1f8b3a27 | Guido Trotter | return (pidfile, pid, alive)
|
87 | 1f8b3a27 | Guido Trotter | |
88 | 0df4d98a | Guido Trotter | @classmethod
|
89 | 0df4d98a | Guido Trotter | def _InstanceMonitor(cls, instance_name): |
90 | c4fbefc8 | Guido Trotter | """Returns the instance monitor socket name
|
91 | c4fbefc8 | Guido Trotter |
|
92 | c4fbefc8 | Guido Trotter | """
|
93 | 0df4d98a | Guido Trotter | return '%s/%s.monitor' % (cls._CTRL_DIR, instance_name) |
94 | c4fbefc8 | Guido Trotter | |
95 | 0df4d98a | Guido Trotter | @classmethod
|
96 | 0df4d98a | Guido Trotter | def _InstanceSerial(cls, instance_name): |
97 | c4fbefc8 | Guido Trotter | """Returns the instance serial socket name
|
98 | c4fbefc8 | Guido Trotter |
|
99 | c4fbefc8 | Guido Trotter | """
|
100 | 0df4d98a | Guido Trotter | return '%s/%s.serial' % (cls._CTRL_DIR, instance_name) |
101 | c4fbefc8 | Guido Trotter | |
102 | 0df4d98a | Guido Trotter | @classmethod
|
103 | 0df4d98a | Guido Trotter | def _InstanceKVMRuntime(cls, instance_name): |
104 | 38e250ba | Guido Trotter | """Returns the instance KVM runtime filename
|
105 | 38e250ba | Guido Trotter |
|
106 | 38e250ba | Guido Trotter | """
|
107 | 0df4d98a | Guido Trotter | return '%s/%s.runtime' % (cls._CONF_DIR, instance_name) |
108 | 38e250ba | Guido Trotter | |
109 | eb58f9b1 | Guido Trotter | def _WriteNetScript(self, instance, seq, nic): |
110 | eb58f9b1 | Guido Trotter | """Write a script to connect a net interface to the proper bridge.
|
111 | eb58f9b1 | Guido Trotter |
|
112 | eb58f9b1 | Guido Trotter | This can be used by any qemu-type hypervisor.
|
113 | eb58f9b1 | Guido Trotter |
|
114 | eb58f9b1 | Guido Trotter | @param instance: instance we're acting on
|
115 | eb58f9b1 | Guido Trotter | @type instance: instance object
|
116 | eb58f9b1 | Guido Trotter | @param seq: nic sequence number
|
117 | eb58f9b1 | Guido Trotter | @type seq: int
|
118 | eb58f9b1 | Guido Trotter | @param nic: nic we're acting on
|
119 | eb58f9b1 | Guido Trotter | @type nic: nic object
|
120 | eb58f9b1 | Guido Trotter | @return: netscript file name
|
121 | eb58f9b1 | Guido Trotter | @rtype: string
|
122 | eb58f9b1 | Guido Trotter |
|
123 | eb58f9b1 | Guido Trotter | """
|
124 | eb58f9b1 | Guido Trotter | script = StringIO() |
125 | eb58f9b1 | Guido Trotter | script.write("#!/bin/sh\n")
|
126 | eb58f9b1 | Guido Trotter | script.write("# this is autogenerated by Ganeti, please do not edit\n#\n")
|
127 | eb58f9b1 | Guido Trotter | script.write("export INSTANCE=%s\n" % instance.name)
|
128 | eb58f9b1 | Guido Trotter | script.write("export MAC=%s\n" % nic.mac)
|
129 | eb58f9b1 | Guido Trotter | script.write("export IP=%s\n" % nic.ip)
|
130 | eb58f9b1 | Guido Trotter | script.write("export BRIDGE=%s\n" % nic.bridge)
|
131 | eb58f9b1 | Guido Trotter | script.write("export INTERFACE=$1\n")
|
132 | eb58f9b1 | Guido Trotter | # TODO: make this configurable at ./configure time
|
133 | eb58f9b1 | Guido Trotter | script.write("if [ -x /etc/ganeti/kvm-vif-bridge ]; then\n")
|
134 | eb58f9b1 | Guido Trotter | script.write(" # Execute the user-specific vif file\n")
|
135 | eb58f9b1 | Guido Trotter | script.write(" /etc/ganeti/kvm-vif-bridge\n")
|
136 | eb58f9b1 | Guido Trotter | script.write("else\n")
|
137 | eb58f9b1 | Guido Trotter | script.write(" # Connect the interface to the bridge\n")
|
138 | eb58f9b1 | Guido Trotter | script.write(" /sbin/ifconfig $INTERFACE 0.0.0.0 up\n")
|
139 | eb58f9b1 | Guido Trotter | script.write(" /usr/sbin/brctl addif $BRIDGE $INTERFACE\n")
|
140 | eb58f9b1 | Guido Trotter | script.write("fi\n\n")
|
141 | eb58f9b1 | Guido Trotter | # As much as we'd like to put this in our _ROOT_DIR, that will happen to be
|
142 | eb58f9b1 | Guido Trotter | # mounted noexec sometimes, so we'll have to find another place.
|
143 | eb58f9b1 | Guido Trotter | (tmpfd, tmpfile_name) = tempfile.mkstemp() |
144 | eb58f9b1 | Guido Trotter | tmpfile = os.fdopen(tmpfd, 'w')
|
145 | eb58f9b1 | Guido Trotter | tmpfile.write(script.getvalue()) |
146 | eb58f9b1 | Guido Trotter | tmpfile.close() |
147 | eb58f9b1 | Guido Trotter | os.chmod(tmpfile_name, 0755)
|
148 | eb58f9b1 | Guido Trotter | return tmpfile_name
|
149 | eb58f9b1 | Guido Trotter | |
150 | eb58f9b1 | Guido Trotter | def ListInstances(self): |
151 | eb58f9b1 | Guido Trotter | """Get the list of running instances.
|
152 | eb58f9b1 | Guido Trotter |
|
153 | c41eea6e | Iustin Pop | We can do this by listing our live instances directory and
|
154 | c41eea6e | Iustin Pop | checking whether the associated kvm process is still alive.
|
155 | eb58f9b1 | Guido Trotter |
|
156 | eb58f9b1 | Guido Trotter | """
|
157 | eb58f9b1 | Guido Trotter | result = [] |
158 | eb58f9b1 | Guido Trotter | for name in os.listdir(self._PIDS_DIR): |
159 | 08137f9e | Iustin Pop | filename = "%s/%s" % (self._PIDS_DIR, name) |
160 | 08137f9e | Iustin Pop | if utils.IsProcessAlive(utils.ReadPidFile(filename)):
|
161 | eb58f9b1 | Guido Trotter | result.append(name) |
162 | eb58f9b1 | Guido Trotter | return result
|
163 | eb58f9b1 | Guido Trotter | |
164 | eb58f9b1 | Guido Trotter | def GetInstanceInfo(self, instance_name): |
165 | eb58f9b1 | Guido Trotter | """Get instance properties.
|
166 | eb58f9b1 | Guido Trotter |
|
167 | c41eea6e | Iustin Pop | @param instance_name: the instance name
|
168 | c41eea6e | Iustin Pop |
|
169 | c41eea6e | Iustin Pop | @return: tuple (name, id, memory, vcpus, stat, times)
|
170 | eb58f9b1 | Guido Trotter |
|
171 | eb58f9b1 | Guido Trotter | """
|
172 | 1f8b3a27 | Guido Trotter | pidfile, pid, alive = self._InstancePidAlive(instance_name)
|
173 | 1f8b3a27 | Guido Trotter | if not alive: |
174 | eb58f9b1 | Guido Trotter | return None |
175 | eb58f9b1 | Guido Trotter | |
176 | eb58f9b1 | Guido Trotter | cmdline_file = "/proc/%s/cmdline" % pid
|
177 | eb58f9b1 | Guido Trotter | try:
|
178 | eb58f9b1 | Guido Trotter | fh = open(cmdline_file, 'r') |
179 | eb58f9b1 | Guido Trotter | try:
|
180 | eb58f9b1 | Guido Trotter | cmdline = fh.read() |
181 | eb58f9b1 | Guido Trotter | finally:
|
182 | eb58f9b1 | Guido Trotter | fh.close() |
183 | 90c024f6 | Guido Trotter | except EnvironmentError, err: |
184 | eb58f9b1 | Guido Trotter | raise errors.HypervisorError("Failed to list instance %s: %s" % |
185 | eb58f9b1 | Guido Trotter | (instance_name, err)) |
186 | eb58f9b1 | Guido Trotter | |
187 | eb58f9b1 | Guido Trotter | memory = 0
|
188 | eb58f9b1 | Guido Trotter | vcpus = 0
|
189 | eb58f9b1 | Guido Trotter | stat = "---b-"
|
190 | eb58f9b1 | Guido Trotter | times = "0"
|
191 | eb58f9b1 | Guido Trotter | |
192 | eb58f9b1 | Guido Trotter | arg_list = cmdline.split('\x00')
|
193 | eb58f9b1 | Guido Trotter | while arg_list:
|
194 | eb58f9b1 | Guido Trotter | arg = arg_list.pop(0)
|
195 | eb58f9b1 | Guido Trotter | if arg == '-m': |
196 | eb58f9b1 | Guido Trotter | memory = arg_list.pop(0)
|
197 | eb58f9b1 | Guido Trotter | elif arg == '-smp': |
198 | eb58f9b1 | Guido Trotter | vcpus = arg_list.pop(0)
|
199 | eb58f9b1 | Guido Trotter | |
200 | eb58f9b1 | Guido Trotter | return (instance_name, pid, memory, vcpus, stat, times)
|
201 | eb58f9b1 | Guido Trotter | |
202 | eb58f9b1 | Guido Trotter | def GetAllInstancesInfo(self): |
203 | eb58f9b1 | Guido Trotter | """Get properties of all instances.
|
204 | eb58f9b1 | Guido Trotter |
|
205 | c41eea6e | Iustin Pop | @return: list of tuples (name, id, memory, vcpus, stat, times)
|
206 | c41eea6e | Iustin Pop |
|
207 | eb58f9b1 | Guido Trotter | """
|
208 | eb58f9b1 | Guido Trotter | data = [] |
209 | eb58f9b1 | Guido Trotter | for name in os.listdir(self._PIDS_DIR): |
210 | 08137f9e | Iustin Pop | filename = "%s/%s" % (self._PIDS_DIR, name) |
211 | 08137f9e | Iustin Pop | if utils.IsProcessAlive(utils.ReadPidFile(filename)):
|
212 | 00ad5362 | Guido Trotter | try:
|
213 | 00ad5362 | Guido Trotter | info = self.GetInstanceInfo(name)
|
214 | 00ad5362 | Guido Trotter | except errors.HypervisorError, err:
|
215 | 00ad5362 | Guido Trotter | continue
|
216 | 00ad5362 | Guido Trotter | if info:
|
217 | 00ad5362 | Guido Trotter | data.append(info) |
218 | eb58f9b1 | Guido Trotter | |
219 | eb58f9b1 | Guido Trotter | return data
|
220 | eb58f9b1 | Guido Trotter | |
221 | ee5f20b0 | Guido Trotter | def _GenerateKVMRuntime(self, instance, block_devices, extra_args): |
222 | ee5f20b0 | Guido Trotter | """Generate KVM information to start an instance.
|
223 | eb58f9b1 | Guido Trotter |
|
224 | eb58f9b1 | Guido Trotter | """
|
225 | 1f8b3a27 | Guido Trotter | pidfile, pid, alive = self._InstancePidAlive(instance.name)
|
226 | eb58f9b1 | Guido Trotter | kvm = constants.KVM_PATH |
227 | eb58f9b1 | Guido Trotter | kvm_cmd = [kvm] |
228 | 8b3fd458 | Iustin Pop | kvm_cmd.extend(['-m', instance.beparams[constants.BE_MEMORY]])
|
229 | 8b3fd458 | Iustin Pop | kvm_cmd.extend(['-smp', instance.beparams[constants.BE_VCPUS]])
|
230 | eb58f9b1 | Guido Trotter | kvm_cmd.extend(['-pidfile', pidfile])
|
231 | eb58f9b1 | Guido Trotter | # used just by the vnc server, if enabled
|
232 | eb58f9b1 | Guido Trotter | kvm_cmd.extend(['-name', instance.name])
|
233 | eb58f9b1 | Guido Trotter | kvm_cmd.extend(['-daemonize'])
|
234 | 6b5605e8 | Iustin Pop | if not instance.hvparams[constants.HV_ACPI]: |
235 | eb58f9b1 | Guido Trotter | kvm_cmd.extend(['-no-acpi'])
|
236 | eb58f9b1 | Guido Trotter | |
237 | 66d5dbef | Guido Trotter | boot_disk = (instance.hvparams[constants.HV_BOOT_ORDER] == "disk")
|
238 | 66d5dbef | Guido Trotter | boot_cdrom = (instance.hvparams[constants.HV_BOOT_ORDER] == "cdrom")
|
239 | 069cfbf1 | Iustin Pop | for cfdev, dev_path in block_devices: |
240 | d34b16d7 | Iustin Pop | if cfdev.mode != constants.DISK_RDWR:
|
241 | d34b16d7 | Iustin Pop | raise errors.HypervisorError("Instance has read-only disks which" |
242 | d34b16d7 | Iustin Pop | " are not supported by KVM")
|
243 | eb58f9b1 | Guido Trotter | # TODO: handle FD_LOOP and FD_BLKTAP (?)
|
244 | 66d5dbef | Guido Trotter | if boot_disk:
|
245 | 9dd363eb | Guido Trotter | kvm_cmd.extend(['-boot', 'c']) |
246 | eb58f9b1 | Guido Trotter | boot_val = ',boot=on'
|
247 | 66d5dbef | Guido Trotter | boot_disk = False
|
248 | eb58f9b1 | Guido Trotter | else:
|
249 | eb58f9b1 | Guido Trotter | boot_val = ''
|
250 | eb58f9b1 | Guido Trotter | |
251 | d47d3d38 | Guido Trotter | # TODO: handle different if= types
|
252 | d47d3d38 | Guido Trotter | if_val = ',if=virtio'
|
253 | d47d3d38 | Guido Trotter | |
254 | 069cfbf1 | Iustin Pop | drive_val = 'file=%s,format=raw%s%s' % (dev_path, if_val, boot_val)
|
255 | eb58f9b1 | Guido Trotter | kvm_cmd.extend(['-drive', drive_val])
|
256 | eb58f9b1 | Guido Trotter | |
257 | 66d5dbef | Guido Trotter | iso_image = instance.hvparams[constants.HV_CDROM_IMAGE_PATH] |
258 | 66d5dbef | Guido Trotter | if iso_image:
|
259 | 9dd363eb | Guido Trotter | options = ',format=raw,media=cdrom'
|
260 | 66d5dbef | Guido Trotter | if boot_cdrom:
|
261 | 9dd363eb | Guido Trotter | kvm_cmd.extend(['-boot', 'd']) |
262 | 66d5dbef | Guido Trotter | options = '%s,boot=on' % options
|
263 | 9dd363eb | Guido Trotter | else:
|
264 | 9dd363eb | Guido Trotter | options = '%s,if=virtio' % options
|
265 | 66d5dbef | Guido Trotter | drive_val = 'file=%s%s' % (iso_image, options)
|
266 | 66d5dbef | Guido Trotter | kvm_cmd.extend(['-drive', drive_val])
|
267 | 66d5dbef | Guido Trotter | |
268 | df5ab9f0 | Guido Trotter | kernel_path = instance.hvparams[constants.HV_KERNEL_PATH] |
269 | df5ab9f0 | Guido Trotter | if kernel_path:
|
270 | df5ab9f0 | Guido Trotter | kvm_cmd.extend(['-kernel', kernel_path])
|
271 | df5ab9f0 | Guido Trotter | initrd_path = instance.hvparams[constants.HV_INITRD_PATH] |
272 | df5ab9f0 | Guido Trotter | if initrd_path:
|
273 | df5ab9f0 | Guido Trotter | kvm_cmd.extend(['-initrd', initrd_path])
|
274 | df5ab9f0 | Guido Trotter | root_append = 'root=%s ro' % instance.hvparams[constants.HV_ROOT_PATH]
|
275 | df5ab9f0 | Guido Trotter | if instance.hvparams[constants.HV_SERIAL_CONSOLE]:
|
276 | df5ab9f0 | Guido Trotter | kvm_cmd.extend(['-append', 'console=ttyS0,38400 %s' % root_append]) |
277 | df5ab9f0 | Guido Trotter | else:
|
278 | df5ab9f0 | Guido Trotter | kvm_cmd.extend(['-append', root_append])
|
279 | eb58f9b1 | Guido Trotter | |
280 | 8470c8db | Guido Trotter | # FIXME: handle vnc password
|
281 | 8470c8db | Guido Trotter | vnc_bind_address = instance.hvparams[constants.HV_VNC_BIND_ADDRESS] |
282 | 8470c8db | Guido Trotter | if vnc_bind_address:
|
283 | 8470c8db | Guido Trotter | kvm_cmd.extend(['-usbdevice', 'tablet']) |
284 | 8447f52b | Guido Trotter | if utils.IsValidIP(vnc_bind_address):
|
285 | 377d74c9 | Guido Trotter | if instance.network_port > constants.VNC_BASE_PORT:
|
286 | 377d74c9 | Guido Trotter | display = instance.network_port - constants.VNC_BASE_PORT |
287 | 8447f52b | Guido Trotter | if vnc_bind_address == '0.0.0.0': |
288 | 8447f52b | Guido Trotter | vnc_arg = ':%d' % (display)
|
289 | 8447f52b | Guido Trotter | else:
|
290 | 8447f52b | Guido Trotter | vnc_arg = '%s:%d' % (constants.HV_VNC_BIND_ADDRESS, display)
|
291 | 8470c8db | Guido Trotter | else:
|
292 | 8447f52b | Guido Trotter | logging.error("Network port is not a valid VNC display (%d < %d)."
|
293 | 8447f52b | Guido Trotter | " Not starting VNC" %
|
294 | 8447f52b | Guido Trotter | (instance.network_port, |
295 | 377d74c9 | Guido Trotter | constants.VNC_BASE_PORT)) |
296 | 8447f52b | Guido Trotter | vnc_arg = 'none'
|
297 | 8b2d1013 | Guido Trotter | |
298 | 8b2d1013 | Guido Trotter | # Only allow tls and other option when not binding to a file, for now.
|
299 | 8b2d1013 | Guido Trotter | # kvm/qemu gets confused otherwise about the filename to use.
|
300 | 8b2d1013 | Guido Trotter | vnc_append = ''
|
301 | 8b2d1013 | Guido Trotter | if instance.hvparams[constants.HV_VNC_TLS]:
|
302 | 8b2d1013 | Guido Trotter | vnc_append = '%s,tls' % vnc_append
|
303 | 8b2d1013 | Guido Trotter | if instance.hvparams[constants.HV_VNC_X509_VERIFY]:
|
304 | 8b2d1013 | Guido Trotter | vnc_append = '%s,x509verify=%s' % (vnc_append,
|
305 | 8b2d1013 | Guido Trotter | instance.hvparams[constants.HV_VNC_X509]) |
306 | 8b2d1013 | Guido Trotter | elif instance.hvparams[constants.HV_VNC_X509]:
|
307 | 8b2d1013 | Guido Trotter | vnc_append = '%s,x509=%s' % (vnc_append,
|
308 | 8b2d1013 | Guido Trotter | instance.hvparams[constants.HV_VNC_X509]) |
309 | 8b2d1013 | Guido Trotter | vnc_arg = '%s%s' % (vnc_arg, vnc_append)
|
310 | 8b2d1013 | Guido Trotter | |
311 | 8470c8db | Guido Trotter | else:
|
312 | 8b2d1013 | Guido Trotter | vnc_arg = 'unix:%s/%s.vnc' % (vnc_bind_address, instance.name)
|
313 | 8b2d1013 | Guido Trotter | |
314 | 8447f52b | Guido Trotter | kvm_cmd.extend(['-vnc', vnc_arg])
|
315 | 8470c8db | Guido Trotter | else:
|
316 | 8470c8db | Guido Trotter | kvm_cmd.extend(['-nographic'])
|
317 | 8470c8db | Guido Trotter | |
318 | c4fbefc8 | Guido Trotter | monitor_dev = 'unix:%s,server,nowait' % \
|
319 | c4fbefc8 | Guido Trotter | self._InstanceMonitor(instance.name)
|
320 | eb58f9b1 | Guido Trotter | kvm_cmd.extend(['-monitor', monitor_dev])
|
321 | a2faf9ee | Guido Trotter | if instance.hvparams[constants.HV_SERIAL_CONSOLE]:
|
322 | a2faf9ee | Guido Trotter | serial_dev = 'unix:%s,server,nowait' % self._InstanceSerial(instance.name) |
323 | a2faf9ee | Guido Trotter | kvm_cmd.extend(['-serial', serial_dev])
|
324 | a2faf9ee | Guido Trotter | else:
|
325 | a2faf9ee | Guido Trotter | kvm_cmd.extend(['-serial', 'none']) |
326 | eb58f9b1 | Guido Trotter | |
327 | ee5f20b0 | Guido Trotter | # Save the current instance nics, but defer their expansion as parameters,
|
328 | ee5f20b0 | Guido Trotter | # as we'll need to generate executable temp files for them.
|
329 | ee5f20b0 | Guido Trotter | kvm_nics = instance.nics |
330 | ee5f20b0 | Guido Trotter | |
331 | ee5f20b0 | Guido Trotter | return (kvm_cmd, kvm_nics)
|
332 | ee5f20b0 | Guido Trotter | |
333 | 38e250ba | Guido Trotter | def _WriteKVMRuntime(self, instance_name, data): |
334 | 38e250ba | Guido Trotter | """Write an instance's KVM runtime
|
335 | 38e250ba | Guido Trotter |
|
336 | 38e250ba | Guido Trotter | """
|
337 | 38e250ba | Guido Trotter | try:
|
338 | 38e250ba | Guido Trotter | utils.WriteFile(self._InstanceKVMRuntime(instance_name),
|
339 | 38e250ba | Guido Trotter | data=data) |
340 | 90c024f6 | Guido Trotter | except EnvironmentError, err: |
341 | 38e250ba | Guido Trotter | raise errors.HypervisorError("Failed to save KVM runtime file: %s" % err) |
342 | 38e250ba | Guido Trotter | |
343 | 38e250ba | Guido Trotter | def _ReadKVMRuntime(self, instance_name): |
344 | 38e250ba | Guido Trotter | """Read an instance's KVM runtime
|
345 | 38e250ba | Guido Trotter |
|
346 | 38e250ba | Guido Trotter | """
|
347 | 38e250ba | Guido Trotter | try:
|
348 | 38e250ba | Guido Trotter | file_content = utils.ReadFile(self._InstanceKVMRuntime(instance_name))
|
349 | 90c024f6 | Guido Trotter | except EnvironmentError, err: |
350 | 38e250ba | Guido Trotter | raise errors.HypervisorError("Failed to load KVM runtime file: %s" % err) |
351 | 38e250ba | Guido Trotter | return file_content
|
352 | 38e250ba | Guido Trotter | |
353 | 38e250ba | Guido Trotter | def _SaveKVMRuntime(self, instance, kvm_runtime): |
354 | 38e250ba | Guido Trotter | """Save an instance's KVM runtime
|
355 | 38e250ba | Guido Trotter |
|
356 | 38e250ba | Guido Trotter | """
|
357 | 38e250ba | Guido Trotter | kvm_cmd, kvm_nics = kvm_runtime |
358 | 38e250ba | Guido Trotter | serialized_nics = [nic.ToDict() for nic in kvm_nics] |
359 | 38e250ba | Guido Trotter | serialized_form = serializer.Dump((kvm_cmd, serialized_nics)) |
360 | 38e250ba | Guido Trotter | self._WriteKVMRuntime(instance.name, serialized_form)
|
361 | 38e250ba | Guido Trotter | |
362 | 30e42c4e | Guido Trotter | def _LoadKVMRuntime(self, instance, serialized_runtime=None): |
363 | 38e250ba | Guido Trotter | """Load an instance's KVM runtime
|
364 | 38e250ba | Guido Trotter |
|
365 | 38e250ba | Guido Trotter | """
|
366 | 30e42c4e | Guido Trotter | if not serialized_runtime: |
367 | 30e42c4e | Guido Trotter | serialized_runtime = self._ReadKVMRuntime(instance.name)
|
368 | 30e42c4e | Guido Trotter | loaded_runtime = serializer.Load(serialized_runtime) |
369 | 38e250ba | Guido Trotter | kvm_cmd, serialized_nics = loaded_runtime |
370 | 38e250ba | Guido Trotter | kvm_nics = [objects.NIC.FromDict(snic) for snic in serialized_nics] |
371 | 38e250ba | Guido Trotter | return (kvm_cmd, kvm_nics)
|
372 | 38e250ba | Guido Trotter | |
373 | 30e42c4e | Guido Trotter | def _ExecuteKVMRuntime(self, instance, kvm_runtime, incoming=None): |
374 | ee5f20b0 | Guido Trotter | """Execute a KVM cmd, after completing it with some last minute data
|
375 | ee5f20b0 | Guido Trotter |
|
376 | 30e42c4e | Guido Trotter | @type incoming: tuple of strings
|
377 | 30e42c4e | Guido Trotter | @param incoming: (target_host_ip, port)
|
378 | 30e42c4e | Guido Trotter |
|
379 | ee5f20b0 | Guido Trotter | """
|
380 | 1f8b3a27 | Guido Trotter | pidfile, pid, alive = self._InstancePidAlive(instance.name)
|
381 | 1f8b3a27 | Guido Trotter | if alive:
|
382 | ee5f20b0 | Guido Trotter | raise errors.HypervisorError("Failed to start instance %s: %s" % |
383 | ee5f20b0 | Guido Trotter | (instance.name, "already running"))
|
384 | ee5f20b0 | Guido Trotter | |
385 | ee5f20b0 | Guido Trotter | temp_files = [] |
386 | ee5f20b0 | Guido Trotter | |
387 | ee5f20b0 | Guido Trotter | kvm_cmd, kvm_nics = kvm_runtime |
388 | ee5f20b0 | Guido Trotter | |
389 | ee5f20b0 | Guido Trotter | if not kvm_nics: |
390 | ee5f20b0 | Guido Trotter | kvm_cmd.extend(['-net', 'none']) |
391 | ee5f20b0 | Guido Trotter | else:
|
392 | ee5f20b0 | Guido Trotter | for nic_seq, nic in enumerate(kvm_nics): |
393 | ee5f20b0 | Guido Trotter | nic_val = "nic,macaddr=%s,model=virtio" % nic.mac
|
394 | ee5f20b0 | Guido Trotter | script = self._WriteNetScript(instance, nic_seq, nic)
|
395 | ee5f20b0 | Guido Trotter | kvm_cmd.extend(['-net', nic_val])
|
396 | ee5f20b0 | Guido Trotter | kvm_cmd.extend(['-net', 'tap,script=%s' % script]) |
397 | ee5f20b0 | Guido Trotter | temp_files.append(script) |
398 | ee5f20b0 | Guido Trotter | |
399 | 30e42c4e | Guido Trotter | if incoming:
|
400 | 30e42c4e | Guido Trotter | target, port = incoming |
401 | 30e42c4e | Guido Trotter | kvm_cmd.extend(['-incoming', 'tcp:%s:%s' % (target, port)]) |
402 | 30e42c4e | Guido Trotter | |
403 | eb58f9b1 | Guido Trotter | result = utils.RunCmd(kvm_cmd) |
404 | eb58f9b1 | Guido Trotter | if result.failed:
|
405 | eb58f9b1 | Guido Trotter | raise errors.HypervisorError("Failed to start instance %s: %s (%s)" % |
406 | eb58f9b1 | Guido Trotter | (instance.name, result.fail_reason, |
407 | eb58f9b1 | Guido Trotter | result.output)) |
408 | eb58f9b1 | Guido Trotter | |
409 | eb58f9b1 | Guido Trotter | if not utils.IsProcessAlive(utils.ReadPidFile(pidfile)): |
410 | eb58f9b1 | Guido Trotter | raise errors.HypervisorError("Failed to start instance %s: %s" % |
411 | eb58f9b1 | Guido Trotter | (instance.name)) |
412 | eb58f9b1 | Guido Trotter | |
413 | 08137f9e | Iustin Pop | for filename in temp_files: |
414 | 08137f9e | Iustin Pop | utils.RemoveFile(filename) |
415 | eb58f9b1 | Guido Trotter | |
416 | ee5f20b0 | Guido Trotter | def StartInstance(self, instance, block_devices, extra_args): |
417 | ee5f20b0 | Guido Trotter | """Start an instance.
|
418 | ee5f20b0 | Guido Trotter |
|
419 | ee5f20b0 | Guido Trotter | """
|
420 | 1f8b3a27 | Guido Trotter | pidfile, pid, alive = self._InstancePidAlive(instance.name)
|
421 | 1f8b3a27 | Guido Trotter | if alive:
|
422 | ee5f20b0 | Guido Trotter | raise errors.HypervisorError("Failed to start instance %s: %s" % |
423 | ee5f20b0 | Guido Trotter | (instance.name, "already running"))
|
424 | ee5f20b0 | Guido Trotter | |
425 | ee5f20b0 | Guido Trotter | kvm_runtime = self._GenerateKVMRuntime(instance, block_devices, extra_args)
|
426 | 38e250ba | Guido Trotter | self._SaveKVMRuntime(instance, kvm_runtime)
|
427 | ee5f20b0 | Guido Trotter | self._ExecuteKVMRuntime(instance, kvm_runtime)
|
428 | ee5f20b0 | Guido Trotter | |
429 | 6567aff3 | Guido Trotter | def _CallMonitorCommand(self, instance_name, command): |
430 | 6567aff3 | Guido Trotter | """Invoke a command on the instance monitor.
|
431 | 6567aff3 | Guido Trotter |
|
432 | 6567aff3 | Guido Trotter | """
|
433 | 6567aff3 | Guido Trotter | socat = ("echo %s | %s STDIO UNIX-CONNECT:%s" %
|
434 | 6567aff3 | Guido Trotter | (utils.ShellQuote(command), |
435 | 6567aff3 | Guido Trotter | constants.SOCAT_PATH, |
436 | 6567aff3 | Guido Trotter | utils.ShellQuote(self._InstanceMonitor(instance_name))))
|
437 | 6567aff3 | Guido Trotter | result = utils.RunCmd(socat) |
438 | 6567aff3 | Guido Trotter | if result.failed:
|
439 | 6567aff3 | Guido Trotter | msg = ("Failed to send command '%s' to instance %s."
|
440 | 6567aff3 | Guido Trotter | " output: %s, error: %s, fail_reason: %s" %
|
441 | 6567aff3 | Guido Trotter | (instance.name, result.stdout, result.stderr, result.fail_reason)) |
442 | 6567aff3 | Guido Trotter | raise errors.HypervisorError(msg)
|
443 | 6567aff3 | Guido Trotter | |
444 | 6567aff3 | Guido Trotter | return result
|
445 | 6567aff3 | Guido Trotter | |
446 | 6567aff3 | Guido Trotter | def _RetryInstancePowerdown(self, instance, pid, timeout=30): |
447 | 6567aff3 | Guido Trotter | """Wait for an instance to power down.
|
448 | 6567aff3 | Guido Trotter |
|
449 | 6567aff3 | Guido Trotter | """
|
450 | 6567aff3 | Guido Trotter | # Wait up to $timeout seconds
|
451 | 6567aff3 | Guido Trotter | end = time.time() + timeout |
452 | 6567aff3 | Guido Trotter | wait = 1
|
453 | 6567aff3 | Guido Trotter | while time.time() < end and utils.IsProcessAlive(pid): |
454 | 6567aff3 | Guido Trotter | self._CallMonitorCommand(instance.name, 'system_powerdown') |
455 | 6567aff3 | Guido Trotter | time.sleep(wait) |
456 | 6567aff3 | Guido Trotter | # Make wait time longer for next try
|
457 | 6567aff3 | Guido Trotter | if wait < 5: |
458 | 6567aff3 | Guido Trotter | wait *= 1.3
|
459 | 6567aff3 | Guido Trotter | |
460 | eb58f9b1 | Guido Trotter | def StopInstance(self, instance, force=False): |
461 | eb58f9b1 | Guido Trotter | """Stop an instance.
|
462 | eb58f9b1 | Guido Trotter |
|
463 | eb58f9b1 | Guido Trotter | """
|
464 | 1f8b3a27 | Guido Trotter | pidfile, pid, alive = self._InstancePidAlive(instance.name)
|
465 | 1f8b3a27 | Guido Trotter | if pid > 0 and alive: |
466 | 6b5605e8 | Iustin Pop | if force or not instance.hvparams[constants.HV_ACPI]: |
467 | eb58f9b1 | Guido Trotter | utils.KillProcess(pid) |
468 | eb58f9b1 | Guido Trotter | else:
|
469 | 6567aff3 | Guido Trotter | self._RetryInstancePowerdown(instance, pid)
|
470 | eb58f9b1 | Guido Trotter | |
471 | eb58f9b1 | Guido Trotter | if not utils.IsProcessAlive(pid): |
472 | 1f8b3a27 | Guido Trotter | utils.RemoveFile(pidfile) |
473 | c4fbefc8 | Guido Trotter | utils.RemoveFile(self._InstanceMonitor(instance.name))
|
474 | c4fbefc8 | Guido Trotter | utils.RemoveFile(self._InstanceSerial(instance.name))
|
475 | 38e250ba | Guido Trotter | utils.RemoveFile(self._InstanceKVMRuntime(instance.name))
|
476 | 6567aff3 | Guido Trotter | return True |
477 | 6567aff3 | Guido Trotter | else:
|
478 | 6567aff3 | Guido Trotter | return False |
479 | eb58f9b1 | Guido Trotter | |
480 | eb58f9b1 | Guido Trotter | def RebootInstance(self, instance): |
481 | eb58f9b1 | Guido Trotter | """Reboot an instance.
|
482 | eb58f9b1 | Guido Trotter |
|
483 | eb58f9b1 | Guido Trotter | """
|
484 | eb58f9b1 | Guido Trotter | # For some reason if we do a 'send-key ctrl-alt-delete' to the control
|
485 | eb58f9b1 | Guido Trotter | # socket the instance will stop, but now power up again. So we'll resort
|
486 | eb58f9b1 | Guido Trotter | # to shutdown and restart.
|
487 | 1f8b3a27 | Guido Trotter | pidfile, pid, alive = self._InstancePidAlive(instance.name)
|
488 | 1f8b3a27 | Guido Trotter | if not alive: |
489 | 1f8b3a27 | Guido Trotter | raise errors.HypervisorError("Failed to reboot instance %s: not running" % |
490 | 1f8b3a27 | Guido Trotter | (instance.name)) |
491 | f02881e0 | Guido Trotter | # StopInstance will delete the saved KVM runtime so:
|
492 | f02881e0 | Guido Trotter | # ...first load it...
|
493 | f02881e0 | Guido Trotter | kvm_runtime = self._LoadKVMRuntime(instance)
|
494 | f02881e0 | Guido Trotter | # ...now we can safely call StopInstance...
|
495 | f02881e0 | Guido Trotter | if not self.StopInstance(instance): |
496 | f02881e0 | Guido Trotter | self.StopInstance(instance, force=True) |
497 | f02881e0 | Guido Trotter | # ...and finally we can save it again, and execute it...
|
498 | f02881e0 | Guido Trotter | self._SaveKVMRuntime(instance, kvm_runtime)
|
499 | f02881e0 | Guido Trotter | self._ExecuteKVMRuntime(instance, kvm_runtime)
|
500 | eb58f9b1 | Guido Trotter | |
501 | 30e42c4e | Guido Trotter | def MigrationInfo(self, instance): |
502 | 30e42c4e | Guido Trotter | """Get instance information to perform a migration.
|
503 | 30e42c4e | Guido Trotter |
|
504 | 30e42c4e | Guido Trotter | @type instance: L{objects.Instance}
|
505 | 30e42c4e | Guido Trotter | @param instance: instance to be migrated
|
506 | 30e42c4e | Guido Trotter | @rtype: string
|
507 | 30e42c4e | Guido Trotter | @return: content of the KVM runtime file
|
508 | 30e42c4e | Guido Trotter |
|
509 | 30e42c4e | Guido Trotter | """
|
510 | 30e42c4e | Guido Trotter | return self._ReadKVMRuntime(instance.name) |
511 | 30e42c4e | Guido Trotter | |
512 | 30e42c4e | Guido Trotter | def AcceptInstance(self, instance, info, target): |
513 | 30e42c4e | Guido Trotter | """Prepare to accept an instance.
|
514 | 30e42c4e | Guido Trotter |
|
515 | 30e42c4e | Guido Trotter | @type instance: L{objects.Instance}
|
516 | 30e42c4e | Guido Trotter | @param instance: instance to be accepted
|
517 | 30e42c4e | Guido Trotter | @type info: string
|
518 | 30e42c4e | Guido Trotter | @param info: content of the KVM runtime file on the source node
|
519 | 30e42c4e | Guido Trotter | @type target: string
|
520 | 30e42c4e | Guido Trotter | @param target: target host (usually ip), on this node
|
521 | 30e42c4e | Guido Trotter |
|
522 | 30e42c4e | Guido Trotter | """
|
523 | 30e42c4e | Guido Trotter | kvm_runtime = self._LoadKVMRuntime(instance, serialized_runtime=info)
|
524 | 30e42c4e | Guido Trotter | incoming_address = (target, constants.KVM_MIGRATION_PORT) |
525 | 30e42c4e | Guido Trotter | self._ExecuteKVMRuntime(instance, kvm_runtime, incoming=incoming_address)
|
526 | 30e42c4e | Guido Trotter | |
527 | 30e42c4e | Guido Trotter | def FinalizeMigration(self, instance, info, success): |
528 | 30e42c4e | Guido Trotter | """Finalize an instance migration.
|
529 | 30e42c4e | Guido Trotter |
|
530 | 30e42c4e | Guido Trotter | Stop the incoming mode KVM.
|
531 | 30e42c4e | Guido Trotter |
|
532 | 30e42c4e | Guido Trotter | @type instance: L{objects.Instance}
|
533 | 30e42c4e | Guido Trotter | @param instance: instance whose migration is being aborted
|
534 | 30e42c4e | Guido Trotter |
|
535 | 30e42c4e | Guido Trotter | """
|
536 | 30e42c4e | Guido Trotter | if success:
|
537 | 30e42c4e | Guido Trotter | self._WriteKVMRuntime(instance.name, info)
|
538 | 30e42c4e | Guido Trotter | else:
|
539 | 30e42c4e | Guido Trotter | self.StopInstance(instance, force=True) |
540 | 30e42c4e | Guido Trotter | |
541 | 30e42c4e | Guido Trotter | def MigrateInstance(self, instance_name, target, live): |
542 | 30e42c4e | Guido Trotter | """Migrate an instance to a target node.
|
543 | 30e42c4e | Guido Trotter |
|
544 | 30e42c4e | Guido Trotter | The migration will not be attempted if the instance is not
|
545 | 30e42c4e | Guido Trotter | currently running.
|
546 | 30e42c4e | Guido Trotter |
|
547 | 30e42c4e | Guido Trotter | @type instance_name: string
|
548 | 30e42c4e | Guido Trotter | @param instance_name: name of the instance to be migrated
|
549 | 30e42c4e | Guido Trotter | @type target: string
|
550 | 30e42c4e | Guido Trotter | @param target: ip address of the target node
|
551 | 30e42c4e | Guido Trotter | @type live: boolean
|
552 | 30e42c4e | Guido Trotter | @param live: perform a live migration
|
553 | 30e42c4e | Guido Trotter |
|
554 | 30e42c4e | Guido Trotter | """
|
555 | 30e42c4e | Guido Trotter | pidfile, pid, alive = self._InstancePidAlive(instance_name)
|
556 | 30e42c4e | Guido Trotter | if not alive: |
557 | 30e42c4e | Guido Trotter | raise errors.HypervisorError("Instance not running, cannot migrate") |
558 | 30e42c4e | Guido Trotter | |
559 | 30e42c4e | Guido Trotter | if not live: |
560 | 30e42c4e | Guido Trotter | self._CallMonitorCommand(instance_name, 'stop') |
561 | 30e42c4e | Guido Trotter | |
562 | 30e42c4e | Guido Trotter | migrate_command = ('migrate -d tcp:%s:%s' %
|
563 | 30e42c4e | Guido Trotter | (target, constants.KVM_MIGRATION_PORT)) |
564 | 30e42c4e | Guido Trotter | self._CallMonitorCommand(instance_name, migrate_command)
|
565 | 30e42c4e | Guido Trotter | |
566 | 30e42c4e | Guido Trotter | info_command = 'info migrate'
|
567 | 30e42c4e | Guido Trotter | done = False
|
568 | 30e42c4e | Guido Trotter | while not done: |
569 | 30e42c4e | Guido Trotter | result = self._CallMonitorCommand(instance_name, info_command)
|
570 | 30e42c4e | Guido Trotter | match = self._MIGRATION_STATUS_RE.search(result.stdout)
|
571 | 30e42c4e | Guido Trotter | if not match: |
572 | 30e42c4e | Guido Trotter | raise errors.HypervisorError("Unknown 'info migrate' result: %s" % |
573 | 30e42c4e | Guido Trotter | result.stdout) |
574 | 30e42c4e | Guido Trotter | else:
|
575 | 30e42c4e | Guido Trotter | status = match.group(1)
|
576 | 30e42c4e | Guido Trotter | if status == 'completed': |
577 | 30e42c4e | Guido Trotter | done = True
|
578 | 30e42c4e | Guido Trotter | elif status == 'active': |
579 | 30e42c4e | Guido Trotter | time.sleep(2)
|
580 | c087266c | Guido Trotter | elif status == 'failed' or status == 'cancelled': |
581 | c087266c | Guido Trotter | if not live: |
582 | c087266c | Guido Trotter | self._CallMonitorCommand(instance_name, 'cont') |
583 | c087266c | Guido Trotter | raise errors.HypervisorError("Migration %s at the kvm level" % |
584 | c087266c | Guido Trotter | status) |
585 | 30e42c4e | Guido Trotter | else:
|
586 | 30e42c4e | Guido Trotter | logging.info("KVM: unknown migration status '%s'" % status)
|
587 | 30e42c4e | Guido Trotter | time.sleep(2)
|
588 | 30e42c4e | Guido Trotter | |
589 | 30e42c4e | Guido Trotter | utils.KillProcess(pid) |
590 | 30e42c4e | Guido Trotter | utils.RemoveFile(pidfile) |
591 | 30e42c4e | Guido Trotter | utils.RemoveFile(self._InstanceMonitor(instance_name))
|
592 | 30e42c4e | Guido Trotter | utils.RemoveFile(self._InstanceSerial(instance_name))
|
593 | 30e42c4e | Guido Trotter | utils.RemoveFile(self._InstanceKVMRuntime(instance_name))
|
594 | 30e42c4e | Guido Trotter | |
595 | eb58f9b1 | Guido Trotter | def GetNodeInfo(self): |
596 | eb58f9b1 | Guido Trotter | """Return information about the node.
|
597 | eb58f9b1 | Guido Trotter |
|
598 | c41eea6e | Iustin Pop | @return: a dict with the following keys (values in MiB):
|
599 | c41eea6e | Iustin Pop | - memory_total: the total memory size on the node
|
600 | c41eea6e | Iustin Pop | - memory_free: the available memory on the node for instances
|
601 | c41eea6e | Iustin Pop | - memory_dom0: the memory used by the node itself, if available
|
602 | eb58f9b1 | Guido Trotter |
|
603 | eb58f9b1 | Guido Trotter | """
|
604 | eb58f9b1 | Guido Trotter | # global ram usage from the xm info command
|
605 | eb58f9b1 | Guido Trotter | # memory : 3583
|
606 | eb58f9b1 | Guido Trotter | # free_memory : 747
|
607 | eb58f9b1 | Guido Trotter | # note: in xen 3, memory has changed to total_memory
|
608 | eb58f9b1 | Guido Trotter | try:
|
609 | eb58f9b1 | Guido Trotter | fh = file("/proc/meminfo") |
610 | eb58f9b1 | Guido Trotter | try:
|
611 | eb58f9b1 | Guido Trotter | data = fh.readlines() |
612 | eb58f9b1 | Guido Trotter | finally:
|
613 | eb58f9b1 | Guido Trotter | fh.close() |
614 | 90c024f6 | Guido Trotter | except EnvironmentError, err: |
615 | eb58f9b1 | Guido Trotter | raise errors.HypervisorError("Failed to list node info: %s" % err) |
616 | eb58f9b1 | Guido Trotter | |
617 | eb58f9b1 | Guido Trotter | result = {} |
618 | eb58f9b1 | Guido Trotter | sum_free = 0
|
619 | eb58f9b1 | Guido Trotter | for line in data: |
620 | eb58f9b1 | Guido Trotter | splitfields = line.split(":", 1) |
621 | eb58f9b1 | Guido Trotter | |
622 | eb58f9b1 | Guido Trotter | if len(splitfields) > 1: |
623 | eb58f9b1 | Guido Trotter | key = splitfields[0].strip()
|
624 | eb58f9b1 | Guido Trotter | val = splitfields[1].strip()
|
625 | eb58f9b1 | Guido Trotter | if key == 'MemTotal': |
626 | eb58f9b1 | Guido Trotter | result['memory_total'] = int(val.split()[0])/1024 |
627 | eb58f9b1 | Guido Trotter | elif key in ('MemFree', 'Buffers', 'Cached'): |
628 | eb58f9b1 | Guido Trotter | sum_free += int(val.split()[0])/1024 |
629 | eb58f9b1 | Guido Trotter | elif key == 'Active': |
630 | eb58f9b1 | Guido Trotter | result['memory_dom0'] = int(val.split()[0])/1024 |
631 | eb58f9b1 | Guido Trotter | result['memory_free'] = sum_free
|
632 | eb58f9b1 | Guido Trotter | |
633 | eb58f9b1 | Guido Trotter | cpu_total = 0
|
634 | eb58f9b1 | Guido Trotter | try:
|
635 | eb58f9b1 | Guido Trotter | fh = open("/proc/cpuinfo") |
636 | eb58f9b1 | Guido Trotter | try:
|
637 | eb58f9b1 | Guido Trotter | cpu_total = len(re.findall("(?m)^processor\s*:\s*[0-9]+\s*$", |
638 | eb58f9b1 | Guido Trotter | fh.read())) |
639 | eb58f9b1 | Guido Trotter | finally:
|
640 | eb58f9b1 | Guido Trotter | fh.close() |
641 | eb58f9b1 | Guido Trotter | except EnvironmentError, err: |
642 | eb58f9b1 | Guido Trotter | raise errors.HypervisorError("Failed to list node info: %s" % err) |
643 | eb58f9b1 | Guido Trotter | result['cpu_total'] = cpu_total
|
644 | 0105bad3 | Iustin Pop | # FIXME: export correct data here
|
645 | 0105bad3 | Iustin Pop | result['cpu_nodes'] = 1 |
646 | 0105bad3 | Iustin Pop | result['cpu_sockets'] = 1 |
647 | eb58f9b1 | Guido Trotter | |
648 | eb58f9b1 | Guido Trotter | return result
|
649 | eb58f9b1 | Guido Trotter | |
650 | 637ce7f9 | Guido Trotter | @classmethod
|
651 | 5431b2e4 | Guido Trotter | def GetShellCommandForConsole(cls, instance, hvparams, beparams): |
652 | eb58f9b1 | Guido Trotter | """Return a command for connecting to the console of an instance.
|
653 | eb58f9b1 | Guido Trotter |
|
654 | eb58f9b1 | Guido Trotter | """
|
655 | a2faf9ee | Guido Trotter | if hvparams[constants.HV_SERIAL_CONSOLE]:
|
656 | a2faf9ee | Guido Trotter | # FIXME: The socat shell is not perfect. In particular the way we start
|
657 | a2faf9ee | Guido Trotter | # it ctrl+c will close it, rather than being passed to the other end.
|
658 | a2faf9ee | Guido Trotter | # On the other hand if we pass the option 'raw' (or ignbrk=1) there
|
659 | a2faf9ee | Guido Trotter | # will be no way of exiting socat (except killing it from another shell)
|
660 | a2faf9ee | Guido Trotter | # and ctrl+c doesn't work anyway, printing ^C rather than being
|
661 | a2faf9ee | Guido Trotter | # interpreted by kvm. For now we'll leave it this way, which at least
|
662 | a2faf9ee | Guido Trotter | # allows a minimal interaction and changes on the machine.
|
663 | a2faf9ee | Guido Trotter | shell_command = ("%s STDIO,echo=0,icanon=0 UNIX-CONNECT:%s" %
|
664 | a2faf9ee | Guido Trotter | (constants.SOCAT_PATH, |
665 | a2faf9ee | Guido Trotter | utils.ShellQuote(cls._InstanceSerial(instance.name)))) |
666 | a2faf9ee | Guido Trotter | else:
|
667 | a2faf9ee | Guido Trotter | shell_command = "echo 'No serial shell for instance %s'" % instance.name
|
668 | 3be34f57 | Guido Trotter | |
669 | 3be34f57 | Guido Trotter | vnc_bind_address = hvparams[constants.HV_VNC_BIND_ADDRESS] |
670 | 3be34f57 | Guido Trotter | if vnc_bind_address:
|
671 | 377d74c9 | Guido Trotter | if instance.network_port > constants.VNC_BASE_PORT:
|
672 | 377d74c9 | Guido Trotter | display = instance.network_port - constants.VNC_BASE_PORT |
673 | 3be34f57 | Guido Trotter | vnc_command = ("echo 'Instance has VNC listening on %s:%d"
|
674 | 3be34f57 | Guido Trotter | " (display: %d)'" % (vnc_bind_address,
|
675 | 3be34f57 | Guido Trotter | instance.network_port, |
676 | 3be34f57 | Guido Trotter | display)) |
677 | 3be34f57 | Guido Trotter | shell_command = "%s; %s" % (vnc_command, shell_command)
|
678 | 3be34f57 | Guido Trotter | |
679 | a2faf9ee | Guido Trotter | return shell_command
|
680 | eb58f9b1 | Guido Trotter | |
681 | eb58f9b1 | Guido Trotter | def Verify(self): |
682 | eb58f9b1 | Guido Trotter | """Verify the hypervisor.
|
683 | eb58f9b1 | Guido Trotter |
|
684 | eb58f9b1 | Guido Trotter | Check that the binary exists.
|
685 | eb58f9b1 | Guido Trotter |
|
686 | eb58f9b1 | Guido Trotter | """
|
687 | eb58f9b1 | Guido Trotter | if not os.path.exists(constants.KVM_PATH): |
688 | eb58f9b1 | Guido Trotter | return "The kvm binary ('%s') does not exist." % constants.KVM_PATH |
689 | 14aa53cb | Guido Trotter | if not os.path.exists(constants.SOCAT_PATH): |
690 | 14aa53cb | Guido Trotter | return "The socat binary ('%s') does not exist." % constants.SOCAT_PATH |
691 | 14aa53cb | Guido Trotter | |
692 | 6b5605e8 | Iustin Pop | |
693 | 6b5605e8 | Iustin Pop | @classmethod
|
694 | 6b5605e8 | Iustin Pop | def CheckParameterSyntax(cls, hvparams): |
695 | 6b5605e8 | Iustin Pop | """Check the given parameters for validity.
|
696 | 6b5605e8 | Iustin Pop |
|
697 | 6b5605e8 | Iustin Pop | For the KVM hypervisor, this only check the existence of the
|
698 | 6b5605e8 | Iustin Pop | kernel.
|
699 | 6b5605e8 | Iustin Pop |
|
700 | 6b5605e8 | Iustin Pop | @type hvparams: dict
|
701 | 6b5605e8 | Iustin Pop | @param hvparams: dictionary with parameter names/value
|
702 | 6b5605e8 | Iustin Pop | @raise errors.HypervisorError: when a parameter is not valid
|
703 | 6b5605e8 | Iustin Pop |
|
704 | 6b5605e8 | Iustin Pop | """
|
705 | 47387b1e | Guido Trotter | super(KVMHypervisor, cls).CheckParameterSyntax(hvparams)
|
706 | 6b5605e8 | Iustin Pop | |
707 | df5ab9f0 | Guido Trotter | kernel_path = hvparams[constants.HV_KERNEL_PATH] |
708 | df5ab9f0 | Guido Trotter | if kernel_path:
|
709 | df5ab9f0 | Guido Trotter | if not os.path.isabs(hvparams[constants.HV_KERNEL_PATH]): |
710 | df5ab9f0 | Guido Trotter | raise errors.HypervisorError("The kernel path must be an absolute path" |
711 | df5ab9f0 | Guido Trotter | ", if defined")
|
712 | 6b5605e8 | Iustin Pop | |
713 | df5ab9f0 | Guido Trotter | if not hvparams[constants.HV_ROOT_PATH]: |
714 | df5ab9f0 | Guido Trotter | raise errors.HypervisorError("Need a root partition for the instance" |
715 | df5ab9f0 | Guido Trotter | ", if a kernel is defined")
|
716 | 074ca009 | Guido Trotter | |
717 | 6b5605e8 | Iustin Pop | if hvparams[constants.HV_INITRD_PATH]:
|
718 | 6b5605e8 | Iustin Pop | if not os.path.isabs(hvparams[constants.HV_INITRD_PATH]): |
719 | df5ab9f0 | Guido Trotter | raise errors.HypervisorError("The initrd path must an absolute path" |
720 | 6b5605e8 | Iustin Pop | ", if defined")
|
721 | 6b5605e8 | Iustin Pop | |
722 | 56fee73b | Guido Trotter | vnc_bind_address = hvparams[constants.HV_VNC_BIND_ADDRESS] |
723 | 56fee73b | Guido Trotter | if vnc_bind_address:
|
724 | 56fee73b | Guido Trotter | if not utils.IsValidIP(vnc_bind_address): |
725 | 8447f52b | Guido Trotter | if not os.path.isabs(vnc_bind_address): |
726 | 8447f52b | Guido Trotter | raise errors.HypervisorError("The VNC bind address must be either" |
727 | 8447f52b | Guido Trotter | " a valid IP address or an absolute"
|
728 | 8447f52b | Guido Trotter | " pathname. '%s' given" %
|
729 | 8447f52b | Guido Trotter | vnc_bind_address) |
730 | 56fee73b | Guido Trotter | |
731 | 8b2d1013 | Guido Trotter | if hvparams[constants.HV_VNC_X509_VERIFY] and \ |
732 | 8b2d1013 | Guido Trotter | not hvparams[constants.HV_VNC_X509]:
|
733 | 8b2d1013 | Guido Trotter | raise errors.HypervisorError("%s must be defined, if %s is" % |
734 | 8b2d1013 | Guido Trotter | (constants.HV_VNC_X509, |
735 | 8b2d1013 | Guido Trotter | constants.HV_VNC_X509_VERIFY)) |
736 | 8b2d1013 | Guido Trotter | |
737 | 8b2d1013 | Guido Trotter | if hvparams[constants.HV_VNC_X509]:
|
738 | 8b2d1013 | Guido Trotter | if not os.path.isabs(hvparams[constants.HV_VNC_X509]): |
739 | 8b2d1013 | Guido Trotter | raise errors.HypervisorError("The vnc x509 path must an absolute path" |
740 | 8b2d1013 | Guido Trotter | ", if defined")
|
741 | 8b2d1013 | Guido Trotter | |
742 | 66d5dbef | Guido Trotter | iso_path = hvparams[constants.HV_CDROM_IMAGE_PATH] |
743 | 66d5dbef | Guido Trotter | if iso_path and not os.path.isabs(iso_path): |
744 | 66d5dbef | Guido Trotter | raise errors.HypervisorError("The path to the CDROM image must be" |
745 | 66d5dbef | Guido Trotter | " an absolute path, if defined")
|
746 | 66d5dbef | Guido Trotter | |
747 | 66d5dbef | Guido Trotter | boot_order = hvparams[constants.HV_BOOT_ORDER] |
748 | 66d5dbef | Guido Trotter | if boot_order not in ('cdrom', 'disk'): |
749 | 66d5dbef | Guido Trotter | raise errors.HypervisorError("The boot order must be 'cdrom' or 'disk'") |
750 | 66d5dbef | Guido Trotter | |
751 | ec91c05d | Guido Trotter | if boot_order == 'cdrom' and not iso_path: |
752 | ec91c05d | Guido Trotter | raise errors.HypervisorError("Cannot boot from cdrom without an ISO path") |
753 | ec91c05d | Guido Trotter | |
754 | 43440815 | Guido Trotter | nic_type = hvparams[constants.HV_NIC_TYPE] |
755 | 43440815 | Guido Trotter | if nic_type not in constants.HT_KVM_VALID_NIC_TYPES: |
756 | 43440815 | Guido Trotter | raise errors.HypervisorError("Invalid NIC type %s specified for the KVM" |
757 | 43440815 | Guido Trotter | " hypervisor. Please choose one of: %s" %
|
758 | 43440815 | Guido Trotter | (nic_type, |
759 | 43440815 | Guido Trotter | constants.HT_KVM_VALID_NIC_TYPES)) |
760 | 43440815 | Guido Trotter | |
761 | 43440815 | Guido Trotter | disk_type = hvparams[constants.HV_DISK_TYPE] |
762 | 43440815 | Guido Trotter | if disk_type not in constants.HT_KVM_VALID_DISK_TYPES: |
763 | 43440815 | Guido Trotter | raise errors.HypervisorError("Invalid disk type %s specified for the KVM" |
764 | 43440815 | Guido Trotter | " hypervisor. Please choose one of: %s" %
|
765 | 43440815 | Guido Trotter | (disk_type, |
766 | 43440815 | Guido Trotter | constants.HT_KVM_VALID_DISK_TYPES)) |
767 | 43440815 | Guido Trotter | |
768 | 6b5605e8 | Iustin Pop | def ValidateParameters(self, hvparams): |
769 | 6b5605e8 | Iustin Pop | """Check the given parameters for validity.
|
770 | 6b5605e8 | Iustin Pop |
|
771 | 6b5605e8 | Iustin Pop | For the KVM hypervisor, this checks the existence of the
|
772 | 6b5605e8 | Iustin Pop | kernel.
|
773 | 6b5605e8 | Iustin Pop |
|
774 | 6b5605e8 | Iustin Pop | """
|
775 | 47387b1e | Guido Trotter | super(KVMHypervisor, self).ValidateParameters(hvparams) |
776 | 6b5605e8 | Iustin Pop | |
777 | 6b5605e8 | Iustin Pop | kernel_path = hvparams[constants.HV_KERNEL_PATH] |
778 | df5ab9f0 | Guido Trotter | if kernel_path and not os.path.isfile(kernel_path): |
779 | 6b5605e8 | Iustin Pop | raise errors.HypervisorError("Instance kernel '%s' not found or" |
780 | 6b5605e8 | Iustin Pop | " not a file" % kernel_path)
|
781 | 6b5605e8 | Iustin Pop | initrd_path = hvparams[constants.HV_INITRD_PATH] |
782 | 6b5605e8 | Iustin Pop | if initrd_path and not os.path.isfile(initrd_path): |
783 | 6b5605e8 | Iustin Pop | raise errors.HypervisorError("Instance initrd '%s' not found or" |
784 | 6b5605e8 | Iustin Pop | " not a file" % initrd_path)
|
785 | 8b2d1013 | Guido Trotter | |
786 | 8b2d1013 | Guido Trotter | vnc_bind_address = hvparams[constants.HV_VNC_BIND_ADDRESS] |
787 | 8b2d1013 | Guido Trotter | if vnc_bind_address and not utils.IsValidIP(vnc_bind_address) and \ |
788 | 8b2d1013 | Guido Trotter | not os.path.isdir(vnc_bind_address):
|
789 | 8b2d1013 | Guido Trotter | raise errors.HypervisorError("Instance vnc bind address must be either" |
790 | 8b2d1013 | Guido Trotter | " an ip address or an existing directory")
|
791 | 8b2d1013 | Guido Trotter | |
792 | 8b2d1013 | Guido Trotter | vnc_x509 = hvparams[constants.HV_VNC_X509] |
793 | 8b2d1013 | Guido Trotter | if vnc_x509 and not os.path.isdir(vnc_x509): |
794 | 8b2d1013 | Guido Trotter | raise errors.HypervisorError("Instance vnc x509 path '%s' not found" |
795 | 8b2d1013 | Guido Trotter | " or not a directory" % vnc_x509)
|
796 | 8b2d1013 | Guido Trotter | |
797 | 66d5dbef | Guido Trotter | iso_path = hvparams[constants.HV_CDROM_IMAGE_PATH] |
798 | 66d5dbef | Guido Trotter | if iso_path and not os.path.isfile(iso_path): |
799 | 66d5dbef | Guido Trotter | raise errors.HypervisorError("Instance cdrom image '%s' not found or" |
800 | 66d5dbef | Guido Trotter | " not a file" % iso_path) |