2009-08-27 07:19:49 +00:00
|
|
|
#
|
|
|
|
# __init__.py
|
2010-01-12 11:45:54 +00:00
|
|
|
#
|
2010-10-12 16:23:29 +00:00
|
|
|
# Copyright (C) 2010 Red Hat, Inc.
|
2010-01-12 11:45:54 +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/>.
|
|
|
|
#
|
2010-02-23 13:20:05 +00:00
|
|
|
# Red Hat Author(s): Martin Gracik <mgracik@redhat.com>
|
|
|
|
# David Cantrell <dcantrell@redhat.com>
|
2011-05-09 23:06:25 +00:00
|
|
|
# Will Woods <wwoods@redhat.com>
|
2009-08-27 07:19:49 +00:00
|
|
|
|
2010-10-12 16:23:29 +00:00
|
|
|
# set up logging
|
|
|
|
import logging
|
2011-01-19 14:37:44 +00:00
|
|
|
logger = logging.getLogger("pylorax")
|
2010-10-12 16:23:29 +00:00
|
|
|
|
|
|
|
sh = logging.StreamHandler()
|
|
|
|
sh.setLevel(logging.INFO)
|
2011-01-19 14:37:44 +00:00
|
|
|
logger.addHandler(sh)
|
2010-10-12 16:23:29 +00:00
|
|
|
|
|
|
|
|
2009-04-22 13:01:28 +00:00
|
|
|
import sys
|
2008-09-13 02:04:02 +00:00
|
|
|
import os
|
2009-09-23 10:21:33 +00:00
|
|
|
import ConfigParser
|
2010-10-12 16:23:29 +00:00
|
|
|
import tempfile
|
2008-09-13 02:04:02 +00:00
|
|
|
|
2010-11-23 12:29:11 +00:00
|
|
|
from base import BaseLoraxClass, DataHolder
|
2009-09-23 10:21:33 +00:00
|
|
|
import output
|
2009-06-04 13:36:56 +00:00
|
|
|
|
2010-10-12 16:23:29 +00:00
|
|
|
import yum
|
|
|
|
import yumhelper
|
|
|
|
import ltmpl
|
|
|
|
|
2011-05-04 22:51:58 +00:00
|
|
|
import imgutils
|
2010-10-12 16:23:29 +00:00
|
|
|
import constants
|
2010-02-23 13:20:05 +00:00
|
|
|
from sysutils import *
|
2009-06-04 13:36:56 +00:00
|
|
|
|
2011-05-09 20:57:41 +00:00
|
|
|
from treebuilder import TreeBuilder
|
2010-11-23 10:14:25 +00:00
|
|
|
from installtree import LoraxInstallTree
|
|
|
|
from buildstamp import BuildStamp
|
|
|
|
from treeinfo import TreeInfo
|
|
|
|
from discinfo import DiscInfo
|
|
|
|
|
2011-04-27 20:25:35 +00:00
|
|
|
class ArchData(object):
|
|
|
|
lib64_arches = ("x86_64", "ppc64", "sparc64", "s390x", "ia64")
|
|
|
|
archmap = {"i386": "i386", "i586":"i386", "i686":"i386", "x86_64":"x86_64",
|
|
|
|
"ppc":"ppc", "ppc64": "ppc",
|
|
|
|
"sparc":"sparc", "sparcv9":"sparc", "sparc64":"sparc",
|
|
|
|
"s390":"s390", "s390x":"s390x",
|
|
|
|
}
|
|
|
|
def __init__(self, buildarch):
|
|
|
|
self.buildarch = buildarch
|
|
|
|
self.basearch = self.archmap.get(buildarch) or buildarch
|
|
|
|
self.libdir = "lib64" if buildarch in self.lib64_arches else "lib"
|
2010-08-17 12:14:36 +00:00
|
|
|
|
2010-02-23 13:20:05 +00:00
|
|
|
class Lorax(BaseLoraxClass):
|
2009-09-23 10:21:33 +00:00
|
|
|
|
2010-10-12 16:23:29 +00:00
|
|
|
def __init__(self):
|
2010-02-23 13:20:05 +00:00
|
|
|
BaseLoraxClass.__init__(self)
|
2010-10-12 16:23:29 +00:00
|
|
|
self._configured = False
|
|
|
|
|
|
|
|
def configure(self, conf_file="/etc/lorax/lorax.conf"):
|
|
|
|
self.conf = ConfigParser.SafeConfigParser()
|
|
|
|
|
|
|
|
# set defaults
|
|
|
|
self.conf.add_section("lorax")
|
|
|
|
self.conf.set("lorax", "debug", "1")
|
|
|
|
self.conf.set("lorax", "sharedir", "/usr/share/lorax")
|
|
|
|
|
|
|
|
self.conf.add_section("output")
|
|
|
|
self.conf.set("output", "colors", "1")
|
|
|
|
self.conf.set("output", "encoding", "utf-8")
|
|
|
|
self.conf.set("output", "ignorelist", "/usr/share/lorax/ignorelist")
|
2009-09-23 10:21:33 +00:00
|
|
|
|
2010-10-12 16:23:29 +00:00
|
|
|
self.conf.add_section("templates")
|
2010-10-27 09:23:47 +00:00
|
|
|
self.conf.set("templates", "ramdisk", "ramdisk.ltmpl")
|
2010-10-12 16:23:29 +00:00
|
|
|
|
2011-04-21 15:06:47 +00:00
|
|
|
self.conf.add_section("yum")
|
|
|
|
self.conf.set("yum", "skipbroken", "0")
|
|
|
|
|
|
|
|
self.conf.add_section("compression")
|
|
|
|
self.conf.set("compression", "type", "xz")
|
|
|
|
self.conf.set("compression", "speed", "9")
|
|
|
|
|
2010-10-12 16:23:29 +00:00
|
|
|
# read the config file
|
|
|
|
if os.path.isfile(conf_file):
|
|
|
|
self.conf.read(conf_file)
|
|
|
|
|
|
|
|
# set up the output
|
|
|
|
debug = self.conf.getboolean("lorax", "debug")
|
|
|
|
output_level = output.DEBUG if debug else output.INFO
|
|
|
|
|
|
|
|
colors = self.conf.getboolean("output", "colors")
|
|
|
|
encoding = self.conf.get("output", "encoding")
|
|
|
|
|
|
|
|
self.output.basic_config(output_level=output_level,
|
|
|
|
colors=colors, encoding=encoding)
|
|
|
|
|
|
|
|
ignorelist = self.conf.get("output", "ignorelist")
|
|
|
|
if os.path.isfile(ignorelist):
|
|
|
|
with open(ignorelist, "r") as fobj:
|
|
|
|
for line in fobj:
|
|
|
|
line = line.strip()
|
|
|
|
if line and not line.startswith("#"):
|
|
|
|
self.output.ignore(line)
|
|
|
|
|
2010-12-02 11:59:08 +00:00
|
|
|
# cron does not have sbin in PATH,
|
2010-11-09 08:46:58 +00:00
|
|
|
# so we have to add it ourselves
|
|
|
|
os.environ["PATH"] = "{0}:/sbin:/usr/sbin".format(os.environ["PATH"])
|
|
|
|
|
2010-10-12 16:23:29 +00:00
|
|
|
self._configured = True
|
|
|
|
|
2011-01-19 14:37:44 +00:00
|
|
|
def init_file_logging(self, logdir, logname="pylorax.log"):
|
|
|
|
fh = logging.FileHandler(filename=joinpaths(logdir, logname), mode="w")
|
|
|
|
fh.setLevel(logging.DEBUG)
|
|
|
|
logger.addHandler(fh)
|
|
|
|
|
2010-12-02 11:59:08 +00:00
|
|
|
def run(self, ybo, product, version, release, variant="", bugurl="",
|
2010-11-08 12:52:11 +00:00
|
|
|
is_beta=False, workdir=None, outputdir=None):
|
2010-10-12 16:23:29 +00:00
|
|
|
|
|
|
|
assert self._configured
|
|
|
|
|
2011-01-19 14:37:44 +00:00
|
|
|
# set up work directory
|
|
|
|
self.workdir = workdir or tempfile.mkdtemp(prefix="pylorax.work.")
|
|
|
|
if not os.path.isdir(self.workdir):
|
|
|
|
os.makedirs(self.workdir)
|
|
|
|
|
|
|
|
# set up log directory
|
|
|
|
logdir = joinpaths(self.workdir, "log")
|
|
|
|
if not os.path.isdir(logdir):
|
|
|
|
os.makedirs(logdir)
|
|
|
|
|
|
|
|
self.init_file_logging(logdir)
|
|
|
|
logger.debug("using work directory {0.workdir}".format(self))
|
|
|
|
logger.debug("using log directory {0}".format(logdir))
|
|
|
|
|
|
|
|
# set up output directory
|
|
|
|
self.outputdir = outputdir or tempfile.mkdtemp(prefix="pylorax.out.")
|
|
|
|
if not os.path.isdir(self.outputdir):
|
|
|
|
os.makedirs(self.outputdir)
|
|
|
|
logger.debug("using output directory {0.outputdir}".format(self))
|
|
|
|
|
2010-10-12 16:23:29 +00:00
|
|
|
# do we have root privileges?
|
|
|
|
logger.info("checking for root privileges")
|
|
|
|
if not os.geteuid() == 0:
|
2010-08-17 12:14:36 +00:00
|
|
|
logger.critical("no root privileges")
|
|
|
|
sys.exit(1)
|
2009-09-23 10:21:33 +00:00
|
|
|
|
2010-12-02 11:59:08 +00:00
|
|
|
# do we have all lorax required commands?
|
2010-10-12 16:23:29 +00:00
|
|
|
self.lcmds = constants.LoraxRequiredCommands()
|
2011-04-27 19:04:13 +00:00
|
|
|
# TODO: actually check for required commands
|
2010-10-12 16:23:29 +00:00
|
|
|
|
|
|
|
# do we have a proper yum base object?
|
|
|
|
logger.info("checking yum base object")
|
2010-12-02 11:59:08 +00:00
|
|
|
if not isinstance(ybo, yum.YumBase):
|
2010-08-17 12:14:36 +00:00
|
|
|
logger.critical("no yum base object")
|
|
|
|
sys.exit(1)
|
2009-09-23 10:21:33 +00:00
|
|
|
|
2010-10-12 16:23:29 +00:00
|
|
|
logger.info("setting up yum helper")
|
2010-12-02 11:59:08 +00:00
|
|
|
self.yum = yumhelper.LoraxYumHelper(ybo)
|
2010-10-12 16:23:29 +00:00
|
|
|
logger.debug("using install root: {0}".format(self.yum.installroot))
|
|
|
|
|
|
|
|
logger.info("setting up build architecture")
|
2011-04-27 20:25:35 +00:00
|
|
|
self.arch = ArchData(self.get_buildarch())
|
|
|
|
for attr in ('buildarch', 'basearch', 'libdir'):
|
|
|
|
logger.debug("self.arch.%s = %s", attr, getattr(self.arch,attr))
|
2010-08-17 12:14:36 +00:00
|
|
|
|
2010-10-12 16:23:29 +00:00
|
|
|
logger.info("setting up install tree")
|
2011-04-28 20:08:50 +00:00
|
|
|
self.installtree = LoraxInstallTree(self.yum, self.arch.libdir)
|
2008-09-13 02:04:02 +00:00
|
|
|
|
2010-10-12 16:23:29 +00:00
|
|
|
logger.info("setting up build parameters")
|
2011-04-27 20:25:35 +00:00
|
|
|
product = DataHolder(name=product, version=version, release=release,
|
|
|
|
variant=variant, bugurl=bugurl, is_beta=is_beta)
|
2010-02-23 13:20:05 +00:00
|
|
|
self.product = product
|
2011-04-27 20:25:35 +00:00
|
|
|
logger.debug("product data: %s" % product)
|
2010-10-12 16:23:29 +00:00
|
|
|
|
2011-05-04 22:51:58 +00:00
|
|
|
logger.info("parsing the runtime template")
|
2010-10-27 09:23:47 +00:00
|
|
|
tfile = joinpaths(self.conf.get("lorax", "sharedir"),
|
|
|
|
self.conf.get("templates", "ramdisk"))
|
2010-10-12 16:23:29 +00:00
|
|
|
|
2011-04-27 20:25:35 +00:00
|
|
|
# TODO: normalize with arch templates:
|
|
|
|
# tvars = dict(product=product, arch=arch)
|
|
|
|
tvars = { "basearch": self.arch.basearch,
|
|
|
|
"buildarch": self.arch.buildarch,
|
|
|
|
"libdir" : self.arch.libdir,
|
|
|
|
"product": self.product.name.lower() }
|
2010-10-12 16:23:29 +00:00
|
|
|
|
|
|
|
template = ltmpl.LoraxTemplate()
|
2011-04-27 20:37:19 +00:00
|
|
|
template.parse(tfile, tvars)
|
2010-10-12 16:23:29 +00:00
|
|
|
|
2011-01-31 19:57:07 +00:00
|
|
|
logger.info("creating tree directories")
|
2011-04-27 20:37:19 +00:00
|
|
|
for d in template.getdata("mkdir"):
|
2011-01-31 19:57:07 +00:00
|
|
|
os.makedirs(joinpaths(self.installtree.root, d))
|
|
|
|
|
2010-10-12 16:23:29 +00:00
|
|
|
# install packages
|
2011-04-27 20:37:19 +00:00
|
|
|
logger.info("getting list of required packages")
|
|
|
|
for package in template.getdata("install"):
|
2010-10-12 16:23:29 +00:00
|
|
|
self.installtree.yum.install(package)
|
2011-04-21 15:06:47 +00:00
|
|
|
|
|
|
|
skipbroken = self.conf.getboolean("yum", "skipbroken")
|
|
|
|
self.installtree.yum.process_transaction(skipbroken)
|
2010-10-12 16:23:29 +00:00
|
|
|
|
2010-11-08 12:52:11 +00:00
|
|
|
# write .buildstamp
|
2011-04-28 18:10:31 +00:00
|
|
|
buildstamp = BuildStamp(self.product.name, self.product.version,
|
2011-04-27 20:25:35 +00:00
|
|
|
self.product.bugurl, self.product.is_beta, self.arch.buildarch)
|
2010-11-08 12:52:11 +00:00
|
|
|
|
2011-04-28 18:10:31 +00:00
|
|
|
buildstamp.write(joinpaths(self.installtree.root, ".buildstamp"))
|
2010-11-03 13:11:08 +00:00
|
|
|
|
2011-04-28 16:31:39 +00:00
|
|
|
logger.debug("saving pkglists to %s", self.workdir)
|
2010-12-06 09:52:39 +00:00
|
|
|
dname = joinpaths(self.workdir, "pkglists")
|
|
|
|
os.makedirs(dname)
|
|
|
|
for pkgname, pkgobj in self.installtree.yum.installed_packages.items():
|
|
|
|
with open(joinpaths(dname, pkgname), "w") as fobj:
|
|
|
|
for fname in pkgobj.filelist:
|
|
|
|
fobj.write("{0}\n".format(fname))
|
2010-11-03 12:40:03 +00:00
|
|
|
|
2010-10-12 16:23:29 +00:00
|
|
|
logger.info("removing locales")
|
|
|
|
self.installtree.remove_locales()
|
|
|
|
|
|
|
|
logger.info("creating keymaps")
|
2011-04-27 22:06:28 +00:00
|
|
|
if self.arch.basearch not in ("s390", "s390x"):
|
|
|
|
self.installtree.create_keymaps(basearch=self.arch.basearch)
|
2010-10-12 16:23:29 +00:00
|
|
|
|
|
|
|
logger.info("creating screenfont")
|
2011-04-27 22:06:28 +00:00
|
|
|
self.installtree.create_screenfont(basearch=self.arch.basearch)
|
2010-10-12 16:23:29 +00:00
|
|
|
|
|
|
|
logger.info("moving stubs")
|
|
|
|
self.installtree.move_stubs()
|
|
|
|
|
|
|
|
logger.info("getting list of required modules")
|
2011-04-27 20:37:19 +00:00
|
|
|
# Need a list to pass to cleanup_kernel_modules, not a generator
|
|
|
|
modules = list(template.getdata("module"))
|
2010-11-12 12:00:05 +00:00
|
|
|
|
2011-04-28 16:31:39 +00:00
|
|
|
self.installtree.install_kernel_modules(modules)
|
2010-10-12 16:23:29 +00:00
|
|
|
|
2010-11-08 12:52:11 +00:00
|
|
|
logger.info("moving anaconda repos")
|
2010-10-19 15:35:50 +00:00
|
|
|
self.installtree.move_repos()
|
|
|
|
|
2010-11-08 12:52:11 +00:00
|
|
|
logger.info("creating depmod.conf")
|
2010-10-19 15:35:50 +00:00
|
|
|
self.installtree.create_depmod_conf()
|
|
|
|
|
|
|
|
# misc tree modifications
|
2011-04-27 22:06:28 +00:00
|
|
|
if self.arch.basearch in ("s390", "s390x"):
|
|
|
|
# TODO: move this to the arch template
|
|
|
|
self.installtree.misc_s390_modifications()
|
2010-10-19 15:35:50 +00:00
|
|
|
self.installtree.misc_tree_modifications()
|
|
|
|
|
|
|
|
# get config files
|
|
|
|
config_dir = joinpaths(self.conf.get("lorax", "sharedir"),
|
|
|
|
"config_files")
|
|
|
|
|
|
|
|
self.installtree.get_config_files(config_dir)
|
2010-11-08 12:52:11 +00:00
|
|
|
self.installtree.setup_sshd(config_dir)
|
2011-04-27 22:06:28 +00:00
|
|
|
if self.arch.basearch in ("s390", "s390x"):
|
|
|
|
self.installtree.generate_ssh_keys()
|
2010-10-19 15:35:50 +00:00
|
|
|
|
2010-10-22 14:02:53 +00:00
|
|
|
# get anaconda portions
|
|
|
|
self.installtree.get_anaconda_portions()
|
2010-10-19 15:35:50 +00:00
|
|
|
|
2010-11-03 13:11:08 +00:00
|
|
|
# write .discinfo
|
2011-04-28 18:10:31 +00:00
|
|
|
discinfo = DiscInfo(self.product.release, self.arch.basearch)
|
|
|
|
discinfo.write(joinpaths(self.outputdir, ".discinfo"))
|
2011-03-07 16:15:27 +00:00
|
|
|
|
2011-05-09 23:06:25 +00:00
|
|
|
logger.info("backing up installroot")
|
|
|
|
installroot = joinpaths(self.workdir, "installroot")
|
|
|
|
linktree(self.installtree.root, installroot)
|
2010-11-08 12:52:11 +00:00
|
|
|
|
2010-10-12 16:23:29 +00:00
|
|
|
logger.info("getting list of not required packages")
|
2011-05-04 22:51:58 +00:00
|
|
|
removepkgs = template.getdata("remove", mode="lines")
|
|
|
|
self.installtree.remove_packages(removepkgs)
|
2010-10-12 16:23:29 +00:00
|
|
|
|
2010-10-22 14:02:53 +00:00
|
|
|
logger.info("cleaning up python files")
|
|
|
|
self.installtree.cleanup_python_files()
|
|
|
|
|
2011-04-28 20:41:24 +00:00
|
|
|
logger.info("creating the runtime image")
|
2011-05-04 22:51:58 +00:00
|
|
|
# TODO: different img styles / create_runtime implementations
|
|
|
|
runtimedir = joinpaths(self.workdir, "runtime")
|
|
|
|
# FIXME: compression options (type, speed, etc.)
|
|
|
|
create_runtime(self.installtree.root, runtimedir)
|
|
|
|
|
|
|
|
logger.info("preparing to build output tree and boot images")
|
2011-05-09 21:34:48 +00:00
|
|
|
treebuilder = TreeBuilder(self.product, self.arch,
|
2011-05-10 03:45:51 +00:00
|
|
|
installroot, self.outputdir,
|
|
|
|
templatedir=self.conf.get("lorax", "sharedir"))
|
2011-05-04 22:51:58 +00:00
|
|
|
|
|
|
|
# TODO: different image styles may do this part differently
|
|
|
|
logger.info("rebuilding initramfs images")
|
|
|
|
treebuilder.rebuild_initrds(add_args=["--xz", "--add", "btrfs"])
|
|
|
|
|
|
|
|
# TODO: keep small initramfs for split initramfs/runtime media
|
|
|
|
logger.info("adding runtime to initrds")
|
|
|
|
treebuilder.initrd_append(runtimedir)
|
|
|
|
|
|
|
|
logger.info("populating output tree and building boot images")
|
2011-04-28 20:41:24 +00:00
|
|
|
treebuilder.build()
|
2011-05-04 22:51:58 +00:00
|
|
|
|
|
|
|
logger.info("writing .treeinfo")
|
|
|
|
treeinfo = TreeInfo(self.product.name, self.product.version,
|
|
|
|
self.product.variant, self.arch.basearch)
|
2011-05-10 19:46:25 +00:00
|
|
|
for section, data in treebuilder.treeinfo_data.items():
|
2011-04-28 20:41:24 +00:00
|
|
|
treeinfo.add_section(section, data)
|
2011-04-28 18:10:31 +00:00
|
|
|
treeinfo.write(joinpaths(self.outputdir, ".treeinfo"))
|
2010-11-03 13:11:08 +00:00
|
|
|
|
2010-10-12 16:23:29 +00:00
|
|
|
def get_buildarch(self):
|
|
|
|
# get architecture of the available anaconda package
|
2010-12-02 11:59:08 +00:00
|
|
|
_, available = self.yum.search("anaconda")
|
2009-12-15 14:26:01 +00:00
|
|
|
|
2010-10-12 16:23:29 +00:00
|
|
|
if available:
|
2010-11-08 15:09:04 +00:00
|
|
|
anaconda = available.pop(0)
|
|
|
|
# src is not a real arch
|
|
|
|
if anaconda.arch == "src":
|
|
|
|
anaconda = available.pop(0)
|
2010-10-12 16:23:29 +00:00
|
|
|
buildarch = anaconda.arch
|
|
|
|
else:
|
|
|
|
# fallback to the system architecture
|
|
|
|
logger.warning("using system architecture")
|
|
|
|
buildarch = os.uname()[4]
|
2010-02-23 13:20:05 +00:00
|
|
|
|
2010-10-12 16:23:29 +00:00
|
|
|
return buildarch
|
2011-05-04 22:51:58 +00:00
|
|
|
|
|
|
|
def create_runtime(inroot, outdir):
|
|
|
|
runtime = "squashfs.img"
|
|
|
|
# these exact paths are required by dracut
|
|
|
|
livedir = "LiveOS"
|
|
|
|
rootfs = "rootfs.img"
|
|
|
|
cmdline = "etc/cmdline"
|
|
|
|
# make live rootfs image
|
|
|
|
fssize = 2 * (1024*1024*1024) # 2GB sparse file compresses down to nothin'
|
|
|
|
livedir = joinpaths(outdir, livedir)
|
|
|
|
os.makedirs(livedir)
|
|
|
|
imgutils.mkext4img(inroot, joinpaths(livedir, rootfs),
|
|
|
|
label="Anaconda", size=fssize)
|
|
|
|
# squash the live image
|
|
|
|
imgutils.mksquashfs(livedir, joinpaths(outdir, runtime),
|
|
|
|
compressargs=["-no-progress"])
|
|
|
|
remove(livedir)
|
|
|
|
# make the cmdline file
|
|
|
|
os.makedirs(joinpaths(outdir, os.path.dirname(cmdline)))
|
|
|
|
with open(joinpaths(outdir, cmdline), "w") as fobj:
|
|
|
|
fobj.write("root=live:/%s\n" % runtime)
|
|
|
|
return (runtime, cmdline)
|