Statistics
| Branch: | Tag: | Revision:

root / lib / hypervisor / hv_kvm.py @ 5661b908

History | View | Annotate | Download (12.7 kB)

1
#
2
#
3

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

    
21

    
22
"""KVM hypervisor
23

24
"""
25

    
26
import os
27
import os.path
28
import re
29
import tempfile
30
from cStringIO import StringIO
31

    
32
from ganeti import utils
33
from ganeti import constants
34
from ganeti import errors
35
from ganeti.hypervisor import hv_base
36

    
37

    
38
class KVMHypervisor(hv_base.BaseHypervisor):
39
  """Fake hypervisor interface.
40

41
  This can be used for testing the ganeti code without having to have
42
  a real virtualisation software installed.
43

44
  """
45
  _ROOT_DIR = constants.RUN_GANETI_DIR + "/kvm-hypervisor"
46
  _PIDS_DIR = _ROOT_DIR + "/pid"
47
  _CTRL_DIR = _ROOT_DIR + "/ctrl"
48
  _DIRS = [_ROOT_DIR, _PIDS_DIR, _CTRL_DIR]
49

    
50
  PARAMETERS = [
51
    constants.HV_KERNEL_PATH,
52
    constants.HV_INITRD_PATH,
53
    constants.HV_ACPI,
54
    ]
55

    
56
  def __init__(self):
57
    hv_base.BaseHypervisor.__init__(self)
58
    # Let's make sure the directories we need exist, even if the RUN_DIR lives
59
    # in a tmpfs filesystem or has been otherwise wiped out.
60
    for mydir in self._DIRS:
61
      if not os.path.exists(mydir):
62
        os.mkdir(mydir)
63

    
64
  def _WriteNetScript(self, instance, seq, nic):
65
    """Write a script to connect a net interface to the proper bridge.
66

67
    This can be used by any qemu-type hypervisor.
68

69
    @param instance: instance we're acting on
70
    @type instance: instance object
71
    @param seq: nic sequence number
72
    @type seq: int
73
    @param nic: nic we're acting on
74
    @type nic: nic object
75
    @return: netscript file name
76
    @rtype: string
77

78
    """
79
    script = StringIO()
80
    script.write("#!/bin/sh\n")
81
    script.write("# this is autogenerated by Ganeti, please do not edit\n#\n")
82
    script.write("export INSTANCE=%s\n" % instance.name)
83
    script.write("export MAC=%s\n" % nic.mac)
84
    script.write("export IP=%s\n" % nic.ip)
85
    script.write("export BRIDGE=%s\n" % nic.bridge)
86
    script.write("export INTERFACE=$1\n")
87
    # TODO: make this configurable at ./configure time
88
    script.write("if [ -x /etc/ganeti/kvm-vif-bridge ]; then\n")
89
    script.write("  # Execute the user-specific vif file\n")
90
    script.write("  /etc/ganeti/kvm-vif-bridge\n")
91
    script.write("else\n")
92
    script.write("  # Connect the interface to the bridge\n")
93
    script.write("  /sbin/ifconfig $INTERFACE 0.0.0.0 up\n")
94
    script.write("  /usr/sbin/brctl addif $BRIDGE $INTERFACE\n")
95
    script.write("fi\n\n")
96
    # As much as we'd like to put this in our _ROOT_DIR, that will happen to be
97
    # mounted noexec sometimes, so we'll have to find another place.
98
    (tmpfd, tmpfile_name) = tempfile.mkstemp()
99
    tmpfile = os.fdopen(tmpfd, 'w')
100
    tmpfile.write(script.getvalue())
101
    tmpfile.close()
102
    os.chmod(tmpfile_name, 0755)
103
    return tmpfile_name
104

    
105
  def ListInstances(self):
106
    """Get the list of running instances.
107

108
    We can do this by listing our live instances directory and
109
    checking whether the associated kvm process is still alive.
110

111
    """
