VirtualBox技能
直接从openclaw控制和管理VirtualBox虚拟机,使用VBoxManage命令行界面。这项技能提供了全面的VM生命周期管理、配置和监控功能。
设置
先决条件
- 主机系统上必须安装VirtualBox
- VBoxManage CLI必须可访问(VirtualBox安装后通常在PATH中)
- 用户必须有适当的权限来控制VM
验证安装
VBoxManage --version
常见路径
- Linux:
/usr/bin/VBoxManage或/usr/local/bin/VBoxManage - macOS:
/Applications/VirtualBox.app/Contents/MacOS/VBoxManage - Windows:
C:\Program Files\Oracle\VirtualBox\VBoxManage.exe
核心能力
VM生命周期管理
- 创建、启动、停止、暂停和删除VM
- 管理VM状态(运行中、已暂停、已保存、已关闭电源)
- 强制停止和ACPI关机选项
- 重置和重启VM
配置管理
- 修改VM设置(CPU、RAM、存储)
- 配置网络适配器和模式
- 设置共享文件夹
- 管理USB设备直通
快照和克隆
- 创建和恢复快照
- 克隆现有VM
- 导出/导入设备
监控和信息
- 列出所有VM及其状态
- 获取VM详细信息
- 监控VM指标和性能
- 查看日志和调试信息
使用方法
列出所有VM
# 列出所有注册的VM
VBoxManage list vms
# 仅列出正在运行的VM
VBoxManage list runningvms
# 获取有关所有VM的详细信息(JSON样式输出)
VBoxManage list vms --long
VM信息
# 获取有关特定VM的详细信息
VBoxManage showvminfo "VM_NAME"
# 以机器可读格式获取信息
VBoxManage showvminfo "VM_NAME" --machinereadable
启动VM
# 启动带GUI的VM
VBoxManage startvm "VM_NAME"
# 启动无头VM(无GUI)
VBoxManage startvm "VM_NAME" --type headless
# 启动单独UI进程的VM
VBoxManage startvm "VM_NAME" --type separate
停止VM
# ACPI关机(优雅,就像按电源按钮)
VBoxManage controlvm "VM_NAME" acpipowerbutton
# 断电(硬停止,就像拔插头)
VBoxManage controlvm "VM_NAME" poweroff
# 保存状态(休眠)
VBoxManage controlvm "VM_NAME" savestate
# 暂停VM
VBoxManage controlvm "VM_NAME" pause
# 恢复已暂停的VM
VBoxManage controlvm "VM_NAME" resume
# 重置VM(硬重启)
VBoxManage controlvm "VM_NAME" reset
创建新VM
# 创建新VM
VBoxManage createvm --name "NewVM" --register
# 设置操作系统类型
VBoxManage modifyvm "NewVM" --ostype "Ubuntu_64"
# 设置内存(RAM以MB为单位)
VBoxManage modifyvm "NewVM" --memory 4096
# 设置CPU数量
VBoxManage modifyvm "NewVM" --cpus 2
# 创建虚拟磁盘
VBoxManage createhd --filename "/path/to/NewVM.vdi" --size 50000
# 添加存储控制器
VBoxManage storagectl "NewVM" --name "SATA Controller" --add sata
# 附加虚拟磁盘
VBoxManage storageattach "NewVM" --storagectl "SATA Controller" \
--port 0 --device 0 --type hdd --medium "/path/to/NewVM.vdi"
# 附加用于安装的ISO
VBoxManage storageattach "NewVM" --storagectl "SATA Controller" \
--port 1 --device 0 --type dvddrive --medium "/path/to/install.iso"
克隆VM
# 全克隆(复制所有磁盘)
VBoxManage clonevm "SourceVM" --name "ClonedVM" --register
# 链接克隆(使用相同的基础磁盘,节省空间)
VBoxManage clonevm "SourceVM" --name "LinkedVM" --options link --register
# 从特定快照克隆
VBoxManage clonevm "SourceVM" --name "FromSnapshotVM" \
--snapshot "SnapshotName" --register
删除VM
# 删除VM(保留磁盘)
VBoxManage unregistervm "VM_NAME"
# 删除VM及所有相关文件
VBoxManage unregistervm "VM_NAME" --delete
快照
# 列出快照
VBoxManage snapshot "VM_NAME" list
# 拍摄快照
VBoxManage snapshot "VM_NAME" take "SnapshotName" --description "Description here"
# 恢复快照
VBoxManage snapshot "VM_NAME" restore "SnapshotName"
# 删除快照
VBoxManage snapshot "VM_NAME" delete "SnapshotName"
# 恢复当前快照(返回到最后一个快照)
VBoxManage snapshot "VM_NAME" restorecurrent
网络配置
# 列出网络适配器
VBoxManage showvminfo "VM_NAME" | grep -A 5 "NIC"
# 设置NAT网络
VBoxManage modifyvm "VM_NAME" --nic1 nat
# 设置桥接网络
VBoxManage modifyvm "VM_NAME" --nic1 bridged --bridgeadapter1 eth0
# 设置仅主机网络
VBoxManage modifyvm "VM_NAME" --nic1 hostonly --hostonlyadapter1 vboxnet0
# 端口转发(仅NAT)
VBoxManage modifyvm "VM_NAME" --natpf1 "ssh,tcp,,2222,,22"
# 删除端口转发
VBoxManage modifyvm "VM_NAME" --natpf1 delete "ssh"
# 列出仅主机网络
VBoxManage list hostonlyifs
# 创建仅主机网络
VBoxManage hostonlyif create
# 配置仅主机网络
VBoxManage hostonlyif ipconfig vboxnet0 --ip 192.168.56.1 --netmask 255.255.255.0
共享文件夹
# 添加共享文件夹
VBoxManage sharedfolder add "VM_NAME" --name "share" --hostpath "/path/on/host"
# 添加只读共享文件夹
VBoxManage sharedfolder add "VM_NAME" --name "share" --hostpath "/path/on/host" --readonly
# 添加自动挂载的共享文件夹
VBoxManage sharedfolder add "VM_NAME" --name "share" --hostpath "/path/on/host" --automount
# 删除共享文件夹
VBoxManage sharedfolder remove "VM_NAME" --name "share"
# 列出共享文件夹
VBoxManage showvminfo "VM_NAME" | grep -A 5 "Shared Folder"
修改VM设置
# 更改内存分配
VBoxManage modifyvm "VM_NAME" --memory 8192
# 更改CPU数量
VBoxManage modifyvm "VM_NAME" --cpus 4
# 启用/禁用VRAM(视频内存)
VBoxManage modifyvm "VM_NAME" --vram 128
# 启用3D加速
VBoxManage modifyvm "VM_NAME" --accelerate3d on
# 启用嵌套虚拟化
VBoxManage modifyvm "VM_NAME" --nested-hw-virt on
# 设置VRDE(远程桌面)端口
VBoxManage modifyvm "VM_NAME" --vrde on --vrdeport 3389
# 更改VM名称
VBoxManage modifyvm "VM_NAME" --name "NewName"
# 设置描述
VBoxManage modifyvm "VM_NAME" --description "Production server VM"
USB设备直通
# 列出USB设备
VBoxManage list usbhost
# 将USB设备附加到正在运行的VM
VBoxManage controlvm "VM_NAME" usbattach "UUID_OR_ADDRESS"
# 从VM中分离USB设备
VBoxManage controlvm "VM_NAME" usbdetach "UUID_OR_ADDRESS"
# 添加USB设备过滤器(持久性)
VBoxManage usbfilter add 0 --target "VM_NAME" --name "FilterName" \
--vendorid "XXXX" --productid "XXXX"
导出/导入设备
# 导出VM为OVA/OVF
VBoxManage export "VM_NAME" --output "/path/to/export.ova"
# 导出多个VM
VBoxManage export "VM1" "VM2" --output "/path/to/export.ova"
# 导入设备
VBoxManage import "/path/to/export.ova"
# 导入选项
VBoxManage import "/path/to/export.ova" --vsys 0 --vmname "ImportedVM"
监控和指标
# 列出可用指标
VBoxManage metrics list
# 设置指标收集
VBoxManage metrics setup --period 10 --samples 5 "VM_NAME"
# 收集并显示指标
VBoxManage metrics collect "VM_NAME"
# 查询特定指标
VBoxManage metrics query "VM_NAME" "CPU/Load"
VBoxManage metrics query "VM_NAME" "RAM/Usage"
VBoxManage metrics query "VM_NAME" "Net/Rate"
# 列出VM的所有指标
VBoxManage metrics list "VM_NAME"
介质(磁盘)管理
# 列出所有虚拟磁盘
VBoxManage list hdds
# 获取磁盘信息
VBoxManage showhdinfo "/path/to/disk.vdi"
# 调整虚拟磁盘大小
VBoxManage modifyhd "/path/to/disk.vdi" --resize 100000
# 克隆虚拟磁盘
VBoxManage clonemedium "/path/to/source.vdi" "/path/to/clone.vdi"
# 压缩磁盘(缩小)
VBoxManage modifymedium "/path/to/disk.vdi" --compact
# 设置磁盘类型
VBoxManage modifymedium "/path/to/disk.vdi" --type normal
VBoxManage modifymedium "/path/to/disk.vdi" --type immutable
VBoxManage modifymedium "/path/to/disk.vdi" --type writethrough
客户控制(需要客户附加组件)
# 在客户机中执行命令
VBoxManage guestcontrol "VM_NAME" run --exe "/bin/ls" \
--username user --password pass -- -la /home
# 将文件复制到客户机
VBoxManage guestcontrol "VM_NAME" copyto \
--username user --password pass \
"/host/path/file.txt" "/guest/path/file.txt"
# 从客户机复制文件
VBoxManage guestcontrol "VM_NAME" copyfrom \
--username user --password pass \
"/guest/path/file.txt" "/host/path/file.txt"
# 在客户机中创建目录
VBoxManage guestcontrol "VM_NAME" mkdir \
--username user --password pass \
"/home/user/newdir"
# 在客户机中删除文件
VBoxManage guestcontrol "VM_NAME" rm \
--username user --password pass \
"/home/user/file.txt"
# 列出客户机进程
VBoxManage guestcontrol "VM_NAME" process list \
--username user --password pass
调试和日志
# 查看VM日志位置
VBoxManage showvminfo "VM_NAME" | grep -i log
# 典型日志路径:
# Linux/macOS: ~/VirtualBox VMs/VM_NAME/Logs/
# Windows: %USERPROFILE%\VirtualBox VMs\VM_NAME\Logs\
# 调试VM
VBoxManage debugvm "VM_NAME" info item
# 获取VM统计信息
VBoxManage debugvm "VM_NAME" statistics
实际例子
快速VM状态检查
# 检查特定VM是否正在运行
VBoxManage list runningvms | grep "VM_NAME"
# 获取所有VM及其状态
VBoxManage list vms --long | grep -E "Name:|State:"
自动化VM启动脚本
#!/bin/bash
# 以无头模式启动VM
for vm in "WebServer" "Database" "Cache"; do
echo "Starting $vm..."
VBoxManage startvm "$vm" --type headless
sleep 10
done
echo "All VMs started"
带有快照的备份脚本
#!/bin/bash
VM_NAME="ProductionVM"
DATE=$(date +%Y%m%d_%H%M%S)
SNAPSHOT_NAME="Backup_$DATE"
# 创建快照
VBoxManage snapshot "$VM_NAME" take "$SNAPSHOT_NAME" \
--description "Automated backup $DATE"
# 只保留最后5个快照
SNAPSHOTS=$(VBoxManage snapshot "$VM_NAME" list --machinereadable | grep SnapshotName | wc -l)
if [ $SNAPSHOTS -gt 5 ]; then
OLDEST=$(VBoxManage snapshot "$VM_NAME" list --machinereadable | grep SnapshotName | head -1 | cut -d'"' -f4)
VBoxManage snapshot "$VM_NAME" delete "$OLDEST"
fi
完整的VM克隆工作流
#!/bin/bash
SOURCE_VM="TemplateVM"
NEW_VM="DevVM_$(date +%s)"
# 确保源已停止
VBoxManage controlvm "$SOURCE_VM" poweroff 2>/dev/null
# 首先拍摄一个干净的快照
VBoxManage snapshot "$SOURCE_VM" take "PreClone"
# 克隆VM
VBoxManage clonevm "$SOURCE_VM" --name "$NEW_VM" --register
# 修改克隆
VBoxManage modifyvm "$NEW_VM" --memory 2048 --cpus 2
# 启动克隆
VBoxManage startvm "$NEW_VM" --type headless
echo "Cloned VM '$NEW_VM' is now running"
网络端口转发设置
#!/bin/bash
VM_NAME="WebServer"
# SSH访问
VBoxManage modifyvm "$VM_NAME" --natpf1 "ssh,tcp,,2222,,22"
# HTTP访问
VBoxManage modifyvm "$VM_NAME" --natpf1 "http,tcp,,8080,,80"
# HTTPS访问
VBoxManage modifyvm "$VM_NAME" --natpf1 "https,tcp,,8443,,443"
# 验证
VBoxManage showvminfo "$VM_NAME" | grep "NIC 1 Rule"
监控资源使用情况
#!/bin/bash
VM_NAME="ProductionVM"
# 设置指标
VBoxManage metrics setup --period 5 --samples 12 "$VM_NAME"
# 收集1分钟并显示结果
sleep 60
VBoxManage metrics query "$VM_NAME" "CPU/Load:RAM/Usage:Net/Rate"
常见问题和解决方案
VM无法启动
# 检查VM状态
VBoxManage showvminfo "VM_NAME" | grep State
# 检查锁定文件
VBoxManage showvminfo "VM_NAME" | grep -i lock
# 尝试以详细输出启动
VBoxManage startvm "VM_NAME" --type headless 2>&1
无法删除VM
# 确保VM已停止
VBoxManage controlvm "VM_NAME" poweroff
# 检查是否附加了媒体
VBoxManage showvminfo "VM_NAME" | grep -E "Storage|Medium"
# 如有需要,强制注销
VBoxManage unregistervm "VM_NAME" --delete
网络问题
# 检查适配器状态
VBoxManage showvminfo "VM_NAME" | grep -A 10 "NIC 1"
# 重置网络适配器
VBoxManage modifyvm "VM_NAME" --nic1 none
VBoxManage modifyvm "VM_NAME" --nic1 nat
# 验证仅主机接口是否存在
VBoxManage list hostonlyifs
性能问题
# 检查当前分配
VBoxManage showvminfo "VM_NAME" | grep -E "Memory|CPU"
# 增加资源(VM必须已停止)
VBoxManage modifyvm "VM_NAME" --memory 8192 --cpus 4
# 启用硬件加速
VBoxManage modifyvm "VM_NAME" --hwvirtex on --nestedpaging on
重要说明
-
带空格的VM名称:总是引用包含空格的VM名称
VBoxManage startvm "My Production VM" -
UUID与名称:VM名称和UUID可以互换使用
VBoxManage startvm "VM_NAME" VBoxManage startvm "12345678-1234-1234-1234-123456789abc" -
运行中与停止的操作:
controlvm- 操作运行中的VMmodifyvm- 操作已停止的VM(大多数)
-
无头模式:在没有GUI的服务器环境中始终使用
--type headless -
权限:某些操作需要提升权限或成为特定组的成员(例如,Linux上的
vboxusers) -
客户附加组件:需要用于:
- 共享剪贴板
- 拖放
- 自动挂载共享文件夹
- 客户控制命令
- 无缝模式
操作系统类型参考
--ostype参数的常见操作系统类型:
Windows11_64- Windows 11(64位)Windows10_64- Windows 10(64位)Ubuntu_64- Ubuntu Linux(64位)Debian_64- Debian Linux(64位)Fedora_64- Fedora Linux(64位)ArchLinux_64- Arch Linux(64位)macOS_ARM64- Apple Silicon上的macOSmacOS_128- Intel上的macOS(64位)FreeBSD_64- FreeBSD(64位)Other_64- 其他操作系统(64位)
获取完整列表:
VBoxManage list ostypes
快速参考卡
| 操作 | 命令 |
|---|---|
| 列出VM | VBoxManage list vms |
| 启动VM | VBoxManage startvm "NAME" --type headless |
| 停止VM | VBoxManage controlvm "NAME" acpipowerbutton |
| 强制停止 | VBoxManage controlvm "NAME" poweroff |
| VM信息 | VBoxManage showvminfo "NAME" |
| 快照 | VBoxManage snapshot "NAME" take "SnapName" |
| 恢复 | VBoxManage snapshot "NAME" restore "SnapName" |
| 克隆 | VBoxManage clonevm "SRC" --name "NEW" --register |
| 删除 | VBoxManage unregistervm "NAME" --delete |
| 修改RAM | VBoxManage modifyvm "NAME" --memory 4096 |
| 修改CPU | VBoxManage modifyvm "NAME" --cpus 2 |
| 端口转发 | VBoxManage modifyvm "NAME" --natpf1 "rule,tcp,,host,,guest" |