将物理磁盘标记为物理卷
$ sudo lvmdiskscan
输出将显示LVM可以管理的所有可用存储设备:
/dev/sda [ 200.00 GiB]
/dev/ram1 [ 64.00 MiB]
. . .
/dev/ram15 [ 64.00 MiB]
/dev/sdb [ 100.00 GiB]
2 disks
17 partitions
0 LVM physical volume whole disks
0 LVM physical volumes
从上面的输出中,我们可以看到目前有2个磁盘和17个分区。 这些分区大部分是/ dev / ram *分区,这些分区被用作Ram Disk以提高性能。 这个例子中的磁盘是/ dev / sda,它有200G的空间,/ dev / sdb,有100G。
sudo pvcreate /dev/sda /dev/sdb Output Physical volume "/dev/sda" successfully created Physical volume "/dev/sdb" successfully created
这会将LVM header写入磁盘以表明它们已准备好添加到卷组中。凡是物理磁盘携带有LVM header的,都被认为是物理卷(physical volumes)
$ sudo pvs Output PV VG Fmt Attr PSize PFree /dev/sda lvm2 --- 200.00g 200.00g /dev/sdb lvm2 --- 100.00g 100.00g
这两种设备都存在于PV 栏目下面,代表物理卷(physical volumes)。
前面提到过lvmdiskscan扫描当前系统上可用磁盘,加上-l参数可以只显示物理卷(physical volumes)
$ sudo lvmdiskscan -l Output WARNING: only considering LVM devices /dev/sda [ 200.00 GiB] LVM physical volume /dev/sdb [ 100.00 GiB] LVM physical volume 2 LVM physical volume whole disks 0 LVM physical volumes
pvscan命令与lvmdiskscan -l非常类似,它会在LVM physical volumes中搜索所有可用设备。 而lvmdiskscan -l则是在所有可用磁盘(加lvm header和没加lvm header的磁盘)中列出LVM physical volumes。两条命令输出格式有点不同,pvscan包含少量的附加信息:
$ sudo pvscan Output PV /dev/sda VG LVMVolGroup lvm2 [200.00 GiB / 0 free] PV /dev/sdb VG LVMVolGroup lvm2 [100.00 GiB / 10.00 GiB free] Total: 2 [299.99 GiB] / in use: 2 [299.99 GiB] / in no VG: 0 [0 ]
If you need more detail, the pvs
and pvdisplay
commands are better options.
The pvs
command is highly configurable and can display information in many different formats. Because its output can be tightly controlled, it is frequently used in when scripting or automation is needed. Its basic output provides a useful at-a-glance summary similar to the earlier commands:
$ sudo pvs Output PV VG Fmt Attr PSize PFree /dev/sda LVMVolGroup lvm2 a-- 200.00g 0 /dev/sdb LVMVolGroup lvm2 a-- 100.00g 10.00g
For more verbose, human-readable output, the pvdisplay
command is usually a better option:
sudo pvdisplay Output --- Physical volume --- PV Name /dev/sda VG Name LVMVolGroup PV Size 200.00 GiB / not usable 4.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 51199 Free PE 0 Allocated PE 51199 PV UUID kRUOyU-0ib4-ujPh-kAJP-eeQv-ztRL-4EkaDQ --- Physical volume --- PV Name /dev/sdb VG Name LVMVolGroup PV Size 100.00 GiB / not usable 4.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 25599 Free PE 2560 Allocated PE 23039 PV UUID udcuRJ-jCDC-26nD-ro9u-QQNd-D6VL-GEIlD7
As you can see the pvdisplay
command is often the easiest command for getting detailed information about physical volumes.
To discover the logical extents that have been mapped to each volume, pass in the -m
option to pvdisplay
:
sudo pvdisplay -m Output --- Physical volume --- PV Name /dev/sda VG Name LVMVolGroup PV Size 200.00 GiB / not usable 4.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 51199 Free PE 38395 Allocated PE 12804 PV UUID kRUOyU-0ib4-ujPh-kAJP-eeQv-ztRL-4EkaDQ --- Physical Segments --- Physical extent 0 to 0: Logical volume /dev/LVMVolGroup/db_rmeta_0 Logical extents 0 to 0 Physical extent 1 to 5120: Logical volume /dev/LVMVolGroup/db_rimage_0 Logical extents 0 to 5119 . . .
This can be very useful when trying to determine which data is held on which physical disk for management purposes.
将物理卷添加到卷组
$ sudo vgcreate LVMVolGroup /dev/sda /dev/sdb Output Volume group "LVMVolGroup" successfully created
检查pvs输出结果,发现2个物理卷现在已属于卷组LVMVolGroup
$ sudo pvs Output PV VG Fmt Attr PSize PFree /dev/sda LVMVolGroup lvm2 a-- 200.00g 200.00g /dev/sdb LVMVolGroup lvm2 a-- 100.00g 100.00g
下面命令可以查看一个卷组的简单描述
$ sudo vgs Output VG #PV #LV #SN Attr VSize VFree LVMVolGroup 2 0 0 wz--n- 299.99g 299.99g
当前我们的卷组有2个物理卷,0个逻辑卷,卷组的大小是2个物理卷大小之和。
在卷组池(Volume Group Pool)之上创建逻辑卷
- 10G "projects" volume
- 5G "www" volume for web content
- 20G "db" volume for a database
- "workspace" volume that will fill the remaining space
$ sudo lvcreate -L 10G -n projects LVMVolGroup $ sudo lvcreate -L 5G -n www LVMVolGroup $ sudo lvcreate -L 20G -n db LVMVolGroup Output Logical volume "projects" created. Logical volume "www" created. Logical volume "db" created.
通过在vgs命令中选择自定义输出,我们可以看到逻辑卷及其与卷组的关系:
$ sudo vgs -o +lv_size,lv_name Output VG #PV #LV #SN Attr VSize VFree LSize LV LVMVolGroup 2 3 0 wz--n- 299.99g 264.99g 10.00g projects LVMVolGroup 2 3 0 wz--n- 299.99g 264.99g 5.00g www LVMVolGroup 2 3 0 wz--n- 299.99g 264.99g 20.00g db
最后两列可以看到逻辑卷的名字,大小
$ sudo lvcreate -l 100%FREE -n workspace LVMVolGroup Output Logical volume "workspace" created.
再次检查卷组信息,可以发现我们使用了全部存储空间。
$ sudo vgs -o +lv_size,lv_name Output VG #PV #LV #SN Attr VSize VFree LSize LV LVMVolGroup 2 4 0 wz--n- 299.99g 0 10.00g projects LVMVolGroup 2 4 0 wz--n- 299.99g 0 5.00g www LVMVolGroup 2 4 0 wz--n- 299.99g 0 20.00g db LVMVolGroup 2 4 0 wz--n- 299.99g 0 264.99g workspace
格式化、挂载逻辑卷
- /dev/volume_group_name/logical_volume_name
- /dev/mapper/volume_group_name-logical_volume_name
$ sudo mkfs.ext4 /dev/LVMVolGroup/projects $ sudo mkfs.ext4 /dev/LVMVolGroup/www $ sudo mkfs.ext4 /dev/LVMVolGroup/db $ sudo mkfs.ext4 /dev/LVMVolGroup/workspace
或者这样
$ sudo mkfs.ext4 /dev/mapper/LVMVolGroup-projects $ sudo mkfs.ext4 /dev/mapper/LVMVolGroup-www $ sudo mkfs.ext4 /dev/mapper/LVMVolGroup-db $ sudo mkfs.ext4 /dev/mapper/LVMVolGroup-workspace
格式化之后,创建挂载点
$ sudo mkdir -p /mnt/{projects,www,db,workspace}
把逻辑卷挂载到适当位置
$ sudo mount /dev/LVMVolGroup/projects /mnt/projects $ sudo mount /dev/LVMVolGroup/www /mnt/www $ sudo mount /dev/LVMVolGroup/db /mnt/db $ sudo mount /dev/LVMVolGroup/workspace /mnt/workspace
要使挂载持久化,请将它们添加到/ etc / fstab,就像使用普通块设备一样:
$ sudo nano /etc/fstab /etc/fstab . . . /dev/LVMVolGroup/projects /mnt/projects ext4 defaults,nofail 0 0 /dev/LVMVolGroup/www /mnt/www ext4 defaults,nofail 0 0 /dev/LVMVolGroup/db /mnt/db ext4 defaults,nofail 0 0 /dev/LVMVolGroup/workspace /mnt/workspace ext4 defaults,nofail 0 0
现在,操作系统在引导时自动挂载LVM逻辑卷。
https://www.slashroot.in/advanced-guide-lvm-logical-volume-management-linux-part-1
How To Use LVM To Manage Storage Devices on Ubuntu 16.04