phosphor-state-manager: create symlinks based on host-instances

As prep work for the multi-host designs, update the target generation
to take into account the OBMC_HOST_INSTANCES value and generate the
targets and links using it.

Tested:

Booted Witherspoon in QEMU and compared /lib/systemd/system before
and after the change.  Obtained the same results in both:

```
root@witherspoon:/lib/systemd/system# find | sort | md5sum
27b2eee499f272cf78b1384ad50a6bb8  -
```

Booted Bletchley in QEMU and confirmed that the undesired @0 targets
are no longer present in the `systemctl list-jobs` after boot.

```
root@bletchley:~# systemctl list-jobs | awk '{ print $2; }' | grep -e service -e target | grep 0
mapper-wait@-xyz-openbmc_project-control-host0-power_restore_policy.service
mapper-wait@-xyz-openbmc_project-control-host0-auto_reboot.service
```

(These remaining ones are induced by xyz.openbmc_project.State.Host,
 which still needs some clean up.)

Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: Iff058ecaa74a4cd4d830543b3a9c4fe0305595a8
diff --git a/meta-phosphor/recipes-phosphor/state/phosphor-state-manager-systemd-links.inc b/meta-phosphor/recipes-phosphor/state/phosphor-state-manager-systemd-links.inc
index 71f7d8e..86415a4 100644
--- a/meta-phosphor/recipes-phosphor/state/phosphor-state-manager-systemd-links.inc
+++ b/meta-phosphor/recipes-phosphor/state/phosphor-state-manager-systemd-links.inc
@@ -1,162 +1,183 @@
+
+# TODO: There are some targets that are chassis oriented, but there is no
+#       obvious way to map HOST<->CHASSIS relationships.  Making an assumption
+#       currently that there is a 1-to-1 relationship between the two.
+
 pkg_postinst:${PN}-obmc-targets:append() {
 
-    mkdir -p $D$systemd_system_unitdir/multi-user.target.requires
-    LINK="$D$systemd_system_unitdir/multi-user.target.requires/obmc-host-reset@0.target"
-    TARGET="../obmc-host-reset@.target"
-    ln -s $TARGET $LINK
+    for i in ${OBMC_HOST_INSTANCES};
+    do
+        mkdir -p $D$systemd_system_unitdir/multi-user.target.requires
+        LINK="$D$systemd_system_unitdir/multi-user.target.requires/obmc-host-reset@${i}.target"
+        TARGET="../obmc-host-reset@.target"
+        ln -s $TARGET $LINK
 
-    LINK="$D$systemd_system_unitdir/multi-user.target.requires/phosphor-discover-system-state@0.service"
-    TARGET="../phosphor-discover-system-state@.service"
-    ln -s $TARGET $LINK
+        LINK="$D$systemd_system_unitdir/multi-user.target.requires/phosphor-discover-system-state@${i}.service"
+        TARGET="../phosphor-discover-system-state@.service"
+        ln -s $TARGET $LINK
 
-    LINK="$D$systemd_system_unitdir/obmc-bmc-service-quiesce@0.target"
-    TARGET="./obmc-bmc-service-quiesce@.target"
-    ln -s $TARGET $LINK
+        LINK="$D$systemd_system_unitdir/obmc-bmc-service-quiesce@${i}.target"
+        TARGET="./obmc-bmc-service-quiesce@.target"
+        ln -s $TARGET $LINK
 
-    mkdir -p $D$systemd_system_unitdir/obmc-chassis-poweron@0.target.wants
-    LINK="$D$systemd_system_unitdir/obmc-chassis-poweron@0.target.wants/phosphor-reset-host-recovery@0.service"
-    TARGET="../phosphor-reset-host-recovery@.service"
-    ln -s $TARGET $LINK
+        mkdir -p $D$systemd_system_unitdir/obmc-chassis-poweron@${i}.target.wants
+        LINK="$D$systemd_system_unitdir/obmc-chassis-poweron@${i}.target.wants/phosphor-reset-host-recovery@${i}.service"
+        TARGET="../phosphor-reset-host-recovery@.service"
+        ln -s $TARGET $LINK
 
-    mkdir -p $D$systemd_system_unitdir/obmc-host-start@0.target.requires
-    LINK="$D$systemd_system_unitdir/obmc-host-start@0.target.requires/obmc-host-startmin@0.target"
-    TARGET="../obmc-host-startmin@.target"
-    ln -s $TARGET $LINK
+        mkdir -p $D$systemd_system_unitdir/obmc-host-start@${i}.target.requires
+        LINK="$D$systemd_system_unitdir/obmc-host-start@${i}.target.requires/obmc-host-startmin@${i}.target"
+        TARGET="../obmc-host-startmin@.target"
+        ln -s $TARGET $LINK
 
-    LINK="$D$systemd_system_unitdir/obmc-host-start@0.target.requires/phosphor-reset-host-reboot-attempts@0.service"
-    TARGET="../phosphor-reset-host-reboot-attempts@.service"
-    ln -s $TARGET $LINK
+        LINK="$D$systemd_system_unitdir/obmc-host-start@${i}.target.requires/phosphor-reset-host-reboot-attempts@${i}.service"
+        TARGET="../phosphor-reset-host-reboot-attempts@.service"
+        ln -s $TARGET $LINK
 
-    mkdir -p $D$systemd_system_unitdir/obmc-host-startmin@0.target.wants
-    LINK="$D$systemd_system_unitdir/obmc-host-startmin@0.target.wants/phosphor-set-host-transition-to-running@0.service"
-    TARGET="../phosphor-set-host-transition-to-running@.service"
-    ln -s $TARGET $LINK
+        mkdir -p $D$systemd_system_unitdir/obmc-host-startmin@${i}.target.wants
+        LINK="$D$systemd_system_unitdir/obmc-host-startmin@${i}.target.wants/phosphor-set-host-transition-to-running@${i}.service"
+        TARGET="../phosphor-set-host-transition-to-running@.service"
+        ln -s $TARGET $LINK
 
-    mkdir -p $D$systemd_system_unitdir/obmc-host-stop@0.target.wants
-    LINK="$D$systemd_system_unitdir/obmc-host-stop@0.target.wants/phosphor-set-host-transition-to-off@0.service"
-    TARGET="../phosphor-set-host-transition-to-off@.service"
-    ln -s $TARGET $LINK
+        mkdir -p $D$systemd_system_unitdir/obmc-host-stop@${i}.target.wants
+        LINK="$D$systemd_system_unitdir/obmc-host-stop@${i}.target.wants/phosphor-set-host-transition-to-off@${i}.service"
+        TARGET="../phosphor-set-host-transition-to-off@.service"
+        ln -s $TARGET $LINK
 
-    mkdir -p $D$systemd_system_unitdir/obmc-host-reset@0.target.requires
-    LINK="$D$systemd_system_unitdir/obmc-host-reset@0.target.requires/phosphor-reset-host-running@0.service"
-    TARGET="../phosphor-reset-host-running@.service"
-    ln -s $TARGET $LINK
+        mkdir -p $D$systemd_system_unitdir/obmc-host-reset@${i}.target.requires
+        LINK="$D$systemd_system_unitdir/obmc-host-reset@${i}.target.requires/phosphor-reset-host-running@${i}.service"
+        TARGET="../phosphor-reset-host-running@.service"
+        ln -s $TARGET $LINK
 
-    mkdir -p $D$systemd_system_unitdir/obmc-host-stop@0.target.wants
-    LINK="$D$systemd_system_unitdir/obmc-host-stop@0.target.wants/phosphor-reset-sensor-states@0.service"
-    TARGET="../phosphor-reset-sensor-states@.service"
-    ln -s $TARGET $LINK
+        mkdir -p $D$systemd_system_unitdir/obmc-host-stop@${i}.target.wants
+        LINK="$D$systemd_system_unitdir/obmc-host-stop@${i}.target.wants/phosphor-reset-sensor-states@${i}.service"
+        TARGET="../phosphor-reset-sensor-states@.service"
+        ln -s $TARGET $LINK
 
-    mkdir -p $D$systemd_system_unitdir/obmc-host-quiesce@0.target.wants
-    LINK="$D$systemd_system_unitdir/obmc-host-quiesce@0.target.wants/phosphor-reset-sensor-states@0.service"
-    TARGET="../phosphor-reset-sensor-states@.service"
-    ln -s $TARGET $LINK
+        mkdir -p $D$systemd_system_unitdir/obmc-host-quiesce@${i}.target.wants
+        LINK="$D$systemd_system_unitdir/obmc-host-quiesce@${i}.target.wants/phosphor-reset-sensor-states@${i}.service"
+        TARGET="../phosphor-reset-sensor-states@.service"
+        ln -s $TARGET $LINK
 
-    mkdir -p $D$systemd_system_unitdir/obmc-host-shutdown@0.target.requires
-    LINK="$D$systemd_system_unitdir/obmc-host-shutdown@0.target.requires/obmc-chassis-poweroff@0.target"
-    TARGET="../obmc-chassis-poweroff@.target"
-    ln -s $TARGET $LINK
+        mkdir -p $D$systemd_system_unitdir/obmc-host-shutdown@${i}.target.requires
+        LINK="$D$systemd_system_unitdir/obmc-host-shutdown@${i}.target.requires/obmc-chassis-poweroff@${i}.target"
+        TARGET="../obmc-chassis-poweroff@.target"
+        ln -s $TARGET $LINK
 
-    mkdir -p $D$systemd_system_unitdir/obmc-host-reboot@0.target.requires
-    LINK="$D$systemd_system_unitdir/obmc-host-reboot@0.target.requires/obmc-host-shutdown@0.target"
-    TARGET="../obmc-host-shutdown@.target"
-    ln -s $TARGET $LINK
+        mkdir -p $D$systemd_system_unitdir/obmc-host-reboot@${i}.target.requires
+        LINK="$D$systemd_system_unitdir/obmc-host-reboot@${i}.target.requires/obmc-host-shutdown@${i}.target"
+        TARGET="../obmc-host-shutdown@.target"
+        ln -s $TARGET $LINK
 
-    LINK="$D$systemd_system_unitdir/obmc-host-reboot@0.target.requires/phosphor-reboot-host@0.service"
-    TARGET="../phosphor-reboot-host@.service"
-    ln -s $TARGET $LINK
+        LINK="$D$systemd_system_unitdir/obmc-host-reboot@${i}.target.requires/phosphor-reboot-host@${i}.service"
+        TARGET="../phosphor-reboot-host@.service"
+        ln -s $TARGET $LINK
 
-    mkdir -p $D$systemd_system_unitdir/obmc-host-warm-reboot@0.target.requires
-    LINK="$D$systemd_system_unitdir/obmc-host-warm-reboot@0.target.requires/xyz.openbmc_project.Ipmi.Internal.SoftPowerOff.service"
-    TARGET="../xyz.openbmc_project.Ipmi.Internal.SoftPowerOff.service"
-    ln -s $TARGET $LINK
+        mkdir -p $D$systemd_system_unitdir/obmc-host-warm-reboot@${i}.target.requires
+        LINK="$D$systemd_system_unitdir/obmc-host-warm-reboot@${i}.target.requires/xyz.openbmc_project.Ipmi.Internal.SoftPowerOff.service"
+        TARGET="../xyz.openbmc_project.Ipmi.Internal.SoftPowerOff.service"
+        ln -s $TARGET $LINK
 
-    mkdir -p $D$systemd_system_unitdir/obmc-host-force-warm-reboot@0.target.requires
-    LINK="$D$systemd_system_unitdir/obmc-host-force-warm-reboot@0.target.requires/obmc-host-stop@0.target"
-    TARGET="../obmc-host-stop@.target"
-    ln -s $TARGET $LINK
+        mkdir -p $D$systemd_system_unitdir/obmc-host-force-warm-reboot@${i}.target.requires
+        LINK="$D$systemd_system_unitdir/obmc-host-force-warm-reboot@${i}.target.requires/obmc-host-stop@${i}.target"
+        TARGET="../obmc-host-stop@.target"
+        ln -s $TARGET $LINK
 
-    LINK="$D$systemd_system_unitdir/obmc-host-force-warm-reboot@0.target.requires/phosphor-reboot-host@0.service"
-    TARGET="../phosphor-reboot-host@.service"
-    ln -s $TARGET $LINK
+        LINK="$D$systemd_system_unitdir/obmc-host-force-warm-reboot@${i}.target.requires/phosphor-reboot-host@${i}.service"
+        TARGET="../phosphor-reboot-host@.service"
+        ln -s $TARGET $LINK
 
-    LINK="$D$systemd_system_unitdir/obmc-host-warm-reboot@0.target.requires/obmc-host-force-warm-reboot@0.target"
-    TARGET="../obmc-host-force-warm-reboot@.target"
-    ln -s $TARGET $LINK
+        LINK="$D$systemd_system_unitdir/obmc-host-warm-reboot@${i}.target.requires/obmc-host-force-warm-reboot@${i}.target"
+        TARGET="../obmc-host-force-warm-reboot@.target"
+        ln -s $TARGET $LINK
 
-    mkdir -p $D$systemd_system_unitdir/obmc-chassis-poweroff@0.target.wants
-    LINK="$D$systemd_system_unitdir/obmc-chassis-poweroff@0.target.wants/phosphor-clear-one-time@0.service"
-    TARGET="../phosphor-clear-one-time@.service"
-    ln -s $TARGET $LINK
+        mkdir -p $D$systemd_system_unitdir/obmc-chassis-poweroff@${i}.target.wants
+        LINK="$D$systemd_system_unitdir/obmc-chassis-poweroff@${i}.target.wants/phosphor-clear-one-time@${i}.service"
+        TARGET="../phosphor-clear-one-time@.service"
+        ln -s $TARGET $LINK
+    done
 }
 
 pkg_postinst:${PN}-chassis-check-power-status:append() {
 
-    mkdir -p $D$systemd_system_unitdir/obmc-chassis-poweron@0.target.requires
-    LINK="$D$systemd_system_unitdir/obmc-chassis-poweron@0.target.requires/phosphor-chassis-check-power-status@0.service"
-    TARGET="../phosphor-chassis-check-power-status@.service"
-    ln -s $TARGET $LINK
+    for i in ${OBMC_HOST_INSTANCES};
+    do
+        mkdir -p $D$systemd_system_unitdir/obmc-chassis-poweron@${i}.target.requires
+        LINK="$D$systemd_system_unitdir/obmc-chassis-poweron@${i}.target.requires/phosphor-chassis-check-power-status@${i}.service"
+        TARGET="../phosphor-chassis-check-power-status@.service"
+        ln -s $TARGET $LINK
+    done
 }
 
 pkg_prerm:${PN}-obmc-targets:append() {
 
-    LINK="$D$systemd_system_unitdir/multi-user.target.requires/obmc-host-reset@0.target"
-    rm $LINK
+    for i in ${OBMC_HOST_INSTANCES};
+    do
 
-    LINK="$D$systemd_system_unitdir/multi-user.target.requires/phosphor-discover-system-state@0.service"
-    rm $LINK
+        LINK="$D$systemd_system_unitdir/multi-user.target.requires/obmc-host-reset@${i}.target"
+        rm $LINK
 
-    LINK="$D$systemd_system_unitdir/obmc-chassis-poweron@0.target.wants/phosphor-reset-host-recovery@0.service"
-    rm $LINK
+        LINK="$D$systemd_system_unitdir/multi-user.target.requires/phosphor-discover-system-state@${i}.service"
+        rm $LINK
 
-    LINK="$D$systemd_system_unitdir/obmc-host-start@0.target.requires/obmc-host-startmin@0.target"
-    rm $LINK
+        LINK="$D$systemd_system_unitdir/obmc-chassis-poweron@${i}.target.wants/phosphor-reset-host-recovery@${i}.service"
+        rm $LINK
 
-    LINK="$D$systemd_system_unitdir/obmc-host-start@0.target.requires/phosphor-reset-host-reboot-attempts@0.service"
-    rm $LINK
+        LINK="$D$systemd_system_unitdir/obmc-host-start@${i}.target.requires/obmc-host-startmin@${i}.target"
+        rm $LINK
 
-    LINK="$D$systemd_system_unitdir/obmc-host-startmin@0.target.wants/phosphor-set-host-transition-to-running@0.service"
-    rm $LINK
+        LINK="$D$systemd_system_unitdir/obmc-host-start@${i}.target.requires/phosphor-reset-host-reboot-attempts@${i}.service"
+        rm $LINK
 
-    LINK="$D$systemd_system_unitdir/obmc-host-stop@0.target.wants/phosphor-set-host-transition-to-off@0.service"
-    rm $LINK
+        LINK="$D$systemd_system_unitdir/obmc-host-startmin@${i}.target.wants/phosphor-set-host-transition-to-running@${i}.service"
+        rm $LINK
 
-    LINK="$D$systemd_system_unitdir/obmc-host-stop@0.target.wants/phosphor-reset-sensor-states@0.service"
-    rm $LINK
+        LINK="$D$systemd_system_unitdir/obmc-host-stop@${i}.target.wants/phosphor-set-host-transition-to-off@${i}.service"
+        rm $LINK
 
-    LINK="$D$systemd_system_unitdir/obmc-host-quiesce@0.target.wants/phosphor-reset-sensor-states@0.service"
-    rm $LINK
+        LINK="$D$systemd_system_unitdir/obmc-host-stop@${i}.target.wants/phosphor-reset-sensor-states@${i}.service"
+        rm $LINK
 
-    LINK="$D$systemd_system_unitdir/obmc-host-reset@0.target.requires/phosphor-reset-host-running@0.service"
-    rm $LINK
+        LINK="$D$systemd_system_unitdir/obmc-host-quiesce@${i}.target.wants/phosphor-reset-sensor-states@${i}.service"
+        rm $LINK
 
-    LINK="$D$systemd_system_unitdir/obmc-host-shutdown@0.target.requires/obmc-chassis-poweroff@0.target"
-    rm $LINK
+        LINK="$D$systemd_system_unitdir/obmc-host-reset@${i}.target.requires/phosphor-reset-host-running@${i}.service"
+        rm $LINK
 
-    LINK="$D$systemd_system_unitdir/obmc-host-reboot@0.target.requires/obmc-host-shutdown@0.target"
-    rm $LINK
+        LINK="$D$systemd_system_unitdir/obmc-host-shutdown@${i}.target.requires/obmc-chassis-poweroff@${i}.target"
+        rm $LINK
 
-    LINK="$D$systemd_system_unitdir/obmc-host-reboot@0.target.requires/phosphor-reboot-host@0.service"
-    rm $LINK
+        LINK="$D$systemd_system_unitdir/obmc-host-reboot@${i}.target.requires/obmc-host-shutdown@${i}.target"
+        rm $LINK
 
-    LINK="$D$systemd_system_unitdir/obmc-host-warm-reboot@0.target.requires/xyz.openbmc_project.Ipmi.Internal.SoftPowerOff.service"
-    rm $LINK
+        LINK="$D$systemd_system_unitdir/obmc-host-reboot@${i}.target.requires/phosphor-reboot-host@${i}.service"
+        rm $LINK
 
-    LINK="$D$systemd_system_unitdir/obmc-host-force-warm-reboot@0.target.requires/obmc-host-stop@0.target"
-    rm $LINK
+        LINK="$D$systemd_system_unitdir/obmc-host-warm-reboot@${i}.target.requires/xyz.openbmc_project.Ipmi.Internal.SoftPowerOff.service"
+        rm $LINK
 
-    LINK="$D$systemd_system_unitdir/obmc-host-force-warm-reboot@0.target.requires/phosphor-reboot-host@0.service"
-    rm $LINK
+        LINK="$D$systemd_system_unitdir/obmc-host-force-warm-reboot@${i}.target.requires/obmc-host-stop@${i}.target"
+        rm $LINK
 
-    LINK="$D$systemd_system_unitdir/obmc-host-warm-reboot@0.target.requires/obmc-host-force-warm-reboot@0.target"
-    rm $LINK
+        LINK="$D$systemd_system_unitdir/obmc-host-force-warm-reboot@${i}.target.requires/phosphor-reboot-host@${i}.service"
+        rm $LINK
 
-    LINK="$D$systemd_system_unitdir/obmc-chassis-poweroff@0.target.wants/phosphor-clear-one-time@0.service"
-    rm $LINK
+        LINK="$D$systemd_system_unitdir/obmc-host-warm-reboot@${i}.target.requires/obmc-host-force-warm-reboot@${i}.target"
+        rm $LINK
+
+        LINK="$D$systemd_system_unitdir/obmc-chassis-poweroff@${i}.target.wants/phosphor-clear-one-time@${i}.service"
+        rm $LINK
+
+    done
 }
 
 pkg_prerm:${PN}-chassis-check-power-status:append() {
 
-    LINK="$D$systemd_system_unitdir/obmc-chassis-poweron@0.target.requires/phosphor-chassis-check-power-status@0.service"
-    rm $LINK
+    for i in ${OBMC_HOST_INSTANCES};
+    do
+
+        LINK="$D$systemd_system_unitdir/obmc-chassis-poweron@${i}.target.requires/phosphor-chassis-check-power-status@${i}.service"
+        rm $LINK
+
+    done
 }