1

From http://man7.org/linux/man-pages/man2/mount.2.html

mount() attaches the filesystem specified by source (which is often a pathname referring to a device, but can also be the pathname of a directory or file, or a dummy string) to the location (a directory or file) specified by the pathname in target.

And I test a little bit, where test.ext4 is an ext4 image file and mnt just a normal plain file.

before anything happen

 xtricman@archlinux  ~  ls mnt test.ext4 -ali
1093586 -rw-r--r-- 1 xtricman users        0 2月  27 10:13 mnt
1085615 -rw-r--r-- 1 xtricman users 16777216 2月  24 19:35 test.ext4

bind mount directory to file, failed

 xtricman@archlinux  ~  sudo mount --bind /var mnt
mount: /home/xtricman/mnt: mount point is not a directory.

let mount create a loop device and (not bind-ly) mount the device to file, failed

 xtricman@archlinux  ~  sudo mount test.ext4 mnt
mount: /home/xtricman/mnt: mount point is not a directory.

bind mount file to directory, failed

 xtricman@archlinux  ~  sudo mount --bind test.ext4 /mnt
mount: /mnt: mount(2) system call failed: 不是目录.

bind mount file to file, succeed!

 xtricman@archlinux  ~  sudo mount --bind test.ext4 mnt
 xtricman@archlinux  ~  ls -ali mnt test.ext4
1085615 -rw-r--r-- 1 xtricman users 16777216 2月  27 10:16 mnt
1085615 -rw-r--r-- 1 xtricman users 16777216 2月  27 10:16 test.ext4
 xtricman@archlinux  ~  sudo umount mnt
 xtricman@archlinux  ~  echo "Trash" >  new_file
 xtricman@archlinux  ~  ls -ali new_file mnt test.ext4 
1093586 -rw-r--r-- 1 xtricman users        0 2月  27 10:13 mnt
1093810 -rw-r--r-- 1 xtricman users        6 2月  27 10:58 new_file
1085615 -rw-r--r-- 1 xtricman users 16777216 2月  27 10:16 test.ext4
 xtricman@archlinux  ~  sudo mount --bind new_file mnt
 xtricman@archlinux  ~  ls -ali new_file mnt test.ext4
1093810 -rw-r--r-- 1 xtricman users        6 2月  27 10:58 mnt
1093810 -rw-r--r-- 1 xtricman users        6 2月  27 10:58 new_file
1085615 -rw-r--r-- 1 xtricman users 16777216 2月  27 10:16 test.ext4

So, is it really true that we can only bind mount directory to directory and file to file? And what does the "dummy string" mean? Wouldn't a non-exist path as source or target simply fail?

  • @SergiyKolodyazhnyy That's mounting a device to a directory, not what I'm asking. You should know there's a difference between "use image file as loop device and mount the device somewhere" and "mount --bind a file to file" – 炸鱼薯条德里克 Feb 27 at 2:48
  • @SergiyKolodyazhnyy you don't bind mount something to a loop device, though. That something is set as the backing device of loop device, and then that loop device is mounted somewhere. – Olorin Feb 27 at 2:59
  • 1
    mount -t tmpfs dummystring /tmp – A.B Feb 27 at 4:03

Your Answer

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

Browse other questions tagged or ask your own question.