rodeo/rodeo-overlay.sh

124 lines
3.4 KiB
Bash
Executable file

#!/bin/sh
VERSION=0.1
CONFIG_DIR=/rodeo/configuration
OVERLAY_DIRECTORY=/rodeo/overlays
DEFAULT_OVERLAY_FILE=$CONFIG_DIR/included_overlays
MANAGED_DIRECTORIES_FILE=$CONFIG_DIR/managed_directories
export PATH="/bin:$PATH"
fatal() {
echo "Error: $1"
exit 1
}
error () {
echo "Error: $1"
}
info() {
echo "$1"
}
debug() {
[ "$RODEO_DEBUG" = "1" ] && info "$1"
}
warning() {
echo "Warning: $1"
}
get_lower_dirs() {
overlay_config="$1"
dir="$2"
while IFS="" read -r overlay || [ -n "$overlay" ]
do
if [ -f "$overlay" ]; then
get_lower_dirs "$overlay" "$dir"
else
debug "Checking if $overlay has $dir"
cd $OVERLAY_DIRECTORY || fatal "Failed to change directory"
overlay_path="$(realpath "$overlay")"
if [ -d "$overlay_path/$dir" ]; then
debug "$overlay has $dir"
if [ -z "$lowerdirs" ]; then
lowerdirs="$overlay_path/$dir"
else
lowerdirs="$lowerdirs:$overlay_path/$dir"
fi
else
debug "$overlay does not have $dir"
fi
fi
done < "$overlay_config"
}
remount() {
while IFS="" read -r dir || [ -n "$dir" ]
do
debug "Scanning for overlays to be mounted on /$dir"
overlay_config=$DEFAULT_OVERLAY_FILE
[ -n "$1" ] && overlay_config="$1"
lowerdirs=""
get_lower_dirs "$overlay_config" "$dir"
umount "/$dir" || error "Unmounting of /$dir failed"
if [ -z "$lowerdirs" ]; then
warning "No overlay has $dir"
mount --bind "/rodeo/root/$dir" "/$dir" || error "Bind-Mounting /$dir failed"
else
debug "Overlays to be mounted on /$dir: $lowerdirs"
mount -t overlay overlay -o lowerdir="$lowerdirs",upperdir="/rodeo/root/$dir",workdir="/rodeo/tmp/$dir" "/$dir" || error "Overlay-Mounting /$dir failed"
fi
done < $MANAGED_DIRECTORIES_FILE
}
initial_mount() {
overlay_config=$DEFAULT_OVERLAY_FILE
[ -n "$1" ] && overlay_config="$1"
while IFS="" read -r dir || [ -n "$dir" ]
do
debug "Scanning for overlays to be mounted on /$dir"
lowerdirs=""
get_lower_dirs "$overlay_config" "$dir"
if [ -z "$lowerdirs" ]; then
warning "No overlay has $dir"
mount --bind "/rodeo/root/$dir" "/$dir" || error "Bind-Mounting /$dir failed"
else
debug "Overlays to be mounted on /$dir: $lowerdirs"
mount -t overlay overlay -o lowerdir="$lowerdirs",upperdir="/rodeo/root/$dir",workdir="/rodeo/tmp/$dir" "/$dir" || error "Overlay-Mounting /$dir failed"
fi
done < $MANAGED_DIRECTORIES_FILE
}
transfer_directory() {
dir="$1"
mv "/$dir" "/rodeo/root/$dir"
mkdir -p "/$dir" "/rodeo/tmp/$dir"
echo "$dir" >> $MANAGED_DIRECTORIES_FILE
info "Added $dir to the list of managed directories"
initial_mount
}
shell() {
target="$1"
overlay_file=$DEFAULT_OVERLAY_FILE
command="/bin/sh"
[ -n "$2" ] && overlay_file="$2"
[ -n "$3" ] && command="$3"
[ ! -d "$target" ] && mkdir -p "$target"
info "Mounting Base Filesystems"
mount --bind / "$target"
mount --bind /dev "$target/dev"
mount --bind /sys "$target/sys"
mount --bind /proc "$target/proc"
info "Mounting Overlays"
chroot "$target" /sbin/rodeo-overlay.sh initial-mount "$overlay_file"
info "Executing $command"
chroot "$target" "$command"
}
[ "$1" = "transfer-directory" ] && transfer_directory "$2"
[ "$1" = "remount" ] && remount "$2"
[ "$1" = "initial-mount" ] && initial_mount "$2"
[ "$1" = "shell" ] && shell "$2" "$3" "$4"