LVM 逻辑卷的使用
1、添加磁盘
(注:先关闭虚拟机,否则可能会出现意想不到的问题,由于我多次踩雷,哈哈哈)
进入虚拟机的设置(使用的是VMware Workstation软件)
添加———>硬盘————>下一步————>下一步————>下一步————>将虚拟机磁盘存储为单个文件(o),下一步————>完成————>确定
配好网卡后连接CRT软件
[root@hahaha ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 19.5G 0 part
├─centos-root 253:0 0 17.5G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
sr0 11:0 1 4G 0 rom
2、对磁盘进行分区
[root@hahaha ~]# fdisk /dev/sdb
p (回车)
n (回车)
p (回车——回车——回车)
+5G(回车)
n (回车)
p (回车——回车——回车)
+5G(回车)
p (回车)
w (回车,保存并退出)
[root@hahaha ~]# fdisk /dev/sdb
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.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xa6e909a0.
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xa6e909a0
Device Boot Start End Blocks Id System
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G
Partition 1 of type Linux and of size 5 GiB is set
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2):
First sector (10487808-41943039, default 10487808):
Using default value 10487808
Last sector, +sectors or +size{K,M,G} (10487808-41943039, default 41943039): +5G
Partition 2 of type Linux and of size 5 GiB is set
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xa6e909a0
Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux
/dev/sdb2 10487808 20973567 5242880 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@hahaha ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 19.5G 0 part
├─centos-root 253:0 0 17.5G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 5G 0 part
└─sdb2 8:18 0 5G 0 part
sr0 11:0 1 4G 0 rom
(此时已经发现已经完成了磁盘分区)
3、对这两个分区进行创建物理卷操作
[root@hahaha ~]# pvcreate /dev/sdb1 /dev/sdb2
[root@hahaha ~]# pvcreate /dev/sdb1 /dev/sdb2
Physical volume “/dev/sdb1” successfully created
Physical volume “/dev/sdb2” successfully created
创建完成后可以用以下命令查看物理卷的简单信息和详细信息
[root@hahaha ~]# pvs
[root@hahaha ~]# pvdisplay
[root@hahaha ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- 19.51g 40.00m
/dev/sdb1 lvm2 — 5.00g 5.00g
/dev/sdb2 lvm2 — 5.00g 5.00g
[root@hahaha ~]# pvdisplay
— Physical volume —
PV Name /dev/sda2
VG Name centos
PV Size 19.51 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 4994
Free PE 10
Allocated PE 4984
PV UUID DMOQIf-xcgD-1kdI-iI2P-eHFR-3f5f-3QvruM
“/dev/sdb1” is a new physical volume of “5.00 GiB”
— NEW Physical volume —
PV Name /dev/sdb1
VG Name
PV Size 5.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID J3f20c-11b1-7czA-A1xk-9Nuj-BHNh-60dosT
“/dev/sdb2” is a new physical volume of “5.00 GiB”
— NEW Physical volume —
PV Name /dev/sdb2
VG Name
PV Size 5.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID UGCa4W-JN6S-s3CE-kwGG-gZSO-fiCT-SqrduG
4、创建卷组
将刚创好的物理卷创建名为‘myvg’的卷组
[root@hahaha ~]# vgcreate myvg /dev/sdb[1-2]
Volume group "myvg" successfully created
查看卷组信息
[root@hahaha ~]# vgs
[root@hahaha ~]# vgdisplay
[root@hahaha ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz–n- 19.51g 40.00m
myvg 2 0 0 wz–n- 9.99g 9.99g
[root@hahaha ~]# vgdisplay
— Volume group —
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.51 GiB
PE Size 4.00 MiB
Total PE 4994
Alloc PE / Size 4984 / 19.47 GiB
Free PE / Size 10 / 40.00 MiB
VG UUID jp3gCF-18bF-5FXs-52yE-3Fcz-Dtji-uUWxoS
— Volume group —
VG Name myvg
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 aMKMyQ-l6eX-Fx0s-2rtP-acrM-tk1v-i6lIpd
5、删除卷组,重新创建卷组并指定PE大小为16MB
[root@hahaha ~]# vgremove myvg
Volume group "myvg" successfully removed
[root@hahaha ~]# vgcreate -s 16m myvg /dev/sdb[1-2]
Volume group "myvg" successfully created
[root@hahaha ~]# vgdisplay
可发现PE大小为16MB
6、在myvg卷组中添加一个物理卷
在/dev/sdb上再分一个/dev/sdb3分区,并将此分区加入myvg卷组中
[root@hahaha ~]# fdisk /dev/sdb
p(回车)
n(回车)
p(回车——回车——回车)
+5G(回车)
w(回车,保存并退出)
[root@hahaha ~]# vgextend myvg /dev/sdb3
[root@hahaha ~]# vgextend myvg /dev/sdb3
Physical volume “/dev/sdb3” successfully created
Volume group “myvg” successfully extended
[root@hahaha ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz–n- 19.51g 40.00m
myvg 3 0 0 wz–n- 14.95g 14.95g (可看到卷组中有三个物理卷)
7、创建逻辑卷
创建名为mylv的逻辑卷,大小为5G
[root@hahaha ~]# lvcreate -L +5G -n mylv myvg
Logical volume "mylv" created.
(-L:创建逻辑卷的大小;-n:创建逻辑卷的名称)
查看逻辑卷
[root@hahaha ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- 17.47g
swap centos -wi-ao---- 2.00g
mylv myvg -wi-a----- 5.00g
扫描上一步创建的逻辑卷
[root@hahaha ~]# lvscan
ACTIVE '/dev/centos/swap' [2.00 GiB] inherit
ACTIVE '/dev/centos/root' [17.47 GiB] inherit
ACTIVE '/dev/myvg/mylv' [5.00 GiB] inherit
使用ext4文件系统格式化逻辑卷mylv
[root@hahaha ~]# mkfs.ext4 /dev/mapper/myvg-mylv
将逻辑卷mylv挂载到/mnt/下并验证
[root@hahaha ~]# mount /dev/mapper/myvg-mylv /mnt/
[root@hahaha ~]# df -h
[root@hahaha ~]# mount /dev/mapper/myvg-mylv /mnt/
[root@hahaha ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 18G 880M 17G 5% /
devtmpfs 903M 0 903M 0% /dev
tmpfs 913M 0 913M 0% /dev/shm
tmpfs 913M 8.6M 904M 1% /run
tmpfs 913M 0 913M 0% /sys/fs/cgroup
/dev/sda1 497M 125M 373M 25% /boot
tmpfs 183M 0 183M 0% /run/user/0
/dev/mapper/myvg-mylv 4.8G 20M 4.6G 1% /mnt
8、对创建的LVM卷扩容至1GB
[root@hahaha ~]# lvextend -L +1G /dev/mapper/myvg-mylv
[root@hahaha ~]# lvs
[root@hahaha ~]# df -h
[root@hahaha ~]# resize2fs /dev/mapper/myvg-mylv(对文件系统进行扩容)
[root@hahaha ~]# df -h
[root@hahaha ~]# lvextend -L +1G /dev/mapper/myvg-mylv
Size of logical volume myvg/mylv changed from 5.00 GiB (320 extents) to 6.00 GiB (384 extents).
Logical volume mylv successfully resized.
[root@hahaha ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- 17.47g
swap centos -wi-ao---- 2.00g
mylv myvg -wi-ao---- 6.00g
[root@hahaha ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 18G 880M 17G 5% /
devtmpfs 903M 0 903M 0% /dev
tmpfs 913M 0 913M 0% /dev/shm
tmpfs 913M 8.6M 904M 1% /run
tmpfs 913M 0 913M 0% /sys/fs/cgroup
/dev/sda1 497M 125M 373M 25% /boot
tmpfs 183M 0 183M 0% /run/user/0
/dev/mapper/myvg-mylv 4.8G 20M 4.6G 1% /mnt
[root@hahaha ~]# resize2fs /dev/mapper/myvg-mylv
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/myvg-mylv is mounted on /mnt; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mapper/myvg-mylv is now 1572864 blocks long.
[root@hahaha ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 18G 880M 17G 5% /
devtmpfs 903M 0 903M 0% /dev
tmpfs 913M 0 913M 0% /dev/shm
tmpfs 913M 8.6M 904M 1% /run
tmpfs 913M 0 913M 0% /sys/fs/cgroup
/dev/sda1 497M 125M 373M 25% /boot
tmpfs 183M 0 183M 0% /run/user/0
/dev/mapper/myvg-mylv 5.8G 20M 5.5G 1% /mnt
(至此逻辑卷扩容完成)