在Linux下往往会碰到这样的问题,磁盘的设备文件,比如/dev/sda, sdb, sdc等等在某些情况下会混乱掉,比如sda变成了sdb或者sdc变成了sdb等等,这样无形中会导致磁盘
在Linux下往往会碰到这样的问题,磁盘的设备文件,比如/dev/sda, sdb, sdc等等在某些情况下会混乱掉,比如sda变成了sdb或者sdc变成了sdb等等,这样无形中会导致磁盘设备管理的混乱,最常见的比如Linux文件 系统的启动问题。很多人在遇到这种问题的时候都去找磁盘、阵列厂家,怀疑是他们的问题,其实这种底层的磁盘(单个磁盘或者RAID阵列)和Linux下磁 盘设备文件的映射并不是磁盘、阵列厂家来决定的,而是Linux内核自身的原因。

目前Linux内核对于这种磁盘设备的映射基本上取决于三个顺序,
一是磁盘驱动程序的加载;
二是主机PCI插槽的监测;
三是磁盘本身的监测,先来的当然是a,以此类推。所以,在出现热插拔了某些设备、重启等特殊情况下,实际磁盘在Linux下映射的设备文件可能由于这种“排队”的原因而发生改变,而这种底层“偷偷的”变化有时候会让管理员犯一些低级错误。

这是Linux Kernel的限制,所以目前还没办法来正面的克服应对,但有两个“迂回战术”的办法来减少可能出现的问题,一个是采用UUID设备唯一识别的方法,另一个是采用对设备卷做Label标识的办法。

一、 UUID (globally unique identifier),唯一的身份识别,是采用SCSI Inquiry命令的Page 83信息来映射磁盘设备的。例如我们可以在Linux下查询一些磁盘设备的UUID标识代码。

引用

bash# ls -la /dev/disk/by-id
total 0
drwxr-xr-x 2 root root 280 Mar 11 12:29 .
drwxr-xr-x 5 root root 100 Mar 11 12:28 ..
lrwxrwxrwx 1 root root 9 Mar 11 12:29 edd-int13_dev80 -> ../../sda
lrwxrwxrwx 1 root root 10 Mar 11 12:29 edd-int13_dev80-part1 -> ../../sda1
lrwxrwxrwx 1 root root 10 Mar 11 12:29 edd-int13_dev80-part3 -> ../../sda3
lrwxrwxrwx 1 root root 10 Mar 11 12:29 edd-int13_dev80-part4 -> ../../sda4
lrwxrwxrwx 1 root root 10 Mar 11 12:29 edd-int13_dev80-part5 -> ../../sda5
lrwxrwxrwx 1 root root 10 Mar 11 12:29 edd-int13_dev80-part6 -> ../../sda6
lrwxrwxrwx 1 root root 9 Mar 11 12:28 scsi-3600050e03d7c67007bf400009f890000 -
> ../../sda
lrwxrwxrwx 1 root root 10 Mar 11 12:28 scsi-3600050e03d7c67007bf400009f890000-p
art1 -> ../../sda1
lrwxrwxrwx 1 root root 10 Mar 11 12:28 scsi-3600050e03d7c67007bf400009f890000-p
art3 -> ../../sda3
lrwxrwxrwx 1 root root 10 Mar 11 12:28 scsi-3600050e03d7c67007bf400009f890000-p
art4 -> ../../sda4
lrwxrwxrwx 1 root root 10 Mar 11 12:28 scsi-3600050e03d7c67007bf400009f890000-p
art5 -> ../../sda5
lrwxrwxrwx 1 root root 10 Mar 11 12:28 scsi-3600050e03d7c67007bf400009f890000-p
art6 -> ../../sda6

找到了磁盘设备唯一的UUID代码后,就可以加到/etc/grub.conf和/etc/fstab中,这样即使初始的系统盘sda变成了sdb,但Linux和文件系统的启动加载都是按照UUID来的,所以上层也不会受到影响,例如,
在 /etc/grub.conf系统启动入口中做以下更改:

引用

kernel /boot/vmlinuz-2.6.27.7 root=/dev/disk/by-id/scsi-3600050e03d7c67007bf400009f890000-part1


在 /etc/fstab文件系统启动入口中做以下更改:

引用

/dev/disk/by-id/scsi-3600050e03d7c67007bf400009f890000-part1 / ext3 1 1
/dev/disk/by-id/scsi-234892819987c8f828473829becf38289-part2 /home ext3 1 1



二、 第二种算是比较老式的解决方法,即对磁盘卷设置Label标签,同样的道理,系统启动的时候只看标签,不看底层的sda/sdb等设备号,所以也不会影响到系统、文件系统的启动。例如,
使用e2label命令对sda1和sdb1设置标签:

引用

/sbin/e2label /dev/sda1 myroot
/sbin/e2label /dev/sdb1 myhome

之后在 /etc/grub.conf 系统启动入口中做以下更改:
kernel /boot/vmlinuz-2.6.29 ro root=LABEL=myroot
在 /etc/fstab文件系统启动入口中做以下更改:

引用

LABEL=myroot / ext3 defaults 1 1
LABEL=myhome /home ext3 defaults 1 1



