Jump to content

Fstab

From 太極

Basics

  • man fstab, man mount
  • https://help.ubuntu.com/community/Fstab
    • Format: [Device] [Mount Point] [File System Type] [Options] [Dump] [Pass]
    • Example: /dev/sdb1 /mnt/external vfat defaults 0 0
    • <File System Type>: ext2, ext3, ext4, msdos, vfat (for FAT32), ntfs, minix, hfs, xfs
    • <dump> Enable or disable backing up of the device/partition (the command dump). This field is usually set to 0, which disables it.
    • <pass> Controls the order in which fsck checks the device/partition for errors at boot time. A value of 0 means that the file system should not be checked by fsck. A value of 1 means that the file system should be checked first (like the "/" root file system), and a value of 2 or higher means that the file system should be checked after all file systems with a lower value have been checked. PS. However, running fsck on a file system can take some time, especially if the file system is large or contains a lot of data. This can increase the time it takes for your system to boot. If you are using an external drive for temporary storage or if you don’t mind waiting a bit longer for your system to boot, you may want to set the last field to 2. Otherwise, you may want to leave it set to 0 to skip the fsck check and speed up the boot process.
  • What Are UUIDs and Why Are They Useful
  • https://wiki.archlinux.org/index.php/Fstab
    • Devices that are listed and not present will result in an error unless the nofail option is used. (good for external devices)
  • https://help.ubuntu.com/community/UsingUUID
  • Mount /tmp securely
  • http://www.thegeekstuff.com/2013/01/mount-umount-examples/
  • Graphical method using Disks
  • http://www.instructables.com/id/Using-a-USB-external-hard-drive-with-your-Raspberr/?ALLSTEPS Use UUID instead of /dev/sdXY to specify the partition in /etc/fstab to avoid any changes with /dev/sdXY. The UUID can be obtained using
    $ sudo blkid  #  list devices even not mounted yet
    

    and the result should be compared with

    $ sudo fdisk -l
    
  • Use defaults,ro if we don't want everyone to have the write access to a volume.
  • sudo mount /mnt/WD640 (no need to specify UUID, ...) will automatically mount a device according to the file /etc/fstab
  • x-systemd.device-timeout=10
    • Behavior: If the device doesn't appear within 10 seconds, systemd will consider the mount operation failed and move on.
    • Unit specification: You can specify the time in seconds or explicitly append a unit such as "s", "min", "h", or "ms". In this case, 10 is implicitly treated as 10 seconds.
    • Usage context: This option is typically used in /etc/fstab entries for devices that may not be immediately available at boot time.
    • Failure handling: If the timeout is reached, systemd will log an error and potentially continue the boot process, depending on other configuration options.
    • Flexibility: This allows for more flexible boot processes, especially with removable or network-attached storage that might not always be present.
    • Alternative to infinite wait: Without this option, systemd might wait indefinitely for a device to appear, potentially hanging the boot process.

How to Mount a USB Drive

How to Mount a USB Drive Every Time Linux Boots Up

One-time only

sudo mount -o credentials=/root/.smb,uid=1000,gid=1000 //192.168.X.XX/wd8t /media/wd8t

How To Mount A Drive Permanently

How To Mount A Drive Permanently In Linux Using Fstab: A Step-by-Step Guide

SMB share

Procedures:

  • Install Prerequisites
    sudo apt update
    sudo apt install cifs-utils
    
  • Create a Credentials File. nano ~/.smbcredentials
    username=your_smb_username
    password=your_smb_password
    domain=WORKGROUP
    
  • Secure the file so only root (and you) can read it:
    chmod 600 ~/.smbcredentials
    
  • Create the Mount Point
    sudo mkdir -p /mnt/my_share
    
  • Edit /etc/fstab
    //192.168.1.100/share_name  /mnt/my_share  cifs  credentials=/home/your_user/.smbcredentials,uid=1000,gid=1000,_netdev,x-systemd.automount  0  0
    
    • uid=1000,gid=1000: This makes the mounted files owned by your user (usually user ID 1000 on Debian) rather than root, allowing you to read/write to them easily.
    • _netdev: This tells the system that this mount requires network access. It attempts to delay the mount until the network is up.
    • x-systemd.automount: (Highly Recommended) This is the "magic" option for reliability. It tells the system to mount the share on demand (the moment you try to access the folder) rather than strictly during the boot sequence. This effectively eliminates boot failures caused by a slow network connection.
  • Verify the Configuration
    sudo systemctl daemon-reload
    sudo mount /mnt/my_share
    

