How to disable a specific usb port permanently in linux? - linux

Is it possible to permanently disable a usb port in linux?
I have already figured out how to disable it:
echo -n "0000:00:12.0" > /sys/bus/pci/drivers/ohci_hcd/unbind
BUT after restart it is enabled.
I have placed this script :
#!/bin/sh
case "$1" in
resume|thaw)
echo -n "0000:00:12.0" > /sys/bus/pci/drivers/ohci_hcd/unbind
;;
esac
in /etc/pm/sleep.d/0_disable_usb2
But again without success. I also thought that i could disable it through bios but as i could see i can disable the whole pci.
Is there any way of doing this?
.. My Operating system is Debian 7.7 64bit. The reason i want to do this is I am trying to configure my system for realtime capabilities and my usb soundcard sharing the same IRQ with this port.

For me usb mounting is handle by a service udisk2.service if you would like to stop usb mounting then stop below service
root#mahasan-Inspiron-5537:~# systemctl start udisks2.service
root#mahasan-Inspiron-5537:~# systemctl disable udisks2.service

First of all you should find your USB number of your device, Simply by using lsusb.
And in Linux everything is a file, So you can manage all of your hardware using the files.
As it describes Here if your kernel is > 2.6.38 you should use this keywords:
echo "0" > "/sys/bus/usb/devices/usb2/power/autosuspend_delay_ms"
Then:
echo "auto" > "/sys/bus/usb/devices/usb1/power/control"

echo "0" > /sys/bus/usb/devices/usb{x}/authorized

Related

How to get hardware info about remote linux servers?

