Linux

#135 CentOS: Expand root LVM partition


Let’s say you have a 10GB single disk in a VM running CentOS and you want to expand the root partition. Increase the disk to 20GB from VMWare console. This is how it will look like. You’ve added 10 extra gigs, but there are no changes.

df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  8.0G  1.2G  6.9G  15% /
devtmpfs                 908M     0  908M   0% /dev
tmpfs                    920M     0  920M   0% /dev/shm
tmpfs                    920M  8.8M  911M   1% /run
tmpfs                    920M     0  920M   0% /sys/fs/cgroup
/dev/sda1               1014M  180M  835M  18% /boot
tmpfs                    184M     0  184M   0% /run/user/1000

Or if you type pvscan.

pvscan
  PV /dev/sda2   VG centos          lvm2 [<9.00 GiB / 0    free]
  Total: 1 [<9.00 GiB] / in use: 1 [<9.00 GiB] / in no VG: 0 [0   ]

Or if you type fdisk -l.

fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x000b9ce3

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

Disk /dev/mapper/centos-root: 8585 MB, 8585740288 bytes, 16769024 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: 1073 MB, 1073741824 bytes, 2097152 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

We have to delete /dev/sda2 first. Type fdisk /dev/sda first. At the prompt type p to see the parititions.

Command (m for help): p

Disk /dev/sda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x000b9ce3

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

Delete the 2nd partition.

Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 is deleted

Now, recreate the 2nd partition again. You’ll see that the end sector is different. Follow the highlighted lines, press enter when asked for the sectors.

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2099200-41943039, default 2099200):
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-41943039, default 41943039):
Using default value 41943039
Partition 2 of type Linux and of size 19 GiB is set

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.

Because we modified the boot disk, partprobe won’t work, so you have to reboot.
Once the system comes back, we have to expand the physical volume.

pvresize /dev/sda2
  Physical volume "/dev/sda2" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized

Add the spare 10GB to the logical volume. In my case it’s centos. In your case it might be different. Do vgdisplay | grep Name and see what does it say.

lvextend -L +10G /dev/mapper/centos-root
  Size of logical volume centos/root changed from <8.00 GiB (2047 extents) to <18.00 GiB (4607 extents).
  Logical volume centos/root successfully resized.

Finally, extend the file system.

xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=524032 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=2096128, 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 2096128 to 4717568

If you do df -h now, you’ll see that you are all set.

df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   18G  1.2G   17G   7% /
devtmpfs                 908M     0  908M   0% /dev
tmpfs                    920M     0  920M   0% /dev/shm
tmpfs                    920M  8.8M  911M   1% /run
tmpfs                    920M     0  920M   0% /sys/fs/cgroup
/dev/sda1               1014M  180M  835M  18% /boot
tmpfs                    184M     0  184M   0% /run/user/1000
Linux
#44 CentOS 6.3: postfix, dovecot, MUA – virtual domains, virtual users
DevOps
#126 Chef: Deploy WordPress on CentOS 7
AWS
#94 Amazon AWS: Elastic File System (EFS) on FreeBSD/RHEL/CentOS/AWS Linux
There are currently no comments.

This site uses Akismet to reduce spam. Learn how your comment data is processed.