How to automate rclone-mount without Termux or Magisk?



  • I want to use rclone-mount on Android to mount remote directories from my own server as well as from pCloud. I want the mount points to be established automatically at boot time and to remain mounted full time.

    I have not been able to install Magisk on this device yet. See https://android.stackexchange.com/questions/242322/how-to-install-magisk-without-an-unlocked-bootloader

    I tried the following rclone-mount method with Termux:

    https://android.stackexchange.com/questions/225604/how-to-mount-rclone-on-android

    Unfortunately, that results in permissions errors that I have not been able to solve. I posted a question about it here:

    https://android.stackexchange.com/questions/242247/how-to-use-rclone-mount-with-termuxboot

    That remains unresolved. I have worked on this for many hours, so I could add more info to my other question if needed. However, with the assistance of @alecxs I tried another approach and resolved the permission errors. Now rclone can now successfully mount both pcloud and my own server when I run the commands at the CLI.

    My current question is, how to automate this without Termux:Boot or Magisk? II can't install Magisk and Termux gave me tons of trouble for a couple weeks and I only got this working by ditching Termux. Again, see the above question if you think you know the reason for the Termux problems.)


    For background, and to possibly help others, I am listing all my steps so far since the commands are working and this might prove to be a useful supplement for https://android.stackexchange.com/users/218526/irfan-latif 's great answer that helped me get started with rclone-mount on Android.

    • removed Termux (& verified all files are gone)
    • removed Magisk app (the module was never installed)
    • kept original SuperSU v2.82 (& verified it doesn't have trackers)
    • gave network access permissions to apps running under root in AFWall+
    • used "Simple SSHD" app (from F-Droid) to access device via SSH (from my PC)
    • I have a working rclone.conf file already
    • my env var LD_LIBRARY_PATH is empty and I did not have to modify it with this method
    • my device is Arm64
    • echo $PATH = /sbin:/system/sbin:/system/bin:/system/xbin and I put the binaries into /system/bin EDIT: @alecxs pointed out that these custom binaries should have been installed to /system/xbin.

    First, I put the binaries into the correct directories on my Android device:

    cd /path/to/files/gocryptfs_fusermount-ARMHF_2021-01-13/
    

    NOTE: the fusermount binary comes from XDA / mirfatif: https://forum.xda-developers.com/m/mirfatif.7805402/ https://forum.xda-developers.com/t/fusermount-on-android-rclone-mount.3866652/page-2#post-80237591

    $ scp -P 2222 fusermount 192.168.1.123:~/
    

    On Android, become superuser:

    su
    

    install fusermount binary:

    mount -o rw,remount /system
    mv /system/bin/fusermount /system/bin/fusermount.termux
    cp ./fusermount /system/bin/fusermount
    chmod 775 /system/bin/rclone
    chown system:shell /system/bin/fusermount
    

    check SELinux context and DAC permissions:

    # ls -laZ /system/bin/fusermount
    -rwxrwxr-x 1 system shell u:object_r:system_file:s0 114056 2021-10-25 01:29 /system/bin/fusermount
    

    Install the official rclone binary: NOTE: The rclone binary I'm using is from: https://beta.rclone.org/test/testbuilds-latest/rclone-android-21-armv8a.gz

    I have it on my phone at: /storage/emulated/0/Documents/rclone-android-21-armv8a

    mount -o rw,remount /system
    ls -la /system/bin/rclone
    lrwxrwxrwx 1 system shell 42 2021-10-21 19:55 /system/bin/rclone -> /data/data/com.termux/files/usr/bin/rclone
    rm /system/bin/rclone
    cp /storage/emulated/0/Documents/rclone-android-21-armv8a /system/bin/rclone
    chmod 775 /system/bin/rclone
    chown system:shell /system/bin/rclone
    

    ls -laZ /system/bin/rclone

    -rwxrwxr-x 1 system shell u:object_r:system_file:s0 62257128 2021-10-26 19:45 /system/bin/rclone

    Optional (if needed, restore SELinux context -- I did not need to do this):

    restorecon -F /system/bin/rclone
    restorecon -F /system/bin/fusermount
    

    Finished, so mount readonly again:

    mount -o ro,remount /system
    

    Check the mount namespace:

    # readlink /proc/1/ns/mnt
    mnt:[4026531840]
    # readlink /proc/self/ns/mnt
    mnt:[4026531840]
    

    If they don't match, run the command below. (Mine match)

    nsenter -t 1 -m -- "$0"
    

    Verify the remote is not already mounted:

    # ls -la /mnt/runtime/write/emulated/0/mypcloud/
    

    Set env var for rclone config:

    export RCLONE_CONFIG=/data/data/org.galexander.sshd/files/rclone.conf
    

    Now manually mount remote using rclone:

    rclone -vvv mount mypcloud: /mnt/runtime/write/emulated/0/mypcloud --gid 9997 --dir-perms 0771 --file-perms 0660 --umask=0 --allow-other --cache-dir /storage/emulated/0/.cache --vfs-cache-mode full --vfs-cache-max-age 2h0m0s --vfs-cache-poll-interval 5m0s
    

    Success!

    to umount:

    fusermount -u /mnt/runtime/write/emulated/0/mypcloud
    

    There is really nothing new in the steps above that https://android.stackexchange.com/users/218526/irfan-latif didn't already discuss. However, there are differences. For example, I did not need to set LD_LIBRARY_PATH. Also, rclone emits this warning:

    WARNING: linker: /system/bin/rclone: unsupported flags DT_FLAGS_1=0x8000001
    

    However, that doesn't appear to be a problem. I also show more info about SELinux contexts and a few other things. There are enough differences from the existing answer that my steps might help someone else with their own troubleshooting.


    Now, my main question how can I automate this at boot time without Termux:Boot or Magisk? I guess I could put a script into init somewhere, but I would appreciate an expert answer. I have reviewed a lot of answers about init.rc and executing scripts at boot. They have mostly sent me to solutions that were obsolete by Android 4.x and they also do not deal with the question of how to handle the case of booting up when a network connection is not available.

    EDIT: @alecxs contributed the following idea for booting up when a network connection is not available:

    until dumpsys connectivity | grep -iq state..connected
    do
        sleep 1
    done && rclone  & 
    

    My device is running Android 7.0 (US Samsung Galaxy S8). It is rooted, I have true root, it has SuperSu 2.82, SELinux is permissive, and I can remount /system rw (as shown above). I do have the /system/su.d/ directory.

    Also, related question, where is a good / safe location (path) on my Android device to keep my pcloud token and my private ssh key (for the remote SFTP server)?



  • Create a file named rclone_mount.sh (or any name you choose). My PC is running Linux, so I don't have to worry about line endings.

    Add these commands to it:

    #!/system/bin/sh
    

    export RCLONE_CONFIG=/data/data/org.galexander.sshd/files/rclone.conf;

    rclone mount mypcloud: /mnt/runtime/write/emulated/0/mypcloud --gid 9997 --dir-perms 0771 --file-perms 0660 --umask=0 --allow-other --cache-dir /storage/emulated/0/.cache --vfs-cache-mode full --vfs-cache-max-age 48h0m0s --vfs-cache-poll-interval 2m0s

    Set your rclone options in the above command as desired.

    Send the file from PC to Android device. I use scp like this:

    scp -P 2222 rclone_mount.sh  192.168.1.123:~/
    

    On my Android device, install the file:

    mount -o rw,remount /system
    cp /data/data/org.galexander.sshd/files/rclone_mount.sh /system/su.d/
    chmod 700 /system/su.d/rclone_mount.sh
    chown root:root /system/su.d/rclone_mount.sh
    mount -o ro,remount /system
    

    The hardest part of this is probably finding a location for the script where it will get executed automatically when the phone boots. Since I have SuperSU, the choice was easier: /system/su.d/.

    For all the reasons indicated in the OP, my script was simpler than might have been expected. Since it runs on boot, there is no need to check/unmount the remotes. I don't need to set LD_LIBRARY_PATH. I don't need to become su (as it is run by SuperSU), etc. It's a very simple script.

    Caveat: I only tested it once so far. I'm open to suggestions and I will be using it extensively over the coming days/weeks, so I may find issues that need to be fixed.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2