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