From f792336c1c62063b6246862be77ea255ca51eefc Mon Sep 17 00:00:00 2001
From: Helmut Buchsbaum <helmut.buchsbaum@opensource.tttech-industrial.com>
Date: Tue, 3 May 2022 11:29:05 +0200
Subject: [PATCH] debian: Fixup ACRN lifemngr package build

Part of fix for
https://github.com/tttech-industrial-buchsbaum/acrn-hypervisor/issues/6

Tracked-On: #6688
Signed-off-by: Helmut Buchsbaum <helmut.buchsbaum@opensource.tttech-industrial.com>
---
 debian/acrn-lifemngr.install           |  3 +-
 debian/acrn-lifemngr.postrm            | 46 ++++++++++++++++++++++++++
 debian/acrn-lifemngr.service           | 13 --------
 debian/control                         |  2 +-
 debian/lifemngr/acrn-lifemngr.conf     |  9 -----
 debian/lifemngr/life_mngr.conf.user_vm | 20 +++++++++++
 debian/lifemngr/start-acrn-lifemngr.sh | 18 +++++-----
 debian/rules                           | 24 ++++++++++----
 8 files changed, 97 insertions(+), 38 deletions(-)
 create mode 100644 debian/acrn-lifemngr.postrm
 delete mode 100644 debian/acrn-lifemngr.service
 delete mode 100644 debian/lifemngr/acrn-lifemngr.conf
 create mode 100644 debian/lifemngr/life_mngr.conf.user_vm

diff --git a/debian/acrn-lifemngr.install b/debian/acrn-lifemngr.install
index ff9edb7da..6bf748508 100644
--- a/debian/acrn-lifemngr.install
+++ b/debian/acrn-lifemngr.install
@@ -1,2 +1,3 @@
 usr/bin/acrn-lifemngr
