Update configuration.nix
This commit is contained in:
@@ -135,13 +135,13 @@
|
||||
############################################################
|
||||
|
||||
systemd.services.shutdown-on-root-usb-loss = {
|
||||
description = "Shutdown if USB device backing / disappears";
|
||||
description = "Shutdown if device backing / disappears";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
after = [ "local-fs.target" ];
|
||||
|
||||
serviceConfig = {
|
||||
Type = "simple";
|
||||
ExecStart = pkgs.writeShellScript "watch-root-usb" ''
|
||||
ExecStart = pkgs.writeShellScript "watch-root-device" ''
|
||||
set -e
|
||||
|
||||
FINDMNT=${pkgs.util-linux}/bin/findmnt
|
||||
@@ -150,23 +150,21 @@
|
||||
SLEEP=${pkgs.coreutils}/bin/sleep
|
||||
SYSTEMCTL=${pkgs.systemd}/bin/systemctl
|
||||
|
||||
# Resolve root source
|
||||
ROOT_SRC="$($FINDMNT -n -o SOURCE / || true)"
|
||||
[ -z "$ROOT_SRC" ] && exit 0
|
||||
|
||||
if [[ "$ROOT_SRC" == /dev/dm-* ]]; then
|
||||
PARENT="$($LSBLK -no PKNAME "$ROOT_SRC")"
|
||||
ROOT_DEV="/dev/$PARENT"
|
||||
else
|
||||
ROOT_DEV="$ROOT_SRC"
|
||||
fi
|
||||
# Walk up to the top-level block device
|
||||
DEV="$ROOT_SRC"
|
||||
while PARENT="$($LSBLK -no PKNAME "$DEV" 2>/dev/null)"; [ -n "$PARENT" ]; do
|
||||
DEV="/dev/$PARENT"
|
||||
done
|
||||
|
||||
TRAN="$($LSBLK -no TRAN "$ROOT_DEV" || true)"
|
||||
[ "$TRAN" != "usb" ] && exec $SLEEP infinity
|
||||
|
||||
DEV_NAME="$($BASENAME "$ROOT_DEV")"
|
||||
DEV_NAME="$($BASENAME "$DEV")"
|
||||
|
||||
# Watch until the block device disappears
|
||||
while [ -e "/sys/class/block/$DEV_NAME" ]; do
|
||||
$SLEEP 1
|
||||
$SLEEP 0.5
|
||||
done
|
||||
|
||||
$SYSTEMCTL poweroff
|
||||
|
||||
Reference in New Issue
Block a user