Skip to content

Instantly share code, notes, and snippets.

@muzi502
Last active November 5, 2020 01:01
Show Gist options
  • Save muzi502/5982c8bf9af0bb25b64aa2bbe3a1a295 to your computer and use it in GitHub Desktop.
Save muzi502/5982c8bf9af0bb25b64aa2bbe3a1a295 to your computer and use it in GitHub Desktop.
use shell to gc instead /bin/registry command , delete all old images blobs only save the tag latest images blobs
#!/bin/bash
set -x
v2=$1
v2=${v2:="/var/lib/registry/docker/registry/v2"}
cd ${v2}
all_blobs=/tmp/all_blobs.list
: > ${all_blobs}
# delete unlink blob's link file in _layers
for link in $(find repositories -type f -name "link" | grep -E "_layers\/sha256\/.*"); do
link_sha256=$(echo ${link} | grep -Eo "_layers\/sha256\/.*" | sed 's/_layers\/sha256\///g;s/\/link//g')
link_short=${link:0:2}
link_dir=$(echo ${link} | sed 's/\/link//')
data_file=blobs/sha256/${link_short}/${link}
if [[ ! -d ${data_file} ]]; then rm -rf ${link_dir}; fi
done
#marking all the blob by all images manifest
for tag in $(find repositories -name "link" | grep current); do
link=$(cat ${tag} | cut -c8-71)
mfs=blobs/sha256/${link:0:2}/${link}/data
echo ${link} >> ${all_blobs}
grep sha256 ${mfs} |cut -d "\"" -f4 | cut -c8-71 >> ${all_blobs}
done
#delete blob if the blob doesn't exist in all_blobs.list
for blob in $(find blobs -name "data" | cut -d "/" -f4); do
if ! grep ${blob} ${all_blobs}; then
rm -rf blobs/sha256/${blob:0:2}/${blob}
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment