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

Detailed explanation of Linux logical volume management LVM

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

Share

Shulou(Shulou.com)06/02 Report--

Reproduced from: http://blog.csdn.net/ikikik2002/article/details/5187276

I. Preface

LVM, short for logical Volume Management (Logical Volume Manager), is an abstraction layer built on top of physical storage devices, allowing you to generate logical storage volumes, providing more flexibility than using physical storage directly for management.

LVM virtualizes storage, uses logical volumes, you are not limited by the size of physical disks, and hardware-related storage settings are hidden by it, so you can resize volumes or migrate data without having to stop applying or unmounting file systems. This can reduce the operating cost.

Compared to using physical storage directly, LVM has the following benefits:

1. Flexible capacity.

When using logical volumes, the file system can be extended to multiple disks and you can aggregate multiple disks or disk partitions into a single logical volume.

two。 Scalable storage pool.

You can use simple commands to expand or reduce the size of logical volumes without reformatting or partitioning disk devices.

3. Online data redistribution.

You can move data online, and data can be redistributed while the disk is online. For example, you can replace hot-swappable disks online.

4. Convenient device naming

Logical volumes can name all the names in a way that you find convenient.

5. The disk is fragmented.

You can generate a logical disk whose data can be stored in blocks on 2 or more disks. This can significantly improve data throughput.

6. Image volume

Logical volumes provide a convenient way to image your data.

7. Volume snapshot

Using logical volumes, you can get device snapshots for consistent backups or test data updates without affecting real data.

II. Basic terminology of LVM

As mentioned earlier, LVM is a logical layer added to physical storage to shield the file system from the underlying hardware storage devices, providing an abstract volume and establishing a file system on the volume. First, let's discuss the following LVM terms:

* physical storage media (The Physical Media)

Here, the storage devices of the system, such as / dev/hda1, / dev/sda, and so on, are the storage units at the lowest level of the storage system.

* physical volumes (PV, Physical Volume)

Physical volumes refer to disks, disk partitions or devices that logically have the same function as disk partitions (such as RAID). It is the basic storage logic block of LVM, but compared with basic physical storage media (such as partitions, disks, etc.), it contains management parameters related to LVM. The current LVM allows you to save 0 to 2 metadata copies of this physical volume on each physical volume. The default is 1, saved at the beginning of the device. At 2, save the second backup at the end of the device.

* Volume group (VG, Volume Group)

LVM volume groups are similar to physical hard disks in non-LVM systems and are made up of physical volumes. You can create one or more LVM partitions (logical volumes) on a volume group, which consists of one or more physical volumes.

* logical volumes (LV, Logical Volume)

LVM's logical volumes are similar to hard disk partitions in non-LVM systems, and file systems can be built on top of logical volumes (such as / home or / usr, etc.).

* Linear logical Volume (Linear Volumes)

A linear logical volume aggregates multiple physical volumes into a single logical volume. For example, if you have two 60GB hard drives, you can generate 120GB logical volumes.

* striped logical volumes (Striped Logical Volumes)

When you write data to this logical volume, the file system can place the data on multiple physical volumes. For a large number of connection read and write operations, it can improve the efficiency of the data I _ handle O.

* logical volume of the image (Mirrored Logical Volumes)

Images hold consistent data on different devices. The data is written to both the original device and the imaging device. He provides fault tolerance between devices.

* snapshot volume (Snapshot Volumes)

The snapshot volume provides a virtual image of the device at a specific moment, and when the snapshot starts, he copies a change to the current data area, and because he prioritizes these changes, he can reconstruct the state of the current device.

* PE (physical extent)

Each physical volume is divided into basic units called PE (Physical Extents), and PE with a unique number is the smallest unit that can be addressed by LVM. The size of the PE can be set, and the default is 4MB.

* LE (logical extent)

Logical volumes are also divided into basic addressable units called LE (Logical Extents). In the same volume group, the size of LE and PE are the same and correspond one to one.

Like non-LVM systems, metadata containing partition information is stored in the partition table at the beginning of the partition, and the metadata related to logical volumes and volume groups is also stored in the VGDA (volume group descriptor area) at the beginning of the physical volume. VGDA includes the following: PV descriptors, VG descriptors, LV descriptors, and some PE descriptors. When the system starts LVM, activate VG and load the VGDA into memory to identify the actual physical storage location of the LV. When the system performs an iCandle O operation, it accesses the actual physical location according to the mapping mechanism established by VGDA.

Third, install LVM

First determine if the lvm tool is installed on the system:

[root@test2 root] # rpm? qa | grep lvm

Lvm-x-x-x

