Extending Partitions and File Systems on Linux (fdisk)
Scenarios
In Linux, after the capacity expansion succeeded, the additional disk space needs to be allocated to an existing partition or a new partition.
This topic uses CentOS 7.0 64bit to describe how to allocate the additional disk space to a partition using fdisk.
The method for allocating the additional space varies depending on the server OS. This document is used for reference only. For the detailed operations and differences, see the corresponding OS documents.
- Create a new partition (services will not be interrupted).
Creating a new partition after expansion does not require the original partitions to be unmounted. Therefore, the impacts on services are minor than re-creating an existing partition. This method is recommended for system disks or disks carrying services that cannot be interrupted.
If the MBR partition style is used, ensure that the disk capacity does not exceed 2 TB and the number of partitions does not reach the upper limit after the expansion.
- Re-create an existing partition (services will be interrupted).
If the MBR partition style is used and the number of partitions has reached the upper limit, you can only allocate the additional space to an existing partition. Re-creating an existing partition does not delete its data, but requires the partition to unmount. Therefore, services will be interrupted.
If the MBR partition style is used and the disk capacity after expansion will exceed 2 TB, the space exceeding 2 TB cannot be used. To make use of that space, change the disk partition style from MBR to GPT. Data on the disk will be cleared during such a change. Therefore, back up the disk data before changing the partition style.
Performing the expansion operations with caution. Misoperation may lead to data loss or exceptions. Therefore, you are advised to use CBR to back up the disk data before expansion. For details, see Managing Backups.
Prerequisites
- You have logged in to the server.
- For how to log in to an ECS, see the Elastic Cloud Server User Guide.
- For how to log in to a BMS, see the Bare Metal Server User Guide.
- You have attached the disk to the server, and the additional space has not been allocated.
Checking File Systems on the To-be-expanded Disk
Before expanding the disk capacity, check whether the file systems on the disk can be properly mounted.
- (Optional) If there is an unmounted partition, run the following command to mount the partition on the specified directory:
mount Disk partition Mount point
Example command:
mount /dev/xvdb1 /mnt/sdc
If the system returns a mount error, check whether the file system is correctly created. For example, a user may create the file system for the /dev/xvdb disk instead of the /dev/xvdb1 partition. In this case, what mounted is actually the /dev/xvdb disk, but not the /dev/xvdb1 partition.
- Run the following command to view the disk mounting information:
df -TH
Information similar to the following is displayed:
[root@ecs-b656 test]# df -TH Filesystem Type Size Used Avail Use% Mounted on /dev/xvda2 xfs 11G 7.4G 3.2G 71% / devtmpfs devtmpfs 4.1G 0 4.1G 0% /dev tmpfs tmpfs 4.1G 82k 4.1G 1% /dev/shm tmpfs tmpfs 4.1G 9.2M 4.1G 1% /run tmpfs tmpfs 4.1G 0 4.1G 0% /sys/fs/cgroup /dev/xvda3 xfs 1.1G 39M 1.1G 4% /home /dev/xvda1 xfs 1.1G 131M 915M 13% /boot /dev/xvdb1 ext4 11G 38M 9.9G 1% /mnt/sdc
In the command output, the file system of the /dev/xvdb1 partition is ext4, and the partition is mounted on /mnt/sdc.
- Run the following command to switch to the mounting directory to view the files on the disk:
ll Mounting directory
Example command:
ll /mnt/sdc
If the files on the disk can be viewed, the disk is normal and can be expanded.
Viewing the Partition Style
Before allocating the additional space, query the current disk partition style. If MBR is used, you can use either the fdisk or parted partitioning tool. If GPT is used, you must use the parted partitioning tool.
- Run the following command to view the current disk partition style:
fdisk -l
Information similar to the following is displayed:[root@ecs-1120 linux]# fdisk -l Disk /dev/xvda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000c5712 Device Boot Start End Blocks Id System /dev/xvda1 2048 83886079 41942016 83 Linux WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion. Disk /dev/xvdb: 161.1 GB, 161061273600 bytes, 314572800 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: gpt # Start End Size Type Name 1 34 209715166 100G Microsoft basic opt 2 209715167 314572766 50G Microsoft basic opt1 WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion. Disk /dev/xvdc: 42.9 GB, 42949672960 bytes, 83886080 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: gpt # Start End Size Type Name 1 34 16777215 8G Microsoft basic opt 2 16777216 83884031 32G Microsoft basic opt
In the command output, parameter Disk label type indicates the disk partition style. Value dos indicates the MBR partition style, and value gpt indicates the GPT partition style.
Creating a New Partition
The following example shows you how to make use of the additional space of a system disk by creating a new partition and mount the partition on /opt without interrupting services.
- Run the following command to view the disk partition information:
fdisk -l
Information similar to the following is displayed: (/dev/xvda is the system disk.)[root@ecs-bab9 test]# fdisk -l Disk /dev/xvda: 64.4 GB, 64424509440 bytes, 125829120 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: 0x000cc4ad Device Boot Start End Blocks Id System /dev/xvda1 * 2048 2050047 1024000 83 Linux /dev/xvda2 2050048 22530047 10240000 83 Linux /dev/xvda3 22530048 24578047 1024000 83 Linux /dev/xvda4 24578048 83886079 29654016 5 Extended /dev/xvda5 24580096 26628095 1024000 82 Linux swap / Solaris
- Run the following command to enter fdisk:
fdisk /dev/xvda
Information similar to the following is displayed:[root@ecs-2220 ~]# fdisk /dev/xvda 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. Command (m for help):
- Enter n and press Enter to create a new partition.
Information similar to the following is displayed:
Command (m for help): n All primary partitions are in use Adding logical partition 6 First sector (26630144-83886079, default 26630144):
If the MBR partition style is used, a maximum of 4 primary partitions, or 3 primary partitions and 1 extended partition can be created. The extended partition cannot be used directly and must be divided into logical partitions before use.
In this example, the maximum number of primary partitions has been reached for the system disk, and the disk already has 5 partitions (3 primary partitions and 2 logical partitions). Therefore, the system automatically creates a sixth logical partition in the extended partition.
To view the example operations in case that the maximum number of primary partitions is not reached for the system disk, see Extending Partitions and File Systems of a System Disk on Linux (fdisk).
- Enter the new partition's start sector, for example the default value, and press Enter.
The start sector must be greater than end sector of the existing partition.
Information similar to the following is displayed:First sector (26630144-83886079, default 26630144): Using default value 26630144 Last sector, +sectors or +size{K,M,G} (26630144-83886079, default 83886079):
- Enter the new partition's end sector and press Enter.
The default end sector is used in this example.
Information similar to the following is displayed:Last sector, +sectors or +size{K,M,G} (26630144-83886079, default 83886079): Using default value 83886079 Partition 6 of type Linux and of size 27.3 GiB is set Command (m for help):
- Enter p and press Enter to view the new partition.
Information similar to the following is displayed:
Disk /dev/xvda: 64.4 GB, 64424509440 bytes, 125829120 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: 0x000cc4ad Device Boot Start End Blocks Id System /dev/xvda1 * 2048 2050047 1024000 83 Linux /dev/xvda2 2050048 22530047 10240000 83 Linux /dev/xvda3 22530048 24578047 1024000 83 Linux /dev/xvda4 24578048 83886079 29654016 5 Extended /dev/xvda5 24580096 26628095 1024000 82 Linux swap / Solaris /dev/xvda6 26630144 83886079 28627968 83 Linux Command (m for help):
- Enter w and press Enter to write the changes to the partition table.
Information similar to the following is displayed:
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 partition is created.
In case that you want to discard the changes made before, you can exit fdisk by entering q.
- Run the following command to synchronize the new partition table to the OS:
partprobe
- Run the following command to set the file system format for the new partition:
(The ext4 file system is used in this example.)
mkfs -t ext4 /dev/xvda6
The procedure for setting the xfs file system is the same as that for the ext3 or ext4 file system. The command for creating the xfs file system is mkfs -t xfs /dev/xvda6.
Information similar to the following is displayed:[root@ecs-bab9 test]# mkfs -t ext4 /dev/xvda6 mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 1790544 inodes, 7156992 blocks 357849 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2155872256 219 block groups 32768 blocks per group, 32768 fragments per group 8176 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done
The formatting takes a while, and you need to observe the system running status. Once done is displayed in the command output, the formatting is complete.
- Run the following command to mount the new partition on a space-demanding directory, for example /opt:
mount /dev/xvda6 /opt
Information similar to the following is displayed:[root@ecs-bab9 test]# mount /dev/xvda6 /opt [root@ecs-bab9 test]#
If the new partition is mounted on a directory that is not empty, the subdirectories and files in the directory will be hidden. Therefore, you are advised to mount the new partition on an empty directory or a new directory. If the new partition must be mounted on a directory that is not empty, move the subdirectories and files in this directory to another directory temporarily. After the partition is successfully mounted, move the subdirectories and files back.
- Run the following command to view the mount result:
df -TH
Information similar to the following is displayed:[root@ecs-bab9 test]# df -TH Filesystem Type Size Used Avail Use% Mounted on /dev/xvda2 xfs 11G 7.4G 3.2G 71% / devtmpfs devtmpfs 4.1G 0 4.1G 0% /dev tmpfs tmpfs 4.1G 82k 4.1G 1% /dev/shm tmpfs tmpfs 4.1G 9.2M 4.1G 1% /run tmpfs tmpfs 4.1G 0 4.1G 0% /sys/fs/cgroup /dev/xvda3 xfs 1.1G 39M 1.1G 4% /home /dev/xvda1 xfs 1.1G 131M 915M 13% /boot /dev/xvda6 ext4 29G 47M 28G 1% /opt
Re-creating an Existing Partition
The following example shows you how to make use of the additional space of a disk attached to a server by re-creating the /dev/xvdb1 partition and mounting the partition on /mnt/sdc. During this process, services will be interrupted.
During an expansion, the additional space is added to the end of the disk. When the disk has multiple partitions, only the partition at the end of this disk can be extended.
- Run the following command to view the disk partition information:
fdisk -l
Information similar to the following is displayed:[root@ecs-b656 test]# fdisk -l Disk /dev/xvda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000cc4ad Device Boot Start End Blocks Id System /dev/xvda1 * 2048 2050047 1024000 83 Linux /dev/xvda2 2050048 22530047 10240000 83 Linux /dev/xvda3 22530048 24578047 1024000 83 Linux /dev/xvda4 24578048 83886079 29654016 5 Extended /dev/xvda5 24580096 26628095 1024000 82 Linux swap / Solaris Disk /dev/xvdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0xb00005bd Device Boot Start End Blocks Id System /dev/xvdb1 2048 20971519 10484736 83 Linux
In the command output, the total capacity of the /dev/xvdb disk is 21.5 GB, and the disk has only one partition, dev/xvdb1. The partition's start sector is 2048, and its end sector is 20971519.
View the /dev/xvdb capacity and check whether the additional space is included.- If the additional space is not included, refresh the capacity according to Extending Partitions and File Systems of a SCSI Data Disk on Linux (fdisk).
- If the additional space is included, take note of the start and end sectors of the dev/xvdb1 partition and then go to 2. These values will be used in the following operations.
- Run the following command to unmount the partition:
umount /mnt/sdc
- Run the following command to enter fdisk and enter d to delete the /dev/xvdb1 partition:
fdisk /dev/xvdb
Information similar to the following is displayed:[root@ecs-b656 test]# fdisk /dev/xvdb 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. Command (m for help): d Selected partition 1 Partition 1 is deleted Command (m for help):
After deleting the partition, re-create the partition according to the following steps, and data on this disk will not be lost.
- Enter n and press Enter to create a new partition.
Entering n creates a new partition.
Information similar to the following is displayed:Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended
There are two types of disk partitions:- Choosing p creates a primary partition.
- Choosing e creates an extended partition.
- Ensure that the entered partition type is the same as the partition had before. In this example, a primary partition is used. Therefore, enter p and press Enter to create a primary partition.
Information similar to the following is displayed:
Select (default p): p Partition number (1-4, default 1):
In the command output, Partition number specifies the primary partition number.
- Ensure that the entered partition number is the same as the partition had before. In this example, partition number 1 is used. Therefore, enter 1 and press Enter.
Information similar to the following is displayed:
Partition number (1-4, default 1): 1 First sector (2048-41943039, default 2048):
In the command output, First sector specifies the start sector.
Data will be lost if the following operations are performed:- Select a start sector other than the partition had before.
- Select an end sector smaller than the partition had before.
- Ensure that the entered start sector is the same as the partition had before. In this example, start sector 2048 is recorded in 1. Therefore, enter 2048 and press Enter.
Information similar to the following is displayed:
First sector (2048-41943039, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):
In the command output, Last sector specifies the end sector.
- Ensure that the entered end sector is greater than or equal to the end sector recorded in 1. In this example, the recorded end sector is 20971519, and the default end sector is used. Therefore, enter 41943039 and press Enter.
Information similar to the following is displayed:
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): Using default value 41943039 Partition 1 of type Linux and of size 20 GiB is set Command (m for help):
The partition is successfully created.
- Enter p and press Enter to view details about the new partition.
Information similar to the following is displayed:
Command (m for help): p Disk /dev/xvdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0xb00005bd Device Boot Start End Blocks Id System /dev/xvdb1 2048 41943039 20970496 83 Linux Command (m for help):
Details about the /dev/xvdb1 partition are displayed.
- Enter w and press Enter to write the changes to the partition table.
Information similar to the following is displayed:
Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
The partition is created.
In case that you want to discard the changes made before, you can exit fdisk by entering q.
- Perform the following operations based on the file system of the disk:
- For the ext3 or ext4 file system
- Run the following command to check the correctness of the file system on /dev/xvdb1:
e2fsck -f /dev/xvdb1
Information similar to the following is displayed:[root@ecs-b656 test]# e2fsck -f /dev/xvdb1 e2fsck 1.42.9 (28-Dec-2013) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/xvdb1: 11/655360 files (0.0% non-contiguous), 83137/2621184 blocks
- Run the following command to extend the size of the file system on /dev/xvdb1:
resize2fs /dev/xvdb1
Information similar to the following is displayed:[root@ecs-b656 test]# resize2fs /dev/xvdb1 resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/xvdb1 to 5242624 (4k) blocks. The filesystem on /dev/xvdb1 is now 5242624 blocks long.
- Run the following command to mount the new partition on /mnt/sdc:
mount /dev/xvdb1 /mnt/sdc
- Run the following command to check the correctness of the file system on /dev/xvdb1:
- For the xfs file system
- Run the following command to mount the new partition on /mnt/sdc:
mount /dev/xvdb1 /mnt/sdc
- Run the following command to extend the size of the file system on /dev/xvdb1:
sudo xfs_growfs /dev/xvdb1
- Run the following command to mount the new partition on /mnt/sdc:
- For the ext3 or ext4 file system
- Run the following command to view the mount result:
df -TH
Setting Automatic Mounting at System Start
To automatically mount partitions at system starts, do not specify partitions, for example, /dev/xvdb1, in /etc/fstab because the sequence of cloud devices, and therefore their names may change during the server stop or start. You are advised to use the UUID in /etc/fstab to set automatic mounting at system start.
UUID is the unique character string for disk partitions in a Linux system.
- Run the following command to query the partition UUID:
blkid Disk partition
For example, run the following command to query the UUID of the /dev/xvdb1 partition:
blkid /dev/xvdb1
Information similar to the following is displayed:
[root@ecs-b656 test]# blkid /dev/xvdb1 /dev/xvdb1: UUID="1851e23f-1c57-40ab-86bb-5fc5fc606ffa" TYPE="ext4"
The UUID of the /dev/xvdb1 partition is displayed.
- Run the following command to open the fstab file using the vi editor:
vi /etc/fstab
- Press i to enter the editing mode.
- Move the cursor to the end of the file and press Enter. Then, add the following information:
UUID=1851e23f-1c57-40ab-86bb-5fc5fc606ffa /mnt/sdc ext3 defaults 0 2
UUID=1851e23f-1c57-40ab-86bb-5fc5fc606ffa /mnt/sdc ext4 defaults 0 2
The preceding content is used for reference only. Add the information that is used in the environment. The parameters are described as follows:- The first column indicates the partition UUID obtained in 1.
- The second column indicates the directory on which the partition is mounted. You can query the mount point using the df -TH command.
- The third column indicates the file system format of the partition. You can query the file system format using the df -TH command.
- The fourth column indicates the partition mount option. Normally, this parameter is set to defaults.
- The fifth column indicates the Linux dump backup option.
- 0: not use Linux dump backup. Normally, dump backup is not used, and you can set this parameter to 0.
- 1: use Linux dump backup.
- The sixth column indicates the fsck option, that is, whether to use fsck to check the attached disk during startup.
- 0: not use fsck.
- If the mount point is the root partition (/), this parameter must be set to 1.
When this parameter is set to 1 for the root partition, this parameter for other partitions must start with 2 so that the system checks the partitions in the ascending order of the values.
- Press Esc, enter :wq, and press Enter.
The system saves the configurations and exits the vi editor.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot