linux的軟RAID和LVM測試

liiinuuux發表於2012-11-22
原創文章,轉載請標明出處
http://space.itpub.net/26239116/viewspace-749754
檢視硬碟
[root@test ~]# fdisk -l

Disk /dev/sda: 17.1 GB, 17179869184 bytes
255 heads, 63 sectors/track, 2088 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        1957    15719571   83  Linux
/dev/sda2            1958        2088     1052257+  82  Linux swap / Solaris

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdb doesn't contain a valid partition table

Disk /dev/sdc: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdc doesn't contain a valid partition table

Disk /dev/sdd: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdd doesn't contain a valid partition table

為新硬碟分割槽
[root@test ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-130, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-130, default 130):
Using default value 130

Command (m for help):
Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@test ~]# fdisk /dev/sdc
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-130, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-130, default 130):
Using default value 130

Command (m for help):
Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@test ~]# fdisk /dev/sdd
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-130, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-130, default 130):
Using default value 130

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

[root@test ~]# fdisk -l

Disk /dev/sda: 17.1 GB, 17179869184 bytes
255 heads, 63 sectors/track, 2088 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        1957    15719571   83  Linux
/dev/sda2            1958        2088     1052257+  82  Linux swap / Solaris

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         130     1044193+  83  Linux

Disk /dev/sdc: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1         130     1044193+  83  Linux

Disk /dev/sdd: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdd1               1         130     1044193+  83  Linux


用mdadm建立RAID
[root@test ~]# mdadm --create /dev/md0 --level=5 --raid-devices=3 /dev/sdb1 /dev/sdc1 /dev/sdd1
mdadm: array /dev/md0 started.

