Skip to content

Instantly share code, notes, and snippets.

@CypherpunkSamurai
Last active April 6, 2025 17:10
Show Gist options
  • Save CypherpunkSamurai/978748791452982249e303d24280f326 to your computer and use it in GitHub Desktop.
Save CypherpunkSamurai/978748791452982249e303d24280f326 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Cleanup script for the custom Ubuntu ISO creator
# Run as root (sudo) if the main script fails
set -e
# set -o pipefail # Optional: might be too strict for cleanup
# Configuration variables (should match the main script)
WORK_DIR="/tmp/custom-ubuntu-iso"
TARGET_DIR="$WORK_DIR/chroot"
# --- Safety Check ---
# Ensure WORK_DIR is set and not something dangerous like "/"
if [ -z "$WORK_DIR" ] || [ "$WORK_DIR" = "/" ] || [ "$WORK_DIR" = "/tmp" ]; then
echo "ERROR: WORK_DIR variable is not set or is set to a potentially dangerous location ('$WORK_DIR'). Exiting."
exit 1
fi
# --- Ensure script is run as root ---
if [[ $EUID -ne 0 ]]; then
echo "This cleanup script must be run as root"
exit 1
fi
echo "Starting cleanup process for $WORK_DIR..."
# --- Unmount chroot filesystems ---
# Unmount in reverse order of mounting, or use -lf which is often sufficient
echo "Attempting to unmount filesystems..."
# List of mount points relative to TARGET_DIR
mount_points=(
"$TARGET_DIR/dev/pts"
"$TARGET_DIR/dev"
"$TARGET_DIR/proc"
"$TARGET_DIR/sys"
"$TARGET_DIR/run"
)
for mp in "${mount_points[@]}"; do
if mountpoint -q "$mp"; then
echo "Unmounting $mp..."
umount -lf "$mp" || echo "Warning: Failed to unmount $mp, continuing cleanup..."
else
# Check if the directory exists at all before printing message
if [ -d "$mp" ]; then
echo "$mp exists but is not a mount point."
else
echo "$mp does not exist, skipping unmount."
fi
fi
done
# An extra attempt for the main chroot dir itself, in case of bind mounts
# Although the original script doesn't mount TARGET_DIR itself, this is harmless
if mountpoint -q "$TARGET_DIR"; then
echo "Unmounting $TARGET_DIR itself (if it's a mount point)..."
umount -lf "$TARGET_DIR" || echo "Warning: Failed to unmount $TARGET_DIR, continuing cleanup..."
fi
# --- Remove Working Directory ---
if [ -d "$WORK_DIR" ]; then
echo "Removing working directory $WORK_DIR..."
rm -rf "$WORK_DIR"
echo "Working directory removed."
else
echo "Working directory $WORK_DIR does not exist."
fi
# --- Note on Final ISO ---
# This script does *not* automatically remove the final ISO file
# (e.g., $HOME/custom-ubuntu-*.iso) because its exact name depends
# on the date the main script was run and the $HOME variable context.
# Please manually remove any partially created ISO file if necessary.
echo "Cleanup finished."
echo "Note: Please manually remove any partially created ISO file (e.g., in your home directory or /root) if it exists."
exit 0
#!/bin/bash
# CelestialOS ISO Build Cleanup Script
# Unmounts filesystems and removes the temporary build directory.
# Run as root (sudo)
# --- Configuration ---
# !!! IMPORTANT: Ensure this matches the DISTRO_NAME in your main build script !!!
DISTRO_NAME="CelestialOS"
# --- End Configuration ---
# Derived variables
WORK_DIR="/tmp/${DISTRO_NAME,,}-iso-build" # Lowercase distro name for directory
TARGET_DIR="$WORK_DIR/chroot"
# Color definitions
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
BOLD='\033[1m'
NC='\033[0m' # No Color
# Function for colorized output
print_step() {
echo -e "${BOLD}${BLUE}==> ${GREEN}$1${NC}"
}
print_warning() {
echo -e "${YELLOW}WARNING: $1${NC}"
}
print_error() {
echo -e "${RED}ERROR: $1${NC}"
}
print_success() {
echo -e "${GREEN}SUCCESS: $1${NC}"
}
# Ensure script is run as root
if [[ $EUID -ne 0 ]]; then
print_error "This script must be run as root to unmount filesystems and remove directories."
exit 1
fi
print_step "Starting cleanup process for ${DISTRO_NAME} build..."
echo "Working directory targeted: ${WORK_DIR}"
# Check if the working directory exists
if [ ! -d "$WORK_DIR" ]; then
print_warning "Working directory '${WORK_DIR}' not found. Nothing to clean up."
exit 0
fi
# Attempt to unmount filesystems within the chroot target directory
print_step "Attempting to unmount filesystems (errors ignored if already unmounted)"
# Unmount in reverse order, using -lf (lazy and force)
umount -lf "${TARGET_DIR}/run" 2>/dev/null || true
umount -lf "${TARGET_DIR}/sys" 2>/dev/null || true
umount -lf "${TARGET_DIR}/proc" 2>/dev/null || true
umount -lf "${TARGET_DIR}/dev/pts" 2>/dev/null || true
umount -lf "${TARGET_DIR}/dev" 2>/dev/null || true
# Check if any mounts persist (optional, but helpful for debugging)
if findmnt --target "$TARGET_DIR" | grep -q "$TARGET_DIR"; then
print_warning "Some filesystems under ${TARGET_DIR} might still be mounted. Manual intervention may be required."
findmnt --target "$TARGET_DIR"
else
print_success "Filesystem unmount checks passed."
fi
# Remove the working directory
print_step "Removing working directory: ${WORK_DIR}"
rm -rf "$WORK_DIR"
if [ $? -eq 0 ]; then
print_success "Cleanup complete. Working directory removed."
else
print_error "Failed to completely remove working directory '${WORK_DIR}'. Check permissions or lingering processes."
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment