#! /bin/bash
# Copyright (c) 2020 SUSE LLC
# SPDX-License-Identifier: GPL-2.0-or-later
#
# bsc#1174287: On PowerPC, exclude memory devices from udev coldplug
#
# This generator creates a drop-in for systemd-udev-trigger.service
# that overrides the default action, skipping coldplug for memory devices.

export PATH=/usr/bin
# /tmp may not be writable yet. Use a different TMPDIR that's guaranteed
# to be writable. Needed for the "here-document" below.
export TMPDIR=/dev/shm
trap 'echo $0: error in "$BASH_COMMAND" >&2' ERR

[[ $(uname -m) = ppc64le ]] || [[ $(uname -m) = ppc64 ]] || exit 0
case $(systemd-detect-virt) in
    none|powervm) ;;
    *) exit 0;;
esac

DIR=$1
[[ $DIR && -d "$DIR" ]]
DROPIN="$DIR/systemd-udev-trigger.service.d"
mkdir -p "$DROPIN"
cat >"$DROPIN/30-skip-memory-devices.conf" <<EOF
# generated by $0
[Service]
ExecStart=
ExecStart=/usr/bin/udevadm trigger --type=subsystems --action=add
ExecStart=/usr/bin/udevadm trigger --type=devices --action=add --subsystem-nomatch=memory
EOF
