From 810eccbad22cc50323b27161033399eb87658932 Mon Sep 17 00:00:00 2001 From: Jason Yundt Date: Thu, 15 Feb 2024 13:20:25 -0500 Subject: [PATCH] Allow Disko to be run in a systemd service MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this change, if you created a systemd service that ran “disko --mode disko ”, then you would (probably) get this error: disk-deactivate: line 6: /dev/stderr: No such device or address Since disk-deactivate uses “set -e”, that error would prevent disk-deactivate from actually deactivating any disks. The problem is that disk-deactivate tries to print text to stderr by doing this: echo "Text" >/dev/stderr systemd.exec(5) explains why that doesn’t work and goes on to say [1]: > This means when executing shell scripts the construct > **echo "hello" > /dev/stderr** for writing text to stderr will not > work. To mitigate this use the construct **echo "hello" >&2** instead, > which is mostly equivalent and avoids this pitfall. [1]: --- You can test this commit by doing the following: 1. Download a copy of this gist: 2. Build a custom NixOS installation ISO that contains a systemd service that runs disko: nix-build --arg diskoDir Once that command finishes, the ISO will be in ./result/iso/. 3. Create a VM. Make sure that it has a VirtIO disk that’s more than 500M large. Also make sure that it has an optical disc drive. 4. Put the ISO file from step 2 into the VM’s optical disc drive. 5. Turn on the VM and boot into the installation ISO. 6. Start the systemd service: sudo systemctl start disko-test 7. Check journalctl to see if there are any errors related to disk-deactivate. --- disk-deactivate/disk-deactivate | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/disk-deactivate/disk-deactivate b/disk-deactivate/disk-deactivate index 9a95418..9869298 100755 --- a/disk-deactivate/disk-deactivate +++ b/disk-deactivate/disk-deactivate @@ -3,6 +3,6 @@ set -efux -o pipefail # dependencies: bash jq util-linux lvm2 mdadm zfs disk=$(realpath "$1") -lsblk -a -f >/dev/stderr +lsblk -a -f >&2 lsblk --output-all --json | jq -r --arg disk_to_clear "$disk" -f "$(dirname "$0")/disk-deactivate.jq" | bash -x -lsblk -a -f >/dev/stderr +lsblk -a -f >&2