歡迎您光臨本站 註冊首頁

LVM---動態調整磁碟容量

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

LVM:logical volume manager(邏輯卷管理);LVM屏蔽了底層磁碟布局,方便於動態調整磁碟容量.

一、創建邏輯卷的步驟:
1)通過fdisk 工具將磁碟轉換為linux分區
2)通過pvcreate命令將linux分區轉換成物理卷(PV);
3)通過vgcreate命令將創建好的物理卷處理成卷組(VG);
4)通過lvcreate命令將卷組分成若干個邏輯卷(LV);
5)對邏輯卷進行格式化,掛載,動態調整邏輯卷的大小,並且該操作不會影響邏輯卷(Lv)上的數據.

二、物理卷(PV)創建及管理具體操作步驟:
1)先查看linux分區,將未使用空間轉換為物理卷(先使用fdisk建立普通分區)

[[email protected] ~]# fdisk -l /dev/sdb #查看linux分區情況

Disk /dev/sdb: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 500 4016218 83 Linux
/dev/sdb2 501 1000 4016250 83 Linux
/dev/sdb3 1001 1500 4016250 83 Linux
/dev/sdb4 1501 2610 8916075 5 Extended
/dev/sdb5 1501 2610 8916043 83 Linux

備註:/dev/sdb是一塊新增加的磁碟,上面沒有任何數據,也未掛載

2)將linux物理分區轉變為物理卷
[[email protected] ~]# pvcreate /dev/sdb{1,2} #將物理分區/dev/sdb{1,2}轉變為物理卷
Physical volume "/dev/sdb1" successfully created


Physical volume "/dev/sdb2" successfully created

3)#使用Pvscan查看物理卷信息
[[email protected] ~]# pvscan #查看物理卷信息,會顯示所有物理卷信息
PV /dev/sda2 VG VolGroup00 lvm2 [39.88 GB / 0 free]
PV /dev/sdb1 lvm2 [3.83 GB]
PV /dev/sdb2 lvm2 [3.83 GB]
Total: 3 [47.54 GB] / in use: 1 [39.88 GB] / in no VG: 2 [7.66 GB]

4)使用pvdisplay查看各物理卷詳細參數
[[email protected] ~]# pvdisplay #查看各物理卷詳細參數
--- Physical volume ---
PV Name /dev/sda2
VG Name VolGroup00
PV Size 39.90 GB / not usable 20.79 MB
Allocatable yes (but full)
PE Size (KByte) 32768
Total PE 1276
Free PE 0
Allocated PE 1276
PV UUID aJlaad-NHPT-Cgg3-7yu4-a2RJ-kJJ1-qxSFgD

--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 3.83 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID v2VajD-yS53-SiQA-yTzu-KOiD-RyT3-p0wTvt

--- NEW Physical volume ---
PV Name /dev/sdb2
VG Name
PV Size 3.83 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID iOoK3V-yuww-ZlLF-cRLq-v7hC-CL7c-0bQU1x


----------------------------------------------------------------------
當物理卷沒有被使用時可刪除物理卷
[[email protected] /]# pvremove /dev/sdb2 #刪除物理卷,
Labels on physical volume "/dev/sdb2" successfully wiped
----------------------------------------------------------------------
三、卷組(VG)創建及管理具體操作步驟:

1)使用vgcreate將物理卷轉化為卷組

[[email protected] /]# vgcreate vg01 /dev/sdb{1,2} #將已經是物理卷的/dev/sdb{1,2}轉化為卷組名為vg01的卷組
Volume group "vg01" successfully created
備註:以上未加參數,擴展塊(PE)大小默認4M,若通過 vgcreate -s 8M vg01 /dev/sdb{1,2},則指定了擴展塊大小為8M

2)使用vgdisplay 查看所有卷組詳細信息
[[email protected] /]# vgdisplay #看所有卷組詳細信息
--- Volume group ---
VG Name vg01
System ID
Format lvm2
Metadata Areas 2
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 2
Act PV 2
VG Size 7.66 GB
PE Size 4.00 MB
Total PE 1960
Alloc PE / Size 0 / 0
Free PE / Size 1960 / 7.66 GB
VG UUID 1g8QL0-0cGM-TJji-Q98P-LJ3f-PhDN-2ouSM3



--- Volume group ---
VG Name VolGroup00
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 39.88 GB
PE Size 32.00 MB
Total PE 1276
Alloc PE / Size 1276 / 39.88 GB
Free PE / Size 0 / 0
VG UUID AhhisY-vDrc-s4jx-XIsn-QmCp-wMiT-2v01YZ
備註:也可以通過 [[email protected] /]# vgdisplay -v /dev/vg01 查看具體某一卷組詳細信息

