1. 程式人生 > >Linux 第12天 lvm

Linux 第12天 lvm

allocated probe har provide 通過 ecif 那一刻 cati shrink

Linux 第12天 lvm

時間: 20180728


目錄

LVM概念

LVM的實現

PV創建,VG創建,LV創建,掛載

LVM擴容,縮減

LVM快照



LVM Logical Volume Manager

功能:重點在於可以靈活的增加文件系統容量,而非在數據的存儲效率和安全上面。

Lvm將多個分區或者硬盤整合為一個大的邏輯硬盤,然後再在其之上分配邏輯分區,並還可以

調整邏輯分區的大小。


PV (Phycical Volume)

用戶來將硬盤或分區格式化為lvm所使用物理卷,供上層VG使用


VG (Volume Group)

用來將多個PV組合成一個或多個VG供上層LV劃分分區使用

LV (Logic Volume)

用來將VG的容量劃分成具體的邏輯分區,並在後續創建文件系統掛載使用


LVM當中數據存儲的最小單位是PE,PE與block作用一樣,LVM默認使用的是4MB的PE區塊,而

LVM的VG最多僅能含有65534個PE,因此默認的LVM的VG會有4M*65534/1024M=256G,我們可以

在創建VG的時候通過設定PE的大小來決定的VG的大小。



LVM的實現

1. 創建PV

PV的管理命令: pvcreate, pvdisplay, pvs, pvscan, pvremove, pvmove

pvck, pvresize

pvcreate: 初始化硬盤硬盤或整個硬盤,使用其成為PV

pvscan: 搜尋當前系統裏面任何具有PV的硬盤

pvdisplay:顯示PV的屬性信息

pvs: 顯示pv信息比pvdisplay顯示的簡略一些

pvremove:移除所指定的PV


(1) 先準備塊設備,調整其id為8e

(2) 創建pv pvcreate /dev/sdb{2,3,4}

(3) 查看pv信息 pvdisplay


2. 創建VG

VG的管理命令: vgcreate, vgscan, vgdisplay, vgs, vgextend, vgreduce

vgchange, vgremove, vgrename, vgck

vgcreate: 創建vg

vgscan: 搜尋系統上的vg信息

vgdisplay,vgs: 查看系統上VG的狀態

vgextend: 在VG內添加額外的PV

vgreduce: 在VG內移除PV

vgchange: 配置VG是否啟動active

vgremove: 刪除一個VG


(1) 新增一個VG,並指明其包含哪個或哪些pv

vgcreate VGNAME /dev/pv1...

本例中使用 vgcreate -s 16M mysky /dev/sda{5,6}

vgscan

pvscan

vgdisplay

(2) 此時如果想要增加VG的容量,可以進行如下的操作

vgextend mysky /dev/sda7

vgdisplay


3. 創建LV

lv的管理命令: lvcreate, lvscan, lvdisplay, lvextend, lvreduce,

lvremove, lvresize

lvcreate: 創建LV

lvscan: 顯示系統上的lv狀態

lvdisplay: 顯示LV詳細信息

lvextend: 在LV裏添加容量

lvreduce: 在LV裏減少容量

lvremove: 刪除一個LV

lvresize: 對LV進行容量大小調整


lvcreate 使用方法

lvcreate -L #[bBsSkKmMgGtTpPeE] [-n Logical Volume Name]

VGNAME

本例中使用: lvcreate -L 3G -n myskylv mysky


lvs lvdisplay lvscan查看lv的狀態


4. 文件系統掛載

真正使用lvm就必須對lv進行格式化掛載後才可使用

(1) mkfs -t ext3 /dev/mysky/myskylv

(2) mkdir /data

(3) mount -t ext3 /dev/mysky/myskylv /data


LV擴容

1. 用fdisk配置新的具有8e system ID的磁盤分區

partprobe

2. 利用pvcreate 創建PV

pvcreate /dev/sda7

3. 利用vgextend將PV加入之前創建的VG mysky (VG擴容)

vgextend mysky /dev/sda7

4. 利用lvresize將新加入的PV內的PE加入myskylv LV中 (LV擴容)

lvresize -L +3G /dev/mysky/myskylv

5. 通過resize2fs將文件系統的容量確實添加

resize2fs /dev/mysky/myskylv


至此lv擴容結束


LV縮減

註意:有的文件系統不支持容量縮減,如xfs只可以擴容不可縮減

