[rclone] Init.d script - why does this fail?

You can try to use the same environment:

env > /etc/rclone.env
sed -i -r -e "
1a . /etc/rclone.env
" /etc/init.d/mount-onedrive

Or it may require to export environment variables:

...
while read ENV_VAR
do export "${ENV_VAR}"
done < /etc/rclone.env
...

And/or run the commands in a sub-shell:

...
( rclone ... )
...
1 Like

Seems promising but gives the following error:

root@OpenWrt:/etc/init.d# ./mount-onedrive enabe
/etc/rc.common: /etc/rclone.env: line 2: 62270: not found
/etc/rc.common: /etc/rclone.env: line 7: u@h:: not found
/etc/rc.common: /etc/rclone.env: line 14: 62270: not found

Should it be 'import' or 'source' or something like that?

Is there an easier way?

I'm pretty sure you don't really need all environment variables.
Perhaps there's only a couple is actually necessary.

Yay! This works!

So how should I fix all this.

My script now looks like this:

root@OpenWrt:/etc/init.d# cat mount-onedrive
#!/bin/sh /etc/rc.common
# Copyright (C) 2007 OpenWrt.org

while read ENV_VAR
do export "${ENV_VAR}"
done < /etc/rclone.env

exec &> /tmp/mount-onedrive.log
set -x -v
env

START=97
STOP=5

start() {
        rclone --config /etc/rclone/rclone.conf mkdir /tmp/OneDrive
        rclone --config /etc/rclone/rclone.conf mount "OneDrive:/Scanned Documents/" /tmp/OneDrive/ --vfs-cache-mode full --umask 000 --allow-other --daemon
}

stop() {
        fusermount -zu /tmp/OneDrive
}
1 Like

Try excluding the variables one by one.
This way you can isolate the necessary ones.

root@OpenWrt:/etc/init.d# cat /etc/rclone.env
USER=root
SSH_CLIENT=192.168.1.168 62270 22
SHLVL=1
HOME=/root
OLDPWD=/root
SSH_TTY=/dev/pts/0
PS1=\[\e]0;\u@\h: \w\a\]\u@\h:\w\$
ENV=/etc/shinit
LOGNAME=root
TERM=xterm-256color
PATH=/usr/sbin:/usr/bin:/sbin:/bin
SHELL=/bin/ash
PWD=/etc/init.d
SSH_CONNECTION=192.168.1.168 62270 192.168.1.1 22
1 Like

The SSH* and PS* ones are most likely irrelevant, but the rest require testing.

So it turns out that it just needed:

export PATH=/usr/sbin:/usr/bin:/sbin:/bin

I think this may be because the rclone binary is located in /usr/bin/rclone? Although I do not understand why just launching it with full path /usr/bin/rclone would not work then? Or could there be another explanation why this export is needed?

So my script is now:

#!/bin/sh /etc/rc.common
# Copyright (C) 2007 OpenWrt.org

export PATH=/usr/sbin:/usr/bin:/sbin:/bin

exec &> /tmp/mount-onedrive.log

START=97
STOP=5

start() {
        rclone --config /etc/rclone/rclone.conf mkdir /tmp/OneDrive
        rclone --config /etc/rclone/rclone.conf mount "OneDrive:/Scanned Documents/" /tmp/OneDrive/ --vfs-cache-mode ful}

stop() {
        fusermount -zu /tmp/OneDrive
}

Does this look safe? I read that if init.d scripts fail it can be dangerous in certain circumstances:

:!: WARNING :!: OpenWrt initscripts will be run while building OpenWrt images (when installing packages in what will become a ROM image) in the host system (right now, for actions “enable ” and “disable ”). They must not fail, or have undesired side-effects in that situation. When being run by the build system, environment variable ${IPKG_INSTROOT} will be set to the working directory being used. On the “target system”, that environment variable will be empty/unset. Refer to “/lib/functions.sh” and also to “/etc/rc.common” in package “base-files” for the nasty details.

Can this fail in such a way?

2 Likes

In any case, thank you very much @vgaetera for your help here!

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.