Adding More Space to Hard Drives
This guide will help you modify partitions and extend partitions and LVM volumes.
Partitions
It is unusual to need to extend partitions but it can be required especially if you have imaged a volume onto a larger disk or perhaps you are using a VM and need the volume to be bigger.
Partitions require great care because the tools for their manipulation can be very destructive if used improperly. As a general rule, you can only add space on to the end of a partition and not at the beginning. Because of the volatile nature, this method is not supported and it is highly advised that you have a very good backup before proceeding.
Let us assume that you have a VM and this is what your layout looks like:
[root@test1 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_system-lv_root 5.5G 1.6G 3.7G 30% / tmpfs 499M 0 499M 0% /dev/shm /dev/sda1 485M 53M 407M 12% /boot
[root@test1 ~]# fdisk -l Disk /dev/sda: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000876bf Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 64 1045 7875584 8e Linux LVM
I have an 8GB disk on my VM and use tools to make it bigger (i.e. 20GB). However you do it, this document assumes that you have a bigger physical disk now and are seeing your existing partitions as a small footprint.
[root@test1 ~]# fdisk -l Disk /dev/sda: 21.0 GB, 20971520000 bytes 255 heads, 63 sectors/track, 2549 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000876bf Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 64 1045 7875584 8e Linux LVM
As you can see, it is now bigger and our partition does not fill up the disk. With partitions, we are only able to increase the size of /dev/sda2 on this disk. This is because we can move the end of a partition but not the beginning. On this disk /dev/sda1 ends right where /dev/sda2 begins. If we were to extend /dev/sda1 in anyway, it would overwrite the first blocks of /dev/sda2 and destroy that partition.
Take extra special note of start and end boundary of the partition. In this case, we are going to extend /dev/sda2 to the end of our new 20GB disk. The start block for this partition is 64, the Id type is '8e'. This is crucial to note and remember.
Since we are using this partition, we cannot modify it while in use. Reboot the system in Rescue mode by using the ClearBOX rescue mode or your original install CD. Do not mount the partition when asked. Re-run the 'fdisk -l' command when at the rescue mode command prompt to ensure that the rescue mode sees the disk drive names as expected.
Essentially, we will delete and recreate the partition but this time with a new ending partition location. That is why this is dangerous.
fdisk /dev/sda
Type 'p' to print the partition table.
Type 'd' to delete the partition.
Type the number of the last partition (in my case it is 2)
Type 'n' to create the partition again.
Type the number of the last partition (in my case it is 2)
Type the number of the block that starts the partition (MUST BE THE SAME)
Type the last block you wish to use (Enter defaults to the last block)
Type 't' to set the partition type.
Type the partition type you recorded earlier (in my case it is '8e').
Type 'p' to reprint the modified partition table.
Ensure that the information is correct. Type 'w' to write the partition to disk.
Reboot the system and ensure that it comes up. If it does not, you may have to recover from backup.
[root@test1 ~]# fdisk -l Disk /dev/sda: 21.0 GB, 20971520000 bytes 255 heads, 63 sectors/track, 2549 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000876bf Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 64 2549 19961818+ 8e Linux LVM
LVM
Ok, so our partition is bigger but we cannot use it yet. As you can see though, it is available now to the LVM system. To resize, let us look at the physical volume:
[root@test1 ~]# pvdisplay --- Physical volume --- PV Name /dev/sda2 VG Name vg_system PV Size 7.51 GiB / not usable 3.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 1922 Free PE 0 Allocated PE 1922 PV UUID dVU16X-1n4n-CcQZ-2kVk-UXZ0-bxQT-gGFuSs
We can see that it is at our previous 7.51 GB, let's resize it:
[root@test1 ~]# pvresize /dev/sda2 Physical volume "/dev/sda2" changed 1 physical volume(s) resized / 0 physical volume(s) not resized
Now let's re-examine:
[root@test1 ~]# pvdisplay --- Physical volume --- PV Name /dev/sda2 VG Name vg_system PV Size 19.04 GiB / not usable 37.50 KiB Allocatable yes PE Size 4.00 MiB Total PE 4873 Free PE 2951 Allocated PE 1922 PV UUID dVU16X-1n4n-CcQZ-2kVk-UXZ0-bxQT-gGFuSs
Ok, now we have our space inside the physical volume of our LVM. At this point, you can either create a new volume group or assign the data to the existing volume group. We are going to assign it to an existing volume group:
[root@test1 ~]# vgs VG #PV #LV #SN Attr VSize VFree vg_system 1 2 0 wz--n- 19.04g 11.53g
[root@test1 ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert lv_root vg_system -wi-ao--- 5.54g lv_swap vg_system -wi-ao--- 1.97g
Now we can increase the size of our 'lv_root' LVM
lvextend -l +100%FREE /dev/mapper/vg_system-lv_root
Extending logical volume lv_root to 17.07 GiB Logical volume lv_root successfully resized
Now our partition for root is much bigger. Verify this by running 'lvs':
[root@test1 ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert lv_root vg_system -wi-ao--- 17.07g lv_swap vg_system -wi-ao--- 1.97g
Alternately, you can allocate blocks instead of a percentage. This can be used to increase the size of all the LVM devices under management.
Here is an example:
lvextend -l +1024 /dev/mapper/vg_system-lv_swap
EXT2 EXT3 EXT4
Ok, the last step requires telling the EXT4 partition that it is bigger. It will require a reboot once you are done.
resize2fs /dev/mapper/vg_system-lv_root
It should say something like this:
resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/mapper/vg_system-lv_root is mounted on /; on-line resizing required old desc_blocks = 1, new_desc_blocks = 2 Performing an on-line resize of /dev/mapper/vg_system-lv_root to 4473856 (4k) blocks. The filesystem on /dev/mapper/vg_system-lv_root is now 4473856 blocks long.
Under ClearOS 6, this is all that is needed:
[root@test1 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_system-lv_root 17G 1.6G 15G 10% / tmpfs 499M 0 499M 0% /dev/shm /dev/sda1 485M 53M 407M 12% /boot
On some systems, a reboot is necessary. If you can get away with it, we'd recommend that on 6 as well just to be sure that everything comes up alright.