linux LVM分区和扩容

LVM分区流程图

在使用fdisk/parted等分区工具对硬盘分区之后,是否使用LVM由用户决定 :
1)可以直接对分区后的硬盘进行格式化操作,之后挂载分区;
2)也可以先用LVM逻辑卷管理工具对分区后的物理卷进行管理,之后再对逻辑卷进行格式化操作,之后挂载分区。( LVM的分区类型为8e)


LVM结构图

1. 手动添加物理硬盘

对硬盘进行分区

  1. 使用fdisk -l 查看添加的物理硬盘
[root@collector13 ~]# fdisk -l

Disk /dev/vda: 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: 0x000445d4

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    41943039    20458496   8e  Linux LVM

Disk /dev/mapper/centos-root: 18.8 GB, 18756927488 bytes, 36634624 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: 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 /dev/vdb: 2147.5 GB, 2147483648000 bytes, 4194304000 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
  1. 使用fdisk /dev/vdb 进行分区
[root@collector13 ~]# fdisk /dev/vdb 
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 0x88cd1926.

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): p

Disk /dev/vdb: 2147.5 GB, 2147483648000 bytes, 4194304000 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: 0x88cd1926

   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): 
First sector (2048-4194303999, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4194303999, default 4194303999): 
Using default value 4194303999
Partition 1 of type Linux and of size 2 TiB is set

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
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@collector13 ~]# fdisk -l

Disk /dev/vda: 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: 0x000445d4

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    41943039    20458496   8e  Linux LVM

Disk /dev/mapper/centos-root: 18.8 GB, 18756927488 bytes, 36634624 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: 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 /dev/vdb: 2147.5 GB, 2147483648000 bytes, 4194304000 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: 0x88cd1926

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048  4194303999  2097150976   8e  Linux LVM
  1. 对分区执行LVM操作
[root@collector13 ~]# pvcreate /dev/vdb1
  Physical volume "/dev/vdb1" successfully created.
[root@collector13 ~]# vgcreate vg_data /dev/vdb1
  Volume group "vg_data" successfully created
[root@collector13 ~]# lvcreate -l +100%FREE -n lv_data vg_data
  Logical volume "lv_data" created.
  
[root@collector11 ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vg_data/lv_data
  LV Name                lv_data
  VG Name                vg_data
  LV UUID                dApUBU-pHWI-D4Ar-IvpK-GVnk-yq17-AcN6Q7
  LV Write Access        read/write
  LV Creation host, time collector11, 2018-10-16 01:53:24 -0400
  LV Status              available
  # open                 1
  LV Size                1.95 TiB
  Current LE             511999
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                32jIrc-Qdvd-7M8u-Dp3T-lEJW-HJ0j-wbBm38
  LV Write Access        read/write
  LV Creation host, time host-10-10-4-179, 2018-06-20 03:44:23 -0400
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  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                4AV4CF-25lk-RDt5-dnzi-KET7-wglP-fTaLWD
  LV Write Access        read/write
  LV Creation host, time host-10-10-4-179, 2018-06-20 03:44:24 -0400
  LV Status              available
  # open                 1
  LV Size                <17.47 GiB
  Current LE             4472
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

  1. 格式化分区
[root@collector13 ~]# mkfs.ext4 /dev/vg_data/lv_data 
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
131072000 inodes, 524286976 blocks
26214348 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2671771648
16000 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, 71663616, 78675968, 
    102400000, 214990848, 512000000

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done   

  1. 挂载分区到指定目录
[root@collector13 ~]# mkdir /data

[root@collector13 ~]# mount /dev/vg_data/lv_data /data
  1. 修改/etc/fstab 启动自动挂载硬盘
#
# /etc/fstab
# Created by anaconda on Wed Jun 20 03:44:25 2018
#
# 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/centos-swap none    swap    sw,comment=cloudconfig  0       0
/dev/vg_data/lv_data                     /data                     ext4    defaults       0 0

LVM扩容根目录

  1. 查看磁盘状态 fdisk -l
    如下linux根目录才20G,太小了,不能满足业务需求。新加了一个500G硬盘,用于扩容根目录。
[root@collector13 ~]# fdisk -l

Disk /dev/vda: 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: 0x000445d4

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    41943039    20458496   8e  Linux LVM

Disk /dev/mapper/centos-root: 18.8 GB, 18756927488 bytes, 36634624 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/vdd: 536.9 GB, 536870912000 bytes, 1048576000 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
  1. 创建新的分区
    这部分参考上面有介绍
fdisk  /dev/hda

n

l        #选择逻辑分区,如果没有,则首先创建扩展分区,然后再添加逻辑分区(硬盘:最多四个分区P-P-P-P或P-P-P-E)

6        #分区号(从5开始),/dev/hda6

t      8e   #分区类型8e表示LVM分区

w        #写入分区表

partprobe   #重读分区表

mkfs.ext4 /dev/vg_data/lv_data  #格式化 ,如果是扩展根目录,则不需要格式化

partx /dev/hda #查看当前硬盘的分区表及使用情况
  1. 创建PV,扩容VG,LV
  1. 创建PV
[root@collector13 ~]# fdisk -l
Disk /dev/vdd: 536.9 GB, 536870912000 bytes, 1048576000 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: 0xaacdf342

 Device Boot      Start         End      Blocks   Id  System
/dev/vdd1            2048  1048575999   524286976   8e  Linux LVM
[root@collector13 ~]# pvcreate /dev/vdd1
Physical volume "/dev/vdd1" successfully created.

2)扩容LG
扩容前先查看先要扩容的VG。例如使用如下的VG Name:centos
把刚刚的硬盘分区挂载到要扩容的VG。

[root@collector13 ~]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/vda2
  VG Name               centos
  PV Size               19.51 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              4994
  Free PE               10
  Allocated PE          4984
  PV UUID               St079E-R9o3-S5aA-FP0n-R0p3-QRxp-hgF02n

扩容VG

[root@collector13 ~]# vgextend centos /dev/vdd1
  Volume group "centos" successfully extended

3)扩容LV

[root@collector13 ~]# lvextend -l +100%FREE /dev/centos/root
  Size of logical volume centos/root changed from <17.47 GiB (4472 extents) to 517.50 GiB (132481 extents).
  Logical volume centos/root successfully resized.

或者可以使用如下命令
如果不希望把硬盘所有的空间全部分布可以定做分配
lvextend -L +100g /dev/centos/root

  1. 格式化 但是有报错信息
[root@collector13 ~]# resize2fs /dev/centos/root 
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/centos/root
Couldn't find valid filesystem superblock.

如果要扩容的分区是xfs文件系统类型,执行resize2fs时,可能会报错:
需要执行xfs_growfs命令执行扩展操作。

/dev/mapper/vg_data-lv_data  2.0T   81M  1.9T   1% /data
[root@collector13 ~]# xfs_growfs /dev/centos/root 
meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=1144832 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=4579328, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
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 4579328 to 135660544
[root@collector13 ~]# df -h
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/centos-root      518G  1.5G  517G   1% /
devtmpfs                      63G     0   63G   0% /dev
tmpfs                         63G     0   63G   0% /dev/shm
tmpfs                         63G  8.7M   63G   1% /run
tmpfs                         63G     0   63G   0% /sys/fs/cgroup
/dev/vda1                    497M  192M  306M  39% /boot
tmpfs                         13G     0   13G   0% /run/user/0
/dev/mapper/vg_data-lv_data  2.0T   81M  1.9T   1% /data

通过以上,根目录扩展完成。

你可能感兴趣的:(linux LVM分区和扩容)