Statistics
| Branch: | Tag: | Revision:

root / lib / hypervisor / hv_xen.py @ 5b460366

History | View | Annotate | Download (22.2 kB)

1 65a6f9b7 Michael Hanselmann
#
2 65a6f9b7 Michael Hanselmann
#
3 65a6f9b7 Michael Hanselmann
4 65a6f9b7 Michael Hanselmann
# Copyright (C) 2006, 2007, 2008 Google Inc.
5 65a6f9b7 Michael Hanselmann
#
6 65a6f9b7 Michael Hanselmann
# This program is free software; you can redistribute it and/or modify
7 65a6f9b7 Michael Hanselmann
# it under the terms of the GNU General Public License as published by
8 65a6f9b7 Michael Hanselmann
# the Free Software Foundation; either version 2 of the License, or
9 65a6f9b7 Michael Hanselmann
# (at your option) any later version.
10 65a6f9b7 Michael Hanselmann
#
11 65a6f9b7 Michael Hanselmann
# This program is distributed in the hope that it will be useful, but
12 65a6f9b7 Michael Hanselmann
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 65a6f9b7 Michael Hanselmann
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 65a6f9b7 Michael Hanselmann
# General Public License for more details.
15 65a6f9b7 Michael Hanselmann
#
16 65a6f9b7 Michael Hanselmann
# You should have received a copy of the GNU General Public License
17 65a6f9b7 Michael Hanselmann
# along with this program; if not, write to the Free Software
18 65a6f9b7 Michael Hanselmann
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 65a6f9b7 Michael Hanselmann
# 02110-1301, USA.
20 65a6f9b7 Michael Hanselmann
21 65a6f9b7 Michael Hanselmann
22 65a6f9b7 Michael Hanselmann
"""Xen hypervisors
23 65a6f9b7 Michael Hanselmann

24 65a6f9b7 Michael Hanselmann
"""
25 65a6f9b7 Michael Hanselmann
26 65a6f9b7 Michael Hanselmann
import os
27 65a6f9b7 Michael Hanselmann
import os.path
28 65a6f9b7 Michael Hanselmann
import time
29 b48909c8 Iustin Pop
import logging
30 65a6f9b7 Michael Hanselmann
from cStringIO import StringIO
31 65a6f9b7 Michael Hanselmann
32 65a6f9b7 Michael Hanselmann
from ganeti import constants
33 65a6f9b7 Michael Hanselmann
from ganeti import errors
34 65a6f9b7 Michael Hanselmann
from ganeti import utils
35 a2d32034 Michael Hanselmann
from ganeti.hypervisor import hv_base
36 65a6f9b7 Michael Hanselmann
37 65a6f9b7 Michael Hanselmann
38 a2d32034 Michael Hanselmann
class XenHypervisor(hv_base.BaseHypervisor):
39 65a6f9b7 Michael Hanselmann
  """Xen generic hypervisor interface
40 65a6f9b7 Michael Hanselmann

41 65a6f9b7 Michael Hanselmann
  This is the Xen base class used for both Xen PVM and HVM. It contains
42 65a6f9b7 Michael Hanselmann
  all the functionality that is identical for both.
43 65a6f9b7 Michael Hanselmann

44 65a6f9b7 Michael Hanselmann
  """
45 65a6f9b7 Michael Hanselmann
46 5661b908 Iustin Pop
  @classmethod
47 5661b908 Iustin Pop
  def _WriteConfigFile(cls, instance, block_devices, extra_args):
48 65a6f9b7 Michael Hanselmann
    """Write the Xen config file for the instance.
49 65a6f9b7 Michael Hanselmann

50 65a6f9b7 Michael Hanselmann
    """
51 65a6f9b7 Michael Hanselmann
    raise NotImplementedError
52 65a6f9b7 Michael Hanselmann
53 65a6f9b7 Michael Hanselmann
  @staticmethod
54 4390ccff Guido Trotter
  def _WriteConfigFileStatic(instance_name, data):
55 4390ccff Guido Trotter
    """Write the Xen config file for the instance.
56 4390ccff Guido Trotter

57 4390ccff Guido Trotter
    This version of the function just writes the config file from static data.
58 4390ccff Guido Trotter

59 4390ccff Guido Trotter
    """
60 4390ccff Guido Trotter
    utils.WriteFile("/etc/xen/%s" % instance_name, data=data)
61 4390ccff Guido Trotter
62 4390ccff Guido Trotter
  @staticmethod
63 4390ccff Guido Trotter
  def _ReadConfigFile(instance_name):
64 4390ccff Guido Trotter
    """Returns the contents of the instance config file.
65 4390ccff Guido Trotter

66 4390ccff Guido Trotter
    """
67 4390ccff Guido Trotter
    try:
68 4390ccff Guido Trotter
      file_content = utils.ReadFile("/etc/xen/%s" % instance_name)
69 4390ccff Guido Trotter
    except EnvironmentError, err:
70 4390ccff Guido Trotter
      raise errors.HypervisorError("Failed to load Xen config file: %s" % err)
71 4390ccff Guido Trotter
    return file_content
72 4390ccff Guido Trotter
73 4390ccff Guido Trotter
  @staticmethod
74 53c776b5 Iustin Pop
  def _RemoveConfigFile(instance_name):
75 65a6f9b7 Michael Hanselmann
    """Remove the xen configuration file.
76 65a6f9b7 Michael Hanselmann

77 65a6f9b7 Michael Hanselmann
    """
78 53c776b5 Iustin Pop
    utils.RemoveFile("/etc/xen/%s" % instance_name)
79 65a6f9b7 Michael Hanselmann
80 65a6f9b7 Michael Hanselmann
  @staticmethod
81 65a6f9b7 Michael Hanselmann
  def _GetXMList(include_node):
82 65a6f9b7 Michael Hanselmann
    """Return the list of running instances.
83 65a6f9b7 Michael Hanselmann

84 c41eea6e Iustin Pop
    If the include_node argument is True, then we return information
85 65a6f9b7 Michael Hanselmann
    for dom0 also, otherwise we filter that from the return value.
86 65a6f9b7 Michael Hanselmann

87 c41eea6e Iustin Pop
    @return: list of (name, id, memory, vcpus, state, time spent)
88 65a6f9b7 Michael Hanselmann

89 65a6f9b7 Michael Hanselmann
    """
90 65a6f9b7 Michael Hanselmann
    for dummy in range(5):
91 65a6f9b7 Michael Hanselmann
      result = utils.RunCmd(["xm", "list"])
