今天的实验是raid5 + lvm,环境是centos6.0,VMware® Workstation 8.0

新添加3块5G的硬盘,sdbb,sdc,sdd组成raid5.

先格式化硬盘

 
  
  1. [root@shanker ~]# fdisk /dev/sdb 
  2. Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel 
  3. Building a new DOS disklabel with disk identifier 0xa3349e6d. 
  4. Changes will remain in memory only, until you decide to write them. 
  5. After that, of course, the previous content won't be recoverable. 
  6.  
  7. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) 
  8.  
  9. WARNING: DOS-compatible mode is deprecated. It's strongly recommended to 
  10.          switch off the mode (command 'c') and change display units to 
  11.          sectors (command 'u'). 
  12.  
  13. Command (m for help): 
  14. Command action 
  15.    e   extended 
  16.    p   primary partition (1-4) 
  17. p 
  18. Partition number (1-4): 1 
  19. First cylinder (1-652, default 1): 1 
  20. Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652): //选择默认
  21. Using default value 652 
  22.  
  23. Command (m for help): 
  24. Selected partition 1 
  25. Hex code (type L to list codes): 
  26.  
  27.  0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris 
  28.  1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT- 
  29.  2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT- 
  30.  3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT- 
  31.  4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx 
  32.  5  Extended        42  SFS             86  NTFS volume set da  Non-FS data 
  33.  6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / . 
  34.  7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility 
  35.  8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt 
  36.  9  AIX bootable    50  OnTrack DM      93  Amoeba          e1  DOS access 
  37.  a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O 
  38.  b  W95 FAT32       52  CP/M            9f  BSD/OS          e4  SpeedStor 
  39.  c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs 
  40.  e  W95 FAT16 (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  GPT 
  41.  f  W95 Ext'd (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/ 
  42. 10  OPUS            56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b 
  43. 11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor 
  44. 12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor 
  45. 14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary 
  46. 16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS 
  47. 17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
  48. 18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto 
  49. 1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep 
  50. 1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT 
  51. 1e  Hidden W95 FAT1 
  52. Hex code (type L to list codes): fd 
  53. Changed system type of partition 1 to fd (Linux raid autodetect) 
  54.  
  55. Command (m for help): 
  56. The partition table has been altered! 
  57.  
  58. Calling ioctl() to re-read partition table. 
  59. Syncing disks. 

其余两块硬盘的操作与此类似

操作完成后创建raid5

 

 
  
  1. [root@shanker ~]# mdadm --create /dev/md0 --level=5 --raid-device=3 /dev/sd[bcd]1 
  2. mdadm: Defaulting to version 1.2 metadata 
  3. mdadm: array /dev/md0 started. 

然后cat /proc/mdstat查看数据同步状态

 
  
  1. [root@shanker ~]# cat /proc/mdstat 
  2. Personalities : [raid6] [raid5] [raid4] 
  3. md0 : active raid5 sdd1[3] sdc1[1] sdb1[0] 
  4.       10471424 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/2] [UU_] 
  5.       [======>..............]  recovery = 33.5% (1754752/5235712) finish=1.4min speed=38698K/sec 
  6.  

当recover这行数据到到100%的时候就代表同步结束

可以用mdadm --detail /dev/md0来查看刚才新建的设备状态

 
  
  1. [root@shanker ~]# mdadm --detail /dev/md0 
  2. /dev/md0: 
  3.         Version : 1.2 
  4.   Creation Time : Fri Mar 23 05:18:44 2012 
  5.      Raid Level : raid5 
  6.      Array Size : 10471424 (9.99 GiB 10.72 GB) 
  7.   Used Dev Size : 5235712 (4.99 GiB 5.36 GB) 
  8.    Raid Devices : 3 
  9.   Total Devices : 3 
  10.     Persistence : Superblock is persistent 
  11.  
  12.     Update Time : Fri Mar 23 05:20:54 2012 
  13.           State : clean 
  14.  Active Devices : 3 
  15. Working Devices : 3 
  16.  Failed Devices : 0 
  17.   Spare Devices : 0 
  18.  
  19.          Layout : left-symmetric 
  20.      Chunk Size : 512K 
  21.  
  22.            Name : shanker:0  (local to host shanker) 
  23.            UUID : deb85091:1f514788:eafc2e11:99b0a7b2 
  24.          Events : 18 
  25.  
  26.     Number   Major   Minor   RaidDevice State 
  27.        0       8       17        0      active sync   /dev/sdb1 
  28.        1       8       33        1      active sync   /dev/sdc1 
  29.        3       8       49        2      active sync   /dev/sdd1 

