Home > Operation and Maintenance > Linux Operation and Maintenance > Detailed explanation of examples of logical volume management lvm

Detailed explanation of examples of logical volume management lvm

PHP中文网
Release: 2017-06-21 11:44:22
Original
1754 people have browsed it

Logical Volume Management LVM

Create a logical volume

##1 Prepare partitions or hard disks

Here we use two hard disks of /dev/sdb and /dev/sdc and two partitions of /dev/sda9 and /dev/sda10, both in size. It's 1G and the disk is limited, so I don't want to be so picky.

Add partitions /dev/sda9, /dev/sda10

[root@centos7 ~]# fdisk /dev/sda

Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): n
All primary partitions are in use
Adding logical partition 9
First sector (31885312-41943039, default 31885312):
Using default value 31885312
Last sector, +sectors or +size{K,M,G} (31885312-41943039, default 41943039): +1G
Partition 9 of type Linux and of size 1 GiB is set
Copy after login
Note that you need to change the partition type to Linux LVM

Command (m for help): t
Partition number (1-9, default 9): 8e
Partition number (1-9, default 9): 9
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help):
Copy after login
Create /dev/sda10 in the same way.

[root@centos7 ~]# lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda9 8:9 0 1G 0 part
└─sda10 8:10 0 1G 0 part
sdb 8:16 0 1G 0 disk
sdc 8:32 0 1G 0 disk
Copy after login

2 Create PV (physical volume)

The command format is: pvcreate DEVICE, you can create one device at a time , you can also create multiple devices at once.

View physical volume commands: pvdisplay (view detailed information), pvs (view simple information)

[root@centos7 ~]# pvcreate /dev/sda9

Physical volume "/dev/sda9" successfully created.
[root@centos7 ~]# pvcreate /dev/sda10 /dev/sdb /dev/sdc
Physical volume "/dev/sda10" successfully created.
Physical volume "/dev/sdb" successfully created.
Physical volume "/dev/sdc" successfully created.
Copy after login
View the physical volume and see that it has been successfully created.