92 65a6f9b7 Michael Hanselmann
      if not result.failed:
93 65a6f9b7 Michael Hanselmann
        break
94 b48909c8 Iustin Pop
      logging.error("xm list failed (%s): %s", result.fail_reason,
95 b48909c8 Iustin Pop
                    result.output)
96 65a6f9b7 Michael Hanselmann
      time.sleep(1)
97 65a6f9b7 Michael Hanselmann
98 65a6f9b7 Michael Hanselmann
    if result.failed:
99 65a6f9b7 Michael Hanselmann
      raise errors.HypervisorError("xm list failed, retries"
100 65a6f9b7 Michael Hanselmann
                                   " exceeded (%s): %s" %
101 65a6f9b7 Michael Hanselmann
                                   (result.fail_reason, result.stderr))
102 65a6f9b7 Michael Hanselmann
103 65a6f9b7 Michael Hanselmann
    # skip over the heading
104 65a6f9b7 Michael Hanselmann
    lines = result.stdout.splitlines()[1:]
105 65a6f9b7 Michael Hanselmann
    result = []
106 65a6f9b7 Michael Hanselmann
    for line in lines:
107 65a6f9b7 Michael Hanselmann
      # The format of lines is:
108 65a6f9b7 Michael Hanselmann
      # Name      ID Mem(MiB) VCPUs State  Time(s)
109 65a6f9b7 Michael Hanselmann
      # Domain-0   0  3418     4 r-----    266.2
110 65a6f9b7 Michael Hanselmann
      data = line.split()
111 65a6f9b7 Michael Hanselmann
      if len(data) != 6:
112 65a6f9b7 Michael Hanselmann
        raise errors.HypervisorError("Can't parse output of xm list,"
113 65a6f9b7 Michael Hanselmann
                                     " line: %s" % line)
114 65a6f9b7 Michael Hanselmann
      try:
115 65a6f9b7 Michael Hanselmann
        data[1] = int(data[1])
116 65a6f9b7 Michael Hanselmann
        data[2] = int(data[2])
117 65a6f9b7 Michael Hanselmann
        data[3] = int(data[3])
118 65a6f9b7 Michael Hanselmann
        data[5] = float(data[5])
119 65a6f9b7 Michael Hanselmann
      except ValueError, err:
120 65a6f9b7 Michael Hanselmann
        raise errors.HypervisorError("Can't parse output of xm list,"
121 65a6f9b7 Michael Hanselmann
                                     " line: %s, error: %s" % (line, err))
122 65a6f9b7 Michael Hanselmann
123 65a6f9b7 Michael Hanselmann
      # skip the Domain-0 (optional)
124 65a6f9b7 Michael Hanselmann
      if include_node or data[0] != 'Domain-0':
125 65a6f9b7 Michael Hanselmann
        result.append(data)
126 65a6f9b7 Michael Hanselmann
127 65a6f9b7 Michael Hanselmann
    return result
128 65a6f9b7 Michael Hanselmann
129 65a6f9b7 Michael Hanselmann
  def ListInstances(self):
130 65a6f9b7 Michael Hanselmann
    """Get the list of running instances.
131 65a6f9b7 Michael Hanselmann

132 65a6f9b7 Michael Hanselmann
    """
133 65a6f9b7 Michael Hanselmann
    xm_list = self._GetXMList(False)
134 65a6f9b7 Michael Hanselmann
    names = [info[0] for info in xm_list]
135 65a6f9b7 Michael Hanselmann
    return names
136 65a6f9b7 Michael Hanselmann
137 65a6f9b7 Michael Hanselmann
  def GetInstanceInfo(self, instance_name):
138 65a6f9b7 Michael Hanselmann
    """Get instance properties.
139 65a6f9b7 Michael Hanselmann

140 c41eea6e Iustin Pop
    @param instance_name: the instance name
141 c41eea6e Iustin Pop

142 c41eea6e Iustin Pop
    @return: tuple (name, id, memory, vcpus, stat, times)
143 65a6f9b7 Michael Hanselmann

144 65a6f9b7 Michael Hanselmann
    """
145 65a6f9b7 Michael Hanselmann
    xm_list = self._GetXMList(instance_name=="Domain-0")
146 65a6f9b7 Michael Hanselmann
    result = None
147 65a6f9b7 Michael Hanselmann
    for data in xm_list:
148 65a6f9b7 Michael Hanselmann
      if data[0] == instance_name:
149 65a6f9b7 Michael Hanselmann
        result = data
150 65a6f9b7 Michael Hanselmann
        break
151 65a6f9b7 Michael Hanselmann
    return result
152 65a6f9b7 Michael Hanselmann
153 65a6f9b7 Michael Hanselmann
  def GetAllInstancesInfo(self):
154 65a6f9b7 Michael Hanselmann
    """Get properties of all instances.
155 65a6f9b7 Michael Hanselmann

156 c41eea6e Iustin Pop
    @return: list of tuples (name, id, memory, vcpus, stat, times)
157 c41eea6e Iustin Pop

158 65a6f9b7 Michael Hanselmann
    """
159 65a6f9b7 Michael Hanselmann
    xm_list = self._GetXMList(False)
160 65a6f9b7 Michael Hanselmann
    return xm_list
161 65a6f9b7 Michael Hanselmann
162 65a6f9b7 Michael Hanselmann
  def StartInstance(self, instance, block_devices, extra_args):
163 c41eea6e Iustin Pop
    """Start an instance.
164 c41eea6e Iustin Pop

165 c41eea6e Iustin Pop
    """
166 65a6f9b7 Michael Hanselmann
    self._WriteConfigFile(instance, block_devices, extra_args)
167 65a6f9b7 Michael Hanselmann
    result = utils.RunCmd(["xm", "create", instance.name])
168 65a6f9b7 Michael Hanselmann
169 65a6f9b7 Michael Hanselmann
    if result.failed:
170 65a6f9b7 Michael Hanselmann
      raise errors.HypervisorError("Failed to start instance %s: %s (%s)" %
171 65a6f9b7 Michael Hanselmann
                                   (instance.name, result.fail_reason,
172 65a6f9b7 Michael Hanselmann
                                    result.output))
173 65a6f9b7 Michael Hanselmann
174 65a6f9b7 Michael Hanselmann
  def StopInstance(self, instance, force=False):
175 c41eea6e Iustin Pop
    """Stop an instance.
176 c41eea6e Iustin Pop

177 c41eea6e Iustin Pop
    """
178 53c776b5 Iustin Pop
    self._RemoveConfigFile(instance.name)
