0

I have a startup script which executes the following commands:

sudo cryptsetup open /dev/sda3 dm_crypt && sudo mount -t btrfs /dev/mapper/dm_crypt /mnt && cd /mnt && ls -la && sudo -s

This basically opens my LUKS device and mounts the BTRFS root partition and starts a root shell for further work.

Its all great but when I try to do:

umount -f /mnt

in the root shell I get:

umount: /mnt: target is busy.

Can someone tell me why this is happening ?

Am I right, if all the commands in my startup script are executed as mutual processes ? Then what is keeping /mnt busy ?

This is the output of lsof | grep /mnt:

bash      1890                       liveuser  cwd       DIR               0,45        42        256 /mnt
sudo      2168                           root  cwd       DIR               0,45        42        256 /mnt

Is it because the root shell is child process of the script or something ?

This does not make any sense to me.

Constraints: I will calling cryptsetup close from within the child root shell.

1 Answer 1

3

/mnt is kept busy by two processes: the shell running your script, and sudo itself. You can’t change their working directories from your root shell.

You could either change your script so it doesn’t cd into your mount point, or you could use umount -l before exiting your root shell. The latter will lazily unmount, and when you exit the shell, sudo and the parent shell will exit too, freeing the mount point.

If you want to be able to close the LUKS volume too, the best approach is to add that to your script:

sudo cryptsetup open /dev/sda3 dm_crypt && sudo mount -t btrfs /dev/mapper/dm_crypt /mnt && cd /mnt && ls -la && sudo -s
cd - && sudo umount /mnt && sudo cryptsetup close dm_crypt
6
  • 1
    or add && cd / && umount /mnt to the end of the script, (should probably add some more to turn off the decryption.) Jan 2, 2021 at 11:39
  • @stephen-kitt I don't understand how lazy unmounts work. I just tested with a bind mount and lazily unmounted from a child sudo shell, then killed both parent and child shells. Next when I open up the terminal I see the directory is still mounted. What is going on ?
    – ng.newbie
    Jan 2, 2021 at 11:54
  • I haven’t checked with bind mounts, but umount -l works fine with non-bind mounts. Lazy unmounts work by returning immediately and really unmounting as soon as the mount point is no longer used. Jan 2, 2021 at 14:04
  • @StephenKitt This is not going to work since I will be calling the unmount and LUKS close from the child root shell. Any way I can do sudo with passing a command and keeping it open after the command has executed ?
    – ng.newbie
    Jan 2, 2021 at 14:52
  • You should edit your question to mention those constraints. The best solution in those circumstances is ctrl-alt-delor’s suggestion, see the updated answer. Jan 2, 2021 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.