Centos7创建LVM及扩容

2020-07-22 01:57:40 浏览数 (1)

本文编写于 109 天前,最后修改于 109 天前,其中某些信息可能已经过时。

代码语言:javascript复制
                       **环境为VMware虚拟机上的Centos7系统**

1.列出当前系统磁盘信息,可看到/dev/sdb可用

代码语言:javascript复制
[root@xls ~]# 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: 0x00000a46

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

Disk /dev/mapper/cl-root: 18.2 GB, 18249416704 bytes, 35643392 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
代码语言:javascript复制
Disk /dev/mapper/cl-swap: 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
代码语言:javascript复制
Disk /dev/sdb: 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

2.使用fdisk进行分区

代码语言:javascript复制
[root@xls ~]# fdisk /dev/sdb

Command (m for help): p #打印分区,查看当前分区状态

Disk /dev/sdb: 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: 0x62cab278

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n #开始分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p #选择主分区
Partition number (1-4, default 1): 1 #分区号1
First sector (2048-20971519, default 2048):  #回车选择默认值
Using default value 2048
Last sector,  sectors or  size{K,M,G} (2048-20971519, default 20971519):  #回车选择默认分区大小
Using default value 20971519
Partition 1 of type Linux and of size 10 GiB is set

3.修改分区类型,8e为LVM分区类型

代码语言:javascript复制
Command (m for help): p #打印分区,查看当前分区状态
代码语言:javascript复制
Disk /dev/sdb: 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: 0x62cab278

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20971519    10484736   83  Linux

Command (m for help): Command (m for help): t #更改分区类型
Selected partition 1
Hex code (type L to list all codes): 8e #lvm分区类型
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p #打印分区,可看到类型修改为8e,linux LVM

Disk /dev/sdb: 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: 0x62cab278

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20971519    10484736   8e  Linux LVM
Command (m for help): w #保存分区

4.开始创建pv

