Run command after mount/unmout usb pen drive - linux

I have Raspberry Pi (with Raspbian) and using it as DLNA/UPnP server and renderer. I run minidlna as DLNA server and i have some media files on USB.
I would like to automaticaly rebuild DLNA DB when drive is mounted and unmounted. This is done by command:
sudo service minidlna force-reload
Is threre any way how to autorun this command?
BTW I use "USBmount" package for automount USB drives.
Thanx

You can do this using the tool usbmount.
It has the possibility to add scripts that will be run on mount/umount events in /etc/usbmount/mount.d/ and /etc/usbmount/umount.d/.

Start by finding your device in lsusb. Note the ID (eg 12f5:a91a)
Create a new udev rules file in /etc/udev/rules.d/ eg /etc/udev/rules.d/100-my-mount.rules and write a new rule in there like this:
ACTION=="add", ATTRS{idVendor}=="12f5", ATTRS{idProduct}=="a91a", RUN+="/home/your_username/bin/my-mount-script.sh"
For unmounted device use ACTION=="remove" in rule and another script.

Related

how to give permission to intel realsense camera on Ubuntu

OS:Ubuntu 18.04
camera: Intel realsense D415
I can use cheese to show the compound image of the camera. But when I run
import pyrealsense2 as rs
pipeline = rs.pipeline()
pipeline.start()
It throws out error access failed for 8086:ad6 uid: 1-1-12
Is anyone that knows how to solve this? Thanks!
You need to install Intel Realsense permission scripts located in librealsense source directory.
For that, first, git clone the librealsense to a local folder
git clone https://github.com/IntelRealSense/librealsense
Next, run the following commands to copy the 99-realsense-libusb.rules files to the rules.d folder
sudo cp config/99-realsense-libusb.rules /etc/udev/rules.d/
And, then udevadm control to modify the internal state of the running udev dameon to reload rules. Also, udevadm trigger to request device events from the kernel.
sudo udevadm control --reload-rules
sudo udevadm trigger
Update: I recently faced the Runtime error: backend-v412.cpp:988 - Frames didn't arrived within 5 seconds. Seems like the pipeline is not able to handle the framebuffers, and there is quite a lot of drop in the frames, specifically above 720p, 15 frames/sec.
Any resolutions?
Update 2:
Resolution: As suspected it is a pipelining issue w.r.t the amount of the data the USB port can carry. To prevent frame drop or overload of data through the USB, it has to be connected to a Motherboard that has USB 3.1 Gen 1 specifications. Refer to page 78 of this document https://www.intelrealsense.com/wp-content/uploads/2020/06/Intel-RealSense-D400-Series-Datasheet-June-2020.pdf

How to automatically run 'sudo modprobe -r ftdi_sio' whenever the device is plugged into my computer

I have a USB device that I'm using and I'm developing an application using WebUSB with Google Chrome.
The thing is whenever I plug the USB device into my Linux computer, I have to manually run sudo modprobe -r ftdi_sio to unload it.
I want it to be unloaded automatically whenever I plug that device into my computer instead of having to type it manually every single time.
Any ideas on how this could be implemented ? Help would be much appreciated
One option would be to "blacklist" the ftdi_sio module to stop it being loaded automatically. To do that create the following file:
/etc/modprobe.d/ftdi_sio-blacklist.conf
# This is a comment. Change it if you want.
blacklist ftdi_sio
Put your command in /etc/rc.local and restart. See if it works. Or you can find how other .ko are configured to automatically load during system startup. Follow the same to make your module load automatically.
The proper way is to create a udev rule that is triggered when the specific USB device is attached.
Create a file /etc/udev/rules.d/99-usb-load.rules, and replace the "7523" and "1a86" with the Product ID and Vendor ID of your USB device.
# For debugging if the rule is working
ACTION=="add", ENV{ID_MODEL_ID}=="7523", ENV{ID_VENDOR}=="1a86", RUN+="/bin/sh -c '/bin/echo inserted device >> /tmp/udev_file'"
ACTION=="remove", ENV{ID_MODEL_ID}=="7523", ENV{ID_VENDOR}=="1a86", RUN+="/bin/sh -c '/bin/echo removed device >> /tmp/udev_file'"
# Actual rules
ACTION=="add", ENV{ID_MODEL_ID}=="7523", ENV{ID_VENDOR}=="1a86", RUN+="/sbin/rmmod ftdi_sio"
ACTION=="remove", ENV{ID_MODEL_ID}=="7523", ENV{ID_VENDOR}=="1a86", RUN+=""
Restart the udev daemon
sudo /etc/init.d/udev restart
The add rule will be executed whenever the USB device with matching PID/VID is attached and it will unload the module ftdi_sio. The first two rules are for debugging purposes which write a line into /tmp/udev_file and can be used to verify if the rules has been triggered.
Looks like USB device is new and there are no existing drivers to handle as soon as it is plugged in. You need an interrupt line and a USB driver code for your requirement. You need to register your device to that driver and driver to the USB bus. Also need to write appropriate interrupt routines to be called as soon as your device is plugged in. This will make it work as you want !!
If you already have a .rules file for the USB device then append the following to the pre-existing file in the /etc/udev/rules.d/ directory. Otherwise, create a file in the /etc/udev/rules.d/ directory with the following content:
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6010", ACTION=="add", RUN+="/sbin/rmmod ftdi_sio"
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6010", ACTION=="remove", RUN+=""
This means that when the device with vendorID 0403 and product ID 6010 is plugged in it runs /sbin/rmmod ftdi_sio which removes the ftdi_sio module. The second rule means nothing will be run when unplugged. See [writing udev rules] for more info on how to name the .rules file (e.g. 99-mydevice.rules)
You'll have to replace 0403 and 6010 with your device's vendor ID and prodcut ID. The vendor ID and product ID can be found by running lsusb on the command line after plugging in the USB device. It will have the format:
Bus xxx Device xxx: ID idVendor:idProduct ManufacturerName
After creating or editing the .rules file in /etc/udev/rules.d/ directory be sure to reload the .rules file with the following command:
sudo udevadm control --reload
Some further references on udev rules:
debian wiki
arch wiki
writing udev rules