179 65a6f9b7 Michael Hanselmann
    if force:
180 65a6f9b7 Michael Hanselmann
      command = ["xm", "destroy", instance.name]
181 65a6f9b7 Michael Hanselmann
    else:
182 65a6f9b7 Michael Hanselmann
      command = ["xm", "shutdown", instance.name]
183 65a6f9b7 Michael Hanselmann
    result = utils.RunCmd(command)
184 65a6f9b7 Michael Hanselmann
185 65a6f9b7 Michael Hanselmann
    if result.failed:
186 65a6f9b7 Michael Hanselmann
      raise errors.HypervisorError("Failed to stop instance %s: %s" %
187 65a6f9b7 Michael Hanselmann
                                   (instance.name, result.fail_reason))
188 65a6f9b7 Michael Hanselmann
189 65a6f9b7 Michael Hanselmann
  def RebootInstance(self, instance):
190 c41eea6e Iustin Pop
    """Reboot an instance.
191 c41eea6e Iustin Pop

192 c41eea6e Iustin Pop
    """
193 65a6f9b7 Michael Hanselmann
    result = utils.RunCmd(["xm", "reboot", instance.name])
194 65a6f9b7 Michael Hanselmann
195 65a6f9b7 Michael Hanselmann
    if result.failed:
196 65a6f9b7 Michael Hanselmann
      raise errors.HypervisorError("Failed to reboot instance %s: %s" %
197 65a6f9b7 Michael Hanselmann
                                   (instance.name, result.fail_reason))
198 65a6f9b7 Michael Hanselmann
199 65a6f9b7 Michael Hanselmann
  def GetNodeInfo(self):
200 65a6f9b7 Michael Hanselmann
    """Return information about the node.
201 65a6f9b7 Michael Hanselmann

202 0105bad3 Iustin Pop
    @return: a dict with the following keys (memory values in MiB):
203 c41eea6e Iustin Pop
          - memory_total: the total memory size on the node
204 c41eea6e Iustin Pop
          - memory_free: the available memory on the node for instances
205 c41eea6e Iustin Pop
          - memory_dom0: the memory used by the node itself, if available
206 0105bad3 Iustin Pop
          - nr_cpus: total number of CPUs
207 0105bad3 Iustin Pop
          - nr_nodes: in a NUMA system, the number of domains
208 0105bad3 Iustin Pop
          - nr_sockets: the number of physical CPU sockets in the node
209 65a6f9b7 Michael Hanselmann

210 65a6f9b7 Michael Hanselmann
    """
211 65a6f9b7 Michael Hanselmann
    # note: in xen 3, memory has changed to total_memory
212 65a6f9b7 Michael Hanselmann
    result = utils.RunCmd(["xm", "info"])
213 65a6f9b7 Michael Hanselmann
    if result.failed:
214 b48909c8 Iustin Pop
      logging.error("Can't run 'xm info' (%s): %s", result.fail_reason,
215 b48909c8 Iustin Pop
                    result.output)
216 65a6f9b7 Michael Hanselmann
      return None
217 65a6f9b7 Michael Hanselmann
218 65a6f9b7 Michael Hanselmann
    xmoutput = result.stdout.splitlines()
219 65a6f9b7 Michael Hanselmann
    result = {}
220 0105bad3 Iustin Pop
    cores_per_socket = threads_per_core = nr_cpus = None
221 65a6f9b7 Michael Hanselmann
    for line in xmoutput:
222 65a6f9b7 Michael Hanselmann
      splitfields = line.split(":", 1)
223 65a6f9b7 Michael Hanselmann
224 65a6f9b7 Michael Hanselmann
      if len(splitfields) > 1:
225 65a6f9b7 Michael Hanselmann
        key = splitfields[0].strip()
226 65a6f9b7 Michael Hanselmann
        val = splitfields[1].strip()
227 65a6f9b7 Michael Hanselmann
        if key == 'memory' or key == 'total_memory':
228 65a6f9b7 Michael Hanselmann
          result['memory_total'] = int(val)
229 65a6f9b7 Michael Hanselmann
        elif key == 'free_memory':
230 65a6f9b7 Michael Hanselmann
          result['memory_free'] = int(val)
231 e8a4c138 Iustin Pop
        elif key == 'nr_cpus':
232 0105bad3 Iustin Pop
          nr_cpus = result['cpu_total'] = int(val)
233 0105bad3 Iustin Pop
        elif key == 'nr_nodes':
234 0105bad3 Iustin Pop
          result['cpu_nodes'] = int(val)
235 0105bad3 Iustin Pop
        elif key == 'cores_per_socket':
236 0105bad3 Iustin Pop
          cores_per_socket = int(val)
237 0105bad3 Iustin Pop
        elif key == 'threads_per_core':
238 0105bad3 Iustin Pop
          threads_per_core = int(val)
239 0105bad3 Iustin Pop
240 0105bad3 Iustin Pop
    if (cores_per_socket is not None and
241 0105bad3 Iustin Pop
        threads_per_core is not None and nr_cpus is not None):
242 0105bad3 Iustin Pop
      result['cpu_sockets'] = nr_cpus / (cores_per_socket * threads_per_core)
243 0105bad3 Iustin Pop
244 65a6f9b7 Michael Hanselmann
    dom0_info = self.GetInstanceInfo("Domain-0")
245 65a6f9b7 Michael Hanselmann
    if dom0_info is not None:
246 65a6f9b7 Michael Hanselmann
      result['memory_dom0'] = dom0_info[2]
247 65a6f9b7 Michael Hanselmann
248 65a6f9b7 Michael Hanselmann
    return result
249 65a6f9b7 Michael Hanselmann
250 637ce7f9 Guido Trotter
  @classmethod
251 5431b2e4 Guido Trotter
  def GetShellCommandForConsole(cls, instance, hvparams, beparams):
252 65a6f9b7 Michael Hanselmann
    """Return a command for connecting to the console of an instance.
253 65a6f9b7 Michael Hanselmann

254 65a6f9b7 Michael Hanselmann
    """
255 04c4330c Alexander Schreiber
    return "xm console %s" % instance.name
256 04c4330c Alexander Schreiber
257 65a6f9b7 Michael Hanselmann
258 65a6f9b7 Michael Hanselmann
  def Verify(self):
259 65a6f9b7 Michael Hanselmann
    """Verify the hypervisor.
260 65a6f9b7 Michael Hanselmann

261 65a6f9b7 Michael Hanselmann
    For Xen, this verifies that the xend process is running.
262 65a6f9b7 Michael Hanselmann

263 65a6f9b7 Michael Hanselmann
    """
