在上章学习33.linux-实现u盘自动挂载(详解)后,只是讲解了普通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