libusb calls without sudo using udev-rules

Tested on Kubuntu 16.04 64 bit only. I have an application which source is not under my control. It uses some libusb calls which ends up in e.g.:
libusb: error [_get_usbfs_fd] libusb couldn't open USB device /dev/bus/usb/001/031: Permission denied
libusb: error [_get_usbfs_fd] libusb requires write access to USB device nodes.
When running the above mentioned application as root, it works as expected.
When I change the permissions of the regarding file like:
sudo chmod a+w /dev/bus/usb/001/031
then the application will work with standard user rights (until I disconnect / reconnect my usb device).
Now I'm looking for a way, to e.g. automatically execute the chmod a+w each time when the specific usb device is plugged in.
Might this be possible by writing a specific udev rule?
Maybe other solutions the libusb calls without root rights?
Solution: Based upon David Grayson's answer, I'd now added an additional line with SUBSYSTEM=="usb" to my rule file. My rules file now finally looks like this:
SUBSYSTEM=="tty", ATTRS{idVendor}=="1234", ATTRS{idProduct}=="5678", MODE="0666"
SUBSYSTEM=="usb", ATTRS{idVendor}=="1234", ATTRS{idProduct}=="5678", MODE="0666"
I suggest that you add a new file in /etc/udev/rules.d named usb.rules. It should have the following contents:
SUBSYSTEM=="usb", MODE="0666"
This will make all USB devices readable and writable by all users.
You could also narrow it down to specific USB devices using idVendor and idProduct attributes mentioned in Ignacio's answer.
Assuming Kubuntu 16.04 uses PolicyKit, put the following in a file in /etc/udev/rules.d, naming it similarly to the files that already exist there:
ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="xxxx", TAG+="uaccess", ENV{ID_MM_DEVICE_IGNORE}="1"
Replace the two sets of "xxxx" with the vendor ID and product ID of the device respectively.
Oldie but goldie. Help me to solve my issue with sharing the usb with virtual machines under AQEMU. Thanks a lot.
I added to the /etc/udev/rules.d file usb.rules with this line
SUBSYSTEM=="usb", ATTRS{idVendor}=="1234", ATTRS{idProduct}=="5678", MODE="0666"
and virtual machine can see USB stick being connected life.

IOIO-OTG connect PC udev rules for linux fails to detect

The rules file is called 50-ioio.rules and the the text is:
ACTION=="add", SUBSYSTEM=="tty", SUBSYSTEMS=="usb", SYMLINK+="IOIO%n", MODE="666"
I copied this file to the udev rules directory using:
sudo cp 50-ioio.rules /etc/udev/rules.d
I then restarted udev using:
sudo restart udev
However when I connect the IOIO board via a USB cable and look for the serial port with
ls /dev/IOIO*
It is not being created? I cant find any errors in syslog or anything in dmesg or lsusb - I suspect the udev string is wrong but it is in the documentation for the device?
I restarted the whole box and it got discovered, its not the real answer but at least I can progress

A2DP sink without pulseaudio