3)查看卷組信息
[[email protected] /]# vgscan #查看卷組信息
Reading all physical volumes. This may take a while...
Found volume group "vg01" using metadata type lvm2
Found volume group "VolGroup00" using metadata type lvm2

4)擴展卷組vgextend,將某個物理卷添加到已存在的卷組中
[[email protected] /]# pvcreate /dev/sdb3 #創建一個新的物理卷
Physical volume "/dev/sdb3" successfully created
[[email protected] /]# vgextend vg01 /dev/sdb3 #將新增的物理卷添加到vg01卷組中
Volume group "vg01" successfully extended
-----------------------------------------------------------------------
使用vgremove刪除卷組
[[email protected] /]# vgremove /dev/vg01


Volume group "vg01" successfully removed
-----------------------------------------------------------------------


四、邏輯卷(LV)創建及管理具體操作步驟:

1)創建邏輯卷大小為6G卷名為data,從vg01生成
[[email protected] /]# lvcreate -L 6G -n data vg01 #從卷組vg01上劃分6G的空間為邏輯卷data
Logical volume "data" created

2)對劃分的邏輯卷進行格式化
[[email protected] /]# mkfs -t ext3 /dev/vg01/data #以ext3的文件格式化邏輯卷
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
786432 inodes, 1572864 blocks
78643 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1610612736
48 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736

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

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

備註:也可通過 [[email protected] /]# mkfs.ext3 /dev/vg01/data 格式化

3)使用lvsacn查看邏輯卷的信息
[[email protected] /]# lvscan #查看邏輯卷的信息
ACTIVE '/dev/vg01/data' [6.00 GB] inherit


ACTIVE '/dev/VolGroup00/LogVol00' [38.88 GB] inherit
ACTIVE '/dev/VolGroup00/LogVol01' [1.00 GB] inherit

4)使用lvdisplay查看邏輯卷的具體參數:
[[email protected] /]# lvdisplay #查看邏輯卷的具體參數
--- Logical volume ---
LV Name /dev/vg01/data
VG Name vg01
LV UUID QUmuTB-ofgI-9BbG-1DvN-gWzo-7Vqb-Twmf45
LV Write Access read/write
LV Status available
# open 0
LV Size 6.00 GB
Current LE 1536
Segments 2
Allocation inherit
Read ahead sectors 0
Block device 253:2

--- Logical volume ---
LV Name /dev/VolGroup00/LogVol00
VG Name VolGroup00
LV UUID SrNP2L-bOWm-4clq-22Lh-Fg10-ydeg-7dNpdH
LV Write Access read/write
LV Status available
# open 1
LV Size 38.88 GB
Current LE 1244
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 253:0

--- Logical volume ---
LV Name /dev/VolGroup00/LogVol01
VG Name VolGroup00
LV UUID e7u6Wx-MXhq-Nc2o-lrF9-yea1-Hia5-Cv7d7e
LV Write Access read/write
LV Status available
# open 1
LV Size 1.00 GB
Current LE 32
Segments 1
Allocation inherit
Read ahead sectors 0


Block device 253:1
備註:也可使用 [[email protected] /]# lvdisplay -v /dev/vg01/data 查看某一邏輯卷詳細參數

5)使用lvextend增大邏輯卷大小,在線擴容
[[email protected] /]# lvextend -L 1G /dev/vg01/data #從卷組vg01上對邏輯卷/dev/vg01/data進行擴容,邏輯卷大小變為7GB
Extending logical volume data to 7.00 GB
Logical volume data successfully resized

6)使用resize2fs命令更新系統識別的文件系統大小,立即生效
[[email protected] /]# resize2fs /dev/vg01/data #使增加的邏輯卷大小立即生效
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/vg01/data to 1835008 (4k) blocks.
The filesystem on /dev/vg01/data is now 1835008 blocks long.

7)使用lvreduce減小邏輯卷大小,必須是離線方式(即先卸載文件系統)
[[email protected] /]# lvreduce -L -1G /dev/vg01/data #將邏輯卷/dev/vg01/data容量減小1GB
/dev/cdrom: open failed: Read-only file system
WARNING: Reducing active logical volume to 6.00 GB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce data? [y/n]: y
Reducing logical volume data to 6.00 GB
Logical volume data successfully resized
[[email protected] /]# resize2fs /dev/vg01/data #使減少的邏輯卷大小立即生效
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/vg01/data to 1572864 (4k) blocks.
resize2fs: Can't read an block bitmap while trying to resize /dev/vg01/data


