Ich beschäftige mich ja gerade intensiv mit systemd. Momentan steht das Thema automounten an.
Die Aufgabenstellung:
Ich habe eine externe HD, welche mit cryptsetup und einem Key auf einem extra USB-Stick verschlüsselt und versperrt ist.
Wenn ich die Platte anstecke, soll erstmal gar nichts geschehen.
Sobald ich aber in das Backupverzeichnis wechsle, oder das Backup von cron angestoßen wird, soll die Platte entsperrt und gemountet werden. Also -> Automount. Und da systemd so praktisch ist, will ich das damit erledigen lassen.
Ausgehängt wird die Platte dann manuell.
Soweit so gut. Die große Herausforderung war das Entsperren der Platte, da in systemd die Option "keyscript" ignoriert wird. Gelöst hab ich es durch folgenden systemd-Service:
Code: Alles auswählen
meinRechner: # cat systemd-cryptsetup@mars.service
# Automatically generated by systemd-cryptsetup-generator
[Unit]
Description=Cryptography Setup for %I
Documentation=man:systemd-cryptsetup@.service(8) man:crypttab(5)
SourcePath=/etc/crypttab
Conflicts=umount.target
DefaultDependencies=no
BindsTo=dev-mapper-%i.device
After=systemd-readahead-collect.service systemd-readahead-replay.service
Before=var-cache-backup.device
#Before=cryptsetup.target
RequiresMountsFor=/dev/disk/by-id/usb-_USB_DISK_Pro_XXXXXXXXXXXX-0:0
BindsTo=dev-disk-by\x2duuid-XXXXXXXX\x2dXXXX\x2dXXXX\x2dXXXX\x2dXXXXXXXXXXXX.device
After=dev-disk-by\x2duuid-XXXXXXXX\x2dXXXX\x2dXXXX\x2dXXXX\x2dXXXXXXXXXXXX.device
Before=umount.target
[Service]
Type=oneshot
RemainAfterExit=yes
TimeoutSec=15
ExecStart=/lib/systemd/systemd-cryptsetup attach 'mars' '/dev/disk/by-uuid/XXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX' '/dev/disk/by-id/usb-_USB_DISK_Pro_XXXXXXXXXXXX-0:0' 'luks,tries=3,keyfile-size=1024,keyfile-offset=1024'
ExecStop=/lib/systemd/systemd-cryptsetup detach 'mars'
[Install]
WantedBy=emergency.target multi-user.target
RequiredBy=var-cache-backup.device backup.device
Code: Alles auswählen
/dev/mapper/mars /var/cache/backup ext4 defaults,nofail,comment=systemd.automount,noauto 0 3
/var/cache/backup/snapshots /backup none bind,ro,nofail,comment=systemd.automount,noauto 0 0
Code: Alles auswählen
systemd-1 on /backup type autofs (rw,relatime,fd=41,pgrp=1,timeout=300,minproto=5,maxproto=5,direct)
systemd-1 on /var/cache/backup type autofs (rw,relatime,fd=27,pgrp=1,timeout=300,minproto=5,maxproto=5,direct)
Code: Alles auswählen
systemctl enable systemd-cryptsetup@mars.service
Code: Alles auswählen
systemctl --system daemon-reload
Das Ergebnis:
Der Service systemd-cryptsetup@mars.service wird automatisch gestartet (Es gibt keine udev-Regel!!!) und damit die Platte entsperrt. /dev/mapper/mars wird angelegt. (Soll es aber nicht!!)
Code: Alles auswählen
ls /backup
Das Syslog sagt folgendes:
Code: Alles auswählen
Sep 8 21:53:36 pluto systemd[1]: Starting Cryptography Setup for mars...
Sep 8 21:53:36 pluto systemd[1]: Expecting device /dev/mapper/mars...
Sep 8 21:53:36 pluto systemd-cryptsetup[24256]: Set cipher aes, mode xts-plain64, key size 512 bits for device /dev/disk/by-uuid/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.
Sep 8 21:53:42 pluto systemd[1]: Found device /dev/mapper/mars.
Sep 8 21:53:42 pluto systemd[1]: Starting File System Check on /dev/mapper/mars...
Sep 8 21:53:42 pluto systemd[1]: Started Cryptography Setup for mars.
Ein erneutes
Code: Alles auswählen
systemctl --system daemon-reload
Syslog sagt dann folgendes:
Code: Alles auswählen
Sep 8 21:55:37 pluto systemd[1]: Reloading.
Sep 8 21:55:37 pluto systemd-fsck[24614]: /dev/mapper/mars: sauber, 938310/27525120 Dateien, 82841815/110079488 Blöcke
Sep 8 21:55:37 pluto systemd[1]: Started File System Check on /dev/mapper/mars.
Sep 8 21:55:37 pluto systemd[1]: Mounting /var/cache/backup...
Sep 8 21:55:38 pluto kernel: [ 4804.575005] EXT4-fs (dm-8): mounted filesystem with ordered data mode. Opts: (null)
Sep 8 21:55:38 pluto systemd[1]: Mounted /var/cache/backup.
Sep 8 21:55:38 pluto systemd[1]: Mounting /backup...
Sep 8 21:55:38 pluto mount[30147]: mount: warning: /backup seems to be mounted read-write.
Sep 8 21:55:38 pluto systemd[1]: Mounted /backup.
Wenn ich die Platte wieder verschließe (nachdem /backup und /var/cache/backup gemountet ist) - das mache ich mit
Code: Alles auswählen
systemctl stop systemd-cryptsetup@mars.service
Code: Alles auswählen
Sep 8 21:56:45 pluto systemd[1]: Unmounting /backup...
Sep 8 21:56:45 pluto systemd[1]: Stopping /sys/devices/virtual/block/dm-8.
Sep 8 21:56:45 pluto systemd[1]: Stopping /sys/devices/virtual/block/dm-8.
Sep 8 21:56:45 pluto systemd[1]: Stopping /sys/devices/virtual/block/dm-8.
Sep 8 21:56:45 pluto systemd[1]: Stopping /sys/devices/virtual/block/dm-8.
Sep 8 21:56:45 pluto systemd[1]: Stopping /sys/devices/virtual/block/dm-8.
Sep 8 21:56:45 pluto systemd[1]: Stopping Cryptography Setup for mars...
Sep 8 21:56:45 pluto systemd[1]: Unmounted /backup.
Sep 8 21:56:45 pluto systemd[1]: Unmounting /var/cache/backup...
Sep 8 21:56:45 pluto systemd[1]: Starting Cryptography Setup for mars...
Sep 8 21:56:45 pluto systemd[1]: Found device /sys/devices/virtual/block/dm-8.
Sep 8 21:56:45 pluto systemd[1]: Found device /dev/dm-8.
Sep 8 21:56:45 pluto systemd[1]: Found device /dev/disk/by-id/dm-name-mars.
Sep 8 21:56:45 pluto systemd[1]: Found device /dev/disk/by-id/dm-uuid-CRYPT-LUKS1-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-mars.
Sep 8 21:56:45 pluto systemd[1]: Found device /dev/disk/by-uuid/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.
Sep 8 21:56:45 pluto systemd[1]: Found device /dev/mapper/mars.
Sep 8 21:56:45 pluto systemd[1]: Starting File System Check on /dev/mapper/mars...
Sep 8 21:56:45 pluto systemd[1]: Starting Cryptography Setup for mars...
Sep 8 21:56:45 pluto systemd[1]: Unmounted /var/cache/backup.
Sep 8 21:56:45 pluto systemd[1]: Stopping File System Check on /dev/mapper/mars...
Sep 8 21:56:45 pluto systemd[1]: Stopping Cryptography Setup for mars...
Sep 8 21:56:45 pluto systemd[1]: Stopped Cryptography Setup for mars.
Sep 8 21:56:46 pluto systemd[1]: Stopped File System Check on /dev/mapper/mars.
Ein anderes Mal bringt das verschließen der externen HD mit dem Befehl
Code: Alles auswählen
# systemctl stop systemd-cryptsetup@mars.service
Job for systemd-cryptsetup@mars.service canceled.
Code: Alles auswählen
Sep 8 22:02:35 pluto systemd[1]: Unmounting /var/cache/backup...
Sep 8 22:02:35 pluto systemd[1]: Stopping /sys/devices/virtual/block/dm-8.
Sep 8 22:02:35 pluto systemd[1]: Stopping /sys/devices/virtual/block/dm-8.
Sep 8 22:02:35 pluto systemd[1]: Stopping /sys/devices/virtual/block/dm-8.
Sep 8 22:02:35 pluto systemd[1]: Stopping /sys/devices/virtual/block/dm-8.
Sep 8 22:02:35 pluto systemd[1]: Stopping /sys/devices/virtual/block/dm-8.
Sep 8 22:02:35 pluto systemd[1]: Stopping Cryptography Setup for mars...
Sep 8 22:02:35 pluto systemd[1]: Starting Cryptography Setup for mars...
Sep 8 22:02:35 pluto systemd[1]: Found device /sys/devices/virtual/block/dm-8.
Sep 8 22:02:35 pluto systemd[1]: Found device /dev/dm-8.
Sep 8 22:02:35 pluto systemd[1]: Found device /dev/disk/by-id/dm-name-mars.
Sep 8 22:02:35 pluto systemd[1]: Found device /dev/disk/by-id/dm-uuid-CRYPT-LUKS1-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-mars.
Sep 8 22:02:35 pluto systemd[1]: Found device /dev/disk/by-uuid/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.
Sep 8 22:02:35 pluto systemd[1]: Mounting /var/cache/backup...
Sep 8 22:02:35 pluto kernel: [ 5221.937219] EXT4-fs (dm-8): mounted filesystem with ordered data mode. Opts: (null)
Sep 8 22:02:35 pluto systemd[1]: Mounted /var/cache/backup.
Sep 8 22:02:40 pluto systemd-cryptsetup[18029]: Failed to deactivate: Device or resource busy
Sep 8 22:02:40 pluto systemd[1]: systemd-cryptsetup@mars.service: control process exited, code=exited status=1
Sep 8 22:02:40 pluto systemd[1]: Unit systemd-cryptsetup@mars.service entered failed state.
Sep 8 22:02:40 pluto systemd[1]: Starting Cryptography Setup for mars...
Sep 8 22:02:40 pluto systemd-cryptsetup[18224]: Volume mars already active.
Sep 8 22:02:40 pluto systemd[1]: Started Cryptography Setup for mars.
Ich habe schon rausgefunden, wenn ich mit
Code: Alles auswählen
systemctl disable var-cache-backup.automount
Ein manuelles umounten mit
Code: Alles auswählen
umount /var/cache/backup
Wie mache ich das richtig?
![Twisted Evil :twisted:](./images/smilies/icon_twisted.gif)
lg scientific