pv # 物理卷,被处理的物理分区
vg # 物理卷组
pz # 物理拓展
lvm # pz里分出来的逻辑卷
pvs # 查看物理卷
pgs # 查看物理卷组
lvs # 查看逻辑卷
[root@server ~]# fdisk /dev/vdb # 建立分区,并修改分区类型
Command (m for help): n
Command (m for help): p # 查看建立的分区
Command (m for help): t
Partition number (1-5, default 5): 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Command (m for help): wq
[root@server ~]# partprobe
[root@server ~]# pvcreate /dev/vdb1 # 把物理分区做成物理卷
[root@server ~]# vgcreate vg0 /dev/vdb1 # 创建物理卷组
Volume group "vg0" successfully created
[root@server ~]# lvcreate -L 50M -n lv0 vg0 # 创建逻辑卷,-L是指定大小,-n是起名字,vg0是把逻辑卷归属于vg0组
Rounding up size to full physical extent 52.00 MiB
Logical volume "lv0" created
[root@server ~]# mkfs.xfs /dev/vg0/lv0 # 格式化
[root@server ~]# mount /dev/vg0/lv0 /mnt
[root@server ~]# watch -n 1 'pvs;echo +++++++++;vgs;echo +++++++++;lvs;echo +++++++++;df -h /vg0' # 监控命令,监控pv,vg,lvs的创建
[root@server ~]# lvextend -L 100M /dev/vg0/lv0 # 发现扩容失败,因为分区的大小最多只有100M
Extending logical volume lv0 to 100.00 MiB
Insufficient free space: 12 extents needed, but only 11 available
[root@server ~]# lvextend -L 90M /dev/vg0/lv0 # 拉伸到90M, 扩容时,要确保扩容大小在范围内
[root@server ~]# watch -n 1 'pvs;echo +++++++++;vgs;echo +++++++++;lvs;echo +++++++++;df -h /vg0' # 监控命令
[root@server ~]# xfs_growfs /dev/vg0/lv0 # 对文件系统进行扩容
[root@server ~]# watch -n 1 'pvs;echo +++++++++;vgs;echo +++++++++;lvs;echo +++++++++;df -h /vg0' # 监控命令
[root@server ~]# fdisk /dev/vdb
Command (m for help): p
Command (m for help): t
Partition number (1-5, default 5): 2
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
[root@server ~]# partprobe
[root@server ~]# pvcreate /dev/vdb2 #把/dev/vdb2添加到物理卷中
Physical volume "/dev/vdb2" successfully created
[root@server ~]# vgextend vg0 /dev/vdb2 # 把新添加的/dev/vdb2设备添加到vg0组中
[root@server ~]# watch -n 1 'pvs;echo +++++++++;vgs;echo +++++++++;lvs;echo +++++++++;df -h /vg0' # 监控命令
[root@server ~]# lvextend -L 150M /dev/vg0/lv0 # 扩容逻辑卷
[root@server ~]# watch -n 1 'pvs;echo +++++++++;vgs;echo +++++++++;lvs;echo +++++++++;df -h /vg0' # 监控命令
[root@server ~]# xfs_growfs /dev/vg0/lv0 # 扩容文件系统
[root@server ~]# watch -n 1 'pvs;echo +++++++++;vgs;echo +++++++++;lvs;echo +++++++++;df -h /vg0' # 监控命令
注:xfs文件系统不支持缩减
[root@server ~]# umount /mnt # 先卸载设备
[root@server ~]# mkfs.ext4 /dev/vg0/lv0 # 格式化成为ext4类型的
[root@server ~]# mount /dev/vg0/lv0 /mnt # 挂载设备
[root@server ~]# lvextend -L 180M /dev/vg0/lv0 # 扩容设备到180M,记住,扩容的大小一定要在物理卷组的大小范围内
Extending logical volume lv0 to 180.00 MiB
Logical volume lv0 successfully resized
[root@server ~]# resize2fs /dev/vg0/lv0 # 更新逻辑卷信息,使文件系统的大小同步
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg0/lv0 is mounted on /mnt; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 2
The filesystem on /dev/vg0/lv0 is now 184320 blocks long.
[root@server ~]# watch -n 1 'pvs;echo +++++++++;vgs;echo +++++++++;lvs;echo +++++++++;df -h /vg0' # 监控命令
[root@server ~]# umount /mnt # 先卸载设备
[root@server ~]# e2fsck -f /dev/vg0/lv0 # 扫描,查看文件是否被损坏,如果损坏,就不能被缩减
[root@server ~]# resize2fs /dev/vg0/lv0 50M # 缩减文件系统为50M
[root@server ~]# mount /dev/vg0/lv0 /mnt # 挂载设备
[root@server ~]# lvreduce -L 50M /dev/vg0/lv0 # 再缩减逻辑卷为50M
[root@server ~]# watch -n 1 'pvs;echo +++++++++;vgs;echo +++++++++;lvs;echo +++++++++;df -h /vg0' # 监控命令
[root@server ~]# vgreduce vg0 /dev/vdb2 # 把/dev/vdb2这个设备从vg0组中移除
Removed "/dev/vdb2" from volume group "vg0"
[root@server ~]# pvremove /dev/vdb2 # 移除/dev/vdb2这个设备
Labels on physical volume "/dev/vdb2" successfully wiped
[root@server ~]# pvmove /dev/vdb1 /dev/vdb2 # 把/dev/vdb1上的数据迁移到/dev/vdb2上
[root@server ~]# vgreduce vg0 /dev/vdb1
Removed "/dev/vdb1" from volume group "vg0"
[root@server ~]# pvremove /dev/vdb1
Labels on physical volume "/dev/vdb1" successfully wiped
[root@server ~]# df # 查看磁盘是否挂载
[root@server ~]# cd /mnt
[root@server mnt]# touch file{1..10} # 给/dev/vg0/lv0设备中写内容,方便后面检验
[root@server mnt]# ls
file1 file10 file2 file3 file4 file5 file6 file7 file8 file9
[root@server ~]# umount /mnt
[root@server mnt]# lvcreate -L 30M -n lv0_backup -s /dev/vg0/lv0 # 创建一个大小为30M的快照,-L是指定大小,-n是起名字,-s是制定创建快照的对象
[root@server ~]# mount /dev/vg0/lv0_backup /mnt # 挂载快照
[root@server ~]# ls /mnt # 可以查看母盘中的内容
file1 file10 file2 file3 file4 file5 file6 file7 file8 file9
[root@server ~]# rm -fr /mnt/* # 删除设备里的内容
[root@server ~]# ls /mnt
[root@server ~]# umount /dev/vg0/lv0_backup # 先卸载快照
[root@server ~]# lvremove /dev/vg0/lv0_backup # 删除快照
Do you really want to remove active logical volume lv0_backup? [y/n]: y
Logical volume "lv0_backup" successfully removed
[root@server ~]# lvcreate -L 20M -n lv0_backup1 -s /dev/vg0/lv0 # 重新创建快照
Logical volume "lv0_backup1" created
[root@server ~]# mount /dev/vg0/lv0_backup1 /mnt # 挂载
[root@server ~]# ls /mnt # 可见快照并不影响母盘的内容
file1 file10 file2 file3 file4 file5 file6 file7 file8 file9
PE(physical extent):物理区域是物理卷中可用于分配的最小存储单元,物理区域大小在建立卷 组时指定,一
旦确定不能更改,同一卷组所有物理卷的物理区域大小需一致,新的pv加入到vg后,pe的大小自动更改为vg中定义
的pe大小。
[root@server ~]# vgdisplay # 显示LVM卷组的详细信息,可见默认PE的大小是4M
[root@server ~]# pvcreate /dev/vdb3 # 这个分区是我自己新建立的
[root@server ~]# vgcreate -s 16M vg0 /dev/vdb3 # -s,指定pe的大小为16M,最小是2K,最大是 2T。
[root@server ~]# vgdisplay # 查看更成功
[root@server ~]# umount /mnt/ # 卸载设备
[root@server ~]# lvremove /dev/vg0/lv0_backup1 # 删除快照
Do you really want to remove active logical volume lv0_backup1? [y/n]: Y
Logical volume "lv0_backup1" successfully removed
[root@server ~]# lvremove /dev/vg0/lv0 # 删除母盘设备
Do you really want to remove active logical volume lv0? [y/n]: Y
Logical volume "lv0" successfully removed
[root@server ~]# vgremove vg0 # 删除物理卷组
Volume group "vg0" successfully removed
[root@server ~]# pvremove /dev/vdb2 # 删除物理卷
Labels on physical volume "/dev/vdb2" successfully wiped
[root@server ~]# fdisk /dev/vdb # 删除所有的物理分区
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): d
Partition number (2-5, default 5): 2
Partition 2 is deleted
Command (m for help): d
Partition number (3-5, default 5): 3
Partition 3 is deleted
Command (m for help): d
Partition number (4,5, default 5): 4
Partition 4 is deleted
Command (m for help): d
No partition is defined yet!
Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.