[root@test11 ~]# fdisk -l //查看分区的情况
[root@test11 ~]# fdisk /dev/sda //进行分区
Command (m for help): n //n为添加一个分区,p查看分区
First cylinder (2898-10443, default 2898): //按【Enter】键决定
Using default value 2898 //起始柱面选择默认 值
Last cylinder or +size or +sizeM or +sizeK (289810443, default 10443): +2G // 给2G的大小
Command (m for help): t //更改分区类型
Partition number (1-5): 5 //选着分区
Hex code (type L to list codes): l //分区类型列表
Hex code (type L to list codes):8e //将新添加的分区标注成lvm
Command (m for help): p //查看分区表
/dev/sda5 2756 2999 1959898+ 8e Linux LVM
Command (m for help): w //保存
[root@test11 ~]# partprobe /dev/sda //是系统识别分区(代替重启)
[root@test11 ~]# mkfs.ext4 /dev/sda5 //格式化新添加的分区
2、PV的创建
[root@test11 ~]# pvcreate /dev/sda5 //创建pv
Writing physical volume data to disk "/dev/sda5"
Physical volume "/dev/sda5" successfully created
[root@test11 ~]# pvs //查看当前pv的信息
PV VG Fmt Attr PSize PFree
/dev/sda5 lvm2 a-- 1.87G 1.87G
3、VG的创建
[root@test11 ~]# vgcreate vg0 /dev/sda5 //vg0为当前vg的名
Volume group "vg0" successfully created
[root@test11 ~]# vgs //查看vg的信息
VG #PV #LV #SN Attr VSize VFree
vg0 1 0 0 wz--n- 1.87G 1.87G
4、lv的创建
[root@test11 ~]# lvcreate -L 500M -n lv01 vg0
Logical volume "lv01" created
[root@test11 ~]# lvs //查看lv的信息
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
lv01 vg0 -wi-a- 500.00M
[root@test11 ~]# mkfs.ext4 /dev/vg0/lv01 //格式化
5、lv的增大
[root@test11 ~]# lvextend -L +300M /dev/vg0/lv01 //(原来lv大小500M ,拉伸分区到 800M) Extending logical volume lv01 to 800.00 MB
Logical volume lv01 successfully resized
[root@test11 ~]# resize2fs /dev/vg0/lv01 //拉伸文件系统
[root@test11 ~]# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
lv01 vg0 -wi-ao 800.00M //大小变成了800M
6、lv的减小
# umount /dev/vg0/lv01 //卸载逻辑卷/dev/vg0/lv01
# df -h //查看
# e2fsck -f /dev/vg0/lv01 //磁盘校验
# resize2fs /dev/vg0/lv01 500M //回缩文件系统到 500M
# lvreduce -L 500M /dev/vg0/lv01 //回缩分区到 500M
WARNING: Reducing active logical volume to 500.00 MB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv01? [y/n]: y
# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
lv01 vg0 -wi-a- 500.00M //此时已变成500M大小
# mount /dev/vg0/lv01 /mnt/lv01 //重新挂载分区
# df -h //查看
# ls /mnt/lv01 //查看文件
四、lvm的关闭
[root@test11 ~]# umount /mnt/lv01
[root@test11 ~]# lvremove /dev/vg0/lv01 //删除lv
Do you really want to remove active logical volume lv01? [y/n]: y
Logical volume "lv01" successfully removed
[root@test11 ~]# vgchange -a n vg0 //使vg0不具有active标志
0 logical volume(s) in volume group "vg0" now active
[root@test11 ~]# vgremove vg0 //删除vg
Volume group "vg0" successfully removed
[root@test11 ~]# pvremove /dev/sda5 //删除pv
Labels on physical volume "/dev/sda5" successfully wiped
五、扩展根分区和SWAP
[root@zkdb1 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda3 vg_zkdb1 lvm2 a-- 557.46g 521.46g
[root@zkdb1 ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
root vg_zkdb1 -wi-ao---- 20.00g
swap vg_zkdb1 -wi-ao---- 16.00g
[root@zkdb1 ~]# lvcreate -L 30720M vg_zkdb1
Logical volume "lvol0" created
[root@zkdb1 ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lvol0 vg_zkdb1 -wi-a----- 30.00g
root vg_zkdb1 -wi-ao---- 20.00g
swap vg_zkdb1 -wi-ao---- 16.00g
[root@zkdb1 ~]# lvextend -L +30G /dev/mapper/vg_zkdb1-root
Extending logical volume root to 50.00 GiB
Logical volume root successfully resized
[root@zkdb1 ~]# e2fsck -f /dev/mapper/vg_zkdb1-root
e2fsck 1.41.12 (17-May-2010)
/dev/mapper/vg_zkdb1-root is mounted.
e2fsck: Cannot continue, aborting.
[root@zkdb1 ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lvol0 vg_zkdb1 -wi-a----- 30.00g
root vg_zkdb1 -wi-ao---- 50.00g
swap vg_zkdb1 -wi-ao---- 16.00g
[root@zkdb1 ~]# resize2fs /dev/mapper/vg_zkdb1-root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg_zkdb1-root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 4
Performing an on-line resize of /dev/mapper/vg_zkdb1-root to 13107200 (4k) blocks.
The filesystem on /dev/mapper/vg_zkdb1-root is now 13107200 blocks long.
扩展SWAP
[root@zkdb1 ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lvol0 vg_zkdb1 -wi-a----- 30.00g
root vg_zkdb1 -wi-ao---- 50.00g
swap vg_zkdb1 -wi-ao---- 16.00g
[root@zkdb1 ~]# lvextend -L +16G /dev/mapper/vg_zkdb1-swap
Extending logical volume swap to 32.00 GiB
Logical volume swap successfully resized
[root@zkdb1 ~]# e2fsck -f /dev/mapper/vg_zkdb1-swap
e2fsck 1.41.12 (17-May-2010)
/dev/mapper/vg_zkdb1-swap is mounted.
e2fsck: Cannot continue, aborting.
[root@zkdb1 ~]# resize2fs /dev/mapper/vg_zkdb1-swap
resize2fs 1.41.12 (17-May-2010)
resize2fs: Bad magic number in super-block while trying to open /dev/mapper/vg_zkdb1-swap
Couldn't find valid filesystem superblock.
[root@zkdb1 ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lvol0 vg_zkdb1 -wi-a----- 30.00g
root vg_zkdb1 -wi-ao---- 50.00g
swap vg_zkdb1 -wi-ao---- 32.00g