112
    result = []
113
    for name in os.listdir(self._PIDS_DIR):
114
      filename = "%s/%s" % (self._PIDS_DIR, name)
115
      if utils.IsProcessAlive(utils.ReadPidFile(filename)):
116
        result.append(name)
117
    return result
118

    
119
  def GetInstanceInfo(self, instance_name):
120
    """Get instance properties.
121

122
    @param instance_name: the instance name
123

124
    @return: tuple (name, id, memory, vcpus, stat, times)
125

126
    """
127
    pidfile = "%s/%s" % (self._PIDS_DIR, instance_name)
128
    pid = utils.ReadPidFile(pidfile)
129
    if not utils.IsProcessAlive(pid):
130
      return None
131

    
132
    cmdline_file = "/proc/%s/cmdline" % pid
133
    try:
134
      fh = open(cmdline_file, 'r')
135
      try:
136
        cmdline = fh.read()
137
      finally:
138
        fh.close()
139
    except IOError, err:
140
      raise errors.HypervisorError("Failed to list instance %s: %s" %
141
                                   (instance_name, err))
142

    
143
    memory = 0
144
    vcpus = 0
145
    stat = "---b-"
146
    times = "0"
147

    
148
    arg_list = cmdline.split('\x00')
149
    while arg_list:
150
      arg =  arg_list.pop(0)
151
      if arg == '-m':
152
        memory = arg_list.pop(0)
153
      elif arg == '-smp':
154
        vcpus = arg_list.pop(0)
155

    
156
    return (instance_name, pid, memory, vcpus, stat, times)
157

    
158
  def GetAllInstancesInfo(self):
159
    """Get properties of all instances.
160

161
    @return: list of tuples (name, id, memory, vcpus, stat, times)
162

163
    """
164
    data = []
165
    for name in os.listdir(self._PIDS_DIR):
166
      filename = "%s/%s" % (self._PIDS_DIR, name)
167
      if utils.IsProcessAlive(utils.ReadPidFile(filename)):
168
        data.append(self.GetInstanceInfo(name))
169

    
170
    return data
171

    
172
  def StartInstance(self, instance, block_devices, extra_args):
173
    """Start an instance.
174

175
    """
176
    temp_files = []
177
    pidfile = self._PIDS_DIR + "/%s" % instance.name
178
    if utils.IsProcessAlive(utils.ReadPidFile(pidfile)):
179
      raise errors.HypervisorError("Failed to start instance %s: %s" %
180
                                   (instance.name, "already running"))
181

    
182
    kvm = constants.KVM_PATH
183
    kvm_cmd = [kvm]
184
    kvm_cmd.extend(['-m', instance.beparams[constants.BE_MEMORY]])
185
    kvm_cmd.extend(['-smp', instance.beparams[constants.BE_VCPUS]])
186
    kvm_cmd.extend(['-pidfile', pidfile])
187
    # used just by the vnc server, if enabled
188
    kvm_cmd.extend(['-name', instance.name])
189
    kvm_cmd.extend(['-daemonize'])
190
    if not instance.hvparams[constants.HV_ACPI]:
191
      kvm_cmd.extend(['-no-acpi'])
192
    if not instance.nics:
193
      kvm_cmd.extend(['-net', 'none'])
194
    else:
195
      nic_seq = 0
196
      for nic in instance.nics:
197
        script = self._WriteNetScript(instance, nic_seq, nic)
198
        # FIXME: handle other models
199
        nic_val = "nic,macaddr=%s,model=virtio" % nic.mac
200
        kvm_cmd.extend(['-net', nic_val])
201
        kvm_cmd.extend(['-net', 'tap,script=%s' % script])
202
        temp_files.append(script)
203
        nic_seq += 1
204

    
205
    boot_drive = True
206
    for cfdev, rldev in block_devices:
207
      # TODO: handle FD_LOOP and FD_BLKTAP (?)