備註:縮小邏輯卷通常要先卸載文件系統,並且縮小后空間容量必須大於等於文件當前佔用的容量,若操作不當,會導致數據丟失,須謹慎.

[[email protected] /]# lvscan #查看邏輯卷大小變為6GB
ACTIVE '/dev/vg01/data' [6.00 GB] inherit
ACTIVE '/dev/VolGroup00/LogVol00' [38.88 GB] inherit
ACTIVE '/dev/VolGroup00/LogVol01' [1.00 GB] inherit
備註:
--------------------------------------------------------------------
刪除邏輯卷
[[email protected] /]#lvremove /dev/vg01/data
--------------------------------------------------------------------
五、掛載邏輯卷
1)將邏輯卷掛載到/quota目錄
[[email protected] /]# mount /dev/vg01/data /quota/ #將邏輯卷掛載到/quota
[[email protected] /]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
ext3 38G 11G 26G 29% /
/dev/sda1 ext3 99M 12M 82M 13% /boot
tmpfs tmpfs 233M 0 233M 0% /dev/shm
/dev/hdc iso9660 224M 224M 0 100% /media/cdrom
/dev/mapper/vg01-data
ext3 6.9G 142M 6.5G 3% /quota

2)設置開機自動掛載
[[email protected] /]#vi /etc/fstab #設置開機自動掛載
/dev/VolGroup00/LogVol00 / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2


devpts /dev/pts devpts gid=5,mode=620 0 0
tmpfs /dev/shm tmpfs defaults 0 0
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
/dev/VolGroup00/LogVol01 swap swap defaults 0 0
/dev/vg01/data /quota ext3 defaults 0 0

六、邏輯卷快照管理功能,可以把卷中的數據凍結起來,類似於為這些數據拍一張照片,可以永久保存建立快照當時的狀態.
1)創建卷快照
[[email protected] ~]# lvcreate -L 1G -s -n snaplv1 /dev/vg01/data # lvcreate -L 原始邏輯卷大小的15%-20% -s -n 快照名 源邏輯卷名
Logical volume "snaplv1" created
備註:和創建邏輯卷差不多,只是加了參數 -s
[[email protected] ~]# lvscan #查看狀態為 snapshot
ACTIVE Original '/dev/vg01/data' [6.00 GB] inherit
ACTIVE Snapshot '/dev/vg01/snaplv1' [1.00 GB] inherit
ACTIVE '/dev/VolGroup00/LogVol00' [38.88 GB] inherit
ACTIVE '/dev/VolGroup00/LogVol01' [1.00 GB] inherit

2)創建好快照后也需要掛載點
[[email protected] ~]# mkdir /snap #創建快照掛載點
[[email protected] ~]# mount /dev/vg01/snaplv1 /snap

#掛載快照到/snap
備註:snap中的文件與/quota下一樣,即使在/quota下新增或刪除文件,/snap仍然保持不變,此時可對/snap進行備份.

3)由於每產生一個卷快照就會佔用一部分卷組空間,因此產生的卷快照越多,卷組可用的空間就會越來越少.在完成備份后就可刪除快照了.
[[email protected] quota]# umount /snap/ #卸載快照
[[email protected] quota]# lvremove /dev/vg01/snaplv1 #刪除快照
/dev/cdrom: open failed: Read-only file system
Do you really want to remove active logical volume "snaplv1"? [y/n]: y
Logical volume "snaplv1" successfully removed

七、當有一天實體磁碟的分區出現了故障,必須要更換硬碟時,該如何處理?LVM提供了pvmove工具,可以將一個物理卷上的數據轉移到另外一個物理卷上.
1)將新的物理卷加入到卷組中
[[email protected] /]# pvcreate /dev/sdc1 #將linux分區轉換為物理卷
Physical volume "/dev/sdc1" successfully created
[[email protected] /]# vgextend vg01 /dev/sdc1 #將新增的物理卷添加到vg01卷組中
Volume group "vg01" successfully extended

2)移動物理卷上的數據到新加入的物理卷上


[[email protected] ~]# pvmove /dev/sdb1 /dev/sdc1 #將/dev/sdb1的數據移動到/dev/sdc1上
/dev/sdb1: Moved: 41.7%
/dev/sdb1: Moved: 84.2%
/dev/sdb1: Moved: 100.0%
備註:此時可使用pvscan查看變化

