1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-16 22:50:10 +03:00

M #: Improve create_docker_image script

This commit is contained in:
Christian González 2020-05-18 12:25:13 +02:00
parent ec252e2aaf
commit 87677e531c
No known key found for this signature in database
GPG Key ID: BC941A50DF6A42EA
2 changed files with 24 additions and 25 deletions

View File

@ -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

View File

@ -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