208
      if boot_drive:
209
        boot_val = ',boot=on'
210
        boot_drive = False
211
      else:
212
        boot_val = ''
213

    
214
      # TODO: handle different if= types
215
      if_val = ',if=virtio'
216

    
217
      drive_val = 'file=%s,format=raw%s%s' % (rldev.dev_path, if_val, boot_val)
218
      kvm_cmd.extend(['-drive', drive_val])
219

    
220
    kvm_cmd.extend(['-kernel', instance.hvparams[constants.HV_KERNEL_PATH]])
221

    
222
    initrd_path = instance.hvparams[constants.HV_INITRD_PATH]
223
    if initrd_path:
224
      kvm_cmd.extend(['-initrd', initrd_path])
225

    
226
    kvm_cmd.extend(['-append', 'console=ttyS0,38400 root=/dev/vda'])
227

    
228
    #"hvm_boot_order",
229
    #"hvm_cdrom_image_path",
230

    
231
    kvm_cmd.extend(['-nographic'])
232
    # FIXME: handle vnc, if needed
233
    # How do we decide whether to have it or not?? :(
234
    #"vnc_bind_address",
235
    #"network_port"
236
    base_control = '%s/%s' % (self._CTRL_DIR, instance.name)
237
    monitor_dev = 'unix:%s.monitor,server,nowait' % base_control
238
    kvm_cmd.extend(['-monitor', monitor_dev])
239
    serial_dev = 'unix:%s.serial,server,nowait' % base_control
240
    kvm_cmd.extend(['-serial', serial_dev])
241

    
242
    result = utils.RunCmd(kvm_cmd)
243
    if result.failed:
244
      raise errors.HypervisorError("Failed to start instance %s: %s (%s)" %
245
                                   (instance.name, result.fail_reason,
246
                                    result.output))
247

    
248
    if not utils.IsProcessAlive(utils.ReadPidFile(pidfile)):
249
      raise errors.HypervisorError("Failed to start instance %s: %s" %
250
                                   (instance.name))
251

    
252
    for filename in temp_files:
253
      utils.RemoveFile(filename)
254

    
255
  def StopInstance(self, instance, force=False):
256
    """Stop an instance.
257

258
    """
259
    pid_file = self._PIDS_DIR + "/%s" % instance.name
260
    pid = utils.ReadPidFile(pid_file)
261
    if pid > 0 and utils.IsProcessAlive(pid):
262
      if force or not instance.hvparams[constants.HV_ACPI]:
263
        utils.KillProcess(pid)
264
      else:
265
        # This only works if the instance os has acpi support
266
        monitor_socket = '%s/%s.monitor'  % (self._CTRL_DIR, instance.name)
267
        socat = 'socat -u STDIN UNIX-CONNECT:%s' % monitor_socket
268
        command = "echo 'system_powerdown' | %s" % socat
269
        result = utils.RunCmd(command)
270
        if result.failed:
271
          raise errors.HypervisorError("Failed to stop instance %s: %s" %
272
                                       (instance.name, result.fail_reason))
273

    
274
    if not utils.IsProcessAlive(pid):
275
      utils.RemoveFile(pid_file)
276

    
277
  def RebootInstance(self, instance):
278
    """Reboot an instance.
279

280
    """
281
    # For some reason if we do a 'send-key ctrl-alt-delete' to the control
282
    # socket the instance will stop, but now power up again. So we'll resort
283
    # to shutdown and restart.
284
    self.StopInstance(instance)
285
    self.StartInstance(instance)
286

    
287
  def GetNodeInfo(self):
288
    """Return information about the node.
289

290
    @return: a dict with the following keys (values in MiB):
291
          - memory_total: the total memory size on the node
292
          - memory_free: the available memory on the node for instances
293
          - memory_dom0: the memory used by the node itself, if available
294

295
    """
296
    # global ram usage from the xm info command
297
    # memory                 : 3583
