LVM的应用

1.LVM的应用
问题
1)逻辑卷的管理
准备两个10GB的分区,建立卷组vgnsd
再准备一个10GB的分区,建立卷组vgdata
从卷组vgnsd中划出一个15GB的逻辑卷lvhome
从卷组vgdata中划出一个4GB的逻辑卷lvswap
2)逻辑卷的实际使用
将/home目录迁移到逻辑卷lvhome上
将逻辑卷lvswap扩展到交换空间
确保上述卷开机后自动挂载
方案
LVM是在Linux中特殊的磁盘卷,它可以整合多个小的磁盘或分区,并且空间能够实现伸缩。创建及使用LVM过程:创建PV --> 创建VG --> 创建LVM --> 格式化LVM --> mount。
用来管理LVM命令主要命令的语法:
pvcreate 设备名…
vgcreate 卷组名 物理卷…
lvcreate -L 大小 -n 逻辑卷名 卷组名
步骤
实现此案例需要按照如下步骤进行。
步骤一:逻辑卷的管理
准备两个10GB的分区,建立卷组vgnsd,命令操作如下所示:
[root@localhost /]# parted /dev/sdb print //输出sdb分区表
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 85.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system 标志
1 512B 1000MB 1000MB primary ext4
2 1000MB 2000MB 999MB primary ext4
3 2000MB 85.9GB 83.9GB extended lba
5 2001MB 3000MB 999MB logical

[root@localhost /]# parted /dev/sdb mktable gpt //将sdb分区方式更改为gpt
警告: 正在使用 /dev/sdb 上的分区。
忽略/Ignore/放弃/Cancel? i
警告: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue?
是/Yes/否/No? y
警告: WARNING: the kernel failed to re-read the partition table on /dev/sdb (设备或资源忙). As a result, it may not reflect all of your changes until after reboot.
[root@localhost /]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 85.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name 标志

[root@localhost /]# parted /dev/sdb mkpart primary 0 10G //划分10G分区
警告: The resulting partition is not properly aligned for best performance.
忽略/Ignore/放弃/Cancel? i
警告: WARNING: the kernel failed to re-read the partition table on /dev/sdb (设备或资源忙). As a
result, it may not reflect all of your changes until after reboot.
[root@localhost /]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 85.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name 标志
1 17.4kB 10.0GB 10000MB primary

[root@localhost /]# parted /dev/sdb mkpart primary 10G 20G
警告: WARNING: the kernel failed to re-read the partition table on /dev/sdb (设备或资源忙). As a
result, it may not reflect all of your changes until after reboot.
[root@localhost /]# parted /dev/sdb mkpart primary 20G 30G
警告: WARNING: the kernel failed to re-read the partition table on /dev/sdb (设备或资源忙). As a
result, it may not reflect all of your changes until after reboot.
[root@localhost /]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 85.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name 标志
1 17.4kB 10.0GB 10000MB primary
2 10.0GB 20.0GB 9999MB primary
3 20.0GB 30.0GB 10.0GB primary

[root@localhost /]#
这个时候需要系统重新识别GPT分区模式以及新的分区表,建议大家重启。
[root@localhost ~]# init 6
可以给LVM分区加一个标志以方便管理及查看。
[root@localhost ~]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 85.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name 标志
1 17.4kB 10.0GB 10000MB primary
2 10.0GB 20.0GB 9999MB primary
3 20.0GB 30.0GB 10.0GB primary

[root@localhost ~]# parted /dev/sdb set 1 lvm on //set:添加标志;1:为分区编号;lvm:标志;on:开启
信息: You may need to update /etc/fstab.

[root@localhost ~]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 85.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name 标志
1 17.4kB 10.0GB 10000MB primary lvm
2 10.0GB 20.0GB 9999MB primary
3 20.0GB 30.0GB 10.0GB primary

[root@localhost ~]# parted /dev/sdb set 2 lvm on
信息: You may need to update /etc/fstab.

[root@localhost ~]# parted /dev/sdb set 3 lvm on
信息: You may need to update /etc/fstab.

[root@localhost ~]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 85.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name 标志
1 17.4kB 10.0GB 10000MB primary lvm
2 10.0GB 20.0GB 9999MB primary lvm
3 20.0GB 30.0GB 10.0GB primary lvm

