Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 87 lines (65 sloc) 2.2 KB
#!/bin/sh
IMAGE_FILE="$1"
IMAGE_SIZE="1600M"
DATA_LOCATION="/mnt/easy_mirror"
MIRROR_NAME="$2"
NEW_IMAGE="0"
if [ -z "$IMAGE_FILE" ]; then
echo "Please pass the path where you want your image file" >&2
fi
if [ -z "$MIRROR_NAME" ]; then
MIRROR_NAME="$(basename "$IMAGE_FILE" | sed 's/\.[^.]*$//')"
fi
MIRROR_LOCATION="$DATA_LOCATION/$MIRROR_NAME"
# Need to be root for some mount operations
if [ "$(whoami)" != "root" ]; then
echo "You're going to want to be root" >&2
exit 2
fi
if [ ! -e "$IMAGE_FILE" ]; then
# Creating Image
if ! truncate -s "$IMAGE_SIZE" "$IMAGE_FILE"; then
rm "$IMAGE_FILE"
echo "Failed To Allocate $IMAGE_SIZE for Image" >&2
exit 1
fi
mkfs.ext2 -M "$UNION_LOCATION" -F "$IMAGE_FILE"
echo "Image Created"
NEW_IMAGE="1"
fi
if [ -d "$MIRROR_LOCATION" ]; then
echo "Folder already exists at '$MIRROR_LOCATION'" >&2
echo "Remove that folder if you wish to continue, or choose another name for this mirror" >&2
exit 1
fi
CACHE_LOCATION="$MIRROR_LOCATION/cache"
UNION_LOCATION="$MIRROR_LOCATION/union"
mkdir -p "$MIRROR_LOCATION" "$CACHE_LOCATION" "$MIRROR_LOCATION/union"
if ! mount -t ext2 -o loop "$IMAGE_FILE" "$CACHE_LOCATION"; then
echo "Failed to Mount Image" >&2
exit 1
fi
echo "Mounted Image"
# Create Union
if ! mount -t aufs -o dirs="$CACHE_LOCATION:/=ro" none "$UNION_LOCATION"; then
echo "Failed to Mount Union" >&2
exit 1
fi
echo "Mounted Union"
mount --bind /dev "$UNION_LOCATION"/dev 2> /dev/null
mount -t devpts devpts "$UNION_LOCATION"/dev/pts 2> /dev/null
mount -t tmpfs shm "$UNION_LOCATION"/dev/shm 2> /dev/null
mount -t sysfs sysfs "$UNION_LOCATION"/sys 2> /dev/null
mount -t proc proc "$UNION_LOCATION"/proc 2> /dev/null
mount --bind /tmp "$UNION_LOCATION"/tmp 2> /dev/null
# Check for "callbacks"
NEW_MIRROR_CALLBACK="/home/$SUDO_USER/.local/share/easy_mirror/new_mirror.sh"
NEW_IMAGE_CALLBACK="/home/$SUDO_USER/.local/share/easy_mirror/new_image.sh"
if [ "$NEW_IMAGE" = 1 -a -x "$NEW_IMAGE_CALLBACK" ]; then
echo "Running New Image Callback"
enter_mirror "$MIRROR_NAME" "$NEW_IMAGE_CALLBACK" "$MIRROR_NAME"
fi
if [ -x "$UNION_LOCATION/$NEW_MIRROR_CALLBACK" ]; then
echo "Running New Mirror Callback"
enter_mirror "$MIRROR_NAME" "$NEW_MIRROR_CALLBACK" "$MIRROR_NAME"
fi
Something went wrong with that request. Please try again.