About implementing Udev/mdev automatic mount and unload

Source: Internet
Author: User

There are a lot of mdev on the internet is basically a version of the automatic mount, after testing the automatic mount is really feasible, but about the automatic unloading mdev does not seem to be very good support, after modification can be done with the effect of udev similar. You cannot hot-plug in a mounted directory, otherwise the problem may occur, but this issue will not affect the next time you insert the USB drive, possibly damaging the USB stick.

In this paper, two methods of Mdev and Udev are introduced to realize automatic mount, and readers can choose one of them according to their needs.

First introduce the relationship between Mdev and Udev:

Mdev is a busybox in the Udev management program of a lite version, he can also achieve automatic device node creation and automatic device mount, but in the implementation of the process is a little different, in the event of hot-plug time, Mdev is hotplug direct call, Mdev the action and DEVPATH in the environment variable to determine the behavior of the hot-swap event and the directory in the/sys. You will then see if there is a "dev" property file in this directory, and if so, use this information to create a device node file for this device in/dev.

/************************************************************************************************************** *********************************************************************/

1.mdev Support

① when making the root file system with BusyBox, select the support Mdev mechanism

Linux System Utilities--->
[*] Mdev
[*] Support/etc/mdev.conf
[*] Support command execution at device Addition/removal

② Add the following in the file system/ETC/INIT.D/RSC file

Vi/etc/init.d/rcs
Mount-t Tmpfs Mdev/dev
Mount-t Sysfs Sysfs/sys
Mkdir/dev/pts
Mount-t devpts devpts/dev/pts

Echo/sbin/mdev>/proc/sys/kernel/hotplug
Mdev–s

The addition of these statements can be found in the BusyBox/doc/mdev.txt.

③ adds a response to hot-swappable events, enabling automatic mounting of USB and SD cards.

Vi/etc/mdev.conf
SD[A-Z][0-9] 0:0 666 @/etc/mdev/udisk_insert

Sd[a-z] 0:0 666 $/etc/mdev/udisk_remove

The red part, is a script, the script content can be customized according to our needs, can implement mount, uninstall or some other functions.

Note: @ means that the following script is executed after inserting (creating a Device node), which means that the following script is executed before unplugging (deleting the device node).

The following is the name of the script that is automatically mounted and unloaded and its contents:

#!/bin/sh

If [-d/sys/block/*/$MDEV]; Then

Mkdir-p/media/$MDEV

mount/dev/$MDEV/media/$MDEV

Fi

The contents of the Etc/mdev/udisk_remove file in the root file system:
#!/bin/sh umount-l/media/$MDEV rm-rf/media/$MDEV #!/bin/sh umount-l/media /sd* rm-rf/media/sd*

Can be mounted automatically when modified to a red part

The above two scripts require executable permissions: chmod +x/etc/mdev/udisk_insert

chmod +x Etc/mdev/udisk_remove

/************************************************************************************************************** *********************************************************************/

2.udev Support

Linux traditionally uses static device-created methods to create a large number of nodes under Dev, regardless of the presence of the corresponding hardware devices for those nodes. Using the Udev method, the system detects that the device is not going to create the corresponding node for these devices.

Here, let's simply talk about how Udev works:

Udev is dependent on SYSFS, and when a new device is added to the system, the kernel detects a HotPlug event and finds/proc/sys/kernel/hotplug to find the user-space program that manages the device connection, if Udev has started, The kernel notifies Udev to detect information about the new device in SYSFS and to create a device node. such as/dev/vcs, in/sys/class/tty/vcs/dev storage is "7:0", both the primary and secondary equipment number/dev/vcs. And Udev will also implement some corresponding functions according to the rules files in/ETC/UDEV/RULES.D.

Below we introduce how to realize the automatic creation of the device node and the automatic mounting of the U-disk or SD card.

Because Udev is not supported by default in the file system, we are porting a udev.

1. Download the Udev source udev-100.tar.bz2 and Unzip

Website: http://www.us.kernel.org/pub/linux/utils/kernel/hotplug

2. Cross-compiling.

Modify the makefile, specifically as follows:

Cross = arm-linux-

Save exit.

Then execute the command: make to compile, then execute Arm-linux-strip udev udevd udevstart udevinfo udevtest, and copy these files to the target board root file/bin directory below.

3. Add support for Udev

The following three methods function the same

(1) and modify the Etc/init.d/rcs script, then add the following command:

/bin/mount-t Sysfs Sysfs/sys

/bin/mount-t Tmpfs Tmpfs/dev

/BIN/UDEVD--daemon

/bin/udevstart

(2) If LINUXRC is a binary file

Rm/linuxrc

Vi/linuxrc

Add the following content

/bin/mount-t Sysfs Sysfs/sys

/bin/mount-t Tmpfs Tmpfs/dev

/BIN/UDEVD--daemon

/bin/udevstart

Exec/sbin/init

(3) Modify/etc/fstab to

#device mount-point Type options dump fsck order

PROC/PROC proc Defaults 0 0

Tmpfs/tmp TMPFS Defaults 0 0

Sysfs/sys Sysfs Defaults 0 0

Tmpfs/dev TMPFS Defaults 0 0

To modify/etc/init.d/rcs, add the following:

/BIN/UDEVD--daemon

/bin/udevstart

Restart the system and the file system will be able to create nodes automatically.

4. Create the Udev directory under/etc

5. Under/etc/udev, wear a catalog rules.d and file udev.conf

6. In udev.conf, add the following:

# udev.conf

# The initial syslog (3) Priority: ' Err ', ' info ', ' debug ' or its

# numerical equivalent. For runtime debugging, the Daemons internal

# state can is changed with: "Udevcontrol log_priority=<value>".

Udev_log= "Err"

7. Create a rule file under RULES.D

If you implement automatic mounting of the U-disk

Vim 11-add-usb.rules

Add the following content

Action!= "Add", goto= "Farsight"

kernel== "sd[a-z][0-9]", run+= "/sbin/mount-usb.sh%k"

Label= "Farsight"

The action in this file is a description of what the event, after the kernel is what device such as SDA1,MMCBLK0P1, and so on, run this device to execute which program%k is passed in the parameters of this program, where the%k=kernel value is sda1 and other HTTP ://www.woaidiannao.com.

Create a mount-usb.sh file under/sbin/Add the following computer

#!/bin/sh

/bin/mount-t vfat/dev/$1/tmp

Sync

Modify file permissions to add executable permissions for it.

This realizes the automatic mounting of the U-disk, the following is attached to the U-disk unloading rules files and SD card files

USB Offload

11-add-remove.rules

Action! = "Remove", goto= "Farsight"

subsystem!= "Block", goto= "Farsight"

kernel== "sd[a-z][0-9]", run+= "/sbin/umount-usb.sh"

Label= "Farsight"

umount-usb.sh

#!/bin/sh

Sync

umount/tmp/

SD card Mount

12-add-sd.rules

Action!= "Add", goto= "Farsight"

kernel== "mmcblk[0-9]p[0-9]", run+= "/sbin/mount-sd.sh%k"

Label= "Farsight"

mount-sd.sh

#!/bin/sh

/bin/mount-t vfat/dev/$1/tmp

Sync

SD Uninstall

12-remove-sd.rules

Action! = "Remove", goto= "Farsight"

subsystem!= "Block", goto= "Farsight"

kernel== "mmcblk*", run+= "/sbin/umount-sd.sh"

Label= "Farsight"

umount-sd.sh

#!/bin/sh

Sync

/bin/umount/tmp/

About implementing Udev/mdev automatic mount and unload

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.