Skip to content

Commit

Permalink
supermin: don't cd $workdir in supermin-init-prelude
Browse files Browse the repository at this point in the history
I've been investigating why a seemingly innocuous change
(changing compression on OSBuild generated qemu qcow2) would
cause disk images to not boot [1]. I think I have found the issue.

I was first trying to make sure 100% that the files got written
out over the virtiofs mount before the VM got shutdown so I decided
to add a `umount $workdir` to the process. But this ended up with
a `umount: /srv/: target is busy.` error.

When the supermin VM gets run we `cd "${workdir}"` at the end of
supermin-init-prelude.sh. This has the effect of causing all
spawned processes (including PID1/init) to have a cwd of /srv/.

```
bash-5.2# lsof /srv
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF     NODE NAME
init        1 root  cwd    DIR   0,26     4096 10485829 /srv
kthreadd    2 root  cwd    DIR   0,26     4096 10485829 /srv
pool_work   3 root  cwd    DIR   0,26     4096 10485829 /srv
kworker/R   4 root  cwd    DIR   0,26     4096 10485829 /srv
...
...
```

Which means it's unlikely that the virtiofs mount ever gets cleanly
unmounted. Let's rework things here so that actual work gets spawned
in a subshell to prevent `init` from having a cwd on the virtiofs mount.

We also add in an `umount` of the cache qcow2 (if exists) and the virtiofs
mount to strengthen our chances of a clean unmount.

[1] #3728
  • Loading branch information
dustymabe committed Feb 16, 2024
1 parent f8fb4df commit 519c33e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
15 changes: 11 additions & 4 deletions src/cmdlib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -753,17 +753,24 @@ export USER=$(id -u)
export RUNVM_NONET=${RUNVM_NONET:-}
$(cat "${DIR}"/supermin-init-prelude.sh)
rc=0
# tee to the virtio port so its output is also part of the supermin output in
# case e.g. a key msg happens in dmesg when the command does a specific operation
# - tee to the virtio port so its output is also part of the supermin output in
# case e.g. a key msg happens in dmesg when the command does a specific operation.
# - Use a subshell because otherwise init will use workdir as its cwd and we won't
# be able to unmount the virtiofs mount cleanly. This leads to consistency issues.
if [ -z "${RUNVM_SHELL:-}" ]; then
bash ${tmp_builddir}/cmd.sh |& tee /dev/virtio-ports/cosa-cmdout || rc=\$?
(cd ${workdir}; bash ${tmp_builddir}/cmd.sh |& tee /dev/virtio-ports/cosa-cmdout) || rc=\$?
else
bash; poweroff -f -f; sleep infinity
(cd ${workdir}; bash)
fi
echo \$rc > ${rc_file}
if [ -n "\${cachedev}" ]; then
/sbin/fstrim -v ${workdir}/cache
mount -o remount,ro ${workdir}/cache
fsfreeze -f ${workdir}/cache
fsfreeze -u ${workdir}/cache
umount ${workdir}/cache
fi
umount ${workdir}
/sbin/reboot -f
EOF
chmod a+x "${vmpreparedir}"/init
Expand Down
3 changes: 0 additions & 3 deletions src/supermin-init-prelude.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,3 @@ touch /etc/cosa-supermin
# the missing link. Hehe.
update-alternatives --install /etc/alternatives/iptables iptables /usr/sbin/iptables-legacy 1
update-alternatives --install /etc/alternatives/ip6tables ip6tables /usr/sbin/ip6tables-legacy 1

# https://github.com/koalaman/shellcheck/wiki/SC2164
cd "${workdir}" || exit

0 comments on commit 519c33e

Please sign in to comment.