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