然后配置raid的配置文件

 
  
  1. [root@shanker ~]# echo device /dev/sdb1 /dev/sdc1 /dev/sdd1 > /etc/mdadm.conf  
  2. [root@shanker ~]# mdadm --detail --scan >> /etc/mdadm.conf 

或者mdadm -D -s >> /etc/mdadm.conf

mdadm -S /dev/md0 //停止raid5

mdadm -As /dev/md0 //启动

然后我们来做LVM实验

 
  
  1. [root@shanker ~]# pvcreate  /dev/md0 
  2.   Physical volume "/dev/md0" successfully created 
  3. [root@shanker ~]# vgcreate vg /dev/md0 
  4.   Volume group "vg" successfully created 
  5. [root@shanker ~]# pvscan 
  6.   PV /dev/md0   VG vg   lvm2 [9.98 GiB / 9.98 GiB free] 
  7.   Total: 1 [9.98 GiB] / in use: 1 [9.98 GiB] / in no VG: 0 [0   ] 
  8. [root@shanker ~]# vgscan 
  9.   Reading all physical volumes.  This may take a while... 
  10.   Found volume group "vg" using metadata type lvm2 
  11. [root@shanker ~]# pvdisplay 
  12.   --- Physical volume --- 
  13.   PV Name               /dev/md0 
  14.   VG Name               vg 
  15.   PV Size               9.99 GiB / not usable 2.00 MiB 
  16.   Allocatable           yes 
  17.   PE Size               4.00 MiB 
  18.   Total PE              2556 
  19.   Free PE               2556 
  20.   Allocated PE          0 
  21.   PV UUID               qWZXxe-DJ7H-K8xN-vPY2-uT7x-vt2M-yPNvhh 
  22.  
  23. [root@shanker ~]# vgdisplay 
  24.   --- Volume group --- 
  25.   VG Name               vg 
  26.   System ID 
  27.   Format                lvm2 
  28.   Metadata Areas        1 
  29.   Metadata Sequence No  1 
  30.   VG Access             read/write 
  31.   VG Status             resizable 
  32.   MAX LV                0 
  33.   Cur LV                0 
  34.   Open LV               0 
  35.   Max PV                0 
  36.   Cur PV                1 
  37.   Act PV                1 
  38.   VG Size               9.98 GiB 
  39.   PE Size               4.00 MiB 
  40.   Total PE              2556 
  41.   Alloc PE / Size       0 / 0 
  42.   Free  PE / Size       2556 / 9.98 GiB 
  43.   VG UUID               DS0q5R-H00g-A5Au-6mHp-uNM5-5MkS-a1tDmq 

