scaleway-image-archlinux/images/lib.sh

110 lines
1.9 KiB
Bash
Raw Normal View History

2014-10-15 13:30:39 +02:00
# Declares helpers for image building
set -e
[ "$DEBUG" = "1" ] && set -x
prepare_nbd_volume() {
device=$1
if ! `mountpoint -q "$TARGET"`; then
sudo mkfs.ext4 "$device"
sudo mkdir -p "$TARGET"
sudo mount "$device" "$TARGET"
fi
}
require_debootstrap() {
type -P debootstrap >/dev/null && return
sudo apt-get update
sudo apt-get -y install debootstrap
}
clean_workspace() {
sudo rm -rf $TARGET/* $TARGET/.??*
}
debootstrap() {
sudo debootstrap \
--arch="$ARCH" \
--variant="$VARIANT" \
--components="$COMPONENTS" \
--include="$PKGS_INCLUDE" \
--foreign \
"$VERSION" \
"$TARGET" \
"$MIRROR" \
"$SCRIPT"
}
2014-10-15 19:30:46 +02:00
upgrade_debs() {
2014-10-15 19:34:58 +02:00
do_in_target apt-get update
2014-10-16 12:59:39 +02:00
do_in_target apt-get -y upgrade
2014-10-15 19:30:46 +02:00
}
2014-10-15 13:30:39 +02:00
secondstage() {
# This step could be done directly by removing
sudo chroot "$TARGET" /debootstrap/debootstrap --second-stage
}
patch_target() {
patches_dir=$1
for file in $(find "$patches_dir" -type f | sed -n "s|^$patches_dir/||p"); do
sudo mkdir -p "$TARGET/$(dirname $file)"
sudo cp "$patches_dir/$file" "$TARGET/$file"
done
}
clean_target() {
clean_paths=$1
2014-10-15 19:30:46 +02:00
do_in_target apt-get clean
2014-10-15 13:30:39 +02:00
for path in $clean_paths; do
if [ -e "$TARGET/$path" ]; then
sudo rm -rf "$TARGET/$path"
fi
done
2014-10-15 19:30:46 +02:00
for file in $(find "$TARGET/var/log" -type f); do
2014-10-15 23:12:21 +02:00
echo | sudo tee $file
2014-10-15 19:30:46 +02:00
done
2014-10-15 13:30:39 +02:00
}
archive_target() {
sudo tar -C "$TARGET" -czf "$NAME.tar.gz" .
}
do_in_target() {
sudo chroot "$TARGET" $@
}
2014-10-15 13:30:39 +02:00
cli() {
case $1 in
2014-10-16 10:43:23 +02:00
"clean")
echo "not yet implemented"
exit 1
;;
"dev")
build_image
2014-10-15 19:34:58 +02:00
patch_image
exit 0
;;
2014-10-15 13:30:39 +02:00
"tarball")
build_image
2014-10-15 19:34:58 +02:00
patch_image
2014-10-15 13:30:39 +02:00
archive_target
exit 0
;;
"image")
NBD_DEVICE=$2
prepare_nbd_volume $NBD_DEVICE
build_image
2014-10-15 19:34:58 +02:00
patch_image
exit 0
;;
"patch")
patch_image
2014-10-15 13:30:39 +02:00
exit 0
;;
esac
2014-10-16 10:43:23 +02:00
echo >&2 "usage: [DEBUG=1] $0 (tarball|image|dev|patch)"
2014-10-15 13:30:39 +02:00
exit 1
}