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盘和移动硬盘的设备名

 

你可能感兴趣的:(linux)