> Linux教程 > linux基础 >

解析linux中raid和lvm

raid(磁盘阵列)分为软、硬二种:
软raid
硬raid

raid 0    1    5    10
raid0 :
优点:读写速度加快
缺点:不安全

raid1:
优点:读取速度快,安全
缺点:写入速度慢

raid10:radi1和raid0的集合

raid5:
优点和缺点介于0和1之间

准备4个新分区:
 

复制代码代码示例:
[root@station230 ~]# mdadm  --create  /dev/md0  --level=5 --raid-devices=3 --spare-devices=1  /dev/sda{7,8,9,10}
 

或者:
 

复制代码代码示例:
[root@station230 md0]# mdadm -C /dev/md0 -l 5 -n 3 -x 1 /dev/sda{7,8,9,10}
[root@station230 md0]# mdadm -D /dev/md0
[root@station230 md0]# mdadm -Ds 
[root@station230 md0]# mkfs.ext3 /dev/md0
[root@station230 ~]# mkdir /mnt/md0
[root@station230 ~]# mount /dev/md0  /mnt/md0
[root@station230 ~]# cd /mnt/md0
[root@station230 md0]# ls
lost+found

主设备号
次设备号

创建设备文件:
 

复制代码代码示例:

mknod 

/proc/devices

[root@station230 dev]# mdadm -Ds  > /etc/mdadm.conf
[root@station230 dev]# echo 'mount /dev/md0 /mnt/md0'  >>  /etc/rc.local
[root@station230 dev]# vim /etc/fstab
/dev/md0      /mnt/md0     ext3    defaults      0     0

[root@station230 dev]# mdadm -D /dev/md0
 Number   Major   Minor   RaidDevice State
       0       8        7        0      active sync   /dev/sda7
       1       8       10        1      active sync   /dev/sda10
       2       8        9        2      active sync   /dev/sda9
       3       8        8        -      faulty spare   /dev/sda8

[root@station230 dev]# mdadm -f /dev/md0  /dev/sda8
mdadm: set /dev/sda8 faulty in /dev/md0

[root@station230 dev]# mdadm -D /dev/md0
 Number   Major   Minor   RaidDevice State
       0       8        7        0      active sync   /dev/sda7
       1       8       10        1      active sync   /dev/sda10
       2       8        9        2      active sync   /dev/sda9
       3       8        8        -      faulty spare   /dev/sda8

[root@station230 dev]# mdadm -r /dev/md0 /dev/sda8
mdadm: hot removed /dev/sda8
[root@station230 dev]# mdadm -a /dev/md0 /dev/sda8
mdadm: added /dev/sda8

lvm(逻辑卷)

创建4个分区:
创建Pv:
 

复制代码代码示例:
[root@station230 ~]# pvcreate /dev/sda{11,12,13,14}
  Physical volume "/dev/sda11" successfully created
  Physical volume "/dev/sda12" successfully created
  Physical volume "/dev/sda13" successfully created
  Physical volume "/dev/sda14" successfully created

创建并加入卷组:
 

复制代码代码示例:
[root@station230 ~]# vgcreate vg0 /dev/sda{11,12,13}
  Volume group "vg0" successfully created

创建lv:
 

复制代码代码示例:
[root@station230 dev]# lvcreate -L 70M -n lv0  vg0
  Rounding up size to full physical extent 72.00 MB
  Logical volume "lv0" created

创建文件系统:
 

复制代码代码示例:

[root@station230 dev]# mkdir /mnt/lv0
[root@station230 dev]# mkfs.ext3 /dev/vg0/lv0

[root@station230 dev]# mount /dev/vg0/lv0 /mnt/lv0
[root@station230 dev]# cd /mnt/lv0
[root@station230 lv0]# ls
lost+found

查看:
 

复制代码代码示例:

[root@station230 lv0]# pvdisplay 
[root@station230 lv0]# pvscan

[root@station230 lv0]# vgdisplay 
[root@station230 lv0]# vgscan

