From 1187f9d5df6d6eddba478ac75d3834391480755f Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Sun, 7 Jun 2015 16:44:10 -0400 Subject: [PATCH] Add docker output support We can already produce tarballs, which is the input format docker import expects. This makes it trivial to add docker as an output format. Co-Authored-By: Dan Prince Change-Id: Ib60db3b717d33d4cf3181d70fe0ffbfa86fd5d02 --- bin/disk-image-create | 22 ++++++++++++++++++++-- tests/image_output_formats.bash | 12 +++++++----- tests/test_functions.bash | 25 +++++++++++++++++++------ 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/bin/disk-image-create b/bin/disk-image-create index 2778218b4..4f9a1c5f6 100755 --- a/bin/disk-image-create +++ b/bin/disk-image-create @@ -45,7 +45,7 @@ function show_options () { echo "Options:" echo " -a i386|amd64|armhf -- set the architecture of the image(default amd64)" echo " -o imagename -- set the imagename of the output image file(default image)" - echo " -t qcow2,tar,vhd,raw -- set the image types of the output image files (default qcow2)" + echo " -t qcow2,tar,vhd,docker,raw -- set the image types of the output image files (default qcow2)" echo " File types should be comma separated. VHD outputting requires the vhd-util" echo " executable be in your PATH." echo " -x -- turn on tracing" @@ -71,6 +71,7 @@ function show_options () { echo " Defaults to 'ramdisk'. Should be set to 'dracut-ramdisk' for platforms such" echo " as RHEL and CentOS that do not package busybox." echo " --install-type -- specify the default installation type. Defaults to 'source'. Set to 'package' to use package based installations by default." + echo " --docker-target -- specify the repo and tag to use if the output type is docker. Defaults to the value of output imagename" if [ "$IS_RAMDISK" == "0" ]; then echo " -n skip the default inclusion of the 'base' element" echo " -p package[,package,package] -- list of packages to install in the image" @@ -102,7 +103,8 @@ COMPRESS_IMAGE="true" export DIB_ROOT_LABEL="" DIB_DEFAULT_INSTALLTYPE=${DIB_DEFAULT_INSTALLTYPE:-"source"} MKFS_OPTS="" -TEMP=`getopt -o a:ho:t:xucnp: -l no-tmpfs,offline,help,min-tmpfs:,image-size:,image-cache:,max-online-resize:,mkfs-options:,qemu-img-options:,ramdisk-element:,root-label:,install-type: -n $SCRIPTNAME -- "$@"` +DOCKER_TARGET="" +TEMP=`getopt -o a:ho:t:xucnp: -l no-tmpfs,offline,help,min-tmpfs:,image-size:,image-cache:,max-online-resize:,mkfs-options:,qemu-img-options:,ramdisk-element:,root-label:,install-type:,docker-target: -n $SCRIPTNAME -- "$@"` if [ $? -ne 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi # Note the quotes around `$TEMP': they are essential! @@ -130,6 +132,7 @@ while true ; do --root-label) export DIB_ROOT_LABEL=$2; shift 2;; --ramdisk-element) RAMDISK_ELEMENT=$2; shift 2;; --install-type) DIB_DEFAULT_INSTALLTYPE=$2; shift 2;; + --docker-target) export DOCKER_TARGET=$2; shift 2 ;; --) shift ; break ;; *) echo "Internal error!" ; exit 1 ;; esac @@ -179,6 +182,16 @@ for X in ${!IMAGE_TYPES[@]}; do exit 1 fi ;; + docker) + if [ -z "$(which docker)" ]; then + echo "docker output format specified but no docker executable found." + exit 1 + fi + if [ -z "$DOCKER_TARGET" ]; then + echo "Please set --docker-target." + exit 1 + fi + ;; esac done @@ -271,6 +284,11 @@ for X in ${!IMAGE_TYPES[@]} ; do --exclude ./proc --xattrs --xattrs-include=\* . sudo chown $USER: $IMAGE_NAME.tar unset IMAGE_TYPES[$X] + elif [ "${IMAGE_TYPES[$X]}" == "docker" ]; then + sudo tar -C ${TMP_BUILD_DIR}/mnt -cf - --exclude ./sys \ + --exclude ./proc --xattrs --xattrs-include=\* . \ + | docker import - $DOCKER_TARGET + unset IMAGE_TYPES[$X] fi done diff --git a/tests/image_output_formats.bash b/tests/image_output_formats.bash index 57ffa72e8..dd8248010 100755 --- a/tests/image_output_formats.bash +++ b/tests/image_output_formats.bash @@ -5,11 +5,13 @@ set -o pipefail source $(dirname $0)/test_functions.bash -test_formats="tar raw qcow2" -if [ -z "$(which qemu-img)" ]; then - echo "Warning: No qemu-img binary found, cowardly refusing to run tests." - exit 0 -fi +test_formats="tar raw qcow2 docker" +for binary in qemu-img docker ; do + if [ -z "$(which $binary)" ]; then + echo "Warning: No $binary binary found, cowardly refusing to run tests." + exit 0 + fi +done for format in '' $test_formats; do build_test_image $format diff --git a/tests/test_functions.bash b/tests/test_functions.bash index 810daba29..67f28bfd4 100644 --- a/tests/test_functions.bash +++ b/tests/test_functions.bash @@ -12,25 +12,38 @@ function build_test_image() { format="qcow2" fi dest_dir=$(mktemp -d) + base_dest=$(basename $dest_dir) trap "rm -rf $dest_dir" EXIT + trap "docker rmi $base_dest/image" EXIT ELEMENTS_PATH=$DIB_ELEMENTS:$TEST_ELEMENTS \ - $DIB_CMD -x $type_arg -o $dest_dir/image -n fake-os + $DIB_CMD -x $type_arg --docker-target=$base_dest/image \ + -o $dest_dir/image -n fake-os format=$(echo $format | tr ',' ' ') for format in $format; do - img_path="$dest_dir/image.$format" - if ! [ -f "$img_path" ]; then - echo "Error: No image with name $img_path found!" - exit 1 + if [ $format != 'docker' ]; then + img_path="$dest_dir/image.$format" + if ! [ -f "$img_path" ]; then + echo "Error: No image with name $img_path found!" + exit 1 + else + echo "Found image $img_path." + fi else - echo "Found image $img_path." + if ! docker image | grep $base_dest/image ; then + echo "Error: No docker image with name $base_dest/image found!" + exit 1 + else + echo "Found docker image $base_dest/image" + fi fi done trap EXIT rm -rf $dest_dir + docker rmi $base_dest/image } function run_element_test() {