lix/lib/pkg.sh

101 lines
2.9 KiB
Bash

#!/bin/sh -e
. "$LIXROOT/lib/built.sh"
. "$LIXROOT/lib/dependencies.sh"
. "$LIXROOT/lib/clearopaques.sh"
. "$LIXROOT/lib/lowerlayers.sh"
. "$LIXROOT/lib/versionformat.sh"
export PKGHOW=""
export PKGDEPS=""
# sets up the package chroot, runs the given command in it, then tears it down.
pkgdo() {
if [ -z "$FAKEROOT" ]; then
FAKEROOT="$(mktemp -d)"
CLEANUPFAKEROOT="yes"
log "loading $name version $version overlay"
mountpkg "$FAKEROOT" "$2" \
|| err "failed to mount $name $version overlay."
fi
chin "$FAKEROOT" "cd /mnt/src; $1"
}
mountpkghow() {
# mount the 'how' overlay for the given package and version if not mounted.
PKGHOW="$(lyr mountdir "lix/$name/$version/how")"
if ! (mountpoint -q "$PKGHOW"); then
lyr mk "lix/$name/$version/how"
lyr up "$(how "$name" "$version")" "lix/$name/$version/how" > /dev/null
fi
}
loadpkgdeps() {
[ "$PKGHOW" ] || mountpkghow
if [ -z "$PKGDEPS" ]; then
[ -f "$PKGHOW/deps" ] \
|| err "could not find a 'deps' file in the package's 'how' overlay!"
PKGDEPS="$(dependencies "$(cat "$PKGHOW/deps")")"
fi
}
# sets up the package chroot overlay. optionally takes a path at which the
# overlay should be created.
mountpkg() {
loadpkgdeps
tgt="$1"
pkglower="$(lowerlayers "$PKGDEPS" "$BSLAYER")"
# make and mount package root filesystem overlay
lyr mk "lix/$name/$version/fs"
fs="$(lyr up "$pkglower" "lix/$name/$version/fs")"
[ -n "$fs" ] || err "could not bring up chroot layer for $name $version."
# bind mount overlay and override $fs value for remainder of function if a
# target path was specified.
mount -o bind "$fs" "$tgt"
# bind the how overlay to /mnt/how in the chroot.
mkdir -p "$tgt/mnt/how"
mount -o bind "$PKGHOW" "$tgt/mnt/how"
# mount source code overlay and bind to /mnt/src in the chroot.
lyr mk "lix/$name/$version/src"
srcmnt="$(lyr up "$SRCREPO/$name/$version" "lix/$name/$version/src")"
mkdir -p "$tgt/mnt/src"
mount -o bind "$srcmnt" "$tgt/mnt/src"
# create build layer and bind to /mnt/build in the chroot.
# this layer exists to support packages that need an out-of-source build.
# doesn't really *need* to be a layer, but it's a handy place to stash any
# writes we make to it.
lyr mk "lix/$name/$version/build"
mkdir -p "$tgt/mnt/build"
mount -o bind "$(lyr upperdir "lix/$name/$version/build")" "$tgt/mnt/build"
}
unmountpkg() {
_dn() { mountpoint -q "$1" && umount "$1"; };
_dnandrm() { _dn "$1" && rmdir "$1" 2> /dev/null; }
_dnandrm "$1/mnt/build" || true
_dnandrm "$1/mnt/src" || true
_dnandrm "$1/mnt/how" || true
_dn "$1" || true
lyr dn "lix/$name/$version/src" || true
lyr dn "lix/$name/$version/fs" || true
_dn "$PKGHOW"
rmdir "$(lyr upperdir lix/$name/$version)/fs/mnt" || true
clearopaques "$(lyr upperdir lix/$name/$version)/fs" || true
}