Loading

Ubuntu开机自动挂载磁盘,以及挂载的磁盘为只读模式的解决方案

1、挂载命令(mount)

首先简单的介绍一下Linux的挂载命令mount,也是在正常使用情况下的挂载磁盘的命令,其参数也有很多,这里简单的介绍一下。

平常使用到的命令格式:
mount [-t vfstype] [-o options] device dir
[e.g.]: mount -t ntfs -o remount,rw /dev/sdb1 /media/imaginemiracle/Disk
参数简介:
<1> [-t typedef]:用来指定文件系统的类型,若不指定则由mount命令自动选择正确的类型。
       常见类型: (格式:[文件系统类型 : 命令参数])
           [光盘或光盘镜像 : is09660];
           [DOS fat16 : msdos];
           [Windows 9x fat32 : vfat];
           [Windows NT ntfs : ntfs];
           [Mount Windows Samba : smbfs];
           [UNIX(LINUX) nfs : nfs]。
<2> [-o options]:主要指定挂载方式。
       常见参数:
           [loop]: 把目标作为硬盘分区挂载到系统;
           [ro]: 挂载的目标为只读模式;
           [rw]: 挂载的目标为读写模式;
           [iocharset]: 指定访问文件系统所用的字符集。
<3> <device>: 要挂载的设备(挂载的设备)。
<4> <dir>: 挂载设备的挂载点(一个目录文件)。

2、开机挂载磁盘设置

使用fdisk命令查看系统当前存在的磁盘:

imaginemiracle@:~$ sudo fdisk -l
......
......
......
Disk /dev/sdb: 931.53 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: WDC WD10SPCX-24H
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt 
Disk identifier: 75AEEA26-7630-4F41-BB5F-7B6FD7213F00

Device         Start        End    Sectors   Size Type
/dev/sdb1       2048  614399999  614397952   293G Microsoft basic data
/dev/sdb2  614402048 1953521663 1339119616 638.6G Microsoft basic data


Disk /dev/loop8: 218.102 MiB, 229629952 bytes, 448496 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop9: 62.9 MiB, 65105920 bytes, 127160 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes 

找到需要挂载的设备,这里用我自己的例子来说明,这里我需要开机挂载/dev/sdb1/dev/sdb2。需要在/etc/fstab中添加开机挂载的设备。

2.1、/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/nvme0n1p7 during installation
UUID=3a5fa27f-338c-4c53-b435-87efe35ce710 /               ext4    errors=remount-ro 0       1
# /boot/efi was on /dev/nvme0n1p2 during installation
UUID=5406-F734  /boot/efi       vfat    umask=0077      0       1
/swapfile                                 none            swap    sw              0       0

打开/etc/fstab文件可以看到其中有介绍添加挂载设备的格式:(具体参数介绍可参照Ubuntu Manpage:fstab)

<file system>  <mount point>  <type>  <options>  <dump>  <pass>
<1> <file system>: 根据`fstab`的默认配置,这里只需要填写磁盘的`UUID`即可;(可以通过`blkid`命令查看磁盘的`UUID`)
<2> <mount point>: 磁盘的挂载点;(`注:` 必须是绝对路径,且文件事先存在,在设置前创建好即可)
<3> <type>: 文件系统类型,常用的有`ext4`、`ntfs`、`fat`、`vfat`等,根据实际情况设置;(可以通过`blkid`命令查看磁盘的`TYPE`)
<4> <options>: 
	     default:		使用默认选项: rw, suid, dev, exec, auto, nouser, async
	     noauto:        当设置`mount -a`时(例如:在系统boot的时候),noauto将不会挂载
	     user:			允许用户挂载
	     owner:         允许设备所有者挂载
	     comment or x-: 供`fstab`维护程序使用
	     nofail:        不报告该设备的错误(如果该设备不存在)
