Explain how to add hard disk to CentOS on VPS without restarting the server

Time:2020-10-11

For most system administrators, expanding the disk space of Linux server is one of the daily tasks. So this article will use Linux commands to demonstrate some simple steps on CentOS 7 to expand your disk space without having to restart your production server. About expanding and adding new disks to the Linux system, we will mention a variety of methods and feasibility, you can choose the most suitable one according to your needs.

1. Expand the disk space in the virtual machine client:
Before adding a disk volume to a Linux system, you need to add a new physical disk or set it in VMware vshare, VMware Workstation and other virtual environment software you use to increase the capacity of a virtual disk.
2016422121335402.png (697×617)

2. Check disk space:
Run the following command to check the current disk space size.

Copy code

The code is as follows:

# df -h
# fdisk -l

2016422121413072.png (850×632)

As you can see, although we have increased it to 50 GB on the back end, the total disk size is still 10 GB at this time.

3. Expand the space without restarting the virtual machine
Now run the following command. By rescanning the SCSI bus and adding SCSI devices, the system can expand the physical volume disk space of the operating system without restarting the virtual machine.

Copy code

The code is as follows:

# ls /sys/class/scsi_host/
# echo “- – -” > /sys/class/scsi_host/host0/scan
# echo “- – -” > /sys/class/scsi_host/host1/scan
# echo “- – -” > /sys/class/scsi_host/host2/scan

Use the following command to check the name of the SCSI device, and then rescan the SCSI bus.

Copy code

The code is as follows:

# ls /sys/class/scsi_device/
# echo 1 > /sys/class/scsi_device/0\:0\:0\:0/device/rescan
# echo 1 > /sys/class/scsi_device/2\:0\:0\:0/device/rescan

As shown in the figure below, the SCSI bus will be rescanned, and then the disk size we set in the virtual machine client will be displayed normally.
2016422121430023.png (907×718)

4. Create a new partition:
Once you can see the expanded disk space in the system, you can run the following command to format your disk to create a new partition. Follow these steps to expand your physical disk volume.

Copy code

The code is as follows:

# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2) press the ‘m’ key for help
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
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
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)
Command (m for help):

Type ‘p’ to view the current partition table information, and then type ‘n’ to create a new primary partition and select all available sectors. Use the’t ‘command to change the disk type to’ Linux LVM ‘, and then select the code’ 8e ‘or not selected by default. Its default type code is’ 83′.
Now enter ‘W’ to save the partition table information and exit the command environment as follows:

Copy code

The code is as follows:

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)

2016422121447674.png (874×740)

5. Create physical volume:
According to the above prompt, run the ‘partprob’ or ‘kpartx’ command to make the partition table effective, and then use the following command to create a new physical volume.

Copy code

The code is as follows:

# partprobe
# pvresize /dev/sda3

To check the newly created volume, run the following command to see if the new physical volume has been created and available. Next, we can use this new physical volume to expand the ‘CentOS’ volume group, as follows:

Copy code

The code is as follows:

# pvdisplay
# vgextend centos /dev/sda3

2016422121505787.png (719×569)

6. Expand logical volume:
Now we are using the following commands to expand the logical disk space of our system.

Copy code

The code is as follows:

# lvextend -L +40G /dev/mapper/centos-root

Once the success message is returned, you can run the following command to expand your logical volume size.

Copy code

The code is as follows:

# xfs_growfs /dev/mapper/centos-root

The size of the ‘/’ partition has been successfully increased. You can use the ‘DF’ command to check the size of your disk drive. As shown in the figure.
2016422121526415.png (943×465)

7. Expand the root partition by adding new disks without restarting the system
This is the second method, which uses very simple commands to increase the size of logical volume space on CentOS 7 systems.
So the first step is to open the settings page of your virtual machine client, click the “add” button, and then proceed to the next step.
2016422121543596.png (694×627)

Select the required configuration information for the new disk, as shown in the figure below. Select the size and type of the new disk.
2016422121724563.png (647×502)

Then enter the server and repeat the following command to scan your disk device to make the new disk visible in the system.

Copy code

The code is as follows:

# echo “- – -” > /sys/class/scsi_host/host0/scan
# echo “- – -” > /sys/class/scsi_host/host1/scan
# echo “- – -” > /sys/class/scsi_host/host2/scan

List the names of your SCSI devices:

Copy code

The code is as follows:

# ls /sys/class/scsi_device/
# echo 1 > /sys/class/scsi_device/1\:0\:0\:0/device/rescan
# echo 1 > /sys/class/scsi_device/2\:0\:0\:0/device/rescan
# echo 1 > /sys/class/scsi_device/3\:0\:0\:0/device/rescan
# fdisk -l

2016422121738129.png (742×737)

Once the new disks are visible, you can run the following command to create a new physical volume and then add it to the volume group, as shown below.

Copy code

The code is as follows:

# pvcreate /dev/sdb
# vgextend centos /dev/sdb
# vgdisplay

2016422121753550.png (690×736)

Now expand the logical volume based on the space size of this disk and add it to the root partition.

Copy code

The code is as follows:

# lvextend -L +20G /dev/mapper/centos-root
# xfs_growfs /dev/mapper/centos-root
# df -h

2016422121808388.png (986×737)

Conclusion
The process of managing disk partitions on Linux CentOS 7 is very simple. You can use the steps described in this article to expand the disk space of any logical volume. You don’t need to restart the server on the production line, just simply rescan the SCSI devices, and expand the LVM you want (logical volume management). We hope this article will be useful to you. Please feel free to post useful comments and suggestions.

Recommended Today

JS function

1. Ordinary function Grammar: Function function name (){ Statement block } 2. Functions with parameters Grammar: Function function name (parameter list){ Statement block } 3. Function with return value Grammar: Function function name (parameter list){ Statement block; Return value; } Allow a variable to accept the return value after calling the function Var variable name […]