实验环境
VMware® Workstation 15 Pro、Centos 7
对LVM的个人理解
笔者水平较浅,粗略的理解就是,LVM 技术是用于解决硬盘分区格式化,挂载到文件目录后,当容量需要扩容会很困难的问题。
它的原理是在硬盘和文件系统之间增加了一个逻辑层,分为PV(物理卷)、VG(卷组)、LV(逻辑卷)三个概念。
实验步骤
1.给虚拟机添加两块虚拟硬盘,容量1G,然后开机,可以看到系统已经认到了这两 块硬盘/dev/sdb,/dev/sdc
[root@jose_ch ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 12G 0 part
│ ├─centos-root 253:0 0 10G 0 lvm /
│ └─centos-swap 253:1 0 2G 0 lvm [SWAP]
└─sda3 8:3 0 7.8G 0 part /u01
sdb 8:16 0 1G 0 disk
sdc 8:32 0 1G 0 disk
sr0 11:0 1 4.3G 0 rom /run/media/root/CentOS 7 x86_64
2.创建PV相当于把上述两块硬盘纳入到LVM的池子里,就像把菜洗了待用一样
# pvcreate /dev/sdb /dev/sdc
Physical volume "/dev/sdb" successfully created.
Physical volume "/dev/sdc" successfully created.
# pvscan
PV /dev/sda2 VG centos lvm2 [12.00 GiB / 4.00 MiB free]
PV /dev/sdb lvm2 [1.00 GiB]
PV /dev/sdc lvm2 [1.00 GiB]
Total: 3 [14.00 GiB] / in use: 1 [12.00 GiB] / in no VG: 2 [2.00 GiB]
3.下面就是建立VG卷组,可以理解为把两个硬盘归入一个队伍里,值得注意的是,需要给卷组起个名字
# vgcreate kitty /dev/sdb /dev/sdc
Volume group "kitty" successfully created
# vgscan
Reading volume groups from cache.
Found volume group "kitty" using metadata type lvm2
Found volume group "centos" using metadata type lvm2
4.从卷组VG里切割出一个LV逻辑卷来使用啦,我们给逻辑卷取名为mike,使用参数-L切割出一个150M的LV
# lvcreate -n mike -L 150M kitty
Rounding up size to full physical extent 152.00 MiB
Logical volume "mike" created.
# lvscan
ACTIVE '/dev/kitty/mike' [152.00 MiB] inherit
ACTIVE '/dev/centos/root' [10.00 GiB] inherit
ACTIVE '/dev/centos/swap' [2.00 GiB] inherit
5.对生成的逻辑卷mike进行格式化,并做挂载使用,逻辑卷的路径也成为了/dev/kitty/mike
# mkfs.ext4 /dev/kitty/mike
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
38912 inodes, 155648 blocks
7782 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33816576
19 block groups
8192 blocks per group, 8192 fragments per group
2048 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
# mkdir cat
# mount /dev/kitty/mike /cat
# df -h
/dev/mapper/kitty-mike 144M 1.6M 132M 2% /cat
6.将mount状态写入到配置文件,确保设备下次开机挂载依然存在
# echo "/dev/kitty/mike /cat ext4 defaults 0 0" >> /etc/fstab
#reboot
7.扩展逻辑卷/dev/kitty/mike的容量,值得注意的是,需要先取消挂载再扩容
# umount /cat
#lvextend -L 200M /dev/kitty/mike
# e2fsck -f /dev/kitty/mike
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/kitty/mike: 11/38912 files (0.0% non-contiguous), 10567/155648 blocks
# resize2fs /dev/kitty/mike
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/kitty/mike to 204800 (1k) blocks.
The filesystem on /dev/kitty/mike is now 204800 blocks long.
# mount /dev/kitty/mike /cat
8.删除的步骤依照建立的反推回去,即逻辑卷、卷组、物理卷的顺序
# umount /cat
# vi /etc/fstab
# lvremove //dev/kitty/mike
Do you really want to remove active logical volume kitty/mike? [y/n]: y
Logical volume "mike" successfully removed
# vgremove kitty
Volume group "kitty" successfully removed
# pvremove /dev/sdb /dev/sdc
Labels on physical volume "/dev/sdb" successfully wiped.
Labels on physical volume "/dev/sdc" successfully wiped.
结语
今天的实验到此为止,表述有误的请指正!