If the command result input is similar to the above example, then the system has LVM management tools installed; if the command is not output, it means that LVM management tools are not installed, then you need to download the LVM rpm toolkit from the network or install the LVM rpm toolkit from the CD.

After installing the RPM package for LVM, you also need to set up the kernel to support LVM to use LVM. The default kernel of RedHat supports LVM. If you need to recompile the kernel, you need to go to the Multi-device Support (RAID and LVM) submenu when setting up the kernel and select the option:

Multiple devices driver support (RAID and LVM)

Device mapper support

Snapshot target (EXPERIMENTAL)

Mirror target (EXPERIMENTAL)

Then recompile the kernel to add LVM support to the new kernel.

In order to use LVM, to ensure that LVM is activated at system startup, the system startup script already has support for activating LVM in the version of RedHa, with the following in / etc/rc.d/rc.sysinit:

If [- x / sbin/lvm.static]; then

Action $"Setting up Logical Volume Management:" / sbin/lvm.static vgchange-a y-- ignorelockingfailure

Fi

The vgchange-a y command activates all volume groups on the system.

Create and manage LVM

To create a LVM system, you generally need to go through the following steps:

1. Create a partition

Use partition tools (such as fdisk, etc.) to create LVM partitions in the same way as other generic partitions, except that the partition type of LVM is 8e.

# fdisk-l / dev/sdb / dev/sdc

Disk / dev/sdb: 18.2 GB, 18200739840 bytes

255 heads, 63 sectors/track, 2212 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Disk / dev/sdb doesn't contain a valid partition table

Disk / dev/sdc: 18.2 GB, 18200739840 bytes

255 heads, 63 sectors/track, 2212 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/ dev/sdc1 1 200 1606468 + 8e Linux LVM

If you want to use the whole disk, you can overwrite the original partition information on the disk with the following command:

# dd if=/dev/zero of=/dev/sdc bs=512 count=1

2. Create a physical volume

The command to create a physical volume is pvcreate, which creates all partitions or disks that you want to add to the volume group as physical volumes. The command to create the entire disk as a physical volume is:

# pvcreate / dev/sdb

Physical volume "/ dev/sdb" successfully created

The command to create a single partition as a physical volume is:

# pvcreate / dev/sdc1

Physical volume "/ dev/sdc1" successfully created

You can also generate multiple volumes at the same time:

# pvcreate / dev/sdb / dev/sdc1

3. Scan block equipment

Through lvmdiskscan, you can see that those devices have become physical volumes.

# lvmdiskscan

/ dev/ramdisk [16.00 MB]

/ dev/sda [4.00 GB]

/ dev/root [2.88 GB]

/ dev/ram [16.00 MB]

/ dev/sda1 [101.94 MB]

/ dev/VolGroup00/LogVol01 [1.00 GB]

/ dev/ram2 [16.00 MB]

/ dev/sda2 [3.90 GB] LVM physical volume

/ dev/lvm_test/test [508.00 MB]

/ dev/ram3 [16.00 MB]

/ dev/ram4 [16.00 MB]

/ dev/ram5 [16.00 MB]

/ dev/ram6 [16.00 MB]

/ dev/ram7 [16.00 MB]

/ dev/ram8 [16.00 MB]

/ dev/ram9 [16.00 MB]

/ dev/ram10 [16.00 MB]

/ dev/ram11 [16.00 MB]

/ dev/ram12 [16.00 MB]

/ dev/ram13 [16.00 MB]

/ dev/ram14 [16.00 MB]

/ dev/ram15 [16.00 MB]

/ dev/sdb [512.00 MB] LVM physical volume

/ dev/sdc [512.00 MB] LVM physical volume

5 disks

16 partitions

2 LVM physical volume whole disks

1 LVM physical volume

4. Display physical volumes

You can use pvs,pvscan,pvdisplay to display the physical volumes in the current system.

# pvs

PV VG Fmt Attr PSize PFree

/ dev/sda2 VolGroup00 lvm2 a-3.88G 0

/ dev/sdb lvm_test lvm2 a-508.00M 0

/ dev/sdc lvm2-512.00M 512.00m

# pvscan

PV / dev/sdb VG lvm_test lvm2 [508.00 MB / 0 free]

PV / dev/sda2 VG VolGroup00 lvm2 [3.88 GB / 0 free]

PV / dev/sdc lvm2 [512.00 MB]

Total: 3 [4.87 GB] / in use: 2 [4.37 GB] / in no VG: 1 [512.00 MB]

# pvdisplay

-Physical volume

PV Name / dev/sdb

VG Name lvm_test

