How to mount a character device like eeprom in LINUX - linux

I know I can mount a block device like SD card by the following command;
mount /dev/mmcblk /mnt/SD
Then I can use ls and cp command in the filesystem in SD. I can also execute a file in it.
However, I don't know how to mount a character device like eeprom or nor flash which is controled by I2C or SPI.
I want to make a embedded system that doesn't depend on SD. My goal is to use ls or cp command, and execute some files in such devices like you can do it in SD.
Actually, I made an original I2C driver in which I can open, read and write data through /dev/myi2cdriver.
But when I mount that device file, error message appears saying "device is not a block device" and mount fails.
Could you give me advices?

As you have seen, this isn't intended to work.
You could probably copy /dev/myi2cdriver to a file and mount -o loop that. You might be able to just mount -o loop your character device directly.
Alternatively, develop a block device driver?

Related

Auto mounting USB on a yocto linux embedded project with udev

I have a linux embedded system based on yocto up and running and need to get it to automount USB devices. The system uses udev and the following is the /etc/udev/rules.d/99-auto-mount.rules.
KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_auto_mount_end"
# Import FS infos
IMPORT{program}="/sbin/blkid -o udev -p %N"
ENV{ID_PATH}!="*-usb-*", GOTO="media_by_label_auto_mount_end"
# Get a label if present, otherwise specify one
ENV{dir_name}="USB%k"
# Global mount options
ACTION=="add", ENV{mount_options}="relatime"
# Filesystem-specific mount options
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002", ENV{DISPLAY}=":0", RUN+="/usr/local/bin/announce /media/%E{dir_name} 1"
# Mount the device
ACTION=="add", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}"
# Clean up after removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}", ENV{DISPLAY}=":0", RUN+="/usr/local/bin/announce /media/%E{dir_name} 0"
# Exit
LABEL="media_by_label_auto_mount_end"
It works, after a fashion, but is unable to do all I want it to do. When an USB memory stick is inserted (/dev/sda1 for the partition) it does create the folder /media/USBsda1 during add and delete the folder /media/USBsda1 during remove when the USB memory stick is yanked.
But it never mounts the USB memory stick.
I boiled the rules file down to the following bare bones file just to try and get it to mount the USB memory stick.
A USB memory stick is inserted and it's partition is then located at /dev/sda1 in the system.
KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_auto_mount_end"
# Mount the device
ACTION=="add", RUN+="/bin/mkdir -p /media/USBsda1", RUN+="/bin/mount /dev/sda1 /media/USBsda1"
# Exit
LABEL="media_by_label_auto_mount_end"
The folder gets created, but the stick is not mounted.
However, right after inserting the stick and the folder is created I can mount it manually in the console with the exact command from the rules
$> /bin/mount /dev/sda1 /media/USBsda1
and it mounts just fine?
Does anyone have any idea as to what could possibly be the problem (or more likely what is missing) or any suggestion of lines of investigations to conduct?
Yocto version = 2.1.3
udevadm version = 229
After much tinkering and reading information on the web I found a solution that worked on my system.
I had to insert a systemd service after the udev rule and then a bash script called from the service that did the heavy lifting.
So a thanks goes out to Mike Blackwell for his excellent answer to a similar question over on stackexchange. https://serverfault.com/a/767079
I used his suggestion with a few tweeks for my own system and it worked perfectly.

Write a bash script that executes a command when a USB device is plugged in/removed [duplicate]

Is there a Bash script and/or daemon that I can write that will detect a specific USB drive and then sync that drive with a directory?
For future reference, here's how to run a bash script upon detection of a USB drive.
Connect your device and run lsusb to retrieve the device's info. You should see something similar to this:
$ lsusb
Bus 002 Device 039: ID 0bc2:2100 Seagate RSS LLC
In this case, the vendor ID of the device is 0bc2 and the product ID is 2100.
Now you can create your UDEV rule using a text editor of your choice.
$sudo vi /etc/udev/rules.d/85-my_usb_device_rule.rules
And add this:
ACTION=="add", SUBSYSTEM=="usb", SYSFS{idVendor}=="0bc2",
SYSFS{idProduct}=="2100", RUN+="/home/myhome/my_script"
/home/myhome/my_script is the path to your script which it would do whatever you want.
To make sure the detection script will execute right away, run this command to reload the UDEV rules:
$sudo udevadm control --reload-rules
This was tested on Fedora 14.
I didn't do it myself, but you can try udev rules like this:
# Hitachi SimpleDrive mini, model HTS545050B9A300 (500 GB USB HDD)
SUBSYSTEM=="block", ATTR{size}=="976768002", ATTRS{product}=="SimpleDrive mini", ATTRS{serial}=="2512009121920487", ACTION=="add", RUN+="/lib/udev/local.usb.hdd.sh add $devpath"
Place it in /etc/udev/rules.d/90-local.rules or similar place, certainly dependable on your OS.
Here is an example python deamon that you could use for the listening part, then copying the files to your directory shouldn't be a problem.
There are excellent answers here already but depending on your use case, it could be as simple as
[ -d /run/media/$USER/USB_STICK ] && do_stuff
USB_STICK here is the label of the usb stick.

