Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

USB device is not visible inside Vagrant

I tried to make visible USB device inside Vagrant (OS: Windows 10) that's why I added these two lines to Vagrant file

 vb.customize ['modifyvm', :id, '--usb', 'on']
 # Provider-specific configuration so you can fine-tune various
 # backing providers for Vagrant. These expose provider-specific options.
 # Example for VirtualBox:
 config.vm.provider "virtualbox" do |vb|
 #   # Display the VirtualBox GUI when booting the machine
 vb.gui = true
 #   # Configure dongle
 vb.customize ['modifyvm', :id, '--usb', 'on']
 vb.customize ['usbfilter', 'add', '0', '--target', :id, '--name', 'VC', '--vendorid', '0x046E', '--productid', '0x0001']

end

But when I use vagrant up I am unable to see this USB device. It marks as unknown devices in VirtualBox device manager. Device is visible on local PC and sometimes it becomes visible after several vagrant reload commands. But I am looking for stable solution which gives me ability to make USB device visible as soon as machine is booted.

like image 552
Mikhail R Avatar asked Jan 05 '23 13:01

Mikhail R


1 Answers

This can be accomplished via the VirtualBox Settings -> USB -> USB Device Filters, or by defining the properties via the Vagrantfile.

The usermod definition allows user access to USB devices via the VirtualBox Extension Pack that implements USB integration between the host and guest machine.

Before starting a VM with vagrant up run the following commands:

# You need the virtualbox-extensions package to enable USB
sudo apt install virtualbox-ext-pack # accept the license to install
# add host user to 'vboxusers' group
sudo usermod -a -G vboxusers $USER
# You will need to logout and back in or reboot to get access to USB
# Make sure you have the stuff below in your Vagrantfile, then:
vagrant up

Note :- $USER should resolve to the current user on the host machine.

Before defining the attributes for USB integration in the Vagrantfile you should run VBoxManage to help get you the USB information currently mounted on host machine:

sudo VBoxManage list usbhost

[sudo] password for rohan: 
Host USB Devices:

UUID:               xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
VendorId:           0x0781 (0781)
ProductId:          0x5575 (5575)
Revision:           1.39 (0139)
Port:               0
USB version/speed:  2/2
Manufacturer:       SanDisk
Product:            Cruzer Glide
SerialNumber:       xxxxxxxxxxxxxxxxxxxx
Address:            sysfs:/sys/devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.1//device:/dev/vboxusb/001/011
Current State:      Busy

Now, the Vagrantfile can implement the configuration attributes (determined above):

 # Enable USB Controller on VirtualBox
  config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--usb", "on"]
    vb.customize ["modifyvm", :id, "--usbehci", "on"]
  end

  # Implement determined configuration attributes
  config.vm.provider "virtualbox" do |vb|
    vb.customize ["usbfilter", "add", "0",
        "--target", :id,
        "--name", "Any Cruzer Glide",
        "--product", "Cruzer Glide"]
  end

If any more results or any modification needed to be done on the attributes then add values in config.vm.provider according to VBoxManage

Following are the properties you can define with respect to usbfilter:

usbfilter                 add <index,0-N>
                            --target <uuid|vmname>|global
                            --name <string>
                            --action ignore|hold (global filters only)
                            [--active yes|no] (yes)
                            [--vendorid <XXXX>] (null)
                            [--productid <XXXX>] (null)
                            [--revision <IIFF>] (null)
                            [--manufacturer <string>] (null)
                            [--product <string>] (null)
                            [--remote yes|no] (null, VM filters only)
                            [--serialnumber <string>] (null)
                            [--maskedinterfaces <XXXXXXXX>]

After defining above the USB device can be mounted on guest machine upon vagrant halt, followed by vagrant up, or vagrant provision, followed by vagrant reload.

Note: Since USB devices may vary from time to time, it is difficult to predict ahead of time, which device should be defined within the Vagrantfile. Therefore, defining a USB filter via the VirtualBox GUI (Settings > USB > USB Device Filters), is many times preferred over the above Vagrantfile implementation.

Ref : using terminal, using UI

like image 137
Rohan Khude Avatar answered Jan 14 '23 13:01

Rohan Khude