modified to use a single R/W snapshot

This commit is contained in:
ak95 2024-09-03 17:00:45 -07:00
parent d58d047d2a
commit 74a055dfe6
5 changed files with 20 additions and 116 deletions

View file

@ -1,40 +1,25 @@
# snap-pac # snap-pac
![Tests](https://github.com/wesbarnett/snap-pac/workflows/Tests/badge.svg)
![Docs](https://github.com/wesbarnett/snap-pac/workflows/Docs/badge.svg)
## Synopsis ## Synopsis
This is a set of [pacman](https://wiki.archlinux.org/index.php/Pacman) hooks and script This is a set of [pacman](https://wiki.archlinux.org/index.php/Pacman) hooks and scripts that automatically cause [snapper](http://snapper.io/) to perform a snapshot before pacman transactions.
that automatically causes [snapper](http://snapper.io/) to perform a pre and post
snapshot before and after pacman transactions, similar to how YaST does with OpenSuse.
This provides a simple way to undo changes to a system after a pacman transaction.
For more information, [see the documentation](https://wesbarnett.github.io/snap-pac/). Forked from Wes Barnett's solution for my own personal needs. Takes a single screenshot vs. the original's pre/post pair.
## Installation ## [Installation] (https://wesbarnett.github.io/snap-pac/installation.html).
Install the `snap-pac` package using pacman.
For instructions on how to install without pacman, [see the
documentation](https://wesbarnett.github.io/snap-pac/installation.html).
## Configuration ## Configuration
Most likely, configuration is not needed. By default, the snapper configuration named Most likely, configuration is not needed. By default, the snapper configuration named `root` will have snapshots taken before every pacman transaction.
`root` will have pre/post snapshots taken for every pacman transaction.
For more information on configuring snap-pac, see [the For more information on configuring snap-pac, see [the documentation](https://wesbarnett.github.io/snap-pac/configuration.html).
documentation](https://wesbarnett.github.io/snap-pac/configuration.html).
## Documentation ## Documentation
See the [documentation here](https://wesbarnett.github.io/snap-pac/) or `man 8 snap-pac` See the [documentation here](https://wesbarnett.github.io/snap-pac/) or `man 8 snap-pac` after installation.
after installation.
## Troubleshooting ## Troubleshooting
After reviewing the documentation, [check the issues page] and file a new issue if your After reviewing the documentation, [check the issues page] and file a new issue if your problem is not covered.
problem is not covered.
[check the issues page]: https://github.com/wesbarnett/snap-pac/issues [check the issues page]: https://codeberg.org/ak95/snap-pac/issues

View file

@ -20,14 +20,6 @@
## Default is number ## Default is number
#cleanup_algorithm = number #cleanup_algorithm = number
## Pre snapshot description.
## Default is the pacman command that triggered the hook
#pre_description = pacman pre snapshot
## Post snapshot description.
## Default is the list of packages involved in the pacman transaction
#post_description = pacman post snapshot
## Uncomment to add "important=yes" to userdata for snapshots referring to these packages ## Uncomment to add "important=yes" to userdata for snapshots referring to these packages
## Default is [] ## Default is []
#important_packages = ["linux", "linux-lts"] #important_packages = ["linux", "linux-lts"]

View file

@ -24,9 +24,9 @@ Type = Package
Target = * Target = *
[Action] [Action]
Description = Performing snapper pre snapshots for the following configurations... Description = Taking snapper snapshot before installing...
Depends = snap-pac Depends = snap-pac
When = PreTransaction When = PreTransaction
Exec = /usr/share/libalpm/scripts/snap-pac pre Exec = /usr/share/libalpm/scripts/snap-pac
NeedsTargets NeedsTargets
AbortOnFail AbortOnFail

View file

@ -1,31 +0,0 @@
# snap-pac
# https://github.com/wesbarnett/snap-pac
# Copyright (C) 2016, 2017, 2018 James W. Barnett
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
[Trigger]
Operation = Upgrade
Operation = Install
Operation = Remove
Type = Package
Target = *
[Action]
Description = Performing snapper post snapshots for the following configurations...
Depends = snap-pac
When = PostTransaction
Exec = /usr/share/libalpm/scripts/snap-pac post
NeedsTargets

View file

@ -14,7 +14,7 @@
# You should have received a copy of the GNU General Public License along # You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc., # with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""Script for taking pre/post snapshots; run from pacman hooks.""" """Script for taking snapshots; run from pacman hooks."""
from argparse import ArgumentParser from argparse import ArgumentParser
from configparser import ConfigParser from configparser import ConfigParser
@ -31,8 +31,7 @@ logging.basicConfig(format="%(message)s", level=logging.INFO)
class SnapperCmd: class SnapperCmd:
def __init__(self, config, snapshot_type, cleanup_algorithm, description="", def __init__(self, config, cleanup_algorithm, description="", nodbus=False, userdata=""):
nodbus=False, pre_number=None, userdata=""):
self.cmd = ["snapper"] self.cmd = ["snapper"]
if nodbus: if nodbus:
self.cmd.append("--no-dbus") self.cmd.append("--no-dbus")
@ -41,19 +40,11 @@ class SnapperCmd:
f"--cleanup-algorithm {cleanup_algorithm}", f"--cleanup-algorithm {cleanup_algorithm}",
"--print-number" "--print-number"
]) ])
self.cmd.append("--read-write")
if description: if description:
self.cmd.append(f"--description \"{description}\"") self.cmd.append(f"--description \"{description}\"")
if userdata: if userdata:
self.cmd.append(f"--userdata \"{userdata}\"") self.cmd.append(f"--userdata \"{userdata}\"")
if snapshot_type == "post":
if pre_number is not None:
self.cmd.append(f"--pre-number {pre_number}")
else:
logging.debug("snapshot type specified as 'post' but no pre snapshot number, "
"so setting snapshot type to 'single'. If installing "
"snap-pac this is normal.")
snapshot_type = "single"
self.cmd.append(f"--type {snapshot_type}")
def __call__(self): def __call__(self):
return os.popen(self.__str__()).read().rstrip("\n") return os.popen(self.__str__()).read().rstrip("\n")
@ -64,7 +55,7 @@ class SnapperCmd:
class ConfigProcessor: class ConfigProcessor:
def __init__(self, ini_file, snapshot_type, parent_cmd=None, packages=None): def __init__(self, ini_file, parent_cmd=None, packages=None):
"""Set up defaults for snap-pac configuration.""" """Set up defaults for snap-pac configuration."""
if parent_cmd is None: if parent_cmd is None:
@ -77,14 +68,11 @@ class ConfigProcessor:
else: else:
self.packages = packages self.packages = packages
self.snapshot_type = snapshot_type
self.config = ConfigParser() self.config = ConfigParser()
self.config["DEFAULT"] = { self.config["DEFAULT"] = {
"snapshot": False, "snapshot": False,
"cleanup_algorithm": "number", "cleanup_algorithm": "number",
"pre_description": self.parent_cmd, "description": self.parent_cmd,
"post_description": " ".join(self.packages),
"desc_limit": 72, "desc_limit": 72,
"important_packages": [], "important_packages": [],
"important_commands": [], "important_commands": [],
@ -100,7 +88,7 @@ class ConfigProcessor:
def get_description(self, section): def get_description(self, section):
desc_limit = self.config.getint(section, "desc_limit") desc_limit = self.config.getint(section, "desc_limit")
return self.config.get(section, f"{self.snapshot_type}_description")[:desc_limit] return self.config.get(section, "description")[:desc_limit]
def check_important_commands(self, section): def check_important_commands(self, section):
return self.parent_cmd in json.loads(self.config.get(section, "important_commands")) return self.parent_cmd in json.loads(self.config.get(section, "important_commands"))
@ -138,38 +126,12 @@ def get_snapper_configs(conf_file):
return line[1].lstrip("\"").split() return line[1].lstrip("\"").split()
class Prefile:
"""Handles reading and writing of pre snapshot number."""
def __init__(self, snapper_config, snapshot_type):
self.file = Path(tempfile.gettempdir()) / f"snap-pac-pre_{snapper_config}"
self.snapshot_type = snapshot_type
def read(self):
if self.snapshot_type == "pre":
pre_number = None
else:
try:
pre_number = self.file.read_text().rstrip("\n")
except FileNotFoundError:
pre_number = None
logging.debug(f"prefile {self.file} not found. Ensure you have run the pre snapshot first. "
"If installing snap-pac this is normal.")
else:
self.file.unlink()
return pre_number
def write(self, num):
if self.snapshot_type == "pre":
self.file.write_text(num)
def check_skip(): def check_skip():
return os.getenv("SNAP_PAC_SKIP", "n").lower() in ["y", "yes", "true", "1"] return os.getenv("SNAP_PAC_SKIP", "n").lower() in ["y", "yes", "true", "1"]
def parse_args(): def parse_args():
parser = ArgumentParser(description="Script for taking pre/post snapper snapshots. Used with pacman hooks.") parser = ArgumentParser(description="Script for taking snapper snapshots. Used with pacman hooks.")
parser.add_argument(dest="type", choices=["pre", "post"], help="snapper snapshot type")
parser.add_argument( parser.add_argument(
"--ini", dest="snap_pac_ini", type=Path, "--ini", dest="snap_pac_ini", type=Path,
default=Path("/etc/snap-pac.ini"), help="snap-pac ini file path" default=Path("/etc/snap-pac.ini"), help="snap-pac ini file path"
@ -188,16 +150,12 @@ if __name__ == "__main__":
quit() quit()
args = parse_args() args = parse_args()
config_processor = ConfigProcessor(args.snap_pac_ini, args.type) config_processor = ConfigProcessor(args.snap_pac_ini)
chroot = os.stat("/") != os.stat("/proc/1/root/.") chroot = os.stat("/") != os.stat("/proc/1/root/.")
for snapper_config in get_snapper_configs(args.snapper_conf_file): for snapper_config in get_snapper_configs(args.snapper_conf_file):
data = config_processor(snapper_config) data = config_processor(snapper_config)
if data["snapshot"]: if data["snapshot"]:
prefile = Prefile(snapper_config, args.type) num = SnapperCmd(snapper_config, data["cleanup_algorithm"], data["description"], chroot, data["userdata"])()
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}") logging.info(f"==> {snapper_config}: {num}")
prefile.write(num)