2015-05-08 15:39:06 +00:00
|
|
|
#!/usr/bin/python3
|
2011-09-24 00:36:09 +00:00
|
|
|
#
|
|
|
|
# Live Media Creator
|
|
|
|
#
|
2015-05-08 15:39:06 +00:00
|
|
|
# Copyright (C) 2011-2015 Red Hat, Inc.
|
2011-09-24 00:36:09 +00:00
|
|
|
#
|
|
|
|
# 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
# Author(s): Brian C. Lane <bcl@redhat.com>
|
|
|
|
#
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger("livemedia-creator")
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
import subprocess
|
|
|
|
import shutil
|
2012-05-24 22:55:46 +00:00
|
|
|
import hashlib
|
2014-07-30 15:59:27 +00:00
|
|
|
import glob
|
2015-10-19 22:30:33 +00:00
|
|
|
import json
|
|
|
|
from math import ceil
|
2016-01-06 00:49:40 +00:00
|
|
|
import socket
|
2016-03-24 21:10:14 +00:00
|
|
|
import selinux
|
2011-09-24 00:36:09 +00:00
|
|
|
|
|
|
|
# Use pykickstart to calculate disk image size
|
|
|
|
from pykickstart.parser import KickstartParser
|
|
|
|
from pykickstart.version import makeVersion
|
2015-11-16 22:07:32 +00:00
|
|
|
from pykickstart.constants import KS_SHUTDOWN
|
2011-09-24 00:36:09 +00:00
|
|
|
|
2012-05-24 22:55:46 +00:00
|
|
|
# Use Mako templates for appliance builder descriptions
|
|
|
|
from mako.template import Template
|
|
|
|
from mako.exceptions import text_error_template
|
|
|
|
|
2011-09-24 00:36:09 +00:00
|
|
|
# Use the Lorax treebuilder branch for iso creation
|
2016-02-11 18:50:46 +00:00
|
|
|
from pylorax import ArchData, setup_logging, find_templates
|
2011-09-24 00:36:09 +00:00
|
|
|
from pylorax.base import DataHolder
|
2012-01-26 01:23:25 +00:00
|
|
|
from pylorax.treebuilder import TreeBuilder, RuntimeBuilder, udev_escape
|
2014-01-15 00:06:33 +00:00
|
|
|
from pylorax.treebuilder import findkernels
|
2013-01-30 22:16:25 +00:00
|
|
|
from pylorax.sysutils import joinpaths, remove
|
2012-05-12 00:35:16 +00:00
|
|
|
from pylorax.imgutils import PartitionMount, mksparse, mkext4img, loop_detach
|
2013-01-30 22:16:25 +00:00
|
|
|
from pylorax.imgutils import get_loop_name, dm_detach, mount, umount, Mount
|
2015-12-18 23:30:51 +00:00
|
|
|
from pylorax.imgutils import mksquashfs, mkqemu_img, mktar, mkrootfsimg
|
2016-01-06 00:49:40 +00:00
|
|
|
from pylorax.imgutils import copytree, mkcpio
|
2015-07-29 22:42:57 +00:00
|
|
|
from pylorax.executils import execWithRedirect, execReadlines, runcmd
|
2015-05-27 17:17:38 +00:00
|
|
|
from pylorax.monitor import LogMonitor
|
2015-05-27 17:48:11 +00:00
|
|
|
from pylorax.mount import IsoMountpoint
|
2016-03-18 21:24:31 +00:00
|
|
|
from pylorax.cmdline import lmc_parser
|
2011-09-24 00:36:09 +00:00
|
|
|
|
2012-02-07 19:32:14 +00:00
|
|
|
# Default parameters for rebuilding initramfs, override with --dracut-args
|
2015-11-23 17:33:42 +00:00
|
|
|
DRACUT_DEFAULT = ["--xz", "--add", "livenet dmsquash-live convertfs pollcdrom qemu qemu-net",
|
2015-08-31 17:03:56 +00:00
|
|
|
"--omit", "plymouth", "--no-hostonly", "--debug", "--no-early-microcode"]
|
2011-09-24 00:36:09 +00:00
|
|
|
|
2013-01-30 22:16:25 +00:00
|
|
|
ROOT_PATH = "/mnt/sysimage/"
|
|
|
|
RUNTIME = "images/install.img"
|
2015-11-04 02:03:09 +00:00
|
|
|
UEFI_FIRMWARE="loader={0}/OVMF_CODE.fd,loader_ro=yes,loader_type=pflash,nvram_template={0}/OVMF_VARS.fd"
|
|
|
|
|
2013-01-30 22:16:25 +00:00
|
|
|
|
|
|
|
class InstallError(Exception):
|
|
|
|
pass
|
|
|
|
|
2011-09-24 00:36:09 +00:00
|
|
|
|
2015-03-09 16:15:31 +00:00
|
|
|
class FakeDNF(object):
|
|
|
|
"""
|
|
|
|
A minimal DNF object suitable for passing to RuntimeBuilder
|
|
|
|
|
|
|
|
lmc uses RuntimeBuilder to run the arch specific iso creation
|
|
|
|
templates, so the the installroot config value is the important part of
|
|
|
|
this. Everything else should be a nop.
|
|
|
|
"""
|
|
|
|
def __init__(self, conf):
|
|
|
|
self.conf = conf
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2016-01-06 00:49:40 +00:00
|
|
|
def find_free_port(start=5900, end=5999, host="127.0.0.1"):
|
|
|
|
""" Return first free port in range.
|
|
|
|
|
|
|
|
:param int start: Starting port number
|
|
|
|
:param int end: Ending port number
|
|
|
|
:param str host: Host IP to search
|
|
|
|
:returns: First free port or -1 if none found
|
|
|
|
:rtype: int
|
2011-09-24 00:36:09 +00:00
|
|
|
"""
|
2016-01-06 00:49:40 +00:00
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
for port in range(start, end+1):
|
|
|
|
try:
|
|
|
|
s.bind((host, port))
|
|
|
|
s.close()
|
|
|
|
return port
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return -1
|
|
|
|
|
|
|
|
|
|
|
|
def append_initrd(initrd, files):
|
|
|
|
""" Append files to an initrd.
|
|
|
|
|
|
|
|
:param str initrd: Path to initrd
|
|
|
|
:param list files: list of file paths to add
|
|
|
|
:returns: Path to a new initrd
|
|
|
|
:rtype: str
|
|
|
|
|
|
|
|
The files are added to the initrd by creating a cpio image
|
|
|
|
of the files (stored at /) and writing the cpio to the end of a
|
|
|
|
copy of the initrd.
|
|
|
|
|
|
|
|
The initrd is not changed, a copy is made before appending the
|
|
|
|
cpio archive.
|
|
|
|
"""
|
|
|
|
qemu_initrd = tempfile.mktemp(prefix="lmc-initrd-", suffix=".img")
|
|
|
|
shutil.copy2(initrd, qemu_initrd)
|
|
|
|
ks_dir = tempfile.mkdtemp(prefix="lmc-ksdir-")
|
|
|
|
for ks in files:
|
|
|
|
shutil.copy2(ks, ks_dir)
|
|
|
|
ks_initrd = tempfile.mktemp(prefix="lmc-ks-", suffix=".img")
|
|
|
|
mkcpio(ks_dir, ks_initrd)
|
|
|
|
shutil.rmtree(ks_dir)
|
|
|
|
with open(qemu_initrd, "ab") as initrd_fp:
|
|
|
|
with open(ks_initrd, "rb") as ks_fp:
|
|
|
|
while True:
|
|
|
|
data = ks_fp.read(1024**2)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
initrd_fp.write(data)
|
|
|
|
os.unlink(ks_initrd)
|
|
|
|
|
|
|
|
return qemu_initrd
|
|
|
|
|
|
|
|
|
|
|
|
class QEMUInstall(object):
|
2011-09-24 00:36:09 +00:00
|
|
|
"""
|
2016-01-06 00:49:40 +00:00
|
|
|
Run qemu using an iso and a kickstart
|
|
|
|
"""
|
|
|
|
# Mapping of arch to qemu command
|
|
|
|
QEMU_CMDS = {"x86_64": "qemu-system-x86_64",
|
|
|
|
"i386": "qemu-system-i386",
|
|
|
|
"arm": "qemu-system-arm",
|
|
|
|
"aarch64": "qemu-system-aarch64",
|
|
|
|
"ppc": "qemu-system-ppc",
|
|
|
|
"ppc64": "qemu-system-ppc64"
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, opts, iso, ks_paths, disk_img, img_size=2048,
|
2015-11-13 21:46:08 +00:00
|
|
|
kernel_args=None, memory=1024, vcpus=None, vnc=None, arch=None,
|
2014-05-09 16:28:01 +00:00
|
|
|
log_check=None, virtio_host="127.0.0.1", virtio_port=6080,
|
2015-12-18 23:30:51 +00:00
|
|
|
image_type=None, boot_uefi=False, ovmf_path=None):
|
2011-09-24 00:36:09 +00:00
|
|
|
"""
|
2014-05-09 18:46:32 +00:00
|
|
|
Start the installation
|
|
|
|
|
|
|
|
:param iso: Information about the iso to use for the installation
|
|
|
|
:type iso: IsoMountpoint
|
|
|
|
:param list ks_paths: Paths to kickstart files. All are injected, the
|
|
|
|
first one is the one executed.
|
|
|
|
:param str disk_img: Path to a disk image, created it it doesn't exist
|
|
|
|
:param int img_size: The image size, in MiB, to create if it doesn't exist
|
|
|
|
:param str kernel_args: Extra kernel arguments to pass on the kernel cmdline
|
|
|
|
:param int memory: Amount of RAM to assign to the virt, in MiB
|
2015-11-13 21:46:08 +00:00
|
|
|
:param int vcpus: Number of virtual cpus
|
2016-01-06 00:49:40 +00:00
|
|
|
:param str vnc: Arguments to pass to qemu -display
|
2014-05-09 18:46:32 +00:00
|
|
|
:param str arch: Optional architecture to use in the virt
|
|
|
|
:param log_check: Method that returns True if the installation fails
|
|
|
|
:type log_check: method
|
|
|
|
:param str virtio_host: Hostname to connect virtio log to
|
|
|
|
:param int virtio_port: Port to connect virtio log to
|
2015-12-18 23:30:51 +00:00
|
|
|
:param str image_type: Type of qemu-img disk to create, or None.
|
2015-11-04 02:03:09 +00:00
|
|
|
:param bool boot_uefi: Use OVMF to boot the VM in UEFI mode
|
|
|
|
:param str ovmf_path: Path to the OVMF firmware
|
2011-09-24 00:36:09 +00:00
|
|
|
"""
|
2016-01-06 00:49:40 +00:00
|
|
|
# Lookup qemu-system- for arch if passed, or try to guess using host arch
|
|
|
|
qemu_cmd = [self.QEMU_CMDS.get(arch or os.uname().machine, "qemu-system-"+os.uname().machine)]
|
|
|
|
if not os.path.exists("/usr/bin/"+qemu_cmd[0]):
|
|
|
|
raise InstallError("%s does not exist, cannot run qemu" % qemu_cmd[0])
|
2011-09-24 00:36:09 +00:00
|
|
|
|
2016-01-06 00:49:40 +00:00
|
|
|
qemu_cmd += ["-nodefconfig"]
|
|
|
|
qemu_cmd += ["-m", str(memory)]
|
|
|
|
if vcpus:
|
|
|
|
qemu_cmd += ["-smp", str(vcpus)]
|
2012-01-26 01:23:25 +00:00
|
|
|
|
2016-01-06 00:49:40 +00:00
|
|
|
if not opts.no_kvm and os.path.exists("/dev/kvm"):
|
|
|
|
qemu_cmd += ["--machine", "accel=kvm"]
|
2012-01-26 01:23:25 +00:00
|
|
|
|
2016-01-06 00:49:40 +00:00
|
|
|
# Copy the initrd from the iso, create a cpio archive of the kickstart files
|
|
|
|
# and append it to the temporary initrd.
|
|
|
|
qemu_initrd = append_initrd(iso.initrd, ks_paths)
|
|
|
|
qemu_cmd += ["-kernel", iso.kernel]
|
|
|
|
qemu_cmd += ["-initrd", qemu_initrd]
|
2012-01-26 01:23:25 +00:00
|
|
|
|
2016-01-06 00:49:40 +00:00
|
|
|
# Add the disk and cdrom
|
|
|
|
if not os.path.isfile(disk_img):
|
|
|
|
mksparse(disk_img, img_size * 1024**2)
|
|
|
|
drive_args = "file=%s" % disk_img
|
|
|
|
drive_args += ",cache=unsafe,discard=unmap"
|
|
|
|
if image_type:
|
|
|
|
drive_args += ",format=%s" % image_type
|
|
|
|
else:
|
|
|
|
drive_args += ",format=raw"
|
|
|
|
qemu_cmd += ["-drive", drive_args]
|
|
|
|
|
|
|
|
drive_args = "file=%s,media=cdrom,readonly=on" % iso.iso_path
|
|
|
|
qemu_cmd += ["-drive", drive_args]
|
|
|
|
|
|
|
|
# Setup the cmdline args
|
|
|
|
# ======================
|
|
|
|
cmdline_args = "ks=file:/%s" % os.path.basename(ks_paths[0])
|
|
|
|
cmdline_args += " inst.stage2=hd:LABEL=%s" % udev_escape(iso.label)
|
|
|
|
if opts.proxy:
|
|
|
|
cmdline_args += " inst.proxy=%s" % opts.proxy
|
2011-09-24 00:36:09 +00:00
|
|
|
if kernel_args:
|
2016-01-06 00:49:40 +00:00
|
|
|
cmdline_args += " "+kernel_args
|
|
|
|
cmdline_args += " inst.text inst.cmdline"
|
2012-01-26 01:23:25 +00:00
|
|
|
|
2016-01-06 00:49:40 +00:00
|
|
|
qemu_cmd += ["-append", cmdline_args]
|
2012-01-26 01:23:25 +00:00
|
|
|
|
2016-01-06 00:49:40 +00:00
|
|
|
if not opts.vnc:
|
|
|
|
vnc_port = find_free_port()
|
|
|
|
if vnc_port == -1:
|
|
|
|
raise InstallError("No free VNC ports")
|
|
|
|
display_args = "vnc=127.0.0.1:%d" % (vnc_port - 5900)
|
|
|
|
else:
|
|
|
|
display_args = opts.vnc
|
|
|
|
log.info("qemu %s", display_args)
|
|
|
|
qemu_cmd += ["-nographic", "-display", display_args ]
|
2012-01-26 01:23:25 +00:00
|
|
|
|
2016-01-06 00:49:40 +00:00
|
|
|
# Setup the virtio log port
|
|
|
|
qemu_cmd += ["-device", "virtio-serial-pci,id=virtio-serial0"]
|
|
|
|
qemu_cmd += ["-device", "virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0"
|
|
|
|
",id=channel0,name=org.fedoraproject.anaconda.log.0"]
|
|
|
|
qemu_cmd += ["-chardev", "socket,id=charchannel0,host=%s,port=%s" % (virtio_host, virtio_port)]
|
2012-05-29 18:21:15 +00:00
|
|
|
|
2016-01-06 00:49:40 +00:00
|
|
|
# PAss through rng from host
|
|
|
|
qemu_cmd += ["-object", "rng-random,id=virtio-rng0,filename=/dev/random"]
|
|
|
|
qemu_cmd += ["-device", "virtio-rng-pci,rng=virtio-rng0,id=rng0,bus=pci.0,addr=0x9"]
|
2015-11-13 21:46:08 +00:00
|
|
|
|
2015-11-04 02:03:09 +00:00
|
|
|
if boot_uefi and ovmf_path:
|
2016-01-06 00:49:40 +00:00
|
|
|
qemu_cmd += ["-drive", "file=%s/OVMF_CODE.fd,if=pflash,format=raw,unit=0,readonly=on" % ovmf_path]
|
|
|
|
qemu_cmd += ["-drive", "file=%sOVMF_VARS.fd,if=pflash,format=raw,unit=1" % ovmf_path]
|
2015-11-04 02:03:09 +00:00
|
|
|
|
2016-01-06 00:49:40 +00:00
|
|
|
log.info("Running qemu")
|
|
|
|
log.debug(qemu_cmd)
|
2014-07-02 22:10:14 +00:00
|
|
|
try:
|
2016-01-06 00:49:40 +00:00
|
|
|
execWithRedirect(qemu_cmd[0], qemu_cmd[1:], reset_lang=False, raise_err=True,
|
|
|
|
callback=lambda p: not log_check())
|
2014-07-02 22:10:14 +00:00
|
|
|
except subprocess.CalledProcessError as e:
|
2016-01-06 00:49:40 +00:00
|
|
|
log.error("Running qemu failed:")
|
|
|
|
log.error("cmd: %s", " ".join(e.cmd))
|
|
|
|
log.error("output: %s", e.output or "")
|
|
|
|
raise InstallError("QEMUInstall failed")
|
|
|
|
except (OSError, KeyboardInterrupt) as e:
|
|
|
|
log.error("Running qemu failed: %s", str(e))
|
|
|
|
raise InstallError("QEMUInstall failed")
|
|
|
|
finally:
|
|
|
|
os.unlink(qemu_initrd)
|
2011-09-24 00:36:09 +00:00
|
|
|
|
|
|
|
if log_check():
|
2016-01-06 00:49:40 +00:00
|
|
|
log.error("Installation error detected. See logfile for details.")
|
|
|
|
raise InstallError("QEMUInstall failed")
|
2011-09-24 00:36:09 +00:00
|
|
|
else:
|
2016-01-06 00:49:40 +00:00
|
|
|
log.info("Installation finished without errors.")
|
2011-09-24 00:36:09 +00:00
|
|
|
|
|
|
|
|
2012-05-12 00:35:16 +00:00
|
|
|
def is_image_mounted(disk_img):
|
|
|
|
"""
|
2014-05-09 18:46:32 +00:00
|
|
|
Check to see if the disk_img is mounted
|
|
|
|
|
|
|
|
:returns: True if disk_img is in /proc/mounts
|
|
|
|
:rtype: bool
|
2012-05-12 00:35:16 +00:00
|
|
|
"""
|
|
|
|
with open("/proc/mounts") as mounts:
|
2014-05-09 00:22:14 +00:00
|
|
|
for mnt in mounts:
|
|
|
|
fields = mnt.split()
|
2012-05-12 00:35:16 +00:00
|
|
|
if len(fields) > 2 and fields[1] == disk_img:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2014-07-30 15:59:27 +00:00
|
|
|
def find_ostree_root(phys_root):
|
|
|
|
"""
|
|
|
|
Find root of ostree deployment
|
|
|
|
|
|
|
|
:param str phys_root: Path to physical root
|
|
|
|
:returns: Relative path of ostree deployment root
|
|
|
|
:rtype: str
|
|
|
|
:raise Exception: More than one deployment roots were found
|
|
|
|
"""
|
|
|
|
ostree_root = ""
|
2016-04-27 00:25:22 +00:00
|
|
|
ostree_sysroots = glob.glob(joinpaths(phys_root, "ostree/boot.?/*/*/0"))
|
|
|
|
log.debug("ostree_sysroots = %s", ostree_sysroots)
|
2014-07-30 15:59:27 +00:00
|
|
|
if ostree_sysroots:
|
|
|
|
if len(ostree_sysroots) > 1:
|
|
|
|
raise Exception("Too many deployment roots found: %s" % ostree_sysroots)
|
|
|
|
ostree_root = os.path.relpath(ostree_sysroots[0], phys_root)
|
|
|
|
return ostree_root
|
2011-09-24 00:36:09 +00:00
|
|
|
|
2014-01-15 00:06:33 +00:00
|
|
|
def get_arch(mount_dir):
|
2014-05-09 18:46:32 +00:00
|
|
|
"""
|
|
|
|
Get the kernel arch
|
|
|
|
|
|
|
|
:returns: Arch of first kernel found at mount_dir/boot/ or i386
|
|
|
|
:rtype: str
|
2011-12-09 01:12:06 +00:00
|
|
|
"""
|
2014-01-15 00:06:33 +00:00
|
|
|
kernels = findkernels(mount_dir)
|
|
|
|
if not kernels:
|
|
|
|
return "i386"
|
|
|
|
return kernels[0].arch
|
2011-09-24 00:36:09 +00:00
|
|
|
|
2016-03-24 22:52:29 +00:00
|
|
|
def squashfs_args(opts):
|
|
|
|
""" Returns the compression type and args to use when making squashfs
|
|
|
|
|
|
|
|
:param opts: ArgumentParser object with compression and compressopts
|
|
|
|
:returns: tuple of compression type and args
|
|
|
|
:rtype: tuple
|
|
|
|
"""
|
|
|
|
compression = opts.compression or "xz"
|
|
|
|
arch = ArchData(opts.arch or os.uname().machine)
|
|
|
|
if compression == "xz" and arch.bcj:
|
|
|
|
compressargs = ["-Xbcj", arch.bcj]
|
|
|
|
else:
|
|
|
|
compressargs = []
|
|
|
|
return (compression, compressargs)
|
|
|
|
|
2011-09-24 00:36:09 +00:00
|
|
|
|
2012-05-24 22:55:46 +00:00
|
|
|
def make_appliance(disk_img, name, template, outfile, networks=None, ram=1024,
|
2012-05-29 18:21:15 +00:00
|
|
|
vcpus=1, arch=None, title="Linux", project="Linux",
|
2016-03-04 16:57:55 +00:00
|
|
|
releasever="25"):
|
2012-05-24 22:55:46 +00:00
|
|
|
"""
|
|
|
|
Generate an appliance description file
|
|
|
|
|
2014-05-09 18:46:32 +00:00
|
|
|
:param str disk_img: Full path of the disk image
|
|
|
|
:param str name: Name of the appliance, passed to the template
|
|
|
|
:param str template: Full path of Mako template
|
|
|
|
:param str outfile: Full path of file to write, using template
|
|
|
|
:param list networks: List of networks(str) from the kickstart
|
|
|
|
:param int ram: Ram, in MiB, passed to template. Default is 1024
|
|
|
|
:param int vcpus: CPUs, passed to template. Default is 1
|
|
|
|
:param str arch: CPU architecture. Default is 'x86_64'
|
|
|
|
:param str title: Title, passed to template. Default is 'Linux'
|
|
|
|
:param str project: Project, passed to template. Default is 'Linux'
|
2016-03-04 16:57:55 +00:00
|
|
|
:param str releasever: Release version, passed to template. Default is 25
|
2012-05-24 22:55:46 +00:00
|
|
|
"""
|
|
|
|
if not (disk_img and template and outfile):
|
|
|
|
return None
|
|
|
|
|
2015-05-01 00:10:08 +00:00
|
|
|
log.info("Creating appliance definition using %s", template)
|
2012-05-24 22:55:46 +00:00
|
|
|
|
2012-05-29 18:21:15 +00:00
|
|
|
if not arch:
|
|
|
|
arch = "x86_64"
|
2012-05-24 22:55:46 +00:00
|
|
|
|
2015-05-01 00:10:08 +00:00
|
|
|
log.info("Calculating SHA256 checksum of %s", disk_img)
|
2012-05-24 22:55:46 +00:00
|
|
|
sha256 = hashlib.sha256()
|
|
|
|
with open(disk_img) as f:
|
|
|
|
while True:
|
2014-04-03 18:52:41 +00:00
|
|
|
data = f.read(1024**2)
|
2012-05-24 22:55:46 +00:00
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
sha256.update(data)
|
2015-05-01 00:10:08 +00:00
|
|
|
log.info("SHA256 of %s is %s", disk_img, sha256.hexdigest())
|
2012-05-24 22:55:46 +00:00
|
|
|
disk_info = DataHolder(name=os.path.basename(disk_img), format="raw",
|
|
|
|
checksum_type="sha256", checksum=sha256.hexdigest())
|
|
|
|
try:
|
|
|
|
result = Template(filename=template).render(disks=[disk_info], name=name,
|
2014-02-05 22:39:04 +00:00
|
|
|
arch=arch, memory=ram, vcpus=vcpus, networks=networks,
|
2012-05-24 22:55:46 +00:00
|
|
|
title=title, project=project, releasever=releasever)
|
|
|
|
except Exception:
|
|
|
|
log.error(text_error_template().render())
|
|
|
|
raise
|
|
|
|
|
|
|
|
with open(outfile, "w") as f:
|
|
|
|
f.write(result)
|
|
|
|
|
|
|
|
|
2014-04-03 18:52:41 +00:00
|
|
|
def make_fsimage(diskimage, fsimage, img_size=None, label="Anaconda"):
|
2012-02-28 21:57:17 +00:00
|
|
|
"""
|
2014-04-02 23:56:28 +00:00
|
|
|
Copy the / partition of a partitioned disk image to an un-partitioned
|
|
|
|
disk image.
|
2012-02-28 21:57:17 +00:00
|
|
|
|
2014-05-09 18:46:32 +00:00
|
|
|
:param str diskimage: The full path to partitioned disk image with a /
|
|
|
|
:param str fsimage: The full path of the output fs image file
|
|
|
|
:param int img_size: Optional size of the fsimage in MiB or None to make
|
|
|
|
it as small as possible
|
|
|
|
:param str label: The label to apply to the image. Defaults to "Anaconda"
|
2012-02-28 21:57:17 +00:00
|
|
|
"""
|
2014-04-02 23:56:28 +00:00
|
|
|
with PartitionMount(diskimage) as img_mount:
|
2012-05-22 20:11:53 +00:00
|
|
|
if not img_mount or not img_mount.mount_dir:
|
|
|
|
return None
|
|
|
|
|
2014-05-09 00:22:14 +00:00
|
|
|
log.info("Creating fsimage %s (%s)", fsimage, img_size or "minimized")
|
2014-04-03 18:52:41 +00:00
|
|
|
if img_size:
|
|
|
|
# convert to Bytes
|
|
|
|
img_size *= 1024**2
|
|
|
|
|
|
|
|
mkext4img(img_mount.mount_dir, fsimage, size=img_size, label=label)
|
2012-02-28 21:57:17 +00:00
|
|
|
|
|
|
|
|
2016-03-05 01:22:51 +00:00
|
|
|
def make_runtime(opts, mount_dir, work_dir, size=None):
|
2013-01-30 22:16:25 +00:00
|
|
|
"""
|
|
|
|
Make the squashfs image from a directory
|
|
|
|
|
2014-05-09 18:46:32 +00:00
|
|
|
:param opts: options passed to livemedia-creator
|
|
|
|
:type opts: argparse options
|
|
|
|
:param str mount_dir: Directory tree to compress
|
|
|
|
:param str work_dir: Output compressed image to work_dir+images/install.img
|
2016-03-05 01:22:51 +00:00
|
|
|
:param int size: Size of disk image, in GiB
|
2013-01-30 22:16:25 +00:00
|
|
|
"""
|
2014-01-15 00:06:33 +00:00
|
|
|
kernel_arch = get_arch(mount_dir)
|
2013-01-30 22:16:25 +00:00
|
|
|
|
2015-03-09 16:15:31 +00:00
|
|
|
# Fake dnf object
|
|
|
|
fake_dbo = FakeDNF(conf=DataHolder(installroot=mount_dir))
|
2013-01-30 22:16:25 +00:00
|
|
|
# Fake arch with only basearch set
|
2014-05-20 00:29:27 +00:00
|
|
|
arch = ArchData(kernel_arch)
|
2013-01-30 22:16:25 +00:00
|
|
|
# TODO: Need to get release info from someplace...
|
|
|
|
product = DataHolder(name=opts.project, version=opts.releasever, release="",
|
|
|
|
variant="", bugurl="", isfinal=False)
|
|
|
|
|
|
|
|
# This is a mounted image partition, cannot hardlink to it, so just use it
|
|
|
|
# symlink mount_dir/images to work_dir/images so we don't run out of space
|
|
|
|
os.makedirs(joinpaths(work_dir, "images"))
|
|
|
|
|
2015-03-09 16:15:31 +00:00
|
|
|
rb = RuntimeBuilder(product, arch, fake_dbo)
|
2016-03-24 22:52:29 +00:00
|
|
|
compression, compressargs = squashfs_args(opts)
|
2013-01-30 22:16:25 +00:00
|
|
|
log.info("Creating runtime")
|
2016-03-24 22:52:29 +00:00
|
|
|
rb.create_runtime(joinpaths(work_dir, RUNTIME), size=size,
|
|
|
|
compression=compression, compressargs=compressargs)
|
2013-01-30 22:16:25 +00:00
|
|
|
|
2014-07-30 15:59:27 +00:00
|
|
|
def rebuild_initrds_for_live(opts, sys_root_dir, results_dir):
|
|
|
|
"""
|
|
|
|
Rebuild intrds for pxe live image (root=live:http://)
|
|
|
|
|
|
|
|
:param opts: options passed to livemedia-creator
|
|
|
|
:type opts: argparse options
|
|
|
|
:param str sys_root_dir: Path to root of the system
|
|
|
|
:param str results_dir: Path of directory for storing results
|
|
|
|
"""
|
|
|
|
if not opts.dracut_args:
|
|
|
|
dracut_args = DRACUT_DEFAULT
|
|
|
|
else:
|
|
|
|
dracut_args = []
|
|
|
|
for arg in opts.dracut_args:
|
|
|
|
dracut_args += arg.split(" ", 1)
|
2015-05-01 00:10:08 +00:00
|
|
|
log.info("dracut args = %s", dracut_args)
|
2014-07-30 15:59:27 +00:00
|
|
|
|
|
|
|
dracut = ["dracut", "--nomdadmconf", "--nolvmconf"] + dracut_args
|
|
|
|
|
|
|
|
kdir = "boot"
|
|
|
|
if opts.ostree:
|
2016-04-27 00:25:22 +00:00
|
|
|
kernels_dir = glob.glob(joinpaths(sys_root_dir, "boot/ostree/*"))
|
|
|
|
if kernels_dir:
|
|
|
|
kdir = os.path.relpath(kernels_dir[0], sys_root_dir)
|
2014-07-30 15:59:27 +00:00
|
|
|
|
2016-05-05 01:42:03 +00:00
|
|
|
kernels = [kernel for kernel in findkernels(sys_root_dir, kdir)]
|
2014-07-30 15:59:27 +00:00
|
|
|
if not kernels:
|
|
|
|
raise Exception("No initrds found, cannot rebuild_initrds")
|
|
|
|
|
|
|
|
# Hush some dracut warnings. TODO: bind-mount proc in place?
|
|
|
|
open(joinpaths(sys_root_dir,"/proc/modules"),"w")
|
|
|
|
|
|
|
|
if opts.ostree:
|
|
|
|
# Dracut assumes to have some dirs in disk image
|
|
|
|
# /var/tmp for temp files
|
|
|
|
vartmp_dir = joinpaths(sys_root_dir, "var/tmp")
|
|
|
|
if not os.path.isdir(vartmp_dir):
|
|
|
|
os.mkdir(vartmp_dir)
|
|
|
|
# /root (maybe not fatal)
|
|
|
|
root_dir = joinpaths(sys_root_dir, "var/roothome")
|
|
|
|
if not os.path.isdir(root_dir):
|
|
|
|
os.mkdir(root_dir)
|
|
|
|
# /tmp (maybe not fatal)
|
|
|
|
tmp_dir = joinpaths(sys_root_dir, "sysroot/tmp")
|
|
|
|
if not os.path.isdir(tmp_dir):
|
|
|
|
os.mkdir(tmp_dir)
|
|
|
|
|
2016-05-05 01:42:03 +00:00
|
|
|
# Write the new initramfs directly to the results directory
|
|
|
|
os.mkdir(joinpaths(sys_root_dir, "results"))
|
|
|
|
mount(results_dir, opts="bind", mnt=joinpaths(sys_root_dir, "results"))
|
|
|
|
# Dracut runs out of space inside the minimal rootfs image
|
|
|
|
mount("/var/tmp", opts="bind", mnt=joinpaths(sys_root_dir, "var/tmp"))
|
2014-07-30 15:59:27 +00:00
|
|
|
for kernel in kernels:
|
2016-05-05 01:42:03 +00:00
|
|
|
if hasattr(kernel, "initrd"):
|
|
|
|
outfile = os.path.basename(kernel.initrd.path)
|
|
|
|
else:
|
|
|
|
# Construct an initrd from the kernel name
|
|
|
|
outfile = os.path.basename(kernel.path.replace("vmlinuz-", "initrd-") + ".img")
|
2014-07-30 15:59:27 +00:00
|
|
|
log.info("rebuilding %s", outfile)
|
|
|
|
|
|
|
|
kver = kernel.version
|
|
|
|
|
2016-05-05 01:42:03 +00:00
|
|
|
cmd = dracut + ["/results/"+outfile, kver]
|
2014-07-30 15:59:27 +00:00
|
|
|
runcmd(cmd, root=sys_root_dir)
|
|
|
|
|
|
|
|
shutil.copy2(joinpaths(sys_root_dir, kernel.path), results_dir)
|
2016-05-05 01:42:03 +00:00
|
|
|
umount(joinpaths(sys_root_dir, "var/tmp"), delete=False)
|
|
|
|
umount(joinpaths(sys_root_dir, "results"), delete=False)
|
2014-07-30 15:59:27 +00:00
|
|
|
os.unlink(joinpaths(sys_root_dir,"/proc/modules"))
|
|
|
|
|
|
|
|
def create_pxe_config(template, images_dir, live_image_name, add_args = None):
|
|
|
|
"""
|
|
|
|
Create template for pxe to live configuration
|
|
|
|
|
|
|
|
:param str images_dir: Path of directory with images to be used
|
|
|
|
:param str live_image_name: Name of live rootfs image file
|
|
|
|
:param list add_args: Arguments to be added to initrd= pxe config
|
|
|
|
"""
|
|
|
|
|
|
|
|
add_args = add_args or []
|
|
|
|
|
|
|
|
kernels = [kernel for kernel in findkernels(images_dir, kdir="")
|
|
|
|
if hasattr(kernel, "initrd")]
|
|
|
|
if not kernels:
|
|
|
|
return
|
|
|
|
|
|
|
|
kernel = kernels[0]
|
|
|
|
|
|
|
|
add_args_str = " ".join(add_args)
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
result = Template(filename=template).render(kernel=kernel.path,
|
|
|
|
initrd=kernel.initrd.path, liveimg=live_image_name,
|
|
|
|
addargs=add_args_str)
|
|
|
|
except Exception:
|
|
|
|
log.error(text_error_template().render())
|
|
|
|
raise
|
|
|
|
|
|
|
|
with open (joinpaths(images_dir, "PXE_CONFIG"), "w") as f:
|
|
|
|
f.write(result)
|
2013-01-30 22:16:25 +00:00
|
|
|
|
2015-10-19 22:30:33 +00:00
|
|
|
|
|
|
|
def create_vagrant_metadata(path, size=0):
|
|
|
|
""" Create a default Vagrant metadata.json file
|
|
|
|
|
|
|
|
:param str path: Path to metadata.json file
|
|
|
|
:param int size: Disk size in MiB
|
|
|
|
"""
|
|
|
|
metadata = { "provider":"libvirt", "format":"qcow2", "virtual_size": ceil(size / 1024) }
|
|
|
|
with open(path, "wt") as f:
|
|
|
|
json.dump(metadata, f, indent=4)
|
|
|
|
|
|
|
|
|
|
|
|
def update_vagrant_metadata(path, size):
|
|
|
|
""" Update the Vagrant metadata.json file
|
|
|
|
|
|
|
|
:param str path: Path to metadata.json file
|
|
|
|
:param int size: Disk size in MiB
|
|
|
|
|
|
|
|
This function makes sure that the provider, format and virtual size of the
|
|
|
|
metadata file are set correctly. All other values are left untouched.
|
|
|
|
"""
|
|
|
|
with open(path, "rt") as f:
|
|
|
|
try:
|
|
|
|
metadata = json.load(f)
|
|
|
|
except ValueError as e:
|
|
|
|
log.error("Problem reading metadata file %s: %s", path, e)
|
|
|
|
return
|
|
|
|
|
|
|
|
metadata["provider"] = "libvirt"
|
|
|
|
metadata["format"] = "qcow2"
|
|
|
|
metadata["virtual_size"] = ceil(size / 1024)
|
|
|
|
with open(path, "wt") as f:
|
|
|
|
json.dump(metadata, f, indent=4)
|
|
|
|
|
|
|
|
|
2013-01-30 22:16:25 +00:00
|
|
|
def make_livecd(opts, mount_dir, work_dir):
|
2011-09-24 00:36:09 +00:00
|
|
|
"""
|
|
|
|
Take the content from the disk image and make a livecd out of it
|
|
|
|
|
2014-05-09 18:46:32 +00:00
|
|
|
:param opts: options passed to livemedia-creator
|
|
|
|
:type opts: argparse options
|
|
|
|
:param str mount_dir: Directory tree to compress
|
|
|
|
:param str work_dir: Output compressed image to work_dir+images/install.img
|
|
|
|
|
2011-09-24 00:36:09 +00:00
|
|
|
This uses wwood's squashfs live initramfs method:
|
|
|
|
* put the real / into LiveOS/rootfs.img
|
|
|
|
* make a squashfs of the LiveOS/rootfs.img tree
|
2014-05-09 18:46:32 +00:00
|
|
|
* This is loaded by dracut when the cmdline is passed to the kernel:
|
|
|
|
root=live:CDLABEL=<volid> rd.live.image
|
2011-09-24 00:36:09 +00:00
|
|
|
"""
|
2014-01-15 00:06:33 +00:00
|
|
|
kernel_arch = get_arch(mount_dir)
|
2013-01-30 22:16:25 +00:00
|
|
|
|
2014-05-20 00:29:27 +00:00
|
|
|
arch = ArchData(kernel_arch)
|
2013-01-30 22:16:25 +00:00
|
|
|
# TODO: Need to get release info from someplace...
|
|
|
|
product = DataHolder(name=opts.project, version=opts.releasever, release="",
|
|
|
|
variant="", bugurl="", isfinal=False)
|
|
|
|
|
|
|
|
# Link /images to work_dir/images to make the templates happy
|
|
|
|
if os.path.islink(joinpaths(mount_dir, "images")):
|
|
|
|
os.unlink(joinpaths(mount_dir, "images"))
|
|
|
|
execWithRedirect("/bin/ln", ["-s", joinpaths(work_dir, "images"),
|
|
|
|
joinpaths(mount_dir, "images")])
|
|
|
|
|
|
|
|
# The templates expect the config files to be in /tmp/config_files
|
|
|
|
# I think these should be release specific, not from lorax, but for now
|
|
|
|
configdir = joinpaths(opts.lorax_templates,"live/config_files/")
|
|
|
|
configdir_path = "tmp/config_files"
|
|
|
|
fullpath = joinpaths(mount_dir, configdir_path)
|
|
|
|
if os.path.exists(fullpath):
|
|
|
|
remove(fullpath)
|
2015-05-08 15:39:06 +00:00
|
|
|
copytree(configdir, fullpath)
|
2013-01-30 22:16:25 +00:00
|
|
|
|
2014-06-30 20:42:11 +00:00
|
|
|
isolabel = opts.volid or "{0.name}-{0.version}-{1.basearch}".format(product, arch)
|
2013-01-30 22:16:25 +00:00
|
|
|
if len(isolabel) > 32:
|
|
|
|
isolabel = isolabel[:32]
|
2015-12-16 19:53:38 +00:00
|
|
|
log.warning("Truncating isolabel to 32 chars: %s", isolabel)
|
2013-01-30 22:16:25 +00:00
|
|
|
|
2013-09-26 23:47:05 +00:00
|
|
|
tb = TreeBuilder(product=product, arch=arch, domacboot=opts.domacboot,
|
2013-01-30 22:16:25 +00:00
|
|
|
inroot=mount_dir, outroot=work_dir,
|
|
|
|
runtime=RUNTIME, isolabel=isolabel,
|
|
|
|
templatedir=joinpaths(opts.lorax_templates,"live/"))
|
2014-05-09 16:28:01 +00:00
|
|
|
log.info("Rebuilding initrds")
|
2013-01-30 22:16:25 +00:00
|
|
|
if not opts.dracut_args:
|
|
|
|
dracut_args = DRACUT_DEFAULT
|
|
|
|
else:
|
|
|
|
dracut_args = []
|
|
|
|
for arg in opts.dracut_args:
|
|
|
|
dracut_args += arg.split(" ", 1)
|
2015-05-01 00:10:08 +00:00
|
|
|
log.info("dracut args = %s", dracut_args)
|
2013-01-30 22:16:25 +00:00
|
|
|
tb.rebuild_initrds(add_args=dracut_args)
|
|
|
|
log.info("Building boot.iso")
|
|
|
|
tb.build()
|
2012-02-28 21:57:17 +00:00
|
|
|
|
2013-01-30 22:16:25 +00:00
|
|
|
return work_dir
|
2011-09-24 00:36:09 +00:00
|
|
|
|
2014-07-30 15:59:27 +00:00
|
|
|
def mount_boot_part_over_root(img_mount):
|
|
|
|
"""
|
|
|
|
Mount boot partition to /boot of root fs mounted in img_mount
|
|
|
|
|
|
|
|
Used for OSTree so it finds deployment configurations on live rootfs
|
|
|
|
|
|
|
|
param img_mount: object with mounted disk image root partition
|
|
|
|
type img_mount: imgutils.PartitionMount
|
|
|
|
"""
|
|
|
|
root_dir = img_mount.mount_dir
|
|
|
|
is_boot_part = lambda dir: os.path.exists(dir+"/loader.0")
|
2016-01-07 22:32:48 +00:00
|
|
|
tmp_mount_dir = tempfile.mkdtemp(prefix="lmc-tmpdir-")
|
2014-07-30 15:59:27 +00:00
|
|
|
sysroot_boot_dir = None
|
|
|
|
for dev, _size in img_mount.loop_devices:
|
|
|
|
if dev is img_mount.mount_dev:
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
mount("/dev/mapper/"+dev, mnt=tmp_mount_dir)
|
|
|
|
if is_boot_part(tmp_mount_dir):
|
|
|
|
umount(tmp_mount_dir)
|
2016-04-27 00:25:22 +00:00
|
|
|
sysroot_boot_dir = joinpaths(root_dir, "boot")
|
2014-07-30 15:59:27 +00:00
|
|
|
mount("/dev/mapper/"+dev, mnt=sysroot_boot_dir)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
umount(tmp_mount_dir)
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
log.debug("Looking for boot partition error: %s", e)
|
|
|
|
remove(tmp_mount_dir)
|
|
|
|
return sysroot_boot_dir
|
2013-01-30 22:16:25 +00:00
|
|
|
|
2015-01-09 21:13:55 +00:00
|
|
|
def novirt_log_check(log_check, proc):
|
|
|
|
"""
|
|
|
|
Check to see if there has been an error in the logs
|
|
|
|
|
|
|
|
:param log_check: method to call to check for an error in the logs
|
|
|
|
:param proc: Popen object for the anaconda process
|
|
|
|
:returns: True if the process has been terminated
|
|
|
|
|
|
|
|
The log_check method should return a True if an error has been detected.
|
|
|
|
When an error is detected the process is terminated and this returns True
|
|
|
|
"""
|
|
|
|
if log_check():
|
|
|
|
proc.terminate()
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2016-04-05 16:59:29 +00:00
|
|
|
def anaconda_cleanup(dirinstall_path):
|
|
|
|
"""
|
|
|
|
Cleanup any leftover mounts from anaconda
|
|
|
|
|
|
|
|
:param str dirinstall_path: Path where anaconda mounts things
|
|
|
|
|
|
|
|
If anaconda crashes it may leave things mounted under this path. It will
|
|
|
|
typically be set to /mnt/sysimage/
|
|
|
|
"""
|
|
|
|
dirinstall_path = os.path.abspath(dirinstall_path)
|
|
|
|
# unmount filesystems
|
|
|
|
for mounted in reversed(open("/proc/mounts").readlines()):
|
|
|
|
(_device, mountpoint, _rest) = mounted.split(" ", 2)
|
2016-04-26 21:37:19 +00:00
|
|
|
if mountpoint.startswith(dirinstall_path) and os.path.ismount(mountpoint):
|
2016-04-05 16:59:29 +00:00
|
|
|
umount(mountpoint)
|
|
|
|
|
|
|
|
|
2016-02-11 18:20:09 +00:00
|
|
|
def novirt_install(opts, disk_img, disk_size):
|
2013-01-30 22:16:25 +00:00
|
|
|
"""
|
|
|
|
Use Anaconda to install to a disk image
|
2014-04-03 18:52:41 +00:00
|
|
|
|
2014-05-09 18:46:32 +00:00
|
|
|
:param opts: options passed to livemedia-creator
|
|
|
|
:type opts: argparse options
|
|
|
|
:param str disk_img: The full path to the disk image to be created
|
|
|
|
:param int disk_size: The size of the disk_img in MiB
|
|
|
|
|
2016-03-24 21:10:14 +00:00
|
|
|
This method runs anaconda to create the image and then based on the opts
|
|
|
|
passed creates a qemu disk image or tarfile.
|
2013-01-30 22:16:25 +00:00
|
|
|
"""
|
2015-10-20 21:23:33 +00:00
|
|
|
dirinstall_path = ROOT_PATH
|
2013-01-30 22:16:25 +00:00
|
|
|
|
2013-11-19 01:09:00 +00:00
|
|
|
# Clean up /tmp/ from previous runs to prevent stale info from being used
|
|
|
|
for path in ["/tmp/yum.repos.d/", "/tmp/yum.cache/"]:
|
|
|
|
if os.path.isdir(path):
|
|
|
|
shutil.rmtree(path)
|
|
|
|
|
2016-02-11 18:20:09 +00:00
|
|
|
args = ["--kickstart", opts.ks[0], "--cmdline"]
|
2013-01-30 22:16:25 +00:00
|
|
|
if opts.anaconda_args:
|
|
|
|
for arg in opts.anaconda_args:
|
|
|
|
args += arg.split(" ", 1)
|
|
|
|
if opts.proxy:
|
|
|
|
args += ["--proxy", opts.proxy]
|
|
|
|
if opts.armplatform:
|
|
|
|
args += ["--armplatform", opts.armplatform]
|
|
|
|
|
2016-04-21 17:38:29 +00:00
|
|
|
if opts.make_iso or opts.make_fsimage or opts.make_pxe_live:
|
2013-01-30 22:16:25 +00:00
|
|
|
# Make a blank fs image
|
|
|
|
args += ["--dirinstall"]
|
|
|
|
|
2014-04-03 18:52:41 +00:00
|
|
|
mkext4img(None, disk_img, label=opts.fs_label, size=disk_size * 1024**2)
|
2015-10-20 21:23:33 +00:00
|
|
|
if not os.path.isdir(dirinstall_path):
|
|
|
|
os.mkdir(dirinstall_path)
|
|
|
|
mount(disk_img, opts="loop", mnt=dirinstall_path)
|
|
|
|
elif opts.make_tar or opts.make_oci:
|
|
|
|
# Install under dirinstall_path, make sure it starts clean
|
|
|
|
if os.path.exists(dirinstall_path):
|
|
|
|
shutil.rmtree(dirinstall_path)
|
|
|
|
|
|
|
|
if opts.make_oci:
|
|
|
|
# OCI installs under /rootfs/
|
|
|
|
dirinstall_path = joinpaths(dirinstall_path, "rootfs")
|
|
|
|
args += ["--dirinstall", dirinstall_path]
|
|
|
|
else:
|
|
|
|
args += ["--dirinstall"]
|
2014-04-04 21:38:51 +00:00
|
|
|
|
2015-10-20 21:23:33 +00:00
|
|
|
os.makedirs(dirinstall_path)
|
2013-01-30 22:16:25 +00:00
|
|
|
else:
|
|
|
|
args += ["--image", disk_img]
|
|
|
|
|
|
|
|
# Create the sparse image
|
2014-04-03 18:52:41 +00:00
|
|
|
mksparse(disk_img, disk_size * 1024**2)
|
2013-01-30 22:16:25 +00:00
|
|
|
|
2015-02-02 20:24:06 +00:00
|
|
|
log_monitor = LogMonitor(timeout=opts.timeout)
|
2015-01-09 21:13:55 +00:00
|
|
|
args += ["--remotelog", "%s:%s" % (log_monitor.host, log_monitor.port)]
|
|
|
|
|
2014-02-21 00:27:10 +00:00
|
|
|
# Make sure anaconda has the right product and release
|
2014-07-02 22:10:14 +00:00
|
|
|
log.info("Running anaconda.")
|
|
|
|
try:
|
2015-07-29 22:42:57 +00:00
|
|
|
for line in execReadlines("anaconda", args, reset_lang=False,
|
|
|
|
env_add={"ANACONDA_PRODUCTNAME": opts.project,
|
|
|
|
"ANACONDA_PRODUCTVERSION": opts.releasever},
|
|
|
|
callback=lambda p: not novirt_log_check(log_monitor.server.log_check, p)):
|
|
|
|
log.info(line)
|
2014-08-06 16:11:55 +00:00
|
|
|
|
|
|
|
# Make sure the new filesystem is correctly labeled
|
2015-10-20 21:23:33 +00:00
|
|
|
setfiles_args = ["-e", "/proc", "-e", "/sys", "-e", "/dev",
|
|
|
|
"/etc/selinux/targeted/contexts/files/file_contexts", "/"]
|
2016-04-26 21:37:19 +00:00
|
|
|
|
|
|
|
# setfiles may not be available, warn instead of fail
|
|
|
|
try:
|
|
|
|
if "--dirinstall" in args:
|
|
|
|
execWithRedirect("setfiles", setfiles_args, root=dirinstall_path)
|
|
|
|
else:
|
|
|
|
with PartitionMount(disk_img) as img_mount:
|
|
|
|
if img_mount and img_mount.mount_dir:
|
|
|
|
execWithRedirect("setfiles", setfiles_args, root=img_mount.mount_dir)
|
|
|
|
except (subprocess.CalledProcessError, OSError) as e:
|
|
|
|
log.warning("Running setfiles on install tree failed: %s", str(e))
|
|
|
|
|
2015-07-29 22:42:57 +00:00
|
|
|
except (subprocess.CalledProcessError, OSError) as e:
|
2014-07-02 22:10:14 +00:00
|
|
|
log.error("Running anaconda failed: %s", e)
|
2013-01-30 22:16:25 +00:00
|
|
|
raise InstallError("novirt_install failed")
|
2014-07-02 22:10:14 +00:00
|
|
|
finally:
|
2015-01-09 21:13:55 +00:00
|
|
|
log_monitor.shutdown()
|
|
|
|
|
2016-04-05 16:59:29 +00:00
|
|
|
# Make sure any leftover anaconda mounts have been cleaned up
|
|
|
|
anaconda_cleanup(dirinstall_path)
|
2015-07-29 22:42:57 +00:00
|
|
|
|
2014-07-02 22:10:14 +00:00
|
|
|
# Move the anaconda logs over to a log directory
|
|
|
|
log_dir = os.path.abspath(os.path.dirname(opts.logfile))
|
|
|
|
log_anaconda = joinpaths(log_dir, "anaconda")
|
|
|
|
if not os.path.isdir(log_anaconda):
|
|
|
|
os.mkdir(log_anaconda)
|
2015-01-14 19:01:23 +00:00
|
|
|
for l in glob.glob("/tmp/*log")+glob.glob("/tmp/anaconda-tb-*"):
|
2015-05-01 00:10:08 +00:00
|
|
|
shutil.copy2(l, log_anaconda)
|
|
|
|
os.unlink(l)
|
2014-07-02 22:10:14 +00:00
|
|
|
|
2016-04-21 17:38:29 +00:00
|
|
|
if not opts.make_iso and not opts.make_fsimage and not opts.make_pxe_live:
|
2014-07-02 22:10:14 +00:00
|
|
|
dm_name = os.path.splitext(os.path.basename(disk_img))[0]
|
|
|
|
dm_path = "/dev/mapper/"+dm_name
|
|
|
|
if os.path.exists(dm_path):
|
|
|
|
dm_detach(dm_path)
|
|
|
|
loop_detach(get_loop_name(disk_img))
|
|
|
|
|
2015-12-18 23:30:51 +00:00
|
|
|
# qemu disk image is used by bare qcow2 images and by Vagrant
|
|
|
|
if opts.image_type:
|
|
|
|
log.info("Converting %s to %s", disk_img, opts.image_type)
|
|
|
|
qemu_args = []
|
|
|
|
for arg in opts.qemu_args:
|
|
|
|
qemu_args += arg.split(" ", 1)
|
|
|
|
|
|
|
|
# convert the image to the selected format
|
|
|
|
if "-O" not in qemu_args:
|
|
|
|
qemu_args.extend(["-O", opts.image_type])
|
2016-01-07 22:32:48 +00:00
|
|
|
qemu_img = tempfile.mktemp(prefix="lmc-disk-", suffix=".img")
|
2015-12-18 23:30:51 +00:00
|
|
|
execWithRedirect("qemu-img", ["convert"] + qemu_args + [disk_img, qemu_img], raise_err=True)
|
2015-10-21 00:28:10 +00:00
|
|
|
if not opts.make_vagrant:
|
2015-12-18 23:30:51 +00:00
|
|
|
execWithRedirect("mv", ["-f", qemu_img, disk_img], raise_err=True)
|
2015-10-21 00:28:10 +00:00
|
|
|
else:
|
|
|
|
# Take the new qcow2 image and package it up for Vagrant
|
|
|
|
compress_args = []
|
|
|
|
for arg in opts.compress_args:
|
|
|
|
compress_args += arg.split(" ", 1)
|
|
|
|
|
2016-01-07 22:32:48 +00:00
|
|
|
vagrant_dir = tempfile.mkdtemp(prefix="lmc-tmpdir-")
|
2015-10-21 00:28:10 +00:00
|
|
|
metadata_path = joinpaths(vagrant_dir, "metadata.json")
|
2015-12-18 23:30:51 +00:00
|
|
|
execWithRedirect("mv", ["-f", qemu_img, joinpaths(vagrant_dir, "box.img")], raise_err=True)
|
2015-10-21 00:28:10 +00:00
|
|
|
if opts.vagrant_metadata:
|
|
|
|
shutil.copy2(opts.vagrant_metadata, metadata_path)
|
|
|
|
else:
|
|
|
|
create_vagrant_metadata(metadata_path)
|
|
|
|
update_vagrant_metadata(metadata_path, disk_size)
|
|
|
|
if opts.vagrantfile:
|
|
|
|
shutil.copy2(opts.vagrantfile, joinpaths(vagrant_dir, "vagrantfile"))
|
|
|
|
|
|
|
|
log.info("Creating Vagrant image")
|
|
|
|
rc = mktar(vagrant_dir, disk_img, opts.compression, compress_args, selinux=False)
|
|
|
|
if rc:
|
|
|
|
raise InstallError("novirt_install mktar failed: rc=%s" % rc)
|
|
|
|
shutil.rmtree(vagrant_dir)
|
2014-04-04 21:38:51 +00:00
|
|
|
elif opts.make_tar:
|
|
|
|
compress_args = []
|
|
|
|
for arg in opts.compress_args:
|
|
|
|
compress_args += arg.split(" ", 1)
|
|
|
|
|
2015-10-20 21:23:33 +00:00
|
|
|
rc = mktar(dirinstall_path, disk_img, opts.compression, compress_args)
|
|
|
|
shutil.rmtree(dirinstall_path)
|
|
|
|
|
|
|
|
if rc:
|
|
|
|
raise InstallError("novirt_install mktar failed: rc=%s" % rc)
|
|
|
|
elif opts.make_oci:
|
|
|
|
# An OCI image places the filesystem under /rootfs/ and adds the json files at the top
|
|
|
|
# And then creates a tar of the whole thing.
|
|
|
|
compress_args = []
|
|
|
|
for arg in opts.compress_args:
|
|
|
|
compress_args += arg.split(" ", 1)
|
|
|
|
|
|
|
|
shutil.copy2(opts.oci_config, ROOT_PATH)
|
|
|
|
shutil.copy2(opts.oci_runtime, ROOT_PATH)
|
2014-04-04 21:38:51 +00:00
|
|
|
rc = mktar(ROOT_PATH, disk_img, opts.compression, compress_args)
|
|
|
|
|
|
|
|
if rc:
|
2014-07-02 22:10:14 +00:00
|
|
|
raise InstallError("novirt_install mktar failed: rc=%s" % rc)
|
2014-03-08 02:43:14 +00:00
|
|
|
|
2013-01-30 22:16:25 +00:00
|
|
|
|
|
|
|
def virt_install(opts, install_log, disk_img, disk_size):
|
|
|
|
"""
|
2016-01-06 00:49:40 +00:00
|
|
|
Use qemu to install to a disk image
|
2014-04-02 23:56:28 +00:00
|
|
|
|
2014-05-09 18:46:32 +00:00
|
|
|
:param opts: options passed to livemedia-creator
|
|
|
|
:type opts: argparse options
|
2016-01-06 00:49:40 +00:00
|
|
|
:param str install_log: The path to write the log from qemu
|
2014-05-09 18:46:32 +00:00
|
|
|
:param str disk_img: The full path to the disk image to be created
|
|
|
|
:param int disk_size: The size of the disk_img in MiB
|
|
|
|
|
2016-01-06 00:49:40 +00:00
|
|
|
This uses qemu with a boot.iso and a kickstart to create a disk
|
2014-05-09 18:46:32 +00:00
|
|
|
image and then optionally, based on the opts passed, creates tarfile.
|
2013-01-30 22:16:25 +00:00
|
|
|
"""
|
|
|
|
iso_mount = IsoMountpoint(opts.iso, opts.location)
|
2016-01-06 00:49:40 +00:00
|
|
|
if not iso_mount.stage2:
|
|
|
|
iso_mount.umount()
|
|
|
|
raise InstallError("ISO is missing stage2, cannot continue")
|
|
|
|
|
2015-02-02 20:24:06 +00:00
|
|
|
log_monitor = LogMonitor(install_log, timeout=opts.timeout)
|
2013-01-30 22:16:25 +00:00
|
|
|
|
|
|
|
kernel_args = ""
|
|
|
|
if opts.kernel_args:
|
|
|
|
kernel_args += opts.kernel_args
|
|
|
|
if opts.proxy:
|
|
|
|
kernel_args += " proxy="+opts.proxy
|
|
|
|
|
2015-12-18 23:30:51 +00:00
|
|
|
if opts.image_type and not opts.make_fsimage:
|
|
|
|
qemu_args = []
|
|
|
|
for arg in opts.qemu_args:
|
|
|
|
qemu_args += arg.split(" ", 1)
|
|
|
|
if "-f" not in qemu_args:
|
|
|
|
qemu_args += ["-f", opts.image_type]
|
2014-03-08 02:43:14 +00:00
|
|
|
|
2015-12-18 23:30:51 +00:00
|
|
|
mkqemu_img(disk_img, disk_size*1024**2, qemu_args)
|
2014-03-08 02:43:14 +00:00
|
|
|
|
2015-10-19 22:35:50 +00:00
|
|
|
if opts.make_fsimage or opts.make_tar or opts.make_oci:
|
2016-01-07 22:32:48 +00:00
|
|
|
diskimg_path = tempfile.mktemp(prefix="lmc-disk-", suffix=".img")
|
2014-04-02 23:56:28 +00:00
|
|
|
else:
|
|
|
|
diskimg_path = disk_img
|
|
|
|
|
2014-05-29 23:54:49 +00:00
|
|
|
try:
|
2016-01-06 00:49:40 +00:00
|
|
|
QEMUInstall(opts, iso_mount, opts.ks, diskimg_path, disk_size,
|
|
|
|
kernel_args, opts.ram, opts.vcpus, opts.vnc, opts.arch,
|
|
|
|
log_check = log_monitor.server.log_check,
|
|
|
|
virtio_host = log_monitor.host,
|
|
|
|
virtio_port = log_monitor.port,
|
|
|
|
image_type=opts.image_type, boot_uefi=opts.virt_uefi,
|
|
|
|
ovmf_path=opts.ovmf_path)
|
2014-05-29 23:54:49 +00:00
|
|
|
log_monitor.shutdown()
|
|
|
|
except InstallError as e:
|
|
|
|
log.error("VirtualInstall failed: %s", e)
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
log.info("unmounting the iso")
|
|
|
|
iso_mount.umount()
|
2013-01-30 22:16:25 +00:00
|
|
|
|
|
|
|
if log_monitor.server.log_check():
|
2015-02-02 20:24:06 +00:00
|
|
|
if not log_monitor.server.error_line and opts.timeout:
|
|
|
|
msg = "virt_install failed due to timeout"
|
|
|
|
else:
|
|
|
|
msg = "virt_install failed on line: %s" % log_monitor.server.error_line
|
|
|
|
raise InstallError(msg)
|
2013-01-30 22:16:25 +00:00
|
|
|
|
2014-04-02 23:56:28 +00:00
|
|
|
if opts.make_fsimage:
|
2014-04-03 18:52:41 +00:00
|
|
|
make_fsimage(diskimg_path, disk_img, disk_size, label=opts.fs_label)
|
2014-04-02 23:56:28 +00:00
|
|
|
os.unlink(diskimg_path)
|
2014-04-04 21:38:51 +00:00
|
|
|
elif opts.make_tar:
|
|
|
|
compress_args = []
|
|
|
|
for arg in opts.compress_args:
|
|
|
|
compress_args += arg.split(" ", 1)
|
|
|
|
|
|
|
|
with PartitionMount(diskimg_path) as img_mount:
|
|
|
|
if img_mount and img_mount.mount_dir:
|
|
|
|
rc = mktar(img_mount.mount_dir, disk_img, opts.compression, compress_args)
|
2015-11-20 22:47:41 +00:00
|
|
|
else:
|
|
|
|
rc = 1
|
2014-04-04 21:38:51 +00:00
|
|
|
os.unlink(diskimg_path)
|
|
|
|
|
|
|
|
if rc:
|
|
|
|
raise InstallError("virt_install failed")
|
2015-10-19 22:35:50 +00:00
|
|
|
elif opts.make_oci:
|
|
|
|
# An OCI image places the filesystem under /rootfs/ and adds the json files at the top
|
|
|
|
# And then creates a tar of the whole thing.
|
|
|
|
compress_args = []
|
|
|
|
for arg in opts.compress_args:
|
|
|
|
compress_args += arg.split(" ", 1)
|
|
|
|
|
|
|
|
with PartitionMount(diskimg_path, submount="rootfs") as img_mount:
|
|
|
|
if img_mount and img_mount.temp_dir:
|
|
|
|
shutil.copy2(opts.oci_config, img_mount.temp_dir)
|
|
|
|
shutil.copy2(opts.oci_runtime, img_mount.temp_dir)
|
|
|
|
rc = mktar(img_mount.temp_dir, disk_img, opts.compression, compress_args)
|
2015-11-20 22:47:41 +00:00
|
|
|
else:
|
|
|
|
rc = 1
|
2015-10-19 22:35:50 +00:00
|
|
|
os.unlink(diskimg_path)
|
|
|
|
|
|
|
|
if rc:
|
|
|
|
raise InstallError("virt_install failed")
|
2015-10-19 22:30:33 +00:00
|
|
|
elif opts.make_vagrant:
|
|
|
|
compress_args = []
|
|
|
|
for arg in opts.compress_args:
|
|
|
|
compress_args += arg.split(" ", 1)
|
|
|
|
|
2016-01-07 22:32:48 +00:00
|
|
|
vagrant_dir = tempfile.mkdtemp(prefix="lmc-tmpdir-")
|
2015-10-19 22:30:33 +00:00
|
|
|
metadata_path = joinpaths(vagrant_dir, "metadata.json")
|
|
|
|
execWithRedirect("mv", ["-f", disk_img, joinpaths(vagrant_dir, "box.img")], raise_err=True)
|
|
|
|
if opts.vagrant_metadata:
|
|
|
|
shutil.copy2(opts.vagrant_metadata, metadata_path)
|
|
|
|
else:
|
|
|
|
create_vagrant_metadata(metadata_path)
|
|
|
|
update_vagrant_metadata(metadata_path, disk_size)
|
|
|
|
if opts.vagrantfile:
|
|
|
|
shutil.copy2(opts.vagrantfile, joinpaths(vagrant_dir, "vagrantfile"))
|
|
|
|
|
|
|
|
rc = mktar(vagrant_dir, disk_img, opts.compression, compress_args, selinux=False)
|
|
|
|
if rc:
|
|
|
|
raise InstallError("virt_install failed")
|
2015-10-21 00:28:10 +00:00
|
|
|
shutil.rmtree(vagrant_dir)
|
2014-04-02 23:56:28 +00:00
|
|
|
|
2013-01-30 22:16:25 +00:00
|
|
|
|
2016-03-24 22:52:29 +00:00
|
|
|
def make_squashfs(opts, disk_img, work_dir):
|
2013-01-30 22:16:25 +00:00
|
|
|
"""
|
2014-05-09 18:46:32 +00:00
|
|
|
Create a squashfs image of an unpartitioned filesystem disk image
|
|
|
|
|
|
|
|
:param str disk_img: Path to the unpartitioned filesystem disk image
|
|
|
|
:param str work_dir: Output compressed image to work_dir+images/install.img
|
|
|
|
:param str compression: Compression type to use
|
2016-03-09 22:29:18 +00:00
|
|
|
:returns: True if squashfs creation was successful. False if there was an error.
|
|
|
|
:rtype: bool
|
2014-05-09 18:46:32 +00:00
|
|
|
|
2013-01-30 22:16:25 +00:00
|
|
|
Take disk_img and put it into LiveOS/rootfs.img and squashfs this
|
2014-05-09 18:46:32 +00:00
|
|
|
tree into work_dir+images/install.img
|
2016-03-09 22:29:18 +00:00
|
|
|
|
|
|
|
fsck.ext4 is run on the disk image to make sure there are no errors and to zero
|
|
|
|
out any deleted blocks to make it compress better. If this fails for any reason
|
|
|
|
it will return False and log the error.
|
2013-01-30 22:16:25 +00:00
|
|
|
"""
|
2016-01-13 02:11:06 +00:00
|
|
|
# Make sure free blocks are actually zeroed so it will compress
|
|
|
|
rc = execWithRedirect("/usr/sbin/fsck.ext4", ["-y", "-f", "-E", "discard", disk_img])
|
|
|
|
if rc != 0:
|
2016-03-09 22:29:18 +00:00
|
|
|
log.error("Problem zeroing free blocks of %s", disk_img)
|
|
|
|
return False
|
2016-01-13 02:11:06 +00:00
|
|
|
|
2013-01-30 22:16:25 +00:00
|
|
|
liveos_dir = joinpaths(work_dir, "runtime/LiveOS")
|
|
|
|
os.makedirs(liveos_dir)
|
|
|
|
os.makedirs(os.path.dirname(joinpaths(work_dir, RUNTIME)))
|
2014-04-25 16:11:54 +00:00
|
|
|
|
|
|
|
rc = execWithRedirect("/bin/ln", [disk_img, joinpaths(liveos_dir, "rootfs.img")])
|
|
|
|
if rc != 0:
|
|
|
|
shutil.copy2(disk_img, joinpaths(liveos_dir, "rootfs.img"))
|
|
|
|
|
2016-03-24 22:52:29 +00:00
|
|
|
compression, compressargs = squashfs_args(opts)
|
2013-01-30 22:16:25 +00:00
|
|
|
mksquashfs(joinpaths(work_dir, "runtime"),
|
2016-01-13 02:11:06 +00:00
|
|
|
joinpaths(work_dir, RUNTIME), compression, compressargs)
|
2013-01-30 22:16:25 +00:00
|
|
|
remove(joinpaths(work_dir, "runtime"))
|
2016-03-09 22:29:18 +00:00
|
|
|
return True
|
2013-01-30 22:16:25 +00:00
|
|
|
|
2016-03-05 01:22:51 +00:00
|
|
|
def calculate_disk_size(opts, ks):
|
|
|
|
""" Calculate the disk size from the kickstart
|
2013-01-30 22:16:25 +00:00
|
|
|
|
2014-05-09 18:46:32 +00:00
|
|
|
:param opts: options passed to livemedia-creator
|
|
|
|
:type opts: argparse options
|
|
|
|
:param str ks: Path to the kickstart to use for the installation
|
2016-03-21 19:14:28 +00:00
|
|
|
:returns: Disk size in MiB
|
|
|
|
:rtype: int
|
2013-01-30 22:16:25 +00:00
|
|
|
"""
|
2015-08-26 22:54:44 +00:00
|
|
|
# Disk size for a filesystem image should only be the size of /
|
|
|
|
# to prevent surprises when using the same kickstart for different installations.
|
2015-12-12 23:35:16 +00:00
|
|
|
unique_partitions = dict((p.mountpoint, p) for p in ks.handler.partition.partitions)
|
2015-08-26 22:54:44 +00:00
|
|
|
if opts.no_virt and (opts.make_iso or opts.make_fsimage):
|
2015-12-12 23:35:16 +00:00
|
|
|
disk_size = 2 + sum(p.size for p in unique_partitions.values() if p.mountpoint == "/")
|
2015-08-26 22:54:44 +00:00
|
|
|
else:
|
2015-12-12 23:35:16 +00:00
|
|
|
disk_size = 2 + sum(p.size for p in unique_partitions.values())
|
2016-03-05 01:22:51 +00:00
|
|
|
log.info("Using disk size of %sMiB", disk_size)
|
|
|
|
return disk_size
|
|
|
|
|
|
|
|
def make_image(opts, ks):
|
|
|
|
"""
|
|
|
|
Install to a disk image
|
|
|
|
|
|
|
|
:param opts: options passed to livemedia-creator
|
|
|
|
:type opts: argparse options
|
|
|
|
:param str ks: Path to the kickstart to use for the installation
|
|
|
|
:returns: Path of the image created
|
|
|
|
:rtype: str
|
2013-01-30 22:16:25 +00:00
|
|
|
|
2016-03-05 01:22:51 +00:00
|
|
|
Use qemu+boot.iso or anaconda to install to a disk image.
|
|
|
|
"""
|
2013-01-30 22:16:25 +00:00
|
|
|
if opts.image_name:
|
2015-03-20 00:22:16 +00:00
|
|
|
disk_img = joinpaths(opts.result_dir, opts.image_name)
|
2013-01-30 22:16:25 +00:00
|
|
|
else:
|
2016-01-07 22:32:48 +00:00
|
|
|
disk_img = tempfile.mktemp(prefix="lmc-disk-", suffix=".img", dir=opts.result_dir)
|
2014-04-02 23:56:28 +00:00
|
|
|
log.info("disk_img = %s", disk_img)
|
2016-03-05 01:22:51 +00:00
|
|
|
disk_size = calculate_disk_size(opts, ks)
|
2013-01-30 22:16:25 +00:00
|
|
|
try:
|
|
|
|
if opts.no_virt:
|
2016-02-11 18:20:09 +00:00
|
|
|
novirt_install(opts, disk_img, disk_size)
|
2011-09-24 00:36:09 +00:00
|
|
|
else:
|
2014-04-02 23:56:28 +00:00
|
|
|
install_log = os.path.abspath(os.path.dirname(opts.logfile))+"/virt-install.log"
|
|
|
|
log.info("install_log = %s", install_log)
|
|
|
|
|
2013-01-30 22:16:25 +00:00
|
|
|
virt_install(opts, install_log, disk_img, disk_size)
|
|
|
|
except InstallError as e:
|
2015-05-01 00:10:08 +00:00
|
|
|
log.error("Install failed: %s", e)
|
2014-05-29 23:54:49 +00:00
|
|
|
if not opts.keep_image and os.path.exists(disk_img):
|
2013-01-30 22:16:25 +00:00
|
|
|
log.info("Removing bad disk image")
|
|
|
|
os.unlink(disk_img)
|
|
|
|
raise
|
2011-09-24 00:36:09 +00:00
|
|
|
|
2013-01-30 22:16:25 +00:00
|
|
|
log.info("Disk Image install successful")
|
|
|
|
return disk_img
|
2011-09-24 00:36:09 +00:00
|
|
|
|
|
|
|
|
2016-04-26 21:37:19 +00:00
|
|
|
def make_live_images(opts, work_dir, disk_img):
|
2014-07-30 15:59:27 +00:00
|
|
|
"""
|
|
|
|
Create live images from direcory or rootfs image
|
|
|
|
|
|
|
|
:param opts: options passed to livemedia-creator
|
|
|
|
:type opts: argparse options
|
|
|
|
:param str work_dir: Directory for storing results
|
2016-04-26 21:37:19 +00:00
|
|
|
:param str disk_img: Path to disk image (fsimage or partitioned)
|
2016-03-09 22:29:18 +00:00
|
|
|
:returns: Path of directory with created images or None
|
2014-07-30 15:59:27 +00:00
|
|
|
:rtype: str
|
2016-03-09 22:29:18 +00:00
|
|
|
|
|
|
|
fsck.ext4 is run on the rootfs_image to make sure there are no errors and to zero
|
|
|
|
out any deleted blocks to make it compress better. If this fails for any reason
|
|
|
|
it will return None and log the error.
|
2014-07-30 15:59:27 +00:00
|
|
|
"""
|
|
|
|
sys_root = ""
|
|
|
|
|
|
|
|
squashfs_root_dir = joinpaths(work_dir, "squashfs_root")
|
|
|
|
liveos_dir = joinpaths(squashfs_root_dir, "LiveOS")
|
|
|
|
os.makedirs(liveos_dir)
|
2016-04-26 21:37:19 +00:00
|
|
|
rootfs_img = joinpaths(liveos_dir, "rootfs.img")
|
2014-07-30 15:59:27 +00:00
|
|
|
|
2016-04-26 21:37:19 +00:00
|
|
|
if opts.fs_image or opts.no_virt:
|
|
|
|
# Find the ostree root in the fsimage
|
|
|
|
if opts.ostree:
|
|
|
|
with Mount(disk_img, opts="loop") as mnt_dir:
|
|
|
|
sys_root = find_ostree_root(mnt_dir)
|
2016-01-13 02:11:06 +00:00
|
|
|
|
2016-04-26 21:37:19 +00:00
|
|
|
# Try to hardlink the image, if that fails, copy it
|
|
|
|
rc = execWithRedirect("/bin/ln", [disk_img, rootfs_img])
|
2014-07-30 15:59:27 +00:00
|
|
|
if rc != 0:
|
2016-04-26 21:37:19 +00:00
|
|
|
shutil.copy2(disk_img, rootfs_img)
|
2014-07-30 15:59:27 +00:00
|
|
|
else:
|
2016-04-26 21:37:19 +00:00
|
|
|
is_root_part = None
|
|
|
|
if opts.ostree:
|
|
|
|
is_root_part = lambda dir: os.path.exists(dir+"/ostree/deploy")
|
|
|
|
with PartitionMount(disk_img, mount_ok=is_root_part) as img_mount:
|
|
|
|
if img_mount and img_mount.mount_dir:
|
|
|
|
try:
|
|
|
|
mounted_sysroot_boot_dir = None
|
|
|
|
if opts.ostree:
|
|
|
|
sys_root = find_ostree_root(img_mount.mount_dir)
|
|
|
|
mounted_sysroot_boot_dir = mount_boot_part_over_root(img_mount)
|
|
|
|
if opts.live_rootfs_keep_size:
|
|
|
|
size = img_mount.mount_size / 1024**3
|
|
|
|
else:
|
|
|
|
size = opts.live_rootfs_size or None
|
|
|
|
log.info("Creating live rootfs image")
|
|
|
|
mkrootfsimg(img_mount.mount_dir, rootfs_img, "LiveOS", size=size, sysroot=sys_root)
|
|
|
|
finally:
|
|
|
|
if mounted_sysroot_boot_dir:
|
|
|
|
umount(mounted_sysroot_boot_dir)
|
2016-04-27 00:25:22 +00:00
|
|
|
log.debug("sys_root = %s", sys_root)
|
2016-04-26 21:37:19 +00:00
|
|
|
|
|
|
|
# Make sure free blocks are actually zeroed so it will compress
|
|
|
|
rc = execWithRedirect("/usr/sbin/fsck.ext4", ["-y", "-f", "-E", "discard", rootfs_img])
|
|
|
|
if rc != 0:
|
|
|
|
log.error("Problem zeroing free blocks of %s", disk_img)
|
|
|
|
return None
|
2014-07-30 15:59:27 +00:00
|
|
|
|
|
|
|
log.info("Packing live rootfs image")
|
|
|
|
add_pxe_args = []
|
|
|
|
live_image_name = "live-rootfs.squashfs.img"
|
2016-03-24 22:52:29 +00:00
|
|
|
compression, compressargs = squashfs_args(opts)
|
2016-04-26 21:37:19 +00:00
|
|
|
mksquashfs(squashfs_root_dir, joinpaths(work_dir, live_image_name), compression, compressargs)
|
2014-07-30 15:59:27 +00:00
|
|
|
|
|
|
|
log.info("Rebuilding initramfs for live")
|
2016-04-27 00:25:22 +00:00
|
|
|
with Mount(rootfs_img, opts="loop") as mnt_dir:
|
|
|
|
try:
|
|
|
|
mount(joinpaths(mnt_dir, "boot"), opts="bind", mnt=joinpaths(mnt_dir, sys_root, "boot"))
|
|
|
|
rebuild_initrds_for_live(opts, joinpaths(mnt_dir, sys_root), work_dir)
|
|
|
|
finally:
|
|
|
|
umount(joinpaths(mnt_dir, sys_root, "boot"), delete=False)
|
|
|
|
|
|
|
|
remove(squashfs_root_dir)
|
2014-07-30 15:59:27 +00:00
|
|
|
|
|
|
|
if opts.ostree:
|
|
|
|
add_pxe_args.append("ostree=/%s" % sys_root)
|
|
|
|
template = joinpaths(opts.lorax_templates, "pxe-live/pxe-config.tmpl")
|
|
|
|
create_pxe_config(template, work_dir, live_image_name, add_pxe_args)
|
|
|
|
|
|
|
|
return work_dir
|
|
|
|
|
|
|
|
|
2016-03-29 00:20:49 +00:00
|
|
|
def default_image_name(compression, basename):
|
|
|
|
""" Return a default image name with the correct suffix for the compression type.
|
|
|
|
|
|
|
|
:param str compression: Compression type
|
|
|
|
:param str basename: Base filename
|
|
|
|
:returns: basename with compression suffix
|
|
|
|
|
|
|
|
If the compression is unknown it defaults to xz
|
|
|
|
"""
|
|
|
|
SUFFIXES = {"xz": ".xz", "gzip": ".gz", "bzip2": ".bz2", "lzma": ".lzma"}
|
|
|
|
return basename + SUFFIXES.get(compression, ".xz")
|
|
|
|
|
|
|
|
|
2014-05-09 00:22:14 +00:00
|
|
|
def main():
|
2016-03-18 21:24:31 +00:00
|
|
|
parser = lmc_parser()
|
2011-09-24 00:36:09 +00:00
|
|
|
opts = parser.parse_args()
|
|
|
|
|
2015-05-27 17:36:40 +00:00
|
|
|
setup_logging(opts.logfile, log)
|
2011-09-24 00:36:09 +00:00
|
|
|
|
|
|
|
log.debug( opts )
|
|
|
|
|
2016-02-11 18:50:46 +00:00
|
|
|
# Find the lorax templates
|
|
|
|
opts.lorax_templates = find_templates(opts.lorax_templates or "/usr/share/lorax")
|
|
|
|
|
2014-04-03 17:22:54 +00:00
|
|
|
# Check for invalid combinations of options, print all the errors and exit.
|
|
|
|
errors = []
|
|
|
|
if not opts.disk_image and not opts.fs_image and not opts.ks:
|
|
|
|
errors.append("Image creation requires a kickstart file")
|
|
|
|
|
|
|
|
if opts.ks and not os.path.exists(opts.ks[0]):
|
|
|
|
errors.append("kickstart file (%s) is missing." % opts.ks[0])
|
2011-09-24 00:36:09 +00:00
|
|
|
|
2016-02-11 18:50:46 +00:00
|
|
|
if opts.make_iso and not os.path.exists(opts.lorax_templates):
|
2014-04-03 17:22:54 +00:00
|
|
|
errors.append("The lorax templates directory (%s) doesn't "
|
|
|
|
"exist." % opts.lorax_templates)
|
2011-09-24 00:36:09 +00:00
|
|
|
|
|
|
|
if opts.result_dir and os.path.exists(opts.result_dir):
|
2014-04-03 17:22:54 +00:00
|
|
|
errors.append("The results_dir (%s) should not exist, please delete or "
|
|
|
|
"move its contents" % opts.result_dir)
|
2011-09-24 00:36:09 +00:00
|
|
|
|
2016-01-07 18:16:07 +00:00
|
|
|
# Default to putting results under tmp
|
|
|
|
if not opts.result_dir:
|
|
|
|
opts.result_dir = opts.tmp
|
|
|
|
|
2014-05-09 16:28:01 +00:00
|
|
|
if opts.iso and not os.path.exists(opts.iso):
|
2014-04-03 17:22:54 +00:00
|
|
|
errors.append("The iso %s is missing." % opts.iso)
|
2011-09-24 00:36:09 +00:00
|
|
|
|
2014-05-09 16:28:01 +00:00
|
|
|
if opts.disk_image and not os.path.exists(opts.disk_image):
|
2014-04-03 17:22:54 +00:00
|
|
|
errors.append("The disk image %s is missing." % opts.disk_image)
|
2011-09-24 00:36:09 +00:00
|
|
|
|
2014-05-09 16:28:01 +00:00
|
|
|
if opts.fs_image and not os.path.exists(opts.fs_image):
|
2014-04-03 17:22:54 +00:00
|
|
|
errors.append("The filesystem image %s is missing." % opts.fs_image)
|
2013-01-30 22:16:25 +00:00
|
|
|
|
|
|
|
is_install = not (opts.disk_image or opts.fs_image)
|
|
|
|
if is_install and not opts.no_virt and not opts.iso:
|
2014-04-03 17:22:54 +00:00
|
|
|
errors.append("virt install needs an install iso.")
|
2011-09-24 00:36:09 +00:00
|
|
|
|
2012-03-08 01:29:31 +00:00
|
|
|
if opts.volid and len(opts.volid) > 32:
|
2014-04-03 17:22:54 +00:00
|
|
|
errors.append("the volume id cannot be longer than 32 characters")
|
2012-03-08 01:29:31 +00:00
|
|
|
|
2013-01-30 22:16:25 +00:00
|
|
|
if is_install and not opts.no_virt \
|
2016-01-06 00:49:40 +00:00
|
|
|
and not any(glob.glob("/usr/bin/qemu-system-*")):
|
|
|
|
errors.append("qemu needs to be installed.")
|
2012-05-10 21:21:42 +00:00
|
|
|
|
2013-05-24 19:20:56 +00:00
|
|
|
if is_install and opts.no_virt \
|
2013-01-30 22:16:25 +00:00
|
|
|
and not os.path.exists("/usr/sbin/anaconda"):
|
2014-04-03 17:22:54 +00:00
|
|
|
errors.append("no-virt requires anaconda to be installed.")
|
2012-05-24 22:55:46 +00:00
|
|
|
|
2016-03-24 21:10:14 +00:00
|
|
|
if is_install and opts.no_virt:
|
|
|
|
if selinux.is_selinux_enabled() and selinux.security_getenforce():
|
|
|
|
errors.append("selinux must be disabled or in Permissive mode.")
|
|
|
|
|
2012-05-24 22:55:46 +00:00
|
|
|
if opts.make_appliance and not opts.app_template:
|
|
|
|
opts.app_template = joinpaths(opts.lorax_templates,
|
|
|
|
"appliance/libvirt.tmpl")
|
|
|
|
|
|
|
|
if opts.make_appliance and not os.path.exists(opts.app_template):
|
2014-04-03 17:22:54 +00:00
|
|
|
errors.append("The appliance template (%s) doesn't "
|
|
|
|
"exist" % opts.app_template)
|
2012-05-24 22:55:46 +00:00
|
|
|
|
2015-03-20 00:22:16 +00:00
|
|
|
if opts.image_name and os.path.exists(joinpaths(opts.result_dir, opts.image_name)):
|
2014-04-03 17:22:54 +00:00
|
|
|
errors.append("The disk image to be created should not exist.")
|
2012-05-24 22:55:46 +00:00
|
|
|
|
2015-10-19 22:30:33 +00:00
|
|
|
# Vagrant creates a qcow2 inside a tar, turn on qcow2
|
|
|
|
if opts.make_vagrant:
|
2015-12-18 23:30:51 +00:00
|
|
|
opts.image_type = "qcow2"
|
|
|
|
|
|
|
|
# Alias --qcow2 to --image-type=qcow2
|
|
|
|
if opts.qcow2:
|
|
|
|
opts.image_type = "qcow2"
|
2015-10-19 22:30:33 +00:00
|
|
|
|
2015-12-18 23:30:51 +00:00
|
|
|
if opts.image_type and not os.path.exists("/usr/bin/qemu-img"):
|
|
|
|
errors.append("image-type requires the qemu-img utility to be installed." % opts.image_type)
|
2014-03-08 02:43:14 +00:00
|
|
|
|
2015-12-18 23:30:51 +00:00
|
|
|
if opts.image_type and opts.make_iso:
|
|
|
|
errors.append("image-type cannot be used to make a bootable iso.")
|
2014-04-03 17:22:54 +00:00
|
|
|
|
2015-12-18 23:30:51 +00:00
|
|
|
if opts.image_type and opts.make_fsimage:
|
|
|
|
errors.append("image-type cannot be used to make filesystem images.")
|
2014-04-03 17:22:54 +00:00
|
|
|
|
2015-12-18 23:30:51 +00:00
|
|
|
if opts.image_type and opts.make_tar:
|
|
|
|
errors.append("image-type cannot be used to make a tar.")
|
2014-04-04 21:38:51 +00:00
|
|
|
|
2015-10-19 22:35:50 +00:00
|
|
|
if opts.make_oci and not (opts.oci_config and opts.oci_runtime):
|
|
|
|
errors.append("--make-oci requires --oci-config and --oci-runtime")
|
|
|
|
|
2015-10-20 21:23:33 +00:00
|
|
|
if opts.make_oci and not os.path.exists(opts.oci_config):
|
|
|
|
errors.append("oci % file is missing" % opts.oci_config)
|
|
|
|
|
|
|
|
if opts.make_oci and not os.path.exists(opts.oci_runtime):
|
|
|
|
errors.append("oci % file is missing" % opts.oci_runtime)
|
|
|
|
|
2015-10-21 00:28:10 +00:00
|
|
|
if opts.make_vagrant and opts.vagrant_metadata and not os.path.exists(opts.vagrant_metadata):
|
|
|
|
errors.append("Vagrant metadata file %s is missing" % opts.vagrant_metadata)
|
|
|
|
|
2015-11-04 02:03:09 +00:00
|
|
|
if opts.virt_uefi and not os.path.isdir(opts.ovmf_path):
|
|
|
|
errors.append("The OVMF firmware is missing from %s" % opts.ovmf_path)
|
|
|
|
|
2014-04-03 17:22:54 +00:00
|
|
|
if os.getuid() != 0:
|
|
|
|
errors.append("You need to run this as root")
|
2014-03-08 02:43:14 +00:00
|
|
|
|
2014-04-03 17:22:54 +00:00
|
|
|
if errors:
|
2015-05-08 15:39:06 +00:00
|
|
|
list(log.error(e) for e in errors)
|
2014-04-03 17:22:54 +00:00
|
|
|
sys.exit(1)
|
2014-04-02 23:56:28 +00:00
|
|
|
|
2016-01-07 18:16:07 +00:00
|
|
|
if not os.path.exists(opts.result_dir):
|
2015-05-08 15:39:06 +00:00
|
|
|
os.makedirs(opts.result_dir)
|
|
|
|
|
2014-04-02 23:56:28 +00:00
|
|
|
# AMI image is just a fsimage with an AMI label
|
|
|
|
if opts.make_ami:
|
|
|
|
opts.make_fsimage = True
|
|
|
|
if not opts.image_name:
|
|
|
|
opts.image_name = "ami-root.img"
|
|
|
|
if opts.fs_label == "Anaconda":
|
|
|
|
opts.fs_label = "AMI"
|
2014-04-04 21:38:51 +00:00
|
|
|
elif opts.make_tar:
|
|
|
|
if not opts.image_name:
|
2016-03-29 00:20:49 +00:00
|
|
|
opts.image_name = default_image_name(opts.compression, "root.tar")
|
2014-04-04 21:38:51 +00:00
|
|
|
if opts.compression == "xz" and not opts.compress_args:
|
|
|
|
opts.compress_args = ["-9"]
|
2015-10-19 22:35:50 +00:00
|
|
|
elif opts.make_oci:
|
|
|
|
if not opts.image_name:
|
2016-03-29 00:20:49 +00:00
|
|
|
opts.image_name = default_image_name(opts.compression, "bundle.tar")
|
2015-10-19 22:35:50 +00:00
|
|
|
if opts.compression == "xz" and not opts.compress_args:
|
|
|
|
opts.compress_args = ["-9"]
|
2015-10-19 22:30:33 +00:00
|
|
|
elif opts.make_vagrant:
|
|
|
|
if not opts.image_name:
|
2016-03-29 00:20:49 +00:00
|
|
|
opts.image_name = default_image_name(opts.compression, "vagrant.tar")
|
2015-10-19 22:30:33 +00:00
|
|
|
if opts.compression == "xz" and not opts.compress_args:
|
|
|
|
opts.compress_args = ["-9"]
|
2014-04-02 23:56:28 +00:00
|
|
|
|
2012-05-24 22:55:46 +00:00
|
|
|
if opts.app_file:
|
2015-03-20 00:22:16 +00:00
|
|
|
opts.app_file = joinpaths(opts.result_dir, opts.app_file)
|
2012-05-24 22:55:46 +00:00
|
|
|
|
2014-07-30 15:59:27 +00:00
|
|
|
if opts.make_ostree_live:
|
|
|
|
opts.make_pxe_live = True
|
|
|
|
opts.ostree = True
|
|
|
|
else:
|
|
|
|
opts.ostree = False
|
|
|
|
|
2012-05-11 20:12:17 +00:00
|
|
|
tempfile.tempdir = opts.tmp
|
2012-05-24 22:55:46 +00:00
|
|
|
disk_img = None
|
2012-05-11 20:12:17 +00:00
|
|
|
|
2012-05-24 22:55:46 +00:00
|
|
|
# Parse the kickstart
|
|
|
|
if opts.ks:
|
2011-09-24 00:36:09 +00:00
|
|
|
ks_version = makeVersion()
|
2014-05-09 16:28:01 +00:00
|
|
|
ks = KickstartParser(ks_version, errorsAreFatal=False, missingIncludeIsFatal=False)
|
|
|
|
ks.readKickstart(opts.ks[0])
|
2012-05-24 22:55:46 +00:00
|
|
|
|
2016-04-05 23:15:30 +00:00
|
|
|
# live iso usually needs dracut-live so warn the user if it is missing
|
|
|
|
if opts.ks and opts.make_iso:
|
|
|
|
if "dracut-live" not in ks.handler.packages.packageList:
|
|
|
|
log.error("dracut-live package is missing from the kickstart.")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2013-01-30 22:16:25 +00:00
|
|
|
# Make the disk or filesystem image
|
|
|
|
if not opts.disk_image and not opts.fs_image:
|
|
|
|
errors = []
|
2016-04-21 17:29:18 +00:00
|
|
|
if opts.no_virt and ks.handler.method.method not in ("url", "nfs") \
|
|
|
|
and not ks.handler.ostreesetup.seen:
|
|
|
|
errors.append("Only url, nfs and ostreesetup install methods are currently supported."
|
|
|
|
"Please fix your kickstart file." )
|
2011-09-24 00:36:09 +00:00
|
|
|
|
2015-12-01 08:34:00 +00:00
|
|
|
if ks.handler.method.method in ("url", "nfs") and not ks.handler.network.seen:
|
|
|
|
errors.append("The kickstart must activate networking if "
|
|
|
|
"the url or nfs install method is used.")
|
|
|
|
|
2013-01-30 22:16:25 +00:00
|
|
|
if ks.handler.displaymode.displayMode is not None:
|
|
|
|
errors.append("The kickstart must not set a display mode (text, cmdline, "
|
|
|
|
"graphical), this will interfere with livemedia-creator.")
|
2011-09-24 00:36:09 +00:00
|
|
|
|
2016-04-21 17:29:18 +00:00
|
|
|
if opts.make_fsimage or (opts.make_pxe_live and opts.no_virt):
|
2014-04-03 17:45:06 +00:00
|
|
|
# Make sure the kickstart isn't using autopart and only has a / mountpoint
|
|
|
|
part_ok = not any(p for p in ks.handler.partition.partitions
|
|
|
|
if p.mountpoint not in ["/", "swap"])
|
|
|
|
if not part_ok or ks.handler.autopart.seen:
|
|
|
|
errors.append("Filesystem images must use a single / part, not autopart or "
|
|
|
|
"multiple partitions. swap is allowed but not used.")
|
2014-04-02 23:56:28 +00:00
|
|
|
|
2015-11-16 22:07:32 +00:00
|
|
|
if not opts.no_virt and ks.handler.reboot.action != KS_SHUTDOWN:
|
|
|
|
errors.append("The kickstart must include shutdown when using virt installation.")
|
|
|
|
|
2013-01-30 22:16:25 +00:00
|
|
|
if errors:
|
2015-05-08 15:39:06 +00:00
|
|
|
list(log.error(e) for e in errors)
|
2013-01-30 22:16:25 +00:00
|
|
|
sys.exit(1)
|
2011-12-09 01:12:06 +00:00
|
|
|
|
2013-01-30 22:16:25 +00:00
|
|
|
# Make the image. Output of this is either a partitioned disk image or a fsimage
|
|
|
|
try:
|
|
|
|
disk_img = make_image(opts, ks)
|
|
|
|
except InstallError as e:
|
2014-05-09 00:22:14 +00:00
|
|
|
log.error("ERROR: Image creation failed: %s", e)
|
2013-01-30 22:16:25 +00:00
|
|
|
sys.exit(1)
|
2012-05-12 00:35:16 +00:00
|
|
|
|
2013-01-30 22:16:25 +00:00
|
|
|
if not opts.image_only:
|
|
|
|
result_dir = None
|
|
|
|
if opts.make_iso:
|
2016-01-07 22:32:48 +00:00
|
|
|
work_dir = tempfile.mkdtemp(prefix="lmc-work-")
|
2015-05-01 00:10:08 +00:00
|
|
|
log.info("working dir is %s", work_dir)
|
2013-01-30 22:16:25 +00:00
|
|
|
|
|
|
|
if (opts.fs_image or opts.no_virt) and not opts.disk_image:
|
|
|
|
# Create iso from a filesystem image
|
|
|
|
disk_img = opts.fs_image or disk_img
|
|
|
|
|
2016-03-24 22:52:29 +00:00
|
|
|
if not make_squashfs(opts, disk_img, work_dir):
|
2016-03-09 22:29:18 +00:00
|
|
|
log.error("squashfs.img creation failed")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2013-01-30 22:16:25 +00:00
|
|
|
with Mount(disk_img, opts="loop") as mount_dir:
|
|
|
|
result_dir = make_livecd(opts, mount_dir, work_dir)
|
|
|
|
else:
|
|
|
|
# Create iso from a partitioned disk image
|
|
|
|
disk_img = opts.disk_image or disk_img
|
|
|
|
with PartitionMount(disk_img) as img_mount:
|
|
|
|
if img_mount and img_mount.mount_dir:
|
2016-03-21 19:14:28 +00:00
|
|
|
make_runtime(opts, img_mount.mount_dir, work_dir, calculate_disk_size(opts, ks)/1024.0)
|
2013-01-30 22:16:25 +00:00
|
|
|
result_dir = make_livecd(opts, img_mount.mount_dir, work_dir)
|
|
|
|
|
2016-01-07 18:16:07 +00:00
|
|
|
# --iso-only removes the extra build artifacts, keeping only the boot.iso
|
|
|
|
if opts.iso_only and result_dir:
|
|
|
|
boot_iso = joinpaths(result_dir, "images/boot.iso")
|
|
|
|
if not os.path.exists(boot_iso):
|
|
|
|
log.error("%s is missing, skipping --iso-only.", boot_iso)
|
|
|
|
else:
|
|
|
|
iso_dir = tempfile.mkdtemp(prefix="lmc-result-")
|
2016-01-11 17:54:40 +00:00
|
|
|
dest_file = joinpaths(iso_dir, opts.iso_name or "boot.iso")
|
2016-01-07 18:16:07 +00:00
|
|
|
shutil.move(boot_iso, dest_file)
|
|
|
|
shutil.rmtree(result_dir)
|
|
|
|
result_dir = iso_dir
|
|
|
|
|
2013-01-30 22:16:25 +00:00
|
|
|
# cleanup the mess
|
|
|
|
# cleanup work_dir?
|
|
|
|
if disk_img and not (opts.keep_image or opts.disk_image or opts.fs_image):
|
|
|
|
os.unlink(disk_img)
|
|
|
|
log.info("Disk image erased")
|
|
|
|
disk_img = None
|
|
|
|
elif opts.make_appliance:
|
|
|
|
if not opts.ks:
|
|
|
|
networks = []
|
|
|
|
else:
|
|
|
|
networks = ks.handler.network.network
|
|
|
|
make_appliance(opts.disk_image or disk_img, opts.app_name,
|
|
|
|
opts.app_template, opts.app_file, networks, opts.ram,
|
2015-11-13 21:46:08 +00:00
|
|
|
opts.vcpus or 1, opts.arch, opts.title, opts.project, opts.releasever)
|
2014-07-30 15:59:27 +00:00
|
|
|
elif opts.make_pxe_live:
|
2016-01-07 22:32:48 +00:00
|
|
|
work_dir = tempfile.mkdtemp(prefix="lmc-work-")
|
2015-05-01 00:10:08 +00:00
|
|
|
log.info("working dir is %s", work_dir)
|
2016-04-26 21:37:19 +00:00
|
|
|
disk_img = opts.fs_image or disk_img
|
|
|
|
log.debug("disk image is %s", disk_img)
|
2014-07-30 15:59:27 +00:00
|
|
|
|
2016-04-26 21:37:19 +00:00
|
|
|
result_dir = make_live_images(opts, work_dir, disk_img)
|
2016-03-09 22:29:18 +00:00
|
|
|
if result_dir is None:
|
2016-04-26 21:37:19 +00:00
|
|
|
log.error("Creating PXE live image failed.")
|
2016-03-09 22:29:18 +00:00
|
|
|
sys.exit(1)
|
2013-01-30 22:16:25 +00:00
|
|
|
|
2015-03-20 00:22:16 +00:00
|
|
|
if opts.result_dir != opts.tmp and result_dir:
|
2016-01-07 18:16:07 +00:00
|
|
|
copytree(result_dir, opts.result_dir, preserve=False)
|
2014-05-09 16:28:01 +00:00
|
|
|
shutil.rmtree(result_dir)
|
2016-01-07 18:16:07 +00:00
|
|
|
result_dir = None
|
2011-09-24 00:36:09 +00:00
|
|
|
|
|
|
|
log.info("SUMMARY")
|
|
|
|
log.info("-------")
|
2015-05-01 00:10:08 +00:00
|
|
|
log.info("Logs are in %s", os.path.abspath(os.path.dirname(opts.logfile)))
|
2012-05-24 22:55:46 +00:00
|
|
|
if disk_img:
|
2015-05-01 00:10:08 +00:00
|
|
|
log.info("Disk image is at %s", disk_img)
|
2012-05-24 22:55:46 +00:00
|
|
|
if opts.make_appliance:
|
2015-05-01 00:10:08 +00:00
|
|
|
log.info("Appliance description is in %s", opts.app_file)
|
2016-01-07 18:16:07 +00:00
|
|
|
log.info("Results are in %s", result_dir or opts.result_dir)
|
2011-09-24 00:36:09 +00:00
|
|
|
|
|
|
|
sys.exit( 0 )
|
|
|
|
|
2014-05-09 00:22:14 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|