通过 Systemd 自动挂载时 Cryptsetup 设备超时

通过 Systemd 自动挂载时 Cryptsetup 设备超时

由于某种原因,systemd 在尝试挂载我的一台 cryptsetup 设备时出现超时。以下是单位和脚本:

udev规则:

ACTION=="add", KERNEL=="sd[a-z][0-9]", ATTRS{serial}=="NA8F9WKF",                                                               SYMLINK+="BigHDD", TAG+="systemd"
ACTION=="add", KERNEL=="sd[a-z][0-9]", ATTRS{serial}=="AA00000000065396",                                                       SYMLINK+="clavem", TAG+="systemd"
ACTION=="add", KERNEL=="sd[a-z][0-9]", ATTRS{wwid}=="t10.ATA     WDC WD10EZRZ-00Z5HB0                         WD-WCC4M6HRPV4V", SYMLINK+="HDDsB",  TAG+="systemd"
ACTION=="add", KERNEL=="sd[a-z][0-9]", ATTRS{wwid}=="t10.ATA     WDC WD10EZRZ-00Z5HB0                         WD-WCC4M3EYESNV", SYMLINK+="HDDsC",  TAG+="systemd"

mnt-HDD.automount

[Unit]
Description=Automount HDDs
Requires=dev-clavem.device

[Automount]
Where=/mnt/HDDs

[Install]
WantedBy=multi-user.target

mnt-HDD.mount:

[Unit]
BindsTo=HDDs-unlock.service dev-clavem.device media-clavem.mount
After=HDDs-unlock.service dev-HDDsB.device dev-HDDsC.device

[Mount]
What=/dev/mapper/cryptHDDC
Where=/mnt/HDDs
Type=btrfs
Options= noatime,compress=lzo,autodefrag

HDD-unlock.service:

[Unit]
Description=HDDs unlock
BindsTo=mnt-HDDs.mount dev-clavem.device dev-HDDsB.device dev-HDDsC.device
Requires=media-clavem.mount dev-HDDsB.device dev-HDDsC.device
After=media-clavem.mount dev-HDDsB.device dev-HDDsC.device

[Service]
Type=oneshot
RemainAfterExit=yes
KillMode=none
ExecStart=/home/rbenedetti/teste.sh
ExecStop=/sbin/cryptsetup luksClose cryptHDDB ; /sbin/cryptsetup luksClose 
cryptHDDC

[Install]
RequiredBy=mnt-HDDs.mount

测试文件

#!/bin/bash

i=0
while [[ ! -b /dev/disk/by-uuid/42e7eacf-5e09-4476-8dbc-1919675c1595 ]]; do
    echo "cant find /dev/disk/by-uuid/42e7eacf-5e09-4476-8dbc-1919675c1595... waiting. $i" > ~/testLog
    if [[ "$counter" -gt 10 ]]; then
        echo "giving up." > ~/testLog
        exit 1
    fi
    sleep 0.5
    let i++
done

i=0
while [[ ! -b /dev/disk/by-uuid/826b9963-eb49-4eac-a9c0-e741552491d4 ]]; do
    echo "cant find /dev/disk/by-uuid/826b9963-eb49-4eac-a9c0-e741552491d4... waiting. $i" > ~/testLog
    if [[ "$counter" -gt 10 ]]; then
        echo "giving up." > ~/testLog
        exit 1
    fi
    sleep 0.5
    let i++
done


/sbin/cryptsetup --key-file /media/clavem/k luksOpen /dev/disk/by-uuid/42e7eacf-5e09-4476-8dbc-1919675c1595 cryptHDDC
/sbin/cryptsetup --key-file /media/clavem/k luksOpen /dev/disk/by-uuid/826b9963-eb49-4eac-a9c0-e741552491d4 cryptHDDB 


i=0
while [[ ! -b /dev/mapper/cryptHDDB ]]; do
    echo "cant find /dev/mapper/cryptHDDB... waiting. $i" > ~/testLog
    /sbin/cryptsetup --key-file /media/clavem/k luksOpen /dev/disk/by-uuid/826b9963-eb49-4eac-a9c0-e741552491d4 cryptHDDB 
    if [[ "$counter" -gt 10 ]]; then
        echo "giving up." > ~/testLog
        exit 1
    fi
    sleep 0.5
    let i++
done

i=0
while [[ ! -b /dev/mapper/cryptHDDC ]]; do
    echo "cant find /dev/mapper/cryptHDDC... waiting. $i" > ~/testLog
    /sbin/cryptsetup --key-file /media/clavem/k luksOpen /dev/disk/by-uuid/42e7eacf-5e09-4476-8dbc-1919675c1595 cryptHDDC
    if [[ "$counter" -gt 10 ]]; then
        echo "giving up." > ~/testLog
        exit 1
    fi
    sleep 0.5
    let i++
done

 echo "somehow passed...>" > ~/testLog

teste.sh 大部分都是测试,因为我试图弄清楚为什么一次性服务中会发生超时。由于某种原因,在执行时systemctl restart mnt-HDDs.mount,一切顺利并安装了我的分区...它给出了超时mnt-mapper-cryptHDDC.device

答案1

好吧,显然通过安装cryptHDDB工作让我相信 udev 和 Systemd 之间存在一些问题。

相关内容