I had a lack of free disk space on root LVM volume on one of my test servers.
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 1.7G 1.2G 516M 71% /
And as long as I use LVM on the server I decided just to extend existing volume instead of creating new one and mounting it to the Root. What did I have:
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a– 2.00g 0
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao—- 1.70g
swap centos -wi-ao—- 308.00m
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz–n- 2.00g 0
On the virtual machines host system I added 2 GB to the existing disk. Now I had simply use them.
First of all I used fdisk on free space to create new partition sda3(I already had sda1 and sda2), and gave it 8e system ID – this is Linux LVM:
[root@localhost ~]# 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): p
Disk /dev/sda: 5368 MB, 5368709120 bytes, 10485760 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: 0x000b7436
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 6291455 2096128 8e Linux LVM
/dev/sda3 6291456 10485759 2097152 8e Linux LVM
Command (m for help):
ОК, there was need only to give it to LVM and extend existing disk. But LVM didn’t like my plans:
[root@localhost ~]# pvcreate /dev/sda3
Device /dev/sda3 not found (or ignored by filtering).
[root@localhost ~]# vgextend centos /dev/sda3
Device /dev/sda3 not found (or ignored by filtering).
And only after several checks I remembered that the cause wasn’t in LMV, but the problem was that I’ve forgotten to tell OS about new partition. Here is what OS knew:
[root@localhost ~]# cat /proc/partitions
major minor #blocks name
8 0 5242880 sda
8 1 1048576 sda1
8 2 2096128 sda2
11 0 1048575 sr0
253 0 1777664 dm-0
253 1 315392 dm-1
As we see from the output OS didn’t know that there also was sda3 partition. Not a problem at all, we’ll just inform OS about new partition:
[root@localhost ~]# partprobe /dev/sda
[root@localhost ~]#
Checking:
[root@localhost ~]# cat /proc/partitions
major minor #blocks name
8 0 5242880 sda
8 1 1048576 sda1
8 2 2096128 sda2
8 3 2097152 sda3
11 0 1048575 sr0
253 0 1777664 dm-0
253 1 315392 dm-1
Perfect. Now OS can work with new partition. And now we can extend LV:
[root@localhost ~]# pvcreate /dev/sda3
Physical volume “/dev/sda3” successfully created.
[root@localhost ~]#
[root@localhost ~]# vgextend centos /dev/sda3
Volume group “centos” successfully extended
[root@localhost ~]#
Nice, disk is created, volume group is extended:
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 2 2 0 wz–n- 3.99g 2.00g
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao—- 1.70g
swap centos -wi-ao—- 308.00m
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a– 2.00g 0
/dev/sda3 centos lvm2 a– 2.00g 2.00g
[root@localhost ~]#
Now logical volume would be extended:
[root@localhost ~]# lvextend -L +2G /dev/mapper/centos-root
Insufficient free space: 512 extents needed, but only 511 available
But here I’ve got an issue again. Due to specific disk size counting not all 2 original gigabytes where available for me. I didn’t want to bother with counting and just decided to extend volume to all available free space:
[root@localhost ~]# lvextend -l +100%FREE /dev/mapper/centos-root
Size of logical volume centos/root changed from 1.70 GiB (434 extents) to 3.69 GiB (945 extents).
Logical volume centos/root successfully resized.
Wonderful, there was I last step – change the file system to fit new size. Unfortunately I’ve made a mistake here too:
[root@localhost ~]# resize2fs /dev/mapper/centos-root
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/mapper/centos-root
Couldn’t find valid filesystem superblock.
[root@localhost ~]#
Speeking more precisely I forgot, that by default CentOS 7 uses XFS file system rather than EXT4. No problem – we’ll just use xfs_growfs utility instead of resize2fs.
[root@localhost ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=111104 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=444416, 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 444416 to 967680
[root@localhost ~]#
Thatt’s it. Checking current partition size:
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 3.7G 1.2G 2.5G 33% /