2017-05-26 20:08:45 +00:00
|
|
|
"""
|
2018-01-04 03:53:35 +00:00
|
|
|
Copyright 2018 Oliver Smith
|
2017-05-26 20:08:45 +00:00
|
|
|
|
|
|
|
This file is part of pmbootstrap.
|
|
|
|
|
|
|
|
pmbootstrap 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
pmbootstrap 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 pmbootstrap. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import glob
|
|
|
|
import filecmp
|
|
|
|
|
|
|
|
import pmb.chroot
|
|
|
|
import pmb.chroot.apk_static
|
|
|
|
import pmb.config
|
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.
|
2017-08-20 12:34:31 +00:00
|
|
|
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 = args.work + "/chroot_" + suffix + host
|
2017-08-20 12:34:31 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2018-01-23 00:54:48 +00:00
|
|
|
def create_device_nodes(args, suffix):
|
2018-03-10 14:24:16 +00:00
|
|
|
error = "Failed to create device nodes in the '" + suffix + "' chroot."
|
2018-01-23 00:54:48 +00:00
|
|
|
|
|
|
|
# Folder sturcture
|
|
|
|
chroot = args.work + "/chroot_" + suffix
|
|
|
|
pmb.helpers.run.root(args, ["mkdir", "-p", chroot + "/dev"])
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Create all device nodes as specified in the config
|
|
|
|
for dev in pmb.config.chroot_device_nodes:
|
|
|
|
path = chroot + "/dev/" + str(dev[4])
|
|
|
|
if not os.path.exists(path):
|
|
|
|
pmb.helpers.run.root(args, ["mknod",
|
|
|
|
"-m", str(dev[0]), # permissions
|
|
|
|
path, # name
|
|
|
|
str(dev[1]), # type
|
|
|
|
str(dev[2]), # major
|
|
|
|
str(dev[3]), # minor
|
|
|
|
])
|
|
|
|
|
|
|
|
# Verify major and minor numbers of created nodes
|
|
|
|
for dev in pmb.config.chroot_device_nodes:
|
|
|
|
path = chroot + "/dev/" + str(dev[4])
|
|
|
|
stat_result = os.stat(path)
|
|
|
|
rdev = stat_result.st_rdev
|
|
|
|
assert os.major(rdev) == dev[2], "Wrong major in " + path
|
|
|
|
assert os.minor(rdev) == dev[3], "Wrong minor in " + path
|
|
|
|
|
|
|
|
# Verify /dev/zero reading and writing
|
|
|
|
path = chroot + "/dev/zero"
|
|
|
|
with open(path, "r+b", 0) as handle:
|
|
|
|
assert handle.write(bytes([0xff])), "Write failed for " + path
|
|
|
|
assert handle.read(1) == bytes([0x00]), "Read failed for " + path
|
|
|
|
|
|
|
|
# On failure: Show filesystem-related error
|
|
|
|
except Exception as e:
|
|
|
|
logging.info(str(e) + "!")
|
|
|
|
raise RuntimeError(error)
|
|
|
|
|
|
|
|
|
2017-05-26 20:08:45 +00:00
|
|
|
def init(args, suffix="native"):
|
|
|
|
# When already initialized: just prepare the chroot
|
|
|
|
chroot = args.work + "/chroot_" + suffix
|
|
|
|
arch = pmb.parse.arch.from_chroot_suffix(args, suffix)
|
2017-07-26 19:01:44 +00:00
|
|
|
emulate = pmb.parse.arch.cpu_emulation_required(args, arch)
|
|
|
|
|
2017-05-26 20:08:45 +00:00
|
|
|
pmb.chroot.mount(args, suffix)
|
|
|
|
if os.path.islink(chroot + "/bin/sh"):
|
2017-07-26 19:01:44 +00:00
|
|
|
if emulate:
|
2017-05-26 20:08:45 +00:00
|
|
|
pmb.chroot.binfmt.register(args, arch)
|
|
|
|
copy_resolv_conf(args, suffix)
|
2017-07-11 17:19:39 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
# Non-native chroot: require qemu-user-static
|
2017-07-26 19:01:44 +00:00
|
|
|
if emulate:
|
2017-05-26 20:08:45 +00:00
|
|
|
pmb.chroot.apk.install(args, ["qemu-user-static-repack",
|
|
|
|
"qemu-user-static-repack-binfmt"])
|
|
|
|
pmb.chroot.binfmt.register(args, arch)
|
|
|
|
|
|
|
|
logging.info("(" + suffix + ") install alpine-base")
|
|
|
|
|
2018-01-23 00:54:48 +00:00
|
|
|
# Initialize device nodes and cache
|
|
|
|
create_device_nodes(args, suffix)
|
2017-05-26 20:08:45 +00:00
|
|
|
apk_cache = args.work + "/cache_apk_" + arch
|
2017-11-04 02:19:23 +00:00
|
|
|
pmb.helpers.run.root(args, ["ln", "-s", "-f", "/var/cache/apk", chroot +
|
2017-05-26 20:08:45 +00:00
|
|
|
"/etc/apk/cache"])
|
|
|
|
|
2017-07-11 17:19:39 +00:00
|
|
|
# Initialize /etc/apk/keys/, resolv.conf, repositories
|
2017-05-26 20:08:45 +00:00
|
|
|
for key in glob.glob(pmb.config.apk_keys_path + "/*.pub"):
|
|
|
|
pmb.helpers.run.root(args, ["cp", key, args.work +
|
|
|
|
"/config_apk_keys/"])
|
|
|
|
copy_resolv_conf(args, suffix)
|
2017-07-11 17:19:39 +00:00
|
|
|
pmb.chroot.apk.update_repository_list(args, suffix)
|
2017-05-26 20:08:45 +00:00
|
|
|
|
2018-01-23 00:54:48 +00:00
|
|
|
# Non-native chroot: install qemu-user-binary
|
2017-07-26 19:01:44 +00:00
|
|
|
if emulate:
|
2017-05-26 20:08:45 +00:00
|
|
|
arch_debian = pmb.parse.arch.alpine_to_debian(arch)
|
2018-01-23 00:54:48 +00:00
|
|
|
pmb.helpers.run.root(args, ["mkdir", "-p", chroot + "/usr/bin"])
|
2017-05-26 20:08:45 +00:00
|
|
|
pmb.helpers.run.root(args, ["cp", args.work +
|
|
|
|
"/chroot_native/usr/bin/qemu-" + arch_debian + "-static",
|
|
|
|
chroot + "/usr/bin/qemu-" + arch_debian + "-static"])
|
2018-01-23 00:54:48 +00:00
|
|
|
|
2018-03-04 13:44:27 +00:00
|
|
|
# Install alpine-base
|
|
|
|
pmb.helpers.repo.update(args, arch)
|
2018-02-09 18:43:58 +00:00
|
|
|
pmb.chroot.apk_static.run(args, ["--no-progress", "--root", chroot,
|
2018-01-23 00:54:48 +00:00
|
|
|
"--cache-dir", apk_cache, "--initdb", "--arch", arch,
|
|
|
|
"add", "alpine-base"])
|
2017-05-26 20:08:45 +00:00
|
|
|
|
2017-10-12 20:08:10 +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(chroot + link_dir):
|
|
|
|
pmb.chroot.user(args, ["mkdir", "-p", link_dir], suffix)
|
|
|
|
pmb.chroot.user(args, ["ln", "-s", target, link_name], suffix)
|
|
|
|
pmb.chroot.root(args, ["chown", "pmos:pmos", target],
|
|
|
|
suffix)
|