Linux LVM使用小记

编程入门 行业动态 更新时间:2024-10-08 18:39:05

Linux LVM使用<a href=https://www.elefans.com/category/jswz/34/1764832.html style=小记"/>

Linux LVM使用小记

对于Linux LVM一直不太理解,直到最近使用了简单功能后才稍微明白点。

对于硬盘空间物理上的使用,我们都是先对硬盘进行分区,然后格式化成文件系统支持的类型,最后给操作系统使用。但是这种使用方式很不灵活,分完区之后如果想动态扩大或者缩小容量需要格式化硬盘后重新分区调整。LVM正好可以解决这个问题,提供一种灵活可变的硬盘空间使用方式。

LVM的基本结构如下图:

其中Disk Partition是物理硬盘分区,我们需要在每个物理分区上建立对应的PV(物理卷),Disk Partition和PV应该是一一对应的。VG是一个逻辑层,将所有PV的资源进行整合,形成一个存储池。然后可以将VG再细分为LV(逻辑卷),LV是最后提供给操作系统使用的卷。

 本文将对一个已建立的LVM进行硬盘的扩容,步骤如下:

1、查看分区情况

# fdisk -lDisk /dev/sda: 966.4 GB, 966367641600 bytes
255 heads, 63 sectors/track, 117487 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0003f3bdDevice Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64       10444    83373056   8e  Linux LVM
/dev/sda3           10444       78325   545259482+  83  Linux
/dev/sda4           78326      117487   314568765   83  LinuxDisk /dev/mapper/vg_template-lv_root: 875.1 GB, 875099586560 bytes
255 heads, 63 sectors/track, 106391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000Disk /dev/mapper/vg_template-lv_swap: 8405 MB, 8405385216 bytes
255 heads, 63 sectors/track, 1021 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000Disk /dev/mapper/vg_template-lv_home: 23.3 GB, 23278387200 bytes
255 heads, 63 sectors/track, 2830 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
View Code

可以看到硬盘目前966G左右,已经使用900G,我将为LVM再分配30G进去。

2、可以通过命令pvscan和pvdisplay查看目前已有的PV

# pvscanPV /dev/sda2   VG vg_template   lvm2 [79.51 GiB / 0    free]PV /dev/sda3   VG vg_template   lvm2 [520.00 GiB / 0    free]PV /dev/sda4   VG vg_template   lvm2 [299.99 GiB / 54.99 GiB freeTotal: 3 [899.50 GiB] / in use: 3 [899.50 GiB] / in no VG: 0 [0
View Code
# pvdisplay--- Physical volume ---PV Name               /dev/sda2VG Name               vg_templatePV Size               79.51 GiB / not usable 3.00 MiBAllocatable           yes (but full)PE Size               4.00 MiBTotal PE              20354Free PE               0Allocated PE          20354PV UUID               cf72cq-WGB1-AIdu-dUuN-NhGA-qXtL-2lIPSC--- Physical volume ---PV Name               /dev/sda3VG Name               vg_templatePV Size               520.00 GiB / not usable 3.96 MiBAllocatable           yes (but full)PE Size               4.00 MiBTotal PE              133119Free PE               0Allocated PE          133119PV UUID               os0owi-lP6g-3QXI-p3SF-OVYN-Dmwq-t4bcgq--- Physical volume ---PV Name               /dev/sda4VG Name               vg_templatePV Size               300.00 GiB / not usable 4.06 MiBAllocatable           yesPE Size               4.00 MiBTotal PE              76798Free PE               14077Allocated PE          62721PV UUID               fSH2ET-c01w-pRdK-FlE1-Rf4l-kem4-Sk5keD
View Code

3、也可以通过vgdisplay查看VG中的剩余空间

# vgdisplay--- Volume group ---VG Name               vg_templateSystem IDFormat                lvm2Metadata Areas        3Metadata Sequence No  12VG Access             read/writeVG Status             resizableMAX LV                0Cur LV                3Open LV               3Max PV                0Cur PV                3Act PV                3VG Size               899.50 GiBPE Size               4.00 MiBTotal PE              230271Alloc PE / Size       216194 / 844.51 GiBFree  PE / Size       14077 / 54.99 GiBVG UUID               YjcKe2-688m-1Iar-Zpa6-vbE7-TuXf-A1sgvh
View Code

可以看到,VG中海油54G左右的空间

4、查看目前已有的LV

# lvdisplay--- Logical volume ---LV Path                /dev/vg_template/lv_rootLV Name                lv_rootVG Name                vg_templateLV UUID                QzjkFn-VtVG-ruvw-Mpr4-UpjH-Mjpf-e6TSdKLV Write Access        read/writeLV Creation host, time Template, 2014-09-05 20:52:37 +0800LV Status              available# open                 1LV Size                815.00 GiBCurrent LE             208640Segments               3Allocation             inheritRead ahead sectors     auto- currently set to     1024Block device           253:0--- Logical volume ---LV Path                /dev/vg_template/lv_homeLV Name                lv_homeVG Name                vg_templateLV UUID                PSnzWl-xeGd-aKP9-EmNL-8vxK-sMIn-z6KMDELV Write Access        read/writeLV Creation host, time Template, 2014-09-05 20:52:40 +0800LV Status              available# open                 1LV Size                21.68 GiBCurrent LE             5550Segments               1Allocation             inheritRead ahead sectors     auto- currently set to     1024Block device           253:2--- Logical volume ---LV Path                /dev/vg_template/lv_swapLV Name                lv_swapVG Name                vg_templateLV UUID                z4oFd5-IvA9-Y0EY-KcC1-oaOr-QZEF-7IAZSlLV Write Access        read/writeLV Creation host, time Template, 2014-09-05 20:52:42 +0800LV Status              available# open                 1LV Size                7.83 GiBCurrent LE             2004Segments               1Allocation             inheritRead ahead sectors     auto- currently set to     1024Block device           253:1
View Code

