原文:https://www.cnblogs.com/lifexy/p/10107310.html
上篇博客只是讲解了普通U盘挂载,并没有涉及到多分区U盘,接下来本章来继续学习
1.多分区U盘和普通U盘区别
- 1)U盘插上只会创建一个/dev/sda文件,这种一般表示该U盘没有分区,这个sda文件便代表该U盘总大小,我们只需要挂载/dev/sda即可
- 2)另一种U盘则会创建/dev/sda以及/dev/sda1文件,这种一般表示该u盘只有一个分区sda1,我们只需要挂载/dev/sda1即可
- 3)最后一种U盘则是多分区情况,会创建/dev/sda,/dev/sda1,/dev/sda2... .../dev/sdaN等,这种表示该U盘有sda1~sdaN多个分区,我们需要挂载/dev/sda1~sdaN才行
2.接下来重新修改/etc/mdev.conf文件
sd[a-z] 0:0 660 * if [ $ACTION = "add" ]; then /etc/hotplug/usb/udisk_insert.sh; else /etc/hotplug/usb/udisk_remove.sh; fi
sd[a-z][1-9]+ 0:0 660 * if [ $ACTION = "add" ]; then /etc/hotplug/usb/udisk_insert1.sh; else /etc/hotplug/usb/udisk_remove.sh; fi
上面表示:
- 当出现/dev/sda时,则会调用/etc/hotplug/usb/udisk_insert.sh,当移除/dev/sda时,则会调用/etc/hotplug/usb/udisk_remove.sh
- 当出现/dev/sda1~9时,则会调用/etc/hotplug/usb/udisk_insert1.sh,当移除/dev/sda1~9时,则会调用/etc/hotplug/usb/udisk_remove.sh
3.添加/etc/hotplug/usb/udisk_insert.sh文件
#!/bin/sh
MOUNT_POINT=/mnt/usbdisk
if [ ! -f $MDEV ]; then
if [ ! -d $MOUNT_POINT ]; then #判断挂载目录是否存在
mkdir -p $MOUNT_POINT
fi
echo "${MDEV} usb insert"
sleep 0.5 #延时500ms,判断一下是否有sda1,如果没有,则表示没有分区
if [! -e "/dev/sda1"]; then #如果有/dev/sda1,则属于多分区U盘,所以不需要再挂载/dev/sda了
echo " ${MDEV} don't have to create " > /dev/console
exit
fi
echo "create usb disk mount point: $MOUNT_POINT." > /dev/console
mount -t vfat,exfat,ntfs /dev/$MDEV $MOUNT_POINT
if [ $? -ne 0 ]; then
echo "mount usb disk ${MDEV} falied!" > /dev/console
else
echo "mount usb disk successfully!" > /dev/console
fi
fi
注意: 由于/dev/sda文件最先创建,所以上面需要延时500ms后,判断是否有/dev/sda1,来区别是否是多分区
4.添加/etc/hotplug/usb/udisk_insert1.sh文件
#!/bin/sh
POINT_SOURCE=/mnt/usbdisk
num=${MDEV#*sda} #获取sda后缀数字
MOUNT_POINT=${POINT_SOURCE}${num}
if [ ! -f $MDEV ]; then
if [ ! -d $MOUNT_POINT ]; then
mkdir -p $MOUNT_POINT
fi
echo "${MDEV} usb insert1"
echo "create usb disk mount point: $MOUNT_POINT." > /dev/console
mount -t vfat,exfat,ntfs /dev/$MDEV $MOUNT_POINT
if [ $? -ne 0 ]; then
echo "mount usb disk ${MDEV} falied!" > /dev/console
else
echo "mount usb disk successfully!" > /dev/console
fi
fi
5.添加/etc/hotplug/usb/udisk_remove.sh文件
#!/bin/sh
POINT_SOURCE=/mnt/usbdisk
num=${MDEV#*sda}
MOUNT_POINT=${POINT_SOURCE}${num}
umount $MOUNT_POINT
if [ $? -ne 0 ]; then
echo "umount usb disk falied! ${MDEV} " > /dev/console
else
echo "umount usb disk successfully!" > /dev/console
fi
rm -rf $MOUNT_POINT
echo "remove usb disk mount point: $MOUNT_POINT." > /dev/console