|
| 1 | +#!/bin/bash |
| 2 | +# Cleanup Docker orphan overlay2 directories which do pile up over time |
| 3 | +# https://github.com/docker/buildx/issues/2061 |
| 4 | +set -eu |
| 5 | + |
| 6 | +# Define the overlay2 directory |
| 7 | +overlay_dir="/var/lib/docker/overlay2" |
| 8 | + |
| 9 | +# Verify that the overlay2 directory exists |
| 10 | +if [ ! -d "$overlay_dir" ]; then |
| 11 | + echo "The directory $overlay_dir does not exist. Please check the path." |
| 12 | + exit 1 |
| 13 | +fi |
| 14 | + |
| 15 | +# Get all layer IDs associated with current containers (MergedDir, LowerDir, UpperDir, WorkDir) |
| 16 | +container_layer_ids=$(docker ps -qa | xargs -r docker inspect --format '{{ .GraphDriver.Data.MergedDir }} {{ .GraphDriver.Data.LowerDir }} {{ .GraphDriver.Data.UpperDir }} {{ .GraphDriver.Data.WorkDir }}' | tr ' ' '\n' | tr ':' '\n' | awk -F'/' '{print $(NF-1)}' | sort | uniq) |
| 17 | + |
| 18 | +# Get all layer IDs associated with images |
| 19 | +image_layer_ids=$(docker images -qa | xargs -r docker inspect --format '{{ .GraphDriver.Data.MergedDir }} {{ .GraphDriver.Data.LowerDir }} {{ .GraphDriver.Data.UpperDir }} {{ .GraphDriver.Data.WorkDir }}' | tr ' ' '\n' | tr ':' '\n' | awk -F'/' '{print $(NF-1)}' | sort | uniq) |
| 20 | + |
| 21 | +# Get all cache IDs of type source.local |
| 22 | +source_local_cache_ids=$(docker system df -v | grep 'source.local' | awk '{print $1}' | sort | uniq) |
| 23 | + |
| 24 | +# Combine the layer IDs of containers, images, and source.local caches |
| 25 | +all_layer_ids=$(echo -e "$container_layer_ids\n$image_layer_ids" | sort | uniq) |
| 26 | + |
| 27 | +echo "source_local_cache_ids:" |
| 28 | +echo "$source_local_cache_ids" |
| 29 | + |
| 30 | +echo "all_layer_ids:" |
| 31 | +echo "$all_layer_ids" |
| 32 | + |
| 33 | +# List all subdirectories in overlay2 |
| 34 | +overlay_subdirs=$(ls -1 $overlay_dir) |
| 35 | + |
| 36 | +# Find and remove orphan directories that are not in the list of active layers or caches |
| 37 | +echo "Searching for and removing orphan directories in $overlay_dir..." |
| 38 | + |
| 39 | +for dir in $overlay_subdirs; do |
| 40 | + # Ignore directories ending in "-init" and the "l" directory |
| 41 | + if [[ "$dir" == *"-init" ]] || [[ "$dir" == "l" ]]; then |
| 42 | + echo "Ignoring special directory: $overlay_dir/$dir" |
| 43 | + continue |
| 44 | + fi |
| 45 | + |
| 46 | + # Check if the directory name starts with any of the source.local cache IDs |
| 47 | + preserve_dir=false |
| 48 | + for cache_id in $source_local_cache_ids; do |
| 49 | + if [[ "$dir" == "$cache_id"* ]]; then |
| 50 | + preserve_dir=true |
| 51 | + break |
| 52 | + fi |
| 53 | + done |
| 54 | + |
| 55 | + # If directory should be preserved, skip it |
| 56 | + if $preserve_dir; then |
| 57 | + echo "Preserving cache directory: $overlay_dir/$dir" |
| 58 | + continue |
| 59 | + fi |
| 60 | + |
| 61 | + # Check if the directory is associated with an active container or image |
| 62 | + if ! echo "$all_layer_ids" | grep -q "$dir"; then |
| 63 | + echo "Removing orphan directory: $overlay_dir/$dir" |
| 64 | + rm -rf "$overlay_dir/$dir" |
| 65 | + fi |
| 66 | +done |
| 67 | + |
| 68 | +echo "Process completed." |
0 commit comments