Linux LVM 系统盘的根 / 扩容

Linux 系统盘的根 / 是 LVM,使用系统盘所在的物理硬盘中未使用的空间进行扩容:

文本在图片下面

[root@centos7-2009 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 898M     0  898M   0% /dev
tmpfs                    910M     0  910M   0% /dev/shm
tmpfs                    910M  9.6M  901M   2% /run
tmpfs                    910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos-root   47G  1.3G   46G   3% /
/dev/sda1               1014M  150M  865M  15% /boot
tmpfs                    182M     0  182M   0% /run/user/0
[root@centos7-2009 ~]# fdisk -l

Disk /dev/sda: 85.9 GB, 85899345920 bytes, 167772160 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: 0x000a25d9

  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

[root@centos7-2009 ~]# fdisk /dev/sda
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 (2 primary, 0 extended, 2 free)
  e   extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (104857600-167772159, default 104857600): 
Using default value 104857600
Last sector, +sectors or +size{K,M,G} (104857600-167772159, default 167772159): 
Using default value 167772159
Partition 3 of type Linux and of size 30 GiB is set

Command (m for help): t
Partition number (1-3, default 3): 
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sda: 85.9 GB, 85899345920 bytes, 167772160 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: 0x000a25d9

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

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@centos7-2009 ~]# ll /dev/sda*
brw-rw----. 1 root disk 8, 0 Sep 20 19:24 /dev/sda
brw-rw----. 1 root disk 8, 1 Sep 20 19:24 /dev/sda1
brw-rw----. 1 root disk 8, 2 Sep 20 19:24 /dev/sda2
[root@centos7-2009 ~]# reboot 
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(192.168.61.140) at 11:25:12.

