From 9d13084c4183b63587e1f5e4b03395a8df6538f6 Mon Sep 17 00:00:00 2001
From: Tristan Cacqueray <tdecacqu@redhat.com>
Date: Sun, 18 Dec 2016 01:59:07 +0000
Subject: [PATCH] Add squashfs output image format

The squashfs format brings a couple of advantages over the other
formats. Image is often an order of magnitude smaller and it can
be used natively, either as an initrd, either with loop mount.

Change-Id: If72940b0c4dafb2504c52dd0429a8eb3f8305751
---
 bin/disk-image-create                              | 14 ++++++++++++--
 bindep.txt                                         |  1 +
 doc/source/user_guide/building_an_image.rst        |  1 +
 .../notes/squashfs-output-91c1f0dc37474d3c.yaml    |  3 +++
 tests/run_output_format_test.sh                    |  4 ++--
 5 files changed, 19 insertions(+), 4 deletions(-)
 create mode 100644 releasenotes/notes/squashfs-output-91c1f0dc37474d3c.yaml

diff --git a/bin/disk-image-create b/bin/disk-image-create
index eee85c011..e70bc94f1 100755
--- a/bin/disk-image-create
+++ b/bin/disk-image-create
@@ -102,7 +102,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,tgz,vhd,docker,aci,raw -- set the image types of the output image files (default qcow2)"
+    echo "    -t qcow2,tar,tgz,squashfs,vhd,docker,aci,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. ACI outputting requires the ACI_MANIFEST "
     echo "       environment variable be a path to a manifest file."
@@ -268,6 +268,12 @@ for X in ${!IMAGE_TYPES[@]}; do
                 exit 1
             fi
             ;;
+        squashfs)
+            if [ -z "$(which mksquashfs)" ]; then
+                echo "squashfs output format specified but no mksquashfs executable found."
+                exit 1
+            fi
+            ;;
         docker)
             if [ -z "$(which docker)" ]; then
                 echo "docker output format specified but no docker executable found."
@@ -452,6 +458,10 @@ for X in ${!IMAGE_TYPES[@]} ; do
     fi
     sudo chown $USER: $IMAGE_NAME.${IMAGE_TYPES[$X]}
     unset IMAGE_TYPES[$X]
+  elif [ "${IMAGE_TYPES[$x]}" == "squashfs" ]; then
+    sudo mksquashfs ${TMP_BUILD_DIR}/mnt $IMAGE_NAME.squash -comp xz \
+                    -noappend -root-becomes ${TMP_BUILD_DIR}/mnt \
+                    -wildcards -e "proc/*" -e "sys/*" -no-recovery
   elif [ "${IMAGE_TYPES[$X]}" == "docker" ]; then
     sudo tar -C ${TMP_BUILD_DIR}/mnt -cf - --exclude ./sys \
              --exclude ./proc --xattrs --xattrs-include=\* . \
@@ -476,7 +486,7 @@ if [[ (! $IMAGE_ELEMENT =~ no-final-image) && "$IS_RAMDISK" == "0" ]]; then
     # We have to do raw last because it is destructive
     if [ "$IMAGE_TYPE" = "raw" ]; then
       has_raw_type=1
-    else
+    elif [ "$IMAGE_TYPE" != "squashfs" ]; then
       compress_and_save_image $IMAGE_NAME.$IMAGE_TYPE
     fi
   done
diff --git a/bindep.txt b/bindep.txt
index 4f9b42547..5a9c382ed 100644
--- a/bindep.txt
+++ b/bindep.txt
@@ -1,2 +1,3 @@
 # This is a cross-platform list tracking distribution packages needed by tests;
 # see http://docs.openstack.org/infra/bindep/ for additional information.
+squashfs-tools
diff --git a/doc/source/user_guide/building_an_image.rst b/doc/source/user_guide/building_an_image.rst
index a401a96f6..ebacfd9b4 100644
--- a/doc/source/user_guide/building_an_image.rst
+++ b/doc/source/user_guide/building_an_image.rst
@@ -52,6 +52,7 @@ formats are:
  * qcow2
  * tar
  * tgz
+ * squashfs
  * vhd
  * docker
  * raw
diff --git a/releasenotes/notes/squashfs-output-91c1f0dc37474d3c.yaml b/releasenotes/notes/squashfs-output-91c1f0dc37474d3c.yaml
new file mode 100644
index 000000000..98b1c5a2b
--- /dev/null
+++ b/releasenotes/notes/squashfs-output-91c1f0dc37474d3c.yaml
@@ -0,0 +1,3 @@
+---
+features:
+  - New squashfs image output format.
diff --git a/tests/run_output_format_test.sh b/tests/run_output_format_test.sh
index 6d0f21c7f..8f2e968ad 100755
--- a/tests/run_output_format_test.sh
+++ b/tests/run_output_format_test.sh
@@ -59,8 +59,8 @@ function build_test_image() {
     fi
 }
 
-test_formats="tar tgz raw qcow2 docker aci"
-for binary in qemu-img docker ; do
+test_formats="tar tgz squashfs raw qcow2 docker aci"
+for binary in qemu-img docker mksquashfs; do
     if [ -z "$(which $binary)" ]; then
         echo "Warning: No $binary binary found, cowardly refusing to run tests."
         exit 1