cryptsetup 无法关闭映射设备

cryptsetup 无法关闭映射设备

当我跑步时:

LANG=C cryptsetup --debug luksClose /dev/mapper/Pool-A

it fails as follows:

    device-mapper: remove ioctl on Pool-A failed: Device or resource busy

Device /dev/mapper/Pool-A is still in use.
Command failed with code 16: Device or resource busy

(……)(它重复了很多这样的台词)

# cryptsetup 1.7.2 processing "cryptsetup --debug luksClose /dev/mapper/Pool-A"
# Running command close.
# Locking memory.
# Installing SIGINT/SIGTERM handler.
# Unblocking interruption on signal.
# Allocating crypt device context by device /dev/mapper/Pool-A.
# Initialising device-mapper backend library.
# dm version   OF   [16384] (*1)
# dm versions   OF   [16384] (*1)
# Detected dm-crypt version 1.14.1, dm-ioctl version 4.33.0.
# Device-mapper backend running with UDEV support enabled.
# dm status Pool-A  OF   [16384] (*1)
# Releasing device-mapper backend.
# Trying to open and read device /dev/sdb1 with direct-io.
# Allocating crypt device /dev/sdb1 context.
# Trying to open and read device /dev/sdb1 with direct-io.
# Initialising device-mapper backend library.
# dm table Pool-A  OFW    [16384] (*1)
# Trying to open and read device /dev/sdb1 with direct-io.
# Crypto backend (gcrypt 1.5.3) initialized in cryptsetup library version 1.7.2.
# Detected kernel Linux 3.10.0-327.36.3.el7.x86_64 x86_64.
# Reading LUKS header of size 1024 from device /dev/sdb1
# Key length 32, device size 3906961375 sectors, header size 2050 sectors.
# Deactivating volume /dev/mapper/Pool-A.
# dm status Pool-A  OF   [16384] (*1)
# Udev cookie 0xd4d14d3 (semid 917504) created
# Udev cookie 0xd4d14d3 (semid 917504) incremented to 1
# Udev cookie 0xd4d14d3 (semid 917504) incremented to 2
# Udev cookie 0xd4d14d3 (semid 917504) assigned to REMOVE task(2) with flags         (0x0)
# dm remove Pool-A  OFT    [16384] (*1)

(...)(重复了 25 行)

# Releasing crypt device /dev/sdb1 context.
# Releasing device-mapper backend.
# Unlocking memory.


I've found that this problem arises when I tri to use udev rules like that:

ACTION=="add",ENV{ID_SERIAL_SHORT}=="57584E314135364632334141", RUN+="/etc/bacula/openmount.sh A" 
ACTION=="remove",ENV{ID_SERIAL_SHORT}=="57584E314135364632334141", RUN+="/etc/bacula/umountclose.sh A"

这是低级信息:

[root@backup ~]# dmsetup info Pool-A 
Name:              Pool-A
State:             ACTIVE
Read Ahead:        256
Tables present:    LIVE
Open count:        1
Event number:      0
Major, minor:      253, 3
Number of targets: 1
UUID: CRYPT-LUKS1-2b69b6e48b6d4bd1942ae7505d530f27-Pool-A

块设备信息如下:

    lsblk -o +UUID /dev/sdb
NAME       MAJ:MIN RM  SIZE RO TYPE  MOUNTPOINT UUID
sdb          8:16   0  1,8T  0 disk             
└─sdb1       8:17   0  1,8T  0 part             
  └─Pool-A 253:3    0  1,8T  0 crypt            48c1accf-47a6-45ec-aacc-6686e8a8a2fa

我的操作系统是:

CentOS Linux 版本 7.2.1511(核心),内核为 3.10.0-327.36.3.el7.x86_64

这是 systemd-udev 的问题吗?我该如何修复它?或者这只是一个无害的警告?

答案1

这是 systemd-udev 与传统 udev 之间的区别。systemd-udev 安装在单独的命名空间中,因此用户看不到 RUN 脚本中的安装。您可以尝试一下,systemctl restart udev如果释放了锁,那就是问题所在。要正确执行此操作,您必须在 systemd 中工作。对我有用的是:一条 udev 规则来捕获正在插入的 USB 驱动器:

ACTION=="add", KERNEL=="sd?1", ENV{ID_SERIAL_SHORT}=="575...", SYMLINK+="offsitebackup", TAG+="systemd", ENV{SYSTEMD_WANTS}+="offsite-backup.service"

这将创建指向驱动器第一个分区的 /dev/offsitebackup 链接并触发服务。服务文件为:

[Unit]
Description=Offsite Backup script
Requisite=dev-offsitebackup.device
BindsTo=dev-offsitebackup.device
After=dev-offsitebackup.device

[Service]
ExecStart=/usr/local/scripts/offsite-backup.ksh offsitebackup
Type=oneshot
StandardOutput=journal

[Install]
WantedBy=dev-offsitebackup.device

然后脚本执行 luksOpen/mount/copy/umount/luksClose。此外,为了测试这些,你可以使用以下命令模拟拔出/重新插入 USB 驱动器:

udevadm trigger -v -c remove /dev/sda
udevadm trigger -v -c add /dev/sda

您可以使用以下命令查看 systemd 中的设备/脚本状态:

systemctl status dev-offsitebackup.device
systemctl status offsite-backup.service

有助于弄清楚这一切的两个页面是这里这里

相关内容