How to resize and extend centos 7 disk manually

 How to resize and extend centos 7 disk manually

Resizing The Partition

You need to modify the Linux partition table within the virtual machine to recognize that the disk size has changed. This can be done using the fdisk utility, which is a partition editor. For the virtual machine provided by Nagios Enterprises:

  • sda1 is used for the boot partition
  • sda2 is the device for all logical volumes, this is the partition you need to edit

Start fdisk by executing the following command:

fdisk /dev/sda

 

You should print the current partition table so you can keep track of your changes as you go:

Command (m for help): p

Take a screenshot or copy the screen contents into a text file in case you want to reference it later.

To resize a partition, you actually "delete" it and then add a new one with the new size, making sure to place the beginning of the new partition at the same sector/cylinder as the old one started. This process is NOT destructive, your data still remains on the disk. These first steps are for deleting the partition:

Command (m for help): d
Partition number (1-4): 2

 

These next steps re-create the partition:

Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2): 2

 

You will be prompted for the first and last sectors to use, simply accept the defaults for the start and end point (just press Enter). Double-check that they make sense when compared to the information printed earlier.

Next you need to configure the partition type to LVM (8e):

Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

 

At this point you are done making your changes, and just need to write them to the disk. It is a good idea to print (p) the new partition table to ensure you entered things correctly. Once thing look okay, you can commit (write) the partition table to disk: 

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

 

The write command will exit the fdisk program. You will now need to reboot the virtual machine to allow the kernel to recognize the new partition table, execute the following command:

reboot

 

Resizing The Physical Volume (PV)

The CentOS system installed in the virtual machine image we ship uses Logical Volume Management (LVM) for its partitions. If you have never worked with LVM before and want to better understand the terminology herein, visit:

In order to make use of the newly increased disk space, LVM needs to be told that it is there and should be used. For this step you will adjust the size of the physical volume to match the new size of the partition. Establish a terminal session to your Nagios server and then execute the following command:

pvresize /dev/sda2

 

This should produce the following output:

  Physical volume "/dev/sda2" changed
1 physical volume(s) resized / 0 physical volume(s) not resized

 

Resizing The Logical Volume (LV)

The next step is to resize the logical volume to use the new space available in the LVM physical volume. Execute the following command:

lvresize /dev/mapper/centos-www /dev/sda2

 

This should produce the following output (it will differ depending on how much disk space you're adding):

  Size of logical volume centos/www changed from 36.99 GiB (9470 extents) to <77.00 GiB (24831 extents).
Logical volume centos/www successfully resized.

 

Resizing The Filesystem

The last step is to change the filesystem residing on the logical volume so that it knows to put files in that space.

First, determine if you have an xfs or ext filesystem by running the following command:

 df -Th /

The output should display the filesystem type in the Type column.

If XFS, then execute the following command:

xfs_growfs /dev/mapper/centos-www

If EXT, then execute the following command:

resize2fs /dev/mapper/centos-www

This should produce the following output (it will differ depending on how much disk space you're adding):

meta-data=/dev/mapper/centos-www isize=512 agcount=4, agsize=406016 blks
         =     sectsz=512  attr=2, projid32bit=1
         =     crc=1       finobt=0 spinodes=0
data     =     bsize=4096  blocks=1624064, imaxpct=25
         =     sunit=0     swidth=0 blks
naming   =version 2        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 1624064 to 4769792

 

You're done! You can confirm the disk resize worked by using df -h to display a summary of filesystem size and usage. 

If you took a snapshot of your VM before proceeding you should now shutdown the VM and delete the snapshot as it is no longer required for roll-back purposes.

Comments

Popular posts from this blog

List Of MSI Motherboard with type of Processor Slot