Udisks (简体中文)

From ArchWiki
Revision as of 17:33, 7 October 2015 by Aaron chen (talk | contribs)
Jump to navigation Jump to search
翻译状态: 本文是英文页面 Udisks翻译,最后翻译时间:2015-10-08,点击这里可以查看翻译后英文页面的改动。

udisks 提供了 udisksd 守护进程,它实现了用于查询和管理存储设备的 D-Bus 接口;还提供了一个命令行工具 udisksctl,用于查询和使用该守护进程。

安装

有两个版本的 udisks,分别称为 udisksudisks2udisks 的开发已终止以利于 udisks2[1]

udisksd (udisks2) 和 udisks-daemon (udisks) 都是由 D-Bus 在后台启动,并且不应该被显式地启用。(参阅 man udisksdman udisks-daemon)。它们分别可以通过 udisksctludisks 以命令行方式管控。详情参阅 man udisksctlman udisks

配置

用户通过 udisks 可执行的动作受限于 Polkit。If your session is not activated or present, configure policykit manually. The following file sets common udisks permissions for the storage group. [2]

/etc/polkit-1/rules.d/50-udisks.rules
polkit.addRule(function(action, subject) {
  var YES = polkit.Result.YES;
  var permission = {
    // only required for udisks1:
    "org.freedesktop.udisks.filesystem-mount": YES,
    "org.freedesktop.udisks.filesystem-mount-system-internal": YES,
    "org.freedesktop.udisks.luks-unlock": YES,
    "org.freedesktop.udisks.drive-eject": YES,
    "org.freedesktop.udisks.drive-detach": YES,
    // only required for udisks2:
    "org.freedesktop.udisks2.filesystem-mount": YES,
    "org.freedesktop.udisks2.filesystem-mount-system": YES,
    "org.freedesktop.udisks2.encrypted-unlock": YES,
    "org.freedesktop.udisks2.eject-media": YES,
    "org.freedesktop.udisks2.power-off-drive": YES,
    // required for udisks2 if using udiskie from another seat (e.g. systemd):
    "org.freedesktop.udisks2.filesystem-mount-other-seat": YES,
    "org.freedesktop.udisks2.encrypted-unlock-other-seat": YES,
    "org.freedesktop.udisks2.eject-media-other-seat": YES,
    "org.freedesktop.udisks2.power-off-drive-other-seat": YES
  };
  if (subject.isInGroup("storage")) {
    return permission[action.id];
  }
});

See [3] for a more restrictive example. Note the org.freedesktop.udisks2.filesystem-* settings, which are required to start udiskie from a systemd service.

挂载助手

Automatic mounting of devices is easily achieved with udisks wrappers. See also List of applications#Mount tools and File manager functionality#Mounting.

Devmon

udevil includes devmon, which is compatible to udisks and udisks2. It uses mount helpers with the following priority:

  1. udevil (SUID)
  2. pmount (SUID)
  3. udisks
  4. udisks2

To mount devices with udisks or udisks2, remove the SUID permission from udevil:

# chmod -s /usr/bin/udevil
Note: chmod -x /usr/bin/udevil as root causes devmon to use udisks for device monitoring
Tip: To run devmon in the background and automatically mount devices, enable it with devmon@.service, taking the user name as argument: devmon@user.service. Keep in mind services run outside the session. Adjust Polkit rules where appropriate, or run devmon from the user session (see Autostart).

inotify

You may use inotify-tools to monitor /dev, and mount drives when a new block device is created. Stale mount points are automatically removed by udisksd, such that no special action is required on deletion.

#!/bin/bash
pattern='sd[b-z][1-9]$'
coproc inotifywait --monitor --event create,delete --format '%e %w%f' /dev

while read -r -u "${COPROC[0]}" event file; do
    if [[ $file =~ $pattern ]]; then
	case $event in
	    CREATE)
		echo "Settling..."; sleep 1
		udisksctl mount --block-device $file --no-user-interaction
		;;
	    DELETE)
		;;
	esac
    fi
done

提示与技巧

禁止隐藏设备(udisks2)

Udisks2 hides certain devices from the user by default. If this is undesired or otherwise problematic, copy /usr/lib/udev/rules.d/80-udisks2.rules to /etc/udev/rules.d/80-udisks2.rules and remove the following section in the copy:

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

挂载到 /media (udisks2)

By default, udisks2 mounts removable drives under the ACL controlled directory /run/media/$USER/. If you wish to mount to /media instead, use this rule:

/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"

挂载 ISO 镜像

To easily mount ISO images, use the following command:

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

This will create a loop device and show the ISO image ready to mount. Once unmounted, the loop device will be terminated by udev.

隐藏选中的分区

If you wish to prevent certain partitions or drives appearing on the desktop, you can create a udev rule, for example /etc/udev/rules.d/10-local.rules:

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

shows all partitions with the exception of sda1 and sda2 on your desktop. Notice if you are using udisks2 the above will not work as UDISKS_PRESENTATION_HIDE is no longer supported. Instead use UDISKS_IGNORE as follows:

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

排错

udisks: Devices do not remain unmounted

udisks remounts devices after a given period, or polls those devices. This can cause unexpected behaviour, for example when formatting drives, sharing them in a virtual machine, power saving, or removing a drive that was not detached with --detach before.

To disable polling for a given device, for example a CD/DVD device:

# udisks --inhibit-polling /dev/sr0

or for all devices:

# udisks --inhibit-all-polling

See man udisks for more information.

参阅