LVM management in Linux

Source: Internet
Author: User
For Linux users, when installing a Linux machine, one of the problems encountered is to estimate and assign sufficient hard disk space to each partition. This is a very common problem for a system administrator who is looking for space for a server or a common user who is about to use up a disk. The solution is to use symbolic links or

For Linux users, when installing a Linux machine, one of the problems encountered is to estimate and assign sufficient hard disk space to each partition. This is a very common problem for a system administrator who is looking for space for a server or a common user who is about to use up a disk. The solution is usually to use symbolic links or some tools (such as parted) to adjust the partition size ). However, this is only a temporary solution. soon, we will face the same problem.

If you are the system administrator of a website and manage a large number of servers connected to the Internet, every minute you shut down, it will bring great losses to the company. In addition, after the partition table is modified, the system must be restarted every time. LVM (logical volume manager) can help us solve these problems.

LVM introduction

LinuxLVM makes management easier. Compared with hard disks and partitions, LVM looks at the storage space at a higher level. Before using LVM, let's take a look at some concepts that will be used.

Physical volume

A physical volume refers to a hard disk partition or a device (such as a RAID device) that looks logically similar to a hard disk partition ).

Logical Volume

One or more physical volumes form a logical volume. For LVM, logical volumes are similar to hard disk partitions in non-LVM systems. A logical volume can contain a file system (such as/home or/usr ).

Volume Group

One or more logical volumes form a volume group. For LVM, volume groups are similar to physical hard disks in non-LVM systems. A volume group combines multiple logical volumes to form a manageable unit.

How LVM works

Let's take a look at how LVM works. Each physical volume is divided into several basic units, namely, PhysicalExtents ). The PE size is variable, but must be the same as the physical volume of the volume group to which it belongs. In each physical volume, each PE has a unique number. PE is the smallest unit in physical storage that can be addressed by LVM.

Each logical volume is also divided into some basic units that can be addressable, that is, the so-called LE (LogicalExtents ). In the same volume group, the size of LE is the same as that of PE. Obviously, the size of LE is the same for all logical volumes in a volume group.

In a physical volume, each PE has a unique number, but this is not necessarily necessary for a logical volume. This is because logical volumes can be grouped by physical volumes when these PEID numbers cannot be used. Therefore, the LEID is used to identify LE and the specific PE associated with it. As mentioned above, LE and PE correspond one to one. Each time the storage area is accessed by addressing or the ID of the LE is used, data is written on the physical storage device.

You may wonder where all the metadata about the logical volume and logical volume Group is stored. Similarly, in a non-LVM system, partition data is stored in the partition table, and the partition table is stored in the starting position of each physical volume. The VGDA (volume group descriptor region) function is like a LVM partition table, which is stored at the beginning of each physical volume.

VGDA consists of the following information:

· A pv descriptor

· A vg descriptor

· LV descriptor

· Some PE descriptors

When the system starts the LV, the VG is activated and the VGDA is loaded to the memory. VGDA helps identify the actual storage location of the LV. When the system wants to access the storage device, the ing mechanism established by VGDA is used to access the actual physical location for I/O operations.

The following describes how to use LVM.

Step 1: configure the kernel. Before installing LVM, the LVM module should be included in the kernel. you can follow these steps:

# Cd/usr/src/linux

# Make menuconfig

Select the Multi-device Support (RAID and LVM) sub-menu and select the following two options:

[*] Multiple devices driver support (RAID and LVM)

<*> Logical volume manager (LVM) Support.

Note: If you have installed LVM software packages when installing Linux, skip the preceding steps and go directly to step 2.

Step 2: check the total amount of free disk space on the drive. You can run the following command to complete the process:

# Df-h

Filesystem Size Used Avail Use % Mounted on

/Dev/hda1 3.1G 2.7G 398 M 87%/

/Dev/hda2 4.0G 3.2G 806 M 80%/home

/Dev/hda5 2.1G 1.0G 1.1G 48%/var

Step 3: Create an LVM partition on the hard disk. Use fdisk or other partitioning tools to create an LVM partition. The partition type of LinuxLVM is 8e.

# Fdisk/dev/hda

Press p (to print the partition table) and n (to create a newpartition)

Step 4: create a physical volume. The following command creates a volume group descriptor at the beginning of the partition:

# Pvcreate/dev/hda6

Pvcreate ---physical volume "/dev/hda6" successfullycreated

# Pvcreate/dev/hda7

Pvcreate--- physical volume "/dev/hda7" successfullycreated

Step 5: Create a volume group. Use the following method to create a new volume group and add two physical volumes:

# Vgcreate test_lvm/dev/hda6/dev/hda7

Vgcreate--- INFO: using default physical extent size 4 MB

Vgcreate--- INFO: maximum logical volume size is 255.99 Gigabyte

Vgcreate--- doing automatic backup of volume group "test_lvm"

Vgcreate--- volume group "test_lvm" successfully created andactivated

