Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze LVM snapshot

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

What this article shares with you is the analysis of how to carry out LVM snapshot. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Logical Volume Manager (LVM) provides the ability to snapshot any Logical Volume (LV) to obtain a state-consistent backup of a partition.

When making a backup in a certain state, an application may be accessing a file or database, that is, the file is in one state at the time of backup, but after the backup, the file is in another state. As a result, the backup is inconsistent, and it is almost impossible to recover database data in this state.

The solution to the state is to mount its partition as read-only and then back up the data through table-level locking (table-level write locks) of the database or even stopping the database. All of these methods unintentionally seriously affect the availability of the service. Using LVM snapshot allows for consistent backups without affecting the availability of the server.

As a reminder, snapshot is an approach that works only for LVM and not for non-LVM file systems.

There are many ways to implement snapshot (see the connection at the end of the article). Here's how to implement copy on write of snapshot in LVM.

When a snapshot is created, only the metadata (meta-data) of the data in the original volume is copied. At the time of creation, there is no physical copy of the data, so the creation of snapshot is almost real-time. When a write operation is performed on the original volume, snapshot tracks the change of the original volume block. At this time, the data to be changed on the original volume is copied to the space reserved by snapshot before the change, so the implementation of this principle is called copy-on-write.

Before the write operation writes to the block, CoW moves the raw data to the snapshot space, which ensures that all data is consistent when the snapshot is created. For the read operation of snapshot, if the read block is unmodified, the read operation will be redirected directly to the original volume, and if you want to read the modified block, then read the block copied to the snapshot.

In this way, there is a change in the usual file Ihandle 0 process, that is, a cow layer is added between the file system and the device driver, which looks like this:

File I filesystem 0-- > filesystem-- > CoW-- > block I / O

The following figure may make it easier to understand how CoW works:

When implementing CoW, the size of the snapshot does not need to be as large as the original volume, but only needs to be considered in two aspects: how much the block changes during the period from the creation of the shapshot to the time it is released, and the frequency of data updates. Once the space of the snapshot is full of information about the original volume block transformation, the snapshot is immediately released and cannot be used, resulting in the invalidation of the snapshot. So, it's very important to do what you need to do during the life cycle of snapshot. Of course, if your snapshot is the same size as the original volume, or even larger, then its life is "as long as the sky".

Snapshot actually has many other uses besides backup.

1) Virtualization

When using LVM2, snapshots may not be read-only. This means that after the snapshot is created, the snapshot can be mounted, read and written like a regular block device.

Because popular virtualization systems such as Xen, VMWare, Qemu, and KVM can use block devices as guest images, you can create full copies of these images and use them as needed, like virtual machines with low memory footprint. The benefits of this are rapid deployment (it often takes less than a few seconds to create a snapshot) and space savings (guest shares most of the data of the original image).

The steps for setting up are as follows:

1. Create a logical volume for the original image.

two。 Use this LV as the disk image to install the guest virtual machine.

3. Pause this virtual machine. The memory image can be a regular file in which all other snapshots are placed.

4. Create a read / write snapshot of the original LV.

5. Generate a new virtual machine using the snapshot volume as the disk image. Modify the network / console settings if necessary.

6. Log in to the virtual machine that has been created and modify the network settings / hostname.

After completing these steps, you can give users access to the virtual machine you just created. If you need another virtual machine, just repeat steps 4 through 6 (so there is no need to reinstall the virtual machine). You can also automate these steps with a script.

After using the virtual machine, you can stop the virtual machine and destroy the snapshot.

2) data backtracking

To perform some operations on a production system, you need to be very careful. Even if you have done many tests in the simulation environment, there is no problem, but there is no guarantee of success in the production environment, so at this time, we make the system a snapshot, so that if there is a problem with the new operation, you can immediately go back to the time when the snapshot was created. Of course, you can also think that this is an extended use of backup.

Finally, we give some examples to deepen our understanding of snapshot.

A) create a 20m snapshot and perform some operations to see what the CoW does.

Let's give an example of how to create and use snapshot. Let's assume that we create a 120m snapshot, which means that you can only change the amount of data by 120m during the snapshot life cycle.

The following command creates / dev/vg/lvdata-sp for / dev/vg/lvdata

# lvcreate-L20m-s-n lvdata-sp / dev/vg/lvdata

Logical volume "lvdata-sp" >

Where the lvdata size is 120MB.

# lvdisplay / dev/vg/lvdata-sp

-Logical volume

LV Name / dev/vg/lvdata-sp

VG Name vg

LV UUID Yl0fQU-Ve9T-lfmp-xJPq-Uwrd-RVVM-lDDVz0

LV Write Access read/write

LV snapshot status active destination for / dev/vg/lvdata

