From 84e04ccf8570e9f8072486f7d750d326225c7117 Mon Sep 17 00:00:00 2001 From: Andrew Brooks Date: Mon, 6 Feb 2023 12:19:29 -0600 Subject: [PATCH] dockerTools: Preprocess layers list before unpack to handle repeated layers --- pkgs/build-support/docker/default.nix | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/pkgs/build-support/docker/default.nix b/pkgs/build-support/docker/default.nix index 0e4011a532b3..7fa5aeafc8e3 100644 --- a/pkgs/build-support/docker/default.nix +++ b/pkgs/build-support/docker/default.nix @@ -229,6 +229,15 @@ rec { mount /dev/${vmTools.hd} disk cd disk + function dedup() { + declare -A seen + while read ln; do + if [[ -z "''${seen["$ln"]:-}" ]]; then + echo "$ln"; seen["$ln"]=1 + fi + done + } + if [[ -n "$fromImage" ]]; then echo "Unpacking base image..." mkdir image @@ -245,7 +254,8 @@ rec { parentID="$(cat "image/manifest.json" | jq -r '.[0].Config | rtrimstr(".json")')" fi - cat ./image/manifest.json | jq -r '.[0].Layers | .[]' > layer-list + # In case of repeated layers, unpack only the last occurrence of each + cat ./image/manifest.json | jq -r '.[0].Layers | .[]' | tac | dedup | tac > layer-list else touch layer-list fi @@ -259,14 +269,13 @@ rec { mkdir -p image/$extractionID/layer tar -C image/$extractionID/layer -xpf image/$layerTar + rm image/$layerTar find image/$extractionID/layer -name ".wh.*" -exec bash -c 'name="$(basename {}|sed "s/^.wh.//")"; mknod "$(dirname {})/$name" c 0 0; rm {}' \; # Get the next lower directory and continue the loop. lowerdir=image/$extractionID/layer''${lowerdir:+:}$lowerdir done - # Don't remove tarballs until all unpacked in case some are used more than once - awk '{print "image/"$0}' layer-list | xargs rm -f mkdir work mkdir layer