lvm 虚拟机管理

来源:互联网 发布:c语言入门那本书比较好 编辑:程序博客网 时间:2024/05/22 14:42

1,/dev/mapper/VoLGroup00-LogVol00,是一个逻辑卷设备 !这个设备是逻辑设备,不是物理设备,所以不能用fdisk -l来查看,你可以通过  lvdisplay  查看 !

2,在安装的时候,你先建立了一个 swap空间,剩余的空间系统首先建立一个physical volumes,这个物理卷的大小就应该是你分完swap分区后剩余的大小,然后在physical volumes上创建 volume groups ,最后在volume groups上面创建 logical volumes.这个 /dev/mapper/VoLGroup00-LogVol00就是 logical volumes的名字!

3的意思就是上面我说的 把/dev/sda2分区全部分配给了这个逻辑卷 !分区的大小你不用太关心,毕竟虚拟机上面分配的大小不是太准确 !

三,Logical Volume Manager(LVM)

creating logical volumes
1,create physical volumes
pvcreate /dev/sda3

2,assign physical volumes to volume groups
vgcreate vg0 /dev/sda3

3,create logical volumes from volume groups
lvcreate -L 256M -n data vg0
mke2fs -j /dev/vg0 data



resizing logical volumes
growing volumes
lvextend: can grow logical volumes
resize2fs: can grow EXT3 filesystems online
vgextend:adds new physical volumes to an existing volume group

shrinking volumes
filesystem must be reduced first
requires a filesystem check and cannot be performed online
lvreduce can then reduce the volume
volume groups can be reduced with:

pvmove /dev/sda4
vgreduce vg0 /dev/sda4


using LVM Snapshosts
1,create snapshot of existing logical volume
#lvcreate -l 64 -s -n databackup /dev/vg0/data
-l: 大小,(PE数量)
-s: 创建快照
-n: 快照名称

2,mount snapshot
#mkdir -p /mnt/databackup
#mount -o ro /dev/vg0/databackup /mnt/databackup

3,remove snapshot
#umount /mnt/databackup
#lvremove /dev/vg0/databackup


例子1:
创建lvm

分区情况:
/dev/sda5            1864        2113     2008093+  83  Linux
/dev/sda6            2114        2363     2008093+  83  Linux
/dev/sda7            2364        2613     2008093+  83  Linux

1,准备物理分区
[root@tonykorn97 ~]# pvcreate  /dev/sda5 /dev/sda6
  Physical volume "/dev/sda5" successfully created
  Physical volume "/dev/sda6" successfully created

2,创建卷组(vg)
[root@tonykorn97 ~]# vgcreate vg0 /dev/sda5 /dev/sda6
  Volume group "vg0" successfully created

3,创建逻辑卷(lv)
[root@tonykorn97 ~]# lvcreate -L 256M -n data vg0
  Logical volume "data" created
[root@tonykorn97 ~]# mke2fs -j /dev/vg0/data

