How to expand root partition (centos7 as an example)

Posted by Dizzee15 on Fri, 30 Aug 2019 09:44:19 +0200

All files in linux system are stored in the root partition. If the capacity of the root partition is about to run out, we need to expand the root partition. We can use lsblk command to see that the root partition of the system is actually a logical volume, so if we want to expand the root partition, we just need to expand the logical volume. At this time, my root partition capacity is 17G, which is already 12G. What should I do to expand it?

[root@v1 ~]# df -h
//File system capacity is used. Available% is used. mount point
/dev/mapper/cl-root   17G   12G  5.8G   66% /
devtmpfs             478M     0  478M    0% /dev
tmpfs                489M     0  489M    0% /dev/shm
tmpfs                489M  6.7M  482M    2% /run
tmpfs                489M     0  489M    0% /sys/fs/cgroup
/dev/sda1           1014M  139M  876M   14% /boot
tmpfs                 98M     0   98M    0% /run/user/0
/dev/sr0             4.1G  4.1G     0  100% /mnt

[root@v1 ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   20G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   19G  0 part 
  ├─cl-root 253:0    0   17G  0 lvm  /
  └─cl-swap 253:1    0    2G  0 lvm  [SWAP]
sr0          11:0    1  4.1G  0 rom  /mnt

 

1. Add a new hard disk

[root@v1 ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   20G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   19G  0 part 
  ├─cl-root 253:0    0   17G  0 lvm  /
  └─cl-swap 253:1    0    2G  0 lvm  [SWAP]
sdb           8:16   0   10G  0 disk 
sr0          11:0    1  4.1G  0 rom 

 

2. Create partitions for newly added disks

[root@v1 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will stay in memory until you decide to write them to disk.
Think twice before using write commands.

Device does not contain a recognized partition table
Create a new DOS disk label using disk identifier 0x0d87e961.

Command (Enter m for help):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 1):
Start sector (2048-2097 1519, default 2048):
The default value 2048 will be used
Last sector, +sector or+size {K, M, G} (2048-2097 1519, default 2097 1519):
The default value 2097 1519 will be used
Partition 1 has been set to Linux type with size of 10 GiB
Command (Enter m for help):w
The partition table has been altered!

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

[root@v1 ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   20G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   19G  0 part 
  ├─cl-root 253:0    0   17G  0 lvm  /
  └─cl-swap 253:1    0    2G  0 lvm  [SWAP]
sdb           8:16   0   10G  0 disk 
└─sdb1        8:17   0   10G  0 part 
sr0          11:0    1  4.1G  0 rom  

A partition has been created

3. Create the sdb1 partition as a physical volume

[root@v1 ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.

 

4. Extend the created physical volume to the volume group where the root partition is located

[root@v1 ~]# vgdisplay -v
  --- Volume group ---
  VG Name               cl
  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               28.99 GiB
  PE Size               4.00 MiB
  Total PE              7422
  Alloc PE / Size       4863 / 19.00 GiB
  Free  PE / Size       2559 / 10.00 GiB
  VG UUID               oKc6Ae-l0QZ-xNir-7U0d-sH4K-HTtN-RDm4hl
   
  --- Logical volume ---
  LV Path                /dev/cl/swap
  LV Name                swap
  VG Name                cl
  LV UUID                6ENYna-OkIl-avmF-m977-tE4z-8iCO-VyVC44
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2019-05-29 19:59:53 +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/cl/root  This is the logical volume path mounted by the root partition
  LV Name                root
  VG Name                cl  This is the name of the volume group where the root partition is located.
  LV UUID                IS5BXN-jIrh-lrr5-mp7O-SAU5-YLSt-MMWcdC
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2019-05-29 19:59:53 +0800
  LV Status              available
  # open                 1
  LV Size                17.00 GiB
  Current LE             4351
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Physical volumes ---
  PV Name               /dev/sda2     
  PV UUID               YRWhHU-7nfd-cJkw-uhNG-iW7U-Z8ZB-3n2IHG
  PV Status             allocatable
  Total PE / Free PE    4863 / 0

  PV Name               /dev/sdb1     
  PV UUID               OIu5Ny-VPsy-w0f8-O1Y1-QtOU-Ayey-3Hb9Om
  PV Status             allocatable
  Total PE / Free PE    2559 / 2559  We found that this physical volume is available. PE For all

Extended volume group capacity

[root@v1 ~]# vgextend cl /dev/sdb1
  Volume group "cl" successfully extended

 

5. Extend the logical volume path mounted by the root partition and make the expansion effective

[root@v1 ~]# lvextend -l +100%FREE /dev/cl/root  Expand all remaining available capacity to/dev/cl/root
  Size of logical volume cl/root changed from 17.00 GiB (4351 extents) to 26.99 GiB (6910 extents).
  Logical volume cl/root successfully resized.

[root@v1 ~]# xfs_growfs /dev/cl/root
meta-data=/dev/mapper/cl-root    isize=512    agcount=4, agsize=1113856 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=4455424, 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 4455424 to 707584

 

6. View the expanded root partition capacity

[root@v1 ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   20G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   19G  0 part 
  ├─cl-root 253:0    0   27G  0 lvm  /
  └─cl-swap 253:1    0    2G  0 lvm  [SWAP]
sdb           8:16   0   10G  0 disk 
└─sdb1        8:17   0   10G  0 part 
  └─cl-root 253:0    0   27G  0 lvm  /
sr0          11:0    1  4.1G  0 rom  

[root@v1 ~]# df -h
//File system capacity is used. Available% is used. mount point
/dev/mapper/cl-root   27G   12G   16G   42% /  Root partition capacity has been extended to 27 G
devtmpfs             478M     0  478M    0% /dev
tmpfs                489M     0  489M    0% /dev/shm
tmpfs                489M  6.7M  482M    2% /run
tmpfs                489M     0  489M    0% /sys/fs/cgroup
/dev/sda1           1014M  139M  876M   14% /boot
tmpfs                 98M     0   98M    0% /run/user/0

Topics: Linux ascii