4

Recently, I will write a few articles for my friends about "How to restore files after accidentally deleted files under linux". For data recovery of files that are not occupied by processes, different file system formats need to use different tools, such as: ext4, xfs, etc. I searched all my virtual machine servers and couldn't find the ext4 file format. Because ext4 is still a very common file system format after all, I want to be able to be more systematic when writing things, so this article first introduces: how to mount a new hard disk for the linux operating system and format the new hard disk into ext4 format.

1. Create a new hard disk or install a hard disk

A hard disk can be divided into multiple partitions through tools, but in Linux operation and maintenance, usually a new hard disk only needs one partition. Each partition in the hard disk needs to establish a mount relationship with a file directory in the Linux operating system. Subsequent read and write operations for files in this directory are actually file read and write operations for the disk.
For the operation of installing the hard disk in this step, if it is a real server, just install the hard disk on the rack of the server. For the virtual machine, we need to create a new hard disk and plan the hard disk space. So the following operations are only for virtual machines, I use virtualbox virtual machine, other virtual machines are similar. Virtual Machine Settings -> Storage -> Controller SATA -> Add New Hard Disk -> Create New Virtual Disk.

Then select the format, size, and storage path of the virtual disk according to the prompts. Note that the format of the virtual disk is the file storage format of the virtual machine, which has nothing to do with the file format of the Linux operating system. After the creation is completed, a new hard disk will be added under the controller SATA.

2. Add a hard disk partition

Next, let's mount the hard disk to the specified directory. This step does not distinguish whether it is a virtual machine or a server. It is the same operation. We use the command lsblk -f to check the hard drive letter that the operating system can recognize now. We can see that the hard disk with the sda drive letter is the hard disk when the operating system is installed, including swap, boot partition, etc.

Note that the hard disk with the red part of the sdb drive letter is our newly installed hard disk. Let's partition the sdb hard disk. The command for disk partition is fdisk /dev/sdb , sdb is the new partition drive letter we mentioned above. After the operation is completed as shown in the figure, the new hard disk has completed the partition operation, and a hard disk has only one partition. Through lsblk -f to check the hard disk partition again, we see that there is an extra partition sdb1 under the sdb hard disk, which proves that our partition operation is completed correctly.

The only difference is: the old hard disk partition has a UUID and mount directory MOUNTPOINT, but our new hard disk partition does not have one. We need to complete the formatting and directory mounting of the hard disk partition below.

3. Formatting hard disk partitions and mounting directories

Next, let's format the partition. The formatting operation is relatively simple. Just execute the mkfs -t ext4 /dev/sdb1 command, where sdb1 is our newly created partition, and ext4 is a hard disk storage format supported by the Linux operating system.

 [root ~]# mkdir -p /data;        #新建一个目录用于挂载新硬盘
[root ~]# mount /dev/sdb1 /data;   #挂载新硬盘到目录
[root ~]# lsblk -f
NAME            FSTYPE      LABEL UUID                                   MOUNTPOINT
sdb                                                                      
└─sdb1          ext4              0f75f926-222b-4385-84ac-8c69c613aa0a   /data

After the formatting is complete, we need to mount the formatted partition to a directory in the Linux operating system. After all operations are completed, we use lsblk -f to view the partition, we can see that we have newly added the hard disk sdb, created a new partition sdb1, and completed the sdb1 partition format. After the sdb1 partition has its own UUID, the formatted The storage format is ext4, and there is a MOUNTPOINT after the mount operation is completed. In the future, the read and write operations for the /data directory under this operating system are actually performed for the hard disk sdb we just added.

Fourth, it does not fail after restarting

The mount relationship completed above will become invalid after the operating system restarts. If you want to automatically mount the next time the operating system restarts, you need to add the following line to the /etc/fstab file to achieve automatic mounting.

 /dev/sdb1  /data  ext4 defaults 0 0

Executing the following command will reload the configuration file and make it effective:

 mount -a

Recommended reading

Limited to the length of the blog post, I will not list more exciting content one by one, it is recommended to read and watch

"Original high-quality video and supporting documents: springboot-recorded 97 sections (free)"


字母哥博客
933 声望1.5k 粉丝