[root@station230 lv0]# lvdisplay 
[root@station230 lv0]# lvscan

扩容:
扩大vg:
 

复制代码代码示例:
[root@station230 lv0]# vgextend vg0 /dev/sda14
  Volume group "vg0" successfully extended
 

扩大lv:
 

复制代码代码示例:
[root@station230 lv0]# lvextend -L 100M /dev/vg0/lv0 
  Extending logical volume lv0 to 100.00 MB
  Logical volume lv0 successfully resized
 

或者:
 

复制代码代码示例:

[root@station230 lv0]# lvextend -L +30M /dev/vg0/lv0

[root@station230 lv0]# resize2fs /dev/vg0/lv0 100M
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 102400 (1k) blocks.
The filesystem on /dev/vg0/lv0 is now 102400 blocks long.



lvm缩容:
卸载:
 

 

复制代码代码示例:
[root@station230 ~]# umount /dev/vg0/lv0

检查文件系统:
 

复制代码代码示例:
[root@station230 ~]# 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: 11/26624 files (9.1% non-contiguous), 9031/102400 blocks

文件系统级别缩容:
 

复制代码代码示例:
[root@station230 ~]# resize2fs /dev/vg0/lv0 80M
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/vg0/lv0 to 81920 (1k) blocks.
The filesystem on /dev/vg0/lv0 is now 81920 blocks long.

lv缩容:
 

复制代码代码示例:

[root@station230 ~]# lvreduce  -L -20M /dev/vg0/lv0 
  WARNING: Reducing active logical volume to 80.00 MB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv0? [y/n]: y
  Reducing logical volume lv0 to 80.00 MB
  Logical volume lv0 successfully resized

 [root@station230 ~]# vgreduce  vg0  /dev/sda14
  Removed "/dev/sda14" from volume group "vg0"

[root@station230 ~]# pvremove /dev/sda14
  Labels on physical volume "/dev/sda14" successfully wiped

[root@station230 ~]# vgremove vg0
[root@station230 ~]# lvremove lv0

lvm快照:
 

复制代码代码示例:

[root@station230 lv0]# lvcreate  -s /dev/vg0/lv0  -n lv0bak -L 20M 
  Logical volume "lv0bak" created

[root@station230 lv0bak]# lvscan 
  ACTIVE   Original '/dev/vg0/lv0' [80.00 MB] inherit
  ACTIVE   Snapshot '/dev/vg0/lv0bak' [20.00 MB] inherit

替换物理卷:
把新的pv添加到vg:
 

复制代码代码示例:
[root@station230 lv0bak]# pvcreate /dev/sda14
  Physical volume "/dev/sda14" successfully created
[root@station230 lv0bak]# vgextend vg0 /dev/sda14
  Volume group "vg0" successfully extended

移动数据:
 

复制代码代码示例:
[root@station230 ~]# pvmove /dev/sda11 /dev/sda14
  /dev/sda11: Moved: 100.0%

移出旧pv:
 

复制代码代码示例:
[root@station230 ~]# vgreduce vg0 /dev/sda11
  Removed "/dev/sda11" from volume group "vg0"
[root@station230 ~]# cd /mnt/lv0
[root@station230 lv0]# ls
a  b  c  d  lost+found

迁移:

原来的主机:
 

复制代码代码示例:
[root@station230 ~]# umount /dev/vg0/lv0 
[root@station230 ~]# vgchange -a n vg0
  0 logical volume(s) in volume group "vg0" now active
[root@station230 ~]# vgexport vg0
  Volume group "vg0" successfully exported

替换的主机:
 

复制代码代码示例:
[root@station230 ~]# vgimport vg0
  Volume group "vg0" successfully imported
[root@station230 ~]# vgchange -a y vg0
  1 logical volume(s) in volume group "vg0" now active
[root@station230 ~]# mount /dev/vg0/lv0 /mnt/lv0
(责任编辑:IT)