How to insert my driver automatically on the insertion of USB mouse in Linux System?

I know that on the insertion of any usb device in the Linux system a specific device driver got loaded. Now I want to insert my driver on the insertion of USB mouse.
I know that I can do this task using two approaches: by using depmod or using udev concept. I have read a few things about it on the Internet but I don't get the exact answer. Can anyone suggest which approach is best to use and for that in the Linux kernel tree where I need to make changes?
Thanks all for your help.
I follow the udev approach to load module automatically on the USB insertion
Below is the procedure to load your Driver automatically on the Insertion of Hot plug-gable device (I experiment with the USB mouse and below procedure is working fine for it)
Run Following command
cmd > udevadm info -a -p $(udevadm info -q path -n /dev/input/mouse)
In place of ?? in the above command user need to add the device ID based on its entry in /dev (e.g.for USB flash drive: sdb1 or sda1 etc. based on the device identity)
Get the Value of the below parameters from the output of above command
KERNEL, ATTRS{idVendor}, ATTRS{idProduct}, ATTRS{serial}
Go to /etc/dev/rule.d directory and Add your rule
cmd > sudo vim 40-usbmouse.rules
ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd?1", ATTRS{idVendor}=="058f", ATTRS{idProduct}=="6387", ATTRS{serial} =="4EPLXAXE", SYMLINK+="usbpd", RUN+="/usr/local/bin/InsertModule.sh"
Save this file.
Above rule is defined for the USB Mouse.
Parameter SYMLINK creates a link of your device in the /dev directory and In RUN+ you can give your script location which is going to execute on your device insertion.
For more info on How to write a rule refer below link
http://hackaday.com/2009/09/18/how-to-write-udev-rules/
Now after you define your rule user need to restart the udev to take your rule in notice by kernel.
cmd > sudo stop udev
cmd > sudo start udev
Insert your USB and validate that your script which you define in your rule shows its effact.
For Mouse user can use below command
cmd > udevadm info -a -p $(udevadm info -q path -n /dev/input/mouse)
P.S.: cmd stands for the command shell (Terminal).The above procedure is working with any USB device.
You may use MODULE_SOFTDEP macro defined in module.h in your driver where you can specify the name of the USB driver which gets loaded when the USB mouse is inserted. This will load your driver automatically. The depmod approach is the way to go.
Use MODULE_DEVICE_TABLE macro. That will export supported device table so that your hotplug tool (whether it udev or something else) can load your module. I've described the loading process in this answer.
To see example usage, refer to drivers/hid/usbhid/usbmouse.c. If this driver compiled as module, it is loaded every time any usb mouse is attached.

bash command to force file closure on usb drive

I thought doing a sync in my bash script would force the file to be completely written out. When I looked at the thumb drive, it showed all the files I had copied, but after a power supply failure, the usb drive showed 0 files. Do I have to eject the drive manually or is there something I can do programmatically in my script?
If you want to eject the usb device from your bash script a simple umount on the device should do the trick. For example
mount /dev/usb /mnt/usb
# Your copy operations here... then on success:
umount /mnt/usb
you can also try to use the linux sync instruction that syncronize writing over disk if you're usb key is using a journalized file system

programmatically detect SD card write protect

Back in the good-old days of floppy, if you enable write protection of a floppy, DOS would kindly tell you that you cannot write to it. Now we have SD card that can hold the content of thousands of floppy and we still have the write protection - and it's handy sometime. But nobody is able to tell me I can't write to it, at least on Linux. I have a lovely script that partition and format a SD card in a way I like. It took me 1/2 hour of debugging just to find out that the SD card is write-protected.
So the question, is there a way that the software can detect such condition?
Thanks,
The driver knows when the card is write-protected, and it actually warns when you mount it via command line:
# mount /dev/sdc1 /media/flash
mount: block device /dev/sdc1 is write-protected, mounting read-only
In case you would like to check it yourself at device level, you can use the hdparm command to query read-only status of disk device, including SD card and USB flash drive in general. This program should be available in most GNU/Linux distributions, commonly in a package named "hdparm".
If you are not root, be sure to specify full path to the hdparm command; and this assumes you have read permission on your card of course.
For example: my SD card is inserted, detected as /dev/sdc, and write protection tab is at Unlock:
$ /sbin/hdparm -r /dev/sdc
/dev/sdc:
readonly = 0 (off)
When I slided the write protection tab to Lock, re-insert the card, and run the command again:
$ /sbin/hdparm -r /dev/sdc
/dev/sdc:
readonly = 1 (on)
If you would like to do it in shell script, you can try something like:
READONLY=`/sbin/hdparm -r /dev/sdc 2>&1 | sed -n 's/^.*= *\([01]\) .*$/\1/p'`
if [ "$READONLY" = "0" ]
then
echo Card is writable.
else
echo Card is not writable.
fi
Note: If you prefer to do it in C, you can try either:
Opening the device file in write mode and see if it fails with errno value EROFS (Read-only file system), or...
Opening the device file in read mode, then issue ioctl() named BLKROGET, and check if the result value is nonzero; this is the way hdparm work.

Resources