From 6761fbaaf85d35e7820c3f9d5482ba81de7e53d0 Mon Sep 17 00:00:00 2001 From: pauljako Date: Sat, 8 Feb 2025 22:52:26 +0100 Subject: [PATCH] Initial commit --- .idea/.gitignore | 3 + .../inspectionProfiles/profiles_settings.xml | 6 + .idea/misc.xml | 7 ++ .idea/modules.xml | 8 ++ .idea/rodeo-overlay.iml | 8 ++ rodeo-overlay.sh | 115 ++++++++++++++++++ 6 files changed, 147 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/rodeo-overlay.iml create mode 100644 rodeo-overlay.sh diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..d378f41 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..c85e8c7 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/rodeo-overlay.iml b/.idea/rodeo-overlay.iml new file mode 100644 index 0000000..53190fe --- /dev/null +++ b/.idea/rodeo-overlay.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/rodeo-overlay.sh b/rodeo-overlay.sh new file mode 100644 index 0000000..1e416d5 --- /dev/null +++ b/rodeo-overlay.sh @@ -0,0 +1,115 @@ +#!/bin/sh + +fatal() { + echo "Error: $1" + exit 1 +} + +error () { + echo "Error: $1" +} + +info() { + echo "$1" +} + +debug() { + [ "$RODEO_DEBUG" = "1" ] && info "$1" +} + +warning() { + echo "Warning: $1" +} + +remount () { + overlay_config=/rodeo/configuration/included_overlays + [ -n "$1" ] && overlay_config="$1" + while IFS="" read -r dir || [ -n "$dir" ] + do + debug "Scanning for overlays to be mounted on /$dir" + lowerdirs="" + while IFS="" read -r overlay || [ -n "$overlay" ] + do + debug "Checking if $overlay has $dir" + if [ -d "/rodeo/overlays/$overlay/$dir" ]; then + debug "$overlay has $dir" + if [ -z "$lowerdirs" ]; then + lowerdirs="/rodeo/overlays/$overlay/$dir" + else + lowerdirs="$lowerdirs:/rodeo/overlays/$overlay/$dir" + fi + else + debug "$overlay does not have $dir" + fi + done < "$overlay_config" + 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 + mount -t overlay overlay -o lowerdir="$lowerdirs",upperdir="/rodeo/root/$dir",workdir="/rodeo/tmp/$dir" "/$dir" || error "Overlay-Mounting /$dir failed" + fi + done < /rodeo/configuration/managed_directories +} + +initial_mount () { + overlay_config=/rodeo/configuration/included_overlays + [ -n "$1" ] && overlay_config="$1" + while IFS="" read -r dir || [ -n "$dir" ] + do + debug "Scanning for overlays to be mounted on /$dir" + lowerdirs="" + while IFS="" read -r overlay || [ -n "$overlay" ] + do + debug "Checking if $overlay has $dir" + if [ -d "/rodeo/overlays/$overlay/$dir" ]; then + debug "$overlay has $dir" + if [ -z "$lowerdirs" ]; then + lowerdirs="/rodeo/overlays/$overlay/$dir" + else + lowerdirs="$lowerdirs:/rodeo/overlays/$overlay/$dir" + fi + else + debug "$overlay does not have $dir" + fi + done < "$overlay_config" + if [ -z "$lowerdirs" ]; then + warning "No overlay has $dir" + mount --bind "/rodeo/root/$dir" "/$dir" || error "Bind-Mounting /$dir failed" + else + mount -t overlay overlay -o lowerdir="$lowerdirs",upperdir="/rodeo/root/$dir",workdir="/rodeo/tmp/$dir" "/$dir" || error "Overlay-Mounting /$dir failed" + fi + done < /rodeo/configuration/managed_directories +} + +transfer_directory() { + dir="$1" + cp -r "/$dir" "/rodeo/root/$dir" + mkdir "/$dir" "/rodeo/tmp/$dir" + echo "$dir" >> /rodeo/configuration/managed_directories + info "Added $dir to the list of managed directories" + initial_mount +} + +shell() { + target="$1" + overlay_file=/rodeo/configuration/included_overlays + 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"