Skip to content

build.sh: add option to build in a 'bind-mount' directory #137

Merged
merged 2 commits into from
Jan 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 31 additions & 2 deletions tools/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ die() {
}

die_usage() {
die "usage: $0 example-1.2.3-0 [--purge]"
die "usage: $0 example-1.2.3-0 [--purge] [--bind directory]"
}

ere_quote() {
Expand All @@ -23,9 +23,10 @@ log_cmd() {
"$@"
}

eval set -- $(getopt --options "" --longoptions purge -- "$@")
eval set -- $(getopt --options "" --longoptions purge,bind: -- "$@")
while true; do
case "$1" in
--bind) opt_bind=1;bind_dir=$2;shift 2;;
--purge) opt_purge=1;shift;;
--) shift;break;;
*) die "internal error: unknown option $!"
Expand Down Expand Up @@ -57,10 +58,27 @@ if [ -n "$opt_purge" ]; then
test -z "$PKG" && die "internal error: PKG is empty"
log_cmd find "/pkg/$PKG" -mindepth 1 -delete
test "$(stat -c %U "/pkg/$PKG")" = "build" || log_cmd chown build:build "/pkg/$PKG"
if [ -n "$opt_bind" -a -d "$bind_dir/pkg_bindmnt" ]; then
log_cmd find "$bind_dir/pkg_bindmnt" -mindepth 1 -delete
fi
fi

chmod a+rx "$PKG.build.sh"

if [ -n "$opt_bind" ]; then
mkdir -p "$bind_dir/pkg_bindmnt"
chown build:build "$bind_dir/pkg_bindmnt"
# check that '/pkg/$PKG' differs from intended location (i.e. isn't mounted already)
if [ $(stat --format=%D:%i "$bind_dir/pkg_bindmnt") != $(stat --format=%D:%i /pkg/$PKG) ]; then
log_cmd mount --bind "$bind_dir/pkg_bindmnt" /pkg/$PKG
# prevent automount from unmounting
exec 3</pkg/$PKG/.
test $(stat --format=%D:%i "$bind_dir/pkg_bindmnt") = $(stat --format=%D:%i /pkg/$PKG) || die "unable to mount and lock '$bind_dir/pkg_bindmnt' on /pkg/$PKG"
else
die "'$bind_dir/pkg_bindmnt' is already mounted on /pkg/$PKG (try 'umount /pkg/$PKG')"
fi
fi

echo "sudo -u build script -q -e -c \"./$PKG.build.sh\" /package/pkg/$PKG/$PKG.build.log"
(
sudo -u build script -q -e -c "./$PKG.build.sh" /package/pkg/$PKG/$PKG.build.log
Expand All @@ -70,3 +88,14 @@ echo "sudo -u build script -q -e -c \"./$PKG.build.sh\" /package/pkg/$PKG/$PKG.b
# make double sure PKG is not empty even if bugs are introduced in code above
test -z "$PKG" && die "internal error: PKG is empty"
log_cmd chown -R bin:bin "/pkg/$PKG"

if [ -n "$opt_bind" ]; then
test -z "$PKG" && die "internal error: PKG variable is empty"
test $(ls "/pkg/$PKG" | wc -l) = 0 && die "/pkg/$PKG contains no files"
# withdraw our 'foot in the door'
exec 3<&-
log_cmd umount /pkg/$PKG
# enforce single filesystem as source, and ignore .*
log_cmd cp -ax $bind_dir/pkg_bindmnt/* /package/pkg/$PKG
log_cmd chown bin:bin "/package/pkg/$PKG"
fi