halimg: Add hal-rpmdb-checker.service to check rpm version compatibility 95/258895/3
authorChanwoo Choi <cw00.choi@samsung.com>
Thu, 29 Apr 2021 03:14:16 +0000 (12:14 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Mon, 21 Jun 2021 08:58:39 +0000 (08:58 +0000)
For removing the h/w dependency from Tizen core image, decide to create
Tizen core image (core.img) and hal.img separately. It means that each
image is able to be built on different build time. So that in order to
guarnatee the compatibility between images, must need to check
the version compatiblity of rpm packages of hal backend packages in hal.img.

hal-rpmdb-checker.service will check the  the version compatiblity
between core.img and hal.img on booting time.

If there are no problem between images, hal-rpmdb-checker script
will create /opt/etc/hal/.hal-img-version.ini with hal.img build
information. Also, after writing the core.img, hal-rpmdb-checker
script will create /opt/etc/hal/.first-booting file to distinguish
whether booting is first or not.

Exmaple of /opt/etc/hal/.hal-img-version.ini
$cat /opt/etc/hal/.hal-img-version.ini
tizen-6.5-unified-gbm_20210525.030006;

Change-Id: I06c740d48478eae6671cec555e627e58e06ea119
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
packaging/hal-api-common.spec
packaging/hal-rpmdb-checker [new file with mode: 0755]
packaging/hal-rpmdb-checker.service [new file with mode: 0644]

index 1844696..206d858 100644 (file)
@@ -17,6 +17,8 @@ Source4:    macros.hal-api
 Source5:    haltest.target
 Source6:    reboot-haltest
 Source7:    reboot-normal
+Source8:    hal-rpmdb-checker.service
+Source9:    hal-rpmdb-checker
 
 Requires(post): /sbin/ldconfig
 Requires(postun): /sbin/ldconfig
@@ -53,6 +55,8 @@ Haltests for hal-api-common
 %prep
 %setup -q
 
+%define hal_rpmdb_checker_path /opt/etc/hal
+
 cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} -DCMAKE_LIBDIR_PREFIX=%{_libdir}
 
 %build
@@ -67,18 +71,24 @@ rm -rf %{buildroot}
 %make_install
 
 mkdir -p %{buildroot}/hal
+mkdir -p %{buildroot}%{hal_rpmdb_checker_path}
 install -D -m 0644 %{SOURCE2} %{buildroot}%{_sysconfdir}/ld.so.conf.d/libhal-api.conf
 install -D -m 0755 %{SOURCE3} %{buildroot}%{_systemdgeneratordir}/systemd-hal-firmware-generator
 install -D -m 0644 %{SOURCE4} %{buildroot}%{_sysconfdir}/rpm/macros.hal-api
 install -D -m 0644 %{SOURCE5} %{buildroot}%{_unitdir}/haltest.target
 install -D -m 0755 %{SOURCE6} %{buildroot}%{_bindir}/reboot-haltest
 install -D -m 0755 %{SOURCE7} %{buildroot}%{_bindir}/reboot-normal
+install -D -m 0644 %{SOURCE8} %{buildroot}%{_unitdir}/hal-rpmdb-checker.service
+install -D -m 0755 %{SOURCE9} %{buildroot}%{_bindir}/hal-rpmdb-checker
+
+%install_service sysinit.target.wants hal-rpmdb-checker.service
 
 %clean
 rm -rf %{buildroot}
 
 %post
 /sbin/ldconfig
+chsmack -a "System" -t %{hal_rpmdb_checker_path}
 
 if [ ! -d %{TZ_SYS_RO_ETC} ]
 then
@@ -106,7 +116,12 @@ fi
 %{_sysconfdir}/rpm/macros.hal-api
 
 %files -n %{test_name}
+%dir %{hal_rpmdb_checker_path}
+%attr(0755,system_fw,system_fw) %{hal_rpmdb_checker_path}
 %{_bindir}/hal/common-haltests
 %{_unitdir}/haltest.target
 %{_bindir}/reboot-haltest
 %{_bindir}/reboot-normal
