CentOS快速建立邏輯卷,簡單快照

來源:互聯網
上載者:User

CentOS快速建立邏輯卷,簡單快照

今天就寫一篇關於lvm 邏輯卷的文章,本人對於lvm知道的也不是很多,只是根據想要實現的功能,用到了lvm快照。

LVM介紹:

LVM(Logical Volume Manager)即邏輯卷管理器,它最先是在Linux 2.4核心中被整合到核心中去的,它的出現改變了傳統的磁碟空間管理理念。以往在安裝作業系統時需要規劃好分區大小,即使利用了RAID技術也要規劃好每個分區的大小,因為一旦分好區後要改變其大小是非常困難的事情。在Windows下有大家熟悉的Partition Magic工具可以用來調整分區大小,但它有一個缺點是要麼在調整前要關閉系統或調整後重啟系統。

這在普通PC機上使用還行,要在提供不間斷服務的伺服器上使用就會造成服務中斷,不過這個問題在Linux下隨著LV技術的出現一切都得到解決,LV可以在不用重啟系統的情況動態增加可用空間大小,不過前提得是使用熱插拔硬碟,或事先將硬碟裝入而不使用。本文我將使用名詞LV(可不是LV包)而不是LVM,因為LV才是主角。

首先我們要弄清楚LV領取的幾個關鍵名詞概念:

物理介質:就是我們通常說的硬碟,簡稱pm
物理卷:就是我們通常說的硬碟分區,簡稱pv
物理分區:它是由LVM命令在物理卷上建立的最小LVM定址單元,在建立物理卷時可以指定物理分區的大小,預設值是4M,簡稱pe
卷組:由一個多多個物理卷組成的邏輯單元,簡稱vg
邏輯分區:它與物理分區一一對應,大小相等,簡稱le
邏輯卷:從卷組中分配一定大小的空間建立的邏輯單元,也可以使用一個完整的卷組空間大小來建立一個邏輯卷,簡稱lv

(lvm 介紹摘抄與 51cto上)

1,建立物理卷PV

首先在2塊磁碟上建立分區,這個分區會轉成物理卷PV

123456789101112131415161718192021222324
[root@xyly ~]# fdisk -lDisk /dev/sda: 42.9 GB, 42949672960 bytes255 heads, 63 sectors/track, 5221 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesDevice Boot      Start         End      Blocks   Id  System/dev/sda1   *           1          13      104391   83  Linux/dev/sda2              14        1288    10241437+  83  Linux/dev/sda3            1289        1925     5116702+  83  Linux/dev/sda4            1926        5221    26475120    5  Extended/dev/sda5            1926        2056     1052226   82  Linux swap / Solaris/dev/sda6            2057        5221    25422831   83  LinuxDisk /dev/sdb: 10.7 GB, 10737418240 bytes255 heads, 63 sectors/track, 1305 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesDevice Boot      Start         End      Blocks   Id  System/dev/sdb1               1        1305    10482381   83  LinuxDisk /dev/sdc: 10.7 GB, 10737418240 bytes255 heads, 63 sectors/track, 1305 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytes

以上是我格式化好的2個 /dev/sdb1 和 /dev/sdbc1.

2,把分區轉成物理卷pv

12345
[root@xyly ~]# pvcreate /dev/sdb1Physical volume "/dev/sdb1" successfully created[root@xyly ~]# pvcreate /dev/sdc1Physical volume "/dev/sdc1" successfully created

使用pvdisplay 查看系統所有的pv資訊 或者 指定的單個

12345678910111213141516171819202122
[root@xyly ~]# pvdisplay--- Physical volume ---PV Name /dev/sdb1VG Name nyvg1PV Size 10.00 GB / not usable 717.00 KBAllocatable yesPE Size (KByte) 4096Total PE 2559Free PE 2506Allocated PE 53PV UUID W23uI8-kZ46-4W3y-L0tW-Za22-nMeG-YbLeoD--- Physical volume ---PV Name /dev/sdc1VG Name nyvg1PV Size 10.00 GB / not usable 717.00 KBAllocatable yesPE Size (KByte) 4096Total PE 2559Free PE 2521Allocated PE 38PV UUID Mzv5fV-xret-4IyG-s2fC-py33-mvCW-iBVF0Z

3,建立VG卷組

12345678910111213141516171819202122232425
[root@xyly ~]#[root@xyly ~]# vgcreate lvm001 /dev/sd[bc]1 #講/dev/sdb1和/dev/sdbc1 加入到卷組lvm001 中/dev/cdrom: open failed: Read-only file systemVolume group "lvm001" successfully created[root@xyly ~]# vgdisplay #使用vgdisplay 查看卷組--- Volume group ---VG Name lvm001System IDFormat lvm2Metadata Areas 2Metadata Sequence No 1VG Access read/writeVG Status resizableMAX LV 0Cur LV 0Open LV 0Max PV 0Cur PV 2Act PV 2VG Size 19.99 GBPE Size 4.00 MB #預設的PE大小為4MTotal PE 5118Alloc PE / Size 0 / 0Free PE / Size 5118 / 19.99 GB #一共5118個pe,未使用的。VG UUID oKhjBB-L9At-I2KC-Zrd3-fR2g-QoTe-3x0AST

4,建立邏輯卷LV

