Skip to content

Instantly share code, notes, and snippets.

@rak16
Last active February 6, 2026 08:43
Show Gist options
  • Select an option

  • Save rak16/e1cacf7863ad0dee7a70de6794f71636 to your computer and use it in GitHub Desktop.

Select an option

Save rak16/e1cacf7863ad0dee7a70de6794f71636 to your computer and use it in GitHub Desktop.
Simple script to generate container images. You can configure the total size and number of layers. The script generates a Dockerfile along with bin files, which can be then used to build an image. Note that the unit used is GB and not GiB.
#!/bin/bash
# Usage: ./generate_strict_gb.sh <TOTAL_GB_SI> <LAYER_COUNT> <OUTPUT_DIR>
TOTAL_GB=$1
LAYERS=$2
OUT_DIR=$3
if [ -z "$OUT_DIR" ]; then
echo "Usage: $0 <TOTAL_GB> <LAYERS> <OUTPUT_DIR>"
echo "Note: Uses strict SI Gigabytes (1 GB = 1,000,000,000 bytes)"
exit 1
fi
mkdir -p "$OUT_DIR"
# Use scratch for zero overhead
cat <<EOF > "$OUT_DIR/Dockerfile"
FROM scratch
EOF
# Total bytes per layer
BYTES_PER_LAYER=$(python3 -c "print(int(($TOTAL_GB * 1000000000) / $LAYERS))")
MB_DISPLAY=$(python3 -c "print(round($BYTES_PER_LAYER / 1000000, 2))")
# Chunk size configuration (safe limit - 500MB per write)
SAFE_CHUNK_SIZE=500000000
echo "----------------------------------------------------------------"
echo "Target: ${TOTAL_GB} GB (SI) | Layers: ${LAYERS}"
echo "Per Layer: ${BYTES_PER_LAYER} bytes (~${MB_DISPLAY} MB)"
echo "Base Image: scratch (0 bytes overhead)"
echo "----------------------------------------------------------------"
for i in $(seq 1 $LAYERS); do
FILENAME="layer_${i}.bin"
FILEPATH="$OUT_DIR/$FILENAME"
echo -ne "Generating $FILENAME ... "
# Reset/Create empty file
: > "$FILEPATH"
CURRENT_SIZE=0
REMAINING=$BYTES_PER_LAYER
# Append chunks until layer is full
while [ $REMAINING -gt 0 ]; do
if [ $REMAINING -ge $SAFE_CHUNK_SIZE ]; then
NEXT_CHUNK=$SAFE_CHUNK_SIZE
else
NEXT_CHUNK=$REMAINING
fi
# Append random data to the file
openssl rand $NEXT_CHUNK >> "$FILEPATH"
REMAINING=$((REMAINING - NEXT_CHUNK))
done
echo "Done."
# Copy to root since we have no WORKDIR
echo "COPY $FILENAME /$FILENAME" >> "$OUT_DIR/Dockerfile"
done
echo -e "\n✅ Context generated in: $OUT_DIR"

Comments are disabled for this gist.