PV Size 512.00 MB / not usable 4.00 MB

Allocatable yes (but full)

PE Size (KByte) 4096

Total PE 127

Free PE 0

Allocated PE 127

PV UUID Pkp5Cq-SD1w-ANw2-cDDe-BGtw-nmFS-jTxXFD

-NEW Physical volume

PV Name / dev/sdc

VG Name

PV Size 512.00 MB

Allocatable NO

PE Size (KByte) 0

Total PE 0

Free PE 0

Allocated PE 0

PV UUID BNCVEE-YWlK-0mrV-LOcf-0tCY-WWNw-DeySk0

5. Remove physical volumes

# pvremove / dev/sdc

Labels on physical volume "/ dev/sdc" successfully wiped

6. Create a volume group

The command to create a volume group is vgcreate, which creates a complete volume group from the physical volume established using pvcreate:

# vgcreate lvm_test / dev/sdc1 / dev/sdb

Volume group "lvm_test" successfully created

The first argument to the vgcreate command is to specify the logical name of the volume group: lvm_test. The latter parameter is to specify all partitions and disks that you want to add to the volume group. In addition to creating a volume group lvm_test, vgcreate also sets to use a PE of size 4MB (default is 4MB), which means that all logical volumes created on the volume group are expanded or shrunk in increments of 4MB. The minimum PE is 1KB and must always be a multiple of 2 ^ n of 1KB (specified with-s, see man vgcreate for details).

7. Activate the volume group

Volume groups are activated by default when they are created, or you can use vgchange to activate volume groups:

# vgchange-a y lvm_test

8. Add a new physical volume to the volume group

When the system installs a new disk or partition and creates a new physical volume, and you want to add it to an existing volume group, you need to use the vgextend command:

# fdisk-l / dev/sdc

Disk / dev/sdc: 18.2 GB, 18200739840 bytes

255 heads, 63 sectors/track, 2212 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/ dev/sdc1 1 200 1606468 + 8e Linux LVM

/ dev/sdc2 201 400 1606500 8e Linux LVM

# pvcreate / dev/sdc2

Physical volume "/ dev/sdc2" successfully created

# vgextend lvm_test / dev/sdc2

Volume group "lvm_test" successfully extended

Here / dev/sdc2 is the new physical volume.

9. Show volume groups

Shows that volume groups can use vgs and vgdisplay.

# vgs

VG # PV # LV # SN Attr VSize VFree

VolGroup00 1 2 0 wz--n- 3.88G 0

Lvm_test 1 1 0 wz--n- 508.00M 0

# vgdisplay

-Volume group

VG Name lvm_test

System ID

Format lvm2

Metadata Areas 1

Metadata Sequence No 10

VG Access read/write

VG Status resizable

MAX LV 0

Cur LV 1

Open LV 1

Max PV 0

Cur PV 1

Act PV 1

VG Size 508.00 MB

PE Size 4.00 MB

Total PE 127

Alloc PE / Size 127 / 508.00 MB

Free PE / Size 0 / 0

VG UUID uJx24t-WWdY-vffu-97Of-mgFB-FEov-eRwzAf

10. Scan the disk to generate cache files

# vgscan

Reading all physical volumes. This may take a while...

Found volume group "lvm_test" using metadata type lvm2

Found volume group "VolGroup00" using metadata type lvm2

11. Remove a physical volume from the volume group

To delete a physical volume from a volume group, first to verify that the physical volume to be deleted is not in use by all logical volumes, use the pvdisplay command to view the physical volume information:

# pvdisplay / dev/sdc2

-Physical volume

PV Name / dev/sdc2

VG Name lvm_test

PV Size 1.53 GB / not usable 868.00 KB

Allocatable yes

PE Size (KByte) 4096

Total PE 392

Free PE 392

Allocated PE 0 (indicates that it is not used)

PV UUID jAiils-1vRz-Td9k-1AiD-kIJs-191z-YMz09H

If a physical volume is being used by a logical volume, you need to back up the data of that physical volume somewhere else and then delete it. The command to delete the physical volume is vgreduce:

# vgreduce lvm_test / dev/sdc2

Removed "/ dev/sdc2" from volume group "lvm_test"

12. Create a logical volume

The command to create a logical volume is lvcreate:

# lvcreate-L 1500? n test lvm_test

Logical volume "test" created

This command creates a logical volume with the name test and size 1500m on the volume group lvm_test, and the device entry is / dev/lvm_test/test (lvm_test is the volume group name and test is the logical volume name). If you want to create a logical volume that uses all volume groups, you need to first look at the PE number of the volume group, and then specify when you create the logical volume:

