1、检测U盘
当U盘插进去之后,在/proc/scsi/里面就会新增一个文件夹-----usb-storage,当前插了多少个U盘,usb-storage下就会有多少个文件(不包括 "." ".."),因此也可以将这个作为检测U盘是否插上的接口。
要准确的查找U盘,应该用 fdisk -l 命令,(下面为当前插了一个U盘的情况)
Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x00012d75
Device Boot Start End Blocks Id System
/dev/sda1 * 1 25 200781 83 Linux
/dev/sda2 26 2610 20764012+ 8e Linux LVM
Disk /dev/dm-0: 20.1 GB, 20132659200 bytes
255 heads, 63 sectors/track, 2447 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x00000000
Disk /dev/dm-0 doesn't contain a valid partition table
Disk /dev/dm-1: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x30307800
Disk /dev/dm-1 doesn't contain a valid partition table
Disk /dev/sdb: 4214 MB, 4214226944 bytes
130 heads, 62 sectors/track, 1021 cylinders
Units = cylinders of 8060 * 512 = 4126720 bytes
Disk identifier: 0x6f20736b
This doesn't look like a partition table
Probably you selected the wrong device.
Device Boot Start End Blocks Id System
/dev/sdb1 ? 96543 238170 570754815+ 72 Unknown
Partition 1 has different physical/logical beginnings (non-Linux?):
phys=(357, 116, 40) logical=(96542, 119, 11)
Partition 1 has different physical/logical endings:
phys=(357, 32, 45) logical=(238169, 54, 51)
Partition 1 does not end on cylinder boundary.
/dev/sdb2 ? 20930 261132 968014120 65 Novell Netware 386
Partition 2 has different physical/logical beginnings (non-Linux?):
phys=(288, 115, 43) logical=(20929, 28, 47)
Partition 2 has different physical/logical endings:
phys=(367, 114, 50) logical=(261131, 30, 42)
Partition 2 does not end on cylinder boundary.
/dev/sdb3 ? 231996 472198 968014096 79 Unknown
Partition 3 has different physical/logical beginnings (non-Linux?):
phys=(366, 32, 33) logical=(231995, 28, 30)
Partition 3 has different physical/logical endings:
phys=(357, 32, 43) logical=(472197, 29, 39)
Partition 3 does not end on cylinder boundary.
/dev/sdb4 ? 358025 358032 27749+ d Unknown
Partition 4 has different physical/logical beginnings (non-Linux?):
phys=(372, 97, 50) logical=(358024, 124, 25)
Partition 4 has different physical/logical endings:
phys=(0, 10, 0) logical=(358031, 109, 33)
Partition 4 does not end on cylinder boundary.
Partition table entries are not in disk order
/dev/sda 是虚拟机默认的一个,如果插上了U盘,Disk /dev/sdb才表示U盘,在虚拟机中,U盘的排法是从sdb开始的。
当检测到了U盘之后,/dev/bus/usb里面的文件夹的个数也会有变化。
2、挂载U盘
(1)手动挂载
根据 fdisk -l获取盘符后,将U盘进行挂载。如将U盘挂载到 /mnt/myusb上,可以输入命令
mount -t vfat /dev/sdb /mnt/myusb
这样就可以将U盘挂载到 /mnt/myusb上了。对myusb里的文件操作相当于对U盘的操作。
(2)自动挂载
首先弄清楚你linux下U盘连接名的排法,我的虚拟机上的排法是从sdb开始到sdz
然后自己编写脚本,自动挂载。用udev进行挂载,在/etc/udev下有一个文件件rules.d,在这个文件夹里有一些启动规则,我们需要做的就是新建一个自动
挂载U盘的规则。我新建的规则文件名是10-myusb.rules,里面的内容是
ACTION=="add", KERNEL=="sd[b-z]", SYMLINK+="usbhd-%k", GROUP="users", NAME="%k"
ACTION=="add", KERNEL=="sd[b-z]", RUN+="/bin/mkdir -p /media/usbhd-%k"
ACTION=="add", KERNEL=="sd[b-z]", PROGRAM=="/lib/udev/vol_id -t %N", RESULT=="vfat", RUN+="/bin/mount -t vfat -o rw,utf8=true,codepage=936,noauto,flush,quiet,nodev,nosuid,noexec,noatime,dmask=000,fmask=111 /dev/%k /media/usbhd-%k", OPTIONS="last_rule"
ACTION=="add", KERNEL=="sd[[b-z]", RUN+="/bin/mount -t auto -o rw,locale=zh_CN.UTF-8,noauto,sync,dirsync,noexec,nodev,noatime /dev/%k /media/usbhd-%k", OPTIONS="last_rule"
ACTION=="remove", KERNEL=="sd[b-z]", RUN+="/bin/umount -l /media/usbhd-%k"
ACTION=="remove", KERNEL=="sd[b-z]", RUN+="/bin/rm -rf /media/usbhd-%k", OPTIONS="last_rule"
自动mount到 media上。
再修改下udev.conf,内容为
# udev.conf
# The initial syslog(3) priority: "err", "info", "debug" or its
# numerical equivalent. For runtime debugging, the daemons internal
# state can be changed with: "udevcontrol log_priority=<value>".
udev_root=”/dev/”
udev_rules=”/etc/udev/rules.d/”
udev_log=”err”
自动挂载SD卡与U盘类似,
USB Auto Mounting
I think I read somewhere that this is a problem with the new hal, that a lot of people are having problems auto mounting their USB devices in X since upgrading. A useful alternative which I used is to use Udev to auto-mount your USB devices. See the section on auto mounting USB storage devices on the Udev wiki page or copy the following configured code.
#/etc/udev/rules.d/10-usb.rules KERNEL=="sd[b-z]", NAME="%k", SYMLINK+="usbhd-%k", GROUP="users", OPTIONS="last_rule" ACTION=="add", KERNEL=="sd[b-z][0-9]", SYMLINK+="usbhd-%k", GROUP="users", NAME="%k" ACTION=="add", KERNEL=="sd[b-z][0-9]", RUN+="/bin/mkdir -p /media/usbhd-%k" ACTION=="add", KERNEL=="sd[b-z][0-9]", RUN+="/bin/ln -s /media/usbhd-%k /mnt/usbhd-%k" ACTION=="add", KERNEL=="sd[b-z][0-9]", PROGRAM=="/lib/udev/vol_id -t %N", RESULT=="vfat", RUN+="/bin/mount -t vfat -o rw,noauto,flush,quiet,nodev,nosuid,noexec,noatime,dmask=000,fmask=111 /dev/%k /media/usbhd-%k", OPTIONS="last_rule" ACTION=="add", KERNEL=="sd[b-z][0-9]", RUN+="/bin/mount -t auto -o rw,noauto,sync,dirsync,noexec,nodev,noatime /dev/%k /media/usbhd-%k", OPTIONS="last_rule" ACTION=="remove", KERNEL=="sd[b-z][0-9]", RUN+="/bin/rm -f /mnt/usbhd-%k" ACTION=="remove", KERNEL=="sd[b-z][0-9]", RUN+="/bin/umount -l /media/usbhd-%k" ACTION=="remove", KERNEL=="sd[b-z][0-9]", RUN+="/bin/rmdir /media/usbhd-%k", OPTIONS="last_rule"
You can do the same for the SD card reader:
#/etc/udev/rules.d/11-sd.rules KERNEL=="mmcblk[0-9]", NAME="%k", SYMLINK+="sd-%k", GROUP="users", OPTIONS="last_rule" ACTION=="add", KERNEL=="mmcblk[0-9]p[0-9]", SYMLINK+="sd-%k", GROUP="users", NAME="%k" ACTION=="add", KERNEL=="mmcblk[0-9]p[0-9]", RUN+="/bin/mkdir -p /media/sd-%k" ACTION=="add", KERNEL=="mmcblk[0-9]p[0-9]", RUN+="/bin/ln -s /media/sd-%k /mnt/sd-%k" ACTION=="add", KERNEL=="mmcblk[0-9]p[0-9]", PROGRAM=="/lib/udev/vol_id -t %N", RESULT=="vfat", RUN+="/bin/mount -t vfat -o rw,noauto,flush,quiet,nodev,nosuid,noexec,noatime,dmask=000,fmask=111 /dev/%k /media/sd-%k", OPTIONS="last_rule" ACTION=="add", KERNEL=="mmcblk[0-9]p[0-9]", RUN+="/bin/mount -t auto -o rw,noauto,sync,dirsync,noexec,nodev,noatime /dev/%k /media/sd-%k", OPTIONS="last_rule" ACTION=="remove", KERNEL=="mmcblk[0-9]p[0-9]", RUN+="/bin/rm -f /mnt/sd-%k" ACTION=="remove", KERNEL=="mmcblk[0-9]p[0-9]", RUN+="/bin/umount -l /media/sd-%k" ACTION=="remove", KERNEL=="mmcblk[0-9]p[0-9]", RUN+="/bin/rmdir /media/sd-%k", OPTIONS="last_rule"
转自:http://wiki.archlinux.org/index.php/HP_Pavilion_dv9510eo
但还有一种情况,就是移动硬盘只有一个分区的情况,如
[root@localhost 用户界面]# fdisk -l
Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x00012d75
Device Boot Start End Blocks Id System
/dev/sda1 * 1 25 200781 83 Linux
/dev/sda2 26 2610 20764012+ 8e Linux LVM
Disk /dev/dm-0: 20.1 GB, 20132659200 bytes
255 heads, 63 sectors/track, 2447 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x00000000
Disk /dev/dm-0 doesn't contain a valid partition table
Disk /dev/dm-1: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x30307800
Disk /dev/dm-1 doesn't contain a valid partition table
Disk /dev/sdb: 1016 MB, 1016070144 bytes
64 heads, 32 sectors/track, 969 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Disk identifier: 0xe1a8e1a8
Device Boot Start End Blocks Id System
/dev/sdb1 * 1 969 992240 b W95 FAT32
这个时候,挂载sdb已经没用了,sdb1才是有用的接口,所以规则可以写成
#挂载只有一个分区的
KERNEL=="sd[b-z]1", SYMLINK+="camera", GROUP="users"
ACTION=="add", KERNEL=="sd[b-z]1", RUN+="/bin/mkdir -p /media/usbhd"
ACTION=="add", KERNEL=="sd[b-z]1", RUN+="/bin/mount -t auto -o rw,noauto,sync,dirsync,noexec,nodev,noatime /dev/%k /media/usbhd", OPTIONS="last_rule"
#卸载只有一个分区的
ACTION=="remove", KERNEL=="sd[b-z]1", RUN+="/bin/umount -l /media/usbhd"
ACTION=="remove", KERNEL=="sd[b-z]1", RUN+="/bin/rm -rf /media/usbhd",OPTIONS="last_rule"
#挂载有多个分区的
ACTION=="add", KERNEL=="sd[b-z]", SYMLINK+="camera", GROUP="users"
ACTION=="add", KERNEL=="sd[b-z]", RUN+="/bin/mkdir -p /media/usbhd"
ACTION=="add", KERNEL=="sd[b-z]", PROGRAM=="/lib/udev/vol_id -t %N", RESULT=="vfat", RUN+="/bin/mount -t vfat -o rw,utf8=true,codepage=936,noauto,flush,quiet,nodev,nosuid,noexec,noatime,dmask=000,fmask=111 /dev/%k /media/usbhd", OPTIONS="last_rule"
ACTION=="add", KERNEL=="sd[[b-z]", RUN+="/bin/mount -t auto -o rw,locale=zh_CN.UTF-8,noauto,sync,dirsync,noexec,nodev,noatime /dev/%k /media/usbhd", OPTIONS="last_rule"
#卸载
ACTION=="remove", KERNEL=="sd[b-z]", RUN+="/bin/umount -l /media/usbhd"
ACTION=="remove", KERNEL=="sd[b-z]", RUN+="/bin/rm -rf /media/usbhd", OPTIONS="last_rule"