1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How can I modify my $PATH

Discussion in 'Tomato Firmware' started by darksky, Jul 7, 2013.

  1. darksky

    darksky Networkin' Nut Member

    How can I modify my root user's $PATH on tomatousb? For example, I'd like to add /opt/usr/local/bin to the system PATH. If I do the obvious thing, redefine it in for example /root/.bashrc this file is transient and only lives while I am logged in.

    Thanks!
     
  2. Planiwa

    Planiwa LI Guru Member

    That's not the case. A file such as ~/.profile persists until the computer is rebooted.

    If you are a reboot-prone user you deserve what you get. :)

    Code:
    Boot   29d3h ago 
    INIT   5 Jul 15:11 
    WAN up 5 Jul 15:11 
    FW  up 5 Jul 15:11 
    
     
  3. koitsu

    koitsu Network Guru Member

    One can use nvram setfile2nvram to make a file permanently written (and restored) to/from NVRAM. Ex.

    nvram setfile2nvram /root/.profile
    nvram commit

    Be aware, however: many people (especially on this forum) tend to muck up their .profile which causes other anomalies, causing them to show up and complain about broken libraries, broken binaries, etc. when it's their own environment/setup which is causing the problem. There's even an example recently of someone logging in, running bash, and then reporting "all sorts of problems". So consider yourself warned. :)
     
  4. Planiwa

    Planiwa LI Guru Member

    A less sophisticated option is to keep all your config files, etc. on a persistent writable device (same place where you keep /opt, for example), and to copy them over with SCRIPTS at the appropriate time, e.g. INIT, etc.

    I used to have all my tools on a host on the other side of the globe, and copy them over automagically with wget, if necessary, at WAN-UP time.
     
  5. darksky

    darksky Networkin' Nut Member


    Excellent idea, thanks. Care to post your .profile and related files?
     
  6. koitsu

    koitsu Network Guru Member

    Using Scripts -> Init et al for this is not the way to go -- sometimes that code can get run before USB devices are actually enumerated/mounted, which means commands there will fail.

    The proper solution is to make a shell script called mount.autorun and place it onto the filesystem backed by the USB device. Make sure to use a filesystem like ext2 or ext3, not NTFS or FAT/FAT32, because file permissions matter (file needs to be executable, e.g. 0755).

    For example, what I use auto-creates the /opt bind from /tmp/mnt/usbflash (the name "usbflash" comes from the ext3 label I chose) and properly deals with unmounting situations (such as when clicking "Save" in the TomatoUSB GUI, otherwise you'll end up with multiple /opt binds which can cause all sorts of problems):

    Code:
    #!/bin/sh
    #
    # automount script for USB flash drives
    #
    # USB flash drives:
    #
    # The drive will be auto-mounted under /tmp/mnt/{drivelabel}.
    # The drivelabel is generated during mkfs.ext3:
    # http://www.linksysinfo.org/index.php?threads/creating-ext3-swap-partitions-on-tomato.48655/
    #
    # To format a drive:
    #
    # mkfs.ext3 -L usbflash /dev/sda1
    # {refresh/save in TomatoUSB GUI}
    # tune2fs -c 0 /dev/sda1
    # {manually unmount/remount in TomatoUSB GUI}
    #
    
    if /bin/grep -q /opt /proc/mounts
    then
      /bin/umount /opt
    fi
    
    /bin/mount -o bindable /tmp/mnt/usbflash /opt
    
    I use this exclusively and with no commands under USB and NAS -> Run after/before mounting/unmounting.

    It should become very obvious to you how to take this and do what you need. Good luck, and keep in mind what I said earlier about /root/.profile and the serious implications it has.
     
  7. darksky

    darksky Networkin' Nut Member

    @koitsu - Thanks... I use something very similar (see below). Can you post your ~/.profile to this thread?

    Run after mounting:
    Code:
    #!/bin/sh
    [[ -d /tmp/mnt/router ]] && \
      mount -o noatime -t ext3 -obind /tmp/mnt/router/Optware /opt
    sleep 1s
    [[ -f /opt/skel/inputrc ]] && cp /opt/skel/inputrc /tmp/home/root/.profile
    [[ -f /opt/skel/ash_history ]] && cp /opt/skel/ash_history /tmp/home/root/.ash_history
    [[ -x /opt/etc/init.d/S80lighttpd ]] && /opt/etc/init.d/S80lighttpd start
    sleep 2s
    [[ -x /opt/etc/init.d/S57rsyncd ]] && /opt/etc/init.d/S57rsyncd start
    
    Run before unmounting:
    Code:
    #!/bin/sh
    cp /tmp/home/root/.ash_history /opt/skel/ash_history
    [[ -x /opt/etc/init.d/S80lighttpd ]] && /opt/etc/init.d/S80lighttpd stop
    [[ -x /opt/etc/init.d/S57rsyncd ]] && /opt/etc/init.d/S57rsyncd stop
    umount /opt
    
     
  8. koitsu

    koitsu Network Guru Member

    I have no /root/.profile, for what should be obvious by now (stated it twice). :)
     
  9. Planiwa

    Planiwa LI Guru Member

    Are you able to give a very, very brief, simple, clear, example of this "obvious" danger?
     
  10. koitsu

    koitsu Network Guru Member

    Most of them have been encountered/discussed on the linksysinfo.org forums or the older tomatousb.org forums. The most recent one was from a few days ago:

    http://www.linksysinfo.org/index.php?threads/fstab-nvram-issue-shibby-tomato-110.68728/#post-230265

    You can find others using the magic of a search engine.

    People screwing with LD_LIBRARY_PATH (shown in that thread as well) is also very common, which is a huge no-no unless you're familiar with the repercussions.

    http://tomatousb.org/forum/t-303534/php-or-tomato-bug
    http://www.linksysinfo.org/index.php?threads/shibby-fw-how-can-i-mount-my-usb-drive-to-opt.56213/ (warning: long, but you will see my early speculation turns out to be true)
    http://www.linksysinfo.org/index.ph...rom-the-optware-distribution-on-tomato.33411/
    http://tomatousb.org/forum/t-275444

    ...I've discussed that as well, further advocating use of Entware (not Optware) which builds its binaries with gcc -rpath which deals with the situation cleanly/correctly so there's no conflicting libraries. I explain that here:

    http://www.linksysinfo.org/index.php?threads/my-utilities-web-site-revived.31987/page-5#post-186625

    And one recent user insisted he "need to be able to copy libraries into /lib", furthering my point (that people who often don't understand *IX or have familiarity with that level end up causing a mess):

    http://www.linksysinfo.org/index.php?threads/how-to-adding-some-extra-files-into-tomato-lib.68732/

    Finally, the continual focus on .profile often indicates the user lacks understanding of shells and dotfiles; they've become accustomed to a specific method on a specific OS (Linux) and have not really gotten familiar with the ramifications of such. For example, I strongly advocate keeping an entirely clean environment for one's router until one executes a sub-shell of sorts (ex. bash in most cases), i.e. telnet router, log in, then run "bash" -- and thankfully bash has .bash_profile and .bashrc which it can/will read, while using .profile would affect the stock Busybox shell (/bin/sh a.k.a. /bin/ash (not a typo)) and could cause you issues. It becomes worse when you consider that a good portion of the underlying code in this firmware uses system() or similar calls which spawns a shell to execute commands -- which means .profile gets read there too, and can cause you unexpected problems even during things like clicking "Save" in the router GUI (bet you didn't think of that one did you!) depending what all you've done.

    Basically what I'm trying to say in my last paragraph is to segregate these "changes" you're making into a specific environment that you know for a fact you can get out of in the case you run into problems (example: "hmm, all these commands are acting weird, but when I exit bash everything works correctly..."). The best way to do that is to install Entware + bash and use .bash_profile and .bashrc. Put all your necessary "stuff" in .bashrc, and your .bash_profile can have something like this in it (but I urge everyone to read about the difference/understand the difference between a login shell, and interactive shells vs. non-interactive shells):

    Code:
    # vim:ft=sh
     
    if [ -f "${HOME}/.bashrc" ]; then
      source "${HOME}/.bashrc"
    fi
    
    Then leave all the "fun stuff" up to .bashrc, for example:

    Code:
    # vim:ft=sh
     
    umask 077
     
    # see strftime manpage
    export HISTTIMEFORMAT="%T  "
     
    set bell-style none
    export LESS="-MRXn"
    export LESSHISTFILE="-"
    export PAGER="less"
    export PS1='\[\e[0;0;1;30m\](\[\e[1;37m\]\t \[\e[1;32m\]\u@\h\[\e[1;30m\]) \[\e[1;37;44m\]\w\[\e[0;0m\] \$ '
     
    alias more="less"
    alias More="less"
    alias mroe="less"
    alias moer="less"
    alias Less="less"
     
    if [ -x "/opt/bin/vim" ]; then
      export EDITOR="vim"
      export VISUAL="vim"
      alias vi="vim"
    fi
    
    TL;DR -- Leave the stock/base environment alone at all costs, instead segregate out a separate environment for yourself using intelligent use of Entware and proper dotfiles that don't get honoured by the base environment.
     

Share This Page