+%{_unitdir}/sysinit.target.wants/hal-rpmdb-checker.service
+%{_unitdir}/hal-rpmdb-checker.service
+%{_bindir}/hal-rpmdb-checker
diff --git a/packaging/hal-rpmdb-checker b/packaging/hal-rpmdb-checker
new file mode 100755 (executable)
index 0000000..ed11f83
--- /dev/null
@@ -0,0 +1,186 @@
+#!/bin/bash
+PATH=/bin:/usr/bin:/sbin:/usr/sbin
+
+### Define constant variables
+UNKNOWN="UNKNOWN"
+ROOT_OWNER="root"
+
+HAL_RPMDB_DIR_PATH='/hal/lib/rpm'
+HAL_RPMDB_ETC_DIR_PATH='/hal/etc/rpm'
+HAL_RPMDB_FILE_PATH='/hal/etc/hal-rpmdb.ini'
+HAL_IMG_INFO_FILE_PATH='/hal/etc/hal-info.ini'
+
+FIRST_BOOTING_FILE_PATH="/opt/etc/hal/.first-booting"
+HAL_IMG_VERSION_FILE_PATH="/opt/etc/hal/.hal-img-version.ini"
+
+### Define variables
+first_booting=0
+hal_img=0
+hal_rpmdb_match=1
+hal_img_version=$UNKNOWN
+cur_hal_img_version=$UNKNOWN
+owner=$UNKNOWN
+
+### Define functions
+
+backup_core_rpmdb() {
+       if [ "$hal_img" -eq 0 ]; then
+               return
+       elif [ "$owner" != "$ROOT_OWNER" ]; then
+               return
+       elif [ "$first_booting" -eq 0 ]; then
+               return
+       fi
+
+       if ! [ -e /opt/etc/hal/Packages.origin ] ; then
+               cp /var/lib/rpm/Packages /opt/etc/hal/Packages.origin
+               echo "HAL_RPMDB: Backup Core RPMDB"
+       fi
+}
+
+import_hal_rpmdb_to_core_rpmdb() {
+       if [ "$hal_img" -eq 0 ]; then
+               return
+       elif [ "$hal_rpmdb_match" -eq 0 ]; then
+               return
+       elif [ "$owner" != "$ROOT_OWNER" ]; then
+               return
+       fi
+
+       # Restore core-rpmdb from core-rpmdb backup
+       if [ -e /opt/etc/hal/Packages.origin ] ; then
+               rm -rf /var/lib/rpm/Packages
+               cp /opt/etc/hal/Packages.origin /var/lib/rpm/Packages
+               echo "HAL_RPMDB: Restore Core RPMDB"
+       fi
+
+       # Import hal-rpmdb into core-rpmdb
+       if [ -e "$HAL_RPMDB_FILE_PATH" ] ; then
+               /usr/bin/rpmdb --importdb < $HAL_RPMDB_FILE_PATH
+               echo "HAL_RPMDB: Import hal-rpdmb into core-rpmdb"
+       else
+               echo "HAL_RPMDB: Failed to find $HAL_RPMDB_FILE_PATH"
+       fi
+}
+
+init() {
+       first_booting=0
+       hal_img=0
+       owner=`whoami`
+       local_hal_part=`/usr/bin/lsblk | grep hal`
+
+       if [ "$local_hal_part" == "" ]; then
+               echo "HAL_RPMDB: It doesnt' contain both /hal partition and hal.img"
+               exit 0
+       elif ! [ -e "$HAL_RPMDB_DIR_PATH" ] ; then
+               echo "HAL_RPMDB: Failed to find $HAL_RPMDB_DIR_PATH path"
+               exit 0
+       elif ! [ -e "$HAL_RPMDB_ETC_DIR_PATH" ] ; then
+               echo "HAL_RPMDB: Failed to find $HAL_RPMDB_ETC_DIR_PATH path"
+               exit 0
+       elif ! [ -e "$HAL_IMG_INFO_FILE_PATH" ] ; then
+               echo "HAL_RPMDB: Failed to find $HAL_IMG_INFO_FILE_PATH"
+               exit 0
+       else
+               if ! [ -e /opt/etc/hal/.first-booting ] ; then
+                       /usr/bin/echo "0" > $FIRST_BOOTING_FILE_PATH
+                       first_booting=1
+               fi
+
+               hal_img=1
+
+               backup_core_rpmdb
+       fi
+}
+
+get_hal_img_version() {
+       if [ "$hal_img" -eq 0 ]; then
+               return
+       fi
+
+       # Get hal_img_version from /hal/etc/info.ini
+       tmp=`cat $HAL_IMG_INFO_FILE_PATH | grep Build=`
+       hal_img_version=`echo ${tmp:6}`
+
+       # Get current hal_img_version from $HAL_IMG_VERSION_FILE_PATH
+
+       if ! [ -e "$HAL_IMG_VERSION_FILE_PATH" ] ; then
+               cur_hal_img_version="$HAL_IMG_VERSION_UNKNOWN"
+       else
+               cur_hal_img_version=`cat $HAL_IMG_VERSION_FILE_PATH`
+       fi
+
+       echo "HAL_RPMDB: hal.img version ($hal_img_version) of /hal/etc/hal-info.ini"
+       echo "HAL_RPMDB: hal.img version ($cur_hal_img_version) of $HAL_IMG_VERSION_FILE_PATH"
+}
+
+check_hal_rpmdb() {
+       if [ "$hal_img" -eq 0 ]; then
+               return
+       fi
+
+       echo "HAL_RPMDB: Start hal-rpmdb  dependency check ... "
+
+       hal_rpmdb_match=1
+       HAL_BACKEND_PKGS=`/usr/bin/ls $HAL_RPMDB_ETC_DIR_PATH`
+       for hal_backend_pkg in ${HAL_BACKEND_PKGS}; do
+               while read line; do
+                       IS_INSTALLED_IN_RPM=`/usr/bin/rpm -q $line`
+
+                       if [[ $IS_INSTALLED_IN_RPM != $line ]]; then
+                               IS_INSTALLED_IN_HAL_RPM=`/usr/bin/rpm --dbpath=$HAL_RPMDB_DIR_PATH -q $line`
+
+                               if [[ $IS_INSTALLED_IN_HAL_RPM != $line ]]; then
+                                       echo "HAL_RPMDB: WARNING!!! $line is not installed for $hal_backend_pkg"
+                                       hal_rpmdb_match=0
+                               fi
+                       fi
+               done < $HAL_RPMDB_ETC_DIR_PATH/$hal_backend_pkg
+       done
+
+       if [ "$hal_rpmdb_match" -eq 1 ]; then
+               # Update hal.img version when version matched
+               /usr/bin/echo "$hal_img_version" > $HAL_IMG_VERSION_FILE_PATH
+
+               echo "HAL_RPMDB: Finish hal-rpmdb dependency check (Version Matched) "
+       else
+               # Remove hal.img version file when version mismatched
+               if [ -e "$HAL_IMG_VERSION_FILE_PATH" ] ; then
+                       rm -rf $HAL_IMG_VERSION_FILE_PATH
+               fi
+
+               echo "HAL_RPMDB: Finish hal-rpmdb dependency check (Version Mismatched) "
+               exit 0
+       fi
+}
+
+main() {
+       # Check whether h/w device is using hal.img or not
+       init
+       # Get hal.img version info
+       get_hal_img_version
+
+       if [ "$cur_hal_img_version" == "$hal_img_version" ] ; then
+               # Have finished the version check of hal rpmdb
+               # and imported hal rpmdb into core rpmdb.
+               echo "HAL_RPMDB: Already Version Matched!"
+               return
+       elif [ "$cur_hal_img_version" == "$HAL_IMG_VERSION_UNKNOWN" ] ; then
+               # In case of among,
+               # - first booting or
+               # - core.img is only updated or
+               # - hal.img was only updated but, version mismatched
+               check_hal_rpmdb
+       else
+               # In case of hal.img is only updated, must need to restore
+               # the core rpmdb and then execute check_hal_rpmdb
+               echo "HAL_RPMDB: hal.img is only updated!"
+               check_hal_rpmdb
+
+               # Import hal-rpmdb (/hal/lib/rpm) into core-rpmdb (/var/lib/rpm)
+               import_hal_rpmdb_to_core_rpmdb
+       fi
+}
+
+### Execute main function
+main
diff --git a/packaging/hal-rpmdb-checker.service b/packaging/hal-rpmdb-checker.service
new file mode 100644 (file)
index 0000000..4780d46
--- /dev/null
@@ -0,0 +1,18 @@
+[Unit]
+Description=HAL RPMDB Checker
+RefuseManualStart=yes
+RefuseManualStop=yes
+DefaultDependencies=no
+After=local-fs.target systemd-tmpfiles-setup.service
+Before=shutdown.target
+
+[Service]
+Type=oneshot
+RemainAfterExit=yes
+ExecStart=/usr/bin/hal-rpmdb-checker
+SmackProcessLabel=System
+User=system_fw
+Group=system_fw
+
+[Install]
+WantedBy=sysinit.target