From 29b5140a343448ed1873cc5225ffe656a3b13fda Mon Sep 17 00:00:00 2001 From: Baitinq Date: Mon, 17 Oct 2022 15:24:26 +0200 Subject: Hardware: Disks: Add diff-root utility This utility allows to check what changed in the / from boot till the execution of the command. Useful for knowing what to persist. --- hardware/virtualbox/disks.nix | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'hardware/virtualbox/disks.nix') diff --git a/hardware/virtualbox/disks.nix b/hardware/virtualbox/disks.nix index ab2e053..330b4ac 100644 --- a/hardware/virtualbox/disks.nix +++ b/hardware/virtualbox/disks.nix @@ -44,6 +44,22 @@ let mount -o compress-force=zstd,noatime /dev/mapper/encrypted_root_pool-persist /mnt/persist mount -o compress-force=zstd,noatime /dev/mapper/encrypted_root_pool-nix /mnt/nix ''; + + # Utility to save a snapshot of the root tree + save-root = pkgs.writers.writeDashBin "save-root" '' + ${pkgs.findutils}/bin/find \ + / -xdev \( -path /tmp -o -path /var/tmp -o -path /var/log/journal \) \ + -prune -false -o -print0 | sort -z | tr '\0' '\n' > "$1" + ''; + + # Utility to compare the root tree + diff-root = pkgs.writers.writeDashBin "diff-root" '' + export PATH=${with pkgs; lib.makeBinPath [ diffutils less ]}:$PATH + current="$(mktemp current-root.XXX --tmpdir)" + trap 'rm "$current"' EXIT INT HUP + ${save-root}/bin/save-root "$current" + diff -u /run/initial-root "$current" --color=always | ''${PAGER:-less -R} + ''; in { config = { @@ -118,7 +134,19 @@ in config.disks-create config.disks-format config.disks-mount + + diff-root ]; + + systemd.services.save-root-snapshot = { + description = "save a snapshot of the initial root tree"; + wantedBy = [ "sysinit.target" ]; + requires = [ "-.mount" ]; + after = [ "-.mount" ]; + serviceConfig.Type = "oneshot"; + serviceConfig.RemainAfterExit = true; + serviceConfig.ExecStart = ''${save-root}/bin/save-root /run/initial-root''; + }; }; options.disks-create = with lib; mkOption rec { -- cgit 1.4.1