# vgdisplay lvm_test | grep "Total PE"

Total PE 4731

# lvcreate-l 4731 lvm_test-n test

Logical volume "test" created

13. Create striped logical volumes

# lvcreate-L 500m-i2-n test lvm_test

Using default stripesize 64.00 KB

Rounding size (125extents) up to stripe boundary size (126extents)

Logical volume "test" created

-i2 means that this logical volume stores data in chunks between two physical volumes, and the default size is 64KB.

14. Create the logical volume of the image.

# lvcreate-L 52m-M1-n test lvm_test / dev/sdb1 / dev/sdc1 / dev/sdb2

Logical volume "test" created

-M1 means only a single image is generated, the image is placed on / dev/sdb1 and / dev/sdc1, and the image log is placed on / dev/sdb2.

Create a snapshot volume.

# lvcreate-size 10-snapshot-name snaptest / dev/lvm_test/test

16. Create a file system

If you use the ext3 file system:

# mkfs.ext3 / dev/lvm_test/test

Mke2fs 1.35 (28-Feb-2004)

Max_blocks 4294967295, rsv_groups = 0, rsv_gdb = 1024

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

2424832 inodes, 4844544 blocks

242227 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=8388608

148 block groups

32768 blocks per group, 32768 fragments per group

16384 inodes per group

Superblock backups stored on blocks:

32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208

4096000

Writing inode tables: done

Inode.i_blocks = 90120, i_size = 4243456

Creating journal (8192 blocks): done

Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 25 mounts or

180 days, whichever comes first. Use tune2fs-c or-i to override.

Once you have created the file system, you can load and use it:

# mkdir / mnt/test

# mount / dev/lvm_test/test / mnt/test

If you want the file system to be loaded automatically when the system starts, you also need to add something to / etc/fstab:

/ dev/lvm_test/test / mnt/test ext3 defaults 1 2

17. Delete a logical volume

Before deleting a logical volume, you need to unmount it, and then delete it:

# umount / dev/lvm_test/test

# lvremove / dev/lvm_test/test

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

Logical volume "test" successfully removed

18. Expand the logical volume size

LVM provides the ability to easily resize logical volumes, and the command to expand the logical volume size is lvextend:

# lvextend-L12G / dev/lvm_test/test

Extending logical volume test to 12.00 GB

Logical volume test successfully resized

The above command implements the expansion of the logical volume test to 12G.

# lvextend-Log 1G / dev/lvm_test/test

Extending logical volume test to 13.00 GB

Logical volume test successfully resized

The above command increases the size of the logical volume test by 1G.

After you increase the capacity of the logical volume, you need to modify the file system size to take advantage of the expanded space.

If you want to resize the loaded file system, use ext2online (ext2resize) or resize2fs.

# df-h

# ext2online / dev/lvm_test/test

Ext2online / dev/lvm_test/test

Ext2online v1.1.18-2001-03-18 for EXT2FS 0.5b

# df-h

Filesystem capacity used available used mount point

/ dev/mapper/VolGroup00-LogVol01

16G 11G 3.6G 76% /

/ dev/sda1 494m 18m 451m 4% / boot

None 506m 0506m 0% / dev/shm

/ dev/mapper/lvm_test-test

13G 63m 13G 1% / mnt/test

It is generally recommended that you unmount the file system, resize it, and then load it:

# umount / dev/lvm_test/test

# resize2fs / dev/lvm_test/test

# mount / dev/lvm_test/test / mnt/test

19. Reduce logical volume size

You can use lvreduce to achieve the capacity of logical volumes, and you also need to unmount the file system first:

# umount / mnt/test

# e2fsck-f / dev/lvm_test/test

# resize2fs / dev/lvm_test/test 11G

# lvreduce-L-1.992G / dev/lvm_test/test (size less than 2 PE)

# resize2fs / dev/lvm_test/test (use this command to confirm whether the file system size matches the shrinking logical volume size)

# mount / dev/lvm_test/test / mnt/test

20. Display logical volumes

You can see the logical volumes that exist in the current system through lvscan,lvs and lvdisplay.

# lvdisplay

-Logical volume

LV Name / dev/VolGroup00/LogVol00

VG Name VolGroup00

LV UUID sPmLMD-6xq4-wStB-uSAP-jlQc-YKTm-3bt8Pc

LV Write Access read/write

LV Status available

# open 1

LV Size 2.88 GB

Current LE 92

Segments 1

Allocation inherit

Read ahead sectors 0

Block device 253:0

# lvscan

Lvscan

ACTIVE'/ dev/VolGroup00/LogVol00' [2.88 GB] inherit

