When we use a VPS host, we often encounter the situation of increasing the hard drive capacity. For example, Alibaba Cloud, Tencent Cloud, and Linode can directly add hard drives. If you use a SolusVM VPS host, you can directly increase the hard drive capacity in SolusVM. . However, after the hard disk capacity is expanded, we still need to manually mount the hard disk partition. Generally speaking, there are two ways to expand the hard disk of a Linux VPS host. One is commonly used, which is to mount the hard disk partition to a certain directory; the other is to directly expand the partition on the original hard disk. The first way to expand the hard drive is simple to operate, but if you want to add more hard drives later, you will face the trouble of adding partitions again. The second method is more suitable for users who frequently expand hard disk capacity. It can achieve dynamic expansion without affecting the original directory structure. This article will share how to use the fdisk command to expand the hard disk capacity of a Linux VPS host. The fdisk command is used to observe the physical usage of the hard disk and also partition the hard disk. It uses a traditional question-and-answer interface, which is a bit cumbersome to operate. More tutorials on Linux VPS host operation are available here:

  1. Solution to Linux system disk space full-No Space Left on Device error
  2. aaPanel Pagoda Free VPS Control Panel International Version - Automatically apply for SSL and multiple PHP versions to coexist
  3. OneDrive turns into an external network disk - Aria2+Aria2Ng+OneIndex automatically uploads to OneDrive
PS: Updated on March 1, 2019, If your hard disk storage is always changing, you can try dynamic expansion to solve the problem of insufficient storage capacity "once and for all": Linux dedicated servers and VPS hosts Dynamic expansion of hard disk - LVM logical volume expansion and reduction method.

1. fdisk command

fdisk command Syntax:
fdisk(选项)(参数)
Options:
-b<分区大小>:指定每个分区的大小;
-l:列出指定的外围设备的分区表状况;
-s<分区编号>:将指定的分区大小输出到标准输出上,单位为区块;
-u:搭配"-l"参数列表,会用分区数目取代柱面数目,来表示每个分区的起始地址;
-v:显示版本信息。
Parameter: Device file: Specify the hard disk device file to be partitioned or displayed. Example: First select the disk to operate on:
[root@localhost ~]# fdisk /dev/sdb
Enter m to list executable commands:
command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)
The command description is as follows:
a toggle bootable flag b edit bsd disklabel c toggle dos compatibility flag d delete partition l list known partition types m print this menu n add a new partition o create a new empty DOS partition table p print partition table q exit without saving changes s Create a new empty Sun disk label t Change the system ID of a partition u Change display/input units v Verify partition table w Write table to disk and exit x Extra features (Professional)

2. Linux VPS hard disk partition

The following operation is to use the fdisk command to access the disk: /dev/vda → View partition → Create a new partition → Set partition to 3 (originally there are already 1 and 2) → View partition. (Click to enlarge)

3. Linux VPS hard disk mounting

Command 1: Format partition
mkfs.ext4 /dev/vda3   //格式化分区(也可先选ext3)
Command 2: Automatically mount when booting
vim /etc/fstab 
join in:
/dev/sdb1(磁盘分区)  /data1(挂载目录-需要提前创建好) ext4(文件格式,也可写ext3)defaults  0  0 
You can also write according to the format of the file you sent, so there will be no mistakes. As shown below: Command 3: Mount all directories
mount -a(注意是英文的-)
Mounted successfully.

4. Dynamic expansion of Linux VPS hard disk

Above, we successfully mounted the new partition to a certain directory on the server through hard disk mounting. There is a problem in that if we want to increase the hard disk later, we have to mount another directory. The operation is simple, but practical. Not strong. After partitioning the hard disk according to the above, enter the command t, and then enter: 8e. At this time, the partition format will be converted to LVM. As shown below: After saving the partition and restarting the VPS, creates a logical volume and logical volume group and activates them. The command is as follows:
pvcreate /dev/vda3  //创建物理卷
vgcreate qyfoutt /dev/vda3   //创建逻辑卷组
vgchange -ay qyfoutt //激活逻辑卷组
vgdisplay qyfoutt | grep "Total PE"  //查看该卷组所有的PE
lvcreate -l 17919 -n qimm qyfoutt  //创建逻辑卷
mkfs.ext4 /dev/qyfoutt/qimm //格式化逻辑卷
Here I create a logical volume group named qyfoutt, with a total of 70GB available for allocation. Then created a logical volume named qimm and assigned 70GB to it. Now we can edit the /etc/fstab file as above, and mount the newly added /dev/qyfoutt/qimm partition to a certain directory. The format is as follows:
/dev/qyfoutt/qimm     /ttfou            ext4    defaults        0 0
Dynamic expansion. If another hard disk is added, here we can add the new hard disk space to the original hard disk. Create a new partition according to the above method (here my new partition is vda4) and set it to LVM format. Use the following methods to expand capacity:
pvcreate /dev/vda4  //创建物理卷
vgextend qyfoutt /dev/vda4  //将新硬盘加入卷组
vgdisplay qyfoutt | grep "Total PE"  //查看所有PE
lvresize -l 30718 /dev/qyfoutt/qimm  //重新规定大小
resize2fs /dev/qyfoutt/qimm //扩容
After restarting, we can see that the hard disk has been successfully expanded based on the original size.

5. Mount multiple directories on the same partition

The method we used above is to mount the partition in one directory. If you want to mount a partition in multiple directories, you can use the bind mount command.
mount /dev/vda4 /data/wwwroot
mount --bind /data/wwwroot /home
In the above command, I first mounted the partition vda4 in the /data/wwwroot directory, and then bind mounted it to the home directory. In this way, both the home directory and the wwwroot directory use the vda4 partition. If you want to cancel the mount, you can use the following command:
umount /home
umount /dev/vda3

6. Summary

If you use the dh and fdisk commands to view the hard disk display differently, it may be partitioned but the capacity has not been expanded to the hard disk. Please execute the resize2fs expansion command. If you find that the partition and hard disk capacity checked by dh and fdisk are consistent, but there are still a lot of free disks, it should be that there are no partitions. Please partition the free hard disk first. If an LVM-formatted partition already exists, you can directly expand the new partition on the LVM-formatted hard disk. Please make a data backup before operating the hard disk. If you are not careful, you will lose the original data. This is how "deleting the database and running away" occurs.

Leave a Reply