From 15ff43369f43bdd50bcedd576e583b2db989de33 Mon Sep 17 00:00:00 2001 From: Girish Ramakrishnan Date: Tue, 22 Jun 2021 09:53:31 -0700 Subject: [PATCH] mount: if unmount failed, do not proceed --- src/mounts.js | 3 ++- src/scripts/addmount.sh | 10 +++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/mounts.js b/src/mounts.js index 590ecbc8f..46fb1e72e 100644 --- a/src/mounts.js +++ b/src/mounts.js @@ -158,7 +158,8 @@ async function tryAddMount(volume, options) { if (!safe.fs.writeFileSync(keyFilePath, `${volume.mountOptions.privateKey}\n`, { mode: 0o600 })) throw new BoxError(BoxError.FS_ERROR, safe.error); } - await safe(shell.promises.sudo('addMount', [ ADD_MOUNT_CMD, renderMountFile(volume), options.timeout ], {})); + const [error] = await safe(shell.promises.sudo('addMount', [ ADD_MOUNT_CMD, renderMountFile(volume), options.timeout ], {})); + if (error && error.code === 2) throw new BoxError(BoxError.MOUNT_ERROR, 'Failed to unmount existing mount'); // at this point, the old mount config is still there const status = await getStatus(volume.mountType, volume.hostPath); if (status.state !== 'active') { // cleanup diff --git a/src/scripts/addmount.sh b/src/scripts/addmount.sh index 70bc10fec..0ec4eb93c 100755 --- a/src/scripts/addmount.sh +++ b/src/scripts/addmount.sh @@ -27,9 +27,13 @@ mount_filename=$(systemd-escape -p --suffix=mount "$where") mount_file="/etc/systemd/system/${mount_filename}" # cleanup any previous mount of same name (after midway box crash?) -if systemctl -q is-active mnt-volumes-ext4data.mount; then +if systemctl -q is-active "${mount_filename}"; then echo "Previous mount active, unmounting" - systemctl stop "${mount_filename}" || true + # unmounting can fail if a user is cd'ed into the directory. if we go ahead and mount anyway, systemd says "active" because it's referring to the previous mount config + if ! systemctl stop "${mount_filename}"; then + echo "Failed to unmount" + exit 2 + fi fi echo "$mount_file_contents" > "${mount_file}" @@ -38,7 +42,7 @@ systemctl daemon-reload if ! timeout "${timeout}" systemctl enable --now "${mount_filename}"; then echo "Failed to mount" - exit 1 + exit 3 fi echo "Mount succeeded"