当然,以上两种都是为了不影响系统和文件系统的启动采用的变通方法,在实际的系统管理时还是要密切注意底层设备的变化,否则如果出现了磁盘分区误删除的事情罪过可就大了



====================================================

one disk or disk partition can be considerred a PV, like sda, or sda1, or sda2.

every PV is divided into PE. usually it is 4M byte.

one , or more than one PV form a VG.

For example, disk sda was divided 2 partition, sda1 and sda2.

sda1 was used to store boot loader, so we don't need to create lv on it.

sda2 was used to store data,so we need to create VG on it, and then create LV on VG.


df -lh

Filesystem                   1K-blocks     Used Available Use% Mounted on
/dev/mapper/VolGroup-lv_root  51606140  8690444  40294256  18% /
tmpfs                          1820768      236   1820532   1% /dev/shm
/dev/sda1                       495844    69544    400700  15% /boot
/dev/mapper/VolGroup-lv_home  50395844  1084612  46751232   3% /home
/dev/mapper/VolGroup-lv_opt  100791728 16247552  79424176  17% /opt

from above output, there were not VG and LV on disk partition /dev/sda1


the LV, like lv_home,lv_root,lv_opt, shoul be create on sda2.

 vgdisplay --verbose
    Finding all volume groups
    Finding volume group "VolGroup"
  --- Volume group ---
  VG Name               VolGroup
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               465.27 GiB
  PE Size               4.00 MiB
  Total PE              119109
  Alloc PE / Size       50300 / 196.48 GiB
  Free  PE / Size       68809 / 268.79 GiB
  VG UUID               eiUY7U-YAne-xuce-o08D-ikv7-ue9Q-pK7whN
   
  --- Logical volume ---
  LV Path                /dev/VolGroup/lv_root
  LV Name                lv_root
  VG Name                VolGroup
  LV UUID                gR5yI0-OBRg-mSew-cXhm-LHRc-4IjE-u0af9s
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2016-05-24 21:04:09 +0800
  LV Status              available
  # open                 1
  LV Size                50.00 GiB
  Current LE             12800
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/VolGroup/lv_opt
  LV Name                lv_opt
  VG Name                VolGroup
  LV UUID                FJJ5O0-N8ZV-0CBE-VbS6-bwhe-R3ft-tQ8Dem
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2016-05-24 21:04:19 +0800
  LV Status              available
  # open                 1
  LV Size                97.66 GiB
  Current LE             25000
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/VolGroup/lv_home
  LV Name                lv_home
  VG Name                VolGroup
  LV UUID                YSulS6-YfCa-3fsP-YGt7-1Xy9-65Rc-aUOk2D
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2016-05-24 21:04:38 +0800
  LV Status              available
  # open                 1
  LV Size                48.83 GiB
  Current LE             12500
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2
   
  --- Physical volumes ---
  PV Name               /dev/sda2     
  PV UUID               t05rhs-cX12-y03y-fZWk-BDoo-MEvc-BBAhLv
  PV Status             allocatable
  Total PE / Free PE    119109 / 68809

fdisk -l

Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0xbb9abb9a

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64       60802   487873536   8e  Linux LVM

 from the above ouput, and considerring the previous output, sda1 was not used to lvm, sda2 was used to LVM, that means sda2 as a PV form a VG, and create some LV on VG.


Basically, the disk sda could be a local disk, or a local virtual disk which consist of RAID.


Furtherly, linux use device mapper(DM) .

# iostat -x
Linux 2.6.32-431.el6.x86_64 (15090279host)     06/19/2016     _x86_64_    (4 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           1.85    0.00    0.43    0.27    0.00   97.45

Device:         rrqm/s   wrqm/s     r/s     w/s   rsec/s   wsec/s avgrq-sz avgqu-sz   await  svctm  %util
sda               0.44    29.03    1.61    1.58    51.93   238.07    91.03     0.10   32.52   3.77   1.20
dm-0            0.00     0.00    1.89   22.98    49.19   183.65     9.37     7.66  308.28   0.24   0.59
dm-1              0.00     0.00    0.03    0.00     0.23     0.00     7.95     0.00    3.36   0.88   0.00
dm-2              0.00     0.00    0.09    6.44     1.47    52.25     8.23     0.51   78.08   1.00   0.65

[root@15090279host linux-2.6.32]# cd /dev/mapper/
[root@15090279host mapper]# ls -l
total 0
crw-rw---- 1 root root 10, 58 Jun 19 00:10 control
lrwxrwxrwx 1 root root      7 Jun 19 00:10 VolGroup-lv_home -> ../dm-2
lrwxrwxrwx 1 root root      7 Jun 19 00:10 VolGroup-lv_opt -> ../dm-1
lrwxrwxrwx 1 root root      7 Jun 19 00:10 VolGroup-lv_root -> ../dm-0


 ls -l
total 0
lrwxrwxrwx 1 root root 7 Jun 19 00:10 lv_home -> ../dm-2
lrwxrwxrwx 1 root root 7 Jun 19 00:10 lv_opt -> ../dm-1
lrwxrwxrwx 1 root root 7 Jun 19 00:10 lv_root -> ../dm-0
[root@15090279host VolGroup]# pwd
/dev/VolGroup

best practice:

VG shoud reserve a number of space for space extend(emergency, or normal).