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,31 +30,35 @@ TMPDIR=${TMP}/cloud/systemvm
3030umask 022
3131
3232clean_up () {
33- rm -rf --preserve-root $MOUNTPATH
34- }
35-
36- backup_iso () {
37- $SUDO cp -b ${systemvmpath} ${systemvmpath} .bak
33+ $SUDO umount $MOUNTPATH
3834}
3935
4036inject_into_iso () {
4137 local isofile=${systemvmpath}
4238 local newpubkey=$2
39+ local backup=${isofile} .bak
4340 local tmpiso=${TMP} /$1
4441 mkdir -p $MOUNTPATH
4542 [ ! -f $isofile ] && echo " $( basename $0 ) : Could not find systemvm iso patch file $isofile " && return 1
46- bsdtar -C $MOUNTPATH -xf $isofile
47- [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to extract original iso $isofile " && clean_up && 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
4845 diff -q $MOUNTPATH /authorized_keys $newpubkey & > /dev/null && clean_up && return 0
49- backup_iso
46+ $SUDO cp -b $isofile $backup
5047 [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to backup original iso $isofile " && clean_up && return 1
51- $SUDO cp $newpubkey $MOUNTPATH /authorized_keys
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
5254 [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to copy key $newpubkey from original iso to new iso " && clean_up && 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
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
5559 $SUDO cp -f $tmpiso $isofile
5660 [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to overwrite old iso $isofile with $tmpiso " && return 1
57- clean_up
61+ rm -rf $TMPDIR
5862}
5963
6064copy_priv_key () {
7074 SUDO=" sudo -n "
7175fi
7276
73- mkdir -p $MOUNTPATH
77+ $SUDO mkdir -p $MOUNTPATH
7478
7579[ $# -ne 3 ] && echo " Usage: $( basename $0 ) <new public key file> <new private key file> <systemvm iso path>" && exit 3
7680newpubkey=$1
@@ -81,8 +85,17 @@ systemvmpath=$3
8185
8286command -v mkisofs > /dev/null || (echo " $( basename $0 ) : mkisofs not found, please install or ensure PATH is accurate" ; exit 4)
8387
84- if [ ! -x " $( command -v bsdtar) " ]; then
85- echo " bsdtar is unavailable. Skipping ssh key insertion in systemvm.iso"
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
8699else
87100 inject_into_iso systemvm.iso $newpubkey
88101 [ $? -ne 0 ] && exit 5
0 commit comments