So I'm trying to make my linux server play music sent from my Android phone using bluetooth (the linux machine is the A2DP sink and the phone is the source).
What I have done so far is to:
install bluez and enable audiosource/audiosink
pair phone and server
connect to server from phone (phone says it's streaming audio over bluetooth)
But I can't hear anything. Also, most guides on the internet assumes Pulseaudio and I would prefer to use ALSA.
I currently have the following in /etc/asound.conf:
pcm.!default{
type bluetooth
profile "auto"
}
I'm running Bluez v4.99 and Alsa v1.0.25.
Any ideas?
I know this is an old post, but hopefully the answer is useful to people currently working on this.
You can use /etc/bluetooth/audio.conf, which is the system-wide file, or ~/.asoundrc, which is your local file. Both are read by BlueZ/ALSA. However, I think you need to include the MAC address in your config file, z.B.:
pcm.btheadset {
type bluetooth
device "XX:XX:XX:XX:XX:XX" #MAC address
profile "auto"
}
The best resources I've found for this are:
1) James B's blog post: Bluez must be one of the best kept secrets in Linux
He explains the structure and interface between BlueZ and ALSA, which I found nowhere else on the internet.
2) His second post with code: Bluez A2DP AudioSink for ALSA
3) The ALSA site, which introduces the structure of pcm plugins, but doesn't really explain them very well.
4) Some ALSA plugin tutorials: The ALSA wiki
Some useful commands:
$sudo service bluetooth restart
$sudo alsa force-reload
Run these after you change the asoundrc or audio.conf files.
Here you find a manual to configure bluetooth with ALSA or Pulseaudio:
-tested on Linux Mint 17.3 Mate, 64bit / Ubuntu 14.04 Mate, 64bit-
HOW TO MAKE ALSA AND BLUETOOTH WORK TOGETHER WITHOUT PA
This part is for pure ALSA-based systems without Pulseaudio like KXStudio, QStudio64,Tango studio..!
Be sure that Pulseaudio is deinstalled and your soundcards
configured right in ALSA!
delete pulse-audio cookies and files in
/home/USERNAME/.config/pulse
Modify your bluetooth-audio.conf:
type:
gksudo pluma /etc/bluetooth/audio.conf
Set
# This section contains options which are not specific to any
# particular interface
[General]
Enable=Socket
Save the file.
setup ~/.asoundrc file
type:
sudo hcitool scan
Scanning ...
XX:XX:XX:XX:XX:XX
Stereo Headset
Create a hidden-file named .asoundrc in your home-folder!and write:
pcm.!default {
type plug
slave.pcm {
type bluetooth
device "XX:XX:XX:XX:XX:XX"
profile "auto"
}
}
Replace “XX:XX..” with the adress of your device.
Save the file!
Run these after you change the asoundrc or audio.conf files:
sudo service bluetooth restart
sudo alsa force-reload
or reboot your computer!
———————————————————————————————————–—-
Go to your blueman-applet at your taskbar, search & setup your new device!
Note: Some bluetooth-devices need a passphrase (eg. 0000) by default
while some others takes shuffle-pairing.
Keep your found and paired device to “trust”!
Connect your device as AUDIO via A2DP!
————————————————————————————————————
If your device is connected with your system you can play sound with totem (gui), vlc (gui+terminal), mplayer (terminal), qmmp (gui), banshee and browsers over bluetooth while setting the output in players to “default”!
—————————————————————————————————–———
RENAME THE .ASOUNDRC IN HOME-FOLDER TO “.ASOUNDRC_OFF”
TO GET YOUR OLD SYSTEM-SETTINGS WITHOUT BLUETOOTH BACK!
type: mv /home/USERNAME/.asoundrc /home/USERNAME/.asoundrcOFF
->to disable the bluetooth-specific asoundrc
type: mv /home/USERNAME/.asoundrcOFF /home/USERNAME/.asoundrc
-> to enable it!
Alternative:
Make two “scripts” to activate/deactivate bluetooth with .asoundrc in your home-folder:
Create one empty file and write/paste:
#!/bin/bash
mv /home/USERNAME/.asoundrc /home/USERNAME/.asoundrcOFF
or:
#!/bin/bash
cd /home/USERNAME/
mv .asoundrc .asoundrcOFF
To activate your .asoundrc for bluetooth again, create another file with following entry:
#!/bin/bash
mv /home/USERNAME/.asoundrcOFF /home/USERNAME/.asoundrc
or:
#!/bin/bash
cd /home/USERNAME/
mv .asoundrcOFF .asoundrc
Make the files executable and run them with “open with terminal”!
Now you have two “buttons” to switch bluetooth-sound On or Off.
Give them individual-icons:
;-)
————————————————————————————————————-
PLAYERS
————–
It’s recommend to use totem, Qmmp or Banshee-player - because there is nothing more to do than
play and listen! Also totem-player shows videos too.
VLC-player needs the “default” sounddriver for bluetooth to working right!
Check in: /home/USERNAME/.config/vlc/vlcrc
that alsa-audio-device
in >>line1666<<
is:
# ALSA Device Name (string)
alsa-audio-device=default
Use this commands to use players with terminal:
To play music, type:
MPlayer
———––
mplayer /home/USERNAME/Musik/1.mp3
If you hear no sound or get errors try:
mplayer -ao alsa:device=default /home/USERNAME/Musik/1.mp3
VLC-Player
—————-
cvlc /home/USERNAME/Musik/1.mp3
vlc /home/USERNAME/Musik/1.mp3
rvlc /home/USERNAME/Musik/1.mp3
To add whole directories type:
rvlc add /home/USERNAME/Musik/
type: start/stop/next... to navigate!
or use the vlc-gui.
—————————————————————————————————–——-
To use mplayer with graphical-interface you have to install the gui-package:
type:
sudo apt-get install mplayer-gui
Open a terminal via Ctrl-Alt-T and use the commands below.Use the lines and type/copy one by one to avoid confusion(!):
cd /usr/share/mplayer/skins/default
for FILE in *.png; do sudo convert “$FILE” -define png:format=png24 “$FILE”; done
cd /usr/share/mplayer/skins; sudo rm default; sudo ln -s Blue default
———————————————————————————————————–
Now you can start the mplayer-gui without errors!
If you prefer the terminal type:
gmplayer /home/USERNAME/Musik/1.mp3
or
gmplayer -ao alsa:device=default /home/USERNAME/Musik/1.mp3
if you have problems!
terminal command without gui:
mplayer /DIRECTION/TO/FILE
Available Output-Modules and Drivers:
mplayer -vo help
———————————————————————-
BLUETOOTH WITH PULSEAUDIO
————————————————–--------
For Linux systems that comes with pulseaudio:
(1.) Edit /etc/bluetooth/audio.conf and uncomment the following line:
AutoConnect=true
Run the following command to install latest packages for blueman and related modules:
sudo add-apt-repository ppa:blueman/ppa
sudo apt-get update
sudo apt-get upgrade blueman
enter code here
(3.) Check if the following lines are added in /etc/pulse/default.pa:
.ifexists module-bluetooth-discover.so
load-module module-bluetooth-discover
.endif
If not, add the lines.
REBOOT!
Run the new blueman-applet.
Right click on the panel applet and turn bluetooth on.
The old profie of you bluetooth device would still be there, remove it.
Right click on the blueman-panel applet and select Setup New Device.
Complete the wizard for your device.(If you have problems with “pairing” restart your system and skip this step!)
Now you get a connected bluetooth device!
To send/hear audio over it just follow these 2 steps:
Open Mate-volume control-settings (or pavucontrol) with rightclick in taskbar,
browse to “HARDWARE” -
and use the dropdown to turn the Built-in audio profiles of all other soundcards “off”
so that the bluetooth-device gets active!
use the other dropdown to set the bluetooth device to use A2DP
Now start the totem-player or vlc (with pulseaudio-output) to send audio to bluetooth!
Enjoy your sound!
;-)
WLAN-ISSUES:
——————–—
In case you see a WiFi connection drop after connecting to the bluetooth device run the following
command:
$ echo “options iwlwifi 11n_disable=1 bt_coex_active=0 power_save=0 auto_agg=0 swcrypto=1” |
sudo tee -a /etc/modprobe.d/iwlwifi.conf > /dev/null
tested 2017 for LM 17.3 (Rosa) MATE and Ubuntu 14.04 by chalee:
http://mayastudio.tumblr.com/bluetooth
My guess is that your audio path isn't routing the bluetooth to your speakers. I have seen similar issue, which was fixed by manually doing step 5 "Connect PulseAudio bluetooth source to PulseAudio ALSA sink" from this link: http://jprvita.wordpress.com/2009/12/15/1-2-3-4-a2dp-stream/
I just tried to do the same thing, I'm using Fedora 19, so your mileage my vary. These are the steps I took.
Made computer discoverable
Pair phone to computer (it had a headphone logo next to it)
On my HTC phone I clicked "connect" on the pairing
Go into gnome3's sound settings
Go to the input tab
Select the bluetooth item for your phone
Maybe adjust volume...

Resources