123456789101112131415161718192021222324252627282930313233343536
[root@xyly ~]# lvcreate -L 1000 -n test1 lvm001 #-L 分配給lv的空間大小。 -n是建立邏輯卷的名字Logical volume "test1" created #建立成功[root@xyly ~]# lvcreate -L 1000M -n test2 lvm001 #-lL 分配給lv空間的大小Logical volume "test2" created[root@xyly ~]# lvdisplay #查看LV的資訊--- Logical volume ---LV Name /dev/lvm001/test1VG Name lvm001LV UUID ZiqMqR-XqD6-tNuv-dnv1-6a1C-PN0n-ZmRP2TLV Write Access read/writeLV Status available# open 0LV Size 1000.00 MBCurrent LE 250Segments 1Allocation inheritRead ahead sectors auto- currently set to 256Block device 253:0--- Logical volume ---LV Name /dev/lvm001/test2VG Name lvm001LV UUID oDWKde-dLeM-HsEi-CHOr-b9TF-SH3J-Gh6esLLV Write Access read/writeLV Status available# open 0LV Size 1000.00 MBCurrent LE 250Segments 1Allocation inheritRead ahead sectors auto- currently set to 256Block device 253:1

把這2個LV 格式化為ext3檔案系統

123
[root@xyly ~]# mkfs -t ext3 /dev/lvm001/test1或者[root@xyly ~]# mkfs.ext3 /dev/lvm001/test2

都可以格式化為ext3的檔案系統

格式化完畢。現在要掛載

12345678910
[root@xyly ~]# mkdir /lvm /lvm1 #建立掛載點[root@xyly ~]# mount /dev/lvm001/test1 /lvm[root@xyly ~]# mount /dev/lvm001/test2 /lvm1[root@xyly ~]# df -hFilesystem Size Used Avail Use% Mounted on/dev/mapper/lvm001-test1985M 18M 918M 2% /lvm/dev/mapper/lvm001-test2985M 18M 918M 2% /lvm1

掛載邏輯卷完畢。

5,建立lvm快照。

123456789101112131415161718192021222324252627282930313233343536373839404142434445
[root@xyly ~]# lvcreate -L 100M -s -n test1_snap /dev/lvm001/test1 #-L表示快照的大小。-s表示建立快照 -n 是快照的名字Logical volume "test1_snap" created[root@xyly ~]# lvscanACTIVE Original '/dev/lvm001/test1' [1000.00 MB] inheritACTIVE '/dev/lvm001/test2' [1000.00 MB] inheritACTIVE Snapshot '/dev/lvm001/test1_snap' [100.00 MB] inherit[root@xyly ~]# lvdisplay /dev/lvm001/test1*--- Logical volume ---LV Name /dev/lvm001/test1VG Name lvm001LV UUID ZiqMqR-XqD6-tNuv-dnv1-6a1C-PN0n-ZmRP2TLV Write Access read/writeLV snapshot status source of#lvm快照源/dev/lvm001/test1_snap [active]LV Status available# open 1LV Size 1000.00 MB #lvm 大小Current LE 250Segments 1Allocation inheritRead ahead sectors auto- currently set to 256Block device 253:0--- Logical volume ---LV Name /dev/lvm001/test1_snapVG Name lvm001LV UUID 9mapX2-7OD2-emXG-rG37-XLj5-hcAl-DmanQvLV Write Access read/write #是否可寫LV snapshot status active destination for /dev/lvm001/test1 #處於活動狀態,鏡像的路徑LV Status available# open 0LV Size 1000.00 MBCurrent LE 250COW-table size 100.00 MBCOW-table LE 25Allocated to snapshot 0.01% #快照使用的百分比Snapshot chunk size 4.00 KBSegments 1Allocation inheritRead ahead sectors auto- currently set to 256Block device 253:2

快照建立完成,下邊示範快照的作用。

12345678910111213141516171819
[root@xyly ~]# mount /dev/lvm001/test2 /lvm1 #掛載test2 到/lvm1 (前邊用的是test1)[root@xyly ~]# ls /lvm1#查看/lvm1 目錄下的內容,現在只有一個檔案夾lost+found[root@xyly ~]# touch /lvm1/test{1,2,3,4,5,6} #在/lvm1目錄下建立測試檔案[root@xyly ~]# ls /lvm1lost+found test1 test2 test3 test4 test5 test6#建立的檔案[root@xyly ~]# mkdir /snap #建立快照掛載點[root@xyly ~]# ls /snap/ #現在/snap目錄為空白[root@xyly ~]# lvcreate -L 50M -s -n test_snap2 /dev/lvm001/test2  #建立快照Rounding up size to full physical extent 52.00 MBLogical volume "test_snap2" created[root@xyly ~]# mount /dev/lvm001/test_snap2 /snap/ #掛載快照[root@xyly ~]# ls /snap/ #查看快照的內容lost+found test1 test2 test3 test4 test5 test6[root@xyly ~]# umount /snap/ #卸載快照[root@xyly ~]# ls /snap/[root@xyly ~]# lvremove /dev/lvm001/test_snap2 #移除快照Do you really want to remove active logical volume test_snap2? [y/n]: yLogical volume "test_snap2" successfully removed

移除快照成功! 到此就算有一個段落。

從網上搜尋的資料,看到lvm快照還有其他的應用,用LVM快照的其他應用:

1),虛擬化

2),資料庫

3),資料回溯

其他的就不一一列舉了。LVM的刪除,我在這裡也寫一下吧 ,有建立就有刪除

1234567
[root@xyly ~]# umount /lvm #首先要卸載掛載目錄,否則刪除不了[root@xyly ~]# lvremove /dev/lvm001/test1 #使用lvremove命令可以刪除卷組裡的LVM邏輯卷Do you really want to remove active logical volume test1? [y/n]: yLogical volume "test1" successfully removed[root@xyly ~]# vgremove lvm001 #使用vgremove命令可以刪除現有LVM卷組Volume group "lvm001" successfully removed[root@xyly ~]# vgdisplay #查看卷組。

關於lvm 更多的介紹我就不再一一說了,如果想看這方面更多的資訊,請參考lvm指南(http://tldp.org/HOWTO/LVM-HOWTO/index.html)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.