Skip to content

Commit

Permalink
Update from base builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Neilpang committed Jan 14, 2025
1 parent 7258502 commit e7005df
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 23 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ concurrency:
jobs:
build:
strategy:
fail-fast: false
matrix:
os: ["6.4.0"]
runs-on: ubuntu-24.04
Expand Down
130 changes: 107 additions & 23 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ export VM_RELEASE
export VM_OCR
export VM_DISK
export VM_ARCH

export VM_CPU
export VM_USE_CONSOLE_BUILD
export VM_USE_SSHROOT_BUILD_SSH
export VM_NO_VNC_BUILD

##############################################################


waitForText() {
_text="$1"
$vmsh waitForText $osname "$_text"
_sec="$2"
$vmsh waitForText $osname "$_text" "$_sec"
}

#keys splitted by ;
Expand Down Expand Up @@ -75,10 +79,14 @@ fi


if [ "$VM_ISO_LINK" ]; then
#start from iso, install to the vir disk

$vmsh createVM $VM_ISO_LINK $osname $ostype $sshport

sleep 2

$vmsh openConsole "$osname"

if [ -e "hooks/installOpts.sh" ]; then
echo "hooks/installOpts.sh"
cat "hooks/installOpts.sh"
Expand All @@ -100,15 +108,34 @@ if [ "$VM_ISO_LINK" ]; then
fi

while $vmsh isRunning $osname; do
sleep 5
sleep 20
done

$vmsh closeConsole "$osname"


if [[ "$VM_ISO_LINK" == *"img" ]]; then
$vmsh detachIMG "$osname"
fi

elif [ "$VM_VHD_LINK" ]; then
#if the vm disk is already provided FreeBSD, just import it.
if [ ! -e "$osname.qcow2" ]; then
if [ ! -e "$osname.qcow2.xz" ]; then
$vmsh download "$VM_VHD_LINK" $osname.qcow2.xz
if [[ "$VM_VHD_LINK" == *"img.gz" ]]; then
_img="$osname.img"
if [ ! -e "$_img" ]; then
rm -f "$_img.gz"
$vmsh download "$VM_VHD_LINK" "$_img.gz"
gunzip -c "$_img.gz" > "$_img"
fi
qemu-img convert -f raw -O qcow2 -o preallocation=off "$_img" "$osname.qcow2"
else
if [ ! -e "$osname.qcow2.xz" ]; then
$vmsh download "$VM_VHD_LINK" $osname.qcow2.xz
fi
xz -d -T 0 --verbose "$osname.qcow2.xz"
fi
xz -d -T 0 --verbose "$osname.qcow2.xz"

fi

