summaryrefslogtreecommitdiff
path: root/virt-convert
blob: 9a44d5f553ff4a91d374c1f117d8cce444a0601d (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
#!/usr/bin/env python2
#
# Copyright 2008, 2013, 2014  Red Hat, Inc.
# Joey Boggs <jboggs@redhat.com>
# Cole Robinson <crobinso@redhat.com>
#
# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# 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 sys

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

from virtconv import VirtConverter

# Example appliances:
#
# OVF/OVA:
#   http://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)

    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)

    if conn is None:
        conn = cli.getConnection(options.connect)
    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()

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

        if options.xmlonly:
            print_stdout(guest.start_install(return_xml=True)[1],
                do_force=True)
        elif not options.dry:
            print_stdout(_("Creating guest '%s'.") % guest.name)
            guest.start_install()
            cli.connect_console(guest, conscb, True)
    except:
        converter.cleanup()
        raise

    return 0


if __name__ == "__main__":
    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)