I've 120 remote linux servers with different OS's like ubuntu, debian, alt and I need to get hardware info (pci, usb devices, model cpu, hard drive etc). How I can do it easyest way by bash or something else? Tryed do via ssh and inxi but some servers closed for ssh connetcion and script exit.
cat server.list | while read i ; do ssh "$i" 'sudo apt-get install inxi' && ssh "$i" 'sudo inxi -Fxxx' > /tmp/summ/"$i".txt ; done```
Finaly I used OCS Inventory - it's better way for my task

How to set up a udev rule for ETH "link down"/"link up"?

I like to switch on a green LED(connected through GPIOs), when eth0 is connected. When disconnected I like to switch the green LED of and a red one on.
Thought that udev is maybe the right place for it. I created the simple demo rule:
KERNEL=="eth0", SUBSYSTEM=="net", ACTION=="add", RUN+="/sbin/set_BSPleds eth0 on"
This rule should call a script when the eth0 is added. It was never executed.
After I was looking to the udev monitor by entering "udevadm monitor -k -u" at the shell. There were no events coming when I unplug/plug the lan cable.
root#sama5d3xek:/etc/udev/rules.d# udevadm monitor -k -uh0
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent
root#sama5d3xek:/etc/udev/rules.d#
Seems that there are no uevents for eth0. The ethernet driver is provided my ATMEL. I am building a custom Linux by the help of Yocto.
My question is, how to get the "link down"/"link up" events to udev?
If it does not works with udev, what alternative way is the best?
As others have already mentioned, it seems one can't use udev for this.
Ubuntu: wait for network link up and execute a bash command suggests
inotifywait -e modify /sys/class/net/eth0/carrier; echo 'Change detected'
(Currently, / is on nfs for my box, so I can't really say if it will work.)
In other posts, there are some concerns about using the inotify API on /sys: inotify_add_watch fails on /sys/class/net/eth0/operstate .
I think the Right Way(TM) do do this would be to use the netlink(7) API, preferably through a daemon such as netplugd.
Hope that helps :)
Ethernet devices are devices, but connections are not.
You could trace connection through /dev/xconsole, dmesg or /var/log/kern.log.
Sample, using rsyslog:
You could (as root):
echo ':msg, contains, "Link is" |/run/netlink' >/etc/rsyslog.d/netlinkstate.conf
mkfifo /run/netlink
chgrp user /run/netlink
chmod 640 /run/netlink
service rsyslog force-reload
Then, logged as user, simply:
read line < /run/netlink
will wait for input from fifo, then hold until something happen.
state=${line#*Link is } eventtime=${line%% $HOSTNAME*}
echo $eventtime $state
2016-11-21T17:40:50.956181+01:00 Down
or
echo $eventtime $state
2016-11-21T17:40:50.956181+01:00 Up 100 Mbps Full Duplex, Flow Control: Rx/Tx
echo $eventtime ${state%% *}
2016-11-21T17:40:50.956181+01:00 Up
Under bash, you could use timeout for emptying fifo and read only last entry:
while read -t .01 entry </run/netlink;do line="$entry";done
state=${line#*Link is }
eventtime=${line%% $HOSTNAME*}
shortstate=${state%% *}
Nota: I've used /run to store fifo. This could be not the better place as this won't exist on next reboot.

Detect if an ethernet port is unplugged

I want to trap the event of unplugging an ethernet port on a Linux Ubuntu 14.04.
I want to create a script that detect whenever an ethernet port is unplugged and write it to a log.
Which is the best way to trap such an event
Just put an executable script inside /etc/network/if-post-down.d.
#!/bin/bash
set -e
if [[ "$IFACE" == "wlan0" ]]; then
logger "The wlan0 interface is down!"
# Do whatever you want here.
fi
Make sure to chmod +x it, also.
Read more about these events/scripts here on the Ubuntu Wiki.

Force unmount of usb drive by closing open applications programatically [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
When I unplug the AC adapter from my laptop I want all USB drives to automatically unmount. If applications are open that block the device, they should be killed. Once everything is killed and unmount a signal tone could be plaid to indicate that it's now safe to unplug it.
The use case is to quickly grab and go your laptop without having to fumble with the ui to get all drives disconnected but avoid unclean unmounts.
Any hints on how to start would be fantastic, thanks you!
ANSWER
For a full copy&paste script see my answer below.
If your USB devices mount to /mount/media do:
kill -9 $(lsof -t $(mount | grep "/mount/media" | cut -d " " -f 1)) # Exit processes blocking umount cleanly
kill $(lsof -t $(mount | grep "/mount/media" | cut -d " " -f 1)) # Force kill remaining open processes
umount $(mount | grep "/mount/media" | cut -d " " -f 1) # Unmount USB drives
Be careful with this since if you don't have blocking applications open lsof will return all pids and you will kill your running OS. See the copy&paste script below for a working implementation that handles this case.
Then call this script whenever the AC adapter is unplugged by adding this line to /etc/udev/rules.d
SUBSYSTEM=="power_supply", ACTION=="change", ATTR{online}=="0" , RUN+="/path/to/script/shown/above"
The answer below by Nuetrino shows how to detect the AC unplug event, this answer: How do I find out which process is preventing a umount? shows how to list and kill all processes blocking the device from unmounting (I had more success with lsof than fuser -c which sometimes didn't list any processes even though umount was still being blocked)
Details
Use udevadm monitor to log the event, e.g.
KERNEL[20154.545075] change /devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0003:00/power_supply/ADP0 (power_supply)
then use udevadm info -a -p with the event to get the attributes
udevadm info -a -p /devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0003:00/power_supply/ADP0
looking at device '/devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0003:00/power_supply/ADP0':
KERNEL=="ADP0"
SUBSYSTEM=="power_supply"
DRIVER==""
ATTR{online}=="0"
ATTR{type}=="Mains"
Now you can set up the udev rules with the attributes you like as answered below.
You can define udev rules to do it.
Just put your rule in /etc/udev/rules.d
Here is a example from me which i use to control brightness when i remove the AC adapter.
SUBSYSTEM=="power_supply", ACTION=="change", ATTR{online}=="0" , RUN+="/usr/local/bin/bright_unplug"
SUBSYSTEM=="power_supply", ACTION=="change", ATTR{online}=="1" , RUN+="/usr/local/bin/bright_replug
I run my custom script 'bright_replug' and 'bright_unplug' when I recieve kernel uvent.
You can use udevadm monitor to
monitor will print the received events for: UDEV - the event which
udev sends out after rule processing KERNEL - the kernel uevent
You can use udevadm info to match more attributes
Step by step solution
1.
Create this script e.g. in /home/user/uall.sh and replace mount_root with the folder where your distribution mounts usb drives, e.g. /media/user
#!/bin/bash
mount_root=/run/media/user
echo "Try unmounting.."
umount $(ls -d -1 $mount_root/*) # Unmount USB drives
mounted=$(ls -d -1 $mount_root/*) # Probe if there are still applications blocking
if ! [ -z "$mounted" ]
then
echo "Found blocked devices: $mounted, killing.."
kill -9 $(lsof -t $mounted) # Exit processes blocking umount cleanly
kill $(lsof -t $mounted) # Force kill remaining open processes
echo "Unmounting.."
umount $(ls -d -1 $mount_root/*) # Unmount USB drives
mounted=$(ls -d -1 $mount_root/*) # Probe if there are still applications blocking
fi
if [ -z "$mounted" ]
then
echo "Success!"
echo "All USB devices umount."
paplay /usr/share/sounds/speech-dispatcher/test.wav
else
echo "Error!"
echo "Tried it all but couldn't umount all USB devices."
echo "These devices are still mounted:"
echo "$mounted"
fi
2.
Create a udev wrapper script (call it /home/user/uall-udev-wrapper) that executes uall.sh as your username:
#!/bin/bash
runuser -l <user> -c '/home/user/uall.sh > /home/user/uall.log'
3.
Create the file /etc/udev/rules.d/99-usb-unmount.rules with the content
SUBSYSTEM=="power_supply", ACTION=="change", ATTR{online}=="0" , RUN+="/home/user/uall-udev-wrapper"
4.
Reboot or run sudo udevadm control --reload-rules && udevadm trigger to load the new udev rule
Optional. Add an alias uall=/home/user/uall.sh to your ~/.bashrc to access the script easily from your terminal and use your Desktop environment to configure a hotkey to quickly unmount all usb drives
Caveats
1.
When udev runs the script mount will not display gvfsd-fuse mount points, neither will cat /proc/mounts or cat /etc/mtab not even if using the runuser -l <user> wrapper. Instead I'm using now ls -d -1 $mount_root/* which only returns the devices mounted by the current user specified in $mount_root, on a multi user system devices mounted by a different user will not be unmounted by this script.
2.
When udev runs the script I do not get audio from paplay or spd-say.
Any input on these caveats would be greatly appreciated.

How to initialize Bluetooth in a startup script with Yocto Poky Linux

I have a script that initializes my bluetooth setup on an Intel Edison. It allows pairing and connecting to this headless machine running Yocto Poky Linux. It was suggested that I put the startup script in /etc/init.d and run update-rc.d myscript.sh defaults. The script ran but it didn't work (generated boot errors saying bluetooth device not found) because Bluetooth had not started yet. I did some reasearch and after removing my links I did update-rc.d myscript.sh defaults 99 which was claimed to run the script last but it did't make any differrence -- it still ran in the same place in the boot sequence. I verified that the links had S99 on them so it seemed like they were set up correctly. There is another post on SO asking a similar question but that was a Ubuntu system where mine is Poky Linux. That solution suggested putting the startup script in a directory that does not exist on my system. There were other suggestions, putting it in rc.local, which I did and got the same result, it runs before Bluetooth is initialized.
Here is my script. My program is called nmea_thread and is run last. Everything else is initializing Bluetooth.
#!/bin/sh
/usr/sbin/rfkill unblock bluetooth
/usr/bin/hciconfig hci0 up
/usr/bin/hciconfig hci0 piscan
/usr/bin/hciconfig hic0 sspmode 0
/home/root/simpleAgent/simple-agent &
/home/root/nmea_thread
Often bluetooth is initialized asynchronously, so you can't be sure that your script will be run after hci0 is added. Good solution is to wait for BT initialization in background:
#!/bin/bash
if [ "$1" != "background" ]; then
$0 background &
else
#Wait until BT is initialized
for ((i = 0; i <= 100; i++)) do
hciconfig hci0 && break
usleep 100000
done
/usr/sbin/rfkill unblock bluetooth
/usr/bin/hciconfig hci0 up
/usr/bin/hciconfig hci0 piscan
/usr/bin/hciconfig hic0 sspmode 0
/home/root/simpleAgent/simple-agent &
/home/root/nmea_thread
fi
hciattach is the correct way.
syntax
hciattach /dev/ttyAMA0 bcm43xx 3000000
you need to flash the driver first before initializing it. Currently i don't remember how, but thats how i made it with raspberry pi and yocto.
Note if you use systemV, you can do call it from a script and it will work
Using SystemD, you need to make it in a service and wait. Falshing should be done in the two cases.

Resources