代码语言:javascript复制
[root@xls ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.

5.查看pv信息

代码语言:javascript复制
[root@xls ~]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               cl
  PV Size               19.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4863
  Free PE               0
  Allocated PE          4863
  PV UUID               yunbOn-2uCl-9Xtr-wWV0-3jkn-fUoz-xpw6Ap

  "/dev/sdb1" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               k8y4on-IAJP-URy6-l3eu-xmUH-D3Ov-OI3yfb

6.创建VG,xls_vg为vg的名称

代码语言:javascript复制
[root@xls ~]# vgcreate xls_vg /dev/sdb1
  Volume group "xls_vg" successfully created

7.查看VG信息

代码语言:javascript复制
[root@xls ~]# vgdisplay 
  --- Volume group ---
  VG Name               xls_vg
  System ID             
  Format                lvm2
  Metadata Areas        1
  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                1
  Act PV                1
  VG Size               10.00 GiB
  PE Size               4.00 MiB
  Total PE              2559
  Alloc PE / Size       0 / 0   
  Free  PE / Size       2559 / 10.00 GiB
  VG UUID               NczNSi-vCaA-pe9L-Q857-GDig-ME0f-zzxRiB

8.创建lv,并分配空间,这里是将10个G全部分出去了,xls_lv为lv的名称

代码语言:javascript复制
[root@xls ~]# lvcreate -L 10000M -n xls_lv xls_vg
  Logical volume "xls_lv" created.

9.查看lv的信息

代码语言:javascript复制
[root@xls ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/xls_vg/xls_lv
  LV Name                xls_lv
  VG Name                xls_vg
  LV UUID                fXQxBc-UYQi-4T6y-gkmE-e9FC-ACy3-YfATDN
  LV Write Access        read/write
  LV Creation host, time xls, 2020-04-01 22:55:12 -0400
  LV Status              available
  # open                 0
  LV Size                9.77 GiB
  Current LE             2500
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

10.格式化文件系统

代码语言:javascript复制
[root@xls ~]# mkfs.ext4 /dev/xls_vg/xls_lv 

11.挂载磁盘

代码语言:javascript复制
[root@xls ~]# mount /dev/xls_vg/xls_lv /data/
[root@xls ~]# df -h
Filesystem                 Size  Used Avail Use% Mounted on
/dev/mapper/cl-root         17G 1020M   16G   6% /
devtmpfs                   478M     0  478M   0% /dev
tmpfs                      489M     0  489M   0% /dev/shm
tmpfs                      489M  6.7M  482M   2% /run
tmpfs                      489M     0  489M   0% /sys/fs/cgroup
/dev/sda1                 1014M  139M  876M  14% /boot
tmpfs                       98M     0   98M   0% /run/user/0
/dev/mapper/xls_vg-xls_lv  9.5G   37M  9.0G   1% /data

12.设置挂载重启后不失效

代码语言:javascript复制
[root@xls ~]# vi /etc/fstab 
/dev/mapper/xls_vg-xls_lv /data                 ext4    defaults        0 0

13.开始扩容lvm,在虚拟机中新增一块5G的硬盘,使用echo读取出新增的硬盘

代码语言:javascript复制
[root@xls ~]# echo "- - -" > /sys/class/scsi_host/host2/scan
[root@xls ~]# fdisk -l
Disk /dev/sdc: 5368 MB, 5368709120 bytes, 10485760 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

14.对新加的硬盘进行分区,分区操作同上

代码语言:javascript复制
[root@xls ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x8dd3be7d.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-10485759, default 2048): 
Using default value 2048
Last sector,  sectors or  size{K,M,G} (2048-10485759, default 10485759): 
Using default value 10485759
Partition 1 of type Linux and of size 5 GiB is set

Command (m for help): p

Disk /dev/sdc: 5368 MB, 5368709120 bytes, 10485760 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: 0x8dd3be7d

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048    10485759     5241856   83  Linux

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

15.将新加的硬盘/dev/sdc1创建为pv

代码语言:javascript复制
[root@xls ~]# pvcreate /dev/sdc1 
 Physical volume "/dev/sdc1" successfully created.

16.查看pv

代码语言:javascript复制
[root@xls ~]# pvscan 
  PV /dev/sdb1   VG xls_vg          lvm2 [10.00 GiB / 236.00 MiB free]
  PV /dev/sda2   VG cl              lvm2 [19.00 GiB / 0    free]
  PV /dev/sdc1                      lvm2 [5.00 GiB]
  Total: 3 [33.99 GiB] / in use: 2 [28.99 GiB] / in no VG: 1 [5.00 GiB]

17.将/dec/sdc1加入到vg:xls_vg中

代码语言:javascript复制
[root@xls ~]# vgextend xls_vg /dev/sdc1
 Volume group "xls_vg" successfully extended
[root@xls ~]# vgdisplay  #可以看到vg空间从原10g增加到了15g
  --- Volume group ---
  VG Name               xls_vg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               14.99 GiB
  PE Size               4.00 MiB
  Total PE              3838
  Alloc PE / Size       2500 / 9.77 GiB
  Free  PE / Size       1338 / 5.23 GiB
  VG UUID               NczNSi-vCaA-pe9L-Q857-GDig-ME0f-zzxRiB

18.给lv扩容,这里将vg中所有空闲的空间全部扩入lv

代码语言:javascript复制
[root@xls ~]# lvextend -l  100%FREE /dev/xls_vg/xls_lv 
  Size of logical volume xls_vg/xls_lv changed from 9.77 GiB (2500 extents) to 14.99 GiB (3838 
extents).
  Logical volume xls_vg/xls_lv successfully resized.

19.查看lv信息,空间已扩大到15g

代码语言:javascript复制
[root@xls ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/xls_vg/xls_lv
  LV Name                xls_lv
  VG Name                xls_vg
  LV UUID                fXQxBc-UYQi-4T6y-gkmE-e9FC-ACy3-YfATDN
  LV Write Access        read/write
  LV Creation host, time xls, 2020-04-01 22:55:12 -0400
  LV Status              available
  # open                 1
  LV Size                14.99 GiB
  Current LE             3838
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

20.使扩容的空间写入文件系统,如果文件系统格式是xfs则用xfs_growfs命令

代码语言:javascript复制
[root@xls ~]# resize2fs /dev/xls_vg/xls_lv 
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/xls_vg/xls_lv is mounted on /data; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 2
The filesystem on /dev/xls_vg/xls_lv is now 3930112 blocks long.

21.查看磁盘扩容后的信息

代码语言:javascript复制
[root@xls ~]# df -h
Filesystem                 Size  Used Avail Use% Mounted on
/dev/mapper/cl-root         17G 1021M   16G   6% /
devtmpfs                   478M     0  478M   0% /dev
tmpfs                      489M     0  489M   0% /dev/shm
tmpfs                      489M  6.8M  482M   2% /run
tmpfs                      489M     0  489M   0% /sys/fs/cgroup
/dev/sda1                 1014M  139M  876M  14% /boot
tmpfs                       98M     0   98M   0% /run/user/0
/dev/mapper/xls_vg-xls_lv   15G   41M   14G   1% /data

0 人点赞