在很多情况下,我们将U盘插入Linux系统之后,通过fdisk 进行磁盘的查看发现,并不是每次显示的设备名都一致,比如每次都是sdb,很有可能下次是sdc,sdd,这个跟你在插入U盘之前,系统是否插入存储有关,平时这个问题可能不会有什么影响,但是在ISCSI共享存储中,我们需要将外接存储一直挂载,并且经常进行操作,比如我们挂接了192.168.3.1上面的存储到本地,磁盘显示为:/dev/sdb,我们进行分区,格式化,将/dev/sdb1 挂载到 /test1目录下,我们需要经常对/test1进行读写操作,但是某次开机发现test1目录并没有挂载/dev/sdb1,为何?这就是共享存储在本地显示的设备名已经不再是sdb了,所以为了解决这样的情况,我们使用 UDEV进行设备管理,关于何为UDEV:百度知道如是说:http://baike.baidu.com/view/755910.htm
还是从ISCSI 的Target端跟Initiator端说起
Target端:ParaSec01 192.168.1.90
1.创建分区设备
[root@ParaSec01 ~]# hostname ParaSec01 [root@ParaSec01 ~]# fdisk /dev/sda -l Disk /dev/sda (Sun disk label): 255 heads, 63 sectors, 3649 cylinders Units = cylinders of 16065 * 512 bytes Device Flag Start End Blocks Id System [root@ParaSec01 ~]# cat | fdisk /dev/sda << EOF > n > p > 1 > > +2GB > w > EOF Command (m for help): Partition number (1-8): Partition number (1-8): First cylinder (0-3649): Last cylinder or +size or +sizeM or +sizeK (0-3649, default 3649): Command (m for help): The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks. [root@ParaSec01 ~]# fdisk /dev/sda -l Disk /dev/sda (Sun disk label): 255 heads, 63 sectors, 3649 cylinders Units = cylinders of 16065 * 512 bytes Device Flag Start End Blocks Id System /dev/sda1 0 243 1951897+ 83 Linux native
2.编辑配置文件/etc/tgt/targets.conf,添加
<target iqn.2013-05.com.clovem:target1> backing-store /dev/sda1 </target>
3.启动tgtd服务
[root@ParaSec01 ~]# service tgtd start Starting SCSI target daemon: [ OK ] [root@ParaSec01 ~]# tgt-admin --show Target 1: iqn.2013-05.com.clovem:target1 System information: Driver: iscsi State: ready I_T nexus information: LUN information: LUN: 0 Type: controller SCSI ID: IET 00010000 SCSI SN: beaf10 Size: 0 MB, Block size: 1 Online: Yes Removable media: No Prevent removal: No Readonly: No Backing store type: null Backing store path: None Backing store flags: LUN: 1 Type: disk SCSI ID: IET 00010001 SCSI SN: beaf11 Size: 2007 MB, Block size: 512 Online: Yes Removable media: No Prevent removal: No Readonly: No Backing store type: rdwr Backing store path: /dev/sda1 Backing store flags: Account information: ACL information: ALL
Initiator:hyperStor 192.168.3.30
[root@hyperStor ~]# hostname hyperStor [root@hyperStor ~]# ls /dev/sd* -l brw-rw----. 1 root disk 8, 0 Apr 25 23:46 /dev/sda brw-rw----. 1 root disk 8, 1 Apr 25 23:46 /dev/sda1 brw-rw----. 1 root disk 8, 2 Apr 25 23:46 /dev/sda2 brw-rw----. 1 root disk 8, 3 Apr 25 23:46 /dev/sda3 brw-rw----. 1 root disk 8, 16 May 10 10:55 /dev/sdb brw-rw----. 1 root disk 8, 17 Apr 25 23:46 /dev/sdb1 brw-rw----. 1 root disk 8, 18 Apr 25 23:46 /dev/sdb2 brw-rw----. 1 root disk 8, 21 Apr 25 23:46 /dev/sdb5 brw-rw----. 1 root disk 8, 22 Apr 25 23:46 /dev/sdb6 brw-rw----. 1 root disk 8, 23 Apr 25 23:46 /dev/sdb7 brw-rw----. 1 root disk 8, 24 May 13 09:37 /dev/sdb8 [root@hyperStor ~]# iscsiadm -m discovery -t sendtargets -p 192.168.1.90 Starting iscsid: [ OK ] 192.168.1.90:3260,1 iqn.2013-05.com.clovem:target1 [root@hyperStor ~]# iscsiadm -m node -T iqn.2013-05.com.clovem:target1 -l Logging in to [iface: default, target: iqn.2013-05.com.clovem:target1, portal: 192.168.1.90,3260] (multiple) Login to [iface: default, target: iqn.2013-05.com.clovem:target1, portal: 192.168.1.90,3260] successful. [root@hyperStor ~]# ls /dev/sd* -l brw-rw----. 1 root disk 8, 0 Apr 25 23:46 /dev/sda brw-rw----. 1 root disk 8, 1 Apr 25 23:46 /dev/sda1 brw-rw----. 1 root disk 8, 2 Apr 25 23:46 /dev/sda2 brw-rw----. 1 root disk 8, 3 Apr 25 23:46 /dev/sda3 brw-rw----. 1 root disk 8, 16 May 10 10:55 /dev/sdb brw-rw----. 1 root disk 8, 17 Apr 25 23:46 /dev/sdb1 brw-rw----. 1 root disk 8, 18 Apr 25 23:46 /dev/sdb2 brw-rw----. 1 root disk 8, 21 Apr 25 23:46 /dev/sdb5 brw-rw----. 1 root disk 8, 22 Apr 25 23:46 /dev/sdb6 brw-rw----. 1 root disk 8, 23 Apr 25 23:46 /dev/sdb7 brw-rw----. 1 root disk 8, 24 May 13 09:37 /dev/sdb8 brw-rw----. 1 root disk 8, 32 May 13 10:01 /dev/sdc
[root@hyperStor ~]# udevadm monitor monitor will print the received events for: UDEV - the event which udev sends out after rule processing KERNEL - the kernel uevent KERNEL[1368410441.394299] add /module/iscsi_tcp (module) KERNEL[1368410441.394360] add /devices/virtual/iscsi_transport/tcp (iscsi_transport) KERNEL[1368410441.399931] add /module/ib_addr (module) UDEV [1368410441.403255] add /module/iscsi_tcp (module) UDEV [1368410441.403488] add /devices/virtual/iscsi_transport/tcp (iscsi_transport) UDEV [1368410441.404113] add /module/ib_addr (module) KERNEL[1368410441.438772] add /module/ib_core (module) UDEV [1368410441.439227] add /module/ib_core (module) KERNEL[1368410441.439564] add /class/infiniband (class) UDEV [1368410441.440168] add /class/infiniband (class) KERNEL[1368410441.455582] add /module/ib_mad (module) UDEV [1368410441.456116] add /module/ib_mad (module) KERNEL[1368410441.471202] add /module/ib_sa (module) UDEV [1368410441.471775] add /module/ib_sa (module) KERNEL[1368410441.480950] add /module/iw_cm (module) UDEV [1368410441.481503] add /module/iw_cm (module) KERNEL[1368410441.500212] add /module/ib_cm (module) KERNEL[1368410441.500412] add /class/infiniband_cm (class) UDEV [1368410441.500802] add /module/ib_cm (module) UDEV [1368410441.500877] add /class/infiniband_cm (class) KERNEL[1368410441.526953] add /module/rdma_cm (module) UDEV [1368410441.527567] add /module/rdma_cm (module) KERNEL[1368410441.546686] add /module/ib_iser (module) KERNEL[1368410441.546958] add /devices/virtual/iscsi_transport/iser (iscsi_transport) UDEV [1368410441.547237] add /module/ib_iser (module) UDEV [1368410441.547530] add /devices/virtual/iscsi_transport/iser (iscsi_transport) KERNEL[1368410441.572754] add /module/cxgb3i (module) KERNEL[1368410441.572918] add /devices/virtual/iscsi_transport/cxgb3i (iscsi_transport) UDEV [1368410441.573341] add /module/cxgb3i (module) UDEV [1368410441.573376] add /devices/virtual/iscsi_transport/cxgb3i (iscsi_transport) KERNEL[1368410441.598469] add /module/cxgb4i (module) KERNEL[1368410441.598592] add /devices/virtual/iscsi_transport/cxgb4i (iscsi_transport) UDEV [1368410441.598984] add /module/cxgb4i (module) UDEV [1368410441.599109] add /devices/virtual/iscsi_transport/cxgb4i (iscsi_transport) KERNEL[1368410441.614902] add /module/cnic (module) UDEV [1368410441.615642] add /module/cnic (module) KERNEL[1368410441.616337] add /class/uio (class) UDEV [1368410441.616888] add /class/uio (class) KERNEL[1368410441.622577] add /devices/pci0000:00/0000:00:04.0/0000:04:00.0/uio/uio0 (uio) KERNEL[1368410441.625568] add /devices/pci0000:00/0000:00:04.0/0000:04:00.1/uio/uio1 (uio) KERNEL[1368410441.628628] add /devices/pci0000:00/0000:00:06.0/0000:05:00.0/uio/uio2 (uio) KERNEL[1368410441.631882] add /devices/pci0000:00/0000:00:06.0/0000:05:00.1/uio/uio3 (uio) KERNEL[1368410441.651946] add /module/bnx2i (module) KERNEL[1368410441.652135] add /devices/virtual/iscsi_transport/bnx2i (iscsi_transport) KERNEL[1368410441.652626] add /devices/pci0000:00/0000:00:06.0/0000:05:00.1/host9 (scsi) KERNEL[1368410441.652756] add /devices/pci0000:00/0000:00:06.0/0000:05:00.1/host9/scsi_host/host9 (scsi_host) KERNEL[1368410441.652935] add /devices/pci0000:00/0000:00:06.0/0000:05:00.1/host9/iscsi_host/host9 (iscsi_host) UDEV [1368410441.653566] add /module/bnx2i (module) UDEV [1368410441.654118] add /devices/virtual/iscsi_transport/bnx2i (iscsi_transport) KERNEL[1368410441.654909] add /devices/pci0000:00/0000:00:06.0/0000:05:00.0/host10 (scsi) UDEV [1368410441.655058] add /devices/pci0000:00/0000:00:06.0/0000:05:00.1/host9 (scsi) KERNEL[1368410441.655165] add /devices/pci0000:00/0000:00:06.0/0000:05:00.0/host10/scsi_host/host10 (scsi_host) UDEV [1368410441.655857] add /devices/pci0000:00/0000:00:06.0/0000:05:00.0/host10 (scsi) UDEV [1368410441.657690] add /devices/pci0000:00/0000:00:06.0/0000:05:00.0/host10/scsi_host/host10 (scsi_host) KERNEL[1368410441.664302] add /devices/pci0000:00/0000:00:06.0/0000:05:00.0/host10/iscsi_host/host10 (iscsi_host) UDEV [1368410441.664750] add /devices/pci0000:00/0000:00:06.0/0000:05:00.1/uio/uio3 (uio) UDEV [1368410441.664962] add /devices/pci0000:00/0000:00:06.0/0000:05:00.0/uio/uio2 (uio) UDEV [1368410441.665354] add /devices/pci0000:00/0000:00:06.0/0000:05:00.0/host10/iscsi_host/host10 (iscsi_host) UDEV [1368410441.665613] add /devices/pci0000:00/0000:00:06.0/0000:05:00.1/host9/scsi_host/host9 (scsi_host) UDEV [1368410441.665895] add /devices/pci0000:00/0000:00:06.0/0000:05:00.1/host9/iscsi_host/host9 (iscsi_host) UDEV [1368410441.666201] add /devices/pci0000:00/0000:00:04.0/0000:04:00.0/uio/uio0 (uio) UDEV [1368410441.666256] add /devices/pci0000:00/0000:00:04.0/0000:04:00.1/uio/uio1 (uio) KERNEL[1368410441.666721] add /devices/pci0000:00/0000:00:04.0/0000:04:00.1/host11 (scsi) KERNEL[1368410441.666991] add /devices/pci0000:00/0000:00:04.0/0000:04:00.1/host11/scsi_host/host11 (scsi_host) KERNEL[1368410441.667305] add /devices/pci0000:00/0000:00:04.0/0000:04:00.1/host11/iscsi_host/host11 (iscsi_host) UDEV [1368410441.667432] add /devices/pci0000:00/0000:00:04.0/0000:04:00.1/host11 (scsi) UDEV [1368410441.668033] add /devices/pci0000:00/0000:00:04.0/0000:04:00.1/host11/iscsi_host/host11 (iscsi_host) UDEV [1368410441.668233] add /devices/pci0000:00/0000:00:04.0/0000:04:00.1/host11/scsi_host/host11 (scsi_host) KERNEL[1368410441.668691] add /devices/pci0000:00/0000:00:04.0/0000:04:00.0/host12 (scsi) KERNEL[1368410441.668811] add /devices/pci0000:00/0000:00:04.0/0000:04:00.0/host12/scsi_host/host12 (scsi_host) KERNEL[1368410441.668996] add /devices/pci0000:00/0000:00:04.0/0000:04:00.0/host12/iscsi_host/host12 (iscsi_host) UDEV [1368410441.669223] add /devices/pci0000:00/0000:00:04.0/0000:04:00.0/host12 (scsi) UDEV [1368410441.670053] add /devices/pci0000:00/0000:00:04.0/0000:04:00.0/host12/scsi_host/host12 (scsi_host) UDEV [1368410441.670093] add /devices/pci0000:00/0000:00:04.0/0000:04:00.0/host12/iscsi_host/host12 (iscsi_host) KERNEL[1368410483.991398] add /devices/platform/host13 (scsi) KERNEL[1368410483.991455] add /devices/platform/host13/scsi_host/host13 (scsi_host) KERNEL[1368410483.992838] add /devices/platform/host13/iscsi_host/host13 (iscsi_host) KERNEL[1368410483.992890] add /devices/platform/host13/session2/iscsi_session/session2 (iscsi_session) UDEV [1368410483.994795] add /devices/platform/host13 (scsi) UDEV [1368410483.997767] add /devices/platform/host13/iscsi_host/host13 (iscsi_host) UDEV [1368410483.997797] add /devices/platform/host13/scsi_host/host13 (scsi_host) UDEV [1368410483.998026] add /devices/platform/host13/session2/iscsi_session/session2 (iscsi_session) KERNEL[1368410484.018634] add /devices/platform/host13/session2/connection2:0/iscsi_connection/connection2:0 (iscsi_connection) UDEV [1368410484.020514] add /devices/platform/host13/session2/connection2:0/iscsi_connection/connection2:0 (iscsi_connection) KERNEL[1368410484.280337] add /devices/platform/host13/session2/target13:0:0 (scsi) KERNEL[1368410484.280395] add /devices/platform/host13/session2/target13:0:0/13:0:0:0 (scsi) KERNEL[1368410484.280430] add /devices/platform/host13/session2/target13:0:0/13:0:0:0/scsi_device/13:0:0:0 (scsi_device) UDEV [1368410484.280683] add /devices/platform/host13/session2/target13:0:0 (scsi) KERNEL[1368410484.281316] add /devices/platform/host13/session2/target13:0:0/13:0:0:0/scsi_generic/sg3 (scsi_generic) KERNEL[1368410484.281371] add /devices/platform/host13/session2/target13:0:0/13:0:0:0/bsg/13:0:0:0 (bsg) KERNEL[1368410484.287272] add /devices/platform/host13/session2/target13:0:0/13:0:0:1 (scsi) KERNEL[1368410484.287323] add /devices/platform/host13/session2/target13:0:0/13:0:0:1/scsi_disk/13:0:0:1 (scsi_disk) KERNEL[1368410484.287353] add /devices/platform/host13/session2/target13:0:0/13:0:0:1/scsi_device/13:0:0:1 (scsi_device) KERNEL[1368410484.287547] add /devices/platform/host13/session2/target13:0:0/13:0:0:1/scsi_generic/sg4 (scsi_generic) KERNEL[1368410484.287662] add /devices/platform/host13/session2/target13:0:0/13:0:0:1/bsg/13:0:0:1 (bsg) UDEV [1368410484.288105] add /devices/platform/host13/session2/target13:0:0/13:0:0:1 (scsi) UDEV [1368410484.288619] add /devices/platform/host13/session2/target13:0:0/13:0:0:1/scsi_disk/13:0:0:1 (scsi_disk) KERNEL[1368410484.305162] add /devices/virtual/bdi/8:32 (bdi) UDEV [1368410484.305978] add /devices/virtual/bdi/8:32 (bdi) UDEV [1368410484.306235] add /devices/platform/host13/session2/target13:0:0/13:0:0:0 (scsi) UDEV [1368410484.307427] add /devices/platform/host13/session2/target13:0:0/13:0:0:0/scsi_device/13:0:0:0 (scsi_device) UDEV [1368410484.309146] add /devices/platform/host13/session2/target13:0:0/13:0:0:1/bsg/13:0:0:1 (bsg) UDEV [1368410484.309205] add /devices/platform/host13/session2/target13:0:0/13:0:0:1/scsi_generic/sg4 (scsi_generic) UDEV [1368410484.317218] add /devices/platform/host13/session2/target13:0:0/13:0:0:0/bsg/13:0:0:0 (bsg) UDEV [1368410484.318017] add /devices/platform/host13/session2/target13:0:0/13:0:0:1/scsi_device/13:0:0:1 (scsi_device) UDEV [1368410484.318073] add /devices/platform/host13/session2/target13:0:0/13:0:0:0/scsi_generic/sg3 (scsi_generic) KERNEL[1368410484.335570] add /devices/platform/host13/session2/target13:0:0/13:0:0:1/block/sdc (block) UDEV [1368410484.685488] add /devices/platform/host13/session2/target13:0:0/13:0:0:1/block/sdc (block)
可以发现新设备名为sdc
使用udevadm info -a -p /sys/block/sdc 查看设备详细信息
[root@hyperStor ~]# udevadm info -a -p /sys/block/sdc/ Udevadm info starts with the device specified by the devpath and then walks up the chain of parent devices. It prints for every device found, all possible attributes in the udev rules key format. A rule to match, can be composed by the attributes of the device and the attributes from one single parent device. looking at device '/devices/platform/host13/session2/target13:0:0/13:0:0:1/block/sdc': KERNEL=="sdc" SUBSYSTEM=="block" DRIVER=="" ATTR{range}=="16" ATTR{ext_range}=="256" ATTR{removable}=="0" ATTR{ro}=="0" ATTR{size}=="3919797" ATTR{alignment_offset}=="0" ATTR{discard_alignment}=="0" ATTR{capability}=="52" ATTR{stat}==" 169 1177 1346 541 0 0 0 0 0 541 541" ATTR{inflight}==" 0 0" looking at parent device '/devices/platform/host13/session2/target13:0:0/13:0:0:1': KERNELS=="13:0:0:1" SUBSYSTEMS=="scsi" DRIVERS=="sd" ATTRS{device_blocked}=="0" ATTRS{type}=="0" ATTRS{scsi_level}=="6" ATTRS{vendor}=="IET " ATTRS{model}=="VIRTUAL-DISK " ATTRS{rev}=="0001" ATTRS{state}=="running" ATTRS{timeout}=="30" ATTRS{iocounterbits}=="32" ATTRS{iorequest_cnt}=="0xc8" ATTRS{iodone_cnt}=="0xc8" ATTRS{ioerr_cnt}=="0x1" ATTRS{modalias}=="scsi:t-0x00" ATTRS{evt_media_change}=="0" ATTRS{dh_state}=="detached" ATTRS{queue_depth}=="32" ATTRS{queue_ramp_up_period}=="120000" ATTRS{queue_type}=="none" looking at parent device '/devices/platform/host13/session2/target13:0:0': KERNELS=="target13:0:0" SUBSYSTEMS=="scsi" DRIVERS=="" looking at parent device '/devices/platform/host13/session2': KERNELS=="session2" SUBSYSTEMS=="" DRIVERS=="" looking at parent device '/devices/platform/host13': KERNELS=="host13" SUBSYSTEMS=="scsi" DRIVERS=="" looking at parent device '/devices/platform': KERNELS=="platform" SUBSYSTEMS=="" DRIVERS==""
手动编辑新设备规则
[root@hyperStor ~]# ls /etc/udev/rules.d/ -l total 56 -rw-r--r--. 1 root root 6858 Jul 19 2011 40-hplip.rules -rw-r--r--. 1 root root 706 Jul 19 2011 56-hpmud_support.rules -rw-r--r--. 1 root root 1652 Nov 12 2010 60-fprint-autosuspend.rules -rw-r--r--. 1 root root 1060 Nov 12 2010 60-pcmcia.rules -rw-r--r--. 1 root root 316 Dec 8 2011 60-raw.rules -rw-r--r--. 1 root root 911 Mar 12 11:16 70-persistent-net.rules -rw-r--r--. 1 root root 40 Dec 8 2011 80-kvm.rules -rw-r--r--. 1 root root 320 May 15 2009 90-alsa.rules -rw-r--r--. 1 root root 83 Jul 20 2011 90-hal.rules -rw-r--r--. 1 root root 53 Dec 8 2011 91-drm-modeset.rules -rw-r--r--. 1 root root 2486 Nov 11 2010 97-bluetooth-serial.rules -rw-r--r--. 1 root root 292 Dec 8 2011 98-kexec.rules -rw-r--r--. 1 root root 54 Dec 8 2011 99-fuse.rules
创建新规则叫做 90-iscsi.rules,切记不可以100-xx.rules 命名,切记,好像最多识别到99-xx.rules
[root@hyperStor rules.d]# ls /dev/iscsi/ ls: cannot access /dev/iscsi/: No such file or directory [root@hyperStor rules.d]# cat 90-iscsi.rules ACTION=="add", SUBSYSTEM=="block",DRIVERS=="sd", ENV{ID_MODEL}=="VIRTUAL-DISK",SYMLINK+="iscsi/NETDISK%n", MODE="0644" [root@hyperStor rules.d]# service iscsi restart Stopping iscsi: [ OK ] Starting iscsi: [ OK ] [root@hyperStor rules.d]# ls /dev/iscsi/ NETDISK
ACTION: 动作为添加符号连接
注意符号:
= 赋值
对/dev/sdc 进行分区,查看UDEV名的变化‘
[root@hyperStor rules.d]# fdisk /dev/sdc -l Disk /dev/sdc: 2006 MB, 2006936064 bytes 62 heads, 62 sectors/track, 1019 cylinders Units = cylinders of 3844 * 512 = 1968128 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x4606abe9 Device Boot Start End Blocks Id System /dev/sdc1 1 509 978267 83 Linux /dev/sdc2 510 1018 978298 83 Linux [root@hyperStor rules.d]# ls /dev/iscsi/ NETDISK1 NETDISK2 [root@hyperStor rules.d]# service iscsi restart Stopping iscsi: [ OK ] Starting iscsi: [ OK ] [root@hyperStor rules.d]# ls /dev/iscsi/ NETDISK NETDISK1 NETDISK2
[root@hyperStor rules.d]# ls /dev/iscsi/* -l lrwxrwxrwx. 1 root root 6 May 13 11:12 /dev/iscsi/NETDISK -> ../sdc lrwxrwxrwx. 1 root root 7 May 13 11:12 /dev/iscsi/NETDISK1 -> ../sdc1 lrwxrwxrwx. 1 root root 7 May 13 11:12 /dev/iscsi/NETDISK2 -> ../sdc2