3)將舊的物理卷從卷組中卸載
[[email protected] ~]# vgreduce vg01 /dev/sdb1 #使用vgreduce將/dev/sdb1從卷組vg01中分離
Removed "/dev/sdb1" from volume group "vg01"
[[email protected] ~]# pvremove /dev/sdb1 #要能夠取下物理磁碟維修,則需要刪除此物理卷;若物理磁碟劃分成了多個物理卷,則需要將所有物理卷都刪除才可以.
Labels on physical volume "/dev/sdb1" successfully wiped

八、當有一天需要將整個LVM的磁碟遷移到另外一台計算機中,則需要按以下步驟:
1)在原先的計算機上導出卷組
[[email protected] ~]#umount /dev/vg01/data #導出卷組前要先卸載該卷組中所有邏輯卷
[[email protected] ~]#vgchange -a n vg01 #使用vgchange將卷組修改為非有效(inactive)配置
[[email protected] ~]#vgexport vg01 #使用vgexport導出卷組
2)將LVM磁碟安裝到目的計算機上

3)在目的計算機上導入卷組


[[email protected] ~]#pvscan #使用pvscan掃描所有物理卷,以便讓linux可以驅動這些物理卷
[[email protected] ~]#vgimport vg01 #導入卷組
[[email protected] ~]#vgchange -a y vg01 #將卷組修改為有效(active)配置

4)掛載邏輯卷
[[email protected] ~]#mount /devv/vg01/data /quota
#將邏輯卷掛載到文件系統

九、 以上是對新增磁碟進行LVM劃分.而通常在安裝系統時就採用了LVM劃分磁碟,按需調整磁碟空間.當有一天,你發現某個文件系統空間不夠時,此時需要擴容.
比如說:現在有需求要將weblogic92安裝在 /weblogic下.而 / 空間不夠.這樣就需要將新增的邏輯卷掛載到/weblogic就OK了.

1)使用df查看各文件系統大小
[[email protected] /]# df -hT #查看各文件系統大小
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vg00-LogVol00
ext3 3.0G 2.0G 820M 71% /
/dev/mapper/vg00-lvopt
ext3 3.0G 69M 2.7G 3% /opt
/dev/mapper/vg00-lvusr
ext3 6.8G 4.3G 2.2G 67% /usr
/dev/mapper/vg00-lvhome
ext3 3.0G 75M 2.7G 3% /home
/dev/mapper/vg00-lvpublic
ext3 20G 1.8G 17G 10% /public
/dev/mapper/vg00-lvtmp
ext3 3.0G 70M 2.7G 3% /tmp
/dev/mapper/vg00-lvvar


ext3 3.0G 177M 2.6G 7% /var
/dev/sda1 ext3 99M 20M 75M 21% /boot
tmpfs tmpfs 7.9G 0 7.9G 0% /dev/shm
2)使用lvscan查看可知道卷組名字為vg00
[[email protected] /]# lvscan #使用lvscan查看
ACTIVE '/dev/vg00/LogVol00' [3.00 GB] inherit
ACTIVE '/dev/vg00/lvopt' [3.00 GB] inherit
ACTIVE '/dev/vg00/lvusr' [7.00 GB] inherit
ACTIVE '/dev/vg00/lvhome' [3.00 GB] inherit
ACTIVE '/dev/vg00/lvpublic' [20.00 GB] inherit
ACTIVE '/dev/vg00/lvtmp' [3.00 GB] inherit
ACTIVE '/dev/vg00/lvvar' [3.00 GB] inherit
ACTIVE '/dev/vg00/LogVol01' [17.62 GB] inherit

3) [[email protected] /]#lvcreate -L 20G -n lvweblogic vg00 #從卷組vg00上劃分20G的空間為邏輯卷lvweblogic
Logical volume "lvweblogic" created

4)對劃分的邏輯卷進行格式化
[[email protected] /]# mkfs -t ext3 /dev/vg00/lvweblogic #以ext3的文件格式化邏輯卷

備註:注意,接下來是掛載,為了使開機自動掛載,需要修改/etc/fstab,可參照上面邏輯卷創建方法.


十、邏輯卷創建及刪除推薦步驟

創建邏輯卷(LV)的順序:Linux分區---物理卷(PV)---卷組(VG)---邏輯卷(LV)---掛載到文件系統
刪除邏輯卷(LV)的順序:卸載文件系統----邏輯卷(LV)---卷組(VG)----物理卷(PV)---Linux分區

本文出自 「唐光輝」 博客,請務必保留此出處http://firefly222.blog.51cto.com/899793/413515


[火星人 ] LVM---動態調整磁碟容量已經有1186次圍觀

http://coctec.com/docs/linux/show-post-49590.html