udevルールのスクリプトによるLUKSパーティションの復号化

udevルールのスクリプトによるLUKSパーティションの復号化

外付けハードドライブを接続するときにLUKSパーティションの復号化を試みます。だから、私はudevルールを設定しました:

ACTION=="add", KERNEL=="sdb1", SUBSYSTEM=="block", ATTR{size}=="1048576000", RUN+="/home/user/myfile.sh

myfile.shスクリプト:

password=`su - user -c 'export DISPLAY=:0;kdialog --password "Decrypt HDD"'`
sleep 5
echo "$password" | cryptsetup luksOpen /dev/sdb1 backups
mount /dev/mapper/backups /media/backups/

スクリプトは次のようにパスワードを読み込みます。ダイアログボックス、パーティションの復号化とマウント後。手動で実行すると動作します。問題はUSBを差し込むと、ダイアログボックス起動されたが/dev/sdb1暗号化されたパーティションは、スクリプトが完了するまで検出されません。したがって、cryptsetup luksOpenとmountは何もしません(暗号化されたパーティションが検出される前に開始されるためです)。

udevルールを含むファイルは/etc/udev/rules.dの90-crypt.rulesです。

ベストアンサー1

最後に、RUNを使用すると、長いプログラム/スクリプトを実行できないという問題が発見されました。そして解決策は、スクリプトを実行し、ルールを使用するサービスを作成することです。SYSTEMD_WANTSあなたのサービスとして。

男udevから:

RUN{type}「タイプ」に従って、特定のイベントのすべてのルールを処理し、実行されるプログラムのリストにプログラムを追加します。

   "program"
       Execute an external program specified as the assigned value. If no absolute path is given, the program is expected to live in /lib/udev; otherwise, the absolute path must be specified.          

       This is the default if no type is specified.

   "builtin"
       As program, but use one of the built-in programs rather than an external one.

   The program name and following arguments are separated by spaces. Single quotes can be used to specify arguments with spaces.                                                                         

   This can only be used for very short-running foreground tasks. Running an event process for a long period of time may block all further events for this or a dependent device.                        

   Starting daemons or other long-running processes is not appropriate for udev; the forked processes, detached or not, will be unconditionally killed after the event handling has finished.            

   Note that running programs that access the network or mount/unmount filesystems is not allowed inside of udev rules, due to the default sandbox that is enforced on systemd-udevd.service.   

解決策:

/etc/systemd/system/myservice.service>にサービスを作成する

[Unit]
Description=Auto backup

[Service]
ExecStart=/home/manu/Sysadmin/auto-backup.sh

そしてudevルールを変更します。

ACTION=="add", KERNEL=="sdb1", SUBSYSTEM=="block", ATTR{size}=="1048576000", ENV{SYSTEMD_WANTS}="myservice.service"

おすすめ記事