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 use lvm under linux

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces how to use lvm under linux, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

A brief introduction to LVM

LVM (Logical Volume Manager) is a logical volume manager applied to the kernel of Linux and a mechanism for disk partition management in Linux environment. First introduce a few nouns about LVM

1. PV (physical volume) can be a disk and a partition. Composed of PE (physical extent), multiple PV can form a VG (volume group).

2. VG (volume group) A group composed of multiple physical volumes, but the volume group cannot be used directly. You need to create a LV (logical volume) on it before you can use it. Multiple LV can be created on VG.

3. PE (physical extent) is like the block block of the disk we learned earlier, and the default is 4MB.

4. LV (logical volume) is an available space built on top of a volume group. There are two kinds of boundaries: physical boundary and logical boundary.

2. Introduction of related commands

1. Physical volume PV

Pvcreate creates pv example: pvcreate / dev/sda1

Pvs to view pv information example: enter pvs directly on the command line

Pvdisplay View pv details example: pvcreate / dev/sda1

Pvmove moves pv data to another pv example: pvmove / dev/sda1 / dev/sda4

Pvremove deletes pv example: pvremove / dev/sda1

2. Volume group VG

2.1 create, view, delete vg

Vgcreate creates vg as an example: vgcreate myvg / dev/sda {mrecom n}

Vgs to view vg information example: enter vgs directly on the command line

Vgdisplay View vg details example: vgdisplay myvg

Vgremove deletes vg example: vgremove myvg

2.2 extended vg

First of all, prepare a PV.

Then use the vgextend command to complete the extension

Format: vgextend VG_NAME / PATH/TO/PV

Vgextend myvg / dev/sdb2 extends the myvg volume group, adding / dev/sda2

2.3 reduce vg

1) determine the PV to be removed

2) transfer the data on this PV to another PV

Pvmove / path/to/pv

3) remove this PV from the volume group

Format: vgreduce VG_NAME / PATH/TO/PV

Vgreduce myvg / dev/sdb2 reduce the myvg volume group and remove / dev/sda2

3 logical Volume LV

3.1 create, view, delete lv

Lvcreate creates lv example: lvcreate lv1 / dev/myvg

Lvs to view lv information example: enter lvs directly on the command line

Lvdisplay View lv details example: vgdisplay lv1

Lvremove removes lv example: lvremove / dev/myvg/lv1

3.2 extend lv

Determine how big the expansion is

Make sure that the volume group in which the current logical volume resides has enough free space

Extend:

1. Physical boundary

Lvextend-L [+] SIZE / path/to/lv

2. Logical boundary

Resize2fs / path/to/device

3. 3 reduce lv

Determine the size of the reduction, but only if it can accommodate at least all the original data.

Shrink:

1) unmount and forcibly detect the file system

E2fsck-f

2) logical boundary

Resize2fs / path/to/device SIZE

3) physical boundary

Lvreduce-L [-] SIZE / path/to/lv

4. Snapshot Volume

1) the lifecycle is the entire data duration, during which the growth of data cannot exceed the snapshot volume size.

2) the snapshot volume should be read-only

3) in the same volume group as the original volume

Lvcreate

-L specifies the size of the snapshot volume

-s create snapshot volum

The snapshot volume created by-p r is read-only

Format:

Lvcreate-L SIZE-s-pr-n LV_NAME / path/to/lv

III. An example of lv

1. Create a partition

The code is as follows:

[root@stau11 ~] # fdisk-l / / View the partition

[root@stau11 ~] # fdisk / dev/sda / / to partition

Command (m for help): n / / n to add a partition, p view partition

First cylinder (2898-10443, default 2898): / / Press the [Enter] key to decide

Using default value 2898 / / start cylinder Select default

Last cylinder or + size or + sizeM or + sizeK (289810443, default 10443): + 2G / / give the size of 2G

Command (m for help): t / / change partition type

Partition number (1-5): 5 / / selected partition

Hex code (type L to list codes): l / / list of partition types

Hex code (type L to list codes): 8e / / Mark the newly added partition as lvm

Command (m for help): P / / View partition table

/ dev/sda5 2756 2999 1959898 + 8e Linux LVM

Command (m for help): W / / Save

[root@localhost ~] # partprobe / dev/sda / / is the system identification partition (instead of reboot)

[root@localhost ~] # mkfs.ext3 / dev/sda5 / / format the newly added partition

2. The creation of PV

The code is as follows:

[root@localhost ~] # pvcreate / dev/sda5 / / create pv

Writing physical volume data to disk "/ dev/sda5"

Physical volume "/ dev/sda5" successfully created

