From 87677e531c29a54b926b0a40911201f182797c71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Gonz=C3=A1lez?= Date: Mon, 18 May 2020 12:25:13 +0200 Subject: [PATCH] M #: Improve create_docker_image script --- .../remotes/docker_downloader.sh | 8 +--- src/mad/sh/create_docker_image.sh | 41 +++++++++++-------- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/datastore_mad/remotes/docker_downloader.sh b/src/datastore_mad/remotes/docker_downloader.sh index e51122b9b9..ecce3a14fe 100755 --- a/src/datastore_mad/remotes/docker_downloader.sh +++ b/src/datastore_mad/remotes/docker_downloader.sh @@ -90,11 +90,6 @@ function clean { docker rm -f $container_id > /dev/null 2>&1 || true docker image rm -f one$sid > /dev/null 2>&1 - # Unmount mnt directory (if necessary) - if grep -qs "$dockerdir/mnt" /proc/mounts; then - sudo -n $MK_DOCKER -a "CLEAN" -d $dockerdir - fi - rm -rf $dockerdir } @@ -141,7 +136,6 @@ img_raw=$dockerdir/img.raw img_qcow=$dockerdir/img.qcow # Trap for cleaning temporary directories -trap clean ERR trap clean EXIT mkdir -p $dockerdir @@ -309,7 +303,7 @@ esac # Mount container disk image and untar rootfs contents to it #------------------------------------------------------------------------------- -sudo -n $MK_DOCKER -a "CREATE" -d $dockerdir -i $img_raw -t $tarball +sudo -n $MK_DOCKER -d $dockerdir -i $img_raw -t $tarball if [ "$format" == "qcow2" ]; then qemu-img convert -f raw -O qcow2 $img_raw $img_qcow > /dev/null 2>&1 diff --git a/src/mad/sh/create_docker_image.sh b/src/mad/sh/create_docker_image.sh index b697554113..fdaa07f23d 100755 --- a/src/mad/sh/create_docker_image.sh +++ b/src/mad/sh/create_docker_image.sh @@ -19,41 +19,46 @@ # exit when any command fails set -e +function clean { + if grep -qs "$dockerdir/mnt" /proc/mounts; then + umount $dockerdir/mnt + fi +} + #------------------------------------------------------------------------------- # Configuration attributes and parameters #------------------------------------------------------------------------------- img_raw="" dockerdir="" tarball="" -action="" while getopts ":i:d:t:a:" opt; do case $opt in i) img_raw="$OPTARG" ;; d) dockerdir="$OPTARG" ;; t) tarball="$OPTARG" ;; - a) action="$OPTARG" ;; esac done -if [ -z "$action" ]; then +# Check img_raw is a valid file +if [ ! -f "$img_raw" ]; then exit -1 fi -if [ $action == "CREATE" ]; then - # Mount container disk image and untar rootfs contents to it - mount -o noexec,nodev $img_raw $dockerdir/mnt > /dev/null 2>&1 - chmod o+w $dockerdir/mnt - tar xpf $tarball -C $dockerdir/mnt > /dev/null 2>&1 - - sync - - umount $dockerdir/mnt - - exit 0 -elif [ $action == "CLEAN" ]; then - umount "$dockerdir/mnt" - - exit 0 +# Check dockerdir is different than / and the directory name is an uuid +regex_uuid="^\{?[A-F0-9a-f]{8}-[A-F0-9a-f]{4}-[A-F0-9a-f]{4}-[A-F0-9a-f]{4}-[A-F0-9a-f]{12}\}?$" +if [[ ! `echo $dockerdir | awk -F/ '{print $NF}'` =~ $regex_uuid ]]; then + exit -1 fi +trap clean EXIT + +# Mount container disk image and untar rootfs contents to it +mount -o noexec,nodev $img_raw $dockerdir/mnt > /dev/null 2>&1 +chmod o+w $dockerdir/mnt +tar xpf $tarball -C $dockerdir/mnt > /dev/null 2>&1 + +sync + +exit 0 +