Revision d0c8c01d lib/hypervisor/hv_kvm.py

b/lib/hypervisor/hv_kvm.py
196 196
    constants.HV_MEM_PATH: hv_base.OPT_DIR_CHECK,
197 197
    }
198 198

  
199
  _MIGRATION_STATUS_RE = re.compile('Migration\s+status:\s+(\w+)',
199
  _MIGRATION_STATUS_RE = re.compile("Migration\s+status:\s+(\w+)",
200 200
                                    re.M | re.I)
201 201
  _MIGRATION_INFO_MAX_BAD_ANSWERS = 5
202 202
  _MIGRATION_INFO_RETRY_DELAY = 2
......
257 257
    memory = 0
258 258
    vcpus = 0
259 259

  
260
    arg_list = cmdline.split('\x00')
260
    arg_list = cmdline.split("\x00")
261 261
    while arg_list:
262 262
      arg =  arg_list.pop(0)
263 263
      if arg == "-name":
......
526 526
    kvm = constants.KVM_PATH
527 527
    kvm_cmd = [kvm]
528 528
    # used just by the vnc server, if enabled
529
    kvm_cmd.extend(['-name', instance.name])
530
    kvm_cmd.extend(['-m', instance.beparams[constants.BE_MEMORY]])
531
    kvm_cmd.extend(['-smp', instance.beparams[constants.BE_VCPUS]])
532
    kvm_cmd.extend(['-pidfile', pidfile])
533
    kvm_cmd.extend(['-daemonize'])
529
    kvm_cmd.extend(["-name", instance.name])
530
    kvm_cmd.extend(["-m", instance.beparams[constants.BE_MEMORY]])
531
    kvm_cmd.extend(["-smp", instance.beparams[constants.BE_VCPUS]])
532
    kvm_cmd.extend(["-pidfile", pidfile])
533
    kvm_cmd.extend(["-daemonize"])
534 534
    if not instance.hvparams[constants.HV_ACPI]:
535
      kvm_cmd.extend(['-no-acpi'])
535
      kvm_cmd.extend(["-no-acpi"])
536 536
    if startup_paused:
537
      kvm_cmd.extend(['-S'])
537
      kvm_cmd.extend(["-S"])
538 538

  
539 539
    hvp = instance.hvparams
540 540
    boot_disk = hvp[constants.HV_BOOT_ORDER] == constants.HT_BO_DISK
......
548 548
      kvm_cmd.extend(["-disable-kvm"])
549 549

  
550 550
    if boot_network:
551
      kvm_cmd.extend(['-boot', 'n'])
551
      kvm_cmd.extend(["-boot", "n"])
552 552

  
553 553
    disk_type = hvp[constants.HV_DISK_TYPE]
554 554
    if disk_type == constants.HT_DISK_PARAVIRTUAL:
555
      if_val = ',if=virtio'
555
      if_val = ",if=virtio"
556 556
    else:
557
      if_val = ',if=%s' % disk_type
557
      if_val = ",if=%s" % disk_type
558 558
    # Cache mode
559 559
    disk_cache = hvp[constants.HV_DISK_CACHE]
560 560
    if instance.disk_template in constants.DTS_EXT_MIRROR:
......
575 575
      # TODO: handle FD_LOOP and FD_BLKTAP (?)
576 576
      boot_val = ""
577 577
      if boot_disk:
578
        kvm_cmd.extend(['-boot', 'c'])
578
        kvm_cmd.extend(["-boot", "c"])
579 579
        boot_disk = False
580 580
        if (v_major, v_min) < (0, 14) and disk_type != constants.HT_DISK_IDE:
581 581
          boot_val = ",boot=on"
582 582

  
583
      drive_val = 'file=%s,format=raw%s%s%s' % (dev_path, if_val, boot_val,
583
      drive_val = "file=%s,format=raw%s%s%s" % (dev_path, if_val, boot_val,
584 584
                                                cache_val)
585
      kvm_cmd.extend(['-drive', drive_val])
585
      kvm_cmd.extend(["-drive", drive_val])
586 586

  
587 587
    #Now we can specify a different device type for CDROM devices.
588 588
    cdrom_disk_type = hvp[constants.HV_KVM_CDROM_DISK_TYPE]
......
591 591

  
592 592
    iso_image = hvp[constants.HV_CDROM_IMAGE_PATH]
593 593
    if iso_image:
594
      options = ',format=raw,media=cdrom'
594
      options = ",format=raw,media=cdrom"
595 595
      if boot_cdrom:
596
        kvm_cmd.extend(['-boot', 'd'])
596
        kvm_cmd.extend(["-boot", "d"])
597 597
        if cdrom_disk_type != constants.HT_DISK_IDE:
598
          options = '%s,boot=on,if=%s' % (options, constants.HT_DISK_IDE)
598
          options = "%s,boot=on,if=%s" % (options, constants.HT_DISK_IDE)
599 599
        else:
600
          options = '%s,boot=on' % options
600
          options = "%s,boot=on" % options
601 601
      else:
602 602
        if cdrom_disk_type == constants.HT_DISK_PARAVIRTUAL:
603
          if_val = ',if=virtio'
603
          if_val = ",if=virtio"
604 604
        else:
605
          if_val = ',if=%s' % cdrom_disk_type
606
        options = '%s%s' % (options, if_val)
607
      drive_val = 'file=%s%s' % (iso_image, options)
608
      kvm_cmd.extend(['-drive', drive_val])
605
          if_val = ",if=%s" % cdrom_disk_type
606
        options = "%s%s" % (options, if_val)
607
      drive_val = "file=%s%s" % (iso_image, options)
608
      kvm_cmd.extend(["-drive", drive_val])
609 609

  
610 610
    iso_image2 = hvp[constants.HV_KVM_CDROM2_IMAGE_PATH]
611 611
    if iso_image2:
612
      options = ',format=raw,media=cdrom'
612
      options = ",format=raw,media=cdrom"
613 613
      if cdrom_disk_type == constants.HT_DISK_PARAVIRTUAL:
614
        if_val = ',if=virtio'
614
        if_val = ",if=virtio"
615 615
      else:
616
        if_val = ',if=%s' % cdrom_disk_type
617
      options = '%s%s' % (options, if_val)
618
      drive_val = 'file=%s%s' % (iso_image2, options)
619
      kvm_cmd.extend(['-drive', drive_val])
616
        if_val = ",if=%s" % cdrom_disk_type
617
      options = "%s%s" % (options, if_val)
618
      drive_val = "file=%s%s" % (iso_image2, options)
619
      kvm_cmd.extend(["-drive", drive_val])
620 620

  
621 621
    floppy_image = hvp[constants.HV_KVM_FLOPPY_IMAGE_PATH]
622 622
    if floppy_image:
623
      options = ',format=raw,media=disk'
623
      options = ",format=raw,media=disk"
624 624
      if boot_floppy:
625
        kvm_cmd.extend(['-boot', 'a'])
626
        options = '%s,boot=on' % options
627
      if_val = ',if=floppy'
628
      options = '%s%s' % (options, if_val)
629
      drive_val = 'file=%s%s' % (floppy_image, options)
630
      kvm_cmd.extend(['-drive', drive_val])
625
        kvm_cmd.extend(["-boot", "a"])
626
        options = "%s,boot=on" % options
627
      if_val = ",if=floppy"
628
      options = "%s%s" % (options, if_val)
629
      drive_val = "file=%s%s" % (floppy_image, options)
630
      kvm_cmd.extend(["-drive", drive_val])
631 631

  
632 632
    kernel_path = hvp[constants.HV_KERNEL_PATH]
633 633
    if kernel_path:
634
      kvm_cmd.extend(['-kernel', kernel_path])
634
      kvm_cmd.extend(["-kernel", kernel_path])
635 635
      initrd_path = hvp[constants.HV_INITRD_PATH]
636 636
      if initrd_path:
637
        kvm_cmd.extend(['-initrd', initrd_path])
638
      root_append = ['root=%s' % hvp[constants.HV_ROOT_PATH],
637
        kvm_cmd.extend(["-initrd", initrd_path])
638
      root_append = ["root=%s" % hvp[constants.HV_ROOT_PATH],
639 639
                     hvp[constants.HV_KERNEL_ARGS]]
640 640
      if hvp[constants.HV_SERIAL_CONSOLE]:
641
        root_append.append('console=ttyS0,38400')
642
      kvm_cmd.extend(['-append', ' '.join(root_append)])
641
        root_append.append("console=ttyS0,38400")
642
      kvm_cmd.extend(["-append", " ".join(root_append)])
643 643

  
644 644
    mem_path = hvp[constants.HV_MEM_PATH]
645 645
    if mem_path:
......
649 649
    vnc_bind_address = hvp[constants.HV_VNC_BIND_ADDRESS]
650 650

  
651 651
    if mouse_type:
652
      kvm_cmd.extend(['-usb'])
653
      kvm_cmd.extend(['-usbdevice', mouse_type])
652
      kvm_cmd.extend(["-usb"])
653
      kvm_cmd.extend(["-usbdevice", mouse_type])
654 654
    elif vnc_bind_address:
655
      kvm_cmd.extend(['-usbdevice', constants.HT_MOUSE_TABLET])
655
      kvm_cmd.extend(["-usbdevice", constants.HT_MOUSE_TABLET])
656 656

  
657 657
    keymap = hvp[constants.HV_KEYMAP]
658 658
    if keymap:
......
669 669
        if instance.network_port > constants.VNC_BASE_PORT:
670 670
          display = instance.network_port - constants.VNC_BASE_PORT
671 671
          if vnc_bind_address == constants.IP4_ADDRESS_ANY:
672
            vnc_arg = ':%d' % (display)
672
            vnc_arg = ":%d" % (display)
673 673
          else:
674
            vnc_arg = '%s:%d' % (vnc_bind_address, display)
674
            vnc_arg = "%s:%d" % (vnc_bind_address, display)
675 675
        else:
676 676
          logging.error("Network port is not a valid VNC display (%d < %d)."
677 677
                        " Not starting VNC", instance.network_port,
678 678
                        constants.VNC_BASE_PORT)
679
          vnc_arg = 'none'
679
          vnc_arg = "none"
680 680

  
681 681
        # Only allow tls and other option when not binding to a file, for now.
682 682
        # kvm/qemu gets confused otherwise about the filename to use.
683
        vnc_append = ''
683
        vnc_append = ""
684 684
        if hvp[constants.HV_VNC_TLS]:
685
          vnc_append = '%s,tls' % vnc_append
685
          vnc_append = "%s,tls" % vnc_append
686 686
          if hvp[constants.HV_VNC_X509_VERIFY]:
687
            vnc_append = '%s,x509verify=%s' % (vnc_append,
687
            vnc_append = "%s,x509verify=%s" % (vnc_append,
688 688
                                               hvp[constants.HV_VNC_X509])
689 689
          elif hvp[constants.HV_VNC_X509]:
690
            vnc_append = '%s,x509=%s' % (vnc_append,
690
            vnc_append = "%s,x509=%s" % (vnc_append,
691 691
                                         hvp[constants.HV_VNC_X509])
692 692
        if hvp[constants.HV_VNC_PASSWORD_FILE]:
693
          vnc_append = '%s,password' % vnc_append
693
          vnc_append = "%s,password" % vnc_append
694 694

  
695
        vnc_arg = '%s%s' % (vnc_arg, vnc_append)
695
        vnc_arg = "%s%s" % (vnc_arg, vnc_append)
696 696

  
697 697
      else:
698
        vnc_arg = 'unix:%s/%s.vnc' % (vnc_bind_address, instance.name)
698
        vnc_arg = "unix:%s/%s.vnc" % (vnc_bind_address, instance.name)
699 699

  
700
      kvm_cmd.extend(['-vnc', vnc_arg])
700
      kvm_cmd.extend(["-vnc", vnc_arg])
701 701
    else:
702
      kvm_cmd.extend(['-nographic'])
702
      kvm_cmd.extend(["-nographic"])
703 703

  
704 704
    monitor_dev = ("unix:%s,server,nowait" %
705 705
                   self._InstanceMonitor(instance.name))
706
    kvm_cmd.extend(['-monitor', monitor_dev])
706
    kvm_cmd.extend(["-monitor", monitor_dev])
707 707
    if hvp[constants.HV_SERIAL_CONSOLE]:
708
      serial_dev = ('unix:%s,server,nowait' %
708
      serial_dev = ("unix:%s,server,nowait" %
709 709
                    self._InstanceSerial(instance.name))
710
      kvm_cmd.extend(['-serial', serial_dev])
710
      kvm_cmd.extend(["-serial", serial_dev])
711 711
    else:
712
      kvm_cmd.extend(['-serial', 'none'])
712
      kvm_cmd.extend(["-serial", "none"])
713 713

  
714 714
    if hvp[constants.HV_USE_LOCALTIME]:
715
      kvm_cmd.extend(['-localtime'])
715
      kvm_cmd.extend(["-localtime"])
716 716

  
717 717
    if hvp[constants.HV_KVM_USE_CHROOT]:
718
      kvm_cmd.extend(['-chroot', self._InstanceChrootDir(instance.name)])
718
      kvm_cmd.extend(["-chroot", self._InstanceChrootDir(instance.name)])
719 719

  
720 720
    # Save the current instance nics, but defer their expansion as parameters,
721 721
    # as we'll need to generate executable temp files for them.
......
868 868

  
869 869
    if incoming:
870 870
      target, port = incoming
871
      kvm_cmd.extend(['-incoming', 'tcp:%s:%s' % (target, port)])
871
      kvm_cmd.extend(["-incoming", "tcp:%s:%s" % (target, port)])
872 872

  
873 873
    # Changing the vnc password doesn't bother the guest that much. At most it
874 874
    # will surprise people who connect to it. Whether positively or negatively
......
919 919
                      data=tap)
920 920

  
921 921
    if vnc_pwd:
922
      change_cmd = 'change vnc password %s' % vnc_pwd
922
      change_cmd = "change vnc password %s" % vnc_pwd
923 923
      self._CallMonitorCommand(instance.name, change_cmd)
924 924

  
925 925
    for filename in temp_files:
......
986 986
      if force or not acpi:
987 987
        utils.KillProcess(pid)
988 988
      else:
989
        self._CallMonitorCommand(name, 'system_powerdown')
989
        self._CallMonitorCommand(name, "system_powerdown")
990 990

  
991 991
  def CleanupInstance(self, instance_name):
992 992
    """Cleanup after a stopped instance
......
1097 1097
      raise errors.HypervisorError("Instance not running, cannot migrate")
1098 1098

  
1099 1099
    if not live:
1100
      self._CallMonitorCommand(instance_name, 'stop')
1100
      self._CallMonitorCommand(instance_name, "stop")
1101 1101

  
1102
    migrate_command = ('migrate_set_speed %dm' %
1102
    migrate_command = ("migrate_set_speed %dm" %
1103 1103
        instance.hvparams[constants.HV_MIGRATION_BANDWIDTH])
1104 1104
    self._CallMonitorCommand(instance_name, migrate_command)
1105 1105

  
1106
    migrate_command = ('migrate_set_downtime %dms' %
1106
    migrate_command = ("migrate_set_downtime %dms" %
1107 1107
        instance.hvparams[constants.HV_MIGRATION_DOWNTIME])
1108 1108
    self._CallMonitorCommand(instance_name, migrate_command)
1109 1109

  
1110
    migrate_command = 'migrate -d tcp:%s:%s' % (target, port)
1110
    migrate_command = "migrate -d tcp:%s:%s" % (target, port)
1111 1111
    self._CallMonitorCommand(instance_name, migrate_command)
1112 1112

  
1113
    info_command = 'info migrate'
1113
    info_command = "info migrate"
1114 1114
    done = False
1115 1115
    broken_answers = 0
1116 1116
    while not done:
......
1126 1126
        time.sleep(self._MIGRATION_INFO_RETRY_DELAY)
1127 1127
      else:
1128 1128
        status = match.group(1)
1129
        if status == 'completed':
1129
        if status == "completed":
1130 1130
          done = True
1131
        elif status == 'active':
1131
        elif status == "active":
1132 1132
          # reset the broken answers count
1133 1133
          broken_answers = 0
1134 1134
          time.sleep(self._MIGRATION_INFO_RETRY_DELAY)
1135
        elif status == 'failed' or status == 'cancelled':
1135
        elif status == "failed" or status == "cancelled":
1136 1136
          if not live:
1137 1137
            self._CallMonitorCommand(instance_name, 'cont')
1138 1138
          raise errors.HypervisorError("Migration %s at the kvm level" %

Also available in: Unified diff