chromeos-init-systemd: Initialize device mapper devs

Device mapper udev rules assume that devices are pre-initialized with
DM_UDEV_PRIMARY_SOURCE_FLAG. This is only true for devices that are
created with libdevmapper. COS dm-verity devices are not created with
libdevmapper; they are created by the kernel using the "dm-mod.create"
kernel command line behavior.

The device mapper udev rules respond to "change" uevents regardless of
the DM_UDEV_PRIMARY_SOURCE_FLAG. To properly initialize device mapper
devices in udev, we can trigger a change message for available device
mapper devices on boot.

BUG=b/186546612
TEST=presubmit
RELEASE_NOTE=None

Change-Id: I51f8c67d7363b493e03a3664b18fb3a5cc25a450
Reviewed-on: https://cos-review.googlesource.com/c/cos/overlays/board-overlays/+/16036
Tested-by: Cusky Presubmit Bot <presubmit@cos-infra-prod.iam.gserviceaccount.com>
Reviewed-by: Anil Altinay <aaltinay@google.com>
diff --git a/project-lakitu/chromeos-base/chromeos-init-systemd/chromeos-init-systemd-0.0.1-r23.ebuild b/project-lakitu/chromeos-base/chromeos-init-systemd/chromeos-init-systemd-0.0.1-r24.ebuild
similarity index 100%
rename from project-lakitu/chromeos-base/chromeos-init-systemd/chromeos-init-systemd-0.0.1-r23.ebuild
rename to project-lakitu/chromeos-base/chromeos-init-systemd/chromeos-init-systemd-0.0.1-r24.ebuild
diff --git a/project-lakitu/chromeos-base/chromeos-init-systemd/chromeos-init-systemd-0.0.1.ebuild b/project-lakitu/chromeos-base/chromeos-init-systemd/chromeos-init-systemd-0.0.1.ebuild
index 4f763df..983dc11 100644
--- a/project-lakitu/chromeos-base/chromeos-init-systemd/chromeos-init-systemd-0.0.1.ebuild
+++ b/project-lakitu/chromeos-base/chromeos-init-systemd/chromeos-init-systemd-0.0.1.ebuild
@@ -47,6 +47,8 @@
 	systemd_dounit "${FILESDIR}"/check-secure-boot.service
 	systemd_enable_service multi-user.target check-secure-boot.service
 	systemd_dounit "${FILESDIR}"/prep-logs-dev@.service
+	systemd_dounit "${FILESDIR}"/init-dm-devs.service
+	systemd_enable_service local-fs-pre.target init-dm-devs.service
 
 	systemd_newtmpfilesd "${FILESDIR}"/chromeos-init.tmpfiles chromeos-init.conf
 
@@ -76,6 +78,7 @@
 	doexe "${FILESDIR}"/stateful-dev-sym-sorted
 	doexe "${FILESDIR}"/is-secure-boot
 	doexe "${FILESDIR}"/prep-logs-dev
+	doexe "${FILESDIR}"/init-dm-devs.sh
 }
 
 pkg_preinst() {
diff --git a/project-lakitu/chromeos-base/chromeos-init-systemd/files/init-dm-devs.service b/project-lakitu/chromeos-base/chromeos-init-systemd/files/init-dm-devs.service
new file mode 100644
index 0000000..0daa94a
--- /dev/null
+++ b/project-lakitu/chromeos-base/chromeos-init-systemd/files/init-dm-devs.service
@@ -0,0 +1,12 @@
+[Unit]
+Description=Initialize device mapper devices configured on kernel command line
+DefaultDependencies=no
+After=systemd-udev-trigger.service
+Before=sysinit.target
+
+[Service]
+Type=oneshot
+RemainAfterExit=yes
+ExecStart=/usr/share/cloud/init-dm-devs.sh
+StandardOutput=journal+console
+StandardError=journal+console
diff --git a/project-lakitu/chromeos-base/chromeos-init-systemd/files/init-dm-devs.sh b/project-lakitu/chromeos-base/chromeos-init-systemd/files/init-dm-devs.sh
new file mode 100644
index 0000000..e77bbd1
--- /dev/null
+++ b/project-lakitu/chromeos-base/chromeos-init-systemd/files/init-dm-devs.sh
@@ -0,0 +1,26 @@
+#!/bin/bash
+# Copyright 2021 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+set -o errexit
+
+main() {
+  local -r devices=(/dev/dm-*)
+  for dev in "${devices[@]}"; do
+    udevadm trigger "${dev}"
+  done
+}
+
+main "$@"