The preceding command creates a volume group named test_lvm, which contains two physical volumes:/dev/hda6 and/dev/hda7. Use the following command to activate a volume group:

# Vgchange-ay test_lvm

Run the "vgdisplay" command to view the details of the created volume group.

# Vgdisplay

--- Volume group ---

VG Name test_lvm

VG Access read/write

VG Status available/resizable

VG #0

MAX lv256

Cur LV 1

Open LV 0

Max lv Size 255.99 GB

Max PV 256

Cur PV 2

Act PV 2

VG Size 3.91 GB

PE Size 4 MB

Total PE 1000

Alloc PE/Size 256/1 GB

Free PE/Size 744/2 .91 GB

Vg uuid T34zIt-HDPs-uo6r-cBDT-UjEq-EEPB-GF435E

Step 6: create a logical volume. Use the lvcreate command to create a logical volume in the volume group:

# Lvcreate-L2G-nlogvol1 test_lvm

Step 7: create a file system. On this logical volume, select to use the reiserfs log file system:

# Mkreiserfs/dev/test_lvm/logvol1

Use the mount command to load the new file system.

# Mount-t reiserfs/dev/test_lvm/logvol1/mnt/lv1

Step 8: add an entry in/etc/fstab and/etc/lilo. conf. Add the following entry to/etc/fstab to load the file system at startup:

/Dev/test_lvm/logvol1/mnt/lv1 reiserfs defaults 1 1

If the original kernel is not overwritten, copy a re-compiled kernel and choose whether to use LVM at startup. The content of the LILO file is as follows:

Image =/boot/lvm_kernel_image

Label = linux-lvm

Root =/dev/hda1

Initrd =/boot/init_image

Ramdisk = 8192

After adding the preceding content, run the following command to reload LILO:

#/Sbin/lilo

Step 9: modify the logical volume size. You can use the lvextend command to conveniently modify the logical volume size. the method to increase the logical volume size is as follows:

# Lvextend-L + 1G/dev/test_lvm/logvol1

Lvextend -- extending logical volume "/dev/test_lvm/logvol1" to 3 GB

Lvextend -- doing automatic backup of volume group "test_lvm"

Lvextend -- logical volume "/dev/test_lvm/logvol1" successfully extended


Similarly, the method for reducing the logical volume size is as follows:

# Lvreduce-L-1G/dev/test_lvm/lv1

Lvreduce ---Warning: CiNG Cing active logical volume to2GB

Lvreduce--- This may destroy your data (filesystemetc .)

Lvreduce ---do you really want to reduce "/dev/test_lvm/lv1 "? [Y/n]: y

Lvreduce--- doing automatic backup of volume group "test_lvm"

Lvreduce--- logical volume "/dev/test_lvm/lv1" successfullyreduced

Summary

From the above discussion, we can see that LVM is highly scalable and intuitive to use. Once the volume group is established, it is easy to adjust the size of each logical volume as needed.

Commands related to LVM operations:

Fdisk-l: view the physical hard disks recognized in the system.

Pvdisplay: view the physical volumes that have been created in the system

Pvcreate: creates a new physical volume.

Pvremove: Delete a physical volume (that is, delete an LVM label from the physical volume)

Vgdisplay: View volume groups in the system

Vgcreate: create a new volume group.

Vgreduce: Delete a physical volume from the volume group (that is, shrink the volume group)

Vgremove: Delete a volume group.

Lvdisplay: view the logical volumes that have been created in the system

Lvcreate: create a new logical volume

Lvreduce: reduce the logical volume (that is, reduce some LE from a logical volume)

Lvremove: deletes a logical volume from the system.

Mkfs: creates a file system of the corresponding type based on the logical volume.

Mkdir-p $ mount_piont: Create a Mount Directory

The created file system is located:

/Dev/$ create_vg_name/$ lv_name

Mount/dev/$ create_vg_name/$ lv_name $ mount_piont: mount a file system

Vgscan: Read all volume groups created in the system.

Vgchange-a y: activate all the volume groups (the redhat can be found in the/etc/rc. d/rc. sysinit system startup initialization script)

Vgchange-a n: close all volume groups (note: the execution can be successful only after all umount file systems

Bare device usage:

1. lvreate first

2. raw/dev/raw/raw0/dev/mapper/vgname-lvname

3. modify/etc/sysconfig/rawdevices and add:

/Dev/raw/raw0/dev/mapper/vgname-lvname

4. run the command; service rawdevicesrestart to make the configuration of the bare device in the/etc/sysconfig/rawdevices file take effect.

5. run/sbin/schkconfig rawdevices on to automatically load bare devices after the system is restarted.

6. modify the owner of the bare device so that users with the corresponding permissions have the read and write permissions on the bare device.

Chown-R owner: group/dev/raw/raw0

7. add the modification command to the system startup execution script/etc/rc. local to keep the owner of the bare device unchanged after the system starts.

The above commands and operations require the root permission for execution. the specific usage is not described in detail. you can use the man manual.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.