Welcome to the Linux Foundation Forum!

Chapter 18 - Automatic Filesystem Mounting - doesn't work for me

I was trying to implement "Automatic Filesystem Mounting" using the systemd way. As mentioned in this chapter, in the past the autofs package did the job and I had used it several times without issues.

My host system: Manjaro
Kernel: 5.8.11-1-MANJARO
systemd: systemd 246 (246.6-1-manjaro)

My idea was to automount an external backup drive connected via USB. The filesystem on the backup drive is NTFS, since it backs up the photo collection on my Windows 10 VM. The NTFS driver I use is NTFS-3g which is the default nowadays:

$ ls -lF /usr/bin/mount.ntfs*
lrwxrwxrwx 1 root root 16 Aug 30 12:50 /usr/bin/mount.ntfs -> /usr/bin/ntfs-3g*
lrwxrwxrwx 1 root root 16 Aug 30 12:50 /usr/bin/mount.ntfs-3g -> /usr/bin/ntfs-3g*

The mount options in my fstab before using the systemd approach are as follows:
/dev/disk/by-partlabel/wdbook /media/heiko/wdbook ntfs-3g noauto,rw,user,nls=utf8,uid=1000,gid=1000,dmask=027,fmask=137,nofail,windows_names,locale=en_US.utf8 0 0

which should translate into: noauto = don't mount at boot or with mount -a, rw = read-write access, user = let non-root user mount the filesystem, nls = enable utf8 character encoding, uid/gid = set the file ownership and group to myself (1000), dmask/fmask = mask the directory and file permissions (restrict access), nofail = don't cry if the drive isn't there, windows_names = prevent files, directories and extended attributes to be created with a name not allowed by windows, locale = set the locale (needed for sorting etc.), 0 0 = don't dump and don't fsck. I know it's a mouthful of options, but that worked fine so far.

If I comment out the line in fstab (no entry for the drive), the drive is shown in the file browser but not mounted. I can easily mount it via the file browser. I guess that's the default setting for this desktop OS.

However, the drive doesn't spin down and I was hoping that the systemd approach would allow me to spin down the disk when not in use. Here are the settings I used:

/dev/disk/by-partlabel/wdbook /media/heiko/wdbook   ntfs-3g  noauto,x-systemd.automount,x-systemd.device-timeout=10,x-systemd.idle-timeout=60,rw,user,nls=utf8,uid=1000,gid=1000,dmask=027,fmask=137,nofail,windows_names,locale=en_US.utf8  0 0

The timeout settings had absolutely no effect. By the way, I found the following website: https://www.golinuxcloud.com/automount-file-system-systemd-rhel-centos-7-8/

The article describes how to set up automount using systemd. It talks about first creating a .mount file for the drive, then a .automount config file to have it automounted. If I understand that correctly, the .mount file would be in lieu of the /etc/fstab entry?

I think this whole systemd approach is messing up my mind. What would be the right approach - using systemd - to have an external drive recognized and mounted "on demand", that is not automatically at boot. Also, the drive should spin down after a given idle time.

Thanks for looking into it.

