Linux 第12天 lvm

Linux 第12天 lvm

时间: 20180728


目录

LVM概念

LVM的实现 

PV创建,VG创建,LV创建,挂载

LVM扩容,缩减

LVM快照



扫描二维码关注公众号,回复: 2481565 查看本文章

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目录中的数据做对比,看是否已经备份到相关

   操作之前的数据。


猜你喜欢

转载自blog.51cto.com/winthcloud/2152481