扩容linux的根目录(实操记录!!)

前言

仅尽量,方边后面参考。
参考链接:https://blog.csdn.net/weixin_43139644/article/details/89948983

准备动作

  • 1.数据无价,以下所有操作请务必在虚拟机上先操作1次
  • 2.在服务器中加入硬盘;sdb就是本次加入的硬盘
[ ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  1.8T  0 disk 
├─sda1            8:1    0    2G  0 part /boot
└─sda2            8:2    0  1.8T  0 part 
  └─centos-root 253:0    0  1.8T  0 lvm  /
sdb               8:16   0  1.8T  0 disk

操作步骤:

  • 1.使用 fdisk -l 查看硬盘序号,并用 fdisk 对硬盘操作,格式化成lvm的格式
[ ~]# 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 0xf2edcde9.

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-3907029167, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-3907029167, default 3907029167): 
Using default value 3907029167
Partition 1 of type Linux and of size 1.8 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.
[ ~]#
  • 2.重新识别硬盘
[ ~]# partprobe
  • 3.将刚刚的硬盘重新格式化,格式化为xfs格式
[ ~]# mkfs.xfs /dev/sdb1 
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=122094598 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=488378390, imaxpct=5
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=238466, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[ ~]#
  • 4.使用lvm命令,对根目录进行扩容
# 进入lvm模式
[ ~]# lvm
# 查看当前的pv卷
lvm> pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <1.82 TiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              476419
  Free PE               0
  Allocated PE          476419
  PV UUID               ZSlskf-Hrdk-4by2-jIUq-bbJD-kmfb-EptRPC

# 新建pv卷
lvm> pvcreate /dev/sdb1
WARNING: xfs signature detected on /dev/sdb1 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/sdb1.
  Physical volume "/dev/sdb1" successfully created.

# 再次查看pv卷,已经有两个pv卷了。
lvm> pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <1.82 TiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              476419
  Free PE               0
  Allocated PE          476419
  PV UUID               ZSlskf-Hrdk-4by2-jIUq-bbJD-kmfb-EptRPC

  "/dev/sdb1" is a new physical volume of "<1.82 TiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               <1.82 TiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               p844T5-gdrK-g63T-8zOT-IcYk-1Ren-32D2fZ

lvm> 

# 查看vg组
lvm> vgdisplay 
  --- Volume group ---
  VG Name               centos
  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               <1.82 TiB
  PE Size               4.00 MiB
  Total PE              476419
  Alloc PE / Size       476419 / <1.82 TiB
  Free  PE / Size       0 / 0   
  VG UUID               d7YIEM-gVrC-P6rl-DsH8-IVai-MeTO-9GGP1M

# 将刚刚生成的pv卷 /dev/sdb1 加入到根目录所在的vg组:centos
lvm> vgextend centos /dev/sdb1
  Volume group "centos" successfully extended

# 再次查看,根目录所在的vg组:centos 已经扩容成功
lvm> vgdisplay 
  --- Volume group ---
  VG Name               centos
  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               <3.64 TiB
  PE Size               4.00 MiB
  Total PE              953350
  Alloc PE / Size       476419 / <1.82 TiB
  Free  PE / Size       476931 / <1.82 TiB
  VG UUID               d7YIEM-gVrC-P6rl-DsH8-IVai-MeTO-9GGP1M
# 看到vg size 增加到了3.64T,Free PE size 也有了1.8T(476931)的大小。(我们要增加的就是这个PE的大小!!)

lvm> 

# 先查看lv卷
lvm> lvdisplay 
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                v9hEnv-KnJa-bM3A-gGgQ-pcXJ-G77d-4RAaZM
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-07-17 14:47:37 +0800
  LV Status              available
  # open                 1
  LV Size                <1.82 TiB
  Current LE             476419
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

# 使用 lvextend 为根目录进行扩容
lvm> lvextend -l +476931 /dev/centos/root
  Size of logical volume centos/root changed from <1.82 TiB (476419 extents) to <3.64 TiB (953350 extents).
  Logical volume centos/root successfully resized.

# 再次查看lv卷,LV Size 已经有3.64T啦
lvm> lvdisplay 
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                v9hEnv-KnJa-bM3A-gGgQ-pcXJ-G77d-4RAaZM
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-07-17 14:47:37 +0800
  LV Status              available
  # open                 1
  LV Size                <3.64 TiB
  Current LE             953350
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

lvm> exit
  Exiting.
[ ~]# 
[ ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                  95G     0   95G   0% /dev
tmpfs                     95G     0   95G   0% /dev/shm
tmpfs                     95G  670M   94G   1% /run
tmpfs                     95G     0   95G   0% /sys/fs/cgroup
/dev/mapper/centos-root  1.9T  1.6T  252G  87% /
/dev/sda1                2.0G  173M  1.9G   9% /boot
tmpfs                     19G   36K   19G   1% /run/user/0

# 硬盘大小还是之前大小,再进行多1步:扩容文件系统
[ ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=121963264 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=487853056, imaxpct=5
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=238209, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 487853056 to 976230400

# 扩容成功
[ ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                  95G     0   95G   0% /dev
tmpfs                     95G     0   95G   0% /dev/shm
tmpfs                     95G  670M   94G   1% /run
tmpfs                     95G     0   95G   0% /sys/fs/cgroup
/dev/mapper/centos-root  3.7T  1.6T  2.1T  44% /
/dev/sda1                2.0G  173M  1.9G   9% /boot
tmpfs                     19G   36K   19G   1% /run/user/0
[ ~]#

相关推荐