udisks

出自 Arch Linux 中文维基

udisks 提供了 udisksd 守護進程,它實現了用於查詢和管理存儲設備的 D-Bus 接口;還提供了一個命令行工具 udisksctl,用於查詢和使用該守護進程。

安裝[編輯 | 編輯原始碼]

有兩個版本的 udisks,分別稱為 udisksAURudisks2。為了集中精力開發udisks2udisks 的開發已終止 。[1]

(udisks2的) udisksd(8) 和 (udisks的) udisks-daemon 都是由 D-Bus 在後台啟動,不應該被顯式地啟用。可以通過 udisksctl(1)udisks(1) 以命令行方式分別進行管控。

配置[編輯 | 編輯原始碼]

權限[編輯 | 編輯原始碼]

用戶通過 udisks 可執行的動作由 Polkit 控制。如果會話不活躍或不存在,例如通過 systemd 用戶服務 控制 udisks 時,需要手動配置 policykit.

這裡 包含 storage 群組的 udisk 配置, 這裡有一個更嚴格的版本。

默認掛載選項[編輯 | 編輯原始碼]

It is possible to define default mount options in /etc/udisks2/mount_options.conf. Create the file if it doesn't already exist. The built-in defaults and some examples can be seen in /etc/udisks2/mount_options.conf.example.[2]

The options can target specific filesystem types. For example, mount btrfs filesystems with zstd compression enabled:

[defaults]
btrfs_defaults=compress=zstd
注意: Lines override the corresponding built-in defaults. Make sure not to accidentally remove mount options this way.

使用[編輯 | 編輯原始碼]

要手動掛載移動設備 /dev/sdc:

$ udisksctl mount -b /dev/sdc1

卸載:

$ udisksctl unmount -b /dev/sdc1

參考 udisksctl help

提示與技巧[編輯 | 編輯原始碼]

掛載助手[編輯 | 編輯原始碼]

通過 udisk 輔助程序也可以實現掛載,請參考 List of applications#Mount tools

注意: GNOMEKDE桌面環境 也提供了 udisk 輔助程序。
  • bashmount — A bash script to mount and manage removable media as a regular user with udisks2.
https://github.com/jamielinux/bashmount || bashmountAUR
  • dolphin-plugins — uses udisksctl to either mount or unmount the iso in Dolphin.
https://apps.kde.org/dolphin_plugins/ || dolphin-plugins
  • udiskieudisks2 automounter with optional notifications, tray icon and support for password protected LUKS devices. See the udiskie wiki for details
https://github.com/coldfix/udiskie || udiskie
  • udiskie-dmenu — dmenu interface for udiskie.
https://github.com/fogine/udiskie-dmenu || udiskie-dmenu-gitAUR
  • udisksvm — GUI udisks2 wrapper written in Python3 and using the Qt5 framework. It uses mouse clicks to mount, unmount removable devices or eject a CD/DVD. See the README file for details.
https://github.com/berbae/udisksvm || udisksvmAUR
  • udevil — Includes devmon, which is compatible to udisks and udisks2.
https://github.com/IgnorantGuru/udevil || udevilAUR
注意: devmon only uses udisks or udisks2 for mounting (in this order) if udevil or pmount miss the SUID permission. To remove this permission, run chmod -s /usr/bin/udevil as root.

禁止隱藏設備(udisks2)[編輯 | 編輯原始碼]

Udisks2 在默認情況下會隱藏一些設備,如果不希望隱藏,可以將 /usr/lib/udev/rules.d/80-udisks2.rules 複製到 /etc/udev/rules.d/80-udisks2.rules 並刪除不需要隱藏的設備:

# ------------------------------------------------------------------------
# ------------------------------------------------------------------------
# ------------------------------------------------------------------------
# Devices which should not be display in the user interface
[...]

掛載到 /media[編輯 | 編輯原始碼]

默認情況下, udisks2 在 ACL 控制下將可移動設備掛載到 /run/media/$USER/ 目錄下。如果你希望改為掛載到 /media 目錄下,應用這條規則:

/etc/udev/rules.d/99-udisks2.rules
# UDISKS_FILESYSTEM_SHARED
# ==1: mount filesystem to a shared directory (/media/VolumeName)
# ==0: mount filesystem to a private directory (/run/media/$USER/VolumeName)
# See udisks(8)
ENV{ID_FS_USAGE}=="filesystem|other|crypto", ENV{UDISKS_FILESYSTEM_SHARED}="1"

掛載 loop 設備[編輯 | 編輯原始碼]

要掛載 ISO 鏡像,使用下面命令:

