2014-02-12 17:21:38 +08:00
|
|
|
#!/usr/bin/python2 -tt
|
2013-03-18 05:06:52 +08:00
|
|
|
#
|
|
|
|
# Create a virtual machine from an XML image description
|
|
|
|
#
|
2014-02-12 17:21:38 +08:00
|
|
|
# Copyright 2007, 2014 Red Hat, Inc.
|
2013-03-18 05:06:52 +08:00
|
|
|
# David Lutterkort <dlutter@redhat.com>
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2014-01-21 07:04:23 +08:00
|
|
|
import argparse
|
2014-02-11 04:24:12 +08:00
|
|
|
import logging
|
|
|
|
import os
|
2013-03-18 05:06:52 +08:00
|
|
|
import sys
|
2014-02-11 04:24:12 +08:00
|
|
|
import time
|
2013-09-28 04:52:41 +08:00
|
|
|
|
2013-03-18 05:06:52 +08:00
|
|
|
import urlgrabber.progress as progress
|
|
|
|
|
|
|
|
import virtinst.cli as cli
|
|
|
|
from virtinst.cli import fail, print_stdout, print_stderr
|
2013-08-09 09:00:37 +08:00
|
|
|
from virtinst import virtimage
|
2013-03-18 05:06:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
### Option parsing
|
|
|
|
def parse_args():
|
2013-07-01 03:03:53 +08:00
|
|
|
parser = cli.setupParser(
|
2014-01-19 06:01:43 +08:00
|
|
|
"%(prog)s image.xml [OPTIONS]",
|
2014-01-22 22:06:35 +08:00
|
|
|
_("Create a virtual machine from a virt-image(5) image descriptor."),
|
|
|
|
introspection_epilog=True)
|
2013-03-18 05:06:52 +08:00
|
|
|
cli.add_connect_option(parser)
|
|
|
|
|
2014-01-19 06:01:43 +08:00
|
|
|
parser.add_argument("image", metavar="image.xml", nargs='?',
|
|
|
|
help=_("virt-image(5) image descriptor"))
|
|
|
|
|
|
|
|
geng = parser.add_argument_group(_("General Options"))
|
2014-01-21 07:04:23 +08:00
|
|
|
geng.add_argument("-n", "--name", help=_("Name of the guest instance"))
|
|
|
|
geng.add_argument("-u", "--uuid", help=argparse.SUPPRESS)
|
2014-01-25 07:56:59 +08:00
|
|
|
cli.add_memory_option(geng, backcompat=True)
|
2013-03-18 05:06:52 +08:00
|
|
|
cli.vcpu_cli_options(geng)
|
2013-08-11 06:17:37 +08:00
|
|
|
cli.add_distro_options(geng)
|
2013-09-28 21:36:11 +08:00
|
|
|
cli.add_old_feature_options(geng)
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2014-01-19 06:01:43 +08:00
|
|
|
cli.network_option_group(parser)
|
|
|
|
cli.graphics_option_group(parser)
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2014-01-19 06:01:43 +08:00
|
|
|
misc = parser.add_argument_group(_("Miscellaneous Options"))
|
2014-01-21 07:04:23 +08:00
|
|
|
misc.add_argument("--boot", type=int,
|
2013-03-18 05:06:52 +08:00
|
|
|
help=_("The zero-based index of the boot record to use"))
|
2014-01-19 06:01:43 +08:00
|
|
|
misc.add_argument("--skip-checksum", action="store_true",
|
2013-03-18 05:06:52 +08:00
|
|
|
help=_("Skip disk checksum verification process"))
|
2013-09-28 23:27:26 +08:00
|
|
|
|
|
|
|
cli.add_misc_options(misc, prompt=True, replace=True, printxml=True,
|
|
|
|
noreboot=True)
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2014-01-22 22:06:35 +08:00
|
|
|
return parser.parse_args()
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2013-03-18 05:06:52 +08:00
|
|
|
def main(conn=None):
|
|
|
|
cli.earlyLogging()
|
|
|
|
options = parse_args()
|
|
|
|
|
2013-09-28 23:27:26 +08:00
|
|
|
options.quiet = options.xmlonly or options.quiet
|
2013-03-18 05:06:52 +08:00
|
|
|
cli.setupLogging("virt-image", options.debug, options.quiet)
|
2014-02-05 05:16:39 +08:00
|
|
|
cli.set_prompt(options.prompt)
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2014-01-22 22:06:35 +08:00
|
|
|
cli.convert_old_features(options)
|
|
|
|
parsermap = cli.build_parser_map(options,
|
2014-01-25 07:56:59 +08:00
|
|
|
only=["memory", "vcpus", "cpu", "network", "graphics", "features"])
|
2014-01-22 22:06:35 +08:00
|
|
|
if cli.check_option_introspection(options, parsermap):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
if not options.image:
|
|
|
|
fail(_("You need to provide an image XML descriptor"))
|
|
|
|
|
2013-03-18 05:06:52 +08:00
|
|
|
if conn is None:
|
|
|
|
conn = cli.getConnection(options.connect)
|
|
|
|
|
|
|
|
try:
|
2013-08-09 09:00:37 +08:00
|
|
|
image = virtimage.parse_file(options.image)
|
2013-07-07 02:12:13 +08:00
|
|
|
except RuntimeError, msg:
|
2013-04-14 02:34:52 +08:00
|
|
|
fail("%s '%s': %s" % (_("Cannot parse"), options.image, msg))
|
2013-03-18 05:06:52 +08:00
|
|
|
|
|
|
|
if options.boot is not None:
|
|
|
|
nboots = len(image.domain.boots)
|
|
|
|
if options.boot < 0 or options.boot >= nboots:
|
|
|
|
fail(_("The index for --boot must be between 0 and %d") %
|
|
|
|
(nboots - 1))
|
|
|
|
|
|
|
|
# Build the Installer instance
|
2013-08-09 09:00:37 +08:00
|
|
|
installer = virtimage.ImageInstaller(conn, image, boot_index=options.boot)
|
2013-07-17 19:53:47 +08:00
|
|
|
guest = conn.caps.build_virtinst_guest(conn, *installer.get_caps_guest())
|
|
|
|
guest.installer = installer
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2014-01-25 07:56:59 +08:00
|
|
|
cli.convert_old_memory(options)
|
2014-02-06 22:33:00 +08:00
|
|
|
cli.convert_old_networks(options, image.domain.interface)
|
2013-09-28 08:16:35 +08:00
|
|
|
cli.convert_old_graphics(guest, options,
|
|
|
|
default_override=bool(image.domain.graphics))
|
2014-01-22 04:24:46 +08:00
|
|
|
cli.convert_old_cpuset(options)
|
2014-01-22 03:28:47 +08:00
|
|
|
if not options.vcpus:
|
|
|
|
options.vcpus = image.domain.vcpu or ""
|
2014-01-25 07:56:59 +08:00
|
|
|
if not options.memory and image.domain.memory:
|
|
|
|
options.memory = image.domain.memory
|
2013-09-28 08:16:35 +08:00
|
|
|
|
2013-03-18 05:06:52 +08:00
|
|
|
guest.replace = options.replace
|
2014-01-22 04:24:46 +08:00
|
|
|
cli.set_os_variant(guest, options.distro_type, options.distro_variant)
|
2014-01-22 03:28:47 +08:00
|
|
|
|
2014-02-05 05:16:39 +08:00
|
|
|
name = options.name or image.name
|
|
|
|
if not name:
|
|
|
|
fail(cli.name_missing)
|
|
|
|
guest.name = name
|
2014-01-21 07:15:08 +08:00
|
|
|
if options.uuid:
|
|
|
|
guest.uuid = options.uuid
|
2014-01-22 03:28:47 +08:00
|
|
|
|
|
|
|
cli.parse_option_strings(parsermap, options, guest, None)
|
|
|
|
|
2014-02-08 05:07:32 +08:00
|
|
|
guest.add_default_devices()
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2014-02-11 04:24:12 +08:00
|
|
|
msg = _("\nvirt-image is planned for removal in the near future. "
|
|
|
|
"If you are depending on this tool, please contact the developers "
|
|
|
|
"at virt-tools-list@redhat.com\n")
|
|
|
|
logging.warning(msg)
|
|
|
|
if "VIRTINST_TEST_SUITE" not in os.environ:
|
|
|
|
if options.quiet:
|
|
|
|
print msg
|
|
|
|
time.sleep(3)
|
|
|
|
|
2013-03-18 05:06:52 +08:00
|
|
|
# we've got everything -- try to start the install
|
2013-09-28 23:27:26 +08:00
|
|
|
if options.xmlonly:
|
2013-03-18 05:06:52 +08:00
|
|
|
start_xml, final_xml = guest.start_install(return_xml=True)
|
|
|
|
print_stdout(start_xml or final_xml, do_force=True)
|
|
|
|
return 0
|
|
|
|
|
|
|
|
meter = progress.TextMeter(fo=sys.stdout)
|
|
|
|
|
2014-01-21 07:04:23 +08:00
|
|
|
if not options.skip_checksum:
|
2013-03-18 05:06:52 +08:00
|
|
|
for disk in image.storage.values():
|
|
|
|
disk.check_disk_signature(meter=meter)
|
|
|
|
|
|
|
|
try:
|
|
|
|
print_stdout("\n")
|
|
|
|
print_stdout(_("Creating guest %s...") % guest.name)
|
|
|
|
|
2013-08-09 23:25:05 +08:00
|
|
|
guest.start_install(meter=meter, noboot=options.noreboot)
|
2013-03-18 05:06:52 +08:00
|
|
|
except RuntimeError:
|
|
|
|
raise
|
|
|
|
except Exception, e:
|
|
|
|
fail(e, do_exit=False)
|
|
|
|
cli.install_fail(guest)
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
try:
|
|
|
|
sys.exit(main())
|
|
|
|
except SystemExit, sys_e:
|
|
|
|
sys.exit(sys_e.code)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print_stderr(_("Installation aborted at user request"))
|
|
|
|
except Exception, main_e:
|
|
|
|
fail(main_e)
|