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