在上章學習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