Extending the Logical Volume Part 2 – parted
We have already covered how to extend your partition and the logical volume in Extending the Logical Volume Part 1 – fdisk. If your disk drive is larger than 2TB, you will need to use an alternative utility called parted, as fdisk was created for drives smaller than 2TB. These smaller drives use a Master Boot Record (MBR), whereas 2TB+ drives will require GUID Partition Table (GPT) instead. This is due to filesystem limitations with MBR that are not a problem with GPT.
Let us explore how to use parted to extend your partition and logical volume.
Using parted to extend the partition
You can check if you need to use parted rather than fdisk by typing the following:
fdisk -l
If you get an output, please follow Extending the Logical Volume Part 1 – fdisk (hyperlink this) to extend your partition and logical volume. Use this guide if you get the following error:
WARNING: GPT (GUID Partition Table) detected on '/dev/sda'! The util fdisk doesn't support GPT. Use GNU Parted.
You can open the parted utility by typing the following into your Terminal window:
parted
You should see similar to the following output:
GNU Parted 2.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
Next, you will need to change the units used by parted to sectors by typing the following:
(parted) unit s
NOTE – parted will continue running in an interactive shell as indicated by the (parted) message in your Terminal window.
Next, view the partitions on /dev/sda (or your directory) by typing the following:
p
You will notice that a similar message appears:
Warning: Not all of the space available to /dev/sda appears to be used, you can fix the GPT to use all of the space (an extra 440401920 blocks) or continue with the current setting?
Fix/Ignore?
NOTE – If this does not appear, parted cannot see the newly assigned disk space. You will need to check that your hosting provider has assigned the space before returning to this guide.
This is correct and indicates the new storage space is visible to parted. Type the following to tell parted to fix this problem:
f
You should see a similar output to this:
Model: Msft Virtual Disk (scsi)
Disk /dev/sda: 524288000s
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Number Start End Size File system Name Flags
1 2048s 1048576s 1046529s ext3 primary boot
2 1050624s 83884031s 82833408s primary lvm
Listed above, you will see 2 partitions, with one being flagged as lvm. Make a note of the Start sector, which is 1050624 in our case (ignore the letters, just use the numbers). Also, make a note of the total sector size, which is listed next to Disk /dev/sda: as 524288000 (again, ignore the letters). Finally, note down the partition number, which is 2.
WARNING – The next steps will delete your existing partition. If you have important data saved on this partition, please back it up before continuing with this guide.
Next, you need to delete the existing partition so you can create a new one. Type the following into your Terminal window:
rm 2
We are using rm 2 as the partition number listed above was 2.
You may see one of the following warnings or errors:
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy). As a result, it may not reflect all of your changes until after reboot.
This means parted was able to make the change, but the partition table cannot be refreshed until you reboot. This is due to a running process having ownership of the partition table.
If you see the following, you will need to unmount the partition:
Error: Partition /dev/sda is being used. You must unmount it before you modify it with Parted.
You can unmount a partition by typing the following:
quit
This will exit parted and return you to a basic Terminal window.
Type the following to unmount the partition forcefully:
sudo umount -f /dev/sda
sudo is required as this command requires root privileges.
umount stands for unmount, and is the utility we are using.
-f forces umount to unmount the partition, regardless of whether it is in use or busy.
Finally, /dev/sda is the partition we want to unmount.
Now rm 2 should work, and we can continue with the guide.
Now you need to use mkpart to set a new partition. Start by typing:
mkpart
You will see the following appear:
Partition name? []?
Type the same name as before, and press Enter on the keyboard. In our case, it was:
Primary
Now you will be asked for the File system type? [ext2]?. It defaults to ext2, and we want to use that, so simply press Enter again on the keyboard.
Next, you will see the following:
Start?
You should input the Start sector that we mentioned earlier here. In our case, that was:
Start? 1050624
Now, you need the Total Sector Size value that we mentioned earlier. Whatever that number was for you, -1 away. For example, in our case, 524288000 becomes 524287999. You will see the following message:
End? 524287999
Warning: You requested a partition from 1050624s to 524287999s.
The closest location we can manage is 1050624s to 524287966s.
Is this still acceptable to you?
Yes/No?
NOTE – As this logical partition has a sector size of 512 bytes, parted will automatically find the closest multiple as indicated by The closest location we can manage is.
Type Y to accept this action.
You may get the same warning, as shown before:
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy). As a result, it may not reflect all of your changes until after reboot.
You can continue to ignore this for the remaining steps.
Now we need to set the flag for this new partition to LVM. Type the following and press Enter on the keyboard:
t 2 lvm
t stands for toggle, and sets the flag state for a partition. 2 is the partition number in our example. lvm is the flag state we wish to set.
The warning will appear again.
Finally, confirm your changes are correct by typing the following:
p
p stands for print and will print a list of partitions on your drive.
Check back through, and double-check all of the changes you made are displaying here.
Now, reboot your server. Next, we will extend the logical volume.
Extending the logical volume
Now you can extend the logical volume.
First, you will need to check that the partition is listed as a Physical Volume.
Use the following command to display all existing Physical Volumes on the disk:
pvdisplay
You should see a similar output to this:
--- Physical volume ---
PV Name /dev/sda
VG Name centos
PV Size 39.51 GiB / not usable 2.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 10114
Free PE 0
Allocated PE 10114
PV UUID NP5SVJ-xEhi-hEd9-sMZs-6DGe-VDOi-zuIetu
You can now extend the Physical Volume by typing:
pvresize /dev/sda
/dev/sda applies to our example, but replace this with your Physical Volume name, as listed next to PV Name.
You should get an output saying:
Physical volume "/dev/sda" changed
1 physical volume(s) resized / 0 physical volume(s) not resized
Now display the Logical Volumes on the drive by typing:
lvdisplay
In our example, we saw the following output:
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID w7zRnQ-0TCm-IIJF-dMR4-ypNL-HxZV-tO90iu
LV Write Access read/write
LV Creation host, time localhost, 2014-10-06 08:56:38 +0000
LV Status available
#open 2
LV Size 2.00 GiB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID gaqFxq-HmdB-Msdc-DH32-qgz0-BafH-xERhST
LV Write Access read/write
LV Creation host, time localhost, 2014-10-06 08:56:39 +0000
LV Status available
#open1
LV Size 37.51 GiB
Current LE 9602
Segments 1
Allocation inherit
Read ahead sectors auto
currently set to 8192
Block device 253:1
NOTE – The swap is similar to the page file on Windows, and should be left as is to allow for disk-based caching when programs have been idle for extended periods of time. This helps reduce RAM usage and dramatically improves system stability.
Root is the logical volume we want to extend. That means we should use /dev/centos/root in the next step.
Now you can extend the volume by typing:
lvextend -l+100%FREE /dev/centos/root
lvextend stands for logical volume extend.
-l+100%FREE tells lvextend to extend the volume using 100% of the available free space. If you wish to use a specific number, just change 100% to 95% etc. This may be useful if you want to assign some space to another partition or volume.
You should get a similar output to this:
Size of logical volume centos/root changed from 37.51 GiB (9602 extents) to 97.51 GiB (24962 extents).
Logical volume root successfully resized.
Now, you need to expand the OS filesystem to accommodate the increased volume size.
Please choose the correct type of server by checking both of the options below.
For Fasthosts CloudNX or VPS servers, type the following:
xfs_growfs /dev/centos/root
The output should be:
meta-data=/dev/mapper/centos-root isize=256 agcount=9, agsize=1147392 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=9832448, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
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 9832448 to 25561088
On Fasthosts Bare Metal, Legacy Cloud, and Legacy Virtual servers, type the following:
resize2fs /dev/centos/root
The output will be:
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/centos/root is mounted on /; on-line resizing required
old desc_blocks = 3, new_desc_blocks = 16
Performing an on-line resize of /dev/VolGroup00/LogVol00 to 25561088 (4k) blocks.
The filesystem on /dev/centos/root is now 25561088 blocks long.
After choosing the relevant command for your server type, your Logical Volume should now be extended, and the process complete.
Double-check that the Logical Volume extended by typing:
df -h
If your volume has increased by the additional storage space amount you intended, then the process was successful.