264 e3e66f02 Michael Hanselmann
    result = utils.RunCmd(["xm", "info"])
265 e3e66f02 Michael Hanselmann
    if result.failed:
266 e3e66f02 Michael Hanselmann
      return "'xm info' failed: %s" % result.fail_reason
267 65a6f9b7 Michael Hanselmann
268 65a6f9b7 Michael Hanselmann
  @staticmethod
269 65a6f9b7 Michael Hanselmann
  def _GetConfigFileDiskData(disk_template, block_devices):
270 65a6f9b7 Michael Hanselmann
    """Get disk directive for xen config file.
271 65a6f9b7 Michael Hanselmann

272 65a6f9b7 Michael Hanselmann
    This method builds the xen config disk directive according to the
273 65a6f9b7 Michael Hanselmann
    given disk_template and block_devices.
274 65a6f9b7 Michael Hanselmann

275 c41eea6e Iustin Pop
    @param disk_template: string containing instance disk template
276 c41eea6e Iustin Pop
    @param block_devices: list of tuples (cfdev, rldev):
277 c41eea6e Iustin Pop
        - cfdev: dict containing ganeti config disk part
278 c41eea6e Iustin Pop
        - rldev: ganeti.bdev.BlockDev object
279 65a6f9b7 Michael Hanselmann

280 c41eea6e Iustin Pop
    @return: string containing disk directive for xen instance config file
281 65a6f9b7 Michael Hanselmann

282 65a6f9b7 Michael Hanselmann
    """
283 65a6f9b7 Michael Hanselmann
    FILE_DRIVER_MAP = {
284 65a6f9b7 Michael Hanselmann
      constants.FD_LOOP: "file",
285 65a6f9b7 Michael Hanselmann
      constants.FD_BLKTAP: "tap:aio",
286 65a6f9b7 Michael Hanselmann
      }
287 65a6f9b7 Michael Hanselmann
    disk_data = []
288 2864f2d9 Iustin Pop
    if len(block_devices) > 24:
289 2864f2d9 Iustin Pop
      # 'z' - 'a' = 24
290 2864f2d9 Iustin Pop
      raise errors.HypervisorError("Too many disks")
291 2864f2d9 Iustin Pop
    # FIXME: instead of this hardcoding here, each of PVM/HVM should
292 2864f2d9 Iustin Pop
    # directly export their info (currently HVM will just sed this info)
293 2864f2d9 Iustin Pop
    namespace = ["sd" + chr(i + ord('a')) for i in range(24)]
294 069cfbf1 Iustin Pop
    for sd_name, (cfdev, dev_path) in zip(namespace, block_devices):
295 d34b16d7 Iustin Pop
      if cfdev.mode == constants.DISK_RDWR:
296 d34b16d7 Iustin Pop
        mode = "w"
297 d34b16d7 Iustin Pop
      else:
298 d34b16d7 Iustin Pop
        mode = "r"
299 65a6f9b7 Michael Hanselmann
      if cfdev.dev_type == constants.LD_FILE:
300 d34b16d7 Iustin Pop
        line = "'%s:%s,%s,%s'" % (FILE_DRIVER_MAP[cfdev.physical_id[0]],
301 d34b16d7 Iustin Pop
                                  dev_path, sd_name, mode)
302 65a6f9b7 Michael Hanselmann
      else:
303 d34b16d7 Iustin Pop
        line = "'phy:%s,%s,%s'" % (dev_path, sd_name, mode)
304 65a6f9b7 Michael Hanselmann
      disk_data.append(line)
305 65a6f9b7 Michael Hanselmann
306 65a6f9b7 Michael Hanselmann
    return disk_data
307 65a6f9b7 Michael Hanselmann
308 4390ccff Guido Trotter
  def MigrationInfo(self, instance):
309 4390ccff Guido Trotter
    """Get instance information to perform a migration.
310 4390ccff Guido Trotter

311 4390ccff Guido Trotter
    @type instance: L{objects.Instance}
312 4390ccff Guido Trotter
    @param instance: instance to be migrated
313 4390ccff Guido Trotter
    @rtype: string
314 4390ccff Guido Trotter
    @return: content of the xen config file
315 4390ccff Guido Trotter

316 4390ccff Guido Trotter
    """
317 4390ccff Guido Trotter
    return self._ReadConfigFile(instance.name)
318 4390ccff Guido Trotter
319 4390ccff Guido Trotter
  def AcceptInstance(self, instance, info, target):
320 4390ccff Guido Trotter
    """Prepare to accept an instance.
321 4390ccff Guido Trotter

322 4390ccff Guido Trotter
    @type instance: L{objects.Instance}
323 4390ccff Guido Trotter
    @param instance: instance to be accepted
324 4390ccff Guido Trotter
    @type info: string
325 4390ccff Guido Trotter
    @param info: content of the xen config file on the source node
326 4390ccff Guido Trotter
    @type target: string
327 4390ccff Guido Trotter
    @param target: target host (usually ip), on this node
328 4390ccff Guido Trotter

329 4390ccff Guido Trotter
    """
330 4390ccff Guido Trotter
    pass
331 4390ccff Guido Trotter
332 4390ccff Guido Trotter
  def FinalizeMigration(self, instance, info, success):
333 4390ccff Guido Trotter
    """Finalize an instance migration.
334 4390ccff Guido Trotter

335 4390ccff Guido Trotter
    After a successful migration we write the xen config file.
336 4390ccff Guido Trotter
    We do nothing on a failure, as we did not change anything at accept time.
337 4390ccff Guido Trotter

338 4390ccff Guido Trotter
    @type instance: L{objects.Instance}
339 4390ccff Guido Trotter
    @param instance: instance whose migration is being aborted
340 4390ccff Guido Trotter
    @type info: string
341 4390ccff Guido Trotter
    @param info: content of the xen config file on the source node
342 4390ccff Guido Trotter
    @type success: boolean
343 4390ccff Guido Trotter
    @param success: whether the migration was a success or a failure
344 4390ccff Guido Trotter

345 4390ccff Guido Trotter
    """
346 4390ccff Guido Trotter
    if success:
347 4390ccff Guido Trotter
      self._WriteConfigFileStatic(instance.name, info)
348 4390ccff Guido Trotter
349 6e7275c0 Iustin Pop
  def MigrateInstance(self, instance, target, live):
