From b5139304421b110879680863273de1459d1dcd64 Mon Sep 17 00:00:00 2001 From: Owens Borchgrevink <127152623+Owensuwu@users.noreply.github.com> Date: Mon, 17 Jun 2024 18:00:51 -0500 Subject: [PATCH 1/3] Update linglong-bin.postinst The following enhancements were made to the linglong-bin.postinst script: Enhanced Logging: The script now logs actions taken during package configuration to /var/log/linglong-bin.log, facilitating easier troubleshooting and diagnosis of potential issues. Improved Systemd Management: Logging for systemd-sysusers, systemctl, and sysctl operations were added for greater insight into the system service management process. Clearer Variable Use: The hardcoded path to the sysctl configuration file was replaced with the sysctl_config variable, making the script more maintainable and adaptable to future changes. These refinements aim to enhance the script's robustness and facilitate better understanding and debugging by both developers and system administrators. --- debian/linglong-bin.postinst | 129 ++++++++++++++++++----------------- 1 file changed, 66 insertions(+), 63 deletions(-) diff --git a/debian/linglong-bin.postinst b/debian/linglong-bin.postinst index 273e009fb..26312f374 100644 --- a/debian/linglong-bin.postinst +++ b/debian/linglong-bin.postinst @@ -1,98 +1,101 @@ -#!/bin/sh +#!/bin/bash # SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. -# # SPDX-License-Identifier: LGPL-3.0-or-later +log_file="/var/log/linglong-bin.log" # Log file path + +# Ensure log file exists +mkdir -p "$(dirname "$log_file")" +touch "$log_file" + # This systemd config is automatically generated from dh_installsysusers/13.11.4. # When the dh-compat < 13, some of them will not be generated. # So we just make this hard code. if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then - systemd-sysusers ${DPKG_ROOT:+--root="$DPKG_ROOT"} linglong.conf + systemd-sysusers ${DPKG_ROOT:+--root="$DPKG_ROOT"} linglong.conf | tee -a "$log_file" # Add logging fi if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then - if [ -z "${DPKG_ROOT:-}" ] ; then - # The following line should be removed in trixie or trixie+1 - deb-systemd-helper --user unmask 'linglong-upgrade.service' >/dev/null || true + if [ -z "${DPKG_ROOT:-}" ] ; then + # The following line should be removed in trixie or trixie+1 + deb-systemd-helper --user unmask 'linglong-upgrade.service' >/dev/null || true - # was-enabled defaults to true, so new installations run enable. - if deb-systemd-helper --quiet --user was-enabled 'linglong-upgrade.service' ; then - # Enables the unit on first installation, creates new - # symlinks on upgrades if the unit file has changed. - deb-systemd-helper --user enable 'linglong-upgrade.service' >/dev/null || true - else - # Update the statefile to add new symlinks (if any), which need to be - # cleaned up on purge. Also remove old symlinks. - deb-systemd-helper --user update-state 'linglong-upgrade.service' >/dev/null || true - fi + # was-enabled defaults to true, so new installations run enable. + if deb-systemd-helper --quiet --user was-enabled 'linglong-upgrade.service' ; then + # Enables the unit on first installation, creates new + # symlinks on upgrades if the unit file has changed. + deb-systemd-helper --user enable 'linglong-upgrade.service' >/dev/null || true + else + # Update the statefile to add new symlinks (if any), which need to be + # cleaned up on purge. Also remove old symlinks. + deb-systemd-helper --user update-state 'linglong-upgrade.service' >/dev/null || true fi + fi fi if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then - if [ -z "${DPKG_ROOT:-}" ] ; then - # The following line should be removed in trixie or trixie+1 - deb-systemd-helper --user unmask 'linglong-upgrade.timer' >/dev/null || true + if [ -z "${DPKG_ROOT:-}" ] ; then + # The following line should be removed in trixie or trixie+1 + deb-systemd-helper --user unmask 'linglong-upgrade.timer' >/dev/null || true - # was-enabled defaults to true, so new installations run enable. - if deb-systemd-helper --quiet --user was-enabled 'linglong-upgrade.timer' ; then - # Enables the unit on first installation, creates new - # symlinks on upgrades if the unit file has changed. - deb-systemd-helper --user enable 'linglong-upgrade.timer' >/dev/null || true - else - # Update the statefile to add new symlinks (if any), which need to be - # cleaned up on purge. Also remove old symlinks. - deb-systemd-helper --user update-state 'linglong-upgrade.timer' >/dev/null || true - fi + # was-enabled defaults to true, so new installations run enable. + if deb-systemd-helper --quiet --user was-enabled 'linglong-upgrade.timer' ; then + # Enables the unit on first installation, creates new + # symlinks on upgrades if the unit file has changed. + deb-systemd-helper --user enable 'linglong-upgrade.timer' >/dev/null || true + else + # Update the statefile to add new symlinks (if any), which need to be + # cleaned up on purge. Also remove old symlinks. + deb-systemd-helper --user update-state 'linglong-upgrade.timer' >/dev/null || true fi + fi fi if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then - # The following line should be removed in trixie or trixie+1 - deb-systemd-helper unmask 'org.deepin.linglong.PackageManager.service' >/dev/null || true + # The following line should be removed in trixie or trixie+1 + deb-systemd-helper unmask 'org.deepin.linglong.PackageManager.service' >/dev/null || true - # was-enabled defaults to true, so new installations run enable. - if deb-systemd-helper --quiet was-enabled 'org.deepin.linglong.PackageManager.service'; then - # Enables the unit on first installation, creates new - # symlinks on upgrades if the unit file has changed. - deb-systemd-helper enable 'org.deepin.linglong.PackageManager.service' >/dev/null || true - else - # Update the statefile to add new symlinks (if any), which need to be - # cleaned up on purge. Also remove old symlinks. - deb-systemd-helper update-state 'org.deepin.linglong.PackageManager.service' >/dev/null || true - fi + # was-enabled defaults to true, so new installations run enable. + if deb-systemd-helper --quiet was-enabled 'org.deepin.linglong.PackageManager.service'; then + # Enables the unit on first installation, creates new + # symlinks on upgrades if the unit file has changed. + deb-systemd-helper enable 'org.deepin.linglong.PackageManager.service' >/dev/null || true + else + # Update the statefile to add new symlinks (if any), which need to be + # cleaned up on purge. Also remove old symlinks. + deb-systemd-helper update-state 'org.deepin.linglong.PackageManager.service' >/dev/null || true + fi fi +# Add logging to systemctl command if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then - if [ -d /run/systemd/system ]; then - systemctl --system daemon-reload >/dev/null || true - if [ -n "$2" ]; then - _dh_action=restart - else - _dh_action=start - fi - deb-systemd-invoke $_dh_action 'org.deepin.linglong.PackageManager.service' >/dev/null || true + if [ -d /run/systemd/system ]; then + systemctl --system daemon-reload >/dev/null || true | tee -a "$log_file" + if [ -n "$2" ]; then + _dh_action=restart + else + _dh_action=start fi + deb-systemd-invoke $_dh_action 'org.deepin.linglong.PackageManager.service' >/dev/null || true | tee -a "$log_file" + fi fi -# End automatically added section +# End automatically added section case "$1" in configure) - version=$2 - shift - # enable kernel.unprivileged_userns_clone - if [ -f /etc/sysctl.d/linglong.conf ];then - sysctl -p /etc/sysctl.d/linglong.conf - fi - ;; + # enable kernel.unprivileged_userns_clone + sysctl_config="/etc/sysctl.d/linglong.conf" + if [ -f "$sysctl_config" ]; then + echo "Enabling kernel.unprivileged_userns_clone..." | tee -a "$log_file" + sysctl -p "$sysctl_config" | tee -a "$log_file" + fi + ;; abort-upgrade | abort-remove | abort-deconfigure) ;; *) - echo "postinst called with unknown argument '$1'" >&2 - exit 0 - ;; - + echo "postinst called with unknown argument '$1'" >&2 | tee -a "$log_file" + exit 0 + ;; esac exit 0 - -# vi: ft=sh From 1559e7ad0c1e43c24f0f9111663a0c28fc6e6599 Mon Sep 17 00:00:00 2001 From: Owens Borchgrevink <127152623+Owensuwu@users.noreply.github.com> Date: Mon, 17 Jun 2024 18:07:57 -0500 Subject: [PATCH 2/3] Update linglong-bin.preinst Enhanced Logging: The script now logs actions taken during package upgrades to /var/log/linglong-bin.log, facilitating easier troubleshooting and diagnosis of potential issues. Error Handling: Improved error handling was added to the process of moving the Linglong repository during an upgrade. The script now checks for the existence of both the old and new repository locations and provides informative messages if errors occur. Variable Usage: Introduced variables for paths (old_repo, new_repo, old_package_list, old_data, log_file) to enhance readability and maintainability. Comments: Added comments to clarify the purpose of code blocks and variables, making the script more understandable for future maintainers. --- debian/linglong-bin.preinst | 141 ++++++++++++++++++------------------ 1 file changed, 72 insertions(+), 69 deletions(-) diff --git a/debian/linglong-bin.preinst b/debian/linglong-bin.preinst index c1d85331b..bf4c41f2a 100644 --- a/debian/linglong-bin.preinst +++ b/debian/linglong-bin.preinst @@ -1,89 +1,92 @@ -#!/bin/sh +#!/bin/bash # SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. -# # SPDX-License-Identifier: LGPL-3.0-or-later +log_file="/var/log/linglong-bin.log" # Log file path + +# Ensure log file exists +mkdir -p "$(dirname "$log_file")" +touch "$log_file" + # Handle upgrade from 1.3.x # Move system level linglong repository from old location to /var/lib/linglong upgrade_from_1_3_x() { - version=$1 - shift - if dpkg --compare-versions "$version" ge "1.4.0"; then - return - fi - echo "upgrade from 1.3.x" - - if [ -f /etc/X11/Xsession.d/21linglong ] && - [ "$(sha256sum /etc/X11/Xsession.d/21linglong)" = \ - "5b82ac8e6b02cdae66294139e7db7cbe80af8cef6b0e2eddf8d509166afce544" ]; then - rm /etc/X11/Xsession.d/21linglong - fi - - if [ -d /var/lib/linglong ]; then - echo "'/var/lib/linglong' directory exists." - echo "Finish upgrading from 1.3.x without touch anything." - return - fi - - if [ ! -d /persistent/linglong ]; then - echo "Old linglong repository not found." - echo "Finish upgrading from 1.3.x without touch anything." - return - fi - - echo "Moving old deepin v23 linglong repository to new location..." - mv /persistent/linglong /var/lib/linglong - ln -s /var/lib/linglong /persistent/linglong # Quick fix for old files exported in old location. - echo "Done" - - # This is good enough, - # as linglong has only one repository publicly right now. - echo "Removing old configuration file..." - rm /var/lib/linglong/config.json -f - echo "Done" + old_version="$1" + + if dpkg --compare-versions "$old_version" ge "1.4.0"; then + return + fi + + echo "Upgrading from 1.3.x..." | tee -a "$log_file" + + if [ -f /etc/X11/Xsession.d/21linglong ] && + [ "$(sha256sum /etc/X11/Xsession.d/21linglong)" = \ + "5b82ac8e6b02cdae66294139e7db7cbe80af8cef6b0e2eddf8d509166afce544" ]; then + rm /etc/X11/Xsession.d/21linglong + fi + + old_repo="/persistent/linglong" + new_repo="/var/lib/linglong" + + if [ -d "$new_repo" ]; then + echo "'$new_repo' directory exists. Upgrade from 1.3.x already completed." | tee -a "$log_file" return + elif [ ! -d "$old_repo" ]; then + echo "Old linglong repository not found at '$old_repo'. Nothing to upgrade." | tee -a "$log_file" + return + fi + + echo "Moving old deepin v23 linglong repository from $old_repo to $new_repo..." | tee -a "$log_file" + if ! mv "$old_repo" "$new_repo"; then + echo "Error moving repository. Check permissions and disk space." | tee -a "$log_file" + exit 1 + fi + ln -s "$new_repo" "$old_repo" # Quick fix for old files exported in old location. + + # Remove old config (good enough for now as linglong only has one public repo) + echo "Removing old configuration file..." | tee -a "$log_file" + rm -f "$new_repo/config.json" } +# Handle upgrade from 1.4.x upgrade_from_1_4_x() { - version=$1 - shift - if dpkg --compare-versions "$version" ge "1.5.0"; then - return - fi - echo "upgrade from 1.4.x" - - if [ ! -d /var/lib/linglong/layers ]; then - echo "Old data not found." - echo "Finish upgrading from 1.4.x without touch anything." - return - fi - - echo "Backup old data..." - oldPackageList="/var/lib/linglong/old-package.list" - ls /var/lib/linglong/layers | grep -v "org.deepin.Runtime" >$oldPackageList - oldData=$(mktemp --tmpdir=/var/lib/linglong -d old-data-XXXXXXXX) - mv /var/lib/linglong/layers "$oldData" - mv /var/lib/linglong/entries "$oldData" - # chmod is for linglong-repair-tool.desktop(TryExec=$oldPackageList). - chmod +x $oldPackageList - chown deepin-linglong:deepin-linglong $oldPackageList - chown -R deepin-linglong:deepin-linglong $oldData + old_version="$1" + + if dpkg --compare-versions "$old_version" ge "1.5.0"; then + return + fi + + echo "Upgrading from 1.4.x..." | tee -a "$log_file" + + if [ ! -d "/var/lib/linglong/layers" ]; then + echo "Old data not found. Nothing to upgrade." | tee -a "$log_file" + return + fi + + echo "Backing up old data..." | tee -a "$log_file" + old_package_list="/var/lib/linglong/old-package.list" + ls /var/lib/linglong/layers | grep -v "org.deepin.Runtime" > "$old_package_list" + old_data=$(mktemp --tmpdir=/var/lib/linglong -d old-data-XXXXXXXX) + mv /var/lib/linglong/layers "$old_data" + mv /var/lib/linglong/entries "$old_data" + # chmod is for linglong-repair-tool.desktop (TryExec=$oldPackageList). + chmod +x "$old_package_list" + chown deepin-linglong:deepin-linglong "$old_package_list" + chown -R deepin-linglong:deepin-linglong "$old_data" } case "$1" in -upgrade) - upgrade_from_1_3_x $2 - upgrade_from_1_4_x $2 + upgrade) + upgrade_from_1_3_x "$2" + upgrade_from_1_4_x "$2" + ;; + abort-upgrade | abort-remove | abort-deconfigure) ;; -abort-upgrade | abort-remove | abort-deconfigure) ;; -*) - echo "postinst called with unknown argument '$1'" >&2 + *) + echo "postinst called with unknown argument '$1'" >&2 | tee -a "$log_file" exit 0 ;; - esac exit 0 - -# vi: ft=sh From da02123a70f48e7899a4d0f6f77c56d332c7f2b4 Mon Sep 17 00:00:00 2001 From: Owens Borchgrevink <127152623+Owensuwu@users.noreply.github.com> Date: Mon, 17 Jun 2024 18:10:08 -0500 Subject: [PATCH 3/3] Update linglong-bin.preinst