Extending Partitions and File Systems on Linux (parted)
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 EulerOS 2.0 64bit to describe how to allocate the additional disk space to a partition using parted.
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.
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 disk information:
lsblk
Information similar to the following is displayed:[root@ecs-1120 linux]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvda 202:0 0 40G 0 disk └─xvda1 202:1 0 40G 0 part / xvdb 202:16 0 150G 0 disk ├─xvdb1 202:17 0 100G 0 part /mnt/sdc └─xvdb2 202:18 0 50G 0 part /mnt/opt xvdc 202:32 0 40G 0 disk ├─xvdc1 202:33 0 8G 0 part └─xvdc2 202:34 0 32G 0 part
- Run the following command and enter p to view the current disk partition style:
parted Disk name
For example, run the following command to view the partition style of the /dev/xvdb disk:
parted /dev/xvdb
Information similar to the following is displayed:root@ecs-1120 linux]# parted /dev/xvdb GNU Parted 3.1 Using /dev/xvdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) p Model: Xen Virtual Block Device (xvd) Disk /dev/xvdb: 161GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 17.4kB 107GB 107GB ext4 opt 2 107GB 161GB 53.7GB ext4 opt1
In the command output, parameter Partition Table indicates the disk partition style. Value msdos indicates the MBR partition style, and value gpt indicates the GPT partition style.
- Enter q and press Enter to exit parted.
- Check the disk partition style of other disks. For details, see 2 to 3.
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:
lsblk
Information similar to the following is displayed:[root@ecs-1120 linux]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvda 202:0 0 80G 0 disk └─xvda1 202:1 0 40G 0 part / xvdb 202:16 0 250G 0 disk ├─xvdb1 202:17 0 100G 0 part └─xvdb2 202:18 0 50G 0 part xvdc 202:32 0 40G 0 disk ├─xvdc1 202:33 0 8G 0 part └─xvdc2 202:34 0 32G 0 part
In the command output, the capacity of the dev/xvda system disk is 80 GB, in which the in-use dev/xvda1 partition takes 40 GB and the additional 40 GB has not been allocated.
- Run the following command to enter parted to allocate the additional space of the system disk to a partition:
parted System disk
In this example, run the following command:
parted /dev/xvda
Information similar to the following is displayed:
[root@ecs-1120 linux]# parted /dev/xvda GNU Parted 3.1 Using /dev/xvda Welcome to GNU Parted! Type 'help' to view a list of commands.
- Enter unit s and press Enter to set the measurement unit of the disk to sector.
- Enter p and press Enter to view the current disk partition style.
Information similar to the following is displayed:
(parted) unit s (parted) p Model: Xen Virtual Block Device (xvd) Disk /dev/xvda: 167772160s Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 2048s 83886079s 83884032s primary ext4
- Enter mkpart and press Enter to create a new 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:
(parted) mkpart Partition type? primary/extended? p File system type? [ext2]? ext4 Start? 83886080 End? 1677722159
- Set the file system type and size for the new partition.
Value 83886080 indicates the start sector of the dev/xvda2 partition you created, and value 167772159 indicates the end sector. The two values are used for reference only. You can determine the number of partitions and partition sizes based on your service requirements.
Information similar to the following is displayed:
(parted) mkpart Partition type? primary/extended? p File system type? [ext2]? ext4 Start? 83886080 End? 1677722159
The file system type may fail to set in this step. Therefore, reconfigure the file system type according to 10 after the partition is created.
The maximum sector can be obtained in either of the following ways:
- Run the fdisk -l command and take note of the value.
- Enter -1s, and the value displayed is the desired value.
- Enter p and press Enter to view the new partition.
Information similar to the following is displayed:
(parted) p Model: Xen Virtual Block Device (xvd) Disk /dev/xvda: 167772160s Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 2048s 83886079s 83884032s primary ext4 2 83886080s 167772159s 83886080s primary
The dev/xvda2 partition is created.
- Enter q and press Enter to exit parted.
- 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/xvda2
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/xvda2.
Information similar to the following is displayed:
[[root@ecs-1120 linux]# mkfs -t ext4 /dev/xvda2 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: 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-1120 linux]# mount /dev/xvda2 /opt [root@ecs-1120 linux]#
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-1120 linux]# df -TH Filesystem Type Size Used Avail Use% Mounted on /dev/xvda1 ext4 43G 8.3G 33G 21% / devtmpfs devtmpfs 885M 0 885M 0% /dev tmpfs tmpfs 894M 0 894M 0% /dev/shm tmpfs tmpfs 894M 18M 877M 2% /run tmpfs tmpfs 894M 0 894M 0% /sys/fs/cgroup tmpfs tmpfs 179M 0 179M 0% /run/user/2000 tmpfs tmpfs 179M 0 179M 0% /run/user/0 tmpfs tmpfs 179M 0 179M 0% /run/user/1001 /dev/xvda2 ext4 43G 51M 40G 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/xvdc1 partition mounted on /mnt/sdc. Because the /dev/xvdc disk only has one partition, this partition is regarded as the partition at the disk end. During the partition re-creation, 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:
lsblk
Information similar to the following is displayed:[root@ecs-1120 sdc]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvda 202:0 0 80G 0 disk ├─xvda1 202:1 0 40G 0 part / └─xvda2 202:2 0 40G 0 part /opt xvdb 202:16 0 350G 0 disk ├─xvdb1 202:17 0 100G 0 part └─xvdb2 202:18 0 200G 0 part xvdc 202:32 0 60G 0 disk └─xvdc1 202:33 0 10G 0 part /mnt/sdc
In the command output, the total capacity of the /dev/xvdc disk is 60 GB, in which the existing partition takes 10 GB. The /dev/xvdc1 partition is at the end of the disk and has been mounted on /mnt/sdc.
View the /dev/xvdc 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, go to 2.
- Run the following command to unmount the partition:
umount /mnt/sdc
- Run the following command to view the unmount result:
lsblk
Information similar to the following is displayed:[root@ecs-1120 linux]# umount /mnt/sdc [root@ecs-1120 linux]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvda 202:0 0 80G 0 disk ├─xvda1 202:1 0 40G 0 part / └─xvda2 202:2 0 40G 0 part /opt xvdb 202:16 0 350G 0 disk ├─xvdb1 202:17 0 100G 0 part └─xvdb2 202:18 0 200G 0 part xvdc 202:32 0 60G 0 disk └─xvdc1 202:33 0 10G 0 part
- Run the following command to enter parted to allocate the additional space of the data disk to a partition:
parted Data disk
In this example, run the following command:
parted /dev/xvdc
Information similar to the following is displayed:[root@ecs-1120 linux]# parted /dev/xvdc GNU Parted 3.1 Using /dev/xvdc Welcome to GNU Parted! Type 'help' to view a list of commands.
- Enter unit s and press Enter to set the measurement unit of the disk to sector.
- 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:
(parted) mkpart Partition type? primary/extended? p File system type? [ext2]? ext4 Start? 83886080 End? 1677722159
- The /dev/xvdc1 partition number is 1. Therefore, enter rm 1 and press Enter to delete the partition.
- Enter p and press Enter to check whether the /dev/xvdc1 partition has been deleted.
Information similar to the following is displayed:
(parted) rm 1 (parted) p Model: Xen Virtual Block Device (xvd) Disk /dev/xvdc: 125829120s Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags
- Enter mkpart opt 2048s 125829119 and press Enter to re-create the partition.
2048 specifies the start sector recorded in 6, and 125829119 specifies the end sector, which must be greater than or equal to the end sector recorded in 6.
Information similar to the following is displayed:(parted) mkpart opt 2048s 125829119s Warning: You requested a partition from 2048s to 125829199s (sectors 2048..125829199). The closest location we can manage is 2048s to 125829036s (sectors 2048..125829036). Is this still acceptable to you? Yes/No? Yes
Enter Yes as prompted to set the end sector.
If the following warning message is displayed, enter Ignore to ignore the performance warning. The warning message will not be displayed if the start sector with the optimal disk performance has been entered. In this example, 2048s is one of such start sectors, so the system does not display the warning message.Warning: The resulting partition is not properly aligned for best performance. Ignore/Cancel? Ignore
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.
- Enter p and press Enter to check whether the /dev/xvdc1 partition has been re-created.
Information similar to the following is displayed:
(parted) p Model: Xen Virtual Block Device (xvd) Disk /dev/xvdb: 125829120s Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 2048s 125829086s 125827039s ext4 opt
The /dev/xvdc1 partition has been re-created.
- Enter q and press Enter to exit parted.
- 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/xvdc1:
e2fsck -f /dev/xvdc1
Information similar to the following is displayed:[root@ecs-1120 linux]# e2fsck -f /dev/xvdb2 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/xvdc1: 11/655360 files (0.0% non-contiguous), 83137/2620928 blocks
- Run the following command to extend the size of the file system on /dev/xvdc1:
resize2fs /dev/xvdc1
Information similar to the following is displayed:[root@ecs-1120 linux]# resize2fs /dev/xvdc1 resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/xvdc1 to 15728379 (4k) blocks. The filesystem on /dev/xvdc1 is now 15728379 blocks long.
- Run the following command to view the disk partition information after the partition re-creation:
Information similar to the following is displayed:
[root@ecs-1120 linux]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvda 202:0 0 80G 0 disk ├─xvda1 202:1 0 40G 0 part / └─xvda2 202:2 0 40G 0 part /opt xvdb 202:16 0 350G 0 disk ├─xvdb1 202:17 0 100G 0 part └─xvdb2 202:18 0 200G 0 part xvdc 202:32 0 60G 0 disk └─xvdc1 202:33 0 60G 0 part
In the command output, the total capacity of the /dev/xvdc disk is 60 GB, in which the additional 50 GB has been allocated to the dev/xvdc1 partition.
- Run the following command to mount the new partition on /mnt/sdc:
mount /dev/xvdc1 /mnt/sdc
- Run the following command to check the correctness of the file system on /dev/xvdc1:
- For the xfs file system
- Run the following command to mount the new partition on /mnt/sdc:
mount /dev/xvdc1 /mnt/sdc
- Run the following command to extend the size of the file system on /dev/xvdc1:
sudo xfs_growfs /dev/xvdc1
- Run the following command to view the disk partition information after the partition re-creation:
Information similar to the following is displayed:
[root@ecs-1120 linux]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvda 202:0 0 80G 0 disk ├─xvda1 202:1 0 40G 0 part / └─xvda2 202:2 0 40G 0 part /opt xvdb 202:16 0 350G 0 disk ├─xvdb1 202:17 0 100G 0 part └─xvdb2 202:18 0 200G 0 part xvdc 202:32 0 60G 0 disk └─xvdc1 202:33 0 60G 0 part
In the command output, the total capacity of the /dev/xvdc disk is 60 GB, in which the additional 50 GB has been allocated to the dev/xvdc1 partition.
- 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
Information similar to the following is displayed:[root@ecs-1120 linux]# mount /dev/xvdc1 /mnt/sdc [root@ecs-1120 linux]# df -TH Filesystem Type Size Used Avail Use% Mounted on /dev/xvda1 ext4 43G 8.3G 33G 21% / devtmpfs devtmpfs 885M 0 885M 0% /dev tmpfs tmpfs 894M 0 894M 0% /dev/shm tmpfs tmpfs 894M 18M 877M 2% /run tmpfs tmpfs 894M 0 894M 0% /sys/fs/cgroup tmpfs tmpfs 179M 0 179M 0% /run/user/2000 tmpfs tmpfs 179M 0 179M 0% /run/user/0 tmpfs tmpfs 179M 0 179M 0% /run/user/1001 /dev/xvda2 ext4 43G 51M 40G 1% /opt /dev/xvdc1 ext4 64G 55M 60G 1% /mnt/sdc
The /dev/xvdc1 partition has been mounted on the /mnt/sdc directory.
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