然后创建lvm

 
  
  1. [root@shanker ~]# lvcreate -L 1g -n lvm1 vg 
  2.   Logical volume "lvm1" created 
  3. [root@shanker ~]# lvcreate -L 500m -n lvm2 vg 
  4.   Logical volume "lvm2" created 
  5. [root@shanker ~]# lvscan 
  6.   ACTIVE            '/dev/vg/lvm1' [1.00 GiB] inherit 
  7.   ACTIVE            '/dev/vg/lvm2' [500.00 MiB] inherit 
  8. [root@shanker ~]# lvdisplay 
  9.   --- Logical volume --- 
  10.   LV Name                /dev/vg/lvm1 
  11.   VG Name                vg 
  12.   LV UUID                p1MvrS-Dxse-vVYb-uO6O-hhY5-tvKk-ONEu1Y 
  13.   LV Write Access        read/write 
  14.   LV Status              available 
  15.   # open                 0 
  16.   LV Size                1.00 GiB 
  17.   Current LE             256 
  18.   Segments               1 
  19.   Allocation             inherit 
  20.   Read ahead sectors     auto 
  21.   - currently set to     4096 
  22.   Block device           253:0 
  23.  
  24.   --- Logical volume --- 
  25.   LV Name                /dev/vg/lvm2 
  26.   VG Name                vg 
  27.   LV UUID                d0jopc-JGbI-Colb-sa0b-aUxf-zpqg-X6vCrf 
  28.   LV Write Access        read/write 
  29.   LV Status              available 
  30.   # open                 0 
  31.   LV Size                500.00 MiB 
  32.   Current LE             125 
  33.   Segments               1 
  34.   Allocation             inherit 
  35.   Read ahead sectors     auto 
  36.   - currently set to     4096 
  37.   Block device           253:1 
  38.  
  39. [root@shanker ~]# mkfs.ext3 /dev/vg 
  40. vg/          vga_arbiter 
  41. [root@shanker ~]# mkfs.ext3 /dev/vg/lvm* 
  42. mke2fs 1.41.12 (17-May-2010) 
  43. mkfs.ext3: invalid blocks count - /dev/vg/lvm2 
  44. [root@shanker ~]# mkfs.ext3 /dev/vg/lvm1 
  45. mke2fs 1.41.12 (17-May-2010) 
  46. Filesystem label
  47. OS type: Linux 
  48. Block size=4096 (log=2
  49. Fragment size=4096 (log=2
  50. Stride=128 blocks, Stripe width=256 blocks 
  51. 65536 inodes, 262144 blocks 
  52. 13107 blocks (5.00%) reserved for the super user 
  53. First data block=0 
  54. Maximum filesystem blocks=268435456 
  55. 8 block groups 
  56. 32768 blocks per group, 32768 fragments per group 
  57. 8192 inodes per group 
  58. Superblock backups stored on blocks: 
  59.         32768, 98304, 163840, 229376 
  60.  
  61. Writing inode tables: done 
  62. Creating journal (8192 blocks): done 
  63. Writing superblocks and filesystem accounting information: done 
  64.  
  65. This filesystem will be automatically checked every 29 mounts or 
  66. 180 days, whichever comes first.  Use tune2fs -c or -i to override. 
  67. [root@shanker ~]# mkfs.ext3 /dev/vg/lvm2 
  68. mke2fs 1.41.12 (17-May-2010) 
  69. Filesystem label
  70. OS type: Linux 
  71. Block size=1024 (log=0
  72. Fragment size=1024 (log=0
  73. Stride=512 blocks, Stripe width=1024 blocks 
  74. 128016 inodes, 512000 blocks 
  75. 25600 blocks (5.00%) reserved for the super user 
  76. First data block=1 
  77. Maximum filesystem blocks=67633152 
  78. 63 block groups 
  79. 8192 blocks per group, 8192 fragments per group 
  80. 2032 inodes per group 
  81. Superblock backups stored on blocks: 
  82.         8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409 
  83.  
  84. Writing inode tables: done 
  85. Creating journal (8192 blocks): done 
  86. Writing superblocks and filesystem accounting information: done 
  87.  
  88. This filesystem will be automatically checked every 30 mounts or 
  89. 180 days, whichever comes first.  Use tune2fs -c or -i to override. 
  90. [root@shanker ~]# 

mkdir /mnt/test{1..2}

mount /dev/vg/lvm1 /mnt/test1

mount /dev/vg/lvm2 /mnt/test2

然后vi /etc/fstab 将挂载点写入fstab,实现开机自动挂载

/dev/vg/lvm1/ /mnt/test1 ext3 defaults 0 0
/dev/vg/lvm2/ /mnt/test2 ext3 defaults 0 0

然后实验raid5设备有故障

 
  
  1. [root@shanker tmp]# mdadm /dev/md0 -f /dev/sdb1 
  2. mdadm: set /dev/sdb1 faulty in /dev/md0 
  3. [root@shanker tmp]# cat /proc/mdstat 
  4. Personalities : [raid6] [raid5] [raid4] 
  5. md0 : active raid5 sdd1[3] sdc1[1] sdb1[0](F) 
  6.       10471424 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/2] [_UU] 
  7.  
 

有故障的设备有个(F)标志

删除故障盘

 
  
  1. [root@shanker tmp]# mdadm /dev/md0 -r /dev/sdb1 
  2. mdadm: hot removed /dev/sdb1 from /dev/md0 
  3. [root@shanker tmp]# cat /proc/mdstat 
  4. Personalities : [raid6] [raid5] [raid4] 
  5. md0 : active raid5 sdd1[3] sdc1[1] 
  6.       10471424 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/2] [_UU] 
 

此时pvdisplay 发现容量并没有减少,我们添加新硬盘/dev/sde然后格式化,添加到rai5,其实用之前移出的sdb盘也可以

 
  
  1. [root@shanker tmp]# pvdisplay 
  2.   --- Physical volume --- 
  3.   PV Name               /dev/md0 
  4.   VG Name               vg 
  5.   PV Size               9.99 GiB / not usable 2.00 MiB 
  6.   Allocatable           yes 
  7.   PE Size               4.00 MiB 
  8.   Total PE              2556 
  9.   Free PE               2175 
  10.   Allocated PE          381 
  11.   PV UUID               qWZXxe-DJ7H-K8xN-vPY2-uT7x-vt2M-yPNvhh 
  12.  
  13. [root@shanker tmp]# fdisk /dev/sde 
  14. Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel 
  15. Building a new DOS disklabel with disk identifier 0xd18a15b7. 
  16. Changes will remain in memory only, until you decide to write them. 
  17. After that, of course, the previous content won't be recoverable. 
  18.  
  19. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) 
  20.  
  21. WARNING: DOS-compatible mode is deprecated. It's strongly recommended to 
  22.          switch off the mode (command 'c') and change display units to 
  23.          sectors (command 'u'). 
  24.  
  25. Command (m for help): n 
  26. Command action 
  27.    e   extended 
  28.    p   primary partition (1-4) 
  29. Partition number (1-4): 1 
  30. First cylinder (1-652, default 1): 1 
  31. Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652): 
  32. Using default value 652 
  33.  
  34. Command (m for help): t 
  35. Selected partition 1 
  36. Hex code (type L to list codes): fd 
  37. Changed system type of partition 1 to fd (Linux raid autodetect) 
  38.  
  39. Command (m for help): w 
  40. The partition table has been altered! 
  41.  
  42. Calling ioctl() to re-read partition table. 
  43. Syncing disks. 
  44. [root@shanker tmp]# mdadm /dev/md0 -a /dev/sde1 
  45. mdadm: added /dev/sde1 
  46. [root@shanker tmp]# cat /proc/mdstat 
  47. Personalities : [raid6] [raid5] [raid4] 
  48. md0 : active raid5 sde1[4] sdd1[3] sdc1[1] 
  49.       10471424 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/2] [_UU] 
  50.       [=>...................]  recovery =  6.6% (346112/5235712) finish=1.8min speed=43264K/sec 
  51.  
 

