2015-03-27 04:38:07 -04:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -e
|
|
|
|
|
|
|
|
if ! command -v qemu-nbd &> /dev/null; then
|
2020-03-02 22:27:49 -05:00
|
|
|
echo >&2 'error: "qemu-nbd" not found!'
|
|
|
|
exit 1
|
2015-03-27 04:38:07 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
usage() {
|
2020-03-02 22:27:49 -05:00
|
|
|
echo "Convert disk image to docker image"
|
|
|
|
echo ""
|
|
|
|
echo "usage: $0 image-name disk-image-file [ base-image ]"
|
|
|
|
echo " ie: $0 cirros:0.3.3 cirros-0.3.3-x86_64-disk.img"
|
|
|
|
echo " $0 ubuntu:cloud ubuntu-14.04-server-cloudimg-amd64-disk1.img ubuntu:14.04"
|
2015-03-27 04:38:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if [ "$#" -lt 2 ]; then
|
2020-03-02 22:27:49 -05:00
|
|
|
usage
|
|
|
|
exit 1
|
2015-03-27 04:38:07 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
CURDIR=$(pwd)
|
|
|
|
|
|
|
|
image_name="${1%:*}"
|
|
|
|
image_tag="${1#*:}"
|
|
|
|
if [ "$image_tag" == "$1" ]; then
|
2020-03-02 22:27:49 -05:00
|
|
|
image_tag="latest"
|
2015-03-27 04:38:07 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
disk_image_file="$2"
|
|
|
|
docker_base_image="$3"
|
|
|
|
|
|
|
|
block_device=/dev/nbd0
|
|
|
|
|
|
|
|
builddir=$(mktemp -d)
|
|
|
|
|
|
|
|
cleanup() {
|
2020-03-02 22:27:49 -05:00
|
|
|
umount "$builddir/disk_image" || true
|
|
|
|
umount "$builddir/workdir" || true
|
|
|
|
qemu-nbd -d $block_device &> /dev/null || true
|
|
|
|
rm -rf $builddir
|
2015-03-27 04:38:07 -04:00
|
|
|
}
|
|
|
|
trap cleanup EXIT
|
|
|
|
|
|
|
|
# Mount disk image
|
|
|
|
modprobe nbd max_part=63
|
|
|
|
qemu-nbd -rc ${block_device} -P 1 "$disk_image_file"
|
|
|
|
mkdir "$builddir/disk_image"
|
|
|
|
mount -o ro ${block_device} "$builddir/disk_image"
|
|
|
|
|
|
|
|
mkdir "$builddir/workdir"
|
|
|
|
mkdir "$builddir/diff"
|
|
|
|
|
|
|
|
base_image_mounts=""
|
|
|
|
|
|
|
|
# Unpack base image
|
|
|
|
if [ -n "$docker_base_image" ]; then
|
2020-03-02 22:27:49 -05:00
|
|
|
mkdir -p "$builddir/base"
|
|
|
|
docker pull "$docker_base_image"
|
|
|
|
docker save "$docker_base_image" | tar -xC "$builddir/base"
|
|
|
|
|
|
|
|
image_id=$(docker inspect -f "{{.Id}}" "$docker_base_image")
|
|
|
|
while [ -n "$image_id" ]; do
|
|
|
|
mkdir -p "$builddir/base/$image_id/layer"
|
|
|
|
tar -xf "$builddir/base/$image_id/layer.tar" -C "$builddir/base/$image_id/layer"
|
|
|
|
|
|
|
|
base_image_mounts="${base_image_mounts}:$builddir/base/$image_id/layer=ro+wh"
|
|
|
|
image_id=$(docker inspect -f "{{.Parent}}" "$image_id")
|
|
|
|
done
|
2015-03-27 04:38:07 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Mount work directory
|
|
|
|
mount -t aufs -o "br=$builddir/diff=rw${base_image_mounts},dio,xino=/dev/shm/aufs.xino" none "$builddir/workdir"
|
|
|
|
|
|
|
|
# Update files
|
|
|
|
cd $builddir
|
2015-07-02 18:16:33 -04:00
|
|
|
LC_ALL=C diff -rq disk_image workdir \
|
2020-03-02 22:27:49 -05:00
|
|
|
| sed -re "s|Only in workdir(.*?): |DEL \1/|g;s|Only in disk_image(.*?): |ADD \1/|g;s|Files disk_image/(.+) and workdir/(.+) differ|UPDATE /\1|g" \
|
|
|
|
| while read action entry; do
|
|
|
|
case "$action" in
|
|
|
|
ADD | UPDATE)
|
|
|
|
cp -a "disk_image$entry" "workdir$entry"
|
|
|
|
;;
|
|
|
|
DEL)
|
|
|
|
rm -rf "workdir$entry"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Error: unknown diff line: $action $entry" >&2
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
2015-03-27 04:38:07 -04:00
|
|
|
|
|
|
|
# Pack new image
|
|
|
|
new_image_id="$(for i in $(seq 1 32); do printf "%02x" $(($RANDOM % 256)); done)"
|
|
|
|
mkdir -p $builddir/result/$new_image_id
|
|
|
|
cd diff
|
|
|
|
tar -cf $builddir/result/$new_image_id/layer.tar *
|
|
|
|
echo "1.0" > $builddir/result/$new_image_id/VERSION
|
2020-03-02 22:27:49 -05:00
|
|
|
cat > $builddir/result/$new_image_id/json <<- EOS
|
|
|
|
{ "docker_version": "1.4.1"
|
|
|
|
, "id": "$new_image_id"
|
|
|
|
, "created": "$(date -u +%Y-%m-%dT%H:%M:%S.%NZ)"
|
2015-03-27 04:38:07 -04:00
|
|
|
EOS
|
|
|
|
|
|
|
|
if [ -n "$docker_base_image" ]; then
|
2020-03-02 22:27:49 -05:00
|
|
|
image_id=$(docker inspect -f "{{.Id}}" "$docker_base_image")
|
|
|
|
echo ", \"parent\": \"$image_id\"" >> $builddir/result/$new_image_id/json
|
2015-03-27 04:38:07 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo "}" >> $builddir/result/$new_image_id/json
|
|
|
|
|
|
|
|
echo "{\"$image_name\":{\"$image_tag\":\"$new_image_id\"}}" > $builddir/result/repositories
|
|
|
|
|
|
|
|
cd $builddir/result
|
|
|
|
|
|
|
|
# mkdir -p $CURDIR/$image_name
|
|
|
|
# cp -r * $CURDIR/$image_name
|
|
|
|
tar -c * | docker load
|