Logical Volume Manager(LVM) 筆記

tonykorn97發表於2008-07-18


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[@more@]



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

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

相關文章