Extend Disk on Linux Cloud Server

Before you begin

Step 1

Check the current storage space using the df -h command.

 

[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   38G   21G   18G  54% /
devtmpfs                 1.2G     0  1.2G   0% /dev
tmpfs                    1.2G     0  1.2G   0% /dev/shm
tmpfs                    1.2G   24M  1.2G   2% /run
tmpfs                    1.2G     0  1.2G   0% /sys/fs/cgroup
/dev/sda1                497M  189M  308M  39% /boot
tmpfs                    234M     0  234M   0% /run/user/0

 

Take a note of the LVM name you are extending. This is mounted on / in the above output.

In this example, we are extending /dev/mapper/centos-root.

Step 2

Determine what type of file system structure is in place using fdisk -l.

 

[root@localhost ~]# fdisk -l

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000a2b1e

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    83886079    41430016   8e  Linux LVM

Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/mapper/centos-root: 40.3 GB, 40273707008 bytes, 78659584 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

 
 
If the output of fdisk mentions gparted or parted, follow the section Extending the Partition Using parted further down in the article.

In this example, we are extending sda. Take note of the partition number which is labeled as Linux LVM under the System column and the number in the Start as these will be needed soon.

In this example, the partition number is sda2 and the Start is 1026048.

Extending the Partition Using fdisk

Step 1

Start modifying the parameters of the drive by using fdisk [drive].

 

[root@localhost ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
 
 
If you want to view the partition table again to confirm you are modifying the correct drive, simply type p at the prompt.

Step 2

Delete the partition by typing d then pressing enter.

 

Command (m for help): d

 

Step 3

Specify the partition number and press enter.

 

Partition number (1,2, default 2): 2
Partition 2 is deleted

 

Step 4

Type n to add a partition.

 

Command (m for help): n
Partition type:
p   primary (1 primary, 0 extended, 3 free)
e   extended

 

Step 5

Select the type as Primary by typing p then pressing enter.

 

Select (default p): p

 

Step 6

Type the same partition number that you just deleted.

 

Partition number (2-4, default 2): 2

 

Step 7

Specify the first sector by typing the number which was previously under the "Start" column.

In this example, that was 1026048.

 

First sector (1026048-209715199, default 1026048): 1026048

 

Step 8

At the next prompt, just press enter to use the full available disk space.

 

Last sector, +sectors or +size{K,M,G} (1026048-209715199, default 209715199):
Using default value 209715199
Partition 2 of type Linux and of size 99.5 GiB is set

 

Step 9

Next, change the partition type by typing t and pressing enter.

 

Command (m for help): t

 

Step 10

Specify the partition number again and press enter.

 

Partition number (1,2, default 2): 2

 

Step 11

When prompted for a Hex code, enter 8e which is Linux LVM.

Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Step 12

Confirm the change is correct by entering p to print out the new partition table.

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000a2b1e

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048   209715199   104344576   8e  Linux LVM

Step 13

Exit and write the change.

Command (m for help): w
The partition table has been altered!Calling ioctl() to re-read partition table.

You may get this warning when you try to write the 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.

If you get this message, a restart of the server will be required.

Step 14

Check the partition table to ensure the changes have taken effect by running fdisk -l again.

[root@localhost ~]# fdisk -l

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000a2b1e

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048   209715199   104344576   8e  Linux LVM

Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/mapper/centos-root: 40.3 GB, 40273707008 bytes, 78659584 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

 

Step 15

Proceed to the section Extending the Volume further down in this article.

Extending the Partition Using parted

These steps will need to be following if you get the following message when you run fdisk -l.

 

WARNING: GPT (GUID Partition Table) detected on '/dev/sda'! The util fdisk doesn't support GPT. Use GNU Parted.

 

Step 1

Open parted by running parted.

 

[root@localhost ~]# parted
GNU Parted 2.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.

 

Step 2

Change the units to sectors by typing u s then pressing enter.

 

(parted) u s

 

Step 3

View the partition. When you do this, you will get a message similar to the following:

 

(parted) p
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?

 

Choose to fix this by typing f and pressing enter.

 

Fix/Ignore? f
Model: Msft Virtual Disk (scsi)
Disk /dev/sda: 524288000s
Sector size (logical/physical): 512B/4096B
Partition Table: gptNumber  Start     End        Size       File system  Name     Flags
1      2048s     1048576s   1046529s   ext3         primary  boot
2      1050624s  83884031s  82833408s               primary  lvm

 

Step 4

From the above output of Step 3, look at the partition with the "lvm" flag. Take note of the start sector for that partition found under the start column and the total sector size which is next to the disk letter.

In this case, the start is 1050624 and the total is 524288000. Also take note of the name.

Step 5

Delete the partition by typing rm followed by the partition number.

 

(parted) rm 2
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.

 

Step 6

Recreate the partition by typing mkpart and pressing enter.

 

(parted) mkpart

 

Step 7

Call the partition the same name as before.

 

Partition name?  []? primary

 

Step 8

Leave the file system type as the default by just pressing enter.

 

File system type?  [ext2]?

 

Step 9

Enter the start sector as noted earlier.

 

Start? 1050624

 

Step 10

Enter the total sectors minus 1.

In this case, we take 524288000 and minus one to get 524287999.

 

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?

 

Step 11

You will get the above warning saying the partition is too large and offer to set it to the largest possible. Say yes to this. Note that this step can take some time to finish.

 

Yes/No? y
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.

 

Step 12

Modify the flag of the recreated partition to lvm by typing t [partition number] lvm.

 

(parted) t 2 lvm
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.

 

Step 13

Confirm your changes are correct by printing the partition table with p.

 

(parted) p
Model: Msft Virtual Disk (scsi)
Disk /dev/sda: 524288000s
Sector size (logical/physical): 512B/4096B
Partition Table: gptNumber  Start     End         Size        File system  Name     Flags
1      2048s     1048576s    1046529s    ext3         primary  boot
2      1050624s  524287966s  523237343s               primary  lvm

 

Step 14

Quit gparted by typing q.

 

(parted) q
Information: You may need to update /etc/fstab.
 
Ignore the message mentioning that you may need to update the fstab.

Step 15

Reboot your server.

Extending the Volume

Step 1

Make sure the partition is currently a listed Physical Volume with pvdisplay.

 

[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name               /dev/sda2
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

 

Step 2

Extend the Physical Volume with pvresize [partition].

 

[root@localhost ~]# pvresize /dev/sda2
Physical volume "/dev/sda2" changed
1 physical volume(s) resized / 0 physical volume(s) not resized

 

Step 3

View the current Logical Volumes with lvdisplay.

[root@localhost ~]# lvdisplay
--- 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
# open                 1
LV Size                37.51 GiB
Current LE             9602
Segments               1
Allocation             inherit
Read ahead sectors     auto
- currently set to     8192
Block device           253:1

Step 4

Take note of the LV path of the Logical Volume you are extending. It will be similar but not necessarily the same as the name found in step one.

In this example, the name in step one was /dev/mapper/centos-root meaning the path is /dev/centos/root.

Step 5

Extend the logical volume to use all the new space by running:

 

lvextend -l+100%FREE [Logical Volume]

 

[root@localhost ~]# lvextend -l+100%FREE /dev/centos/root
Size of logical volume centos/root changed from 37.51 GiB (9602 extents) to 97.51 GiB (24962 extents).
Logical volume root successfully resized.

Step 6

Resize the file system to use the new space. Please note that this is different depending on the server you are working on. Both of the following commands will take a while to complete. Do not interrupt them.

On CloudNX Cloud and Virtual Private Servers run the command:

xfs_growfs [Logical Volume].

 

[root@localhost ~]# xfs_growfs /dev/centos/root
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 Bare Metal, Legacy Cloud and Legacy Virtual servers run:

resize2fs [Logical Volume].

 

[root@localhost ~]# resize2fs /dev/centos/root
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.

 

Step 7

Confirm the space has been added with df -h.

 

[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   98G   21G   78G  21% /
devtmpfs                 1.2G     0  1.2G   0% /dev
tmpfs                    1.2G     0  1.2G   0% /dev/shm
tmpfs                    1.2G  8.5M  1.2G   1% /run
tmpfs                    1.2G     0  1.2G   0% /sys/fs/cgroup
/dev/sda1                497M  189M  308M  39% /boot
tmpfs                    234M     0  234M   0% /run/user/0


Was this article helpful?

mood_bad Dislike 0
mood Like 0
visibility Views: 862