$ udisksctl loop-setup -r -f image.iso

這條命令會創建 loop 設備並顯示可以掛載的 ISO 鏡像,卸載後,loop 設備會被 udev 刪除.

提示:This mounts a read only image. To mount raw disk images, such as for QEMU, remove the -r flag, and release the image after use with udisksctl loop-delete -b /dev/loop0. Substitute /dev/loop0 with the name of the loop device.

隱藏選中的分區[編輯 | 編輯原始碼]

如果要在桌面中隱藏某些分區或設備,可以創建類似下面的 udev 規則 /etc/udev/rules.d/10-local.rules:

KERNEL=="sda1", ENV{UDISKS_IGNORE}="1"
KERNEL=="sda2", ENV{UDISKS_IGNORE}="1"

僅顯示 sda1sda2 之外的分區。

Because block device names can change between reboots, it is also possible to use UUIDs to hide partitions or whole devices:

For example:

# blkid /dev/sdX
/dev/sdX: LABEL="Filesystem Label" UUID="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXX" UUID_SUB="YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYYY" TYPE="btrfs"

Then the following line can be used:

ENV{ID_FS_UUID}=="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXX", ENV{UDISKS_IGNORE}="1"

The above line is also useful to hide multi device btrfs filesystems, as all the devices from a single btrtfs filesystem will share the same UUID across the devices but will have different SUB_UUID for each individual device.

Apply ATA settings[編輯 | 編輯原始碼]

在啟動和連接驅動器時,udisksd將應用存儲在文件 /etc/udisks2/IDENTIFIER.conf 中的配置,其中 IDENTIFIER 是驅動器的Drive:Id屬性的值。 對於支持ATA設置,有關可用選項,請參閱udisks(8)。 這些設置與hdparm的設置基本上具有相同的效果,但是只要udisks守護程序自動啟動,它們就可以保留。

例如,要將一個驅動器的standby超時設置為240(20分鐘),請添加以下內容:

/etc/udisks2/DriveId.conf
[ATA]
StandbyTimeout=240

要獲取驅動器的DriveId,請使用udevadm info --query=all --name=sdx | grep ID_SERIAL | sed "s/_/-/g"

或者,使用GUI程序來管理配置文件,例如gnome-disk-utility

udevadm monitor[編輯 | 編輯原始碼]

可以使用 udevadm monitor 監測塊設備事件並在新的塊設備被創建時進行掛載。無用的掛載點會被 udisksd 自動刪除,所以刪除時不需要額外動作。

#!/bin/bash

pathtoname() {
    udevadm info -p "/sys/$1" | awk -v FS== '/DEVNAME/ {print $2}'
}

while read -r _ _ event devpath _; do
        if [[ $event == add ]]; then
            devname=$(pathtoname "$devpath")
            udisksctl mount --block-device "$devname" --no-user-interaction
        fi
done < <(stdbuf -o L udevadm monitor --udev -s block)

排錯[編輯 | 編輯原始碼]

隱藏設備[編輯 | 編輯原始碼]

默認情況下,Udisks2對用戶隱藏某些設備。如果這是不希望的或有其他問題,將/usr/lib/udev/rules.d/80-udisks2.rules複製到/etc/udev/rules.d/80-udisks2.rules,並刪除以下內容:

# ------------------------------------------------------------------------
# ------------------------------------------------------------------------
# ------------------------------------------------------------------------
# Devices which should not be display in the user interface
[...]

Broken standby timer[編輯 | 編輯原始碼]

The udisks daemon polls S.M.A.R.T. data from drives regularly. Hard drives with a longer standby timeout than the polling interval may fail to enter standby. Drives that are already spun down are usually not affected. There seems no way to disable polling or change the interval as for udisks2 by now. See [3], [4].

However, Standby timeout applied by udisks2 seems to be unaffected. To set standby timeout via udisks, see #Apply ATA settings (udisks2).

Other possible workarounds could be setting the timeout below the polling interval (10 minutes) or forcing a manaul spindown using hdparm -y /dev/sdx.

NTFS掛載失敗[編輯 | 編輯原始碼]

如果掛載ntfs分區失敗並顯示以下錯誤:

Error mounting /dev/sdXY at [...]: wrong fs type, bad option, bad superblock on /dev/sdXY, missing codepage or helper program, or other error

以 root 權限執行 journalctl/dmesg 內核日誌中顯示下面的信息:

ntfs: (device sdXY): parse_options(): Unrecognized mount option windows_names.

請安裝 NTFS-3G 來解決這個問題。

參閱[編輯 | 編輯原始碼]