LVM邏輯卷管理(Logical Volume Manager)node
隨着數據的逐漸增長,當初設計的磁盤大小已經知足不了如今的需求了,當磁盤面臨着滿盤時,運維人員如何解決問題?是換一個更大的磁盤,把原先的數據copy到如今更大的磁盤裏面,繼續撐一段時間,仍是尋找其它方法?LVM的出現很好的解決了這一問題。LVM是linux環境下對磁盤分區進行管理的一種機制,是創建在硬盤和分區之上的一個邏輯層,來提升磁盤分區管理的靈活性。linux
LVM有兩個特性bash
一、便捷的調整文件系統大小app
二、支持快照snapshot運維
建立LVM的示意圖ide
PV能夠是硬盤的分區,PV組成了VG,在VG上面割取PE就組成了LVspa
物理卷physical volume (PV)設計
物理卷是硬盤分區,也能夠是整個硬盤或已建立的Software RAID 卷。3d
卷組volume group (VG)code
卷組是LVM 中最高抽象層,是由一個或多個物理卷所組成的存儲器池。
物理塊physical extent (PE)
物理卷按大小相等的「塊」爲單位存儲,塊的大小與卷組中邏輯卷塊的大小相同。
邏輯塊logical extent (LE)
邏輯卷按「塊」爲單位存儲,在一卷組中的全部邏輯卷的塊大小是相同的。
邏輯卷logical volume (LV)
邏輯卷至關於非LVM 系統中的分區,它在卷組上創建,是一個標準的塊設備,能夠在其上創建文件系統
從一塊硬盤到能使用LV文件系統的步驟:
硬盤----分區----PV----VG----LV----格式化(LV爲ext文件系統)----掛載
1、LVM的第一個特性,便捷的調整文件系統大小
一、PV的相關操做
先對硬盤進行分區
pvcreate 建立PV
[root@Redhat6 ~]# pvcreate /dev/sda5 /dev/sda6 Physical volume "/dev/sda5" successfully created Physical volume "/dev/sda6" successfully created
pvromve 移除PV
[root@Redhat6 ~]# pvremove /dev/sda5 Labels on physical volume "/dev/sda5" successfully wiped
pvdisplay 顯示PV的詳細信息,用pvs也能夠顯示pv的信息
[root@Redhat6 ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg0 lvm2 a-- 59.99g 7.99g /dev/sda5 lvm2 a-- 10.00g 10.00g /dev/sda6 lvm2 a-- 10.00g 10.00g
pvmove 移動pv,將此PV的數據轉移至其它PV
[root@Redhat6 ~]# pvmove /dev/sda5 /dev/sda6 #把sda5的數據轉移到sda6上面 /dev/sda5: Moved: 100.0%
二、VG的相關操做
vgcreate 建立vg,-s PE:指定PE大小,默認爲4M
[root@Redhat6 ~]# vgcreate myvg /dev/sda5 /dev/sda6 Volume group "myvg" successfully created
vgdisplay 顯示VG的詳細信息,用vgs也能夠顯示VG的信息
[root@Redhat6 ~]# vgdisplay --- Volume group --- VG Name myvg System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 2 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 3 Act PV 3 VG Size 25.01 GiB PE Size 4.00 MiB Total PE 6402 Alloc PE / Size 0 / 0 Free PE / Size 6402 / 25.01 GiB VG UUID cLkCtE-LB7X-SYCE-GcKF-I30f-pBYH-MTCLZC
vgremove 移除vg,只需跟vg的名稱便可
[root@Redhat6 ~]# vgremove myvg Volume group "myvg" successfully removed
vgextend vg的擴展,先必須添加其它的PV
首先準備好一個PV--->而後使用vgextend命令便可完成擴展
[root@Redhat6 ~]# pvcreate /dev/sda5 /dev/sda6 #建立PV Physical volume "/dev/sda5" successfully created Physical volume "/dev/sda6" successfully created [root@Redhat6 ~]# vgcreate myvg /dev/sda5 /dev/sda6 #建立VG Volume group "myvg" successfully created [root@Redhat6 ~]# pvcreate /dev/sda3 #把另一個分區建立爲PV Physical volume "/dev/sda3" successfully created [root@Redhat6 ~]# vgextend myvg /dev/sda3 #在myvg組裏面擴展VG Volume group "myvg" successfully extended
vgreduce vg的縮減,
肯定要移除的PV--->將此PV的數據轉移至其它PV---->從卷組中將此PV移除
[root@Redhat6 ~]# pvs #查看PV,肯定要移除哪一個PV PV VG Fmt Attr PSize PFree /dev/sda2 vg0 lvm2 a-- 59.99g 7.99g /dev/sda3 myvg lvm2 a-- 5.01g 5.01g /dev/sda5 myvg lvm2 a-- 10.00g 3.00g /dev/sda6 myvg lvm2 a-- 10.00g 10.00g [root@Redhat6 ~]# pvmove /dev/sda3 /dev/sda5 #把sda3的數據轉移到sda5上面 /dev/sda3: Moved: 100.0% [root@Redhat6 ~]# vgreduce myvg /dev/sda3 #從VG中把PV移除 Removed "/dev/sda3" from volume group "myvg" [root@Redhat6 ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg0 lvm2 a-- 59.99g 7.99g /dev/sda3 lvm2 a-- 5.01g 5.01g /dev/sda5 myvg lvm2 a-- 10.00g 3.00g /dev/sda6 myvg lvm2 a-- 10.00g 10.00g [root@Redhat6 ~]#
三、LV的相關操做
lvcreate 建立LV,相關參數:-l 指定PE個數,-L指定LV的大小,
-s建立一個快照卷,-n LV的名稱
[root@Redhat6 ~]# lvcreate -L 5G -n lv1 myvg Logical volume "lv1" created
lvdisplay 顯示LV的詳細信息,用lvs也能夠顯示LV的信息
[root@Redhat6 ~]# lvdisplay --- Logical volume --- LV Path /dev/myvg/lv1 LV Name lv1 VG Name myvg LV UUID 7Ewvyg-pEFI-o4XL-kFDa-Jtvl-hmak-5fg3qT LV Write Access read/write LV Creation host, time Redhat6, 2013-07-23 05:42:33 +0800 LV Status available # open 0 LV Size 5.00 GiB #建立一個5G的lv Current LE 1280 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:4
lvremove 移除lv,須要移除lv的設備文件:
/dev/myvg/lv1或者/dev/mapper/myvg-lv1
[root@Redhat6 ~]# lvremove /dev/myvg/lv1 Do you really want to remove active logical volume lv1? [y/n]: y Logical volume "lv1" successfully removed
lvextend 擴展LV
先肯定要擴展多大-->肯定當前VG有足夠的空閒空間-->擴展(一、物理邊界---二、邏輯邊界)
當VG不夠時,先增長PV,在擴展VG,最後在VG上面擴展LV。
[root@Redhat6 ~]# mke2fs -t ext4 /dev/myvg/lv1 #先把lv格式化 [root@Redhat6 ~]# lvextend -L +4G /dev/myvg/lv1 #實現物理擴容 Extending logical volume lv1 to 9.00 GiB #以前只有5G,如今擴了4G Logical volume lv1 successfully resized [root@Redhat6 ~]# resize2fs /dev/myvg/lv1 #實現邏輯擴容 resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/myvg/lv1 to 2359296 (4k) blocks. The filesystem on /dev/myvg/lv1 is now 2359296 blocks long.
lvreduce 縮小LV
先卸載--->而後減少邏輯邊界---->最後減少物理邊界--->在檢測文件系統 ====謹慎用===
[root@Redhat6 ~]# umount /dev/mapper/myvg-lv1 [root@Redhat6 ~]# e2fsck -f /dev/myvg/lv1 e2fsck 1.41.12 (17-May-2010) 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/myvg/lv1: 13/589824 files (0.0% non-contiguous), 73829/2359296 blocks [root@Redhat6 ~]# resize2fs /dev/myvg/lv1 7G #把lv減小到7G,減小了2G resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/myvg/lv1 to 524288 (4k) blocks. The filesystem on /dev/myvg/lv1 is now 524288 blocks long. [root@Redhat6 ~]# lvs #查看lv的大小發現並無實際的減小 LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert lv1 myvg -wi-a---- 9.00g root vg0 -wi-ao--- 20.00g swap vg0 -wi-ao--- 2.00g usr vg0 -wi-ao--- 10.00g var vg0 -wi-ao--- 20.00g [root@Redhat6 ~]# lvreduce -L -2G /dev/myvg/lv1 #減小lv的物理邊界,指定減小2G WARNING: Reducing active logical volume to 7.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce lv1? [y/n]: y Reducing logical volume lv1 to 7.00 GiB Logical volume lv1 successfully resized [root@Redhat6 ~]# lvs #查看lv的大小發現已經實際的減小了 LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert lv1 myvg -wi-a---- 7.00g root vg0 -wi-ao--- 20.00g swap vg0 -wi-ao--- 2.00g usr vg0 -wi-ao--- 10.00g var vg0 -wi-ao--- 20.00g [root@Redhat6 ~]# e2fsck -f /dev/myvg/lv1 #減小以後檢查文件系統 e2fsck 1.41.12 (17-May-2010) 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/myvg/lv1: 13/131072 files (0.0% non-contiguous), 43698/524288 blocks
2、LVM的第二個特性,在備份數據的時候創鍵快照卷
當須要備份LV裏面的數據時,且LV的數據也在進行寫操做時,這個時候建立快照就頗有必要了,快照是一個訪問LV裏面數據的一個通道,他不會保存LV裏面全部的數據,當LV某些PE裏面數據變化時,快照保存以前PE的數據,未變化的PE將不會被保存。
步驟:
分區--->建立PV--->建立VG--->建立LV--->格式化LV爲ext4--->掛載--->寫入數據--->須要備份(建立快照)--->建立快照卷--->再次繼續寫入數據---->掛載快照卷--->備份以前的數據到快照卷裏面--->卸載快照--->刪除快照卷
root@station128 ~]# fdisk /dev/sda #先創建兩個邏輯分區,大小爲10G Command (m for help): n First cylinder (9165-15665, default 9165): Using default value 9165 Last cylinder, +cylinders or +size{K,M,G} (9165-15665, default 15665): +10G Command (m for help): t Partition number (1-5): 5 Hex code (type L to list codes): 8e #並改成LVM文件系統 Changed system type of partition 5 to 8e (Linux LVM) Command (m for help): n First cylinder (10471-15665, default 10471): Using default value 10471 Last cylinder, +cylinders or +size{K,M,G} (10471-15665, default 15665): +10G Command (m for help): t Partition number (1-6): 6 Hex code (type L to list codes): 8e Changed system type of partition 6 to 8e (Linux LVM) Command (m for help): w [root@Redhat6 ~]# pvcreate /dev/sda5 /dev/sda6 #建立PV Physical volume "/dev/sda5" successfully created Physical volume "/dev/sda6" successfully created [root@Redhat6 ~]# vgcreate -s 8M myvg /dev/sda5 /dev/sda6 #建立VG,並指定PE爲8M Volume group "myvg" successfully created [root@Redhat6 ~]# lvcreate -L 4G -n lv1 myvg #建立LV爲4G Logical volume "lv1" created [root@Redhat6 ~]# mkfs -t ext4 /dev/myvg/lv1 #格式化LV爲ext4 [root@Redhat6 ~]# blkid #顯示設備的UUID及文件系統類型,及卷標 /dev/mapper/vg0-var: UUID="c9776f08-bbaf-4a36-a009-3e16a09ad924" TYPE="ext4" /dev/sda1: UUID="dc3cc214-a9f6-4730-a186-d08765ccfa4f" TYPE="ext4" /dev/sda2: UUID="zPpbgE-0Ip7-A4m2-GG4K-sBrC-eUeU-V8VdCI" TYPE="LVM2_member" /dev/sda5: UUID="e5eLw1-JTrG-H9Q7-18qA-SrPV-axMc-QLPW5M" TYPE="LVM2_member" /dev/sda6: UUID="7OOZcJ-XzLf-zJJq-JbdF-e386-xCTl-Ym2UE3" TYPE="LVM2_member" /dev/mapper/vg0-root: UUID="bae7835a-af3b-4935-a04b-5932b2fc123b" TYPE="ext4" /dev/mapper/vg0-swap: UUID="3e0cb4a7-d393-4f75-af12-1fe6e8c96273" TYPE="swap" /dev/mapper/vg0-usr: UUID="46d516b8-03ea-4c0d-bb03-0e9d6893bd1a" TYPE="ext4" /dev/mapper/myvg-lv1: UUID="2d44718c-993d-42e6-896b-05e06af45aee" TYPE="ext4" [root@Redhat6 ~]# mkdir /mnt/lv1 #建立LV的掛載點 [root@Redhat6 ~]# mount /dev/myvg/lv1 /mnt/lv1/ #掛載LV [root@Redhat6 ~]# cd /mnt/lv1/ #進入到LV的掛載點 [root@Redhat6 lv1]# ls lost+found [root@Redhat6 lv1]# cp /etc/rc.sysinit /etc/inittab ./ #copy一個文件到LV的掛載目錄裏面 [root@Redhat6 lv1]# ls inittab lost+found rc.sysinit [root@Redhat6 lv1]# lvcreate -L 50M -s -p r -n MY_SNAP /dev/myvg/lv1 #建立快照卷爲50M,且只能讀,名稱爲MY_SNAP Rounding up size to full physical extent 56.00 MiB Logical volume "MY_SNAP" created [root@Redhat6 lv1]# mkdir /mnt/snap #建立快照卷的掛載點 [root@Redhat6 lv1]# mount /dev/myvg/MY_SNAP /mnt/snap/ #掛載快照卷 mount: block device /dev/mapper/myvg-MY_SNAP is write-protected, mounting read-only [root@Redhat6 lv1]# mkdir /backup #建立備份目錄 [root@Redhat6 lv1]# tar -jcf /backup/lv1.tar.bz2 /mnt/snap/* #把快照卷裏面的數據歸檔 tar: Removing leading `/' from member names [root@Redhat6 lv1]# cp /etc/fstab /mnt/lv1/ #copy文件到LV文件,使LV文件與以前快照的文件有所改變 [root@Redhat6 lv1]# umount /dev/myvg/MY_SNAP #備份完成後,卸載快照卷的掛載點 [root@Redhat6 lv1]# lvremove /dev/myvg/MY_SNAP #移除快照卷 Do you really want to remove active logical volume MY_SNAP? [y/n]: y Logical volume "MY_SNAP" successfully removed [root@Redhat6 lv1]# cd /mnt/lv1/ [root@Redhat6 lv1]# ls #能夠查看到LV文件裏面有3個文件 fstab inittab lost+found rc.sysinit [root@Redhat6 lv1]# cd /backup/ [root@Redhat6 backup]# tar xf lv1.tar.bz2 #解壓以前備份的文件 [root@Redhat6 backup]# ls lv1.tar.bz2 mnt [root@Redhat6 backup]# cd mnt/lv1/ [root@Redhat6 lv1]# ls #查看到快照只備份了以前的文件 inittab lost+found rc.sysinit [root@Redhat6 lv1]#