[root@localhost ~] # pvs / / View the current pv information

PV VG Fmt Attr PSize PFree

/ dev/sda5 lvm2 Amuri-1.87G 1.87G

3. The creation of VG

The code is as follows:

[root@localhost ~] # vgcreate vg0 / dev/sda5 / / vg0 is the name of the current vg

Volume group "vg0" successfully created

[root@localhost ~] # vgs / / View the information of vg

VG # PV # LV # SN Attr VSize VFree

Vg0 1 0 0 wz--n- 1.87G 1.87G

4. The creation of lv

The code is as follows:

[root@localhost] # lvcreate-L 500m-n lv01 vg0

Logical volume "lv01" created

[root@localhost ~] # lvs / / View the information of lv

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

Lv01 vg0-wi-a- 500.00m

You have mail in / var/spool/mail/root

[root@localhost ~] # mkfs.ext3 / dev/vg0/lv01 / / format

The use of lvm

5. The increase of lv

The code is as follows:

[root@localhost ~] # lvextend-L + 300m / dev/vg0/lv01 / / (original lv size 500m, stretch partition to 800m) Extending logical volume lv01 to 800.00 MB

Logical volume lv01 successfully resized

[root@localhost ~] # resize2fs / dev/vg0/lv01 / / stretch the file system

[root@localhost ~] # lvs

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

Lv01 vg0-wi-ao 800.00m / / size becomes 800m

6. Reduction of lv

The code is as follows:

# umount / dev/vg0/lv01 / / unmount logical volumes / dev/vg0/lv01

# df-h / / View

# e2fsck-f / dev/vg0/lv01 / / disk check

# resize2fs / dev/vg0/lv01 500m / / retract the file system to 500m

# lvreduce-L 500m / dev/vg0/lv01 / / retract partition to 500m

WARNING: Reducing active logical volume to 500.00 MB

THIS MAY DESTROY YOUR DATA (filesystem etc.)

Do you really want to reduce lv01? [y/n]: y

# lvs

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

Lv01 vg0-wi-a- 500.00m / / now 500m size

# mount / dev/vg0/lv01 / mnt/lv01 / / remount the partition

# df-h / / View

# ls / mnt/lv01 / / View files

IV. System snapshot of lvm

1. Mount lvm

The code is as follows:

[root@localhost ~] # mount / dev/vg0/lv01 / mnt/lv01

[root@localhost ~] # cd / mnt/lv01

[root@localhost lv01] # touch {a,b,c} {a,b,c}

[root@localhost lv01] # ls

Aa ab ac ba bb bc ca cb cc lost+found lv.file restoresymtable

2. Create a snapshot for lvm

The code is as follows:

[root@localhost lv01] # lvcreate-L 64m-s-n lv-backup / dev/vg0/lv01

Logical volume "lv-backup" created

3. Mount the snapshot

The code is as follows:

[root@localhost lv01] # mkdir / mnt/lv-backup

[root@localhost mnt] # mount-o ro / dev/vg0/lv-backup / mnt/lv-backup/

4. Back up the snapshot

The code is as follows:

[root@localhost tmp] # dump-0u-f / tmp/lv-backup.dump / mnt/lv-backup/ backup

DUMP: Date of this level 0 dump: Sun Nov 11 14:53:31 2012

DUMP: Dumping / dev/mapper/vg0-lv--backup (/ mnt/lv-backup) to / tmp/lv-backup.dump

5. Delete a snapshot

The code is as follows:

[root@localhost tmp] # umount / mnt/lv-backup/

[root@localhost tmp] # lvremove / dev/vg0/lv-backup

Do you really want to remove active logical volume lv-backup? [y/n]: y

Logical volume "lv-backup" successfully removed

6. Clear the content under / dev/vg0/lv01

The code is as follows:

[root@localhost tmp] # umount / mnt/lv01

[root@localhost tmp] # mkfs.ext3 / dev/vg0/lv01

Mke2fs 1.39 (29-May-2006)

Filesystem label=

OS type: Linux

7. Shutdown of lvm

The code is as follows:

[root@localhost ~] # umount / mnt/lv01

[root@localhost ~] # lvremove / dev/vg0/lv01 / / Delete lv

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

Logical volume "lv01" successfully removed

[root@localhost ~] # vgchange-an vg0 / / makes vg0 without the active flag

0 logical volume (s) in volume group "vg0" now active

[root@localhost ~] # vgremove vg0 / / Delete vg

Volume group "vg0" successfully removed

[root@localhost ~] # pvremove / dev/sda5 / / Delete pv

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

Thank you for reading this article carefully. I hope the article "how to use lvm under linux" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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