美文网首页
lvm扩容root目录实战

lvm扩容root目录实战

作者: 小黑佬 | 来源:发表于2020-10-17 14:35 被阅读0次

很无奈,今天有需求要扩展pve vm的root空间,安装linux centos7系统使用了默认的lvm格式。
先简单说说lvm原理,具体流程是PV -> VG -> LV,弄懂什么是PV VG LV基本就可以了。


image.png

物理卷: 也就是图中第二层,是通过物理分区创建而来,与物理分区一一对应。创建为物理卷后,则可以将物理卷合并为一个大的卷组。通过命令pvdisplay 查看物理卷

卷组: 也就是图中第三层,卷组是不同的物理卷合并而成,可以将所有物理卷的大小整合成一个大的卷组,相当于一个大的整合的逻辑磁盘,然后在卷组中就可以根据需求划分逻辑卷。通过命令vgdisplay 查看卷组。

逻辑卷: 也就是图中最顶层,逻辑卷是从卷组中划分出来的,相当于逻辑分区,此逻辑卷就可以直接格式化挂载到目录,就可以直接使用了。通过命令lvdisplay 查看逻辑卷。

sda -> pv1
sdb -> pv2 pv3
sdc -> pv4 pv5

p1 pv2 pv3 ->lv2
p4 p5 -> lv2

添加一块物理硬盘

Disk /dev/sdb: 536.9 GB

$ fdisk -l

Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x00044ae5

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

Disk /dev/mapper/centos-root: 50.5 GB, 50457477120 bytes, 98549760 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/sdb: 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

对新盘分区

使用 fdisk 命令对新盘进行分区,这里建立了一个主分区 /dev/sdb1 ,大小 500GB,最后使用 partprobe 命令重新读取分区表,命令记录如下:

#进去分区
$ 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.

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

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

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1 80  Old Minix      
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.
#重新读取分区信息表
$ partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
#验证是否已经分区好了,留意 ID 83 和8e
$ fdisk -l 

Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x00044ae5

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

Disk /dev/mapper/centos-root: 50.5 GB, 50457477120 bytes, 98549760 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/sdb: 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: 0x79bd83e1

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048  1048575999   524286976   8e  Linux LVM

创建物理卷(PV)

$ pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <49.00 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              12543
  Free PE               1
  Allocated PE          12542
  PV UUID               YrKREU-HMSO-0DtT-HR9I-M3UB-oJ29-F2PCAV
   
$ pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.
$ pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <49.00 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              12543
  Free PE               1
  Allocated PE          12542
  PV UUID               YrKREU-HMSO-0DtT-HR9I-M3UB-oJ29-F2PCAV
   
  "/dev/sdb1" is a new physical volume of "<500.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               <500.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               QHHlhW-paNf-rPiR-PQWK-MDnv-oM52-K8I69p

将PV加入卷组(VG)

#查看卷组,可用 PE 为 1
$ vgdisplay
  --- 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               <49.00 GiB
  PE Size               4.00 MiB
  Total PE              12543
  Alloc PE / Size       12542 / 48.99 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               Z50sk6-ev4r-1eKd-Iq2B-NKTf-MgyA-6gJJmK
# 将新的 PV 加入 centos 组卷   
$ vgextend centos /dev/sdb1
  Volume group "centos" successfully extended
# 查看卷组,可用 PE 为 128000了
$ vgdisplay 
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               548.99 GiB
  PE Size               4.00 MiB
  Total PE              140542
  Alloc PE / Size       12542 / 48.99 GiB
  Free  PE / Size       128000 / 500.00 GiB
  VG UUID               Z50sk6-ev4r-1eKd-Iq2B-NKTf-MgyA-6gJJmK

创建逻辑卷(LV)

#此处省略,因为我们这次不是创建新的逻辑卷,而是扩容root目录

逻辑卷(LV)扩容 (扩容root目录)