350 6e7275c0 Iustin Pop
    """Migrate an instance to a target node.
351 6e7275c0 Iustin Pop

352 6e7275c0 Iustin Pop
    The migration will not be attempted if the instance is not
353 6e7275c0 Iustin Pop
    currently running.
354 6e7275c0 Iustin Pop

355 fdf7f055 Guido Trotter
    @type instance: string
356 fdf7f055 Guido Trotter
    @param instance: instance name
357 fdf7f055 Guido Trotter
    @type target: string
358 fdf7f055 Guido Trotter
    @param target: ip address of the target node
359 fdf7f055 Guido Trotter
    @type live: boolean
360 fdf7f055 Guido Trotter
    @param live: perform a live migration
361 fdf7f055 Guido Trotter

362 6e7275c0 Iustin Pop
    """
363 6e7275c0 Iustin Pop
    if self.GetInstanceInfo(instance) is None:
364 6e7275c0 Iustin Pop
      raise errors.HypervisorError("Instance not running, cannot migrate")
365 6e7275c0 Iustin Pop
    args = ["xm", "migrate"]
366 6e7275c0 Iustin Pop
    if live:
367 6e7275c0 Iustin Pop
      args.append("-l")
368 6e7275c0 Iustin Pop
    args.extend([instance, target])
369 6e7275c0 Iustin Pop
    result = utils.RunCmd(args)
370 6e7275c0 Iustin Pop
    if result.failed:
371 6e7275c0 Iustin Pop
      raise errors.HypervisorError("Failed to migrate instance %s: %s" %
372 6e7275c0 Iustin Pop
                                   (instance, result.output))
373 53c776b5 Iustin Pop
    # remove old xen file after migration succeeded
374 53c776b5 Iustin Pop
    try:
375 53c776b5 Iustin Pop
      self._RemoveConfigFile(instance)
376 c979d253 Iustin Pop
    except EnvironmentError:
377 c979d253 Iustin Pop
      logging.exception("Failure while removing instance config file")
378 6e7275c0 Iustin Pop
379 65a6f9b7 Michael Hanselmann
380 65a6f9b7 Michael Hanselmann
class XenPvmHypervisor(XenHypervisor):
381 65a6f9b7 Michael Hanselmann
  """Xen PVM hypervisor interface"""
382 65a6f9b7 Michael Hanselmann
383 f48148c3 Iustin Pop
  PARAMETERS = [
384 f48148c3 Iustin Pop
    constants.HV_KERNEL_PATH,
385 f48148c3 Iustin Pop
    constants.HV_INITRD_PATH,
386 074ca009 Guido Trotter
    constants.HV_ROOT_PATH,
387 f48148c3 Iustin Pop
    ]
388 f48148c3 Iustin Pop
389 f48148c3 Iustin Pop
  @classmethod
390 f48148c3 Iustin Pop
  def CheckParameterSyntax(cls, hvparams):
391 f48148c3 Iustin Pop
    """Check the given parameters for validity.
392 f48148c3 Iustin Pop

393 f48148c3 Iustin Pop
    For the PVM hypervisor, this only check the existence of the
394 f48148c3 Iustin Pop
    kernel.
395 f48148c3 Iustin Pop

396 f48148c3 Iustin Pop
    @type hvparams:  dict
397 f48148c3 Iustin Pop
    @param hvparams: dictionary with parameter names/value
398 f48148c3 Iustin Pop
    @raise errors.HypervisorError: when a parameter is not valid
399 f48148c3 Iustin Pop

400 f48148c3 Iustin Pop
    """
401 f48148c3 Iustin Pop
    super(XenPvmHypervisor, cls).CheckParameterSyntax(hvparams)
402 f48148c3 Iustin Pop
403 f48148c3 Iustin Pop
    if not hvparams[constants.HV_KERNEL_PATH]:
404 f48148c3 Iustin Pop
      raise errors.HypervisorError("Need a kernel for the instance")
405 f48148c3 Iustin Pop
406 f48148c3 Iustin Pop
    if not os.path.isabs(hvparams[constants.HV_KERNEL_PATH]):
407 50cb2e2a Guido Trotter
      raise errors.HypervisorError("The kernel path must be an absolute path")
408 f48148c3 Iustin Pop
409 074ca009 Guido Trotter
    if not hvparams[constants.HV_ROOT_PATH]:
410 074ca009 Guido Trotter
      raise errors.HypervisorError("Need a root partition for the instance")
411 074ca009 Guido Trotter
412 f48148c3 Iustin Pop
    if hvparams[constants.HV_INITRD_PATH]:
413 f48148c3 Iustin Pop
      if not os.path.isabs(hvparams[constants.HV_INITRD_PATH]):
414 50cb2e2a Guido Trotter
        raise errors.HypervisorError("The initrd path must be an absolute path"
415 f48148c3 Iustin Pop
                                     ", if defined")
416 f48148c3 Iustin Pop
417 f48148c3 Iustin Pop
  def ValidateParameters(self, hvparams):
418 f48148c3 Iustin Pop
    """Check the given parameters for validity.
419 f48148c3 Iustin Pop

420 f48148c3 Iustin Pop
    For the PVM hypervisor, this only check the existence of the
421 f48148c3 Iustin Pop
    kernel.
422 f48148c3 Iustin Pop

423 f48148c3 Iustin Pop
    """
424 f48148c3 Iustin Pop
    super(XenPvmHypervisor, self).ValidateParameters(hvparams)
425 f48148c3 Iustin Pop
426 f48148c3 Iustin Pop
    kernel_path = hvparams[constants.HV_KERNEL_PATH]
427 f48148c3 Iustin Pop
    if not os.path.isfile(kernel_path):
428 f48148c3 Iustin Pop
      raise errors.HypervisorError("Instance kernel '%s' not found or"
429 f48148c3 Iustin Pop
                                   " not a file" % kernel_path)
430 f48148c3 Iustin Pop
    initrd_path = hvparams[constants.HV_INITRD_PATH]
431 f48148c3 Iustin Pop
    if initrd_path and not os.path.isfile(initrd_path):
432 f48148c3 Iustin Pop
      raise errors.HypervisorError("Instance initrd '%s' not found or"
433 f48148c3 Iustin Pop
                                   " not a file" % initrd_path)
434 f48148c3 Iustin Pop
435 65a6f9b7 Michael Hanselmann
  @classmethod
436 65a6f9b7 Michael Hanselmann
  def _WriteConfigFile(cls, instance, block_devices, extra_args):
437 65a6f9b7 Michael Hanselmann
    """Write the Xen config file for the instance.
438 65a6f9b7 Michael Hanselmann

439 65a6f9b7 Michael Hanselmann
    """
