pmbootstrap/pmb/chroot/init.py

114 lines
4.2 KiB
Python
Raw Normal View History

2021-01-07 22:30:30 +00:00
# Copyright 2021 Oliver Smith
# SPDX-License-Identifier: GPL-3.0-or-later
2017-05-26 20:08:45 +00:00
import logging
import os
import glob
import filecmp
import pmb.chroot
import pmb.chroot.apk_static
import pmb.config
import pmb.config.workdir
Properly rebuild/install packages when something changed (Fix #120, #108, #131) (#129) TLDR: Always rebuild/install packages when something changed when executing "pmbootstrap install/initfs/flash", more speed in dependency resolution. --- pmbootstrap has already gotten some support for "timestamp based rebuilds", which modifies the logic for when packages should be rebuilt. It doesn't only consider packages outdated with old pkgver/pkgrel combinations, but also packages, where a source file has a newer timestamp, than the built package has. I've found out, that this can lead to more rebuilds than expected. For example, when you check out the pmbootstrap git repository again into another folder, although you have already built packages. Then all files have the timestamp of the checkout, and the packages will appear to be outdated. While this is not largely a concern now, this will become a problem once we have a binary package repository, because then the packages from the binary repo will always seem to be outdated, if you just freshly checked out the repository. To combat this, git gets asked if the files from the aport we're looking at are in sync with upstream, or not. Only when the files are not in sync with upstream and the timestamps of the sources are newer, a rebuild gets triggered from now on. In case this logic should fail, I've added an option during "pmbootstrap init" where you can enable or disable the "timestamp based rebuilds" option. In addition to that, this commit also works on fixing #120: packages do not get updated in "pmbootstrap install" after they have been rebuilt. For this to work, we specify all packages explicitly for abuild, instead of letting abuild do the resolving. This feature will also work with the "timestamp based rebuilds". This commit also fixes the working_dir argument in pmb.helpers.run.user, which was simply ignored before. Finally, the performance of the dependency resolution is faster again (when compared to the current version in master), because the parsed apkbuilds and finding the aport by pkgname gets cached during one pmbootstrap call (in args.cache, which also makes it easy to put fake data there in testcases). The new dependency resolution code can output lots of verbose messages for debugging by specifying the `-v` parameter. The meaning of that changed, it used to output the file names where log messages come from, but no one seemed to use that anyway.
2017-07-10 15:23:43 +00:00
import pmb.helpers.repo
2017-05-26 20:08:45 +00:00
import pmb.helpers.run
import pmb.parse.arch
def copy_resolv_conf(args, suffix="native"):
"""
Use pythons super fast file compare function (due to caching)
and copy the /etc/resolv.conf to the chroot, in case it is
different from the host.
If the file doesn't exist, create an empty file with 'touch'.
2017-05-26 20:08:45 +00:00
"""
host = "/etc/resolv.conf"
chroot = f"{args.work}/chroot_{suffix}{host}"
if os.path.exists(host):
if not os.path.exists(chroot) or not filecmp.cmp(host, chroot):
pmb.helpers.run.root(args, ["cp", host, chroot])
else:
pmb.helpers.run.root(args, ["touch", chroot])
2017-05-26 20:08:45 +00:00
def mark_in_chroot(args, suffix="native"):
"""
Touch a flag so we can know when we're running in chroot (and
don't accidentally flash partitions on our host)
"""
in_chroot_file = f"{args.work}/chroot_{suffix}/in-pmbootstrap"
if not os.path.exists(in_chroot_file):
pmb.helpers.run.root(args, ["touch", in_chroot_file])
def setup_qemu_emulation(args, suffix):
arch = pmb.parse.arch.from_chroot_suffix(args, suffix)
if not pmb.parse.arch.cpu_emulation_required(args, arch):
return
chroot = f"{args.work}/chroot_{suffix}"
arch_qemu = pmb.parse.arch.alpine_to_qemu(arch)
# mount --bind the qemu-user binary
pmb.chroot.binfmt.register(args, arch)
pmb.helpers.mount.bind_file(args, f"{args.work}/chroot_native"
f"/usr/bin/qemu-{arch_qemu}",
f"{chroot}/usr/bin/qemu-{arch_qemu}-static",
create_folders=True)
2017-05-26 20:08:45 +00:00
def init(args, suffix="native"):
# When already initialized: just prepare the chroot
chroot = f"{args.work}/chroot_{suffix}"
2017-05-26 20:08:45 +00:00
arch = pmb.parse.arch.from_chroot_suffix(args, suffix)
2017-05-26 20:08:45 +00:00
pmb.chroot.mount(args, suffix)
setup_qemu_emulation(args, suffix)
mark_in_chroot(args, suffix)
if os.path.islink(f"{chroot}/bin/sh"):
pmb.config.workdir.chroot_check_channel(args, suffix)
2017-05-26 20:08:45 +00:00
copy_resolv_conf(args, suffix)
pmb.chroot.apk.update_repository_list(args, suffix)
2017-05-26 20:08:45 +00:00
return
# Require apk-tools-static
pmb.chroot.apk_static.init(args)
logging.info(f"({suffix}) install alpine-base")
2017-05-26 20:08:45 +00:00
# Initialize cache
apk_cache = f"{args.work}/cache_apk_{arch}"
pmb.helpers.run.root(args, ["ln", "-s", "-f", "/var/cache/apk",
f"{chroot}/etc/apk/cache"])
2017-05-26 20:08:45 +00:00
# Initialize /etc/apk/keys/, resolv.conf, repositories
for key in glob.glob(f"{pmb.config.apk_keys_path}/*.pub"):
pmb.helpers.run.root(args, ["cp", key, f"{args.work}"
"/config_apk_keys/"])
2017-05-26 20:08:45 +00:00
copy_resolv_conf(args, suffix)
pmb.chroot.apk.update_repository_list(args, suffix)
2017-05-26 20:08:45 +00:00
pmb.config.workdir.chroot_save_init(args, suffix)
# Install alpine-base
pmb.helpers.repo.update(args, arch)
pmb.chroot.apk_static.run(args, ["--root", chroot,
"--cache-dir", apk_cache,
"--initdb", "--arch", arch,
"add", "alpine-base"])
2017-05-26 20:08:45 +00:00
# Building chroots: create "pmos" user, add symlinks to /home/pmos
if not suffix.startswith("rootfs_"):
pmb.chroot.root(args, ["adduser", "-D", "pmos", "-u",
pmb.config.chroot_uid_user],
suffix, auto_init=False)
# Create the links (with subfolders if necessary)
for target, link_name in pmb.config.chroot_home_symlinks.items():
link_dir = os.path.dirname(link_name)
if not os.path.exists(f"{chroot}{link_dir}"):
pmb.chroot.user(args, ["mkdir", "-p", link_dir], suffix)
if not os.path.exists(f"{chroot}{target}"):
pmb.chroot.root(args, ["mkdir", "-p", target], suffix)
pmb.chroot.user(args, ["ln", "-s", target, link_name], suffix)
pmb.chroot.root(args, ["chown", "pmos:pmos", target], suffix)