1

Is it possible to change what device is mounted to a mount point atomically, so at no moment of time mount point can be observed empty? Here is what I am trying to do:

$ mkdir /point
$ mkdir /device1 /device2
$ echo 1 > /device1/text
$ echo 2 > /device2/text
$ mount --bind /device1 /point
$ # do some kind of magic to get /device2 mounted at /point and /device1 unmounted

Technically, I can ignore previous mount and mount new device on top, but kernel will keep keep record of both now, so it feels like "mount leak".

KAction
  • 587
  • 2
  • 10

1 Answers1

0

I have run following successfully [ubuntu "20.04.3 LTS (Focal Fossa)"]:

/tmp# cat test.sh
#!/bin/bash

set -e
mkdir -p /tmp/{point,device1,device2}
echo 1 > /tmp/device1/text
echo 2 > /tmp/device2/text
mount --bind /tmp/device1 /tmp/point
cat /tmp/point/text
mount --bind /tmp/device2 /tmp/point
cat /tmp/point/text
umount /tmp/device1
echo "Finished successfully"

/tmp# bash test.sh
1
2
Finished successfully
/tmp#
Philippe
  • 20,025
  • 2
  • 23
  • 32