We’ve got a virtual server that’s low on disk space. The system administrator responsible for the virtual machine has expanded the disk. Now we need to expand the filesystem to include the additional disk space.
TIP: There are two ways we can do this. 1) Don’t expand the disk but rather add a second disk and use that disk for whatever is taking up the most space such as “/var/www/html” on a web-server. That is the lowest risk option. 2) Create a new partition (using “fdisk” of type “8e”) on the expanded disk to use up the remaining space and then create a new LVM disk (using “pvcreate /dev/sdaX”). Then expand the LVM volume group to include the new disk (using “vgextend NAME-X /dev/sdaX”). Then expand the Volume LVM_NAME to include the new volume (using “lvextend /dev/mapper/NAME-Y /dev/sdaX”). Finally we expand the filesystem to include the new/unused disk space (using “resize2fs /dev/mapper/NAME-Y”).
We’re using option 2 here. It’s more risky because it involves working on live disks (potentially) so make sure to have a plan if all goes bad.
Let’s start with a view of the disks. In the following we can see there is a 100G disk but only about 30G has been used.
[[email protected] ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 100G 0 disk ├─sda1 8:1 0 500M 0 part /boot └─sda2 8:2 0 29.5G 0 part ├─VolGroup-lv_root (dm-0) 253:0 0 26.5G 0 lvm / └─VolGroup-lv_swap (dm-1) 253:1 0 3G 0 lvm [SWAP] sr0 11:0 1 1024M 0 rom
Create the new partition on “/dev/sda”. it will be called “/dev/sda3” as that’s the next partition. Remember to make the new partition a “type 8e” partition.
It will look similar to this:
Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux /dev/sda2 64 3917 30944256 8e Linux LVM /dev/sda3 3917 13054 73398975 8e Linux LVM
Convert the new filesystem into an LVM volume:
If the above gives an error such as “Device /dev/sda3 not found (or ignored by filtering)” you’ll likely need to reboot.
Run “vgs” to get the Volume Group name:
[[email protected] ~]# vgs VG #PV #LV #SN Attr VSize VFree VolGroup 1 2 0 wz--n- 29.51g 0
We can see above that ‘my’ Volume Group is called “VolGroup”. To include our new volume into the “VolGroup” Volume Group, run this:
vgextend VolGroup /dev/sda3
Now we expand to include all available disk space offer by the new physical volume. This is a two step process. We need the Volume path and then we extend over it.
[[email protected] ~]# df -h / Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 26G 14G 12G 55% /
The full path is “/dev/mapper/VolGroup-lv_root”. Now extend it with the following command:
lvextend /dev/mapper/VolGroup-lv_root /dev/sda3
Finally we extend the filesystem over the larger disk:
Confirm it has worked with these two commands “df -h” and “lsblk”:
[[email protected] ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 100G 0 disk ├─sda1 8:1 0 500M 0 part /boot ├─sda2 8:2 0 29.5G 0 part │ ├─VolGroup-lv_root (dm-0) 253:0 0 96.5G 0 lvm / │ └─VolGroup-lv_swap (dm-1) 253:1 0 3G 0 lvm [SWAP] └─sda3 8:3 0 70G 0 part └─VolGroup-lv_root (dm-0) 253:0 0 96.5G 0 lvm / sr0 11:0 1 1024M 0 rom
… and …
[[email protected] ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 95G 14G 77G 16% / tmpfs 7.6G 0 7.6G 0% /dev/shm /dev/sda1 477M 114M 339M 26% /boot