首页 > 解决方案 > 如何使用 VirtualBox CLI 重新启动 VM 并等待它再次完全启动?

问题描述

我正在使用VBoxManage unattended install并希望在安装后运行脚本来自动创建 VM,这让我--post-install-command选择了。这里的问题是:我的脚本中的下一步需要Guest Additions,所以我--install-additions在安装中使用了选项。不幸的是,这个选项在安装 Guest Additions 后不会重新启动机器,所以我正在寻找一种解决方法,所以我重新启动 VM(从主机或来宾),然后继续我的主脚本。

标签: virtualboxvirtualbox-guest-additions

解决方案


不幸的是,我没有发现 VBox 在 VM 启动时触发的任何事件,所以我不得不等待它完全启动。

完全启动平均需要 2-3 分钟,所以我使用了 3 分钟的计时器。

$postInstallCommands = 'VBoxControl guestproperty set installation_finished y && (shutdown /s || shutdown -P now)'

########## Initiate unattended installation
VBoxManage unattended install "$vmName"  `
    --iso="$isoFile"                         `
    --user="$userName"                       `
    --password="$password"                   `
    --full-user-name="$userName"             `
    --install-additions                      `
    --locale=en_US                           `
    --country=US                             `
    --time-zone=EST                          `
    --image-index=1                          `
    --post-install-command="$postInstallCommands" *> $null
    
########## Start VM and wait to finish install
VBoxManage startvm "$vmName" --type headless

Write-Host "Waiting for VM to finish OS installation..."
VBoxManage guestproperty wait "$vmName" installation_finished *> $null

Start-Sleep -s 180

推荐阅读