298
    # free_memory            : 747
299
    # note: in xen 3, memory has changed to total_memory
300
    try:
301
      fh = file("/proc/meminfo")
302
      try:
303
        data = fh.readlines()
304
      finally:
305
        fh.close()
306
    except IOError, err:
307
      raise errors.HypervisorError("Failed to list node info: %s" % err)
308

    
309
    result = {}
310
    sum_free = 0
311
    for line in data:
312
      splitfields = line.split(":", 1)
313

    
314
      if len(splitfields) > 1:
315
        key = splitfields[0].strip()
316
        val = splitfields[1].strip()
317
        if key == 'MemTotal':
318
          result['memory_total'] = int(val.split()[0])/1024
319
        elif key in ('MemFree', 'Buffers', 'Cached'):
320
          sum_free += int(val.split()[0])/1024
321
        elif key == 'Active':
322
          result['memory_dom0'] = int(val.split()[0])/1024
323
    result['memory_free'] = sum_free
324

    
325
    cpu_total = 0
326
    try:
327
      fh = open("/proc/cpuinfo")
328
      try:
329
        cpu_total = len(re.findall("(?m)^processor\s*:\s*[0-9]+\s*$",
330
                                   fh.read()))
331
      finally:
332
        fh.close()
333
    except EnvironmentError, err:
334
      raise errors.HypervisorError("Failed to list node info: %s" % err)
335
    result['cpu_total'] = cpu_total
336

    
337
    return result
338

    
339
  @staticmethod
340
  def GetShellCommandForConsole(instance):
341
    """Return a command for connecting to the console of an instance.
342

343
    """
344
    # TODO: we can either try the serial socket or suggest vnc
345
    return "echo Console not available for the kvm hypervisor yet"
346

    
347
  def Verify(self):
348
    """Verify the hypervisor.
349

350
    Check that the binary exists.
351

352
    """
353
    if not os.path.exists(constants.KVM_PATH):
354
      return "The kvm binary ('%s') does not exist." % constants.KVM_PATH
355

    
356
  @classmethod
357
  def CheckParameterSyntax(cls, hvparams):
358
    """Check the given parameters for validity.
359

360
    For the KVM hypervisor, this only check the existence of the
361
    kernel.
362

363
    @type hvparams:  dict
364
    @param hvparams: dictionary with parameter names/value
365
    @raise errors.HypervisorError: when a parameter is not valid
366

367
    """
368
    super(KVMHypervisor, cls).CheckParameterSyntax(hvparams)
369

    
370
    if not hvparams[constants.HV_KERNEL_PATH]:
371
      raise errors.HypervisorError("Need a kernel for the instance")
372

    
373
    if not os.path.isabs(hvparams[constants.HV_KERNEL_PATH]):
374
      raise errors.HypervisorError("The kernel path must an absolute path")
375

    
376
    if hvparams[constants.HV_INITRD_PATH]:
377
      if not os.path.isabs(hvparams[constants.HV_INITRD_PATH]):
378
        raise errors.HypervisorError("The initrd path must an absolute path"
379
                                     ", if defined")
380

    
381
  def ValidateParameters(self, hvparams):
382
    """Check the given parameters for validity.
383

384
    For the KVM hypervisor, this checks the existence of the
385
    kernel.
386

387
    """
388
    super(KVMHypervisor, self).ValidateParameters(hvparams)
389

    
390
    kernel_path = hvparams[constants.HV_KERNEL_PATH]
391
    if not os.path.isfile(kernel_path):
392
      raise errors.HypervisorError("Instance kernel '%s' not found or"
393
                                   " not a file" % kernel_path)
394
    initrd_path = hvparams[constants.HV_INITRD_PATH]
395
    if initrd_path and not os.path.isfile(initrd_path):
396
      raise errors.HypervisorError("Instance initrd '%s' not found or"
397
                                   " not a file" % initrd_path)