[root@centos7 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda10 lvm2 --- 1.00g 1.00g
/dev/sda9 lvm2 --- 1.00g 1.00g
/dev/sdb lvm2 --- 1.00g 1.00g
/dev/sdc lvm2 --- 1.00g 1.00g
Copy after login

3 Create a volume group (VG)

The command format is: vgcreate -s PE VGNAME (vg name ) DEVICE (device name)

-s: Specify the physical extension block size, which is the size of the PE

View commands: vgs, vgdisplay

[root@centos7 ~]# vgcreate -s 16M vg0 /dev/sda9 /dev/sda10 /dev/sdb /dev/sdc
Volume group "vg0" successfully created
Copy after login
Check it out

[root@centos7 ~]# vgs

VG #PV #LV #SN Attr VSize VFree
vg0 4 0 0 wz--n- 3.94g 3.94g
Copy after login
and then look at the details

[root@centos7 ~]# vgdisplay

--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 4
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 4
Act PV 4
VG Size 3.94 GiB
PE Size 16.00 MiB
Total PE 252
Alloc PE / Size 0 / 0
Free PE / Size 252 / 3.94 GiB
VG UUID qmoIMg-5wQR-GKCS-dpq9-HLrt-zVIi-JBC9ZX
Copy after login
Everything is as expected, the size is about 4G, which is exactly two partitions and two The sum of hard drives.

4 Create a logical volume (LV)

The command format is: lvcreate -n LVNAME (specify LV name) -L SIZE (specify LV size) vg0 (VG to be used)

-l Specify PE size

View command (similar to PV, VG): lvs, lvdisplay

[root@centos7 ~]# lvcreate -n lv0 -L 3G vg0

Logical volume "lv0" created.
Copy after login
Created successfully, check

[root@centos7 ~]# lvs

LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-a----- 3.00g
Copy after login
and then check the details

[root@centos7 ~]# lvdisplay

--- Logical volume ---
LV Path /dev/vg0/lv0
LV Name lv0
VG Name vg0
LV UUID 622i7m-uy6s-3nZI-8xSb-sxrR-cZzz-C9pwkd
LV Write Access read/write
LV Creation host, time centos7.3.loacl, 2017-04-25 16:25:13 +0800
LV Status available
# open 0
LV Size 3.00 GiB
Current LE 192
Segments 4
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
Copy after login
You can confirm that it has been successfully created.

Now you can use /dev/vg0/lv0 as a hard disk.

Now look at the disk information, it has changed.

[root@centos7 ~]# lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 4K 0 disk
sda 8:0 0 20G 0 disk
├─sda9 8:9 0 1G 0 part
│ └─vg0-lv0 253:0 0 3G 0 lvm
└─sda10 8:10 0 1G 0 part
└─vg0-lv0 253:0 0 3G 0 lvm
sdb 8:16 0 1G 0 disk
└─vg0-lv0 253:0 0 3G 0 lvm
sdc 8:32 0 1G 0 disk
└─vg0-lv0 253:0 0 3G 0 lvm
Copy after login
You can see that the added partition and hard disk already belong to vg0-lv0.

5 Create the file system and format it

mkfs.ext4 /dev/vg0/lv0

[root@centos7 ~]# mkfs.ext4 /dev/vg0/lv0
Copy after login

6 Mount using

[root@centos7 ~]# mkdir /mnt/lv0

[root@centos7 ~]# mount /dev/vg0/lv0 /mnt/lv0
Copy after login

Second expansion LV

Extension command format:

lvextend -L 100G /dev/vg0/lv0

lvextend -l +100G /dev/vg0/lv0

lvextend -l +100%FREE /dev/vg0/lv0
Copy after login
Note –L is how much to extend, -l is how much to extend (increase), +100%FREE is to extend 100% of the remaining vg, which can be adjusted according to personal circumstances Make your selection.

Since vg0 still reserves nearly 1G space, now expand lv0

1 Let’s check the original LV first

[root@centos7 ~]# vgs

VG #PV #LV #SN Attr VSize VFree
vg0 4 1 0 wz--n- 3.94g 960.00m
Copy after login

2 Extend LV

[root@centos7 ~]# lvextend -r -l +100%FREE /dev/vg0/lv0
Copy after login
-r is resizefs. It’s so convenient to do it in one step

Or It's a little troublesome, step by step

[root@centos7 ~]# lvextend -l +100%FREE /dev/vg0/lv0

Size of logical volume vg0/lv0 changed from 3.00 GiB (192 extents) to 3.94 GiB (252 extents).
Logical volume vg0/lv0 successfully resized.
Copy after login

3 Come down and redefine the file system size

ext series file system redefinition : resize2fs /dev/vg0/lv0

xfs series file system redefinition: xfs_growfs /dev/vg0/lv0

[root@centos7 ~]# resize2fs /dev/vg0/lv0

resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg0/lv0 is mounted on /mnt/lv0; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vg0/lv0 is now 1032192 blocks long.
Copy after login

4 Check the file System completion (optional)

[root@centos7 ~]# fsck.ext4 /dev/vg0/lv0

e2fsck 1.42.9 (28-Dec-2013)
/dev/vg0/lv0: clean, 20/262144 files, 35918/1032192 blocks
Copy after login

5 Confirm the logical volume size

[root@centos7 ~]# df -h /dev/vg0/lv0

Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-lv0 3.9G 12M 3.6G 1% /mnt/lv0
Copy after login
It is obvious that the expansion has been successful.

6 Mount using

[root@centos7 ~]# mount -o remount /dev/vg0/lv0
Copy after login

Three reduced LV

1 Backup data

Although there is generally no loss of data, for the sake of safety . It's better to back up first.

Look at the existing data in the /mnt/lv0 directory

[root@centos7 ~]# ls /mnt/lv0/

lost+found myfile2 myfile4 myfile6 myfile8

myfile1 myfile3 myfile5 myfile7 myfile9

[root@centos7 ~]# mkdir /backup

[root@centos7 ~]# cp -a /mnt/lv0/* /backup/
Copy after login

##2 Uninstall /dev/vg0/lv0

[root@centos7 ~]# umount /mnt/lv0/
Copy after login

3 Check the file systeme2fsck -f /dev/vg0/lv0 Redefining the file system Disk completion must be checked before size

[root@centos7 ~]# e2fsck -f /dev/vg0/lv0

e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg0/lv0: 20/262144 files (0.0% non-contiguous), 35918/1032192 blocks
Copy after login

4下来重定义文件系统大小

resize2fs /dev/vg0/lv0 2G #2G位缩减后的LV 大小。

[root@centos7 ~]# resize2fs /dev/vg0/lv0 2G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg0/lv0 to 524288 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 524288 blocks long.
Copy after login

5 缩减LV

lvreduce -L 2G /dev/vg0/lv0 #缩减到2G

[root@centos7 ~]# lvreduce -L 2G /dev/vg0/lv0

WARNING: Reducing active logical volume to 2.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg0/lv0? [y/n]: y
Size of logical volume vg0/lv0 changed from 3.94 GiB (252 extents) to 2.00 GiB (128 extents).
Logical volume vg0/lv0 successfully resized.
Copy after login

6 挂载使用

mount /dev/vg0/lv0 /mnt/lv0

[root@centos7 ~]# mount /dev/vg0/lv0 /mnt/lv0/
Copy after login

查看磁盘挂载情况

[root@centos7 ~]# df -h /dev/vg0/lv0

Filesystem Size Used Avail Use% Mounted on

/dev/mapper/vg0-lv0 2.0G 9.0M 1.8G 1% /mnt/lv0
Copy after login

7 确认数据是否丢失

现在再来开心目录/mnt/lv0 的内容

[root@centos7 ~]# ls /mnt/lv0/

lost+found myfile2 myfile4 myfile6 myfile8
myfile1 myfile3 myfile5 myfile7 myfile9
Copy after login

 

很完美,数据并没有丢失!

The above is the detailed content of Detailed explanation of examples of logical volume management lvm. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template