<5> <dump>: 该字段用于检查这些文件系统哪个需要转储。若该字段值为`0`,返回并转储将那些那些不需要转储的文件系统
<6> <pass>: 使用此字段来设置`fsck`程序在重新引导时扫描文件系统顺序。根文件系统`/`该字段设置为`1`,其它的文件系统逐渐递增。若该字段不存在或其值为`0`,则`fsck`不做检查。

2.2、查看UUID和TYPE

imaginemiracle:~$ sudo blkid /dev/sdb1
/dev/sdb1: LABEL="Disk" UUID="900A49470A492C12" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="ead7709a-28c9-4c8c-9b95-b29f1654fc13"
imaginemiracle:~$ sudo blkid /dev/sdb2
/dev/sdb2: LABEL="Disk" UUID="0086A1C386A1BA14" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="1a81d012-502b-4516-abd5-2ee01bb631fc"

2.3、配置/etc/fstab

将需要挂载的设备相应信息添加到/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/nvme0n1p7 during installation
UUID=3a5fa27f-338c-4c53-b435-87efe35ce710 /               ext4    errors=remount-ro 0       1
# /boot/efi was on /dev/nvme0n1p2 during installation
UUID=5406-F734  /boot/efi       vfat    umask=0077      0       1
/swapfile                                 none            swap    sw              0       0

UUID=900A49470A492C12 /media/imaginemiracle/Disk_D ntfs defaults 0 2 
UUID=0086A1C386A1BA14 /media/imaginemiracle/Disk_E ntfs defaults 0 2 

2.4、重启系统

在设置完/etc/fstab且保存退出后,重启操作系统,就可以看到在设置的挂载点下已经自动挂载了设备。

3、自动挂载设备为只读模式(Read-Only)的解决方案

在挂载点(挂载的目录下),对其中的文件做修改操作,或是新建文件/目录都会报错,错误信息如下:

Error mounting /dev/sdb1 at /media/imaginemiracle/Disk-D: Command-line `mount -t "ntfs" -o "uhelper=udisks2,nodev,nosuid,uid=1000,gid=1000,dmask=007
Failed to mount '/dev/sdb1': Operation not permitted
The NTFS partition is in an unsafe state. Please resume and shutdown
Windows fully (no hibernation or fast restarting), or mount the volume
read-only with the 'ro' mount option.

由于笔者安装是的windows10+Ubuntu20.04双系统,而两个分区是windows下的,可以看得出来windows的磁盘似乎出了些问题,这里尝试使用ntfsfix修复。
若没有修复工具可以使用以下命令进行安装:

apt-get install ntfs-3g

对磁盘进行修复:(注: 因为是在自己已经修复之后写的博客,所以下面的修复工具未检测出问题,大家仅参考ntfsfix的命令使用即可

#对`/dev/sdb1`开始修复
imaginemiracle:imaginemiracle$ sudo ntfsfix /dev/sdb1                                                                                                 
Mounting volume... OK
Processing of $MFT and $MFTMirr completed successfully.
Checking the alternate boot sector... OK
NTFS volume version is 3.1.
NTFS partition /dev/sdb1 was processed successfully.

#对’/dev/sdb2‘开始修复
imaginemiracle:imaginemiracle$ sudo ntfsfix /dev/sdb2
Mounting volume... OK
Processing of $MFT and $MFTMirr completed successfully.
Checking the alternate boot sector... OK
NTFS volume version is 3.1.
NTFS partition /dev/sdb2 was processed successfully.

若这一步结束,该问题还没有解决,则重启系统进入windows,将电源选项中的快速启动关闭即可。
windows操作流程: 进入控制面板 > 电源管理 > 选择关闭盖子的功能 > 更改不能更改的选项 > 取消快速启动前的勾勾 > 保存。
接下来就是重启,进入linux系统,挂载的目录就可以正常使用啦!

到此,有关开机自动挂载磁盘的所有内容已完,若还有其它问题可以在下方留言!

posted @ 2021-02-01 14:14  Imagine_Miracle  阅读(2085)  评论(0编辑  收藏  举报