`

LVM snapshots explained

 
阅读更多

This article explains how LVM snapshots work and what the advantage of read/write snapshots is.

We will go through a simple example to illustrate our explanation.

First, create a dummy device that we will initialize as a PV:

# dd if=/dev/zero of=dummydevice bs=8192 count=131072
# losetup /dev/loop0 dummydevice
# pvcreate /dev/loop0
# pvs
PV VG Fmt Attr PSize PFree
/dev/loop0 lvm2 a-- 1.00g 1.00g

We now have a 1GB LVM2 Physical Volume.

# vgcreate vg0
# vgs
VG #PV #LV #SN Attr VSize VFree
vg0 1 0 0 wz--n- 1020.00m 1020.00m
# lvcreate -n lv0 -l 100 vg0
# lvs
LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert
lv0 vg0 -wi-a---- 400.00m

We now have a Volume Group vg0 and a 400MB Logical Volume lv0. Let’s see what our device mapper looks like

# dmsetup table
vg0-lv0: 0 819200 linear 7:0 2048

We have a single device vg0-lv0, as expected.

Let’s take a snapshot of our Logical Volume:

# lvcreate -s -n snap1 -l 30 /dev/vg0/lv0
# lvs
LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert
lv0 vg0 owi-a-s-- 400.00m
snap1 vg0 swi-a-s-- 120.00m lv0 0.00

We’ve created a 30 extents (120MB) snapshot of lv0 called snap1.

Let’s take a look at our device mapper:

# dmsetup table
vg0-lv0-real: 0 819200 linear 7:0 2048
vg0-snap1-cow: 0 245760 linear 7:0 821248
vg0-lv0: 0 819200 snapshot-origin 252:2
vg0-snap1: 0 819200 snapshot 252:2 252:3 P 8

This is interesting. We’d expect to see two devices. Instead, we have four. Let’s draw the dependencies:

 

LVM snapshots explained

We can see that LVM renamed our vg0-lv0 device as vg0-lv0-real and created a new vg0-lv0 device. The user has now transparently switched to using vg0-lv0.
A new device named vg0-snap1-cow is the device of size 30 extents (120MB) that will hold the contents of the snapshot.
A new device named vg0-snap1 is the device that the user will interact with.

Reading from the original volume
The user reads from vg0-lv0. The request is forwarded and the user retrieves data from vg0-lv0-real.

Writing to the original volume
The user writes to vg0-lv0. The original data is first copied from vg0-lv0-real to vg0-snap1-cow (This is why it’s called COW, Copy On Write). Then the new data is written to vg0-lv0-real. If the user writes again to vg0-lv0, modifying data that has already been copied to vg0-snap1-cow, the data from vg0-lv0-real is simply overwritten. The data on vg0-snap1-cow remains the data that was valid at the time of the creation of the snapshot.

Reading from the snapshot
When the user reads data from vg0-snap1, a lookup is done on vg0-snap1-cow to see if that particular piece of data has been copied from vg0-lv0-real to vg0-snap1-cow. If that is the case, the value from vg0-snap1-cow is returned. Otherwise, the data from vg0-lv0-real is returned. The user effectively sees the data that was valid at the time of creation of the snapshot.

A few important things to note:

  • The snapshot is empty at creation, whatever size it is. This means that the creation of a snapshot is immediate and invisible to the user.
  • The snapshot will hold a copy of the original data, as the original data is modified. This means that the snapshot will grow over time. Therefore, it is not necessary to make a snapshot as big as the original volume. The snapshot should be big enough to hold the amount of data that is expected to be modified over the time of existence of the snapshot. Creating a snapshot bigger than the original volume is useless and creating a snapshot as big as the original volume will ensure that all data on the original volume can be copied over to the snapshot.
  • If a snapshot is not big enough to hold all the modified data of the original volume, the snapshot is removed and suddenly disappears from the system, with all the consequences of removing a mounted device from a live system.
  • A volume that has one or more snapshots will provide much less I/O performance. Remove the snapshot as soon as you’re done with it.

Writing to the snapshot

With LVM2, as opposed to LVM1, the snapshot is read-write.
When the user writes to vg0-snap1, the data on vg0-snap1-cow is modified. In reality, what happens is that the a lookup is done to see if the corresponding data on vg0-lv0-real has already been copied to vg0-snap1-cow. If not, a copy is done from vg0-lv0-real to vg0-snap1-cow and then overwritten with the data the user is trying to write to vg0-snap1. This is because the granularity of the data copy process from vg0-lv0-real to vg0-snap1-cow might be bigger than the data the user is trying to modify. Let’s imagine that data is being copied from vg0-lv0-real to vg0-snap1-cow in chunks of 8KB and the user wants to modify 1KB of data. The whole chunk is copied over to the snapshot first. Then the 1KB are modified.
A second write to the same data will result in simply overwriting data in vg0-snap1-cow.

Removing an LVM snapshot
The following command will remove the snapshot:

# lvremove vg0/snap1
  • This operation is very fast because LVM will simply destroy vg0-snap1-cow, vg0-lv0 and vg0-snap1, and rename vg0-lv0-real to vg0-lv0.
  • All data that has eventually been written to the snapshot (through vg0-snap1) is LOST.

Merging a snapshot
The following command will merge the snapshot:

# lvconvert --merge vg0/snap1
Merging of volume snap1 started.
lv0: Merged: 100.0%
Merge of snapshot into logical volume lv0 has finished.
Logical volume "snap1" successfully removed
  • The merge will start immediately if the filesystems in the original volume and in the snapshot are unmounted (i.e. if the volumes are not in use), otherwise the merge will start as soon as both filesystems are unmounted and one of the volumes is activated. This means that if a merge of the root filesystem is planned, it will happen after a reboot, as soon as the original volume is activated.
  • This operation can take time, because data needs to be copied from the snapshot to the original volume.
  • As soon as the merge begins, any read-write operation to the original volume is transparently redirected to the snapshot that is in the process of being merged. Therefore, the operation is transparent to the user who thinks he’s using the merged volume. This means that as soon as the merge begins, users interact with a volume that contains the data at the time of the creation of the snapshot (+ data that has eventually been written to the snapshot since then).

Benefits of read-write snapshots
Snapshots are of course very useful to make a consistent backup of a volume. The backup will be performed on vg0-snap1, reading data either from vg0-lv0-real or from vg0-snap1-cow if the data has been modified since the creation of the snapshot. This allows for a consistent backup of the volume at a certain point in time, without taking the volume offline.
But this can be done with read-only snapshots. So what are read-write snapshots useful for ?
Read-write snapshots (also called branching snapshots because of the fact that they create different versions of the data, a bit like a version control branch) are useful for virtualization, sandboxing and virtual hosting:

Read-write snapshots for sandboxing
Since the snapshot is read-write, you can use either the snapshot or the original volume to experiment on. Which one to choose will depend on the likeliness of having to revert. If you’re doing a system upgrade that is likely to succeed, take a snapshot and perform the upgrade on the original volume. If everything goes fine, remove the snapshot. If the upgrade fails and you need to revert, merge the snapshot.
If you’re booting a system every day at the same state (e.g. a Virtual Desktop Infrastructure situation), and you know you’re going to revert to the original state in the evening, let your user use the snapshot instead of the original volume. At the end of the day, just remove the snapshot. Every once in a while you’re going to merge because you want to keep a patch you’ve applied.

Read-write snapshots in virtualization
When using LVM volumes as storage backends for virtualization, a snapshot can be taken, effectively allowing the creation of a new VM. This VM can then evolve separately.

Read-write snapshots for virtual hosting
You can use an LVM volume to contain the image of a virtual machine. Then take a snapshot for one version of the VM, another snapshot for another modified version of the VM, etc. That way, the majority of the space of each VM instance resides in the original volume and the snapshots only contain the differences that are specific to a modified version of the VM or to an instance of the VM. This trades performance for space.

 

From :

http://clevernetsystems.com/lvm-snapshots-explained/

分享到:
评论

相关推荐

    离线安装LVM2-CentOS分区工具.zip

    4. **快照(Snapshots)**:LVM2还支持创建卷的快照,这允许在不影响现有数据的情况下创建一个临时的备份,用于进行数据恢复或测试新的软件更新。 5. **镜像和条带化**:LVM2支持创建镜像卷,以提供冗余和数据保护...

    lvm使用手册,详细介绍lvm的操作命令

    LVM 快照(Snapshots) LVM 提供了快照功能,可以在不停服务的情况下,把用户数据从旧硬盘转移到新硬盘空间中去。 LVM 命令 LVM 提供了多种命令来管理逻辑卷,例如: 1. vgcreate:创建卷组。 2. vgextend:...

    lvm2源码,c语言

    LVM2(Logical Volume Manager version 2)是Linux操作系统中用于管理磁盘存储的一种高级工具。它通过将物理硬盘组织成逻辑卷,提供更灵活的磁盘管理和扩展性。LVM2源码由C语言编写,是开源社区的重要贡献,为开发者...

    ListView Messages (LVM_)ListView控件类的消息常量常量

    - **代码示例**:`Public Const LVM_GETITEM = (LVM_FIRST + 5)` - **应用场景**:当需要获取列表中的某一项的具体信息时,如项的状态、文本等,可以使用该消息。 ##### 2. **LVM_SETITEM** - **定义**:用于...

    KMV的MATLAB的代码-lvmsnapshot:创建和挂载LVM快照的Bashscript

    snapshots. USAGE: lvmsnapshot OPTIONS: -c CONFIGFILE Use specified config file -d Debug output -e LVMEXTENSION LVM volume extension, which will be appended to the volume name -g GROUPNAME LVM ...

    lvm2代码 可编译 已验证

    **LVM2(Logical Volume Manager 2)**是Linux操作系统中的一个高级磁盘管理工具,它允许用户在物理硬盘上创建逻辑卷,从而提供更大的灵活性和扩展性。LVM2比传统的分区方式更加灵活,因为它可以跨越多个硬盘,动态...

    lvm基本命令详解

    lvm 基本命令详解 LVM(Logical Volume Manager)是一种逻辑卷管理器,用于管理磁盘分区和文件系统之间的逻辑层。它提供了一个抽象的盘卷,在盘卷上建立文件系统。 LVM 的基本术语包括: * 物理存储介质(The ...

    lvm2离线安装包 ,亲测可用

    LVM(Logical Volume Manager)是Linux系统中一种高级的存储管理技术,它允许管理员将物理硬盘组织成逻辑卷,提供更灵活的磁盘管理和扩展能力。LVM2是其第二代版本,相较于第一代,它增加了更多的功能和优化,提高了...

    Linux LVM硬盘管理及LVM扩容

    "Linux LVM硬盘管理及LVM扩容" 根据提供的文件信息,以下是相关的知识点: 一、LVM简介 LVM(Logical Volume Manager)是一种逻辑卷管理系统,由Heinz Mauelshagen在Linux 2.4内核上实现。LVM可以将一个或多个...

    LVM详细配置管理手册

    从给定的文件信息来看,LVM(Logical Volume Manager)是一种高级的磁盘管理技术,主要解决了传统分区方式中磁盘空间无法动态扩展的问题。LVM允许管理员在Linux系统上更加灵活地管理和分配磁盘空间,它将物理磁盘...

    LVM(逻辑卷管理)

    **LVM(逻辑卷管理)**是Linux操作系统中一种先进的磁盘管理技术,它允许管理员在物理硬盘之上创建逻辑存储单元,以实现更灵活的磁盘空间管理和数据存储。LVM将物理硬盘组织成卷组(Volume Group),然后在卷组上...

    linux常用命令 lvm

    Linux LVM,全称为Logical Volume Manager,是Linux操作系统下的一种磁盘管理工具,它提供了一种灵活、可扩展的方式来管理磁盘存储空间。LVM允许系统管理员创建和管理逻辑卷,这些逻辑卷可以跨越多个物理硬盘,从而...

    Linux7扩容根分区(LVM+非LVM).docx

    Linux 7 扩容根分区(LVM+非 LVM) Linux 7 扩容根分区是指在 Linux 7 操作系统中扩展根分区的大小,以满足存储需求。在本文中,我们将介绍使用 LVM(Logical Volume Manager)和非 LVM 两种方法来扩容根分区。 使用...

    LINUX下LVM技术文档教程

    LINUX下的LVM(Logical Volume Manager)是一种先进的磁盘管理技术,它允许系统管理员在Linux环境中灵活地管理和调整磁盘分区。LVM的核心优势在于它提供了动态调整存储空间的能力,无需停机,使得扩展文件系统变得...

    [原创]CentOS6.9数据库生产服务器扩容手册(LVM+非LVM)

    CentOS6.9 数据库服务器扩容手册(LVM+非LVM) 本文档旨在指导用户在 CentOS6.9 环境下扩容数据库服务器,涵盖了 LVM 和非 LVM 两种情况。该手册将分为四个部分:准备工作、LVM 扩容、非 LVM 扩容和总结。 准备...

    Linux LVM硬盘管理

    Linux LVM 硬盘管理 Linux LVM 硬盘管理是指通过 Logical Volume Manager(逻辑卷管理)来管理硬盘的存储空间。LVM 将一个或多个硬盘的分区在逻辑上集合,相当于一个大硬盘来使用。当硬盘的空间不够使用的时候,...

    linux LVM管理整合打包

    Linux LVM(Logical Volume Manager,逻辑卷管理)是Linux操作系统中一种高级的磁盘管理工具,它提供了一种灵活的方式来组织存储空间,使得磁盘管理更为便捷和高效。本资料包整合了关于LVM管理的各种知识,涵盖了从...

    HP-UX LVM使用手册

    **2.8 快照 (Snapshots)** LVM 还提供了一种快照功能,可以在不停止服务的情况下对 LV 创建某个时间点的镜像。这对于备份和恢复非常有用,尤其是对于在线的数据保护。 #### 3. LVM 基本操作 ##### 3.1 Physical ...

    AIX LVM管理介绍 AIX LVM管理介绍

    ### AIX LVM管理介绍 逻辑卷管理器(Logical Volume Manager, LVM)是IBM AIX操作系统中的一个重要组件,用于简化存储管理和提高数据可用性。本文将深入探讨AIX LVM的基本概念、管理方法以及一些实用技巧。 #### 1...

Global site tag (gtag.js) - Google Analytics