2013-10-28 04:59:46 +08:00
|
|
|
# Copyright (C) 2013 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
|
|
|
#
|
|
|
|
|
|
|
|
import os
|
2014-02-06 08:09:26 +08:00
|
|
|
import re
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import tempfile
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2019-06-17 09:12:39 +08:00
|
|
|
from virtinst import log
|
2014-02-06 08:09:26 +08:00
|
|
|
from virtinst import StoragePool
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2013-03-18 06:18:22 +08:00
|
|
|
|
2014-02-06 08:09:26 +08:00
|
|
|
class parser_class(object):
|
2013-03-18 05:06:52 +08:00
|
|
|
"""
|
|
|
|
Base class for particular config file format definitions of
|
|
|
|
a VM instance.
|
|
|
|
|
|
|
|
Warning: this interface is not (yet) considered stable and may
|
|
|
|
change at will.
|
|
|
|
"""
|
|
|
|
@staticmethod
|
|
|
|
def identify_file(input_file):
|
|
|
|
"""
|
|
|
|
Return True if the given file is of this format.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@staticmethod
|
2014-02-06 08:09:26 +08:00
|
|
|
def export_libvirt(conn, input_file):
|
2013-03-18 05:06:52 +08:00
|
|
|
"""
|
2014-02-06 08:09:26 +08:00
|
|
|
Import a configuration file and turn it into a libvirt Guest object
|
2013-03-18 05:06:52 +08:00
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
2014-09-13 03:59:22 +08:00
|
|
|
def _get_parsers():
|
|
|
|
from .vmx import vmx_parser
|
|
|
|
from .ovf import ovf_parser
|
|
|
|
return [vmx_parser, ovf_parser]
|
2014-02-06 08:09:26 +08:00
|
|
|
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2014-02-06 08:09:26 +08:00
|
|
|
def _is_test():
|
|
|
|
return bool(os.getenv("VIRTINST_TEST_SUITE"))
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2014-02-06 08:09:26 +08:00
|
|
|
def _find_parser_by_name(input_name):
|
2013-03-18 05:06:52 +08:00
|
|
|
"""
|
|
|
|
Return the parser of the given name.
|
|
|
|
"""
|
2014-09-13 03:59:22 +08:00
|
|
|
parsers = [p for p in _get_parsers() if p.name == input_name]
|
2013-03-18 05:06:52 +08:00
|
|
|
if len(parsers):
|
|
|
|
return parsers[0]
|
2014-02-06 08:09:26 +08:00
|
|
|
raise RuntimeError(_("No parser found for type '%s'") % input_name)
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2014-02-06 08:09:26 +08:00
|
|
|
def _find_parser_by_file(input_file):
|
2013-03-18 05:06:52 +08:00
|
|
|
"""
|
|
|
|
Return the parser that is capable of comprehending the given file.
|
|
|
|
"""
|
2014-09-13 03:59:22 +08:00
|
|
|
for p in _get_parsers():
|
2013-03-18 05:06:52 +08:00
|
|
|
if p.identify_file(input_file):
|
|
|
|
return p
|
2014-02-06 08:09:26 +08:00
|
|
|
raise RuntimeError(_("Don't know how to parse file %s") % input_file)
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2014-02-06 08:09:26 +08:00
|
|
|
def _run_cmd(cmd):
|
2013-03-18 05:06:52 +08:00
|
|
|
"""
|
2014-02-06 08:09:26 +08:00
|
|
|
Return the exit status and output to stdout and stderr.
|
2013-03-18 05:06:52 +08:00
|
|
|
"""
|
2019-06-17 09:12:39 +08:00
|
|
|
log.debug("Running command: %s", " ".join(cmd))
|
2014-02-06 08:09:26 +08:00
|
|
|
proc = subprocess.Popen(cmd, stderr=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
close_fds=True)
|
|
|
|
stdout, stderr = proc.communicate()
|
|
|
|
ret = proc.wait()
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2019-06-17 09:12:39 +08:00
|
|
|
log.debug("stdout=%s", stdout)
|
|
|
|
log.debug("stderr=%s", stderr)
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2014-02-06 08:09:26 +08:00
|
|
|
if ret == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
out = stdout
|
|
|
|
if stderr:
|
|
|
|
if out:
|
|
|
|
out += "\n"
|
|
|
|
out += stderr
|
|
|
|
raise RuntimeError("%s: failed with exit status %d: %s" %
|
|
|
|
(" ".join(cmd), ret, out))
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2014-02-06 08:09:26 +08:00
|
|
|
def _find_input(input_file, parser, print_cb):
|
2013-03-18 05:06:52 +08:00
|
|
|
"""
|
2014-02-06 08:09:26 +08:00
|
|
|
Given the input file, determine if its a directory, archive, etc
|
2013-03-18 05:06:52 +08:00
|
|
|
"""
|
2014-02-06 08:09:26 +08:00
|
|
|
force_clean = []
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2014-02-06 08:09:26 +08:00
|
|
|
try:
|
|
|
|
ext = os.path.splitext(input_file)[1]
|
|
|
|
tempdir = None
|
2016-01-06 12:22:57 +08:00
|
|
|
binname = None
|
|
|
|
pkg = None
|
2014-02-06 08:09:26 +08:00
|
|
|
if ext and ext[1:] in ["zip", "gz", "ova",
|
|
|
|
"tar", "bz2", "bzip2", "7z", "xz"]:
|
|
|
|
basedir = "/var/tmp"
|
|
|
|
if _is_test():
|
|
|
|
tempdir = os.path.join(basedir, "virt-convert-tmp")
|
|
|
|
else:
|
|
|
|
tempdir = tempfile.mkdtemp(
|
|
|
|
prefix="virt-convert-tmp", dir=basedir)
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2014-02-06 08:09:26 +08:00
|
|
|
base = os.path.basename(input_file)
|
2014-03-05 10:31:59 +08:00
|
|
|
|
2016-01-06 12:22:57 +08:00
|
|
|
if (ext[1:] == "zip"):
|
|
|
|
binname = "unzip"
|
|
|
|
pkg = "unzip"
|
|
|
|
cmd = ["unzip", "-o", "-d", tempdir, input_file]
|
|
|
|
elif (ext[1:] == "7z"):
|
|
|
|
binname = "7z"
|
|
|
|
pkg = "p7zip"
|
|
|
|
cmd = ["7z", "-o" + tempdir, "e", input_file]
|
|
|
|
elif (ext[1:] == "ova" or ext[1:] == "tar"):
|
|
|
|
binname = "tar"
|
|
|
|
pkg = "tar"
|
|
|
|
cmd = ["tar", "xf", input_file, "-C", tempdir]
|
|
|
|
elif (ext[1:] == "gz"):
|
|
|
|
binname = "gzip"
|
|
|
|
pkg = "gzip"
|
|
|
|
cmd = ["tar", "zxf", input_file, "-C", tempdir]
|
|
|
|
elif (ext[1:] == "bz2" or ext[1:] == "bzip2"):
|
|
|
|
binname = "bzip2"
|
|
|
|
pkg = "bzip2"
|
|
|
|
cmd = ["tar", "jxf", input_file, "-C", tempdir]
|
|
|
|
elif (ext[1:] == "xz"):
|
|
|
|
binname = "xz"
|
|
|
|
pkg = "xz"
|
|
|
|
cmd = ["tar", "Jxf", input_file, "-C", tempdir]
|
2019-06-06 02:15:23 +08:00
|
|
|
if not shutil.which(binname):
|
2014-03-05 10:31:59 +08:00
|
|
|
raise RuntimeError(_("%s appears to be an archive, "
|
2016-01-06 12:22:57 +08:00
|
|
|
"but '%s' is not installed. "
|
|
|
|
"Please either install '%s', or extract the archive "
|
2014-03-05 10:31:59 +08:00
|
|
|
"yourself and point virt-convert at "
|
2016-01-06 12:22:57 +08:00
|
|
|
"the extracted directory.") % (base, pkg, pkg))
|
2014-03-05 10:31:59 +08:00
|
|
|
|
2014-02-06 08:09:26 +08:00
|
|
|
print_cb(_("%s appears to be an archive, running: %s") %
|
|
|
|
(base, " ".join(cmd)))
|
|
|
|
|
|
|
|
_run_cmd(cmd)
|
|
|
|
force_clean.append(tempdir)
|
|
|
|
input_file = tempdir
|
|
|
|
|
|
|
|
if not os.path.isdir(input_file):
|
|
|
|
if not parser:
|
|
|
|
parser = _find_parser_by_file(input_file)
|
|
|
|
return input_file, parser, force_clean
|
|
|
|
|
2014-09-13 03:59:22 +08:00
|
|
|
parsers = parser and [parser] or _get_parsers()
|
2014-02-06 08:09:26 +08:00
|
|
|
for root, ignore, files in os.walk(input_file):
|
|
|
|
for p in parsers:
|
|
|
|
for f in [f for f in files if f.endswith(p.suffix)]:
|
|
|
|
path = os.path.join(root, f)
|
|
|
|
if p.identify_file(path):
|
|
|
|
return path, p, force_clean
|
|
|
|
|
|
|
|
raise RuntimeError("Could not find parser for file %s" % input_file)
|
2017-07-24 16:26:48 +08:00
|
|
|
except Exception:
|
2014-02-06 08:09:26 +08:00
|
|
|
for f in force_clean:
|
|
|
|
shutil.rmtree(f)
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
class VirtConverter(object):
|
2013-03-18 05:06:52 +08:00
|
|
|
"""
|
2014-02-06 08:09:26 +08:00
|
|
|
Public interface for actually performing the conversion
|
2013-03-18 05:06:52 +08:00
|
|
|
"""
|
2014-06-01 04:39:57 +08:00
|
|
|
def __init__(self, conn, input_file, print_cb=-1, input_name=None):
|
2014-02-06 08:09:26 +08:00
|
|
|
self.conn = conn
|
|
|
|
self._err_clean = []
|
|
|
|
self._force_clean = []
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2016-04-19 04:42:12 +08:00
|
|
|
# pylint: disable=redefined-variable-type
|
2014-06-01 05:37:19 +08:00
|
|
|
if print_cb == -1 or print_cb is None:
|
2014-02-06 08:09:26 +08:00
|
|
|
def cb(msg):
|
2014-06-01 05:37:19 +08:00
|
|
|
if print_cb == -1:
|
2017-05-06 02:16:59 +08:00
|
|
|
print(msg)
|
2014-06-01 05:37:19 +08:00
|
|
|
self.print_cb = cb
|
|
|
|
else:
|
|
|
|
self.print_cb = print_cb
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2014-02-06 08:09:26 +08:00
|
|
|
parser = None
|
|
|
|
if input_name:
|
|
|
|
parser = _find_parser_by_name(input_name)
|
|
|
|
|
2014-09-21 06:26:37 +08:00
|
|
|
input_file = os.path.abspath(input_file)
|
2019-06-17 09:12:39 +08:00
|
|
|
log.debug("converter __init__ with input=%s parser=%s",
|
2014-02-06 08:09:26 +08:00
|
|
|
input_file, parser)
|
|
|
|
|
|
|
|
(self._input_file,
|
|
|
|
self.parser,
|
|
|
|
self._force_clean) = _find_input(input_file, parser, self.print_cb)
|
2014-06-01 04:05:29 +08:00
|
|
|
self._top_dir = os.path.dirname(os.path.abspath(self._input_file))
|
2014-02-06 08:09:26 +08:00
|
|
|
|
2019-06-17 09:12:39 +08:00
|
|
|
log.debug("converter not input_file=%s parser=%s",
|
2014-02-06 08:09:26 +08:00
|
|
|
self._input_file, self.parser)
|
|
|
|
|
2018-02-23 08:25:05 +08:00
|
|
|
self._guest = self.parser.export_libvirt(self.conn,
|
|
|
|
self._input_file)
|
2014-02-06 08:09:26 +08:00
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
for f in self._force_clean:
|
|
|
|
shutil.rmtree(f)
|
|
|
|
|
|
|
|
def get_guest(self):
|
|
|
|
return self._guest
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
"""
|
|
|
|
Remove any generated output.
|
|
|
|
"""
|
|
|
|
for path in self._err_clean:
|
|
|
|
if os.path.isfile(path):
|
|
|
|
os.remove(path)
|
|
|
|
if os.path.isdir(path):
|
|
|
|
shutil.rmtree(path)
|
|
|
|
|
|
|
|
def _copy_file(self, absin, absout, dry):
|
|
|
|
self.print_cb("Copying %s to %s" % (os.path.basename(absin), absout))
|
|
|
|
if not dry:
|
|
|
|
shutil.copy(absin, absout)
|
|
|
|
|
|
|
|
def _qemu_convert(self, absin, absout, disk_format, dry):
|
|
|
|
"""
|
|
|
|
Use qemu-img to convert the given disk. Note that at least some
|
|
|
|
version of qemu-img cannot handle multi-file VMDKs, so this can
|
|
|
|
easily go wrong.
|
|
|
|
Gentoo, Debian, and Ubuntu (potentially others) install kvm-img
|
|
|
|
with kvm and qemu-img with qemu. Both would work.
|
|
|
|
"""
|
|
|
|
binnames = ["qemu-img", "kvm-img"]
|
|
|
|
|
2016-03-07 18:39:23 +08:00
|
|
|
decompress_cmd = None
|
|
|
|
|
2014-02-06 08:09:26 +08:00
|
|
|
if _is_test():
|
|
|
|
executable = "/usr/bin/qemu-img"
|
|
|
|
else:
|
|
|
|
for binname in binnames:
|
2019-06-06 02:15:23 +08:00
|
|
|
executable = shutil.which(binname)
|
2014-02-06 08:09:26 +08:00
|
|
|
if executable:
|
|
|
|
break
|
|
|
|
|
|
|
|
if executable is None:
|
|
|
|
raise RuntimeError(_("None of %s tools found.") % binnames)
|
|
|
|
|
|
|
|
base = os.path.basename(absin)
|
2016-03-07 18:39:23 +08:00
|
|
|
ext = os.path.splitext(base)[1]
|
|
|
|
if (ext and ext[1:] == "gz"):
|
2019-06-06 02:15:23 +08:00
|
|
|
if not shutil.which("gzip"):
|
2016-03-07 18:39:23 +08:00
|
|
|
raise RuntimeError("'gzip' is needed to decompress the file, "
|
|
|
|
"but not found.")
|
|
|
|
decompress_cmd = ["gzip", "-d", absin]
|
|
|
|
base = os.path.splitext(base)[0]
|
|
|
|
absin = absin[0:-3]
|
|
|
|
self.print_cb("Running %s" % " ".join(decompress_cmd))
|
2014-02-06 08:09:26 +08:00
|
|
|
cmd = [executable, "convert", "-O", disk_format, base, absout]
|
|
|
|
self.print_cb("Running %s" % " ".join(cmd))
|
|
|
|
if dry:
|
|
|
|
return
|
|
|
|
|
|
|
|
cmd[4] = absin
|
2016-03-07 18:39:23 +08:00
|
|
|
if decompress_cmd is not None:
|
|
|
|
_run_cmd(decompress_cmd)
|
2014-02-06 08:09:26 +08:00
|
|
|
_run_cmd(cmd)
|
|
|
|
|
|
|
|
def convert_disks(self, disk_format, destdir=None, dry=False):
|
|
|
|
"""
|
|
|
|
Convert a disk into the requested format if possible, in the
|
|
|
|
given output directory. Raises RuntimeError or other failures.
|
|
|
|
"""
|
|
|
|
if disk_format == "none":
|
|
|
|
disk_format = None
|
|
|
|
|
|
|
|
if destdir is None:
|
2019-06-11 20:26:04 +08:00
|
|
|
poolxml = StoragePool.build_default_pool(self.conn)
|
|
|
|
destdir = poolxml.target_path
|
2014-02-06 08:09:26 +08:00
|
|
|
|
|
|
|
guest = self.get_guest()
|
2018-03-21 05:23:34 +08:00
|
|
|
for disk in guest.devices.disk:
|
2014-02-06 08:09:26 +08:00
|
|
|
if disk.device != "disk":
|
|
|
|
continue
|
|
|
|
|
|
|
|
if disk_format and disk.driver_type == disk_format:
|
2019-06-17 09:12:39 +08:00
|
|
|
log.debug("path=%s is already in requested format=%s",
|
2014-02-06 08:09:26 +08:00
|
|
|
disk.path, disk_format)
|
|
|
|
disk_format = None
|
|
|
|
|
|
|
|
basepath = os.path.splitext(os.path.basename(disk.path))[0]
|
|
|
|
newpath = re.sub(r'\s', '_', basepath)
|
|
|
|
if disk_format:
|
|
|
|
newpath += ("." + disk_format)
|
|
|
|
newpath = os.path.join(destdir, newpath)
|
2016-01-15 15:27:44 +08:00
|
|
|
if os.path.exists(newpath) and not _is_test():
|
2014-02-06 08:09:26 +08:00
|
|
|
raise RuntimeError(_("New path name '%s' already exists") %
|
|
|
|
newpath)
|
|
|
|
|
|
|
|
if not disk_format or disk_format == "none":
|
|
|
|
self._copy_file(disk.path, newpath, dry)
|
|
|
|
else:
|
|
|
|
self._qemu_convert(disk.path, newpath, disk_format, dry)
|
|
|
|
disk.driver_type = disk_format
|
|
|
|
disk.path = newpath
|
|
|
|
self._err_clean.append(newpath)
|