使用systemd掛載檔案系統

2021-09-25 03:02:56 字數 3094 閱讀 2982

文章目錄

為什麼使用systemd來掛載檔案系統而不使用fstab?使用fstab的時候如果裝置被移除會導致系統啟動失敗,無法進入系統(只能進入臨時修復系統),systemd則沒有這個問題。

一、掛載本地磁碟

#vim /etc/systemd/system/***-***.mount
以下為配置內容:

[unit]

description = mount disk

[mount]

what = /dev/sdb4

where = /mnt/data

type = ext4

options = defaults

[install]

wantedby = local-fs.target

二、掛載網路檔案系統

#vim /etc/systemd/system/***-***.mount
[unit]

description = mount nfs disk

[mount]

what = nfs.example.com:/export/scratch

where = /mnt/data/nfs

type = nfs

options = defaults

[install]

wantedby = multi-user.target

三、自動掛載

#vim /etc/systemd/system/***-***-***.automount
[unit]

description = auto mount nfs disk

[automount]

where = /mnt/data/nfs

[install]

wantedby = multi-user.target

四、開啟關停

# systemctl start ***-***-***.mount

# systemctl stop ***-***-***.mount

# systemctl disable ***-***-***.mount

# systemctl statu ***-***-***.mount

如果使用automount,則必須使用:

# systemctl enable ***-***-***.automount

沒用使用automount,則使用:

# systemctl enable ***-***-***.mount

# systemctl is-enabled mnt-scratch.mount

disabled

# systemctl is-enabled mnt-scratch.automount

enabled

# systemctl start mnt-scratch.automount

# ls /mnt/scratch >/dev/null

# systemctl status mnt-scratch.automount

● mnt-scratch.automount - automount scratch

loaded: loaded (/etc/systemd/system/mnt-scratch.automount; enabled; vendor preset: disabled)

active: active (running) since mon 2016-04-18 10:49:04 cest; 4h 33min ago

where: /mnt/scratch

apr 18 10:49:04 oat systemd[1]: set up automount automount scratch.

apr 18 10:49:14 oat systemd[1]: mnt-scratch.automount: got automount request for /mnt/scratch, triggered by 20266 (zsh)

# systemctl status mnt-scratch.mount

● mnt-scratch.mount - scratch

loaded: loaded (/proc/self/mountinfo; disabled; vendor preset: disabled)

active: active (mounted) since mon 2016-04-18 10:49:16 cest; 4h 33min ago

where: /mnt/scratch

what: nfs.example.com:/export/scratch

apr 18 10:49:14 oat systemd[1]: mounting scratch...

apr 18 10:49:16 oat systemd[1]: mounted scratch.

五、重要事項:

(以下內容非常重要,可能直接導致掛載失敗。)

mount檔名稱必須要與你掛載路徑一致,把路徑裡的「/」換成「 -」,例如本例要掛的目錄為/mnt/data,則檔名稱必須為mnt-data.mount。可以多層目錄如***-***-***.mount

如果掛載nfs的話需要安裝nfs客戶端:

伺服器端:

$sudo apt install nfs-kernel-server

$sudo apt install portmap(如果安裝上乙個,可以不用執行此操作)

客戶端:

#apt install nfs-common

automatic mounts with systemd:

systemd target mount automount介紹:

掛載檔案系統

將乙個檔案系統的頂層目錄掛到另乙個檔案系統的子目錄上,使它們成為乙個整體,稱為掛載。把該子目錄稱為掛載點。注意 1 掛載點必須是乙個目錄。2 乙個分割槽掛載在乙個已存在的目錄上,這個目錄可以不為空,但掛 載後這個目錄下以前的內容將不可用。對於其他作業系統建立的檔案系統的掛載也是這樣。但是需要理解的是...

Linux 掛載檔案系統

1 掛載點必須是乙個目錄。2 乙個分割槽掛載在乙個已存在的目錄上,這個目錄可以不為空,但掛載後這個目錄下以前的內容將不可用。對於其他作業系統建立的檔案系統的掛載也是這樣。但是需要理解的是 光碟 軟盤 其他作業系統使用的檔案系統的格式與linux使用的檔案系統格式是不一樣的。光碟是iso9660 軟盤...

adb 讀寫模式 掛載檔案系統

shell android mount o rw,remount yassf2 system adb shell shell android su sushell android mount o rw,remount yassf2 system chmod 644 常用adb命令 adb 預設不支援...