$vmsh createVMFromVHD $osname $ostype $sshport
Expand All @@ -128,6 +155,8 @@ ls -lh
start_and_wait() {
$vmsh startVM $osname
sleep 2
$vmsh openConsole "$osname"

if [ -e "hooks/waitForLoginTag.sh" ]; then
echo "hooks/waitForLoginTag.sh"
cat "hooks/waitForLoginTag.sh"
Expand All @@ -153,6 +182,9 @@ shutdown_and_wait() {
while $vmsh isRunning $osname; do
sleep 5
done

$vmsh closeConsole "$osname"

}

restart_and_wait() {
Expand All @@ -162,14 +194,15 @@ restart_and_wait() {

###############################################

start_and_wait

inputKeys "string root; enter; sleep 1;"
if [ "$VM_ROOT_PASSWORD" ]; then
inputKeys "string $VM_ROOT_PASSWORD ; enter"
#start the installed vm, and initialize the ssh access:


if [ -z "$VM_NO_VNC_BUILD" ]; then
export VM_USE_CONSOLE_BUILD=""
fi
inputKeys "enter"
sleep 2

start_and_wait


if [ ! -e ~/.ssh/id_rsa ] ; then
Expand All @@ -179,21 +212,57 @@ fi
cat enablessh.txt >enablessh.local


echo "echo '$(cat ~/.ssh/id_rsa.pub)' >>~/.ssh/authorized_keys" >>enablessh.local
echo "" >>enablessh.local
echo "" >>enablessh.local
echo "" >>enablessh.local

#add ssh key twice, to avoid bugs.
echo "echo '$(base64 -w 0 ~/.ssh/id_rsa.pub)' | openssl base64 -d >>~/.ssh/authorized_keys" >>enablessh.local
echo "" >>enablessh.local

echo "echo '$(cat ~/.ssh/id_rsa.pub)' >>~/.ssh/authorized_keys" >>enablessh.local
echo "" >>enablessh.local


echo >>enablessh.local
echo "chmod 600 ~/.ssh/authorized_keys">>enablessh.local
echo "exit">>enablessh.local

echo "">>enablessh.local
echo >>enablessh.local


$vmsh inputFile $osname enablessh.local
cat enablessh.local


if [ "$VM_USE_SSHROOT_BUILD_SSH" ]; then
vmip=$($vmsh getVMIP $osname)
sshpass -p "$VM_ROOT_PASSWORD" ssh -o StrictHostKeyChecking=no -tt root@$vmip TERM=xterm <enablessh.local
#sleep for the sshd server to restart
sleep 10
inputKeys "enter"
sleep 2
inputKeys "enter"
sleep 2
echo "check ssh access:"
ssh -vv root@$vmip pwd
echo "ssh OK"
else
inputKeys "string root; enter; sleep 1;"
if [ "$VM_ROOT_PASSWORD" ]; then
inputKeys "string $VM_ROOT_PASSWORD ; enter"
fi
inputKeys "enter"
sleep 2

$vmsh screenText $osname
$vmsh inputFile $osname enablessh.local
$vmsh screenText $osname
#sleep for the sshd server to restart
sleep 10
inputKeys "enter"
sleep 2
inputKeys "enter"
sleep 2
fi


###############################################################
Expand All @@ -206,7 +275,7 @@ echo 'StrictHostKeyChecking=no' >.ssh/config
echo "Host host" >>.ssh/config
echo " HostName 192.168.122.1" >>.ssh/config
echo " User runner" >>.ssh/config
echo " User $USER" >>.ssh/config
echo " ServerAliveInterval 1" >>.ssh/config
EOF
Expand All @@ -216,14 +285,23 @@ EOF
if [ -e "hooks/postBuild.sh" ]; then
echo "hooks/postBuild.sh"
cat "hooks/postBuild.sh"
ssh $osname sh<"hooks/postBuild.sh"
export VM_RELEASE
ssh -o "SendEnv=VM_RELEASE" $osname sh<"hooks/postBuild.sh"

# Reboot here, possible there were system updates done that need
# a reboot to take effect before more operations can be done
restart_and_wait
fi

ssh $osname 'cat ~/.ssh/id_rsa.pub' >$osname-$VM_RELEASE-id_rsa.pub

output="$osname-$VM_RELEASE"
if [ "$VM_ARCH" ]; then
output="$osname-$VM_RELEASE-$VM_ARCH"
fi

ssh $osname 'cat ~/.ssh/id_rsa.pub' >$output-id_rsa.pub



#upload reboot.sh
if [ -e "hooks/reboot.sh" ]; then
Expand All @@ -239,6 +317,7 @@ env | grep SSH_CLIENT | cut -d = -f 2 | cut -d ' ' -f 1 >$osname.rebooted
END
EOF
ssh "$osname" "cat /reboot.sh"
fi

#set cronjob
Expand All @@ -264,7 +343,8 @@ fi
if [ -e "hooks/finalize.sh" ]; then
echo "hooks/finalize.sh"
cat "hooks/finalize.sh"
ssh $osname sh<"hooks/finalize.sh"
export VM_RELEASE
ssh -o "SendEnv=VM_RELEASE" $osname sh<"hooks/finalize.sh"
fi

# Done!
Expand All @@ -283,11 +363,13 @@ ls -lah
echo "free space:"
df -h

ova="$osname-$VM_RELEASE.qcow2"


ova="$output.qcow2"
echo "Exporting $ova"
$vmsh exportOVA $osname "$ova"

cp ~/.ssh/id_rsa $osname-$VM_RELEASE-host.id_rsa
cp ~/.ssh/id_rsa $output-host.id_rsa

echo "contents after export:"
ls -lah
Expand All @@ -300,7 +382,7 @@ echo "Checking the packages: $VM_RSYNC_PKG $VM_SSHFS_PKG"
if [ -z "$VM_RSYNC_PKG$VM_SSHFS_PKG" ]; then
echo "skip"
else
$vmsh addSSHAuthorizedKeys $osname-$VM_RELEASE-id_rsa.pub
$vmsh addSSHAuthorizedKeys $output-id_rsa.pub
$vmsh startVM $osname
$vmsh waitForVMReady $osname
if [ "$VM_RSYNC_PKG" ]; then
Expand All @@ -311,4 +393,6 @@ else
fi
fi

echo "Build finished."


0 comments on commit e7005df

Please sign in to comment.