檢視結果
[root@test ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sdd1[3] sdc1[1] sdb1[0]
      2088192 blocks level 5, 64k chunk, algorithm 2 [3/2] [UU_]
      [============>........]  recovery = 60.1% (629640/1044096) finish=0.0min speed=69960K/sec
     
unused devices:

fdisk -l 中出現md0
Disk /dev/md0: 2138 MB, 2138308608 bytes
2 heads, 4 sectors/track, 522048 cylinders
Units = cylinders of 8 * 512 = 4096 bytes

此時在/var/log/message中出現日誌
Nov 22 09:44:01 test kernel: RAID5 conf printout:
Nov 22 09:44:01 test kernel:  --- rd:3 wd:3 fd:0
Nov 22 09:44:01 test kernel:  disk 0, o:1, dev:sdb1
Nov 22 09:44:01 test kernel:  disk 1, o:1, dev:sdc1
Nov 22 09:44:01 test kernel:  disk 2, o:1, dev:sdd1

建立配置檔案,以後mdadm就讀取這個配置檔案了
[root@test etc]# echo DEVICE /dev/sdb1 /dev/sdc1 /dev/sdd1 >> /etc/mdadm.conf
[root@test etc]# mdadm --detail --scan >> /etc/mdadm.conf
[root@test etc]# cat /etc/mdadm.conf
DEVICE /dev/sdb1 /dev/sdc1 /dev/sdd1
ARRAY /dev/md0 level=raid5 num-devices=3 metadata=0.90 UUID=52dd5dda:c89e89e7:6f23fb83:33ff6967



測試停止RAID
[root@test etc]# mdadm -S /dev/md0
mdadm: stopped /dev/md0

[root@test etc]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
unused devices:
[root@test etc]# fdisk -l

Disk /dev/sda: 17.1 GB, 17179869184 bytes
255 heads, 63 sectors/track, 2088 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        1957    15719571   83  Linux
/dev/sda2            1958        2088     1052257+  82  Linux swap / Solaris

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         130     1044193+  83  Linux

Disk /dev/sdc: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1         130     1044193+  83  Linux

Disk /dev/sdd: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdd1               1         130     1044193+  83  Linux


測試透過配置檔案啟動RAID
[root@test etc]# mdadm -As /dev/md0
mdadm: /dev/md0 has been started with 3 drives.
[root@test etc]#
[root@test etc]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sdb1[0] sdd1[2] sdc1[1]
      2088192 blocks level 5, 64k chunk, algorithm 2 [3/3] [UUU]
     
unused devices:
[root@test etc]# fdisk -l

Disk /dev/sda: 17.1 GB, 17179869184 bytes
255 heads, 63 sectors/track, 2088 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        1957    15719571   83  Linux
/dev/sda2            1958        2088     1052257+  82  Linux swap / Solaris

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         130     1044193+  83  Linux

Disk /dev/sdc: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1         130     1044193+  83  Linux

Disk /dev/sdd: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdd1               1         130     1044193+  83  Linux

Disk /dev/md0: 2138 MB, 2138308608 bytes
2 heads, 4 sectors/track, 522048 cylinders
Units = cylinders of 8 * 512 = 4096 bytes

Disk /dev/md0 doesn't contain a valid partition table

將ARID(或各種磁碟)標記為物理卷
[root@test etc]# pvcreate /dev/md0
  Physical volume "/dev/md0" successfully created


將物理卷組成卷組(後期可以透過vgextend vg0 /dev/sde之類的命令新增pv)
[root@test dev]# vgcreate vg0 /dev/md0
  Volume group "vg0" successfully created
[root@test dev]#
[root@test dev]# vgdisplay
  --- Volume group ---
  VG Name               vg0
  System ID            
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               1.99 GB
  PE Size               4.00 MB
  Total PE              509
  Alloc PE / Size       0 / 0  
  Free  PE / Size       509 / 1.99 GB
  VG UUID               KGXMpn-QwMa-N7TA-MXLp-C84N-LqZs-1QON6n


建立邏輯卷,-l後面指定的大小,對應vgdiskplay的total pe
[root@test dev]# lvcreate  -l 100 -n lv0 vg0
  Logical volume "lv0" created
[root@test dev]# lvcreate  -l 100 -n lv1 vg0
  Logical volume "lv1" created
或者用-L,以兆、G等單位指定大小
[root@test dev]# lvcreate -L 512M -n lv0 vg0
  Logical volume "lv0" created
[root@test dev]# lvcreate -L 512M -n lv1 vg0
  Logical volume "lv1" created


檢視結果
[root@test dev]# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vg0/lv0
  VG Name                vg0
  LV UUID                AnNP25-bCf5-OhzC-iKvR-sVMd-oPaq-3jsELV
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                512.00 MB
  Current LE             128
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     512
  Block device           253:0
  
  --- Logical volume ---
  LV Name                /dev/vg0/lv1
  VG Name                vg0
  LV UUID                VUVNiK-h3YR-BzAJ-s5uo-xfUh-Tu3j-mItA5h
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                512.00 MB
  Current LE             128
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     512
  Block device           253:1


建立檔案系統
[root@test dev]# mkfs.ext3  /dev/vg0/lv0
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
64 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
     8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

Writing inode tables: done                           
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 34 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@test dev]# mkfs.ext3  /dev/vg0/lv1
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
64 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
     8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

Writing inode tables: done                           
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.


掛載
[root@test mnt]# mount /dev/vg0/lv0 /mnt/lv0
[root@test mnt]# mount /dev/vg0/lv1 /mnt/lv1


模擬磁碟故障
[root@test mnt]# mdadm /dev/md0 -f /dev/sdc1
mdadm: set /dev/sdc1 faulty in /dev/md0

在日誌中有如下資訊
[root@test mnt]# tail /var/log/messages
Nov 22 14:06:33 test kernel: raid5: Disk failure on sdc1, disabling device. Operation continuing on 2 devices
Nov 22 14:06:33 test kernel: RAID5 conf printout:
Nov 22 14:06:33 test kernel:  --- rd:3 wd:2 fd:1
Nov 22 14:06:33 test kernel:  disk 0, o:1, dev:sdb1
Nov 22 14:06:33 test kernel:  disk 1, o:0, dev:sdc1
Nov 22 14:06:33 test kernel:  disk 2, o:1, dev:sdd1
Nov 22 14:06:33 test kernel: RAID5 conf printout:
Nov 22 14:06:33 test kernel:  --- rd:3 wd:2 fd:1
Nov 22 14:06:33 test kernel:  disk 0, o:1, dev:sdb1
Nov 22 14:06:33 test kernel:  disk 2, o:1, dev:sdd1