4,查看逻辑卷状态(pvdisplay,vgdisplay,lvdisplay):
[root@tonykorn97 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda5
  VG Name               vg0
  PV Size               1.92 GB / not usable 1.03 MB
  Allocatable           yes
  PE Size (KByte)       4096
  Total PE              490
  Free PE               426
  Allocated PE          64
  PV UUID               IGrsYz-U1Cd-d2zc-qMZU-mN2F-6lhN-SnurgQ
   
  --- Physical volume ---
  PV Name               /dev/sda6
  VG Name               vg0
  PV Size               1.92 GB / not usable 1.03 MB
  Allocatable           yes
  PE Size (KByte)       4096
  Total PE              490
  Free PE               490
  Allocated PE          0
  PV UUID               42JR9t-eVBb-b10D-J3YB-DwIe-30Tn-A4f1qq
   
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               vol0
  PV Size               13.67 GB / not usable 17.96 MB
  Allocatable           yes
  PE Size (KByte)       32768
  Total PE              437
  Free PE               165
  Allocated PE          272
  PV UUID               zQcqMn-cl8j-vDJX-q1RK-qZu7-tOOd-U4bhsd
   
[root@tonykorn97 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg0
  System ID            
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               3.83 GB
  PE Size               4.00 MB
  Total PE              980
  Alloc PE / Size       64 / 256.00 MB
  Free  PE / Size       916 / 3.58 GB
  VG UUID               pVpA1x-0zHs-a2K4-f43m-lgs2-LmHt-wFIylW
   
  --- Volume group ---
  VG Name               vol0
  System ID            
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               13.66 GB
  PE Size               32.00 MB
  Total PE              437
  Alloc PE / Size       272 / 8.50 GB
  Free  PE / Size       165 / 5.16 GB
  VG UUID               mvOUOL-nHtO-aqUo-vM82-27hf-gUw0-iqUAhK
   
[root@tonykorn97 ~]# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vg0/data
  VG Name                vg0
  LV UUID                zJWu4F-3AkP-nGlZ-vivg-g0EM-DrjQ-z80oaa
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                256.00 MB
  Current LE             64
  Segments               1
  Allocation             inherit
  Read ahead sectors     0
  Block device           253:2
   
  --- Logical volume ---
  LV Name                /dev/vol0/root
  VG Name                vol0
  LV UUID                2YFBhR-WfBk-JFjy-uRB7-Z1lt-2hVi-62GTED
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                8.00 GB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     0
  Block device           253:0
   
  --- Logical volume ---
  LV Name                /dev/vol0/home
  VG Name                vol0
  LV UUID                7RcMbr-BAXi-kJ3t-5A1P-vqNh-3OuC-bY2qlk
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                512.00 MB
  Current LE             16
  Segments               1
  Allocation             inherit
  Read ahead sectors     0
  Block device           253:1
   
[root@tonykorn97 ~]#


例子2:增大逻辑卷的步骤
简单的步骤如下(文件系统没有加载):
[root@tonykorn97 ~]# lvextend -L +500M /dev/vg0/data
  Extending logical volume data to 756.00 MB
  Logical volume data successfully resized
[root@tonykorn97 ~]# resize2fs /dev/vg0/data
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/vg0/data to 774144 (1k) blocks.
The filesystem on /dev/vg0/data is now 774144 blocks long.

[root@tonykorn97 ~]# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vg0/data
  VG Name                vg0
  LV UUID                zJWu4F-3AkP-nGlZ-vivg-g0EM-DrjQ-z80oaa
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                756.00 MB
  Current LE             189
  Segments               1
  Allocation             inherit
  Read ahead sectors     0
  Block device           253:2
   
详细步骤:
1,lvextend -L +500M /dev/vg0/data
2,umount /data
3,e2fsck -f /dev/vg0/data
4,resize2fs /dev/vg0/data
5,lvdisplay

例子3,缩小vg的容量大小(一般情况下不要用)
1,umont /data
2,e2fack -f /dev/vg0/data
3,[root@tonykorn97 ~]# resize2fs /dev/vg0/data 100M
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/vg0/data to 102400 (1k) blocks.
The filesystem on /dev/vg0/data is now 102400 blocks long.

[root@tonykorn97 ~]#

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

5,lvdisplay
[root@tonykorn97 ~]# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vg0/data
  VG Name                vg0
  LV UUID                zJWu4F-3AkP-nGlZ-vivg-g0EM-DrjQ-z80oaa
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                100.00 MB
  Current LE             25
  Segments               1
  Allocation             inherit
  Read ahead sectors     0
  Block device           253:2
   


例子4,增大物理卷组的步骤
1,pvcreate /dev/sda7  
2,vgextend vg0 /dev/sda7
3,vgdisplay

原来的状态:
[root@tonykorn97 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg0
  System ID            
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               3.83 GB
  PE Size               4.00 MB
  Total PE              980
  Alloc PE / Size       25 / 100.00 MB
  Free  PE / Size       955 / 3.73 GB

创建pv
[root@tonykorn97 ~]# pvcreate /dev/sda7
  Physical volume "/dev/sda7" successfully created

扩展vg
[root@tonykorn97 ~]# vgextend vg0 /dev/sda7
  Volume group "vg0" successfully extended

查看现在的状态:
[root@tonykorn97 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg0
  System ID            
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               5.74 GB
  PE Size               4.00 MB
  Total PE              1470
  Alloc PE / Size       25 / 100.00 MB
  Free  PE / Size       1445 / 5.64 GB
  VG UUID               pVpA1x-0zHs-a2K4-f43m-lgs2-LmHt-wFIylW
   

例子5,删除逻辑卷
[root@tonykorn97 ~]# lvremove /dev/vg0/data
Do you really want to remove active logical volume "data"? [y/n]: y
  Logical volume "data" successfully removed
[root@tonykorn97 ~]#

例子6,删除pv,此时这个分区正在使用,需要先删除vg,然后才可以再删除pv
[root@tonykorn97 ~]# pvremove /dev/sda7
  Can't pvremove physical volume "/dev/sda7" of volume group "vg0" without -ff
[root@tonykorn97 ~]# vgremove /dev/vg0
  Volume group "vg0" successfully removed
[root@tonykorn97 ~]# pvremove /dev/sda7
  Labels on physical volume "/dev/sda7" successfully wiped
[root@tonykorn97 ~]#

不要使用pvremove的-ff参数,使用的话会把vg信息破坏掉。

上面的命令需要删除vg,如果vg中存在内容的话就不能使用,正确的做法是:
[root@tonykorn97 ~]# vgreduce vg0 /dev/sda7
  Removed "/dev/sda7" from volume group "vg0"
[root@tonykorn97 ~]# pvremove /dev/sda7
  Labels on physical volume "/dev/sda7" successfully wiped

原创粉丝点击