Extending Partitions and File Systems of a System Disk on Linux (fdisk)
Scenarios
In Linux, after the capacity expansion succeeds, the additional disk space needs to be allocated to an existing partition or a new partition.
This topic uses CentOS 7.4 64bit to describe how to allocate the additional system disk space to a partition using fdisk.
- To create new partitions using fdisk in CentOS 7.0 64bit, see Creating a New Partition.
- To create new partitions using parted in CentOS 7.0 64bit, see Creating a New Partition.
A disk has been attached to a server, and the original disk capacity is 40 GB. In addition, a 40-GB space has been added to the disk on the console, and the total disk capacity should be 80 GB. The following operations guide you to use the additional space and extend the partition.
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 unmounting the original partitions. Therefore, the impacts on services are smaller 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 be unmounted. Therefore, services will be interrupted.
If the MBR partition style is used and the disk capacity after expansion will exceed 2 TB, the space beyond 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.
Perform 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 How Do I Log In to My ECS?
- For how to log in to a BMS, see Linux BMS Login Methods.
- You have attached the disk to the server, but the additional space has not been allocated.
Creating a New Partition
The following example shows you how to use the additional capacity of a system disk attached to a server by creating a new partition. The system disk already has the /dev/vda1 partition, which is mounted on /. In the following operations, new partition /dev/vda2 will be created and mounted on /opt. During this process, services are not interrupted.
- Run the following command to view the disk partition information:
fdisk -l
Information similar to the following is displayed:[root@ecs-2220 ~]# fdisk -l Disk /dev/vda: 85.9 GB, 85899345920 bytes, 167772160 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: 0x0008d18f Device Boot Start End Blocks Id System /dev/vda1 * 2048 83886079 41942016 83 Linux
In the command output, the capacity of the /dev/vda system disk is 80 GB, in which the in-use /dev/vda1 partition takes 40 GB and the additional 40 GB has not been allocated.
- Run the following command to enter fdisk:
fdisk /dev/vda
Information similar to the following is displayed:[root@ecs-2220 ~]# fdisk /dev/vda 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 Partition type: p primary (1 primary, 0 extended, 3 free) e extended
There are two types of disk partitions:- Choosing p creates a primary partition.
- Choosing e creates an extended partition.
- In this example, a primary partition is created. Therefore, enter p and press Enter to create a primary partition. Information similar to the following is displayed:
Select (default p): p Partition number (2-4, default 2):
- Enter the serial number of the primary partition and press Enter. Partition number 2 is used in this example. Therefore, enter 2 and press Enter. Information similar to the following is displayed:
Partition number (2-4, default 2): 2 First sector (83886080-167772159, default 83886080):
- Enter the new partition's start sector, for example the default value, and press Enter.
The default start sector is used in this example.
Information similar to the following is displayed:First sector (83886080-167772159, default 83886080): Using default value 83886080 Last sector, +sectors or +size{K,M,G} (83886080-167772159,default 167772159): - 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} (83886080-167772159, default 167772159): Using default value 167772159 Partition 2 of type Linux and of size 40 GiB is set Command (m for help): - Enter p and press Enter to view the new partition. Information similar to the following is displayed:
Command (m for help): p Disk /dev/vda: 85.9 GB, 85899345920 bytes, 167772160 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: 0x0008d18f Device Boot Start End Blocks Id System /dev/vda1 * 2048 83886079 41942016 83 Linux /dev/vda2 83886080 167772159 41943040 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/vda2
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/vda2.
Information similar to the following is displayed:[root@ecs-2220 ~]# mkfs -t ext4 /dev/vda2 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 2621440 inodes, 10485760 blocks 524288 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2157969408 320 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: doneThe 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/vda2 /opt
Information similar to the following is displayed:[root@ecs-bab9 test]# mount /dev/vda2 /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-2220 ~]# df -TH Filesystem Type Size Used Avail Use% Mounted on /dev/vda1 ext4 43G 2.0G 39G 5% / devtmpfs devtmpfs 509M 0 509M 0% /dev tmpfs tmpfs 520M 0 520M 0% /dev/shm tmpfs tmpfs 520M 7.2M 513M 2% /run tmpfs tmpfs 520M 0 520M 0% /sys/fs/cgroup tmpfs tmpfs 104M 0 104M 0% /run/user/0 /dev/vda2 ext4 43G 51M 40G 1% /opt
Configuring Auto Mount at System Startup
To automatically mount partitions at system startup, do not specify partition names, for example, /dev/xvdb1, in /etc/fstab because the sequence of cloud devices and their names may change during a server stop or start. You are advised to use the UUID in /etc/fstab to configure auto mount at startup.
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 ext4 defaults 0 2
The preceding information is used for reference only. The line starting with UUID is the information added. Edit this line from left to right to match the following:- UUID: The UUID obtained in 1.
- Mount point: The directory on which the partition is mounted. You can query the mount point using df -TH.
- Filesystem: The file system format of the partition. You can query the file system format using df -TH.
- Mount option: The partition mount option. Usually, this parameter is set to defaults.
- Dump: The Linux dump backup option.
- 0: Linux dump backup is not used. Normally, dump backup is not used, and you can set this parameter to 0.
- 1: Linux dump backup is used.
- fsck: The fsck option, which means whether to use fsck to check the disk during startup.
- 0: The fsck option is not used.
- 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