本次实验环境是redhat7.0系统
逻辑卷管理器是linux系统用于对硬盘分区进行管理的一种机制,初衷是为了解决硬盘设备在创建分区后不易修改分区大小的缺陷。它提供了一个抽象的卷组,可以把多块硬盘进行卷组合并,用户不必关系物理硬盘设备的底层架构和布局,就可以实现对硬盘分区的动态调整。
常用的命令
作用 | 物理卷管理 | 卷组管理 | 逻辑卷管理 |
---|---|---|---|
扫描 | pvscan | vgscan | lvscan |
建立 | pvcreate | vgcreate | lvcreate |
显示 | pvdisplay | vgdisplay | lvdisplay |
删除 | pvremove | vgremove | lvremove |
扩展 | vgextend | lvextend | |
缩小 | vgreduce | lvreduce |
创建pv
将/dev/sdb /dev/sdc创建成pv
[root@localhost mnt]# pvcreate /dev/sdb /dev/sdc
Physical volume "/dev/sdb" successfully created
Physical volume "/dev/sdc" successfully created
查看pv
[root@localhost mnt]# pvdisplay
--- Physical volume ---
PV Name /dev/sdb
VG Name vg1
PV Size 5.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 1279
Free PE 1279
Allocated PE 0
PV UUID 7HM45W-ceIl-sEFh-9uj3-cz4W-jZ5Z-cWavvd
--- Physical volume ---
PV Name /dev/sdc
VG Name vg1
PV Size 5.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 1279
Free PE 1279
Allocated PE 0
PV UUID B1oEjB-UJcE-l1Lk-9DTI-BWy3-2FdC-dmdgbI
创建vg
创建一个vg,命名为vg1
[root@localhost mnt]# vgcreate vg1 /dev/sdb /dev/sdc
Volume group "vg1" successfully created
查看vg
[root@localhost mnt]# vgdisplay
--- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas 2
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 2
Act PV 2
VG Size 9.99 GiB
PE Size 4.00 MiB
Total PE 2558
Alloc PE / Size 0 / 0
Free PE / Size 2558 / 9.99 GiB
VG UUID m3pfeB-v3os-itGn-RYKz-gYLi-dHqY-fRAQ2L
创建一个lv
创建一个大小为150M的逻辑卷 对逻辑卷进行切割时有两种计量单位:
- 以容量为单位,所使用的参数为-L。例如:-L 150M
- 以基本单元的个数为单位,使用的参数为-l,默认基本单元是4MB。例如 -l 10 为4MB x 10 =40MB
[root@localhost mnt]# lvcreate -n lv1 -L 150M vg1
Logical volume "lv1" created
查看lv
[root@localhost mnt]# lvdisplay
--- Logical volume ---
LV Path /dev/vg1/lv1
LV Name lv1
VG Name vg1
LV UUID EuYC0s-eXZo-3Vlq-xOQF-Suej-AkLY-vkQbv2
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2020-02-25 17:06:06 +0800
LV Status available
# open 0
LV Size 152.00 MiB
Current LE 38
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
对lv进行格式化,挂载使用
[root@localhost mnt]# mkfs.ext4 /dev/vg1/lv1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
38912 inodes, 155648 blocks
7782 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33816576
19 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
[root@localhost mnt]# mkdir lv1
[root@localhost mnt]# mount /dev/vg1/lv1 /mnt/lv1/
[root@localhost mnt]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/rhel-root xfs 18G 3.0G 15G 17% /
devtmpfs devtmpfs 1.5G 0 1.5G 0% /dev
tmpfs tmpfs 1.5G 140K 1.5G 1% /dev/shm
tmpfs tmpfs 1.5G 8.9M 1.5G 1% /run
tmpfs tmpfs 1.5G 0 1.5G 0% /sys/fs/cgroup
/dev/sr0 iso9660 3.5G 3.5G 0 100% /media/cdrom
/dev/sda1 xfs 497M 119M 379M 24% /boot
/dev/mapper/vg1-lv1 ext4 144M 1.6M 132M 2% /mnt/lv1
将挂载目录写入到/etc/fstab,永久生效
[root@localhost mnt]# vim /etc/fstab
/dev/vg1/lv1 /mnt/lv1 ext4 defaults 0 0
对lv扩容
扩容之前,先卸载lv
[root@localhost mnt]# umount /dev/vg1/lv1
扩容到200M
[root@localhost mnt]# lvextend -L 200M /dev/vg1/lv1
Extending logical volume lv1 to 200.00 MiB
Logical volume lv1 successfully resized
检查硬盘完整性
[root@localhost mnt]# e2fsck -f /dev/vg1/lv1
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/vg1/lv1: 11/38912 files (0.0% non-contiguous), 10567/155648 blocks
重置硬盘容量
[root@localhost mnt]# resize2fs /dev/vg1/lv1
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg1/lv1 to 204800 (1k) blocks.
The filesystem on /dev/vg1/lv1 is now 204800 blocks long.
重新挂载,查看状态
[root@localhost mnt]# mount -a
[root@localhost mnt]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/rhel-root xfs 18G 3.0G 15G 17% /
devtmpfs devtmpfs 1.5G 0 1.5G 0% /dev
tmpfs tmpfs 1.5G 140K 1.5G 1% /dev/shm
tmpfs tmpfs 1.5G 8.9M 1.5G 1% /run
tmpfs tmpfs 1.5G 0 1.5G 0% /sys/fs/cgroup
/dev/sr0 iso9660 3.5G 3.5G 0 100% /media/cdrom
/dev/sda1 xfs 497M 119M 379M 24% /boot
/dev/mapper/vg1-lv1 ext4 190M 1.6M 175M 1% /mnt/lv1
对lv缩容
缩容之前,做好数据备份,先卸载lv
[root@localhost mnt]# umount /dev/vg1/lv1
检查硬盘完整性
[root@localhost mnt]# e2fsck -f /dev/vg1/lv1
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/vg1/lv1: 11/51200 files (0.0% non-contiguous), 12115/204800 blocks
容量减小到100M
[root@localhost mnt]# resize2fs /dev/vg1/lv1 100M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg1/lv1 to 102400 (1k) blocks.
The filesystem on /dev/vg1/lv1 is now 102400 blocks long.
[root@localhost mnt]# lvreduce -L 100M /dev/vg1/lv1
WARNING: Reducing active logical volume to 100.00 MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv1? [y/n]: y
Reducing logical volume lv1 to 100.00 MiB
Logical volume lv1 successfully resized
重新挂载,查看状态
[root@localhost mnt]# mount -a
[root@localhost mnt]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/rhel-root xfs 18G 3.0G 15G 17% /
devtmpfs devtmpfs 1.5G 0 1.5G 0% /dev
tmpfs tmpfs 1.5G 140K 1.5G 1% /dev/shm
tmpfs tmpfs 1.5G 8.9M 1.5G 1% /run
tmpfs tmpfs 1.5G 0 1.5G 0% /sys/fs/cgroup
/dev/sr0 iso9660 3.5G 3.5G 0 100% /media/cdrom
/dev/sda1 xfs 497M 119M 379M 24% /boot
/dev/mapper/vg1-lv1 ext4 93M 1.6M 85M 2% /mnt/lv1
创建逻辑卷快照
快照特点:
- 快照卷的容量必须等同于逻辑卷的容量
- 快照卷仅一次有效,一旦执行还原操作后会被删除
查看vg状态,确保有足够的容量创建快照
[root@localhost mnt]# vgdisplay
--- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
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 9.99 GiB
PE Size 4.00 MiB
Total PE 2558
Alloc PE / Size 25 / 100.00 MiB
Free PE / Size 2533 / 9.89 GiB
VG UUID m3pfeB-v3os-itGn-RYKz-gYLi-dHqY-fRAQ2L
对lv1创建快照, -s 创建快照;-n 快照名称;-L 快照的容量 创建快照时,不用卸载lv
[root@localhost mnt]# lvcreate -s -n snap-lv1 -L 100M /dev/vg1/lv1
Logical volume "snap-lv1" created
恢复快照,恢复之前先卸载lv
[root@localhost mnt]# umount /dev/vg1/lv1
[root@localhost mnt]# lvconvert --merge /dev/vg1/snap-lv1
Merging of volume snap-lv1 started.
lv1: Merged: 100.0%
Merge of snapshot into logical volume lv1 has finished.
Logical volume "snap-lv1" successfully removed
重新挂载lv,查看数据
删除lv、vg、pv
如果不再使用lvm,执行删除操作,删除的顺序是:lv-vg-pv,顺序不能错,不然删除不了,删除之间先卸载lv
[root@localhost mnt]# lvremove /dev/vg1/lv1
Do you really want to remove active logical volume lv1? [y/n]: y
Logical volume "lv1" successfully removed
[root@localhost mnt]# vgremove vg1
Volume group "vg1" successfully removed
[root@localhost mnt]# pvremove /dev/sdb /dev/sdc
Labels on physical volume "/dev/sdb" successfully wiped
Labels on physical volume "/dev/sdc" successfully wiped