440 65a6f9b7 Michael Hanselmann
    config = StringIO()
441 65a6f9b7 Michael Hanselmann
    config.write("# this is autogenerated by Ganeti, please do not edit\n#\n")
442 65a6f9b7 Michael Hanselmann
443 65a6f9b7 Michael Hanselmann
    # kernel handling
444 f48148c3 Iustin Pop
    kpath = instance.hvparams[constants.HV_KERNEL_PATH]
445 65a6f9b7 Michael Hanselmann
    config.write("kernel = '%s'\n" % kpath)
446 65a6f9b7 Michael Hanselmann
447 65a6f9b7 Michael Hanselmann
    # initrd handling
448 f48148c3 Iustin Pop
    initrd_path = instance.hvparams[constants.HV_INITRD_PATH]
449 65a6f9b7 Michael Hanselmann
    if initrd_path:
450 65a6f9b7 Michael Hanselmann
      config.write("ramdisk = '%s'\n" % initrd_path)
451 65a6f9b7 Michael Hanselmann
452 65a6f9b7 Michael Hanselmann
    # rest of the settings
453 8b3fd458 Iustin Pop
    config.write("memory = %d\n" % instance.beparams[constants.BE_MEMORY])
454 8b3fd458 Iustin Pop
    config.write("vcpus = %d\n" % instance.beparams[constants.BE_VCPUS])
455 65a6f9b7 Michael Hanselmann
    config.write("name = '%s'\n" % instance.name)
456 65a6f9b7 Michael Hanselmann
457 65a6f9b7 Michael Hanselmann
    vif_data = []
458 65a6f9b7 Michael Hanselmann
    for nic in instance.nics:
459 65a6f9b7 Michael Hanselmann
      nic_str = "mac=%s, bridge=%s" % (nic.mac, nic.bridge)
460 65a6f9b7 Michael Hanselmann
      ip = getattr(nic, "ip", None)
461 65a6f9b7 Michael Hanselmann
      if ip is not None:
462 65a6f9b7 Michael Hanselmann
        nic_str += ", ip=%s" % ip
463 65a6f9b7 Michael Hanselmann
      vif_data.append("'%s'" % nic_str)
464 65a6f9b7 Michael Hanselmann
465 65a6f9b7 Michael Hanselmann
    config.write("vif = [%s]\n" % ",".join(vif_data))
466 65a6f9b7 Michael Hanselmann
    config.write("disk = [%s]\n" % ",".join(
467 65a6f9b7 Michael Hanselmann
                 cls._GetConfigFileDiskData(instance.disk_template,
468 65a6f9b7 Michael Hanselmann
                                            block_devices)))
469 074ca009 Guido Trotter
470 074ca009 Guido Trotter
    rpath = instance.hvparams[constants.HV_ROOT_PATH]
471 074ca009 Guido Trotter
    config.write("root = '%s ro'\n" % rpath)
472 65a6f9b7 Michael Hanselmann
    config.write("on_poweroff = 'destroy'\n")
473 65a6f9b7 Michael Hanselmann
    config.write("on_reboot = 'restart'\n")
474 65a6f9b7 Michael Hanselmann
    config.write("on_crash = 'restart'\n")
475 65a6f9b7 Michael Hanselmann
    if extra_args:
476 65a6f9b7 Michael Hanselmann
      config.write("extra = '%s'\n" % extra_args)
477 65a6f9b7 Michael Hanselmann
    # just in case it exists
478 65a6f9b7 Michael Hanselmann
    utils.RemoveFile("/etc/xen/auto/%s" % instance.name)
479 65a6f9b7 Michael Hanselmann
    try:
480 73cd67f4 Guido Trotter
      utils.WriteFile("/etc/xen/%s" % instance.name,
481 73cd67f4 Guido Trotter
                      data=config.getvalue())
482 73cd67f4 Guido Trotter
    except EnvironmentError, err:
483 73cd67f4 Guido Trotter
      raise errors.HypervisorError("Cannot write Xen instance confile"
484 73cd67f4 Guido Trotter
                                   " file /etc/xen/%s: %s" %
485 73cd67f4 Guido Trotter
                                   (instance.name, err))
486 73cd67f4 Guido Trotter
487 65a6f9b7 Michael Hanselmann
    return True
488 65a6f9b7 Michael Hanselmann
489 65a6f9b7 Michael Hanselmann
490 65a6f9b7 Michael Hanselmann
class XenHvmHypervisor(XenHypervisor):
491 65a6f9b7 Michael Hanselmann
  """Xen HVM hypervisor interface"""
492 65a6f9b7 Michael Hanselmann
493 f48148c3 Iustin Pop
  PARAMETERS = [
494 f48148c3 Iustin Pop
    constants.HV_ACPI,
495 f48148c3 Iustin Pop
    constants.HV_BOOT_ORDER,
496 f48148c3 Iustin Pop
    constants.HV_CDROM_IMAGE_PATH,
497 f48148c3 Iustin Pop
    constants.HV_DISK_TYPE,
498 f48148c3 Iustin Pop
    constants.HV_NIC_TYPE,
499 f48148c3 Iustin Pop
    constants.HV_PAE,
500 f48148c3 Iustin Pop
    constants.HV_VNC_BIND_ADDRESS,
501 f48148c3 Iustin Pop
    ]
502 f48148c3 Iustin Pop
503 f48148c3 Iustin Pop
  @classmethod
504 f48148c3 Iustin Pop
  def CheckParameterSyntax(cls, hvparams):
505 f48148c3 Iustin Pop
    """Check the given parameter syntax.
506 f48148c3 Iustin Pop

507 f48148c3 Iustin Pop
    """
508 f48148c3 Iustin Pop
    super(XenHvmHypervisor, cls).CheckParameterSyntax(hvparams)
509 f48148c3 Iustin Pop
    # boot order verification
510 f48148c3 Iustin Pop
    boot_order = hvparams[constants.HV_BOOT_ORDER]
511 30948aa6 Guido Trotter
    if not boot_order or len(boot_order.strip("acdn")) != 0:
512 f48148c3 Iustin Pop
      raise errors.HypervisorError("Invalid boot order '%s' specified,"
513 f48148c3 Iustin Pop
                                   " must be one or more of [acdn]" %
514 f48148c3 Iustin Pop
                                   boot_order)
515 f48148c3 Iustin Pop
    # device type checks
516 f48148c3 Iustin Pop
    nic_type = hvparams[constants.HV_NIC_TYPE]
517 f48148c3 Iustin Pop
    if nic_type not in constants.HT_HVM_VALID_NIC_TYPES:
