From 0319228a458f471c017c326ac39fdb6925156271 Mon Sep 17 00:00:00 2001 From: Justin Bedo Date: Sat, 8 May 2021 21:49:39 +1000 Subject: [PATCH 1/6] docker-tools: add example for exportImage functionality and test --- nixos/tests/docker-tools.nix | 5 +++++ pkgs/build-support/docker/examples.nix | 3 +++ 2 files changed, 8 insertions(+) diff --git a/nixos/tests/docker-tools.nix b/nixos/tests/docker-tools.nix index 4c3c26980aa2..1f0088d63fda 100644 --- a/nixos/tests/docker-tools.nix +++ b/nixos/tests/docker-tools.nix @@ -378,5 +378,10 @@ import ./make-test-python.nix ({ pkgs, ... }: { docker.succeed( "docker run --rm ${examples.layeredImageWithFakeRootCommands.imageName} sh -c 'stat -c '%u' /home/jane | grep -E ^1000$'" ) + + with subtest("exportImage produces a valid tarball"): + docker.succeed( + "tar -tf ${examples.exportBash} > /dev/null" + ) ''; }) diff --git a/pkgs/build-support/docker/examples.nix b/pkgs/build-support/docker/examples.nix index c66aca56fea0..6ec25711dd18 100644 --- a/pkgs/build-support/docker/examples.nix +++ b/pkgs/build-support/docker/examples.nix @@ -541,4 +541,7 @@ rec { config.Cmd = [ "hello" ]; includeStorePaths = false; }; + + # Example export of the bash image + exportBash = pkgs.dockerTools.exportImage { fromImage = bash; }; } From fa0cc611ff8ae49efd76e2ed7700a2cdef5707c4 Mon Sep 17 00:00:00 2001 From: Justin Bedo Date: Fri, 14 Feb 2020 09:27:07 +1100 Subject: [PATCH 2/6] dockerTools: fix export --- pkgs/build-support/docker/default.nix | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pkgs/build-support/docker/default.nix b/pkgs/build-support/docker/default.nix index 4bda4d2a5c23..2d327a9426dd 100644 --- a/pkgs/build-support/docker/default.nix +++ b/pkgs/build-support/docker/default.nix @@ -280,9 +280,13 @@ rec { ''); in runCommand name { } '' - mkdir -p $out cd ${result} - cp layer.tar json VERSION $out + if [ -e json ] && [ -e VERSION ] ; then + mkdir -p $out + cp layer.tar json VERSION $out + else + cp layer.tar $out + fi ''; exportImage = { name ? fromImage.name, fromImage, fromImageName ? null, fromImageTag ? null, diskSize ? 1024 }: @@ -291,7 +295,7 @@ rec { postMount = '' echo "Packing raw image..." - tar -C mnt --hard-dereference --sort=name --mtime="@$SOURCE_DATE_EPOCH" -cf $out . + tar -C mnt --hard-dereference --sort=name --mtime="@$SOURCE_DATE_EPOCH" -cf $out/layer.tar . ''; }; From 8863a5199db598b4a0d23c9997a525a9457cbcb7 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 29 Sep 2021 09:33:14 +0200 Subject: [PATCH 3/6] vmTools.createEmptyImage: Add destination parameter --- pkgs/build-support/vm/default.nix | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/pkgs/build-support/vm/default.nix b/pkgs/build-support/vm/default.nix index 3f819b44e612..c501f63ef00a 100644 --- a/pkgs/build-support/vm/default.nix +++ b/pkgs/build-support/vm/default.nix @@ -257,14 +257,23 @@ rec { eval "$postVM" ''; - - createEmptyImage = {size, fullName}: '' - mkdir $out - diskImage=$out/disk-image.qcow2 + /* + A bash script fragment that produces a disk image at `destination`. + */ + createEmptyImage = { + # Disk image size in MiB + size, + # Name that will be written to ${destination}/nix-support/full-name + fullName, + # Where to write the image files, defaulting to $out + destination ? "$out" + }: '' + mkdir -p ${destination} + diskImage=${destination}/disk-image.qcow2 ${qemu}/bin/qemu-img create -f qcow2 $diskImage "${toString size}M" - mkdir $out/nix-support - echo "${fullName}" > $out/nix-support/full-name + mkdir ${destination}/nix-support + echo "${fullName}" > ${destination}/nix-support/full-name ''; From 63bf4539b9eaecee667100197ebf6f2be6522914 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 29 Sep 2021 09:34:06 +0200 Subject: [PATCH 4/6] dockerTools.runWithOverlay: Avoid cluttering $out and copying --- pkgs/build-support/docker/default.nix | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/pkgs/build-support/docker/default.nix b/pkgs/build-support/docker/default.nix index 2d327a9426dd..edb8837015c8 100644 --- a/pkgs/build-support/docker/default.nix +++ b/pkgs/build-support/docker/default.nix @@ -191,13 +191,13 @@ rec { , postMount ? "" , postUmount ? "" }: - let - result = vmTools.runInLinuxVM ( + vmTools.runInLinuxVM ( runCommand name { preVM = vmTools.createEmptyImage { size = diskSize; fullName = "docker-run-disk"; + destination = "./image"; }; inherit fromImage fromImageName fromImageTag; @@ -278,16 +278,6 @@ rec { ${postUmount} ''); - in - runCommand name { } '' - cd ${result} - if [ -e json ] && [ -e VERSION ] ; then - mkdir -p $out - cp layer.tar json VERSION $out - else - cp layer.tar $out - fi - ''; exportImage = { name ? fromImage.name, fromImage, fromImageName ? null, fromImageTag ? null, diskSize ? 1024 }: runWithOverlay { From 1a0edf135a3000a6af7200d4f2e4e344922f186d Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 29 Sep 2021 09:34:45 +0200 Subject: [PATCH 5/6] dockerTools.exportImage: Make $out a tarball again --- pkgs/build-support/docker/default.nix | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkgs/build-support/docker/default.nix b/pkgs/build-support/docker/default.nix index edb8837015c8..47fd99c12f8e 100644 --- a/pkgs/build-support/docker/default.nix +++ b/pkgs/build-support/docker/default.nix @@ -287,6 +287,12 @@ rec { echo "Packing raw image..." tar -C mnt --hard-dereference --sort=name --mtime="@$SOURCE_DATE_EPOCH" -cf $out/layer.tar . ''; + + postUmount = '' + mv $out/layer.tar . + rm -rf $out + mv layer.tar $out + ''; }; # Create an executable shell script which has the coreutils in its From 020e88bf7a46a089b94cc6ae962d5bada7233341 Mon Sep 17 00:00:00 2001 From: Justin Bedo Date: Wed, 29 Sep 2021 21:40:31 +1000 Subject: [PATCH 6/6] nixos/tests/docker-tools: check explicitly for file in exportImage --- nixos/tests/docker-tools.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nixos/tests/docker-tools.nix b/nixos/tests/docker-tools.nix index 1f0088d63fda..a3ab5826960e 100644 --- a/nixos/tests/docker-tools.nix +++ b/nixos/tests/docker-tools.nix @@ -381,7 +381,7 @@ import ./make-test-python.nix ({ pkgs, ... }: { with subtest("exportImage produces a valid tarball"): docker.succeed( - "tar -tf ${examples.exportBash} > /dev/null" + "tar -tf ${examples.exportBash} | grep '\./bin/bash' > /dev/null" ) ''; })