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
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):
[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
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.
[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
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
[root@centos7 ~]# vgs VG #PV #LV #SN Attr VSize VFree vg0 4 0 0 wz--n- 3.94g 3.94g
[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
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.
[root@centos7 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv0 vg0 -wi-a----- 3.00g
[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
[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
5 Create the file system and format it
mkfs.ext4 /dev/vg0/lv0 [root@centos7 ~]# mkfs.ext4 /dev/vg0/lv0
6 Mount using
[root@centos7 ~]# mkdir /mnt/lv0 [root@centos7 ~]# mount /dev/vg0/lv0 /mnt/lv0
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
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
2 Extend LV
[root@centos7 ~]# lvextend -r -l +100%FREE /dev/vg0/lv0
[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.
3 Come down and redefine the file system size
ext series file system redefinition : resize2fs /dev/vg0/lv0xfs 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.
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
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
6 Mount using
[root@centos7 ~]# mount -o remount /dev/vg0/lv0
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/
##2 Uninstall /dev/vg0/lv0
[root@centos7 ~]# umount /mnt/lv0/
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
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.
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.
6 挂载使用
mount /dev/vg0/lv0 /mnt/lv0 [root@centos7 ~]# mount /dev/vg0/lv0 /mnt/lv0/
查看磁盘挂载情况
[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
7 确认数据是否丢失
现在再来开心目录/mnt/lv0 的内容
[root@centos7 ~]# ls /mnt/lv0/ lost+found myfile2 myfile4 myfile6 myfile8 myfile1 myfile3 myfile5 myfile7 myfile9
很完美,数据并没有丢失!
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!