Resize Storage in Centos7

Recently one of the VirtualBox system (OS is Centos7) is running out of space, so trying to enlarge the storage.

Change disk/snapshot size in VirtualBox

  • Open Vitual Media Manager
  • Select the disk or snapshot to be resized, current is 20GB
  • Enter the new size 40GB and click Apply
  • turn on the system, and check with lsblk
# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   40G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]

Allocate the Free Space

  • Enter command cfdisk
  • Select the Free Space one
  • Select New and Press Enter
  • Select Primary and Press Enter
  • Update Size or keep default to use full free space, and Press Enter
  • Select Write and type yes to write the partition table to disk
  • Select Quit to exit from cfdisk

Extend partition

# fdisk /dev/sda
  • Enter p to print the initial partition table
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
/dev/sda3        41943040    83886079    20971520   83  Linux
  • Enter t then 3 then 8e to change the new partion type to “Linux LVM”
  • Enter p to print the new partition table
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
/dev/sda3        41943040    83886079    20971520   8e  Linux LVM
  • Enter w to write the partition table to disk

Update kernel in-memory partition table

Update the kernel in-memory partition table, and we can see sda3

# partx -u /dev/sda
# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   40G  0 disk
├─sda1            8:1    0    1G  0 part /boot
├─sda2            8:2    0   19G  0 part
│ ├─centos-root 253:0    0   17G  0 lvm  /
│ └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
└─sda3            8:3    0   20G  0 part

Create a new physical volume

# pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created.

# vgs
 VG     #PV #LV #SN Attr   VSize   VFree
  centos   1   2   0 wz--n- <19.00g    0

# vgextend centos /dev/sda3
  Volume group "centos" successfully extended

# vgs
  VG     #PV #LV #SN Attr   VSize  VFree
  centos   2   2   0 wz--n- 38.99g <20.00g

# vgdisplay

Resize LV and file sytem

# lvs
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- <17.00g
  swap centos -wi-ao----   2.00g

# lvdisplay
  ...
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  ...  
// LV path will be used in next command

# lvextend -l +100%FREE /dev/centos/root    
  Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to 36.99 GiB (9470 extents).
  Logical volume centos/root successfully resized.

# lvs
  LV   VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- 36.99g
  swap centos -wi-ao----  2.00g

 # df -h
 Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 1.9G     0  1.9G   0% /dev
tmpfs                    1.9G     0  1.9G   0% /dev/shm
tmpfs                    1.9G  8.6M  1.9G   1% /run
tmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root   17G   16G  1.9G  90% /
/dev/sda1               1014M  233M  782M  23% /boot

# lsblk -f
NAME            FSTYPE      LABEL           UUID                                   MOUNTPOINT
sda
├─sda1          xfs                         acc8dcfc-5393-4745-9450-f612aa41ae5   /boot
├─sda2          LVM2_member                 5WmxoE-N2Aa-Yu1T-ptls-3Unc-hKYH-Chi07b
│ ├─centos-root xfs                         a4eca922-5b67-4fd5-a7e5-fbaa0fea09482   /
│ └─centos-swap swap                        6c0cc394-6c89-45be-824c-dc8aab2951c9   [SWAP]
└─sda3          LVM2_member                 NRlE8g-cmrj-dViF-Cegx-GODD-xBfw-AJjwTQ
  └─centos-root xfs                         a4eca922-5b67-4fd5-a7e5-fb60feff9482   /

# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-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 9697280

# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 1.9G     0  1.9G   0% /dev
tmpfs                    1.9G     0  1.9G   0% /dev/shm
tmpfs                    1.9G  8.6M  1.9G   1% /run
tmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root   37G   16G   22G  42% /
/dev/sda1               1014M  233M  782M  23% /boot

Now the root space has been successfully extented from 17GB to 37GB.