Centos 7 磁盘管理(LVM)

导读:本篇文章讲解 Centos 7 磁盘管理(LVM),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

一、无需重启主机增加磁盘

1、为虚拟机添加多一块磁盘

Centos 7 磁盘管理(LVM)

2、检测磁盘空间

无法发现新增的2G磁盘

Centos 7 磁盘管理(LVM)

3、,重新扫描磁盘

通过重新扫描 SCSI (注:Small Computer System Interface 小型计算机系统接口)总线并添加 SCSI 设备,系统就可以扩展操作系统的物理卷磁盘空间,而且不需要重启虚拟机。

[root@localhost ~]# ls /sys/class/scsi_host/
host0  host1  host2
[root@localhost ~]# echo "- - -" > /sys/class/scsi_host/host0/scan
[root@localhost ~]# echo "- - -" > /sys/class/scsi_host/host1/scan
[root@localhost ~]# echo "- - -" > /sys/class/scsi_host/host2/scan

使用下面的命令来检查 SCSI 设备的名称,然后重新扫描 SCSI 总线。

[root@localhost ~]# ls /sys/class/scsi_device/
0:0:0:0  0:0:1:0  2:0:0:0
[root@localhost ~]# echo 1>/sys/class/scsi_device/0\:0\:0\:0/device/rescan
[root@localhost ~]# echo 1>/sys/class/scsi_device/0\:0\:1\:0/device/rescan
[root@localhost ~]# echo 1>/sys/class/scsi_device/2\:0\:0\:0/device/rescan

Centos 7 磁盘管理(LVM)

二、扩容主机现有的根分区

如果磁盘使用率超过前期预算的,又需要保留所有数据,可以选择扩容根分区

1、查看磁盘使用率和“/”目录大小

[root@localhost ~]# df -HT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs       8.6G  1.1G  7.5G  13% /
devtmpfs                devtmpfs  4.1G     0  4.1G   0% /dev
tmpfs                   tmpfs     4.2G     0  4.2G   0% /dev/shm
tmpfs                   tmpfs     4.2G  9.3M  4.1G   1% /run
tmpfs                   tmpfs     4.2G     0  4.2G   0% /sys/fs/cgroup
/dev/sda1               xfs       1.1G  153M  912M  15% /boot
tmpfs                   tmpfs     821M     0  821M   0% /run/user/0

2、为新增的磁盘分区格式化

查看新的磁盘(是否存在/dev/sdb)

[root@localhost ~]# fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x00002170

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    20971519     9436160   8e  Linux LVM

Disk /dev/mapper/centos-root: 8585 MB, 8585740288 bytes, 16769024 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/centos-swap: 1073 MB, 1073741824 bytes, 2097152 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/sdb: 2147 MB, 2147483648 bytes, 4194304 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

对磁盘进行分区

[root@localhost ~]# fdisk /dev/sdb

fdisk的交互模式,依次输入
n  --创建新分区
p  --创建主分区
<回车>  --默认分区编号
<回车>  --默认起始扇区位置。
<回车>  --默认结束扇区位置。
w  --写入分区表

Centos 7 磁盘管理(LVM)

3、再查看磁盘能看到新增的/dev/sdb1

[root@localhost ~]# fdisk -l
......
Disk /dev/sdb: 2147 MB, 2147483648 bytes, 4194304 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: 0x07276705

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4194303     2096128   83  Linux

4、将新磁盘分区扩容到根分区 “/”

4.1、查看卷分组

[root@localhost ~]# vgdisplay -v
  --- Volume group ---
  VG Name               centos
  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               <9.00 GiB
  PE Size               4.00 MiB
  Total PE              2303
  Alloc PE / Size       2303 / <9.00 GiB
  Free  PE / Size       0 / 0   
  VG UUID               DmqyXY-Kj13-sfSR-v9i5-wpLc-ht7c-kPJIvC
......

  --- Physical volumes ---
  PV Name               /dev/sda2     
  PV UUID               7CPA1z-xrMe-CAo7-g14k-cL8B-zgm2-vXbuP3
  PV Status             allocatable
  Total PE / Free PE    2303 / 0

4.2、为新增的分区创建物理卷