因為縮減容量可能會出現問題,導致文件系統裏的文件無法訪問,好在ext支持容量縮減


1. 先卸載已經掛載的LV

umount /dev/mysky/myskylv

2. 強制進行磁盤檢測

e2fsck -f /dev/mysky/myskylv

3. ext文件系統縮減容量為3G

resize2fs /dev/mysky/myskylv 3G

4. LV縮減3G容量

lvreduce -L -3G /dev/mysky/myskylv

5. 重新掛載分區

mount /dev/mysky/myskylv /data/

6. vgreduce

vgreduce mysky /dev/sda7


LVM快照

快照就是在當時創建快照那一刻將所指定的分區定格在那一個時間,裏邊的文件只保存至

那一刻,快照卷裏分兩部分數據一部分是定格後該分區變更的數據,和在定格前所保存的

數據,快照卷裏其實保存的是目錄列表和文件所對應的inode編號,只有新更新的文件

或數據才會被臨時存儲至這個快照卷裏。 創建快照卷其實是為了備份數據使用,即保存

創建快照那一時刻文件系統裏所存儲的數據。

-s|--snapshot

Create a snapshot. Snapshots provide a "frozen image" of an

origin LV. The snapshot LV can be used, e.g. for backups,

while the origin LV continues to be used. This option can

create a COW (copy on write) snapshot, or a thin snapshot (in

a thin pool.) Thin snapshots are created when the origin is

a thin LV and the size option is NOT specified. Thin snap‐

shots share the same blocks in the thin pool, and do not

allocate new space from the VG. Thin snapshots are created

with the "activation skip" flag, see --setactivationskip. A

thin snapshot of a non-thin "external origin" LV is created

when a thin pool is specified. Unprovisioned blocks in the

thin snapshot LV are read from the external origin LV. The

external origin LV must be read-only. See lvmthin(7) for

more information about LVM thin provisioning. COW snapshots

are created when a size is specified. The size is allocated

from space in the VG, and is the amount of space that can be

used for saving COW blocks as writes occur to the origin or

snapshot. The size chosen should depend upon the amount of

writes that are expected; often 20% of the origin LV is

enough. If COW space runs low, it can be extended with lvex‐

tend (shrinking is also allowed with lvreduce.) A small

amount of the COW snapshot LV size is used to track COW block

locations, so the full size is not available for COW data

blocks. Use lvs to check how much space is used, and see

--monitor to to automatically extend the size to avoid run‐

ning out of space.

with the "activation skip" flag, see --setactivationskip. A

thin snapshot of a non-thin "external origin" LV is created

when a thin pool is specified. Unprovisioned blocks in the

thin snapshot LV are read from the external origin LV. The

external origin LV must be read-only. See lvmthin(7) for

more information about LVM thin provisioning. COW snapshots

are created when a size is specified. The size is allocated

from space in the VG, and is the amount of space that can be

used for saving COW blocks as writes occur to the origin or

snapshot. The size chosen should depend upon the amount of

writes that are expected; often 20% of the origin LV is

enough. If COW space runs low, it can be extended with lvex‐

tend (shrinking is also allowed with lvreduce.) A small

amount of the COW snapshot LV size is used to track COW block

locations, so the full size is not available for COW data

blocks. Use lvs to check how much space is used, and see

--monitor to to automatically extend the size to avoid run‐

ning out of space.


創建快照

lvcreate -L 2G -s -n myskysnap /dev/mysky/myskylv


lvsdisplay


創建快照目錄 mkdir /mnt/snap

掛載 mount /dev/mysky/myskysnap /mnt/snap/


利用快照恢復數據

註意: 所要恢復的數據不能超過快照區的總大小,因為如果變動的數據大小超過了

快照區的空間,那麽快照區是不能容納所有變動的數據,這個時候功能會失效


演示: 先在原來的LV中寫入一些數據,然後通過lvm的快照功能恢復之前的操作

1. cp -r /etc /data

2. 創建一個數據恢復的臨時目錄 mkdir /tmp/backup

3. cd /mnt/snap

4. tar -zcf /tmp/backup/lvm.tzr.gz *

5. 將myskysnap LV卸載並移除,因為裏面的內容已經備份出來了

umount /mnt/snap

lvremove /dev/mysky/myskysnap

6. 解壓備份出來的文件和/data目錄中的數據做對比,看是否已經備份到相關

操作之前的數據。


Linux 第12天 lvm