Comments

  • heiko_s
    heiko_s Posts: 99

    I rechecked the setup. It turns out I had created a media-heiko-wdbook.mount file with the following content:

    #  https://www.golinuxcloud.com/automount-file-system-systemd-rhel-centos-7-8/
    
    [Unit]
    Description=Western Digital Book wdbook external drive
    DefaultDependencies=no
    Conflicts=umount.target
    Before=local-fs.target umount.target
    After=swap.target
    
    [Mount]
    What=/dev/disk/by-partlabel/wdbook
    Where=/media/heiko/wdbook
    Type=ntfs-3g
    Options=noauto,rw,user,nls=utf8,uid=1000,gid=1000,dmask=027,fmask=137,nofail,windows_names,locale=en_US.utf8
    
    [Install]
    WantedBy=multi-user.target
    
    

    This mount option was enabled.

    When I stopped and disabled the mount service the drive does NOT auto-mount:

    $ systemctl status media-heiko-wdbook.mount
    ● media-heiko-wdbook.mount - Western Digital Book wdbook external drive
         Loaded: loaded (/etc/systemd/system/media-heiko-wdbook.mount; disabled; vendor preset: disabled)
         Active: inactive (dead)
          Where: /media/heiko/wdbook
           What: /dev/disk/by-partlabel/wdbook
    

    To check it:

    $ lsblk
    NAME                    MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
    sdf                       8:80   0   3.6T  0 disk 
    └─sdf1                    8:81   0   3.6T  0 part
    

    or better:

    $ systemctl -t mount
      UNIT                          LOAD   ACTIVE SUB     DESCRIPTION                               
      -.mount                       loaded active mounted Root Mount                                
      boot-efi.mount                loaded active mounted /boot/efi                                 
      dev-hugepages.mount           loaded active mounted /dev/hugepages                            
      dev-mqueue.mount              loaded active mounted POSIX Message Queue File System           
      home.mount                    loaded active mounted /home                                     
      run-user-1000-gvfs.mount      loaded active mounted /run/user/1000/gvfs                       
      run-user-1000.mount           loaded active mounted /run/user/1000                            
      sys-fs-fuse-connections.mount loaded active mounted FUSE Control File System                  
      sys-kernel-config.mount       loaded active mounted Kernel Configuration File System          
      sys-kernel-debug.mount        loaded active mounted Kernel Debug File System                  
      sys-kernel-tracing.mount      loaded active mounted Kernel Trace File System                  
      tmp.mount                     loaded active mounted Temporary Directory (/tmp)   
    
    

    It turns out thunar-volman was installed which handles removable drives automatically. Thunar is the file manager on XFCE Arch Linux (and Manjaro) and it's quite convenient.

    Below is a summary of tests I've done:

    1. No /etc/fstab entry, media-heiko-wdbook.mount disabled:

    Using the Thunar file manager I can manually mount/unmount the drive. However, the disk never spins down.

    1. /etc/fstab entry, media-heiko-wdbook.mount disabled:

    I can no more mount the volume via Thunar and get the following error:

    Here is the fstab entry:

    /dev/disk/by-partlabel/wdbook /media/heiko/wdbook ntfs-3g noauto,rw,user,nls=utf8,uid=1000,gid=1000,dmask=027,fmask=137,nofail,windows_names,locale=en_US.utf8 0 0

    Note the "user" option which should allow me to mount the drive.

    1. With fstab entry, media-heiko-wdbook.mount enabled and running:

    sudo systemctl enable media-heiko-wdbook.mount
    sudo systemctl start media-heiko-wdbook.mount

    The moment I enter systemctl start media...mount command the drive is mounted.

    I can unmount the drive using the Thunar file manager, but when I try to mount it again using Thunar, I get the error window shown above.

    Trying to remount the drive with root privileges works:

    $ sudo mount /media/heiko/wdbook/
    [heiko@woody ~]$ cat /proc/mounts | grep sdf1
    /dev/sdf1 /media/heiko/wdbook fuseblk rw,nosuid,nodev,noexec,relatime,user_id=0,group_id=0,default_permissions,allow_other,blksize=4096 0 0
    

    But trying to mount as regular user doesn't:

    $ mount /media/heiko/wdbook/
    Unprivileged user can not mount NTFS block devices using the external FUSE
    library. Either mount the volume as root, or rebuild NTFS-3G with integrated
    FUSE support and make it setuid root. Please see more information at
    http://tuxera.com/community/ntfs-3g-faq/#unprivileged
    
    1. No /etc/fstab entry, media-heiko-wdbook.mount enabled and running:

    I commented out the /etc/fstab entry and rebooted.

    The drive is automatically mounted under /media/heiko/wdbook:

    $ lsblk
    NAME                    MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
    sdf                       8:80   0   3.6T  0 disk 
    └─sdf1                    8:81   0   3.6T  0 part /media/heiko/wdbook
    

    A look at /proc/mounts shows the mount options used:

    $ cat /proc/mounts | grep sdf1
    /dev/sdf1 /media/heiko/wdbook fuseblk rw,nosuid,nodev,noexec,relatime,user_id=0,group_id=0,default_permissions,allow_other,blksize=4096 0 0
    

    Seems like the only thing taken from media-heiko-wdbook.mount options is the mount point.

    I can unmount the drive using Thunar, and remount it again. This time, however, it mounts under a different folder:

    $ lsblk
    NAME                    MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
    sdf                       8:80   0   3.6T  0 disk 
    └─sdf1                    8:81   0   3.6T  0 part /run/media/heiko/32BB56432280CBF1
    

    The Thunar volume mounter has disregarded both the mount point and the mount options specified in media-heiko-wdbook.mount:

    /proc/mounts reveals the following:

    $ cat /proc/mounts | grep sdf1
    /dev/sdf1 /run/media/heiko/32BB56432280CBF1 fuseblk rw,nosuid,nodev,relatime,user_id=0,group_id=0,default_permissions,allow_other,blksize=4096 0 0
    

    My conclusion and questions

    1. Some higher-level convenience tools don't play nicely with "low-level" configurations, overriding manual systemd and fstab configurations.
    2. The ntfs-3g driver complains about non-root user trying to use external FUSE library. This may explain why I always get user_id=0 and group_id=0. Any ideas?
    3. As far as I can tell, thunar-volman is using udev to automount? If yes, how does that work?
    4. Is there an easy way to let both methods live side by side - that is systemd mounting and udev / thunar-volman mounting - without one interfering with the other?

    In any case, the automount systemd feature doesn't seem to be that straightforward.

  • Note: we were taking a look to this on the Office Hours. Heiko will do another test on Ubuntu, as Manjaro was doing weird for some things. He was able to expose the external disk to a guest, using virsh/quemu.

    Heiko will update this thread with his findings :)

    Luis.

  • coop
    coop Posts: 915

    Just a reminder:

    We make no attempt to make sure anything works on Manjaro or with thunar etc, as we do not try to cover the entire universe of distributions and versions. We test on CentOS and Ubuntu primarily, and OpenSUSE secondarily, and this almost always means Red Hat, Fedora, Debian and SLES provide no real problems.

    The LFCS and LFCE exams are on CentOS and Ubuntu and getting absorbed with the variations for Manjaro, Kali, Mint etc may be fun but are unlikely to get much attention here, at least not mine. The same goes for trying to run complex stuff on RPI etc. Have fun but our help will be (consciously) limited.

  • heiko_s
    heiko_s Posts: 99

    Update

    I've passed through the external hard drive to a Ubuntu 18.04 server VM and edited the /etc/fstab file as follows:

    /dev/disk/by-partlabel/wdbook /mnt ntfs-3g noauto,rw,user,x-systemd.automount,x-systemd.device-timeout=10,x-systemd.idle-timeout=60,nls=utf8,uid=1000,gid=1000,dmask=027,fmask=137,nofail,windows_names,locale=en_US.utf8 0 0

    The drive is mounted as /dev/sda1:

    heiko@worker4:~$ ls -lF /dev/disk/by-partlabel/
    total 0
    lrwxrwxrwx 1 root root 10 Oct  5 19:30 wdbook -> ../../sda1
    

    Then I entered:

    root@worker4:/etc# systemctl daemon-reload
    root@worker4:/etc# systemctl restart local-fs.target
    root@worker4:/etc# lsblk -o name,mountpoint,partlabel,fstype
    NAME                      MOUNTPOINT           PARTLABEL FSTYPE
    ...
    sda                                                      
    └─sda1                                         wdbook    ntfs
    ...
    
    

    The drive is not mounted automatically, just as expected.

    As non-root user I subsequently entered:

    heiko@worker4:~$ ls /mnt
    '$RECYCLE.BIN'  'My File Backup'  'System Volume Information'
    

    and my drive content shows up magically.

    heiko@worker4:~$ lsblk -o name,mountpoint,partlabel,fstype
    NAME                      MOUNTPOINT           PARTLABEL FSTYPE
    ...
    sda                                                      
    └─sda1                    /mnt                 wdbook    ntfs
    ...
    
    heiko@worker4:~$ cat /proc/mounts | grep mnt
    systemd-1 /mnt autofs rw,relatime,fd=41,pgrp=1,timeout=60,minproto=5,maxproto=5,direct,pipe_ino=24929 0 0
    /dev/sda1 /mnt fuseblk rw,nosuid,nodev,noexec,relatime,user_id=0,group_id=0,default_permissions,allow_other,blksize=4096 0 0
    

    After the specified timeout the drive automatically unmounts:

    heiko@worker4:~$ lsblk -o name,mountpoint,partlabel,fstype
    NAME                      MOUNTPOINT           PARTLABEL FSTYPE
    ...
    sda                                                      
    └─sda1                                         wdbook    ntfs
    ...
    

    Systemd automount works as advertised.

  • That's pretty good, @heiko_s !!

    I'm glad it worked as advertised, hehehe. Now it's more interesting because you are passing the device through the host to the guest. Oh, I don't forget it's a NTFS fs using the ntfs-3g driver ;)

    Many regards,
    Luis.

  • heiko_s
    heiko_s Posts: 99

    @coop said:
    Just a reminder:

    We make no attempt to make sure anything works on Manjaro or with thunar etc, as we do not try to cover the entire universe of distributions and versions. We test on CentOS and Ubuntu primarily, and OpenSUSE secondarily, and this almost always means Red Hat, Fedora, Debian and SLES provide no real problems.

    The LFCS and LFCE exams are on CentOS and Ubuntu and getting absorbed with the variations for Manjaro, Kali, Mint etc may be fun but are unlikely to get much attention here, at least not mine. The same goes for trying to run complex stuff on RPI etc. Have fun but our help will be (consciously) limited.

    Thanks for the comment. From now on I will only present issues that pertain to CentOS or Ubuntu. The reason I brought it up was that this was such a basic exercise and something that should just work. Normally I do all my labs and exercises on a Ubuntu or Centos VM, but that harddrive being attached and used by my host OS enticed me to do the exercise on the Manjaro (Arch Linux) host. Lesson learned, as you can see in my previous post.

  • lee42x
    lee42x Posts: 380

    I also re tested this on Ubuntu20-04 my fstab:

    /dev/nvme0n1p3 /home/win10 ntfs noauto,x-systemd.idle-timeout=30,x-
    systemd.automount,rw,relatime,user_id=0,group_id=0,allow_other 0 0

    As expected it worked as documented, one observation to keep in mind with the idle timer, the gnome GUI keeps the mount active and does not unmount automaticly. If a text console login used the automounter works.

  • heiko_s
    heiko_s Posts: 99

    Thanks Lee, that's good to know. Well, it's the same with trying to umount a drive with an open file browser - it won't work.

Categories

Upcoming Training