①硬盘的多个分区由LVM统一为卷组管理,可以方便的加入或移走分区以扩大或减小卷组的可用容量,充分利用硬盘空间
②用户/用户组的空间建立在LVM上,可以随时按要求增大,或根据使用情况对各逻辑卷进行调整。当系统空间不足而加入新的硬盘时,不必把用户的数据从原硬盘迁移到新硬盘,而只须把新的分区加入卷组并扩充逻辑卷即可。同样,使用LVM可以在不停服务的情况下。把用户数据从旧硬盘转移到新硬盘空间中去
PV: 物理
VG:卷组
LV :逻辑卷
将磁盘加入pv # pvcreate /dev/sdb /dev/sdc /dev/sdd
将pv加入到vg卷组 # vgcreate datavg /dev/sdb /dev/sdc /dev/sdd
分配lv使用 # lvcreate -L 100M -n lv1 datavg
查看命令:pvs、vgs、lvs
先使用两块,sdd稍后用来示范VG扩展和移除
[root@localhost ~]# lsblk |grep "sd[bcd]"
sdb 8:16 0 204M 0 disk
sdc 8:32 0 204M 0 disk
sdd 8:48 0 204M 0 disk
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 30G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 25.8G 0 part
│ ├─rhel-root 253:0 0 23.8G 0 lvm /
│ └─rhel-home 253:1 0 2G 0 lvm /home
└─sda3 8:3 0 4G 0 part [SWAP]
sdb 8:16 0 204M 0 disk
sdc 8:32 0 204M 0 disk
sdd 8:48 0 204M 0 disk
sr0 11:0 1 3.8G 0 rom
[root@localhost ~]#
[root@localhost ~]# pvcreate /dev/sdb
Physical volume "/dev/sdb" successfully created
[root@localhost ~]# echo $?
0
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- 25.80g 0
/dev/sdb lvm2 --- 204.00m 204.00m
[root@localhost ~]#
如果加入失败可以先将磁盘格式化:mkfs.etx4 /dev/sdb 格式化成什么格式自己选,这里是ext4
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- 25.80g 0
/dev/sdb lvm2 --- 204.00m 204.00m
[root@localhost ~]#
[root@localhost ~]# pvcreate /dev/sdc
Physical volume "/dev/sdc" successfully created
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- 25.80g 0
/dev/sdb lvm2 --- 204.00m 204.00m
/dev/sdc lvm2 --- 204.00m 204.00m
[root@localhost ~]#
思考:现在有两个PV物理卷了,我们需要将这两个物理卷加入卷组,所以需要创建一个卷组VG
注意:在某个物理卷在加入卷组时,会将物理卷的最小存储单元设定为一个固定的值,这个值称为PE(physical extent)。这个值的创建,是为了保证用统一的最小分配单元来创建逻辑卷,不至于因为分配单元大小不同而造成空间浪费。举个例子:用于远洋运输的集装箱的设计是是有着统一标准的,最重要一点是集装箱大小完全相同,这样做的好处是集装箱相互堆叠在一起不会留下多余的空隙,完全利用了空间,且便于管理。设定PE的原因也与此相同。LVM以最小分配单元来创建逻辑卷,该最小分配单元的值称为LE(logical extent)。一般来说PE=LE,且大小为2^n。
[root@localhost ~]# vgcreate datavg /dev/sdb /dev/sdc
Volume group "datavg" successfully created
[root@localhost ~]# vds
bash: vds: command not found...
通过以下两个命令可以看出sdb和sdc物理卷都已加入名为datavg的卷组中了
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
datavg 2 0 0 wz--n- 400.00m 400.00m
rhel 1 2 0 wz--n- 25.80g 0
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- 25.80g 0
/dev/sdb datavg lvm2 a-- 200.00m 200.00m
/dev/sdc datavg lvm2 a-- 200.00m 200.00m
[root@localhost ~]#
datavg卷组还可以扩展新的pv ,这里我们暂时不扩展
[root@localhost ~]# lvcreate -L 100M -n lv1 datavg
Logical volume "lv1" created.
[root@localhost ~]#
查看下逻辑卷的配置情况:
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 datavg -wi-a----- 100.00m
home rhel -wi-ao---- 2.00g
root rhel -wi-ao---- 23.80g
[root@localhost ~]# lvscan
ACTIVE '/dev/datavg/lv1' [100.00 MiB] inherit
ACTIVE '/dev/rhel/home' [2.00 GiB] inherit
ACTIVE '/dev/rhel/root' [23.80 GiB] inherit
[root@localhost ~]#
[root@localhost ~]# mkfs.ext4 /dev/datavg/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
25688 inodes, 102400 blocks
5120 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33685504
13 block groups
8192 blocks per group, 8192 fragments per group
1976 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 ~]# echo $?
0
[root@localhost ~]#
知识点:确认上一步操作是否成功可用 [root@localhost ~]# echo $? 检查,为0表示成功,其他都是失败
[root@localhost /]# mkdir /lv1
[root@localhost /]# mount /dev/datavg/lv1 /lv1/
[root@localhost /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 24G 3.6G 19G 16% /
devtmpfs 898M 0 898M 0% /dev
tmpfs 913M 84K 913M 1% /dev/shm
tmpfs 913M 9.2M 904M 1% /run
tmpfs 913M 0 913M 0% /sys/fs/cgroup
/dev/sda1 190M 133M 43M 76% /boot
/dev/mapper/rhel-home 2.0G 12M 1.8G 1% /home
tmpfs 183M 0 183M 0% /run/user/0
tmpfs 183M 16K 183M 1% /run/user/42
/dev/mapper/datavg-lv1 93M 1.6M 85M 2% /lv1
[root@localhost /]#
当然我们也可以设置将逻辑卷永久挂载
①将逻辑卷的lv1的UUID写到/etc/fstab配置文件中
[root@localhost lv1]# blkid |grep "lv1" |awk '{print $2}' >> /etc/fstab
②重新加载下配置文件/etc/fstab
[root@localhost /]# mount -a
[root@localhost /]# vgs
VG #PV #LV #SN Attr VSize VFree
datavg 2 1 0 wz--n- 400.00m 300.00m
rhel 1 2 0 wz--n- 25.80g 0
[root@localhost /]#
[root@localhost /]# vgextend datavg /dev/sdd
[root@localhost ~]# vgreduce datavg /dev/sdd
[root@localhost ~]# vgremove datavg
假如pv物理卷/dev/sdb硬盘已经老化,需要将sdb里的数据在线迁移到sdc物理卷
[root@localhost /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 24G 3.6G 19G 16% /
devtmpfs 898M 0 898M 0% /dev
tmpfs 913M 84K 913M 1% /dev/shm
tmpfs 913M 9.2M 904M 2% /run
tmpfs 913M 0 913M 0% /sys/fs/cgroup
/dev/sda1 190M 133M 43M 76% /boot
/dev/mapper/rhel-home 2.0G 12M 1.8G 1% /home
tmpfs 183M 0 183M 0% /run/user/0
tmpfs 183M 16K 183M 1% /run/user/42
/dev/mapper/datavg-lv1 93M 33M 54M 39% /lv1
[root@localhost /]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- 25.80g 0
/dev/sdb datavg lvm2 a-- 200.00m 100.00m
/dev/sdc datavg lvm2 a-- 200.00m 200.00m
/dev/sdd datavg lvm2 a-- 200.00m 200.00m
[root@localhost /]#
[root@localhost etc]# pvmove /dev/sdb /dev/sdc
/dev/sdb: Moved: 12.0%
/dev/sdb: Moved: 100.0%
[root@localhost etc]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- 25.80g 0
/dev/sdb datavg lvm2 a-- 200.00m 200.00m
/dev/sdc datavg lvm2 a-- 200.00m 100.00m
/dev/sdd datavg lvm2 a-- 200.00m 200.00m
[root@localhost etc]#
[root@localhost etc]# vgreduce datavg /dev/sdb
Removed "/dev/sdb" from volume group "datavg"
[root@localhost etc]# vgs
VG #PV #LV #SN Attr VSize VFree
datavg 2 1 0 wz--n- 400.00m 300.00m
rhel 1 2 0 wz--n- 25.80g 0
[root@localhost etc]#
// partprobe命令用于重读分区表,当出现删除文件后,出现仍然占用空间。可以partprobe在不重启的情况下重读分区。
//partprobe命令k可以不用重启系统即可配合fdisk、gdisk工具创建新的分区
[root@localhost /]# partprobe
// 制作/dev/datavg/lv1的快照lv1-bak
[root@localhost lv1]# df -h
.......
/dev/mapper/datavg-lv1 93M 1.6M 85M 2% /lv1
[root@localhost lv1]#
// 制作快照前最好先卸载/dev/datavg/lv1
[root@localhost /]# umount /lv1/
[root@localhost /]# lvcreate -L 35M -n lv1-bak -s /dev/datavg/lv1
Rounding up size to full physical extent 36.00 MiB
Logical volume "lv1-bak" created.
[root@localhost /]# df -h
........
/dev/mapper/datavg-lv1--bak 93M 33M 54M 39% /lv1
lvm的拉伸与缩减
// 此操作只能扩大
[root@server188 ~]# lvextend -L 500M /dev/datavg/lv1
Extending logical volume lv0 to 500.00 MiB
Logical volume lv0 successfully resized
[root@server188 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
….. ….. ….. …
/dev/mapper/datavg-lv1 197M 11M 187M 6% /lv1
// xfs格式同步下设备
[root@server188 ~]# xfs_growfs /dev/datavg/lv1
[root@server188 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
……. …… … …
/dev/mapper/datavg-lv1 497M 11M 487M 3% /lv1
针对ext文件系统—–lvm设备的扩大(可拉伸也可缩减)
lvm设备的拉伸
//将/dev/mapper/datavg-lv1格式化为ext格式
[root@server188 ~]# umount /dev/mapper/datavg-lv1
[root@server188 ~]# mkfs.ext4 /dev/mapper/vg0-lv0
//此时要扩展到600M,但vg中容量<600M,因此vg中加入新的物理卷pv
[root@localhost /]# vgextend datavg /dev/sdd
[root@server188 ~]# lvextend -L 2G /dev/vg0/lv0
Extending logical volume lv0 to 2.00 GiB
Insufficient free space: 387 extents needed, but only 130 available
lvm也可以缩减,但是一般很少会缩减,具体方法参考:
使用逻辑卷管理器管理灵活存储 lvm
// lvm丢失pv设备:unknown device datavg lvm2 a-m 200.00m 200.00m
[root@localhost ~]# pvs
WARNING: Device for PV MQ1kYt-eH2x-ECas-Gmd6-QNje-j8fq-Of62rx not found or rejected by a filter.
WARNING: Device for PV 8MPnLa-2u7t-UJus-XZev-M3wj-zbx0-q3AZwT not found or rejected by a filter.
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- 25.80g 0
/dev/sdb lvm2 --- 204.00m 204.00m
/dev/sdc datavg lvm2 a-- 200.00m 200.00m
unknown device datavg lvm2 a-m 200.00m 200.00m
unknown device datavg lvm2 a-m 200.00m 200.00m
// 当想删除未知设备[unknown device]时用下面的命令
[root@localhost ~]# vgreduce --removemissing datavg
WARNING: Device for PV MQ1kYt-eH2x-ECas-Gmd6-QNje-j8fq-Of62rx not found or rejected by a filter.
WARNING: Device for PV 8MPnLa-2u7t-UJus-XZev-M3wj-zbx0-q3AZwT not found or rejected by a filter.
WARNING: Device for PV MQ1kYt-eH2x-ECas-Gmd6-QNje-j8fq-Of62rx not found or rejected by a filter.
WARNING: Device for PV 8MPnLa-2u7t-UJus-XZev-M3wj-zbx0-q3AZwT not found or rejected by a filter.
Wrote out consistent volume group datavg
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- 25.80g 0
/dev/sdb lvm2 --- 204.00m 204.00m
/dev/sdc datavg lvm2 a-- 200.00m 200.00m
[root@localhost ~]#