Skip to content

Mount Disk in Linux on Boot

Bref

开机自动挂载硬盘

1. Check Partition.

在挂载磁盘前,需要知道每个磁盘的路径或UUID,此时主要依靠fdiskblkid两个指令,前一个指令获取分区信息,后一个指令获取Block device块设备的属性。

$ sudo blkid
/dev/sda1: LABEL="Win10" UUID="C4A0E65EA0E65708" TYPE="ntfs" PARTUUID="6190c592-01"
/dev/sda2: UUID="AE3C137D3C133FAF" TYPE="ntfs" PARTUUID="6190c592-02"
/dev/sdb1: LABEL="Apps" UUID="0000678400004823" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="45ed07d2-c9e3-4167-8868-3e33f62784e1"
/dev/sdb2: LABEL="Data" UUID="0000678400004823" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="2a8b4bf1-8357-4d65-bed4-f73f01b96431"
/dev/sdb3: LABEL="Backup" UUID="0000678400004823" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="a7187083-2820-4eef-83ff-05d8899879a2"
从上面的输出结果就可以将磁盘对应到具体的块设备路径/dev/sdax,因为电脑含两块磁盘,所以包含sdasdb。这里的块设备路径和UUID会将下面的配置参数中用到。

2. Temporary Mount

对于临时解决方案,可以使用以下指令将/dev/sda1挂载到/mnt/c目录

sudo mkdir /mnt/c
sudo mount -t ntfs /dev/sda1 /mnt/c

3. Auto Mount on Boot

(1). 修改配置文件

开机启动后自动挂载的方案就需要修改配置文件/etc/fstab,使用编辑器打开后,按以下格式添加新的挂载项

$ cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sdb5 during installation
UUID=4d2e5a83-d9fe-47bb-aa02-fee9ac8535e3 /               ext4    errors=remount-ro 0       1
# /boot was on /dev/sdb6 during installation
UUID=9b521dc2-ad85-4ca6-bb8b-f285b312aa49 /boot           ext4    defaults        0       2
# /home was on /dev/sdb7 during installation
UUID=f6b796dd-9e59-4f79-b70a-c2c8e5977163 /home           ext4    defaults        0       2
# swap was on /dev/sdb4 during installation
UUID=9430ea0e-7ff9-4167-9bf7-1f3e08d26d34 none            swap    sw              0       0
# /mnt/Data was on /dev/sdb2 during installation
/dev/sdb2                                /mnt/Data        ntfs    defaults        0       2
# /mnt/Backup was on /dev/sdb3 during installation
/dev/sdb3                                /mnt/Backup      ntfs    defaults        0       2

后面两项就是用来挂载磁盘的,当然每行前面的/dev/sdbx也可以换成UUID=xxxxx,但是由于这两个磁盘的UUID一致,所以可能会有问题,也许可以使用blkid输出的PARTUUID

(2). 挂载测试

sudo mkdir /mnt/{Data,Backup}
sudo mount -a

Mount SMB/CIFS

利用systemd 开机自动挂载smb

# This is a sample service script to mount CIFS/SAMBA shares.
# Please read carefully the comments in this file. For production usage
# you can remove all comments (lines beginning with "#") from this file.


[Unit]
# The description should be used to explain what this servicefile is for
Description=nas cifs mount home

# if we do network mounts like here we *require* 'network-online.service'
# which checks if the network is online
Requires=network-online.target

# our scripts must start *after* 'network-online.service', on timeout and if
# 'network-online.service' fails we can not mount and this scripts fails too
After=network-online.target

# usually we mount networks shares because we want they avaible *before* XBMC starts.
# so XBMC has access to this mounts from beginning. Note: this slows down the boot!
#Before=kodi.service


[Mount]
# The share we want mount
What=//IP_ADDRESS/Data

# Where we want mount this share
Where=/PATH_TO_MOUNT

# Any options you usually use with the "-o" parameter in the mount command
Options=username=haoran.qi,password=password,rw,vers=3.0,dir_mode=0775,file_mode=0664

# filesystem type
Type=cifs

[Install]
WantedBy=multi-user.target

REF

[1]. https://www.litreily.top/2019/02/17/mount/