How to automatically mount a volume at instance startup¶
To automatically mount a volume at boot time, you need to edit the /etc/fstab file. Before editing this file, it is better to create a backup of it just in case something unwanted happens.
sudo cp /etc/fstab /etc/fstab.old
lsblk command to get the UUID of the partition you want to automount; in this way, the mount will work also in cases where the device letter changes.
sudo lsblk -o NAME,FSTYPE,UUID,SIZE,LABEL
Edit the /etc/fstab file and append your volume info at the end.
sudo vim /etc/fstab
Each line of this file matches the following format:
<filesystem UUID> <mount point> <type> <options> <dump> <pass> (each element is separated by whitespace)
<filesystem UUID> describes the volume or remote filesystem UUID to be mounted, which can be obtained with the
<mount point> describes the mount point for the filesystem; in other words, the path to access the files stored on volume.
<type> describes the type of filesystem to be used (usually ext4).
<options> can be used to add metadata to the associated filesystem.
Here are some related options:
- auto/noauto - The filesystem will be mounted automatically at startup/The filesystem will NOT be automatically mounted at startup.
- Note: when setting the mount point for a ScienceCloud VM noauto should be used to avoid the situation where a VM cannot start because it cannot find the external volume.
- ro/rw - Mount read-only/Mount read-write.
- user - Permit any ordinary user to mount the filesystem. (This automatically implies noexec, nosuid, nodev unless overridden) overrides the default to only permit root to mount the filesystem.
- uid/gid Set the owner and group of the files in the filesystem (default:uid=gid=0)
- defaults - Use default settings. Equivalent to rw, suid, dev, exec, auto, nouser, async. To see all of the available options, please refer to the mount documentation.
x-systemd.automount automatically mounts the filesystem when needed (i.e., a process tries to access the mountpoint)
x-systemd.idle-timeout=<sec> specifies the number of seconds of inactivity before cleanly unmounting the filesystem. To see all the available systemd mount options please refer to systemd.mount.
<dump> if you need to dump the file system. Leave it blank or zero if the filesystem does not need to be dumped.
<pass> determine the order in which filesystem checks are done at reboot time. Leaving this blank will assume that the filesystem does not need to be checked.
Examples of fstab entries¶
To mount an external volume with UUID
b411dc99-f0a0-4c87-9e05-184977be8539into a pre-existing directory called
dataon Ubuntu 16.04 and later:The previous options will allow the volume to be mounted on demand when you try to access the mountpoint and be unmounted automatically when it is no longer needed thus greatly reducing the risk of inconsistencies following a crash.
UUID=b411dc99-f0a0-4c87-9e05-184977be8539 /data ext4 rw,user,noauto,x-systemd.automount,x-systemd.idle-timeout=300 0 0
Following up on the example provided on the ScienceCloud Training Handout, if you'd like to retain the configuration across reboots:
UUID=25f1305a-f6a8-4d37-a9ec-d1e391afed88 /mnt ext4 rw,noauto,user,x-systemd.automount,x-systemd.idle-timeout=300 0 0
The following automatically command mounts a network share (for example, a share on the SMB File Service of UZH or hosted by your department). Assuming your share requires version 3 cifs, adapt and add the following line to your /etc/fstab:
//idnasXX.uzh.ch/g_my_department$/mydept/myname /home/ubuntu/myname cifs rw,credentials=/home/ubuntu/.credentials.smb,uid=ubuntu,vers=3.0,user,exec,noauto,x-systemd.automount,x-systemd.idle-timeout=300 0 0
- Create a file under your ubuntu home directory with the credentials of the share you want to automatically mount: the domain could be something like 'UZH'.
username=share_user_name password=share_password domain=share_domain
- Secure permissions with
chmod 500 .credentials.smb
- Make systemd aware of the changes in the fstab file
sudo systemctl daemon-reload sudo systemctl restart remote-fs.target
- Create a file under your ubuntu home directory with the credentials of the share you want to automatically mount:
- Now the network share should be automatically mounted each time it is accessed, and it should be unmounted correctly after a period of inactivity. Note that with the
noautooption this setup will not prevent the VM from booting correctly if the network share is unavailable during boot.