lvm explanation
DiskA 分区分别为Physical Volume1、Physical Volume2;DiskB 分区为Physical Volume3,在这里Physical volume1,2,3就是磁盘分区
也就是物理卷;然后由单个或多个物理卷组成物理卷组,最后在物理卷的基础上划分出逻辑卷,而逻辑卷格式化后挂载上就可以使用了
Preparing the Disk Partition Phase
- fdisk /dev/sdb
- Create three new partitions, each with a size of 2G
- Change the partition type to 8e (use t in fdisk to change the type)
- Create new partition
//这里我创建了5个2G的分区,多出的2个留待测试用
[root@24centos7-01 /]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
//创建第一个分区
命令(输入 m 获取帮助):n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
分区号 (1-4,默认 1):1
起始 扇区 (2048-20971519,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-20971519,默认为 20971519):+2000M
分区 1 已设置为 Linux 类型,大小设为 2 GiB
//创建第二个分区
命令(输入 m 获取帮助):n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
分区号 (2-4,默认 2):
起始 扇区 (4098048-20971519,默认为 4098048):+2000M
值超出范围。
起始 扇区 (4098048-20971519,默认为 4098048):
将使用默认值 4098048
Last 扇区, +扇区 or +size{K,M,G} (4098048-20971519,默认为 20971519):+2000M
分区 2 已设置为 Linux 类型,大小设为 2 GiB
//创建第三个分区
命令(输入 m 获取帮助):n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
分区号 (3,4,默认 3):
起始 扇区 (8194048-20971519,默认为 8194048):
将使用默认值 8194048
Last 扇区, +扇区 or +size{K,M,G} (8194048-20971519,默认为 20971519):+2000M
分区 3 已设置为 Linux 类型,大小设为 2 GiB
//创建第四、五个分区(省略)
..................
//查看/dev/sdb的分区信息
命令(输入 m 获取帮助):p
磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x6bca4892
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 4098047 2048000 83 Linux
/dev/sdb2 4098048 8194047 2048000 83 Linux
/dev/sdb3 8194048 12290047 2048000 83 Linux
/dev/sdb4 12290048 20971519 4340736 5 Extended
/dev/sdb5 12292096 16388095 2048000 83 Linux
/dev/sdb6 16390144 20971519 2290688 83 Linux
//保存退出
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。
- Change the partition type to 8e
[root@24centos7-01 /]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):t
分区号 (1-6,默认 6):1
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”
命令(输入 m 获取帮助):t
分区号 (1-6,默认 6):2
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”
命令(输入 m 获取帮助):t
分区号 (1-6,默认 6):3
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”
命令(输入 m 获取帮助):t
分区号 (1-6,默认 6):5
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”
命令(输入 m 获取帮助):t
分区号 (1-6,默认 6):6
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”
//保存退出
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。
//查看更改后的磁盘信息
[root@24centos7-01 /]# fdisk -l
磁盘 /dev/sda:32.2 GB, 32212254720 字节,62914560 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0002f5a8
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 4605951 2097152 82 Linux swap / Solaris
/dev/sda3 4605952 62914559 29154304 83 Linux
磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x6bca4892
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 4098047 2048000 8e Linux LVM
/dev/sdb2 4098048 8194047 2048000 8e Linux LVM
/dev/sdb3 8194048 12290047 2048000 8e Linux LVM
/dev/sdb4 12290048 20971519 4340736 5 Extended
/dev/sdb5 12292096 16388095 2048000 8e Linux LVM
/dev/sdb6 16390144 20971519 2290688 8e Linux LVM
- Create physical volumes
[root@24centos7-01 ~]# pvcreate /dev/sdb1
WARNING: dos signature detected on /dev/sdb1 at offset 510. Wipe it? [y/n]: y
Wiping dos signature on /dev/sdb1.
Physical volume "/dev/sdb1" successfully created.
[root@24centos7-01 ~]# pvcreate /dev/sdb2
WARNING: dos signature detected on /dev/sdb2 at offset 510. Wipe it? [y/n]: y
Wiping dos signature on /dev/sdb2.
Physical volume "/dev/sdb2" successfully created.
[root@24centos7-01 ~]# pvcreate /dev/sdb3
Physical volume "/dev/sdb3" successfully created.
//查看当前物理卷
[root@24centos7-01 ~]# pvdisplay
"/dev/sdb2" is a new physical volume of "1.95 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb2
VG Name
PV Size 1.95 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID c4ZHGE-AcJH-Lodo-kai3-XZ5n-fKi1-axrWyc
"/dev/sdb1" is a new physical volume of "1.95 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 1.95 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID UwJYZT-Ws6v-TncS-LaJd-GsJt-cO7O-xPFOhy
"/dev/sdb3" is a new physical volume of "1.95 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb3
VG Name
PV Size 1.95 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID VAiW5C-b1lh-VNWu-Xn03-uyZx-yLYq-DTU8hH
[root@24centos7-01 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 --- 1.95g 1.95g
/dev/sdb2 lvm2 --- 1.95g 1.95g
/dev/sdb3 lvm2 --- 1.95g 1.95g
- Create a physical volume group
[root@24centos7-01 ~]# vgcreate vg1 /dev/sdb1 /dev/sdb2
Volume group "vg1" successfully created
[root@24centos7-01 ~]# vgdisplay
--- Volume group ---
VG Name vg1
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 <3.90 GiB
PE Size 4.00 MiB
Total PE 998
Alloc PE / Size 0 / 0
Free PE / Size 998 / <3.90 GiB
VG UUID 723vEb-tBkQ-E0Gu-eZXI-xAiD-yrNw-DQ6YyH
[root@24centos7-01 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg1 2 0 0 wz--n- <3.90g <3.90g
- Create a logical volume and format it, mount it
[root@24centos7-01 ~]# lvcreate -L 100M -n lv1 vg1 //创建逻辑卷
WARNING: ext4 signature detected on /dev/vg1/lv1 at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/vg1/lv1.
Logical volume "lv1" created.
[root@24centos7-01 ~]# mkfs.ext4 /dev/vg1/lv1 //格式化ext4格式
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@24centos7-01 ~]# mount /dev/vg1/lv1 /mnt/
[root@24centos7-01 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 28G 1.4G 27G 5% /
devtmpfs 483M 0 483M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.7M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 197M 109M 88M 56% /boot
tmpfs 99M 0 99M 0% /run/user/1000
/dev/mapper/vg1-lv1 93M 1.6M 85M 2% /mnt
expansion operation
- Expand logical volume
- lvresize -L 1000M /dev/vg1/lv1 reset the volume size
- e2fsck -f /dev/vg1/lv1 Check disk errors (ext4 execution)
- resize2fs /dev/vg1/lv1 Update logical volume information (ext4 execution)
//lvresize前先卸载
[root@24centos7-01 ~]# umount /mnt/
//重新设置卷大小
[root@24centos7-01 ~]# lvresize -L 500M /dev/vg1/lv1
Size of logical volume vg1/lv1 changed from 100.00 MiB (25 extents) to 500.00 MiB (125 extents).
Logical volume vg1/lv1 successfully resized.
//检查磁盘错误
[root@24centos7-01 ~]# e2fsck -f /dev/vg1/lv1
e2fsck 1.42.9 (28-Dec-2013)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/vg1/lv1: 11/25688 files (9.1% non-contiguous), 8896/102400 blocks
//更新逻辑卷信息
[root@24centos7-01 ~]# resize2fs /dev/vg1/lv1
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg1/lv1 to 1024000 (1k) blocks.
The filesystem on /dev/vg1/lv1 is now 1024000 blocks long.
shrink operation
- Shrink a logical volume (not supported by xfs)
- Unmount umount first
- e2fsck -f /dev/vg1/lv1 Check disk errors
- resize2fs /dev/vg1/lv1 100M Update logical volume information
- lvresize -L 100M /dev/vg1/lv1 reset volume size
//先卸载挂载
[root@24centos7-01 ~]# umount /mnt/
//检查磁盘错误
[root@24centos7-01 ~]# e2fsck -f /dev/vg1/lv1
e2fsck 1.42.9 (28-Dec-2013)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/vg1/lv1: 11/247000 files (9.1% non-contiguous), 37816/1024000 blocks
//更新逻辑卷信息
[root@24centos7-01 ~]# resize2fs /dev/vg1/lv1 500M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg1/lv1 to 512000 (1k) blocks.
The filesystem on /dev/vg1/lv1 is now 512000 blocks long.
//重新设置卷大小
[root@24centos7-01 ~]# lvresize -L 500M /dev/vg1/lv1
WARNING: Reducing active logical volume to 500.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 1000.00 MiB (250 extents) to 500.00 MiB (125 extents).
Logical volume vg1/lv1 successfully resized.
//查看结果
[root@24centos7-01 ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vg1/lv1
LV Name lv1
VG Name vg1
LV UUID R3KUYz-wVeX-ED3N-4Rf5-KJNR-6B27-dT2JjK
LV Write Access read/write
LV Creation host, time 24centos7-01, 2017-11-02 22:21:14 +0800
LV Status available
# open 0
LV Size 500.00 MiB
Current LE 125
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
//重新挂载
[root@24centos7-01 ~]# mount /dev/vg1/lv1 /mnt/
[root@24centos7-01 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 28G 1.4G 27G 5% /
devtmpfs 483M 0 483M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.7M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 197M 109M 88M 56% /boot
tmpfs 99M 0 99M 0% /run/user/1000
/dev/mapper/vg1-lv1 481M 2.3M 450M 1% /mnt
Expansion of xfs format
- xfs expansion does not need to be uninstalled and then operated
- lvresize -L 1000M /dev/vg1/lv1 reset the volume size
- xfs_growfs /dev/vg1/lv1 xfs file system needs to execute this command
//先卸载格式化为xfs格式
[root@24centos7-01 ~]# umount /mnt/
[root@24centos7-01 ~]# mkfs.xfs -f /dev/vg1/lv1
meta-data=/dev/vg1/lv1 isize=512 agcount=4, agsize=32000 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=128000, 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@24centos7-01 ~]# !mount
mount /dev/vg1/lv1 /mnt/
//创建测试文件
[root@24centos7-01 ~]# touch /mnt/nn.txt
[root@24centos7-01 ~]# echo "linux" > /mnt/nn.txt
[root@24centos7-01 ~]# cat /mnt/nn.txt
linux
//查看当前容量
[root@24centos7-01 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg1 -wi-ao---- 500.00m
//扩容xfs时不需要先卸载再操作,然后重新挂载,这里直接重新设置卷大小
[root@24centos7-01 ~]# lvresize -L 1000M /dev/vg1/lv1
Size of logical volume vg1/lv1 changed from 500.00 MiB (125 extents) to 1000.00 MiB (250 extents).
Logical volume vg1/lv1 successfully resized.
[root@24centos7-01 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg1 -wi-ao---- 1000.00m
//查看显示逻辑卷未改变,因为还差最后一步
[root@24centos7-01 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 28G 1.4G 27G 5% /
devtmpfs 483M 0 483M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.7M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 197M 109M 88M 56% /boot
tmpfs 99M 0 99M 0% /run/user/1000
/dev/mapper/vg1-lv1 497M 26M 472M 6% /mnt
//最后一步操作,执行完逻辑卷大小就变化了
[root@24centos7-01 ~]# xfs_growfs /dev/vg1/lv1
meta-data=/dev/mapper/vg1-lv1 isize=512 agcount=4, agsize=32000 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=128000, 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 128000 to 256000
//验证逻辑卷容量是否已变化
[root@24centos7-01 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 28G 1.4G 27G 5% /
devtmpfs 483M 0 483M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.7M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 197M 109M 88M 56% /boot
tmpfs 99M 0 99M 0% /run/user/1000
/dev/mapper/vg1-lv1 997M 26M 972M 3% /mnt
//操作完成,文件未变化
[root@24centos7-01 ~]# cat /mnt/nn.txt
linux
Extend volume group
- fdisk /dev/sdb add /dev/sdb5 (logical partition 8e) 2G
- pvcreate /dev/sdb5
- vgextend vg1 /dev/sdb5
- lvresize -L 4500M /dev/vg1/lv1
//查看当前卷组信息
[root@24centos7-01 ~]# vgdisplay
--- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas 2
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 2
Act PV 2
VG Size <3.90 GiB
PE Size 4.00 MiB
Total PE 998
Alloc PE / Size 250 / 1000.00 MiB
Free PE / Size 748 / 2.92 GiB
VG UUID 723vEb-tBkQ-E0Gu-eZXI-xAiD-yrNw-DQ6YyH
//增加卷组 sdb3 sdb5 sdb6
[root@24centos7-01 ~]# vgextend vg1 /dev/sdb3 /dev/sdb5 /dev/sdb6
Physical volume "/dev/sdb5" successfully created.
Physical volume "/dev/sdb6" successfully created.
Volume group "vg1" successfully extended
//查看增加后的卷组信息1
[root@24centos7-01 ~]# vgdisplay
--- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas 5
Metadata Sequence No 9
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 5
Act PV 5
VG Size 9.98 GiB
PE Size 4.00 MiB
Total PE 2555
Alloc PE / Size 250 / 1000.00 MiB
Free PE / Size 2305 / 9.00 GiB
VG UUID 723vEb-tBkQ-E0Gu-eZXI-xAiD-yrNw-DQ6YyH
//查看增加后的卷组信息2
[root@24centos7-01 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg1 5 1 0 wz--n- 9.98g 9.00g
//重新设置逻辑卷大小
[root@24centos7-01 ~]# lvresize -L 4500M /dev/vg1/lv1
Size of logical volume vg1/lv1 changed from <2.93 GiB (750 extents) to 4.39 GiB (1125 extents).
Logical volume vg1/lv1 successfully resized.
//更新逻辑卷容量
[root@24centos7-01 ~]# xfs_growfs /dev/vg1/lv1
meta-data=/dev/mapper/vg1-lv1 isize=512 agcount=24, agsize=32000 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=768000, 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 768000 to 1152000
[root@24centos7-01 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 28G 1.4G 27G 5% /
devtmpfs 483M 0 483M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.7M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 197M 109M 88M 56% /boot
tmpfs 99M 0 99M 0% /run/user/1000
/dev/mapper/vg1-lv1 4.4G 27M 4.4G 1% /mnt
Disk failure case
When we operate the /etc/fstab file and the system cannot be started, there may be a problem with the disk mount, that is to say, some disks cannot be mounted to the mount point normally. How to solve the problem and make the system work normally What about startup?
//首先编辑/etc/fstab文件,将添加的自动挂载项删除
[root@24centos7-01 ~]# vi /etc/fstab
/etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Oct 13 05:25:58 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=63c670fa-8f04-465b-be58-3d503456ba89 / xfs defaults 0 0
UUID=025ff7fe-197a-4c98-b68a-74531830867c /boot xfs defaults 0 0
UUID=969c1d6a-9dd4-4dc6-a785-1b79065ecf02 swap swap defaults 0 0
/dev/sdb1 /mnt xfs defaults 0 0 //删除添加的自动挂载项
//保存退出后重启系统即OK
[root@24centos7-01 ~]# reboot