当前位置:网站首页>逻辑卷创建与扩容

逻辑卷创建与扩容

2022-04-23 14:10:00 小灰墩墩

逻辑卷创建与扩容

[root@centos7 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk
└─sda1   8:1    0 97.7G  0 part /
sdb      8:16   0   20G  0 disk
sdc      8:32   0   30G  0 disk
sr0     11:0    1 1024M  0 rom

首先先对磁盘进行分区fdisk /dev/sdb

# fdisk -l #查看磁盘信息

[root@centos7 ~]# fdisk /dev/sdb
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.


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

Command (m for help): p

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

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41943039    20970496   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
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

重新识别磁盘

[root@centos7 ~]# partprobe /dev/sdb

划分逻辑卷

[root@centos7 ~]# pvcreate /dev/sdb1
WARNING: ext4 signature detected on /dev/sdb1 at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/sdb1.
  Physical volume "/dev/sdb1" successfully created.
[root@centos7 ~]# vgcreate vg0 /dev/sdb1
  Volume group "vg0" successfully created
  
[root@centos7 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg0
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <20.00 GiB
  PE Size               4.00 MiB
  Total PE              5119
  Alloc PE / Size       5118 / 19.99 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               JiOUQ3-FiaR-YdYc-81gP-cNHy-2ck9-f5pZtA

  
[root@centos7 ~]# lvcreate -L 19.99G -n lvm-data vg0
  Rounding up size to full physical extent 19.99 GiB
  Logical volume "lvm-data" created.
[root@centos7 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg0/lvm-data
  LV Name                lvm-data
  VG Name                vg0
  LV UUID                7j9GTA-Ri25-Gc80-t0Dj-ckn0-u2V2-BvCwcC
  LV Write Access        read/write
  LV Creation host, time centos7.swyer.club, 2021-06-28 16:15:13 +0800
  LV Status              available
  # open 0
  LV Size                19.99 GiB
  Current LE             5118
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0


对磁盘进行格式化

[root@centos7 ~]# mkfs.xfs /dev/vg0/lvm-data
meta-data=/dev/vg0/lvm-data      isize=512    agcount=4, agsize=1310208 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=5240832, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

挂载

#临时挂载,服务器重启后消失
[root@centos7 ~]# mount /dev/vg0/lvm-data /mnt/

#永久挂载
[root@centos7 ~]# vim /etc/fstab
/dev/vg0/lvm-data  /mnt xfs defaults 0 0

[root@centos7 ~]# mount -a

如果磁盘不够用了怎么办?就需要用到逻辑卷扩容

lvm扩容

[root@centos7 ~]# lsblk
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                 8:0    0  200G  0 disk
└─sda1              8:1    0 97.7G  0 part /
sdb                 8:16   0   20G  0 disk
└─sdb1              8:17   0   20G  0 part
  └─vg0-lvm--data 253:0    0   20G  0 lvm  /mnt
sdc                 8:32   0   30G  0 disk    #使用此盘进行扩容
sr0                11:0    1 1024M  0 rom

#然后通过fdisk /dev/sdc进行磁盘分区,参考上一步
[root@centos7 ~]# pvcreate /dev/sdc1
  Physical volume "/dev/sdc1" successfully created.
[root@centos7 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               vg0
  PV Size               <20.00 GiB / not usable 3.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              5119
  Free PE               1
  Allocated PE          5118
  PV UUID               JJEL8r-UYa7-K6F6-nJwB-pMbg-xElO-UXDfaX

  "/dev/sdc1" is a new physical volume of "<30.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc1
  VG Name
  PV Size               <30.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               k2Rzje-DLWa-e3Nz-dzLl-92LL-fTxt-Au0DQS

#查看vg组
[root@centos7 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg0
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <20.00 GiB
  PE Size               4.00 MiB
  Total PE              5119
  Alloc PE / Size       5118 / 19.99 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               JiOUQ3-FiaR-YdYc-81gP-cNHy-2ck9-f5pZtA

#将/dev/sdc1加入到vg0组内
[root@centos7 ~]# vgextend vg0 /dev/sdc1
  Volume group "vg0" successfully extended
[root@centos7 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg0
  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               49.99 GiB   #容量已经增加
  PE Size               4.00 MiB   #PE块的大小
  Total PE              12798    #PE总个数都有变化
  Alloc PE / Size       5118 / 19.99 GiB
  Free  PE / Size       7680 / 30.00 GiB
  VG UUID               JiOUQ3-FiaR-YdYc-81gP-cNHy-2ck9-f5pZtA

[root@centos7 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg0/lvm-data
  LV Name                lvm-data
  VG Name                vg0
  LV UUID                7j9GTA-Ri25-Gc80-t0Dj-ckn0-u2V2-BvCwcC
  LV Write Access        read/write
  LV Creation host, time centos7.swyer.club, 2021-06-28 16:15:13 +0800
  LV Status              available
  # open 1
  LV Size                19.99 GiB
  Current LE             5118
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
  
[root@centos7 ~]# lvextend -L +29.99G /dev/vg0/lvm-data
  Rounding size to boundary between physical extents: 29.99 GiB.
  Size of logical volume vg0/lvm-data changed from 19.99 GiB (5118 extents) to 49.98 GiB (12796 extents).
  Logical volume vg0/lvm-data successfully resized.

#df -TH查看文件系统还是之前那么大
[root@centos7 ~]# df -TH
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  764M     0  764M   0% /dev
tmpfs                     tmpfs     775M     0  775M   0% /dev/shm
tmpfs                     tmpfs     775M   11M  765M   2% /run
tmpfs                     tmpfs     775M     0  775M   0% /sys/fs/cgroup
/dev/sda1                 xfs       105G  2.7G  103G   3% /
tmpfs                     tmpfs     155M     0  155M   0% /run/user/0
/dev/mapper/vg0-lvm--data xfs        22G   34M   22G   1% /mnt

#CentOS7上使用的命令
[root@centos7 ~]# xfs_growfs /dev/vg0/lvm-data
meta-data=/dev/mapper/vg0-lvm--data isize=512    agcount=4, agsize=1310208 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=5240832, 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 5240832 to 13103104

[root@centos7 ~]# df -TH
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  764M     0  764M   0% /dev
tmpfs                     tmpfs     775M     0  775M   0% /dev/shm
tmpfs                     tmpfs     775M   11M  765M   2% /run
tmpfs                     tmpfs     775M     0  775M   0% /sys/fs/cgroup
/dev/sda1                 xfs       105G  2.7G  103G   3% /
tmpfs                     tmpfs     155M     0  155M   0% /run/user/0
/dev/mapper/vg0-lvm--data xfs        54G   35M   54G   1% /mnt    #大小已经改变 

#CentOS6上使用的命令
[root@centos6 ~]# resize2fs /dev/vg0/lvm-data

扩展成功!!!

版权声明
本文为[小灰墩墩]所创,转载请带上原文链接,感谢
https://blog.csdn.net/swyer_66/article/details/118447191