34

I have centos 7 server (CentOS Linux release 7.3.1611 (Core)) When I was updated my server I saw error you need extra space. But I had 20GB disk on server when I check disk spaces I saw only 4.5GB partition created and 16GB partition is free space no unallocated. How I can extend partition from 16GB free space?

lsblk:

NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0               2:0    1    4K  0 disk
sda               8:0    0   20G  0 disk
├─sda1            8:1    0  500M  0 part /boot
└─sda2            8:2    0  4.5G  0 part
  ├─centos-root 253:0    0    4G  0 lvm  /
  └─centos-swap 253:1    0  512M  0 lvm  [SWAP]
sr0              11:0    1 1024M  0 rom

enter image description here

1
30

There are three steps to make:

  1. alter your partition table so sda2 ends at end of disk
  2. reread the partition table (will require a reboot)
  3. resize your LVM pv using pvresize

Step 1 - Partition table

Run fdisk /dev/sda. Issue p to print your current partition table and copy that output to some safe place. Now issue d followed by 2 to remove the second partition. Issue n to create a new second partition. Make sure the start equals the start of the partition table you printed earlier. Make sure the end is at the end of the disk (usually the default).

Issue t followed by 2 followed by 8e to toggle the partition type of your new second partition to 8e (Linux LVM).

Issue p to review your new partition layout and make sure the start of the new second partition is exactly where the old second partition was.

If everything looks right, issue w to write the partition table to disk. You will get an error message from partprobe that the partition table couldn't be reread (because the disk is in use).

Reboot your system

This step is neccessary so the partition table gets re-read.

Resize the LVM PV

After your system rebooted invoke pvresize /dev/sda2. Your Physical LVM volume will now span the rest of the drive and you can create or extend logical volumes into that space.

9
  • a great solution, it works perfectly. still only the part how to extend logical volumes.
    – Mimouni
    Mar 12 '18 at 15:00
  • 7
    This excelent answer stopped just short of the finish line! I figured out how to finish it off. The example shared in the question showed two logical volumes underneith sda2: centos-root and centos-swap. In this case, the volume group is centos and the two logical volumes inside that group are root and swap. If we wanted to extend the centos-root volume in this example with 50% of the available space in our freshly-extended sda2 partition, we would use lvextend -r -l +50%FREE /dev/centos/root. The -r resizes the underlying file system to match the extended logical volume.
    – Shadoninja
    Jul 1 '19 at 18:25
  • 1
    @AndreasRogge I would argue that is entirely within the scope of this question as the terminology in this space can be confusing and easy to conflate. It is worth posting a complete summary of how to get unallocated disk space usable by the file system since that will be an extremely common reason for people to find this question.
    – Shadoninja
    Jul 9 '19 at 17:36
  • 1
    This solution didn't work for me: ` pvresize /dev/sda2 Failed to find physical volume "/dev/sda2". 0 physical volume(s) resized or updated / 0 physical volume(s) not resized`
    – Alex G
    May 27 '20 at 14:59
  • 1
    I used this command on my centos box with 2 partitions: sudo lvextend -v -r -l +100%FREE /dev/centos/root. -v will give a great verbose output.
    – user674669
    Jun 18 '20 at 18:31
38

You can do this without rebooting in CentOS 7. Assuming your disk is /dev/vda and standard RHEL/CentOS partitioning:

Extend partition

# fdisk /dev/vda

Enter p to print your initial partition table.

Enter d (delete) followed by 2 to delete the existing partition definition (partition 1 is usually /boot and partition 2 is usually the root partition).

Enter n (new) followed by p (primary) followed by 2 to re-create partition number 2 and enter to accept the start block and enter again to accept the end block which is defaulted to the end of the disk.

Enter t (type) then 2 then 8e to change the new partition type to "Linux LVM".

Enter p to print your new partition table and make sure the start block matches what was in the initial partition table printed above.

Enter w to write the partition table to disk. You will see an error about Device or resource busy which you can ignore.

Update kernel in-memory partition table

After changing your partition table, run the following command to update the kernel in-memory partition table:

# partx -u /dev/vda

Resize physical volume

Resize the PV to recognize the extra space

# pvresize /dev/vda2

Resize LV and filesystem

In this command centos is the PV, root is the LV and /dev/vda2 is the partition that was extended. Use pvs and lvs commands to see your physical and logical volume names if you don't know them. The -r option in this command resizes the filesystem appropriately so you don't have to call resize2fs or xfs_growfs separately.

# lvextend -r centos/root /dev/vda2
3
  • More complicated answer then "selected answer" one !
    – Furkat U.
    Feb 18 '19 at 9:47
  • 2
    This is brilliant. Thank you! This should be the accepted answer. I was missing the last step to resize the lv and file system!
    – Skinner927
    Jan 31 '20 at 20:39
  • Also thank you, it worked. For other readers, I got asked an extra question by fdisk while executing the above (centos 7.8), I answered with No, and it worked: Partition #5 contains a LVM2_member signature. Do you want to remove the signature? [Y]es/[N]o: n
    – ionescu77
    Oct 19 '20 at 14:53
5

For those who're having trouble extending logical volumes like me, reading this post might be helping. In summary, you can use lvextend to extend your logical volume: lvextend -l +<PE> <LV_PATH> you can obtain the number of remaining PE(<PE>) by vgdisplay and obtain the path of your logical volume(LV_PATH) by lvdisplay.

Then depending on your distribution, you either want xfs_growfs <LV_PATH> (Centos 7) or resize2fs <LV_PATH> to finally resize.

4

Check this out — everything on a single line, no questions:

parted ---pretend-input-tty /dev/vda resizepart 2 100%;
partx -u /dev/vda; pvresize /dev/vda2;
lvextend -r centos/var /dev/vda2

Here:

  • parted extends partition (---pretend-input-tty is a hidden parted flag)
  • partx updates kernel in-memory partition table
  • pvresize resizes physical volume
  • lvextend resizes logical volume and filesystem

This way reboot is not needed.

The above implies that you have vd* volumes, and under vda2 there is an lvm volume group centos and lvm logical volume var:

$ lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0               2:0    1    4K  0 disk 
vda               8:0    0   40G  0 disk 
├─vda1            8:1    0    1G  0 part /boot
└─vda2            8:2    0   19G  0 part 
  ├─centos-var  253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sr0              11:0    1 1024M  0 rom

If you have sd* volumes, like here:

$ lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0           2:0    1    4K  0 disk 
sda           8:0    0   40G  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 1024M  0 rom

You can do it the same way:

parted ---pretend-input-tty /dev/sda resizepart 2 100%;
partx -u /dev/sda; pvresize /dev/sda2;
lvextend -r /dev/cl/root /dev/sda2
2
  • You should consider adding explanations and your environment. For instance, my partx doesn't have option -u (Centos6.7 util-linux-ng-2.17.2-12.28.el6).
    – Boop
    Aug 19 '19 at 9:40
  • @Boop looks like it works on Centos 7.x + 8.x
    – Hix
    Feb 24 '20 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.