Type `help' to learn how to use Xshell prompt.
[C:\~]$ 

Connecting to 192.168.61.140:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Tue Sep 20 19:25:45 2022 from 192.168.61.1
[root@centos7-2009 ~]# ll /dev/sda*
brw-rw----. 1 root disk 8, 0 Sep 20 19:25 /dev/sda
brw-rw----. 1 root disk 8, 1 Sep 20 19:25 /dev/sda1
brw-rw----. 1 root disk 8, 2 Sep 20 19:25 /dev/sda2
brw-rw----. 1 root disk 8, 3 Sep 20 19:25 /dev/sda3
[root@centos7-2009 ~]# fdisk -l

Disk /dev/sda: 85.9 GB, 85899345920 bytes, 167772160 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: 0x000a25d9

  Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   104857599    51379200   8e  Linux LVM
/dev/sda3       104857600   167772159    31457280   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

[root@centos7-2009 ~]# df -T /dev/sda1
Filesystem     Type 1K-blocks   Used Available Use% Mounted on
/dev/sda1      xfs    1038336 153592    884744  15% /boot
[root@centos7-2009 ~]# mkfs.ext4 /dev/sda3
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
1966080 inodes, 7864320 blocks
393216 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2155872256
240 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

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

[root@centos7-2009 ~]# pvcreate /dev/sda3
WARNING: ext4 signature detected on /dev/sda3 at offset 1080. Wipe it? [y/n]: y
 Wiping ext4 signature on /dev/sda3.
 Physical volume "/dev/sda3" successfully created.
[root@centos7-2009 ~]# 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               blYys0-jLwd-uNLn-0y5Q-9Ymi-ecCg-zCjccL
  
 "/dev/sda3" is a new physical volume of "30.00 GiB"
 --- NEW Physical volume ---
 PV Name               /dev/sda3
 VG Name               
 PV Size               30.00 GiB
 Allocatable           NO
 PE Size               0   
 Total PE              0
 Free PE               0
 Allocated PE          0
 PV UUID               HfSyAk-E37q-o23Z-J7fC-wZMY-q38L-av48iv
  
[root@centos7-2009 ~]# vgextend centos /dev/sda3
 Volume group "centos" successfully extended
[root@centos7-2009 ~]# 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               blYys0-jLwd-uNLn-0y5Q-9Ymi-ecCg-zCjccL
  
 --- Physical volume ---
 PV Name               /dev/sda3
 VG Name               centos
 PV Size               30.00 GiB / not usable 4.00 MiB
 Allocatable           yes 
 PE Size               4.00 MiB
 Total PE              7679
 Free PE               7679
 Allocated PE          0
 PV UUID               HfSyAk-E37q-o23Z-J7fC-wZMY-q38L-av48iv
  
[root@centos7-2009 ~]# vgs
 VG     #PV #LV #SN Attr   VSize  VFree 
 centos   2   2   0 wz--n- 78.99g 30.00g
[root@centos7-2009 ~]# lvextend -l +100%FREE /dev/mapper/centos-root
 Size of logical volume centos/root changed from 46.99 GiB (12030 extents) to 76.99 GiB (19710 extents).
 Logical volume centos/root successfully resized.
[root@centos7-2009 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 898M     0  898M   0% /dev
tmpfs                    910M     0  910M   0% /dev/shm
tmpfs                    910M  9.6M  901M   2% /run
tmpfs                    910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos-root   47G  1.3G   46G   3% /
/dev/sda1               1014M  150M  865M  15% /boot
tmpfs                    182M     0  182M   0% /run/user/0
[root@centos7-2009 ~]# xfs_growfs /
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 20183040
[root@centos7-2009 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 898M     0  898M   0% /dev
tmpfs                    910M     0  910M   0% /dev/shm
tmpfs                    910M  9.6M  901M   2% /run
tmpfs                    910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos-root   77G  1.3G   76G   2% /
/dev/sda1               1014M  150M  865M  15% /boot
tmpfs                    182M     0  182M   0% /run/user/0
[root@centos7-2009 ~]# 

Linux 系统盘的根 / 是 LVM,新增物理硬盘进行扩容:

文本在图片下面

[root@centos7-2009 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 898M     0  898M   0% /dev
tmpfs                    910M     0  910M   0% /dev/shm
tmpfs                    910M  9.6M  901M   2% /run
tmpfs                    910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos-root   47G  1.3G   46G   3% /
/dev/sda1               1014M  150M  865M  15% /boot
tmpfs                    182M     0  182M   0% /run/user/0
[root@centos7-2009 ~]# 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: 0x000a25d9

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

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 /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

[root@centos7-2009 ~]# pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created.
[root@centos7-2009 ~]# 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               blYys0-jLwd-uNLn-0y5Q-9Ymi-ecCg-zCjccL
   
  "/dev/sdb" is a new physical volume of "20.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb
  VG Name               
  PV Size               20.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               Nq2B5X-bEg0-UjV5-CIsU-26hK-w83d-KqKsx6
   
[root@centos7-2009 ~]# vgextend centos /dev/sdb
  Volume group "centos" successfully extended
[root@centos7-2009 ~]# 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               blYys0-jLwd-uNLn-0y5Q-9Ymi-ecCg-zCjccL
   
  --- Physical volume ---
  PV Name               /dev/sdb
  VG Name               centos
  PV Size               20.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              5119
  Free PE               5119
  Allocated PE          0
  PV UUID               Nq2B5X-bEg0-UjV5-CIsU-26hK-w83d-KqKsx6
   
[root@centos7-2009 ~]# vgs
  VG     #PV #LV #SN Attr   VSize  VFree 
  centos   2   2   0 wz--n- 68.99g 20.00g
[root@centos7-2009 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 898M     0  898M   0% /dev
tmpfs                    910M     0  910M   0% /dev/shm
tmpfs                    910M  9.6M  901M   2% /run
tmpfs                    910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos-root   47G  1.3G   46G   3% /
/dev/sda1               1014M  150M  865M  15% /boot
tmpfs                    182M     0  182M   0% /run/user/0
[root@centos7-2009 ~]# lvextend -l +100%FREE /dev/mapper/centos-root 
  Size of logical volume centos/root changed from 46.99 GiB (12030 extents) to 66.99 GiB (17150 extents).
  Logical volume centos/root successfully resized.
[root@centos7-2009 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 898M     0  898M   0% /dev
tmpfs                    910M     0  910M   0% /dev/shm
tmpfs                    910M  9.6M  901M   2% /run
tmpfs                    910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos-root   47G  1.3G   46G   3% /
/dev/sda1               1014M  150M  865M  15% /boot
tmpfs                    182M     0  182M   0% /run/user/0
[root@centos7-2009 ~]# xfs_growfs /
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 17561600
[root@centos7-2009 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 898M     0  898M   0% /dev
tmpfs                    910M     0  910M   0% /dev/shm
tmpfs                    910M  9.6M  901M   2% /run
tmpfs                    910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos-root   67G  1.3G   66G   2% /
/dev/sda1               1014M  150M  865M  15% /boot
tmpfs                    182M     0  182M   0% /run/user/0
[root@centos7-2009 ~]# 

说明:如果执行 xfs_growfs / 提示 xfs_growfs: / is not a mounted XFS filesystem,则需要使用 resize2fs /dev/mapper/centos-root**