9

As mentioned here, "you must create the mount point before you mount the partition." But after adding the following lines in fstab and rebooting the system

/dev/sdxy /media/user/hdd_mount_point ext4 defaults 0 0
tmpfs /mnt/tmpfs_mount_point tmpfs rw,nosuid,noatime,nodev,size=4G,mode=1777 0 0

the mount points (target directories) hdd_mount_point and tmpfs_mount_point are automatically created if they do not exist. I know that you can make a mount point if it does not exist by adding the X-mount.mkdir option to mount command as mentioned in its manual page, but I really like to know if this option is implicitly used when mounting file systems via fstab.

And NO, it is not a duplicate of Automatic Adding of Filesystems to /etc/fstab

Dante
  • 2,319
  • 4
  • 25
  • 35
  • part and parcel of the installation process? – graham Jan 02 '21 at 14:45
  • What? Actually I edited fstab many times to be sure it creates the mount point every time and not during a fresh install of OS. – Dante Jan 02 '21 at 14:48
  • 1
    Does this answer your question? [Automatic Adding of Filesystems to /etc/fstab](https://askubuntu.com/questions/1072821/automatic-adding-of-filesystems-to-etc-fstab) – graham Jan 02 '21 at 14:55
  • See: https://help.ubuntu.com/community/Mount/USB It is called Automounting but it only temporarily creates the mount points. If you want them permanently you will have to create them yourself and add them to the `/etc/fstab`. – Terrance Jan 02 '21 at 15:01
  • @Terrance It is about fstab not mounting a file system using mount command. And it is an internal hard drive not USB. – Dante Jan 02 '21 at 15:04
  • 2
    @Dante You need to rewrite your question then. I'm not sure what you are asking. The only mount points that are created automatically are done during the initial install of the OS. All others you have to create yourself afterwards or use the automounting where you double-click the drive after each reboot and that one creates the `/media/user/mountpoint` as you are saying there. – Terrance Jan 02 '21 at 15:06

2 Answers2

8

This used to be true, and still is if you wish to use the classic mount command.

In recent years, the entries in /etc/fstab are mounted at boot time by systemd, which automatically creates missing mountpoints as needed, irrespective of the presence of the X-mount.mkdir option.

It also provides the command systemd-mount as an alternative to mount, should you wish to mount a partition yourself without first manually creating a directory to mount over.

Martin Thornton
  • 5,151
  • 11
  • 30
  • 39
  • 1
    But without `X-mount.mkdir` option, you get `mount: my_mount_point: mount point does not exist.` error if `my_mount_point` directory does not exist already. – Dante Jan 02 '21 at 17:19
  • Yes, if you still use `mount`. – Martin Thornton Jan 02 '21 at 17:29
  • 1
    @Dante if you still use `mount` AFTER the system is booted, it bypasses SystemD's "at boot" mountpoint generation mechanism. So if you are not mounting automatically at boot, you need the mountpoint to be created first. SystemD removes most of the "at boot" work but after things are mounted at boot that's about it, it won't go digging afterwards when you call `mount` to create mountpoints. – Thomas Ward Jan 02 '21 at 17:30
3

And here the essential parts from the documentation:

Mount units may either be configured via unit files, or via /etc/fstab
(see fstab(5) for details). Mounts listed in /etc/fstab will be converted
into native units dynamically at boot and when the configuration of the
system manager is reloaded. In general, configuring mount points through
/etc/fstab is the preferred approach. See systemd-fstab-generator(8) for
details about the conversion.

Further down you'll find this:

Where=

    Takes an absolute path of a file or directory for the mount point;
    in particular,the destination cannot be a symbolic link. If the
    mount point does not exist at the time of mounting, it is created
    as directory.
mook765
  • 14,911
  • 5
  • 35
  • 67
  • Thanks for the reference, but what is mentioned [here](https://help.ubuntu.com/community/Fstab#Mount_point) is still weird and misleading IMO. – Dante Jan 02 '21 at 18:53
  • The information on that page appears to be outdated regarding this point, it was correct before systemd was used, look at the [revision history](https://help.ubuntu.com/community/Fstab?action=info), since 2014 there are only a few edits made. – mook765 Jan 03 '21 at 08:07