Linux系统中的LVM常规操作
逻辑卷管理(Logical Volume Management,简写LVM)在Linux系统中扮演着重要的角色,它可以提高可用性、磁盘 I/O、性能和磁盘管理的能力。它在物理磁盘和文件系统之间增加了一个额外的层,允许你创建一个逻辑卷而不是物理磁盘。LVM 允许你在需要的时候轻松地调整、扩展和减少逻辑卷的大小。
LVM的常用命令见上表。下面我来实际进行一下LVM的常规操作。
一、建立LVM:
要使用LVM,先要确认你的系统已经安装了lvm2,如果没有安装,请先安装lvm2。
Cenots使命令安装:
$ yum install lvm2
Ubuntu/Debian使用如下命令安装:
$ apt-get install lvm2
我添加了一块硬盘,想配置为LVM,首先要初始化这块硬盘:
$ fdisk /dev/sdb
Welcome to fdisk (util-linux 2.33.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xa4e06196.
Command (m for help):
按m可以看命令提示,根据提示,依次输入 n,p,1,2次回车,在保存前调整分区类型:输入t,8e,这一步将分区类型修改为Linux LVM,最后wq保存,/dev/sdb1分区就有了。
硬盘准备好了,下面来进行LVM的操作。
1、将这块硬盘创建为PV(物理卷):
$ pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
查看PV可使用pvs或pvdisplay命令来查看:
$ pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 --- <10.00g <10.00g
$ pvdisplay
"/dev/sdb1" is a new physical volume of "<10.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size <10.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID rY2eS3-Sp4Y-Tytg-ZoZH-wdLb-zNGu-y6c6AL
2、将刚创建的PV(物理卷)纳入到VG(卷组)中:
$ vgcreate vg-name /dev/sdb1
Volume group "vg-name" successfully created
查看VG可使用vgs或vgdisplay命令来查看:
$ vgs
VG #PV #LV #SN Attr VSize VFree
vg-name 1 0 0 wz--n- <10.00g <10.00g
$ vgdisplay
--- Volume group ---
VG Name vg-name
System ID
Format lvm2
Metadata Areas 1
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 1
Act PV 1
VG Size <10.00 GiB
PE Size 4.00 MiB
Total PE 2559
Alloc PE / Size 0 / 0
Free PE / Size 2559 / <10.00 GiB
VG UUID WtqdBm-macD-DsTh-e9Gh-zJdb-u7Uu-XCity4
3、在VG(卷组)中划分空间创建LV(逻辑卷):
$ lvcreate -L 5G -n lv-name vg-name
Logical volume "lv-name" created.
查看LV可使用lvs或lvdisplay命令来查看:
$ lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv-name vg-name -wi-a----- 5.00g
$ lvdisplay
--- Logical volume ---
LV Path /dev/vg-name/lv-name
LV Name lv-name
VG Name vg-name
LV UUID xtpe9E-tIeQ-1mvH-muMC-8VYP-z0AN-5RY31v
LV Write Access read/write
LV Creation host, time debian, 2021-03-27 07:24:44 -0700
LV Status available
# open 0
LV Size 5.00 GiB
Current LE 1280
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 254:0
4、格式化LV(逻辑卷):
对于ext3、ext4文件系统:
$ mkfs.ext4 /dev/vg-name/lv-name
对于xfs文件系统:
$ mkfs.xfs /dev/vg-name/lv-name
5、挂载该LV(逻辑卷)到/lvm-data:
$ mount /dev/vg-name/lv-name /lvm-data
6、查看挂载的LV目录:
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 97G 1.1G 91G 2% /
tmpfs 462M 0 462M 0% /sys/fs/cgroup
/dev/sda1 511M 5.2M 506M 2% /boot/efi
/dev/mapper/vg--name-lv--name 4.9G 20M 4.6G 1% /lvm-data
二、在线扩容LV(逻辑卷)的容量:
在扩容前看一下VG里还有多少剩余空间:
$ vgdisplay
--- Volume group ---
VG Name vg-name
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size <10.00 GiB
PE Size 4.00 MiB
Total PE 2559
Alloc PE / Size 1280 / 5.00 GiB
Free PE / Size 1279 / <5.00 GiB
VG UUID WtqdBm-macD-DsTh-e9Gh-zJdb-u7Uu-XCity4
显示信息Free PE/Size<5.00 GiB。
1、扩容指定容量可以使用以下3种方式:
a、使用容量单位来扩容(注意参数大小写有区别:-L):
$ lvextend -L +1G /dev/vg-name/lv-name
Size of logical volume vg-name/lv-name changed from 5.00 GiB (1280 extents) to 6.00 GiB (1536 extents).
Logical volume vg-name/lv-name successfully resized.
b、使用PE大小来扩展逻辑卷(注意参数大小写有区别:-l):
$ lvextend -l +1023 /dev/vg-name/lv-name
Rounding size to boundary between physical extents: 1.00 GiB.
Size of logical volume vg-name/lv-name changed from 6.00 GiB (1536 extents) to 7.00 GiB (1792 extents).
Logical volume vg-name/lv-name successfully resized.
c、使用剩余容量的百分比来扩容(注意参数大小写有区别:-l):
$ lvextend -l +50%FREE /dev/vg-name/lv-name
Size of logical volume vg-name/lv-name changed from <8.91 GiB (2280 extents) to <14.45 GiB (3699 extents).
Logical volume vg-name/lv-name successfully resized.
2、现在,逻辑卷已经扩展,你需要调整文件系统的大小以扩展逻辑卷内的空间:
a、对于基于 ext3 和 ext4 的文件系统,运行以下命令:
$ resize2fs /dev/vg-name/lv-name
resize2fs 1.44.5 (15-Dec-2018)
Filesystem at /dev/vg-name/lv-name is mounted on /lvm-data; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vg-name/lv-name is now 1572864 (4k) blocks long.
b、对于 xfs 文件系统,使用以下命令:
$ xfs_growfs /dev/vg-name/lv-name
3、查看LV(逻辑卷)大小是否变化:
$ lvdisplay
--- Logical volume ---
LV Path /dev/vg-name/lv-name
LV Name lv-name
VG Name vg-name
LV UUID xtpe9E-tIeQ-1mvH-muMC-8VYP-z0AN-5RY31v
LV Write Access read/write
LV Creation host, time debian, 2021-03-27 07:24:44 -0700
LV Status available
# open 1
LV Size 6.00 GiB
Current LE 1792
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 254:0
三、增加硬盘,增加卷组的大小:
如果PV不够了,就无法给LV扩容,此时要先给PV扩容。给PV扩容就是加硬盘了,增加一块硬盘,将其始化为/dev/sdc1:
$ fdisk /dev/sdc
Welcome to fdisk (util-linux 2.33.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xa4e06196.
Command (m for help):
按m可以看命令提示,根据提示,依次输入 n,p,1,2次回车,(在保存前调整分区类型:输入t,8e,这一步将分区类型修改为Linux LVM),最后wq保存,/dev/sdc1分区就有了。
$ fdisk -l
......
Disk /dev/sdc: 10 GiB, 10737418240 bytes, 20971520 sectors
Disk model: Virtual Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0xa4e06196
Device Boot Start End Sectors Size Id Type
/dev/sdc1 2048 20971519 20969472 10G 8e Linux LVM
......
1、将这块新加的硬盘创建为PV(物理卷):
$ pvcreate /dev/sdc1
Physical volume "/dev/sdc1" successfully created
2、将刚创建的PV(物理卷)扩展到已存在的VG(卷组)中:
$ vgextend vg-name /dev/sdc1
Volume group "vg-name" successfully extended
这样VG的大小就增加了,然后就可以给LV扩容了。查看验证VG的总容量:
$ vgdisplay
--- Volume group ---
VG Name vg-name
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 6
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 2
Act PV 2
VG Size 19.99 GiB
PE Size 4.00 MiB
Total PE 5118
Alloc PE / Size 1280 / 5.00 GiB
Free PE / Size 3838 / 14.99 GiB
VG UUID WtqdBm-macD-DsTh-e9Gh-zJdb-u7Uu-XCity4
四、离线缩减LV(逻辑卷)的容量:
缩减LV(逻辑卷)容量,强烈不建议操作,有可能丢失数据,除非你备份了这个LV里的所有数据:
1、将已挂载的LV(逻辑卷)卸载:
$ umount /lvm-data
2、检查磁盘错误:
$ e2fsck -f /dev/vg-name/lv-name
e2fsck 1.44.5 (15-Dec-2018)
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/vg-name/lv-name: 11/393216 files (0.0% non-contiguous), 46190/1572864 blocks
3、缩减文件系统容量:
对于ext3、ext4文件系统:
$ resize2fs /dev/vg-name/lv-name 5G
resize2fs 1.44.5 (15-Dec-2018)
Resizing the filesystem on /dev/vg-name/lv-name to 1310720 (4k) blocks.
The filesystem on /dev/vg-name/lv-name is now 1310720 (4k) blocks long.
对于xfs文件系统:
$ xfs_growfs /dev/vg-name/lv-name
4、缩减LV(逻辑卷)的容量:
$ lvreduce -L 5G /dev/vg-name/lv-name
WARNING: Reducing active logical volume to 5.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg-name/lv-name? [y/n]: y
Size of logical volume vg-name/lv-name changed from 7.00 GiB (1792 extents) to 5.00 GiB (1280 extents).
Logical volume vg-name/lv-name successfully resized.
提示缩减容量可能会删除数据,输入y确认即可。
5、将缩减容量的LV重新挂载:
$ mount /dev/vg-name/lv-name /lvm-data
五、删除操作:
执行删除前,请先卸载已挂载的分区:
$ umount /lvm-data
再将挂载点信息从/etc/fstab中删除,不然再次启动系统时可能造成问题。
1、先停用、删除LV(逻辑卷):
$ lvchange -an /dev/vg-name/lv-name
$ lvremove /dev/vg-name/lv-name
2、再停用、删除VG(卷组):
$ vgchange -an /dev/vg-name
$ vgremove /dev/vg-name
3、最后删除PV(物理卷):
$ pvremove /dev/sdb1
注意1:
在/etc/fstab文件中添加新的逻辑卷详细信息,以便系统启动时自动挂载:
$ vi /etc/fstab
/dev/vg-name/lv-name /lvm-data ext4 defaults 0 0
注意2:
增加容量有“+”表示在当前容量基础上再增加的容量,容量前没有“+”表示最终的总容量,例如:
$ lvextend -L +10G /dev/vg-name/lv-name/lv-name
$ lvextend -L 10G /dev/vg-name/lv-name/lv-name
这2个命令是有区别的,如果原来的容量是5G,则第一个表示的是5+10=15G,第二个表示最终容量是10G,实际增加了5G容量。
注意3:
在线扩容时,注意参数 “-L”和“-l”的区别:“+L”后面接实际要增加的容量,默认单位是M,“+l”后面接PE数量或是百分比值。