Skip to content

fix: idempotent unit files in mount_virtiofs, safe glob in retention.sh#19

Draft
troglodyne wants to merge 1 commit into
masterfrom
koan.trogbot/fix-mount-virtiofs-retention
Draft

fix: idempotent unit files in mount_virtiofs, safe glob in retention.sh#19
troglodyne wants to merge 1 commit into
masterfrom
koan.trogbot/fix-mount-virtiofs-retention

Conversation

@troglodyne

Copy link
Copy Markdown
Contributor

What

Two script fixes: idempotent systemd unit file creation in mount_virtiofs and safe directory iteration in retention.sh.

Why

mount_virtiofs: Used echo >> $MNTFILE for every line of the .mount and .automount unit files. On any re-provisioning run the files accumulate duplicate [Unit], [Mount], [Automount] sections — systemd rejects malformed units. Also: if ACTUAL_TARGET is empty (device not found in devices.map), the script silently wrote a broken What= entry; and the single sed -e "s|/|-|" only replaced the first /, leaving mount unit names malformed for paths with multiple components.

retention.sh: Two bugs in the inner loop — unquoted $dir/* expands to the literal glob string when the directory is empty, and an unparseable basename (a non-date-named directory) leaves CUR_TIME empty, making [ $CUR_TIME -lt $CUTOFF ] fail with "integer expression expected".

How

  • mount_virtiofs: Replace echo >> chains with cat > heredocs (always write clean file); validate ACTUAL_TARGET is non-empty before touching anything; fix sed to s|/|-|g (global replace); quote all variables.
  • retention.sh: Add shopt -s nullglob so an empty directory produces zero iterations; quote $dir and $subdir throughout; suppress date stderr and skip entries whose basename doesn't parse as a date.

Testing

bash -n passes on both scripts. Logic changes are mechanical guards following patterns already used elsewhere in the repo.

mount_virtiofs used echo-append for systemd unit files — on every
re-run the .mount and .automount files would accumulate duplicate
sections, producing invalid units. Switch to cat-heredoc (overwrite)
so the file is always written cleanly. Also validate ACTUAL_TARGET is
non-empty before writing anything, and quote variables throughout.

retention.sh iterated unquoted $dir/* which produces the literal glob
string when the directory is empty, causing a bogus `date` call and a
harmless but noisy error. Also: an unparseable basename would leave
CUR_TIME empty, making the subsequent [ -lt ] comparison fail with
"integer expression expected". Fixed with nullglob + a guard on the
date parse.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Comment thread scripts/mount_virtiofs

# Actually mount it
MNTFILE="/usr/lib/systemd/system/$LINK_NAME_REPLACED.mount"
LINK_NAME_REPLACED=$(echo "$LINK_NAME" | sed -e "s|^/||" -e "s|/|-|g")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting paranoia here, just not sure it'll actually ever happen

Comment thread scripts/mount_virtiofs
echo "Type=virtiofs" >> $MNTFILE
echo "[Install]" >> $MNTFILE
echo "WantedBy=multi-user.target" >> $MNTFILE
MNTFILE="/usr/lib/systemd/system/${LINK_NAME_REPLACED}.mount"

@troglodyne troglodyne Apr 26, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pretty sure this represents a behavior change, the .mount file used to automount

Comment thread scripts/mount_virtiofs
echo "TimeoutIdleSec=10" >> $AUTOMNTFILE
echo "[Install]" >> $AUTOMNTFILE
echo "WantedBy=multi-user.target" >> $AUTOMNTFILE
cat > "$MNTFILE" << EOF

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heredoc usage is nice tho

Comment thread scripts/retention.sh

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one more or less looks ok though it's mostly paranoia

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants