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系統,掛載的目錄就可以正常使用啦!

到此,有關開機自動掛載磁盤的所有內容已完,若還有其它問題可以在下方留言!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM