1.准备Linux操作系统:Redhat 7.6
2.准备VMware虚拟机软件
Linux的动态磁盘管理LVM逻辑卷的基本概念以及LVM的工作原理,包括LVM中最重要的四个基本点(PE、PV、VG以及LV),以下内容我会详细的讲解LVM逻辑卷的创建、使用以及删除。
使用 pvcreate 命令将准备好的物理硬盘(虚拟机添加硬盘文件),通过 fdisk -l 可以查看各个硬盘信息。
[root@localhost /]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000e76c9
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 616447 307200 83 Linux
/dev/sda2 616448 37748735 18566144 8e Linux LVM
/dev/sda3 37748736 41943039 2097152 82 Linux swap / Solaris
Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/sdc: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/mapper/rhel-root: 19.0 GB, 19008585728 bytes, 37126144 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
根据上图代码中可以看到 /dev/sdb /dev/sdc 两个硬盘,我们需要将这两个硬盘分配到一个PV中。这里我们可以使用pvcreate命令。
[root@localhost /]# pvcreate /dev/sdb /dev/sdc
Physical volume "/dev/sdb" successfully created.
Physical volume "/dev/sdc" successfully created.
如上图代码PV创建成功,我们可以使用pvdisplay命令查看pv详情信息,还可以使用pvs命令查看pv的信息。如下:
[root@localhost /]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name rhel
PV Size <17.71 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4532
Free PE 0
Allocated PE 4532
PV UUID bPx9BI-xIGd-umpK-N1Zp-I5Af-GJE2-1C133I
"/dev/sdb" is a new physical volume of "50.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb
VG Name
PV Size 50.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID uoA1pR-w3wv-Tz7Q-WvRW-5ILJ-EUOd-PjjAre
"/dev/sdc" is a new physical volume of "50.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc
VG Name
PV Size 50.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 7ZNr7h-yQ4X-63lW-sKED-HYXJ-pzeO-VeGkOf
[root@localhost /]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- 17.70g 0
/dev/sdb lvm2 --- 50.00g 50.00g
/dev/sdc lvm2 --- 50.00g 50.00g
通过这两个命令我们可以看到已经创建好的PV的信息,两个PV都是50G,目前还没有使用,PFree都是50G。
在创建完PV以后,这时候我们需要创建一个VG,然后将我们的PV都加入到这个卷组当中,在创建卷组时要给该卷组起一个名字。可以使用 vgcreate 命令创建。如下:
[root@localhost /]# vgcreate vgdata /dev/sdb /dev/sdc
Volume group "vgdata" successfully created
创建好VG以后,可以使用 vgdisplay 或者 vgs 命来来查看VG的信息。如下:
[root@localhost /]# vgdisplay
--- Volume group ---
VG Name vgdata
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 99.99 GiB
PE Size 4.00 MiB
Total PE 25598
Alloc PE / Size 0 / 0
Free PE / Size 25598 / 99.99 GiB
VG UUID oW3rfw-aY56-hBDy-edEN-mMbm-kxg4-Ez05qO
--- Volume group ---
VG Name rhel
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size 17.70 GiB
PE Size 4.00 MiB
Total PE 4532
Alloc PE / Size 4532 / 17.70 GiB
Free PE / Size 0 / 0
VG UUID w8UFmO-yoym-4uqV-sT2D-9fdo-Kca8-m8LKzX
[root@localhost /]# vgs
VG #PV #LV #SN Attr VSize VFree
rhel 1 1 0 wz--n- 17.70g 0
vgdata 2 0 0 wz--n- 99.99g 99.99g
通过查看vg信息可以看到vgdata下VSize和VFree为99.99G。
根据创建好的PV、VG,我们需要在上层创建逻辑卷LV。我们可以通过 lvcreate 创建逻辑卷。如下:
[root@localhost /]# lvcreate -n lvdata -L 99.8g vgdata
Rounding up size to full physical extent 99.80 GiB
WARNING: ext4 signature detected on /dev/vgdata/lvdata at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/vgdata/lvdata.
Logical volume "lvdata" created.
通过 lvcreate 命令基于VG完成逻辑卷创建,lv名字为lvdata,大小为99.8G,这里我们可以通过使用 lvdisplay 或者 lvs 命令来查看创建好的逻辑卷的信息。如下:
[root@localhost /]# lvdisplay
--- Logical volume ---
LV Path /dev/vgdata/lvdata
LV Name lvdata
VG Name vgdata
LV UUID usy4Q6-95Se-hM6E-dJNs-xcn7-wfxs-chP9bu
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2020-06-04 00:56:29 -0700
LV Status available
# open 1
LV Size 99.80 GiB
Current LE 25549
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
--- Logical volume ---
LV Path /dev/rhel/root
LV Name root
VG Name rhel
LV UUID XetJsE-t7q3-DGl2-BF1E-bTgi-H65q-AZv9dO
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2020-06-03 20:22:55 -0700
LV Status available
# open 1
LV Size 17.70 GiB
Current LE 4532
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
[root@localhost /]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root rhel -wi-ao---- 17.70g
lvdata vgdata -wi-ao---- 99.80g
以上我们逻辑卷(LV)也创建完成了,这个时候我们可以通过pvs、vgs、lvs命令查看相关创建成功信息。
[root@localhost /]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- 17.70g 0
/dev/sdb vgdata lvm2 a-- <50.00g 0
/dev/sdc vgdata lvm2 a-- <50.00g 196.00m
[root@localhost /]# vgs
VG #PV #LV #SN Attr VSize VFree
rhel 1 1 0 wz--n- 17.70g 0
vgdata 2 1 0 wz--n- 99.99g 196.00m
[root@localhost /]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root rhel -wi-ao---- 17.70g
lvdata vgdata -wi-ao---- 99.80g
当每创建完一个LV时,VG与PV的信息都是时时在变化,并且我们创建LV的大小是根据当前VG的大小来决定,不能超过当前VG的剩余大小。
以上我们的LV逻辑卷全部创建完成,下面我们可以通过命令查看逻辑卷。
[root@localhost /]# ls /dev/vgdata/lvdata
/dev/vgdata/lvdata
创建好PV、VG以及LV,这时候我们如果要使用逻辑卷,就必须将其格式化成我们需要用的文件系统,并将其挂载起来,然后就可以像使用分区一样去使用逻辑卷。
[root@localhost /]# mkfs.ext4 /dev/vgdata/lvdata
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
6545408 inodes, 26162176 blocks
1308108 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2174746624
799 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
格式化逻辑卷后,我们就可以使用 mount 命令将其进行挂载,我们将其挂载到 /data 目录下。
[root@localhost /]# mount /dev/vgdata/lvdata /data
[root@localhost /]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=919428k,nr_inodes=229857,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,seclabel,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,net_prio,net_cls)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,pids)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,blkio)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,cpuacct,cpu)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,cpuset)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,hugetlb)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,devices)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,freezer)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,perf_event)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,memory)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/mapper/rhel-root on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
selinuxfs on /sys/fs/selinux type selinuxfs (rw,relatime)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=33,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=22980)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel)
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=186324k,mode=700)
/dev/mapper/vgdata-lvdata on /data type ext4 (rw,relatime,seclabel,data=ordered)
[root@localhost /]# cd /data/
[root@localhost data]# ls
lost+found
[root@localhost data]# touch vgdata.txt lvdata.txt
[root@localhost data]# ls
lost+found lvdata.txt vgdata.txt
将逻辑卷永久挂载到/data目录下。我们可以通过修改/etc/fstab实现。如下:
[root@localhost data]# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Jun 3 20:22:56 2020
#
# 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
#
/dev/mapper/rhel-root / xfs defaults 0 0
UUID=6d53884a-62bc-49ca-a40a-a78e3fcd0da2 /boot xfs defaults 0 0
UUID=05b68163-5d7c-4b41-b5ad-6c3f89f15643 swap swap defaults 0 0
/dev/mapper/vgdata-lvdata /data ext4 defaults 0 0 #在最后添加一行。
当系统重启之后就会自动挂载,到这里我们逻辑卷创建和挂载就已经全部完成了。
我们在创建好逻辑卷后可以通过创建文件系统,挂载逻辑卷来使用它,如果说我们不想用了也可以将其删除掉。
【注意:】对于创建物理卷、创建卷组以及创建逻辑卷我们是有严格顺序的,同样,对于删除逻辑卷、删除卷组以及删除物理卷也是有严格顺序要求的
1.首先将正在使用的逻辑卷卸载掉 通过 umount 命令
2.将逻辑卷先删除 通过 lvremove 命令
3.删除卷组 通过 vgremove 命令
4.最后再来删除我们的物理卷 通过 pvremove 命令
[root@localhost /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 18G 1.5G 17G 9% /
devtmpfs 898M 0 898M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 9.6M 901M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/sda1 297M 129M 169M 44% /boot
tmpfs 182M 0 182M 0% /run/user/0
/dev/mapper/vgdata-lvdata 99G 61M 94G 1% /data
[root@localhost /]# umount /data/
[root@localhost /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 18G 1.5G 17G 9% /
devtmpfs 898M 0 898M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 9.6M 901M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/sda1 297M 129M 169M 44% /boot
tmpfs 182M 0 182M 0% /run/user/0
[root@localhost /]# lvremove /dev/vgdata/lvdata
Do you really want to remove active logical volume vgdata/lvdata? [y/n]: y
Logical volume "lvdata" successfully removed
[root@localhost /]# vgremove vgdata
Volume group "vgdata" successfully removed
[root@localhost /]# pvremove /dev/sdb /dev/sdc
Labels on physical volume "/dev/sdb" successfully wiped.
Labels on physical volume "/dev/sdc" successfully wiped.
此时,我们创建的pv、vg、lv所有信息都删除了,我们可以通过pvs、vgs、lvs命令查看。
[root@localhost /]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- 17.70g 0
[root@localhost /]# vgs
VG #PV #LV #SN Attr VSize VFree
rhel 1 1 0 wz--n- 17.70g 0
[root@localhost /]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root rhel -wi-ao---- 17.70g
到此,通过以上命令,我们可以看到,刚才创建逻辑卷相关的信息已经成功删除了。
以上内容是本次编写的LVM逻辑卷的相关创建和使用。