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 来解决这个问题。

参阅[编辑 | 编辑源代码]