ACTIVE'/ dev/VolGroup00/LogVol01' [1.00 GB] inherit

# lvs-o + devices

LV VG Attr LSize Origin Snap% Move Log Copy% Devices

LogVol00 VolGroup00-wi-ao 2.88G / dev/sda2 (0)

LogVol01 VolGroup00-wi-ao 1.00G / dev/sda2 (92)

21. Use filtering to control the scanning of LVM devices

Define which devices to scan to filter by editing the filter segment in / etc/lvm/lvm.conf.

Filter = ["a | / dev/sd.* |", "a | / dev/hd.* |", "r |. * |"]

Scsi and ide devices are scanned on the top, and other devices are not scanned.

22. Online data migration

Through pvmove, data on one PV can be migrated to a new PV, and a LV on PV can be migrated to another PV.

# lvs-o + devices

LV VG Attr LSize Origin Snap% Move Log Copy% Devices

LogVol00 VolGroup00-wi-ao 2.88G / dev/sda2 (0)

LogVol01 VolGroup00-wi-ao 1.00G / dev/sda2 (92)

Test lvm_test-wi-ao 52.00m / dev/sdb1 (0)

# pvmove-n test / dev/sdb1 / dev/sdc1

# lvs-o + devices

LV VG Attr LSize Origin Snap% Move Log Copy% Devices

LogVol00 VolGroup00-wi-ao 2.88G / dev/sda2 (0)

LogVol01 VolGroup00-wi-ao 1.00G / dev/sda2 (92)

Test lvm_test-wi-ao 52.00m / dev/sdc1 (0)

23. Delete a volume group

Unmount the file system sequentially, delete the logical volume, and then delete the volume group.

# umount / mnt/test

# lvremove / dev/lvm_test/test

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

Logical volume "test" successfully removed

# vgremove lvm_test

Volume group "lvm_test" successfully removed

24. Troubleshooting

More detailed command output can be obtained by adding-v vvvv VV or-VV after the command.

You can better view failed devices by adding-P after lvs,vgs.

# vgs-a-o + devices-P

Partial mode. Incomplete volume groups will be activated read-only.

VG # PV # LV # SN Attr VSize VFree Devices

VolGroup00 1 20 wz--n- 3.88G 0 / dev/sda2 (0)

VolGroup00 1 20 wz--n- 3.88G 0 / dev/sda2 (92)

Lvm_test 2 20 wz--n- 1016.00M 896.00M pvmove0 (0)

Lvm_test 2 20 wz--n- 1016.00M 896.00M / dev/sdb (0), / dev/sdc (0)

# lvs-a-o + devices-P

Partial mode. Incomplete volume groups will be activated read-only.

Failure parsing mirror status fraction: 1 core

Failure parsing mirror status fraction: 1 core

LV VG Attr LSize Origin Snap% Move Log Copy% Devices

LogVol00 VolGroup00-wi-ao 2.88G / dev/sda2 (0)

LogVol01 VolGroup00-wi-ao 1.00G / dev/sda2 (92)

[pvmove0] lvm_test p-C-ao 60.00m / dev/sdb / dev/sdb (0), / dev/sdc (0)

Test lvm_test-wI-a- 60.00m pvmove0 (0)

V. Summary

As you can see from the above discussion, LVM is very scalable and easy to use. It can easily adjust the size of volume groups and logical volumes, further adjust the size of the file system, and facilitate data migration and data integrity protection. If you want more information, please refer to LVM-HOWTO.

This file uses kernel 2.6.22.1, and there is an error when doing pvmove, as above. Patches are as follows:

Index: linux-2.6.21/drivers/md/dm-raid1.c

=

-linux-2.6.21.orig/drivers/md/dm-raid1.c 2007-05-13 18 purl 56 purl 09.000000000 + 0100

+ linux-2.6.21/drivers/md/dm-raid1.c 2007-05-13 1914 09Vera 54.000000000 + 0100

@ @-1288 struct dm_targe 12 + 1288 pm 12 @ @ static int mirror_status (struct dm_targe

For (m = 0; m nr_mirrors; masking +)

DMEMIT ("% s", ms- > Mirror [m] .dev-> name)

-DMEMIT ("% llu/%llu"

+ DMEMIT ("% llu/%llu"

(unsigned long long) ms- > rh.log- > type- >

Get_sync_count (ms- > rh.log)

(unsigned long long) ms- > nr_regions)

-sz = ms- > rh.log- > type- > status (ms- > rh.log, type, result, maxlen)

+ sz + = ms- > rh.log- > type- > status (ms- > rh.log, type, result+sz, maxlen-sz)

Break

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