How can I add a hard drive and expand the root partition on CentOS?
To add a hard drive and expand it to the root partition in a CentOS system, follow these steps:
- Firstly, insert the new hard drive into the server, and use the following command to view the disk device information:
lsblk
Find the name of the newly added hard drive device, such as /dev/sdb.
- Partition and format the new hard drive using the following command.
fdisk /dev/sdb
Type in ‘n’ to create a new partition, choose the default option to create the new partition. Then type in ‘w’ to save the changes and exit.
- Format the new partition.
mkfs.ext4 /dev/sdb1
- Mount a new partition to a temporary directory.
mkdir /mnt/newdisk
mount /dev/sdb1 /mnt/newdisk
- Copy the root partition to a new partition.
rsync -avx / /mnt/newdisk
- The file located at /etc/fstab
/dev/sdb1 / ext4 defaults 1 1
- Remount the root partition:
umount /
mount -a
- Use the following command to check if the root partition has been resized:
df -h
Please make sure to back up important data before proceeding with the above operations to prevent any unexpected situations.