Detailed explanation and practical steps of Linux mounting disk

Mounting a disk in Linux is an important operation, which can be used to expand storage space, manage data files, backup and store important files, etc. This article will introduce in detail the concepts, steps and practical operation guidelines for mounting disks in Linux systems.

1. Disk partition

In Linux, disks are used through partitions. Partition is used to divide a hard disk into several logical parts, and different file systems can be stored in each partition. Therefore, before mounting the disk, we need to partition the disk first. The disk partitioning process can be done through command line tools or GUI tools.

Commonly used disk partitioning tools are:

  1. cfdisk: command line tool, suitable for creating primary partitions and extended partitions, supports MBR and GPT partition tables.

  2. fdisk: command line tool, suitable for creating primary partitions and extended partitions, supports MBR partition table.

  3. gdisk: A command-line tool, suitable for creating primary partitions, extended partitions, and logical partitions, and supports GPT partition tables.

  4. gparted: Graphical interface tool, suitable for creating primary partitions, extended partitions and logical partitions, supporting MBR and GPT partition tables.

The purpose of partitioning is to divide a hard disk into multiple logical parts, each part can be used and managed independently. Of course, partitioning is not necessary, we can also directly mount the entire hard disk as a single partition.

2. File system

In Linux, the file system is an important concept for mounting disks. A file system refers to a way of organizing data that helps us store and manage files on disk. Common Linux file systems include ext2, ext3, ext4, XFS, etc.

Before mounting the disk, we need to create a file system on the disk. The command to create a file system is mkfs.

For example, to create an ext4 filesystem, just run the following command:

mkfs.ext4 /dev/sdb1

Among them, "/dev/sdb1" indicates the partition where the file system is to be created.

3. Mount the disk

In Linux, the command to mount a disk is mount. Before mounting the disk, we need to ensure that the file system has been created, and we need to know the device and mount point to be mounted.

A device refers to a physical disk or partition to be mounted, and its identifier usually starts with /dev, followed by partition information (such as /dev/sda1).

The mount point refers to the directory where the device is to be mounted, and this directory is called the mount point. A mount point is usually an empty directory that must exist in the file system to be used as a mount point.

For example, if we want to mount an ext4 file system to the /mnt/data directory, we only need to run the following command:

mount /dev/sdb1 /mnt/data

Here "/dev/sdb1" is the device to be mounted, and "/mnt/data" is the mount point.

4. Automatically mount at startup

In Linux, if you want the disk to be automatically mounted to the specified directory after each boot, we need to write the mount information into the /etc/fstab file.

/etc/fstab is a file used to store file system mount information in the Linux system. It contains information about all file systems that need to be mounted when the system starts, and the options used when mounting.

We need to add a new line to the /etc/fstab file describing the file system we want to mount. Each line contains the following information:

  1. Device: The name of the device to be mounted, such as /dev/sdb1.

  2. Mount point: Which directory to mount the device to.

  3. File system type: the type of file system to be mounted, such as ext4.

  4. Mount options: options to control the mount behavior, such as noatime (do not update access time) and defaults (use default options), etc.

  5. Mounting order: In what order should the system mount the file system, for example, 0 means to mount first.

For example, we want to mount the device /dev/sdb1 to the /mnt/data directory, and use the ext4 file system type, and use the default option and noatime option when mounting. We need to add the following lines in the /etc/fstab file:

/dev/sdb1 /mnt/data ext4 defaults,noatime 0 0

In this way, after each boot, the system will automatically mount /dev/sdb1 to the /mnt/data directory, and apply the mount options we specified.

5. Unmount the disk

In Linux, the command to unmount a disk is umount. Before unmounting a disk, you need to check whether there is a process on the disk using it. If so, the corresponding process needs to be terminated before the disk can be unmounted safely.

For example, to unmount the file system under the /mnt/data directory, just run the following command:

umount /mnt/data

6. Practical operation

1. Use the lsblk command to view the newly added disk information

$ lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   30G  0 disk 
├─sda1   8:1    0  512M  0 part /boot/efi
├─sda2   8:2    0    8G  0 part [SWAP]
└─sda3   8:3    0 21.5G  0 part /
sdb      8:16   0 100G  0 disk 

It can be seen from the above results that a new disk device /dev/sdb has been added to the system.

2. Use fdisk or cfdisk to partition the new disk, and mark the partition as a Linux file system type (83)

For example using the fdisk command:

$ sudo fdisk /dev/sdb 

Command (m for help): n  
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p  
Partition number (1-4, default 1): 1 
First sector (2048-209715199, default 2048):   
Last sector, +sectors or +size{
    
    K,M,G,T,P} (2048-209715199, default 209715199):   

Created a new partition 1 of type 'Linux' and of size 100 GiB.

Command (m for help): t
Selected partition 1
Partition type (type L to list all types): 83 

Changed type of partition 'Linux' to 'Linux'.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

3. Format the new partition, use the mkfs command

$ sudo mkfs -t ext4 /dev/sdb1
mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 26214400 4k blocks and 6553600 inodes
Filesystem UUID: 882b7297-1258-4e23-9d03-cf20654455b9
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (131072 blocks): done
Writing superblocks and filesystem accounting information: done 

4. Create a mount directory and use the mkdir command

$ sudo mkdir /mnt/data

5. To mount the partition, use the mount command

$ sudo mount /dev/sdb1 /mnt/data

6. Verify that the mount is successful, and use the df -h command to view the storage usage of all file systems:

$ sudo df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            3.9G     0  3.9G   0% /dev
tmpfs           793M  1.7M  792M   1% /run
/dev/sda3        22G  1.5G   19G   8% /
tmpfs           3.9G     0  3.9G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/sda1       511M  4.0K  511M   1% /boot/efi
tmpfs           793M     0  793M   0% /run/user/1000
/dev/sdb1        99G   72M   94G   1% /mnt/data

It can be seen from the above results that the new partition has been successfully mounted to the /mnt/data directory.

7. Add automatic mount, edit /etc/fstab file, add the following content:

$ sudo su
$ echo '/dev/sdb1    /mnt/data    ext4    defaults    0    2' >> /etc/fstab 

7. Summary

Mounting a disk is a basic operation in the Linux system, which can help us expand storage space, manage data files, backup and store important files, etc. Before mounting the disk, we need to partition the disk and create a file system on the partition. Then, we need to use the mount command to mount the device to the specified directory, and if necessary, use the /etc/fstab file to configure automatic mounting at boot. Finally, before unmounting the disk, you need to check whether any process is using the disk and terminate the corresponding process.

Guess you like

Origin blog.csdn.net/u012581020/article/details/131244002