How to manage lvm Logical Volumes

Source: Internet
Author: User
For a hard disk with three partitions: 100 M, 100 M, and 200 M, what if one day M cannot be installed? Can I divide some space of the newly added hard disk into MB space? The logical volume can be implemented, which means we can dynamically increase or decrease the volume (partition) size. LVM (logicalvolumemanager) is built on the hard disk and

For a hard disk with three partitions: 100 M, 100 M, and 200 M, what if one day M cannot be installed? Can I divide some space of the newly added hard disk into MB space?

The logical volume can be implemented, which means we can dynamically increase or decrease the volume (partition) size.

LVM (logical volumemanager) is a logical layer built on hard disks and partitions to improve the flexibility of disk partitions. Lvm is a logical layer added between the disk partition and the file system. It provides an abstract disk volume and then creates a file system on this disk volume.

Related knowledge:

1. physical storage media: hard disk (/dev/sda1,/dev/sdb ...)

2. physical volume (physicalVolume): refers to a hard disk partition, which is the basic storage logic block of LVM and contains parameters related to LVM.

3. Volume Group: similar to a physical hard disk in a non-LVM system, it is composed of physical volumes. You can create one or more LVM partitions (logical volumes) in the volume group. The LVM volume group consists of one or more physical volumes.

4. logical volume: similar to hard disk partitions in a non-LVM system, you can create a file system on a logical volume.

5. PE: physical area block, which is the smallest allocable storage unit in a physical volume.

6. LE: logical area block, which is the minimum storage unit that can be allocated in a logical volume.

Logical Volume creation process: physical volume --> Volume group --> logical volume

1. create an LVM partition. you can use the fdisk in the system to create an LVM partition. The difference is to change the partition type to LINUXLVM, that is, 8E.

2. create a physical volume

Use pvcreate to create a partition or disk that you want to add to the volume group into a physical volume for LVM management.

For example, pvcreate/Dev/sdb1 # create the partition as a physical volume

3. create a volume Group

Create with vgcreate, vgcreate volume group namePhysical volume...

For example, vgcreate vg0/Dev/sdb1

When creating a volume group, the system uses a block of 4 MB as the PE size. The block size determines the maximum capacity of a single logical volume. the PE size range is 8KB-512MB and is always a multiple of 2.

The size of the logical volume is expanded or reduced by the PE size. to make a single logical volume have more space, you need to manually specify the size, for example:

Vgcreate-s 8 M vg0/Dev/sdb1

4. activate the volume Group

Vgchange-ay volume group name

-Ay indicates active yes, that is, activation. If-an is used, activation is disabled.

5. create a logical volume

Lvcreate-L logical volume size-N logical volume nameVolume group name

Indicates creating a logical volume in a volume group. The size is specified by-L. Note that the size should be a multiple of PE and the size can be expressed by the number of-l blocks.

For example, lvcreate-n boot-L 256 M

After creating a logical volume, you must format it before using it. Mkfs. ext3/dev/vg0/boot

6. add a new physical volume to the volume group.

Vgextend volume group namePhysical volume

For example, vgextend vg0/dev/sdb2

7. expand the capacity of the logical volume

Number of lvextend-l + PE blocksLogical Volume name.

-L to keep up with the number of blocks, you can use vgdisplay to check the size of the volume group, and then allocate it to the specified logical volume. of course, you can also specify the-L + capacity to add space.

For example, lvextend-l + 5/dev/vg0/boot # because the PE size is 8 M, 40 M space is added.

8. reduce logical volume capacity

Lvreduce-L-capacityLogical Volume name

-Capacity indicates the capacity reduced from the current logical volume.

For example:

Lvreduce-L-10G/Dev/vg0/boot# Reduce the boot partition space by 10 GB.

If the minus sign is not specified, it will be 10 GB instead of 10 GB.

9. delete a logical volume

LvremoveLogical Volume name

For example, lvremove/dev/vg0/boot

10. delete a volume Group

VgremoveVolume group name

For example, vgremove/dev/vg0/boot

11. delete a physical volume

PvremovePhysical volume name

For example, pvremove/dev/sda1

When you delete a volume, the logical volume> Volume Group> physical volume is added in the reverse order.

Test in virtual machine:

Both hard disks are 500 GB,/dev/sda,/dev/sdb

1. create/dev/sda partition (completed with fdisk)

Device BootStart End BlocksIdSystem

/Dev/sda1 20484194324472097152008eLinux LVM

/Dev/sda24194324488388628472097152008eLinux LVM

/Dev/sda383886284810485759991048565768eLinux LVM

Note: the id is 8e, and it doesn't matter if it is anything else.

II. create a PV physical volume

Root @ ubuntu:/dev # pvcreate/Dev/sda1/dev/sda2/dev/sda3

Physical volume "/dev/sda1" successfully created

Physical volume "/dev/sda2" successfully created

Physical volume "/dev/sda3" successfully created

3. create a volume Group

Root @ ubuntu:/dev # vgcreate-S 8 M vg0/dev/sda1/dev/sda2

Volume group "vg0" successfully created

The size of the PE block in the volume Group is 8 MB, and the volume group name is vg0. the physical volume sdad1 and sda2 are put into the volume group, and sda3 is not put.

4. create a logical volume

Root @ ubuntu :~ # Lvcreate-n boot-L 256 M vg0

The link/dev/vg0/boot shoshould had been created by udev but itwas not found. Falling back to direct link creation.

Logical volume "boot" created

Root @ ubuntu :~ # Mkfs. ext4/dev/vg # format a logical volume (partition)

Follow this method to create other logical volumes

Lvcreate-n root-L 100G vg0

Lvcreate-n home-L 299G vg0

You can use vgdisplay to view the volume group space.

Root @ ubuntu :~ # Vgdisplay

--- Volume group ---

VG Name Vg0

System ID

Format Lvm2

Metadata Areas 2

Metadata Sequence No6

VG Access Read/write

VG Status Resizable

MAX LV 0

Cur LV 3

Open LV 0

Max PV 0

Cur PV 2

Act PV 2

VG Size 399.98 GiB # total available VG size.

PE Size 8.00 MiB # The block size is 8 MB

Total PE 51198 # total number of PE blocks

Alloc PE/Size51198/399 .98 GiB #51198 indicates the allocated block

FreePE/Size0/0# Zero free space left, indicating that all entries have been divided

VG UUID JnxPsp-74TB-u7Wu-XRLA-1xD4-iuf4-mJrp2l

5. Add/dev/sda3 to vg0 of the volume group.

Root @ ubuntu :~ # VgextendVg0/dev/sda3

Volume group "vg0" successfully extended

The volume group information is displayed again, and 99 GB of free space is found. This indicates that the free space can be added at any time.

6. extend the logical volume size

Root @ ubuntu :~ # Lvextend-l + 12799/dev/vg0/boot

Extending logical volume boot to 99.99 GiB

Logical volume boot successfully resized

You can use lvdisplay/dev/vg0/boot to check whether the capacity has increased.

I installed javastuserver on the virtual machine, and then added the second hard disk to VG. And adjusted the partition size. it seems that this function is really good.

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.