This saves f4d6f687's fix for [[bugs/sdmem_on_eject_broken_for_CD]].
[tails-test.git] / config / chroot_local-includes / usr / local / sbin / udev-watchdog-wrapper
blobacb771fcbb96aaf3364fe40d9e6a12bfdb1dad16
1 #!/bin/sh
3 set -e
5 ### Helper functions
7 # For whatever reason, the initscript that calls us sets a pretty scarse $PATH
8 PATH="/usr/bin:${PATH}"
10 ### Helper functions
12 using_fromiso() {
13 grep -qs -w -E '(fromiso|isofrom)=' /proc/cmdline
16 # Returns the boot device's path in a form that can be passed to the
17 # eject command, e.g. /dev/scd0 or /dev/block/NN:MM.
18 boot_device() {
19 if using_fromiso ; then
20 # When booting with e.g. fromiso=/dev/sdx3/tails-XXX.iso, a loop device
21 # is mounted onto /live/image => we cannot get the boot device from there.
22 # This loop device's backing file is seen by the system as
23 # /isofrom/XXX.iso, which is not available presumably because pivotroot
24 # was run => we cannot get the boot device from there either.
25 # Instead, we parse fromiso='s argument the same way live-boot does
26 # in order to extract the device path (/dev/sdx3)
27 for ARGUMENT in $(cat /proc/cmdline) ; do
28 case "${ARGUMENT}" in
29 isofrom=*|fromiso=*)
30 FROMISO="${ARGUMENT#*=}"
32 esac
33 done
34 echo $(dirname "$FROMISO")
35 else
36 # Refactorer, beware: the rest of this script depends on the fact that
37 # the path returned in this case is suitable to be passed as an argument
38 # to --path in "udevadm info --query" commands... which is not the case
39 # of paths in the /dev/sdxN form.
40 DEV_NUMBER="$(udevadm info --device-id-of-file=/live/image)"
41 echo "/dev/block/$DEV_NUMBER"
45 # First clean the screen, then brutally shutdown the machine.
46 do_stop() {
47 /etc/init.d/gdm3 stop 2>&1 >/dev/null || true
48 /etc/init.d/kexec-load stop 2>&1 >/dev/null || true
49 /etc/init.d/tails-kexec stop 2>&1 >/dev/null || true
53 ### Main
55 BOOT_DEVICE=$(boot_device)
57 # Assign to QUERY_SELECTOR an option that can be passed as a query selector
58 # to udevadm info --query commands.
59 if using_fromiso ; then
60 DEV_NAME=$(basename "$BOOT_DEVICE")
61 QUERY_SELECTOR="--name $DEV_NAME"
62 else
63 QUERY_SELECTOR="--path $BOOT_DEVICE"
66 DEV_UDEV_PATH=$(udevadm info --query path $QUERY_SELECTOR)
67 DEV_TYPE_LINE=$(udevadm info --query property $QUERY_SELECTOR | grep -w '^ID_TYPE')
68 DEV_TYPE="${DEV_TYPE_LINE#*=}"
70 # If the world was sane we'd want to *disable* the eject lock, but it turns out
71 # that blocks the block events so udev-watchdog never receives the "change"
72 # event. See [[bugs/sdmem_on_eject_broken_for_CD]].
73 if [ "$DEV_TYPE" = "cd" ]; then
74 eject -i on "${BOOT_DEVICE}"
77 # Start udev-watchdog and stop on clean exit.
78 /usr/local/sbin/udev-watchdog "$DEV_UDEV_PATH" "$DEV_TYPE" && do_stop