From 2e1650c6b1cd54462416b5d303343c6e7bf91527 Mon Sep 17 00:00:00 2001 From: rankaiyx Date: Sun, 8 Jun 2025 01:24:41 +0800 Subject: [PATCH 1/3] Update snap_pac.py to Solve the problem that pacman cannot run normally after rollback due to the pacman lock file in the snapshot. --- scripts/snap_pac.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/scripts/snap_pac.py b/scripts/snap_pac.py index 302238a..bc4b724 100755 --- a/scripts/snap_pac.py +++ b/scripts/snap_pac.py @@ -201,3 +201,18 @@ def parse_args(): data["description"], chroot, pre_number, data["userdata"])() logging.info(f"==> {snapper_config}: {num}") prefile.write(num) + + # Solve the problem that pacman cannot run normally after rollback due to the pacman lock file in the snapshot. + try: + # Set the snapshot to read-write mode + os.system(f"snapper --config {snapper_config} modify --read-write {num}") + + # Delete the specified file + file_path = f"/.snapshots/{num}/snapshot/var/lib/pacman/db.lck" + os.system(f"rm -f {file_path}") + + # Restore the snapshot to read-only mode + os.system(f"snapper --config {snapper_config} modify --read-only {num}") + except Exception as e: + logging.error(f"Error modifying snapshot {num}: {e}") + From 6126ca6515f9598d86047de63c37b29d7762fb03 Mon Sep 17 00:00:00 2001 From: rankaiyx Date: Sun, 8 Jun 2025 06:33:35 +0800 Subject: [PATCH 2/3] Update snap_pac.py fix a bug: kernel: BTRFS warning: Attempt to set subvolume xx read-write during send --- scripts/snap_pac.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/scripts/snap_pac.py b/scripts/snap_pac.py index bc4b724..a913adc 100755 --- a/scripts/snap_pac.py +++ b/scripts/snap_pac.py @@ -24,6 +24,8 @@ import os import sys import tempfile +import subprocess +import time logging.basicConfig(format="%(message)s", level=logging.INFO) @@ -204,15 +206,17 @@ def parse_args(): # Solve the problem that pacman cannot run normally after rollback due to the pacman lock file in the snapshot. try: - # Set the snapshot to read-write mode - os.system(f"snapper --config {snapper_config} modify --read-write {num}") + time.sleep(0.2) + subprocess.run(["btrfs", "property", "set", "-ts", f"/.snapshots/{num}/snapshot", "ro", "false"], check=True) - # Delete the specified file - file_path = f"/.snapshots/{num}/snapshot/var/lib/pacman/db.lck" - os.system(f"rm -f {file_path}") + lock_file = Path(f"/.snapshots/{num}/snapshot/var/lib/pacman/db.lck") + lock_file.unlink() - # Restore the snapshot to read-only mode - os.system(f"snapper --config {snapper_config} modify --read-only {num}") + subprocess.run(["btrfs", "property", "set", "-ts", f"/.snapshots/{num}/snapshot", "ro", "true"], check=True) + + except subprocess.CalledProcessError as e: + logging.error(f"Failed to modify snapshot properties: {e}") except Exception as e: - logging.error(f"Error modifying snapshot {num}: {e}") + logging.error(f"Error deleting db.lck: {e}") + From 871e4650a1280077a0029b262430391899cdca84 Mon Sep 17 00:00:00 2001 From: rankaiyx Date: Thu, 12 Jun 2025 01:52:04 +0800 Subject: [PATCH 3/3] An elegant new method with almost no side effects. --- scripts/snap_pac.py | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/scripts/snap_pac.py b/scripts/snap_pac.py index a913adc..27a53d1 100755 --- a/scripts/snap_pac.py +++ b/scripts/snap_pac.py @@ -25,7 +25,6 @@ import sys import tempfile import subprocess -import time logging.basicConfig(format="%(message)s", level=logging.INFO) @@ -197,26 +196,14 @@ def parse_args(): data = config_processor(snapper_config) if data["snapshot"]: + if os.path.isfile("/var/lib/pacman/db.lck"): + subprocess.run("mkdir -p /tmp/snap-pac", shell=True, check=True) + subprocess.run("mv /var/lib/pacman/db.lck /tmp/snap-pac/ && sync", shell=True, check=True) prefile = Prefile(snapper_config, args.type) pre_number = prefile.read() num = SnapperCmd(snapper_config, args.type, data["cleanup_algorithm"], data["description"], chroot, pre_number, data["userdata"])() logging.info(f"==> {snapper_config}: {num}") prefile.write(num) - - # Solve the problem that pacman cannot run normally after rollback due to the pacman lock file in the snapshot. - try: - time.sleep(0.2) - subprocess.run(["btrfs", "property", "set", "-ts", f"/.snapshots/{num}/snapshot", "ro", "false"], check=True) - - lock_file = Path(f"/.snapshots/{num}/snapshot/var/lib/pacman/db.lck") - lock_file.unlink() - - subprocess.run(["btrfs", "property", "set", "-ts", f"/.snapshots/{num}/snapshot", "ro", "true"], check=True) - - except subprocess.CalledProcessError as e: - logging.error(f"Failed to modify snapshot properties: {e}") - except Exception as e: - logging.error(f"Error deleting db.lck: {e}") - - + if os.path.isfile("/tmp/snap-pac/db.lck"): + subprocess.run("mv /tmp/snap-pac/db.lck /var/lib/pacman/ && sync", shell=True, check=True)