~ubuntu-branches/ubuntu/quantal/virtinst/quantal-proposed

« back to all changes in this revision

Viewing changes to virt-install

  • Committer: Bazaar Package Importer
  • Author(s): Jean-Louis Dupond
  • Date: 2010-05-05 03:32:42 UTC
  • mfrom: (1.3.13 sid)
  • Revision ID: james.westby@ubuntu.com-20100505033242-um6f6pjcc89i07m0
Tags: 0.500.3-1ubuntu1
* Merge from debian unstable. (LP: #590068)  Remaining changes:
  - debian/patches/9001_Ubuntu.patch:
     + Added lucid and maverick to OS list and enable virtio for it.
  - debian/patches/0003-Fix-patch-to-keyboard-configuration.patch: disable
    as the keyboard config in Ubuntu is still in /etc/default/console-setup
    and this was causing virt-manager to always default to a en-us
    keyboard. (LP: #524318)
  - debian/control: added acl package to depends. (LP: #533048)
  - Demote virt-viewer to Suggests, as it's in universe.
  - Recommends libvirt-bin (LP: #215084)
* debian/patches/9002-add-ca-keymap.patch: dropped, its now in upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
import os, sys
25
25
import time
26
26
import errno
 
27
import re
 
28
import logging
27
29
import optparse
28
30
from optparse import OptionGroup
29
 
import logging
 
31
 
30
32
import urlgrabber.progress as progress
31
33
 
32
34
import libvirt
37
39
from virtinst.VirtualDevice import VirtualDevice
38
40
from virtinst.cli import fail
39
41
 
40
 
import gettext
41
 
import locale
42
 
 
43
 
locale.setlocale(locale.LC_ALL, '')
44
 
gettext.bindtextdomain(virtinst.gettext_app, virtinst.gettext_dir)
45
 
gettext.install(virtinst.gettext_app, virtinst.gettext_dir, unicode=1)
 
42
cli.setupGettext()
46
43
 
47
44
DEFAULT_POOL_PATH = "/var/lib/libvirt/images"
48
45
DEFAULT_POOL_NAME = "default"
147
144
            fail(_("Error in %(chartype)s device parameters: %(err)s") %
148
145
                 {"chartype": char_type, "err": str(e) })
149
146
 
 
147
def parse_watchdog(guest, optstring):
 
148
    # Peel the model type off the front
 
149
    model, ignore, optstring = partition(optstring, ",")
 
150
    opts = cli.parse_optstr(optstring)
 
151
    dev = virtinst.VirtualWatchdog(guest.conn)
 
152
 
 
153
    def set_param(paramname, dictname, val=None):
 
154
        if not val:
 
155
            if opts.has_key(dictname):
 
156
                val = opts[dictname]
 
157
                del(opts[dictname])
 
158
            else:
 
159
                return
 
160
 
 
161
        setattr(dev, paramname, val)
 
162
 
 
163
    set_param("model", "model", model)
 
164
    set_param("action", "action")
 
165
 
 
166
    # If extra parameters, then user passed some garbage param
 
167
    if opts:
 
168
        raise ValueError(_("Unknown option(s) %s") % opts.keys())
 
169
 
 
170
    return dev
 
171
 
 
172
def get_watchdog(watchdogs, guest):
 
173
    for optstr in cli.listify(watchdogs):
 
174
        try:
 
175
            dev = parse_watchdog(guest, optstr)
 
176
            guest.add_device(dev)
 
177
        except Exception, e:
 
178
            fail(_("Error in watchdog device parameters: %s") % str(e))
 
179
 
 
180
def get_security(security, guest):
 
181
    seclist = cli.listify(security)
 
182
    secopts = seclist and seclist[0] or None
 
183
    if not secopts:
 
184
        return
 
185
 
 
186
    # Parse security opts
 
187
    opts = cli.parse_optstr(secopts)
 
188
    secmodel = virtinst.Seclabel(guest.conn)
 
189
 
 
190
    def get_and_clear(dictname):
 
191
        val = None
 
192
        if opts.has_key(dictname):
 
193
            val = opts[dictname]
 
194
            del(opts[dictname])
 
195
        return val
 
196
 
 
197
    mode = get_and_clear("type")
 
198
    label = get_and_clear("label")
 
199
 
 
200
    if label:
 
201
        secmodel.label = label
 
202
        if not mode:
 
203
            mode = secmodel.SECLABEL_TYPE_STATIC
 
204
    if mode:
 
205
        secmodel.type = mode
 
206
 
 
207
    # If extra parameters, then user passed some garbage param
 
208
    if opts:
 
209
        raise ValueError(_("Unknown option(s) %s") % opts.keys())
 
210
 
 
211
    secmodel.get_xml_config()
 
212
    guest.seclabel = secmodel
150
213
 
151
214
def parse_disk_option(guest, path, size):
152
215
    """helper to properly parse --disk options"""
269
332
 
270
333
def get_disk(disk, size, sparse, guest, conn, is_file_path):
271
334
 
272
 
    # If user specified --disk, disable prompting for the whole validation
273
 
    # process, since falling back to prompting likely wouldn't provide
274
 
    # sufficient options.
275
 
    orig_prompt = cli.doprompt
276
 
    if not is_file_path:
277
 
        cli.set_prompt(False)
278
 
 
279
335
    try:
280
336
        if is_file_path:
281
337
            kwargs = { 'conn': conn, 'path': disk, 'size': size,
290
346
        fail(_("Error with storage parameters: %s" % str(e)))
291
347
 
292
348
    guest.disks.append(d)
293
 
    cli.set_prompt(orig_prompt)
294
349
 
295
350
def get_disks(file_paths, disk_paths, size, sparse, nodisks, guest, conn):
296
351
    if nodisks:
303
358
    elif not file_paths and not disk_paths and not cli.is_prompt():
304
359
        fail(_("A disk must be specified (use --nodisks to override)"))
305
360
 
306
 
    is_file_path = ((file_paths or cli.is_prompt()) or False)
 
361
    is_file_path = (file_paths or (not disk_paths and cli.is_prompt()))
307
362
    disk = (file_paths or disk_paths)
308
363
 
309
364
    # ensure we have equal length lists
417
472
                  ((req_virt_type and req_virt_type or _("default")),
418
473
                   (req_hv_type and req_hv_type or _("default"))))
419
474
 
 
475
    arch = options.arch
 
476
    if re.match("i.86", arch or ""):
 
477
        arch = "i686"
 
478
 
420
479
    try:
421
480
        (capsguest,
422
481
         capsdomain) = virtinst.CapabilitiesParser.guest_lookup(conn=conn,
423
482
                        caps=capabilities, os_type=req_virt_type,
424
 
                        arch=options.arch, type=req_hv_type,
425
 
                        accelerated=req_accel)
 
483
                        arch=arch, type=req_hv_type, accelerated=req_accel)
426
484
    except Exception, e:
427
485
        fail(e)
428
486
 
507
565
### Option parsing
508
566
def parse_args():
509
567
    usage = "%prog --name NAME --ram RAM STORAGE INSTALL [options]"
510
 
    parser = cli.VirtOptionParser(usage=usage,
511
 
                                  formatter=cli.VirtHelpFormatter(),
512
 
                                  version=virtinst.__version__)
 
568
    parser = cli.setupParser(usage)
513
569
 
514
570
    parser.add_option("", "--connect", type="string", dest="connect",
515
571
                      action="callback", callback=cli.check_before_store,
523
579
    geng.add_option("-r", "--ram", type="int", dest="memory",
524
580
                    help=_("Memory to allocate for guest instance in "
525
581
                           "megabytes"))
526
 
    geng.add_option("", "--arch", type="string", dest="arch",
527
 
                    action="callback", callback=cli.check_before_store,
528
 
                    help=_("The CPU architecture to simulate"))
529
 
    geng.add_option("-u", "--uuid", type="string", dest="uuid",
530
 
                    action="callback", callback=cli.check_before_store,
531
 
                    help=_("UUID for the guest."))
532
582
    geng.add_option("", "--vcpus", type="int", dest="vcpus",
533
583
                    help=_("Number of vcpus to configure for your guest"))
534
 
    geng.add_option("", "--check-cpu", action="store_true", dest="check_cpu",
535
 
                    help=_("Check that vcpus do not exceed physical CPUs "
536
 
                             "and warn if they do."))
537
584
    geng.add_option("", "--cpuset", type="string", dest="cpuset",
538
585
                    action="callback", callback=cli.check_before_store,
539
586
                    help=_("Set which physical CPUs Domain can use."))
540
 
    geng.add_option("", "--os-type", type="string", dest="distro_type",
541
 
                    action="callback", callback=cli.check_before_store,
542
 
                    help=_("The OS type for fully virtualized guests, e.g. "
543
 
                           "'linux', 'unix', 'windows'"))
544
 
    geng.add_option("", "--os-variant", type="string", dest="distro_variant",
545
 
                      action="callback", callback=cli.check_before_store,
546
 
                      help=_("The OS variant for fully virtualized guests, "
547
 
                             "e.g. 'fedora6', 'rhel5', 'solaris10', 'win2k'"))
548
 
    geng.add_option("", "--serial", type="string", dest="serials",
549
 
                    action="callback", callback=cli.check_before_append,
550
 
                    help=_("Add a serial device to the domain."))
551
 
    geng.add_option("", "--parallel", type="string", dest="parallels",
552
 
                    action="callback", callback=cli.check_before_append,
553
 
                    help=_("Add a parallel device to the domain."))
554
 
    geng.add_option("", "--host-device", type="string", dest="hostdevs",
555
 
                    action="callback", callback=cli.check_before_append,
556
 
                    help=_("Physical host device to attach to the domain."))
 
587
    geng.add_option("", "--description", type="string", dest="description",
 
588
                    action="callback", callback=cli.check_before_store,
 
589
                    help=_("Human readable description of the VM to store in "
 
590
                           "the generated XML."))
 
591
    geng.add_option("", "--security", type="string", dest="security",
 
592
                    action="callback", callback=cli.check_before_store,
 
593
                    help=_("Set domain security driver configuration."))
557
594
    parser.add_option_group(geng)
558
595
 
559
 
    fulg = OptionGroup(parser, _("Full Virtualization specific options"))
560
 
    fulg.add_option("", "--sound", action="store_true", dest="sound",
561
 
                    default=False, help=_("Use sound device emulation"))
562
 
    fulg.add_option("", "--noapic", action="store_true", dest="noapic",
563
 
                    default=False,
564
 
                    help=_("Disables APIC for fully virtualized guest "
565
 
                           "(overrides value in os-type/os-variant db)"))
566
 
    fulg.add_option("", "--noacpi", action="store_true", dest="noacpi",
567
 
                    default=False,
568
 
                    help=_("Disables ACPI for fully virtualized guest "
569
 
                           "(overrides value in os-type/os-variant db)"))
570
 
    parser.add_option_group(fulg)
571
 
 
572
 
    virg = OptionGroup(parser, _("Virtualization Type Options"))
573
 
    virg.add_option("-v", "--hvm", action="store_true", dest="fullvirt",
574
 
                      help=_("This guest should be a fully virtualized guest"))
575
 
    virg.add_option("-p", "--paravirt", action="store_true", dest="paravirt",
576
 
                    help=_("This guest should be a paravirtualized guest"))
577
 
    virg.add_option("", "--virt-type", type="string", dest="hv_type",
578
 
                    default="",
579
 
                    help=_("Hypervisor name to use (kvm, qemu, xen, ...)"))
580
 
    virg.add_option("", "--accelerate", action="store_true",
581
 
                    dest="accelerate", default=False,
582
 
                    help=optparse.SUPPRESS_HELP)
583
 
    parser.add_option_group(virg)
584
 
 
585
596
    insg = OptionGroup(parser, _("Installation Method Options"))
586
597
    insg.add_option("-c", "--cdrom", type="string", dest="cdrom",
587
598
                    action="callback", callback=cli.check_before_store,
600
611
                    default="",
601
612
                    help=_("Additional arguments to pass to the kernel "
602
613
                           "booted from --location"))
 
614
    insg.add_option("", "--os-type", type="string", dest="distro_type",
 
615
                    action="callback", callback=cli.check_before_store,
 
616
                    help=_("The OS type being installed, e.g. "
 
617
                           "'linux', 'unix', 'windows'"))
 
618
    insg.add_option("", "--os-variant", type="string", dest="distro_variant",
 
619
                      action="callback", callback=cli.check_before_store,
 
620
                      help=_("The OS variant being installed guests, "
 
621
                             "e.g. 'fedora6', 'rhel5', 'solaris10', 'win2k'"))
603
622
    parser.add_option_group(insg)
604
623
 
605
624
    stog = OptionGroup(parser, _("Storage Configuration"))
644
663
    parser.add_option_group(netg)
645
664
 
646
665
    vncg = cli.graphics_option_group(parser)
647
 
    vncg.add_option("", "--video", dest="video", type="string",
648
 
                    action="callback", callback=cli.check_before_append,
649
 
                    help=_("Specify video hardware type."))
650
666
    vncg.add_option("", "--noautoconsole", action="store_false",
651
667
                    dest="autoconsole",
652
668
                    help=_("Don't automatically try to connect to the guest "
653
669
                           "console"))
654
670
    parser.add_option_group(vncg)
655
671
 
 
672
    devg = OptionGroup(parser, _("Device Options"))
 
673
    devg.add_option("", "--serial", type="string", dest="serials",
 
674
                    action="callback", callback=cli.check_before_append,
 
675
                    help=_("Add a serial device to the domain."))
 
676
    devg.add_option("", "--parallel", type="string", dest="parallels",
 
677
                    action="callback", callback=cli.check_before_append,
 
678
                    help=_("Add a parallel device to the domain."))
 
679
    devg.add_option("", "--host-device", type="string", dest="hostdevs",
 
680
                    action="callback", callback=cli.check_before_append,
 
681
                    help=_("Physical host device to attach to the domain."))
 
682
    devg.add_option("", "--soundhw", type='string', action="callback",
 
683
                    callback=cli.check_before_append, dest="soundhw",
 
684
                    help=_("Use sound device emulation"))
 
685
    devg.add_option("", "--watchdog", type="string", dest="watchdog",
 
686
                    action="callback", callback=cli.check_before_append,
 
687
                    help=_("Add a watchdog device to the domain."))
 
688
    devg.add_option("", "--video", dest="video", type="string",
 
689
                    action="callback", callback=cli.check_before_append,
 
690
                    help=_("Specify video hardware type."))
 
691
 
 
692
    # Deprecated
 
693
    devg.add_option("", "--sound", action="store_true", dest="sound",
 
694
                    default=False, help=optparse.SUPPRESS_HELP)
 
695
    parser.add_option_group(devg)
 
696
 
 
697
    virg = OptionGroup(parser, _("Virtualization Platform Options"))
 
698
    virg.add_option("-v", "--hvm", action="store_true", dest="fullvirt",
 
699
                      help=_("This guest should be a fully virtualized guest"))
 
700
    virg.add_option("-p", "--paravirt", action="store_true", dest="paravirt",
 
701
                    help=_("This guest should be a paravirtualized guest"))
 
702
    virg.add_option("", "--virt-type", type="string", dest="hv_type",
 
703
                    default="",
 
704
                    help=_("Hypervisor name to use (kvm, qemu, xen, ...)"))
 
705
    virg.add_option("", "--accelerate", action="store_true",
 
706
                    dest="accelerate", default=False,
 
707
                    help=optparse.SUPPRESS_HELP)
 
708
    virg.add_option("", "--arch", type="string", dest="arch",
 
709
                    action="callback", callback=cli.check_before_store,
 
710
                    help=_("The CPU architecture to simulate"))
 
711
    virg.add_option("", "--noapic", action="store_true", dest="noapic",
 
712
                    default=False,
 
713
                    help=_("Disables APIC for fully virtualized guest "
 
714
                           "(overrides value in os-type/os-variant db)"))
 
715
    virg.add_option("", "--noacpi", action="store_true", dest="noacpi",
 
716
                    default=False,
 
717
                    help=_("Disables ACPI for fully virtualized guest "
 
718
                           "(overrides value in os-type/os-variant db)"))
 
719
    virg.add_option("-u", "--uuid", type="string", dest="uuid",
 
720
                    action="callback", callback=cli.check_before_store,
 
721
                    help=_("UUID for the guest."))
 
722
    parser.add_option_group(virg)
 
723
 
656
724
    misc = OptionGroup(parser, _("Miscellaneous Options"))
657
 
    misc.add_option("-d", "--debug", action="store_true", dest="debug",
658
 
                    help=_("Print debugging information"))
 
725
    misc.add_option("", "--autostart", action="store_true", default=False,
 
726
                    dest="autostart",
 
727
                    help=_("Have domain autostart on host boot up."))
659
728
    misc.add_option("", "--noreboot", action="store_true", dest="noreboot",
660
729
                    help=_("Disables the automatic rebooting when the "
661
730
                           "installation is complete."))
668
737
    misc.add_option("", "--prompt", action="store_true", dest="prompt",
669
738
                    help=_("Request user input for ambiguous situations or "
670
739
                           "required options."), default=False)
 
740
    misc.add_option("", "--check-cpu", action="store_true", dest="check_cpu",
 
741
                    help=_("Check that vcpus do not exceed physical CPUs "
 
742
                             "and warn if they do."))
 
743
    misc.add_option("-d", "--debug", action="store_true", dest="debug",
 
744
                    help=_("Print debugging information"))
671
745
    parser.add_option_group(misc)
672
746
 
673
747
    (options, dummy) = parser.parse_args()
753
827
    cli.get_uuid(options.uuid, guest)
754
828
    cli.get_vcpus(options.vcpus, options.check_cpu, guest, conn)
755
829
    cli.get_cpuset(options.cpuset, guest.memory, guest, conn)
756
 
    if ishvm:
757
 
        cli.get_sound(options.sound, guest)
758
 
        get_chardevs(VirtualDevice.VIRTUAL_DEV_SERIAL, options.serials, guest)
759
 
        get_chardevs(VirtualDevice.VIRTUAL_DEV_PARALLEL, options.parallels,
760
 
                     guest)
761
 
 
762
 
    cli.get_video(options.video, guest)
 
830
    get_security(options.security, guest)
 
831
 
 
832
    get_watchdog(options.watchdog, guest)
 
833
    cli.get_sound(options.sound, options.soundhw, guest)
 
834
    get_chardevs(VirtualDevice.VIRTUAL_DEV_SERIAL, options.serials, guest)
 
835
    get_chardevs(VirtualDevice.VIRTUAL_DEV_PARALLEL, options.parallels, guest)
 
836
 
 
837
    guest.autostart = options.autostart
 
838
    guest.description = options.description
763
839
 
764
840
    # set up disks
765
841
    get_disks(options.file_path, options.diskopts, options.disksize,
769
845
    get_networks(options.mac, options.bridge, options.network,
770
846
                 options.nonetworks, guest)
771
847
 
772
 
    # set up graphics information
 
848
    # set up graphics and video device information
773
849
    cli.get_graphics(options.vnc, options.vncport, options.vnclisten,
774
 
                     options.nographics, options.sdl, options.keymap, guest)
 
850
                     options.nographics, options.sdl, options.keymap,
 
851
                     options.video, guest)
775
852
 
776
853
    # Set host device info
777
854
    cli.get_hostdevs(options.hostdevs, guest)
784
861
                      options.livecd, options.import_install, guest, ishvm)
785
862
 
786
863
    continue_inst = guest.get_continue_inst()
787
 
    if ishvm:
788
 
        if options.noacpi:
789
 
            guest.features["acpi"] = False
790
 
        if options.noapic:
791
 
            guest.features["apic"] = False
 
864
    if options.noacpi:
 
865
        guest.features["acpi"] = False
 
866
    if options.noapic:
 
867
        guest.features["apic"] = False
792
868
 
793
869
    def show_console(dom):
794
870
        if guest.graphics_dev:
868
944
        sys.exit(e.code)
869
945
    except Exception, e:
870
946
        logging.error(e)
871
 
        print (_("Domain installation may not have been\n successful.  If it "
872
 
                 "was, you can restart your domain\n by running 'virsh start "
873
 
                 "%s'; otherwise, please\n restart your installation.") %
874
 
                 guest.name)
 
947
        print _("Domain installation does not appear to have been\n "
 
948
                "successful.  If it was, you can restart your domain\n "
 
949
                "by running 'virsh start %s'; otherwise, please\n "
 
950
                "restart your installation.") % guest.name
875
951
        raise
876
952
 
877
 
def domain_is_shutdown(dom):
878
 
    info     = dom.info()
879
 
    state    = info[0]
880
 
    cpu_time = info[4]
881
 
 
882
 
    if state == libvirt.VIR_DOMAIN_SHUTOFF:
883
 
        return True
884
 
 
885
 
    # If --wait was specified, the dom object we have was looked up
886
 
    # before initially shutting down, which seems to bogus up the
887
 
    # info data (all 0's). So, if it is bogus, assume the domain is
888
 
    # shutdown. We will catch the error later.
889
 
    return state == libvirt.VIR_DOMAIN_NOSTATE and cpu_time == 0
890
953
 
891
954
def do_install(guest, conscb, progresscb, wait, wait_time, start_time,
892
955
               install_func):
893
956
 
894
957
    dom = install_func(conscb, progresscb, wait=(not wait))
895
958
 
896
 
    if dom is None:
897
 
        print _("Guest installation failed.")
898
 
        sys.exit(0)
899
 
 
900
 
    if domain_is_shutdown(dom):
901
 
        return dom
 
959
    # Wait a bit so info is accurate
 
960
    def check_domain_state():
 
961
        dominfo = dom.info()
 
962
        state = dominfo[0]
 
963
 
 
964
        if guest.domain_is_crashed():
 
965
            print _("Domain has crashed.")
 
966
            sys.exit(1)
 
967
 
 
968
        if guest.domain_is_shutdown():
 
969
            return dom, state
 
970
 
 
971
        return None, state
 
972
 
 
973
    do_sleep = bool(conscb)
 
974
    try:
 
975
        ret, state = check_domain_state()
 
976
        if ret:
 
977
            return ret
 
978
    except Exception, e:
 
979
        # Sometimes we see errors from libvirt here due to races
 
980
        logging.exception(e)
 
981
        do_sleep = True
 
982
 
 
983
    if do_sleep:
 
984
        # Sleep a bit and try again to be sure the HV has caught up
 
985
        time.sleep(2)
 
986
 
 
987
    ret, state = check_domain_state()
 
988
    if ret:
 
989
        return ret
 
990
 
 
991
    logging.debug("Domain state after install: %s" % state)
902
992
 
903
993
    # Domain seems to be running
904
994
    if wait and wait_time != 0:
911
1001
 
912
1002
        # Wait loop
913
1003
        while True:
914
 
            if domain_is_shutdown(dom):
 
1004
            if guest.domain_is_shutdown():
915
1005
                print _("Domain has shutdown. Continuing.")
916
1006
                try:
917
1007
                    # Lookup a new domain object incase current