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
@@ -33,32 +33,56 @@ clean_up() {
3333 $SUDO umount $MOUNTPATH
3434}
3535
36+ clean_up_bsdtar () {
37+ rm -rf --preserve-root $MOUNTPATH
38+ }
39+
40+ backup_iso () {
41+ $SUDO cp -b ${systemvmpath} ${systemvmpath} .bak
42+ }
43+
3644inject_into_iso () {
3745 local isofile=${systemvmpath}
3846 local newpubkey=$2
39- local backup=${isofile} .bak
4047 local tmpiso=${TMP} /$1
4148 mkdir -p $MOUNTPATH
4249 [ ! -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
45- diff -q $MOUNTPATH /authorized_keys $newpubkey & > /dev/null && clean_up && return 0
46- $SUDO cp -b $isofile $backup
47- [ $? -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
54- [ $? -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
59- $SUDO cp -f $tmpiso $isofile
60- [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to overwrite old iso $isofile with $tmpiso " && return 1
61- rm -rf $TMPDIR
50+ if [ -x " $( command -v bsdtar) " ]; then
51+ bsdtar -C $MOUNTPATH -xf $isofile
52+ [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to extract original iso $isofile " && clean_up_bsdtar && return 1
53+ diff -q $MOUNTPATH /authorized_keys $newpubkey & > /dev/null && clean_up_bsdtar && return 0
54+ backup_iso
55+ [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to backup original iso $isofile " && clean_up_bsdtar && return 1
56+ $SUDO cp $newpubkey $MOUNTPATH /authorized_keys
57+ [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to copy key $newpubkey from original iso to new iso " && clean_up_bsdtar && return 1
58+ mkisofs -quiet -r -o $tmpiso $MOUNTPATH
59+ [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to create new iso $tmpiso from $MOUNTPATH " && clean_up_bsdtar && return 1
60+ $SUDO cp -f $tmpiso $isofile
61+ [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to overwrite old iso $isofile with $tmpiso " && return 1
62+ clean_up_bsdtar
63+ else
64+ $SUDO mount -o loop $isofile $MOUNTPATH
65+ [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to mount original iso $isofile " && clean_up && return 1
66+ diff -q $MOUNTPATH /authorized_keys $newpubkey & > /dev/null && clean_up && return 0
67+ backup_iso
68+ [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to backup original iso $isofile " && clean_up && return 1
69+ #
70+ rm -rf $TMPDIR
71+ mkdir -p $TMPDIR
72+ [ ! -d $TMPDIR ] && echo " $( basename $0 ) : Could not find/create temporary dir $TMPDIR " && clean_up && return 1
73+ $SUDO cp -fr $MOUNTPATH /* $TMPDIR /
74+ [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to copy from original iso $isofile " && clean_up && return 1
75+ #
76+ $SUDO cp $newpubkey $TMPDIR /authorized_keys
77+ [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to copy key $newpubkey from original iso to new iso " && clean_up && return 1
78+ mkisofs -quiet -r -o $tmpiso $TMPDIR
79+ [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to create new iso $tmpiso from $TMPDIR " && clean_up && return 1
80+ $SUDO umount $MOUNTPATH
81+ [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to unmount old iso from $MOUNTPATH " && return 1
82+ $SUDO cp -f $tmpiso $isofile
83+ [ $? -ne 0 ] && echo " $( basename $0 ) : Failed to overwrite old iso $isofile with $tmpiso " && return 1
84+ rm -rf $TMPDIR
85+ fi
6286}
6387
6488copy_priv_key () {
7498 SUDO=" sudo -n "
7599fi
76100
77- $SUDO mkdir -p $MOUNTPATH
101+ mkdir -p $MOUNTPATH
78102
79103[ $# -ne 3 ] && echo " Usage: $( basename $0 ) <new public key file> <new private key file> <systemvm iso path>" && exit 3
80104newpubkey=$1
@@ -85,17 +109,8 @@ systemvmpath=$3
85109
86110command -v mkisofs > /dev/null || (echo " $( basename $0 ) : mkisofs not found, please install or ensure PATH is accurate" ; exit 4)
87111
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
112+ if [ ! -e /dev/loop0 ] && [ ! -x " $( command -v bsdtar) " ]; then
113+ echo " Loop device is missing and bsdtar is unavailable. Skipping ssh key insertion in systemvm.iso"
99114else
100115 inject_into_iso systemvm.iso $newpubkey
101116 [ $? -ne 0 ] && exit 5
0 commit comments