檢視RAID狀態
[root@test mnt]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sdb1[0] sdd1[2] sdc1[3](F)
      2088192 blocks level 5, 64k chunk, algorithm 2 [3/2] [U_U]

動態移除故障磁碟
[root@test mnt]# mdadm /dev/md0 -r /dev/sdc1
mdadm: hot removed /dev/sdc1

動態新增磁碟
[root@test mnt]# mdadm /dev/md0 -a /dev/sdc1
mdadm: re-added /dev/sdc1

檢視恢復進度
[root@test mnt]# tail -f /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sdc1[1] sdb1[0] sdd1[2]
      2088192 blocks level 5, 64k chunk, algorithm 2 [3/2] [U_U]
      [========>............]  recovery = 41.1% (430208/1044096) finish=0.1min speed=71701K/sec
     
unused devices:

完成後
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sdc1[1] sdb1[0] sdd1[2]
      2088192 blocks level 5, 64k chunk, algorithm 2 [3/3] [UUU]
     
unused devices:



調整邏輯卷空間
先擴充套件邏輯卷
[root@test mnt]# lvextend -L+100M /dev/vg0/lv0
  Extending logical volume lv0 to 612.00 MB
  Logical volume lv0 successfully resized
再擴充套件檔案系統
[root@test mnt]# resize2fs /dev/vg0/lv0
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/vg0/lv0 is mounted on /mnt/lv0; on-line resizing required
Performing an on-line resize of /dev/vg0/lv0 to 626688 (1k) blocks.
The filesystem on /dev/vg0/lv0 is now 626688 blocks long.

[root@test mnt]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              15G  6.4G  7.4G  47% /
tmpfs                 760M     0  760M   0% /dev/shm
vmshare               466G  124G  343G  27% /media/sf_vmshare
/dev/mapper/vg0-lv0   593M   19M  544M   4% /mnt/lv0
/dev/mapper/vg0-lv1   496M   19M  452M   4% /mnt/lv1

收縮邏輯卷(有可能丟失資料)
首先要減小檔案系統的尺寸
[root@test mnt]# resize2fs /dev/vg0/lv0  500M
resize2fs 1.39 (29-May-2006)
Please run 'e2fsck -f /dev/vg0/lv0' first.

[root@test mnt]# e2fsck -f /dev/vg0/lv0
e2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg0/lv0: 12/254000 files (8.3% non-contiguous), 34606/1024000 blocks
[root@test mnt]# resize2fs /dev/vg0/lv0  500M
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/vg0/lv0 to 512000 (1k) blocks.
The filesystem on /dev/vg0/lv0 is now 512000 blocks long.

再減小邏輯卷,減小邏輯卷時,不要小於檔案系統大小
[root@test mnt]# lvreduce  -L -500M /dev/vg0/lv0
  WARNING: Reducing active logical volume to 500.00 MB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv0? [y/n]: y
  Reducing logical volume lv0 to 500.00 MB
  Logical volume lv0 successfully resized

[root@test mnt]# mount /dev/vg0/lv0 lv0/
[root@test mnt]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              15G  6.4G  7.4G  47% /
tmpfs                 760M     0  760M   0% /dev/shm
vmshare               466G  124G  343G  27% /media/sf_vmshare
/dev/mapper/vg0-lv0   485M  2.3M  457M   1% /mnt/lv0

如果邏輯卷的大小本身就大於檔案系統,可以在掛載狀態用lvresize線上收縮邏輯卷。






來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26239116/viewspace-749754/,如需轉載,請註明出處,否則將追究法律責任。

相關文章