-usr/share/acrn-lifemngr/*
\ No newline at end of file
+usr/share/acrn-lifemngr/*
+lib/systemd/system/acrn-lifemngr.service
diff --git a/debian/acrn-lifemngr.postrm b/debian/acrn-lifemngr.postrm
new file mode 100644
index 000000000..7206ebe13
--- /dev/null
+++ b/debian/acrn-lifemngr.postrm
@@ -0,0 +1,46 @@
+#!/bin/sh
+# postrm script for grub-acrn
+#
+# see: dh_installdeb(1)
+
+set -e
+
+# summary of how this script can be called:
+#        * <postrm> `remove'
+#        * <postrm> `purge'
+#        * <old-postrm> `upgrade' <new-version>
+#        * <new-postrm> `failed-upgrade' <old-version>
+#        * <new-postrm> `abort-install'
+#        * <new-postrm> `abort-install' <old-version>
+#        * <new-postrm> `abort-upgrade' <old-version>
+#        * <disappearer's-postrm> `disappear' <overwriter>
+#          <overwriter-version>
+# for details, see https://www.debian.org/doc/debian-policy/ or
+# the debian-policy package
+
+
+case "$1" in
+    remove|purge)
+        # remove any preinstalled & unchanged configfile
+        if cmp -s "/etc/life_mngr/life_mngr.conf" "/usr/share/acrn-lifemngr/life_mngr.conf.service_vm" || \
+           cmp -s "/etc/life_mngr/life_mngr.conf" "/usr/share/acrn-lifemngr/life_mngr.conf.user_vm" ; then
+            rm -f /etc/life_mngr/life_mngr.conf
+            # also remove an empty config directory
+            [ "$(ls -A /etc/life_mngr)" ] && rm -rf /etc/life_mngr
+        fi
+    ;;
+    upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
+    ;;
+
+    *)
+        echo "postrm called with unknown argument \`$1'" >&2
+        exit 1
+    ;;
+esac
+
+# dh_installdeb will replace this with shell code automatically
+# generated by other debhelper scripts.
+
+#DEBHELPER#
+
+exit 0
diff --git a/debian/acrn-lifemngr.service b/debian/acrn-lifemngr.service
deleted file mode 100644
index 39877defe..000000000
--- a/debian/acrn-lifemngr.service
+++ /dev/null
@@ -1,13 +0,0 @@
-[Unit]
-Description=ACRN lifemngr daemon
-After=systemd-resolved.service
-
-[Service]
-Type=simple
-ExecStart=/usr/share/acrn-lifemngr/start-acrn-lifemngr.sh
-StandardOutput=journal
-StandardError=journal
-Restart=always
-
-[Install]
-WantedBy=multi-user.target
diff --git a/debian/control b/debian/control
index 5e9dc5f55..bdf7d8de2 100644
--- a/debian/control
+++ b/debian/control
@@ -120,7 +120,7 @@ Description: Public headers and libs for ACRN
 
 Package: acrn-lifemngr
 Architecture: amd64
-Depends: ${shlibs:Depends}, ${misc:Depends}
+Depends: ${shlibs:Depends}, ${misc:Depends}, setserial
 Suggests: acrn-doc
 Description: ACRN life manager
  ACRN is an open source reference hypervisor, built to meet the unique
diff --git a/debian/lifemngr/acrn-lifemngr.conf b/debian/lifemngr/acrn-lifemngr.conf
deleted file mode 100644
index 6a52ceae5..000000000
--- a/debian/lifemngr/acrn-lifemngr.conf
+++ /dev/null
@@ -1,9 +0,0 @@
-# ACRN lifemngr invocation configuration file
-
-# set the following items accordingly to override default:
-
-# Use 'sos' or 'uos' here. Usually detect automatically.
-# LIFEMNGR_VM=<vm type>
-
-# Use the respective serial port here:
-# LIFEMNGR_TTY=<serial port device>
diff --git a/debian/lifemngr/life_mngr.conf.user_vm b/debian/lifemngr/life_mngr.conf.user_vm
new file mode 100644
index 000000000..e3d84ef92
--- /dev/null
+++ b/debian/lifemngr/life_mngr.conf.user_vm
@@ -0,0 +1,20 @@
+# The VM type of a VM which lifecycle manager will run on
+# -service_vm	the lifecycle manager will be run on the service VM
+# -user_vm	the lifecycle manager will be run on the user VM
+VM_TYPE=user_vm
+
+# The VM name of a VM which lifecycle manager will run on, the name should
+# be unique in the whole system, this name will be embedded log message to
+# help user to do system debugging.
+VM_NAME=user_VM
+
+# The device type and device names of the devices which are used to
+# communicate with other VMs in lifecycle manager.
+# For user VM, the default value is tty:/dev/ttyS1;
+#
+# For service VM, the default setting rule of this field: /dev/ttyS8 is to
+# communicate with VM1 (VM ID in hypervisor = 1), /dev/ttyS9 is for VM2 (VM ID is 2),
+# /dev/ttyS10 is for VM3 (VM ID is 3) ... /dev/ttyS14 is for VM7 (VM ID is 7)
+# note: need to double check related communication vuarts are valid in hypervisor scenario config
+# file.
+DEV_NAME=tty:/dev/ttyS1
diff --git a/debian/lifemngr/start-acrn-lifemngr.sh b/debian/lifemngr/start-acrn-lifemngr.sh
index f7b9e92e4..85c7edff0 100755
--- a/debian/lifemngr/start-acrn-lifemngr.sh
+++ b/debian/lifemngr/start-acrn-lifemngr.sh
@@ -1,14 +1,16 @@
 #!/bin/sh
 # helper to start ACRN lifemnr according to VM type
 
-LIFEMNGR_CONF=/usr/share/acrn-lifemngr/acrn-lifemngr.conf
+# must coincide with fixed coding in
+# misc/services/life_mngr/config.h
+LIFE_MNGR_CONFIG_PATH="/etc/life_mngr/life_mngr.conf"
+# distinguish service/user VM
+LIFEMNGR_VM=${LIFEMNGR_VM:-$(if [ -c /dev/acrn_hsm ]; then echo service_vm; else echo user_vm; fi)}
 
-# eventually include configuration for overriding default configuration
-if [ -f ${LIFEMNGR_CONF} ]; then
-    . ${LIFEMNGR_CONF}
+# eventually install default configuration
+if [ ! -f ${LIFE_MNGR_CONFIG_PATH} ]; then
+    mkdir -p $(dirname ${LIFE_MNGR_CONFIG_PATH})
+    cp /usr/share/acrn-lifemngr/life_mngr.conf.${LIFEMNGR_VM} ${LIFE_MNGR_CONFIG_PATH}
 fi
 
-LIFEMNGR_VM=${LIFEMNGR_VM:-$(if [ -c /dev/acrn_hsm ]; then echo sos; else echo uos; fi)}
-LIFEMNGR_TTY=${LIFEMNGR_TTY:-/dev/ttyS1}
-
-/usr/bin/acrn-lifemngr ${LIFEMNGR_VM} ${LIFEMNGR_TTY}
+exec /usr/bin/acrn-lifemngr
diff --git a/debian/rules b/debian/rules
index 76244cbd2..bd234acec 100755
--- a/debian/rules
+++ b/debian/rules
@@ -245,14 +245,26 @@ override_dh_auto_install-arch:
 	$(Q)$(MAKE) O=$(O)/common DESTDIR=$(DESTDIR) tools-install $(devnull)
 	@:
 	@# install life_mngr
-	@$(call echo-silent,INSTALL devicemodel & tools)
-	$(Q)install -d $(DESTDIR)$(bindir)
-	@# install life_mngr as acrn-lifemngr
-	$(Q)install -m 0755 -T $(O)/common/misc/services/life_mngr $(DESTDIR)$(bindir)/acrn-lifemngr
-	@# install invocation helper and config
+	@$(call echo-silent,INSTALL life_mngr)
+	$(Q)mkdir -p $(O)/common/misc
+	$(Q)$(MAKE) -C misc                             \
+		DESTDIR=$(DESTDIR)                      \
+		OUT_DIR=$(abspath $(O)/common/misc)     \
+		acrn-life-mngr-install $(devnull)
+	@# rename to acrn-lifemngr
+	$(Q)mv $(DESTDIR)$(bindir)/life_mngr $(DESTDIR)$(bindir)/acrn-lifemngr
+	@# install config file templates
 	$(Q)install -d $(DESTDIR)$(datadir)/acrn-lifemngr
+	$(Q)mv $(DESTDIR)$(sysconfdir)/life_mngr/life_mngr.conf \
+	       $(DESTDIR)$(datadir)/acrn-lifemngr/life_mngr.conf.service_vm
+	$(Q)install -m 0644 debian/lifemngr/life_mngr.conf.user_vm $(DESTDIR)$(datadir)/acrn-lifemngr
+	@# install invocation helper
 	$(Q)install -m 0755 debian/lifemngr/start-acrn-lifemngr.sh $(DESTDIR)$(datadir)/acrn-lifemngr
-	$(Q)install -m 0644 debian/lifemngr/acrn-lifemngr.conf $(DESTDIR)$(datadir)/acrn-lifemngr
+	@# adapt life_mngr.service
+	$(Q)mv $(DESTDIR)$(systemd_unitdir)/system/life_mngr.service \
+	       $(DESTDIR)$(systemd_unitdir)/system/acrn-lifemngr.service
+	$(Q)sed -i -e 's#^ExecStart=.*#ExecStart=$(datadir)/acrn-lifemngr/start-acrn-lifemngr.sh#' \
+	        $(DESTDIR)$(systemd_unitdir)/system/acrn-lifemngr.service
 	@:
 	@# move/adapt network configs as examples only
 	$(Q)mkdir -p $(DESTDIR)$(docdir)/acrnd/examples