Revision ee5f20b0

b/lib/hypervisor/hv_kvm.py
178 178

  
179 179
    return data
180 180

  
181
  def StartInstance(self, instance, block_devices, extra_args):
182
    """Start an instance.
181
  def _GenerateKVMRuntime(self, instance, block_devices, extra_args):
182
    """Generate KVM information to start an instance.
183 183

  
184 184
    """
185
    temp_files = []
186 185
    pidfile = self._PIDS_DIR + "/%s" % instance.name
187
    if utils.IsProcessAlive(utils.ReadPidFile(pidfile)):
188
      raise errors.HypervisorError("Failed to start instance %s: %s" %
189
                                   (instance.name, "already running"))
190

  
191 186
    kvm = constants.KVM_PATH
192 187
    kvm_cmd = [kvm]
193 188
    kvm_cmd.extend(['-m', instance.beparams[constants.BE_MEMORY]])
......
198 193
    kvm_cmd.extend(['-daemonize'])
199 194
    if not instance.hvparams[constants.HV_ACPI]:
200 195
      kvm_cmd.extend(['-no-acpi'])
201
    if not instance.nics:
202
      kvm_cmd.extend(['-net', 'none'])
203
    else:
204
      nic_seq = 0
205
      for nic in instance.nics:
206
        script = self._WriteNetScript(instance, nic_seq, nic)
207
        # FIXME: handle other models
208
        nic_val = "nic,macaddr=%s,model=virtio" % nic.mac
209
        kvm_cmd.extend(['-net', nic_val])
210
        kvm_cmd.extend(['-net', 'tap,script=%s' % script])
211
        temp_files.append(script)
212
        nic_seq += 1
213 196

  
214 197
    boot_drive = True
215 198
    for cfdev, dev_path in block_devices:
......
248 231
    serial_dev = 'unix:%s,server,nowait' % self._InstanceSerial(instance.name)
249 232
    kvm_cmd.extend(['-serial', serial_dev])
250 233

  
234
    # Save the current instance nics, but defer their expansion as parameters,
235
    # as we'll need to generate executable temp files for them.
236
    kvm_nics = instance.nics
237

  
238
    return (kvm_cmd, kvm_nics)
239

  
240
  def _ExecuteKVMRuntime(self, instance, kvm_runtime):
241
    """Execute a KVM cmd, after completing it with some last minute data
242

  
243
    """
244
    pidfile = self._PIDS_DIR + "/%s" % instance.name
245
    if utils.IsProcessAlive(utils.ReadPidFile(pidfile)):
246
      raise errors.HypervisorError("Failed to start instance %s: %s" %
247
                                   (instance.name, "already running"))
248

  
249
    temp_files = []
250

  
251
    kvm_cmd, kvm_nics = kvm_runtime
252

  
253
    if not kvm_nics:
254
      kvm_cmd.extend(['-net', 'none'])
255
    else:
256
      for nic_seq, nic in enumerate(kvm_nics):
257
        nic_val = "nic,macaddr=%s,model=virtio" % nic.mac
258
        script = self._WriteNetScript(instance, nic_seq, nic)
259
        kvm_cmd.extend(['-net', nic_val])
260
        kvm_cmd.extend(['-net', 'tap,script=%s' % script])
261
        temp_files.append(script)
262

  
251 263
    result = utils.RunCmd(kvm_cmd)
252 264
    if result.failed:
253 265
      raise errors.HypervisorError("Failed to start instance %s: %s (%s)" %
......
261 273
    for filename in temp_files:
262 274
      utils.RemoveFile(filename)
263 275

  
276
  def StartInstance(self, instance, block_devices, extra_args):
277
    """Start an instance.
278

  
279
    """
280
    pidfile = self._PIDS_DIR + "/%s" % instance.name
281
    if utils.IsProcessAlive(utils.ReadPidFile(pidfile)):
282
      raise errors.HypervisorError("Failed to start instance %s: %s" %
283
                                   (instance.name, "already running"))
284

  
285
    kvm_runtime = self._GenerateKVMRuntime(instance, block_devices, extra_args)
286
    self._ExecuteKVMRuntime(instance, kvm_runtime)
287

  
264 288
  def StopInstance(self, instance, force=False):
265 289
    """Stop an instance.
266 290

  

Also available in: Unified diff