Question 1: If your SMB server is offline during boot, the behavior depends entirely on whether you used the x-systemd.automount option or just the standard _netdev option.

  • Scenario A: Using x-systemd.automount (Recommended).
    • If you use this option, your computer will boot normally and instantly, even if the server is completely dead.
    • During Boot: The system does not try to connect to the SMB server. instead, it simply sets up a "listening" trigger on the folder (/mnt/my_share). It assumes everything is fine and finishes booting immediately.
    • When you access the folder: The moment you click on the folder or list its contents (e.g., ls /mnt/my_share), the system pauses and attempts to connect to the server.
    • If the server is down (upon access): The specific application you are using (e.g., the file manager or terminal) will hang for a few seconds while it tries to connect. Eventually, it will time out and give you an error (like "Host is down" or "Input/output error").
  • Scenario B: Using only _netdev (Without automount)
    • If you do not use x-systemd.automount (even if you use nofail or _netdev), your boot process will likely degrade.
    • During Boot: The system pauses the boot sequence to attempt the connection.
    • If the server is down: The system will wait for the default TCP timeout (often 90 seconds) before giving up.
      • Result: You will stare at a boot screen or a "A start job is running for..." message for 1.5 minutes.
      • Without nofail: If the timeout expires, the boot might fail completely and drop you into "Emergency Mode" (a root shell), forcing you to manually fix it before you can log in.
      • With nofail: It will wait the 90 seconds, fail the mount, and then continue booting. You will eventually get to your desktop, but the folder will be empty.

Question 2: x-systemd.automount option vs x-systemd.device-timeout=10 option

  • Does x-systemd.automount mean x-systemd.device-timeout=0? No. In fact, x-systemd.device-timeout=0 means "wait forever" (infinite timeout), which is the exact opposite of what you probably want.
  • The x-systemd.automount option will boot finishes instantly. Share connects when you click it.
  • The x-systemd.automount option is widely considered the "gold standard" for network mounts on modern Linux systems, primarily because it removes the guesswork.
  • If you (or your cron job) try to access the share and the server is down, it will only hang for 10 seconds before giving up, rather than the default 90 seconds.

mount error(22): Invalid argument

  • tail -f /var/log/kern.log

Examples

  • External storage configuration from raspberrypi.org.
    UUID=5C24-1453 /mnt/mydisk fstype defaults,auto,users,rw,nofail 0 0
    UUID=5C24-1453 /mnt/mydisk ext4   defaults,noatime,nofail 0 2
    
  • https://wiki.archlinux.org/title/Fstab
    • Noatime: – Do not update the file access times on the file system. This can help performance on old hardware.
    • Relatime: Update file access times relative to the file modified time.
    • nofail: Devices that are listed and not present will result in an error unless the nofail option is used.
  • Run sudo mount -a to remount /etc/fstab without reboot, except the partitions with noauto option. The following example shows a problem (as found from the output of df command) with </etc/fstab> where we use /dev/sdXY instead of UUID for specifying hard disks.
    $ sudo blkid
    /dev/sda1: LABEL="WD640" UUID="d3a0a512-bf96-4199-9674-f410f22f0a92" TYPE="ext4"
    /dev/sdb1: UUID="afaa4bde-1172-4c54-8b0a-a324ad855355" TYPE="ext4"
    /dev/sdb5: UUID="fb2a4ada-d80a-4e23-b4a2-67376b8b7e72" TYPE="swap"
    
    $ sudo fdisk -l
    Disk /dev/sda: 640.1 GB, 640135028736 bytes
    ...
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1            2048  1250263039   625130496   83  Linux
    
    Disk /dev/sdb: 640.1 GB, 640135028736 bytes
    ...
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1   *        2048  1217761279   608879616   83  Linux
    /dev/sdb2      1217763326  1250263039    16249857    5  Extended
    /dev/sdb5      1217763328  1250263039    16249856   82  Linux swap / Solaris
    
    $ cat /etc/fstab
    proc            /proc                                 proc    nodev,noexec,nosuid 0 0
    UUID=afaa4bde-1172-4c54-8b0a-a324ad855355 /           ext4    errors=remount-ro   0 1
    UUID=fb2a4ada-d80a-4e23-b4a2-67376b8b7e72 none        swap    sw                  0 0
    /dev/sdb1       /mnt/WD640                            ext4    rw,nosuid,nodev     0 2
    UUID=fb2a4ada-d80a-4e23-b4a2-67376b8b7e70 /mnt/extUSB auto nosuid,nodev,nofail 0 2
    UUID=2ab36808-038a-4dfa-ad52-c10944cf61f2 /mnt/hdd  ext4  defaults  0  2
    
    $ df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sdb1       572G  413G  130G  77% /
    ...
    /dev/sdb1       572G  413G  130G  77% /mnt/WD640
    

    To fix the error here, modify the line starting /dev/sdb1 in /etc/fstab and replace it with the UUID. Then run sudo umount /mnt/WD640 and sudo mount -a. Done!