此次将给lv_root增加容量

5、增加LV容量

# lvextend -L +30G /dev/vg_template/lv_rootExtending logical volume lv_root to 845.00 GiBLogical volume lv_root successfully resized
View Code

6、查看增加后的LV容量

# lvdisplay /dev/vg_template/lv_root      --- Logical volume ---LV Path                /dev/vg_template/lv_rootLV Name                lv_rootVG Name                vg_templateLV UUID                QzjkFn-VtVG-ruvw-Mpr4-UpjH-Mjpf-e6TSdKLV Write Access        read/writeLV Creation host, time Template, 2014-09-05 20:52:37 +0800LV Status              available# open                 1LV Size                845.00 GiBCurrent LE             216320Segments               3Allocation             inheritRead ahead sectors     auto- currently set to     1024Block device           253:0
View Code

可以看到容量从815增加至845G

7、通过df查看磁盘空间

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_template-lv_root803G  732G   30G  97% /
tmpfs                  32G   25G  6.7G  79% /dev/shm
/dev/sda1             485M   38M  423M   9% /boot
/dev/mapper/vg_template-lv_home22G  559M   20G   3% /home
View Code

发现操作系统层面看,使用空间没有变。

8、通过resize2fs重新扫描使用空间

# resize2fs /dev/vg_template/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_template/lv_root is mounted on /; on-line resizing required
old desc_blocks = 51, new_desc_blocks = 53
Performing an on-line resize of /dev/vg_template/lv_root to 221511680 (4k) blocks.
The filesystem on /dev/vg_template/lv_root is now 221511680 blocks long.
View Code

9、再通过df查看使用空间,空间已经扩大

]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_template-lv_root832G  732G   58G  93% /
tmpfs                  32G   25G  6.7G  79% /dev/shm
/dev/sda1             485M   38M  423M   9% /boot
/dev/mapper/vg_template-lv_home22G  559M   20G   3% /home
View Code

以后继续补充如何将新加硬盘如何操作。

==================================================================================================

以上是针对VG有剩余空间直接扩充LV的过程,下面介绍一下对于新加物理硬盘如何加到VG中。

1、查看新建物理磁盘情况

fdisk -l

Disk /dev/sdb: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

可以看到有一个200G左右的磁盘—/dev/sdb

2、对/dev/sdb进行分区

fdisk /dev/sdb

]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x94b29f79.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help):

输入n,新建分区,根据实际情况选择主分区或者扩展分区,这里我选择主分区

Command (m for help): n
Command actione   extendedp   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-26108, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-26108, default 26108):
Using default value 26108Command (m for help):
View Code

输入t,确定分区格式,8e代表Linux LVM 

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)
View Code

输入p,查看分区状况,输入w,保存分区配置

Command (m for help): pDisk /dev/sdb: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x94b29f79Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1       26108   209712478+  8e  Linux LVMCommand (m for help): w
The partition table has been altered!Calling ioctl() to re-read partition table.
Syncing disks.
View Code

3、新建PV

# pvcreate /dev/sdb1Physical volume "/dev/sdb1" successfully created
View Code

4、将新建PV加到VG中

# vgdisplay--- Volume group ---VG Name               vg_templateSystem IDFormat                lvm2Metadata Areas        3Metadata Sequence No  13VG Access             read/writeVG Status             resizableMAX LV                0Cur LV                3Open LV               3Max PV                0Cur PV                3Act PV                3VG Size               899.50 GiBPE Size               4.00 MiBTotal PE              230271Alloc PE / Size       223874 / 874.51 GiBFree  PE / Size       6397 / 24.99 GiBVG UUID               YjcKe2-688m-1Iar-Zpa6-vbE7-TuXf-A1sgvh[root@SICS-MIGPC-DB ~]# vgextend vg_template /dev/sdb1Volume group "vg_template" successfully extended
[root@SICS-MIGPC-DB ~]# vgdisplay--- Volume group ---VG Name               vg_templateSystem IDFormat                lvm2Metadata Areas        4Metadata Sequence No  14VG Access             read/writeVG Status             resizableMAX LV                0Cur LV                3Open LV               3Max PV                0Cur PV                4Act PV                4VG Size               1.07 TiBPE Size               4.00 MiBTotal PE              281470Alloc PE / Size       223874 / 874.51 GiBFree  PE / Size       57596 / 224.98 GiBVG UUID               YjcKe2-688m-1Iar-Zpa6-vbE7-TuXf-A1sgvh
View Code

然后根据之前步骤扩展现有LV空间即可。

 =======================================================================

其他常用命令:

1、从VG新建LV,并格式化LV为特定文件系统

新建一个100G的新LV,名称为VPS

#lvcreate –L 100G –n vps /dev/vg_template;
View Code

格式化为EXT3文件系统格式

#mkfs –t ext3 /dev/vg_template/vps
View Code

2、从VG删除PV,删除硬盘上的LVM分区,去掉硬盘

#vgreduce vg_template /dev/sdb1;
#pvremove /dev/sdb1
#fdisk /dev/sdb1 
输入P,输入D(删除),输入P查看,输入W,保存配置。
View Code

 

转载于:.html

更多推荐

Linux LVM使用小记

本文发布于:2024-02-14 02:24:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1761168.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小记   Linux   LVM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!