LV Status available

# open 1

LV Size 200.00 MB

Current LE 50

COW-table size 20.00 MB

COW-table LE 5

Allocated to snapshot 0.27%

Snapshot chunk size 8.00 KB

Segments 1

Allocation inherit

Read ahead sectors 0

Block device 253:0

The above Allocated to snapshot 0.27% is of concern to us, indicating that 99.73% of the space is still unused.

Let's try to create a 10m file in lvdata and look at this parameter value.

# mount / dev/vg/lvdata / media/lvdata/

# dd if=/dev/hda of=/media/lvdata/10M bs=1M count=10

10: 0 records in

10: 0 records out

10485760 bytes (10 MB) copied, 0.272393 seconds, 38.5 MB/s

# lvdisplay / dev/vg/lvdata-sp

-Logical volume

LV Name / dev/vg/lvdata-sp

VG Name vg

LV UUID Yl0fQU-Ve9T-lfmp-xJPq-Uwrd-RVVM-lDDVz0

LV Write Access read/write

LV snapshot status active destination for / dev/vg/lvdata

LV Status available

# open 0

LV Size 200.00 MB

Current LE 50

COW-table size 20.00 MB

COW-table LE 5

Allocated to snapshot 51.02%

Snapshot chunk size 8.00 KB

Segments 1

Allocation inherit

Read ahead sectors 0

Block device 253:0

"Allocated to snapshot 51.02%", in line with our expectations. At this point, there is less than 10m of space left in snapshot. What will happen if we create a 12m file on lvdata?

# dd if=/dev/hda of=/media/lvdata/12M bs=1M count=12

12'0 records in

12'0 records out

12582912 bytes (13 MB) copied, 0.288311 seconds, 43.6 MB/s

Device-mapper: snapshots: Invalidating snapshot: Unable to allocate exception.

During the creation of the file, an error occurred and the snapshot is no longer valid. Let's look at the details of the snapshot volume.

# lvdisplay / dev/vg/lvdata-sp

/ dev/vg/lvdata-sp: read failed after 0 of 4096 at 0: input / output error

-Logical volume

LV Name / dev/vg/lvdata-sp

VG Name vg

LV UUID Yl0fQU-Ve9T-lfmp-xJPq-Uwrd-RVVM-lDDVz0

LV Write Access read/write

LV snapshot status INACTIVE destination for / dev/vg/lvdata

LV Status available

# open 0

LV Size 200.00 MB

Current LE 50

COW-table size 20.00 MB

COW-table LE 5

Snapshot chunk size 8.00 KB

Segments 1

Allocation inherit

Read ahead sectors 0

The entire snapshot volume has had an Istroke 0 error, and the status of the snapshot is "INACTIVE".

Can you mount it?

# mount / dev/vg/lvdata-sp / media/snapshot/

Mount: you must specify the filesystem type

# dmesg

Buffer I/O error on device dm-0, logical block 0

Buffer I/O error on device dm-0, logical block 1

Buffer I/O error on device dm-0, logical block 2

Buffer I/O error on device dm-0, logical block 3

Buffer I/O error on device dm-0, logical block 4

Buffer I/O error on device dm-0, logical block 5

Buffer I/O error on device dm-0, logical block 6

Buffer I/O error on device dm-0, logical block 7

Buffer I/O error on device dm-0, logical block 8

Buffer I/O error on device dm-0, logical block 9

Hfs: unable to find HFS+ superblock

Judging from the error message of dmesg, the information of the super block is also lost.

Try to activate lvdata-sp

# lvchange-ay / dev/vg/lvdata-sp

/ dev/vg/lvdata-sp: read failed after 0 of 4096 at 0: input / output error

Well, the snapshot has been released, so all that's left to do is delete it.

# lvremove / dev/vg/lvdata-sp

/ dev/vg/lvdata-sp: read failed after 0 of 4096 at 0: input / output error

Do you really want to remove active logical volume "lvdata-sp"? [y/n]: y

Logical volume "lvdata-sp" successfully removed

B) online backup of MySQL databases (or other databases) using snapshot

The process is to first do a flush operation and lock the table, create any snapshot, unlock anything, then back up the data, and finally release the snapshot. In this way, MySQL hardly interrupts its operation.

FLUSH TABLES WITH READ LOCK

\! Lvcreate-- size 100m-- snapshot-- name snap / dev/VolGroup01/LogVol00

UNLOCK TABLES

Then do some backup work.

Mkdir / snap

Mount / dev/VolGroup01/snap / snap

# This is where you back up whatever you need from / snap, e.g. Rsync (1)

Umount / snap

Lvremove / dev/VolGroup01/snap

Rmdir / snap

The above is how to carry out the LVM snapshot analysis, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report