最后来做LVM扩容

 
  
  1. [root@shanker tmp]# df -h /mnt/test1 
  2. Filesystem            Size  Used Avail Use% Mounted on 
  3. /dev/mapper/vg-lvm1  1008M   34M  924M   4% /mnt/test1 
  4. [root@shanker tmp]# lvextend -L +1g /dev/vg 
  5. vg/          vga_arbiter 
  6. [root@shanker tmp]# lvextend -L +1g /dev/vg/lvm1 
  7.   Extending logical volume lvm1 to 2.00 GiB 
  8.   Logical volume lvm1 successfully resized 
  9. [root@shanker tmp]# resize 
  10. resize2fs   resizecons 
  11. [root@shanker tmp]# resize2fs /dev/vg/lvm1 
  12. resize2fs 1.41.12 (17-May-2010) 
  13. Filesystem at /dev/vg/lvm1 is mounted on /mnt/test1; on-line resizing required 
  14. old desc_blocks = 1new_desc_blocks = 1 
  15. Performing an on-line resize of /dev/vg/lvm1 to 524288 (4k) blocks. 
  16. The filesystem on /dev/vg/lvm1 is now 524288 blocks long. 
  17.  
  18. [root@shanker tmp]# df -h /mnt/test1/ 
  19. Filesystem            Size  Used Avail Use% Mounted on 
  20. /dev/mapper/vg-lvm1   2.0G   34M  1.9G   2% /mnt/test1 

done~!