Centos 7自動掛載U盤和移動硬盤

Centos 7自動掛載U盤和移動硬盤

1、拷貝文件99-mount-usb.rules到/etc/udev/rules.d目錄,文件權限  644

#99-mount-usb.rules

SUBSYSTEM=="block", KERNEL=="sd[a-z][0-9]", ENV{ID_BUS}=="usb",  ACTION=="add",  OWNER="root", GROUP="root", MODE="0777",  RUN+="/sbin/mount-usb.sh %k"
SUBSYSTEM=="block", KERNEL=="sd[a-z][0-9]", ENV{ID_BUS}=="usb",  ACTION=="remove",  RUN+="/sbin/mount-usb.sh %k"

2、拷貝文件mount-usb.sh 到/sbin/目錄,文件權限755

#mount-usb.sh

#!/bin/sh
if [ $ACTION == "add" ]
then
    mkdir -p /mnt/usb/$1
    chmod 777 /mnt/usb/$1
    mount /dev/$1 /mnt/usb/$1
    sync
    echo "add /mnt/usb/$1">>/mnt/usb/mount.log
elif [ $ACTION == "remove" ]
then
    sync
    umount /mnt/usb/$1
    rm -rf /mnt/usb/$1
    echo "remove /mnt/usb/$1">>/mnt/usb/mount.log
fi

3、修改文件/usr/lib/systemd/system/systemd-udevd.service,將 MountFlags 改爲 shared

#systemd-udevd.service

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=udev Kernel Device Manager
Documentation=man:systemd-udevd.service(8) man:udev(7)
DefaultDependencies=no
Wants=systemd-udevd-control.socket systemd-udevd-kernel.socket
After=systemd-udevd-control.socket systemd-udevd-kernel.socket systemd-sysusers.service
Before=sysinit.target
ConditionPathIsReadWrite=/sys

[Service]
Type=notify
OOMScoreAdjust=-1000
Sockets=systemd-udevd-control.socket systemd-udevd-kernel.socket
Restart=always
RestartSec=0
ExecStart=/usr/lib/systemd/systemd-udevd
MountFlags=shared    #slave
KillMode=mixed

4、創建/mnt/usb目錄 ,目錄權限777
      sudo mkdir -p /mnt/usb

      sudo chmod 777 /mnt/usb

5、重新加載udev規則

      sudo /sbin/udevadm control --reload

6、重啓udevd服務

      sudo systemctl dameon-reload
      sudo systemctl restart systemd-udevd.service

 

參考1  通過udevadm查看U盤和移動硬盤信息

udevadm info /dev/sdb1   

/dev/sdb1  爲U盤和移動硬盤的設備名

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章