From 1d51930373549735944fd2ab586a20df39ad0c82 Mon Sep 17 00:00:00 2001 From: Kai Takahashi Date: Fri, 26 Jul 2019 09:58:21 +0000 Subject: [PATCH 1/3] Extract systemvm.iso using bsdtar if available. Signed-off-by: Kai Takahashi --- scripts/vm/systemvm/injectkeys.sh | 83 ++++++++++++++++++------------- 1 file changed, 49 insertions(+), 34 deletions(-) diff --git a/scripts/vm/systemvm/injectkeys.sh b/scripts/vm/systemvm/injectkeys.sh index 9df1718253f2..cc4fa3ed257a 100755 --- a/scripts/vm/systemvm/injectkeys.sh +++ b/scripts/vm/systemvm/injectkeys.sh @@ -6,9 +6,9 @@ # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -33,32 +33,56 @@ clean_up() { $SUDO umount $MOUNTPATH } +clean_up_bsdtar() { + rm -rf --preserve-root $MOUNTPATH +} + +backup_iso() { + $SUDO cp -b ${systemvmpath} ${systemvmpath}.bak +} + inject_into_iso() { local isofile=${systemvmpath} local newpubkey=$2 - local backup=${isofile}.bak local tmpiso=${TMP}/$1 mkdir -p $MOUNTPATH [ ! -f $isofile ] && echo "$(basename $0): Could not find systemvm iso patch file $isofile" && return 1 - $SUDO mount -o loop $isofile $MOUNTPATH - [ $? -ne 0 ] && echo "$(basename $0): Failed to mount original iso $isofile" && clean_up && return 1 - diff -q $MOUNTPATH/authorized_keys $newpubkey &> /dev/null && clean_up && return 0 - $SUDO cp -b $isofile $backup - [ $? -ne 0 ] && echo "$(basename $0): Failed to backup original iso $isofile" && clean_up && return 1 - rm -rf $TMPDIR - mkdir -p $TMPDIR - [ ! -d $TMPDIR ] && echo "$(basename $0): Could not find/create temporary dir $TMPDIR" && clean_up && return 1 - $SUDO cp -fr $MOUNTPATH/* $TMPDIR/ - [ $? -ne 0 ] && echo "$(basename $0): Failed to copy from original iso $isofile" && clean_up && return 1 - $SUDO cp $newpubkey $TMPDIR/authorized_keys - [ $? -ne 0 ] && echo "$(basename $0): Failed to copy key $newpubkey from original iso to new iso " && clean_up && return 1 - mkisofs -quiet -r -o $tmpiso $TMPDIR - [ $? -ne 0 ] && echo "$(basename $0): Failed to create new iso $tmpiso from $TMPDIR" && clean_up && return 1 - $SUDO umount $MOUNTPATH - [ $? -ne 0 ] && echo "$(basename $0): Failed to unmount old iso from $MOUNTPATH" && return 1 - $SUDO cp -f $tmpiso $isofile - [ $? -ne 0 ] && echo "$(basename $0): Failed to overwrite old iso $isofile with $tmpiso" && return 1 - rm -rf $TMPDIR + if [ -x "$(command -v bsdtar)" ]; then + bsdtar -C $MOUNTPATH -xf $isofile + [ $? -ne 0 ] && echo "$(basename $0): Failed to extract original iso $isofile" && clean_up_bsdtar && return 1 + diff -q $MOUNTPATH/authorized_keys $newpubkey &> /dev/null && clean_up_bsdtar && return 0 + backup_iso + [ $? -ne 0 ] && echo "$(basename $0): Failed to backup original iso $isofile" && clean_up_bsdtar && return 1 + $SUDO cp $newpubkey $MOUNTPATH/authorized_keys + [ $? -ne 0 ] && echo "$(basename $0): Failed to copy key $newpubkey from original iso to new iso " && clean_up_bsdtar && return 1 + mkisofs -quiet -r -o $tmpiso $MOUNTPATH + [ $? -ne 0 ] && echo "$(basename $0): Failed to create new iso $tmpiso from $MOUNTPATH" && clean_up_bsdtar && return 1 + $SUDO cp -f $tmpiso $isofile + [ $? -ne 0 ] && echo "$(basename $0): Failed to overwrite old iso $isofile with $tmpiso" && return 1 + clean_up_bsdtar + else + $SUDO mount -o loop $isofile $MOUNTPATH + [ $? -ne 0 ] && echo "$(basename $0): Failed to mount original iso $isofile" && clean_up && return 1 + diff -q $MOUNTPATH/authorized_keys $newpubkey &> /dev/null && clean_up && return 0 + backup_iso + [ $? -ne 0 ] && echo "$(basename $0): Failed to backup original iso $isofile" && clean_up && return 1 + # + rm -rf $TMPDIR + mkdir -p $TMPDIR + [ ! -d $TMPDIR ] && echo "$(basename $0): Could not find/create temporary dir $TMPDIR" && clean_up && return 1 + $SUDO cp -fr $MOUNTPATH/* $TMPDIR/ + [ $? -ne 0 ] && echo "$(basename $0): Failed to copy from original iso $isofile" && clean_up && return 1 + # + $SUDO cp $newpubkey $TMPDIR/authorized_keys + [ $? -ne 0 ] && echo "$(basename $0): Failed to copy key $newpubkey from original iso to new iso " && clean_up && return 1 + mkisofs -quiet -r -o $tmpiso $TMPDIR + [ $? -ne 0 ] && echo "$(basename $0): Failed to create new iso $tmpiso from $TMPDIR" && clean_up && return 1 + $SUDO umount $MOUNTPATH + [ $? -ne 0 ] && echo "$(basename $0): Failed to unmount old iso from $MOUNTPATH" && return 1 + $SUDO cp -f $tmpiso $isofile + [ $? -ne 0 ] && echo "$(basename $0): Failed to overwrite old iso $isofile with $tmpiso" && return 1 + rm -rf $TMPDIR + fi } copy_priv_key() { @@ -74,7 +98,7 @@ then SUDO="sudo -n " fi -$SUDO mkdir -p $MOUNTPATH +mkdir -p $MOUNTPATH [ $# -ne 3 ] && echo "Usage: $(basename $0) " && exit 3 newpubkey=$1 @@ -85,17 +109,8 @@ systemvmpath=$3 command -v mkisofs > /dev/null || (echo "$(basename $0): mkisofs not found, please install or ensure PATH is accurate" ; exit 4) -# if running into Docker as unprivileges, skip ssh verification as iso cannot be mounted due to missing loop device. -if [ -f /.dockerenv ]; then - if [ -e /dev/loop0 ]; then - # it's a docker instance with privileges. - inject_into_iso systemvm.iso $newpubkey - [ $? -ne 0 ] && exit 5 - copy_priv_key $newprivkey - else - # this mean it's a docker instance, ssh key cannot be verify. - echo "We run inside Docker, skipping ssh key insertion in systemvm.iso" - fi +if [ ! -e /dev/loop0 ] && [ ! -x "$(command -v bsdtar)" ]; then + echo "Loop device is missing and bsdtar is unavailable. Skipping ssh key insertion in systemvm.iso" else inject_into_iso systemvm.iso $newpubkey [ $? -ne 0 ] && exit 5 From 99acfcf2f4190688009e344c08145ff0a1dd5749 Mon Sep 17 00:00:00 2001 From: Kai Takahashi Date: Fri, 27 Sep 2019 10:55:39 +0900 Subject: [PATCH 2/3] New dependency for CentOS 7 and Debian: bsdtar bsdtar can extract iso images without mounting. Signed-off-by: Kai Takahashi --- debian/control | 2 +- packaging/centos7/cloud.spec | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/debian/control b/debian/control index faedcbc73eb1..7fade97200af 100644 --- a/debian/control +++ b/debian/control @@ -9,7 +9,7 @@ Homepage: http://www.cloudstack.org/ Package: cloudstack-common Architecture: all -Depends: ${misc:Depends}, ${python:Depends}, genisoimage, nfs-common +Depends: ${misc:Depends}, ${python:Depends}, genisoimage, nfs-common, bsdtar Conflicts: cloud-scripts, cloud-utils, cloud-system-iso, cloud-console-proxy, cloud-daemonize, cloud-deps, cloud-python, cloud-setup Description: A common package which contains files which are shared by several CloudStack packages diff --git a/packaging/centos7/cloud.spec b/packaging/centos7/cloud.spec index 3e0dd3cff434..7c98e246029c 100644 --- a/packaging/centos7/cloud.spec +++ b/packaging/centos7/cloud.spec @@ -86,6 +86,7 @@ Requires: python Requires: python3 Requires: python-argparse Requires: python-netaddr +Requires: bsdtar Group: System Environment/Libraries %description common The Apache CloudStack files shared between agent and management server From 6ad2ad836e3cf1480a7c5d828be50d36872a6d9c Mon Sep 17 00:00:00 2001 From: Kai Takahashi Date: Thu, 10 Oct 2019 18:07:41 +0900 Subject: [PATCH 3/3] Remove all 'mount' and 'umount' command call(s). Signed-off-by: Kai Takahashi --- scripts/vm/systemvm/injectkeys.sh | 56 ++++++++----------------------- 1 file changed, 14 insertions(+), 42 deletions(-) diff --git a/scripts/vm/systemvm/injectkeys.sh b/scripts/vm/systemvm/injectkeys.sh index cc4fa3ed257a..b66b8b5f77ef 100755 --- a/scripts/vm/systemvm/injectkeys.sh +++ b/scripts/vm/systemvm/injectkeys.sh @@ -30,10 +30,6 @@ TMPDIR=${TMP}/cloud/systemvm umask 022 clean_up() { - $SUDO umount $MOUNTPATH -} - -clean_up_bsdtar() { rm -rf --preserve-root $MOUNTPATH } @@ -47,42 +43,18 @@ inject_into_iso() { local tmpiso=${TMP}/$1 mkdir -p $MOUNTPATH [ ! -f $isofile ] && echo "$(basename $0): Could not find systemvm iso patch file $isofile" && return 1 - if [ -x "$(command -v bsdtar)" ]; then - bsdtar -C $MOUNTPATH -xf $isofile - [ $? -ne 0 ] && echo "$(basename $0): Failed to extract original iso $isofile" && clean_up_bsdtar && return 1 - diff -q $MOUNTPATH/authorized_keys $newpubkey &> /dev/null && clean_up_bsdtar && return 0 - backup_iso - [ $? -ne 0 ] && echo "$(basename $0): Failed to backup original iso $isofile" && clean_up_bsdtar && return 1 - $SUDO cp $newpubkey $MOUNTPATH/authorized_keys - [ $? -ne 0 ] && echo "$(basename $0): Failed to copy key $newpubkey from original iso to new iso " && clean_up_bsdtar && return 1 - mkisofs -quiet -r -o $tmpiso $MOUNTPATH - [ $? -ne 0 ] && echo "$(basename $0): Failed to create new iso $tmpiso from $MOUNTPATH" && clean_up_bsdtar && return 1 - $SUDO cp -f $tmpiso $isofile - [ $? -ne 0 ] && echo "$(basename $0): Failed to overwrite old iso $isofile with $tmpiso" && return 1 - clean_up_bsdtar - else - $SUDO mount -o loop $isofile $MOUNTPATH - [ $? -ne 0 ] && echo "$(basename $0): Failed to mount original iso $isofile" && clean_up && return 1 - diff -q $MOUNTPATH/authorized_keys $newpubkey &> /dev/null && clean_up && return 0 - backup_iso - [ $? -ne 0 ] && echo "$(basename $0): Failed to backup original iso $isofile" && clean_up && return 1 - # - rm -rf $TMPDIR - mkdir -p $TMPDIR - [ ! -d $TMPDIR ] && echo "$(basename $0): Could not find/create temporary dir $TMPDIR" && clean_up && return 1 - $SUDO cp -fr $MOUNTPATH/* $TMPDIR/ - [ $? -ne 0 ] && echo "$(basename $0): Failed to copy from original iso $isofile" && clean_up && return 1 - # - $SUDO cp $newpubkey $TMPDIR/authorized_keys - [ $? -ne 0 ] && echo "$(basename $0): Failed to copy key $newpubkey from original iso to new iso " && clean_up && return 1 - mkisofs -quiet -r -o $tmpiso $TMPDIR - [ $? -ne 0 ] && echo "$(basename $0): Failed to create new iso $tmpiso from $TMPDIR" && clean_up && return 1 - $SUDO umount $MOUNTPATH - [ $? -ne 0 ] && echo "$(basename $0): Failed to unmount old iso from $MOUNTPATH" && return 1 - $SUDO cp -f $tmpiso $isofile - [ $? -ne 0 ] && echo "$(basename $0): Failed to overwrite old iso $isofile with $tmpiso" && return 1 - rm -rf $TMPDIR - fi + bsdtar -C $MOUNTPATH -xf $isofile + [ $? -ne 0 ] && echo "$(basename $0): Failed to extract original iso $isofile" && clean_up && return 1 + diff -q $MOUNTPATH/authorized_keys $newpubkey &> /dev/null && clean_up && return 0 + backup_iso + [ $? -ne 0 ] && echo "$(basename $0): Failed to backup original iso $isofile" && clean_up && return 1 + $SUDO cp $newpubkey $MOUNTPATH/authorized_keys + [ $? -ne 0 ] && echo "$(basename $0): Failed to copy key $newpubkey from original iso to new iso " && clean_up && return 1 + mkisofs -quiet -r -o $tmpiso $MOUNTPATH + [ $? -ne 0 ] && echo "$(basename $0): Failed to create new iso $tmpiso from $MOUNTPATH" && clean_up && return 1 + $SUDO cp -f $tmpiso $isofile + [ $? -ne 0 ] && echo "$(basename $0): Failed to overwrite old iso $isofile with $tmpiso" && return 1 + clean_up } copy_priv_key() { @@ -109,8 +81,8 @@ systemvmpath=$3 command -v mkisofs > /dev/null || (echo "$(basename $0): mkisofs not found, please install or ensure PATH is accurate" ; exit 4) -if [ ! -e /dev/loop0 ] && [ ! -x "$(command -v bsdtar)" ]; then - echo "Loop device is missing and bsdtar is unavailable. Skipping ssh key insertion in systemvm.iso" +if [ ! -x "$(command -v bsdtar)" ]; then + echo "bsdtar is unavailable. Skipping ssh key insertion in systemvm.iso" else inject_into_iso systemvm.iso $newpubkey [ $? -ne 0 ] && exit 5