Linux LVM學習筆記--基礎
Table of Contents
基礎
LVM, Logical Volume Management,是Linux的一項儲存裝置管理技術,它將物理磁碟或者分割槽標識成物理卷(Physics Volume),然後統一放到一個儲存資源池或者卷組(Volume Group)中,然後從資源池中劃分出一個個邏輯卷(Logic Volume)提供給應用使用,由於邏輯卷的使用非常靈活,可以將多個小的disk合併成一個大的邏輯卷給作業系統使用,可以按需建立固定size的邏輯卷,之後也可以對其進行擴容或者縮減size,還支援快照、條帶化和映象等功能,所以這項技術使我們對儲存資源的使用變得更加方便。
邏輯結構
物理卷、卷組、邏輯卷這些概念都是LVM抽象出來的,下面的圖大概描述了它們之間的關係。
結合上面這張圖,先對LVM的重要概念描述一下:
Physics Volume
Physics Volume就是對physics/virtual disk或者partition這些塊裝置的抽象,用pvcreate將這些disk和partition建立成Physics Volume時,LVM會寫入LVM header到這些裝置中,以此來標識它們已經準備好被新增到一個Volume Group中,disk和partition沒法直接新增到Volume Group中。下面這張圖來自RehHat6官方文件,大致描述了Physics Volume的layout。
note:如果使用partition來建立PV,那麼partition type則必須是Linux LVM(8e),可以用fdisk來查詢並修改partition type。
Logic Volume Group
Logic Volume Group由一個或者多個Physics Volume組成,用vgcreate將這些Physics Volume新增到Volume Group時,Physics Volume將會被分割成一個個的PE(Physics Extend)。所以說建立Logic Volume Group的過程就是將所有Physics Volume劃分成固定尺寸的PE,然後用所有PE組成一個大的儲存資源池,就叫作Volume Group,下面這個圖描述的比較清晰。
Logic Volume
Logic Volume是真正給使用者和應用程式使用的,它從Volume Group中劃分出來,在功能上等價於物理磁碟上的分割槽,格式化並建立好檔案系統後,就可以mount到使用者目錄下使用了。
Extends
Group中的每個Volume都被分割成多個固定size的小塊,這個塊就叫做Extend,Extend的size由Volume Group決定,在建立Volume Group時可以用-s來指定extend的size,預設大小是4M,Extend number = PV size / Extend size。
Extend在Physics Volume裡面叫做Physics Extend(PE),在Logic Volume中叫做Logic Extend(LE),PE是Physics Volume中的最小儲存單元,類似於檔案系統中的block。Logic Volume其實就是LVM維護的PE和LE之間的對映關係,一個Logic Volume作為一個統一的裝置提供給使用者使用,但是其中的LE並不需要對映到連續的PE。
Logic Volume還可以通過簡單地向volume中新增extend或從卷中刪除extend來輕鬆地進行擴充套件或收縮。理解了Extend,也就大概明白Logic Volume是如何做到彈性分配的了。LVM可以複製和重新組織組成Logic Volume的Physics Extend,而不會對使用者造成任何中斷。
Demo
環境資訊
[email protected]:/home/hunk# uname -a
Linux hunk-virtual-machine 4.4.0-21-generic #37-Ubuntu SMP Mon Apr 18 18:33:37 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
[email protected]:/home/hunk# lvm version
LVM version: 2.02.133(2) (2015-10-30)
Library version: 1.02.110 (2015-10-30)
Driver version: 4.34.0
環境是一個虛擬機器,我添加了一個200GB的virtual disk(/dev/sdb)來做demo。
建立PV
[email protected]:/home/hunk# pvcreate /dev/sdb
Physical volume "/dev/sdb" successfully created
[email protected]:/home/hunk# pvdisplay /dev/sdb
"/dev/sdb" is a new physical volume of "200.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb
VG Name
PV Size 200.00 GiB
Allocatable NO #目前還不是可分配的
PE Size 0 #注意建立PV時並未將其分割成一個個的PE
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 67c4NF-4MBI-JrvI-u2AP-RmFS-vyC0-SsVa3B
#此處demo一下移除PV,可以看出建立PV時,LVM往disk或者partition寫入的header就是一個標籤,移除PV就是擦除這個標籤
[email protected]:/home/hunk# pvremove /dev/sdb
Labels on physical volume "/dev/sdb" successfully wiped
[email protected]:/home/hunk# pvcreate /dev/sdb1 #用partition建立PV,因為partition不是Linux LVM無法建立
Device /dev/sdb1 not found (or ignored by filtering).
[email protected]:/home/hunk# fdisk /dev/sdb #修改partition type
Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): t
Selected partition 1
Partition type (type L to list all types): 8e
Changed type of partition 'Linux extended' to 'Linux LVM'.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
[email protected]:/home/hunk# pvcreate /dev/sdb1
WARNING: dos signature detected on /dev/sdb1 at offset 510. Wipe it? [y/n]: y
Wiping dos signature on /dev/sdb1.
Physical volume "/dev/sdb1" successfully created
建立VG
[email protected]:/home/hunk# vgcreate VolGroup /dev/sdb #可以跟多個PV
Volume group "VolGroup" successfully created
[email protected]:/home/hunk# vgs
VG #PV #LV #SN Attr VSize VFree
VolGroup 1 0 0 wz--n- 200.00g 200.00g
ubuntu-vg 1 2 0 wz--n- 99.52g 0
[email protected]:/home/hunk# vgdisplay VolGroup
--- Volume group ---
VG Name VolGroup
System ID
Format lvm2
Metadata Areas 1
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 1
Act PV 1
VG Size 200.00 GiB
PE Size 4.00 MiB
Total PE 51199
Alloc PE / Size 0 / 0
Free PE / Size 51199 / 200.00 GiB
VG UUID cXEzL9-U5jr-GwkA-13UC-L6Zf-boyH-B4YC2H
建立好VG後我們再檢視一下加入的PV的狀態
[email protected]:/home/hunk# pvdisplay /dev/sdb
--- Physical volume ---
PV Name /dev/sdb
VG Name VolGroup
PV Size 200.00 GiB / not usable 4.00 MiB
Allocatable yes #加入到vg後,因為已經劃分成了PE,此時PV就是可分配的了
PE Size 4.00 MiB #預設大小
Total PE 51199 #PE的個數 = PV size / PE size
Free PE 51199
Allocated PE 0
PV UUID 67c4NF-4MBI-JrvI-u2AP-RmFS-vyC0-SsVa3B
建立LV
[email protected]:/home/hunk# lvcreate -L 20G -n sharevolume VolGroup
Logical volume "sharevolume" created.
[email protected]:/home/hunk# ll /dev/VolGroup/sharevolume
lrwxrwxrwx 1 root root 7 11月 26 11:51 /dev/VolGroup/sharevolume -> ../dm-2
[email protected]:/home/hunk# ll /dev/mapper/VolGroup-sharevolume
lrwxrwxrwx 1 root root 7 11月 26 11:54 /dev/mapper/VolGroup-sharevolume -> ../dm-2
[email protected]:/home/hunk# lvdisplay /dev/VolGroup/sharevolume
--- Logical volume ---
LV Path /dev/VolGroup/sharevolume
LV Name sharevolume
VG Name VolGroup
LV UUID ShfdSY-EHIu-9qz9-PHNc-bjEI-t5Ul-009Vn9
LV Write Access read/write
LV Creation host, time hunk-virtual-machine, 2018-11-26 11:51:39 +0800
LV Status available
# open 1
LV Size 20.00 GiB
Current LE 5120
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:2
[email protected]:/home/hunk# vgs -o +lv_size,lv_name
VG #PV #LV #SN Attr VSize VFree LSize LV
VolGroup 1 1 0 wz--n- 200.00g 180.00g 20.00g sharevolume
ubuntu-vg 1 2 0 wz--n- 99.52g 0 91.52g root
ubuntu-vg 1 2 0 wz--n- 99.52g 0 8.00g swap_1
建立FS
[email protected]:/home/hunk# mkfs.ext4 /dev/VolGroup/sharevolume
mke2fs 1.42.13 (17-May-2015)
Creating filesystem with 5242880 4k blocks and 1310720 inodes
Filesystem UUID: 7687197e-882e-4af2-8e1a-c583ca46e5fc
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
Mount
[email protected]:/home/hunk# mount /dev/VolGroup/sharevolume /home/hunk/share/
[email protected]:/home/hunk# cat /etc/fstab
# <file system> <mount point> <type> <options> <dump> <pass>
/dev/mapper/ubuntu--vg-root / ext4 errors=remount-ro 0 1
# /boot was on /dev/sda1 during installation
UUID=22e537a9-8f81-422e-ade1-f8fe4b2516ad /boot ext2 defaults 0 2
/dev/mapper/ubuntu--vg-swap_1 none swap sw 0 0
/dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8 0 0
/dev/mapper/VolGroup-sharevolume /home/hunk/share ext4 defaults,nofail 0 0
調整LV大小
擴容或者縮減容量實際上就是新增或者移除底層對映的PE。
注:當LV有快照時是不能擴充套件和縮減的,除非先將快照刪除。
LV擴容
擴容分為兩步,先用lvextend擴充套件Logic Volume,再用resize2fs對檔案系統擴容。如果VG的容量不夠,就先用vgextend對VG進行擴容,也就是加入新的PV,說到底,在LVM的抽象模型中,儲存空間還是來自於PV。
[email protected]:/home/hunk# lvextend -L +10G /dev/mapper/VolGroup-sharevolume
Size of logical volume VolGroup/sharevolume changed from 20.00 GiB (5120 extents) to 30.00 GiB (7680 extents).
Logical volume sharevolume successfully resized.
[email protected]:/home/hunk# resize2fs -p /dev/mapper/VolGroup-sharevolume
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/mapper/VolGroup-sharevolume is mounted on /home/hunk/share; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 2
The filesystem on /dev/mapper/VolGroup-sharevolume is now 7864320 (4k) blocks long.
lv
[email protected]:/home/hunk# lvdisplay /dev/mapper/VolGroup-sharevolume
--- Logical volume ---
LV Path /dev/VolGroup/sharevolume
LV Name sharevolume
VG Name VolGroup
LV UUID ShfdSY-EHIu-9qz9-PHNc-bjEI-t5Ul-009Vn9
LV Write Access read/write
LV Creation host, time hunk-virtual-machine, 2018-11-26 11:51:39 +0800
LV Status available
# open 1
LV Size 30.00 GiB
Current LE 7680
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:2
LV縮減容量
縮減容量workflow:
- 先確定縮減後的目標大小,並確保對應的目標邏輯卷大小足夠容納原有資料
- 先解除安裝檔案系統,並要執行強制檢測:e2fsck -f
- 縮減檔案系統:resize2fs /dev/mapper/VolGroup-sharevolume 25G 縮減至25G
- 縮減邏輯卷:lvreduce -L 25G /dev/mapper/VolGroup-sharevolume
[email protected]:/home/hunk# mount |grep share
/dev/mapper/VolGroup-sharevolume on /home/hunk/share type ext4 (rw,relatime,data=ordered)
[email protected]:/home/hunk# umount /home/hunk/share #1.解除安裝掛載點
[email protected]:/home/hunk# e2fsck -f /dev/mapper/VolGroup-sharevolume #2.check
e2fsck 1.42.13 (17-May-2015)
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/mapper/VolGroup-sharevolume: 11/1966080 files (0.0% non-contiguous), 167409/7864320 blocks
[email protected]:/home/hunk# resize2fs /dev/mapper/VolGroup-sharevolume 25G #3.縮減FS
resize2fs 1.42.13 (17-May-2015)
Resizing the filesystem on /dev/mapper/VolGroup-sharevolume to 6553600 (4k) blocks.
The filesystem on /dev/mapper/VolGroup-sharevolume is now 6553600 (4k) blocks long.
[email protected]:/home/hunk# lvreduce -L 25G /dev/mapper/VolGroup-sharevolume #4.縮減LV容量
WARNING: Reducing active logical volume to 25.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce sharevolume? [y/n]: y
Size of logical volume VolGroup/sharevolume changed from 30.00 GiB (7680 extents) to 25.00 GiB (6400 extents).
Logical volume sharevolume successfully resized.
[email protected]:/home/hunk# lvdisplay /dev/mapper/VolGroup-sharevolume
--- Logical volume ---
LV Path /dev/VolGroup/sharevolume
LV Name sharevolume
VG Name VolGroup
LV UUID ShfdSY-EHIu-9qz9-PHNc-bjEI-t5Ul-009Vn9
LV Write Access read/write
LV Creation host, time hunk-virtual-machine, 2018-11-26 11:51:39 +0800
LV Status available
# open 0
LV Size 25.00 GiB #已經縮減到25G
Current LE 6400
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:2
[email protected]:/home/hunk# mount /dev/mapper/VolGroup-sharevolume /home/hunk/share #5.重新掛載
快照
當建立一個snapshot的時候,僅拷貝原始卷裡資料的元資料(meta-data)。建立的時候,並不會有資料的物理拷貝,因此snapshot的建立幾乎是實時的,當原始捲上有寫操作執行時,snapshot跟蹤原始卷塊的改變,這個時候原始捲上將要改變的資料在改變之前被拷貝到snapshot預留的空間裡,因此這個原理的實現叫做寫時複製(copy-on-write)。
在寫操作寫入塊之前,原始資料被移動到 snapshot空間裡,這樣就保證了所有的資料在snapshot建立時保持一致。而對於snapshot的讀操作,如果是沒有修改過的塊,那麼會將讀操作直接重定向到原始捲上,如果是已經修改過的塊,那麼就讀取拷貝到snapshot中的塊。
[email protected]:/home/hunk# mount -o remount,ro /dev/mapper/VolGroup-sharevolume /home/hunk/share #將LV改成只讀
[email protected]:/home/hunk# lvcreate -s -L 25G -n VolGroup-sharevolume_snap /dev/mapper/VolGroup-sharevolume #建立25G的快照
Logical volume "VolGroup-sharevolume_snap" created.
[email protected]:/home/hunk# mount -o remount,rw /dev/mapper/VolGroup-sharevolume /home/hunk/share