2018-01-27 23:46:39 +03:00
#!/usr/bin/env python3
2013-03-18 01:06:52 +04:00
#
2014-02-06 04:09:26 +04:00
# Copyright 2008, 2013, 2014 Red Hat, Inc.
2013-03-18 01:06:52 +04:00
# Joey Boggs <jboggs@redhat.com>
2014-02-06 04:09:26 +04:00
# Cole Robinson <crobinso@redhat.com>
2013-03-18 01:06:52 +04:00
#
# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
2018-04-04 16:35:41 +03:00
# This work is licensed under the GNU GPLv2 or later.
2018-03-20 22:00:02 +03:00
# See the COPYING file in the top-level directory.
2013-03-18 01:06:52 +04:00
2014-01-19 02:01:43 +04:00
import sys
2013-03-18 01:06:52 +04:00
2014-01-19 02:01:43 +04:00
from virtinst import cli
2014-02-06 04:09:26 +04:00
from virtinst.cli import fail, print_stderr, print_stdout
2013-03-18 01:06:52 +04:00
2014-02-06 04:09:26 +04:00
from virtconv import VirtConverter
# Example appliances:
#
# OVF/OVA:
2018-04-30 15:56:53 +03:00
# https://virtualboxes.org/tag/ova/
2014-02-06 04:09:26 +04:00
# 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
2013-03-18 01:06:52 +04:00
2014-09-21 02:26:37 +04:00
# Simple live test with
# ./virt-convert --connect test:///default tests/virtconv-files/vmx_input/test-nodisks.vmx
2013-04-11 19:11:21 +04:00
2014-02-06 04:09:26 +04:00
#####################
# Argument handling #
#####################
2013-04-11 19:11:21 +04:00
2013-03-18 01:06:52 +04:00
def parse_args():
2014-02-08 19:30:53 +04:00
desc = _(
2014-02-06 04:09:26 +04:00
"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"
)
2014-01-19 02:01:43 +04:00
parser = cli.setupParser(
2014-02-06 04:09:26 +04:00
"%(prog)s inputconfig [options]", desc)
2013-03-18 01:06:52 +04:00
2014-01-19 02:01:43 +04:00
parser.add_argument("input", metavar="inputconfig", nargs=1,
2014-02-06 04:09:26 +04:00
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)
2014-01-19 02:01:43 +04:00
cong = parser.add_argument_group("Conversion Options")
2014-01-21 03:04:23 +04:00
cong.add_argument("-i", "--input-format",
2014-02-06 04:09:26 +04:00
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."))
2013-03-18 01:06:52 +04:00
2014-01-19 02:01:43 +04:00
misc = parser.add_argument_group("Miscellaneous Options")
2014-02-06 04:09:26 +04:00
cli.add_misc_options(misc, dryrun=True, printxml=True, noautoconsole=True)
2013-03-18 01:06:52 +04:00
2014-02-06 04:09:26 +04:00
options = parser.parse_args()
options.input = options.input[0]
2013-03-18 01:06:52 +04:00
return options
2013-04-13 22:34:52 +04:00
2014-02-06 04:09:26 +04:00
#######################
# Functional handlers #
#######################
2013-03-18 01:06:52 +04:00
2014-02-06 04:09:26 +04:00
def main(conn=None):
2013-03-18 01:06:52 +04:00
cli.earlyLogging()
options = parse_args()
2014-02-06 04:09:26 +04:00
cli.setupLogging("virt-convert", options.debug, options.quiet)
2013-03-18 01:06:52 +04:00
2014-02-06 04:09:26 +04:00
if conn is None:
conn = cli.getConnection(options.connect)
if options.xmlonly:
options.dry = True
options.quiet = True
2015-03-23 20:37:24 +03:00
options.autoconsole = False
2013-03-18 01:06:52 +04:00
2014-06-01 00:39:57 +04:00
print_cb = print_stdout
if options.quiet:
print_cb = None
2013-03-18 01:06:52 +04:00
2014-02-06 04:09:26 +04:00
converter = VirtConverter(conn, options.input,
2014-06-01 00:39:57 +04:00
input_name=options.input_format, print_cb=print_cb)
2013-03-18 01:06:52 +04:00
try:
2014-02-06 04:09:26 +04:00
converter.convert_disks(options.disk_format or "none",
destdir=options.destination, dry=options.dry)
guest = converter.get_guest()
2014-09-21 02:20:41 +04:00
conscb = None
if options.autoconsole:
conscb = cli.get_console_cb(guest) or None
2014-02-06 04:09:26 +04:00
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)
2017-07-24 11:26:48 +03:00
except Exception:
2014-02-06 04:09:26 +04:00
converter.cleanup()
raise
2013-03-18 01:06:52 +04:00
return 0
2014-02-06 04:09:26 +04:00
2013-03-18 01:06:52 +04:00
if __name__ == "__main__":
try:
sys.exit(main())
2017-05-05 19:47:21 +03:00
except SystemExit as sys_e:
2013-03-18 01:06:52 +04:00
sys.exit(sys_e.code)
except KeyboardInterrupt:
print_stderr(_("Aborted at user request"))
2017-05-05 19:47:21 +03:00
except Exception as main_e:
2013-03-18 01:06:52 +04:00
fail(main_e)