summaryrefslogtreecommitdiff
path: root/virt-image
blob: 60e38aed6e4e6a7aefa5598741415ed607f94542 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/python2 -tt
#
# Create a virtual machine from an XML image description
#
# Copyright 2007, 2014  Red Hat, Inc.
# David Lutterkort <dlutter@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.

import argparse
import logging
import os
import sys
import time

import urlgrabber.progress as progress

import virtinst.cli as cli
from virtinst.cli import fail, print_stdout, print_stderr
from virtinst import virtimage


### Option parsing
def parse_args():
    parser = cli.setupParser(
        "%(prog)s image.xml [OPTIONS]",
        _("Create a virtual machine from a virt-image(5) image descriptor."),
        introspection_epilog=True)
    cli.add_connect_option(parser)

    parser.add_argument("image", metavar="image.xml", nargs='?',
                        help=_("virt-image(5) image descriptor"))

    geng = parser.add_argument_group(_("General Options"))
    geng.add_argument("-n", "--name", help=_("Name of the guest instance"))
    geng.add_argument("-u", "--uuid", help=argparse.SUPPRESS)
    cli.add_memory_option(geng, backcompat=True)
    cli.vcpu_cli_options(geng)
    cli.add_distro_options(geng)
    cli.add_old_feature_options(geng)

    cli.network_option_group(parser)
    cli.graphics_option_group(parser)

    misc = parser.add_argument_group(_("Miscellaneous Options"))
    misc.add_argument("--boot", type=int,
                    help=_("The zero-based index of the boot record to use"))
    misc.add_argument("--skip-checksum", action="store_true",
                    help=_("Skip disk checksum verification process"))

    cli.add_misc_options(misc, prompt=True, replace=True, printxml=True,
                         noreboot=True)

    return parser.parse_args()


def main(conn=None):
    cli.earlyLogging()
    options = parse_args()

    options.quiet = options.xmlonly or options.quiet
    cli.setupLogging("virt-image", options.debug, options.quiet)
    cli.set_prompt(options.prompt)

    cli.convert_old_features(options)
    parsermap = cli.build_parser_map(options,
        only=["memory", "vcpus", "cpu", "network", "graphics", "features"])
    if cli.check_option_introspection(options, parsermap):
        return 0

    if not options.image:
        fail(_("You need to provide an image XML descriptor"))

    if conn is None:
        conn = cli.getConnection(options.connect)

    try:
        image = virtimage.parse_file(options.image)
    except RuntimeError, msg:
        fail("%s '%s': %s" % (_("Cannot parse"),  options.image, msg))

    if options.boot is not None:
        nboots = len(image.domain.boots)
        if options.boot < 0 or options.boot >= nboots:
            fail(_("The index for --boot must be between 0 and %d") %
                 (nboots - 1))

    # Build the Installer instance
    installer = virtimage.ImageInstaller(conn, image, boot_index=options.boot)
    guest = conn.caps.build_virtinst_guest(conn, *installer.get_caps_guest())
    guest.installer = installer

    cli.convert_old_memory(options)
    cli.convert_old_networks(options, image.domain.interface)
    cli.convert_old_graphics(guest, options,
        default_override=bool(image.domain.graphics))
    cli.convert_old_cpuset(options)
    if not options.vcpus:
        options.vcpus = image.domain.vcpu or ""
    if not options.memory and image.domain.memory:
        options.memory = image.domain.memory

    guest.replace = options.replace
    cli.set_os_variant(guest, options.distro_type, options.distro_variant)

    name = options.name or image.name
    if not name:
        fail(cli.name_missing)
    guest.name = name
    if options.uuid:
        guest.uuid = options.uuid

    cli.parse_option_strings(parsermap, options, guest, None)

    guest.add_default_devices()

    msg = _("\nvirt-image is planned for removal in the near future. "
        "If you are depending on this tool, please contact the developers "
        "at virt-tools-list@redhat.com\n")
    logging.warning(msg)
    if "VIRTINST_TEST_SUITE" not in os.environ:
        if options.quiet:
            print msg
        time.sleep(3)

    # we've got everything -- try to start the install
    if options.xmlonly:
        start_xml, final_xml = guest.start_install(return_xml=True)
        print_stdout(start_xml or final_xml, do_force=True)
        return 0

    meter = progress.TextMeter(fo=sys.stdout)

    if not options.skip_checksum:
        for disk in image.storage.values():
            disk.check_disk_signature(meter=meter)

    try:
        print_stdout("\n")
        print_stdout(_("Creating guest %s...") % guest.name)

        guest.start_install(meter=meter, noboot=options.noreboot)
    except RuntimeError:
        raise
    except Exception, e:
        fail(e, do_exit=False)
        cli.install_fail(guest)

    return 0

if __name__ == "__main__":
    try:
        sys.exit(main())
    except SystemExit, sys_e:
        sys.exit(sys_e.code)
    except KeyboardInterrupt:
        print_stderr(_("Installation aborted at user request"))
    except Exception, main_e:
        fail(main_e)