「Linux」VirtualBox免鼠标点击,分配USB设备给虚拟机的办法
虚拟机我喜欢开全屏来操作里边的内容,这样子看起来比较爽快一点。
但全屏之后,就不能手动来挂载USB设备了,这一点有点不太好……
好比,最近喜欢上了使用Lenovo K860手机,现在我要通过Linux下的命令来挂载它……
0. 基础条件:
-- 虚拟机名称是WinXP3
1. 查看手机设置的UUID相关信息
scue@Link:~$ vboxmanage list usbhost
UUID: 3bd984a4-3cf3-4a58-94fa-171cb0a1c3be VendorId: 0x17ef (17EF) ProductId: 0x7533 (7533) Revision: 2.38 (0238) Port: 1 USB version/speed: 2/2 Manufacturer: Lenovo Product: K860 SerialNumber: 0123456789ABCDEF Address: sysfs:/sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2//device:/dev/vboxusb/002/004 Current State: Available
得知 3bd984a4-3cf3-4a58-94fa-171cb0a1c3be 既是我们唯一想要的信息
2. 现在通过命令来挂载它到名为WinXP3的虚拟机上
scue@Link:~$ vboxmanage controlvm WinXP3 usbattach 3bd984a4-3cf3-4a58-94fa-171cb0a1c3be
3. 既然能挂载了,也能卸载,卸载的命令
scue@Link:~$ vboxmanage controlvm WinXP3 usbdetach 3bd984a4-3cf3-4a58-94fa-171cb0a1c3be
4. 为了方便,可以写成两个小脚本,放到~/bin/目录下即可实现随时挂载与卸载,Enjoy!
脚本1:~/bin/vboxusbattach,分配任意设备给虚拟机(可选择)
#!/bin/bash - #=============================================================================== # # FILE: ~/bin/vboxattach # # USAGE: vboxattach # # DESCRIPTION: 在终端上操作挂载设备到vbox里边(合适用于WinXP3运行于全屏时); # # OPTIONS: --- # REQUIREMENTS: --- # BUGS: --- # NOTES: 这里假定虚拟机名为当前运行的虚拟机(自动识别) # AUTHOR: linkscue (scue), linkscue@gmail.com # CREATED: 2013年03月11日 14时40分24秒 HKT # COPYRIGHT: Copyright (c) 2013, linkscue # REVISION: 0.1 # ORGANIZATION: #=============================================================================== # 初始化相关的变量 vbox_list=$(vboxmanage list vms | awk -F'"' '{print $2}') line_cout=$(vboxmanage list usbhost | sed -n 's/Product: //gp' | wc -l) select_vm(){ select vbox_name in $vbox_list; do echo "$vbox_name" break; done } # 有多个虚拟时,选择虚拟机 if [[ "$(echo $vbox_list | grep " ")" != "" ]]; then if [[ "$1" != "" ]]; then vbox_name=$(echo $1 | select_vm) shift else vbox_name=$(select_vm) fi echo "Choose the $vbox_name" else vbox_name=$vbox_list fi # 当设备数量不为空时执行if操作 if [[ $line_cout ]]; then cout=1 echo "I: the devices attach pc be listed below:" vboxmanage list usbhost | sed -n 's/Product: //gp' | \ while read line do echo " [$cout] $line" ((cout++)) done fi # 设备的UUID array=( $(vboxmanage list usbhost | grep UUID | awk '{print $2}') ) if [[ ! -z $1 ]]; then seleted_devices=$1 else read -p "I: select devices number to attach to $vbox_name:" seleted_devices fi # 当已经选择了相关的设备序号时,执行以下操作 if [[ $seleted_devices ]]; then uuid=${array[$seleted_devices-1]} echo "<<< vboxmanage controlvm $vbox_name usbattach $uuid " vboxmanage controlvm $vbox_name usbattach $uuid 2>/dev/null if [[ $? != 0 ]]; then echo ">>> attach to vbox failure." else echo ">>> attach to vbox success." fi fi
脚本2:~/bin/vboxusbdetach,从虚拟机上卸载任意设备
#!/bin/bash - #=============================================================================== # # FILE: vboxdetach # # USAGE: ./vboxdetach # # DESCRIPTION: 用于卸载虚拟机上的设备 # # OPTIONS: --- # REQUIREMENTS: --- # BUGS: --- # NOTES: 这里假定虚拟机名为当前正在运行的虚拟机(自动识别) # AUTHOR: linkscue (scue), linkscue@gmail.com # CREATED: 2013年03月11日 14时42分26秒 HKT # COPYRIGHT: Copyright (c) 2013, linkscue # REVISION: 0.1 # ORGANIZATION: #=============================================================================== vbox_list=$(vboxmanage list vms | awk -F'"' '{print $2}') line_cout=$(vboxmanage list usbhost | sed -n 's/Product: //gp' | wc -l) select_vm(){ select vbox_name in $vbox_list; do echo "$vbox_name" break; done } # 有多个虚拟时,选择虚拟机 if [[ "$(echo $vbox_list | grep " ")" != "" ]]; then if [[ "$1" != "" ]]; then vbox_name=$(echo $1 | select_vm) shift else vbox_name=$(select_vm) fi echo "Choose the $vbox_name" else vbox_name=$vbox_list fi # 当设备数量不为空时执行if操作 if [[ $line_cout ]]; then cout=1 echo "I: the devices attach pc be listed below:" vboxmanage list usbhost | sed -n 's/Product: //gp' | \ while read line do echo " [$cout] $line" ((cout++)) done fi # 设备的UUID array=( $(vboxmanage list usbhost | grep UUID | awk '{print $2}') ) if [[ ! -z $1 ]]; then seleted_devices=$1 else read -p "I: select devices number to attach to $vbox_name:" seleted_devices fi # 当已经选择了相关的设备序号时,执行以下操作 if [[ $seleted_devices ]]; then uuid=${array[$seleted_devices-1]} echo "<<< vboxmanage controlvm $vbox_name usbdetach $uuid" vboxmanage controlvm $vbox_name usbdetach $uuid 2>/dev/null if [[ $? != 0 ]]; then echo ">>> detach from vbox failure." else echo ">>> detach from vbox success." fi fi