66# to you under the Apache License, Version 2.0 (the
77# "License"); you may not use this file except in compliance
88# with the License. You may obtain a copy of the License at
9- #
9+ #
1010# http://www.apache.org/licenses/LICENSE-2.0
11- #
11+ #
1212# Unless required by applicable law or agreed to in writing,
1313# software distributed under the License is distributed on an
1414# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -30,35 +30,31 @@ TMPDIR=${TMP}/cloud/systemvm
3030umask 022
3131
3232clean_up () {
33- $SUDO umount $MOUNTPATH
33+ rm -rf --preserve-root $MOUNTPATH
34+ }
35+
36+ backup_iso () {
37+ $SUDO cp -b ${systemvmpath} ${systemvmpath} .bak
3438}
3539
3640inject_into_iso () {
3741 local isofile=${systemvmpath}
3842 local newpubkey=$2
39- local backup=${isofile} .bak
4043 local tmpiso=${TMP} /$1
4144 mkdir -p $MOUNTPATH
4245 [ ! -f $isofile ] && echo " $( basename $0 ) : Could not find systemvm iso patch file $isofile " && return 1
43- $SUDO mount -o loop $isofile $ MOUNTPATH
44- [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to mount original iso $isofile " && clean_up && return 1
46+ bsdtar -C $ MOUNTPATH -xf $isofile
47+ [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to extract original iso $isofile " && clean_up && return 1
4548 diff -q $MOUNTPATH /authorized_keys $newpubkey & > /dev/null && clean_up && return 0
46- $SUDO cp -b $isofile $backup
49+ backup_iso
4750 [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to backup original iso $isofile " && clean_up && return 1
48- rm -rf $TMPDIR
49- mkdir -p $TMPDIR
50- [ ! -d $TMPDIR ] && echo " $( basename $0 ) : Could not find/create temporary dir $TMPDIR " && clean_up && return 1
51- $SUDO cp -fr $MOUNTPATH /* $TMPDIR /
52- [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to copy from original iso $isofile " && clean_up && return 1
53- $SUDO cp $newpubkey $TMPDIR /authorized_keys
51+ $SUDO cp $newpubkey $MOUNTPATH /authorized_keys
5452 [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to copy key $newpubkey from original iso to new iso " && clean_up && return 1
55- mkisofs -quiet -r -o $tmpiso $TMPDIR
56- [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to create new iso $tmpiso from $TMPDIR " && clean_up && return 1
57- $SUDO umount $MOUNTPATH
58- [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to unmount old iso from $MOUNTPATH " && return 1
53+ mkisofs -quiet -r -o $tmpiso $MOUNTPATH
54+ [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to create new iso $tmpiso from $MOUNTPATH " && clean_up && return 1
5955 $SUDO cp -f $tmpiso $isofile
6056 [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to overwrite old iso $isofile with $tmpiso " && return 1
61- rm -rf $TMPDIR
57+ clean_up
6258}
6359
6460copy_priv_key () {
7470 SUDO=" sudo -n "
7571fi
7672
77- $SUDO mkdir -p $MOUNTPATH
73+ mkdir -p $MOUNTPATH
7874
7975[ $# -ne 3 ] && echo " Usage: $( basename $0 ) <new public key file> <new private key file> <systemvm iso path>" && exit 3
8076newpubkey=$1
@@ -85,17 +81,8 @@ systemvmpath=$3
8581
8682command -v mkisofs > /dev/null || (echo " $( basename $0 ) : mkisofs not found, please install or ensure PATH is accurate" ; exit 4)
8783
88- # if running into Docker as unprivileges, skip ssh verification as iso cannot be mounted due to missing loop device.
89- if [ -f /.dockerenv ]; then
90- if [ -e /dev/loop0 ]; then
91- # it's a docker instance with privileges.
92- inject_into_iso systemvm.iso $newpubkey
93- [ $? -ne 0 ] && exit 5
94- copy_priv_key $newprivkey
95- else
96- # this mean it's a docker instance, ssh key cannot be verify.
97- echo " We run inside Docker, skipping ssh key insertion in systemvm.iso"
98- fi
84+ if [ ! -x " $( command -v bsdtar) " ]; then
85+ echo " bsdtar is unavailable. Skipping ssh key insertion in systemvm.iso"
9986else
10087 inject_into_iso systemvm.iso $newpubkey
10188 [ $? -ne 0 ] && exit 5
0 commit comments