#查看逻辑卷
$ lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                mHVwAP-mwnb-9JeJ-lUX0-FsBp-D12u-eWQXA3
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-08-14 10:40:03 +0800
  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                UPgbYl-lqzs-CK2d-9tO8-Hl2T-sdxe-aqZ2eP
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-08-14 10:40:04 +0800
  LV Status              available
  # open                 1
  LV Size                46.99 GiB
  Current LE             12030
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
 
# 扩容root目录  
$ lvextend -l +100%FREE /dev/centos/root 
  Size of logical volume centos/root changed from 46.99 GiB (12030 extents) to 546.99 GiB (140030 extents).
  Logical volume centos/root successfully resized.
# xfs_growfs 对扩容后的 LV 进行 xfs 格式大小调整,Centos 6 使用 resize2fs
$  xfs_growfs  /dev/centos/root 
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=3079680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=12318720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=6015, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 12318720 to 143390720
# 查看卷组vg信息,可用PE已经为0了,全部分配了出去
$  vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               548.99 GiB
  PE Size               4.00 MiB
  Total PE              140542
  Alloc PE / Size       140542 / 548.99 GiB
  Free  PE / Size       0 / 0   
  VG UUID               Z50sk6-ev4r-1eKd-Iq2B-NKTf-MgyA-6gJJmK
 # 查看逻辑卷信息  ,root 容量已经为500多G
$ lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                mHVwAP-mwnb-9JeJ-lUX0-FsBp-D12u-eWQXA3
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-08-14 10:40:03 +0800
  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                UPgbYl-lqzs-CK2d-9tO8-Hl2T-sdxe-aqZ2eP
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-08-14 10:40:04 +0800
  LV Status              available
  # open                 1
  LV Size                546.99 GiB
  Current LE             140030
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

查看挂载信息

$ df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  7.8G     0  7.8G   0% /dev
tmpfs                   tmpfs     7.8G     0  7.8G   0% /dev/shm
tmpfs                   tmpfs     7.8G  8.5M  7.8G   1% /run
tmpfs                   tmpfs     7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs       547G  7.8G  540G   2% /
/dev/sda1               xfs      1014M  150M  865M  15% /boot
tmpfs                   tmpfs     1.6G     0  1.6G   0% /run/user/0

参考:
Centos-7-extend-lvm-volume

相关文章

  • lvm扩容root目录实战

    很无奈,今天有需求要扩展pve vm的root空间,安装linux centos7系统使用了默认的lvm格式。先简...

  • 存储管理Ⅱ-swap、逻辑卷

    一.存储管理Ⅱ 1.逻辑卷LVM 1.1.LVM管理 在线扩容 online 创建LVM VG的扩容 LVM的扩容...

  • Linux 服务器磁盘在线扩容

    CentOS 7 非 LVM 根分区扩容步骤 1、查看现有分区大小 [root@localhost ~]# df ...

  • 2020-03-23 LVM

    1.创建lvm 2.删除lvm 3.lvm fs扩容 4.vg扩容pvcreate /dev/sddvgexten...

  • centos7 磁盘扩容

    LVM扩容(centos7) 一 、LVM扩容准备 1.1 查看fdisk 1.2 查看所有存储设备 其中 /de...

  • kvm虚拟机(centos7)磁盘扩容

    1·添加磁盘 2.创建分区 3.lvm扩容 对xfs格式的lvm扩容后需要使用xfs_growfs进行系统扩容,而...

  • Linux命令:vi/vim (2)

    vim实战 在/root 目录下 新建 vimTest目录进入vimTest目录将/root/lrzsz/appl...

  • Linux_126_LVM原理

    LVM原理 LVM动态扩容大小,其实就是通过互相交换PE的过程,达到能够弹性扩容分区大小 想要减少空间容量,就是剔...

  • 磁盘扩容实战

    不损坏数据的情况下扩容非lvm磁盘 磁盘中有重要数据时请提前备份数据 扩容LVM类型磁盘 fstab 配置详解 第...

  • lvm扩容

    1,插硬盘,装工具 2,创建pv 3,创建vg 4,创建lv 5,格式化lv 6,挂载 7,扩容

网友评论

      本文标题:lvm扩容root目录实战

      本文链接:https://www.haomeiwen.com/subject/krttmktx.html