518 5155ede7 Guido Trotter
      raise errors.HypervisorError("Invalid NIC type %s specified for the Xen"
519 5155ede7 Guido Trotter
                                   " HVM hypervisor. Please choose one of: %s"
520 5155ede7 Guido Trotter
                                   % (nic_type,
521 5155ede7 Guido Trotter
                                      constants.HT_HVM_VALID_NIC_TYPES))
522 f48148c3 Iustin Pop
    disk_type = hvparams[constants.HV_DISK_TYPE]
523 f48148c3 Iustin Pop
    if disk_type not in constants.HT_HVM_VALID_DISK_TYPES:
524 5155ede7 Guido Trotter
      raise errors.HypervisorError("Invalid disk type %s specified for the Xen"
525 5155ede7 Guido Trotter
                                   " HVM hypervisor. Please choose one of: %s"
526 5155ede7 Guido Trotter
                                   % (disk_type,
527 5155ede7 Guido Trotter
                                      constants.HT_HVM_VALID_DISK_TYPES))
528 f48148c3 Iustin Pop
    # vnc_bind_address verification
529 f48148c3 Iustin Pop
    vnc_bind_address = hvparams[constants.HV_VNC_BIND_ADDRESS]
530 5b460366 Iustin Pop
    if vnc_bind_address:
531 f48148c3 Iustin Pop
      if not utils.IsValidIP(vnc_bind_address):
532 f48148c3 Iustin Pop
        raise errors.OpPrereqError("given VNC bind address '%s' doesn't look"
533 f48148c3 Iustin Pop
                                   " like a valid IP address" %
534 f48148c3 Iustin Pop
                                   vnc_bind_address)
535 f48148c3 Iustin Pop
536 f48148c3 Iustin Pop
    iso_path = hvparams[constants.HV_CDROM_IMAGE_PATH]
537 f48148c3 Iustin Pop
    if iso_path and not os.path.isabs(iso_path):
538 5661b908 Iustin Pop
      raise errors.HypervisorError("The path to the HVM CDROM image must"
539 5661b908 Iustin Pop
                                   " be an absolute path or None, not %s" %
540 5661b908 Iustin Pop
                                   iso_path)
541 f48148c3 Iustin Pop
542 f48148c3 Iustin Pop
  def ValidateParameters(self, hvparams):
543 f48148c3 Iustin Pop
    """Check the given parameters for validity.
544 f48148c3 Iustin Pop

545 f48148c3 Iustin Pop
    For the PVM hypervisor, this only check the existence of the
546 f48148c3 Iustin Pop
    kernel.
547 f48148c3 Iustin Pop

548 f48148c3 Iustin Pop
    @type hvparams:  dict
549 f48148c3 Iustin Pop
    @param hvparams: dictionary with parameter names/value
550 f48148c3 Iustin Pop
    @raise errors.HypervisorError: when a parameter is not valid
551 f48148c3 Iustin Pop

552 f48148c3 Iustin Pop
    """
553 f48148c3 Iustin Pop
    super(XenHvmHypervisor, self).ValidateParameters(hvparams)
554 f48148c3 Iustin Pop
555 f48148c3 Iustin Pop
    # hvm_cdrom_image_path verification
556 f48148c3 Iustin Pop
    iso_path = hvparams[constants.HV_CDROM_IMAGE_PATH]
557 f48148c3 Iustin Pop
    if iso_path and not os.path.isfile(iso_path):
558 e09fdcfa Iustin Pop
      raise errors.HypervisorError("The HVM CDROM image must either be a"
559 e09fdcfa Iustin Pop
                                   " regular file or a symlink pointing to"
560 e09fdcfa Iustin Pop
                                   " an existing regular file, not %s" %
561 e09fdcfa Iustin Pop
                                   iso_path)
562 f48148c3 Iustin Pop
563 65a6f9b7 Michael Hanselmann
  @classmethod
564 65a6f9b7 Michael Hanselmann
  def _WriteConfigFile(cls, instance, block_devices, extra_args):
565 65a6f9b7 Michael Hanselmann
    """Create a Xen 3.1 HVM config file.
566 65a6f9b7 Michael Hanselmann

567 65a6f9b7 Michael Hanselmann
    """
568 65a6f9b7 Michael Hanselmann
    config = StringIO()
569 65a6f9b7 Michael Hanselmann
    config.write("# this is autogenerated by Ganeti, please do not edit\n#\n")
570 65a6f9b7 Michael Hanselmann
    config.write("kernel = '/usr/lib/xen/boot/hvmloader'\n")
571 65a6f9b7 Michael Hanselmann
    config.write("builder = 'hvm'\n")
572 8b3fd458 Iustin Pop
    config.write("memory = %d\n" % instance.beparams[constants.BE_MEMORY])
573 8b3fd458 Iustin Pop
    config.write("vcpus = %d\n" % instance.beparams[constants.BE_VCPUS])
574 65a6f9b7 Michael Hanselmann
    config.write("name = '%s'\n" % instance.name)
575 f48148c3 Iustin Pop
    if instance.hvparams[constants.HV_PAE]:
576 a21dda8b Iustin Pop
      config.write("pae = 1\n")
577 a21dda8b Iustin Pop
    else:
578 a21dda8b Iustin Pop
      config.write("pae = 0\n")
579 f48148c3 Iustin Pop
    if instance.hvparams[constants.HV_ACPI]:
580 a21dda8b Iustin Pop
      config.write("acpi = 1\n")
581 a21dda8b Iustin Pop
    else:
582 a21dda8b Iustin Pop
      config.write("acpi = 0\n")
583 65a6f9b7 Michael Hanselmann
    config.write("apic = 1\n")
584 65a6f9b7 Michael Hanselmann
    arch = os.uname()[4]
585 65a6f9b7 Michael Hanselmann
    if '64' in arch:
586 65a6f9b7 Michael Hanselmann
      config.write("device_model = '/usr/lib64/xen/bin/qemu-dm'\n")
587 65a6f9b7 Michael Hanselmann
    else:
588 65a6f9b7 Michael Hanselmann
      config.write("device_model = '/usr/lib/xen/bin/qemu-dm'\n")
589 30948aa6 Guido Trotter
    config.write("boot = '%s'\n" % instance.hvparams[constants.HV_BOOT_ORDER])
590 65a6f9b7 Michael Hanselmann
    config.write("sdl = 0\n")
591 97efde45 Guido Trotter
    config.write("usb = 1\n")
