summaryrefslogtreecommitdiff
path: root/virt-convert
blob: df7fa844295c27bd14c3b1560b16dcd87c1c36cc (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
#!/usr/bin/env python3
#
# Copyright 2008, 2013, 2014  Red Hat, Inc.
#
# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# This work is licensed under the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.

import sys

from virtinst import cli
from virtinst import Installer
from virtinst.cli import fail, print_stderr, print_stdout

from virtconv import VirtConverter

# Example appliances:
#
# OVF/OVA:
#   https://virtualboxes.org/tag/ova/
# VMX, but they are all multipart which is current unsupported
#   http://www.thoughtpolice.co.uk/vmware/
# Minix VMX:
#   http://download.minix3.org/iso/minix3_1_2a_vmware.zip

# Simple live test with
#  ./virt-convert --connect test:///default tests/virtconv-files/vmx_input/test-nodisks.vmx


#####################
# Argument handling #
#####################

def parse_args():
    desc = _(
"Convert an OVF or VMX appliance to native libvirt XML, and run "
"the guest.\nThe VM contents are not altered. Disk images are "
"copied to the hypervisor\ndefault storage location.\n\n"
"Examples:\n"
"  virt-convert fedora18.ova\n"
"  virt-convert centos6.zip --disk-format qcow2"
)
    parser = cli.setupParser(
        "%(prog)s inputconfig [options]", desc)

    parser.add_argument("input", metavar="inputconfig", nargs=1,
        help=_("Conversion input. Can be a ovf/vmx file, a directory "
               "containing a config and disk images, or a zip/ova/7z/etc "
               "archive."))
    cli.add_connect_option(parser)

    cong = parser.add_argument_group("Conversion Options")
    cong.add_argument("-i", "--input-format",
                    help=_("Force the input format. 'vmx' or 'ovf'"))
    cong.add_argument("-D", "--disk-format", default='raw',
                    help=_("Output disk format. default is 'raw'. "
                           "Disable conversion with 'none'"))
    cong.add_argument("--destination", default=None,
                    help=_("Destination directory the disk images should be "
                           "converted/copied to. Defaults to the default "
                           "libvirt directory."))

    misc = parser.add_argument_group("Miscellaneous Options")
    cli.add_misc_options(misc, dryrun=True, printxml=True, noautoconsole=True)

    cli.autocomplete(parser)

    options = parser.parse_args()
    options.input = options.input[0]

    return options


#######################
# Functional handlers #
#######################

def main(conn=None):
    cli.earlyLogging()
    options = parse_args()
    cli.setupLogging("virt-convert", options.debug, options.quiet)

    conn = cli.getConnection(options.connect, conn=conn)
    if options.xmlonly:
        options.dry = True
        options.quiet = True
        options.autoconsole = False

    print_cb = print_stdout
    if options.quiet:
        print_cb = None

    converter = VirtConverter(conn, options.input,
        input_name=options.input_format, print_cb=print_cb)
    try:
        converter.convert_disks(options.disk_format or "none",
            destdir=options.destination, dry=options.dry)

        guest = converter.get_guest()
        installer = Installer(guest.conn)
        installer.set_install_defaults(guest)

        conscb = None
        if options.autoconsole:
            conscb = cli.get_console_cb(guest) or None

        if options.xmlonly:
            print_stdout(installer.start_install(guest, return_xml=True)[1],
                do_force=True)
        elif not options.dry or cli.in_testsuite():
            print_stdout(_("Creating guest '%s'.") % guest.name)
            domain = installer.start_install(guest)
            cli.connect_console(guest, domain, conscb, True, False)
    except Exception:  # pragma: no cover
        converter.cleanup()
        raise

    return 0


if __name__ == "__main__":  # pragma: no cover
    try:
        sys.exit(main())
    except SystemExit as sys_e:
        sys.exit(sys_e.code)
    except KeyboardInterrupt:
        print_stderr(_("Aborted at user request"))
    except Exception as main_e:
        fail(main_e)