Several ways to automatically mount the file system on Linux boot

I. Introduction

On Digitalocean, when adding a hard disk to the host, in order to avoid the trouble of partitioning, formatting, mounting, etc., you can choose to let the operator help to complete the above operations automatically.

Looking at the document, we can see that when the operator adds a hard disk, the following script is automatically executed during the mount phase:

1). Create a mount directory.

mkdir -p /mnt/volume

2). Mount the hard disk (temporary mount, it will fail after the host restarts).

mount -o discard,defaults /dev/disk/by-id/scsi-0DO_Volume_volume /mnt/volume

3). Modify /etc/fstab to make the mount persistent, and the hard disk will be automatically mounted after the host restarts.

echo /dev/disk/by-id/scsi-0DO_Volume_volume /mnt/volume ext4 defaults,nofail,discard 0 0 | sudo tee -a /etc/fstab

Then, when you plan to modify the mount directory /mnt/volume to /mnt/data as required, you only need to create a new mount directory and modify the relevant content of /etc/fstab:

/dev/disk/by-id/scsi-0DO_Volume_volume /mnt/data ext4 defaults,nofail,discard 0 0

Very convenient.

It was indeed possible to do this a few years ago. However, during this actual operation, it was found that there was no corresponding mount configuration in the /etc/fstab file.

Could it be that the operator didn't make a persistent mount, but restarted the host and found that the mount is persistent. When forcibly adding the mount configuration to the /etc/fstab file:

/dev/disk/by-id/scsi-0DO_Volume_volume /mnt/data ext4 defaults,nofail,discard 0 0

After restarting the host, you will find that the hard disk is mounted to two directories at the same time:

/mnt/volume

/mnt/data

This incidates that:

1). The description of the official document about auto-mounting the hard disk has expired.

2). Linux also has other ways to automatically mount hard drives.

2. Several ways to automatically mount the hard disk when Linux is turned on

1. /etc/fstab configures the hard disk to be automatically mounted.

/etc/fstab is the most conventional way to manage the automatic mounting of hard disks, so this article will not repeat them.

2. Develop an initialization script to mount the hard disk.

The /etc/rc.local file is essentially a shell script file, which can be used to add startup commands, and the commands in it will be executed in order when the system starts.

Therefore, the mount command can be directly:

mount -o discard,defaults /dev/disk/by-id/scsi-0DO_Volume_volume /mnt/volume

Write /etc/rc.local, so as to realize the automatic mounting of the hard disk.

3. Use Systemd to automatically mount the hard disk.

Linux systemd, not only can be used to close and open services, but also provides a new way to handle mounting and auto-mounting file systems, that is, to manage file system mounting as a service.

Systemd expands the definition of fstab files, and reasonable use of the mount options provided by Systemd can perfectly solve some pain points when using /etc/fstab in the past.

1). systemd.mount can allow those file systems that require the network to be available to be mounted only when the network is already available, and can also define the timeout period for waiting for the network to be available, so as to avoid long-term stuck during the boot process.

2). systemd can make a certain mount item automatically start to mount and automatically uninstall, instead of being permanently mounted in the background after being mounted at boot time.

3). systemd can make the system skip the failed mount, avoiding the failure of the system to start due to the hard disk mount problem, and the system cannot be entered (only the temporary repair system can be entered).

Currently, the hard disk automatic mounting service provided by Digitalocean uses the Systemd mounting method. Check the /etc/systemd/system directory to see a file named mnt-volume.mount:

/etc/systemd/system/mnt-volume.mount

This file defines a mount item:

[Unit]

Description=Mount DO Volume volume



[Mount]

What=/dev/disk/by-uuid/d946870c-ef31-48ee-a9f1-446acaa56f46

Where=/mnt/volume

Options=defaults,nofail,discard,noatime

Type=ext4



[Install]

WantedBy = multi-user.target

With this mount method, we can manage mounts like a service:

systemctl status mnt-volume.mount
systemctl enable mnt-volume.mount
systemctl disable mnt-volume.mount
systemctl start mnt-volume.mount
systemctl stop mnt-volume.mount

Systemd's file mounting options are very rich, and the details will not be described in this article.

4. Manage hard disk mounting through AutoFs service.

The difference between the AutoFs service program and the mount command is that it is a daemon process, which automatically detects and mounts the file system only when it detects that a user tries to access a file system that has not yet been mounted.

Autofs is very convenient, there are two main points:

1). Set the directory that does not have to be mounted when booting, and it will be automatically mounted when the user accesses the corresponding file system.

2). After the user does not use the automatically mounted directory for a period of time, the file system will be automatically unmounted (the default time is 5 minutes).

AutoFs is a third-party software that requires additional installation:

yum install -y autofs

The specific usage method of AutoFs is also not described in detail in this article.

Guess you like

Origin blog.csdn.net/Dancen/article/details/131236109