592 97efde45 Guido Trotter
    config.write("usbdevice = 'tablet'\n")
593 65a6f9b7 Michael Hanselmann
    config.write("vnc = 1\n")
594 f48148c3 Iustin Pop
    if instance.hvparams[constants.HV_VNC_BIND_ADDRESS] is None:
595 d0c11cf7 Alexander Schreiber
      config.write("vnclisten = '%s'\n" % constants.VNC_DEFAULT_BIND_ADDRESS)
596 d0c11cf7 Alexander Schreiber
    else:
597 f48148c3 Iustin Pop
      config.write("vnclisten = '%s'\n" %
598 f48148c3 Iustin Pop
                   instance.hvparams["vnc_bind_address"])
599 65a6f9b7 Michael Hanselmann
600 377d74c9 Guido Trotter
    if instance.network_port > constants.VNC_BASE_PORT:
601 377d74c9 Guido Trotter
      display = instance.network_port - constants.VNC_BASE_PORT
602 65a6f9b7 Michael Hanselmann
      config.write("vncdisplay = %s\n" % display)
603 65a6f9b7 Michael Hanselmann
      config.write("vncunused = 0\n")
604 65a6f9b7 Michael Hanselmann
    else:
605 65a6f9b7 Michael Hanselmann
      config.write("# vncdisplay = 1\n")
606 65a6f9b7 Michael Hanselmann
      config.write("vncunused = 1\n")
607 65a6f9b7 Michael Hanselmann
608 65a6f9b7 Michael Hanselmann
    try:
609 78f66a17 Guido Trotter
      password = utils.ReadFile(constants.VNC_PASSWORD_FILE)
610 78f66a17 Guido Trotter
    except EnvironmentError, err:
611 78f66a17 Guido Trotter
      raise errors.HypervisorError("Failed to open VNC password file %s: %s" %
612 78f66a17 Guido Trotter
                                   (constants.VNC_PASSWORD_FILE, err))
613 65a6f9b7 Michael Hanselmann
614 65a6f9b7 Michael Hanselmann
    config.write("vncpasswd = '%s'\n" % password.rstrip())
615 65a6f9b7 Michael Hanselmann
616 65a6f9b7 Michael Hanselmann
    config.write("serial = 'pty'\n")
617 65a6f9b7 Michael Hanselmann
    config.write("localtime = 1\n")
618 65a6f9b7 Michael Hanselmann
619 65a6f9b7 Michael Hanselmann
    vif_data = []
620 f48148c3 Iustin Pop
    nic_type = instance.hvparams[constants.HV_NIC_TYPE]
621 f48148c3 Iustin Pop
    if nic_type is None:
622 f48148c3 Iustin Pop
      # ensure old instances don't change
623 f48148c3 Iustin Pop
      nic_type_str = ", type=ioemu"
624 d08f6067 Guido Trotter
    elif nic_type == constants.HT_NIC_PARAVIRTUAL:
625 f48148c3 Iustin Pop
      nic_type_str = ", type=paravirtualized"
626 f48148c3 Iustin Pop
    else:
627 f48148c3 Iustin Pop
      nic_type_str = ", model=%s, type=ioemu" % nic_type
628 65a6f9b7 Michael Hanselmann
    for nic in instance.nics:
629 f48148c3 Iustin Pop
      nic_str = "mac=%s, bridge=%s%s" % (nic.mac, nic.bridge, nic_type_str)
630 65a6f9b7 Michael Hanselmann
      ip = getattr(nic, "ip", None)
631 65a6f9b7 Michael Hanselmann
      if ip is not None:
632 65a6f9b7 Michael Hanselmann
        nic_str += ", ip=%s" % ip
633 65a6f9b7 Michael Hanselmann
      vif_data.append("'%s'" % nic_str)
634 65a6f9b7 Michael Hanselmann
635 65a6f9b7 Michael Hanselmann
    config.write("vif = [%s]\n" % ",".join(vif_data))
636 a21dda8b Iustin Pop
    disk_data = cls._GetConfigFileDiskData(instance.disk_template,
637 a21dda8b Iustin Pop
                                            block_devices)
638 f48148c3 Iustin Pop
    disk_type = instance.hvparams[constants.HV_DISK_TYPE]
639 d08f6067 Guido Trotter
    if disk_type in (None, constants.HT_DISK_IOEMU):
640 5397e0b7 Alexander Schreiber
      replacement = ",ioemu:hd"
641 5397e0b7 Alexander Schreiber
    else:
642 5397e0b7 Alexander Schreiber
      replacement = ",hd"
643 5397e0b7 Alexander Schreiber
    disk_data = [line.replace(",sd", replacement) for line in disk_data]
644 f48148c3 Iustin Pop
    iso_path = instance.hvparams[constants.HV_CDROM_IMAGE_PATH]
645 f48148c3 Iustin Pop
    if iso_path:
646 f48148c3 Iustin Pop
      iso = "'file:%s,hdc:cdrom,r'" % iso_path
647 a21dda8b Iustin Pop
      disk_data.append(iso)
648 a21dda8b Iustin Pop
649 a21dda8b Iustin Pop
    config.write("disk = [%s]\n" % (",".join(disk_data)))
650 a21dda8b Iustin Pop
651 65a6f9b7 Michael Hanselmann
    config.write("on_poweroff = 'destroy'\n")
652 65a6f9b7 Michael Hanselmann
    config.write("on_reboot = 'restart'\n")
653 65a6f9b7 Michael Hanselmann
    config.write("on_crash = 'restart'\n")
654 65a6f9b7 Michael Hanselmann
    if extra_args:
655 65a6f9b7 Michael Hanselmann
      config.write("extra = '%s'\n" % extra_args)
656 65a6f9b7 Michael Hanselmann
    # just in case it exists
657 65a6f9b7 Michael Hanselmann
    utils.RemoveFile("/etc/xen/auto/%s" % instance.name)
658 65a6f9b7 Michael Hanselmann
    try:
659 73cd67f4 Guido Trotter
      utils.WriteFile("/etc/xen/%s" % instance.name,
660 73cd67f4 Guido Trotter
                      data=config.getvalue())
661 73cd67f4 Guido Trotter
    except EnvironmentError, err:
662 73cd67f4 Guido Trotter
      raise errors.HypervisorError("Cannot write Xen instance confile"
663 73cd67f4 Guido Trotter
                                   " file /etc/xen/%s: %s" %
664 73cd67f4 Guido Trotter
                                   (instance.name, err))
665 73cd67f4 Guido Trotter
666 65a6f9b7 Michael Hanselmann
    return True