[root@localhost ~]#
准备两个10GB的分区,创建卷组vgnsd,首先要创建物理卷:
[root@localhost ~]# pvscan //查看现有物理卷
No matching physical volumes found
[root@localhost ~]# ls /dev/sdb[1-3] //查看是否识别新的分区
/dev/sdb1 /dev/sdb2 /dev/sdb3
[root@localhost ~]# pvcreate /dev/sdb1 /dev/sdb2 /dev/sdb3 //创建物理分区
Physical volume “/dev/sdb1” successfully created
Physical volume “/dev/sdb2” successfully created
Physical volume “/dev/sdb3” successfully created
[root@localhost ~]# pvscan //查看结果
PV /dev/sdb1 lvm2 [9.31 GiB]
PV /dev/sdb2 lvm2 [9.31 GiB]
PV /dev/sdb3 lvm2 [9.31 GiB]
Total: 3 [27.94 GiB] / in use: 0 [0 ] / in no VG: 3 [27.94 GiB]
[root@localhost ~]# vgscan //查看现有卷组
Reading all physical volumes. This may take a while…
No volume groups found
[root@localhost ~]# vgcreate vgnsd /dev/sdb1 /dev/sdb2 //创建卷组
Volume group “vgnsd” successfully created
[root@localhost ~]# vgscan //查看结果
Reading all physical volumes. This may take a while…
Found volume group “vgnsd” using metadata type lvm2
再准备一个10GB的分区,建立卷组vgdata:
[root@localhost ~]# pvscan //查看所有物理卷
PV /dev/sdb1 VG vgnsd lvm2 [9.31 GiB / 9.31 GiB free]
PV /dev/sdb2 VG vgnsd lvm2 [9.31 GiB / 9.31 GiB free]
PV /dev/sdb3 lvm2 [9.31 GiB]
Total: 3 [27.93 GiB] / in use: 2 [18.62 GiB] / in no VG: 1 [9.31 GiB]
[root@localhost ~]# vgcreate vgdata /dev/sdb3 //创建卷组vgdata
Volume group “vgdata” successfully created
[root@localhost ~]# pvscan //查看所有物理卷
PV /dev/sdb3 VG vgdata lvm2 [9.31 GiB / 9.31 GiB free]
PV /dev/sdb1 VG vgnsd lvm2 [9.31 GiB / 9.31 GiB free]
PV /dev/sdb2 VG vgnsd lvm2 [9.31 GiB / 9.31 GiB free]
Total: 3 [27.93 GiB] / in use: 3 [27.93 GiB] / in no VG: 0 [0 ]
[root@localhost ~]# vgscan //查看所有卷组
Reading all physical volumes. This may take a while…
Found volume group “vgdata” using metadata type lvm2
Found volume group “vgnsd” using metadata type lvm2
[root@localhost ~]#
从卷组vgnsd中划出一个15GB的逻辑卷lvhome:
[root@localhost ~]# lvs //查看所有逻辑卷
[root@localhost ~]#
[root@localhost ~]# lvcreate -L 15G -n lvhome vgnsd //划分逻辑卷
Logical volume “lvhome” created
[root@localhost ~]# lvs //查看结果
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lvhome vgnsd -wi-a----- 15.00g
[root@localhost ~]#
从卷组vgdata中划出一个4GB的逻辑卷lvswap:
[root@localhost ~]# lvs //查看所有逻辑卷
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lvhome vgnsd -wi-a----- 15.00g
[root@localhost ~]# lvcreate -L 4G -n lvswap vgdata //划分逻辑卷
Logical volume “lvswap” created
[root@localhost ~]# lvs //查看结果
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lvswap vgdata -wi-a----- 4.00g
lvhome vgnsd -wi-a----- 15.00g
[root@localhost ~]#
步骤二:逻辑卷的实际使用
将/home目录迁移到逻辑卷lvhome上。
分析: 原来/home目录所占用空间来源于根分区,现在想把/home目录迁移到其他分区上,以减小根分区压力。具体思路是:先将home目录里内容作一个备份,然后在将lvhome逻辑卷挂载到/home目录,再将内容还原。需注意在挂载使用前需将lvhome格式化。
命令操作如下所示:
[root@localhost ~]# mkdir /home.bak //创建备份目录
[root@localhost ~]# ls /home
gelin01 gelin02 ht02 john kaka lily lisi mike zhangsan
[root@localhost ~]# mv /home/* /home.bak //备份home目录下内容
[root@localhost ~]# mkfs.ext4 /dev/vgnsd/lvhome //格式化逻辑卷
[root@localhost ~]# mount /dev/vgnsd/lvhome /home //挂载lvhome逻辑卷
[root@localhost ~]# mount | grep home //查看挂载结果
/dev/mapper/vgnsd-lvhome on /home type ext4 (rw)
[root@localhost ~]# mv /home.bak/* /home //还原home内容
[root@localhost ~]# ls /home
gelin01 gelin02 ht02 john kaka lily lisi lost+found mike zhangsan
[root@localhost ~]#
将逻辑卷lvswap扩展到交换空间。
分析: 将lvswap逻辑卷格式化swap文件系统,在启用即可。
[root@localhost ~]# mkswap /dev/vgdata/lvswap //格式化成swap文件系统
mkswap: /dev/vgdata/lvswap: warning: don’t erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=ba5895f4-0662-40e0-97ee-41cb095fddd2
[root@localhost ~]# swapon /dev/vgdata/lvswap //启用
[root@localhost ~]# swapon –s //查看
Filename Type Size Used Priority
/dev/sda5 partition 8388600 0 -1
/dev/sda8 partition 1951736 0 -2
/dev/dm-1 partition 4194296 0 -3
[root@localhost ~]# ls -l /dev/vgdata/lvswap //可以看出lvswap是一个链接文件
lrwxrwxrwx. 1 root root 7 2月 28 15:15 /dev/vgdata/lvswap -> …/dm-1
[root@localhost ~]#
确保上述卷开机后自动挂载。
分析: 将设备和参数写入到/etc/fstab配置文件即可。
[root@localhost ~]# vim /etc/fstab
[root@localhost ~]# tail -n 2 /etc/fstab
/dev/vgnsd/lvhome /home ext4 defaults 0 0
/dev/vgdata/lvswap swap swap defaults 0 0
[root@localhost ~]# mount –a //是否有错误输出
[root@localhost ~]# mount | grep lvhome //查看挂载情况
/dev/mapper/vgnsd-lvhome on /home type ext4 (rw)
[root@localhost ~]#

你可能感兴趣的:(Linux,系统学习Linux云计算,系统学习Linux云计算)