lvm逻辑卷创建
在系统中,一块硬盘分区从几个区。以Windows为例子:通常1个500G硬盘分区为C/D/E/F四个区,分别为100G、100G、100G、200G;当D盘用满了,必须存到D盘时,我们可以使用lvm逻辑卷组将DEF三个盘捆绑到一起组成一个逻辑卷组实现三个盘共用;实际生产环境中很少用到这个方案,一般解决方案就是RAID解决,将服务器多个硬盘捆绑成一个大硬盘,直接将大硬盘挂载使用即可;
lvm创建主要分成几步:
- 创建物理分区
- 物理分区转化为物理卷
- 创建物理卷组
- 物理卷组转化为逻辑卷
- 格式化加载
安装lvm包:
yum install -y lvm2
不知道lvm的包时可以搜索下
yum provides "/*/pvcreate"
创建物理分区
fdisk /dev/sdb
分区后更改格式为LVM
命令(输入 m 获取帮助):t
分区号 (1-3,默认 3):
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”
命令(输入 m 获取帮助):p
磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xabd6b164
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 8e Linux LVM
/dev/sdb2 2099200 4196351 1048576 8e Linux LVM
/dev/sdb3 4196352 6293503 1048576 8e Linux LVM
创建物理卷
生成/dev/sdb1文件[root@shu-test ~]# partprobe
创建物理卷
pvcreate /dev/sdb1
[root@shu-test ~]# pvcreate /dev/sdb1
WARNING: xfs signature detected on /dev/sdb1 at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/sdb1.
Physical volume "/dev/sdb1" successfully created.
[root@shu-test ~]# pvcreate /dev/sdb2
Physical volume "/dev/sdb2" successfully created.
[root@shu-test ~]# pvcreate /dev/sdb3
Physical volume "/dev/sdb3" successfully created.
[root@shu-test ~]#
列出当前的物理卷
pvdisplay
[root@shu-test ~]# pvdisplay
"/dev/sdb1" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 1.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID NH8JlM-MGa0-x2hx-PK0x-iHYF-ffSS-c09CM3
"/dev/sdb2" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb2
VG Name
PV Size 1.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID hU0e51-B7tt-dIjf-wYgB-f5Gf-x2zK-YsCWBk
"/dev/sdb3" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb3
VG Name
PV Size 1.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID JiM0Jj-AQ4f-leUm-7kGH-xKGV-yC3a-7HAKyj
[root@shu-test ~]#
pvs查看物理卷
[root@shu-test ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 --- 1.00g 1.00g
/dev/sdb2 lvm2 --- 1.00g 1.00g
/dev/sdb3 lvm2 --- 1.00g 1.00g
[root@shu-test ~]#
删除物理卷pvremove /dev/sdb3
创建卷组
创建卷组vg1,中间包含sdb1与sdb2
vgs命令:查看卷组
[root@shu-test ~]# vgcreate vg1 /dev/sdb1 /dev/sdb2
Volume group "vg1" successfully created
[root@shu-test ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg1 2 0 0 wz--n- 1.99g 1.99g
[root@shu-test ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 vg1 lvm2 a-- 1020.00m 1020.00m
/dev/sdb2 vg1 lvm2 a-- 1020.00m 1020.00m
/dev/sdb3 lvm2 --- 1.00g 1.00g
[root@shu-test ~]#
创建逻辑卷
lvcreate -L 100M -n lv1 vg1
[root@shu-test ~]# lvcreate -L 100M -n lv1 vg1
Logical volume "lv1" created.
[root@shu-test ~]#
格式化与挂载
[root@shu-test ~]# mkfs.ext4 /dev/vg1/lv1
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=1024 (log=0)
分块大小=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
25688 inodes, 102400 blocks
5120 blocks (5.00%) reserved for the super user
第一个数据块=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: 完成
正在写入inode表: 完成
Creating journal (4096 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
[root@shu-test ~]# mount /dev/vg1/lv1 /mnt/
[root@shu-test ~]# df -h |grep -v tmpfs
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 28G 1.5G 27G 6% /
/dev/sda1 197M 97M 100M 50% /boot
/dev/mapper/vg1-lv1 93M 1.6M 85M 2% /mnt
[root@shu-test ~]# ls -l /dev/vg1/lv1
lrwxrwxrwx. 1 root root 7 12月 29 19:33 /dev/vg1/lv1 -> ../dm-0
[root@shu-test ~]#
lvm扩容(ext4)
先卸载/mnt然后修改容量
修改逻辑卷容量大小
[root@shu-test ~]# umount /mnt/
[root@shu-test ~]# ls /mnt/
[root@shu-test ~]# lvresize -L 300M /dev/vg1/lv1
Size of logical volume vg1/lv1 changed from 200.00 MiB (50 extents) to 300.00 MiB (75 extents).
Logical volume vg1/lv1 successfully resized.
[root@shu-test ~]#
检查磁盘错误命令
e2fsck -f /dev/vg1/lv1
[root@shu-test ~]# e2fsck -f /dev/vg1/lv1
e2fsck 1.42.9 (28-Dec-2013)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/vg1/lv1: 13/25688 files (7.7% non-contiguous), 8899/102400 blocks
[root@shu-test ~]#
更新逻辑卷信息
resize2fs /dev/vg1/lv1
[root@shu-test ~]# resize2fs /dev/vg1/lv1
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg1/lv1 to 307200 (1k) blocks.
The filesystem on /dev/vg1/lv1 is now 307200 blocks long.
[root@shu-test ~]#
挂载与检测
[root@shu-test ~]# mount /dev/vg1/lv1 /mnt/
[root@shu-test ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 28G 1.5G 27G 6% /
devtmpfs 483M 0 483M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.8M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 197M 97M 100M 50% /boot
tmpfs 99M 0 99M 0% /run/user/0
/dev/mapper/vg1-lv1 287M 2.1M 266M 1% /mnt
[root@shu-test ~]# ls /mnt/
1212 23.txt lost+found
[root@shu-test ~]# cat /mnt/23.txt
111111111
[root@shu-test ~]#
lvm缩容(ext4)
卸载/mnt/
[root@shu-test ~]# umount /mnt/
[root@shu-test ~]#
检查磁盘错误
e2fsck -f /dev/vg1/lv1
[root@shu-test ~]# e2fsck -f /dev/vg1/lv1
e2fsck 1.42.9 (28-Dec-2013)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/vg1/lv1: 13/75088 files (7.7% non-contiguous), 15640/307200 blocks
[root@shu-test ~]#
更新逻辑卷
resize2fs /dev/vg1/lv1 100M
[root@shu-test ~]# resize2fs /dev/vg1/lv1 100M
resize2fs 1.42.9 (28-Dec-2013)
The filesystem is already 307200 blocks long. Nothing to do!
[root@shu-test ~]#
重新部署卷容量大小
lvresize -L 100M /dev/vg1/lv1
[root@shu-test ~]# lvresize -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 vg1/lv1? [y/n]: y
Size of logical volume vg1/lv1 changed from 300.00 MiB (75 extents) to 100.00 MiB (25 extents).
Logical volume vg1/lv1 successfully resized.
[root@shu-test ~]#
挂载
mount /dev/vg1/lv1 /mnt/
lvm扩容(xfs格式)
xfs格式只能扩容,不能缩小;
格式化逻辑卷
mkfs.xfs -f /dev/vg1/lv1
[root@shu-test ~]# mkfs.xfs -f /dev/vg1/lv1
meta-data=/dev/vg1/lv1 isize=512 agcount=4, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=25600, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=855, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@shu-test ~]#
[root@shu-test ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg1 -wi-ao---- 100.00m
[root@shu-test ~]#
修改容量
lvresize -L 300M /dev/vg1/lv1
[root@shu-test ~]# lvresize -L 300M /dev/vg1/lv1
New size (75 extents) matches existing size (75 extents).
[root@shu-test ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg1 -wi-ao---- 300.00m
[root@shu-test ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 28G 1.5G 27G 6% /
devtmpfs 483M 0 483M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.8M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 197M 97M 100M 50% /boot
tmpfs 99M 0 99M 0% /run/user/0
/dev/mapper/vg1-lv1 97M 5.2M 92M 6% /mnt
自动扩展卷组(自动更新)
xfs_growfs /dev/vg1/lv1
[root@shu-test ~]# xfs_growfs /dev/vg1/lv1
meta-data=/dev/mapper/vg1-lv1 isize=512 agcount=4, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=25600, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=855, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 25600 to 76800
[root@shu-test ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 28G 1.5G 27G 6% /
devtmpfs 483M 0 483M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.8M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 197M 97M 100M 50% /boot
tmpfs 99M 0 99M 0% /run/user/0
/dev/mapper/vg1-lv1 297M 5.5M 292M 2% /mnt
[root@shu-test ~]#
完整步骤:
[root@shu-test ~]# lvresize -L 500M /dev/vg1/lv1
Size of logical volume vg1/lv1 changed from 300.00 MiB (75 extents) to 500.00 MiB (125 extents).
Logical volume vg1/lv1 successfully resized.
[root@shu-test ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg1 -wi-ao---- 500.00m
[root@shu-test ~]# xfs_growfs /dev/vg1/lv1
meta-data=/dev/mapper/vg1-lv1 isize=512 agcount=12, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=76800, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=855, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 76800 to 128000
[root@shu-test ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 28G 1.5G 27G 6% /
devtmpfs 483M 0 483M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.8M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 197M 97M 100M 50% /boot
tmpfs 99M 0 99M 0% /run/user/0
/dev/mapper/vg1-lv1 497M 5.7M 491M 2% /mnt
[root@shu-test ~]#
扩展卷组
新建物理分区
fdisk /dev/sdb
t选项-->设置为8e
新建一个物理分区,更改为lvm模式
添加分区到卷组
将/dev/sdb3加入vg1卷组
vgextend vg1 /dev/sdb3
[root@shu-test ~]# vgextend vg1 /dev/sdb3
Volume group "vg1" successfully extended
[root@shu-test ~]# vgdisplay
--- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 8
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 3
Act PV 3
VG Size <2.99 GiB
PE Size 4.00 MiB
Total PE 765
Alloc PE / Size 125 / 500.00 MiB
Free PE / Size 640 / 2.50 GiB
VG UUID u076Nx-fJYb-OCfu-DNnx-Idd2-irQH-YxPacI
[root@shu-test ~]#
转载于:https://blog.51cto.com/shuzonglu/2056869