How to Detect OS Boot Completion for VirtualBox Virtual Machines

Asked 2 years ago, Updated 2 years ago, 138 views

How do I detect that the VirtualBox virtual machine (VM) operating system is completely booted?

I'm starting a VM with VirtualBox and developing it.

So far, when I started working on it, I started the VM manually, started the SSH login, etc., but I thought it might be possible to automate it, so I tried to do it with a shell script, but it hit a wall.

I don't know when the VM's operating system will boot.

That's what it is.

If you try to automate to the point where SSH connects, SSH will play if you do not know if the operating system (should I say sshd)?

There is a command in Virtualbox to verify that the virtual machine has booted, but this only confirms that the machine is up and does not seem to verify that the OS is up.

Currently, we are using the situation where OS is not up SSSSH connection is possible and OS is not up SSSSH connection is played with the idea of reversal.

In your environment, the VM's operating system takes approximately one minute to boot, so you should wait one minute first and then try repeatedly until the SSH connection is established.The following is a shell script that I created, although I am ashamed to say that I am not good at it.

#!/bin/bash

white_green="\e[37;42;1m]
white_red="\e[37;41;1m]
colorEnd="\e[m"

# argument verification
if [$#-eq0]; then
    /bin/echo-e "${white_red} Please specify VM name ${colorEnd}"
    exit1
fi

VBoxManage list vms | grep "$1"; isvms=$?
if [$isvms-ne0]; then
    /bin/echo-e "${white_red}The specified VM name does not exist ${colorEnd}"
    /bin/echo-e"${white_red}The VM names that exist are:${colorEnd}"
    VBoxManage list vms
    exit1
fi

# boot confirmation
VBoxManage list runningvms | grep "$1"; isRunning=$?
if [$isRunning-eq0]; then
    /bin/echo-e"${white_red}$1 is already started${colorEnd}"
    exit1
fi

# VM Boot
VBoxManage startvm "$1" -- type headless > /dev/null2 > & 1
/bin/echo-e "${white_green}VM Boot ${colorEnd}"
# It takes about a minute for the operating system to boot, so hold the weight.
e = 0
/bin/echo-e "${white_green} Waiting for OS boot (wait 60 seconds) ${colorEnd}"
for e in 0 5 10 15 20 25 30 35 40 45 50 55 60; do
    echo-n "$e seconds…\r" 
    sleep5s
done

# ssh connection
vmname="ssh-p22-i to /.ssh/id_rsa hoge@host"
count = 0
/bin/echo-e "${white_green}ssh connecting…${colorEnd}"
untileval${vmname}
do
    echo-n "…"
    sleep5s    
    count=$(count+1))
    if [$count-gt20]; then
    /bin/echo-e "${white_red}20 attempts failed ${colorEnd}"
    exit255
    fi    
done 

exit0

I've accomplished my purpose, but I don't think it's smart to use SSH connection.

If you know how to get when the VM's operating system is up, please help us.

linux shellscript virtualbox

2022-09-30 19:55

1 Answers


© 2024 OneMinuteCode. All rights reserved.