[root@localhost ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.



查看创建结果,有新建的/dev/sdb1,大小为2G
[root@localhost ~]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <9.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              2303
  Free PE               0
  Allocated PE          2303
  PV UUID               7CPA1z-xrMe-CAo7-g14k-cL8B-zgm2-vXbuP3
   
  "/dev/sdb1" is a new physical volume of "<2.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               <2.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               SnXl2W-LsFp-FHUT-M4Ze-yZy9-Y6WH-p3kQ0S

4.3、扩展卷分组,“centos”就是前面使用vgdisplay查看到的主机原本的卷分组名

[root@localhost ~]# vgextend centos /dev/sdb1
  Volume group "centos" successfully extended

4.4、查看逻辑卷,并扩展逻辑卷

目前/dev/centos/root的LV Size大小为8G

[root@localhost ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                4U7aBO-Nknq-ZkLc-sLXU-bGpH-Ru9B-5lzejQ
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-05-08 02:15:24 -0400
  LV Status              available
  # open                 2
  LV Size                1.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                D20gW5-4CiO-Pdfp-68yn-t4wG-h1cs-aw4ge7
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-05-08 02:15:26 -0400
  LV Status              available
  # open                 1
  LV Size                <8.00 GiB
  Current LE             2047
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

扩展操作

[root@localhost ~]# lvextend -L +1G /dev/centos/root 
  Size of logical volume centos/root changed from <8.00 GiB (2047 extents) to <9.00 GiB (2303 extents).
  Logical volume centos/root successfully resized.

注意:不能全部把2G空间都扩展,因为实际没有这么大的空间

报错如下,提只有511可用
[root@localhost ~]# lvextend -L +2G /dev/centos/root 
  Insufficient free space: 512 extents needed, but only 511 available

执行pvdisplay可以看到可用的PE数量是511,而且每个PE大小是4.00MiB,所以实际空间并没有2G
[root@localhost ~]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <9.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              2303
  Free PE               0
  Allocated PE          2303
  PV UUID               7CPA1z-xrMe-CAo7-g14k-cL8B-zgm2-vXbuP3
   
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               centos
  PV Size               <2.00 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              511
  Free PE               511
  Allocated PE          0
  PV UUID               SnXl2W-LsFp-FHUT-M4Ze-yZy9-Y6WH-p3kQ0S

4.5、因为安装CentOS用的xfs分区,所以使用命令xfs_growfs扩展文件系统大小。

[root@localhost ~]# xfs_growfs /dev/centos/root 
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=524032 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=2096128, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 2096128 to 2358272

5、再次查看根目录大小,/dev/mapper/centos-root已经扩容到9G

[root@localhost ~]# df -HT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs       9.7G  1.1G  8.6G  12% /
devtmpfs                devtmpfs  4.1G     0  4.1G   0% /dev
tmpfs                   tmpfs     4.2G     0  4.2G   0% /dev/shm
tmpfs                   tmpfs     4.2G  9.3M  4.1G   1% /run
tmpfs                   tmpfs     4.2G     0  4.2G   0% /sys/fs/cgroup
/dev/sda1               xfs       1.1G  153M  912M  15% /boot
tmpfs                   tmpfs     821M     0  821M   0% /run/user/0

三、为主机挂载新的磁盘空间

不改变原主机磁盘状态,新挂载一个磁盘做数据存储

1、一样先看是否能看到新添加的磁盘,不能就执行第一大点的操作

[root@localhost ~]# fdisk -l
......

Disk /dev/sdb: 2147 MB, 2147483648 bytes, 4194304 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

2、对新磁盘进行分区

Centos 7 磁盘管理(LVM)

再查看磁盘个数与分区情况

[root@localhost ~]# fdisk -l
......

Disk /dev/sdb: 2147 MB, 2147483648 bytes, 4194304 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: 0xaaed78bd

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4194303     2096128   83  Linux

3、格式化

[root@localhost ~]# partprobe /dev/sdb
[root@localhost ~]# mkfs.ext3 /dev/sdb1 

Centos 7 磁盘管理(LVM)

4、创建挂载目录

[root@localhost ~]# mkdir /mydata

5、挂载分区

[root@localhost ~]# mount /dev/sdb1  /mydata

6、创建开机自动挂载

Centos 7 磁盘管理(LVM)

7、查看挂载

Centos 7 磁盘管理(LVM)

8、也可以通过磁盘的UUID进行挂载

查看uuid

[root@localhost ~]# blkid 

Centos 7 磁盘管理(LVM)

8、挂载

Centos 7 磁盘管理(LVM)

9、重启,再查看挂载

Centos 7 磁盘管理(LVM)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/95215.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!