你可以使用:
vboxmanage showhdinfo "PathToYourVMDK"
它将在输出的最后一行显示它的 UUID。
然后您可以使用 UUID 而不是其名称来启动该虚拟机
vboxmanage startvm YourUUID
所以,基本上你需要的命令是:
vboxmanage startvm $(vboxmanage showhdinfo "PathToYourVMDK" | awk -F" |)" '/^In use/{print $(NF-1)}')
如果您不使用bash,则需要将$() 替换为这样的反引号:
vboxmanage startvm `vboxmanage showhdinfo "PathToYourVMDK" | awk -F" |)" '/^In use/{print $(NF-1)}'`
更新
如果您在编写脚本和调试脚本时遇到问题,请按照以下步骤操作。
首先...让基本的showhdinfo 命令正常工作。所以试试这个:
vboxmanage showhdinfo "PathToYourVMDK"
UUID: c3166b8a-3c21-4531-927c-030f3cfb9728
Parent UUID: base
State: created
Type: normal (base)
Location: ...
Storage format: VDI
Format variant: fixed default
Capacity: 8192 MBytes
Size on disk: 8192 MBytes
In use by VMs: UbuntuVM (UUID: 77743eca-e338-471c-b824-60c5c5c22b6f) <-- THIS ONE
其次...现在看最后一行,该 UUID 可以与 vboxmanage startvm 一起使用,因此请尝试将其复制并粘贴如下:
vboxmanage startvm 77743eca-e338-471c-b824-60c5c5c22b6f
这应该会启动虚拟机。
第三...下一个检查是 awk 正在正确提取 UUID,所以让我们尝试一下
vboxmanage showhdinfo "PathToYourVMDK" | awk -F" |)" '/^In use/{print $(NF-1)}'
77743eca-e338-471c-b824-60c5c5c22b6f
看起来是对的,现在把它全部放到一个脚本中:
#!/bin/bash -xv
UUID=$(vboxmanage showhdinfo "PathToYourVMDK" | awk -F" |)" '/^In use/{print $(NF-1)}')
echo UUID:$UUID
vboxmanage startvm $UUID