virt-manager/virt-convert

133 lines
4.0 KiB
Python
Executable File

#!/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)
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()
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:
print_stdout(_("Creating guest '%s'.") % guest.name)
domain = installer.start_install(guest)
cli.connect_console(guest, domain, conscb, True, False)
except Exception:
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)