LVM是在硬盘分区和文件系统之间添加了一个逻辑层,用户不必关心物理硬盘设备的底层架构和布局,可以实现对硬盘分区的动态调整。
为了验证逻辑卷与物理硬盘无关的特性,在虚拟机增加两块硬盘,然后重启虚拟机。这时还看不到这两块硬盘。
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 18G 3.2G 15G 18% /
devtmpfs 905M 0 905M 0% /dev
tmpfs 914M 140K 914M 1% /dev/shm
tmpfs 914M 9.0M 905M 1% /run
tmpfs 914M 0 914M 0% /sys/fs/cgroup
/dev/sda1 497M 119M 379M 24% /boot
/dev/sr0 3.5G 3.5G 0 100% /mnt/cdrom
这里sdb和sdc就是新加的硬盘
[root@localhost Desktop]# ls /dev/sd*
/dev/sda /dev/sda1 /dev/sda2 /dev/sdb /dev/sdc
现在在sdb和sdc启用lvm
[root@localhost Desktop]# pvcreate /dev/sdb /dev/sdc
Physical volume "/dev/sdb" successfully created
Physical volume "/dev/sdc" successfully created
把sdb和sdc加入storage卷组,sdb是10G,sdc是2G,可以看到下面显示storage是12G
[root@localhost Desktop]# vgcreate storage /dev/sdb /dev/sdc
Volume group "storage" successfully created
[root@localhost Desktop]# vgdisplay
--- Volume group ---
VG Name rhel
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 4994 / 19.51 GiB
Free PE / Size 0 / 0
VG UUID Hgh6Rz-Jw3s-89rg-rlTb-ydtT-o0jD-CzRJfx
--- Volume group ---
VG Name storage
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 11.99 GiB
PE Size 4.00 MiB
Total PE 3070
Alloc PE / Size 0 / 0
Free PE / Size 3070 / 11.99 GiB
VG UUID GG1aS3-kXkF-YeGn-31rE-BFEY-Y9sT-8dpd5l
在storage里切割出150M的一个逻辑卷vo,这里-l 表示使用PE,37个PE = 4M*37=148M, 或者使用-L 150M直接指定大小,因为PE大小默认为4M,结果会变成152M。
[root@localhost Desktop]# lvcreate -n vo -l 37 storage
Logical volume "vo" created
[root@localhost Desktop]# lvdisplay
....
--- Logical volume ---
LV Path /dev/storage/vo
LV Name vo
VG Name storage
LV UUID VIUdJC-hziC-14da-76Y1-E1UO-gxTa-UYmMsf
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2018-10-29 17:28:21 +0000
LV Status available
# open 0
LV Size 148.00 MiB
Current LE 37
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
[root@localhost Desktop]#
格式化为ext4,并挂载到/linuxlv目录下
[root@localhost Desktop]# mkfs.ext4 /dev/storage/vo
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
38000 inodes, 151552 blocks
7577 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
2000 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 Desktop]# mkdir /linuxlv
[root@localhost Desktop]# mount /dev/storage/vo /linuxlv
可以看到已经生效了
[root@localhost Desktop]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 18G 3.1G 15G 18% /
devtmpfs 905M 0 905M 0% /dev
tmpfs 914M 140K 914M 1% /dev/shm
tmpfs 914M 8.9M 905M 1% /run
tmpfs 914M 0 914M 0% /sys/fs/cgroup
/dev/sda1 497M 119M 379M 24% /boot
/dev/sr0 3.5G 3.5G 0 100% /run/media /root/RHEL-7.0 Server.x86_64
/dev/mapper/storage-vo 140M 1.6M 128M 2% /linuxlv
写入配置文件,使其启动生效,也可vim /etc/fstab文件直接增加一行:
[root@localhost Desktop]# echo "/dev/storage/vo /linuxlv ext4 defaults 0 0" >> /etc/fstab
现在尝试扩展vo到300M
首先卸载设备
[root@localhost Desktop]# umount /linuxlv
然后扩展到300M
[root@localhost Desktop]# lvextend -L 300M /dev/storage/vo
Extending logical volume vo to 300.00 MiB
Logical volume vo successfully resized
检查硬盘完整性,重置容量
[root@localhost Desktop]# e2fsck -f /dev/storage/vo
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/storage/vo: 11/38000 files (0.0% non-contiguous), 10453/151552 blocks
[root@localhost Desktop]# resize2fs /dev/storage/vo
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/storage/vo to 307200 (1k) blocks.
The filesystem on /dev/storage/vo is now 307200 blocks long.
重新挂载,再看下vo卷大小已经更改了。
[root@localhost Desktop]# mount -a
[root@localhost Desktop]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 18G 3.1G 15G 18% /
devtmpfs 905M 0 905M 0% /dev
tmpfs 914M 140K 914M 1% /dev/shm
tmpfs 914M 8.9M 905M 1% /run
tmpfs 914M 0 914M 0% /sys/fs/cgroup
/dev/sda1 497M 119M 379M 24% /boot
/dev/sr0 3.5G 3.5G 0 100% /run/media /root/RHEL-7.0 Server.x86_64
/dev/mapper/storage-vo 287M 2.1M 266M 1% /linuxlv
[root@localhost Desktop]#
缩小逻辑卷vo到120M。由于缩小硬盘容量有丢失数据的风险,所以在生产环境中要先备份数据,检查硬盘完整性,再进行缩小容量的操作。记得先卸载设备。
[root@localhost Desktop]# umount /linuxlv
[root@localhost Desktop]# e2fsck -f /dev/storage/vo
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/storage/vo: 11/76000 files (0.0% non-contiguous), 15759/307200 blocks
[root@localhost Desktop]# resize2fs /dev/storage/vo 120M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/storage/vo to 122880 (1k) blocks.
The filesystem on /dev/storage/vo is now 122880 blocks long.
重新挂载看下,vo卷已经减少到120M了。
[root@localhost Desktop]# mount -a
[root@localhost Desktop]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 18G 3.1G 15G 18% /
devtmpfs 905M 0 905M 0% /dev
tmpfs 914M 140K 914M 1% /dev/shm
tmpfs 914M 8.9M 905M 1% /run
tmpfs 914M 0 914M 0% /sys/fs/cgroup
/dev/sda1 497M 119M 379M 24% /boot
/dev/sr0 3.5G 3.5G 0 100% /run/media /root/RHEL-7.0 Server.x86_64
/dev/mapper/storage-vo 113M 1.6M 103M 2% /linuxlv
一路删除逻辑卷-VG-PV:
[root@localhost ~]# umount /mnt/vo
[root@localhost ~]# lvremove /dev/storage/vo
Do you really want to remove active logical volume vo? [y/n]: y
Logical volume "vo" successfully removed
[root@localhost ~]# vgremove storage
Volume group "storage" successfully removed
[root@localhost ~]# pvremove /dev/sdc
Labels on physical volume "/dev/sdc" successfully wiped
[root@localhost ~]# pvscan
PV /dev/sda2 VG rhel lvm2 [19.51 GiB / 0 free]
PV /dev/sdb lvm2 [4.00 GiB]
Total: 2 [23.51 GiB] / in use: 1 [19.51 GiB] / in no VG: 1 [4.00 GiB]
相关命令