Auto-upgrade: Check for VMs, add more output

This commit is contained in:
fruchti 2025-07-17 09:49:52 +02:00
parent 9aae1f5f83
commit 83b5a0df3d

View file

@ -52,6 +52,7 @@ in
nixos-rebuild = "${config.system.build.nixos-rebuild}/bin/nixos-rebuild"; nixos-rebuild = "${config.system.build.nixos-rebuild}/bin/nixos-rebuild";
nix-store = "${pkgs.nix}/bin/nix-store"; nix-store = "${pkgs.nix}/bin/nix-store";
diff = "${pkgs.diffutils}/bin/diff"; diff = "${pkgs.diffutils}/bin/diff";
grep = "${pkgs.gnugrep}/bin/grep";
git = "${pkgs.git}/bin/git"; git = "${pkgs.git}/bin/git";
ssh = "${pkgs.openssh}/bin/ssh"; ssh = "${pkgs.openssh}/bin/ssh";
sudo = "${pkgs.sudo}/bin/sudo"; sudo = "${pkgs.sudo}/bin/sudo";
@ -164,11 +165,26 @@ in
)" )"
activate_configuration="no" activate_configuration="no"
# Check if any user sessions are open if [ "$reboot_allowed" = "yes" ] ; then
active_users=$(users | tr ' ' '\n' | sort | uniq | grep -vE '^(${concatStringsSep "|" cfg.rebootIgnoreUsersActive})$' || true) echo " Checking if a reboot is allowed." | tee -a "$output_file"
if [ "$reboot_allowed" = "yes" ] && [ -n "$active_users" ] ; then
reboot_allowed=no # Check if any user sessions are open
email_body="$(printf "%s\n%s\n%s" "$email_body" "The system cannot reboot since the following users are active:" "$active_users")" active_users=$(users | tr ' ' '\n' | sort | uniq | ${grep} -vE '^(${concatStringsSep "|" cfg.rebootIgnoreUsersActive})$' || true)
if [ -n "$active_users" ] ; then
reboot_allowed=no
email_body="$(printf "%s\n%s\n%s" "$email_body" "The system cannot reboot since the following users are active:" "$active_users")"
echo "$(echo $active_users | wc -l) active users blocking reboot." | tee -a "$output_file"
fi
${optionalString config.virtualisation.libvirtd.enable ''
# Check if virtual machines are running
active_vms=$(${pkgs.libvirt}/bin/virsh list --state-running --no-autostart --id --name || true)
if [ -n "$active_vms" ] ; then
reboot_allowed=no
email_body="$(printf "%s\n%s\n%s" "$email_body" "The system cannot reboot since the following virtual machines are active:" "$active_vms")"
echo "$(echo $active_vms | wc -l) active VMs blocking reboot." | tee -a "$output_file"
fi
''}
fi fi
if [ "$reboot_allowed" = "yes" ] && [ $exit_code -eq 0 ] ; then if [ "$reboot_allowed" = "yes" ] && [ $exit_code -eq 0 ] ; then
@ -193,7 +209,7 @@ in
upgrade_output="$(cat "$output_file")" upgrade_output="$(cat "$output_file")"
rm -f "$output_file" rm -f "$output_file"
possible_warnings="$(grep -e "^\(warning\|trace\|evaluation warning\):" <<<"$upgrade_output" || true)" possible_warnings="$(${grep} -e "^\(warning\|trace\|evaluation warning\):" <<<"$upgrade_output" || true)"
if [ "$possible_warnings" != "" ] ; then if [ "$possible_warnings" != "" ] ; then
send_email=yes send_email=yes
email_subject_additions="$email_subject_additions with warnings" email_subject_additions="$email_subject_additions with warnings"